cpu.rs: Remove stack from main memory

This commit is contained in:
2023-04-17 06:34:48 -05:00
parent e842755d77
commit 45adf0a2b8
6 changed files with 25 additions and 37 deletions

View File

@@ -88,8 +88,6 @@ pub enum Region {
Program,
/// Screen buffer
Screen,
/// Stack space
Stack,
#[doc(hidden)]
/// Total number of named regions
Count,
@@ -104,7 +102,6 @@ impl Display for Region {
Region::Charset => "Charset",
Region::Program => "Program",
Region::Screen => "Screen",
Region::Stack => "Stack",
_ => "",
}
)
@@ -348,7 +345,7 @@ impl Bus {
#[cfg(feature = "drawille")]
{
use drawille::Canvas;
let mut canvas = Canvas::new(dbg!(width * 8), dbg!(height));
let mut canvas = Canvas::new(width * 8, height);
let width = width * 8;
screen
.iter()

View File

@@ -13,9 +13,9 @@ impl CPU {
match instruction {
// Core Chip-8 instructions
Insn::cls => self.clear_screen(bus),
Insn::ret => self.ret(bus),
Insn::ret => self.ret(),
Insn::jmp { A } => self.jump(A),
Insn::call { A } => self.call(A, bus),
Insn::call { A } => self.call(A),
Insn::seb { x, B } => self.skip_equals_immediate(x, B),
Insn::sneb { x, B } => self.skip_not_equals_immediate(x, B),
Insn::se { y, x } => self.skip_equals(x, y),
@@ -74,9 +74,8 @@ impl CPU {
}
/// |`00ee`| Returns from subroutine
#[inline(always)]
pub(super) fn ret(&mut self, bus: &impl ReadWrite<u16>) {
self.sp = self.sp.wrapping_add(2);
self.pc = bus.read(self.sp);
pub(super) fn ret(&mut self) {
self.pc = self.stack.pop().unwrap_or(0x200);
}
}
@@ -97,9 +96,8 @@ impl CPU {
impl CPU {
/// |`2aaa`| Pushes pc onto the stack, then jumps to a
#[inline(always)]
pub(super) fn call(&mut self, a: Adr, bus: &mut impl ReadWrite<u16>) {
bus.write(self.sp, self.pc);
self.sp = self.sp.wrapping_sub(2);
pub(super) fn call(&mut self, a: Adr) {
self.stack.push(self.pc);
self.pc = a;
}
}

View File

@@ -106,17 +106,14 @@ mod sys {
#[test]
fn ret() {
let test_addr = random::<u16>() & 0x7ff;
let (mut cpu, mut bus) = setup_environment();
let sp_orig = cpu.sp;
let (mut cpu, _) = setup_environment();
// Place the address on the stack
bus.write(cpu.sp.wrapping_add(2), test_addr);
cpu.stack.push(test_addr);
cpu.ret(&bus);
cpu.ret();
// Verify the current address is the address from the stack
assert_eq!(test_addr, cpu.pc);
// Verify the stack pointer has moved
assert!(dbg!(cpu.sp.wrapping_sub(sp_orig)) == 0x2);
}
}
@@ -132,9 +129,9 @@ mod cf {
let (mut cpu, _) = setup_environment();
// Test all valid addresses
for addr in 0x000..0xffe {
// Call an address
// Jump to an address
cpu.jump(addr);
// Verify the current address is the called address
// Verify the current address is the jump target address
assert_eq!(addr, cpu.pc);
}
}
@@ -143,15 +140,15 @@ mod cf {
#[test]
fn call() {
let test_addr = random::<u16>();
let (mut cpu, mut bus) = setup_environment();
let (mut cpu, _) = setup_environment();
// Save the current address
let curr_addr = cpu.pc;
// Call an address
cpu.call(test_addr, &mut bus);
cpu.call(test_addr);
// Verify the current address is the called address
assert_eq!(test_addr, cpu.pc);
// Verify the previous address was stored on the stack (sp+2)
let stack_addr: u16 = bus.read(cpu.sp.wrapping_add(2));
let stack_addr: u16 = cpu.stack.pop().expect("This should return test_addr");
assert_eq!(stack_addr, curr_addr);
}
@@ -595,7 +592,7 @@ mod math {
for reg in 0..=0xff {
let (x, y) = (reg & 0xf, reg >> 4);
// set the register under test to `word`
(cpu.v[x], cpu.v[y]) = dbg!(0, word);
(cpu.v[x], cpu.v[y]) = (0, word);
cpu.shift_left(x, y);