Skip to content

Commit

Permalink
misc fixes and tests (yes arith is broken)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsvic-dev committed May 3, 2024
1 parent 1571d94 commit af9d49f
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 10 deletions.
2 changes: 1 addition & 1 deletion example/asm/klaus-functional.asm
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ ram_top = -1

;disable test decimal mode ADC & SBC, 0=enable, 1=disable,
;2=disable including decimal flag in processor status
disable_decimal = 0
disable_decimal = 2

;macros for error & success traps to allow user modification
;example:
Expand Down
7 changes: 7 additions & 0 deletions example/klaustest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ int main(int argc, char **argv) {
// override read PC with Klaus test start
cpu.pc = 0x400;

uint16_t prevPc = 0;

try {
while (true) {
prevPc = cpu.pc;
cpu.executeStep();
if (cpu.pc == prevPc) {
print("caught trap on PC ${:04x}\n", prevPc);
break;
}
}
} catch (...) {
print("[warn] execution ended prematurely due to a v6502 error!!!\n");
Expand Down
7 changes: 4 additions & 3 deletions src/opcodes/logic.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include "v6502/cpu.h"
#include "v6502/opcodes.h"
#include <cstdint>

OPCODE(_and) {
cpu->a = cpu->a & (cpu->fetchByteWithMode(mode));
cpu->evaluateFlags(cpu->a);
}

OPCODE(bit) {
uint8_t result = cpu->a & (cpu->fetchByteWithMode(mode));
const uint8_t mask = (1 << 7) | (1 << 6);
cpu->status = (result & mask) | (cpu->status & ~mask);
uint8_t m = cpu->fetchByteWithMode(mode);
uint8_t result = cpu->a & m;
cpu->status = (cpu->status & ~(0b11 << 6)) | (m & (0b11 << 6));
cpu->clearFlags(STATUS_ZERO);
if (result == 0) {
cpu->setFlags(STATUS_ZERO);
Expand Down
17 changes: 17 additions & 0 deletions tests/instructions/arith/adc-80.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// does ADC #$01 work properly with A=0xFF?
#include "testMain.h"
#include "v6502/cpu.h"
#include <cassert>

TEST {
cpu->bus->write(0, 0x69); // nice
cpu->bus->write(1, 0x80);
cpu->reset();
cpu->executeStep();

assert(cpu->a == 0x80);
assert(FLAG_IS_SET(cpu->status, STATUS_NEGATIVE));
assert(FLAG_IS_CLEAR(cpu->status, STATUS_OVERFLOW));
assert(FLAG_IS_CLEAR(cpu->status, STATUS_ZERO));
assert(FLAG_IS_CLEAR(cpu->status, STATUS_CARRY));
}
File renamed without changes.
5 changes: 4 additions & 1 deletion tests/instructions/arith/sbc-1-02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ TEST {
cpu->executeStep();

assert(cpu->a == 0x00);
STATUS_ASSERT(STATUS_ZERO | STATUS_CARRY);
assert(FLAG_IS_CLEAR(cpu->status, STATUS_NEGATIVE));
assert(FLAG_IS_CLEAR(cpu->status, STATUS_OVERFLOW));
assert(FLAG_IS_SET(cpu->status, STATUS_ZERO));
assert(FLAG_IS_SET(cpu->status, STATUS_CARRY));
}
5 changes: 4 additions & 1 deletion tests/instructions/arith/sbc-1-80.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ TEST {
cpu->executeStep();

assert(cpu->a == 0x7E);
STATUS_ASSERT(STATUS_OVERFLOW | STATUS_CARRY);
assert(FLAG_IS_CLEAR(cpu->status, STATUS_NEGATIVE));
assert(FLAG_IS_SET(cpu->status, STATUS_OVERFLOW));
assert(FLAG_IS_CLEAR(cpu->status, STATUS_ZERO));
assert(FLAG_IS_SET(cpu->status, STATUS_CARRY));
}
6 changes: 5 additions & 1 deletion tests/instructions/arith/sbc-1.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "testMain.h"
#include "v6502/cpu.h"
#include <cassert>

TEST {
// SBC #$01
Expand All @@ -9,5 +10,8 @@ TEST {
cpu->executeStep();

assert(cpu->a == 0xFE);
STATUS_ASSERT(STATUS_NEGATIVE);
assert(FLAG_IS_SET(cpu->status, STATUS_NEGATIVE));
assert(FLAG_IS_CLEAR(cpu->status, STATUS_OVERFLOW));
assert(FLAG_IS_CLEAR(cpu->status, STATUS_ZERO));
assert(FLAG_IS_CLEAR(cpu->status, STATUS_CARRY));
}
18 changes: 18 additions & 0 deletions tests/instructions/arith/sbc-fe-7f.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "testMain.h"
#include "v6502/cpu.h"

TEST {
cpu->a = 0x7F;

// SBC #$FE
cpu->bus->write(0, 0xE9);
cpu->bus->write(1, 0xFE);
cpu->reset();
cpu->executeStep();

assert(cpu->a == 0x80);
assert(FLAG_IS_SET(cpu->status, STATUS_NEGATIVE));
assert(FLAG_IS_SET(cpu->status, STATUS_OVERFLOW));
assert(FLAG_IS_CLEAR(cpu->status, STATUS_ZERO));
assert(FLAG_IS_CLEAR(cpu->status, STATUS_CARRY));
}
6 changes: 3 additions & 3 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ tests = {
'instructions(arith) - ADC #$01': 'instructions/arith/adc-1.cpp',
'instructions(arith) - ADC #$01 with A=$FF': 'instructions/arith/adc-1-ff.cpp',
'instructions(arith) - ADC #$01 with A=$7F': 'instructions/arith/adc-1-7f.cpp',
'instructions(arith) - ADC #$80': 'instructions/arith/adc-80.cpp',
'instructions(arith) - CMP #$3A with A=$3A': 'instructions/arith/cmp-3a-3a.cpp',
'instructions(arith) - SBC #$01': 'instructions/arith/sbc-1.cpp',
'instructions(arith) - SBC #$01 with A=$02': 'instructions/arith/sbc-1-02.cpp',
'instructions(arith) - SBC #$01 with A=$80': 'instructions/arith/sbc-1-80.cpp',
'instructions(arith) - SBC #$FE with A=$7F': 'instructions/arith/sbc-fe-7f.cpp',

'instructions(branch) - BCC with clear carry': 'instructions/branch/bcc-clear.cpp',
'instructions(branch) - BCC with set carry': 'instructions/branch/bcc-set.cpp',
Expand All @@ -40,9 +43,6 @@ tests = {

'instructions(shift) - ASL A with A=$80': 'instructions/shift/asl-a-80.cpp',
'instructions(shift) - ROL A with set carry': 'instructions/shift/rol-sec.cpp',

# old bugs
'old bugs - does CMP #$3A with A=$3A work correctly?': 'bugs/cmp_immediate_3a_works.cpp',
}

foreach name, sources : tests
Expand Down

0 comments on commit af9d49f

Please sign in to comment.