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:
parent
ba22aa482c
commit
b0faea8079
1 changed files with 4 additions and 0 deletions
|
@ -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); }
|
||||||
|
|
Reference in a new issue