#[expect(clippy::struct_excessive_bools, reason = "define 'excessive'")]
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct Flags {
pub carry: bool,
pub interrupt: bool,
pub negative: bool,
pub overflow: bool,
pub zero: bool,
}
impl From<u8> for Flags {
fn from(value: u8) -> Self {
Self {
carry: value & 0x01 != 0,
interrupt: value & 0x04 != 0,
negative: value & 0x80 != 0,
overflow: value & 0x40 != 0,
zero: value & 0x02 != 0,
}
}
}
impl From<Flags> for u8 {
fn from(flags: Flags) -> Self {
let mut value = 0x00;
value |= u8::from(flags.carry);
value |= u8::from(flags.interrupt).strict_shl(2);
value |= u8::from(flags.negative).strict_shl(7);
value |= u8::from(flags.overflow).strict_shl(6);
value |= u8::from(flags.zero).strict_shl(1);
value
}
}
impl Flags {
pub fn update(&mut self, value: u8) {
self.negative = value & 0x80 != 0;
self.zero = value == 0;
}
pub fn update16(&mut self, value: u16) {
self.negative = value & 0x8000 != 0;
self.zero = value == 0;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn flags_are_correctly_decoded() {
let flags = Flags {
carry: true,
interrupt: true,
negative: true,
overflow: true,
zero: true,
};
assert_eq!(Flags::from(0b1100_0111), flags);
}
#[test]
fn flags_are_correctly_encoded() {
let flags = Flags {
carry: true,
interrupt: true,
negative: true,
overflow: true,
zero: true,
};
assert_eq!(u8::from(flags), 0b1100_0111);
}
}