Skip to content

Commit

Permalink
Merge pull request #225 from johnor/fix-jsr-timings
Browse files Browse the repository at this point in the history
Use correct timings for Jsr
  • Loading branch information
johnor authored Feb 25, 2020
2 parents 34845eb + a1fcae9 commit 15ae6ae
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
24 changes: 14 additions & 10 deletions core/src/mos6502.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ void Mos6502::Stack::push_byte(uint8_t byte) {
mmu_->write_byte(ram_offset_ + registers_->sp--, byte);
}

void Mos6502::Stack::push_word(uint16_t word) {
mmu_->write_word(ram_offset_ + --registers_->sp, word);
--registers_->sp;
}

Mos6502::Mos6502(CpuRegisters *const registers, IMmu *const mmu)
: registers_(registers),
mmu_(mmu),
Expand Down Expand Up @@ -175,13 +170,22 @@ Pipeline Mos6502::parse_next_instruction() {
result.push([=]() { clear_flag(C_FLAG); });
break;
case Instruction::JsrAbsolute:
result.append(create_absolute_addressing_steps(
get_memory_access(state_.current_opcode->family)));
result.push([=]() { tmp_ = mmu_->read_byte(registers_->pc++); });
result.push([=]() {
/* Do nothing. */
/* Internal operation. Do nothing. */
});
result.push([=]() {
const auto pch = static_cast<uint8_t>(registers_->pc >> 8u);
stack_.push_byte(pch);
});
result.push([=]() {
const auto pcl = static_cast<uint8_t>(registers_->pc);
stack_.push_byte(pcl);
});
result.push([=]() {
const uint16_t pch = mmu_->read_byte(registers_->pc) << 8u;
registers_->pc = pch | tmp_;
});
result.push([=]() { stack_.push_word(--registers_->pc); });
result.push([=]() { registers_->pc = effective_address_; });
break;
case Instruction::BmiRelative:
result.append(create_branch_instruction(
Expand Down
2 changes: 0 additions & 2 deletions core/src/mos6502.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ class Mos6502 : public IMos6502 {
Stack(CpuRegisters *registers, IMmu *mmu);

uint8_t pop_byte();

void push_byte(uint8_t byte);
void push_word(uint16_t word);

private:
CpuRegisters *const registers_;
Expand Down
16 changes: 6 additions & 10 deletions core/test/src/test_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1179,19 +1179,15 @@ TEST_F(CpuTest, clc) {
}

TEST_F(CpuTest, jsr) {
registers.pc = 0x1234;
stage_instruction(JSR);
expected.pc = 0xDEAD;
expected.sp -= 2; // pc (2 bytes)

const uint8_t expected_pc_stack_addr =
expected.sp - static_cast<uint8_t>(1);
expected.sp -= 2; // 1 word

EXPECT_CALL(mmu, read_byte(registers.pc + 1)).WillOnce(Return(0xAD));
EXPECT_CALL(mmu, read_byte(registers.pc + 2)).WillOnce(Return(0xDE));

EXPECT_CALL(mmu,
write_word(
kStackOffset + expected_pc_stack_addr, registers.pc + 2));
EXPECT_CALL(mmu, read_byte(registers.pc + 1u)).WillOnce(Return(0xAD));
EXPECT_CALL(mmu, write_byte(kStackOffset + registers.sp, 0x12));
EXPECT_CALL(mmu, write_byte(kStackOffset + registers.sp - 1u, 0x36));
EXPECT_CALL(mmu, read_byte(registers.pc + 2u)).WillOnce(Return(0xDE));

step_execution(6);
EXPECT_EQ(expected, registers);
Expand Down

0 comments on commit 15ae6ae

Please sign in to comment.