Skip to content

Commit

Permalink
Use correct timings for absolute indexed addressing mode
Browse files Browse the repository at this point in the history
  • Loading branch information
johnor committed Jan 26, 2020
1 parent f88ccb6 commit 6fae758
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
7 changes: 3 additions & 4 deletions core/src/mos6502.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,11 +929,10 @@ Pipeline Mos6502::create_absolute_indexed_addressing_steps(
const uint8_t *index_reg,
bool is_write) {
Pipeline result;
result.push([=]() { ++registers_->pc; });
result.push([=]() { tmp_ = mmu_->read_byte(registers_->pc++); });
result.push([=]() {
++registers_->pc;
const uint16_t abs_address =
mmu_->read_word(registers_->pc - static_cast<uint16_t>(2));
const uint16_t address_high = mmu_->read_byte(registers_->pc++) << 8u;
const uint16_t abs_address = address_high | tmp_;
const uint8_t offset = *index_reg;

is_crossing_page_boundary_ = cross_page(abs_address, offset);
Expand Down
66 changes: 44 additions & 22 deletions core/test/src/test_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ class CpuTest : public ::testing::Test {
registers.p |= Z_FLAG;
expected.pc += 2;

EXPECT_CALL(mmu, read_word(registers.pc + 1)).WillOnce(Return(0x4567));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x67));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x45));
EXPECT_CALL(mmu, read_byte(0x4567)).WillOnce(Return(127));

step_execution(4);
Expand All @@ -338,7 +339,8 @@ class CpuTest : public ::testing::Test {

expected.pc += 2;

EXPECT_CALL(mmu, read_word(registers.pc + 1)).WillOnce(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0x10)).WillOnce(Return(0x07));

step_execution(4);
Expand All @@ -354,7 +356,8 @@ class CpuTest : public ::testing::Test {

expected.pc += 2;

EXPECT_CALL(mmu, read_word(registers.pc + 1)).WillOnce(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0xAB - 0x0100))
.WillOnce(Return(0xDEAD));
EXPECT_CALL(mmu, read_byte(0x5678 + 0xAB)).WillOnce(Return(0x07));
Expand All @@ -374,7 +377,8 @@ class CpuTest : public ::testing::Test {
expected.pc += 2;

*target_reg = 0x37;
EXPECT_CALL(mmu, read_word(registers.pc + 1)).WillOnce(Return(0x0100));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x00));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x01));
EXPECT_CALL(mmu, read_byte(0x0100 + *index_reg))
.WillOnce(Return(*target_reg));

Expand All @@ -393,7 +397,8 @@ class CpuTest : public ::testing::Test {
expected.pc += 2;

*target_reg = 0x37;
EXPECT_CALL(mmu, read_word(registers.pc + 1)).WillOnce(Return(0x01FF));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0xFF));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x01));

EXPECT_CALL(mmu, read_byte(0x00FF + *index_reg));
EXPECT_CALL(mmu, read_byte(0x01FF + *index_reg))
Expand All @@ -415,7 +420,8 @@ class CpuTest : public ::testing::Test {
stage_instruction(instruction);
expected.pc += 2;

EXPECT_CALL(mmu, read_word(registers.pc + 1)).WillOnce(Return(0x0100));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x00));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x01));
EXPECT_CALL(mmu, read_byte(0x0100 + 0x42)).WillOnce(Return(0));

*target_reg = 0;
Expand All @@ -436,7 +442,8 @@ class CpuTest : public ::testing::Test {
stage_instruction(instruction);
expected.pc += 2;

EXPECT_CALL(mmu, read_word(registers.pc + 1)).WillOnce(Return(0x0100));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x00));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x01));
EXPECT_CALL(mmu, read_byte(0x0100 + 0x42)).WillOnce(Return(230));

*target_reg = 230;
Expand Down Expand Up @@ -934,7 +941,8 @@ TEST_F(CpuTest, and_absx_without_page_crossing) {
expected.pc += 2;
expected.a = 0b00001010;

EXPECT_CALL(mmu, read_word(0x4322)).WillOnce(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0x10)).WillOnce(Return(0b00001111));

step_execution(4);
Expand All @@ -952,7 +960,8 @@ TEST_F(CpuTest, and_absx_with_page_crossing) {
expected.pc += 2;
expected.a = 0b00001010;

EXPECT_CALL(mmu, read_word(0x4322)).WillOnce(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0xAB - 0x0100))
.WillOnce(Return(0xDEAD));
EXPECT_CALL(mmu, read_byte(0x5678 + 0xAB)).WillOnce(Return(0b00001111));
Expand All @@ -972,7 +981,8 @@ TEST_F(CpuTest, and_absy_without_page_crossing) {
expected.pc += 2;
expected.a = 0b00001010;

EXPECT_CALL(mmu, read_word(0x4322)).WillOnce(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0x10)).WillOnce(Return(0b00001111));

step_execution(4);
Expand Down Expand Up @@ -1262,7 +1272,8 @@ TEST_F(CpuTest, adc_absx_no_carry_or_overflow_no_pagecrossing) {
expected.a = 0x71;
expected.pc += 2;

EXPECT_CALL(mmu, read_word(0x4322)).WillOnce(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0x10)).WillOnce(Return(0x21));

step_execution(4);
Expand All @@ -1280,7 +1291,8 @@ TEST_F(CpuTest, adc_absy_no_carry_or_overflow_with_pagecrossing) {
expected.a = 0x71;
expected.pc += 2;

EXPECT_CALL(mmu, read_word(0x4322)).WillOnce(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0xAB - 0x0100))
.WillOnce(Return(0xDEAD));
EXPECT_CALL(mmu, read_byte(0x5678 + 0xAB)).WillOnce(Return(0x21));
Expand Down Expand Up @@ -1416,7 +1428,8 @@ TEST_F(CpuTest, sbc_absx_no_carry_or_overflow_no_pagecrossing) {
expected.a = 0xE0;
expected.pc += 2;

EXPECT_CALL(mmu, read_word(0x4322)).WillOnce(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0x10)).WillOnce(Return(0xF0));

step_execution(4);
Expand All @@ -1435,7 +1448,8 @@ TEST_F(CpuTest, sbc_absy_no_carry_or_overflow_with_pagecrossing) {
expected.a = 0xE0;
expected.pc += 2;

EXPECT_CALL(mmu, read_word(0x4322)).WillOnce(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0xAB - 0x0100))
.WillOnce(Return(0xDEAD));
EXPECT_CALL(mmu, read_byte(0x5678 + 0xAB)).WillOnce(Return(0xF0));
Expand Down Expand Up @@ -2398,7 +2412,8 @@ TEST_F(CpuTest, sta_abs_x_indexed) {

expected.pc += 2;

EXPECT_CALL(mmu, read_word(0x4322)).WillOnce(Return(0x1234));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x34));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x12));
EXPECT_CALL(mmu, read_byte(0x1234 + 0xED - 0x0100))
.WillOnce(Return(0xDEAD));
EXPECT_CALL(mmu, write_byte(0x1234 + 0xED, 0x07));
Expand All @@ -2417,7 +2432,8 @@ TEST_F(CpuTest, sta_abs_y_indexed) {

expected.pc += 2;

EXPECT_CALL(mmu, read_word(0x4322)).WillOnce(Return(0x1234));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x34));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x12));
EXPECT_CALL(mmu, read_byte(0x1234 + 0xED - 0x0100))
.WillOnce(Return(0xDEAD));
EXPECT_CALL(mmu, write_byte(0x1234 + 0xED, 0x07));
Expand Down Expand Up @@ -2795,7 +2811,8 @@ TEST_F(CpuTest, eor_absx_without_page_crossing) {
expected.pc += 2;
expected.a = 0b00110011;

EXPECT_CALL(mmu, read_word(0x4322)).WillOnce(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0x10)).WillOnce(Return(0b11001111));

step_execution(4);
Expand All @@ -2814,7 +2831,8 @@ TEST_F(CpuTest, eor_absx_with_page_crossing) {
expected.a = 0b00000000;
expected.p = Z_FLAG;

EXPECT_CALL(mmu, read_word(0x4322)).WillOnce(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0xAB - 0x0100))
.WillOnce(Return(0xDEAD));
EXPECT_CALL(mmu, read_byte(0x5678 + 0xAB)).WillOnce(Return(0b11110000));
Expand All @@ -2835,7 +2853,8 @@ TEST_F(CpuTest, eor_absy_without_page_crossing) {
expected.a = 0b10011001;
expected.p = N_FLAG;

EXPECT_CALL(mmu, read_word(0x4322)).WillOnce(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0x10)).WillOnce(Return(0b10100101));

step_execution(4);
Expand Down Expand Up @@ -3053,7 +3072,8 @@ TEST_F(CpuTest, ora_absy_without_page_crossing) {
expected.a = 0b11111100;
expected.p = N_FLAG;

ON_CALL(mmu, read_word(0x4322)).WillByDefault(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0x10)).WillOnce(Return(0b11110000));

step_execution(4);
Expand All @@ -3071,7 +3091,8 @@ TEST_F(CpuTest, ora_absx_without_page_crossing) {
expected.pc += 2;
expected.a = 0b00110011;

ON_CALL(mmu, read_word(0x4322)).WillByDefault(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0x10)).WillOnce(Return(0b00100011));

step_execution(4);
Expand All @@ -3089,7 +3110,8 @@ TEST_F(CpuTest, ora_absx_with_page_crossing) {
expected.a = 0b10000001;
expected.p = N_FLAG;

ON_CALL(mmu, read_word(0x4322)).WillByDefault(Return(0x5678));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0x78));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0x56));
EXPECT_CALL(mmu, read_byte(0x5678 + 0xAB - 0x0100))
.WillOnce(Return(0xDEAD));
EXPECT_CALL(mmu, read_byte(0x5678 + 0xAB)).WillOnce(Return(0b00000001));
Expand Down

0 comments on commit 6fae758

Please sign in to comment.