From 5fddf2c5dcfdf926440f07994a5cb20db441738f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kujawa?= Date: Fri, 23 Mar 2018 13:37:07 +0100 Subject: [PATCH] More refactoring and improvements to logging! --- src/bus.c | 11 ++++--- src/debug.c | 13 ++++++--- src/instruction.c | 65 ++++++++++++++++++++++++++++------------- src/instruction.h | 1 + src/log.c | 4 +-- src/log.h | 4 +-- src/rk65c02.c | 73 +++++++++++++++++++++-------------------------- src/rk65c02.h | 3 +- test/test_debug.c | 2 +- 9 files changed, 102 insertions(+), 74 deletions(-) diff --git a/src/bus.c b/src/bus.c index 95e8f0b..6a508d2 100644 --- a/src/bus.c +++ b/src/bus.c @@ -33,6 +33,9 @@ bus_device_add(bus_t *b, device_t *d, uint16_t addr) dm->addr = addr; LL_APPEND((b->dm_head), dm); + + rk65c02_log(LOG_INFO, "Bus mapping added: %x device %s size %x.", + addr, d->name, d->size); } void @@ -71,7 +74,7 @@ bus_access_device(bus_t *t, uint16_t addr, device_t **d, uint16_t *off) } if (*d == NULL) { - rk6502_log(LOG_WARN, "Hitting unmapped bus space @ %x!", addr); + rk65c02_log(LOG_WARN, "Hitting unmapped bus space @ %x!", addr); return; } @@ -93,7 +96,7 @@ bus_read_1(bus_t *t, uint16_t addr) val = d->read_1(d, off); if (t->access_debug) - rk6502_log(LOG_DEBUG, "bus READ @ %x (off %x) value %x\n", + rk65c02_log(LOG_DEBUG, "bus READ @ %x (off %x) value %x\n", addr, off, val); return val; @@ -108,7 +111,7 @@ bus_write_1(bus_t *t, uint16_t addr, uint8_t val) bus_access_device(t, addr, &d, &off); if (t->access_debug) - rk6502_log(LOG_DEBUG, "bus WRITE @ %x (off %x) value %x\n", + rk65c02_log(LOG_DEBUG, "bus WRITE @ %x (off %x) value %x\n", addr, off, val); d->write_1(d, off, val); @@ -163,7 +166,7 @@ bus_load_file(bus_t *t, uint16_t addr, const char *filename) fd = open(filename, O_RDONLY); if (fd == -1) { - rk6502_log(LOG_ERROR, "Problem while trying to open file: %s", + rk65c02_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 950c519..81371c3 100644 --- a/src/debug.c +++ b/src/debug.c @@ -18,6 +18,8 @@ debug_trace_print_all(rk65c02emu_t *e) { trace_t *tr; instruction_t i; + char *instrstr; + char *regsstr; if (e->trace_head == NULL) return; @@ -26,11 +28,14 @@ debug_trace_print_all(rk65c02emu_t *e) i.opcode = tr->opcode; i.op1 = tr->op1; i.op2 = tr->op2; + instrstr = instruction_string_get(&i); + regsstr = rk65c02_regs_string_get(tr->regs); - rk6502_log(LOG_TRACE, " %X:\t", tr->address); - instruction_print(&i); - printf("\t"); - rk65c02_dump_regs(tr->regs); + rk65c02_log(LOG_TRACE, "%X: %s\t%s", tr->address, instrstr, + regsstr); + + free(instrstr); + free(regsstr); } } diff --git a/src/instruction.c b/src/instruction.c index 0f6927b..5dab91f 100644 --- a/src/instruction.c +++ b/src/instruction.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -55,59 +56,83 @@ instruction_fetch(bus_t *b, uint16_t addr) void instruction_print(instruction_t *i) { + char *str; + + str = instruction_string_get(i); + + printf("%s", str); + + free(str); +} + +char * +instruction_string_get(instruction_t *i) +{ +#define INSTR_STR_LEN 16 instrdef_t id; + char *str; + + str = malloc(INSTR_STR_LEN); + if (str == NULL) { + rk65c02_log(LOG_CRIT, "Error allocating memory for buffer: %s.", + strerror(errno)); + return NULL; + } + memset(str, 0, INSTR_STR_LEN); id = instruction_decode(i->opcode); switch (id.mode) { case IMPLIED: - printf("%s", id.mnemonic); + snprintf(str, INSTR_STR_LEN, "%s", id.mnemonic); break; case ACCUMULATOR: - printf("%s A", id.mnemonic); + snprintf(str, INSTR_STR_LEN, "%s A", id.mnemonic); break; case IMMEDIATE: - printf("%s #%#02x", id.mnemonic, i->op1); + snprintf(str, INSTR_STR_LEN, "%s #%#02x", id.mnemonic, i->op1); break; case ZP: - printf("%s %#02x", id.mnemonic, i->op1); + snprintf(str, INSTR_STR_LEN, "%s %#02x", id.mnemonic, i->op1); break; case ZPX: - printf("%s %#02x,X", id.mnemonic, i->op1); + snprintf(str, INSTR_STR_LEN, "%s %#02x,X", id.mnemonic, i->op1); break; case ZPY: - printf("%s %#02x,Y", id.mnemonic, i->op1); + snprintf(str, INSTR_STR_LEN, "%s %#02x,Y", id.mnemonic, i->op1); break; case IZP: - printf("%s (%#02x)", id.mnemonic, i->op1); + snprintf(str, INSTR_STR_LEN, "%s (%#02x)", id.mnemonic, i->op1); break; case IZPX: - printf("%s (%#02x,X)", id.mnemonic, i->op1); + snprintf(str, INSTR_STR_LEN, "%s (%#02x,X)", id.mnemonic, i->op1); break; case IZPY: - printf("%s (%#02x),Y", id.mnemonic, i->op1); + snprintf(str, INSTR_STR_LEN, "%s (%#02x),Y", id.mnemonic, i->op1); break; case ZPR: - printf("%s %#02x,%#02x", id.mnemonic, i->op1, i->op2); + snprintf(str, INSTR_STR_LEN, "%s %#02x,%#02x", id.mnemonic, i->op1, i->op2); break; case ABSOLUTE: - printf("%s %#02x%02x", id.mnemonic, i->op2, i->op1); + snprintf(str, INSTR_STR_LEN, "%s %#02x%02x", id.mnemonic, i->op2, i->op1); break; case ABSOLUTEX: - printf("%s %#02x%02x,X", id.mnemonic, i->op2, i->op1); + snprintf(str, INSTR_STR_LEN, "%s %#02x%02x,X", id.mnemonic, i->op2, i->op1); break; case ABSOLUTEY: - printf("%s %#02x%02x,Y", id.mnemonic, i->op2, i->op1); + snprintf(str, INSTR_STR_LEN, "%s %#02x%02x,Y", id.mnemonic, i->op2, i->op1); break; case IABSOLUTE: - printf("%s (%#02x%02x)", id.mnemonic, i->op2, i->op1); + snprintf(str, INSTR_STR_LEN, "%s (%#02x%02x)", id.mnemonic, i->op2, i->op1); break; case IABSOLUTEX: - printf("%s (%#02x%02x,X)", id.mnemonic, i->op2, i->op1); + snprintf(str, INSTR_STR_LEN, "%s (%#02x%02x,X)", id.mnemonic, i->op2, i->op1); break; case RELATIVE: - printf("%s %#02x", id.mnemonic, i->op1); + snprintf(str, INSTR_STR_LEN, "%s %#02x", id.mnemonic, i->op1); break; } + + return str; } assembler_t @@ -173,7 +198,7 @@ assemble_single_buf(uint8_t **buf, uint8_t *bsize, const char *mnemonic, address } if (!found) { - rk6502_log(LOG_ERROR, + rk65c02_log(LOG_ERROR, "Couldn't find opcode for mnemonic %s mode %x.", mnemonic, mode); return false; @@ -182,7 +207,7 @@ assemble_single_buf(uint8_t **buf, uint8_t *bsize, const char *mnemonic, address *bsize = id.size; *buf = malloc(id.size); if(*buf == NULL) { - rk6502_log(LOG_ERROR, "Error allocating assembly buffer."); + rk65c02_log(LOG_ERROR, "Error allocating assembly buffer."); return false; } @@ -304,7 +329,7 @@ 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: - rk6502_log(LOG_ERROR, + rk65c02_log(LOG_ERROR, "unhandled addressing mode for opcode %x\n", i->opcode); break; } @@ -371,7 +396,7 @@ instruction_data_read_1(rk65c02emu_t *e, instrdef_t *id, instruction_t *i) * PC which is handled within emulation of a given opcode. */ default: - rk6502_log(LOG_ERROR, + rk65c02_log(LOG_ERROR, "unhandled addressing mode for opcode %x\n", i->opcode); break; } diff --git a/src/instruction.h b/src/instruction.h index 0fd11df..98595f0 100644 --- a/src/instruction.h +++ b/src/instruction.h @@ -51,6 +51,7 @@ typedef struct assembler assembler_t; instruction_t instruction_fetch(bus_t *, uint16_t); instrdef_t instruction_decode(uint8_t); void instruction_print(instruction_t *); +char * instruction_string_get(instruction_t *); void disassemble(bus_t *, uint16_t); uint8_t instruction_data_read_1(rk65c02emu_t *, instrdef_t *, instruction_t *); void instruction_data_write_1(rk65c02emu_t *, instrdef_t *, instruction_t *, uint8_t); diff --git a/src/log.c b/src/log.c index 650b738..3ad117f 100644 --- a/src/log.c +++ b/src/log.c @@ -16,12 +16,12 @@ static const char *level_str[] = { static uint8_t level = LOG_INFO; -void rk6502_loglevel_set(uint8_t l) +void rk65c02_loglevel_set(uint8_t l) { level = l; } -void rk6502_log(uint8_t l, const char* fmt, ...) +void rk65c02_log(uint8_t l, const char* fmt, ...) { va_list args; struct timeval t; diff --git a/src/log.h b/src/log.h index 738ff8b..3b50e7f 100644 --- a/src/log.h +++ b/src/log.h @@ -10,6 +10,6 @@ current level, but not when creating new log messages. */ -void rk6502_loglevel_set(uint8_t); -void rk6502_log(uint8_t, const char *, ...); +void rk65c02_loglevel_set(uint8_t); +void rk65c02_log(uint8_t, const char *, ...); diff --git a/src/rk65c02.c b/src/rk65c02.c index 04b6af6..92e5e48 100644 --- a/src/rk65c02.c +++ b/src/rk65c02.c @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -35,7 +36,7 @@ rk65c02_init(bus_t *b) e.trace_head = NULL; e.runtime_disassembly = false; - rk6502_log(LOG_DEBUG, "Initialized new emulator."); + rk65c02_log(LOG_DEBUG, "Initialized new emulator."); return e; } @@ -119,7 +120,7 @@ rk65c02_exec(rk65c02emu_t *e) if (!instruction_modify_pc(&id)) program_counter_increment(e, &id); } else { - rk6502_log(LOG_ERROR, "unimplemented opcode %X @ %X\n", + rk65c02_log(LOG_ERROR, "unimplemented opcode %X @ %X\n", i.opcode, e->regs.PC); e->state = STOPPED; e->stopreason = EMUERROR; @@ -188,50 +189,42 @@ rk65c02_dump_stack(rk65c02emu_t *e, uint8_t n) void rk65c02_dump_regs(reg_state_t regs) { - printf("A: %X X: %X Y: %X PC: %X SP: %X P: ", - regs.A, regs.X, regs.Y, regs.PC, regs.SP); + char *str; - if (regs.P & P_NEGATIVE) - printf("N"); - else - printf("-"); + str = rk65c02_regs_string_get(regs); - if (regs.P & P_SIGN_OVERFLOW) - printf("V"); - else - printf("-"); + printf ("%s", str); - if (regs.P & P_UNDEFINED) - printf("1"); - else - printf("-"); + free(str); +} - if (regs.P & P_BREAK) - printf("B"); - else - printf("-"); +char * +rk65c02_regs_string_get(reg_state_t regs) +{ +#define REGS_STR_LEN 50 + char *str; - if (regs.P & P_DECIMAL) - printf("D"); - else - printf("-"); + /* XXX: string allocation to a separate utility function? */ + str = malloc(REGS_STR_LEN); + if (str == NULL) { + rk65c02_log(LOG_CRIT, "Error allocating memory for buffer: %s.", + strerror(errno)); + return NULL; + } + memset(str, 0, REGS_STR_LEN); - if (regs.P & P_IRQ_DISABLE) - printf("I"); - else - printf("-"); + snprintf(str, REGS_STR_LEN, "A: %X X: %X Y: %X PC: %X SP: %X P: %c%c%c%c%c%c%c%c", + regs.A, regs.X, regs.Y, regs.PC, regs.SP, + (regs.P & P_NEGATIVE) ? 'N' : '-', + (regs.P & P_SIGN_OVERFLOW) ? 'V' : '-', + (regs.P & P_UNDEFINED) ? '1' : '-', + (regs.P & P_BREAK) ? 'B' : '-', + (regs.P & P_DECIMAL) ? 'D' : '-', + (regs.P & P_IRQ_DISABLE) ? 'I' : '-', + (regs.P & P_ZERO) ? 'Z' : '-', + (regs.P & P_CARRY) ? 'C' : '-'); - if (regs.P & P_ZERO) - printf("Z"); - else - printf("-"); - - if (regs.P & P_CARRY) - printf("C"); - else - printf("-"); - - printf("\n"); + return str; } /* int @@ -252,7 +245,7 @@ main(void) bus_write_1(&b, 8, 0x0); bus_write_1(&b, 9, OP_STP); - rk6502_start(&b, 0); + rk65c02_start(&b, 0); bus_finish(&b); } diff --git a/src/rk65c02.h b/src/rk65c02.h index 53df0ec..259b11e 100644 --- a/src/rk65c02.h +++ b/src/rk65c02.h @@ -79,7 +79,8 @@ typedef struct rk65c02emu rk65c02emu_t; rk65c02emu_t rk65c02_init(bus_t *); void rk65c02_start(rk65c02emu_t *); void rk65c02_step(rk65c02emu_t *, uint16_t); -void rk65c02_dump_regs(reg_state_t regs); +char *rk65c02_regs_string_get(reg_state_t); +void rk65c02_dump_regs(reg_state_t); void rk65c02_dump_stack(rk65c02emu_t *, uint8_t); void rk65c02_irq(rk65c02emu_t *e); diff --git a/test/test_debug.c b/test/test_debug.c index fde4017..eb93f58 100644 --- a/test/test_debug.c +++ b/test/test_debug.c @@ -55,7 +55,7 @@ ATF_TC_BODY(trace, tc) trace_t *tr; int i; - rk6502_loglevel_set(LOG_TRACE); + rk65c02_loglevel_set(LOG_TRACE); b = bus_init_with_default_devs(); a = assemble_init(&b, ROM_LOAD_ADDR);