diff --git a/Core/Cpu.Instructions.cpp b/Core/Cpu.Instructions.cpp index c2f7323..52701c5 100644 --- a/Core/Cpu.Instructions.cpp +++ b/Core/Cpu.Instructions.cpp @@ -402,10 +402,10 @@ void Cpu::JSR() void Cpu::RTI() { if(_state.EmulationMode) { - _state.PS = PopByte(); //TODO: incorrect + SetPS(PopByte()); _state.PC = PopWord(); } else { - _state.PS = PopByte(); //TODO: incorrect + SetPS(PopByte()); _state.PC = PopWord(); _state.K = PopByte(); } @@ -699,9 +699,9 @@ void Cpu::PLP() { //"For PLP, (all of) the flags are pulled from the stack. Note that when the e flag is 1, the m and x flag are forced to 1, so after the PLP, both flags will still be 1 no matter what value is pulled from the stack." if(_state.EmulationMode) { - _state.PS = PopByte() | ProcFlags::MemoryMode8 | ProcFlags::IndexMode8; + SetPS(PopByte() | ProcFlags::MemoryMode8 | ProcFlags::IndexMode8); } else { - _state.PS = PopByte(); + SetPS(PopByte()); } } @@ -971,9 +971,7 @@ void Cpu::XCE() _state.EmulationMode = carry; if(_state.EmulationMode) { - SetFlags(ProcFlags::IndexMode8 | ProcFlags::MemoryMode8); - _state.Y &= 0xFF; - _state.X &= 0xFF; + SetPS(_state.PS | ProcFlags::IndexMode8 | ProcFlags::MemoryMode8); _state.SP = 0x100 | (_state.SP & 0xFF); } } diff --git a/Core/Cpu.cpp b/Core/Cpu.cpp index c43cb0c..d8fa8d2 100644 --- a/Core/Cpu.cpp +++ b/Core/Cpu.cpp @@ -326,6 +326,16 @@ void Cpu::SetSP(uint16_t sp) } } +void Cpu::SetPS(uint8_t ps) +{ + _state.PS = ps; + if(CheckFlag(ProcFlags::IndexMode8)) { + //Truncate X/Y when 8-bit indexes are enabled + _state.Y &= 0xFF; + _state.X &= 0xFF; + } +} + void Cpu::SetRegister(uint8_t ®, uint8_t value) { SetZeroNegativeFlags(value); diff --git a/Core/Cpu.h b/Core/Cpu.h index fafc93b..7173b7c 100644 --- a/Core/Cpu.h +++ b/Core/Cpu.h @@ -54,6 +54,7 @@ private: uint32_t FetchEffectiveAddress(); void SetSP(uint16_t sp); + void SetPS(uint8_t ps); void SetRegister(uint8_t ®, uint8_t value); void SetRegister(uint16_t ®, uint16_t value, bool eightBitMode);