"""Quick checks of the board's rules, each against the source that states it. These run without the retail BIOS or any test ROM: a synthetic BIOS (a branch to itself) and a small synthetic cartridge. The test ROMs in ``test_roms.py`` (slow) are the judges; these pin the rules one by one so a regression points at its rule. """ from __future__ import annotations import pytest from gba_python import GBA, state from gba_python.bios import BIOS from gba_python.dma import DMA_ACCESS from gba_python.keypad import START, A from gba_python.testrom import hades_verdict, hwtest_verdict SEQ, CODE = 1, 2 BRANCH_TO_SELF = (0xEAFFFFFE).to_bytes(4, "little") def make(rom: bytes | None = None, **kwargs) -> GBA: bios = BIOS(BRANCH_TO_SELF * 0x1000, "synthetic", "unknown") rom = rom if rom is not None else BRANCH_TO_SELF * 0x100 return GBA(bios, rom, skip_bios=True, **kwargs) def clocks(gba: GBA, action) -> int: start = gba.scheduler.now action() return gba.scheduler.now - start # ----- memory map and wait states (GBATEK "GBA Memory Map", "WAITCNT") ---------------------- def test_iwram_and_ewram_mirror(): gba = make() gba.bus_write(0x03000010, 32, 0x12345678, 0) assert gba.bus_read(0x03FF8010, 32, 0) == 0x12345678 gba.bus_write(0x02000010, 16, 0xBEEF, 0) assert gba.bus_read(0x02FC0010, 16, 0) == 0xBEEF def test_ewram_and_iwram_access_times(): gba = make() assert clocks(gba, lambda: gba.bus_read(0x02000000, 16, 0)) == 3 assert clocks(gba, lambda: gba.bus_read(0x02000000, 32, 0)) == 6 assert clocks(gba, lambda: gba.bus_read(0x03000000, 32, 0)) == 1 assert clocks(gba, lambda: gba.bus_read(0x05000000, 32, 0)) == 2 def test_rom_waits_at_power_on_and_after_waitcnt(): gba = make(bytes(range(256)) * 0x400) gba.bus_read(0x08000100, 16, 0) assert clocks(gba, lambda: gba.bus_read(0x08000200, 16, 0)) == 5 # N: 1 + 4 assert clocks(gba, lambda: gba.bus_read(0x08000202, 16, SEQ)) == 3 # S: 1 + 2 assert clocks(gba, lambda: gba.bus_read(0x08000204, 32, SEQ)) == 6 # S + S assert clocks(gba, lambda: gba.bus_read(0x08000300, 32, 0)) == 8 # N + S gba.bus_write(0x04000204, 16, 0x0014, 0) # WS0 3/1 waits assert clocks(gba, lambda: gba.bus_read(0x08000400, 16, 0)) == 4 assert clocks(gba, lambda: gba.bus_read(0x08000402, 16, SEQ)) == 2 def test_128k_boundary_forces_n_for_the_cpu(): gba = make(bytes(0x40000)) gba.bus_read(0x0801FFFC, 32, 0) assert clocks(gba, lambda: gba.bus_read(0x08020000, 32, SEQ)) == 8 def test_dma_boundary_follows_its_previous_access(): # hw-test/bus/128kb-boundary: a decrementing DMA that reads 8020000 S stays S; the access # after one ending at the boundary is N. gba = make(bytes(0x40000)) gba.bus_read(0x08020004, 32, DMA_ACCESS) assert clocks(gba, lambda: gba.bus_read(0x08020000, 32, DMA_ACCESS | SEQ)) == 6 gba.bus_read(0x0801FFFC, 32, DMA_ACCESS | SEQ) assert clocks(gba, lambda: gba.bus_read(0x0801FFF8, 32, DMA_ACCESS | SEQ)) == 8 def test_rom_reads_past_the_end_return_the_address(): gba = make(bytes(0x100)) assert gba.bus_read(0x08001000, 16, 0) == 0x0800 assert gba.bus_read(0x08001000, 32, 0) == 0x08010800 # ----- 8-bit writes and video memory (quirks 4, 5) ------------------------------------------- def test_8bit_writes_to_video_memory(): gba = make() gba.bus_write(0x05000001, 8, 0xAB, 0) assert gba.bus_read(0x05000000, 16, 0) == 0xABAB gba.bus_write(0x06000003, 8, 0xCD, 0) # BG VRAM: both bytes assert gba.bus_read(0x06000002, 16, 0) == 0xCDCD gba.bus_write(0x06010000, 8, 0xEF, 0) # OBJ VRAM: ignored assert gba.bus_read(0x06010000, 16, 0) == 0 gba.bus_write(0x07000000, 8, 0x12, 0) # OAM: ignored assert gba.bus_read(0x07000000, 16, 0) == 0 def test_vram_upper_mirror_and_bitmap_hole(): gba = make() gba.bus_write(0x06010000, 16, 0x1234, 0) assert gba.bus_read(0x06018000, 16, 0) == 0x1234 gba.bus_write(0x04000000, 16, 3, 0) # mode 3: OBJ tiles start at 6014000 assert gba.bus_read(0x06018000, 16, 0) == 0 # ----- I/O reads (quirk 3) and open bus (quirk 2) -------------------------------------------- def test_io_read_masks_and_unused_halves(): gba = make() gba.bus_write(0x04000008, 16, 0xFFFF, 0) assert gba.bus_read(0x04000008, 16, 0) == 0xDFFF # BG0CNT: no wrap bit gba.bus_write(0x04000084, 16, 0x0080, 0) gba.bus_write(0x04000064, 16, 0xFFFF, 0) assert gba.bus_read(0x04000064, 16, 0) == 0x4000 assert gba.bus_read(0x04000066, 16, 0) == 0 # its word has a readable half def test_joycnt_flags_are_acknowledged_by_writing_one(): # GBATEK "JOYCNT": bits 0-2 are event flags cleared by writing 1 (as IF), bit 6 the reset # IRQ enable, the rest unused. The BIOS writes 7 to acknowledge; it must not read back 7. gba = make() gba.bus_write(0x04000140, 16, 0x0007, 0) assert gba.bus_read(0x04000140, 16, 0) == 0 gba.bus_write(0x04000140, 16, 0xFFFF, 0) assert gba.bus_read(0x04000140, 16, 0) == 0x40 gba.serial.joy[0] |= 0b101 # a device reset and a send completed gba.bus_write(0x04000140, 16, 0x0041, 0) assert gba.bus_read(0x04000140, 16, 0) == 0x44 def test_write_only_word_reads_open_bus(): gba = make() opcode = gba.cpu.pipeline[1] assert gba.bus_read(0x04000010, 32, 0) == opcode # BG0HOFS/BG0VOFS: write-only assert gba.bus_read(0x00004000, 32, 0) == opcode # past the BIOS assert gba.bus_read(0x10000000, 32, 0) == opcode # A28-A31 not connected def test_bios_reads_from_outside_see_its_last_word(): gba = make() gba.bios_latch = 0xE129F000 assert gba.bus_read(0x00000000, 32, 0) == 0xE129F000 # PC is in the cartridge assert gba.bus_read(0x00000002, 16, 0) == 0xE129 def test_data_stays_on_the_bus_for_the_next_open_bus_read(): gba = make() gba.bus_write(0x03000000, 32, 0x51AEFF24, 0) gba.bus_write(0x03000004, 32, 0x08070605, 0) gba.bus_read(0x03000000, 32, 0) gba.bus_read(0x03000004, 16, 0) # IWRAM's bus: only the low lanes change assert gba.bus_read(0x04001000, 32, 0) == 0x51AE0605 gba.bus_write(0x07000004, 32, 0x08070605, 0) gba.bus_read(0x07000004, 16, 0) # OAM's bus is the whole word assert gba.bus_read(0x04001000, 32, 0) == 0x08070605 gba.bus_read(0x08000005, 8, 0) # the Game Pak's 16 bits, on both halves low = gba.bus_read(0x08000004, 16, 0) assert gba.bus_read(0x04001000, 32, 0) == low * 0x10001 gba.bus_read(0x03000004, 16, 2) # an opcode fetch: the prefetch rule again assert gba.bus_read(0x04001000, 32, 0) != 0x51AE0605 def test_an_eeprom_command_lives_within_one_transfer(): from gba_python.cartridge.backup import ACCEPT_COMMAND, EEPROM from gba_python.scheduler import Scheduler def stream(chip, bits): # one DMA: a new transfer, then sequential bits chip.select() for bit in bits: chip.write(bit) chip = EEPROM(Scheduler(), bytes(0x200)) for bit in (1, 0, 1): # three CPU writes: three transfers, three dropped commands chip.select() chip.write(bit) request = [1, 1] + [0] * 6 + [0] # read block 0 stream(chip, request) assert chip.state != ACCEPT_COMMAND # the read is waiting to be clocked out chip.select() # the reading DMA: a new transfer, but the finished command stays assert [chip.read() for _ in range(68)][4:] == [0] * 64 chip.select() chip.write(1) # half a command, then a new transfer drops it chip.select() assert chip.state == ACCEPT_COMMAND and chip.bits == 0 def test_sram_reads_replicate_the_byte(): gba = make(b"SRAM_V113" + bytes(0x100)) gba.bus_write(0x0E000001, 8, 0x5A, 0) assert gba.bus_read(0x0E000001, 32, 0) == 0x5A5A5A5A gba.bus_write(0x0E000002, 32, 0x44332211, 0) # the lane the address selects assert gba.bus_read(0x0E000002, 8, 0) == 0x33 # ----- timers, IRQ, DMA: the cycle rules of docs/timing.md --------------------------------- def irq_ready(): gba = make() gba.bus_write(0x04000208, 16, 1, 0) # IME gba.bus_write(0x04000200, 16, 0xFFFF, 0) # IE gba.step(4) return gba def start_dma(gba, units=8): for offset, value in ((0xD4, 0x03000000), (0xD8, 0x03000100)): gba.bus_write(0x04000000 + offset, 32, value, 0) gba.bus_write(0x040000DC, 32, 0x84000000 | units, 0) # DMA3, 32-bit, immediate gba.step(2) assert gba.dma.running def test_irq_input_follows_a_source_two_cycles_later_and_the_cpu_looks_at_an_access(): gba = irq_ready() gba.irq.raise_(3) gba.step(1) assert not gba.irq.level gba.step(1) assert gba.irq.level assert not gba.cpu.irq # the CPU has not begun a bus cycle since gba._cpu_idle(0) assert gba.cpu.irq assert gba.bus_read(0x04000202, 16, 0) == 1 << 3 def test_the_cpu_looks_at_the_start_of_an_access_not_its_end(): gba = irq_ready() gba.irq.raise_(3) gba.step(1) gba._cpu_read(0x08000000, 16, 0) # several cycles: the input rises during them assert gba.irq.level assert not gba.cpu.irq gba._cpu_read(0x08000002, 16, 0) assert gba.cpu.irq def test_the_synchronizer_stops_while_the_cpu_waits_for_a_dma(): gba = irq_ready() start_dma(gba) gba.irq.raise_(3) began = gba.scheduler.now gba._cpu_read(0x03000000, 32, 0) # the DMA runs first; then the read's one cycle assert gba.scheduler.now - began > 16 assert not gba.cpu.irq # the synchronizer's cycle is the read itself assert gba.irq.level def test_the_synchronizer_runs_through_internal_cycles_a_dma_overlaps(): for idles, taken in ((1, False), (2, True)): gba = irq_ready() start_dma(gba) gba.irq.raise_(3) for _ in range(idles): gba._cpu_idle(0) # the first starts the DMA; they all run beside it gba._cpu_read(0x03000000, 32, 0) assert gba.cpu.irq is taken, idles assert gba.irq.level def test_timer_counts_from_its_start_and_reloads(): gba = make() gba.bus_write(0x04000100, 16, 0xFFF0, 0) gba.bus_write(0x04000102, 16, 0x80, 0) gba.step(2) # the reload and the start land a cycle after their writes assert gba.bus_read(0x04000100, 16, 0) in (0xFFF0, 0xFFF1) gba.step(40) assert 0xFFF0 <= gba.bus_read(0x04000100, 16, 0) <= 0xFFFF def test_immediate_dma_waits_two_cycles(): gba = make() gba.bus_write(0x03000000, 32, 0xCAFEF00D, 0) for offset, value in ((0xD4, 0x03000000), (0xD8, 0x03000100)): gba.bus_write(0x04000000 + offset, 32, value, 0) gba.bus_write(0x040000DC, 32, 0x84000001, 0) # DMA3, 32-bit, immediate, 1 unit assert not gba.dma.running gba.step(2) assert gba.dma.running gba.dma.run() assert gba.bus_read(0x03000100, 32, 0) == 0xCAFEF00D assert gba.bus_read(0x040000DE, 16, 0) & 0x8000 == 0 # the enable bit cleared itself def test_keypad_irq_any_and_all(): gba = make() gba.keypad.write_control(0x4000 | A | START, 0xFFFF) gba.keypad.set_pressed(A) assert gba.irq.pending_if & 1 << 12 gba.irq.pending_if = 0 gba.keypad.write_control(0xC000 | A | START, 0xFFFF) gba.keypad.set_pressed(A) assert not gba.irq.pending_if gba.keypad.set_pressed(A | START) assert gba.irq.pending_if & 1 << 12 # AND is "all selected down", not "exactly the selected down" (GBATEK; keyirq steps 2, 12). gba.irq.pending_if = 0 gba.keypad.write_control(0xC000 | A, 0xFFFF) gba.keypad.set_pressed(A | START) assert gba.irq.pending_if & 1 << 12 # ----- tooling ------------------------------------------------------------------------------ def test_state_round_trip_runs_identically(): a = make() a.run(10_000) b = state.load(state.save(a)) a.run(50_000) b.run(50_000) assert a.scheduler.now == b.scheduler.now assert a.cpu.r == b.cpu.r assert a.ppu.vcount == b.ppu.vcount @pytest.mark.parametrize( ("text", "expected"), [ (["A: PASS 1", "", "congratulations!"], (True, "congratulations!")), (["A: FAIL 2", " expected: 1", "", "pass: 0", "total: 1"], (False, "0/1")), (["LDM: PASS 38", "DMA: PASS 57"], (True, "2/2 visible")), ([""], (None, "no verdict on screen")), ], ) def test_hwtest_verdict(text, expected): assert hwtest_verdict(text) == expected def test_hades_verdict(): assert hades_verdict(["01: PASS", "Total: 10/12"]) == (False, "10/12") def test_rtc_reads_its_start_date_over_gpio(): from datetime import datetime rom = b"SIIRTC_V" + bytes(0x100) bios = BIOS(BRANCH_TO_SELF * 0x1000, "synthetic", "unknown") gba = GBA(bios, rom, skip_bios=True, rtc_start=datetime(2004, 11, 21, 13, 45, 30)) def port(offset, value): gba.bus_write(0x08000000 + offset, 16, value, 0) port(0xC8, 1) # readable port(0xC6, 7) # SCK, SIO, CS out port(0xC4, 0) port(0xC4, 4) # CS rises: a command follows command = 0xA6 # 0110 010 1: read date and time, least significant bit first for bit in range(8): sio = command >> bit & 1 port(0xC4, 4 | sio << 1) port(0xC4, 4 | sio << 1 | 1) # SCK rising edge port(0xC6, 5) # SIO in data = [] for _ in range(7): byte = 0 for bit in range(8): port(0xC4, 4) port(0xC4, 5) byte |= (gba.bus_read(0x080000C4, 16, 0) >> 1 & 1) << bit data.append(byte) assert data == [0x04, 0x11, 0x21, 0x00, 0x13, 0x45, 0x30] # Sunday is day 0