//! The board: both CPUs' memory maps, the Z80's bus and reset lines, and the devices. //! //! Transcribed from megadrive-python's `board.py`. Sources (its docstring and //! docs/start-here.md have the tables): CMD `gen-hw.txt` sections 1-2 for the maps, the //! byte/word rules, the lock-ups, the bus request and reset registers and the bank, //! all "checked on the real thing"; the Manual for the version register and TMSS; MoD's //! `gen_test_z80_bus` (forum t=3021, several machines) for the Z80's failed reads of //! 68000 RAM; and Nuked-MD (closed-box (Nuked-MD)) for the refresh and arbitration constants, //! each with its measurement in the reference's comments. //! //! Rust's differences (docs/handoff-brief.md, "The shape Rust forces"): //! - [`Board`] is the Z80's bus, and holds the VDP and [`Devices`], everything else. //! - What the reference's board does through the system's callbacks (`catch_up_z80`, //! `z80_busack`, `on_z80_reset`, `on_z80_resume`) -- the 68000's side of the board -- //! is in `system.rs`, on the 68000's bus, which owns the Z80 and this board. //! - The 68000's IRC and `step_clocks`, which the reference reads from the CPU, are //! mirrored here by the 68000's bus at every access. //! - The probe (`System.z80_probe`), which swaps the Z80's callables in the reference, //! is [`Devices::probe`], consulted first by the Z80's read and write. //! - [`Lockup`] is recorded in [`Devices::lockup`] rather than raised. //! - The Z80 writing $A11100 or $A11200 through its bank window, which the reference //! handles by probing and resetting the Z80 from inside its own step, stops the run //! with a message; a DMA whose source is the Z80's space or $A1xxxx does not run the //! Z80 forward first, as the reference's would. //! - The reference's `Board.cartridge`, the object that answers the 68000 at //! $000000-$3FFFFF (the slot, the TMSS boot ROM, or `_LowDecode` over a socket's claims, //! chosen by `_remap`), is [`Devices::low`], which of the three answers; the cartridge //! on the slot's bus (the reference's `game_cartridge`, `_slot_bus`) is //! [`Devices::cartridge`]. The expansion sockets' devices are owned here and lent out //! for each call ([`Devices::with_device`], src/sockets.rs). use std::collections::HashMap; use std::sync::Arc; use sn76489_rust::{SN76489, YM7101_PSG}; use ym2612_rust::YM2612; use crate::arbiter::{ z80_access_wait, CUTOFF_MCLK, IDLE_GRANT_MCLK, M68K_EDGE_PHASE, OWN_CYCLE_CARTRIDGE_MCLK, OWN_CYCLE_VDP_MCLK, }; use crate::cartridge::{BootRom, Cartridge}; use crate::io::{Device, IOChip}; use crate::machine::{Machine, M68K_DIVIDER, Z80_DIVIDER}; use crate::sockets::{BusDevice, PortDevice, SlotDevice, CLAIMABLE, CLAIM_PAGE, M68K, Z80}; use crate::vdp::{Vdp, VdpHost}; /// The cartridge-side refresh: every 128 68000 clocks of real time, the next 68000 bus /// cycle absorbs it, and if that cycle is a cartridge access it takes 2 clocks longer /// (Kabuto, "The bus system"; made exact against Nuked-MD in four probe loops). pub const CART_REFRESH_MCLK: i64 = 896; pub const CART_REFRESH_STALL: i64 = 14; /// ... unless the bus was free long enough for it to run by itself, from this long /// after it fell due (Nuked-MD; docs/worklog.md, "The cartridge refresh when the bus is /// free"). pub const CART_REFRESH_SELF_MCLK: i64 = 112; /// The same refresh falling on a 68000 access to the Z80's space stretches it by one /// 68000 clock, not two -- and by none if it fell due only in the last 68000 clock /// before /AS (Nuked-MD, 519 refreshes; docs/worklog.md, "The one-frame slip"). pub const Z80_REFRESH_STALL: i64 = 7; pub const Z80_REFRESH_LATE: i64 = 7; /// A refresh that stretches a cartridge access pulses /REF from 39 to this long after /// it fell due; an access to the Z80's space under the pulse waits a 68000 clock /// (Nuked-MD; docs/worklog.md, "Under the /REF pulse"). pub const CART_REFRESH_PULSE_END_MCLK: i64 = 46; /// The work-RAM refresh (Kabuto: "RAM accesses get their own further slowdowns"), /// fitted to Nuked-MD's 68000 in Overdrive 2's 3D scene (docs/worklog.md "The work-RAM /// refresh"). `RAM_REFRESH_HIDDEN` false keeps the old rule. pub const RAM_REFRESH_STALL: i64 = 21; pub const RAM_REFRESH_AFTER_MCLK: i64 = 819; pub const RAM_REFRESH_TRIGGER_MCLK: i64 = 98; pub const RAM_REFRESH_SELF_MCLK: i64 = 112; pub const RAM_REFRESH_HIDDEN: bool = true; /// The clocks the timer counts from a refresh to the next falling due, in MCLK. pub const RAM_REFRESH_COUNT_MCLK: i64 = 112 * 7; /// Every 68000 access to the Z80's space costs one extra 68000 clock (Nuked-MD and /// MAME's `before_delay(1)` agreeing; the user's decision 1). pub const Z80_SPACE_STALL: i64 = M68K_DIVIDER; /// A Z80 access to the VDP ($7F00-$7F1F) goes over the 68000's bus, through the same /// arbitration as one through the bank window (Nuked-MD; closed-box (Nuked-MD) for reads). pub const Z80_VDP_ARBITRATED: bool = true; /// A Z80 access over the 68000's bus delays the 68000 only from the bus cycle it next /// wants (Nuked-MD's 68000 trace; docs/worklog.md "The ninja's arm colours"). pub const Z80_GRANT_OVERLAPS_IDLE: bool = true; /// The Z80 is brought up to each 68000 bus cycle before it starts (docs/worklog.md, /// "The Z80's grant inside an instruction"). OBSERVED (Nuked-MD @ 594ca2a, closed box). pub const Z80_CATCH_UP_EACH_CYCLE: bool = true; /// The cartridge refresh stretches a Z80 access over the 68000's bus only if it goes /// to the cartridge. OBSERVED (Nuked-MD @ 594ca2a, closed box). pub const Z80_REFRESH_CARTRIDGE_ONLY: bool = true; /// A discrete YM2612's ports 1-3 answer with the status last read at port 0, until that /// copy fades to 0 (hardware run 1 of alewman-probe on a Model 1601 with TMSS, tests YMPT /// and YMP2: a cleared flag still reads 1 there straight after, 0 twenty frames on, and 0 /// after a quiet second while port 0 reads 1). The cart's YMDK test (runs 4 and 6, the same /// both times) finds the copy still there 3-4 NTSC frames on and gone 5-6 frames on; this is /// the middle, 4.5 frames of 896,040 MCLK. It varies: frame-synced runs 7 and 8 lost it /// after 6-7 and 4-5 frames; runs 9-11 kept it 6, 4, 2, the last two warm: 2-7 frames, shorter warm. Hardware-captured. pub const FM_PORT_FADE_MCLK: i64 = 4_000_000; /// A 68000 write to the FM is taken this long after its /AS, and a 68000 read gets the /// status as it stands this long after (Nuked-MD, `scripts/run_fm_busy.py`). The /// nuked-md machine only. pub const M68K_FM_WRITE_MCLK: i64 = 21; pub const M68K_FM_READ_MCLK: i64 = 27; /// When the FM takes a Z80 write, after the access's /MREQ. The chip takes a write in the /// internal cycle under way if it lands before that cycle ends: the die (Nuked-MD, Overdrive /// 2's boot) did not take the driver's register $27 write 22.5 MCLK before a cycle's end /// (+9,150,686 after the Z80's start) and takes writes 24.5 before (frames 2500-2700: the /// flag clears 108.5..147.5 after the write). Die-derived: megadrive-python `board.py` /// `Z80_FM_WRITE_MCLK`; od2-20k `flaws/FABLE-HANDOFF.md`, Experiments 6-12 (2026-11-08). pub const Z80_FM_WRITE_MCLK: i64 = 23; /// Whatever it goes to, a Z80 cycle over the 68000's bus uses up a cartridge refresh /// that fell due by its start (Nuked-MD; docs/worklog.md, "The refresh a Z80 cycle /// uses up"). pub const Z80_REFRESH_USED_UP: bool = true; /// The work-RAM refresh due at `t` once those nobody took since `due` have run by /// themselves. pub fn ram_refresh_self_runs(due: i64, t: i64) -> i64 { let ran = RAM_REFRESH_SELF_MCLK + RAM_REFRESH_STALL; if RAM_REFRESH_HIDDEN && t >= due + ran { let period = RAM_REFRESH_SELF_MCLK + RAM_REFRESH_AFTER_MCLK; return due + ((t - due - ran).div_euclid(period) + 1) * period; } due } /// The work-RAM refresh for a work-RAM access starting at `t` with the next refresh due /// at `due`: the MCLK the access waits, and when the next falls due. pub fn ram_refresh(due: i64, t: i64) -> (i64, i64) { let mut due = due; if t < due { return (0, due); } if !RAM_REFRESH_HIDDEN { return (RAM_REFRESH_STALL, t + 805); } let rolled = ram_refresh_self_runs(due, t); if rolled != due { due = rolled; if t < due { return (0, due); } } if t < due + RAM_REFRESH_TRIGGER_MCLK { return (RAM_REFRESH_STALL, t + RAM_REFRESH_AFTER_MCLK); } let began = due + RAM_REFRESH_SELF_MCLK; // Until its end, on the 68000's next clock edge (the die's waits are 7, 14, 28, 35). let wait = -((t - began - RAM_REFRESH_STALL).div_euclid(M68K_DIVIDER)) * M68K_DIVIDER; (wait, began + RAM_REFRESH_AFTER_MCLK) } /// The work-RAM refresh due time after the VDP held the bus from `start` to `end` for a /// transfer: the refresh timer stops meanwhile and goes on from where it was. A /// self-run that began by `began_by` goes ahead under the pause. pub fn ram_refresh_paused(due: i64, start: i64, end: i64, began_by: i64) -> i64 { let (mut due, mut start) = (due, start); if began_by >= due + RAM_REFRESH_SELF_MCLK { let period = RAM_REFRESH_SELF_MCLK + RAM_REFRESH_AFTER_MCLK; due += ((began_by - due - RAM_REFRESH_SELF_MCLK).div_euclid(period) + 1) * period; start = start.max(due - RAM_REFRESH_COUNT_MCLK); } let edges_before = |t: i64| -((M68K_EDGE_PHASE - t).div_euclid(M68K_DIVIDER)); due + M68K_DIVIDER * (edges_before(end) - edges_before(start)).max(0) } /// A held 68000 bus cycle lasts whole 68000 clocks (docs/worklog.md, "Handover by /// handover"). False keeps the master clocks. pub const HOLD_ON_68K_EDGE: bool = true; /// How long after a write to $A11200 begins the YM2612 is given /IC (docs/worklog.md, /// "The VDPs' offset"). 0. pub const ZRES_PIN_MCLK: i64 = 0; /// A hold of `held` MCLK as the 68000 sees it: rounded up to its clock. pub fn on_edge(held: i64) -> i64 { if HOLD_ON_68K_EDGE && held % M68K_DIVIDER != 0 { return held + M68K_DIVIDER - held % M68K_DIVIDER; } held } /// An access that locks up a real console (CMD `gen-hw.txt` section 1); the system /// stops and reports it. #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct Lockup(pub String); impl std::fmt::Display for Lockup { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(&self.0) } } impl std::error::Error for Lockup {} /// The probe's bus (`System.z80_probe`): what the Z80's next step would access. #[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)] pub struct Probe { pub offsets: Vec, pub ends: Vec, /// Machine cycles over the 68000's bus: (end, start) in T-states ... pub bus_ends: Vec<(i64, i64)>, /// ... and whether each is to the cartridge. pub bus_cart: Vec, pub written: HashMap, pub clock: i64, pub fetching: bool, pub next_fetch: u16, pub after_prefix: bool, } /// What the Z80 settled before an instruction ran (`System._prearbitrate`): its start, /// (the Z80's wait, the 68000's) for each access, and the accesses as (T-state offset, /// to the cartridge). pub type Prearbitrated = (i64, Vec<(i64, i64)>, Vec<(i64, bool)>); /// What answers the 68000 at $000000-$3FFFFF now (the reference's `Board.cartridge`, /// chosen by `_remap`): the slot's cartridge, the TMSS boot ROM, or the claims of a /// socket's device there (`_LowDecode`), the rest of the range to the slot. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub enum Low { #[default] Slot, Boot, Decode, } /// The two sockets, as [`Devices::pages`] names them (0: nothing claims the page). pub const SLOT: u8 = 1; pub const PORT: u8 = 2; /// Everything on the board but the VDP. #[derive(serde::Serialize, serde::Deserialize)] pub struct Devices { /// Configuration (src/state.rs, `Config`). #[serde(skip, default = "crate::state::placeholder_machine")] pub machine: Machine, /// The cartridge (the reference's `game_cartridge`), and the TMSS boot ROM that /// stands in its place while $A14101 bit 0 is clear. pub cartridge: Cartridge, pub tmss_rom: Option, pub tmss_cart_selected: bool, pub ram: Vec, pub z80_ram: Vec, pub io: IOChip, /// The two sound chips, lazy: they catch up to the master clock of an access /// before it. The FM shares /RESET with the Z80, held at power-on. pub fm: YM2612, pub fm_write_lag: i64, pub fm_read_lag: i64, /// The Z80's FM write landing after its access ([`Z80_FM_WRITE_MCLK`] with die figures). pub z80_fm_write_lag: i64, /// The PSG lives inside the VDP and is written at $C00011 or $7F11. pub psg: SN76489, pub psg_mclk: i64, /// The master clock the FM has been run to: it runs on MCLK/7, in whole clocks. pub fm_mclk: i64, /// The status port 0 last gave, and when (`FM_PORT_FADE_MCLK`). pub fm_status_kept: u8, pub fm_status_at: i64, /// 68000 address bits 23-15 of the Z80's bank window; power-on 0. pub bank: u32, /// "On power-up, the Z80 will be reset and will have the bus" (CMD 2.2). pub z80_reset: bool, pub z80_bus_requested: bool, /// TMSS: the VDP is locked until "SEGA" is written to $A14000 (Manual p. 94). pub tmss_register: u32, /// The 68000's IRC and `step_clocks` as of its last bus access (or step), which the /// reference reads from the CPU. pub m68k_irc: u16, /// Where the 68000's latest bus access began (`system::IPL_SYNC_MCLK`). pub m68k_last_access: i64, pub m68k_step_clocks: u32, /// The master clock at which the current CPU step started, set by the system. pub step_start: i64, /// The Z80 instruction under way: its memory accesses' offsets from `step_start` /// in master clocks (None for an interrupt acknowledge), and how many it has made. pub z80_offsets: Option>, pub z80_access: usize, pub z80_bus_access: usize, pub z80_prearbitrated: Option, /// Master clocks the 68000 has been held during the current step. pub stall: i64, /// How much of `stall` is the Z80-space hold, for the MAME lockstep. pub z80_space_stall: i64, pub cart_refresh_due: i64, pub cart_refresh_pulse_end: i64, pub z80_refresh_taken: i64, pub ram_refresh_due: i64, pub refresh_enabled: bool, /// The arbiter's delays (milestone 5); MAME models neither, so the milestone-1 /// lockstep turns this off with `refresh_enabled`. pub arbiter_enabled: bool, /// Each 68000 bus cycle started in the step under way, as `[/AS fell, it stays /// down]`. pub m68k_accesses: Vec<[i64; 2]>, /// The last 68000-to-VDP transfer's hold on the bus (`arbiter::arbiter_grid`). pub dma_bus: Option<(i64, i64, i64)>, pub z80_stall: i64, pub m68k_pending_stall: i64, pub grant_wait: i64, pub m68k_resume_at: i64, pub m68k_step_end: i64, pub bank_accesses: u64, pub bank_z80_cycles: i64, pub bank_m68k_cycles: i64, /// The master clock the Z80 lets go of the bus after the current request. pub busack_at: i64, /// For the Z80 step resuming after a hold: from which clock its accesses wait, and /// for how long. pub z80_hold: Option<(i64, i64)>, /// The probe's bus while the system probes the Z80. pub probe: Option, /// An access that locks up a real console, recorded for the system to report. pub lockup: Option, /// Whether the slot holds a cartridge -- through a slot device, the one it holds -- /// (the reference's `slot_cartridge` is not None): an empty slot is a state of its /// own, which /CART reads ([`Devices::cartridge_present`]; expansion socket 1). An /// empty slot's bus is still [`Devices::cartridge`], a ROM-less stand-in that reads /// $FF, as the board always has (nothing hardware here says what an empty slot reads; /// not claimed). Wiring, as is everything below: a state does not hold it. #[serde(skip)] pub slot_cartridge: bool, /// What answers the 68000 at $000000-$3FFFFF now ([`Devices::remap`]). #[serde(skip)] pub low: Low, /// The expansion sockets (the reference's docs/expansion-sockets.md, src/sockets.rs): /// the device in the cartridge slot and the one on the expansion port, both None on a /// console with nothing plugged in, and the sockets in the order their devices were /// attached, for the events every device hears (DMA starts, resets). #[serde(skip)] pub slot: Option>, #[serde(skip)] pub port: Option>, #[serde(skip)] pub devices: Vec, /// What the header of a save state names each socket's device by ("" for none). #[serde(skip)] pub device_names: [&'static str; 2], /// Every claimed 256-byte page of the 68000's space, by `address >> 8`: the socket /// whose device answers it ([`SLOT`], [`PORT`]), or 0. None as a whole when nothing is /// attached -- the only paths that look at it are ones that had nothing to do before /// (open bus, the 32X area's lock-up, the $A1 page) and the Z80's and DMA's, for one /// `None` test ([`Devices::update_claims`]). #[serde(skip)] pub pages: Option>, /// A device lent out for a call ([`Devices::with_device`]), and whether it asked for /// its claims to be read again meanwhile. #[serde(skip)] lent: u32, #[serde(skip)] claims_changed: bool, } /// The board: the VDP and everything else; the Z80's bus. #[derive(serde::Serialize, serde::Deserialize)] pub struct Board { pub vdp: Vdp, pub dev: Devices, } impl Board { /// `pads` None is the reference's default, two 3-button pads. pub fn new( machine: Machine, cartridge: Option, pads: Option<[Option; 2]>, ) -> Self { let mut fm = YM2612::new(machine.fm); // The FM's write and read timing against the access is closed-box (Nuked-MD) and on every // machine since 2026-11-10 (until then only on nuked-md): with it the PCM stream // of a PAL console's capture of Overdrive 2 sits 1 to 80 ms behind the board's // over the demo, against 6 to 151 ms without (megadrive-python `Board.fm_write_lag`). let (fm_write_lag, fm_read_lag, z80_fm_write_lag) = (M68K_FM_WRITE_MCLK, M68K_FM_READ_MCLK, Z80_FM_WRITE_MCLK); fm.reset_line(0); // held: /IC low at power-on let has_cartridge = cartridge.is_some(); Board { vdp: Vdp::new(machine), dev: Devices { machine, cartridge: cartridge.unwrap_or_else(Cartridge::empty), tmss_rom: None, tmss_cart_selected: true, ram: vec![0; 0x10000], z80_ram: vec![0; 0x2000], io: IOChip::new(machine.version_register(), pads), fm, fm_write_lag, fm_read_lag, z80_fm_write_lag, psg: SN76489::new(YM7101_PSG), psg_mclk: 0, fm_mclk: 0, fm_status_kept: 0, fm_status_at: -FM_PORT_FADE_MCLK, bank: 0, z80_reset: true, z80_bus_requested: false, tmss_register: 0, m68k_irc: 0, m68k_last_access: 0, m68k_step_clocks: 0, step_start: 0, z80_offsets: None, z80_access: 0, z80_bus_access: 0, z80_prearbitrated: None, stall: 0, z80_space_stall: 0, cart_refresh_due: CART_REFRESH_MCLK, cart_refresh_pulse_end: 0, z80_refresh_taken: -1, ram_refresh_due: RAM_REFRESH_AFTER_MCLK, refresh_enabled: true, arbiter_enabled: true, m68k_accesses: Vec::new(), dma_bus: None, z80_stall: 0, m68k_pending_stall: 0, grant_wait: 0, m68k_resume_at: 0, m68k_step_end: 0, bank_accesses: 0, bank_z80_cycles: 0, bank_m68k_cycles: 0, busack_at: 0, z80_hold: None, probe: None, lockup: None, slot_cartridge: has_cartridge, low: Low::Slot, slot: None, port: None, devices: Vec::new(), device_names: ["", ""], pages: None, lent: 0, claims_changed: false, }, } } /// Socket 6's channel to a device: every line the VDP starts up to master clock /// `mclk` is published (`VideoTiming::video_line`) before this returns -- the VDP run /// there, as its own next access or the next 68000 step would run it, so nothing it /// does changes (the same call the 32X made directly, `board.vdp.sync`). A device that /// reads the line records at its own clock has its host call this first (a device is /// lent `Devices` alone, src/sockets.rs). pub fn publish_lines(&mut self, mclk: i64) { let Board { vdp, dev } = self; vdp.sync(dev, mclk); } } impl Devices { pub fn lock_up(&mut self, message: String) { if self.lockup.is_none() { self.lockup = Some(Lockup(message)); } } // -- TMSS -------------------------------------------------------------------------- /// Fit the TMSS boot ROM, which a TMSS console powers up running (the reference's /// docstring: a board without one costs nothing, and a caller asks for it). pub fn install_tmss_rom(&mut self, image: Vec) { self.tmss_rom = Some(BootRom::new(image)); self.tmss_cart_selected = false; self.remap(); } /// Bit 0 of $A14101: 1 puts the cartridge on the bus, 0 the boot ROM. pub fn tmss_select(&mut self, cartridge: bool) { if self.tmss_rom.is_none() || cartridge == self.tmss_cart_selected { return; } self.tmss_cart_selected = cartridge; self.remap(); } /// Choose what answers the 68000 at $000000-$3FFFFF ([`Devices::low`]). /// /// The TMSS boot ROM, while $A14101 bit 0 is clear, sits above everything: in the /// Sega CD's mode 2 the port is what is at $000000, and a TMSS console must still run /// its check first (segacd-python `docs/sockets.md` 2: "TMSS ROM selected > port in /// mode 2 > slot"). Then a device's claims there; then the slot. With nothing /// attached this is exactly the swap the TMSS select always made. pub fn remap(&mut self) { self.low = if self.tmss_rom.is_some() && !self.tmss_cart_selected { Low::Boot } else if self.pages.as_ref().is_some_and(|pages| { pages[..(0x400000 / CLAIM_PAGE) as usize] .iter() .any(|&d| d != 0) }) { Low::Decode } else { Low::Slot }; } /// The socket whose device claims the page of `address`, or 0. #[inline] pub fn claimed(&self, address: u32) -> u8 { match &self.pages { Some(pages) => pages[(address >> 8) as usize], None => 0, } } // What answers $000000-$3FFFFF: the slot's cartridge, the boot ROM, or (the reference's // `_LowDecode`) a claimed page's device at the access's clock and the slot for the // rest. Only the 68000 comes through a claimed page here; the Z80's bank window, its // peeks and the VDP's DMA look the claims up themselves first (socket 3). What // `_LowDecode` lacks of a cartridge's method set (`upper`, `reset`, `dma_read_word`, // the word and lane forms of /TIME, $A15xxx) it does not answer. pub fn cart_read_byte(&mut self, address: u32) -> u8 { match self.low { Low::Slot => self.cartridge.read_byte(address), Low::Boot => self .tmss_rom .as_ref() .expect("the boot ROM") .read_byte(address), Low::Decode => match self.claimed(address) { 0 => self.cartridge.read_byte(address), socket => { let t = self.m68k_access_clock(); self.with_device(socket, |device, board| { device.read_byte(board, address, t, M68K) }) } }, } } pub fn cart_read_word(&mut self, address: u32) -> u16 { match self.low { Low::Slot => self.cartridge.read_word(address), Low::Boot => self .tmss_rom .as_ref() .expect("the boot ROM") .read_word(address), Low::Decode => match self.claimed(address) { 0 => self.cartridge.read_word(address), socket => { let t = self.m68k_access_clock(); self.with_device(socket, |device, board| device.read_word(board, address, t)) } }, } } pub fn cart_write_byte(&mut self, address: u32, value: u8) { match self.low { Low::Slot => self.cartridge.write_byte(address, value), Low::Boot => {} Low::Decode => match self.claimed(address) { 0 => self.cartridge.write_byte(address, value), socket => { let t = self.m68k_access_clock(); self.with_device(socket, |device, board| { device.write_byte(board, address, value, t, M68K) }); } }, } } pub fn cart_write_word(&mut self, address: u32, value: u16) { match self.low { Low::Slot => self.cartridge.write_word(address, value), Low::Boot => {} Low::Decode => match self.claimed(address) { 0 => self.cartridge.write_word(address, value), socket => { let t = self.m68k_access_clock(); self.with_device(socket, |device, board| { device.write_word(board, address, value, t) }); } }, } } pub fn cart_write_time(&mut self, address: u32, high: u8, low: u8, word: bool) { match self.low { Low::Slot => self.cartridge.write_time_lanes(address, high, low, word), Low::Boot => {} Low::Decode => self.cartridge.write_time(address | 1, low), } } pub fn cart_read_time(&mut self, address: u32) -> Option { match self.low { Low::Slot => self.cartridge.read_time_word(address), Low::Boot => None, Low::Decode => self .cartridge .read_time(address | 1) .map(|value| u16::from(value) | u16::from(value) << 8), } } /// $A15xxx goes to the cartridge's board only when it is what is on the bus. pub fn cart_a15(&self) -> bool { self.low == Low::Slot } /// The board decodes $400000-$7FFFFF, and is on the bus. pub fn cart_upper(&self) -> bool { self.low == Low::Slot && self.cartridge.upper } /// The console's reset button reaches the cartridge connector. pub fn cart_reset(&mut self) { if self.low == Low::Slot { self.cartridge.reset(); } } fn cart_dma_read_word(&mut self, address: u32) -> u16 { match self.low { Low::Slot => self.cartridge.dma_read_word(address), Low::Boot => self .tmss_rom .as_ref() .expect("the boot ROM") .read_word(address), // The page is not claimed (the caller looked): the slot's plain read. Low::Decode => self.cartridge.read_word(address), } } // -- the expansion sockets --------------------------------------------------------- /// /CART: low when the slot -- through a slot device, the device's own slot -- holds a /// cartridge that grounds it (`Cartridge::grounds_cart`). The Sega CD reads it to /// choose its mapping; it is told at attach, since hot-plugging is not modelled /// (segacd-python `docs/sockets.md` 2). pub fn cartridge_present(&self) -> bool { self.slot_cartridge && self.cartridge.grounds_cart } /// Plug `device` into the cartridge slot (socket 1). The cartridge it holds takes the /// slot's place on the bus wherever the device does not claim. pub fn attach_slot(&mut self, mut device: Box) { assert!( self.slot.is_none(), "the cartridge slot already holds a device" ); assert!( !self.slot_cartridge, "a slot device holds its own cartridge: pass the board none" ); let held = device.take_cartridge(); self.slot_cartridge = held.is_some(); if let Some(cartridge) = held { self.cartridge = cartridge; } self.device_names[0] = device.name(); let mut device: Box = device; device.attach(self); self.slot = Some(device); self.devices.push(SLOT); self.update_claims(); } /// Plug `device` into the expansion port (socket 2). $A10001 bit 5, "no expansion /// unit" (the Manual, PDF page 84; /DISK on the port), now reads 0, as /// `Machine::expansion` already makes it for a model that has a unit built in. pub fn attach_port(&mut self, device: Box) { assert!( self.port.is_none(), "the expansion port already holds a device" ); self.io.version &= !0x20; self.device_names[1] = device.name(); let mut device: Box = device; device.attach(self); self.port = Some(device); self.devices.push(PORT); self.update_claims(); } /// Read every attached device's claims again and rebuild the page table: at attach, /// and whenever a device's mapping changes (the 32X's ADEN or RV, the Sega CD's /// mode). A claim is [start, end) in 68000 addresses, page-aligned and inside /// [`CLAIMABLE`]; the port's claims win over the slot's where both claim a page. /// Asked for by a device from inside one of its own calls, it is done as that call /// returns ([`Devices::with_device`]). pub fn update_claims(&mut self) { if self.lent != 0 { self.claims_changed = true; return; } if self.devices.is_empty() { self.pages = None; self.remap(); return; } let mut pages = vec![0u8; 0x10000].into_boxed_slice(); for (socket, device) in [(SLOT, &self.slot), (PORT, &self.port)] { let Some(device) = device else { continue }; for (start, end) in device.claims() { assert!( start % CLAIM_PAGE == 0 && end % CLAIM_PAGE == 0 && start < end, "claim ${start:06X}-${end:06X} is not whole pages" ); assert!( CLAIMABLE .iter() .any(|&(low, high)| low <= start && end <= high), "claim ${start:06X}-${end:06X} is not on a socket's bus" ); for page in start / CLAIM_PAGE..end / CLAIM_PAGE { pages[page as usize] = socket; } } } self.pages = Some(pages); self.remap(); } /// Lend the device in `socket` ([`SLOT`] or [`PORT`]) out for one call, with the /// board (Rust's form of the reference's devices keeping the board from `attach`). pub fn with_device( &mut self, socket: u8, call: impl FnOnce(&mut (dyn BusDevice + Send), &mut Devices) -> R, ) -> R { let held = if socket == SLOT { self.slot.take() } else { self.port.take() }; let mut device = held.expect("a device in the socket"); self.lent += 1; let out = call(device.as_mut(), self); self.lent -= 1; if socket == SLOT { self.slot = Some(device); } else { self.port = Some(device); } if self.lent == 0 && self.claims_changed { self.claims_changed = false; self.update_claims(); } out } /// A device's wait states on the access it is answering (socket 4): `mclk` master /// clocks for the 68000 -- on its own clock edge, as the VDP's holds /// ([`HOLD_ON_68K_EDGE`]), and as a longer bus cycle in the arbiter's grid -- or for /// the Z80, added to its step as the arbiter's waits are. pub fn hold(&mut self, master: u8, mclk: i64) { if master == Z80 { self.z80_stall += mclk; return; } let held = on_edge(mclk); self.stall += held; if self.arbiter_enabled { if let Some(last) = self.m68k_accesses.last_mut() { last[1] += held; } } } /// A 68000-to-VDP DMA started at `mclk` (`Vdp::run_transfer`): every device is told, /// with the 68000's data bus as it stands (socket 3). pub fn dma_started(&mut self, mclk: i64) { let bus = self.open_bus(); for index in 0..self.devices.len() { let socket = self.devices[index]; self.with_device(socket, |device, board| device.dma_start(board, mclk, bus)); } } /// One of the three reset events, to every device (socket 9). pub fn reset_devices(&mut self, kind: crate::sockets::Reset, mclk: i64) { for index in 0..self.devices.len() { let socket = self.devices[index]; self.with_device(socket, |device, board| device.reset(board, kind, mclk)); } } // -- time ---------------------------------------------------------------------------- /// The master clock at which the 68000's current bus access starts: m68000-rust /// counts an instruction's clocks as it goes, adding 4 for each bus access before /// calling the host, so `step_clocks - 4` is where the access began. pub fn m68k_access_clock(&self) -> i64 { self.step_start + self.stall + (i64::from(self.m68k_step_clocks) - 4) * M68K_DIVIDER } /// The Z80 runs unless it is held in reset or has granted its bus. pub fn z80_running(&self) -> bool { !self.z80_reset && !self.z80_bus_requested } /// "The Z80 bus can only be accessed by the 68000 when the Z80 is running and the /// 68000 has the bus" (CMD 2.2); with the arbiter modelled an access at `t` before /// the grant is stalled to it. pub fn m68k_has_z80_bus(&mut self, t: Option) -> bool { if !self.z80_bus_requested || self.z80_reset { return false; } if let Some(t) = t { if self.arbiter_enabled && t < self.busack_at { self.stall += on_edge(self.busack_at - t); } } true } /// The one extra 68000 clock every access to $A00000-$A0FFFF costs, counted apart /// as well as charged. pub fn z80_space_cost(&mut self) { self.stall += Z80_SPACE_STALL; self.z80_space_stall += Z80_SPACE_STALL; if self.arbiter_enabled { if let Some(last) = self.m68k_accesses.last_mut() { // The bus cycle itself is that much longer, as for a refresh stretch. last[1] += Z80_SPACE_STALL; } } } /// Data-port waits extend /AS. A control-port DMA hold includes later bus ownership. pub(crate) fn vdp_hold(&mut self, address: u32, mclk: i64) { if self.refresh_enabled && (address >> 2) & 7 == 0 { self.hold(M68K, mclk); } else { self.stall += on_edge(mclk); } } // -- the arbiter ----------------------------------------------------------------------- /// The master clock of the Z80 access under way: its instruction's start, its /// offset in the instruction, and what the arbiter has held the Z80 for so far. pub fn z80_access_time(&self) -> i64 { let k = self.z80_access as i64 - 1; let t = match &self.z80_offsets { Some(offsets) if 0 <= k && (k as usize) < offsets.len() => { self.step_start + offsets[k as usize] + self.z80_stall } _ => self.step_start + self.z80_stall, }; match self.z80_hold { Some((from, held)) if t >= from => t + held, _ => t, } } /// A byte of the Z80's space for looking at code: RAM, cartridge ROM and a claimed /// page's device (`BusDevice::peek_byte`); anything else reads $FF. The scheduler's /// probe reads through it too, so the arbiter's prediction and the access it /// predicts see one decode. pub fn z80_peek(&mut self, address: u16) -> u8 { if address < 0x4000 { return self.z80_ram[usize::from(address & 0x1FFF)]; } if address >= 0x8000 { let full = self.bank | u32::from(address & 0x7FFF); let socket = self.claimed(full); if socket != 0 { return self.with_device(socket, |device, _| device.peek_byte(full)); } if full < 0x400000 { return self.cart_read_byte(full); } } 0xFF } /// What one Z80 access over the 68000's bus -- the bank window, or the VDP -- costs /// both CPUs (`arbiter::z80_access_wait`); the 68000's share is owed. fn bank_delay(&mut self, cartridge: bool) { let index = self.z80_bus_access; self.z80_bus_access += 1; let settled = match &self.z80_prearbitrated { // Settled before the instruction ran, at its own time: only the Z80's // wait remains to be taken. Some(done) if done.0 == self.step_start && index < done.1.len() => Some(done.1[index]), _ => None, }; let (wait, m68k_cycles) = match settled { Some(pair) => pair, None => self.arbitrate(self.z80_access_time(), cartridge), }; self.z80_stall += wait * Z80_DIVIDER; self.bank_accesses += 1; self.bank_z80_cycles += wait; self.bank_m68k_cycles += m68k_cycles; } /// A Z80 access over the 68000's bus at `t`: the 68000's share charged (owed, and /// its later cycles in the grid moved past the grant), the refresh it takes; returns /// the Z80's wait states and the 68000's, in their own clocks. pub fn arbitrate(&mut self, t: i64, cartridge: bool) -> (i64, i64) { // The Z80's own cycle lasts at least OWN_CYCLE_CARTRIDGE_MCLK to the cartridge // and OWN_CYCLE_VDP_MCLK to the VDP. The cartridge refresh tick it may take is // the last one due by its own cycle's start -- the grant is the horizon, not // only the request's lookahead: a tick between t + 50 and the grant is used up // by this cycle and the 68000's next cartridge access is not held for it -- and // only while still pending (tick >= cart_refresh_due): a tick a 68000 cycle has // already passed ran by itself or was taken then; rebuilt as due - 896 it // stretched the Z80's cycle for a refresh 91 MCLK old. (megadrive-python // `Board._arbitrate`; od2-20k `flaws/FABLE-HANDOFF.md`, "The boot walk".) // A Model 1's cartridge cycle is longer (`arbiter::MODEL1_CARTRIDGE_EXTRA_MCLK`). let (own_cycle, release) = if cartridge { ( OWN_CYCLE_CARTRIDGE_MCLK + self.machine.z80_cartridge_extra, self.machine.z80_cartridge_release, ) } else { (OWN_CYCLE_VDP_MCLK, 0) }; let due = self.cart_refresh_due; let dma = self.dma_bus; let grid = crate::arbiter::arbiter_grid(t, &self.m68k_accesses, dma); let held = matches!(grid, std::borrow::Cow::Owned(_)); let (_, _, grant) = z80_access_wait(t, &grid, None, own_cycle, 0); let horizon = (t + CUTOFF_MCLK + IDLE_GRANT_MCLK).max(grant); let tick = due + (horizon - due).div_euclid(CART_REFRESH_MCLK) * CART_REFRESH_MCLK; let taken = tick < due || tick == self.z80_refresh_taken || self .m68k_accesses .iter() .any(|&[begin, _]| tick <= begin && begin <= t + CUTOFF_MCLK); let stretched = !taken && (cartridge || !Z80_REFRESH_CARTRIDGE_ONLY); let (wait, mut resume, own) = z80_access_wait( t, &grid, if stretched { Some(tick) } else { None }, own_cycle, release, ); if !taken && tick <= own { self.z80_refresh_taken = tick; if Z80_REFRESH_USED_UP && self.cart_refresh_due <= tick { self.cart_refresh_due = tick + CART_REFRESH_MCLK; } } let mut wanted = t + CUTOFF_MCLK + 1; let mut next = None; for &[begin, _] in &self.m68k_accesses { if begin > t + CUTOFF_MCLK { next = Some(begin); break; } wanted = wanted.max(begin + 4 * M68K_DIVIDER); // its next, four clocks on } if Z80_GRANT_OVERLAPS_IDLE { // The 68000 waits only from when it wants the bus again: its next bus cycle // in this instruction, or else where its next instruction starts. let next = next.unwrap_or(self.m68k_step_end + self.m68k_pending_stall); wanted = wanted.max(next); } if held { // The 68000 does not want the bus before the transfer gives it back, and its // cycle is then only stretched to the Z80's. wanted = wanted.max(dma.map_or(wanted, |d| d.2)); resume += crate::arbiter::DMA_Z80_RESUME_MCLK - crate::arbiter::RESUME_MCLK; } resume += (wanted - resume).rem_euclid(M68K_DIVIDER); let m68k_cycles = (resume - wanted).max(0) / M68K_DIVIDER; if m68k_cycles != 0 { // The 68000's cycles past the cut-off happen that much later. for cycle in &mut self.m68k_accesses { if cycle[0] > t + CUTOFF_MCLK { cycle[0] += m68k_cycles * M68K_DIVIDER; } } } self.m68k_pending_stall += m68k_cycles * M68K_DIVIDER; (wait, m68k_cycles) } // -- the sound chips ------------------------------------------------------------------- /// Run both sound chips up to `mclk`. pub fn sound_sync(&mut self, mclk: i64) { let fm_clocks = (mclk - self.fm_mclk).div_euclid(M68K_DIVIDER); if fm_clocks > 0 { self.fm_mclk += fm_clocks * M68K_DIVIDER; self.fm.run(fm_clocks as u64); } let clocks = (mclk - self.psg_mclk).div_euclid(Z80_DIVIDER); if clocks > 0 { self.psg_mclk += clocks * Z80_DIVIDER; self.psg.run(clocks as u64); } } /// The YM2612's status byte at $A04000-$A04003 (or $4000-$4003): the discrete chip /// answers on port 0 only and its other three give what port 0 last gave, until it /// fades (`FM_PORT_FADE_MCLK`, hardware-captured); the ASIC answers on all four (Eke, /// forum t=386 start=810). pub fn fm_read(&mut self, port: u8, mclk: i64) -> u8 { self.sound_sync(mclk); // None: a port this part leaves undriven (the variant's status_ports). let Some(value) = self.fm.read(port) else { return if mclk - self.fm_status_at < FM_PORT_FADE_MCLK { self.fm_status_kept } else { 0x00 }; }; if port == 0 { self.fm_status_kept = value; self.fm_status_at = mclk; } value } // -- open bus ---------------------------------------------------------------------------- /// The 68000's undriven data bus: the next opcode word it will fetch (CMD 1 note 4 /// and Kabuto, "The VDP ports"). pub fn open_bus(&self) -> u16 { self.m68k_irc } /// "Reads return the MSB of the next instruction to be fetched, with the LSB set to /// zero" (CMD 1 note 1). pub fn open_bus_msb(&self) -> u16 { self.open_bus() & 0xFF00 } /// One bit into the bank register: bits 15-23 of the window's 68000 address arrive /// in that order, each write shifting the others down (CMD 2.3). pub fn write_bank(&mut self, value: u8) { self.bank = ((self.bank >> 1) | (u32::from(value & 1) << 23)) & 0xFF8000; } /// The VDP decodes 110n n000 nnnn nnnn 000m mmmm (CMD 1 note 5); anything else in /// $C00000-$DFFFFF locks up, and so does a VDP access on a TMSS console before /// "SEGA" is written to $A14000 (Manual PDF page 94). True if the access goes ahead. pub fn vdp_access(&mut self, address: u32) -> bool { if address & 0xE700E0 != 0xC00000 { self.lock_up(format!( "68000 access to ${address:06X}: outside the VDP's mirror pattern" )); return false; } if self.machine.tmss && self.tmss_register != 0x53454741 { self.lock_up(format!( "68000 access to the VDP at ${address:06X} before TMSS was unlocked" )); return false; } true } /// The parts of a write to $A1xxxx that need nothing of the Z80: the I/O chip, /// /TIME, TMSS. Returns false for the bus request and reset registers, which the /// caller handles. `high` is what D15-D8 carry, `low` D7-D0. pub fn system_write_devices( &mut self, address: u32, high: u8, low: u8, word: bool, mclk: i64, ) -> bool { let offset = address & 0xFFFF; if offset < 0x20 { self.io.write(address, low, mclk); } else if offset & 0xFF00 == 0x1100 || offset & 0xFF00 == 0x1200 { return false; } else if offset & 0xFF00 == 0x3000 { self.cart_write_time(address, high, low, word); } else if offset & 0xFF00 == 0x5000 { if self.cart_a15() { self.cartridge.write_a15(address, high, low, word); } } else if offset & 0xFF00 == 0x4100 { // $A14101 bit 0: the cartridge (1) or the TMSS boot ROM (0); only bit 0 of // the byte written matters (the disassembly's `RAM_Code`; MAME `tmss_swap_w`). self.tmss_select(low & 1 != 0); } else if offset & 0xFFFC == 0x4000 { // The TMSS register takes "SEGA" as a long, two words or four bytes. let (shift, value, mask) = if word { ( 16 - (offset & 2) * 8, u32::from(high) << 8 | u32::from(low), 0xFFFF, ) } else { ((3 - (offset & 3)) * 8, u32::from(low), 0xFF) }; self.tmss_register = (self.tmss_register & !(mask << shift)) | (value << shift); } true } /// $A10000-$A1FFFF read as a word, but for the bus request register (which needs /// the Z80 brought up first): the I/O chip, /TIME, open bus. pub fn system_read_devices(&mut self, address: u32, mclk: i64) -> Option { let offset = address & 0xFFFF; if offset < 0x20 { let value = u16::from(self.io.read(address | 1, mclk)); return Some(value | (value << 8)); } if offset & 0xFF00 == 0x1100 { return None; } if offset & 0xFF00 == 0x5000 && self.cart_a15() { if let Some(value) = self.cartridge.read_a15_word(address) { return Some(value); } } if offset & 0xFF00 == 0x3000 { if let Some(value) = self.cart_read_time(address) { return Some(value); } } Some(self.open_bus_msb()) } /// The bus request register's read, once the Z80 has been brought up to `t`: bit 0 /// of the MSB is 1 while the 68000 cannot use the Z80's bus; the rest is the next /// instruction's MSB and a zero LSB (CMD 1 note 4, 2.2). The poll itself is not /// held: it reads "busy" until BUSACK. pub fn busreq_read(&self, t: i64) -> u16 { let mut granted = self.z80_bus_requested && !self.z80_reset; if granted && self.arbiter_enabled { granted = t >= self.busack_at; } let busy = if granted { 0 } else { 0x100 }; (self.open_bus() & 0xFE00) | busy } /// The Z80's space as the 68000 reads it, the bus already granted: Z80 RAM, the FM's /// status, $FF for the bank register and $6100-$7EFF (CMD 2.1). None for the VDP /// through it, which locks up. pub fn z80_area_byte(&mut self, address: u32, t: i64) -> Option { let offset = address & 0x7FFF; // "A08000-A0FFFFh mirror A00000-A07FFFh" (CMD 1) if offset < 0x4000 { return Some(self.z80_ram[(offset & 0x1FFF) as usize]); } if offset < 0x6000 { let lag = self.fm_read_lag; return Some(self.fm_read((offset & 3) as u8, t + lag)); } if offset >= 0x7F00 { self.lock_up(format!( "68000 access to ${address:06X}: the VDP through the Z80's space" )); return None; } Some(0xFF) } /// The Z80's space as the 68000 writes it, the bus already granted. pub fn z80_area_store(&mut self, address: u32, value: u8, t: i64) { let offset = address & 0x7FFF; if offset < 0x4000 { self.z80_ram[(offset & 0x1FFF) as usize] = value; } else if offset < 0x6000 { let lag = self.fm_write_lag; self.sound_sync(t + lag); self.fm.write((offset & 3) as u8, value); } else if offset < 0x6100 { self.write_bank(value); } else if offset >= 0x7F00 { self.lock_up(format!( "68000 access to ${address:06X}: the VDP through the Z80's space" )); } } } impl VdpHost for Devices { /// A word the VDP's 68000-to-VDP DMA reads from the 68000's bus (the 68000 is held /// meanwhile): cartridge, work RAM, or anything else as the 68000 would read it. A /// source in the VDP's own range reads $FFFF (not claimed). A claimed page is its /// device's, at the VDP's clock for the read, `mclk` (socket 3; the device was told of /// the start, [`Devices::dma_started`]). fn dma_read_word(&mut self, address: u32, mclk: i64) -> u16 { let socket = self.claimed(address); if socket != 0 { return self.with_device(socket, |device, board| { device.dma_read_word(board, address, mclk) }); } let top = address >> 16; if top < 0x40 { return self.cart_dma_read_word(address); } if top >= 0xE0 { let offset = (address & 0xFFFF) as usize; return u16::from(self.ram[offset]) << 8 | u16::from(self.ram[offset + 1]); } if top >= 0xC0 { return 0xFFFF; } // The 68000's own read without the refresh (the reference's `read_word` with // `refresh_enabled` off), but for the Z80's catch-up (docs/handoff-brief.md). let t = self.m68k_access_clock(); if top == 0xA0 { let value = if self.m68k_has_z80_bus(Some(t)) { self.z80_area_byte(address, t) } else { None }; self.z80_space_cost(); return match value { Some(value) => u16::from(value) | u16::from(value) << 8, None => self.open_bus_msb(), }; } if top == 0xA1 { return match self.system_read_devices(address, t) { Some(value) => value, None => self.busreq_read(t), }; } if (0x80..0xA0).contains(&top) { self.lock_up(format!( "68000 read of ${address:06X} (32X area) without a 32X" )); return 0xFFFF; } if top < 0x80 && self.cart_upper() { return self.cartridge.read_word(address); } self.open_bus_msb() } fn open_bus(&self) -> u16 { Devices::open_bus(self) } fn has_devices(&self) -> bool { !self.devices.is_empty() } fn dma_started(&mut self, mclk: i64) { Devices::dma_started(self, mclk); } fn sound_sync(&mut self, mclk: i64) { Devices::sound_sync(self, mclk); } fn psg_write(&mut self, value: u8) { self.psg.write(u32::from(value)); } fn psg_write_test(&mut self, bits: u8) { // The Mega Drive's PSG has the test register; Err is impossible here. let _ = self.psg.write_test(u32::from(bits)); } fn cart_refresh_due(&mut self) -> &mut i64 { &mut self.cart_refresh_due } fn dma_bus(&mut self) -> &mut Option<(i64, i64, i64)> { &mut self.dma_bus } fn ram_refresh_due(&mut self) -> &mut i64 { &mut self.ram_refresh_due } } // -- the Z80's bus ---------------------------------------------------------------------------- impl Board { fn z80_read(&mut self, address: u16) -> u8 { let dev = &mut self.dev; dev.z80_access += 1; if address < 0x4000 { return dev.z80_ram[usize::from(address & 0x1FFF)]; } if address >= 0x8000 { let full = dev.bank | u32::from(address & 0x7FFF); if dev.arbiter_enabled { // the arbiter holds both CPUs for this access dev.bank_delay(full < 0x800000); } return self.z80_bank_read(full); } if address < 0x6000 { let t = dev.z80_access_time(); return dev.fm_read((address & 3) as u8, t); } if address >= 0x7F00 { if address >= 0x7F20 { dev.lock_up(format!( "Z80 access to ${address:04X} (beyond the VDP's $7F00-$7F1F)" )); return 0xFF; } if dev.arbiter_enabled && Z80_VDP_ARBITRATED { dev.bank_delay(false); // the VDP is on the 68000's bus } let t = dev.z80_access_time(); return self .vdp .z80_read(&mut self.dev, u32::from(address & 0x1F), t); } 0xFF // the bank register and $6100-$7EFF (CMD 2.1) } fn z80_write(&mut self, address: u16, value: u8) { let dev = &mut self.dev; dev.z80_access += 1; if address < 0x4000 { dev.z80_ram[usize::from(address & 0x1FFF)] = value; } else if address >= 0x8000 { let full = dev.bank | u32::from(address & 0x7FFF); if dev.arbiter_enabled { dev.bank_delay(full < 0x800000); } self.z80_bank_write(full, value); } else if address < 0x6000 { let t = dev.z80_access_time() + dev.z80_fm_write_lag; dev.sound_sync(t); dev.fm.write((address & 3) as u8, value); } else if address < 0x6100 { dev.write_bank(value); } else if address >= 0x7F00 { if address >= 0x7F20 { dev.lock_up(format!( "Z80 access to ${address:04X} (beyond the VDP's $7F00-$7F1F)" )); return; } if dev.arbiter_enabled && Z80_VDP_ARBITRATED { dev.bank_delay(false); } let t = dev.z80_access_time(); self.vdp .z80_write(&mut self.dev, u32::from(address & 0x1F), value, t); } } /// A byte of the 68000's space through the Z80's window: reads of 68000 RAM fail /// (MoD's `gen_test_z80_bus`; CMD "returns FFh"); the Z80's own space locks up; the /// VDP's unused addresses read $FF and its status's top six bits 1 (CMD 1.1). A /// claimed page is its device's, at the Z80's own clock (socket 3), before any of that. fn z80_bank_read(&mut self, address: u32) -> u8 { let dev = &mut self.dev; let socket = dev.claimed(address); if socket != 0 { let t = dev.z80_access_time(); return dev.with_device(socket, |device, board| { device.read_byte(board, address, t, Z80) }); } let top = address >> 16; if top < 0x40 { return dev.cart_read_byte(address); } if top >= 0xE0 { return 0xFF; } if top >= 0xC0 { if address & 0xE700E0 != 0xC00000 { dev.lock_up(format!( "Z80 bank access to ${address:06X}: outside the VDP's pattern" )); return 0xFF; } let t = dev.z80_access_time(); return self.vdp.z80_bank_read(&mut self.dev, address & 0x1F, t); } if top == 0xA0 { dev.lock_up(format!( "Z80 bank access to ${address:06X}: the Z80's own space" )); return 0xFF; } if top == 0xA1 && address & 0xFFFF < 0x20 { let t = dev.z80_access_time(); return dev.io.read(address, t); } if (0x80..0xA0).contains(&top) { dev.lock_up(format!( "Z80 bank access to ${address:06X} (32X area) without a 32X" )); return 0xFF; } 0xFF } fn z80_bank_write(&mut self, address: u32, value: u8) { let dev = &mut self.dev; let socket = dev.claimed(address); if socket != 0 { let t = dev.z80_access_time(); dev.with_device(socket, |device, board| { device.write_byte(board, address, value, t, Z80) }); return; } let top = address >> 16; if top >= 0xE0 { dev.ram[(address & 0xFFFF) as usize] = value; } else if top < 0x40 { dev.cart_write_byte(address, value); } else if top >= 0xC0 { if address & 0xE700E0 != 0xC00000 { dev.lock_up(format!( "Z80 bank access to ${address:06X}: outside the VDP's pattern" )); return; } let t = dev.z80_access_time(); self.vdp.write_byte(&mut self.dev, address & 0x1F, value, t); } else if top == 0xA0 { dev.lock_up(format!( "Z80 bank access to ${address:06X}: the Z80's own space" )); } else if top == 0xA1 { let t = dev.z80_access_time(); if !dev.system_write_devices(address, value, value, false, t) { dev.lock_up(format!( "Z80 write to ${address:06X} through its bank window: the bus request \ and reset registers from the Z80 are not supported by this port" )); } } else if (0x80..0xA0).contains(&top) { dev.lock_up(format!( "Z80 bank access to ${address:06X} (32X area) without a 32X" )); } } // -- the probe (`System.z80_probe`'s bus) -------------------------------------------------- fn probe_read(&mut self, address: u16) -> u8 { let dev = &mut self.dev; let mut probe = dev.probe.take().expect("probing"); let value = if let Some(&value) = probe.written.get(&address) { value } else if address < 0x4000 { dev.z80_ram[usize::from(address & 0x1FFF)] } else { // The bank window through the board's one decode, so a socket's device // answers the probe as it will the access (`Devices::z80_peek`). dev.z80_peek(address) }; if probe.fetching && address == probe.next_fetch { probe.clock += if probe.after_prefix { 4 } else { 3 }; probe.after_prefix = matches!(value, 0xCB | 0xDD | 0xED | 0xFD) && probe.after_prefix; probe.next_fetch = address.wrapping_add(1); } else { probe.fetching = false; probe.offsets.push(probe.clock); probe.clock += 3; if address >= 0x8000 || (0x7F00..0x7F20).contains(&address) { let start = *probe.offsets.last().expect("pushed"); probe.bus_ends.push((probe.clock, start)); probe .bus_cart .push(address >= 0x8000 && (dev.bank | u32::from(address & 0x7FFF)) < 0x800000); } } probe.ends.push(probe.clock); dev.probe = Some(probe); value } fn probe_write(&mut self, address: u16, value: u8) { let dev = &mut self.dev; let probe = dev.probe.as_mut().expect("probing"); probe.fetching = false; probe.written.insert(address, value); probe.offsets.push(probe.clock); probe.clock += 3; if address >= 0x8000 || (0x7F00..0x7F20).contains(&address) { let start = *probe.offsets.last().expect("pushed"); probe.bus_ends.push((probe.clock, start)); probe .bus_cart .push(address >= 0x8000 && (dev.bank | u32::from(address & 0x7FFF)) < 0x800000); } probe.ends.push(probe.clock); } } impl z80_rust::Bus for Board { fn read_byte(&mut self, addr: u16) -> u8 { if self.dev.probe.is_some() { return self.probe_read(addr); } self.z80_read(addr) } fn write_byte(&mut self, addr: u16, value: u8) { if self.dev.probe.is_some() { return self.probe_write(addr, value); } self.z80_write(addr, value); } /// Nothing is on the Z80's I/O bus: z80-python's default reads $FF. fn read_port(&mut self, _addr: u16) -> u8 { 0xFF } /// ... and discards writes. fn write_port(&mut self, _addr: u16, _value: u8) {} }