// SPDX-License-Identifier: MIT //! Rung 3: pokey-python's conformance kit (`validation/conformance.txt`), reproduced exactly. //! //! Transcribed from the reference's `scripts/conformance.py` (at 6ed1ffa): each case is a //! deterministic stimulus played on the shipped chip, one command per cycle, the access //! first, then the tick (B0.1). Every cycle contributes one record: the value read (or -1), //! `levels()`, `irq`, `scan_code`, `dump_active`, `sod`, `oclk`, `bclk_out`, written as //! Python's `repr()` of that tuple; the kit holds, per case, the CRC-32 of each 1,000-cycle //! slice and of the whole case. //! //! The stimuli are the reference's own generators' output (its `cases()`: the stream //! generators of its `scripts/streams.py`, which draw on Python's `random`, and three //! directed cases), exported once into `tests/conformance/` by `scripts/export_streams.py` //! with the reference's `conformance.txt` beside them; scripts/rung3.sh checks that those //! copies are what the reference at the pin writes. use std::path::Path; use pokey::POKEY; const SLICE: u64 = 1000; /// zlib's CRC-32 (IEEE 802.3, reflected, polynomial 0xEDB88320), continued from `crc` as /// `zlib.crc32(data, crc)` continues. fn crc32(data: &[u8], crc: u32) -> u32 { let mut c = !crc; for &byte in data { c ^= byte as u32; for _ in 0..8 { c = if c & 1 != 0 { (c >> 1) ^ 0xEDB8_8320 } else { c >> 1 }; } } !c } /// scripts/streams.py's `apply_pins`. fn apply_pins(chip: &mut POKEY, op: &str, value: u64) { if op == "S" { chip.sid = if value != 0 { 1 } else { 0 }; } else if op == "P" { let mask = value; chip.pot_level = Box::new(move |n, _look| (mask >> n) & 1 != 0); } else if op == "K" { // testbench: v = KR2,KR1 pin levels (idle 3); the core's callable: bit set = asserted let asserted = (!value & 3) as u8; chip.key_response = Box::new(move |_code| asserted); } } fn digests(commands: &str) -> Vec { let mut chip = POKEY::default(); let mut crcs = Vec::new(); let mut crc = 0u32; let mut n = 0u64; for line in commands.lines() { if line.starts_with('#') || line.trim().is_empty() { continue; } let mut parts: Vec<&str> = line.split_whitespace().collect(); let mut times = 1u64; if let Some(last) = parts.last() { if let Some(count) = last.strip_prefix('*') { times = count.parse().unwrap(); parts.pop(); } } let op = parts[0]; let arg = |i: usize| -> u64 { parts[i].parse().unwrap() }; for _ in 0..times { if op == "K" || op == "P" || op == "S" { apply_pins(&mut chip, op, arg(1)); continue; } let repeat = if op == "N" { arg(1) } else { 1 }; for _ in 0..repeat { let mut value: i32 = -1; if op == "W" { chip.write(arg(1) as u8, arg(2) as u8); } else if op == "R" { value = chip.read(arg(1) as u8) as i32; } chip.tick(); let bclk: i32 = match chip.bclk_out() { None => -1, Some(b) => b as i32, }; let (l1, l2, l3, l4) = chip.levels(); let record = format!( "({value}, {l1}, {l2}, {l3}, {l4}, {}, {}, {}, {}, {}, {bclk})", chip.irq as u8, chip.scan_code, chip.dump_active as u8, chip.sod(), chip.oclk() ); crc = crc32(record.as_bytes(), crc); n += 1; if n % SLICE == 0 { crcs.push(crc); } } } } crcs.push(crc); crcs } #[test] fn crc32_matches_zlib() { // zlib.crc32(b"123456789") == 0xCBF43926, and continuing it equals one call assert_eq!(crc32(b"123456789", 0), 0xCBF4_3926); assert_eq!(crc32(b"6789", crc32(b"12345", 0)), 0xCBF4_3926); } #[test] fn conformance_kit_reproduced() { let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/conformance"); let kit = std::fs::read_to_string(dir.join("conformance.txt")).unwrap(); let mut bad = Vec::new(); let mut total = 0; for want in kit.lines().filter(|l| !l.starts_with('#')) { let name = want.split_whitespace().next().unwrap(); let commands = std::fs::read_to_string(dir.join(format!("{name}.txt"))).unwrap(); let crcs = digests(&commands); let line = format!( "{name} {} {}", crcs.len() - 1, crcs.iter() .map(|c| format!("{c:08x}")) .collect::>() .join(" ") ); total += 1; if line != want { bad.push(name.to_string()); } } println!( "{}/{} cases identical{}", total - bad.len(), total, if bad.is_empty() { String::new() } else { format!("; differ: {bad:?}") } ); assert_eq!(total, 11); assert!(bad.is_empty()); }