# SPDX-License-Identifier: MIT """Timers, the IRQ line, HALT, DMA and the cartridge devices, driven through their registers at chosen cycles (docs/timing.md, docs/cartridge.md).""" from __future__ import annotations from programs import board_for from gba_python import irq as irq_module from gba_python import timers as timers_module from gba_python.cartridge.backup import EEPROM, Flash from gba_python.cartridge.detect import detect_kind def board(): return board_for(b"\x00" * 0x100) def catch_up(b, time): b.bus.now = time b.run_events() def test_timer_start_and_first_tick(): b = board() t = b.timers t.write16(0x100, 0xFFF0, 0xFFFF, 100) # reload t.write16(0x102, 0x0080, 0xFFFF, 100) # start in cycle 100 assert t.value(t.timers[0], 101) == 0xFFF0 # loaded in 101 assert t.value(t.timers[0], 102) == 0xFFF0 # first tick in 102 (START_TICK) assert t.value(t.timers[0], 103) == 0xFFF1 assert timers_module.START_TICK == 2 def test_timer_stop_keeps_its_cycle(): b = board() t = b.timers t.write16(0x102, 0x0080, 0xFFFF, 0) t.write16(0x102, 0x0000, 0xFFFF, 10) # the tick of cycle 10 still counts assert t.value(t.timers[0], 20) == 9 # ticks 2..10 def test_timer_reload_lands_at_overflow(): b = board() t = b.timers t.write16(0x100, 0xFFFE, 0xFFFF, 0) t.write16(0x102, 0x0080, 0xFFFF, 0) # ticks 2 (FFFF), 3 (overflow) t.write16(0x100, 0x1234, 0xFFFF, 2) # before the overflow's cycle: used catch_up(b, 4) assert t.value(t.timers[0], 4) == 0x1234 def test_timer_overflow_raises_if_two_cycles_later(): b = board() t = b.timers t.write16(0x100, 0xFFFF, 0xFFFF, 0) t.write16(0x102, 0x00C0, 0xFFFF, 0) # IRQ enable, start: overflow tick in cycle 2 catch_up(b, 3) assert not b.irq.if_ & irq_module.TIMER0 catch_up(b, 4) assert b.irq.if_ & irq_module.TIMER0 def test_tick_before_reload_overflows_from_ffff(): b = board() t = b.timers t.write16(0x100, 0xFFFF, 0xFFFF, 0) t.write16(0x102, 0x00C0, 0xFFFF, 0) t.write16(0x102, 0x0000, 0xFFFF, 1) # stopped at FFFF, before its first tick b.irq.if_ = 0 t.write16(0x100, 0x0000, 0xFFFF, 50) t.write16(0x102, 0x00C0, 0xFFFF, 50) # starting from FFFF ticks once, overflows catch_up(b, 60) assert b.irq.if_ & irq_module.TIMER0 def test_irq_line_delays(): b = board() i = b.irq i.write16(0x200, 0x0001, 0xFFFF, 0) i.write16(0x208, 0x0001, 0xFFFF, 0) b.bus.now = 100 i.request(irq_module.VBLANK, 100) assert not i.line(100 + irq_module.IF_DELAY - 1) assert i.line(100 + irq_module.IF_DELAY) i.write16(0x202, 0x0001, 0xFFFF, 200) # write 1 to acknowledge assert i.if_ == 0 assert i.line(200 + irq_module.IF_DELAY - 1) # the clear also takes the delay assert not i.line(200 + irq_module.IF_DELAY) def test_haltcnt_only_from_the_bios(): b = board() b.bus.fetch_in_bios = False b.irq.write16(0x300, 0x0000, 0xFF00, 0) assert not b.irq.halted b.bus.fetch_in_bios = True b.irq.write16(0x300, 0x0000, 0xFF00, 0) assert b.irq.halted def test_halt_wake_time(): b = board() i = b.irq i.write16(0x200, 0x0008, 0xFFFF, 0) i.halt_time = 10 b.bus.now = 50 i.request(irq_module.TIMER0, 50) assert i.wake_time() == 50 + irq_module.HALT_LINE_DELAY + irq_module.HALT_WAKE def test_immediate_dma_copies_and_keeps_its_latch(): b = board() for n, v in enumerate((0x1111, 0x2222, 0x3333)): b.bus.write_region(3, 0x03000000 + 2 * n, 16, v, 0) d = b.dma d.write16(0xD4, 0x0000, 0xFFFF, 0) d.write16(0xD6, 0x0300, 0xFFFF, 0) d.write16(0xD8, 0x0100, 0xFFFF, 0) d.write16(0xDA, 0x0300, 0xFFFF, 0) d.write16(0xDC, 3, 0xFFFF, 0) d.write16(0xDE, 0x8000, 0xFFFF, 0) # enable, 16-bit, immediate b.bus.now = 10 d.run() assert [b.bus.read_region(3, 0x03000100 + 2 * n, 16, 0) for n in range(3)] == [ 0x1111, 0x2222, 0x3333, ] assert d.channels[3].latch == 0x33333333 assert not d.channels[3].control & 0x8000 def test_dma_from_the_bios_writes_the_old_latch(): b = board() channel = b.dma.channels[0] channel.latch = 0xC0DEC0DE b.dma.unit(channel, 0x00000000, 0x03000000, 16, False) assert b.bus.read_region(3, 0x03000000, 16, 0) == 0xC0DE def test_save_detection(): assert detect_kind(b"....FLASH1M_V102") == "flash128" assert detect_kind(b"....FLASH_V1..FLASH512_V") == "flash64" assert detect_kind(b"....SRAM_V113") == "sram" assert detect_kind(b"...SRAM_V113") == "none" # not word-aligned assert detect_kind(b"") == "none" def test_flash_commands(): f = Flash(0x20000) def command(value): f.write8(0x5555, 0xAA) f.write8(0x2AAA, 0x55) f.write8(0x5555, value) command(0x90) assert (f.read8(0), f.read8(1)) == (0x62, 0x13) command(0xF0) command(0xA0) f.write8(0x0010, 0x5A) assert f.read8(0x0010) == 0x5A command(0xB0) f.write8(0x0000, 1) assert f.read8(0x0010) == 0xFF # bank 1 command(0x80) command(0x10) f.bank = 0 assert f.read8(0x0010) == 0xFF # chip erased def test_eeprom_write_then_read(): e = EEPROM(0x200) bits = [1, 0] + [0, 0, 0, 0, 1, 1] + [1, 0] * 32 + [0] for bit in bits: e.write_bit(bit) for bit in [1, 1] + [0, 0, 0, 0, 1, 1] + [0]: e.write_bit(bit) stream = [e.read_bit() for _ in range(68)] assert stream[4:] == [1, 0] * 32 def test_cpu_internal_cycles_overlap_dma(): """The CPU keeps running internal cycles while a DMA holds the bus and stops only at its next memory access (alyosha-tas Interactions/ReadMe.txt; TEST ROM prose). A regression guard for the model, not hardware evidence: no passing hardware judge exercises this rule yet (docs/validation.md "Seeded faults", blind spots).""" b = board() bus = b.bus bus.now = 100 b.dma_due = 100 def fake_run(): bus.now += 10 # a DMA holding the bus for 10 cycles b.dma_due = 1 << 62 b.dma.run = fake_run for _ in range(4): bus.idle(0x03000000) # four internal cycles beside the DMA assert bus.now == 110 # they cost nothing extra bus.idle(0x03000000) assert bus.now == 110 # the DMA's ten cycles cover the first ten internal cycles def test_keycnt_and_with_no_key_selected_requests_at_once(): """KEYCNT = C000h (IRQ, AND, no key selected) is true: the AGS aging cartridges' interrupt test 7 (TEST ROM, docs/validation.md "Rung 6"); its judge is scripts/board_aging.py.""" b = board() b.keypad.write16(0x132, 0xC000, 0xFFFF, 10) catch_up(b, 20) assert b.irq.if_ & irq_module.KEYPAD def test_video_capture_clears_dma3_at_line_162(): """VCOUNT 162 ends a video capture and clears DMA3's enable (GBATEK, DOC; the AGS aging cartridges' DMA test 7, TEST ROM); its judge is scripts/board_aging.py.""" b = board() d = b.dma d.write16(0xDC, 1, 0xFFFF, 0) d.write16(0xDE, 0xB300, 0xFFFF, 0) # enable, capture (special), repeat, source fixed assert d.channels[3].control & 0x8000 d.end_capture() assert not d.channels[3].control & 0x8000 assert not d.channels[3].pending