Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tests/setup_tests.c
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 13 additions & 0 deletions tests/setup_tests.h
Original file line number Diff line number Diff line change
@@ -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
64 changes: 28 additions & 36 deletions tests/test_lda.c
Original file line number Diff line number Diff line change
@@ -1,65 +1,57 @@
#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, &params);
execute(&cpu, &memory, &params.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);
}

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, &params);
execute(&cpu, &memory, &params.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);
}

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, &params);
execute(&cpu, &memory, &params.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);
}
Expand Down
64 changes: 28 additions & 36 deletions tests/test_ldx.c
Original file line number Diff line number Diff line change
@@ -1,65 +1,57 @@
#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, &params);
execute(&cpu, &memory, &params.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);
}

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, &params);
execute(&cpu, &memory, &params.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);
}

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, &params);
execute(&cpu, &memory, &params.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);
}
Expand Down
64 changes: 28 additions & 36 deletions tests/test_ldy.c
Original file line number Diff line number Diff line change
@@ -1,65 +1,57 @@
#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, &params);
execute(&cpu, &memory, &params.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);
}

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, &params);
execute(&cpu, &memory, &params.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);
}

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, &params);
execute(&cpu, &memory, &params.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);
}
Expand Down