//! The bus arbiter (YM6045): what the Z80 and the 68000 cost each other. //! //! Transcribed from megadrive-python's `arbiter.py`, whose module docstring carries the //! sources at length: Kabuto, "The bus system" (hardware-measured research) and his //! footnote 1's arithmetic; MoD's logic analyser on a Genesis 2 (forum t=1661; //! hardware-captured), which judges the model; Zilog UM0080 for when the Z80 lets go //! of its bus; and Nuked-MD (closed-box (Nuked-MD)) for everything the hardware sources leave //! open. None of this is modelled by MAME, so `Board::arbiter_enabled` turns the whole //! of it off for the milestone-1 lockstep, as `refresh_enabled` does the refresh. /// How long the 68000 actually holds the bus for one bus cycle, in master clocks. /// /// A 68000 bus cycle is four clocks (28 MCLK), but the bus belongs to the 68000 only /// while /AS is asserted (68000 UM, "Bus Arbitration"). Nuked-MD's bus log: in a /// `jmp (a0)` loop 1,706 of its 1,766 68000 cycles hold /AS low for exactly 18 MCLK /// with a 10-MCLK gap to the next, and the other 60 hold it 32 -- 18 plus the /// cartridge refresh's 14 (docs/worklog.md, the timing session). pub const AS_LOW_MCLK: i64 = 18; /// The arbiter's own propagation, in master clocks, from the Z80 letting go of the bus /// to /BUSAK being seen on the 68000's side: the difference of Nuked-MD's mean grant /// (12.34 68000 cycles over 122 grants) and the machine-cycle rule's (4.15), 8.19 /// cycles rounded to 57 MCLK; **closed-box (Nuked-MD) and fitted** (docs/worklog.md, the timing /// session). pub const BUSACK_PROPAGATION_MCLK: i64 = 57; /// The arbitration of one Z80 access over the 68000's bus, as the die model times it /// (Nuked-MD, all 24,343 bank-window and VDP accesses of Overdrive 2's first 80 /// frames; docs/worklog.md, "The arbiter, measured"), from the Z80's /MREQ at `r`: a /// 68000 cycle that begins by `r` + [`CUTOFF_MCLK`] still goes first; the Z80's own /// cycle on the 68000's bus begins [`GRANT_GAP_MCLK`] after the last of them ends and /// no sooner than [`IDLE_GRANT_MCLK`] after `r`, on the next of the 68000's clock /// edges; it ends on the first Z80 clock edge `r` + [`RELEASE_MCLK`] + 15w (w >= 2 /// wait states) at least [`OWN_CYCLE_MCLK`] after it began; and the 68000's next cycle /// waits for the first of its clock edges [`RESUME_MCLK`] after that. Die-derived; /// MoD's logic-analyser figures (hardware-captured) judge it. pub const CUTOFF_MCLK: i64 = 21; pub const GRANT_GAP_MCLK: i64 = 17; pub const IDLE_GRANT_MCLK: i64 = 29; /// 29 from 30: the Z80's wait runs to the first of its clocks at or past the end of /// its own cycle on the 68000's bus, so a cycle a master clock too long cost a whole /// Z80 clock (+16,155,626 and +16,099,290 both want 24-29). Die-derived; /// docs/worklog.md, "The Z80's cycle a master clock shorter". pub const OWN_CYCLE_MCLK: i64 = 30; /// The own cycle depends on what answers it: never shorter than 31 MCLK for a cartridge /// read on the die (31..45, then the refresh tail), never shorter than 24 for a read of /// the VDP (24..38); 29 for both ended 64 of Overdrive 2's 508 cartridge reads a frame a /// wait state early. With these the board reproduces the die's wait-state and /// 68000-hold distributions for both targets, MoD's experiments are unmoved where they /// were right, and his open `pop` case moves to the referee (megadrive-python /// `arbiter.py`; od2-20k `flaws/FABLE-HANDOFF.md`, "The 68000's frame work /// partitioned", 2026-11-07). A bank read of 68000 RAM keeps [`OWN_CYCLE_MCLK`]. pub const OWN_CYCLE_CARTRIDGE_MCLK: i64 = 31; pub const OWN_CYCLE_VDP_MCLK: i64 = 24; /// A Model 1's Z80 pays more for a cartridge read than the die's ideal logic: about 4 /// MCLK (~75 ns), here 3 on the cycle itself (both CPUs: the Z80 waits for it and the /// 68000 is held for it) and 1 by which the Z80's /WAIT is released after the cycle's end /// (the Z80 only; the 68000 resumes from the cycle's own end). Three hardware judges agree /// on the sum and differ a little on the split: the probe cart's cartridge-sharing counts /// on an NTSC Model 1 with TMSS (ZBS, ZRO1/ZRO2) and the PCM stream of a PAL console's /// capture of Overdrive 2. Hardware-captured, on Model 1s only: the Model 2 and Genesis 3 /// keep the die's 31 and 0, as the nuked-md machine does (`Machine::z80_cartridge_extra`, /// `Machine::z80_cartridge_release`): MoD's Genesis 2 with the 68000 stopped waits "2-3" /// clocks, which the die's figures give and these do not. megadrive-python `arbiter.py`, /// `MODEL1_CARTRIDGE_EXTRA_MCLK`; od2-20k `flaws/FABLE-HANDOFF.md`, 2026-11-10. /// The extra went from 3 to 4 on 2026-11-12 with [`REFRESH_SELF_MCLK`]: with the die's /// refresh rule the console's ZBS3 (Z80 9,338-9,339) is met by 4 (9,338) where 3 gives /// 9,383, and the console model's poses stay all 30 on the console's frame. pub const MODEL1_CARTRIDGE_EXTRA_MCLK: i64 = 4; /// The release was 1 until 2026-11-11: the frame each pose of Overdrive 2's console model /// appears on in a PAL console's capture bounds what the 68000 may lose, and (3, 0) shows /// all 30 on the console's frame where (3, 1) shows four (megadrive-python `arbiter.py`). pub const MODEL1_CARTRIDGE_RELEASE_MCLK: i64 = 0; pub const RELEASE_MCLK: i64 = 37; pub const RESUME_MCLK: i64 = 25; /// A 68000-to-VDP transfer owns the 68000's bus from its trigger write to its last read, /// and a Z80 access asked for in that time waits for all of it (the die: a cartridge read /// caught by a 3,376-word transfer lasts 113,235 MCLK). The Z80's own cycle then begins /// this long after the transfer's last read. Die-derived; megadrive-python `arbiter.py`, /// `DMA_Z80_GRANT_MCLK`. pub const DMA_Z80_GRANT_MCLK: i64 = 16; /// After such a read the 68000's cycle is already under way and only stretched, ending 7 /// MCLK after the Z80's read does: as a cycle of the ordinary 18 MCLK it starts this long /// after the read's end, where after any other Z80 access it waits [`RESUME_MCLK`]. /// Die-derived; megadrive-python `arbiter.py`, `DMA_Z80_RESUME_MCLK`. pub const DMA_Z80_RESUME_MCLK: i64 = 7 - AS_LOW_MCLK; /// The bus cycles a Z80 access asked for at `t` is arbitrated against: the 68000's, and a /// 68000-to-VDP transfer's hold on the bus (`dma`: when it took the bus, its last read, /// when the 68000 has the bus back) as one long cycle when the access falls in it /// (megadrive-python `Board.arbiter_grid`). pub fn arbiter_grid( t: i64, m68k_cycles: &[[i64; 2]], dma: Option<(i64, i64, i64)>, ) -> std::borrow::Cow<'_, [[i64; 2]]> { match dma { Some((begin, last, _)) if begin <= t + CUTOFF_MCLK && last > t => { let mut grid = m68k_cycles.to_vec(); // `z80_access_wait` grants GRANT_GAP_MCLK after a cycle's end. grid.push([begin, last + DMA_Z80_GRANT_MCLK - GRANT_GAP_MCLK - begin]); grid.sort_unstable(); std::borrow::Cow::Owned(grid) } _ => std::borrow::Cow::Borrowed(m68k_cycles), } } pub const MIN_WAIT: i64 = 2; pub const REFRESH_MCLK: i64 = 14; /// A refresh left alone on a free bus runs by itself from this long after it fell due, /// for its [`REFRESH_MCLK`]: a Z80 cycle that begins while it runs waits for its end, and /// one after it pays nothing (the die, alewman-probe's ZBS3). Die-derived; megadrive-python /// `arbiter.py`, `REFRESH_SELF_MCLK`. pub const REFRESH_SELF_MCLK: i64 = 112; /// Where the 68000's clock edges fall, mod 7 MCLK: where its cycles begin /// (`system::M68K_POWER_ON_PHASE`, the die's relation in the board's time). pub const M68K_EDGE_PHASE: i64 = 4; /// For a Z80 access over the 68000's bus with /MREQ at `request` (MCLK), given the /// 68000's bus cycles as `[start, span]` and the cartridge refresh still owed /// (`refresh`, the clock it fell due, or None): the Z80's wait states, the master /// clock from which the 68000 may start its next cycle, and when the Z80's own cycle /// began. A refresh owed by then is taken by the Z80's own cycle, which takes /// [`REFRESH_MCLK`] longer -- Kabuto's `b`, "The Z80 bus access itself can be delayed /// as well by what seems to be refresh logic (up to 2 more cycles)". `own_cycle` is the /// least the Z80's own cycle lasts, by what it reads ([`OWN_CYCLE_CARTRIDGE_MCLK`], /// [`OWN_CYCLE_VDP_MCLK`], else [`OWN_CYCLE_MCLK`]). `release` is how long after its /// cycle's end the Z80's /WAIT is let go ([`MODEL1_CARTRIDGE_RELEASE_MCLK`]): the Z80 may /// wait a clock more for it, and the 68000 resumes from the cycle's own end. pub fn z80_access_wait( request: i64, m68k_cycles: &[[i64; 2]], refresh: Option, own_cycle: i64, release: i64, ) -> (i64, i64, i64) { let mut start = request + IDLE_GRANT_MCLK; for &[begin, span] in m68k_cycles { if begin > request + CUTOFF_MCLK { break; } if begin + span > request { start = start.max(begin + span + GRANT_GAP_MCLK); } } start += (M68K_EDGE_PHASE - start).rem_euclid(7); // on one of the 68000's clock edges let mut length = own_cycle; if let Some(refresh) = refresh { if refresh <= start { let late = start - refresh; if late < REFRESH_SELF_MCLK { length += REFRESH_MCLK; } else if late < REFRESH_SELF_MCLK + REFRESH_MCLK { // Running by itself when the cycle begins: the cycle waits for its end. length += (REFRESH_SELF_MCLK + REFRESH_MCLK - late + 6) / 7 * 7; } } } let mut bus_wait = MIN_WAIT; while request + RELEASE_MCLK + 15 * bus_wait < start + length { bus_wait += 1; } let mut wait = bus_wait; while request + RELEASE_MCLK + 15 * wait < start + length + release { wait += 1; } let end = request + RELEASE_MCLK + 15 * bus_wait; (wait, end + RESUME_MCLK, start) } /// The master clock /BUSAK reaches the 68000, given the request's clock and the ends /// of the Z80 step's machine cycles: Zilog UM0080's rule for *when* the Z80 lets go /// (the end of the machine cycle in progress; specification tier) and the arbiter's /// own propagation for how long the board takes to say so /// ([`BUSACK_PROPAGATION_MCLK`]; fitted to Nuked-MD, closed-box (Nuked-MD)). pub fn busack_grant(request: i64, machine_cycle_ends: &[i64]) -> i64 { for &end in machine_cycle_ends { if end >= request { return end + BUSACK_PROPAGATION_MCLK; } } request + BUSACK_PROPAGATION_MCLK }