r8cpu 0.2.0

Support for the R8 fantasy retro CPU architecture.
Documentation
/// The state of the CPU's flag bits.
#[expect(clippy::struct_excessive_bools, reason = "define 'excessive'")]
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct Flags {
    /// Indicates carry (from addition) or 'no borrow' (from subtraction or comparison).
    pub carry: bool,
    /// Disable interrupts.
    pub interrupt: bool,
    /// Indicates a negative result from the last operation.
    pub negative: bool,
    /// Indicates signed arithemetic overflow.
    pub overflow: bool,
    /// Indicates a zero result from the last operation.
    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 {
    /// Updates the negative and zero flags based on `value`.
    pub fn update(&mut self, value: u8) {
        self.negative = value & 0x80 != 0;
        self.zero = value == 0;
    }

    /// Updates the negative and zero flags based on 16-bit `value`.
    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);
    }
}