//! The trace shared by examples/sn76489-trace.rs (rungs 2 and 3, against sn76489-python) and //! tests/closed_box.rs (rung 3 under `cargo test`): one script in, the trace that //! scripts/lockstep_ref.py prints for sn76489-python out. use std::fmt::Write as _; use sn76489::{PSGVariant, GAME_GEAR_PSG, SEGA_VDP_PSG, SN76489, SN76489AN, YM7101_PSG}; fn h(x: f64) -> String { format!("{:016x}", x.to_bits()) } fn ints(values: &[T]) -> String { values .iter() .map(|v| v.to_string()) .collect::>() .join(",") } fn state(psg: &SN76489) -> String { format!( "S {} {} {} {} {} {} {} {} {} {} {} {} {}", ints(&psg.tone), psg.noise, ints(&psg.volume), psg.stereo, psg.latched, ints(&psg.counters), ints(&psg.outputs), psg.noise_counter, psg.noise_input, psg.lfsr, psg.test, psg.clock_remainder, psg.samples.len() ) } fn take(psg: &mut SN76489) -> String { let samples = psg.take_samples(); let mut out = format!("take {}", samples.len()); let mut index = 0; while index < samples.len() { let pair = samples[index]; let mut end = index + 1; while end < samples.len() && samples[end] == pair { end += 1; } write!(out, " {}*{},{}", end - index, h(pair.0), h(pair.1)).unwrap(); index = end; } out } /// The part by the name of its constant, as Python's `getattr(sn76489_python, name)`. fn part(name: &str) -> PSGVariant { match name { "SN76489AN" => SN76489AN, "SEGA_VDP_PSG" => SEGA_VDP_PSG, "GAME_GEAR_PSG" => GAME_GEAR_PSG, "YM7101_PSG" => YM7101_PSG, _ => panic!("unknown part {name}"), } } /// The trace of one script (the lines scripts/lockstep_ref.py lists). pub fn trace(text: &str) -> String { let mut out = String::new(); let lines: Vec<&str> = text.lines().collect(); let mut variant = part(lines[0].split_whitespace().nth(1).unwrap()); let mut psg: Option = None; for line in &lines[1..] { let mut parts = line.split_whitespace(); let op = parts.next().unwrap(); let args: Vec<&str> = parts.collect(); let dec = |i: usize| -> u64 { args[i].parse().unwrap() }; if op == "field" { let value = || dec(1) as u32; match args[0] { "volume_table" => { variant.volume_table = std::array::from_fn(|i| { f64::from_bits(u64::from_str_radix(args[1 + i], 16).unwrap()) }); assert_eq!(args.len(), 17); } "stereo" => variant.stereo = value() != 0, "data_byte_reaches_all_registers" => { variant.data_byte_reaches_all_registers = value() != 0 } "test_override" => variant.test_override = value() != 0, "lfsr_bits" => variant.lfsr_bits = value(), "white_noise_taps" => variant.white_noise_taps = value(), "tone_zero_period" => variant.tone_zero_period = value(), "power_on_attenuation" => variant.power_on_attenuation = value(), "power_on_latch" => variant.power_on_latch = value(), "noise_shift_level" => variant.noise_shift_level = value(), "ready_clocks" => variant.ready_clocks = value(), other => panic!("unknown field {other}"), } continue; } if op == "new" { psg = Some(SN76489::with_silent_start(variant, dec(0) != 0)); } let chip = psg.as_mut().expect("new before any operation"); match op { "new" | "state" => {} "run" => chip.run(dec(0)), "w" => chip.write(dec(0) as u32), "s" => chip.write_stereo(dec(0) as u32), "t" => match chip.write_test(dec(0) as u32) { Ok(()) => out.push_str("t ok\n"), Err(error) => writeln!(out, "t err {error}").unwrap(), }, "reset" => chip.reset(dec(0) != 0), "take" => { out.push_str(&take(chip)); out.push('\n'); } _ => panic!("unknown op {op:?}"), } out.push_str(&state(chip)); out.push('\n'); } out }