# SPDX-License-Identifier: MIT """The manual's tables as data (rung 0). Every table cites the page or drawing it was transcribed from. Sources as in ``docs/chip.md``: **[TI]** the Nov 1982 data manual (tier 2); **[Young]** ``tms9918a.txt`` (tier 3); **[1978]** TI's hand-drawn "9918 Address Select / RAM Data Load Timing" (Guttag archive, tier 2, the plain 9918), read by eye for this core (``docs/schedule.md``). Coordinates used everywhere in the core: - **master clock**: XTAL, 10.738635 MHz [TI 3.6]. Pixel clock = XTAL/2; a memory cycle ("about 372 nanoseconds" [TI 2.1.5]) = 4 master clocks; a line = 342 pixel clocks [TI table 3-3] = 684 master clocks = 171 memory cycles. - **dot**: 0-341, pixel clocks along a line, counted from the 1978 drawing's horizontal count 0 (the first name fetch). The drawing's counter runs 0-295 then 466-511; dot = count for 0-295 and count - 170 for 466-511. - **slot**: 0-170, the memory cycle that starts at dot 2 x slot. - **line**: 0 is the first active line; 0-191 are active [TI 2.3.3, table 3-3]. LSB-first bit numbering throughout ("R1 bit 6" is ``0x40``); TI numbers bits MSB first. """ from __future__ import annotations # --- clocks ------------------------------------------------------------------------- MASTER_CLOCK_HZ = 10_738_635 # [TI 3.6]: 10.738635 MHz MASTER_PER_PIXEL = 2 # pixel clock = XTAL/2 [TI table 3-3, 3.6] MASTER_PER_SLOT = 4 # a memory cycle, ~372 ns [TI 2.1.5] DOTS_PER_LINE = 342 # [TI table 3-3] SLOTS_PER_LINE = DOTS_PER_LINE // 2 # 171 MASTER_PER_LINE = DOTS_PER_LINE * MASTER_PER_PIXEL # 684 # --- horizontal layout -------------------------------------------------------------- #: [TI table 3-3], pixel clocks, in order from the first active pixel. HORIZONTAL_PATTERN = ( ("active", 256), ("right border", 15), ("right blanking", 8), ("sync", 26), ("left blanking", 2), ("burst", 14), ("left blanking", 8), ("left border", 13), ) # fmt: skip #: The same for Text mode: 240 active, borders 25 and 19. HORIZONTAL_TEXT = ( ("active", 240), ("right border", 25), ("right blanking", 8), ("sync", 26), ("left blanking", 2), ("burst", 14), ("left blanking", 8), ("left border", 19), ) # fmt: skip #: The dot at which the first active pixel of the 256-pixel modes is shown. [1978]'s #: "ACTIVE SCREEN" row rises at count 10 and falls at 266, one 8-pixel group after the #: first name fetch at 0 plus two pixels. Read by eye from the drawing (+-1): provisional. DISPLAY_START_DOT = 10 #: Where Text mode's 240 pixels sit in the 256-pixel frame: left border 19 against 13 #: [TI table 3-3], so 6 pixels in, leaving 10 on the right. TEXT_LEFT = 19 - 13 # --- registers ---------------------------------------------------------------------- #: The bits TI defines in each write-only register [TI 2.2, figure 2-3]; other bits read #: as zero inside the chip. R0: M3 (``0x02``), EXTVID (``0x01``). R1: 4/16K, BLANK, IE, #: M1, M2, (bit 2 reserved), SIZE, MAG. R2: name table / 0x400 (4 bits). R3: colour #: table / 0x40. R4: pattern generator / 0x800 (3 bits). R5: sprite attributes / 0x80 #: (7 bits). R6: sprite patterns / 0x800 (3 bits). R7: text colour / backdrop. REGISTER_MASKS = (0x03, 0xFB, 0x0F, 0xFF, 0x07, 0x7F, 0x07, 0xFF) # R0 and R1 bits (TI's names; Young swaps M2 and M3, C2). R0_M3 = 0x02 # Graphics II R0_EXTVID = 0x01 R1_16K = 0x80 R1_BLANK = 0x40 # 0 blanks the display R1_IE = 0x20 R1_M1 = 0x10 # Text R1_M2 = 0x08 # Multicolor R1_SIZE = 0x02 R1_MAG = 0x01 # Status register bits [TI 2.3, figure 2-3]. STATUS_F = 0x80 STATUS_5S = 0x40 STATUS_C = 0x20 STATUS_NUMBER = 0x1F # --- display modes ------------------------------------------------------------------ # Mode numbers: M1 + 2*M2 + 4*M3 (TI's names). [TI 2.4, table 3-2; Young 3.5-3.7] GRAPHICS_I = 0 TEXT = 1 MULTICOLOR = 2 BOGUS_M1_M2 = 3 # Young "1+3" / "1+2+3" in his names: 40 bars, no tables GRAPHICS_II = 4 TEXT_GRAPHICS_II = 5 # undocumented, Young 3.5 ("1+2" in his names) MULTICOLOR_GRAPHICS_II = 6 # undocumented, Young 3.6 ("2+3") BOGUS_ALL = 7 MODE_NAMES = ( "Graphics I", "Text", "Multicolor", "M1+M2 (no tables)", "Graphics II", "Text + Graphics II", "Multicolor + Graphics II", "M1+M2+M3 (no tables)", ) # fmt: skip def mode_of(r0: int, r1: int, graphics2: bool = True) -> int: """The mode number from R0 and R1. M3 is ignored on a part without Graphics II.""" m3 = (r0 & R0_M3) and graphics2 return (1 if r1 & R1_M1 else 0) | (2 if r1 & R1_M2 else 0) | (4 if m3 else 0) def is_text_like(mode: int) -> bool: """Text, its Graphics II variant and the two table-less modes: 40 columns, no sprites.""" return bool(mode & 1) # --- the memory schedule ([1978], docs/schedule.md) --------------------------------- # The slot kinds (CPU, REFRESH, NAME, ...), KIND_NAMES and the line schedules # (GRAPHICS_LINE, MULTICOLOR_LINE, MULTICOLOR_ONE_IN_FOUR, TEXT_LINE, REFRESH_LINE, # EARLY_Y_SLOTS, _pattern_mode_line, _with_early_checks) live in ._schedule, derived from # TI's 1978 drawing alone (provenance wave 2, 2026-11-19). from ._schedule import ( # noqa: E402,F401 COLOUR, CPU, EARLY_Y_SLOTS, GRAPHICS_LINE, KIND_NAMES, MULTICOLOR_LINE, MULTICOLOR_ONE_IN_FOUR, NAME, PATTERN, REFRESH, REFRESH_LINE, SPRITE_H, SPRITE_N, SPRITE_P1, SPRITE_P2, SPRITE_T, SPRITE_V, SPRITE_Y, TEXT_LINE, TEXT_NAME, TEXT_PATTERN, _pattern_mode_line, _with_early_checks, ) assert SLOTS_PER_LINE == len(GRAPHICS_LINE) #: The schedules a line can use: (line kind, are the early checks for the next line on). SCHEDULES: dict[tuple[str, bool], tuple[tuple[int, int], ...]] = { ("graphics", True): GRAPHICS_LINE, ("graphics", False): _with_early_checks(GRAPHICS_LINE, False), ("text", False): TEXT_LINE, ("text", True): TEXT_LINE, # never asked for: the line after a text line has no sprites ("refresh", True): _with_early_checks(REFRESH_LINE, True), ("refresh", False): REFRESH_LINE, } def line_kind(mode: int) -> str: """Which schedule a display line in this mode uses (Multicolor: Graphics', C9).""" return "text" if is_text_like(mode) else "graphics" def cpu_slots(schedule: tuple[tuple[int, int], ...]) -> list[int]: """The slot numbers of the CPU windows in a line's schedule.""" return [s for s, (kind, _) in enumerate(schedule) if kind == CPU] # --- VRAM addressing ---------------------------------------------------------------- def dram_address(va: int, mode_16k: bool, vram_size: int) -> int: """The DRAM cell (as an index into ``vram_size`` bytes) that VDP address ``va`` hits. 16K fitted (4116s): in 16K mode the DRAM sees ``va`` unchanged. In 4K mode the row and column strobes carry different bits (Charles MacDonald's table, measured on a TMS9929A, quoted in [Young 2.1], tier 1): column AD0-AD5 = VA0-VA5, AD6 = VA12; row AD0-AD5 = VA6-VA11, AD6 = VA13. Read as a 16K-mode address, the cell is column bits 0-6 and row bits 7-13. 4K fitted (4027s): AD6 is not wired [TI table 3-1, "DATA ONLY"], so the cell is the six column bits and the six row bits (U5). In 4K mode that is ``va & 0xFFF``. 8K fitted (4108s): TI says 4108s wire like 4116s [TI 3.1.2] but not which half of the array a part uses. PROVISIONAL (U5): the 16K wiring with the top row bit dropped. """ if mode_16k: column, row = va & 0x7F, (va >> 7) & 0x7F else: column = (va & 0x3F) | ((va >> 12) & 1) << 6 row = ((va >> 6) & 0x3F) | ((va >> 13) & 1) << 6 if vram_size == 16384: return row << 7 | column if vram_size == 4096: return (row & 0x3F) << 6 | (column & 0x3F) if vram_size == 8192: return (row & 0x3F) << 7 | column raise ValueError(f"vram_size must be 4096, 8192 or 16384, not {vram_size}") def increment_address(addr: int, mode_16k: bool, roll_over_4k: bool) -> int: """The address register after an access: + 1, wrapping at $3FFF [Young 2.1]. In 4K mode the counter "will 'roll over' the LS bits at 4K" (Guttag, TI message of 1982-06-29, tier 2; C11), so bits 12-13 stay put. ``roll_over_4k`` turns that off. """ if False: return (addr & 0x3000) | ((addr + 1) & 0x0FFF) return (addr + 1) & 0x3FFF # Address derivations [TI table 3-2; Young 3.3-3.6]. `line` is the active line 0-191. def name_address(r2: int, line: int, column: int) -> int: """Pattern modes: name table base (R2 x $400) + row x 32 + column.""" return (r2 & 0x0F) << 10 | (line >> 3) << 5 | column def text_name_address(r2: int, line: int, column: int) -> int: """Text modes: name table base + row x 40 + column [TI table 3-2, "TEXT POSITION"].""" return ((r2 & 0x0F) << 10) + (line >> 3) * 40 + column def graphics1_colour_address(r3: int, name: int) -> int: """Graphics I: colour base (R3 x $40) + the five MSBs of the name [TI table 3-2].""" return r3 << 6 | name >> 3 def graphics1_pattern_address(r4: int, name: int, line: int) -> int: """Graphics I, Text: pattern base (R4 x $800) + name x 8 + line within the row.""" return (r4 & 0x07) << 11 | name << 3 | (line & 7) def multicolor_pattern_address(r4: int, name: int, line: int) -> int: """Multicolor: pattern base + name x 8 + (row & 3) x 2 + (line / 4 & 1) [TI table 3-2; chip.md], which is name x 8 + (line >> 2 & 7).""" return (r4 & 0x07) << 11 | name << 3 | ((line >> 2) & 7) def graphics2_masks(r3: int, r4: int, colour_mask_masks_patterns: bool) -> tuple[int, int]: """The two 10-bit masks over the character number (third x 256 + name) in Graphics II. [Young 3.3]: R4 bits 1-0 AND the character number's bits 9-8; R3 bits 6-0 AND its bits 9-3 for the colour table. C3: on the 99x8 R3's mask also reaches the pattern index. TI's 1982 memo says colour tables "of less than 2K bytes" disturb pattern addressing, which is R3 bits 4-0 (a table under 2K); MAME applies the colour mask's low eight bits only (``m_colourmask & 0xff``), which is the same set. So the pattern mask takes R3 bits 4-0 over character bits 7-3. """ colour_mask = (r3 & 0x7F) << 3 | 0x07 pattern_mask = (r4 & 0x03) << 8 | 0xFF if colour_mask_masks_patterns: pattern_mask &= colour_mask | 0x300 return colour_mask, pattern_mask def graphics2_colour_address(r3: int, r4: int, char: int, line: int, gate: bool) -> int: """Graphics II colour byte: R3 bit 7 is address bit 13 [TI table 3-2].""" colour_mask, _ = graphics2_masks(r3, r4, gate) return (r3 & 0x80) << 6 | (char & colour_mask) << 3 | (line & 7) def graphics2_pattern_address(r3: int, r4: int, char: int, row_byte: int, gate: bool) -> int: """Graphics II (and its Text and Multicolor variants) pattern byte: R4 bit 2 is address bit 13 [TI table 3-2]; ``row_byte`` is line & 7 (or line >> 2 & 7 for the Multicolor variant).""" _, pattern_mask = graphics2_masks(r3, r4, gate) return (r4 & 0x04) << 11 | (char & pattern_mask) << 3 | row_byte def sprite_attribute_address(r5: int, sprite: int, field: int) -> int: """[TI table 3-2, "SPRITE ADDRESS LOCATION"]: base (R5 x $80) + sprite x 4 + field (0 vertical, 1 horizontal, 2 name, 3 tag).""" return (r5 & 0x7F) << 7 | sprite << 2 | field def sprite_pattern_address(r6: int, name: int, size16: bool, row: int) -> int: """[TI table 3-2]: base (R6 x $800) + name x 8 + row (8 x 8), or the six MSBs of the name x 32 + row 0-15 (16 x 16; the right half is 16 bytes on, added by the caller).""" if size16: return (r6 & 0x07) << 11 | (name & 0xFC) << 3 | (row & 0x0F) return (r6 & 0x07) << 11 | name << 3 | (row & 0x07)