"""The picture rules, one at a time, on the renderer's pure functions and a bare board.""" from __future__ import annotations from gba_python import state from gba_python.bios import BIOS from gba_python.board import GBA from gba_python.ppu import objects from gba_python.ppu.background import DIRECT, Latch, affine_line, bitmap_line, text_line from gba_python.ppu.compose import blend, brighten, compose, darken def _board() -> GBA: rom = bytearray(0x200) rom[0:4] = (0xFE, 0xFF, 0xFF, 0xEA) # b . (ARM branch to itself) return GBA(BIOS(bytes(0x4000), "test", "none"), bytes(rom), skip_bios=True) def test_blend_rounds_and_saturates(): white, black = 0x7FFF, 0 # red and blue: (31*8 + 8) >> 4 = 16; green works in 6 bits (62 here, bit 15 clear): 31 >> 1 assert blend(white, black, 8, 8) == 16 << 10 | 15 << 5 | 16 assert blend(white, white, 16, 16) == 0x7FFF # saturates at 31 assert brighten(black, 16) == 0x7FFF assert darken(white, 16) == 0 assert brighten(black, 31) == brighten(black, 16) # EVY above 16 acts as 16 def test_text_bg_reads_its_map_and_flips(): vram = bytearray(0x18000) vram[0x0800:0x0802] = (1, 0x04) # map block 1, entry 0: tile 1, horizontal flip vram[0x0020:0x0024] = (0x21, 0x43, 0x65, 0x87) # tile 1, row 0: indices 1..8 pixels = text_line(vram, 0x0100, 0, 0, 0x10000, Latch()) assert pixels[:8] == [8, 7, 6, 5, 4, 3, 2, 1] assert text_line(vram, 0x0100, 3, 0, 0x10000, Latch())[:5] == [5, 4, 3, 2, 1] def test_affine_bg_wraps_only_when_asked(): vram = bytearray(0x18000) vram[0x4000] = 0 # map entry 0 -> tile 0 vram[0:64] = bytes([9] * 64) start = -8 << 8 # eight dots left of the map clipped = affine_line(vram, 0x0800, start, 0, 0x100, 0, Latch()) # map at 6004000 assert clipped[:8] == [0] * 8 and clipped[8] == 9 wrapped = affine_line(vram, 0x2800, start, 0, 0x100, 0, Latch()) assert wrapped[0] == 9 def test_mode3_is_direct_colour_and_transparent_outside(): vram = bytearray(0x18000) vram[0:2] = (0x1F, 0x00) pixels = bitmap_line(vram, 3, 0, 0, 0, 0x100, 0) assert pixels[0] == 0x1F | DIRECT assert bitmap_line(vram, 3, 0, -1 << 8, 0, 0x100, 0)[0] == 0 def test_obj_in_front_of_bg_of_equal_priority_and_semi_transparent_blends(): palette = bytearray(0x400) palette[2:4] = (0x1F, 0) # BG colour 1: red palette[0x202:0x204] = (0x00, 0x7C) # OBJ colour 1: blue line = objects.ObjLine() line.color[0] = 1 line.priority[0] = 1 out = [0] * 240 layers = [(0, 1, False, [1] * 240)] compose( palette=palette, layers=layers, objects=line, window_masks=None, bldcnt=0, bldalpha=0, bldy=0, mosaic=0, out=out, ) assert out[0] == 0x7C00 and out[1] == 0x1F line.alpha[0] = True # semi-transparent: blends with a second target whatever BLDCNT's mode compose( palette=palette, layers=layers, objects=line, window_masks=None, bldcnt=0x0100, bldalpha=0x0808, bldy=0, mosaic=0, out=out, ) assert out[0] == blend(0x7C00, 0x1F, 8, 8) def test_window_masks_hide_layers_and_effects(): palette = bytearray(0x400) palette[2:4] = (0x1F, 0) out = [0] * 240 masks = [0x01] * 120 + [0x21] * 120 # left: BG0 only; right: BG0 and effects compose( palette=palette, layers=[(0, 0, False, [1] * 240)], objects=None, window_masks=masks, bldcnt=0x0081, bldalpha=0, bldy=16, mosaic=0, out=out, ) assert out[0] == 0x1F # no effect outside the effect window assert out[200] == 0x7FFF # BG0 brightened fully by EVY 16 masks = [0x00] * 240 compose( palette=palette, layers=[(0, 0, False, [1] * 240)], objects=None, window_masks=masks, bldcnt=0, bldalpha=0, bldy=0, mosaic=0, out=out, ) assert out[0] == 0 # BG0 hidden: the backdrop (palette 0, black) def test_dispcnt_bg_enable_takes_two_lines(): gba = _board() ppu = gba.ppu while ppu.vcount != 10: gba.run(1) # stop at the start of line 10 gba.run(200) # past this line's latch at cycle 40 ppu.write_register(0x00, 0x0100, 0xFFFF) # BG0 on, during line 10 seen = {} while ppu.vcount < 15: line = ppu.vcount while ppu.vcount == line: gba.run(1) gba.run(60) # past cycle 46: the line has been drawn seen[ppu.vcount] = bool(ppu.dispcnt_latch[0] & 0x100) # the latch shifts at cycle 40 of lines 11, 12, 13: BG0 shows from line 13 assert seen == {11: False, 12: False, 13: True, 14: True, 15: True} def test_affine_reference_latches_at_line_start_and_steps_by_pb_pd(): gba = _board() ppu = gba.ppu while ppu.vcount != 20: gba.run(1232) ppu.write_register(0x00, 0x0402, 0xFFFF) # mode 2, BG2 on ppu.write_register(0x22, 0x0010, 0xFFFF) # BG2PB = 1/16 ppu.write_register(0x28, 0x0300, 0xFFFF) # BG2X = 3.0 ppu.write_register(0x2A, 0x0000, 0xFFFF) assert ppu.affine_current[0][0] != 0x300 # not before the next line starts while ppu.vcount != 21: gba.run(1) assert ppu.affine_current[0][0] == 0x300 def test_obj_engine_draws_a_regular_8x8(): oam = bytearray(0x400) vram = bytearray(0x18000) oam[0:6] = (10, 0, 20, 0, 0, 0) # y 10, x 20, 8x8, tile 0, priority 0 for i in range(1, 128): oam[i * 8] = 160 # off screen oam[i * 8 + 1] = 0x02 # disabled vram[0x10000:0x10004] = (0x11, 0x11, 0x11, 0x11) line = objects.evaluate(oam, vram, 10, 0x1040, 0) assert line.color[19] == 0 and line.color[20:28] == [1] * 8 and line.color[28] == 0 assert objects.evaluate(oam, vram, 18, 0x1040, 0).color[20] == 0 def test_window_with_left_past_right_wraps_through_the_line_edge(): ppu = _board().ppu ppu.registers[0x40 >> 1] = 200 << 8 | 50 # WIN0H: left 200, right 50 ppu.window_v = [True, False] ppu.window_h = [False, False] def run_line() -> list[bool]: ppu.line_start = ppu.scheduler.now ppu.window_cycle = 0 ppu._sync_window(ppu.line_start + 1024) return list(ppu.window_buffer[0]) first = run_line() assert not any(first[:200]) and all(first[200:]) # the flag rose at 200 and stays up second = run_line() # it carried over: dots 0-49 are inside too assert all(second[:50]) and not any(second[50:200]) and all(second[200:]) def test_window_edge_written_mid_line_moves_only_the_dots_after_it(): ppu = _board().ppu ppu.registers[0x40 >> 1] = 0 << 8 | 240 # WIN0H: the whole line ppu.window_v = [True, False] ppu.window_h = [False, False] ppu.line_start = ppu.scheduler.now ppu.window_cycle = 0 ppu._sync_window(ppu.line_start + 4 * 100) # dots 0-99 run ppu.registers[0x40 >> 1] = 0 << 8 | 120 # the right edge moves to 120 ppu._sync_window(ppu.line_start + 1024) assert all(ppu.window_buffer[0][:120]) and not any(ppu.window_buffer[0][120:]) # ----- dot-exact (rung 4) ------------------------------------------------------------------ def _mode3_board(line: int) -> GBA: """A board showing mode 3 (row 0 of the bitmap on every line: PD is 0), run to the start of ``line``. Row 0 alternates red and green dots.""" gba = _board() ppu = gba.ppu ppu.write_register(0x00, 0x0403, 0xFFFF) # mode 3, BG2 on ppu.write_register(0x20, 0x0100, 0xFFFF) # PA = 1.0 ppu.write_register(0x26, 0x0000, 0xFFFF) # PD = 0 (skip_bios leaves the BIOS's 1.0) for x in range(240): color = 0x001F if x % 2 == 0 else 0x03E0 ppu.vram[2 * x : 2 * x + 2] = color.to_bytes(2, "little") scheduler = gba.scheduler while not (ppu.vcount == line and scheduler.now == ppu.line_start): scheduler.run_until(scheduler.now + 1) return gba def test_green_swap_written_mid_line_reaches_only_the_dots_after_it(): gba = _mode3_board(10) ppu, scheduler = gba.ppu, gba.scheduler start = ppu.line_start scheduler.run_until(start + 46 + 4 * 100) # the merge is at dot 100 ppu.write_register(0x02, 1, 0xFFFF) # GREENSWAP on scheduler.run_until(start + 1232 + 1) row = ppu._drawing[10 * 240 : 11 * 240] assert (row[0], row[1]) == (0x001F, 0x03E0) # before the write: as drawn assert (row[200], row[201]) == (0x03FF, 0x0000) # after it: the pair's greens swapped def test_a_cpu_vram_read_waits_while_the_bg_engine_fetches(): gba = _mode3_board(10) start = gba.ppu.line_start gba.scheduler.run_until(start + 33 + 4 * 50) gba.bus_read(0x06000000, 16, 0) # its cycle, 33 + 4 * 50, has no BG fetch assert gba.scheduler.now == start + 34 + 4 * 50 gba.bus_read(0x06000000, 16, 0) # its cycle is the bitmap fetch for dot 50: one more assert gba.scheduler.now == start + 36 + 4 * 50 def test_state_saved_mid_line_on_the_dot_engine_runs_on_identically(): gba = _mode3_board(10) ppu, scheduler = gba.ppu, gba.scheduler scheduler.run_until(ppu.line_start + 46 + 4 * 100) ppu.write_register(0x02, 1, 0xFFFF) assert ppu.dot is not None copy = state.load(state.save(gba)) for board in (gba, copy): board.scheduler.run_until(board.ppu.line_start + 1232 + 1) assert gba.ppu._drawing == copy.ppu._drawing