# Bus arbitration: who owns the 64-bit bus, and what it costs Sources: **[TRM]** p.7-8, 17, 19, 21, 24, 29, 35-36, 59, 61, 69, 75, 134-138, 141 (tier 2); **[NET]** `tom/ARB.NET` (164 lines, read whole), `tom/MISC.NET` l.171-240 (refresh), `tom/MEM.NET` (735 lines, not read) (tier 2 as text); the libretro project's `docs/gpu-timing-verilator-results.md` and `docs/jtrm-errata.md` (Verilator runs of the netlist translation: **reported numbers, not rerun here**); BigPEmu's FAQ and the MiSTer README (tier 5 and 4, for what they admit). This is the part of the machine no public emulator models from first principles, and the part that decides how fast every game runs. It is the board's (`jaguar_python.bus`). ## Masters and priorities The manual's list [TRM p.8] and the arbiter netlist (`ARB.NET` l.85-95, `Req[10]` down to `Req[0]`) **agree line for line**, highest first: | Level | Master | Request signal | | --- | --- | --- | | 10 | a daisy-chained external bus master | `brlin` | | 9 | DRAM refresh | `refreq` | | 8 | DSP at DMA priority (`D_FLAGS` `DMAEN`) | `dbrl[1]` | | 7 | GPU at DMA priority (`G_FLAGS` bit 15 `DMAEN`: loads and stores only) | `gbreq[1]` | | 6 | Blitter at high priority (`B_CMD` bit 29 `BUSHI`) | `bbreq[1]` | | 5 | Object Processor | `obbreq` | | 4 | DSP | `dbrl[0]` | | 3 | 68000 while handling an interrupt (`ihandler`, until `INT2` is written) | | | 2 | GPU | `gbreq[0]` | | 1 | Blitter | `bbreq[0]` | | 0 | 68000 ("always requesting": the request is tied high) | | ## The mechanism (`ARB.NET`, in our words) - The 68000 owns the bus by default. Tom is a 68000-style peripheral master: when any internal or DSP request outranks the CPU's current level, a three-state machine goes **idle -> request** (assert bus request to the 68000, wait for its bus grant) **-> hold** (assert bus grant acknowledge). The 68000 finishes its current bus cycle and then sits off the bus. - "Bus ownership may only be changed when the CPU has relinquished ownership and when the memory state machine allows it" (comment, l.119-122): inside *hold*, the highest request is re-latched **each time a memory cycle completes** (`Arben := q2 AND ack`). So the masters interleave **per memory cycle** with no hand-over cost, as the manual says [TRM p.21], and ownership "can be lost during a request (but not during a memory cycle)" [TRM p.29]. - When the best request is the CPU's (level 0, or level 3 under interrupt) the machine drops back to idle and the 68000 resumes. **Under interrupt the GPU and Blitter at normal priority are masked out entirely**, which is the manual's warning about `INT2` seen from the other side. - The DSP is outside Tom: Jerry raises one of two request lines and receives a grant that "should only be asserted after current cycle is complete" (l.147). Jerry then runs **16-bit** cycles, so a DSP long is two cycles and a phrase four [TRM p.97]. Tom bug 12 and Jerry bug 3 are glitches of exactly this handshake. ## What each master does with the bus | Master | Pattern | Source | | --- | --- | --- | | 68000 | every instruction fetch and data access; 16-bit; a cycle is asynchronous to Tom, timed by the memory controller | [TRM p.8] | | Object Processor | per object per line: 2-3 header reads, `IWIDTH` data reads, 1-2 header writes; holds the bus between data fetches unless the object's `RELEASE` bit is set | [TRM p.7, 19]; `OB.NET` | | GPU | nothing while it runs from local RAM with local data; each external `LOAD`/`STORE` is one cycle (two halves for `LOADP`/`STOREP`); **running code from DRAM or ROM** fetches a long every two instructions and gives the bus up between fetches unless `G_CTRL` bit 11 `BUS_HOG` | [TRM p.35-36, 61] | | Blitter | requests at `B_CMD` write, keeps requesting until done: up to 8 cycles per inner-loop step | [TRM p.69] | | DSP | as the GPU, 16-bit, its sound driver usually under interrupt at sample rate | [TRM p.97] | | Refresh | a counter collects due refreshes at `CLK / (64 x (REFRATE+1))`; at 8 pending it takes the bus for 8 cycles; after every **stop object** it drains whatever is pending | [TRM p.24]; `MISC.NET` l.180-240 | ## What a cycle costs (system clocks) [TRM p.7, 10] - DRAM, same row as the previous access ("page mode"): **2**. - DRAM, row change: 3 to 7 more by `DRAMSPEED` (the console writes 3: precharge 2, RAS-to-CAS 1). Row = everything above the column bits, so which accesses are "page hits" depends on `MEMCON2` `COLS` and on **who used the bus last**: an interleaved master costs the next one a row miss. This coupling is why `RELEASE`, `BUSHI` and `BUS_HOG` exist. - ROM: 10, 8, 6 or 5 by `ROMSPEED`, per ROM-width unit (a 32-bit cart read as a phrase is two). - I/O (Tom and Jerry registers, GPIO): 18, 10, 4 or 6 by `IOSPEED` (console: 6). - GPU local RAM and registers: no bus cycle at all. - Read-to-write turnaround of the Blitter: +1. **Reported from netlist simulation** (libretro project, Verilator 5 on Torlus's translation of the `graphics` block; their rig, their numbers): an external GPU `LOAD` takes `7 + D` clocks from retire to a dependent use, where `D` is the memory cycle length (9 with a 2-clock cycle on an idle bus); a NOP stream in local RAM retires one instruction per clock with one program fetch every two. These are the first published clock figures for the GPU that do not come from an emulator's guess. Reproducing them needs Verilator (not on `PATH`; a 5.053 build exists in `../sm83-python/reference/build/oss-cad-suite`): open decision 6 in the brief, costs in [validation.md](validation.md). ## The 68000 on this bus - It is **stopped for as long as Tom holds the bus**. A GPU running from DRAM with `BUS_HOG`, or a long blit, or an Object Processor line full of objects, each take the 68000's time away; "the CPU normally has the lowest bus priority" [TRM p.8]. - Games add their own stop: `STOP` between interrupts, or a GPU-driven game whose 68000 only services the vertical interrupt. In both cases the 68000's *wake-up latency* is observable (it has to win the bus back) and is governed by `ihandler` and `INT2`. - The manual's bug 15 says `JUMP`/`JR` executed from external memory can corrupt the prefetch queue, "this rules out almost all programs" in DRAM; yet running GPU code from main RAM is a known technique of later software (not verified title by title here). A model must support external GPU execution with its real bus cost, and must not need the bug. ### What `m68000-python` gives and lacks for this Pin: `3029f56` (HEAD of `../m68000-python`, the v0.1.0 release plus README fixes; `megadrive-python` pins the earlier `38dbd0f`). - **Has**: `step()` per instruction returning clocks; `cpu.step_clocks` inside a bus callable ("at which clock of the step the access ends ... the host adds its own stall clocks"); `set_ipl(level)`; `acknowledge(level)` returning a vector number (the Jaguar's `0x40`); STOP; `reset_devices`. Its claims page puts "bus arbitration ... DMA stalls" and "wait states the host inserts" in the host, by decision. - **Enough for a first build**: the board advances the other masters up to the 68000's clock inside each bus callable (catch-up), asks the arbiter when the bus is free, and adds the wait to its own running clock. Bus request/grant needs no core hook as long as grant happens at bus-cycle boundaries, which is what a 68000 does. - **Lacks, to raise on the core** (issue, not a fork): (1) a statement, tested, that stall clocks added by the host from inside a callable are safe at *every* access of *every* instruction (the Mega Drive uses it for DMA and VDP waits only); (2) the bus cycle's own shape (when address strobe falls, when DTACK is sampled) is "not modelled": the Jaguar's memory controller decides the length of each 68000 cycle, so the board needs the *start* of each access as well as its end, or it cannot say when the cycle could have been granted; (3) an exact STOP-to-handler clock count is *provisional* in the core. None blocks M1-M6. ## The bugs that are arbitration Tom 6 (asynchronous bus grant can crash the arbiter: a console wiring matter), **24 (nothing may outrank the Object Processor: `DMAEN` and `BUSHI` produce black stripes)**, 12 and Jerry 3 (one-clock grants; Jerry seeing a previous grant: wrong write data). The *Software Reference Manual* v2.4 (June 1995; OCRed here) tells developers never to set `DMAEN` ("must not be set due to a bug in the Jaguar Console"), `BUS_HOG` ("should not be set in the Jaguar Console") or `BUSHI`; the *Hardware Bugs & Warnings* sheet adds that `D_FLAGS` `DMAEN` with an external load or store hangs the DSP until reset. Policy: implement the priorities as the netlist has them; **log** every use of those three bits per game in the sweep, because each is a place where hardware misbehaves and a clean emulator picture is the wrong answer. ## What the detectors do (so their agreement is weighed correctly) - **Virtual Jaguar (classic)**: no arbitration; processors run in fixed slices; a blit is instant. - **Virtual Jaguar libretro** (GPL-3.0; read in Phase 0, not fetched since 2026-11-24: HYPOTHESIS): a bus arbiter with priorities from `ARB.NET` and costs partly from the manual, partly labelled estimates; `BUSHI`/`DMAEN` escalation not modelled (its errata A3). - **BigPEmu**: "the heuristic estimates for determining cycle usage during bus contention will often be incorrect"; defaults "err on the side of running too fast"; "full emulation of bus access and priorities" is a stated future goal. - **MAME**: scheduler slices; its own hack for `battlesp`/`battlesg` notes the game "depends on what shared RAM contains at power on and what's the actual blitter status when going idle". - **MiSTer**: the real arbiter and memory controller netlists, but with FPGA memory behind them: "the latency of SDRAM is a bit too high for more games to run cycle accurately with only one SDRAM module". So **no tier 4-5 source can referee contention**. The judges are the netlist (as text now, as a simulation if built) and, for whole-game pace, the very few published hardware observations (the libretro project cites a camcorder measurement of the Doom attract demo at about 4 fields per frame on a console against 2 in emulators: tier 1 in kind, a single informal capture). ## Proposed model One timeline in system clocks. `Bus` holds: the owner, the clock at which the current memory cycle ends, the last DRAM row, the refresh counter, the pending requests by level. Every master is a coroutine-like object that asks `bus.cycle(master, level, address, width, write)` and is told the clock at which its cycle completes; between its bus cycles a master runs free on its own clock (the GPU in local RAM never calls the bus). The scheduler always advances the master that is furthest behind, up to the next clock at which another master could affect it (a register write, an interrupt, a bus request of higher level). Granularity is **the memory cycle**, the unit the hardware arbitrates in. Cost: see the brief's performance section.