# SPDX-License-Identifier: MIT """A start-up stub for a 32X cartridge image, for tests that only need the boot ROMs to run: the image's reset vector sent to code that does what testpico's ``x32x_enable`` does (ADEN, then ADEN | RES, from work RAM because $000100-$3FFFFF goes away with RV = 0), waits for "M_OK" and "S_OK" and sets RV. Written into testpico's ILLEGAL-filled $204-$3BF.""" from __future__ import annotations # move.w #1,$A15100 / move.w #16,d0 / dbra d0,* / move.w #3,$A15100 / # 1: cmpi.w #'M_',$A15120 / bne 1b / 2: cmpi.w #'S_',$A15124 / bne 2b / # bset #0,$A15107 / rts RAM_CODE = bytes.fromhex( "33fc000100a15100303c001051c8fffe33fc000300a15100" "0c794d5f00a1512066f60c79535f00a1512466f608f9000000a151074e75" ) def enable_stub(image: bytes, at: int = 0x204) -> bytes: rom = bytearray(image) ram = RAM_CODE + b"\x4e\x71" * ((-len(RAM_CODE) % 4) // 2) code = bytearray() for i in range(0, len(ram), 4): # move.l #imm,$FF0000+i code += bytes.fromhex("23fc") + ram[i : i + 4] + (0xFF0000 + i).to_bytes(4, "big") code += bytes.fromhex("4eb900ff000060fe") # jsr $FF0000; bra.s * if not all(b in (0x4A, 0xFC) for b in rom[at : at + len(code)]): raise ValueError("the stub would overwrite code") rom[at : at + len(code)] = code rom[4:8] = at.to_bytes(4, "big") return bytes(rom)