// SPDX-License-Identifier: MIT //! A whole console: 6502-rust's MOS6502, as the 6507, on a [`Board`]. //! //! The 6507 is the 6502 die with A13-A15, IRQ and NMI not bonded out (DOC: CSG //! 6500 data sheet, 6507 pin-out; docs/undocumented-behavior.md "The CPU"): the //! board masks every address to 13 bits and never requests an interrupt, and //! nothing else differs (DIE: Sim2600 runs the 6507 netlist, and the board's //! writes land on its half clocks, scripts/die_board.py). The CPU keeps its //! 16-bit PC, as MAME and Stella show it (OBSERVED: MAME 0.285, Stella 2c3d702 //! traces). //! //! Rust-forced differences (a2600-python `system.py` at 28fb095): the CPU owns //! the board (`Mos6502`, the board as its bus), so `system.board` is //! [`System::board`] / [`System::board_mut`] (`cpu.bus`); `MOS6507` is a type //! alias, not a subclass; `System.loaded` keeps the load's description //! ([`LoadedInfo`]) since the board owns the cartridge; `from_rom` takes the //! ROM as a [`Rom`] and the scheme and options explicitly; constructors //! return `Result` where Python raises. `System(..., board=, cpu=)` is //! [`System::with_cpu`]: rewiring 6502-python's replaceable bus callables is //! replacing the core's `bus` field, so the running CPU must be a //! `Mos6502` and the board it was on comes back to the caller (in //! Python the old board lives on beside it, unwired). A host whose CPU sits //! on another bus type moves it with 6502-rust's `capture_state` / //! `restore_state` onto a `Mos6502` first. use std::path::Path; use sixfiveohtwo::Mos6502; // 741419a: bus callables replaceable (read_byte/write_byte) use crate::board::Board; use crate::cartridge::connector::Cartridge; use crate::cartridge::{load, mame_hash, LoadedInfo, Options, Rom}; use crate::machine::{Machine, VCS_NTSC}; /// The 6507: a 6502 whose board decodes 13 address lines and wires no interrupts. pub type MOS6507 = Mos6502; /// Machine + cartridge, powered on. pub struct System { pub machine: Machine, pub cpu: MOS6507, pub instructions: u64, pub loaded: Option, } impl System { /// `board` hands in a board built elsewhere (with existing chips, gates.rs). pub fn new(cartridge: Box, machine: Machine, board: Option) -> Result { let board = match board { Some(board) => board, None => Board::new(machine, cartridge, None, None, 0)?, }; let mut system = System { machine, cpu: Mos6502::new(board), instructions: 0, loaded: None, }; system.power(); Ok(system) } /// `System(board.cartridge, machine, board=board)`: a board built elsewhere. pub fn with_board(board: Board, machine: Machine) -> System { let mut system = System { machine, cpu: Mos6502::new(board), instructions: 0, loaded: None, }; system.power(); system } /// `System(board.cartridge, machine, board=board, cpu=cpu)`. /// /// `cpu` hands in a 6502 that has been running: the 7800's SALLY, which /// executes the BIOS and then jumps through the cartridge's reset vector /// with nothing resetting it (a7800-python's issue 0001, citing the /// BIOS disassembly; the 7800's fact, not this board's). Its bus is rewired to this board /// and it is not powered on: registers, the instruction in progress and /// any pending request stay as they stand, as the TIA and RIOT handed to /// [`Board`] do. Without it a fresh 6507 is powered on. Returns the board /// the CPU was wired to before. pub fn with_cpu(board: Board, machine: Machine, mut cpu: MOS6507) -> (System, Board) { let old = std::mem::replace(&mut cpu.bus, board); // 6502-python's replaceable bus callables let system = System { machine, cpu, instructions: 0, loaded: None, }; (system, old) } /// `System(cartridge)` on the reference row. pub fn with_cartridge(cartridge: Box) -> System { System::new(cartridge, VCS_NTSC, None).expect("the reference row is on the grid") } /// Load `rom` (identified as [`load`] does; `scheme` and `options` go to it). /// /// The row's CPU clock goes to the cartridge as `cpu_hz` unless /// `options` name one. A clocked scheme's oscillator (DPC's music) runs /// in its own hertz, free of the console's clock: DOC Crane's patent US /// 4,644,495 ("an internally generated asynchronous clock", "free /// running"; the cartridge receives only address, chip-select, data and /// power lines), DOC Horton (a 560 kOhm resistor on the cartridge and a /// capacitor on the DPC die set it; touching the resistor bends the /// pitch, on his own cartridge), DOC nocash (the 24-pin slot carries no /// clock). So its ticks per CPU cycle follow each row's crystal, and the /// music plays at the same pitch on every row (docs/cartridge.md). pub fn from_rom(rom: Rom, machine: Machine, scheme: Option<&str>, mut options: Options) -> Result { if options.cpu_hz.is_none() { options.cpu_hz = Some(machine.timing.cpu_hz()); } let loaded = load(rom, scheme, None, &mame_hash(), options).map_err(|e| e.0)?; let info = loaded.info(); let mut system = System::new(loaded.cartridge, machine, None)?; system.loaded = Some(info); Ok(system) } /// `from_rom` for a file with the defaults. pub fn from_path(path: &Path, machine: Machine) -> Result { System::from_rom(Rom::Path(path), machine, None, Options::default()) } pub fn board(&self) -> &Board { &self.cpu.bus } pub fn board_mut(&mut self) -> &mut Board { &mut self.cpu.bus } /// Switch on: registers from the machine's power-on parameters, then the reset sequence. pub fn power(&mut self) { let power = self.machine.power_on; let cpu = &mut self.cpu; cpu.a = power.a; cpu.x = power.x; cpu.y = power.y; cpu.s = power.s; cpu.p = power.p; cpu.frame.request_reset(); } // -- running --------------------------------------------------------- pub fn cycles(&self) -> u64 { self.board().cycles } /// One instruction (or the reset sequence); returns CPU cycles, halted ones included. pub fn step(&mut self) -> u64 { let before = self.board().cycles; self.cpu.step(); self.instructions += 1; self.board().cycles - before } /// Run whole instructions until at least `cycles` more CPU cycles have passed. pub fn run_cycles(&mut self, cycles: u64) { let end = self.board().cycles + cycles; let mut count = 0; while self.cpu.bus.cycles < end { self.cpu.step(); count += 1; } self.instructions += count; } /// Run until the program has started `frames` more frames (VSYNC rising edges). pub fn run_frames(&mut self, frames: u64) { let target = self.board().tia.frame + frames; let mut count = 0; while self.cpu.bus.tia.frame < target { self.cpu.step(); count += 1; } self.instructions += count; } /// Run until `done()` (checked after every instruction); false on timeout. pub fn run_until(&mut self, mut done: impl FnMut(&System) -> bool, max_cycles: u64) -> bool { let end = self.board().cycles + max_cycles; while self.cpu.bus.cycles < end { self.cpu.step(); self.instructions += 1; if done(self) { return true; } } false } }