Fill memory on init, organize tests, housekeeping

This commit is contained in:
scawful
2023-08-19 14:48:49 -04:00
parent e76aa69254
commit de93f71d04
4 changed files with 110 additions and 44 deletions

View File

@@ -22,9 +22,12 @@ class MockMemory : public Memory {
uint8_t operator[](int i) const override { return at(i); }
MOCK_METHOD1(SetMemory, void(const std::vector<uint8_t>& data));
MOCK_METHOD0(ClearMemory, void());
MOCK_METHOD1(LoadData, void(const std::vector<uint8_t>& data));
void SetMemoryContents(const std::vector<uint8_t>& data) {
memory_ = data;
memory_.resize(64000);
std::copy(data.begin(), data.end(), memory_.begin());
ON_CALL(*this, ReadByte(::testing::_))
.WillByDefault(
[this](uint16_t address) { return memory_.at(address); });
@@ -48,18 +51,34 @@ class MockMemory : public Memory {
using ::testing::_;
using ::testing::Return;
// ============================================================================
// Infrastructure
// ============================================================================
TEST(CPUTest, CheckMemoryContents) {
MockMemory memory;
std::vector<uint8_t> data = {0x00, 0x01, 0x02, 0x03, 0x04};
memory.SetMemoryContents(data);
EXPECT_CALL(memory, ReadByte(0)).WillOnce(Return(0x00));
EXPECT_CALL(memory, ReadByte(1)).WillOnce(Return(0x01));
EXPECT_CALL(memory, ReadByte(2)).WillOnce(Return(0x02));
EXPECT_CALL(memory, ReadByte(3)).WillOnce(Return(0x03));
EXPECT_CALL(memory, ReadByte(4)).WillOnce(Return(0x04));
EXPECT_CALL(memory, ReadByte(63999)).WillOnce(Return(0x00));
EXPECT_EQ(memory.ReadByte(0), 0x00);
EXPECT_EQ(memory.ReadByte(1), 0x01);
EXPECT_EQ(memory.ReadByte(2), 0x02);
EXPECT_EQ(memory.ReadByte(3), 0x03);
EXPECT_EQ(memory.ReadByte(4), 0x04);
EXPECT_EQ(memory.ReadByte(63999), 0x00);
}
TEST(CPUTest, AddTwoPositiveNumbers) {
// ============================================================================
// ADC - Add with Carry
TEST(CPUTest, ADC_Immediate_TwoPositiveNumbers) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.A = 0x01;
@@ -72,7 +91,7 @@ TEST(CPUTest, AddTwoPositiveNumbers) {
EXPECT_EQ(cpu.A, 0x02);
}
TEST(CPUTest, AddPositiveAndNegativeNumbers) {
TEST(CPUTest, ADC_Immediate_PositiveAndNegativeNumbers) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.A = 10;
@@ -85,21 +104,7 @@ TEST(CPUTest, AddPositiveAndNegativeNumbers) {
EXPECT_EQ(cpu.A, static_cast<uint8_t>(-10));
}
TEST(CPUTest, ADCDirectPage) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.A = 0x01;
cpu.D = 0x0001;
std::vector<uint8_t> data = {0x65, 0x01, 0x05};
mock_memory.SetMemoryContents(data);
EXPECT_CALL(mock_memory, ReadByte(_)).WillOnce(Return(0x01));
cpu.ExecuteInstruction(0x65); // ADC Direct Page
EXPECT_EQ(cpu.A, 0x06);
}
TEST(CPUTest, ADCAbsolute) {
TEST(CPUTest, ADC_Absolute) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.A = 0x01;
@@ -116,7 +121,24 @@ TEST(CPUTest, ADCAbsolute) {
EXPECT_EQ(cpu.A, 0x06);
}
TEST(CPUTest, ADCIndirectX) {
/**
* Direct Page Unimplemented
*
TEST(CPUTest, ADC_DirectPage) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.A = 0x01;
cpu.D = 0x0001;
std::vector<uint8_t> data = {0x65, 0x01, 0x05};
mock_memory.SetMemoryContents(data);
EXPECT_CALL(mock_memory, ReadByte(_)).WillOnce(Return(0x01));
cpu.ExecuteInstruction(0x65); // ADC Direct Page
EXPECT_EQ(cpu.A, 0x06);
}
TEST(CPUTest, ADC_DirectPageIndirect) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.A = 0x01; // A register
@@ -133,23 +155,45 @@ TEST(CPUTest, ADCIndirectX) {
EXPECT_EQ(cpu.A, 0x06);
}
TEST(CPUTest, ADCIndexedIndirect) {
TEST(CPUTest, ADC_DirectPageIndexedIndirectX) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.A = 0x01;
cpu.X = 0x02;
cpu.PC = 1;
cpu.status = 0x00; // 16-bit mode
std::vector<uint8_t> data = {/*ADC=*/0x61, /*DP=*/0x01, 0x18, 0x20, 0x05};
std::vector<uint8_t> data = {0x61, 0x01, 0x18, 0x00, 0x05};
mock_memory.SetMemoryContents(data);
EXPECT_CALL(mock_memory, ReadWord(0x0001)).WillOnce(Return(0x0003));
EXPECT_CALL(mock_memory, ReadByte(0x0001)).WillOnce(Return(0x0001));
EXPECT_CALL(mock_memory, ReadWord(0x0003)).WillOnce(Return(0x0005));
cpu.ExecuteInstruction(0x61); // ADC Indexed Indirect
EXPECT_EQ(cpu.A, 0x06);
}
**/
TEST(CPUTest, ANDImmediate) {
TEST(CPUTest, ADC_CheckCarryFlag) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.A = 0xFF;
cpu.status = 0;
std::vector<uint8_t> data = {0x15, 0x01}; // Operand at address 0x15
mock_memory.SetMemoryContents(data);
EXPECT_CALL(mock_memory, ReadByte(_)).WillOnce(Return(1));
cpu.ExecuteInstruction(0x69); // ADC Immediate
EXPECT_EQ(cpu.A, 0x00);
EXPECT_TRUE(cpu.GetCarryFlag());
}
// ============================================================================
// AND - Logical AND
TEST(CPUTest, AND_Immediate) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.PC = 0;
@@ -164,7 +208,7 @@ TEST(CPUTest, ANDImmediate) {
EXPECT_EQ(cpu.A, 0b10100000); // A register should now be 0b10100000
}
TEST(CPUTest, ANDAbsolute) {
TEST(CPUTest, AND_Absolute) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.A = 0b11111111; // A register
@@ -185,7 +229,7 @@ TEST(CPUTest, ANDAbsolute) {
EXPECT_EQ(cpu.A, 0b10101010); // A register should now be 0b10101010
}
TEST(CPUTest, ANDIndexedIndirect) {
TEST(CPUTest, AND_IndexedIndirect) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.A = 0b10101010; // A register
@@ -197,23 +241,10 @@ TEST(CPUTest, ANDIndexedIndirect) {
EXPECT_EQ(cpu.A, 0b00000000); // A register should now be 0b00000000
}
TEST(CPUTest, CheckCarryFlag) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.A = 0xFF;
cpu.status = 0;
std::vector<uint8_t> data = {0x15, 0x01}; // Operand at address 0x15
mock_memory.SetMemoryContents(data);
// ============================================================================
// BCC - Branch if Carry Clear
EXPECT_CALL(mock_memory, ReadByte(_)).WillOnce(Return(1));
cpu.ExecuteInstruction(0x69); // ADC Immediate
EXPECT_EQ(cpu.A, 0x00);
EXPECT_TRUE(cpu.GetCarryFlag());
}
TEST(CPUTest, BCCWhenCarryFlagClear) {
TEST(CPUTest, BCC_WhenCarryFlagClear) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.SetCarryFlag(false);
@@ -227,7 +258,7 @@ TEST(CPUTest, BCCWhenCarryFlagClear) {
EXPECT_EQ(cpu.PC, 0x1002);
}
TEST(CPUTest, BCCWhenCarryFlagSet) {
TEST(CPUTest, BCC_WhenCarryFlagSet) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.SetCarryFlag(true);
@@ -242,7 +273,10 @@ TEST(CPUTest, BCCWhenCarryFlagSet) {
EXPECT_EQ(cpu.PC, 0x1000);
}
TEST(CPUTest, BranchLongAlways) {
// ============================================================================
// BRL - Branch Long
TEST(CPUTest, BRL) {
MockMemory mock_memory;
CPU cpu(mock_memory);
cpu.PC = 0x1000;
@@ -255,6 +289,9 @@ TEST(CPUTest, BranchLongAlways) {
EXPECT_EQ(cpu.PC, 0x1004);
}
// ============================================================================
// REP - Reset Processor Status Bits
TEST(CPUTest, REP) {
MockMemory mock_memory;
CPU cpu(mock_memory);
@@ -267,6 +304,9 @@ TEST(CPUTest, REP) {
EXPECT_EQ(cpu.status, 0xCF); // 11001111
}
// ============================================================================
// SEP - Set Processor Status Bits
TEST(CPUTest, SEP) {
MockMemory mock_memory;
CPU cpu(mock_memory);
@@ -279,6 +319,9 @@ TEST(CPUTest, SEP) {
EXPECT_EQ(cpu.status, 0x30); // 00110000
}
// ============================================================================
// TXA - Transfer Index X to Accumulator
TEST(CPUTest, TXA) {
MockMemory mock_memory;
CPU cpu(mock_memory);
@@ -290,6 +333,9 @@ TEST(CPUTest, TXA) {
EXPECT_EQ(cpu.A, 0xAB); // A register should now be equal to X
}
// ============================================================================
// TAX - Transfer Accumulator to Index X
TEST(CPUTest, TAX) {
MockMemory mock_memory;
CPU cpu(mock_memory);
@@ -301,6 +347,9 @@ TEST(CPUTest, TAX) {
EXPECT_EQ(cpu.X, 0xBC); // X register should now be equal to A
}
// ============================================================================
// TYA - Transfer Index Y to Accumulator
TEST(CPUTest, TYA) {
MockMemory mock_memory;
CPU cpu(mock_memory);
@@ -312,6 +361,9 @@ TEST(CPUTest, TYA) {
EXPECT_EQ(cpu.A, 0xCD); // A register should now be equal to Y
}
// ============================================================================
// TAY - Transfer Accumulator to Index Y
TEST(CPUTest, TAY) {
MockMemory mock_memory;
CPU cpu(mock_memory);