// SPDX-License-Identifier: MIT //! The MOS 6532 RIOT: 128 bytes of RAM, two 8-bit ports, the interval timer. //! //! Kept behind its own small interface (`read`/`write` with a CPU-cycle //! count, the port inputs as attributes) so it can be extracted into a core when //! a second board needs it (the 7800; docs/handoff-brief.md). //! //! **Decode** (6532 data sheet; docs/start-here.md "The RIOT"): the board selects //! the RIOT when A12 = 0 and A7 = 1. Then A9 = 0 is RAM (A0-A6). A9 = 1 is I/O: //! with A2 = 0, A1-A0 pick SWCHA, SWACNT, SWCHB, SWBCNT; with A2 = 1, a read with //! A0 = 0 is INTIM and with A0 = 1 the interrupt flags, a write with A4 = 1 loads //! the timer (A1-A0 the interval, A3 its interrupt enable) and with A4 = 0 sets //! the PA7 edge (A0 = 1 positive). The IRQ pin is not connected on the 2600. //! //! **The timer** is evaluated lazily, in CPU cycles (one φ2 per cycle): //! //! - A write loads the counter and selects the interval; the first decrement //! comes one cycle after the write, then one every interval. DOC: MAME's //! BSD-3 mos6530.cpp:315-321; OBSERVED: Stella (closed box) agrees, milestone //! 1's lockstep failing at once with the decrement a cycle late; TEST ROM: the //! #606 console results match with it. //! - When it decrements past 0 it reads $FF, sets the timer flag (TIMINT D7), and //! from then on decrements every cycle. //! - Reading INTIM clears the flag and the counter goes back to the interval, //! continuing from the prescaler's current phase: the console results of Stella //! issue #97 (docs/undocumented-behavior.md C7, hardware-captured photos and //! tables). MAME instead restarts a whole interval on that read //! (BSD-3 mos6530.cpp:510-526); this is one place the MAME lockstep may diverge. //! - A read of INTIM, or a write to the timer, in the very cycle the counter //! wraps does not clear the flag: "the reading or writing of the timer at the //! same time interrupt occurs will not reset the interrupt flag" (MCS6500 //! Family Hardware Manual p.78), confirmed on consoles for both, on the first //! wrap and on later ones, by the TimerTest ROMs of Stella issue #606 //! (docs/undocumented-behavior.md C6). A write there loads the counter, which //! then counts every cycle because the flag is still set. //! //! Rust-forced differences (a2600-python `riot.py` at 28fb095): cycle counts //! are `u64` at the interface and `i64` inside (Python's integers); the //! `port_a_listener` is a boxed closure that is handed the RIOT itself, so a //! device can answer through [`RIOT::set_input_a`] (in Python it holds the //! RIOT); it is not part of a save state. The private `_pa7`, `_countdown`, //! `_as_of` and `_wrapped_at` drop their underscores and stay public, for //! save states and the tests. use serde::{Deserialize, Serialize}; pub const TIMER_FLAG: u8 = 0x80; // DOC: 6532 data sheet, interrupt flag register D7 pub const PA7_FLAG: u8 = 0x40; // DOC: 6532 data sheet, interrupt flag register D6 pub const INTERVALS: [i64; 4] = [1, 8, 64, 1024]; // DOC: 6532 data sheet, A1-A0 of a timer write; TEST ROM: #606 /// A device watching the port A pins: `listener(riot, levels, cycle)`. pub type PortAListener = Box; #[derive(Serialize, Deserialize)] pub struct RIOT { pub ram: Vec, pub out_a: u8, pub ddr_a: u8, pub out_b: u8, pub ddr_b: u8, // The pins' input levels, set by the controllers and console switches: // port A = left joystick in D7-D4, right in D3-D0 (active low). pub input_a: u8, pub input_b: u8, pub flags: u8, pub edge_positive: bool, pub pa7: bool, // The timer: the counter, the interval, the cycles left until the // prescaler's next decrement (1..divider), the cycle the state is as of, // and the cycle the counter last wrapped past 0 (-1: never). pub value: i64, pub divider: i64, pub countdown: i64, pub as_of: i64, pub wrapped_at: i64, // A device that watches the port A pins the 6532 drives (a SaveKey's // I2C lines): called as `listener(riot, levels, cycle)` after every write // to SWCHA or SWACNT, with each pin's level as the RIOT leaves it (1 // where it is an input or outputs 1). The device answers through // [`RIOT::set_input_a`]. #[serde(skip)] pub port_a_listener: Option, } impl RIOT { pub fn new(ram_fill: u8, timer_value: u8, timer_divider: i64, first_decrement: i64) -> RIOT { RIOT { ram: vec![ram_fill; 128], out_a: 0, ddr_a: 0, out_b: 0, ddr_b: 0, input_a: 0xFF, input_b: 0x3F, flags: 0, edge_positive: false, pa7: true, value: (timer_value & 0xFF) as i64, divider: timer_divider, countdown: first_decrement, as_of: 0, wrapped_at: -1, port_a_listener: None, } } // -- the timer --------------------------------------------------------- fn catch_up(&mut self, cycle: i64) { let mut n = cycle - self.as_of; if n <= 0 { return; } self.as_of = cycle; let divider = self.divider; if self.flags & TIMER_FLAG == 0 { let countdown = self.countdown; if n < countdown { self.countdown = countdown - n; return; } // The decrement due after `countdown` cycles, then one per interval. n -= countdown; let steps = 1 + n.div_euclid(divider); self.countdown = divider - n.rem_euclid(divider); if steps <= self.value { self.value -= steps; return; } // Past 0: the wrap happens on the (value + 1)th decrement, at the // cycle below; every cycle after it decrements once more. let wrap = cycle - n + (self.value * divider); self.flags |= TIMER_FLAG; self.wrapped_at = wrap + (cycle - wrap).div_euclid(256) * 256; // and every 256 after self.value = (0xFF - (cycle - wrap)) & 0xFF; return; } // Wrapped: one decrement per cycle, the prescaler still turning; the // counter wraps again whenever it passes 0. // The k-th of these n decrements wraps when k = value + 1 (mod 256). if n > self.value { let last = n - (n - self.value - 1).rem_euclid(256); self.wrapped_at = cycle - n + last; } self.value = (self.value - n) & 0xFF; self.countdown = divider - (divider - self.countdown + n).rem_euclid(divider); } /// INTIM as of `cycle`, without a read's side effect (for debuggers and detectors). pub fn timer_at(&mut self, cycle: u64) -> u8 { self.catch_up(cycle as i64); self.value as u8 } fn load_timer(&mut self, value: u8, divider: i64, cycle: i64) { self.value = value as i64; self.divider = divider; self.countdown = 1; self.as_of = cycle; if self.wrapped_at != cycle { self.flags &= !TIMER_FLAG; } } // -- the bus ----------------------------------------------------------- pub fn read(&mut self, address: u16, cycle: u64) -> u8 { if address & 0x200 == 0 { return self.ram[(address & 0x7F) as usize]; } let cycle = cycle as i64; self.catch_up(cycle); if address & 0x04 == 0 { let register = address & 0x03; if register == 0 { // Wired-AND: an output bit drives low only where SWACNT makes it an output. return (self.out_a | !self.ddr_a) & self.input_a; } if register == 1 { return self.ddr_a; } if register == 2 { return (self.out_b & self.ddr_b) | (self.input_b & !self.ddr_b); } return self.ddr_b; } if address & 0x01 == 0 { if self.wrapped_at != cycle { self.flags &= !TIMER_FLAG; } return self.value as u8; } let flags = self.flags; self.flags &= !PA7_FLAG; flags } pub fn write(&mut self, address: u16, value: u8, cycle: u64) { if address & 0x200 == 0 { self.ram[(address & 0x7F) as usize] = value; return; } let cycle_i = cycle as i64; self.catch_up(cycle_i); if address & 0x04 == 0 { let register = address & 0x03; if register == 0 { self.out_a = value; } else if register == 1 { self.ddr_a = value; } else if register == 2 { self.out_b = value; } else { self.ddr_b = value; } self.edge_detect(); if register < 2 { if let Some(mut listener) = self.port_a_listener.take() { let levels = self.out_a | !self.ddr_a; listener(self, levels, cycle); if self.port_a_listener.is_none() { self.port_a_listener = Some(listener); } } } } else if address & 0x10 != 0 { self.load_timer(value, INTERVALS[(address & 0x03) as usize], cycle_i); } else { self.edge_positive = address & 0x01 != 0; } } /// The port A pins as the controllers drive them (active low). pub fn set_input_a(&mut self, value: u8) { self.input_a = value; self.edge_detect(); } fn edge_detect(&mut self) { let pa7 = (self.out_a | !self.ddr_a) & self.input_a & 0x80 != 0; if pa7 != self.pa7 && pa7 == self.edge_positive { self.flags |= PA7_FLAG; } self.pa7 = pa7; } }