"""Rung 0: the manual's tables, rebuilt from the manual's numbers. Judges: TI's data manual (tier 2), TI's 1978 timing drawing (tier 2), MacDonald's 4K/16K table (tier 1, quoted by Young). The slot schedules are judged against TI's drawing and TI 2.1.5 only (provenance wave 2, 2026-11-19). """ from __future__ import annotations import pytest from tms9918_python import ( LEVELS_9918A, LEVELS_9928A, NTSC_LAYOUT, PAL_LAYOUT, TMS9918A, TMS9928A, TMS9929A, VARIANTS, ) from tms9918_python import tables as T # --- variants [TI 3.6, 3.7, table 2-3] ----------------------------------------------- def test_lines_per_frame(): # [TI 3.6.2]: "The TMS9929A operates at 313 lines per frame"; 9918A/9928A 262 (C8). assert TMS9918A.lines == 262 assert TMS9928A.lines == 262 assert TMS9929A.lines == 313 def test_ntsc_vertical_layout_is_table_3_3(): # [TI table 3-3], vertical column: 192, 24, 3, 3, 13, 27 = 262. assert (NTSC_LAYOUT.active, NTSC_LAYOUT.bottom_border, NTSC_LAYOUT.bottom_blanking, NTSC_LAYOUT.sync, NTSC_LAYOUT.top_blanking, NTSC_LAYOUT.top_border) == ( 192, 24, 3, 3, 13, 27) # fmt: skip assert PAL_LAYOUT.lines == 313 and PAL_LAYOUT.active == 192 def test_variant_rows(): # [TI 3.7]: pin 38 is CPUCLK on the 9918A, R-Y on the 9928A/9929A; outputs differ. assert TMS9918A.pin38 == "cpuclk" and TMS9918A.output == "composite" assert TMS9928A.pin38 == TMS9929A.pin38 == "r-y" assert TMS9928A.output == TMS9929A.output == "colour-difference" assert all(v.graphics2 and v.has_4k_select for v in VARIANTS.values()) assert set(VARIANTS) == {"TMS9918A", "TMS9928A", "TMS9929A"} def test_levels_table_2_3(): # [TI table 2-3]: a few rows read off the page. assert LEVELS_9918A[15] == (1.00, None) # white assert LEVELS_9918A[12] == (0.46, 0.47) # dark green, 9918A column assert LEVELS_9928A[12] == (0.47, 0.13, 0.23) # dark green, 9928A column assert LEVELS_9918A[7][0] == 0.67 and LEVELS_9928A[7][0] == 0.73 # cyan's Y differs assert LEVELS_9928A[1] == (0.00, 0.47, 0.47) # black: zero colour difference assert LEVELS_9928A[0] is None # transparent assert len(LEVELS_9918A) == len(LEVELS_9928A) == 16 # --- clocks and the horizontal layout [TI 2.1.5, 3.6, table 3-3] ---------------------- def test_clock_arithmetic(): assert T.MASTER_CLOCK_HZ == 10_738_635 assert T.DOTS_PER_LINE == 342 and T.SLOTS_PER_LINE == 171 assert T.MASTER_PER_LINE == 684 # "a memory cycle takes about 372 nanoseconds" [TI 2.1.5]: 4 master clocks = 372.5 ns. assert round(T.MASTER_PER_SLOT / T.MASTER_CLOCK_HZ * 1e9, 1) == 372.5 @pytest.mark.parametrize("layout", [T.HORIZONTAL_PATTERN, T.HORIZONTAL_TEXT]) def test_horizontal_layout_sums_to_342(layout): assert sum(n for _, n in layout) == 342 def test_text_area_offset(): # [TI table 3-3]: left border 13 (pattern modes) against 19 (Text): 6 pixels in. assert T.TEXT_LEFT == 6 assert dict(T.HORIZONTAL_TEXT)["active"] == 240 # --- registers [TI 2.2, figure 2-3] ------------------------------------------------ def test_register_masks(): # R0: M3, EXTVID. R1: all but bit 2. R2: 4 bits. R3: 8. R4: 3. R5: 7. R6: 3. R7: 8. assert T.REGISTER_MASKS == (0x03, 0xFB, 0x0F, 0xFF, 0x07, 0x7F, 0x07, 0xFF) def test_mode_numbers_use_ti_names(): # C2: TI's M1 (R1 0x10) is Text, M2 (R1 0x08) Multicolor, M3 (R0 0x02) Graphics II. assert T.mode_of(0, 0) == T.GRAPHICS_I assert T.mode_of(0, 0x10) == T.TEXT assert T.mode_of(0, 0x08) == T.MULTICOLOR assert T.mode_of(0x02, 0) == T.GRAPHICS_II assert T.mode_of(0x02, 0x10) == T.TEXT_GRAPHICS_II assert T.mode_of(0x02, 0x08) == T.MULTICOLOR_GRAPHICS_II assert T.mode_of(0, 0x18) == T.BOGUS_M1_M2 assert T.mode_of(0x02, 0x18) == T.BOGUS_ALL # A part without Graphics II ignores M3. assert T.mode_of(0x02, 0, graphics2=False) == T.GRAPHICS_I # --- the memory schedule [1978 drawing; TI 2.1.5] ----------------------------------- def _gaps(cpu: list[int], start: int, end: int) -> int: """Longest run of memory cycles between CPU windows inside [start, end).""" inside = [s for s in cpu if start <= s < end] return max(b - a for a, b in zip(inside, inside[1:])) def test_every_schedule_has_171_cycles(): for schedule in T.SCHEDULES.values(): assert len(schedule) == 171 def test_graphics_windows_every_16_cycles_in_the_active_area(): # [TI 2.1.5]: "During the active display, CPU windows occur once every 16 memory # cycles". [1978]: CPU at count 2 of each 32, repeated 8 times. cpu = T.cpu_slots(T.GRAPHICS_LINE) assert [s for s in cpu if s < 128] == [1 + 16 * i for i in range(8)] assert _gaps(cpu, 0, 128) == 16 def test_text_windows_at_least_one_in_three(): # [TI 2.1.5]: "In the Text mode the CPU windows occur at least once out of every three". cpu = T.cpu_slots(T.TEXT_LINE) gaps = [b - a for a, b in zip(cpu, cpu[1:])] assert max(gaps) <= 3 def test_multicolor_uses_the_graphics_schedule(): # C9: VATT (tier 3) lands the same fractions in Multicolor as in Graphics II, so the # colour slots are not CPU windows; TI 2.1.5's "at least once out of every four" is # the reading kept aside (it holds in the active area only, and the horizontal blank # has a longer gap even then). assert T.MULTICOLOR_LINE == T.GRAPHICS_LINE assert T.line_kind(T.MULTICOLOR) == T.line_kind(T.GRAPHICS_II) == "graphics" aside = T.cpu_slots(T.MULTICOLOR_ONE_IN_FOUR) assert _gaps(aside, 0, 128) <= 4 whole = [b - a for a, b in zip(aside, aside[1:] + [aside[0] + 171])] assert max(whole) > 4 def test_refresh_and_blank_have_windows_every_other_cycle(): # [TI 2.1.5]: with BLANK = 0 or in the vertical refresh "there is no waiting for an # access window"; [1978] "REFRESH MODE": refresh and CPU alternate. cpu = T.cpu_slots(T.REFRESH_LINE) gaps = [b - a for a, b in zip(cpu, cpu[1:])] assert max(gaps) == 2 assert sum(1 for k, _ in T.REFRESH_LINE if k == T.REFRESH) == 64 def test_window_counts_per_line(): # Counted from the drawing's reading (_schedule.py). assert len(T.cpu_slots(T.GRAPHICS_LINE)) == 19 assert len(T.cpu_slots(T.MULTICOLOR_LINE)) == 19 # C9 assert len(T.cpu_slots(T.MULTICOLOR_ONE_IN_FOUR)) == 51 assert len(T.cpu_slots(T.TEXT_LINE)) == 91 assert len(T.cpu_slots(T.REFRESH_LINE)) == 107 def test_pattern_mode_fetch_order(): # [1978] "PATTERN PLANE": name at count 0, colour at 4, pattern at 6 of each group. g = T.GRAPHICS_LINE for column in range(32): assert g[4 * column] == (T.NAME, column) assert g[4 * column + 2] == (T.COLOUR, column) assert g[4 * column + 3] == (T.PATTERN, column) def test_sprite_checks_and_fetches(): g = T.GRAPHICS_LINE checks = [arg for kind, arg in g if kind == T.SPRITE_Y] # 32 vertical checks: the first eight at counts 496-510 (AAYY), 24 in the active area. assert sorted(checks) == list(range(32)) assert [g[s] for s in T.EARLY_Y_SLOTS] == [(T.SPRITE_Y, i) for i in range(8)] # Four sprites, each V H N T P1 P2 (rows DDYY-RRYY); SP0's V at count 260. for k in range(4): kinds = [kind for kind, arg in g if arg == k and kind >= T.SPRITE_V] assert kinds == [T.SPRITE_V, T.SPRITE_H, T.SPRITE_N, T.SPRITE_T, T.SPRITE_P1, T.SPRITE_P2] assert g[130] == (T.SPRITE_V, 0) and g[149] == (T.SPRITE_N, 2) # count 468 = dot 298 def test_text_fetch_order(): # [1978] "TEXT MODE": per character name, CPU, pattern; 40 characters. t = T.TEXT_LINE for k in range(40): s = 3 + 3 * k assert t[s : s + 3] == ((T.TEXT_NAME, k), (T.CPU, 0), (T.TEXT_PATTERN, k)) # --- VRAM addressing [Young 2.1 (MacDonald), TI 3.1, table 3-2] ----------------------- def test_16k_mode_is_identity(): for va in (0, 1, 0x3F, 0x40, 0x7F, 0x80, 0x1234, 0x3FFF): assert T.dram_address(va, True, 16384) == va def test_macdonald_4k_mux(): # Column AD0-5 = VA0-5, AD6 = VA12; row AD0-5 = VA6-11, AD6 = VA13. assert T.dram_address(0x0040, False, 16384) == 0x0080 # VA6 -> row bit 0 (cell bit 7) assert T.dram_address(0x1000, False, 16384) == 0x0040 # VA12 -> column AD6 (cell bit 6) assert T.dram_address(0x2000, False, 16384) == 0x2000 # VA13 -> row AD6 assert T.dram_address(0x0800, False, 16384) == 0x1000 # VA11 -> row bit 5 (cell bit 12) assert T.dram_address(0x003F, False, 16384) == 0x003F cells = {T.dram_address(va, False, 16384) for va in range(0x4000)} assert len(cells) == 0x4000 # a permutation: no memory is lost in 4K mode def test_4027_board(): # [TI table 3-1]: on 4027s AD6 is "DATA ONLY", so 4K mode sees 4K mirrored. for va in (0, 0x123, 0xFFF, 0x1000, 0x2ABC): assert T.dram_address(va, False, 4096) == va & 0xFFF def test_vram_size_must_be_a_dram_size(): with pytest.raises(ValueError): T.dram_address(0, True, 2048) def test_address_roll_over(): # Guttag 1982-06-29: in 4K mode the counter rolls over at 4K; at 16K in 16K mode. assert T.increment_address(0x3FFF, True, True) == 0 assert T.increment_address(0x0FFF, False, True) == 0x0000 assert T.increment_address(0x1FFF, False, True) == 0x1000 assert T.increment_address(0x1FFF, False, False) == 0x2000 assert T.increment_address(0x1FFF, True, True) == 0x2000 def test_table_3_2_example_section_3_3(): # [TI 3.3]: R2 = 01 "Name Table Start Address @ >400", R3 = 08 "Color Table @ >0200", # R4 = 01 "Pattern Generator @ >800", R5 = 02 "Sprite Attribute Table > 100", # R6 = 00 "Sprite Pattern Generator @ >0000". assert T.name_address(0x01, 0, 0) == 0x400 assert T.graphics1_colour_address(0x08, 0) == 0x200 assert T.graphics1_pattern_address(0x01, 0, 0) == 0x800 assert T.sprite_attribute_address(0x02, 0, 0) == 0x100 assert T.sprite_pattern_address(0x00, 0, False, 0) == 0x000 def test_table_3_2_pattern_modes(): assert T.name_address(0, 191, 31) == 23 * 32 + 31 # row 23, column 31 assert T.graphics1_colour_address(0, 0xFF) == 31 # five MSBs of the name assert T.graphics1_pattern_address(0, 0xFF, 7) == 0x7FF assert T.text_name_address(0, 191, 39) == 23 * 40 + 39 # row x 40 + column # Multicolor: name x 8 + (row & 3) x 2 + (line >> 2 & 1). for line in range(192): row = line >> 3 assert T.multicolor_pattern_address(0, 5, line) == 40 + (row & 3) * 2 + ((line >> 2) & 1) def test_table_3_2_graphics2(): # R3 bit 7 and R4 bit 2 are address bit 13; the vertical counter's two MSBs pick the # third; full masks (R3 = $FF, R4 = $07) give TI's plain derivation. for third in range(3): char = third << 8 | 0x5A assert T.graphics2_pattern_address(0xFF, 0x07, char, 3, True) == 0x2000 | char << 3 | 3 assert T.graphics2_colour_address(0xFF, 0x07, char, 3, True) == 0x2000 | char << 3 | 3 def test_graphics2_masks_young_3_3(): # R4 = 4 (bits 1-0 zero): every third uses the first 256 patterns [Young 3.3]. assert T.graphics2_pattern_address(0xFF, 0x04, 0x25A, 0, True) == 0x2000 | 0x5A << 3 # R3 = $9F: colour mask $FF over the character number's bits 9-3 -> bits 9-8 cleared. assert T.graphics2_colour_address(0x9F, 0x07, 0x25A, 0, True) == 0x2000 | 0x5A << 3 def test_c3_colour_mask_reaches_patterns_on_99x8(): # C3 (TI's 1982 memo, MAME): R3 bits 4-0 also mask pattern character bits 7-3. # R3 = $80 (64-byte colour table): pattern index loses bits 7-3. assert T.graphics2_pattern_address(0x80, 0x07, 0x0FF, 0, True) == 0x2000 | 0x07 << 3 assert T.graphics2_pattern_address(0x80, 0x07, 0x0FF, 0, False) == 0x2000 | 0xFF << 3 # A colour table of 2K or more (R3 bits 4-0 all ones) leaves patterns alone. assert T.graphics2_pattern_address(0x9F, 0x07, 0x2FF, 0, True) == 0x2000 | 0x2FF << 3 def test_sprite_pattern_16x16(): # [TI table 3-2]: SIZE = 1 uses the six MSBs of the name; rows 0-15. assert T.sprite_pattern_address(0, 0x07, True, 15) == 0x04 << 3 | 15 assert T.sprite_pattern_address(1, 0x07, False, 7) == 0x800 | 0x07 << 3 | 7