From afb90fee062b0a80f3e685517381650ad0419a27 Mon Sep 17 00:00:00 2001 From: PalindromicBreadLoaf Date: Thu, 24 Jul 2025 08:36:37 -0400 Subject: [PATCH] Implement a few stack instructions --- src/cpu.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cpu.h | 6 ++++++ 2 files changed, 68 insertions(+) diff --git a/src/cpu.cpp b/src/cpu.cpp index 0f1db53..58f1472 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -596,9 +596,18 @@ void CPU::ExecuteInstruction() { case 0x2B: PLD(); break; // PLD case 0x4B: PHK(); break; // PHK + // Push Effective Instructions + case 0xF4: PEA(); break; //PEA + case 0xD4: PEI(); break; //PEI + case 0x62: PER(); break; //PER + + // REP - Reset Status Bits + case 0xC2: REP(); break; + // More Subroutines case 0x60: RTS(); break; // RTS case 0x6B: RTL(); break; // RTL + case 0x40: RTI(); break; // RTI // ROL - Rotate Left case 0x2A: ROL_Accumulator(); break; // ROL A @@ -4099,4 +4108,57 @@ void CPU::ROR_DirectPageX() { } if (D & 0xFF) cycles++; +} + +void CPU::PEA() { + const uint16_t address = ReadWord(PC); + PC += 2; + + PushWord(address); + cycles += 5; +} + +void CPU::PEI() { + const uint8_t offset = ReadByte(PC++); + const uint32_t indirect_addr = D + offset; + + const uint16_t effective_addr = ReadWord(indirect_addr); + + PushWord(effective_addr); + cycles += 6; + + if (D & 0xFF) cycles++; +} + +void CPU::PER() { + const auto displacement = static_cast(ReadWord(PC)); + PC += 2; + + const auto effective_addr = static_cast(PC + displacement); + + PushWord(effective_addr); + cycles += 6; +} + +void CPU::REP() { + const uint8_t mask = ReadByte(PC++); + + P &= ~mask; + + cycles += 3; +} + +void CPU::RTI() { + if (emulation_mode) { + P = PopByte(); + PC = PopWord(); + cycles += 7; + } else { + // Native mode + P = PopByte(); + const uint16_t pc_addr = PopWord(); + PB = PopByte(); + PC = (static_cast(PB) << 16) | pc_addr; + cycles += 6; + } } \ No newline at end of file diff --git a/src/cpu.h b/src/cpu.h index 411b715..346067a 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -327,6 +327,12 @@ public: void ROR_AbsoluteX(); void ROR_DirectPage(); void ROR_DirectPageX(); + + void PEA(); + void PEI(); + void PER(); + void REP(); + void RTI(); }; #endif //CPU_H