From 5cb6f3488e37ac5f695078cebf8447e370fdd1f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kujawa?= Date: Thu, 22 Mar 2018 15:08:51 +0100 Subject: [PATCH] Further improvements and refactoring to logging. --- src/bus.c | 14 ++++++++++---- src/debug.c | 3 ++- src/instruction.c | 15 +++++++++------ src/log.c | 8 +++++++- src/log.h | 1 + src/rk65c02.c | 4 ++-- test/test_debug.c | 4 ++++ 7 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/bus.c b/src/bus.c index c3bf3ef..95e8f0b 100644 --- a/src/bus.c +++ b/src/bus.c @@ -2,15 +2,18 @@ #include #include #include +#include #include #include #include +#include #include #include #include "bus.h" +#include "log.h" #include "device_ram.h" @@ -68,7 +71,7 @@ bus_access_device(bus_t *t, uint16_t addr, device_t **d, uint16_t *off) } if (*d == NULL) { - fprintf(stderr, "Hitting unmapped bus space @ %x!", addr); + rk6502_log(LOG_WARN, "Hitting unmapped bus space @ %x!", addr); return; } @@ -90,7 +93,8 @@ bus_read_1(bus_t *t, uint16_t addr) val = d->read_1(d, off); if (t->access_debug) - printf("bus READ @ %x (off %x) value %x\n", addr, off, val); + rk6502_log(LOG_DEBUG, "bus READ @ %x (off %x) value %x\n", + addr, off, val); return val; } @@ -104,7 +108,8 @@ bus_write_1(bus_t *t, uint16_t addr, uint8_t val) bus_access_device(t, addr, &d, &off); if (t->access_debug) - printf("bus WRITE @ %x (off %x) value %x\n", addr, off, val); + rk6502_log(LOG_DEBUG, "bus WRITE @ %x (off %x) value %x\n", + addr, off, val); d->write_1(d, off, val); } @@ -158,7 +163,8 @@ bus_load_file(bus_t *t, uint16_t addr, const char *filename) fd = open(filename, O_RDONLY); if (fd == -1) { - perror("Problem while trying to open file"); + rk6502_log(LOG_ERROR, "Problem while trying to open file: %s", + strerror(errno)); return false; } diff --git a/src/debug.c b/src/debug.c index 90cdfdc..950c519 100644 --- a/src/debug.c +++ b/src/debug.c @@ -4,6 +4,7 @@ #include "rk65c02.h" #include "instruction.h" +#include "log.h" #include "debug.h" void @@ -26,7 +27,7 @@ debug_trace_print_all(rk65c02emu_t *e) i.op1 = tr->op1; i.op2 = tr->op2; - printf("TRACE %X:\t", tr->address); + rk6502_log(LOG_TRACE, " %X:\t", tr->address); instruction_print(&i); printf("\t"); rk65c02_dump_regs(tr->regs); diff --git a/src/instruction.c b/src/instruction.c index 19af558..0f6927b 100644 --- a/src/instruction.c +++ b/src/instruction.c @@ -9,6 +9,7 @@ #include "bus.h" #include "rk65c02.h" #include "65c02isa.h" +#include "log.h" #include "instruction.h" instruction_t @@ -172,14 +173,16 @@ assemble_single_buf(uint8_t **buf, uint8_t *bsize, const char *mnemonic, address } if (!found) { - fprintf(stderr, "Couldn't find opcode for mnemonic %s mode %x\n", mnemonic, mode); + rk6502_log(LOG_ERROR, + "Couldn't find opcode for mnemonic %s mode %x.", + mnemonic, mode); return false; } *bsize = id.size; *buf = malloc(id.size); if(*buf == NULL) { - fprintf(stderr, "Error allocating assembly buffer\n"); + rk6502_log(LOG_ERROR, "Error allocating assembly buffer."); return false; } @@ -301,8 +304,8 @@ instruction_data_write_1(rk65c02emu_t *e, instrdef_t *id, instruction_t *i, uint * PC which is handled within emulation of a given opcode. */ default: - printf("unhandled addressing mode for opcode %x\n", - i->opcode); + rk6502_log(LOG_ERROR, + "unhandled addressing mode for opcode %x\n", i->opcode); break; } } @@ -368,8 +371,8 @@ instruction_data_read_1(rk65c02emu_t *e, instrdef_t *id, instruction_t *i) * PC which is handled within emulation of a given opcode. */ default: - printf("unhandled addressing mode for opcode %x\n", - i->opcode); + rk6502_log(LOG_ERROR, + "unhandled addressing mode for opcode %x\n", i->opcode); break; } diff --git a/src/log.c b/src/log.c index 15dde25..650b738 100644 --- a/src/log.c +++ b/src/log.c @@ -1,6 +1,8 @@ #include #include +#include + #include "log.h" static const char *level_str[] = { @@ -22,11 +24,15 @@ void rk6502_loglevel_set(uint8_t l) void rk6502_log(uint8_t l, const char* fmt, ...) { va_list args; + struct timeval t; + + gettimeofday(&t, NULL); if (l > level) return; - fprintf(stderr, "%s:\t", level_str[l]); + fprintf(stderr, "%ld %s:\t", (t.tv_sec * 1000000 + t.tv_usec), + level_str[l]); va_start(args, fmt); vfprintf(stderr, fmt, args); diff --git a/src/log.h b/src/log.h index 99c0eac..738ff8b 100644 --- a/src/log.h +++ b/src/log.h @@ -3,6 +3,7 @@ #define LOG_TRACE 5 #define LOG_DEBUG 4 #define LOG_INFO 3 +#define LOG_WARN 4 #define LOG_ERROR 2 #define LOG_CRIT 1 #define LOG_NOTHING 0 /* At 0 nothing will get logged, can be set as diff --git a/src/rk65c02.c b/src/rk65c02.c index 8b95f06..04b6af6 100644 --- a/src/rk65c02.c +++ b/src/rk65c02.c @@ -119,8 +119,8 @@ rk65c02_exec(rk65c02emu_t *e) if (!instruction_modify_pc(&id)) program_counter_increment(e, &id); } else { - printf("unimplemented opcode %X @ %X\n", i.opcode, - e->regs.PC); + rk6502_log(LOG_ERROR, "unimplemented opcode %X @ %X\n", + i.opcode, e->regs.PC); e->state = STOPPED; e->stopreason = EMUERROR; } diff --git a/test/test_debug.c b/test/test_debug.c index f3231e2..fde4017 100644 --- a/test/test_debug.c +++ b/test/test_debug.c @@ -12,6 +12,7 @@ #include "instruction.h" #include "debug.h" #include "utils.h" +#include "log.h" ATF_TC_WITHOUT_HEAD(breakpoint); ATF_TC_BODY(breakpoint, tc) @@ -54,10 +55,13 @@ ATF_TC_BODY(trace, tc) trace_t *tr; int i; + rk6502_loglevel_set(LOG_TRACE); + b = bus_init_with_default_devs(); a = assemble_init(&b, ROM_LOAD_ADDR); e = rk65c02_init(&b); + e.regs.PC = ROM_LOAD_ADDR; debug_trace_set(&e, true);