# SPDX-License-Identifier: MIT """The memory schedule of a line: which of the 171 memory cycles does what. Derived on 2026-11-19 (provenance wave 2) from TI's own documents and hardware results alone, with no other emulator's tables or source in view: - **[1978]** TI, "9918 Address Select / RAM Data Load Timing", hand-drawn, 1978 (Guttag archive, ``reference/docs/guttag-vdp-99xx/1978_9918_Master_Timing_by_Serigio_Maggi.jpg``, tier 2, the plain 9918). Rows are named by their label and the signal name in the drawing's right-hand column (``S1YY`` and so on). Pulse positions were read against the HCNT labels at full resolution; the grid is one square per memory cycle (two counts). - **[TI]** the Nov 1982 data manual, section 2.1.5 and table 3-3 (tier 2). - **[VATT]** VATT's hardware-run results (msxgl sheet, 21 TI-part entries; tier 3). Evidence tags: DOC (read from a TI document), MEASURED (hardware-run results). Coordinates (as ``tables``): the drawing's HCNT runs 0-295 then 466-511 (342 states); dot = HCNT for 0-295 and HCNT - 170 for 466-511; slot s is the memory cycle that starts at dot 2s. A line is 171 slots [TI table 3-3: 342 pixel clocks; TI 2.1.5: a memory cycle is about 372 ns, two pixel clocks]. """ from __future__ import annotations __all__ = [ "CPU", "REFRESH", "NAME", "COLOUR", "PATTERN", "SPRITE_Y", "SPRITE_V", "SPRITE_H", "SPRITE_N", "SPRITE_T", "SPRITE_P1", "SPRITE_P2", "TEXT_NAME", "TEXT_PATTERN", "KIND_NAMES", "GRAPHICS_LINE", "MULTICOLOR_LINE", "MULTICOLOR_ONE_IN_FOUR", "TEXT_LINE", "REFRESH_LINE", "EARLY_Y_SLOTS", "HBLANK_SPRITE_SLOTS", ] # fmt: skip # Slot kinds. Each slot of a line is (kind, argument). CPU = 0 # a CPU access window [1978 "CPU", HCPB] REFRESH = 1 # a DRAM refresh cycle [1978 "REFRESH ADDRESS SELECT", MMYY] NAME = 2 # pattern name fetch, argument = column 0-31 COLOUR = 3 # colour table fetch, argument = column PATTERN = 4 # pattern generator fetch, argument = column SPRITE_Y = 5 # sprite pre-processing: vertical attribute of sprite `argument` SPRITE_V = 6 # sprite processing for found sprite `argument` 0-3: vertical SPRITE_H = 7 # horizontal SPRITE_N = 8 # name SPRITE_T = 9 # tag (early clock and colour) SPRITE_P1 = 10 # first pattern byte SPRITE_P2 = 11 # second pattern byte (right half of a 16 x 16 sprite) TEXT_NAME = 12 # Text mode name fetch, argument = column 0-39 TEXT_PATTERN = 13 # Text mode pattern fetch, argument = column KIND_NAMES = ( "CPU", "REFRESH", "NAME", "COLOUR", "PATTERN", "SPRITE_Y", "SPRITE_V", "SPRITE_H", "SPRITE_N", "SPRITE_T", "SPRITE_P1", "SPRITE_P2", "TEXT_NAME", "TEXT_PATTERN", ) # fmt: skip _SLOTS = 171 # [TI table 3-3] 342 pixel clocks / 2 per memory cycle def _slot(hcnt: int) -> int: """The slot of a drawing HCNT value (0-295 or 466-511).""" dot = hcnt if hcnt < 296 else hcnt - 170 return dot // 2 # --- the pattern modes: [1978] "PATTERN PLANE ... REPEAT 8 TIMES" ------------------- # # Left sheet, HCNT 0-31, repeated 8 times (DOC): # NAME ADDRESS SELECT (S1YY) pulses at 0, 8, 16, 24 -> +0 of each 8 # COLOR ADDRESS SELECT (T2YY) pulses at 4, 12, 20, 28 -> +4 # PATTERN ADDRESS SELECT (VVYY) pulses at 6, 14, 22, 30 -> +6 # CPU (HCPB) one pulse, at 2 -> +2 of the first 8 # VERT ATT. ADDRESS SELECT (CCYY) pulses at 10, 18, 26 -> +2 of the other three # So in each 32 counts the second memory cycle of every 8 is a CPU window once and a # sprite vertical check three times: 8 windows and 24 checks in the 256 active counts. # [TI 2.1.5] agrees: "During the active display, CPU windows occur once every 16 memory # cycles" (DOC). The 24 checks take sprites 8-31 in order; sprites 0-7 are checked at the # end of the line before (AAYY, below). The row's names (the "8 TIMES" repeat, the order # V..T per sprite) say which sprite each check reads; the order 8, 9, ... is the plain # reading of a sequential attribute scan [TI 2.3.2: sprites are processed in order of # their number] (DOC). #: Right sheet, HCNT 256-511 (DOC). Sprite processing rows, each pulse labelled in the #: drawing (V, H, N, T; P1, P2): #: SP0 (DDYY) V 260, H 262; SP0 (EEYY) N 264, T 266; SP0-3 pattern RAM load (NNYY) P1 268, #: P2 270; SP1 (FFYY) V 272, H 274, N 276, T 278; (PPYY) P1 280, P2 282; #: SP2 (GGYY) V 284, H 286; SP2 (HHYY) N 468, T 470; (QQYY) P1 472, P2 474; #: SP3 (LLYY) V 476, H 478; SP3 (XXYY) N 480, T 482; (RRYY) P1 484, P2 486. #: Every pulse falls on an even count (a memory-cycle boundary) to the reading accuracy. HBLANK_SPRITE_HCNT: tuple[tuple[int, int, int], ...] = ( (260, SPRITE_V, 0), (262, SPRITE_H, 0), (264, SPRITE_N, 0), (266, SPRITE_T, 0), (268, SPRITE_P1, 0), (270, SPRITE_P2, 0), (272, SPRITE_V, 1), (274, SPRITE_H, 1), (276, SPRITE_N, 1), (278, SPRITE_T, 1), (280, SPRITE_P1, 1), (282, SPRITE_P2, 1), (284, SPRITE_V, 2), (286, SPRITE_H, 2), (468, SPRITE_N, 2), (470, SPRITE_T, 2), (472, SPRITE_P1, 2), (474, SPRITE_P2, 2), (476, SPRITE_V, 3), (478, SPRITE_H, 3), (480, SPRITE_N, 3), (482, SPRITE_T, 3), (484, SPRITE_P1, 3), (486, SPRITE_P2, 3), ) # fmt: skip #: The same, as {slot: (kind, sprite)}. HBLANK_SPRITE_SLOTS: dict[int, tuple[int, int]] = { _slot(h): (kind, k) for h, kind, k in HBLANK_SPRITE_HCNT } #: Right sheet CPU row (HCPB): pulses at 256, 258; 288, 290, 292, 294, 466 (five, the #: last just after the 295/466 jump); 488, 490, 492, 494. Eleven windows (DOC). HBLANK_CPU_HCNT = (256, 258, 288, 290, 292, 294, 466, 488, 490, 492, 494) #: "SPRITE PRE-PROCESSING / VERT ATT. ADDRESS SELECT" (AAYY): eight pulses, one per #: memory cycle, at HCNT 496-510 (sprites 0-7; three earlier pulses at 490-494 are drawn #: faintly and rubbed out, so not taken). Slots 163-170 (DOC). Which line they serve is #: the core's reading (docs/schedule.md): the evaluation two lines on. EARLY_Y_SLOTS = range(_slot(496), _slot(510) + 1) _HBLANK_FIRST = _slot(256) # slot 128: the first memory cycle after the 32 groups def _pattern_mode_line(colour_fetch: bool) -> tuple[tuple[int, int], ...]: """A pattern-mode line from [1978] "PATTERN PLANE" and the right sheet. ``colour_fetch=False`` turns the 32 colour slots into CPU windows: TI 2.1.5's Multicolor "at least once out of every four memory cycles" read literally, which VATT contradicts (see ``MULTICOLOR_LINE``). """ slots: list[tuple[int, int]] = [] vertical = 8 # the active-area checks start at sprite 8 (AAYY took 0-7) for g in range(32): slots.append((NAME, g)) # S1YY at count 8g second_is_cpu = g % 4 == 0 # HCPB at count 2 of each 32 (one in four groups) if second_is_cpu: slots.append((CPU, 0)) else: slots.append((SPRITE_Y, vertical)) # CCYY at counts 10, 18, 26 of each 32 vertical += 1 slots.append((COLOUR, g) if colour_fetch else (CPU, 0)) # T2YY at 8g + 4 slots.append((PATTERN, g)) # VVYY at 8g + 6 # 32 groups x 4 slots fill the active area; that sprites 8-31 are all checked is a # rung-0 test, not an assertion here, so a seeded fault still imports. assert len(slots) == _HBLANK_FIRST cpu_slots = {_slot(h) for h in HBLANK_CPU_HCNT} for s in range(_HBLANK_FIRST, _SLOTS): if s in HBLANK_SPRITE_SLOTS: slots.append(HBLANK_SPRITE_SLOTS[s]) elif s in EARLY_Y_SLOTS: slots.append((SPRITE_Y, s - EARLY_Y_SLOTS.start)) elif s in cpu_slots: slots.append((CPU, 0)) else: raise AssertionError(f"slot {s} has no reading in the drawing") return tuple(slots) def _with_early_checks( line: tuple[tuple[int, int], ...], checks: bool ) -> tuple[tuple[int, int], ...]: """``line`` with slots 163-170 as the AAYY vertical checks (``checks``) or as CPU windows. The core's reading (docs/schedule.md): the eight early checks follow the *next* line, running only when it is a sprite line. Provisional.""" out = list(line) for i, s in enumerate(EARLY_Y_SLOTS): out[s] = (SPRITE_Y, i) if checks else (CPU, 0) return tuple(out) #: Graphics I and II (DOC: [1978] as read above). 19 CPU windows: 8 in the active area, #: 11 in the horizontal blank. GRAPHICS_LINE = _pattern_mode_line(colour_fetch=True) #: Multicolor (C9). The drawing has no Multicolor row. VATT (MEASURED, 21 TI-part #: entries) lands the same fraction of fast writes in Multicolor as in Graphics I and II #: at every period from 12 to 31 T, so Multicolor has the Graphics windows: the colour #: slot is still taken. Over TI table 2-2 / 2.1.5's "one in four" (DOC), kept aside below. MULTICOLOR_LINE = _pattern_mode_line(colour_fetch=False) #: TI 2.1.5's Multicolor reading, kept for comparison only (51 windows a line). MULTICOLOR_ONE_IN_FOUR = _pattern_mode_line(colour_fetch=False) # --- Text: [1978] "TEXT MODE ... REPEAT 8 TIMES" ------------------------------------- # # The text rows count characters on their own sub-counter (labels 0, 2, 4, 5, 8, 10, 12, # 13, 16: six states per character, CHAR 0 ... CHAR 39), one character = three memory # cycles (DOC): # NAME ADDRESS SELECT (S7YY) at +0 of each character # CPU (HCPB) at +1 memory cycle # PATTERN ADDRESS SELECT (T8YY) at +2 memory cycles # No sprite or refresh row is drawn for Text, so every other memory cycle of the line is # a CPU window, and [TI 2.1.5] says "at least once out of every three memory cycles" # (DOC). # # Where character 0 starts: the sub-counter labels at the end of the row (325, 328, 329, # 338; 350, 351 near HCNT 262-264 on the right sheet) do not tie the sub-counter to HCNT # unambiguously (the labels themselves do not agree with 40 characters at 8 counts each). # So the start is placed from [TI table 3-3]: Text's left border is 19 pixel clocks # against 13, its picture 6 pixel clocks later than the pattern modes'; with the same # fetch-to-picture delay as the pattern modes (name fetch at dot 0, [1978] ACTIVE SCREEN # rising at 10), character 0's name fetch is at dot 6, slot 3. DOC (table 3-3) plus the # assumption of an equal pipeline delay: PROVISIONAL to +-1 slot. TEXT_FIRST_SLOT = 3 def _text_line() -> tuple[tuple[int, int], ...]: slots: list[tuple[int, int]] = [(CPU, 0)] * _SLOTS for k in range(40): s = TEXT_FIRST_SLOT + 3 * k slots[s] = (TEXT_NAME, k) slots[s + 2] = (TEXT_PATTERN, k) return tuple(slots) #: Text (and its Graphics II variant and the table-less modes): 40 name + 40 pattern #: fetches, 91 CPU windows. TEXT_LINE = _text_line() # --- refresh mode: [1978] "REFRESH MODE" ---------------------------------------------- # # REFRESH ADDRESS SELECT (MMYY) and CPU alternate, refresh first at HCNT 0, one memory # cycle each: four pairs drawn, "REPEAT 56 TIMES", four pairs drawn (64 pairs, 128 # cycles); then a CPU-only run: five drawn, "REPEAT 34 TIMES", four drawn (43 cycles). # 128 + 43 = 171 (DOC). Used on the lines outside the display and while BLANK = 0 # [TI 2.1.5: "the VDP does not have to wait for a CPU access window"]. _REFRESH_PAIRS = 4 + 56 + 4 _REFRESH_TAIL = 5 + 34 + 4 assert 2 * _REFRESH_PAIRS + _REFRESH_TAIL == _SLOTS #: 64 refreshes (even slots 0-126) and 107 CPU windows. REFRESH_LINE: tuple[tuple[int, int], ...] = tuple( [(REFRESH, 0), (CPU, 0)] * _REFRESH_PAIRS + [(CPU, 0)] * _REFRESH_TAIL )