Make flags field private again

old-bit-manip
Nick Krichevsky 2023-04-30 21:23:11 -04:00
parent a593664e43
commit 67824138b1
2 changed files with 30 additions and 3 deletions

View File

@ -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
///

View File

@ -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);