"""The board's command debugger (``gba_python.console``) on a small synthetic program. The CPU commands are arm7tdmi-python's and tested there; these check what the board adds: one step per board boundary in system clocks, the side-effect-free peek, the I/O and video views, the board breakpoints, frames, pictures and states. """ from __future__ import annotations import struct import zlib from gba_python import GBA from gba_python.bios import BIOS from gba_python.console import BoardDebugger, layer_rgb, peek_byte BRANCH_TO_SELF = (0xEAFFFFFE).to_bytes(4, "little") #: assembled with the pinned Arm GNU toolchain (arm-none-eabi-as -march=armv4t) PROGRAM = ( 0xE3A00301, # mov r0, #0x04000000 0xE3A01C01, # mov r1, #0x100 0xE1C010B0, # strh r1, [r0] DISPCNT: mode 0, BG0 0xE3A01008, # mov r1, #8 0xE1C010B4, # strh r1, [r0, #4] DISPSTAT: VBlank IRQ 0xE2802C02, # add r2, r0, #0x200 0xE3A01001, # mov r1, #1 0xE1C210B0, # strh r1, [r2] IE: VBlank 0xE1C210B8, # strh r1, [r2, #8] IME 0xE28030D4, # add r3, r0, #0xD4 0xE3A01302, # mov r1, #0x08000000 0xE5831000, # str r1, [r3] DMA3SAD 0xE3A01402, # mov r1, #0x02000000 0xE5831004, # str r1, [r3, #4] DMA3DAD 0xE3A01004, # mov r1, #4 0xE1C310B8, # strh r1, [r3, #8] DMA3CNT_L: 4 units 0xE3A01B21, # mov r1, #0x8400 0xE1C310BA, # strh r1, [r3, #10] DMA3CNT_H: enable, 32-bit, immediate 0xEAFFFFFE, # b . ) LOOP = 0x08000000 + 4 * (len(PROGRAM) - 1) def board() -> GBA: bios = BIOS(BRANCH_TO_SELF * 0x1000, "synthetic", "unknown") return GBA(bios, struct.pack(f"<{len(PROGRAM)}I", *PROGRAM), skip_bios=True) def run(debugger: BoardDebugger, line: str) -> list[str]: return list(debugger.execute(line).lines) def test_peek_reads_memory_without_clocks(): gba = board() now = gba.scheduler.now gba.ewram[5], gba.iwram[6], gba.ppu.palette[7] = 0x11, 0x22, 0x33 assert peek_byte(gba, 0x02040005) == 0x11 # EWRAM mirror assert peek_byte(gba, 0x03000006) == 0x22 assert peek_byte(gba, 0x05000007) == 0x33 assert peek_byte(gba, 0x08000003) == 0xE3 assert peek_byte(gba, 0x04000006) == gba.ppu.vcount # VCOUNT through its read assert peek_byte(gba, 0x04000010) == 0 # BG0HOFS is write-only assert gba.scheduler.now == now def test_steps_are_board_boundaries_in_system_clocks(): debugger = BoardDebugger(board()) lines = run(debugger, "s") assert lines[0].startswith("#0 08000000 MOV R0, #0x4000000") assert debugger.session.history[-1].cycles > 1 # a ROM fetch has wait states assert run(debugger, "until 0x0800000C")[0].startswith("until:") vcount = f"VCOUNT={debugger.gba.ppu.vcount:04X}" assert run(debugger, "io DISPCNT 0x006") == ["DISPCNT=0100", vcount] def test_io_shows_written_values_dma_and_timers(): gba = board() debugger = BoardDebugger(gba) run(debugger, f"until {LOOP:#x}") run(debugger, "s") # the DMA takes the bus two cycles after its enable: during B . summary = run(debugger, "io") assert summary[0].startswith("DISPCNT=0100 DISPSTAT=") assert "IE=0001" in summary[2] and "IME=0001" in summary[2] assert any(line.startswith("DMA3 off CNT_H=0400 SAD=08000000 DAD=02000000") for line in summary) assert run(debugger, "io BG0HOFS DMA3SAD") == ["BG0HOFS=0000w", "DMA3SAD=08000000w"] assert gba.ewram[:4] == bytes.fromhex("0103a0e3") # the DMA copied the first word def test_break_dma_irq_line_and_frames(): debugger = BoardDebugger(board()) assert run(debugger, "break dma") == ["break on dma"] lines = run(debugger, "c") assert lines[0].startswith("stopped: a DMA transfer ran in the last step") # The immediate DMA starts two cycles after the STRH that enables it: in the next step. assert debugger.session.history[-1].address == LOOP run(debugger, "delete dma") run(debugger, "break line 100") assert run(debugger, "c")[0].startswith("stopped: line 100 started") assert run(debugger, "video")[0].startswith(f"frame {debugger.gba.ppu.frame} VCOUNT 100") run(debugger, "break irq") assert run(debugger, "breakpoints") == ["break irq", "break line 100"] assert run(debugger, "c")[0].startswith("stopped: IRQ entry (IE 0001 IF 0001)") assert debugger.gba.ppu.vcount == 160 run(debugger, "delete irq") run(debugger, "delete line") frame = debugger.gba.ppu.frame assert run(debugger, "frame 2")[0].startswith(f"stopped: frame {frame + 2} ended") run(debugger, "break self") assert run(debugger, "c")[0].startswith("stopped: a branch to itself: 0 steps") def test_pictures_layers_and_states(tmp_path): gba = board() debugger = BoardDebugger(gba) run(debugger, "break 0x08000004") gba.ppu.registers[4] = 8 << 8 # BG0CNT: map in screen block 8 (4000h), tiles at 0 gba.ppu.palette[2:4] = (0x1F, 0) # colour 1: red gba.ppu.vram[0x20:0x40] = b"\x11" * 32 # tile 1: colour 1 gba.ppu.vram[0x4000:0x4002] = b"\x01\x00" # map entry (0, 0): tile 1 run(debugger, f"until {LOOP:#x}") width, height, rgb, kind = layer_rgb(gba, 0) assert (width, height, kind) == (256, 256, "text") assert rgb[:3] == b"\xff\x00\x00" and rgb[8 * 3 : 8 * 3 + 3] == b"\x00\x00\x00" lines = run(debugger, f"layer 0 {tmp_path / 'bg0.png'}") assert lines[0].startswith("wrote BG0 (text, 256x256)") png = (tmp_path / "bg0.png").read_bytes() assert png[:8] == b"\x89PNG\r\n\x1a\n" and zlib.crc32(png) != 0 run(debugger, "frame") lines = run(debugger, f"png {tmp_path / 'frame.png'}") assert lines[0].startswith("wrote the last finished frame (1 so far)") saved = tmp_path / "console.state" assert run(debugger, f"state save {saved}") == [f"saved the console to {saved}"] pc, now = gba.cpu.r[15], gba.scheduler.now run(debugger, "frame") lines = run(debugger, f"state load {saved}") assert lines[0].startswith(f"loaded {saved}") assert debugger.gba is not gba and debugger.gba.scheduler.now == now assert debugger.gba.cpu.r[15] == pc assert run(debugger, "breakpoints")[0] == "break 08000004" # kept across the load assert run(debugger, "s")[0].startswith(f"#{debugger.session.total_steps - 1}") HALTING = ( 0xE3A00301, # mov r0, #0x04000000 0xE3A01008, # mov r1, #8 0xE1C010B4, # strh r1, [r0, #4] DISPSTAT: VBlank IRQ 0xE2802C02, # add r2, r0, #0x200 0xE3A01001, # mov r1, #1 0xE1C210B0, # strh r1, [r2] IE: VBlank (IME stays 0: wake, no IRQ) 0xE1A0E00F, # mov lr, pc 0xE3A0F000, # mov pc, #0 the BIOS's halt routine 0xEAFFFFFE, # b . ) HALT_BIOS = ( 0xE3A00301, # mov r0, #0x04000000 0xE3A01000, # mov r1, #0 0xE5C01301, # strb r1, [r0, #0x301] HALTCNT (only from the BIOS) 0xE1A0F00E, # mov pc, lr ) def test_the_halting_instruction_runs_the_halt(): bios = BIOS(struct.pack("<4I", *HALT_BIOS) + BRANCH_TO_SELF * 0xFFC, "synthetic", "unknown") gba = GBA(bios, struct.pack(f"<{len(HALTING)}I", *HALTING), skip_bios=True) debugger = BoardDebugger(gba) run(debugger, "break halt") assert run(debugger, "c")[0].startswith("stopped: the last step halted the CPU") record = debugger.session.history[-1] assert record.address == 0x00000008 and record.cycles > 1000 # STRB, then the HALT assert not gba.halted and gba.ppu.vcount == 160 # woken by VBlank assert run(debugger, "s")[0].startswith("#") # the next step is the next instruction assert debugger.session.history[-1].address == 0x0000000C