From a1259fa544331f9a8245a8a0929d98c8ba0ea8dc Mon Sep 17 00:00:00 2001 From: jborza Date: Fri, 26 Apr 2019 12:14:25 +0200 Subject: [PATCH] fixed the carry flag in SBC --- cpu.c | 2 +- test6502.c | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/cpu.c b/cpu.c index 6ee02d9..4ae12a0 100644 --- a/cpu.c +++ b/cpu.c @@ -150,7 +150,7 @@ void SBC(State6502 * state, byte operand) { state->a = result; state->flags.n = is_negative(state->a); state->flags.z = state->a == 0; - state->flags.c = result_word > 0xFF; + state->flags.c = result_word <= 0xFF; } void ADC(State6502 * state, byte operand) { diff --git a/test6502.c b/test6502.c index 87fd6f5..465bc8a 100644 --- a/test6502.c +++ b/test6502.c @@ -2078,11 +2078,12 @@ void test_SBC_IMM_(byte a, byte c, byte operand, byte expected_a, byte expected_ void test_SBC_IMM_multiple() { //A, C, OP => A, N, Z, C, V - test_SBC_IMM_(0x08, 0, /* OP */ 0x01, /*A*/ 0x06, /*N*/ 0, /*Z*/ 0, /*C*/ 0, /*V*/ 0); //regular subtraction without carry bit - test_SBC_IMM_(0x08, 1, /* OP */ 0x01, /*A*/ 0x07, /*N*/ 0, /*Z*/ 0, /*C*/ 0, /*V*/ 0); //regular subtraction with carry bit - test_SBC_IMM_(0x0A, 1, /* OP */ 0x0A, /*A*/ 0x00, /*N*/ 0, /*Z*/ 1, /*C*/ 0, /*V*/ 0); //zero - test_SBC_IMM_(0x04, 1, /* OP */ 0x06, /*A*/ 0xFE, /*N*/ 1, /*Z*/ 0, /*C*/ 1, /*V*/ 0); //borrow from carry bit - 4 + 256 - 6 = 255 - test_SBC_IMM_(0x04, 0, /* OP */ 0x06, /*A*/ 0xFD, /*N*/ 1, /*Z*/ 0, /*C*/ 1, /*V*/ 0); //borrow from carry bit - 4 + 256 - 6 = 254 + test_SBC_IMM_(0x08, 0, /* OP */ 0x01, /*A*/ 0x06, /*N*/ 0, /*Z*/ 0, /*C*/ 1, /*V*/ 0); //regular subtraction without carry bit + test_SBC_IMM_(0x08, 1, /* OP */ 0x01, /*A*/ 0x07, /*N*/ 0, /*Z*/ 0, /*C*/ 1, /*V*/ 0); //regular subtraction with carry bit + test_SBC_IMM_(0x0A, 1, /* OP */ 0x0A, /*A*/ 0x00, /*N*/ 0, /*Z*/ 1, /*C*/ 1, /*V*/ 0); //zero + test_SBC_IMM_(0x0A, 0, /* OP */ 0x0A, /*A*/ 0xFF, /*N*/ 1, /*Z*/ 0, /*C*/ 0, /*V*/ 0); //negative + test_SBC_IMM_(0x04, 1, /* OP */ 0x06, /*A*/ 0xFE, /*N*/ 1, /*Z*/ 0, /*C*/ 0, /*V*/ 0); //borrow from carry bit - 4 + 256 - 6 = 255 + test_SBC_IMM_(0x04, 0, /* OP */ 0x06, /*A*/ 0xFD, /*N*/ 1, /*Z*/ 0, /*C*/ 0, /*V*/ 0); //borrow from carry bit - 4 + 256 - 6 = 254 //source: http://www.righto.com/2012/12/the-6502-overflow-flag-explained.html //C7 - carry, B - Borrow, S7 - N, V - V test_SBC_IMM_(0x50, 1, /* OP */ 0xF0, /*A*/ 0x60, /*N*/ 0, /*Z*/ 0, /*C*/ 0, /*V*/ 0); //Unsigned borrow but no signed overflow