"""Rung 1: registers and ports, one directed test per sentence of the manual. Judges: TI's data manual 2.1-2.3 (tier 2), Young 2 (tier 3) where TI is silent, and MacDonald's 4K table (tier 1). Each test names its sentence. C and U numbers: the brief. """ from __future__ import annotations import dataclasses import pytest from helpers import ( LINE, instant, run_to, set_read_address, set_reg, set_write_address, write_block, ) from tms9918_python import TMS9918, TMS9918A, TMS9928A, TMS9929A from tms9918_python import tables as T # --- the control port [TI 2.1.2, 2.1.3, table 2-1; Young 2.2-2.3] --------------------- def test_register_write_pair(): # Table 2-1 "WRITE TO VDP REGISTER": data byte, then 1 0 0 0 0 RS0 RS1 RS2. vdp = instant() for reg, value in enumerate((0x02, 0xE2, 0x0E, 0xFF, 0x03, 0x36, 0x07, 0xF4)): set_reg(vdp, reg, value) assert vdp.regs == [0x02, 0xE2, 0x0E, 0xFF, 0x03, 0x36, 0x07, 0xF4] def test_register_select_ignores_bits_6_to_3(): # Young 2.2: "Bits 6-3 of the second byte are 'don't care'". TI asks for zeros. vdp = instant() vdp.write(1, 0x5A) vdp.write(1, 0xFF) # register 7 assert vdp.regs[7] == 0x5A def test_register_bits_outside_figure_2_3_are_dropped(): vdp = instant() for reg in range(8): set_reg(vdp, reg, 0xFF) assert tuple(vdp.regs) == T.REGISTER_MASKS def test_register_write_destroys_the_address(): # [TI 2.1.2 note]: "The CPU address is destroyed by writing to the VDP register". vdp = instant() set_write_address(vdp, 0x1234) set_reg(vdp, 1, 0xC0) # bytes $C0, $81 assert vdp.addr == 0x01C0 vdp.write(0, 0x77) assert vdp.vram[0x01C0] == 0x77 def test_first_control_byte_goes_to_the_address_at_once(): # U1 (no hardware source; MAME and sms-python): a lone first byte then a data write. vdp = instant() set_write_address(vdp, 0x2100) vdp.write(1, 0x34) # first byte only vdp.write(0, 0xAB) # clears the flip-flop, writes at $2134 assert vdp.vram[0x2134] == 0xAB def test_status_read_reinitialises_the_interface(): # [TI 2.1.2]: "the status register must be read so that internal CPU interface logic # is reinitialized". vdp = instant() vdp.write(1, 0x99) # stray first byte vdp.read(1) set_reg(vdp, 7, 0x1F) assert vdp.regs[7] == 0x1F @pytest.mark.parametrize("access", ["data write", "data read"]) def test_data_access_clears_the_flip_flop(access): # Young 2.3: "this flag is also reset whenever you read port #1, or read or write port #0". vdp = instant() vdp.write(1, 0x99) if access == "data write": vdp.write(0, 0) else: vdp.read(0) set_reg(vdp, 7, 0x2E) assert vdp.regs[7] == 0x2E def test_second_byte_bits_7_and_6(): # Table 2-1: write setup is 0 1 A0-A5; read setup is 0 0 A0-A5; register is 1 x. vdp = instant() set_write_address(vdp, 0x3ABC) assert vdp.addr == 0x3ABC set_read_address(vdp, 0x0123) assert vdp.addr == 0x0124 # the read was started and the address moved on # --- VRAM through the data port [TI 2.1.3, 2.1.5; Young 2.1] ------------------------- def test_write_to_vram_autoincrements(): # [TI 2.1.3]: "A 1-byte transfer is then required ... The address register is then # autoincremented. Sequential VRAM writes require only 1-byte transfers". vdp = instant() write_block(vdp, 0x1800, b"\x10\x20\x30") assert bytes(vdp.vram[0x1800:0x1803]) == b"\x10\x20\x30" assert vdp.addr == 0x1803 def test_address_wraps_at_16k(): # Young 2.1: "After 3fffh (16kB limit) the r/w address wraps to zero." vdp = instant() set_reg(vdp, 1, 0x80) write_block(vdp, 0x3FFF, b"\x01\x02") assert vdp.vram[0x3FFF] == 1 and vdp.vram[0] == 2 def test_read_setup_fetches_the_first_byte(): # [TI 2.1.5]: "By setting up the address this way, a read cycle to VRAM is initiated # and read data will be available for the first data transfer". vdp = instant() write_block(vdp, 0x0400, b"\xa1\xa2\xa3") set_read_address(vdp, 0x0400) assert [vdp.read(0) for _ in range(3)] == [0xA1, 0xA2, 0xA3] def test_read_ahead_is_one_byte_behind_the_address(): # Young 2.1: "Return value in read-ahead buffer. Read next value ... Increase". vdp = instant() write_block(vdp, 0, bytes(range(8))) set_read_address(vdp, 2) vdp.read(0) assert vdp.addr == 4 and vdp.read_ahead == 3 def test_a_write_replaces_the_read_ahead(): # Young 2.1: "Interestingly, the value written is also stored in the read-ahead buffer." vdp = instant() write_block(vdp, 0x100, b"\x11\x22") set_read_address(vdp, 0x100) vdp.write(0, 0x99) # writes $101, read-ahead = $99 assert vdp.read(0) == 0x99 assert vdp.vram[0x101] == 0x99 def test_registers_cannot_be_read_back(): # [TI 2.2]: the registers are write-only; port reads give data or status only. vdp = instant() set_reg(vdp, 2, 0x0E) assert vdp.read(1) & 0x1F == vdp.status & 0x1F # --- status, F and /INT [TI 2.1.6, 2.3; Young 2.4] ----------------------------------- def test_status_read_clears_f_5s_and_c_and_keeps_the_number(): # [TI 2.3.1-2.3.3]: F, C and 5S are "cleared to a 0 after the status register is read"; # C1 (Young says bits 7 and 5 only): TI. vdp = instant() vdp.status = 0xE0 | 0x0B assert vdp.read(1) == 0xEB assert vdp.status == 0x0B def test_f_sets_at_the_end_of_line_191(): # [TI 2.3.1]: "set to 1 at the end of the raster scan of the last line of the active # display" (C10: dot 266 of line 191, the end of its active pixels). vdp = instant() run_to(vdp, 191, 264) assert not vdp.status & 0x80 vdp.run(4) assert vdp.status & 0x80 def test_f_sets_once_a_frame_without_reads(): vdp = instant() run_to(vdp, 191, 270) assert vdp.read(1) & 0x80 vdp.run(LINE * 100) assert not vdp.status & 0x80 vdp.run(LINE * 162) assert vdp.status & 0x80 def test_flag_dot_switch(): # C10's switch: MAME 0.285 sets F while drawing line 193. vdp = instant(flag_dot=(193, 10)) run_to(vdp, 192, 0) assert not vdp.status & 0x80 run_to(vdp, 193, 10) assert vdp.status & 0x80 def test_status_race_switch(): # [TI 2.3]: "asynchronous reads will cause the frame flag (F) bit to be reset and # therefore missed" (U8: off by default; the window is a stand-in). vdp = instant() run_to(vdp, 191, 266) assert vdp.read(1) & 0x80 # default: no race vdp = instant(status_race=True) run_to(vdp, 191, 266) assert not vdp.read(1) & 0x80 # read as F sets: F lost vdp = instant(status_race=True) run_to(vdp, 191, 264) # 4 master clocks before F assert not vdp.read(1) & 0x80 vdp.run(LINE) assert not vdp.status & 0x80 # this frame's F never sets vdp.run(LINE * 262) assert vdp.status & 0x80 # the next frame's does def test_int_follows_f_and_ie(): # [TI 2.1.6]: "The INT output is active when the Interrupt Enable bit (IE) in VDP # Register 1 is a 1 and the F bit of the status register is a 1." vdp = instant() set_reg(vdp, 1, 0x60) assert not vdp.int_line run_to(vdp, 191, 266) assert vdp.int_line def test_setting_ie_while_f_is_set_asserts_int_at_once(): # Young 2.4: "you set it while bit 7 of the status register is set, the INT line goes # low immediately". vdp = instant() run_to(vdp, 191, 266) assert not vdp.int_line set_reg(vdp, 1, 0x20) assert vdp.int_line def test_status_read_releases_int(): # [TI 2.1.6]: "Interrupts are cleared when the status register is read." vdp = instant() set_reg(vdp, 1, 0x20) run_to(vdp, 191, 266) vdp.read(1) assert not vdp.int_line def test_clearing_ie_drops_int_but_keeps_f(): vdp = instant() set_reg(vdp, 1, 0x20) run_to(vdp, 191, 266) set_reg(vdp, 1, 0x00) assert not vdp.int_line and vdp.status & 0x80 def test_status_read_leaves_the_address_alone(): vdp = instant() set_write_address(vdp, 0x0ABC) vdp.read(1) vdp.write(0, 1) assert vdp.vram[0x0ABC] == 1 def test_frame_rates(): # [TI 2.1.6]: interrupts "about every 1/60 second for the TMS9918A/9928A and 1/50 # second for the TMS9929A" (262 and 313 lines of 342 pixel clocks). for variant, lines in ((TMS9918A, 262), (TMS9928A, 262), (TMS9929A, 313)): vdp = TMS9918(variant, windows=False) vdp.run(LINE * lines * 3) assert vdp.frame_count == 3 assert round(T.MASTER_CLOCK_HZ / (LINE * lines)) == (60 if lines == 262 else 50) # --- reset [TI 2.1.7] ---------------------------------------------------------------- def test_reset_clears_r0_r1_and_f(): # [TI 2.1.7]: reset "clears VDP registers 0 and 1"; [TI 2.3.1]: F is "reset ... when # the VDP is externally reset"; C and 5S likewise [TI 2.3.2, 2.3.3]. vdp = instant() for reg in range(8): set_reg(vdp, reg, 0xFF) vdp.status = 0xFF vdp.reset() assert vdp.regs[:2] == [0, 0] and vdp.regs[2:] == list(T.REGISTER_MASKS[2:]) assert vdp.status == 0x1F def test_reset_clears_the_flip_flop_and_counters(): # U2 (provisional: TI says reset "synchronizes all clocks"); U4: counters to line 0. vdp = instant() vdp.write(1, 0x12) vdp.run(12345) vdp.reset() assert (vdp.line, vdp.dot) == (0, 0) set_reg(vdp, 7, 0x44) assert vdp.regs[7] == 0x44 def test_reset_keeps_vram(): vdp = instant() write_block(vdp, 0x200, b"\x55") vdp.reset() assert vdp.vram[0x200] == 0x55 def test_power_on_vram_is_seedable(): vdp = TMS9918(TMS9928A, 4096, power_on_vram=bytes([0xE5]) * 4096) assert vdp.vram[123] == 0xE5 # --- 4K/16K [TI 3.1; Young 2.1 (MacDonald); Guttag 1982 (C11)] --------------------- def test_4k_mode_scrambles_16k_data(): # MacDonald: "This does have consequences if you write to VRAM in one setting, then # change the bit and try to read data back." vdp = instant() set_reg(vdp, 1, 0x80) write_block(vdp, 0x0080, b"\x5a") # 16K mode: cell $0080 set_reg(vdp, 1, 0x00) # 4K mode set_read_address(vdp, 0x0040) # VA6 -> row bit 0 -> cell $0080 assert vdp.read(0) == 0x5A def test_4k_mode_counter_rolls_over_at_4k(): # C11, Guttag: "when in the 4K 'mode' the address counter register will 'roll over' # the LS bits at 4K". vdp = instant(r1=0x00) write_block(vdp, 0x1FFF, b"\x01\x02") assert vdp.addr == 0x1001 vdp2 = instant(r1=0x00, roll_over_4k=False) write_block(vdp2, 0x1FFF, b"\x01\x02") assert vdp2.addr == 0x2001 def test_4027_board_mirrors_4k(): # [TI 3.1.2, table 3-1]: 4027s with the bit at 0. vdp = instant(r1=0x00, vram_size=4096) write_block(vdp, 0x0123, b"\x42") set_read_address(vdp, 0x2123) assert vdp.read(0) == 0x42 def test_91x8_ignores_the_4k_bit(): # [PG 5.1.2 note]: on the TMS91x8 the bit is "Don't Care" (16K assumed). row = dataclasses.replace(TMS9928A, name="TMS9128", has_4k_select=False) vdp = TMS9918(row, windows=False) # R1 = 0 after power-on: "4K" write_block(vdp, 0x0040, b"\x33") assert vdp.vram[0x0040] == 0x33 # --- the host surface (contract) -------------------------------------------------- def test_run_in_pieces_equals_run_at_once(): a, b = instant(), instant() set_reg(a, 1, 0xE0) set_reg(b, 1, 0xE0) a.run(100_000) for n in (1, 3, 17, 683, 98_000, 1296): b.run(n) assert (a.line, a.dot, a.clock, a.status) == (b.line, b.dot, b.clock, b.status) def test_take_frame_once_per_frame(): vdp = instant() assert vdp.take_frame() is None vdp.run(LINE * 262) frame = vdp.take_frame() assert frame is not None and frame.number == 0 assert len(frame.pixels) == 192 and all(len(r) == 256 for r in frame.pixels) assert len(frame.border) == 262 assert vdp.take_frame() is None def test_fewer_than_80_attributes(): # PyPy turns an instance with 80 or more attributes into a dict (family knowledge). assert len(vars(TMS9918(TMS9928A))) < 80 def test_bad_arguments(): with pytest.raises(ValueError): TMS9918(TMS9928A, 2048) with pytest.raises(ValueError): TMS9918(TMS9928A, lost_access="random") with pytest.raises(ValueError): instant().run(-1) # --- access timing [TI 2.1.5, table 2-2] -------------------------------------------- DELAY = TMS9928A.access_delay US = T.MASTER_CLOCK_HZ / 1e6 # master clocks per microsecond def _landing_time(vdp: TMS9918, addr: int, value: int) -> int: """Master clocks from a data write to the moment VRAM holds the byte.""" set_write_address(vdp, addr) vdp.write(0, value) t = 0 while vdp.vram[addr] != value: vdp.run(1) t += 1 return t def _worst_landing(mode_regs: tuple[int, int], lines: range) -> int: worst = 0 for line in lines: for dot in range(0, 342, 2): vdp = TMS9918(TMS9928A) set_reg(vdp, 0, mode_regs[0]) set_reg(vdp, 1, mode_regs[1]) run_to(vdp, line, dot) worst = max(worst, _landing_time(vdp, 0x3000, 0xA5)) return worst def test_transfers_are_not_instantaneous(): # [TI 2.1.5]: "it takes approximately 2 microseconds until the VDP is ready". vdp = TMS9918(TMS9928A) set_reg(vdp, 1, 0x80) # blanked: every other cycle a window t = _landing_time(vdp, 0x0100, 0x5A) assert DELAY <= t <= DELAY + 8 # TI's "approximately 2 microseconds"; the default is VATT's fit, 1.49 us (U13). assert 1.0 <= DELAY / US <= 2.5 def test_table_2_2_graphics_active_display(): # Table 2-2: Graphics I, II active display, total 2-8 us. worst = _worst_landing((0x00, 0xC0), range(100, 101)) assert worst <= round(8.0 * US) + 1 assert worst > round(6.0 * US) # the 16-cycle gap is reached def test_table_2_2_text_active_display(): # Table 2-2: Text, total 2-3.1 us. worst = _worst_landing((0x00, 0xD0), range(100, 101)) assert worst <= round(3.1 * US) + 1 def test_table_2_2_blank_and_vertical_refresh(): # Table 2-2: "Register 1 Blank Bit 0" and "4300 us after Vertical Interrupt": 2 us. assert _worst_landing((0x00, 0x80), range(50, 51)) <= DELAY + 8 assert _worst_landing((0x00, 0xC0), range(200, 201)) <= DELAY + 8 def test_a_read_returns_the_old_buffer_if_its_window_has_not_come(): vdp = TMS9918(TMS9928A) set_reg(vdp, 1, 0xC0) vdp.vram[0x0300:0x0302] = b"\x01\x02" run_to(vdp, 50, 100) set_read_address(vdp, 0x0300) assert vdp.read(0) == 0 # the setup's read has not been served yet vdp.run(LINE) assert vdp.read(0) == 0x02 # served: the buffer holds $0301 def test_lost_write_newer_wins(): # U3, open decision 2: a second access before the first's window replaces it; the # address counts both. vdp = TMS9918(TMS9928A) set_reg(vdp, 1, 0xC0) run_to(vdp, 60, 20) set_write_address(vdp, 0x2000) vdp.write(0, 0x11) vdp.write(0, 0x22) vdp.run(LINE) assert (vdp.vram[0x2000], vdp.vram[0x2001]) == (0x00, 0x22) assert vdp.lost_accesses == 1 and vdp.addr == 0x2002 def test_lost_write_older_wins(): vdp = TMS9918(TMS9928A, lost_access="older") set_reg(vdp, 1, 0xC0) run_to(vdp, 60, 20) set_write_address(vdp, 0x2000) vdp.write(0, 0x11) vdp.write(0, 0x22) vdp.run(LINE) assert (vdp.vram[0x2000], vdp.vram[0x2001]) == (0x11, 0x00) assert vdp.lost_accesses == 1