"""Rung 3: the sprite rules [TI 2.3, 2.4.5; Young 4], one directed test each. Flicky's stream (5S every frame) is compared read by read with MAME's log in test_rung2_pictures.py::test_game_stream_matches_mame. """ from __future__ import annotations from helpers import LINE, run_to, set_reg, write_block from tms9918_python import TMS9918, TMS9928A SAT = 0x1B00 # R5 = $36 SPG = 0x3800 # R6 = $07 def chip(r1: int = 0xC0, **kw) -> TMS9918: """Graphics I with transparent patterns and backdrop 1, sprite tables at SAT/SPG.""" vdp = TMS9918(TMS9928A, windows=False, **kw) set_reg(vdp, 1, 0x80) for reg, value in {0: 0, 2: 0x06, 3: 0x80, 4: 0x00, 5: 0x36, 6: 0x07, 7: 0x01}.items(): set_reg(vdp, reg, value) write_block(vdp, SPG, bytes([0xFF]) * 0x800) # every sprite pattern solid write_block(vdp, SAT, bytes([0xD0])) # empty table set_reg(vdp, 1, r1) return vdp def sprites(vdp: TMS9918, entries) -> None: data = b"".join(bytes(e) for e in entries) + b"\xd0" write_block(vdp, SAT, data) def frame(vdp: TMS9918): vdp.run(LINE * 262 * 2) return vdp.take_frame().pixels def status_after_frame(vdp: TMS9918) -> int: """Clear status at the top of the frame, run to the end of line 191, read.""" run_to(vdp, 0, 0) vdp.read(1) run_to(vdp, 191, 264) # just before F return vdp.read(1) def test_four_sprites_a_line(): # [TI 2.3.2]: four sprites a line; "lower number = higher priority" - the fifth is # not shown. vdp = chip() sprites(vdp, [(49, 8 * i * 3, 0, 2 + i) for i in range(5)]) rows = frame(vdp) assert [rows[50][24 * i] for i in range(5)] == [2, 3, 4, 5, 1] def test_fifth_sprite_flag_and_number(): # [TI 2.3.3]: 5S set with five or more on a line and F = 0; the fifth's number. vdp = chip() sprites(vdp, [(0xD1 if i < 3 else 49, i * 10, 0, 2) for i in range(3)] + [(49, 40 + i * 10, 0, 2) for i in range(5)]) # fmt: skip status = status_after_frame(vdp) assert status & 0x40 and status & 0x1F == 7 def test_fifth_sprite_needs_f_clear(): # [TI 2.3.3]: "... and the frame flag is equal to a 0". vdp = chip() run_to(vdp, 191, 270) vdp.read(1) # clear whatever the power-on frame left run_to(vdp, 191, 270) # F set again; nobody reads it assert vdp.status & 0xC0 == 0x80 sprites(vdp, [(49, i * 10, 0, 2) for i in range(5)]) vdp.run(LINE * 262) status = vdp.read(1) assert status & 0x80 and not status & 0x40 def test_number_without_a_fifth_sprite(): # Young 4.3: with 5S clear the number is the last entry examined: 31, or the first # with Y = 208. vdp = chip() # Terminator at entry 6, no line with five sprites. sprites(vdp, [(49, 0, 0, 2), (100, 0, 0, 2), (120, 0, 0, 2), (140, 0, 0, 2), (160, 0, 0, 2), (170, 0, 0, 2)]) # fmt: skip assert status_after_frame(vdp) & 0x5F == 6 # No terminator; sprite i on lines 5i+1 to 5i+8, never more than two on a line. write_block(vdp, SAT, b"".join(bytes((5 * i, 0, 0, 2)) for i in range(32))) assert status_after_frame(vdp) & 0x5F == 31 def test_fifth_sprite_on_line_192(): # [TI 2.3.3]: "lines 0 to 192" (C5): five sprites whose top line is 192. vdp = chip() sprites(vdp, [(191, i * 20, 0, 2) for i in range(5)]) run_to(vdp, 0, 0) vdp.read(1) run_to(vdp, 191, 264) status = vdp.read(1) assert status & 0x40 and status & 0x1F == 4 def test_terminator(): # [TI 2.3.2]: "Sprites beyond the Sprite Attribute Table terminator (D0) are not # considered". vdp = chip() write_block(vdp, SAT, bytes((49, 0, 0, 2, 0xD0, 0, 0, 0, 49, 20, 0, 3))) rows = frame(vdp) assert rows[50][0] == 2 and rows[50][20] == 1 def test_y_positions(): # [TI 2.4.5]: "A value of -1 puts the sprite butted up at the top"; E1-FF bleed in; # Y is one line above the sprite's first line. vdp = chip() sprites(vdp, [(0xFF, 0, 0, 2), (0xF8, 20, 0, 3), (0, 40, 0, 4), (0xE0, 60, 0, 5)]) rows = frame(vdp) assert rows[0][0] == 2 and rows[7][0] == 2 and rows[8][0] == 1 assert rows[0][20] == 3 and rows[1][20] == 1 # -8: lines -7 to 0 assert rows[0][40] == 1 and rows[1][40] == 4 assert all(rows[y][60] == 1 for y in range(192)) # $E0 = line 225: off screen def test_early_clock(): # [TI 2.4.5]: EC shifts the sprite 32 pixels left. vdp = chip() sprites(vdp, [(49, 36, 0, 0x82)]) rows = frame(vdp) assert list(rows[50][:14]) == [1] * 4 + [2] * 8 + [1] * 2 def test_size_and_magnification(): # [TI table 2-5, 3-2]: SIZE 16 x 16 uses name & $FC, the right half 16 bytes on; MAG # doubles each pixel. vdp = chip(r1=0xC3) write_block(vdp, SPG + 4 * 8, bytes([0x80] * 16 + [0x01] * 16)) # name 4..7 sprites(vdp, [(49, 0, 6, 2)]) rows = frame(vdp) assert list(rows[50][:4]) == [2, 2, 1, 1] assert list(rows[50][28:34]) == [1, 1, 2, 2, 1, 1] assert rows[81][0] == 2 and rows[82][0] == 1 # 32 lines tall def test_priority_and_transparency(): # [TI 2.4]: lower number in front; a transparent sprite lets the plane behind show. vdp = chip() sprites(vdp, [(49, 0, 0, 0x00), (49, 4, 0, 3), (49, 8, 0, 4)]) rows = frame(vdp) assert list(rows[50][:16]) == [1] * 4 + [3] * 8 + [4] * 4 def test_coincidence(): # [TI 2.3.2]: C "if any two sprites on the screen have one overlapping pixel". vdp = chip() sprites(vdp, [(49, 0, 0, 2), (49, 7, 0, 3)]) assert status_after_frame(vdp) & 0x20 vdp2 = chip() sprites(vdp2, [(49, 0, 0, 2), (49, 8, 0, 3)]) assert not status_after_frame(vdp2) & 0x20 def test_transparent_sprites_coincide(): # [TI 2.3.2]: "Transparent colored sprites ... are also considered." vdp = chip() sprites(vdp, [(49, 0, 0, 0), (49, 4, 0, 0)]) assert status_after_frame(vdp) & 0x20 def test_offscreen_coincidence(): # [TI 2.3.2]: "those that are partially or completely off the screen, are also # considered" (C7). Two 8 x 8 sprites overlapping only at columns 256-259. vdp = chip() sprites(vdp, [(49, 252, 0, 2), (49, 255, 0, 3)]) # overlap at 255-259: one visible sprites(vdp, [(49, 252, 0, 2), (49, 0, 0, 0x83)]) # 252-259 and -32..-25: none assert not status_after_frame(vdp) & 0x20 vdp = chip() write_block(vdp, SPG, bytes([0x0F]) * 8) sprites(vdp, [(49, 252, 0, 2), (49, 253, 0, 3)]) # pixels 256-259 and 257-260 assert status_after_frame(vdp) & 0x20 vdp = chip(offscreen_collisions=False) write_block(vdp, SPG, bytes([0x0F]) * 8) sprites(vdp, [(49, 252, 0, 2), (49, 253, 0, 3)]) assert not status_after_frame(vdp) & 0x20 # the detectors' rule def test_dropped_fifth_sprite_does_not_coincide(): # Young 4.3: "If a sprite line is not displayed because it is illegal, it does not # count for sprite collision." vdp = chip() sprites(vdp, [(49, 40 * i, 0, 2) for i in range(4)] + [(49, 0, 0, 3)]) assert not status_after_frame(vdp) & 0x20 def test_no_sprites_in_text_mode(): # [TI 2.3.2]: "The sprites are not active in the Text mode." vdp = chip(r1=0xD0) sprites(vdp, [(49, 6, 0, 2)] * 6) rows = frame(vdp) assert rows[50][6] != 2 assert not status_after_frame(vdp) & 0x60 def test_no_sprites_while_blank(): # Young 1.2: BLANK = 0, "Sprite system inactive"; [TI 2.1.5] only the border shows. vdp = chip(r1=0x80) sprites(vdp, [(49, 0, 0, 2)] * 6) rows = frame(vdp) assert rows[50][0] == 1 assert not status_after_frame(vdp) & 0x60 def test_coincidence_is_set_during_the_line(): # U10: C sets at the dot the first coinciding pixel is shown (pixel x at dot x + 10). vdp = chip() sprites(vdp, [(49, 100, 0, 2), (49, 104, 0, 3)]) run_to(vdp, 0, 0) vdp.read(1) run_to(vdp, 50, 112) # pixel 104 shows at dot 114 assert not vdp.status & 0x20 vdp.run(4) assert vdp.status & 0x20 def test_sprite_table_is_read_the_line_before(): # [1978]: sprite attributes and patterns are fetched in the horizontal blank of the # line before; a change written during line 49's active area misses line 50's fetch # only if it comes after the fetch (U11 territory: the fetch-driven design). vdp = chip() sprites(vdp, [(49, 0, 0, 2)]) run_to(vdp, 49, 320) # after line 49's sprite fetches (dots 260-316) write_block(vdp, SAT + 3, bytes([5])) # colour change vdp.run(LINE * 2) run_to(vdp, 0, 0) vdp.run(LINE * 192) frame_ = vdp.take_frame() assert frame_.pixels[50][0] == 2 # line 50 used the old colour (fetched on line 49) assert frame_.pixels[51][0] == 5