From da24007a36d426ef288056dcd8fb91ae76cf4745 Mon Sep 17 00:00:00 2001 From: jborza Date: Sun, 14 Apr 2019 13:24:02 +0200 Subject: [PATCH] code cleanup --- .gitignore | 2 ++ cpu.c | 1 + disassembler.c | 52 +++++++++++++++++++++++++++++++++----------------- emu6502.c | 2 ++ test6502.c | 4 ++++ types.h | 5 +++-- 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index b3c4f02..619683e 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,5 @@ dkms.conf /.vs/ /Debug /Release +/emu6502.vcxproj.user +/emu6502.vcxproj.filters diff --git a/cpu.c b/cpu.c index 205eb49..5287558 100644 --- a/cpu.c +++ b/cpu.c @@ -3,6 +3,7 @@ #include "opcodes.h" #include #include +#include void* unimplemented_instruction(State6502* state) { printf("Error: unimplemented instruction\n"); diff --git a/disassembler.c b/disassembler.c index a981f7f..10ed42f 100644 --- a/disassembler.c +++ b/disassembler.c @@ -1,32 +1,50 @@ #include "types.h" #include "opcodes.h" #include "disassembler.h" +#include void disassemble_6502(byte* buffer, word pc) { printf("%04d ", pc); byte* code = &buffer[pc]; + int bytes = 1; + char op[128]; switch (*code) { - case 0x00: printf("BRK"); break; - case ORA_IMM: printf("ORA #$%02x", code[1]); break; - case ORA_ZP: printf("ORA $%02x", code[1]); break; - case ORA_ZPX: printf("ORA $%02x,X", code[1]); break; - case ORA_ABS: printf("ORA $%02x%02x", code[2], code[1]); break; - case ORA_ABSX: printf("ORA $%02x%02x,X", code[2], code[1]); break; - case ORA_ABSY: printf("ORA $%02x%02x,Y", code[2], code[1]); break; - case ORA_INDX: printf("ORA ($%02x,X)", code[1]); break; - case ORA_INDY: printf("ORA ($%02x),Y", code[1]); break; - case LDA_IMM: printf("LDA #$%02x", code[1]); break; - case LDA_ZP: printf("LDA $%02x", code[1]); break; - case LDA_ZPX: printf("LDA $%02x,X", code[1]); break; - case LDA_ABS: printf("LDA $%02x%02x", code[2], code[1]); break; - case LDA_ABSX: printf("LDA $%02x%02x,X", code[2], code[1]); break; - case LDA_ABSY: printf("LDA $%02x%02x,Y", code[2], code[1]); break; - case LDA_INDX: printf("LDA ($%02x,X)", code[1]); break; - case LDA_INDY: printf("LDA ($%02x),Y", code[1]); break; + case 0x00: sprintf(op,"BRK"); break; + case ORA_IMM: sprintf(op, "ORA #$%02x", code[1]); break; + case ORA_ZP: sprintf(op, "ORA $%02x", code[1]); break; + case ORA_ZPX: sprintf(op, "ORA $%02x,X", code[1]); break; + case ORA_ABS: sprintf(op, "ORA $%02x%02x", code[2], code[1]); break; + case ORA_ABSX: sprintf(op, "ORA $%02x%02x,X", code[2], code[1]); break; + case ORA_ABSY: sprintf(op, "ORA $%02x%02x,Y", code[2], code[1]); break; + case ORA_INDX: sprintf(op, "ORA ($%02x,X)", code[1]); break; + case ORA_INDY: sprintf(op, "ORA ($%02x),Y", code[1]); break; + case LDA_IMM: sprintf(op, "LDA #$%02x", code[1]); break; + case LDA_ZP: sprintf(op, "LDA $%02x", code[1]); break; + case LDA_ZPX: sprintf(op, "LDA $%02x,X", code[1]); break; + case LDA_ABS: sprintf(op, "LDA $%02x%02x", code[2], code[1]); break; + case LDA_ABSX: sprintf(op, "LDA $%02x%02x,X", code[2], code[1]); break; + case LDA_ABSY: sprintf(op, "LDA $%02x%02x,Y", code[2], code[1]); break; + case LDA_INDX: sprintf(op, "LDA ($%02x,X)", code[1]); break; + case LDA_INDY: sprintf(op, "LDA ($%02x),Y", code[1]); break; case 0xEA: printf("NOP"); break; default: printf("UNKNOWN %02x", *code); } + + if (bytes > 1) { + printf("%02x ", code[1]); + } + else { + printf(" "); + } + if (bytes > 2) { + printf("%02x ", code[2]); + } + else { + printf(" "); + } + printf(op); + printf("\n"); } \ No newline at end of file diff --git a/emu6502.c b/emu6502.c index 27bdd8a..274641c 100644 --- a/emu6502.c +++ b/emu6502.c @@ -21,6 +21,8 @@ byte* read_game() { int main() { run_tests(); + printf("All tests succeeded.\n"); + return 0; } //void emulate() { diff --git a/test6502.c b/test6502.c index 18a7d02..822d595 100644 --- a/test6502.c +++ b/test6502.c @@ -1,9 +1,13 @@ #include +#include +#include #include "types.h" #include "test6502.h" #include "opcodes.h" #include "state.h" #include "disassembler.h" +#include "cpu.h" + void print_state(State6502* state) { printf("\tC=%d,Z=%d,I=%d,D=%d,B=%d,V=%d,N=%d\n", state->flags.c, state->flags.z, state->flags.i, state->flags.d, state->flags.b, state->flags.v, state->flags.n); diff --git a/types.h b/types.h index ec78546..0e18a7f 100644 --- a/types.h +++ b/types.h @@ -1,5 +1,6 @@ #pragma once -typedef unsigned __int8 uint8_t; -typedef unsigned __int16 uint16_t; +#include +//typedef unsigned __int8 uint8_t; +//typedef unsigned __int16 uint16_t; typedef uint8_t byte; typedef uint16_t word; \ No newline at end of file