diff --git a/tests/setup_tests.c b/tests/setup_tests.c new file mode 100644 index 0000000..b5cf664 --- /dev/null +++ b/tests/setup_tests.c @@ -0,0 +1,11 @@ +#include "../src/6502.h" +#include "setup_tests.h" + +void setup_immediate(CPU *cpu, Memory *memory, immediateParams *params) { + reset(cpu, memory); + + cpu->PC = params->startingAddress; + writeByte(memory, params->startingAddress, params->opcode); + writeByte(memory, params->startingAddress + 0x01, params->testValue); + params->cycles = 2; +} \ No newline at end of file diff --git a/tests/setup_tests.h b/tests/setup_tests.h new file mode 100644 index 0000000..5a1da74 --- /dev/null +++ b/tests/setup_tests.h @@ -0,0 +1,13 @@ +#ifndef SETUP_TESTS_H +#define SETUP_TESTS_H + +typedef struct { + word startingAddress; + byte testValue; + byte opcode; + uint cycles; +} immediateParams; + +void setup_immediate(CPU *cpu, Memory *memory, immediateParams *params); + +#endif \ No newline at end of file diff --git a/tests/test_lda.c b/tests/test_lda.c index 6da5601..2d5244f 100644 --- a/tests/test_lda.c +++ b/tests/test_lda.c @@ -1,23 +1,21 @@ #include "CUnit/Basic.h" #include "../src/6502.h" +#include "setup_tests.h" void test_lda_immediate_positive() { CPU cpu; Memory memory; - reset(&cpu, &memory); + immediateParams params = { + .opcode = OP_LDA_IM, .cycles = 2, + .startingAddress = 0x0100, .testValue = 0x7F + }; - word startingAddress = 0x0100; - byte testValue = 0x7F; - cpu.PC = startingAddress; // Arbitrary position - writeByte(&memory, startingAddress, OP_LDA_IM); // Opcode for LDA Immediate - writeByte(&memory, startingAddress + 0x01, testValue); // Positive test value that does not set the negative flag + setup_immediate(&cpu, &memory, ¶ms); + execute(&cpu, &memory, ¶ms.cycles); - uint cycles = 2; - execute(&cpu, &memory, &cycles); - - CU_ASSERT_EQUAL(cpu.A, testValue); - CU_ASSERT_EQUAL(cycles, 0); - CU_ASSERT_EQUAL(cpu.PC, startingAddress + 0x02); + CU_ASSERT_EQUAL(cpu.A, params.testValue); + CU_ASSERT_EQUAL(params.cycles, 0); + CU_ASSERT_EQUAL(cpu.PC, params.startingAddress + 0x02); CU_ASSERT_FALSE(cpu.PS & ZERO_FLAG); CU_ASSERT_FALSE(cpu.PS & NEGATIVE_FLAG); } @@ -25,20 +23,17 @@ void test_lda_immediate_positive() { void test_lda_immediate_zero() { CPU cpu; Memory memory; - reset(&cpu, &memory); - - word startingAddress = 0x0100; - byte testValue = 0x00; - cpu.PC = startingAddress; // Arbitrary position - writeByte(&memory, startingAddress, OP_LDA_IM); // Opcode for LDA Immediate - writeByte(&memory, startingAddress + 0x01, testValue); // Zero value + immediateParams params = { + .opcode = OP_LDA_IM, .cycles = 2, + .startingAddress = 0x0100, .testValue = 0x00 + }; - uint cycles = 2; - execute(&cpu, &memory, &cycles); + setup_immediate(&cpu, &memory, ¶ms); + execute(&cpu, &memory, ¶ms.cycles); - CU_ASSERT_EQUAL(cpu.A, testValue); - CU_ASSERT_EQUAL(cycles, 0); - CU_ASSERT_EQUAL(cpu.PC, startingAddress + 0x02); + CU_ASSERT_EQUAL(cpu.A, params.testValue); + CU_ASSERT_EQUAL(params.cycles, 0); + CU_ASSERT_EQUAL(cpu.PC, params.startingAddress + 0x02); CU_ASSERT_TRUE(cpu.PS & ZERO_FLAG); CU_ASSERT_FALSE(cpu.PS & NEGATIVE_FLAG); } @@ -46,20 +41,17 @@ void test_lda_immediate_zero() { void test_lda_immediate_negative() { CPU cpu; Memory memory; - reset(&cpu, &memory); + immediateParams params = { + .opcode = OP_LDA_IM, .cycles = 2, + .startingAddress = 0x0100, .testValue = 0x80 + }; - word startingAddress = 0x0100; - byte testValue = 0x80; - cpu.PC = startingAddress; // Arbitrary position - writeByte(&memory, startingAddress, OP_LDA_IM); // Opcode for LDA Immediate - writeByte(&memory, startingAddress + 0x01, testValue); // Negative test value (128, which sets the negative flag in 8-bit) - - uint cycles = 2; - execute(&cpu, &memory, &cycles); + setup_immediate(&cpu, &memory, ¶ms); + execute(&cpu, &memory, ¶ms.cycles); - CU_ASSERT_EQUAL(cpu.A, testValue); - CU_ASSERT_EQUAL(cycles, 0); - CU_ASSERT_EQUAL(cpu.PC, startingAddress + 0x02); + CU_ASSERT_EQUAL(cpu.A, params.testValue); + CU_ASSERT_EQUAL(params.cycles, 0); + CU_ASSERT_EQUAL(cpu.PC, params.startingAddress + 0x02); CU_ASSERT_FALSE(cpu.PS & ZERO_FLAG); CU_ASSERT_TRUE(cpu.PS & NEGATIVE_FLAG); } diff --git a/tests/test_ldx.c b/tests/test_ldx.c index a380939..cf56756 100644 --- a/tests/test_ldx.c +++ b/tests/test_ldx.c @@ -1,23 +1,21 @@ #include "CUnit/Basic.h" #include "../src/6502.h" +#include "setup_tests.h" void test_ldx_immediate_positive() { CPU cpu; Memory memory; - reset(&cpu, &memory); - - word startingAddress = 0x0100; - byte testValue = 0x7F; - cpu.PC = startingAddress; // Arbitrary position - writeByte(&memory, startingAddress, OP_LDX_IM); // Opcode for LDX Immediate - writeByte(&memory, startingAddress + 0x01, testValue); // Positive test value that does not set the negative flag + immediateParams params = { + .opcode = OP_LDX_IM, .cycles = 2, + .startingAddress = 0x0100, .testValue = 0x7F + }; - uint cycles = 2; - execute(&cpu, &memory, &cycles); + setup_immediate(&cpu, &memory, ¶ms); + execute(&cpu, &memory, ¶ms.cycles); - CU_ASSERT_EQUAL(cpu.X, testValue); - CU_ASSERT_EQUAL(cycles, 0); - CU_ASSERT_EQUAL(cpu.PC, startingAddress + 0x02); + CU_ASSERT_EQUAL(cpu.X, params.testValue); + CU_ASSERT_EQUAL(params.cycles, 0); + CU_ASSERT_EQUAL(cpu.PC, params.startingAddress + 0x02); CU_ASSERT_FALSE(cpu.PS & ZERO_FLAG); CU_ASSERT_FALSE(cpu.PS & NEGATIVE_FLAG); } @@ -25,20 +23,17 @@ void test_ldx_immediate_positive() { void test_ldx_immediate_zero() { CPU cpu; Memory memory; - reset(&cpu, &memory); - - word startingAddress = 0x0100; - byte testValue = 0x00; - cpu.PC = startingAddress; // Arbitrary position - writeByte(&memory, startingAddress, OP_LDX_IM); // Opcode for LDX Immediate - writeByte(&memory, startingAddress + 0x01, testValue); // Zero test value that sets the zero flag + immediateParams params = { + .opcode = OP_LDX_IM, .cycles = 2, + .startingAddress = 0x0100, .testValue = 0x00 + }; - uint cycles = 2; - execute(&cpu, &memory, &cycles); + setup_immediate(&cpu, &memory, ¶ms); + execute(&cpu, &memory, ¶ms.cycles); - CU_ASSERT_EQUAL(cpu.X, testValue); - CU_ASSERT_EQUAL(cycles, 0); - CU_ASSERT_EQUAL(cpu.PC, startingAddress + 0x02); + CU_ASSERT_EQUAL(cpu.X, params.testValue); + CU_ASSERT_EQUAL(params.cycles, 0); + CU_ASSERT_EQUAL(cpu.PC, params.startingAddress + 0x02); CU_ASSERT_TRUE(cpu.PS & ZERO_FLAG); CU_ASSERT_FALSE(cpu.PS & NEGATIVE_FLAG); } @@ -46,20 +41,17 @@ void test_ldx_immediate_zero() { void test_ldx_immediate_negative() { CPU cpu; Memory memory; - reset(&cpu, &memory); - - word startingAddress = 0x0100; - byte testValue = 0x80; - cpu.PC = startingAddress; // Arbitrary position - writeByte(&memory, startingAddress, OP_LDX_IM); // Opcode for LDX Immediate - writeByte(&memory, startingAddress + 0x01, testValue); // Negative test value that sets the negative flag + immediateParams params = { + .opcode = OP_LDX_IM, .cycles = 2, + .startingAddress = 0x0100, .testValue = 0x80 + }; - uint cycles = 2; - execute(&cpu, &memory, &cycles); + setup_immediate(&cpu, &memory, ¶ms); + execute(&cpu, &memory, ¶ms.cycles); - CU_ASSERT_EQUAL(cpu.X, testValue); - CU_ASSERT_EQUAL(cycles, 0); - CU_ASSERT_EQUAL(cpu.PC, startingAddress + 0x02); + CU_ASSERT_EQUAL(cpu.X, params.testValue); + CU_ASSERT_EQUAL(params.cycles, 0); + CU_ASSERT_EQUAL(cpu.PC, params.startingAddress + 0x02); CU_ASSERT_FALSE(cpu.PS & ZERO_FLAG); CU_ASSERT_TRUE(cpu.PS & NEGATIVE_FLAG); } diff --git a/tests/test_ldy.c b/tests/test_ldy.c index f866947..c965fb4 100644 --- a/tests/test_ldy.c +++ b/tests/test_ldy.c @@ -1,23 +1,21 @@ #include "CUnit/Basic.h" #include "../src/6502.h" +#include "setup_tests.h" void test_ldy_immediate_positive() { CPU cpu; Memory memory; - reset(&cpu, &memory); + immediateParams params = { + .opcode = OP_LDY_IM, .cycles = 2, + .startingAddress = 0x0100, .testValue = 0x7F + }; - word startingAddress = 0x0100; - byte testValue = 0x7F; - cpu.PC = startingAddress; - writeByte(&memory, startingAddress, OP_LDY_IM); - writeByte(&memory, startingAddress + 0x01, testValue); + setup_immediate(&cpu, &memory, ¶ms); + execute(&cpu, &memory, ¶ms.cycles); - uint cycles = 2; - execute(&cpu, &memory, &cycles); - - CU_ASSERT_EQUAL(cpu.Y, testValue); - CU_ASSERT_EQUAL(cycles, 0); - CU_ASSERT_EQUAL(cpu.PC, startingAddress + 0x02); + CU_ASSERT_EQUAL(cpu.Y, params.testValue); + CU_ASSERT_EQUAL(params.cycles, 0); + CU_ASSERT_EQUAL(cpu.PC, params.startingAddress + 0x02); CU_ASSERT_FALSE(cpu.PS & ZERO_FLAG); CU_ASSERT_FALSE(cpu.PS & NEGATIVE_FLAG); } @@ -25,20 +23,17 @@ void test_ldy_immediate_positive() { void test_ldy_immediate_zero() { CPU cpu; Memory memory; - reset(&cpu, &memory); + immediateParams params = { + .opcode = OP_LDY_IM, .cycles = 2, + .startingAddress = 0x0100, .testValue = 0x00 + }; - word startingAddress = 0x0100; - byte testValue = 0x00; - cpu.PC = startingAddress; - writeByte(&memory, startingAddress, OP_LDY_IM); - writeByte(&memory, startingAddress + 0x01, testValue); + setup_immediate(&cpu, &memory, ¶ms); + execute(&cpu, &memory, ¶ms.cycles); - uint cycles = 2; - execute(&cpu, &memory, &cycles); - - CU_ASSERT_EQUAL(cpu.Y, testValue); - CU_ASSERT_EQUAL(cycles, 0); - CU_ASSERT_EQUAL(cpu.PC, startingAddress + 0x02); + CU_ASSERT_EQUAL(cpu.Y, params.testValue); + CU_ASSERT_EQUAL(params.cycles, 0); + CU_ASSERT_EQUAL(cpu.PC, params.startingAddress + 0x02); CU_ASSERT_TRUE(cpu.PS & ZERO_FLAG); CU_ASSERT_FALSE(cpu.PS & NEGATIVE_FLAG); } @@ -46,20 +41,17 @@ void test_ldy_immediate_zero() { void test_ldy_immediate_negative() { CPU cpu; Memory memory; - reset(&cpu, &memory); + immediateParams params = { + .opcode = OP_LDY_IM, .cycles = 2, + .startingAddress = 0x0100, .testValue = 0x80 + }; - word startingAddress = 0x0100; - byte testValue = 0x80; - cpu.PC = startingAddress; - writeByte(&memory, startingAddress, OP_LDY_IM); - writeByte(&memory, startingAddress + 0x01, testValue); - - uint cycles = 2; - execute(&cpu, &memory, &cycles); + setup_immediate(&cpu, &memory, ¶ms); + execute(&cpu, &memory, ¶ms.cycles); - CU_ASSERT_EQUAL(cpu.Y, testValue); - CU_ASSERT_EQUAL(cycles, 0); - CU_ASSERT_EQUAL(cpu.PC, startingAddress + 0x02); + CU_ASSERT_EQUAL(cpu.Y, params.testValue); + CU_ASSERT_EQUAL(params.cycles, 0); + CU_ASSERT_EQUAL(cpu.PC, params.startingAddress + 0x02); CU_ASSERT_FALSE(cpu.PS & ZERO_FLAG); CU_ASSERT_TRUE(cpu.PS & NEGATIVE_FLAG); }