# The Gate Array: video, palette, interrupts, ROM enables, and the Z80's clock Source names as in start-here.md. Technical information in this document is sourced in part from the "Amstrad CPC CRTC Compendium" by Longshot (CC BY-NC-ND); see provenance.md. ## Parts (DOC: ACCC 9; KT `cpcptno`; DIE: Gerald 40010) | Part | Where | Notes | | --- | --- | --- | | 40007 (also marked 40007-4, -4x), 40008 | 464, early 664, rare 6128s | pin-compatible with each other, **not** with the 40010 (ACCC 9, note 1); made by Ferranti (ULA20RA023, ULA16RA023: ACCC 9). Usually under a heat sink | | 40010 (versions 37AA and 36AA) | 664, 6128, later 464 | Matrix LSI HSG3170 / HSG3130 (ACCC 9, crediting Gérald Vincent). **The one with a die-level schematic** (Gerald 40010, 18 KiCad sheets, 2019) | | ASIC 40226 | "cost-down" 464/6128 | Gate Array and 6845 in one ASIC ("CRTC 4"); the 8255 stays separate (ACCC 29.1.2) | | ASIC 40489 | 464 Plus, 6128 Plus, GX4000 | Gate Array, 6845 ("CRTC 3") and 8255 in one ASIC, plus the Plus features (plus-asic.md) | The models are not identical: the 40007/40008 process the fetched byte's bits about 1/16 µs ahead of the 40010 (ACCC 9), and on the 40007, 40008, 40010 and 40226 mode-2 pixels appear one mode-2 pixel (1/16 µs) earlier than the other modes' pixels, which the 40489 does not do (ACCC 9.1). The board therefore takes the Gate Array part as a parameter beside the CRTC type (handoff brief, decision 3). ## Programmer's model (DOC: ap12 E; KT `garray`; ACCC 27.2) One write-only port, `7Fxx` (A15 = 0, A14 = 1). Data bits 7-6 choose the function: | Bits 7-6 | Function | Bits 5-0 | | --- | --- | --- | | 00 | palette pointer | bit 4 = 1: border; else bits 3-0 = pen 0-15 | | 01 | palette memory | bits 4-0 = hardware colour 0-31 (27 distinct colours) | | 10 | mode and ROM enable ("RMR") | bit 4: clear the interrupt counter (see C3); bit 3: upper ROM disable; bit 2: lower ROM disable; bits 1-0: mode | | 11 | not the Gate Array: the 6128's RAM PAL (memory-roms-expansion.md) | bank configuration | - Modes: 0 = 160 x 200, 16 colours; 1 = 320 x 200, 4; 2 = 640 x 200, 2; 3 = 160 x 200, 4, "do not use" (ap12 E) but displayable, with each byte's bits 5-4 and 1-0 ignored (ACCC 9.1). - A mode change takes effect at the next HSYNC (ap12 E: "synchronises mode changing to the next horizontal flyback"; KT `garray`). ACCC 9.3 measures the exact moment per CRTC type, including mid-line splits. - Reset: the RMR is cleared (both ROMs enabled, mode 0); the palette is undefined except the border, which is black (ap12 E). - Pixel bit order per mode: ap12 B's table and ACCC 9.1 agree (mode 0: pixel 0 is bits 7, 3, 5, 1 read as B0..B3; mode 1: pixel 0 is bits 7 and 3). - When a palette write is seen on screen depends on the instruction (`OUT (C),r`, `OUT (n),A`, `OUTI`) and on the CRTC/ASIC; ACCC 9.2.2 gives the pixel-exact diagrams. **C1. RMR encoding.** ap12 E, ACCC 27.2 (`10xIRrmm`) and the MAME run (OBSERVED: the firmware writes `89`, `85`, `81`) say function `10`; KT `garray`'s function table prints "1 1" for both the RMR and the RAM function, and its RMR summary prints "0 1". Read as a slip in KT's tables; `10` is used. ## Video fetch and output (DOC: ap12 B, D; ACCC 4.1, 6, 9) - Every microsecond the Gate Array reads two bytes from the base 64 KB at the address built from the CRTC's MA and RA outputs: address bits 15-14 = MA13-12, bits 13-11 = RA2-0, bits 10-1 = MA9-0, bit 0 = which of the two bytes (ap12 B's description of the 2 KB blocks and the 16 KB pages; to be checked against Gerald 40010's address multiplexer at M2). MA11-10 and RA3-4 are not used for the address (HYPOTHESIS from the same description: check on the schematic). - The CRTC's DISPEN gates pixels against the border colour; HSYNC and VSYNC from the CRTC are reshaped by the Gate Array into the monitor's sync (the Gate Array's HSYNC to the monitor is not the CRTC's raw pulse: ACCC 13, 14 and 27.6 measure the offsets, and on CRTC 3/4 the ASIC's HSYNC is aligned with its own display, ACCC 27.6.1). - Colour output: three outputs per gun at three levels (0, half, full) give 27 colours; the 32 codes include five duplicates (ap12 E; KT `garray`). Duplicates are not strictly identical electrically (ACCC 9.2.1). The output levels are analogue facts the board does not need beyond the 27-colour index; a host's palette is a host choice (later row: measured RGB levels, U4). ## Interrupts (DOC: ap12 A, E; ACCC 27; KT `ints`) A 6-bit counter ("R52") in the Gate Array: 1. Counts at the **end** of each CRTC HSYNC (ACCC 27.1). KT `ints` left the edge open ("to be confirmed"); ACCC states the end, measured. 2. When it passes 51 it returns to 0 and the Gate Array raises /INT (ACCC 27.3.1; KT `ints`; ap12 A: "every 52 scan lines"). 3. /INT stays raised until the Z80 acknowledges (M1 with IORQ), which also clears counter bit 5, so the next interrupt is at least 32 lines away (ap12 A; KT `ints`; ACCC 27.3.1: the clearing happens at the end of the instruction after `EI` when the interrupt was pending). 4. Two HSYNCs after VSYNC starts, the counter is reset to 0; an interrupt is raised at that moment only under the bit-5 condition (C2), which re-synchronises the six interrupts per frame to the frame (ap12 A: "approximately 2 scans (125 microseconds) into the 8 scan ... vertical flyback"). 5. RMR bit 4 = 1 resets the counter (C3) and clears a pending request (KT `ints`). 6. Interrupt timing relative to HSYNC: "an interrupt always starts 1 µsec after the end of the HSYNC regardless of the CRTC", and HSYNC itself starts 1 µs earlier relative to the display on CRTCs 0-2 than on 3-4 (ACCC 27.6.1). R3 = 0 gives no HSYNC, hence no counting, on CRTCs 0 and 1; a 16-µs HSYNC on 2, 3 and 4 (ACCC 27.6.3-27.6.5, 28.1.5). 7. The race at counter 31 when an acknowledge's bit-5 clear and an HSYNC end coincide has two outcomes depending on the instruction's length (ACCC 27.7.1): the board must order the two events by T-state, which is why the interrupt acknowledge's access timing matters (cpu-core.md, N3). **C2. The VSYNC rule's condition.** ACCC 27.3.2: two HSYNCs after VSYNC "an interrupt is requested ... only if bit 5 of R52 is 1", and R52 is set to 0 unconditionally. KT `ints`: "If the top bit of the 6-bit counter is set to 1 (i.e. the counter >= 32), then there is no interrupt request, and the 6-bit counter is reset to 0" (the other branch is cut off by an HTML `<` in the archived page). The two say opposite things. ACCC is measured on real machines with SHAKER and is internally consistent with the "no two interrupts closer than 32 lines" purpose (an interrupt at VSYNC is safe only if the last one was >= 32 lines ago). **Resolution path:** the die (Gerald 40010's interrupt sheet) and a SHAKER interrupt test. ACCC is used until then. **C3. What RMR bit 4 clears.** ap12 E: "writing a 1 to bit 4 clears the top bit of the divide by 52 counter". ACCC 27.2 and KT `ints`: it returns the whole counter to 0 (ACCC also orders it against an HSYNC end in the same microsecond). Resolution: the die sheet, then a SHAKER/interrupt test. ACCC and KT are used until then. IM 2 on the "old" CPCs reads a floating vector byte (no device drives the bus during the acknowledge), which can differ between machines (ACCC 27.5; KT `im2`). The board returns a configurable value, default FF (HYPOTHESIS, U3). The interrupt acknowledge lasts 5 µs in IM 1 (ACCC 27.4: an RST 38 executed as an instruction is 4 µs, the acknowledge 5 µs) and 7 µs in IM 2 (ACCC 27.5). ## The Z80's /WAIT (DOC: ap12 A; ACCC 4.4.4, 26; DIE: Gerald 40010) The Gate Array divides 16 MHz into a 4-phase cycle per microsecond; in three of the four Z80 T-states it holds /WAIT low ("continually generate 3 Tw followed by a no-Tw cycle", ACCC 4.4.4). A Z80 M-cycle that samples /WAIT while it is low waits until the free phase, so every opcode fetch, memory read, memory write and I/O cycle is aligned to the 1-µs grid. This is the mechanism; what it asks of z80-python is in cpu-core.md. The Gate Array also drives /RAS, /CAS and the 74LS153 address multiplexer's select lines, the CRTC clock, the AY clock and the CPU clock from the same sequencer (Gerald 40010's top sheet shows the sequencer and CAS generation blocks); the board models only what software can see: the alignment, the video fetch slot, and the clocks' phases relative to it (U1). ## Not modelled in the first rows - Electrical effects (parasitic pixels at border changes in mode 2 on some units, ACCC 9.2.1). - The KC Compact's TTL/Z8536 replacement. - The analogue colour levels (U4).