//! The chip: registers, timers, LFO, six channels, and the output stage. //! //! Every rule names its section of `docs/spec.md`, which names the document or //! measurement behind it; `docs/provenance-audit.md` gives each statement's chain. //! T numbers are the technical decisions of `docs/handoff-brief.md`, all taken at //! their defaults and all PROVISIONAL. "Emulator tier" means no manufacturer's //! document and no measurement stated in prose gives the rule, and the default is //! ymfm's (BSD-3-Clause, Aaron Giles), named. CHOICE marks what no source speaks to. //! //! The model steps once per sample, which is 144 clocks of the chip's own clock pin //! (spec 4.2). It does not model anything inside a sample: when within the 24 //! internal clocks a write lands, lost writes, or the six output slots as a //! waveform (spec 4.6, 21). What differs between the discrete YM2612 and the YM3438 //! is data on [`FMVariant`]. //! //! Transcribed from ym2612-python's `chip.py`. Where Rust forced a difference: //! //! - Python's unbounded integers: the register and state values are `i32` (none comes near its //! range; `&` and `>>` on negative `i32` match Python's); clock counts (`run`'s argument, the //! remainder, BUSY's count and the clock constants) are `u64`, and BUSY's //! `max(0, busy - clocks)` is a saturating subtraction. //! - `write(port, value)` takes bytes: Python masks `value` to 8 bits, which a `u8` already is. //! `reset_line(level)` takes the pin level as a `u8` (0 low, anything else high, as Python's //! truth test). //! - The `irq` property is a method, `irq()`. Python's public attributes are public fields //! (the transcribed tests reach them as the Python tests do); `samples` is the frames not yet //! taken. Python's `_set_frequency` is `set_frequency`, public and hidden from the docs, //! because a test calls it; the other underscore methods are private. //! - `DELAYED_MODULATION` is a `const`, so `_sample` takes it as a const generic parameter: //! `run()` passes the constant, and the hidden `run_with_delayed_modulation::()` lets the //! transcribed test of decision T4 hear the other setting, as the Python test does by //! assigning the module attribute. //! - In `_sample` the DAC's value is read once before the channel loop rather than at channel 6 //! (Rust will not lend the chip while one of its channels is borrowed); nothing in the loop //! can change it. //! - With the `serde` feature the whole chip (every field, the variant as its record) is //! `Serialize`/`Deserialize`, for a host's save states. ym2612-python has no restore. use crate::operator::Operator; use crate::tables::{ALGORITHMS, KEY_CODE_NOTE, LFO_DIVIDERS, TREMOLO_SHIFT}; use crate::variant::{FMVariant, YM2612_DISCRETE}; /// Clocks of the chip's clock pin per sample (spec 4.2: documented by Yamaha's /// F-number formula, measured by Nemesis on an oscilloscope). pub const CLOCKS_PER_SAMPLE: u64 = 144; /// Yamaha's required waits after a write, in clocks of the chip's pin (spec 2.7: /// documented in both manuals; 83 and 47 measured as real minimums by Stef). The /// core does not enforce them; they are here for a board that wants to warn. pub const WAIT_AFTER_ADDRESS: u64 = 17; pub const WAIT_AFTER_OPERATOR_DATA: u64 = 83; pub const WAIT_AFTER_CHANNEL_DATA: u64 = 47; /// How long BUSY reads set after a data write (spec 12.2: measured by Eke on both /// chips, and a five-stage counter on the die). pub const BUSY_CLOCKS: u64 = 192; /// Spec 9.6, contradiction C11, decision T4. PROVISIONAL: operator 3 takes /// operators 1 and 2 from the previous sample and operator 4 takes operator 2 from /// the previous sample (Sauraen's reading of the die). False gives every modulator /// from the current sample, as ymfm does. pub const DELAYED_MODULATION: bool = true; /// Spec 3.3.5, contradiction C12, decision T7. PROVISIONAL, emulator tier: one /// holding latch per part for the high F-number byte, taken by the next low-byte /// write whatever its channel. False gives one latch per channel, which is how /// Nemesis's sentence reads (p5621). pub const FNUM_LATCH_SHARED: bool = true; /// Spec 7.3.5, unknown U3, decision T13. PROVISIONAL: the envelope counter is 12 /// bits and skips 0 (GManiac, p18838, no test described). pub const ENVELOPE_COUNTER_SKIPS_ZERO: bool = true; /// Operators in the order of the register addresses' bits 3-2 (spec 3.2: /// documented), as indexes into a channel's list, which is in Yamaha's order. pub const REGISTER_SLOT_TO_OPERATOR: [usize; 4] = [0, 2, 1, 3]; /// One of the six channels. `operators` is in Yamaha's order, 1 to 4. /// /// Registers (spec 3.3): `f_number`, `block`, `feedback`, `algorithm`, /// `left`, `right`, `tremolo_depth`, `vibrato_depth`. /// /// State: `feedback_older` and `feedback_newer` are operator 1's last two /// outputs (spec 9.4: two stores, measured by GManiac); `operator2_previous` is /// operator 2's last output (spec 9.6); `output` is the channel's signed 9-bit /// value of the last sample, before the DAC switch. #[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Channel { pub algorithm: i32, pub block: i32, pub f_number: i32, pub feedback: i32, pub feedback_newer: i32, pub feedback_older: i32, pub left: i32, pub operator2_previous: i32, pub operators: [Operator; 4], pub output: i32, pub right: i32, pub tremolo_depth: i32, pub vibrato_depth: i32, } impl Default for Channel { fn default() -> Self { Self::new() } } impl Channel { pub fn new() -> Self { Channel { operators: [ Operator::new(), Operator::new(), Operator::new(), Operator::new(), ], f_number: 0, block: 0, feedback: 0, algorithm: 0, // Spec 16.2 (lightly tested: p8020, p7999): both sides on after power-on. left: 1, right: 1, tremolo_depth: 0, vibrato_depth: 0, feedback_older: 0, feedback_newer: 0, operator2_previous: 0, output: 0, } } } /// One chip. All state is plain fields: integers, arrays, and the records above. /// /// `variant` is fixed at construction and never replaced. `enabled` is the /// board's switch: while false the operators are not stepped and each frame is /// (0, 0); timers and BUSY still run. `samples` holds the frames not yet taken. #[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct YM2612 { pub variant: FMVariant, pub enabled: bool, pub reset_held: bool, pub clock_remainder: u64, pub samples: Vec<(i32, i32)>, pub channels: [Channel; 6], pub address: i32, pub address_part: i32, pub fnum_latch: [i32; 6], pub channel3_latch: [i32; 3], pub channel3_f_number: [i32; 3], pub channel3_block: [i32; 3], pub channel3_mode: i32, pub lfo_enabled: i32, pub lfo_rate: i32, pub lfo_counter: i32, pub lfo_divider: i32, pub timer_a_register: i32, pub timer_b_register: i32, pub timer_a_counter: i32, pub timer_b_counter: i32, pub timer_b_prescaler: i32, pub timer_load: i32, pub timer_enable: i32, pub status_flags: i32, pub csm_key: i32, pub dac_data: i32, pub dac_enabled: i32, pub test_21: i32, pub test_2c: i32, pub envelope_counter: i32, pub envelope_divider: i32, pub busy_clocks: u64, } impl Default for YM2612 { /// Python's default argument: the discrete chip. fn default() -> Self { Self::new(YM2612_DISCRETE) } } impl YM2612 { pub fn new(variant: FMVariant) -> Self { let mut chip = YM2612 { variant, enabled: true, reset_held: false, clock_remainder: 0, samples: Vec::new(), // Every field below is set by power_on(). channels: std::array::from_fn(|_| Channel::new()), address: 0, address_part: 0, fnum_latch: [0; 6], channel3_latch: [0; 3], channel3_f_number: [0; 3], channel3_block: [0; 3], channel3_mode: 0, lfo_enabled: 0, lfo_rate: 0, lfo_counter: 0, lfo_divider: 0, timer_a_register: 0, timer_b_register: 0, timer_a_counter: 0, timer_b_counter: 0, timer_b_prescaler: 0, timer_load: 0, timer_enable: 0, status_flags: 0, csm_key: 0, dac_data: 0, dac_enabled: 0, test_21: 0, test_2c: 0, envelope_counter: 0, envelope_divider: 0, busy_clocks: 0, }; chip.power_on(); chip } /// Spec 16, unknown U14, decision T14. PROVISIONAL: every register 0 with /// the panning bits set, operators in release at the quietest level, timers, /// flags, LFO and envelope counter cleared. Power-on and /IC are the same. fn power_on(&mut self) { self.channels = std::array::from_fn(|_| Channel::new()); // Spec 2.5 (measured, p7673): one latch holds the address and its part. self.address = 0; self.address_part = 0; // Spec 3.3.5: pending high bytes of the F-number, per part and channel // (only index 0 of each part is used while FNUM_LATCH_SHARED). self.fnum_latch = [0, 0, 0, 0, 0, 0]; // Spec 3.3.4, 10.1: channel 3's own frequencies for operators 1, 2, 3. self.channel3_latch = [0, 0, 0]; self.channel3_f_number = [0, 0, 0]; self.channel3_block = [0, 0, 0]; self.channel3_mode = 0; self.lfo_enabled = 0; self.lfo_rate = 0; self.lfo_counter = 0; self.lfo_divider = 0; self.timer_a_register = 0; self.timer_b_register = 0; self.timer_a_counter = 0; self.timer_b_counter = 0; self.timer_b_prescaler = 0; self.timer_load = 0; self.timer_enable = 0; self.status_flags = 0; self.csm_key = 0; self.dac_data = 0x80; self.dac_enabled = 0; self.test_21 = 0; self.test_2c = 0; self.envelope_counter = 0; self.envelope_divider = 0; self.busy_clocks = 0; } // -- the board's side -------------------------------------------------------- /// The /IRQ pin: low while either timer flag is set (spec 11.4: measured). pub fn irq(&self) -> bool { self.status_flags != 0 } /// Drive the /IC pin. `level` is the pin's level: 0 holds the chip in /// reset, 1 releases it (spec 16.1). While held, writes are ignored. /// /// CHOICE: the reset takes effect at once, not after the 192 clocks Yamaha /// asks the pulse to last, and the clock divider's remainder is kept. pub fn reset_line(&mut self, level: u8) { if level == 0 { self.power_on(); } self.reset_held = level == 0; } /// Write a byte to one of the four addresses (spec 2.1, 2.2). /// /// Port 0 and 2 take an address for part I and II; 1 and 3 take data. A data /// byte goes to the latched register in the latched part whichever data port /// it came through (spec 2.5: measured). The register changes now and is /// heard from the next sample (decision T9, unknown U1: PROVISIONAL). pub fn write(&mut self, port: u8, value: u8) { if self.reset_held { return; } let value = value as i32; if port & 1 == 0 { self.address = value; self.address_part = ((port >> 1) & 1) as i32; return; } // Spec 12.2 (measured): a data write, and nothing else, sets BUSY. self.busy_clocks = BUSY_CLOCKS; let address = self.address; if address < 0x30 { // Spec 2.3 (measured, p5648): there are no global registers in part II. if self.address_part == 0 { self.write_global(address, value); } } else if address < 0xA0 { self.write_operator(address, value); } else { self.write_channel(address, value); } } /// The status byte, or None where this part leaves the bus undriven /// (spec 12.1, 12.5). Bit 7 BUSY, bit 1 Timer B's flag, bit 0 Timer A's. pub fn read(&self, port: u8) -> Option { if !self.variant.status_ports[(port & 3) as usize] { return None; } Some(((if self.busy_clocks != 0 { 0x80 } else { 0 }) | self.status_flags) as u8) } /// Advance by clocks of the chip's clock pin; one frame per 144 (spec 4.2). pub fn run(&mut self, clocks: u64) { self.run_with_delayed_modulation::(clocks); } /// `run()` with decision T4 given explicitly: for the transcribed test that hears the /// other setting. A board calls `run()`. #[doc(hidden)] pub fn run_with_delayed_modulation(&mut self, clocks: u64) { self.busy_clocks = self.busy_clocks.saturating_sub(clocks); let mut remainder = self.clock_remainder + clocks; while remainder >= CLOCKS_PER_SAMPLE { remainder -= CLOCKS_PER_SAMPLE; self.sample::(); } self.clock_remainder = remainder; } /// The (left, right) frames since the last call, one per 144 clocks. /// /// Each side is the sum of the six slots in steps of the 9-bit converter /// (decision T11), after the variant's output stage: within -1,554 to +1,554. pub fn take_samples(&mut self) -> Vec<(i32, i32)> { std::mem::take(&mut self.samples) } /// The six signed 9-bit values of the last sample, channels 1 to 6, the /// sixth being the DAC value while the DAC is enabled. For validation. pub fn channel_values(&self) -> [i32; 6] { let mut values: [i32; 6] = std::array::from_fn(|index| self.channels[index].output); if self.dac_enabled != 0 { values[5] = self.dac_value(); } values } // -- registers (spec 3) ------------------------------------------------------ fn write_global(&mut self, address: i32, value: i32) { if address == 0x22 { self.lfo_enabled = (value >> 3) & 1; self.lfo_rate = value & 7; } else if address == 0x24 { self.timer_a_register = (self.timer_a_register & 0x003) | (value << 2); } else if address == 0x25 { self.timer_a_register = (self.timer_a_register & 0x3FC) | (value & 3); } else if address == 0x26 { self.timer_b_register = value; } else if address == 0x27 { self.write_timer_control(value); } else if address == 0x28 { // Spec 3.1.1: channel values 0 to 2 and 4 to 6. Values 3 and 7 are // ignored (unknown U2, decision T15: PROVISIONAL). let select = value & 7; if select & 3 != 3 { let operators = &mut self.channels [((select & 3) + if select & 4 != 0 { 3 } else { 0 }) as usize] .operators; operators[0].key_request = (value >> 4) & 1; operators[1].key_request = (value >> 5) & 1; operators[2].key_request = (value >> 6) & 1; operators[3].key_request = (value >> 7) & 1; } } else if address == 0x2A { self.dac_data = value; } else if address == 0x2B { self.dac_enabled = value >> 7; } else if address == 0x21 { self.test_21 = value; // stored, not acted on (decision T8) } else if address == 0x2C { self.test_2c = value; // only bit 3 is used: the DAC's ninth bit (spec 13.3) } // Spec 3.1.2 (measured, p5680): $29 and $2D to $2F do not exist. } /// Register $27 (spec 10.1, 11.2, 11.3). fn write_timer_control(&mut self, value: i32) { self.channel3_mode = value >> 6; if self.channel3_mode != 2 { self.csm_key = 0; // spec 10.4 (measured, p7967): leaving mode 10 clears it } self.refresh_channel3(); // Spec 11.2 (measured, p5687): a timer reloads when its load bit goes from // 0 to 1, not when 1 is written over 1. let rising = value & !self.timer_load; if rising & 1 != 0 { self.timer_a_counter = self.timer_a_register; } if rising & 2 != 0 { self.timer_b_counter = self.timer_b_register; } self.timer_load = value & 3; self.timer_enable = (value >> 2) & 3; // Spec 11.3 (documented): writing 1 to a reset bit clears that flag. self.status_flags &= !(value >> 4) & 3; } /// Registers $30 to $9F (spec 3.2: documented). fn write_operator(&mut self, address: i32, value: i32) { if address & 3 == 3 { return; } let channel = ((address & 3) + 3 * self.address_part) as usize; let operator = &mut self.channels[channel].operators [REGISTER_SLOT_TO_OPERATOR[((address >> 2) & 3) as usize]]; let group = address & 0xF0; if group == 0x30 { operator.detune = (value >> 4) & 7; operator.multiple = value & 15; } else if group == 0x40 { operator.total_level = value & 0x7F; } else if group == 0x50 { operator.key_scale = value >> 6; operator.attack_rate = value & 0x1F; } else if group == 0x60 { operator.tremolo = value >> 7; operator.decay_rate = value & 0x1F; } else if group == 0x70 { operator.sustain_rate = value & 0x1F; } else if group == 0x80 { operator.sustain_level = value >> 4; operator.release_rate = value & 15; } else { // Spec 3.2.3 (measured, p8037, p14196): the upper four bits do nothing. operator.ssg = value & 15; } } /// Registers $A0 to $B6 (spec 3.3). fn write_channel(&mut self, address: i32, value: i32) { let index = address & 3; if index == 3 { return; } let part = self.address_part; let channel = (index + 3 * part) as usize; let latch = (3 * part + if FNUM_LATCH_SHARED { 0 } else { index }) as usize; let group = address & 0xFC; if group == 0xA4 { // Spec 3.3.5 (documented and measured, p5621): the high byte waits. self.fnum_latch[latch] = value & 0x3F; } else if group == 0xA0 { let high = self.fnum_latch[latch]; self.channels[channel].f_number = ((high & 7) << 8) | value; self.channels[channel].block = high >> 3; Self::refresh_frequency(&mut self.channels[channel]); if part == 0 && index == 2 { self.refresh_channel3(); } } else if group == 0xAC { if part == 0 { self.channel3_latch[if FNUM_LATCH_SHARED { 0 } else { index as usize }] = value & 0x3F; } } else if group == 0xA8 { // Spec 3.3.4 (documented, and measured by Shiru): $A9 is operator 1, // $AA operator 2, $A8 operator 3. Part I only. if part == 0 { let high = self.channel3_latch[if FNUM_LATCH_SHARED { 0 } else { index as usize }]; let operator = [2, 0, 1][index as usize]; self.channel3_f_number[operator] = ((high & 7) << 8) | value; self.channel3_block[operator] = high >> 3; self.refresh_channel3(); } } else if group == 0xB0 { self.channels[channel].feedback = (value >> 3) & 7; self.channels[channel].algorithm = value & 7; } else if group == 0xB4 { let channel = &mut self.channels[channel]; channel.left = value >> 7; channel.right = (value >> 6) & 1; channel.tremolo_depth = (value >> 4) & 3; channel.vibrato_depth = value & 7; } } /// Python's `_set_frequency` (a static method), public because a test calls it. #[doc(hidden)] pub fn set_frequency(operator: &mut Operator, f_number: i32, block: i32) { operator.f_number = f_number; operator.block = block; // Spec 5.5 (documented): the key code is the block and two bits made from // the F-number's top four, taken before vibrato (die, p31049). operator.key_code = (block << 2) | KEY_CODE_NOTE[(f_number >> 7) as usize]; } fn refresh_frequency(channel: &mut Channel) { let (f_number, block) = (channel.f_number, channel.block); for operator in channel.operators.iter_mut() { Self::set_frequency(operator, f_number, block); } } /// Spec 10.1, 10.2: in modes 01, 10 and 11 operators 1 to 3 of channel 3 /// take their own F-number, block and so key code; operator 4 keeps the /// channel's. Mode 01 documented (YM3438 manual), 10 measured (p5650); mode 11 /// PROVISIONAL, emulator tier (unknown U25). fn refresh_channel3(&mut self) { let channel = &mut self.channels[2]; Self::refresh_frequency(channel); if self.channel3_mode != 0 { for index in 0..3 { Self::set_frequency( &mut channel.operators[index], self.channel3_f_number[index], self.channel3_block[index], ); } } } // -- one sample (spec 18) ---------------------------------------------------- /// Spec 11.1, 11.2: Timer A steps every sample, Timer B every sixteenth. fn step_timers(&mut self) { if self.timer_load & 1 != 0 { self.timer_a_counter += 1; if self.timer_a_counter >= 1024 { self.timer_a_counter = self.timer_a_register; self.status_flags |= 1; // Spec 10.3, 10.4 (measured, p5650, p7967): in mode 10 an overflow // sets the CSM key flag, whatever the enable bit says. if self.channel3_mode == 2 { self.csm_key = 1; } } } // Spec 11.5, unknown U9, decision T16. PROVISIONAL, emulator tier: the // divide-by-16 runs freely and is not restarted by the load bit. self.timer_b_prescaler = (self.timer_b_prescaler + 1) & 15; if self.timer_b_prescaler == 0 && self.timer_load & 2 != 0 { self.timer_b_counter += 1; if self.timer_b_counter >= 256 { self.timer_b_counter = self.timer_b_register; if self.timer_enable & 2 != 0 { self.status_flags |= 2; } } } } /// Spec 13.3 (die, p26996): $2A with its top bit inverted is the upper /// eight of nine signed bits; $2C bit 3 is the ninth. fn dac_value(&self) -> i32 { ((self.dac_data - 0x80) << 1) | ((self.test_2c >> 3) & 1) } /// One sample, in the order of spec 18. Where the timers and the LFO come /// in that order is a CHOICE (unknowns U6, U8). fn sample(&mut self) { self.step_timers(); if !self.enabled || self.reset_held { self.samples.push((0, 0)); return; } // The LFO (spec 8.1 to 8.3). Its divider is the manual's (decision T2). // Held at 0 while disabled: PROVISIONAL, emulator tier (U26, decision T12). if self.lfo_enabled != 0 { self.lfo_divider += 1; if self.lfo_divider >= LFO_DIVIDERS[self.lfo_rate as usize] { self.lfo_divider = 0; self.lfo_counter = (self.lfo_counter + 1) & 0x7F; } } else { self.lfo_divider = 0; self.lfo_counter = 0; } let lfo_counter = self.lfo_counter; // Spec 8.4: a triangle of 0 to 63 that starts at its maximum, doubled. let mut triangle = lfo_counter & 0x3F; if lfo_counter & 0x40 == 0 { triangle ^= 0x3F; } triangle <<= 1; // Spec 4.5, 7.3.1 (measured): the envelope's shared counter advances every // third sample. Which of the three is a CHOICE (unknown U4). self.envelope_divider += 1; let envelope_update = self.envelope_divider == 3; if envelope_update { self.envelope_divider = 0; self.envelope_counter = (self.envelope_counter + 1) & 0xFFF; if self.envelope_counter == 0 && ENVELOPE_COUNTER_SKIPS_ZERO { self.envelope_counter = 1; } } let envelope_counter = self.envelope_counter; let csm_key = self.csm_key; self.csm_key = 0; // spec 10.4: cleared once this sample has acted on it let variant = self.variant; let low_limit = -256; let high_limit = variant.accumulator_max; let dac_enabled = self.dac_enabled; let dac_value = self.dac_value(); let mut left = 0; let mut right = 0; for (number, channel) in self.channels.iter_mut().enumerate() { let tremolo = triangle >> TREMOLO_SHIFT[channel.tremolo_depth as usize]; let vibrato = channel.vibrato_depth; // Spec 10.4 (measured, p7967): channel 3's operators are keyed by their // $28 bit or the CSM flag, and only a change of the two together acts. let extra_key = if number == 2 { csm_key } else { 0 }; let mut increments = [0; 4]; let operators = &mut channel.operators; for index in 0..4 { let operator = &mut operators[index]; operator.set_key(operator.key_request | extra_key); operator.ssg_step(); if envelope_update { operator.envelope_step(envelope_counter); } let mut f_number = operator.f_number; if vibrato != 0 { // Spec 8.5 (measured, p8905, p8908): added to the 11-bit // F-number before the block, the sum kept to 11 bits. f_number = (f_number + crate::tables::vibrato_offset(f_number, vibrato, lfo_counter)) & 0x7FF; } increments[index] = operator.phase_increment(f_number); } // Spec 9: operators are worked in the order 1, 3, 2, 4 (spec 4.4: // measured on YM3438s by Sauraen). let wiring = ALGORITHMS[channel.algorithm as usize]; let older = channel.feedback_older; let newer = channel.feedback_newer; let previous2 = channel.operator2_previous; // Spec 9.4: operator 1's last two outputs summed (measured: two // stores) and shifted by the feedback level (documented levels; the // shift is arithmetic from them, unknown U7). let feedback = channel.feedback; let self_modulation = if feedback != 0 { (older + newer) >> (10 - feedback) } else { 0 }; let operators = &mut channel.operators; let out1 = operators[0].clock(increments[0], self_modulation, tremolo); // Spec 9.3 (measured, p18814): a modulation input is the sum of the // modulators' outputs shifted right once. let out2; let out3; let modulation; if DELAYED { let modulation3 = (newer * wiring[1] + previous2 * wiring[2]) >> 1; out3 = operators[2].clock(increments[2], modulation3, tremolo); out2 = operators[1].clock(increments[1], (out1 * wiring[0]) >> 1, tremolo); modulation = (out1 * wiring[3] + previous2 * wiring[4] + out3 * wiring[5]) >> 1; } else { out2 = operators[1].clock(increments[1], (out1 * wiring[0]) >> 1, tremolo); let modulation3 = (out1 * wiring[1] + out2 * wiring[2]) >> 1; out3 = operators[2].clock(increments[2], modulation3, tremolo); modulation = (out1 * wiring[3] + out2 * wiring[4] + out3 * wiring[5]) >> 1; } let out4 = operators[3].clock(increments[3], modulation, tremolo); channel.feedback_older = newer; channel.feedback_newer = out1; channel.operator2_previous = out2; // Spec 14.1 (measured, p11862, p11876): each carrier is cut to its // upper 9 bits before the sum. Spec 14.2: the sum is limited. let mut value = out4 >> 5; if wiring[6] != 0 { value += out1 >> 5; } if wiring[7] != 0 { value += out2 >> 5; } if wiring[8] != 0 { value += out3 >> 5; } if value > high_limit { value = high_limit; } else if value < low_limit { value = low_limit; } channel.output = value; // Spec 13.1, 13.2: the DAC replaces channel 6's output; its operators // ran all the same. if number == 5 && dac_enabled != 0 { value = dac_value; } // Spec 14.3, 15: the channel's slot on each side. The sum of the six // slots is the output (decision T11). if variant.ladder { // Spec 15.2.5, decision T10. PROVISIONAL, emulator tier (U12). value += if value >= 0 { variant.ladder_positive } else { variant.ladder_negative }; left += if channel.left != 0 { value } else { variant.ladder_idle }; right += if channel.right != 0 { value } else { variant.ladder_idle }; } else { if channel.left != 0 { left += value; } if channel.right != 0 { right += value; } } } self.samples.push((left, right)); } }