# The ASIC: video, paging, contention, interrupts and ports The SAM Coupé's one custom chip (MGT's 10,000-gate ASIC, designed by Bruce Gordon; TECH p1, UTM "Hardware oddities") does the work of the Spectrum's ULA and more: it fetches the display in four modes, maps 512K of RAM into the Z80's 64K in 16K pages, contends the Z80's access to internal RAM and to its own ports, raises four interrupts and decodes the ports F8-FF. It stays inside the board (chip rule: no other family machine uses it). Citation keys are in [sources.md](sources.md). Evidence tags as the family uses them: DOC (a document), MEASURED (a hardware measurement someone published), OBSERVED (MAME, run here), HYPOTHESIS. ## Clocks and the frame | Fact | Value | Evidence | | --- | --- | --- | | CPU | Z80B, 6 MHz | DOC (TECH p1); OBSERVED (MAME: "Zilog Z80 @ 6.00 MHz") | | Line | 384 T-states = 48 cells of 8 T-states (one cell = 8 pixels, mode 4 width) | DOC (SO-M1, from his border-bar measurements) | | Frame | 312 lines, so 119,808 T-states and 50.08 Hz | OBSERVED (MAME -listxml: `htotal 768`, `vtotal 312`, pixel clock 12 MHz, refresh 50.080128); consistent with 6,000,000 / (384 x 312) = 50.0801 | | Display | 256 x 192 main area in a 48-cell line; the rest border | DOC (TECH p15-16, SO-M1) | | SAA1099 and WD1772 clocks | 8 MHz | DOC (TECH p11: an 8 MHz line on the drive connector; SAA-DS: nominal 8 MHz); OBSERVED (MAME: both at 8.00 MHz) | MAME's trace (mame-oracle.md) ran 359,419 T-states in its first three frame notifications, 5 T-states short of 3 x 119,808, which agrees with the 312-line frame (OBSERVED, a detector). Where the frame interrupt falls within the 312 lines, and how many lines of border sit above and below the 192, is not in any pinned document (U3). ## Screen modes (VMPR bits 5-6) All four modes read from the page pair named by VMPR bits 0-4 (TECH p15-17). Modes 3 and 4 need 24K, so the fetch runs from the even page into the odd one (TECH p15: "By entering page 12 to the VMPR, video wraps to page 13", "even to odd always applies"). | Mode | VMPR b6,b5 | Picture | Memory | Evidence | | --- | --- | --- | --- | --- | | 1 | 00 | 32 x 24 cells of 8 x 8, two colours per cell from 16 CLUT entries; the Spectrum's bitmap and attribute layout | 6K bitmap + 768 bytes of attributes | DOC (TECH p15) | | 2 | 01 | 32 x 192 cells of 8 x 1, two colours each | 6K bitmap, a 2K gap, 6K attributes (attributes at offset 8192) | DOC (TECH p15, p29) | | 3 | 10 | 512 x 192, 2 bits per pixel, 4 colours per line from 16 (HMPR b5-b6 supply CLUT address bits 2-3) | 24K, linear | DOC (TECH p15, p20) | | 4 | 11 | 256 x 192, 4 bits per pixel, 16 colours | 24K, linear (128 bytes a line); high nibble is the left pixel | DOC (TECH p15, p20) | Mode 1 exists so Spectrum software runs; it also slows the Z80 (see contention). The attribute byte layout (ink, paper, bright, flash) follows the Spectrum's in mode 1 and 2 but indexes the CLUT; the pinned documents do not spell out how FLASH and BRIGHT map onto CLUT entries (U4). **The CLUT.** Sixteen 7-bit write-only registers at port `(n << 8) | F8` for n = 0..15 (TECH p19): bits G1 R1 B1 BRIGHT G0 R0 B0 from bit 6 down, so 128 colours; BRIGHT adds half intensity to all three guns. The ROM loads a default table (TECH p19). Writes to the CLUT and to the border in the border area take effect at the next display "sync" point: erratic-looking but stable in modes 1-2, the nearest 8-pixel block in modes 3-4 (UTM, "Hardware oddities", OBSERVED by its authors on hardware; the rule itself is not measured there). **Screen off (BORDER bit 7, SOFF).** Only in modes 3 and 4: the ASIC stops fetching, the RGB output goes black while sync continues, contention drops to the border rule, and the line and frame interrupts still occur but LPEN/HPEN stop updating (TECH p18, UTM). The change lands one pixel clock after the next sync point in the border, and 8-12 pixels later inside the display (UTM). **Reset.** The ASIC clears most of its registers on reset (VMPR = 0, so a mode 1 screen from page 0) but leaves the CLUT, and keeps refreshing and displaying while RESET is held (UTM, "Reset screens"). The ROM waits about 55 ms for the ASIC to become ready (the samrom README, about two pre-production ROMs that lack the delay; a statement, not measured). ## Memory and paging 512K internal (256K fitted, a socket for 256K more), as 32 pages of 16K; the Z80's space is four 16K sections A, B, C, D (TECH p13-14). | Port | Register | Bits | Evidence | | --- | --- | --- | --- | | FA (250) R/W | LMPR | b0-b4 page for section A (B gets page+1); b5 RAM0: RAM replaces ROM0 in section A; b6 ROM1: ROM1 replaces RAM in section D; b7 WPRAM: section A RAM write-protected | DOC (TECH p18) | | FB (251) R/W | HMPR | b0-b4 page for section C (D gets page+1); b5-b6 mode 3 CLUT address bits; b7 MCNTRL: sections C and D go to external memory (XMEM low) | DOC (TECH p17) | | FC (252) R/W | VMPR | b0-b4 display page; b5-b6 mode; b7 write: MIDI OUT bit, read: MIDI IN bit | DOC (TECH p17) | With LMPR = 0 at reset, section A is ROM0 and section B is RAM page 1 (TECH p13-14, p28). On a 256K machine page numbers wrap or mirror (U5; the unofficial manual has a section "Machines with less than 512K", not present in the pinned 41-page file). External RAM (the 1 Mb packs, up to 4 Mb) pages in through HMPR b7 and its own ports (UTM p1 lists them; out of scope for M1-M6). ## Contention The ASIC and the Z80 share the internal DRAM; the ASIC wins, and the Z80 waits. What the pinned sources say: | Rule | Evidence | | --- | --- | | Contended: internal RAM accesses and I/O to ASIC ports F8-FF. ROM and external RAM are never contended | DOC (SO-T); TECH p33: "RAM contention means that ROM routines usually run slightly faster", "block moves run about 8% faster in ROM" | | Over the border, an internal RAM access may start only once in every 4 T-states; over the 256 x 192 screen, once in every 8 | DOC (SO-T "Rules") | | ASIC port I/O: once in every 8 T-states, always; an access not aligned to the last 4 T-states of an 8 T-state cell waits 4 | DOC (SO-T); MEASURED (SO-M1, from the border write's position) | | Screen off (SOFF) makes the whole frame border for contention | DOC (SO-T; TECH p18 "removes memory contention during off period") | | Mode 1 adds forced delays in blocks of 8 cells, alternating every 8 cells across the full line width including the borders: columns 7-14, 23-30, 39-..., one cell ahead of the visible display because the ASIC prefetches | MEASURED (SO-M1: a border-bar test on a real SAM, repeated on several border lines; the model was then checked against a real SAM running timing tests) | | Mode 2 is slower than modes 3 and 4, less than mode 1 | OBSERVED by UTM's authors ("hard to test") | | Repeats of one instruction realign to the contention grid, so unrolled LDI/OUTI stay 16 T even into contended memory | DOC (SO-T) | What is **not** settled, and is the board's main research risk: - **U1** The exact T-state of each grid point relative to the frame interrupt, and whether the 4- and 8-T-state grids are phased to the cell boundaries as SO-M1's diagrams imply (the diagrams are images, not fetched). SO-T's per-instruction table comes from SimCoupé's model (its own words), so it is a tier-5 detector, not evidence. - **U2** Whether the ASIC sees only MREQ (so internal cycles are never delayed) or also stretches cycles with an address on the bus, as the Spectrum's ULA does. SO-T speaks only of "memory accesses" and "I/O accesses" (HYPOTHESIS: MREQ/IORQ only). - **U6** Mode 2's extra slow-down: present (UTM) but unmeasured. - Opcode fetches (M1, 4 T with refresh) and data accesses (3 T) start at different points in their machine cycles, so the same grid adds different waits (SO-T "Alignment"). The board needs every access's starting T-state within the instruction: that is z80-python's access timing table (cpu-and-reuse.md). ## Interrupts (IM 1) All interrupts arrive on /INT; the program reads STATUS (F9) to tell them apart, a bit low meaning "requesting" (TECH p21). | Source | Bit | When | Evidence | | --- | --- | --- | --- | | LINE | 0 | at the end of the scan line before the one written to the LINE register (F9 write), i.e. at the start of its right border; works for line 0; values 192-255 disable it | DOC (TECH p18) | | MOUSE | 1 | mouse interface | DOC (TECH p21) | | MIDI IN | 2 | a byte received (read it within about 320 us) | DOC (TECH p21) | | FRAME | 3 | once per frame, 50 Hz | DOC (TECH p21) | | MIDI OUT | 4 | the MIDI OUT register has finished sending | DOC (TECH p21) | "Approximate interrupt times are 20 us long" (TECH p21): about 120 T-states at 6 MHz. The FRAME interrupt's line (U3), the exact pulse length, and how a held /INT re-triggers after an early EI are unknown in the pinned documents (U7). ## Ports The ASIC decodes 8-bit ports E0-FF for the machine (TECH p17); the high byte selects keyboard rows and CLUT entries. | Port (low byte) | Read | Write | Evidence | | --- | --- | --- | --- | | E0-E7, F0-F7 | WD1772, drive 1 / drive 2 (fdc-wd1772.md) | same | DOC (TECH p12) | | F6, F8 printer | - | printer (external interface) | DOC (TECH p9: PRINTL decodes 246 and 248) | | F8 (248) | LPEN: horizontal scan position; bit 0 CLUT address bit, bit 1 MIDI OUT busy | CLUT, entry = high byte (0-15) | DOC (TECH p19, p22) | | 1F8 (504) | HPEN: vertical scan position | (CLUT entry 1) | DOC (TECH p22) | | F9 (249) | STATUS: interrupt bits 0-4, keyboard K6-K8 in bits 5-7 | LINE interrupt register | DOC (TECH p18, p21) | | FA, FB, FC | LMPR, HMPR, VMPR | same | DOC | | FD (253) | MIDI IN data | MIDI OUT data (31.25 kbaud) | DOC (TECH p18, p21) | | FE (254) | KEYBOARD: K1-K5, b5 SPEN (light pen / serial in), b6 EAR, b7 per TECH "status bit show if external memory is set" | BORDER: b0-b2, b5 border CLUT index; b3 MIC; b4 BEEP; b6 THROM (MIDI through); b7 SOFF | DOC (TECH p18, p21) | | FF (255) | ATTRIBUTES: the attribute byte being displayed (modes 1-2) or the third byte of every four (modes 3-4) | SAA1099 data | DOC (TECH p17, p21) | | 1FF (511) | - | SAA1099 register address | DOC (TECH p17) | Port FE bit 7 on read: TECH calls it SOFF and describes it as "external memory"; UTM says the screen-off state is read back there (C1). The keyboard is a 9 x 8 matrix: eight rows on A8-A15 and a ninth (RDMSEL) read with A8-A15 all high; K1-K5 on FE, K6-K8 on F9 (TECH p16). The mouse shares FE when A8-A15 are high (TECH p21; protocol not in the pinned documents, U8). Writes to the 16-bit port addresses matter: the CLUT entry comes from the high byte (an `OUT (C),r` with B = entry), and MAME's trace shows the ROM writing LMPR as `5FFA` and `1FFA`, i.e. with the value on the high byte as the Z80 puts A there for `OUT (n),A` (OBSERVED).