31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
"""Build a TRS-80 CoCo Program Pak cartridge ROM (.ccc) holding viewer + image.
|
|
|
|
The CoCo autostarts the cartridge at $C000. Layout: viewer code, then the
|
|
6144-byte PMODE 4 image, padded to an 8KB ROM ($C000-$DFFF)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from . import viewer
|
|
|
|
CART_BASE = 0xC000
|
|
CART_SIZE = 0x2000 # 8 KB
|
|
|
|
|
|
def build_rom(data: bytes, vdg: int = 0xF8, display: str = "forever",
|
|
seconds: int = 0) -> bytes:
|
|
if len(data) != viewer.SCREEN_BYTES:
|
|
raise ValueError(f"unexpected image length {len(data)}")
|
|
vk = dict(vdg=vdg, display=display, seconds=seconds)
|
|
code = viewer.build(0, **vk) # pass 1: measure code length
|
|
data_src = CART_BASE + len(code)
|
|
code = viewer.build(data_src, **vk) # pass 2: real data address
|
|
rom = code + bytes(data)
|
|
if len(rom) > CART_SIZE:
|
|
raise ValueError("viewer + image exceed the 8KB cartridge")
|
|
return rom + bytes(CART_SIZE - len(rom))
|
|
|
|
|
|
def write_ccc(rom: bytes, path: str) -> str:
|
|
with open(path, "wb") as f:
|
|
f.write(rom)
|
|
return path
|