// SPDX-License-Identifier: MIT //! Rung 3 (in progress): the output stage's documented rules (B3.x). //! //! Transcribed from pokey-python's `tests/test_output.py` (at 6ed1ffa). A parametrized //! Python test is one Rust test looping over the same parameters. use pokey::chip::{AUDC1, AUDC2, AUDC3, AUDC4, AUDCTL, AUDF1, AUDF3, SKCTL, STIMER}; use pokey::{C012294, LINEAR, POKEY}; fn write(chip: &mut POKEY, register: u8, value: u8) { chip.write(register, value); chip.tick(); } #[test] fn test_stimer_output_polarity_c2() { // B3.7, B3.6: AHRM 5.3: after STIMER, channels 1 and 2 off, 3 and 4 on (high-pass off) let mut chip = POKEY::default(); for register in [AUDC1, AUDC2, AUDC3, AUDC4] { write(&mut chip, register, 0xAF); } write(&mut chip, STIMER, 0); chip.run(3); assert_eq!(chip.levels(), (0, 0, 15, 15)); } #[test] fn test_volume_only_overrides_after_high_pass() { // B3.8: AHRM 5.3: volume-only channels add in any combination let mut chip = POKEY::default(); for register in [AUDC1, AUDC2, AUDC3, AUDC4] { write(&mut chip, register, 0x1F); } assert_eq!(chip.levels(), (15, 15, 15, 15)); } #[test] fn test_square_toggles_each_fire() { // B3.5, B3.1: pure tone at 1.79 MHz, AUDF1 = 0: the output changes every 4 cycles let mut chip = POKEY::default(); write(&mut chip, AUDCTL, 0x40); write(&mut chip, AUDC1, 0xA1); write(&mut chip, SKCTL, 3); let mut seen = Vec::new(); for _ in 0..40 { chip.tick(); seen.push(chip.levels().0); } let edges: Vec = (1..40).filter(|&i| seen[i] != seen[i - 1]).collect(); let gaps: std::collections::BTreeSet = edges.windows(2).map(|w| w[1] - w[0]).collect(); assert_eq!(gaps, std::collections::BTreeSet::from([4])); } #[test] fn test_noise_is_zero_in_init_mode() { // B3.3: AHRM 5.5: the audio sees 0 from the 4-bit and 9/17-bit generators in init mode for audc in [0xC1, 0x81] { let mut chip = POKEY::default(); write(&mut chip, AUDCTL, 0x40); write(&mut chip, AUDC1, audc); chip.run(100); assert_eq!(chip.ff[0], 0); } } /// AHRM 5.5, the 5-bit pattern const FIVE: &str = "0000011100100010101111011010011"; /// AHRM 5.5; the same from a real chip [Plogue] const FOUR: &str = "000011101100101"; // Plogue: a real C012294 clocked slowly, channel 1 at the machine clock, AUDF1 = 0, the // poly-5-gated square: the output level once per period repeats with this 62-bit pattern. const PLOGUE_5BIT_SQUARE: &str = "00000010111000011001010010011101111110100011110011010110110001"; fn is_rotation(seq: &str, pattern: &str) -> bool { seq.len() == pattern.len() && pattern.repeat(2).contains(seq) } /// `seq` is `pattern` read every `stride` bits from some starting bit (AHRM 5.3, /// "Noise sampling artifacts": when the two periods share a factor the start matters). fn is_sampled(seq: &str, pattern: &str, stride: usize) -> bool { let n = pattern.len(); let p = pattern.as_bytes(); (0..n).any(|start| { let candidate: String = (0..seq.len()) .map(|k| p[(start + k * stride) % n] as char) .collect(); seq == candidate }) } /// Channel 1 at the machine clock: its output level (or toggle) once per period. fn per_fire(audc: u8, audf: u8, fires: usize, field: &str) -> String { let mut chip = POKEY::default(); write(&mut chip, AUDCTL, 0x40); write(&mut chip, AUDF1, audf); write(&mut chip, AUDC1, audc); write(&mut chip, SKCTL, 3); chip.run(200); let mut out: Vec = Vec::new(); for _ in 0..300 * (fires + 1) { if out.len() > fires { break; } chip.tick(); if chip.fired[0] { chip.run(2); // B3.1: the output changes in the tick after the fire out.push(if chip.levels().0 != 0 { 1 } else { 0 }); } } if field == "toggle" { return (1..out.len()) .map(|i| (out[i] ^ out[i - 1]).to_string()) .collect(); } out[1..].iter().map(|v| v.to_string()).collect() } #[test] fn test_5bit_gated_square_is_the_hardware_recording() { // B3.2, B3.5 TEST (hardware recording, [Plogue]): AUDF1 = 0, the 62-bit level pattern let levels = per_fire(0x21, 0, 62, "level"); assert!(is_rotation(&levels, PLOGUE_5BIT_SQUARE)); } #[test] fn test_5bit_gate_samples_every_period() { // B3.2 [Plogue]: the toggles are the 5-bit pattern (1 = pass) taken every N + 4 bits for audf in [0u8, 1, 2, 3, 4] { let toggles = per_fire(0x21, audf, 31, "toggle"); assert!(is_sampled(&toggles, FIVE, audf as usize + 4)); } } #[test] fn test_4bit_noise_samples_every_period() { // B3.3 [Plogue], AHRM 5.5: the 4-bit pattern taken every N + 4 bits; channel 1's // level is the flip-flop XOR the disabled high-pass flip-flop (1), so inverted let inverted: String = FOUR .chars() .map(|c| if c == '0' { '1' } else { '0' }) .collect(); for audf in [0u8, 1, 2, 3, 4] { let levels = per_fire(0xC1, audf, 15, "level"); assert!(is_sampled(&levels, &inverted, audf as usize + 4)); } } #[test] fn test_gate_opens_while_ungated() { // B3.2 OBSERVED (closed box): a channel left ungated for a cycle passes its next fire // even when it is gated again before that fire and its 5-bit bit is 0 let mut chip = POKEY::default(); write(&mut chip, AUDCTL, 0x40); write(&mut chip, AUDF1, 26); // period 30, against the 5-bit's 31 write(&mut chip, AUDC1, 0x21); write(&mut chip, SKCTL, 3); chip.run(100); for _ in 0..2000 { if chip.fired[0] && chip.gate5[0] == 0 { break; } chip.tick(); } assert!(chip.fired[0] && chip.gate5[0] == 0); write(&mut chip, AUDC1, 0xA1); write(&mut chip, AUDC1, 0x21); assert_eq!(chip.gate5[0], 1); } fn gated_square_channel1(audf: u8) -> POKEY { let mut chip = POKEY::default(); write(&mut chip, AUDCTL, 0x40); write(&mut chip, AUDF1, audf); write(&mut chip, AUDC1, 0x21); write(&mut chip, SKCTL, 3); chip.run(100); chip } fn bit5(chip: &POKEY, ch: usize) -> u8 { ((chip.poly5 >> (POKEY::TAP5 - ch)) & 1) as u8 } #[test] fn test_gate_load_counts_a_write_in_the_fire_cycle() { // B3.2 OBSERVED (closed box): an AUDC write made in the fire's own cycle counts for // the load: ungated -> gated in that cycle loads the 5-bit bit, not "open" let mut chip = gated_square_channel1(26); write(&mut chip, AUDC1, 0xA1); for _ in 0..2000 { // the cycle before a fire whose load would take a 0 let mut probe = POKEY::default(); probe.restore(&chip.snapshot()); probe.write(AUDC1, 0x21); probe.tick(); if probe.fired[0] && probe.gate5[0] == 0 { break; } chip.tick(); } chip.write(AUDC1, 0x21); chip.tick(); assert!(chip.fired[0] && chip.gate5[0] == 0); } #[test] fn test_gate_is_open_in_init_mode() { // B3.2 DOC: AHRM 5.5, the 5-bit generator is a constant 1 in init mode: a gated // square at the machine clock toggles on every fire (the closed box does not: D6) let mut chip = POKEY::default(); write(&mut chip, AUDCTL, 0x40); write(&mut chip, AUDC1, 0x21); let mut seen = Vec::new(); for _ in 0..80 { chip.tick(); seen.push(chip.ff[0]); } assert!((1..80).filter(|&i| seen[i] != seen[i - 1]).count() >= 19); } #[test] fn test_stimer_loads_the_gates_in_t_plus_2() { // B3.2 OBSERVED (closed box, the first STIMER after init mode): the gates take the // 5-bit bits of tick T+2 for wait in (0..31).step_by(5) { let mut chip = gated_square_channel1(200); // AUDC2-4 = 0: gated too chip.run(wait); write(&mut chip, STIMER, 0); // T chip.tick(); // T+1 chip.tick(); // T+2 let bits: Vec = (0..4).map(|ch| bit5(&chip, ch)).collect(); assert_eq!(chip.gate5.to_vec(), bits); } } #[test] fn test_high_pass_copies_the_flip_flop_before_its_clock() { // B3.6 OBSERVED (closed box, the manual's $3B/$3C case): channel 3 firing one tick // after channel 1 copies channel 1's flip-flop as it stood before that tick's toggle, // so channel 1's output stays on let mut chip = POKEY::default(); write(&mut chip, AUDCTL, 0x64); // channels 1 and 3 at the machine clock, high-pass 1 by 3 write(&mut chip, AUDC1, 0xAF); write(&mut chip, SKCTL, 3); write(&mut chip, AUDF1, 0); write(&mut chip, AUDF3, 1); write(&mut chip, STIMER, 0); chip.run(3); write(&mut chip, AUDF3, 0); // channel 3: one period of 5, then 4: one tick behind channel 1 chip.run(20); let mut seen = std::collections::BTreeSet::new(); for _ in 0..40 { chip.tick(); seen.insert(chip.levels().0); } assert_eq!(seen, std::collections::BTreeSet::from([15])); } #[test] fn test_stimer_holds_the_flip_flops_two_ticks() { // B3.7 OBSERVED (closed box): a fire in tick T+2 of a STIMER does not toggle let mut chip = POKEY::default(); write(&mut chip, AUDCTL, 0x40); write(&mut chip, AUDC1, 0xA1); write(&mut chip, SKCTL, 3); chip.run(50); for _ in 0..8 { if chip.fired[0] { break; } chip.tick(); } chip.run(1); // three ticks before the next fire (period 4): write STIMER now write(&mut chip, STIMER, 0); // T; the fire comes in tick T+2 assert!(!chip.fired[0]); chip.tick(); chip.tick(); assert!(chip.fired[0]); chip.run(3); assert_eq!(chip.ff, [1, 1, 1, 1]); } #[test] fn test_mix_curve_meets_the_manuals_words() { // B3.9 FITTED: f(30) = 1.5 f(15); f(60) about 2 f(15); linear to 15 let f = |total: f64| C012294.mix_curve[(total * 4.0 + 0.5) as usize]; assert!((f(30.0) / f(15.0) - 1.5).abs() < 0.01); assert!(1.9 < f(60.0) / f(15.0) && f(60.0) / f(15.0) < 2.1); assert!((f(10.0) / f(5.0) - 2.0).abs() < 1e-9); let steps: Vec = (0..15) .map(|v| C012294.dac[v + 1] - C012294.dac[v]) .collect(); assert!(steps[3] > steps[2] && steps[7] > steps[6] && steps[11] > steps[10]); let mut chip = POKEY::new(LINEAR.clone(), false); for register in [AUDC1, AUDC2, AUDC3, AUDC4] { write(&mut chip, register, 0x1F); } assert_eq!(chip.mix(), 1.0); }