From fe9ce3aca354245ff4f7596dfeed02b6332d882c Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Sat, 2 Dec 2017 13:27:30 -0600 Subject: [PATCH] Tests for the new mos6502 functions --- include/mos6502.h | 7 ++++++ src/mos6502.c | 31 ++++++++++++++++++++--- tests/mos6502.c | 53 ++++++++++++++++++++++++++++++++++++++++ tests/mos6502.loadstor.c | 4 +++ 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 tests/mos6502.loadstor.c diff --git a/include/mos6502.h b/include/mos6502.h index c930fe3..66805bb 100644 --- a/include/mos6502.h +++ b/include/mos6502.h @@ -24,6 +24,13 @@ #define END_ADDR_MODE() \ mos6502_free(cpu) +#define START_CPU_TEST(t) \ + t *cpu; \ + cpu = t##_create() + +#define END_CPU_TEST(t) \ + t##_free(cpu) + #define DEFINE_INST(inst) \ void mos6502_handle_##inst (mos6502 *cpu, vm_8bit oper) diff --git a/src/mos6502.c b/src/mos6502.c index faa60f5..c6b06c7 100644 --- a/src/mos6502.c +++ b/src/mos6502.c @@ -20,6 +20,7 @@ * pointers to all of that in the chip structure */ +#include #include #include "log.h" @@ -141,17 +142,39 @@ mos6502_modify_status(mos6502 *cpu, int statuses, vm_8bit oper) } } - if (statuses & ZERO) { - cpu->P &= ~ZERO; - if (oper == 0) { - cpu->P |= ZERO; + if (statuses & OVERFLOW) { + cpu->P &= ~OVERFLOW; + if (oper & OVERFLOW) { + cpu->P |= OVERFLOW; } } + // We're kind of dumb about break, decimal and interrupt. I'm + // probably lacking some knowledge about how these work... + if (statuses & BREAK) { + cpu->P |= BREAK; + } + + if (statuses & DECIMAL) { + cpu->P |= DECIMAL; + } + + if (statuses & INTERRUPT) { + cpu->P |= INTERRUPT; + } + if (statuses & CARRY) { cpu->P &= ~CARRY; if (oper > 0) { cpu->P |= CARRY; } } + + if (statuses & ZERO) { + cpu->P &= ~ZERO; + if (oper == 0) { + cpu->P |= ZERO; + } + } + } diff --git a/tests/mos6502.c b/tests/mos6502.c index f6f60f7..3454249 100644 --- a/tests/mos6502.c +++ b/tests/mos6502.c @@ -35,3 +35,56 @@ Test(mos6502, next_byte) { END_ADDR_MODE(); } + +Test(mos6502, push_stack) +{ + START_CPU_TEST(mos6502); + + mos6502_push_stack(cpu, 0x1234); + cr_assert_eq(vm_segment_get(cpu->memory, 0x0100), 0x12); + cr_assert_eq(vm_segment_get(cpu->memory, 0x0101), 0x34); + + END_CPU_TEST(mos6502); +} + +Test(mos6502, pop_stack) +{ + START_CPU_TEST(mos6502); + + mos6502_push_stack(cpu, 0x1234); + cr_assert_eq(mos6502_pop_stack(cpu), 0x1234); + + END_CPU_TEST(mos6502); +} + +Test(mos6502, modify_status) +{ + START_CPU_TEST(mos6502); + + mos6502_modify_status(cpu, NEGATIVE, 130); + cr_assert_eq(cpu->P & NEGATIVE, NEGATIVE); + mos6502_modify_status(cpu, NEGATIVE, 123); + cr_assert_neq(cpu->P & NEGATIVE, NEGATIVE); + + mos6502_modify_status(cpu, OVERFLOW, 123); + cr_assert_eq(cpu->P & OVERFLOW, OVERFLOW); + mos6502_modify_status(cpu, OVERFLOW, 44); + cr_assert_neq(cpu->P & OVERFLOW, OVERFLOW); + + mos6502_modify_status(cpu, BREAK, 0); + mos6502_modify_status(cpu, INTERRUPT, 0); + mos6502_modify_status(cpu, DECIMAL, 0); + cr_assert_eq(cpu->P & BREAK & INTERRUPT & DECIMAL, BREAK & INTERRUPT & DECIMAL); + + mos6502_modify_status(cpu, CARRY, 23); + cr_assert_eq(cpu->P & CARRY, CARRY); + mos6502_modify_status(cpu, CARRY, 0); + cr_assert_neq(cpu->P & CARRY, CARRY); + + mos6502_modify_status(cpu, ZERO, 0); + cr_assert_eq(cpu->P & ZERO, ZERO); + mos6502_modify_status(cpu, ZERO, 1); + cr_assert_neq(cpu->P & ZERO, ZERO); + + END_CPU_TEST(mos6502); +} diff --git a/tests/mos6502.loadstor.c b/tests/mos6502.loadstor.c new file mode 100644 index 0000000..d6e062a --- /dev/null +++ b/tests/mos6502.loadstor.c @@ -0,0 +1,4 @@ +#include + +#include "mos6502.h" +#include "mos6502.enums.h"