From b566265dbb5e95eabdba0c1e9c5a80d0a09fd1f3 Mon Sep 17 00:00:00 2001 From: Sam M W Date: Sat, 30 Jan 2021 15:11:02 +0000 Subject: [PATCH] Don't panic on stack over- and underflow --- src/registers.rs | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/registers.rs b/src/registers.rs index e28fe32..92caa57 100644 --- a/src/registers.rs +++ b/src/registers.rs @@ -26,7 +26,7 @@ // POSSIBILITY OF SUCH DAMAGE. use address::{Address, AddressDiff}; -use memory::{STACK_ADDRESS_LO, STACK_ADDRESS_HI}; +use memory::{STACK_ADDRESS_HI, STACK_ADDRESS_LO}; // Useful for constructing Status instances #[derive(Copy, Clone)] @@ -88,15 +88,18 @@ impl Status { }) } - pub fn new(StatusArgs { negative, - overflow, - unused, - brk, - decimal_mode, - disable_interrupts, - zero, - carry }: StatusArgs) -> Status - { + pub fn new( + StatusArgs { + negative, + overflow, + unused, + brk, + decimal_mode, + disable_interrupts, + zero, + carry, + }: StatusArgs, + ) -> Status { let mut out = Status::empty(); if negative { @@ -151,11 +154,11 @@ impl StackPointer { // JAM: FIXME: Should we prevent overflow here? What would a 6502 do? pub fn decrement(&mut self) { - self.0 -= 1; + self.0 = self.0.wrapping_sub(1); } pub fn increment(&mut self) { - self.0 += 1; + self.0 = self.0.wrapping_add(1); } } @@ -169,6 +172,12 @@ pub struct Registers { pub status: Status, } +impl Default for Registers { + fn default() -> Self { + Self::new() + } +} + impl Registers { pub fn new() -> Registers { // TODO akeeton: Revisit these defaults.