RET didn't decrement the stack pointer resulting in unintended overflows.

Turns out there _was_ a reason the program was overflowing and it was my fault.
This commit is contained in:
abbie 2022-02-20 18:54:15 +00:00
parent ba22aa482c
commit b0faea8079
No known key found for this signature in database
GPG key ID: 04DDE463F9200F87

View file

@ -48,6 +48,9 @@ impl Cpu {
fn incrementsp(&mut self) { fn incrementsp(&mut self) {
self.sp = self.sp + 1 self.sp = self.sp + 1
} }
fn decrementsp(&mut self) {
self.sp = self.sp - 1
}
fn stackpush(&mut self) { fn stackpush(&mut self) {
if self.stack.len() == 15 { if self.stack.len() == 15 {
println!("Stack overflow in emulated CPU! Bailing out!"); println!("Stack overflow in emulated CPU! Bailing out!");
@ -248,6 +251,7 @@ fn execute(opcode: u16, instruction: Instruction, mut cpu: Cpu) -> Cpu {
if instruction.name == "RET" { if instruction.name == "RET" {
let returnaddress = cpu.stackpop(); let returnaddress = cpu.stackpop();
cpu.decrementsp();
cpu.setpc(returnaddress); cpu.setpc(returnaddress);
} }
else if instruction.name == "JP_ADDR" { cpu.setpc(args[0] as u16); } else if instruction.name == "JP_ADDR" { cpu.setpc(args[0] as u16); }