From 6dec18dada0668fe3ab435274c85a045f2ffa38f Mon Sep 17 00:00:00 2001 From: jborza Date: Tue, 7 May 2019 15:39:43 +0200 Subject: [PATCH] fixed ROR + added ROR tests --- cpu.c | 7 +++---- test6502.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/cpu.c b/cpu.c index c8140e3..809fef4 100644 --- a/cpu.c +++ b/cpu.c @@ -260,15 +260,14 @@ void ROL_MEM(State6502 * state, word address) { } byte ror(State6502 * state, byte operand) { - word result_word = (operand >> 1) | (state->flags.c << 7); - state->flags.c = (result_word & 0x01) != 0; - byte result = result_word & 0xFF; + byte result = (operand >> 1) | (state->flags.c << 7); + state->flags.c = (operand & 0x01) != 0; set_NZ_flags(state, result); return result; } void ROR_A(State6502 * state) { - state->a = rol(state, state->a); + state->a = ror(state, state->a); } void ROR_MEM(State6502 * state, word address) { diff --git a/test6502.c b/test6502.c index 01525f2..2dad20b 100644 --- a/test6502.c +++ b/test6502.c @@ -2422,6 +2422,50 @@ void test_asl_multiple() { test_ASL_ACC(/* A */ 0xFF, /* Result*/ 0xFE, /* C */ 1); } +// ROR + +void test_ROR_ACC(byte a, byte c, byte expected_a, byte expected_c, byte expected_n, byte expected_z) { + State6502 state = create_blank_state(); + state.a = a; + state.flags.c = c; + + //arrange + char program[] = { ROR_ACC }; + memcpy(state.memory, program, sizeof(program)); + + //act + test_step(&state); + + //assert + assertA(&state, expected_a); + assert_flag_c(&state, expected_c); + assert_flag_n(&state, expected_n); + assert_flag_z(&state, expected_z); + + //cleanup + test_cleanup(&state); +} + +void test_ror_multiple() { + test_ROR_ACC(/*A*/ 0x00, /*C*/ 0, /* Result */ 0x00, /* C */ 0, /* N */ 0, /*Z*/ 1); + test_ROR_ACC(/*A*/ 0x01, /*C*/ 0, /* Result */ 0x00, /* C */ 1, /* N */ 0, /*Z*/ 1); + test_ROR_ACC(/*A*/ 0x02, /*C*/ 0, /* Result */ 0x01, /* C */ 0, /* N */ 0, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0x40, /*C*/ 0, /* Result */ 0x20, /* C */ 0, /* N */ 0, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0x80, /*C*/ 0, /* Result */ 0x40, /* C */ 0, /* N */ 0, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0x81, /*C*/ 0, /* Result */ 0x40, /* C */ 1, /* N */ 0, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0xC0, /*C*/ 0, /* Result */ 0x60, /* C */ 0, /* N */ 0, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0xFE, /*C*/ 0, /* Result */ 0x7F, /* C */ 0, /* N */ 0, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0xFF, /*C*/ 0, /* Result */ 0x7F, /* C */ 1, /* N */ 0, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0x00, /*C*/ 1, /* Result */ 0x80, /* C */ 0, /* N */ 1, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0x01, /*C*/ 1, /* Result */ 0x80, /* C */ 1, /* N */ 1, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0x02, /*C*/ 1, /* Result */ 0x81, /* C */ 0, /* N */ 1, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0x40, /*C*/ 1, /* Result */ 0xA0, /* C */ 0, /* N */ 1, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0x80, /*C*/ 1, /* Result */ 0xC0, /* C */ 0, /* N */ 1, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0x81, /*C*/ 1, /* Result */ 0xC0, /* C */ 1, /* N */ 1, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0xC0, /*C*/ 1, /* Result */ 0xE0, /* C */ 0, /* N */ 1, /*Z*/ 0); + test_ROR_ACC(/*A*/ 0xFF, /*C*/ 1, /* Result */ 0xFF, /* C */ 1, /* N */ 1, /*Z*/ 0); +} + ///////////////////// typedef void fp(); @@ -2451,6 +2495,7 @@ fp* tests_brk[] = { test_BRK }; fp* tests_branch[] = { test_branching_multiple }; fp* tests_rti[] = { test_RTI }; fp* tests_asl[] = { test_asl_multiple }; +fp* tests_ror[] = { test_ror_multiple }; #define RUN(suite) run_suite(suite, sizeof(suite)/sizeof(fp*)) @@ -2463,6 +2508,7 @@ void run_suite(fp * *suite, int size) { } void run_tests() { + RUN(tests_ror); RUN(tests_asl); RUN(tests_rti); RUN(tests_branch);