# SPDX-License-Identifier: MIT """testpico's 68000-only 32X checks, read from its source and run as 68000 programs on megadrive-python with the 32X in the slot (milestone 1's hardware-corrected judge). Source: notaz, testpico ``main.c`` (30a5783, ``reference/src/megadrive-notaz/testpico``), run by its author on hardware (docs/validation.md). Transcribed here: - ``t_32x_md_rom``: every check (it runs with ADEN = 1 and RV = 1, which ``t_32x_init`` leaves set through ``x32x_enable``). - ``t_32x_reg_w``: the 68000 half (the DREQ registers' writable bits). Its last check writes $20004006 from an SH-2 and is milestone 2's. - ``t_32x_init``'s "check writable bits without ADEN" block and its power-on reads, which are 68000-only too. - ``t_32x_md_fb``: every check (the 68000 and the frame buffer, FM = 0). - ``t_32x_md_bios`` (needs the user's 68000 boot ROM: it calls the routine at $0000C0). Each program is hand-assembled; the opcodes are in the comments beside them. """ from __future__ import annotations from conftest import SYNTHETIC_VECTORS, ram_long, ram_word, run, system_with_32x from sega32x_python import BootROMs ENABLE = ( "33fc 0001 00a1 5106" # move.w #1,$A15106 RV = 1 (testpico's state after x32x_enable) "33fc 0003 00a1 5100" # move.w #3,$A15100 ADEN | RES ) MD_ROM = ENABLE + ( "2039 0000 0004" # move.l $000004,d0 expect $880200 "2239 0000 0100" # move.l $000100,d1 expect "SEGA" (the cartridge, RV = 1) "2439 0000 0070" # move.l $000070,d2 expect 0 "23fc a512 3456 0000 0070" # move.l #$A5123456,$000070 "23fc ffff ffff 0000 0078" # move.l #~0,$000078 "2639 0000 0078" # move.l $000078,d3 expect $8802AE (ROM: the write did nothing) "2839 0000 0070" # move.l $000070,d4 expect $A5123456 "42b9 0000 0070" # clr.l $000070 "2a39 0000 0070" # move.l $000070,d5 0 again "23c0 00ff 0100 23c1 00ff 0104 23c2 00ff 0108" # move.l d0-d2,$FF0100... "23c3 00ff 010c 23c4 00ff 0110 23c5 00ff 0114" # move.l d3-d5,... "60fe" # bra.s * ) def _md_rom(vector_rom: bytes) -> list[int]: system, device = system_with_32x(MD_ROM, vector_rom=vector_rom) run(system, 25) assert device.aden and device.rv return [ram_long(system, 0xFF0100 + 4 * n) for n in range(6)] def test_md_rom_with_a_stand_in_vector_rom() -> None: """The register behaviour of t_32x_md_rom, with vectors made here (Sega's shape).""" assert _md_rom(SYNTHETIC_VECTORS) == [0x880200, 0x53454741, 0, 0x8802AE, 0xA5123456, 0] def test_md_rom_with_the_users_boot_rom(user_boot_roms: BootROMs) -> None: """t_32x_md_rom exactly: $880200 at $000004 and $8802AE at $000078 are Sega's ROM's, the values testpico expects on hardware.""" assert _md_rom(user_boot_roms.m68k) == [0x880200, 0x53454741, 0, 0x8802AE, 0xA5123456, 0] MD_BIOS = ENABLE + ( # t_32x_md_bios copies its caller to RAM, since the routine at $0000C0 clears RV and # the cartridge's code then vanishes from $000100 (Quirks.low_map_without_rv): # $FF0040: jsr $0000C0.l / bset #0,$A15107 / rts "23fc 4eb9 0000 00ff 0040" # move.l #$4EB90000,$FF0040 "23fc 00c0 08f9 00ff 0044" # move.l #$00C008F9,$FF0044 "23fc 0000 00a1 00ff 0048" # move.l #$000000A1,$FF0048 "23fc 5107 4e75 00ff 004c" # move.l #$51074E75,$FF004C "13fc 0000 00ff 0000" # move.b #0,$FF0000 "227c 00ff 0000" # movea.l #$FF0000,a1 "705a" # moveq #$5A,d0 "4eb9 00ff 0040" # jsr $FF0040 (move.b d0,(a1); RV = 0; RV = 1) "2039 0000 0004 2239 0000 0010 2439 0000 0094" # move.l $4,d0; $10,d1; $94,d2 "23c0 00ff 0100 23c1 00ff 0104 23c2 00ff 0108" "60fe" ) def test_md_bios_with_the_users_boot_rom(user_boot_roms: BootROMs) -> None: """t_32x_md_bios: the routine at $0000C0 of Sega's 68000 ROM stores d0 at (a1) and clears RV; testpico's expected vectors.""" system, device = system_with_32x(MD_BIOS, vector_rom=user_boot_roms.m68k) run(system, 60) assert system.board.ram[0] == 0x5A assert [ram_long(system, 0xFF0100 + 4 * n) for n in range(3)] == [ 0x880200, 0x880212, 0x8802D8, ] assert device.rv # set again by the caller's bset REG_W = ( "23fc ffff ffff 00a1 5108" # move.l #~0,$A15108 "23fc ffff ffff 00a1 510c" # move.l #~0,$A1510C "33fc ffff 00a1 5110" # move.w #~0,$A15110 "2039 00a1 5108 2239 00a1 510c 3439 00a1 5110" # move.l ..,d0; move.l ..,d1; move.w ..,d2 "23c0 00ff 0100 23c1 00ff 0104 33c2 00ff 0108" "60fe" ) def test_reg_w_68000_half() -> None: """t_32x_reg_w: $00FFFFFE, $00FFFFFF, $FFFC (with ADEN = 0, as the board allows).""" system, _device = system_with_32x(REG_W) run(system, 12) assert ram_long(system, 0xFF0100) == 0x00FFFFFE assert ram_long(system, 0xFF0104) == 0x00FFFFFF assert ram_word(system, 0xFF0108) == 0xFFFC def test_init_power_on_reads_and_writable_bits() -> None: """t_32x_init: power-on values, then "check writable bits without ADEN" (every write and expected read of that block, through the 68000's bus).""" system, _device = system_with_32x() board = system.board def word(offset: int) -> int: return board.read_word(0xA15100 + offset) def long(offset: int) -> int: return (word(offset) << 16) | word(offset + 2) def write_long(offset: int, value: int) -> None: board.write_word(0xA15100 + offset, value >> 16) board.write_word(0xA15100 + offset + 2, value & 0xFFFF) assert [word(o) for o in (0x00, 0x02, 0x04, 0x06)] == [0x82, 0, 0, 0] assert board.read_byte(0xA15108) == 0 and board.read_byte(0xA1510C) == 0 assert word(0x10) == 0xFFFF and word(0x12) == 0 assert long(0x14) == long(0x18) == long(0x1C) == 0 board.write_word(0xA15100, 0) assert word(0x00) == 0x80 board.write_word(0xA15100, 0xFFFE) assert word(0x00) == 0x8082 board.write_word(0xA15100, 0x82) board.write_word(0xA15102, 0xFFFF) for offset in (0x04, 0x08, 0x0C, 0x14, 0x18, 0x1C): write_long(offset, 0xFFFFFFFF) board.write_word(0xA15110, 0xFFFF) assert [word(o) for o in (0x00, 0x02, 0x04, 0x06)] == [0x82, 0x03, 0x03, 0x07] assert long(0x08) == 0x00FFFFFE and long(0x0C) == 0x00FFFFFF assert word(0x10) == 0xFFFC and long(0x14) == 0 and word(0x18) == 0 assert word(0x1A) == 0x0101 and long(0x1C) == 0 MD_FB = ENABLE + ( "23fc 1234 5678 0084 0000" # move.l #$12345678,$840000 fbl[0] "23fc 89ab cdef 0086 0004" # move.l #$89ABCDEF,$860004 fol[1] "3039 0084 0002 3239 0086 0004" # move.w $840002,d0; move.w $860004,d1 "13fc 0000 0084 0000" # move.b #0,$840000 fbb[0] = 0 (dropped) "13fc 0000 0086 0001" # move.b #0,$860001 fob[1] = 0 (dropped) "33fc 0000 0084 0002" # move.w #0,$840002 fbw[1] = 0 (written) "33fc 0000 0086 0004" # move.w #0,$860004 fow[2] = 0 (dropped) "33fc 0001 0086 0006" # move.w #1,$860006 fow[3] = 1 (low byte only) "33fc 0200 0086 0006" # move.w #$200,$860006 fow[3] = $200 (high byte only) "2439 0086 0000 2639 0084 0004" # move.l $860000,d2; move.l $840004,d3 "33c0 00ff 0100 33c1 00ff 0102 23c2 00ff 0104 23c3 00ff 0108" "60fe" ) def test_md_fb() -> None: """t_32x_md_fb: $5678, $89AB, then $12340000 and $89AB0201.""" system, _device = system_with_32x(MD_FB) run(system, 25) assert ram_word(system, 0xFF0100) == 0x5678 and ram_word(system, 0xFF0102) == 0x89AB assert ram_long(system, 0xFF0104) == 0x12340000 assert ram_long(system, 0xFF0108) == 0x89AB0201