// SPDX-License-Identifier: MIT //! The board: one timeline in master clocks, the page table, and the devices around the CPU //! (`board.py`). //! //! The CPU core (huc6280-rust) calls `read`, `write` and `idle` once per CPU cycle with a //! 21-bit physical address. **The board prices the cycle**: 12 master clocks at low speed, 3 at high //! speed (`pins.high_speed`), plus the VDC/VCE stretch and any `hold`, all inside the callback; it //! never adds `step()`'s return on top (the reference's docs/handoff-brief.md, the bus contract). //! //! Where inside a cycle a device sees the access is U21 (provisional): the base cycle elapses, then //! the access happens at that instant, then the stretch cycle for a VDC or VCE address elapses. //! //! Differences forced by Rust: //! //! - **The CPU owns the board** (`cpu.bus`), so the board cannot hold the CPU. What the //! reference's board does to its CPU (`set_irq_lines`, `irq_disable`, reading `high_speed`) //! goes through the `Pins` each bus callback is handed, at the same points of the cycle and //! in the same order; `attach_cpu` is `System::new`'s. With no CPU (`advance` called on a //! bare board, as the reference's tests do) the lines are tracked and nothing is delivered. //! - **The decode is here**: `page_read` and `page_write` turn the page table's `Handler` names //! into calls (pagetable.rs). //! - **The constructor's keyword arguments** are the fields of `BoardOptions`, which also //! carries the switches the reference keeps as class attributes (`Psg`'s, `Timer`'s). //! - **The pixel path** is the board's field (`pixel_path`); the capture is handed the mixed //! row. **The mixer** is handed the sources in the reference's order: the PSG, the port //! device's own, then `audio_sources`. //! - **The VDC's wait** (issue 0004): the reference's VDC calls the board's `_serve_vram` from //! inside the access; here the access stops at that point and the board serves the chip and //! makes the decode again (`page_read`, `page_write`, `sockets::VdcAccess`, vdc/chip.rs). //! - **`bus_log`** is the tap the reference's scripts make by wrapping the three callables //! (`scripts/state_roundtrip.py`, `observe`): when it is `Some`, every cycle appends the same //! words. use huc6280::{Bus, Pins}; use crate::cpu_io::CpuIo; use crate::hardware_page::map_hardware_page; use crate::machine::{ Machine, ADDRESS_MASK, CPU_CLOCKS_HIGH, CPU_CLOCKS_LOW, HARDWARE_FIRST_PAGE, HSYNC_CLOCKS, LINE_CLOCKS, PAGE_BITS, PC_ENGINE, }; use crate::mixer::Mixer; use crate::mt::PythonRandom; use crate::pagetable::{Handler, PageTable, Ram}; use crate::psg::{Psg, PsgSwitches}; use crate::sockets::{ AudioSource, BoardPins, Card, CardSlot, ControllerPort, DirectPath, IrqLine, PixelPath, PortDevice, ResetKind, VdcAccess, VideoTiming, }; use crate::vce::Vce; use crate::vdc::Vdc; use crate::video::{Capture, Timeline}; /// The reference's `Board(...)` arguments, with its defaults. pub struct BoardOptions { pub machine: Machine, pub card: Option>, pub port: Option>, pub pad: Option>, pub pixel_path: Option>, /// `None` powers up with zeros (U7, deterministic); a seed randomises RAM, VRAM, the /// sprite table and the colour table reproducibly (Python's `random.Random(seed)`). pub power_on_seed: Option, /// The raster line at time zero (U1: the real power-up phase is not published). pub start_line: i64, pub start_offset: i64, /// U19: see `vdc::sequencer::Sequencer::vblank_dots` (default `Some(-16)`; `None` is the /// display start of the line). pub vblank_dots: Option, /// C7: see vdc/dma.rs. pub dma_resumes: bool, /// The PSG's switches (class attributes of the reference's `Psg`). pub psg: PsgSwitches, /// C25 (the reference's `Timer.underflow_window_mclk`). pub timer_underflow_window_mclk: i64, } impl Default for BoardOptions { fn default() -> Self { BoardOptions { machine: PC_ENGINE, card: None, port: None, pad: None, pixel_path: None, power_on_seed: None, start_line: 0, start_offset: 0, vblank_dots: Some(-16), dma_resumes: true, psg: PsgSwitches::default(), timer_underflow_window_mclk: 3, } } } pub struct Board { pub machine: Machine, pub mclk: i64, pub(crate) held: i64, pub(crate) lines: (bool, bool, bool), /// set by the first new line pub(crate) row_end: Option, /// VideoTiming of the line whose row is still being drawn pub(crate) row_timing: Option, pub irq2: IrqLine, pub vce: Vce, /// One VDC instance per `machine.vdc_count` (docs/sockets.md socket 12). `vdcs[0]` is the /// first; the others are run, synced and read for pixels and IRQ1 by the board, but have /// no decode of their own: an add-on (the SuperGrafx's VPC) claims their pages. pub vdcs: Vec, /// The VDC accesses of the decode being made (issue 0004; sockets.rs). pub(crate) vdc_access: VdcAccess, pub psg: Psg, pub cpu_io: CpuIo, pub ram: Ram, pub slot: CardSlot, pub port: Option>, /// Further mixer inputs, after the PSG and the port device's own. pub audio_sources: Vec>, pub mixer: Mixer, pub timeline: Timeline, pub capture: Capture, pub pixel_path: Box, pub pages: PageTable, /// The opcode the CPU is running, as the last bus callback's `Pins` showed it. pub(crate) opcode: u8, /// When `Some`, every CPU cycle appends: for a read or a write `address << 8 | value` /// then `mclk << 2 | kind` (0 read, 1 write); for an idle cycle `mclk << 2 | 2` alone. pub bus_log: Option>, /// When `Some`, every CPU cycle appends (kind, address, value, mclk): kind 0 read, 1 write, /// 2 idle (address and value 0). The lockstep gate's per-cycle mode. pub bus_detail: Option>, } impl Board { pub fn new(options: BoardOptions) -> Result { let machine = options.machine; let mut rng = options.power_on_seed.map(PythonRandom::new); let vce = Vce::new(rng.as_mut()); let vdcs: Vec = (0..machine.vdc_count) .map(|_| { let mut vdc = Vdc::new(rng.as_mut(), options.vblank_dots, options.dma_resumes); vdc.serve = true; // the reference's `serve=self._serve_vram` vdc }) .collect(); if vdcs.is_empty() { return Err("a machine has at least one VDC".to_string()); } let psg = Psg::new(44100.0, options.psg); let mut cpu_io = CpuIo::new(machine.country_pc_engine, options.pad); cpu_io.timer.underflow_window_mclk = options.timer_underflow_window_mclk; let mut ram = Ram::new(machine.work_ram_bytes); if let Some(rng) = rng.as_mut() { for byte in ram.data.iter_mut() { *byte = rng.getrandbits(8) as u8; } } let timeline = Timeline::new(&machine, &vce, options.start_line, options.start_offset)?; let capture = Capture::new(&vce); let mut pages = PageTable::new(); // [hw: pcetech 3] banks $F8-$FB work RAM ($F9-$FB mirror $F8), $FF the hardware page. pages.set_base(0x1F00, 0x1F7F, Handler::Ram); map_hardware_page(&mut pages); if let Some(card) = options.card.as_ref() { for (first, last) in card.claims() { pages.claim(first, last, Handler::Card)?; } } if let Some(port) = options.port.as_ref() { for (first, last) in port.claims() { pages.claim(first, last, Handler::Port)?; } } let pixel_path = options.pixel_path.unwrap_or_else(|| Box::new(DirectPath)); for (first, last) in pixel_path.claims() { pages.claim(first, last, Handler::PixelPath)?; } let mut board = Board { machine, mclk: 0, held: 0, lines: (false, false, false), row_end: None, row_timing: None, irq2: IrqLine::new("IRQ2"), vce, vdcs, vdc_access: VdcAccess::default(), psg, cpu_io, ram, slot: CardSlot::new(options.card), port: options.port, audio_sources: Vec::new(), mixer: Mixer::default(), timeline, capture, pixel_path, pages, opcode: 0, bus_log: None, bus_detail: None, }; let divider = board.vce.dot_divider(); for vdc in board.vdcs.iter_mut() { vdc.hsync(board.timeline.line_start, divider); if board.timeline.line >= board.timeline.frame_lines - 3 { vdc.vsync(); } } Ok(board) } /// The first VDC (the reference's `board.vdc`). pub fn vdc(&mut self) -> &mut Vdc { &mut self.vdcs[0] } /// Mixed stereo samples (44.1 kHz) of every audio source up to the clock now. pub fn take_audio(&mut self) -> Vec<(i16, i16)> { let mut sources: Vec<&mut dyn AudioSource> = vec![&mut self.psg]; if let Some(port) = self.port.as_mut() { sources.extend(port.audio_sources()); } for source in self.audio_sources.iter_mut() { sources.push(source.as_mut()); } self.mixer.take(self.mclk, sources) } /// The buttons held on pad `port` (`ControllerPort::set_pressed`; the reference's host /// sets `pad.pressed` on the object it kept). pub fn set_pressed(&mut self, port: usize, pressed: u16) { if let Some(device) = self.cpu_io.port.device.as_mut() { device.set_pressed(port, pressed); } } // -- the decode ------------------------------------------------------------------------------- fn cd_attached(&self) -> bool { self.port.as_ref().is_some_and(|port| port.present()) } /// The reference's `pages.read`: one decode for every bus master. A VDC access that stops /// to wait (`Vdc::serve_wanted`) is served and the decode made again (issue 0004). pub fn page_read(&mut self, address: u32, mclk: i64) -> u8 { self.vdc_access.begin(); loop { let value = self.decode_read(address, mclk); match self.vdc_waiting() { None => return value, Some(index) => self.serve_access(index, mclk), } } } /// The reference's `pages.write`; a VDC's wait as in `page_read`. pub fn page_write(&mut self, address: u32, value: u8, mclk: i64) { self.vdc_access.begin(); loop { self.decode_write(address, value, mclk); match self.vdc_waiting() { None => return, Some(index) => self.serve_access(index, mclk), } } } /// The VDC whose access stopped to wait, if any: as `VdcAccess` saw it, or (a device that /// called `Vdc::read` or `write` itself, not `BoardPins::vdc_read` or `vdc_write`) as the /// chip says; such an access is made again with the device's `mclk`. #[inline] fn vdc_waiting(&self) -> Option { self.vdc_access .stopped() .or_else(|| self.vdcs.iter().position(|vdc| vdc.serve_wanted)) } /// The rest of the reference's `Vdc._data_access` with `serve` wired: `served = /// serve(chip)`, the CPU held for `served - mclk` if positive, and the stopped access made at /// `max(served, mclk)` when the decode is made again. fn serve_access(&mut self, index: usize, mclk: i64) { self.vdcs[index].serve_wanted = false; let served = self.serve_vram(index); if served > mclk { self.hold(served - mclk); } self.vdc_access.again(index, served.max(mclk)); } fn decode_read(&mut self, address: u32, mclk: i64) -> u8 { let address = address & ADDRESS_MASK; match self.pages.handler(address) { Handler::Unmapped => 0xFF, Handler::Ram => self.ram.read(address), Handler::Vdc => self.vdc_access.read(&mut self.vdcs, 0, address & 3, mclk), Handler::Vce => self.vce.read(address & 7, mclk), Handler::CpuIo => { let cd_attached = self.cd_attached(); self.cpu_io.read(address & 0x1FFF, mclk, cd_attached) } Handler::Card => { let Board { slot, irq2, held, vdcs, vdc_access, opcode, .. } = self; let mut pins = BoardPins { irq2, held, vdcs, access: vdc_access, opcode: *opcode, card: None, }; match slot.card.as_mut() { Some(card) => card.read(address, mclk, &mut pins), None => 0xFF, } } Handler::Port => { let Board { slot, port, irq2, held, vdcs, vdc_access, opcode, .. } = self; let mut pins = BoardPins { irq2, held, vdcs, access: vdc_access, opcode: *opcode, card: slot.card.as_deref_mut(), }; match port.as_mut() { Some(port) => port.read(address, mclk, &mut pins), None => 0xFF, } } Handler::PixelPath => { let Board { slot, pixel_path, irq2, held, vdcs, vdc_access, opcode, .. } = self; let mut pins = BoardPins { irq2, held, vdcs, access: vdc_access, opcode: *opcode, card: slot.card.as_deref_mut(), }; pixel_path.read(address, mclk, &mut pins) } } } fn decode_write(&mut self, address: u32, value: u8, mclk: i64) { let address = address & ADDRESS_MASK; match self.pages.handler(address) { Handler::Unmapped => {} Handler::Ram => self.ram.write(address, value), Handler::Vdc => self .vdc_access .write(&mut self.vdcs, 0, address & 3, value, mclk), Handler::Vce => self.vce.write(address & 7, value, mclk), Handler::CpuIo => self .cpu_io .write(address & 0x1FFF, value, mclk, &mut self.psg), Handler::Card => { let Board { slot, irq2, held, vdcs, vdc_access, opcode, .. } = self; let mut pins = BoardPins { irq2, held, vdcs, access: vdc_access, opcode: *opcode, card: None, }; if let Some(card) = slot.card.as_mut() { card.write(address, value, mclk, &mut pins); } } Handler::Port => { let Board { slot, port, irq2, held, vdcs, vdc_access, opcode, .. } = self; let mut pins = BoardPins { irq2, held, vdcs, access: vdc_access, opcode: *opcode, card: slot.card.as_deref_mut(), }; if let Some(port) = port.as_mut() { port.write(address, value, mclk, &mut pins); } } Handler::PixelPath => { let Board { slot, pixel_path, irq2, held, vdcs, vdc_access, opcode, .. } = self; let mut pins = BoardPins { irq2, held, vdcs, access: vdc_access, opcode: *opcode, card: slot.card.as_deref_mut(), }; pixel_path.write(address, value, mclk, &mut pins); } } } // -- the CPU ---------------------------------------------------------------------------------- #[inline] fn cycle_clocks(pins: &Pins) -> i64 { if pins.high_speed { CPU_CLOCKS_HIGH } else { CPU_CLOCKS_LOW } } fn finish_access(&mut self, address: u32, cycle: i64, pins: &mut Pins) { // The disable mask a write to $1402 left (the reference's `_irq_disable_changed`, // called from inside the write: nothing has touched the CPU since). if self.cpu_io.irq.mask_changed { self.cpu_io.irq.mask_changed = false; pins.set_irq_disable(self.cpu_io.irq.disable); } // [hw: pcetech 3 note 3] a VDC or VCE access takes one more cycle, reads and writes. // C4: ares charges it only outside burst mode and only for the VDC; Mednafen charges // a statistical VRAM penalty. MacDonald's rule, tier 2, is this model's. let page = (address >> PAGE_BITS) as usize; if (HARDWARE_FIRST_PAGE..HARDWARE_FIRST_PAGE + 8).contains(&page) { self.held += cycle; } for vdc in self.vdcs.iter_mut() { self.held += vdc.take_hold(); } if self.held != 0 { let held = std::mem::take(&mut self.held); self.advance_with(held, Some(&mut *pins)); } // the access itself (a status read) may have changed a line self.deliver_lines(Some(pins), false); } /// A device stretches the current cycle (RDY low); applied when the access returns. pub fn hold(&mut self, clocks: i64) { self.held += clocks; } // -- time ------------------------------------------------------------------------------------- /// Let `clocks` master clocks pass and run every device up to the new time, with no CPU /// to hand the interrupt lines to (the reference's `advance` on a board without one). pub fn advance(&mut self, clocks: i64) { self.advance_with(clocks, None); } fn advance_with(&mut self, clocks: i64, pins: Option<&mut Pins>) { self.mclk += clocks; self.run_to(self.mclk); self.deliver_lines(pins, false); } fn run_to(&mut self, target: i64) { while self.step(target).is_some() {} self.settle(target); } /// Handle the earliest event due at or before `target` and return its time; `None` if /// nothing is due. fn step(&mut self, target: i64) -> Option { let line_at = self.timeline.next_line_start(); let row_at = self.row_end; let mut vdc_at = self.vdcs[0].next_event(); for other in &self.vdcs[1..] { if let Some(at) = other.next_event() { if vdc_at.is_none_or(|first| at < first) { vdc_at = Some(at); } } } let timer_at = self.cpu_io.next_event(); if let Some(row_at) = row_at .filter(|&at| at <= target && at <= line_at && vdc_at.is_none_or(|vdc_at| at <= vdc_at)) { self.end_row(row_at); return Some(row_at); } if line_at <= target && vdc_at.is_none_or(|at| line_at <= at) { self.new_line(line_at); return Some(line_at); } if let Some(vdc_at) = vdc_at.filter(|&at| at <= target) { for chip in self.vdcs.iter_mut() { chip.catch_up(vdc_at); } return Some(vdc_at); } if let Some(timer_at) = timer_at.filter(|&at| at <= target) { self.cpu_io.catch_up(timer_at); return Some(timer_at); } None } /// A VDC holds the CPU (/BUSY on RDY) until its pending VRAM access is served: run every /// device, the line clock included, event by event until then, and return that time. The /// held clocks reach `mclk` when the access returns (`hold`); issue 0004. pub(crate) fn serve_vram(&mut self, index: usize) -> i64 { let mut now = self.mclk; while self.vdcs[index].bsy() { // the next line start always exists: this always moves if let Some(at) = self.step(1 << 62) { now = now.max(at); } } self.settle(now); now } fn settle(&mut self, target: i64) { for chip in self.vdcs.iter_mut() { chip.catch_up(target); } self.cpu_io.catch_up(target); // socket 5, issue 0002: a port device acts by itself let Board { slot, port, irq2, held, vdcs, vdc_access, opcode, .. } = self; if let Some(port) = port.as_mut() { let mut pins = BoardPins { irq2, held, vdcs, access: vdc_access, opcode: *opcode, card: slot.card.as_deref_mut(), }; port.catch_up(target, &mut pins); } } fn new_line(&mut self, at: i64) { for vdc in self.vdcs.iter_mut() { vdc.catch_up(at); } // the line that is ending; its row ends 237 clocks later self.row_timing = Some(self.timeline.timing(&self.vce)); self.row_end = Some(at + HSYNC_CLOCKS); self.timeline.advance(&self.vce); let divider = self.vce.dot_divider(); let vsync = self.timeline.line == self.timeline.frame_lines - 3; for vdc in self.vdcs.iter_mut() { vdc.hsync(at, divider); if vsync { // after the line's hsync [emu: MAME huc6260.cpp: VSync changes 30 master cycles // after HSync falls; the VDC acts on it at the line step] vdc.vsync(); } } } /// The previous line's picture row ends as this line's sync does: hand it to the capture /// before the VDC starts the new line's geometry (events at `at` itself wait). fn end_row(&mut self, at: i64) { for vdc in self.vdcs.iter_mut() { vdc.catch_up(at - 1); } let timing = self.row_timing.expect("a row ends after a line began"); let dots = (LINE_CLOCKS / timing.dot_divider) as usize; let rows: Vec> = self.vdcs.iter().map(|vdc| vdc.pixel_row(dots)).collect(); let row = self.pixel_path.mix(&timing, rows); self.capture.finish_line(&timing, &row, &mut self.vce); self.row_end = None; } // -- interrupts ------------------------------------------------------------------------------- /// (IRQ1, IRQ2, TIQ) as the interrupt controller sees them, before the mask. pub fn lines(&self) -> (bool, bool, bool) { // IRQ1 is shared by every VDC (wired-OR; each is acknowledged by reading its status). ( self.vdcs.iter().any(|vdc| vdc.irq1()), self.irq2.level(), self.cpu_io.tiq(), ) } pub(crate) fn deliver_lines(&mut self, pins: Option<&mut Pins>, force: bool) { let lines = self.lines(); self.cpu_io.irq.irq1 = lines.0; self.cpu_io.irq.irq2 = lines.1; if lines != self.lines || force { if let Some(pins) = pins { pins.set_irq_lines(lines.0, lines.1, lines.2); } } self.lines = lines; } // -- reset ------------------------------------------------------------------------------------ /// Tell the socket devices of a reset event; at power-on also the CPU's on-die timer, /// interrupt controller, I/O port and I/O buffer. /// /// Not a power cycle (issue 0004): the board is built at power-on, and construction is the /// power-on state of the VDC, VCE, PSG and RAM (U7: zeros, or the seed's values). None of /// them is put back here: RAM has no reset; the VCE has no reset pin [wiki: VCE pinout]; /// the VDC has /RST [wiki: VDC pinout] but Hudson's manual says nothing of what it clears, /// and the PSG manual is silent too; the PC Engine has no reset button (`ResetKind`). A host /// that wants to power the machine on again builds a new `Board`. pub fn reset(&mut self, kind: ResetKind) { let Board { slot, port, pixel_path, irq2, held, vdcs, vdc_access, opcode, .. } = self; if let Some(card) = slot.card.as_mut() { let mut pins = BoardPins { irq2, held, vdcs, access: vdc_access, opcode: *opcode, card: None, }; card.reset(kind, &mut pins); } if let Some(port) = port.as_mut() { let mut pins = BoardPins { irq2, held, vdcs, access: vdc_access, opcode: *opcode, card: slot.card.as_deref_mut(), }; port.reset(kind, &mut pins); } let mut pins = BoardPins { irq2, held, vdcs, access: vdc_access, opcode: *opcode, card: slot.card.as_deref_mut(), }; pixel_path.reset(kind, &mut pins); if kind == ResetKind::PowerOn { self.cpu_io.reset(); } } } impl Bus for Board { fn read(&mut self, address: u32, pins: &mut Pins) -> u8 { self.opcode = pins.opcode(); let cycle = Board::cycle_clocks(pins); self.advance_with(cycle, Some(pins)); let value = self.page_read(address, self.mclk); self.finish_access(address, cycle, pins); if let Some(log) = self.bus_log.as_mut() { log.push((address as u64) << 8 | value as u64); log.push((self.mclk as u64) << 2); } if let Some(detail) = self.bus_detail.as_mut() { detail.push((0, address, value, self.mclk)); } value } fn write(&mut self, address: u32, value: u8, pins: &mut Pins) { self.opcode = pins.opcode(); let cycle = Board::cycle_clocks(pins); self.advance_with(cycle, Some(pins)); self.page_write(address, value, self.mclk); self.finish_access(address, cycle, pins); if let Some(log) = self.bus_log.as_mut() { log.push((address as u64) << 8 | value as u64); log.push((self.mclk as u64) << 2 | 1); } if let Some(detail) = self.bus_detail.as_mut() { detail.push((1, address, value, self.mclk)); } } /// A cycle with no bus transfer (internal cycle or dummy read: nothing reacts). fn idle(&mut self, _address: Option, pins: &mut Pins) { self.opcode = pins.opcode(); let cycle = Board::cycle_clocks(pins); self.advance_with(cycle, Some(pins)); if let Some(log) = self.bus_log.as_mut() { log.push((self.mclk as u64) << 2 | 2); } if let Some(detail) = self.bus_detail.as_mut() { detail.push((2, 0, 0, self.mclk)); } } }