From ff1f7b8b2f69fa12604f4592e32b95edba6cd9fe Mon Sep 17 00:00:00 2001 From: Takashi Toyoshima Date: Sun, 7 Dec 2014 14:05:38 +0900 Subject: [PATCH] Fix branch operation bugs - address calculation on not taken - use macro to implement each branch operation --- 6502.S | 69 ++++++++++++++++++++++++---------------------------------- test.c | 3 +++ 2 files changed, 31 insertions(+), 41 deletions(-) diff --git a/6502.S b/6502.S index 86b4233..e69d37c 100644 --- a/6502.S +++ b/6502.S @@ -401,8 +401,27 @@ 1: .endm +.macro _bxc reg + adds PC, PC, #1 + movs r0, #\reg + tst SR, r0 + bne 1f + _bxx +1: +.endm + +.macro _bxs reg + adds PC, PC, #1 + movs r0, #\reg + tst SR, r0 + beq 1f + _bxx +1: +.endm + .macro _bxx - _fromImmb + mov r0, PC + _ldb sxtb r0, r0 add PC, PC, r0 .endm @@ -807,11 +826,7 @@ op0e: // ASL - Absolute _decode op10: // BPL (N==0) - movs r0, #FLAG_N - tst SR, r0 - bne 1f - _bxx -1: + _bxc FLAG_N _decode op11: // ORA - (Indirect), Y @@ -913,11 +928,7 @@ op2e: // ROL - Absolute _decode op30: // BMI (N==1) - movs r0, #FLAG_N - tst SR, r0 - beq 1f - _bxx -1: + _bxs FLAG_N _decode op31: // AND - (Indirect), Y @@ -1010,11 +1021,7 @@ op4e: // LSR - Absolute _decode op50: // BVC (V==0) - movs r0, #FLAG_V - tst SR, r0 - bne 1f - _bxx -1: + _bxc FLAG_V _decode op51: // EOR - (Indirect), Y @@ -1111,11 +1118,7 @@ op6e: // ROR - Absolute _decode op70: // BVS (V==1) - movs r0, #FLAG_V - tst SR, r0 - beq 1f - _bxx -1: + _bxs FLAG_V _decode op71: // ADC - (Indirect), Y @@ -1199,11 +1202,7 @@ op8e: // STX - Absolute _decode op90: // BCC (C==0) - movs r0, #FLAG_C - tst SR, r0 - bne 1f - _bxx -1: + _bxc FLAG_C _decode op91: // STA - (Indirect), Y @@ -1308,11 +1307,7 @@ opae: // LDX - Absolute _decode opb0: // BCS (C==1) - movs r0, #FLAG_C - tst SR, r0 - beq 1f - _bxx -1: + _bxs FLAG_C _decode opb1: // LDA - (Indirect), Y @@ -1423,11 +1418,7 @@ opce: // DEC - Absolute _decode opd0: // BNE (Z==0) - movs r0, #FLAG_Z - tst SR, r0 - bne 1f - _bxx -1: + _bxc FLAG_Z _decode opd1: // CMP - (Indirect), Y @@ -1520,11 +1511,7 @@ opee: // INC - Absolute _decode opf0: // BEQ (Z==1) - movs r0, #FLAG_Z - tst SR, r0 - beq 1f - _bxx -1: + _bxs FLAG_Z _decode opf1: // SBC - (Indirect), Y diff --git a/test.c b/test.c index 9fc7e9a..aa8af8d 100644 --- a/test.c +++ b/test.c @@ -14,15 +14,18 @@ void cpu6502_dump( "NV-B_DIZC=%d%d-%d_%d%d%d%d\n", pc, a, x, y, sp, (sr >> 7) & 1, (sr >> 6) & 1, (sr >> 4) & 1, (sr >> 3) & 1, (sr >> 2) & 1, (sr >> 1) & 1, sr & 1); + fflush(stdout); } uint8_t cpu6502_load(uint16_t addr) { printf("load $%04x => $%02x\n", addr, mem[addr]); + fflush(stdout); return mem[addr]; } void cpu6502_store(uint16_t addr, uint8_t data) { printf("store $%04x <= $%02x\n", addr, data); + fflush(stdout); mem[addr] = data; }