// SPDX-License-Identifier: MIT //! Assemble a console: machine row, BIOS by hash, cartridge through the front door. //! //! A transcription of a5200-python's `system.py` (at cf82d44). Rust-forced differences: //! `Console::new` takes a loaded [`Cart`] (Python also accepts a path or bytes and loads it; //! [`Console::open`] does that for a path) and returns a `Result`; the board lives inside its //! CPU (`console.cpu.bus`, see board.rs), reached through `board()` / `board_mut()`; //! `run_frames` hands the board to the controllers. use std::path::Path; use crate::bios::find_bios; use crate::board::{Board, Cpu, Running}; use crate::cartridge::{load_cartridge, Cart, Source}; use crate::controllers::Controllers; use crate::machine::Machine; /// A 5200 with a cartridge in and controllers attached. pub struct Console { pub machine: Machine, pub cart: Cart, pub cpu: Cpu, pub controllers: Controllers, pub bios_source: String, } impl Console { pub fn new( cart: Cart, machine: Machine, bios: Option>, bios_path: Option<&Path>, ) -> Result { let (bios, bios_source) = match bios { None => find_bios(machine.bios_sha1, bios_path).map_err(|e| e.to_string())?, Some(bios) => (bios, "".to_string()), }; let mut cpu = Board::new(machine, &bios, cart.port())?; let controllers = Controllers::new(&mut cpu.bus); Ok(Console { machine, cart, cpu, controllers, bios_source, }) } /// `Console(path, machine, mapping=...)`: load the cartridge, then build the console. pub fn open(path: &Path, machine: Machine, mapping: Option<&str>) -> Result { let cart = load_cartridge(Source::Path(path), mapping, None).map_err(|e| e.to_string())?; Console::new(cart, machine, None, None) } pub fn board(&self) -> &Board { &self.cpu.bus } pub fn board_mut(&mut self) -> &mut Board { &mut self.cpu.bus } pub fn frames(&self) -> u64 { self.cpu.bus.frames } pub fn run_frames(&mut self, n: u64) { for _ in 0..n { self.controllers.frame(&mut self.cpu.bus); self.cpu.run_frame(); } } }