# The ULA and memory contention The Ferranti ULA (16K/48K, 128K) and Amstrad's gate arrays (+2, +2A/+3) do one job that matters more than any other for accuracy: they share the screen RAM with the Z80 by stopping the Z80's clock. Every Spectrum timing effect that software relies on (border stripes, multicolour engines, "racing the beam", tape loaders' edge timing in contended code) follows from this. Sources and their tiers are in validation.md; short names as in start-here.md. ## The video frame (DOC: FAQ48, FAQ128; DIE: emu-russia/ula) | | 48K | 128K / +2 | +2A / +3 | | --- | --- | --- | --- | | CPU clock | 3.5 MHz (pixel clock 7 MHz, 14 MHz crystal) | 3.54690 MHz | 3.54690 MHz | | T-states per line | 224 | 228 | 228 | | lines per frame | 312 | 311 | 311 | | T-states per frame | 69,888 (50.08 Hz) | 70,908 (50.02 Hz) | 70,908 | | lines before the first screen line | 64 | 63 | 63 | | first screen byte displayed, T after /INT (lines x T/line) | 14,336 (64 x 224) | 14,364 (63 x 228) | 14,364 (FAQ128) | | first screen-byte fetch on the ULA bus (Wiki, floating bus) | 14,338 | 14,364 | (no floating bus table) | | border OUT that lands on the first screen byte ends at T | 14,339-14,342 | 14,365-14,368 | (not given) | DIE: the 6C001 netlist's horizontal counter counts 448 7 MHz ticks a line and the vertical counter resets on the 312th line (specs/ula-modules.en.md sections 3-4): structure agreeing with FAQ48's 224 x 312. The netlist is for revision S-ULA6C001 6790-VII (Issue 3 and later 48K boards); no die is available for 5C112 (Issue 2), 7K010 (128K) or the Amstrad parts. 48K line (FAQ48): 128 T of screen (two bytes fetched, pixels then attribute, every 4 T), 24 T of right border, 48 T of horizontal blanking, 24 T of left border. A pixel every half T-state. FLASH swaps ink and paper every 16 frames. ## Memory contention: the pattern (DOC: FAQ48, FAQ128, Wiki; TEST ROM: collection) During the 128 screen T-states of each of the 192 screen lines the ULA owns the DRAM in 8-T-state cycles. A Z80 access to contended memory that starts at T is delayed by: ``` 48K: T 14335 + 224*line + 8*k + j -> delay [6,5,4,3,2,1,0,0][j] (k = 0..15) 128K/+2: T 14361 + 228*line + 8*k + j -> same pattern +2A/+3: T 14361 (Wiki) or 14365 (FAQ128) + 228*line + 8*k + j -> [1,0,7,6,5,4,3,2][j] ``` FAQ48's version 1.1.5 notes record the move of the first contended cycle from 14,336 to 14,335 (48K) and to 14,361 (128K). For the +2A/+3 the FAQ and the Wiki are **4 T-states apart** (C1): FAQ128 starts its table at 14,365 (delay 1), 14,366 (0), 14,367 (7); the Wiki at 14,361, 14,362, 14,363, and says the pattern stops at 14,490 and restarts at 14,589 where the FAQ says 14,494 and 14,593. The difference is the same 4 T throughout, so one of them numbers from a different origin; ULA 128E +3 Test (azesmbog) and Float+3 decide it on the board (validation.md rung 4). **Which memory is contended** (DOC): 48K 4000-7FFF; 128K and grey +2 banks 1, 3, 5, 7 (the service manual says 4-7; the Wiki puts the difference down to the HAL10H8's B0/B2 inputs: C4); +2A/+3 banks 4, 5, 6, 7 (+3 manual part 24: "RAM pages 4 to 7 which are contended"). The +3 manual part 30 states the duty: "during 128 out of every 228 CPU T states ... and during 192 out of every 311 TV lines ... the CPU is allowed only 1 access to contended RAM in every 8 T states." ## When in an instruction: the per-instruction breakdown (DOC: FAQ48 v1.1.5, Wiki) Contention is applied at **T1 of every machine cycle that puts a contended address on the bus**, and, on the Ferranti ULAs, also on internal T-states during which the Z80 leaves a contended address on the bus with no MREQ. FAQ48 tabulates this per instruction in a notation the board will use directly: `pc:4,pc+1:3,[pc+1:1 x 5]` for `JR n` means "test PC and wait, run 4 T; test PC+1 and wait, run 3 T; if taken, five times: test PC+1 and wait, run 1 T". The Wiki's table uses the same notation with a column for the Amstrad gate array. Examples: | Instruction | FAQ48 (v1.1.5) | Wiki, "ULA" column | | --- | --- | --- | | NOP | pc:4 | pc:4 | | LD A,I / LD A,R | pc:4, pc+1:5 | pc:4, pc+1:4, ir:1 | | INC dd | pc:6 | pc:4, ir:1 x 2 | | ADD HL,dd | pc:11 | pc:4, ir:1 x 7 | | PUSH dd | pc:5, sp-1:3, sp-2:3 | pc:4, ir:1, sp-1:3, sp-2:3 | | LD r,(ii+n) | pc:4, pc+1:4, pc+2:3, pc+2:1 x 5, ii+n:3 | (same) | | INC (HL) | pc:4, hl:3, hl:1, hl(write):3 | (same) | | LDIR | pc:4, pc+1:4, hl:3, de:3, de:1 x 2, [de:1 x 5] | (same) | | EX (SP),HL | pc:4, sp:3, sp+1:4, sp(write):3, sp+1(write):3, sp+1(write):1 x 2 | pc:4, sp:3, sp+1:3, sp+1:1, sp+1(write):3, sp(write):3, sp(write):1 x 2 | Two disagreements matter (C13, C14). **C13:** FAQ48 counts the extra T-states after an opcode fetch (`pc:6`, `pc:11`, `pc+1:5`, `pc:5`) as part of the fetch, uncontended; the Wiki contends each of them with **IR** on the address bus (`ir:1 x n`): the refresh address the Z80 leaves there. That is exactly what Woodmass's IR Contention tests (48K, 2006; 128K, 2023, with real-hardware results on their pages) measure, and it is why I pointing at contended memory slows these instructions. **C14:** in EX (SP),HL FAQ48 writes SP first, the Wiki writes SP+1 first; z80-python writes SP+1 first (`_control.py` `_op_ex_sp_hl`, after the z80-survey's bus-order fix, alewman/z80-python#8, decided by SingleStepTests and FUSE: emulator-derived), so the Wiki and the core agree and FAQ48 is the outlier. The 48K NEC Contention Test Suite and the timing tests are the hardware-verified judges of both. The **+2A/+3 gate array** contends only when MREQ is active (Wiki): the `x:1 x n` internal entries are not contended there, and multiple entries for one address merge (FAQ128: "JR becomes pc:4,pc+1:8"). The `:1 x n` entries are the part no instruction-level core can supply: they need the address the Z80 leaves on the bus during internal T-states. z80-python's access-timing table (origin/main `2f334ef`) gives the offset of every MREQ/IORQ access but not these internal cycles: cpu-core.md, need N1. FAQ48's own table has a slip: `BIT b,(ii+n)` is listed as `pc+1:4,pc+2:3,...`, missing the prefix's `pc:4` that its notes say every DD/FD instruction has (C7, a typo by the notes' own rule). ## Contended I/O (DOC: FAQ48, Wiki "Contended I/O") An I/O cycle is 4 T. Two effects combine: the ULA contends its own port (A0 = 0), and it mistakes a port address in 4000-7FFF for a memory access: | High byte 40-7F? | A0 | Pattern | | --- | --- | --- | | no | 1 | N:4 | | no | 0 | N:1, C:3 | | yes | 1 | C:1, C:1, C:1, C:1 | | yes | 0 | C:1, C:3 | (`C:n` = wait as for a contended memory access at this T, then run n T.) The two sources disagree on the 128K with a contended bank paged at C000: FAQ48 says ports C000-FFFF are never contended "even on a 128K machine if a contended memory bank is paged into the range 0xc000 to 0xffff"; the Wiki says contention "is also applied" there (C3). IO Contend (Woodmass) and the 128K timing tests decide. On the +2A/+3 no I/O contention occurs (Wiki: no MREQ during I/O); FAQ128 says port FE is not contended there and that whether 7FFD and 1FFD are "is currently unknown" (U4). ## Early and late timing (DOC, OBSERVED on real units) FAQ48 (2004): "machine timings vary, even between machines with the same model of ULA. Some machines use the timings given in this FAQ, while others are one T state later ... The reason for this is not currently understood." The Wiki: a ULA "drifts from early timing to late timing" as it heats; the collection's Late-timings page (2025, after Marta Sevillano Mancilla's investigation) instead: /INT begins one T-state earlier when /RFSH was low in the preceding T-state, and the effect is temperature dependent, with some machines late when cold. The ZX Spectrum Timing Tests 48K page records a 6C001E-7 machine showing late timings "cold" after being cooled under an air-conditioning vent. The three accounts disagree on direction and mechanism (C2). Amstrad gate-array machines show no early/late split (Wiki). Board consequence: an `early`/`late` switch per ULA model is the evidence-backed shape; the /RFSH-dependent rule is the most specific account and is HYPOTHESIS until a test in the collection that distinguishes it (Time to First INT, INT Tester) is run against it. ## What the die can and cannot decide (DIE: emu-russia/ula) The netlist (661 cells, 519 matched gate for gate against the module-level HDL under Icarus; specs/hdl-vs-netlist-verification.md) settles structure: counters, the fetch sequence, the port FE logic, which pins feed the arbiter (`nMREQ`, `nIOREQ`, `A14`, `A15`, `Border`, two counter bits). Our reading of its arbiter gates (section 19) is that the "CPU wants RAM" term is formed from A14, A15 and IOREQ, with MREQ entering only through a latch (`MREQT2`) that **cancels** the stall once the access is under way: the same mechanism the Wiki describes ("the contention cancellation mechanism for memory requests") and the reason internal cycles with a contended address are stalled. That reading is HYPOTHESIS (ours, from the gate list), and the project's own simplified C++ model (`cpuRAM = nMREQ == 0 && A14 && !A15`) says the opposite (C8); its author calls the arbiter "an asynchronous ring ... not yet worked out". By the charter's rule a race inside one clock phase yields to real-unit test verdicts: the die is a structural witness here, not the judge of the stall lengths. ## The 128K models' differences (DOC: FAQ128, Wiki) - Snow and the I register: on the 128K, I pointing at contended memory both snows and "appears to crash the machine shortly after" (FAQ128; Wiki: some 128s hang or reset, others do not). floating-bus-and-snow.md. - The +2A/+3 read port FE bit 6 as 0 with no signal, without the 48K's dependence on bits 3 and 4 of the last OUT (FAQ128).