From dc88cf14e609bab9742f7de3b68e22baa3682f0a Mon Sep 17 00:00:00 2001 From: jborza Date: Tue, 7 May 2019 14:09:38 +0200 Subject: [PATCH] fixed a bug setting the carry flag in ASL --- cpu.c | 3 ++- test6502.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/cpu.c b/cpu.c index 4ba35a7..c8140e3 100644 --- a/cpu.c +++ b/cpu.c @@ -208,9 +208,10 @@ void CPY(State6502 * state, byte operand) { cmp_internal(state, state->y, operand); } +//Aritmetic Shift Left byte asl(State6502 * state, byte operand) { byte result = operand << 1; - state->flags.c = operand > 0x80; + state->flags.c = (operand & 0x80) == 0x80; set_NZ_flags(state, result); return result; } diff --git a/test6502.c b/test6502.c index 7ddc9d4..01525f2 100644 --- a/test6502.c +++ b/test6502.c @@ -2391,6 +2391,37 @@ void test_RTI() { test_cleanup(&state); } +// ASL + +void test_ASL_ACC(byte a, byte expected_a, byte expected_c) { + State6502 state = create_blank_state(); + state.a = a; + + //arrange + char program[] = { ASL_ACC }; + memcpy(state.memory, program, sizeof(program)); + + //act + test_step(&state); + + //assert + assertA(&state, expected_a); + assert_flag_c(&state, expected_c); + + //cleanup + test_cleanup(&state); +} + +void test_asl_multiple() { + test_ASL_ACC(/* A */ 0x0, /* Result*/ 0x0, /* C */ 0); + test_ASL_ACC(/* A */ 0x1, /* Result*/ 0x2, /* C */ 0); + test_ASL_ACC(/* A */ 0x1F, /* Result*/ 0x3E, /* C */ 0); + test_ASL_ACC(/* A */ 0x7F, /* Result*/ 0xFE, /* C */ 0); + test_ASL_ACC(/* A */ 0x80, /* Result*/ 0x0, /* C */ 1); + test_ASL_ACC(/* A */ 0x81, /* Result*/ 0x2, /* C */ 1); + test_ASL_ACC(/* A */ 0xFF, /* Result*/ 0xFE, /* C */ 1); +} + ///////////////////// typedef void fp(); @@ -2419,6 +2450,7 @@ fp* tests_jsr_rts[] = { test_JSR, test_JSR_RTS, test_RTS }; fp* tests_brk[] = { test_BRK }; fp* tests_branch[] = { test_branching_multiple }; fp* tests_rti[] = { test_RTI }; +fp* tests_asl[] = { test_asl_multiple }; #define RUN(suite) run_suite(suite, sizeof(suite)/sizeof(fp*)) @@ -2431,6 +2463,7 @@ void run_suite(fp * *suite, int size) { } void run_tests() { + RUN(tests_asl); RUN(tests_rti); RUN(tests_branch); RUN(tests_sbc);