// SPDX-License-Identifier: MIT //! Shared by the tests: register writes spelled the way the manuals number things. //! //! The reference's `tests/helpers.py`. `voice()`'s keyword arguments are the fields of //! [`Voice`], whose `V` holds the Python defaults: `voice(&mut fm, Voice { algorithm: 4, ..V })`. #![allow(dead_code)] use ym2612::{FMVariant, CLOCKS_PER_SAMPLE, YM2612, YM3438_ASIC}; /// Address offset of Yamaha's operators 1 to 4 (index 0 to 3): the register order /// is 1, 3, 2, 4 (spec 3.2). pub const OPERATOR_OFFSET: [u8; 4] = [0, 8, 4, 12]; pub fn reg(fm: &mut YM2612, address: u8, value: u8) { reg_part(fm, address, value, 0); } pub fn reg_part(fm: &mut YM2612, address: u8, value: u8, part: u8) { fm.write(part * 2, address); fm.write(part * 2 + 1, value); } /// Write an operator register; channel 0 to 5, operator 0 to 3 (Yamaha's 1 to 4). pub fn op_reg(fm: &mut YM2612, channel: usize, operator: usize, base: u8, value: u8) { let (part, index) = (channel / 3, channel % 3); reg_part( fm, base + OPERATOR_OFFSET[operator] + index as u8, value, part as u8, ); } pub fn frequency(fm: &mut YM2612, channel: usize, f_number: i32, block: i32) { let (part, index) = ((channel / 3) as u8, (channel % 3) as u8); reg_part( fm, 0xA4 + index, ((block << 3) | (f_number >> 8)) as u8, part, ); reg_part(fm, 0xA0 + index, (f_number & 0xFF) as u8, part); } /// Key the operators in `mask` (bit 0 is operator 1) on and the rest off. pub fn key(fm: &mut YM2612, channel: usize, mask: u8) { reg(fm, 0x28, (mask << 4) | (channel + channel / 3) as u8); } pub fn step(fm: &mut YM2612, samples: u64) -> Vec<(i32, i32)> { fm.run(CLOCKS_PER_SAMPLE * samples); fm.take_samples() } /// `voice()`'s keyword arguments. #[derive(Clone, Copy)] pub struct Voice { pub channel: usize, pub algorithm: u8, pub f_number: i32, pub block: i32, pub total_levels: [u8; 4], pub attack: u8, pub feedback: u8, } /// The defaults of `voice()`. pub const V: Voice = Voice { channel: 0, algorithm: 7, f_number: 0x269, block: 4, total_levels: [127, 127, 127, 0], attack: 31, feedback: 0, }; /// A plain voice: instant attack, no decay, multiple 1, both sides on. pub fn voice(fm: &mut YM2612, v: Voice) { let channel = v.channel; let (part, index) = ((channel / 3) as u8, (channel % 3) as u8); for operator in 0..4 { op_reg(fm, channel, operator, 0x30, 0x01); op_reg(fm, channel, operator, 0x40, v.total_levels[operator]); op_reg(fm, channel, operator, 0x50, v.attack); op_reg(fm, channel, operator, 0x60, 0); op_reg(fm, channel, operator, 0x70, 0); op_reg(fm, channel, operator, 0x80, 0x0F); op_reg(fm, channel, operator, 0x90, 0); } reg_part(fm, 0xB0 + index, (v.feedback << 3) | v.algorithm, part); reg_part(fm, 0xB4 + index, 0xC0, part); frequency(fm, channel, v.f_number, v.block); } /// `chip()`: the default is the ASIC, as in the reference's helpers. pub fn chip() -> YM2612 { YM2612::new(YM3438_ASIC) } pub fn chip_of(variant: FMVariant) -> YM2612 { YM2612::new(variant) } /// The channel's 9-bit value at each of the next `samples` samples. pub fn values(fm: &mut YM2612, samples: usize, channel: usize) -> Vec { let mut out = Vec::new(); for _ in 0..samples { fm.run(CLOCKS_PER_SAMPLE); out.push(fm.channel_values()[channel]); } fm.take_samples(); out } /// The status byte at address 0 (`fm.read(0)` in the tests, which always run a part that /// drives address 0). pub fn status(fm: &YM2612) -> u8 { fm.read(0).expect("address 0 always returns status") }