along with simavr. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <stdlib.h>
+#include <stdarg.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "sim_avr.h"
#define AVR_KIND_DECL
#include "sim_core_decl.h"
+static void std_logger(avr_t * avr, const int level, const char * format, ...);
+logger_t global_logger = std_logger;
+
int avr_init(avr_t * avr)
{
avr->flash = malloc(avr->flashend + 1);
avr->trace_data = calloc(1, sizeof(struct avr_trace_data_t));
#endif
- printf("%s init\n", avr->mmcu);
+ AVR_LOG(avr, LOG_TRACE, "%s init\n", avr->mmcu);
// cpu is in limbo before init is finished.
avr->state = cpu_Limbo;
void avr_reset(avr_t * avr)
{
- printf("%s reset\n", avr->mmcu);
+ AVR_LOG(avr, LOG_TRACE, "%s reset\n", avr->mmcu);
memset(avr->data, 0x0, avr->ramend + 1);
_avr_sp_set(avr, avr->ramend);
void avr_sadly_crashed(avr_t *avr, uint8_t signal)
{
- printf("%s\n", __FUNCTION__);
+ AVR_LOG(avr, LOG_ERROR, "%s\n", __FUNCTION__);
avr->state = cpu_Stopped;
if (avr->gdb_port) {
// enable gdb server, and wait
static void _avr_io_command_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
{
- printf("%s %02x\n", __FUNCTION__, v);
+ AVR_LOG(avr, LOG_TRACE, "%s %02x\n", __FUNCTION__, v);
switch (v) {
case SIMAVR_CMD_VCD_START_TRACE:
if (avr->vcd)
avr_irq_t * src = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_OUTPUT);
avr_irq_t * dst = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_INPUT);
if (src && dst) {
- printf("%s activating uart local echo IRQ src %p dst %p\n", __FUNCTION__, src, dst);
+ AVR_LOG(avr, LOG_TRACE, "%s activating uart local echo IRQ src %p dst %p\n",
+ __FUNCTION__, src, dst);
avr_connect_irq(src, dst);
}
} break;
if (v == '\r' && buf) {
buf[len] = 0;
- printf("O:" "%s" "" "\n", buf);
+ AVR_LOG(avr, LOG_TRACE, "O:" "%s" "" "\n", buf);
fflush(stdout);
len = 0;
return;
void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, avr_flashaddr_t address)
{
if (size > avr->flashend+1) {
- fprintf(stderr, "avr_loadcode(): Attempted to load code of size %d but flash size is only %d.\n",
- size, avr->flashend+1);
+ AVR_LOG(avr, LOG_ERROR, "avr_loadcode(): Attempted to load code of size %d but flash size is only %d.\n",
+ size, avr->flashend + 1);
abort();
}
memcpy(avr->flash + address, code, size);
if (avr->state == cpu_Sleeping) {
if (!avr->sreg[S_I]) {
if (avr->log)
- printf("simavr: sleeping with interrupts off, quitting gracefully\n");
+ AVR_LOG(avr, LOG_TRACE, "simavr: sleeping with interrupts off, quitting gracefully\n");
avr->state = cpu_Done;
return;
}
if (avr->state == cpu_Sleeping) {
if (!avr->sreg[S_I]) {
if (avr->log)
- printf("simavr: sleeping with interrupts off, quitting gracefully\n");
+ AVR_LOG(avr, LOG_TRACE, "simavr: sleeping with interrupts off, quitting gracefully\n");
avr->state = cpu_Done;
return;
}
}
}
if (!maker) {
- fprintf(stderr, "%s: AVR '%s' not known\n", __FUNCTION__, name);
+ AVR_LOG(((avr_t*)0), LOG_ERROR, "%s: AVR '%s' not known\n", __FUNCTION__, name);
return NULL;
}
avr_t * avr = maker->make();
- printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);
- return avr;
+ AVR_LOG(avr, LOG_TRACE, "Starting %s - flashend %04x ramend %04x e2end %04x\n",
+ avr->mmcu, avr->flashend, avr->ramend, avr->e2end);
+ return avr;
+}
+
+static void std_logger(avr_t* avr, const int level, const char * format, ...)
+{
+ if (!avr || avr->log >= level) {
+ va_list args;
+ va_start(args, format);
+ vfprintf((level > LOG_ERROR) ? stdout : stderr , format, args);
+ va_end(args);
+ }
}