"""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) --------------------------------- # Slot kinds. Each slot of a line is (kind, argument). CPU = 0 # a CPU access window REFRESH = 1 # a DRAM refresh cycle ("refresh mode") 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 #: The horizontal-blank part of a pattern-mode line, dots 256-340, as read from [1978] #: (rows DDYY-XXYY for V H N T, NNYY-RRYY for P1 P2, AAYY for the first eight vertical #: checks, HCPB for the CPU). Drawing counts 466-510 are dots 296-340. _HBLANK = { 256: (CPU, 0), 258: (CPU, 0), 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), 288: (CPU, 0), 290: (CPU, 0), 292: (CPU, 0), 294: (CPU, 0), 296: (CPU, 0), # 288-294, count 466 298: (SPRITE_N, 2), 300: (SPRITE_T, 2), 302: (SPRITE_P1, 2), 304: (SPRITE_P2, 2), 306: (SPRITE_V, 3), 308: (SPRITE_H, 3), 310: (SPRITE_N, 3), 312: (SPRITE_T, 3), 314: (SPRITE_P1, 3), 316: (SPRITE_P2, 3), 318: (CPU, 0), 320: (CPU, 0), 322: (CPU, 0), 324: (CPU, 0), # counts 488-494 **{326 + 2 * i: (SPRITE_Y, i) for i in range(8)}, # counts 496-510: sprites 0-7 } # fmt: skip #: Slots 163-170 (dots 326-340): the first eight vertical checks, which belong to the #: evaluation for two lines on (they precede the next line's 24 checks). EARLY_Y_SLOTS = range(163, 171) def _pattern_mode_line(colour_fetch: bool) -> tuple[tuple[int, int], ...]: """The pattern-mode line; ``colour_fetch=False`` gives colour slots to the CPU. [1978] "PATTERN PLANE": in each 8-pixel group g at dot 8g, name at +0, colour at +4, pattern at +6, and at +2 either the CPU (groups 0, 4, 8, ...: "CPU" row at count 2, repeat 8 times of 32 counts) or a sprite vertical check ("VERT ATT. ADDRESS SELECT" at counts 10, 18, 26 of each 32). The 24 checks in the active area are sprites 8-31. The drawing has no Multicolor row; see ``MULTICOLOR_LINE`` (docs/schedule.md). """ slots: list[tuple[int, int]] = [] sprite = 8 for g in range(32): slots.append((NAME, g)) if g % 4 == 0: slots.append((CPU, 0)) else: slots.append((SPRITE_Y, sprite)) sprite += 1 slots.append((COLOUR, g) if colour_fetch else (CPU, 0)) slots.append((PATTERN, g)) for dot in range(256, 342, 2): slots.append(_HBLANK[dot]) return tuple(slots) def _text_line() -> tuple[tuple[int, int], ...]: """Text mode [1978] "TEXT MODE": per 6-pixel character, name at +0, CPU at +2, pattern at +4, 40 characters from dot 6 (the text area is 6 pixels to the right of the pattern modes' [TI table 3-3]); every other slot of the line is a CPU window (no sprites, no refresh on display lines).""" slots: list[tuple[int, int]] = [(CPU, 0)] * 3 # dots 0, 2, 4 for k in range(40): slots += [(TEXT_NAME, k), (CPU, 0), (TEXT_PATTERN, k)] while len(slots) < SLOTS_PER_LINE: slots.append((CPU, 0)) return tuple(slots) def _refresh_line() -> tuple[tuple[int, int], ...]: """[1978] "REFRESH MODE": refresh and CPU alternate through the first 128 slots (dots 0-255), then CPU windows only through the horizontal blank. Used on lines outside the active display and on every line while BLANK = 0 [TI 2.1.5].""" slots: list[tuple[int, int]] = [] for s in range(SLOTS_PER_LINE): slots.append((REFRESH, 0) if s < 128 and s % 2 == 0 else (CPU, 0)) return tuple(slots) def _with_early_checks(line: tuple, checks: bool) -> tuple[tuple[int, int], ...]: """Slots 163-170 as vertical checks (the next line is a sprite line) or CPU windows.""" 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_LINE = _pattern_mode_line(colour_fetch=True) #: Multicolor uses the Graphics schedule: its colour slots are not CPU windows (C9). #: VATT on 21 TI-part MSX1 entries (tier 3) lands the same fraction of fast writes in #: Multicolor as in Graphics I and II at every period, which a Multicolor line with 51 #: windows cannot do (docs/results.md, rung 4). The colour slot's fetch has no use in #: Multicolor; the core makes it and ignores the byte. MULTICOLOR_LINE = GRAPHICS_LINE #: TI 2.1.5's "at least once out of every four memory cycles" read as colour slots given #: to the CPU (openMSX's ``slotsMsx1Gfx3``): kept for the detector comparison only. MULTICOLOR_ONE_IN_FOUR = _pattern_mode_line(colour_fetch=False) TEXT_LINE = _text_line() REFRESH_LINE = _refresh_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 not mode_16k and roll_over_4k: 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) << 4 | 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)