pub struct Flags {
pub s: bool, pub z: bool, pub a: bool, pub p: bool, pub c: bool }
impl Flags {
pub fn new() -> Flags {
Flags {
s: false,
z: false,
a: false,
p: false,
c: false
}
}
pub fn as_byte(&self) -> u8 {
let s = if self.s { 1 << 7 } else { 0 };
let z = if self.z { 1 << 6 } else { 0 };
let a = if self.a { 1 << 4 } else { 0 };
let p = if self.p { 1 << 2 } else { 0 };
let c = if self.c { 1 } else { 0 };
s | z | a | p | c | 2
}
pub fn from_byte(&mut self, bflags: u8) {
self.s = (bflags & 0x80) != 0;
self.z = (bflags & 0x40) != 0;
self.a = (bflags & 0x10) != 0;
self.p = (bflags & 0x04) != 0;
self.c = (bflags & 0x01) != 0;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn flags_from_byte() {
let mut f = Flags::new();
f.from_byte(0xC3);
assert_eq!(f.s, true);
assert_eq!(f.z, true);
assert_eq!(f.c, true);
assert_eq!(f.a, false);
assert_eq!(f.p, false);
}
#[test]
fn flags_as_byte() {
let mut f = Flags::new();
f.from_byte(0xC3);
f.s = true;
f.z = true;
f.c = true;
f.a = false;
f.p = false;
assert_eq!(f.as_byte(), 0xC3);
}
}