use assert_matches::assert_matches;
use chrono::DateTime;
use crate::device::clock::tests::TestClock;
use crate::device::cmos::{Cmos, CmosRegA};
use crate::mem::emulated::Mmio;
#[test]
fn test_cmos() {
let now = DateTime::from_timestamp_nanos(1762559098010_000000);
let cmos = Cmos::new(TestClock { now });
assert_eq!(cmos.size(), 2);
assert_matches!(cmos.write(0x0, 1, 0xb), Ok(_));
assert_matches!(cmos.read(0x0, 1), Ok(0xb));
assert_matches!(cmos.read(0x1, 1), Ok(0b110));
assert_matches!(cmos.write(0x0, 1, 0xd), Ok(_));
assert_matches!(cmos.read(0x1, 1), Ok(0x80));
assert_matches!(cmos.write(0x0, 1, 0xa), Ok(_));
let reg_a = cmos.read(0x1, 1).unwrap();
assert!(
!CmosRegA(reg_a as u8).update_in_progress(),
"CMOS update should be complete"
);
let tests = [
(0x00, 58),
(0x02, 44),
(0x04, 23),
(0x06, 6),
(0x07, 7),
(0x08, 11),
(0x09, 25),
(0x32, 21),
];
for (reg, expected) in tests {
assert_matches!(cmos.write(0x0, 1, reg as u64), Ok(_));
let value = cmos.read(0x1, 1).unwrap();
assert_eq!(
value as u32, expected,
"CMOS register {reg:#02x} should match getter",
);
}
assert_matches!(cmos.write(0x0, 1, 0x01), Ok(_));
assert_matches!(cmos.read(0x1, 1), Ok(0));
assert_matches!(cmos.write(0x1, 1, 0x0), Ok(_));
}
#[test]
fn test_cmos_upgrade_in_progress() {
let now = DateTime::from_timestamp_nanos(1764258300000_000000);
let cmos = Cmos::new(TestClock { now });
assert_matches!(cmos.write(0x0, 1, 0xa), Ok(_));
let reg_a = cmos.read(0x1, 1).unwrap();
assert!(
CmosRegA(reg_a as u8).update_in_progress(),
"CMOS update should be in progress"
);
}