From 1b9024373897a1c939a2af5fcef4d7f3cd9d4b1c Mon Sep 17 00:00:00 2001 From: Sam M W Date: Thu, 13 Apr 2023 10:23:55 +0100 Subject: [PATCH] implementation of RTS --- src/cpu.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/cpu.rs b/src/cpu.rs index a2a0d0c..3cec77d 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -409,11 +409,13 @@ impl CPU { } (Instruction::PLA, OpInput::UseImplied) => { // Pull accumulator + self.pull_from_stack(); let val: u8 = self.pull_from_stack(); self.registers.accumulator = val as i8; } (Instruction::PLP, OpInput::UseImplied) => { // Pull status + self.pull_from_stack(); let val: u8 = self.pull_from_stack(); // The `truncate` here won't do anything because we have a // constant for the single unused flags bit. This probably @@ -443,6 +445,12 @@ impl CPU { CPU::::rotate_right_with_flags(&mut operand, &mut self.registers.status); self.memory.set_byte(addr, operand); } + (Instruction::RTS, OpInput::UseImplied) => { + self.pull_from_stack(); + let pcl: u8 = self.pull_from_stack(); + let pch: u8 = self.fetch_from_stack(); + self.registers.program_counter = (((pch as u16) << 8) | pcl as u16).wrapping_add(1); + } (Instruction::SBC, OpInput::UseImmediate(val)) => { debug!("subtract with carry immediate: {}", val); @@ -921,6 +929,12 @@ impl CPU { self.registers.stack_pointer.increment(); out } + + fn fetch_from_stack(&mut self) -> u8 { + // gets the next value on the stack but does not update the stack pointer + let addr = self.registers.stack_pointer.to_u16(); + self.memory.get_byte(addr) + } } impl core::fmt::Debug for CPU {