Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use correct timings for absolute indexed addressing mode #210

Merged
merged 1 commit into from
Jan 26, 2020
Merged
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
7 changes: 3 additions & 4 deletions core/src/mos6502.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,11 +927,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 @@ -932,7 +939,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 @@ -950,7 +958,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 @@ -970,7 +979,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 @@ -1259,7 +1269,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 @@ -1277,7 +1288,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 @@ -1413,7 +1425,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 @@ -1432,7 +1445,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 @@ -2395,7 +2409,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 @@ -2414,7 +2429,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 @@ -2792,7 +2808,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 @@ -2811,7 +2828,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 @@ -2832,7 +2850,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 @@ -3050,7 +3069,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 @@ -3068,7 +3088,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 @@ -3086,7 +3107,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