1
0
mirror of https://github.com/mre/mos6502.git synced 2024-12-22 12:29:31 +00:00

implementation of RTS

This commit is contained in:
Sam M W 2023-04-13 10:23:55 +01:00
parent 7df9160e32
commit 1b90243738

View File

@ -409,11 +409,13 @@ impl<M: Bus> CPU<M> {
} }
(Instruction::PLA, OpInput::UseImplied) => { (Instruction::PLA, OpInput::UseImplied) => {
// Pull accumulator // Pull accumulator
self.pull_from_stack();
let val: u8 = self.pull_from_stack(); let val: u8 = self.pull_from_stack();
self.registers.accumulator = val as i8; self.registers.accumulator = val as i8;
} }
(Instruction::PLP, OpInput::UseImplied) => { (Instruction::PLP, OpInput::UseImplied) => {
// Pull status // Pull status
self.pull_from_stack();
let val: u8 = self.pull_from_stack(); let val: u8 = self.pull_from_stack();
// The `truncate` here won't do anything because we have a // The `truncate` here won't do anything because we have a
// constant for the single unused flags bit. This probably // constant for the single unused flags bit. This probably
@ -443,6 +445,12 @@ impl<M: Bus> CPU<M> {
CPU::<M>::rotate_right_with_flags(&mut operand, &mut self.registers.status); CPU::<M>::rotate_right_with_flags(&mut operand, &mut self.registers.status);
self.memory.set_byte(addr, operand); 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)) => { (Instruction::SBC, OpInput::UseImmediate(val)) => {
debug!("subtract with carry immediate: {}", val); debug!("subtract with carry immediate: {}", val);
@ -921,6 +929,12 @@ impl<M: Bus> CPU<M> {
self.registers.stack_pointer.increment(); self.registers.stack_pointer.increment();
out 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<M: Bus> core::fmt::Debug for CPU<M> { impl<M: Bus> core::fmt::Debug for CPU<M> {