// SPDX-License-Identifier: MIT //! Suzy: the sprite engine's registers, the math unit, joystick and switches, the //! cart data port. Imports nothing from the board (`suzy/__init__.py`). //! //! Register map: DOC Appx 2 ("Suzy Addresses") unless tagged. //! //! Differences forced by Rust: the Python Suzy holds a reference to the cart port (the //! board owns it here and passes it to `read` and `write`); the math unit's reference to //! the register file is an argument. `run_engine` takes the engine out of Suzy while it //! runs, because the engine reaches Suzy through the board ([`engine::EngineBus`]). pub mod engine; pub mod math; use serde::{Deserialize, Serialize}; use crate::cart::port::CartPort; use engine::{EngineBus, SpriteEngine}; use math::MathUnit; #[derive(Clone, Serialize, Deserialize)] pub struct Suzy { pub busen: u8, pub engine: SpriteEngine, pub joystick: u8, pub leds: u8, pub math: MathUnit, pub regs: Vec, // 64 bytes (an array of 64 has no serde implementation) pub sprcoll: u8, pub sprctl0: u8, pub sprctl1: u8, pub sprgo: u8, pub sprinit: u8, pub sprite_busy: u8, pub sprsys: u8, pub stop_request: u8, pub switches: u8, pub unsafe_: u8, } impl Default for Suzy { fn default() -> Self { Self::new() } } impl Suzy { pub fn new() -> Suzy { Suzy { // $FC00-$FC7F: the SCB registers (24 words $FC00-$FC2F) and the math registers. regs: vec![0; 0x40], // DOC reset x: HYPOTHESIS 0 math: MathUnit::new(), sprctl0: 0, sprctl1: 0, sprcoll: 0, sprinit: 0, busen: 0, // DOC SUZYBUSEN reset 0 sprgo: 0, // DOC SPRGO reset 0 sprsys: 0, // the written control bits sprite_busy: 0, // SPRSYS read B0 stop_request: 0, unsafe_: 0, joystick: 0, // buttons pressed = 1 (Appx 2 reads them as bits) switches: 0, leds: 0, engine: SpriteEngine::new(), } } pub fn word(&self, offset: usize) -> i64 { self.regs[offset] as i64 | ((self.regs[offset + 1] as i64) << 8) } pub fn set_word(&mut self, offset: usize, value: i64) { self.regs[offset] = (value & 0xFF) as u8; self.regs[offset + 1] = ((value >> 8) & 0xFF) as u8; } // -- registers --------------------------------------------------------------------- pub fn read(&mut self, address: u16, now: i64, cart: &mut CartPort) -> u8 { let reg = (address & 0xFF) as u8; if reg < 0x80 { if 0x52 <= reg { // DOC §16 "unsafe access detection is broken for math" + TEST ROM math 1-7 // (SPRSYS reads B2 set after math accesses: "unsafe is always set"). // HYPOTHESIS: every CPU access to a math register sets it. self.unsafe_ = 1; } // TEST ROM memio 3: $FC40-$FC7F mirrors $FC00-$FC3F (one 64-byte file). return self.regs[(reg & 0x3F) as usize]; } if reg == 0x88 { return 0x01; // DOC SUZYHREV = '01' } if reg == 0x92 { return self.read_sprsys(now); } if reg == 0xB0 { return self.read_joystick(); } if reg == 0xB1 { return self.switches & 0x07; } if reg == 0xB2 { return cart.read(0); // DOC: RCART0 uses CART0/ as the strobe } if reg == 0xB3 { return cart.read(1); } // HYPOTHESIS (U18): write-only and unallocated addresses read 0. 0 } pub fn read_joystick(&self) -> u8 { let mut value = self.joystick; if self.sprsys & 0x08 == 0 { // DOC Appx 2 JOYSTICK, two columns: with Lefthand = 0 the four direction bits // swap Up<->Down and Left<->Right (C11; U12 for a hardware result). value = (value & 0x0F) | ((value & 0x80) >> 1) | ((value & 0x40) << 1) | ((value & 0x20) >> 1) | ((value & 0x10) << 1); } value } pub fn read_sprsys(&self, now: i64) -> u8 { // DOC Appx 2 SPRSYS (read): B7 math in process, B6 mathbit, B5 last carry, // B4 Vstretch, B3 Lefthand, B2 unsafe access, B1 stop request, B0 sprite busy. (self.math.working(now) << 7) | (self.math.mathbit << 6) | (self.math.last_carry << 5) | (self.sprsys & 0x18) | (self.unsafe_ << 2) | (self.stop_request << 1) | self.sprite_busy } pub fn write(&mut self, address: u16, value: u8, now: i64, cart: &mut CartPort) { let reg = (address & 0xFF) as u8; if reg < 0x80 { let index = (reg & 0x3F) as usize; // TEST ROM memio 3: one 64-byte file, mirrored at $FC40 self.regs[index] = value; if index & 1 == 0 { // DOC: any CPU write to an LSB sets the MSB to 0 (and the math rule "B, D, // F, H, K, M force 0 into A, C, E, G, J, L" is the same rule). self.regs[index + 1] = 0; } if reg >= 0x52 { self.unsafe_ = 1; // as the read above (DOC §16 + TEST ROM math 1-7) self.math.written(&mut self.regs, index, now); } return; } if reg == 0x80 { self.sprctl0 = value; } else if reg == 0x81 { self.sprctl1 = value; } else if reg == 0x82 { self.sprcoll = value; } else if reg == 0x83 { self.sprinit = value; } else if reg == 0x90 { self.busen = value & 1; } else if reg == 0x91 { self.sprgo = value; // DOC Appx 2: setting or clearing SPRGO clears the stop request. self.stop_request = 0; if value & 1 != 0 { // DOC (start-here "Suzy takes the bus" step 3): busy is set at once. self.sprite_busy = 1; self.engine.start(); } else { // DOC Spec §6: "forced off by writing a 00 to SPRGO". self.engine.steps = None; self.sprite_busy = 0; } } else if reg == 0x92 { self.sprsys = value; self.math.signed = (value >> 7) & 1; self.math.accumulate = (value >> 6) & 1; if value & 0x04 != 0 { self.unsafe_ = 0; } if value & 0x02 != 0 { self.stop_request = 1; } } else if reg == 0xB2 { cart.write(0, value); } else if reg == 0xB3 { cart.write(1, value); } else if reg == 0xC0 { self.leds = value; } } } /// Run the sprite engine a step on `bus`; True when the list is done (Suzy's /// `run_engine`). The engine is taken out of Suzy while it runs and put back. pub fn run_engine(bus: &mut B) -> bool { let mut engine = std::mem::take(&mut bus.suzy().engine); let done = engine.run(bus); bus.suzy().engine = engine; done }