//! Rung 1: the one capture from hardware in hand. //! //! John Kortink digitised the white noise of the SN76489 in his BBC Micro and //! published the 32,767-bit sequence ("SN76489 sound chip details", 15 March 2002, //! https://www.zeridajh.org/articles/me_sn76489_sound_chip_details/; phase0.md S9). //! Credit for the capture, and for the rule the discrete variant's noise follows, //! is his. //! //! What this rung can and cannot say (validation.md): it judges the discrete //! variant's register length, taps and sequence against one real TI chip. It says //! nothing of Sega's parts, and the capture fixes neither the polarity (U5) nor the //! alignment after a noise write (U7): both are stated below as found, not proved. //! //! A transcription of sn76489-python's `tests/test_capture.py` at f8157f4. Where Python's //! pytest skips (a file not on this machine), the Rust test prints why and returns. The //! SMS Power wiki's two files are zip archives of one deflated member each; Python opens //! them with `zipfile`, here a few lines read the local header and `flate2` inflates. mod common; use std::io::Read as _; use std::path::Path; use common::{find, noise_bits, reference_dir, sha256}; use sn76489::SN76489AN; /// SHA-256 of Kortink's `white_noise.bin` as fetched (scripts/fetch_references.py /// pins the same value). With it the comparison runs where the file is absent. const CAPTURE_SHA256: &str = "ae43bc97f5c6d9524c56d7f1e8925242d6ac8a0bd40b33052c8eadd68bcbd6df"; /// Where in the capture the bit after the first shift from a noise write falls, /// counting from 0. Kortink gives the capture's "skew" as 20,862: the same place /// within one, the one being whether the bit before or after the first shift is /// counted. FOUND by search against the capture, not predicted. const ROTATION: usize = 20_863; const SMS_ATTACH: &str = "/data/emu/sms-python/reference/smspower-attach"; fn model_sequence() -> Vec { noise_bits(SN76489AN, 0x04, 32_767) } fn as_captured(model: &[u8]) -> Vec { // POLARITY: the capture is the inverse of the model's bit. Kortink's own rule // says so ("output of the generator: the inverse of new_value[0]"). The core // reports "bit set" as the higher level for every variant (spec section 8), so // the inversion is applied here, in the comparison, and is not a claim about // the pin (U5). let inverted: Vec = model.iter().map(|&bit| 1 - bit).collect(); let cut = inverted.len() - ROTATION; [&inverted[cut..], &inverted[..cut]].concat() } /// Tier 1 (S9), spec 6.2.1 and 6.2.3: 32,767 of 32,767 bits. /// /// The model's sequence from a noise write, inverted and rotated by ROTATION, /// has the SHA-256 of the captured file: every bit agrees. #[test] fn test_discrete_white_noise_is_kortinks_capture_by_its_hash() { assert_eq!(sha256(&as_captured(&model_sequence())), CAPTURE_SHA256); } /// The same against the file itself, with a count of agreeing bits. #[test] fn test_discrete_white_noise_is_kortinks_capture_bit_for_bit() { let Some(path) = reference_dir().map(|d| d.join("kortink/white_noise.bin")) else { eprintln!("skipped: reference/kortink/white_noise.bin not fetched"); return; }; let Ok(capture) = std::fs::read(&path) else { eprintln!("skipped: {} not fetched", path.display()); return; }; assert_eq!(sha256(&capture), CAPTURE_SHA256); assert!(capture.iter().all(|&b| b <= 1) && capture.contains(&0) && capture.contains(&1)); assert_eq!(capture.len(), 32_767); let ours = as_captured(&model_sequence()); let agreeing = ours.iter().zip(&capture).filter(|(a, b)| a == b).count(); assert_eq!(agreeing, 32_767); // The other polarity matches at no rotation at all. assert_eq!(find(&capture.repeat(2), &model_sequence(), 0), None); } /// Tier 1 (S9): "a cycle length of 32767 (2^15-1)". The model's sequence /// continues into a second, identical cycle and has no shorter period. #[test] fn test_the_capture_repeats_after_32767_bits_and_no_sooner() { let twice = noise_bits(SN76489AN, 0x04, 2 * 32_767); let once = &twice[..32_767]; assert_eq!(&twice[32_767..], once); assert_eq!(find(&once.repeat(2), once, 1), Some(32_767)); } /// The one member of a zip archive (a local file header, then the deflated data). fn wiki_file(name: &str) -> Option> { let archive = Path::new(SMS_ATTACH).join(name); let Ok(data) = std::fs::read(&archive) else { eprintln!("skipped: {} is not on this machine", archive.display()); return None; }; let le16 = |at: usize| u16::from_le_bytes([data[at], data[at + 1]]) as usize; let le32 = |at: usize| u32::from_le_bytes(data[at..at + 4].try_into().unwrap()) as usize; assert_eq!(le32(0), 0x0403_4B50, "a zip local file header"); assert_eq!(le16(8), 8, "deflated"); let (compressed, size) = (le32(18), le32(22)); let start = 30 + le16(26) + le16(28); let mut out = Vec::with_capacity(size); flate2::read::DeflateDecoder::new(&data[start..start + compressed]) .read_to_end(&mut out) .unwrap(); assert_eq!(out.len(), size); Some(out) } /// A cross-check that the pinned files are what Phase 0 analysed (S8, C9). /// /// Seed: `test_discrete_noise_matches_the_wikis_bit_pattern`, kept and /// re-anchored. Its judge is now Kortink's capture above; this only records that /// the file SMS Power labels "Sega VDP white noise" is the 15-stage TI sequence /// from reset and its "SN76489 white noise" the same inverted and rotated, so /// neither is a capture of Sega's part. /// /// Corrected against the seed: the seed emitted the bit shifted *out*, this core /// the stage at the output end after the shift (spec 6.2.5), so the model's /// sequence leads the seed's by one bit and the "Sega VDP" file equals it rotated /// by one place rather than as it stands. #[test] fn test_the_wikis_two_noise_files_are_the_same_capture() { let model = model_sequence(); let Some(sega_labelled) = wiki_file("SegaVDPWhiteNoise.zip") else { return; }; let rotated = [&model[model.len() - 1..], &model[..model.len() - 1]].concat(); assert_eq!(sega_labelled, rotated); let Some(ti_labelled) = wiki_file("SN76489WhiteNoise.zip") else { return; }; let inverted: Vec = model.iter().map(|&bit| 1 - bit).collect(); assert!(find(&inverted.repeat(2), &ti_labelled, 0).is_some()); }