diff --git a/src/register.rs b/src/register.rs index 3fb33f5..1803e5c 100644 --- a/src/register.rs +++ b/src/register.rs @@ -1,6 +1,14 @@ //! The register module Holds the basic [`Registers`] type and functions needed //! to access/manipulate them +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum Error { + #[error("Invalid value for the flags register, {0:x}. The lower 4 bits must be zero.")] + InvalidFRegisterValue(u8) +} + const INITIAL_PROGRAM_COUNTER_VALUE: u16 = 0x100; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -57,7 +65,7 @@ pub struct Registers { pub stack_pointer: u16, pub program_counter: u16, // the "F" register and the "flags" register are the same register - pub flags: u8, + flags: u8, } impl Flag { @@ -112,6 +120,25 @@ impl Registers { (self.flags & (1 << pos)) >> pos } + /// Get the raw value of the flags register, ignore bit meanings + #[must_use] + pub fn get_raw_flag_bits(&self) -> u8 { + self.flags + } + + /// Set the bits of the flags register. + /// + /// # Errors + /// Returns an error if the lower 4 bits of the register are set. + pub fn set_raw_flag_bits(&mut self, value: u8) -> Result<(), Error> { + if value & 0xF0 != value { + return Err(Error::InvalidFRegisterValue(value)); + } + + self.flags = value; + Ok(()) + } + /// Set a flag register bit (at position `pos`) to the given value. /// This will only pay attention to the first bit, so only zero or one should be passed /// diff --git a/tests/cpu/jsmoo/tests.rs b/tests/cpu/jsmoo/tests.rs index 18a3e0d..bb27bb5 100644 --- a/tests/cpu/jsmoo/tests.rs +++ b/tests/cpu/jsmoo/tests.rs @@ -20,7 +20,7 @@ fn test_jsmoo_test(filename: &str) { processor.registers.c = test_case.initial.c; processor.registers.d = test_case.initial.d; processor.registers.e = test_case.initial.e; - processor.registers.flags = test_case.initial.f; + processor.registers.set_raw_flag_bits(test_case.initial.f).unwrap(); processor.registers.h = test_case.initial.h; processor.registers.l = test_case.initial.l; processor.registers.program_counter = test_case.initial.pc; @@ -42,7 +42,7 @@ fn test_jsmoo_test(filename: &str) { assert_eq!(processor.registers.c, test_case.r#final.c); assert_eq!(processor.registers.d, test_case.r#final.d); assert_eq!(processor.registers.e, test_case.r#final.e); - assert_eq!(processor.registers.flags, test_case.r#final.f); + assert_eq!(processor.registers.get_raw_flag_bits(), test_case.r#final.f); assert_eq!(processor.registers.h, test_case.r#final.h); assert_eq!(processor.registers.l, test_case.r#final.l); assert_eq!(processor.registers.program_counter, test_case.r#final.pc);