mirror of
https://github.com/jborza/emu6502.git
synced 2025-02-21 06:29:03 +00:00
BIT implementation + tests
ADC, SBC fixes
This commit is contained in:
parent
f5fd1d21c5
commit
1845eb4692
21
cpu.c
21
cpu.c
@ -22,6 +22,7 @@ void set_NV_flags(State6502 * state, byte value) {
|
||||
//N flag
|
||||
state->flags.n = is_negative(value);
|
||||
//TODO implement NV flags
|
||||
state->flags.v = ((1 << 6) & value) != 0;
|
||||
}
|
||||
|
||||
void set_NZ_flags(State6502 * state, byte value) {
|
||||
@ -133,10 +134,11 @@ void SBC(State6502* state, byte operand) {
|
||||
operand_word += 0x100;
|
||||
state->flags.c = 0;
|
||||
}
|
||||
word result = state->a - operand_word;
|
||||
word result_word = state->a - operand_word;
|
||||
byte result = result_word & 0xFF;
|
||||
|
||||
// overflow flag if the the result doesn't fit into the signed byte range -128 to 127
|
||||
state->flags.v = (state->a ^ operand) & 0x80) && ((state->a ^ (result) & 0x80);
|
||||
state->flags.v = ((state->a ^ operand) & 0x80) && ((state->a ^ result) & 0x80);
|
||||
|
||||
state->a -= result;
|
||||
state->flags.n = is_negative(state->a);
|
||||
@ -145,17 +147,24 @@ void SBC(State6502* state, byte operand) {
|
||||
|
||||
void ADC(State6502 * state, byte operand) {
|
||||
//add operand to A
|
||||
word result_word = operand + state->a + state->flags.c ? 1 : 0;
|
||||
word result_word = operand + state->a + (state->flags.c ? 1 : 0);
|
||||
byte result = result_word & 0xFF;
|
||||
//set overflow flag if the result's sign would change - the result doesn't fit into a signed byte
|
||||
//there is overflow if the inputs do not have different signs and the input sign is different from the output sign
|
||||
state->flags.v = !((state->a ^ operand) & 0x80) && ((state->a ^ result) & 0x80);
|
||||
state->a = state->a & 0xFF;
|
||||
state->a = result;
|
||||
state->flags.n = is_negative(state->a);
|
||||
state->flags.z = state->a == 0;
|
||||
state->flags.c = result_word > 0xFF;
|
||||
}
|
||||
|
||||
void BIT(State6502 * state, byte operand) {
|
||||
//BIT sets the Z flag as though the value in the address tested were ANDed with the accumulator.
|
||||
//The N and V flags are set to match bits 7 and 6 respectively in the value stored at the tested address.
|
||||
set_NV_flags(state, operand);
|
||||
state->flags.z = (state->a & operand) == 0;
|
||||
}
|
||||
|
||||
void cmp_internal(State6502 * state, byte register_value, byte operand) {
|
||||
//set carry flag if A >= M
|
||||
state->flags.c = register_value >= operand;
|
||||
@ -388,8 +397,8 @@ int emulate_6502_op(State6502 * state) {
|
||||
case BPL_REL: unimplemented_instruction(state); break;
|
||||
case BVC_REL: unimplemented_instruction(state); break;
|
||||
case BVS_REL: unimplemented_instruction(state); break;
|
||||
case BIT_ZP: unimplemented_instruction(state); break;
|
||||
case BIT_ABS: unimplemented_instruction(state); break;
|
||||
case BIT_ZP: BIT(state, get_byte_zero_page(state)); break;
|
||||
case BIT_ABS: BIT(state, get_byte_absolute(state)); break;
|
||||
case BRK: state->running = 0;
|
||||
state->flags.b = 1;
|
||||
break; //BRK
|
||||
|
54
test6502.c
54
test6502.c
@ -2022,6 +2022,56 @@ void test_SBC_IMM_carry() {
|
||||
test_cleanup(&state);
|
||||
}
|
||||
|
||||
// ADC
|
||||
|
||||
void test_ADC_IMM_exec(byte a, byte c, byte operand, byte expected_a, byte expected_n, byte expected_z, byte expected_c, byte expected_v) {
|
||||
State6502 state = create_blank_state();
|
||||
state.a = a;
|
||||
state.flags.c = c;
|
||||
char program[] = { ADC_IMM, operand };
|
||||
memcpy(state.memory, program, sizeof(program));
|
||||
//act
|
||||
test_step(&state);
|
||||
//assert
|
||||
assertA(&state, expected_a);
|
||||
assert_flag_n(&state, expected_n);
|
||||
assert_flag_z(&state, expected_z);
|
||||
assert_flag_c(&state, expected_c);
|
||||
assert_flag_v(&state, expected_v);
|
||||
}
|
||||
|
||||
void test_ADC_IMM_multiple() {
|
||||
//A, C, OP => A, N, Z, C, V
|
||||
test_ADC_IMM_exec(2, 0, 3, 5, 0, 0, 0, 0); //straight addition
|
||||
test_ADC_IMM_exec(2, 1, 3, 6, 0, 0, 0, 0); //straight addition with carry
|
||||
test_ADC_IMM_exec(2, 0, 254, 0, 0, 1, 1, 0); //carry and zero
|
||||
test_ADC_IMM_exec(2, 0, 253, 255, 1, 0, 0, 1); //just negative
|
||||
test_ADC_IMM_exec(253, 0, 6, 3, 0, 0, 1, 1); //carry and overflow
|
||||
test_ADC_IMM_exec(125, 1, 2, 128, 1, 0, 0, 1); //negative and overflow
|
||||
}
|
||||
|
||||
// BIT
|
||||
void test_BIT_exec(byte a, byte expected_a, byte expected_n, byte expected_v, byte expected_z) {
|
||||
State6502 state = create_blank_state();
|
||||
state.a = a;
|
||||
char program[] = { BIT_ABS, 0x45, 0x03};
|
||||
memcpy(state.memory, program, sizeof(program));
|
||||
state.memory[0x0345] = 0xF3;
|
||||
//act
|
||||
test_step(&state);
|
||||
//assert
|
||||
assertA(&state, expected_a);
|
||||
assert_flag_n(&state, expected_n);
|
||||
assert_flag_z(&state, expected_z);
|
||||
assert_flag_v(&state, expected_v);
|
||||
}
|
||||
|
||||
void test_BIT_multiple() {
|
||||
test_BIT_exec(128, 128, 1, 1, 0);
|
||||
test_BIT_exec(5, 5, 1, 1, 0);
|
||||
test_BIT_exec(4, 4, 1, 1, 1); // 128 & 4 = 0 -> Z = 1
|
||||
test_BIT_exec(3, 3, 1, 1, 0);
|
||||
}
|
||||
|
||||
/////////////////////
|
||||
|
||||
@ -2045,6 +2095,8 @@ fp* tests_php_plp[] = { test_PHP, test_PLP };
|
||||
fp* tests_jmp[] = { test_JMP, test_JMP_IND, test_JMP_IND_wrap };
|
||||
fp* tests_cmp[] = { test_CMP_ABS_equal, test_CMP_ABS_greater, test_CMP_ABS_greater_2, test_CMP_ABS_less_than, test_CPX_ABS, test_CPY_ABS };
|
||||
fp* tests_sbc[] = { test_SBC_IMM };
|
||||
fp* tests_adc[] = { test_ADC_IMM_multiple };
|
||||
fp* tests_bit[] = { test_BIT_multiple };
|
||||
|
||||
#define RUN(suite) run_suite(suite, sizeof(suite)/sizeof(fp*))
|
||||
|
||||
@ -2054,6 +2106,8 @@ void run_suite(fp * *suite, int size) {
|
||||
}
|
||||
|
||||
void run_tests() {
|
||||
RUN(tests_bit);
|
||||
RUN(tests_adc);
|
||||
RUN(tests_sbc);
|
||||
RUN(tests_lda);
|
||||
RUN(tests_ora);
|
||||
|
Loading…
x
Reference in New Issue
Block a user