"""The minimal harness itself: mapping, the VRAM port, DMA, $4210 and the stop rules. A hand-assembled 32 KiB LoROM program, so the quick loop needs no toolchain and no fetched ROM. """ import zipfile from pathlib import Path from w65c816.harness import Harness, detect_mapping, hirom_offset, load_rom # $00:8000, entered from the reset vector. PROGRAM = bytes( [ 0x18, # CLC 0xFB, # XCE: native mode 0xC2, 0x10, # REP #$10: 16-bit X, Y 0xE2, 0x20, # SEP #$20: 8-bit A 0xA9, 0x80, # LDA #$80 0x8D, 0x15, 0x21, # STA $2115: step after the high byte 0xA2, 0x00, 0x10, # LDX #$1000 0x8E, 0x16, 0x21, # STX $2116: VRAM word $1000 0xA9, 0x41, # LDA #'A' 0x8D, 0x18, 0x21, # STA $2118 0x9C, 0x19, 0x21, # STZ $2119: step to $1001 0x9C, 0x15, 0x21, # STZ $2115: step after the low byte 0x9C, 0x00, 0x43, # STZ $4300: DMA 0, A to B, one register, increment 0xA9, 0x18, # LDA #$18 0x8D, 0x01, 0x43, # STA $4301: to $2118 0xA2, 0x00, 0x81, # LDX #$8100 0x8E, 0x02, 0x43, # STX $4302: from $00:8100 0x9C, 0x04, 0x43, # STZ $4304 0xA2, 0x02, 0x00, # LDX #2 0x8E, 0x05, 0x43, # STX $4305: two bytes 0xA9, 0x01, # LDA #1 0x8D, 0x0B, 0x42, # STA $420B: run channel 0 0xA9, 0x5A, # LDA #$5A 0x8F, 0x34, 0x12, 0x7E, # STA $7E1234 0x2C, 0x10, 0x42, # BIT $4210: wait for vblank 0x10, 0xFB, # BPL back to the BIT 0x80, 0xFE, # BRA to itself ] ) # fmt: skip SPIN = 0x8000 + len(PROGRAM) - 2 def make_rom() -> bytes: rom = bytearray(0x8000) rom[: len(PROGRAM)] = PROGRAM rom[0x100:0x102] = b"HI" rom[0x7FC0:0x7FD5] = b"HARNESS TEST".ljust(21) rom[0x7FDC:0x7FE0] = bytes((0xFF, 0xFF, 0x00, 0x00)) # complement, checksum rom[0x7FFC:0x7FFE] = bytes((0x00, 0x80)) # reset vector $8000 return bytes(rom) def test_program_runs_to_its_spin_and_leaves_its_marks() -> None: harness = Harness(make_rom(), frame_cycles=2_000) result = harness.run(10_000) assert result.reason == "spin" assert result.pc == SPIN assert harness.screen(0x1000, 1) == ["AHI"] assert harness.wram[0x1234] == 0x5A assert harness.peek(0x001234) == 0x5A # bank $00 mirrors the first 8 KiB assert harness.cycles > 2_000 - 2_000 // 8 # it waited for vblank def test_mapping_is_found_from_the_header() -> None: rom = make_rom() assert detect_mapping(rom) == "lorom" assert Harness(rom).peek(0x80FFFC) == 0x00 # LoROM: bank $80 mirrors bank $00 assert Harness(rom).peek(0x00FFFD) == 0x80 def test_rdnmi_sets_once_per_frame_and_clears_on_read() -> None: harness = Harness(make_rom(), frame_cycles=800) harness.cycles = 700 - 1 harness._tick() assert harness.io_read(0x4210) & 0x80 assert not harness.io_read(0x4210) & 0x80 assert harness.io_read(0x4212) & 0x80 # still in vblank def test_multiply_and_divide_finish_at_once() -> None: harness = Harness(make_rom()) harness.io_write(0x4202, 12) harness.io_write(0x4203, 13) assert harness.io_read(0x4216) | harness.io_read(0x4217) << 8 == 156 harness.io_write(0x4204, 0x39) harness.io_write(0x4205, 0x30) # 12345 harness.io_write(0x4206, 100) assert harness.io_read(0x4214) | harness.io_read(0x4215) << 8 == 123 assert harness.io_read(0x4216) == 45 def test_hirom_banks_fe_and_ff_are_rom_throughout() -> None: assert hirom_offset(0xFE, 0x0003) == 0x3E0003 assert hirom_offset(0xFF, 0x7FFF) == 0x3F7FFF assert hirom_offset(0x7E, 0x0003) is None # WRAM assert hirom_offset(0x00, 0x2100) is None # I/O def test_load_rom_takes_a_zip_member_and_drops_a_copier_header(tmp_path: Path) -> None: image = bytes(range(256)) * 128 # 32 KiB archive = tmp_path / "game.zip" with zipfile.ZipFile(archive, "w") as bundle: bundle.writestr("readme.txt", "not this") bundle.writestr("game.smc", bytes(512) + image) assert load_rom(archive) == image plain = tmp_path / "game.sfc" plain.write_bytes(image) assert load_rom(plain) == image