"""The VDP's ports, counters, interrupts and renderers, against msvdp and FluBBa's rules.""" from __future__ import annotations import pytest from sms_python.vdp import ( MCLOCKS_PER_LINE, SEGA_315_5124, SEGA_315_5246, SEGA_315_5313, SEGA_GAME_GEAR, STATUS_COLLISION, STATUS_FRAME, STATUS_OVERFLOW, TMS9918A, TMS9918A_RGB, VDP, sms_rgb, v_counter_table, ) LINE_CYCLES = MCLOCKS_PER_LINE // 3 def set_address(vdp: VDP, address: int, code: int) -> None: vdp.write_control(address & 0xFF) vdp.write_control((code << 6) | (address >> 8)) def set_register(vdp: VDP, number: int, value: int) -> None: vdp.write_control(value) vdp.write_control(0x80 | number) # -- ports (FluBBa's VDP test names in quotes) ---------------------------------------- def test_first_control_byte_sets_the_low_address_at_once(): # "1byte wr sets rd adr" / "1byte wr sets wr adr" vdp = VDP() set_address(vdp, 0x1234, 1) vdp.write_control(0x56) assert vdp.address == 0x1256 assert vdp.code == 1 # "1byte wr keeps wr mode" def test_code_0_prefetches_and_reads_return_the_buffer(): vdp = VDP() vdp.vram[0x100:0x103] = b"\x11\x22\x33" set_address(vdp, 0x100, 0) assert vdp.address == 0x101 assert [vdp.read_data() for _ in range(3)] == [0x11, 0x22, 0x33] @pytest.mark.parametrize("code", [1, 2, 3]) def test_other_codes_do_not_prefetch(code): # "Read after code1/2/3 wr": no prefetch, otherwise the same. vdp = VDP() vdp.vram[0x200] = 0x99 vdp.read_buffer = 0x42 vdp.write_control(0x00) vdp.write_control((code << 6) | 0x02) assert vdp.address == 0x200 assert vdp.read_data() == 0x42 @pytest.mark.parametrize("code", [1, 3]) def test_data_writes_load_the_read_buffer(code): # "VRAM wr set VDPbuffer" / "CRAM wr set VDPbuffer" vdp = VDP() set_address(vdp, 0x0000, code) vdp.write_data(0x5A) assert vdp.read_buffer == 0x5A @pytest.mark.parametrize("reset", ["read_status", "read_data", "write_data"]) def test_status_and_data_accesses_reset_the_latch(reset): # "Rd VDPCtrl reset latch", "Rd VDPData reset latch", "Wr VDPData reset latch" vdp = VDP() vdp.write_control(0x00) assert vdp.second_byte if reset == "write_data": vdp.write_data(0) else: getattr(vdp, reset)() assert not vdp.second_byte def test_address_wraps_at_3fff(): vdp = VDP() set_address(vdp, 0x3FFF, 1) vdp.write_data(0xAB) vdp.write_data(0xCD) assert vdp.vram[0x3FFF] == 0xAB assert vdp.vram[0x0000] == 0xCD def test_vram_and_cram_share_one_address(): vdp = VDP() set_address(vdp, 0x0005, 3) vdp.write_data(0x3F) assert vdp.cram[5] == 0x3F assert vdp.address == 0x0006 def test_registers_11_to_15_are_ignored(): # "Unused regs, no effect" vdp = VDP() before = bytes(vdp.registers) for number in range(11, 16): set_register(vdp, number, 0xFF) assert bytes(vdp.registers) == before def test_register_number_bits_above_3_are_ignored(): # "VDP Register mirrors": bits 12 and 13 of the command word have no effect. vdp = VDP() vdp.write_control(0x77) vdp.write_control(0x80 | 0x30 | 0x07) assert vdp.registers[7] == 0x77 def test_tms9918a_register_writes_use_bit_7_and_three_bits(): vdp = VDP(TMS9918A) vdp.write_control(0x21) vdp.write_control(0xC0 | 0x0B) # bit 6 set too; register 11 & 7 = 3 assert vdp.registers[3] == 0x21 def test_game_gear_cram_writes_on_the_odd_byte(): # msvdp section 5: even address latches; odd writes latch + byte as one colour. vdp = VDP(SEGA_GAME_GEAR) set_address(vdp, 0x0000, 3) vdp.write_data(0xFF) assert vdp.cram[0] == 0 set_address(vdp, 0x0021, 3) vdp.write_data(0x0F) assert (vdp.cram[0x20], vdp.cram[0x21]) == (0xFF, 0x0F) assert vdp.cram[0] == 0 # -- counters and interrupts ----------------------------------------------------------- def test_v_counter_tables_match_msvdp(): for (region, height), jump in { ("NTSC", 192): (0xDA, 0xD5), ("NTSC", 224): (0xEA, 0xE5), ("PAL", 192): (0xF2, 0xBA), }.items(): table = v_counter_table(region, height) assert len(table) == (262 if region == "NTSC" else 313) index = table.index(jump[0]) assert table[index + 1] == jump[1] assert table[-1] == 0xFF def test_v_counter_reads_follow_the_line(): vdp = VDP() vdp.run(LINE_CYCLES * 219) assert vdp.read_v_counter() == 0xD5 # line 219: after the $DA -> $D5 jump vdp.run(LINE_CYCLES * 43) assert vdp.read_v_counter() == 0x00 def advance_to(vdp: VDP, line: int, m: int) -> None: """Run whole CPU cycles until the VDP is at (line, m) or at most 2 mclocks past it.""" target = line * MCLOCKS_PER_LINE + m now = vdp.line * MCLOCKS_PER_LINE + vdp.m if target < now: # into the next frame target += vdp.lines_per_frame * MCLOCKS_PER_LINE vdp.run(-(-(target - now) // 3)) def test_frame_irq_and_flag_at_the_end_of_line_192(): # Fitted to FluBBa's "Frame IRQ HCount" and "VINT flag HCount": the IRQ rises at # m = 683 of line 192 and the flag shows at the V counter change. CPU cycles fall # every 3 mclocks, so software sees both at the same boundary. vdp = VDP() set_register(vdp, 1, 0x20) advance_to(vdp, 192, 681) assert not vdp.irq assert not vdp.status & STATUS_FRAME advance_to(vdp, 193, 0) assert vdp.irq assert vdp.status & STATUS_FRAME assert vdp.read_status() & STATUS_FRAME assert not vdp.irq def test_frame_flag_is_kept_until_status_is_read(): # "VINT flag keept" vdp = VDP() advance_to(vdp, 200, 0) advance_to(vdp, 261, 0) assert vdp.status & STATUS_FRAME def test_enabling_the_frame_irq_late_raises_it_at_once(): vdp = VDP() advance_to(vdp, 200, 0) assert not vdp.irq set_register(vdp, 1, 0x20) assert vdp.irq @pytest.mark.parametrize("reload", [0, 3]) def test_line_interrupt_every_reload_plus_one_lines(reload): vdp = VDP() set_register(vdp, 0, 0x14) # Mode 4, line interrupts on set_register(vdp, 10, reload) advance_to(vdp, 261, 0) # counter reloaded outside the display vdp.read_status() # drop what the skipped frame left pending fired = [] # the line the handler would see in the V counter for line in range(0, 193): advance_to(vdp, line, 1) if vdp.line_pending: fired.append(vdp.line) vdp.read_status() # FluBBa's "Line IRQ VCount": register 10 = N interrupts with the V counter at N. assert fired == list(range(reload, 193, reload + 1)) def test_h_counter_matches_flubbas_events(): # FluBBa: the V counter changes at HCount $F4; HCount advances one per 4 mclocks. vdp = VDP() advance_to(vdp, 10, 0) vdp.latch_h_counter() assert vdp.read_h_counter() == 0xF4 advance_to(vdp, 10, 399) vdp.latch_h_counter() assert vdp.read_h_counter() == 0xF4 + 100 & 0xFF # -- rendering ------------------------------------------------------------------------- def mode4(vdp: VDP) -> None: set_register(vdp, 0, 0x06) set_register(vdp, 1, 0x40) # display on set_register(vdp, 2, 0xFF) # name table $3800 set_register(vdp, 5, 0xFF) # sprite table $3F00 set_register(vdp, 6, 0xFB) # sprite patterns at $0000 vdp.vram[0x3F00] = 0xD0 def test_mode4_background_pixel_uses_cram(): vdp = VDP() mode4(vdp) vdp.vram[0x0020:0x0024] = b"\x80\x00\x00\x00" # tile 1, row 0: left pixel colour 1 vdp.vram[0x3800:0x3802] = b"\x01\x00" # top-left entry: tile 1 vdp.cram[1] = 0x03 # red row = vdp._render_line(0) assert row[0] == sms_rgb(0x03) assert row[1] == sms_rgb(vdp.cram[0]) def test_mode4_sprite_over_background_and_under_priority_tiles(): vdp = VDP() mode4(vdp) vdp.vram[0x0020:0x0024] = b"\xff\x00\x00\x00" # tile 1 row 0: colour 1 vdp.vram[0x0040:0x0044] = b"\xff\xff\x00\x00" # tile 2 row 0: colour 3 vdp.vram[0x3800:0x3802] = b"\x01\x00" # column 0: tile 1, low priority vdp.vram[0x3802:0x3804] = b"\x01\x10" # column 1: tile 1, high priority vdp.vram[0x3F00:0x3F02] = b"\xff\xd0" # sprite 0 at y+1 = 0 vdp.vram[0x3F80:0x3F82] = b"\x04\x02" # x = 4, tile 2 vdp.cram[1], vdp.cram[16 + 3] = 0x03, 0x0C row = vdp._render_line(0) assert row[4] == sms_rgb(0x0C) # sprite over a low-priority tile assert row[9] == sms_rgb(0x03) # high-priority opaque tile over the sprite def test_mode4_left_column_mask(): vdp = VDP() mode4(vdp) set_register(vdp, 0, 0x26) set_register(vdp, 7, 0x05) vdp.cram[16 + 5] = 0x30 assert vdp._render_line(0)[:8] == [sms_rgb(0x30)] * 8 def test_mode4_ninth_sprite_sets_overflow_at_its_flag_time(): vdp = VDP() mode4(vdp) for index in range(9): vdp.vram[0x3F00 + index] = 0x09 # all on lines 10-17 vdp.vram[0x3F09] = 0xD0 advance_to(vdp, 9, 681) assert not vdp.status & STATUS_OVERFLOW advance_to(vdp, 10, 0) # line 10 begins: its sprites were parsed during line 9 assert vdp.status & STATUS_OVERFLOW def test_mode4_collision_needs_opaque_overlap(): vdp = VDP() mode4(vdp) vdp.vram[0x0000:0x0004] = b"\x80\x00\x00\x00" # tile 0 row 0: one opaque pixel vdp.vram[0x3F00:0x3F03] = b"\x04\x04\xd0" # sprites 0 and 1 on line 5 vdp.vram[0x3F80:0x3F84] = b"\x20\x00\x20\x00" # both at x = 32, tile 0 advance_to(vdp, 5, 0) advance_to(vdp, 5, 2 * 32 + 70 + 1) assert vdp.status & STATUS_COLLISION def test_sms1_zoom_widens_only_the_first_four_sprites(): for variant, widened in ((SEGA_315_5124, 4), (SEGA_315_5246, 6)): vdp = VDP(variant) mode4(vdp) set_register(vdp, 1, 0x41) # display on, zoom vdp.vram[0x0000:0x0004] = b"\xff\x00\x00\x00" for index in range(6): vdp.vram[0x3F00 + index] = 0xFF vdp.vram[0x3F80 + index * 2] = index * 40 vdp.vram[0x3F06] = 0xD0 sprites = vdp._mode4_sprites(0, [False] * 256) wide = sum(1 for index in range(6) if sprites[index * 40 + 12] is not None) assert wide == widened def test_tms_graphics_i_foreground_and_backdrop(): vdp = VDP(TMS9918A) set_register(vdp, 1, 0x40) set_register(vdp, 2, 0x06) # name table $1800 set_register(vdp, 3, 0x80) # colour table $2000 set_register(vdp, 4, 0x00) # patterns $0000 set_register(vdp, 5, 0x36) # sprites $1B00 set_register(vdp, 7, 0x04) vdp.vram[0x1B00] = 0xD0 vdp.vram[0x1800] = 0x00 vdp.vram[0x0000] = 0xF0 # pattern 0 row 0: left four pixels set vdp.vram[0x2000] = 0xF0 # foreground white, background transparent row = vdp._render_line(0) assert row[0] == TMS9918A_RGB[15] assert row[4] == TMS9918A_RGB[4] # transparent background shows the backdrop def test_tms_text_mode_starts_six_pixels_in(): # TMS9918A data manual, table 3-3: left border 13 pixels in the pattern modes # and 19 in text, right border 15 and 25. vdp = VDP(TMS9918A) set_register(vdp, 1, 0x50) set_register(vdp, 2, 0x06) set_register(vdp, 4, 0x00) set_register(vdp, 7, 0xF4) for column in range(40): vdp.vram[0x1800 + column] = 0x01 vdp.vram[0x0008] = 0xFC # pattern 1 row 0: all six pixels set row = vdp._render_line(0) assert row[:6] == [TMS9918A_RGB[4]] * 6 assert row[6:246] == [TMS9918A_RGB[15]] * 240 assert row[246:] == [TMS9918A_RGB[4]] * 10 def test_tms_fifth_sprite_number(): vdp = VDP(TMS9918A) set_register(vdp, 1, 0x40) set_register(vdp, 5, 0x36) for index in range(6): vdp.vram[0x1B00 + index * 4] = 0x09 vdp.vram[0x1B00 + 6 * 4] = 0xD0 vdp._render_line(10) assert vdp.status & STATUS_OVERFLOW assert vdp.fifth_sprite == 4 def test_game_gear_lcd_shows_the_central_160x144(): vdp = VDP(SEGA_GAME_GEAR) for height, top in ((192, 24), (224, 40)): vdp.frame = [[y * 1000 + x for x in range(256)] for y in range(height)] shown = vdp.visible_frame() assert (len(shown), len(shown[0])) == (144, 160) assert shown[0][0] == top * 1000 + 48 def test_vertical_scroll_holds_for_a_whole_active_display(): # msvdp section 9: a register 9 write during the active display waits for its # end; one made in the border or blanking (Streets of Rage, GG) shows next frame. vdp = VDP(SEGA_315_5246) advance_to(vdp, 100, 0) set_register(vdp, 9, 8) advance_to(vdp, 150, 0) assert vdp.y_scroll == 0 advance_to(vdp, 230, 0) # bottom blanking: stored, not yet shown set_register(vdp, 9, 16) advance_to(vdp, 5, 0) assert vdp.y_scroll == 16 def test_tms_transparent_sprite_lets_the_next_one_show_but_still_collides(): # Young 4.1: colour 0 is "transparent (not visible)"; 4.3: collision counts # pattern bits whatever the colour. Ping Pong's ball sits under such a sprite. vdp = VDP(TMS9918A) set_register(vdp, 1, 0x40) set_register(vdp, 5, 0x36) # sprite attributes at $1B00 set_register(vdp, 6, 0x07) # sprite patterns at $3800 vdp.vram[0x3800:0x3808] = b"\xff" * 8 for index, colour in enumerate((0, 6)): vdp.vram[0x1B00 + index * 4 : 0x1B04 + index * 4] = bytes([9, 20, 0, colour]) vdp.vram[0x1B08] = 0xD0 row = vdp._tms_sprites(10, [0] * 256) assert row[20:28] == [6] * 8 assert vdp.status & STATUS_COLLISION def test_zoomed_sprite_rows_compare_halved_lines(): # MAME 0.285's rule (VDP._mode4_sprite_row): Y = 151 zoomed shows pattern row 0 # on lines 151-152, then each row on two lines (Racer's score digits). rows = [VDP._mode4_sprite_row(line, 151, True) for line in range(150, 156)] assert rows == [-1, 0, 0, 1, 1, 2] assert [VDP._mode4_sprite_row(line, 150, True) for line in (150, 151, 152, 153)] == [ -1, 0, 0, 1, ] assert VDP._mode4_sprite_row(152, 151, False) == 0 def test_game_gear_tms_modes_colour_through_the_second_palette(): # The wiki's Palette.pmwiki: the GG maps TMS colours to palette indices and # does not set them; the indices are 16-31 (MAME; Justin Harder Reel). vdp = VDP(SEGA_GAME_GEAR) vdp.cram[2 * 21], vdp.cram[2 * 21 + 1] = 0x0F, 0x00 # colour 21: red assert vdp._tms_rgb(5) == vdp._cram_rgb(21) != vdp._cram_rgb(5) # The Mega Drive's 315-5313 in Mode 4 (msvdp section 13; FluBBa's Mega Drive tests). def test_mega_drive_first_control_byte_waits_for_the_second(): vdp = VDP(SEGA_315_5313) vdp.vram[0x1200:0x1202] = b"\x11\x22" vdp.vram[0x1234] = 0x33 set_address(vdp, 0x1200, 0) vdp.write_control(0x34) # alone: the address keeps its low byte assert vdp.read_data() == 0x11 assert vdp.read_data() == 0x22 vdp.write_control(0x34) vdp.write_control(0x12) # the pair sets it assert vdp.read_data() == 0x33 def test_mega_drive_data_writes_leave_the_read_buffer(): vdp = VDP(SEGA_315_5313) vdp.vram[0x1000] = 0x5A set_address(vdp, 0x1000, 0) # the buffer now holds $5A vdp.write_data(0x99) assert vdp.read_data() == 0x5A @pytest.mark.parametrize("code", [2, 3]) def test_mega_drive_data_writes_go_to_cram_when_code_bit_1_is_set(code): vdp = VDP(SEGA_315_5313) vdp.vram[0x0005] = 0x77 set_address(vdp, 0x0005, code) vdp.write_data(0x2A) assert vdp.vram[0x0005] == 0x77 assert vdp.cram[5] == 0x2A def test_mega_drive_register_number_has_five_bits(): vdp = VDP(SEGA_315_5313) for command, written in ((0x80, True), (0xA0, True), (0x90, False), (0xB0, False)): vdp.registers[0] = 0 vdp.write_control(0x14) vdp.write_control(command) assert (vdp.registers[0] == 0x14) is written, hex(command) def test_mega_drive_shows_black_in_the_tms9918a_modes(): vdp = VDP(SEGA_315_5313) set_register(vdp, 1, 0x40) # display on, Mode 4 not selected set_register(vdp, 7, 0x05) assert vdp._render_line(0) == [0] * 256 def test_mega_drive_sprites_do_not_zoom(): vdp = VDP(SEGA_315_5313) mode4(vdp) set_register(vdp, 1, 0x41) # display on, the zoom bit vdp.vram[0x0000:0x0004] = b"\xff\x00\x00\x00" vdp.vram[0x3F00:0x3F02] = b"\xff\xd0" vdp.vram[0x3F80] = 40 sprites = vdp._mode4_sprites(0, [False] * 256) assert sprites[47] is not None and sprites[48] is None # 8 pixels wide, not 16 assert all(pixel is None for pixel in vdp._mode4_sprites(8, [False] * 256)) def test_mega_drive_collision_shows_as_the_line_begins(): vdp = VDP(SEGA_315_5313) mode4(vdp) vdp.vram[0x0000:0x0004] = b"\x80\x00\x00\x00" vdp.vram[0x3F00:0x3F03] = b"\x04\x04\xd0" # sprites 0 and 1 on line 5 vdp.vram[0x3F80:0x3F84] = b"\xc8\x00\xc8\x00" # both at x = 200 advance_to(vdp, 5, 15) assert not vdp.status & STATUS_COLLISION advance_to(vdp, 5, 18) # FluBBa's two reads, one CPU cycle apart assert vdp.status & STATUS_COLLISION def test_mega_drive_no_overflow_with_the_display_off(): vdp = VDP(SEGA_315_5313) mode4(vdp) set_register(vdp, 1, 0x00) for index in range(9): vdp.vram[0x3F00 + index] = 0x04 vdp.vram[0x3F09] = 0xD0 advance_to(vdp, 8, 0) assert not vdp.status & STATUS_OVERFLOW