# Timing: one clock, three CPU speeds, and who owns the bus Every number here names its source and tier ([validation.md](validation.md)). "Measured" means read off a console by someone who shows how; "fitted" means an emulator or FPGA author chose it so that programs behave; "specified" means a GCC or Atari document says so. Where sources disagree the row points to a numbered contradiction in [undocumented-behavior.md](undocumented-behavior.md). Sources used most, with the short names used below: | Short | Source | Tier | | --- | --- | --- | | [1702B] | *GCC1702B "MARIA" Chip Acceptance Specification*, March 1984 (`reference/docs/Gcc1702b_maria_specs.pdf`, page numbers are the document's) | 2 | | [MT84] | *General Computer MARIA Technology*, October 1984 (`reference/docs/7800_Maria_Specs.pdf`) | 2 | | [SG] | *7800 Software Guide* as kept on the 7800 Development Wiki, Appendix 3 "DMA timing" and Appendix 4 "Frame timing", which state where a value was measured (`reference/8bitdev-wiki/software-guide.txt`); the 1984 scan is `reference/docs/7800_software.pdf` | 2, with tier-3 corrections | | [HF] | AtariAge thread 224025 "7800 hardware facts" (RevEng's test routines run on NTSC and PAL consoles) | 3, measurements described | | [MiS] | Atari7800_MiSTer `rtl/Maria/DMA.sv`, `rtl/6502/sally.sv` at `0dc8ad2` (MIT): the PLA of the gate-level diagrams transcribed, plus the author's notes on a logic capture | 4 | | [GLD] | *TIA-MARIA Gate-Level Logic Diagrams*, April 1984, 50 sheets (`reference/docs/TIA-Maria_Gate-Level_Logic_Diagrams.pdf`) | 2 (schematic) | | [HALT] | `../a5200-python/docs/sally-halt.md` (sibling repository): the sibling board's finding on SALLY's HALT and RDY, written for both boards | mixed, stated there | ## The units One oscillator. NTSC: 14.31818 MHz crystal ([1702B] pin list: "14.318080 MHz nominal"); PAL: 14.187576 MHz ([HF]). MARIA divides it by two for its own clock, here called the **MARIA clock** (7.159 MHz NTSC, 7.094 MHz PAL): one 320-mode pixel. [1702B] p. 29 calls the input "CLKIN (4 x 3.58 MHz): 70 nSEC period". | Unit | MARIA clocks | Notes | | --- | --- | --- | | 320-mode pixel | 1 | | | 160-mode pixel, TIA colour clock | 2 | MARIA supplies TIA's 3.58 MHz clock (TCLK, [MT84] 2.1) | | CPU cycle, fast | 4 | 1.79 MHz | | CPU cycle, slow | 6 | 1.19 MHz: TIA, RIOT, and everything in 2600 mode | | Line | 454 | = 113.5 fast CPU cycles = 227 colour clocks ([SG] App. 3, [HF]: "people have scoped that there are 454") | | Frame, NTSC | 263 lines = 119,402 | 29,850.5 fast CPU cycles ([HF], measured by a frame-long delay loop that held a mid-line colour change still) | | Frame, PAL | 313 lines = 142,102 | ([HF]: line counts) | **The board's time base is the MARIA clock.** A fast CPU cycle is 4 of them, a slow one 6, a line 454. Because 454 is not a multiple of 4, the CPU's cycle grid slides half a cycle against the line every line; nothing re-aligns it ([HF]: "there is no clock sync when DMA is turned off"). The TIA is not synchronised to MARIA's line either: its own horizontal counter divides the colour clock by 228, MARIA's line is 227 colour clocks. In 7800 mode the TIA's counter free-runs and only its audio (two ticks per TIA line, 31,399.5 Hz on NTSC, the figure MAME's `tia` chip reports) and its input ports matter. In 2600 mode the TIA makes the picture and MARIA's line counter is irrelevant. ## The slow cycle [MT84] 2.1: "logic to slow the clock when an address falls on the TIA or 6532 space, stretching the normal 1.79 MHz PCLK0 equivalent to an operating speed of 1.19 MHz". [1702B] p. 29: "Clock stretch timing for TIA and 6532 access: divider shifts to by-6 from by-4 for pclk2 and following pclk1 time. In TIA mode divider stays in by-6 mode." - The decision is made from the address of the cycle itself: **only the cycle that addresses the TIA or RIOT is long** ([HF]: "the clock slowdown only happens during the data-fetch part of the instruction"). - [HF] measured the cost as **+0.5 fast cycle per access to $00-$1F** in every addressing mode: 6 MARIA clocks instead of 4 is +2 = half a fast cycle. - The address ranges that are slow are the "slow" rows of [MT84] Figure 2: TIA ($00-$1F and its mirrors at $100, $200, $300) and 6532 ($280-$2FF, $380-$3FF, $480-$4FF, $580-$5FF). A dummy read of zero page by an indexed mode, or `NOP $00`, counts ([HF]: the sleep macro's `nop 0` did it). - Whether a write to INPTCTRL before the lock, or a MARIA register at $20-$3F, is slow: Figure 2 lists the slow rows by address with A5 = 0 for the TIA row, so $20-$3F is fast. Unknown: the exact stretch of the cycle *after* ("following pclk1 time"): see U3. - "In TIA mode divider stays in by-6 mode": in 2600 mode every cycle is 6 MARIA clocks = 3 colour clocks, the 2600's rate. The CPU clock still comes from MARIA, not from the TIA's Φ0 output as on a 2600: see [2600 mode](#2600-mode). ## The line and the frame [SG] Appendix 4 (MARIA clocks, both standards): ``` |<- 134 horizontal blank ->|<-------- 320 visible -------->| = 454 ``` | | NTSC | PAL | Source | | --- | --- | --- | --- | | Lines | 263 | 313 | [HF] | | VBLANK ends, DMA begins | raster 16 | raster 16 | [SG] MSTAT: "This transition occurs at raster 16" | | Lines with DMA | 243 | 293 | [SG] App. 4 (16 + 243 + 4 = 263; 16 + 293 + 4 = 313) | | "Visible" as the guide draws it | 25 + 192 + 26 | 25 + 242 + 26 | [SG] App. 4 | | Zone-list total a program must supply | 242 or 243 | 292 or 293 | C7: [MT84] 2.2 says 242 / 292, [SG] 243 / 293 | MSTAT ($28) D7 reads 1 during vertical blank. Which MARIA clock of which line it changes on is unknown (U4). MAME's screen is 454 x 263 with vertical blank start at line 251 and end at 27 of its own numbering, which is MAME's choice of origin, not a fact about the chip. ## DMA: when it starts and what it costs MARIA builds each line's line RAM by reading the display list while the previous line is displayed. All figures in MARIA clocks. | Event | Cost | Source and tier | | --- | --- | --- | | DMA request | at the leading edge of horizontal blank; the first DMA of a frame at the trailing edge of vertical blank | [1702B] p. 18 ("End-Of-VBlank DMA is initiated on the trailing edge of VBlank. Regular DMA is initiated on the leading edge of HBlank"); [MiS] DMA.sv | | CPU keeps running after the line starts | 28 (7 fast cycles) | [SG] App. 3 "DMA start"; MAME starts DMA 7 cycles into the line (`a7800.cpp`) | | Start-up + shutdown, a line that is not the last of its zone | 16 | **measured** [HF] (113.5 - 109.5 fast cycles with null display lists); [SG] App. 3 | | Start-up + shutdown, last line of a zone | 24 | **measured** [HF] (two fast cycles more); the extra 8 covers reading the next three-byte display-list-list entry (inference: 3 bytes at 2 each, plus 2; no source splits it) | | 4-byte header | 8 | [SG]; [MiS]: header and list bytes take 2 each | | 5-byte header | 10 | same | | Graphics byte, direct | 3 | [SG]; [1702B] p. 27-28 gives graphics accesses the longer access time (ROM allowed) | | Character-map byte + its graphics byte (indirect, CWIDTH = 0) | 6 | [SG]: 3 for the map read, 3 for the graphics | | Indirect, CWIDTH = 1 (two graphics bytes per map byte) | 9 | [SG] | | Object whose graphics address falls in a Holey-DMA hole | header, then 3 / 6 / 9 once (direct / indirect / indirect wide) and the object is abandoned | [SG] App. 3; **C3** (the same author measured "no penalty at all" in 2014 and 3 in a later test) | | End-of-list header (second byte & $5F == 0) | part of the 16 / 24 | [SG]; the mask is from the wiki's emulator notes, see C5 | | First DMA of a frame | 8 extra, once | **measured** [SG] App. 3 "Frame start" (counted per frame; which line pays is unknown, U5) | | DLI | 0 | **measured** [SG] App. 3 ("frames with a DLI on every zone use exactly as many CPU cycles as the same frames without"); MAME charges 17: **C4** | | DMA forced to stop | at the leading edge of the right border | [1702B] p. 18 ("DMA will be aborted (to shutdown) on the leading edge of Border"); MAME and A7800 cap the list at 426 = 454 - 28 clocks, then run no CPU until the next line | The earlier documents give ranges instead of single values, and they do not agree with each other or with the measurement (C2): GCC "start-up 5-12, shutdown 13-17 (other line) or 19-23 (last line)", Atari's guide "5-9, 4-7, 10-13". The measured totals 16 and 24 are what the board implements; the ranges are what one would see with the CPU's alignment included. ### Rounding to the CPU's cycle HALT can take the bus only between CPU cycles, and gives it back only between MARIA's own (DMA runs on the MARIA clock in 2- and 3-clock reads). So the CPU loses the DMA's length rounded up to its grid: - [SG] App. 3 "Lining up with the 6502 clock": "If the 6502 isn't at the end of a cycle when DMA starts, up to 3 more MARIA cycles are lost. If it is partway through a slow TIA or RIOT access, up to 5 are lost. A scanline is 113.5 CPU cycles long, so this usually happens every other line." - [MiS] sally.sv, from a 16 MHz logic capture of SYNC, BLANK, /HALT and PCLK2 on a console (the capture file itself is not published, U1): "/HALT is low for exactly 36 samples = 2.250 us = 4.03 CPU cycles on all three pulses in the capture; and every /HALT edge lands ~70 ns after phi2 falls". Four CPU cycles is the measured 16 above: on a null list **HALT is low for a whole number of CPU cycles and the CPU loses exactly that many**. What the board does: MARIA requests the bus at its fixed clock of the line; the request is honoured at the end of the CPU cycle in progress (4 or 6 clocks long); MARIA's reads then run on the MARIA clock; when the list ends, the CPU's next cycle starts at the next boundary of the CPU clock divider. Whether the divider keeps its phase across DMA or restarts on release is not stated by any source (U2); the two give the "up to 3" above either at entry or at exit. Milestone 2's judge (the frame-total and line-slope measurements of [HF]) cannot tell them apart; a published capture could. ## SALLY's HALT: the finding both boards cite The question (asked by this board for MARIA and by `a5200-python` for ANTIC): when does the 6502C stop, can it stop on a write cycle, and does `6502-python` need a change? [HALT] answers from the 400/800 hardware manual, the Altirra manual, ANTIC's die-derived schematic and a scope thread: **HALT stops the CPU's clock for whole cycles in front of any cycle, read or write; RDY only stretches reads; the two must not be merged; `6502-python` at `origin/main` (`741419a`) needs no change.** The 7800's own evidence agrees and adds this: | Point | 7800 evidence | Tier | | --- | --- | --- | | HALT is a separate pin from RDY | SALLY pin 35 from MARIA pin 40 with a 4.7 k pull-up; RDY is MARIA pin 41 (open drain) wire-ANDed with TIA's RDY ([1702B] pin list p. 7-8; console schematic `reference/schematics/Schematic_Atari7800_NTSC_4000.jpg` as traced in [MiS] sally.sv) | 2 | | When the bus changes hands | "The busses are available for display-list DMA operation after the bus cycle when HALT- is asserted. Microprocessor operation resumes the next bus cycle after HALT- is de-asserted." ([MT84] 2.1) | 2 | | HALT is asserted just after Φ2 falls | "every /HALT edge lands ~70 ns after phi2 falls" ([MiS], capture) | 1, reported in prose | | It stops writes too | [HALT]'s sources; and on this machine an FPGA that let writes run under HALT hung Galaga: "Galaga lost the second write of an `INC $67` into a bus MARIA already owned" ([MiS] sally.sv) | 4 (behaviour of a commercial program) | | The CPU is not at an instruction boundary | "DMA will halt the processor mid-instruction" (AtariAge 86087); [SG] App. 3: the DLI is taken after "the instruction it was running when DMA began" finishes | 3 | | Test DMA modes take the bus without HALT | CTRL DM = 0 or 1: "this unusual entry into DMA does not assert the HALT pin to the 6502, it simply starts taking over the address bus without asking, causing bus contention" ([1702B] p. 10; [MT84] p. 5) | 2 | | HALT is gated off at power-up | "When the 7800 is first powered up HALT is blocked from getting to the 6502, but after 2 writes to the control register (the data written doesn't make a difference), the HALT input will be enabled" ([SG] INPTCTRL) | 2 | One older statement contradicts "mid-instruction": [HF] says "we already knew that MARIA waits for the CPU to complete an instruction before it halts the CPU for DMA". It is recorded as C1 and rejected: MARIA has no SYNC input ([1702B] pin list p. 7-8: from the CPU it takes only PCLK2, R/W and the buses), so it cannot know where an instruction ends. A second, independent statement of the latency comes from cartridge hardware that works on consoles: the bankset board switches from SALLY's ROM to MARIA's on HALT, and "once the HALT signal goes low, Sally's bankset remains active until the second falling edge of PHI2, after which Maria's bankset is active until HALT goes high again" (wiki "Bankset bankswitching", by the scheme's designers; tier 3). So the CPU still owns two Φ2 periods after HALT falls. [MiS] also *fits* a two-cycle latency: MARIA's first read is two CPU cycles after HALT falls and the CPU's first cycle two after it rises, equal on both sides so the loss is exactly the time HALT was low. Its author says this is "fitted, not established" (the capture has no address lines). At the board's resolution only the loss and its position on the line matter; the 28-clock "DMA start" of [SG] already includes whatever latency there is. ### What 6502-python needs: nothing Checked at `origin/main` = `741419a33d1de400a63f7c24c7916bb600694ca2`, the revision `a2600-python` pins. (The local branch called `main` in the sibling `6502-python` checkout is older, `b5387f6`, and lacks the hooks; read `origin/main`.) - **HALT**: the core makes exactly one bus callback per cycle, dummy cycles included. The board runs MARIA's stolen clocks **at the top of both `read_byte` and `write_byte`**, before performing the CPU's access. The core's state does not move while a callback runs, which is the held clock. No `note_stall()`, no `dma_stalled`. - **WSYNC** is RDY (next section) and uses `CPUFrame.note_stall()` exactly as `famicom-python` does for its DMC DMA, because on this machine an NMI can arrive while the CPU waits. - **DLI NMI**: `request_non_maskable_interrupt()` from inside the callback of the CPU cycle in which MARIA pulses INT; the core's poll rules then apply (second-to-last cycle). - **IRQ** is wired to the cartridge and expansion connectors (POKEY and YM2151 on carts may use it: the A78 v4 header has an interrupt field); `request_maskable_interrupt()`. Open, and numbered in undocumented-behavior.md: whether SALLY's interrupt inputs are sampled while the clock is held (U6; it cannot matter for MARIA's own NMI, which is pulsed after release, but a cartridge IRQ can rise during DMA), and whether a held clock in front of an unstable store changes the value stored (U7). Both are shared with [HALT]. ## The DLI - [1702B] p. 14: "If is set, the NMI- pin will be asserted for one cpu cycle, one cycle after HALT- is released." - [SG]: the DLI bit of a display-list-list entry interrupts "following DMA on the last line of the PREVIOUS zone": the bit is in the entry MARIA reads at the end of the last line of the zone before, and the interrupt comes right after that read. - [SG] App. 3: the CPU finishes the instruction that DMA interrupted, then takes the interrupt (7 cycles); so the handler's start moves by up to one instruction, more when that instruction touches TIA or RIOT (measured: 7-cycle instructions jitter more than 3-cycle ones). - A DLI is edge-triggered NMI and cannot be masked. Programs that enable DMA before their lists exist take stray NMIs (kevtris, AtariAge 215357: Klax). With CTRL DM = 0 or 1 MARIA fetches one header from wherever its pointer is, and a set bit 7 raises NMI (same post, Beef Drop); an FPGA author suppressed NMI unless DM = 2, which is a fix for a program, not a statement about the chip (U8). ## WSYNC Writing $24 sets a latch that pulls RDY low until the line ends ([1702B] p. 11 register map; [MT84] 2.1 "WaitSYNC"). - RDY does not hold a write, so the STA completes and **the CPU stops on its next read**, the following opcode fetch ([HALT]; the wiki's "halts on the write cycle of the STA WSYNC" in [SG] App. 3 describes the same thing from the program's side). - Release: at the start of the next line's horizontal blank. A screen measurement on a console-matched emulator puts the first cycle after release about 5 pixels (160-mode) into the line (AtariAge 339785, mono); MAME 0.285 resumes the CPU within the first 5 MARIA clocks (our run: the first instruction after a WSYNC write starts at beamx 0-4, [mame-oracle.md](mame-oracle.md)). The exact MARIA clock is unknown (U9). - **Jitter**: unlike the 2600, the release is not on a fixed CPU phase: cycles are 4 or 6 clocks and the line is 113.5 cycles, so the CPU comes out up to one cycle late (AtariAge 164265, supercat). - A WSYNC written in the last cycles of a line waits for the end of the *next* line ([SG] App. 3 "WSYNC near the end of a line"). The deadline clock is unknown (U9); AtariAge 339785 places the right border at MARIA clock 413. - DMA goes on while the CPU waits (HALT and RDY are independent), and a DLI that falls in the wait is taken when the CPU is released: "STA WSYNC halts the 6502 so the DLI doesn't kick in as early" (AtariAge 164265, EricBall). This is the case `note_stall()` exists for. - In 2600 mode WSYNC is the TIA's ($02), released by the TIA's own horizontal counter; `a2600-python`'s model applies unchanged apart from the CPU phase below. ## The RIOT's clock The 6532 is clocked by the CPU clock MARIA makes. Two things follow, both from the wiki's "RIOT limitations and workarounds" (Saarna): - "When Maria halts the 6502 for DMA, it doesn't have a corresponding halt line for the RIOT chip, so RIOT continues to run while the 6502 is halted." So the interval timer counts clock pulses, not CPU cycles: during DMA it keeps counting at whatever rate MARIA's CPU-clock output has while HALT is low (assumed 4 MARIA clocks per pulse; not measured, U10). - Timer and RIOT-RAM accesses are unreliable under DMA on real consoles (a halt in mid-access removes address and R/W), and vary by console. The board models the reliable case and records this as a known non-goal. ### Φ2 periods at the cartridge port (the answer `pokey-python` asks for) `pokey-python`'s brief asks this board how many Φ2 periods a cartridge POKEY sees during a stretched access and during DMA; it calls `tick()` once per period, after the cycle's bus access. | Situation | Φ2 periods | Length of each | Source and standing | | --- | --- | --- | --- | | Fast CPU cycle | 1 | 4 MARIA clocks | [MT84], [1702B] | | Slow cycle (TIA, RIOT; every cycle in 2600 mode) | **1**, long | 6 MARIA clocks | [1702B] p. 29: the divider "shifts to by-6 from by-4": one period stretched, not two. Tier 2. Its high/low split is U3 | | CPU waiting on WSYNC (RDY) | 1 per repeated read | 4 | the CPU clock runs; RDY only repeats the read | | DMA (HALT low) | **1 per 4 MARIA clocks, for as long as HALT is low** | 4 | SALLY's Φ2 output "keeps running through a halt even though the core inside does not" ([MiS] `sally.sv`, whose capture recorded PCLK2 beside /HALT: HALT low for 36 samples = 4.03 periods); "RIOT continues to run while the 6502 is halted" (wiki). Tier 3 and 4, in agreement; no published trace, so U10 stays open for the phase of the first period after release (U2) | So a POKEY (and the RIOT timer) counts **time**, in periods that are 4 clocks long except where a slow access makes one 6 long. Over a line of 454 clocks with no slow access that is 113.5 periods; each TIA or RIOT access removes half a period. The board ticks cartridge devices once per period in every row above, DMA included, and performs a cycle's access before its tick. `a2600-python`'s `RIOT.read(address, cycle)` / `write(address, value, cycle)` take the count of clock pulses as an argument, so the 7800 board passes its own pulse count; no change to the chip model is needed for this. ## 2600 mode After the BIOS locks INPTCTRL with MARIA disabled, the machine is a 2600 made of the same TIA and RIOT ([MT84] 2.1: MARIA "is otherwise held in a passive reset state, with its bus and video outputs turned off", providing "chip select signals [and] address mapping identical to that of the Atari 2600"). - Every CPU cycle is 6 MARIA clocks = 3 colour clocks; a TIA line is 228 colour clocks = 76 cycles, as on a 2600. - **The CPU clock is MARIA's divide-by-6, not the TIA's Φ0.** On a 2600 the TIA reloads its divide-by-three at every line start, which pins the CPU's cycles to fixed colour clocks of the line (`a2600-python` enforces this as "power-on must fall on the grid: hclock = 2 mod 3"). On a 7800 the phase between MARIA's divider and the TIA's horizontal counter is whatever it was when the TIA's counter last reset; it then stays constant (228 is a multiple of 3). Which of the three colour-clock phases (six half-clock phases) a console lands on, and whether it is the same at every power-up, is unknown (U11). The #699 serial tests that `a2600-python` already matches for a PAL 7800 constrain it: those tables were recorded on a 7800 in 2600 mode. - Stella issue #246 records a 7800 whose TIA picture changes as it warms; `a2600-python` does not model it and neither does this board. ## What a cycle looks like to the board ``` read_byte(address) / write_byte(address, value): 1. while MARIA holds HALT: advance one MARIA clock (its DMA engine, the display, TIA at half that rate, the RIOT's pulse counter, cart audio) 2. (reads only) while RDY is low (WSYNC): cpu.note_stall(); advance one CPU-clock period; repeat step 1 inside it 3. length = 6 if the address is in a slow row or the machine is in 2600 mode, else 4; advance length - 1 clocks, perform the access (a TIA write lands on the clock where Φ2 falls), advance the last clock 4. hand the cycle to the cartridge (address, data, R/W, HALT) and to any bus observer ``` `step()`'s returned cycle count is never used for time. The trace records the MARIA clock of every access. ## Power-on and reset Undefined on hardware and chosen to match the detectors, each choice labelled (as `a2600-python` does): CPU registers, RAM contents, the phase of the CPU divider against the line, the TIA's horizontal counter, the RIOT timer. MAME's first instruction (our trace: PC $F884 on the NTSC BIOS, A = 0, X = $80, Y = 0, P = $36, S = $1FD, beam line 251) is the alignment target for milestone 1. INPTCTRL powers up 0: BIOS mapped, MARIA and system RAM disabled, TIA and RIOT reachable, HALT blocked ([SG]). The reset button is the CPU's RESET line; INPTCTRL is not cleared by it once locked ("The only way to clear the lock is to power cycle the console", [SG]).