diff options
author | dweller <dweller@cabin.digital> | 2025-03-26 18:25:34 +0200 |
---|---|---|
committer | dweller <dweller@cabin.digital> | 2025-03-26 18:25:34 +0200 |
commit | 42e9b31e0d7324106a65ad9ff42ea2377a80fa51 (patch) | |
tree | ceee87f9e0067b6d5c71f5e728fe0b3fd0fd8b72 /sources/main.c | |
parent | 76d89eb9973671726dc7fffaf22b06a7a77be550 (diff) |
fix disasm not showing instruction hex; implement a few instructions exec; add report logger
Diffstat (limited to 'sources/main.c')
-rw-r--r-- | sources/main.c | 230 |
1 files changed, 37 insertions, 193 deletions
diff --git a/sources/main.c b/sources/main.c index 56630df..8c4ac97 100644 --- a/sources/main.c +++ b/sources/main.c @@ -4,11 +4,13 @@ #include <string.h> #include <assert.h> #include <time.h> +#include <stdarg.h> #include <endian.h> #include "bits.c" #include "fnt.c" +#include "log.c" #define C8_RESET_VECTOR 0x200 @@ -46,14 +48,13 @@ typedef struct } chip8; -#include "instr.c" - #define V mem.sys.v #define STACK mem.sys.stack #define DISPLAY mem.sys.disp #define FONT mem.sys.font #define RAM mem.ram +#include "instr.c" void c8_reset(chip8* c8) { @@ -64,6 +65,7 @@ void c8_reset(chip8* c8) memcpy(c8->FONT, fnt, sizeof(fnt)); c8->pc = C8_RESET_VECTOR; + c8->running = true; } void c8_dump_state(chip8* c8, bool stack) @@ -91,197 +93,6 @@ void c8_dump_state(chip8* c8, bool stack) } } -void _c8_loop(chip8* c8, bool disasm, u16 from, u16 len) -{ - u16 to = 0; - u16 pc = 0; - - assert(c8); - - if(disasm) - { - pc = c8->pc; - c8->pc = from; - to = from + len*2; - } - - c8->running = true; - while(c8->running) - { - int tmp; - u16 word, instr, nnn; - u8 o, x, y, n, kk; - - if(c8->pc >= sizeof(c8->mem)) - { - printf("PC Overflow!\n"); - c8->running = false; - break; - } - - if(c8->pc & 0x0001) - { - printf("PC is not aligned!\n"); - c8->running = false; - break; - } - - word = *(u16*)(c8->RAM + c8->pc); - instr = be16toh(word); - o = (instr & 0xF000) >> 12; - nnn = (instr & 0x0FFF); - x = (instr & 0x0F00) >> 8; - y = (instr & 0x00F0) >> 4; - n = (instr & 0x000F); - kk = (instr & 0x00FF); - - printf("%04X: ", c8->pc); - switch(o) - { - case 1: - printf(" %04X JP 0x%03X\n", instr, nnn); - if(disasm) break; - c8->pc = nnn - 2; - break; - - case 2: - printf(" %04X CALL 0x%03X\n", instr, nnn); - if(disasm) break; - c8->STACK[c8->sp++] = c8->pc; /* docs says ++sp then stack[sp] = pc ??? */ - c8->pc = nnn - 2; - break; - - case 3: - printf(" %04X SE V%x, 0x%02X\n", instr, x, kk); - if(disasm) break; - if(c8->V[x] == kk) c8->pc += 2; - break; - - case 5: - printf(" %04X SE V%x, V%x\n", instr, x, y); - if(disasm) break; - if(c8->V[x] == c8->V[y]) c8->pc += 2; - break; - - case 6: - printf(" %04X LD V%x, 0x%02X\n", instr, x, kk); - if(disasm) break; - c8->V[x] = kk; - break; - - case 7: - printf(" %04X ADD V%x, 0x%02X\n", instr, x, kk); - if(disasm) break; - tmp = (int)c8->V[x] + (int)kk; - c8->V[0xf] = (tmp > 255); - /* c8->V[x] = (u8)tmp; /* spec says Vf not set */ - break; - - case 8: - { - switch(n) - { - case 4: - printf(" %04X ADD V%x, V%x\n", instr, x, y); - if(disasm) break; - tmp = (int)c8->V[x] + (int)c8->V[y]; - c8->V[0xf] = (tmp > 255); - c8->V[x] = (u8)tmp; - break; - - default: - printf(" %04X Unknown instruction\n", instr); - if(disasm) break; - c8->running = false; - break; - } - } - break; - - case 0xA: - printf(" %04X LD I, 0x%03X\n", instr, nnn); - if(disasm) break; - c8->i = nnn; - break; - - case 0xC: - printf(" %04X RND V%x, 0x%02X\n", instr, x, kk); - if(disasm) break; - c8->V[x] = (rand() % 256) & kk; - break; - - case 0xD: - printf(" %04X DRW V%x, V%x, %d\n", instr, x, y, n); - if(disasm) break; - /* TODO: implement */ - break; - - case 0xE: - if(kk == 0x9E) - { - printf(" %04X SKP V%x\n", instr, x); - if(disasm) break; - if(c8->keys & bit(x)) c8->pc += 2; - } - else if(kk == 0xA1) - { - printf(" %04X SKNP V%x\n", instr, x); - if(disasm) break; - if(!(c8->keys & bit(x))) c8->pc += 2; - } - else - { - printf(" %04X Unknown instruction\n", instr); - if(disasm) break; - c8->running = false; - } - break; - - case 0xF: - if(kk == 0x07) - { - printf(" %04X LD V%x, DT\n", instr, x); - if(disasm) break; - c8->V[x] = c8->dt; - } - else if(kk == 0x15) - { - printf(" %04X LD DT, V%x\n", instr, x); - if(disasm) break; - c8->dt= c8->V[x]; - } - else - { - printf(" %04X Unknown instruction\n", instr); - if(disasm) break; - c8->running = false; - } - break; - - default: - printf(" %04X Unknown instruction\n", instr); - if(disasm) break; - c8->running = false; - break; - } - - if(!disasm) c8_dump_state(c8, false); - - c8->pc += 2; - c8->cycles++; - if((c8->cycles % C8_CYCLES_PER_FRAME) == 0) - { - if(c8->dt) c8->dt--; - if(c8->st) c8->st--; - } - - if(disasm && (c8->pc >= to)) c8->running = false; - } - - if(disasm) c8->pc = pc; -} - - int main(int argc, char** argv) { u64 sz = 0; @@ -346,5 +157,38 @@ int main(int argc, char** argv) } } + printf("\nExec:\n"); + while(c8.running) + { + char buf[512] = {0}; + + if(c8.pc >= sizeof(c8.mem)) + { + report(ERR, "PC Overflow!\n"); + c8.running = false; + } + + if(c8.pc & 0x0001) + { + report(ERR, "PC is not aligned!\n"); + c8.running = false; + } + + c8_disasm(c8.RAM, c8.pc, buf); + printf("%04X: %s\n", c8.pc, buf); + + c8_exec(c8.RAM, c8.pc, &c8); + c8_dump_state(&c8, false); + + c8.cycles++; + if((c8.cycles % C8_CYCLES_PER_FRAME) == 0) + { + if(c8.dt) c8.dt--; + if(c8.st) c8.st--; + } + + c8.pc += 2; + } + return 0; } |