# SPDX-License-Identifier: MIT """Rung 4's dot model, driven through the bus at chosen cycles (docs/video.md "The dot model as built"). These guard the model's mechanics; the hardware judges are in docs/validation.md "Rung 4".""" from __future__ import annotations from programs import board_for from gba_python.ppu import dots as dots_module from gba_python.ppu import timing as timing_module LINE = timing_module.LINE_CYCLES def board(): return board_for(b"\x00" * 0x100) def catch_up(b, time): b.bus.now = time b.run_events() def origin(line: int, frame: int = 1) -> int: """The board cycle of a line's origin (power-on is cycle POWER_ON of line 0).""" return (frame * timing_module.LINES + line) * LINE - timing_module.POWER_ON def mode3_board(): b = board() ppu = b.ppu ppu.write16(0x00, 0x0403, 0xFFFF, 0) # mode 3, BG2 on ppu.write16(0x20, 0x0100, 0xFFFF, 0) # BG2PA = 1.0 for x in range(240): b.bus.vram[x * 2] = 0x1F # red in row 0 of the bitmap return b def test_vcount_changes_at_the_origin_and_the_flag_a_cycle_later(): b = board() catch_up(b, origin(5) - 1) b.ppu.write16(0x04, 5 << 8, 0xFF00, b.bus.now) # LYC = 5 assert b.ppu.read16(0x06) == 4 catch_up(b, origin(5)) assert b.ppu.read16(0x06) == 5 assert not b.ppu.read16(0x04) & 4 catch_up(b, origin(5) + 1) assert b.ppu.read16(0x04) & 4 def test_a_mid_line_vram_write_reaches_only_later_dots(): b = mode3_board() catch_up(b, origin(0, 2)) # a few frames so the display latch is on y0 = origin(0, 3) catch_up(b, y0) at = y0 + dots_module.FETCH_START + 4 * 100 # dot 100's fetch cycle b.bus.write_region(6, 0x06000000 + 2 * 150, 16, 0x03E0, at) # green at bitmap x=150, row 0 b.bus.write_region(6, 0x06000000 + 2 * 50, 16, 0x03E0, at) # and at x=50 (already drawn) catch_up(b, origin(1, 3)) line = b.ppu.frame[0] assert line[50] == 0x1F # fetched before the write assert line[150] == 0x03E0 # fetched after it def test_lines_without_writes_match_the_line_renderer(): b = mode3_board() catch_up(b, origin(0, 3)) expected = [0] * 240 b.ppu.renderer.render(0, expected) catch_up(b, origin(1, 3)) assert b.ppu.frame[0] == expected def test_window_vertical_flag_survives_a_y1_written_after_its_line(): b = board() ppu = b.ppu ppu.write16(0x00, 0x2000, 0xFFFF, 0) # window 0 on ppu.write16(0x44, (10 << 8) | 20, 0xFFFF, 0) # Y1 = 10, Y2 = 20 catch_up(b, origin(15)) assert ppu.dots.win_v[0] ppu.write16(0x44, (12 << 8) | 20, 0xFFFF, origin(15) + 100) # Y1 moves above the line catch_up(b, origin(16)) assert ppu.dots.win_v[0] # still set until line 20 catch_up(b, origin(20)) assert not ppu.dots.win_v[0] def test_one_wait_on_obj_vram_while_an_obj_is_read(): b = board() ppu = b.ppu ppu.write16(0x00, 0x1000, 0xFFFF, 0) # OBJs on oam = b.bus.oam for n in range(1, 128): oam[n * 8 + 1] = 0x02 # disabled oam[0:6] = bytes((0, 0, 0, 0xC0, 0, 0)) # OBJ 0: y 0, 64x64, x 0 start = origin(0, 2) + dots_module.OBJ_START catch_up(b, origin(0, 2)) b.bus.now = start + 5 b.bus.video_wait(6, 0x06010000, 16) assert b.bus.now == start + 6 # one wait, though the fetch goes on b.bus.now = start + 64 + 5 b.bus.video_wait(6, 0x06010000, 16) assert b.bus.now == start + 64 + 5 # the 64 cycles are over