"""Make a CP/M .COM of IMSAI SCS-1 from a tape dump in Intel HEX (the forkay tapes). python tools/scs1/make.py artifacts/forkay-c/forkayC.bin [FORKAYC.COM] The dump must hold SCS-1 at 0050H-0E49H with every record's checksum good; lines that aren't records (capture leftovers such as a lone '8') are skipped. SCS1.ASM is assembled with tools/asm8080 and the program goes into its IMAGE area. Neither the tape nor the .COM is committed: SCS-1 is IMSAI's software. """ from __future__ import annotations import hashlib import pathlib import re import subprocess import sys import tempfile HERE = pathlib.Path(__file__).resolve().parent ROOT = HERE.parent.parent LOAD_AT, LENGTH = 0x0050, 0x0DFA IMAGE_OFFSET = 3 # IMAGE follows the JMP at 0100H def read_hex(path: pathlib.Path) -> bytes: memory: dict[int, int] = {} skipped = 0 for line in re.split(r"[\r\n]+", path.read_bytes().decode("latin-1")): line = line.strip("\x00 ") if not line: continue if not re.fullmatch(r":[0-9A-Fa-f]+", line): skipped += 1 continue record = bytes.fromhex(line[1:]) if record[0] + 5 != len(record) or sum(record) & 0xFF: sys.exit(f"{path}: bad record {line}") if record[3] == 0: address = record[1] << 8 | record[2] for i, value in enumerate(record[4:-1]): memory[address + i] = value wanted = range(LOAD_AT, LOAD_AT + LENGTH) missing = [a for a in wanted if a not in memory] if missing or len(memory) != LENGTH: sys.exit(f"{path}: expected SCS-1 at 0050H-0E49H; {len(missing)} bytes missing, {len(memory)} in all") print(f"{path.name}: {LENGTH} bytes at 0050H-0E49H, every checksum good, {skipped} non-record lines skipped") return bytes(memory[a] for a in wanted) def main() -> None: if len(sys.argv) not in (2, 3): sys.exit(__doc__) source = pathlib.Path(sys.argv[1]) out = pathlib.Path(sys.argv[2]) if len(sys.argv) == 3 else source.with_name(source.stem.upper() + ".COM") image = read_hex(source) print(f"SHA-256 {hashlib.sha256(image).hexdigest()}") with tempfile.TemporaryDirectory() as temp: loader = pathlib.Path(temp) / "SCS1.COM" subprocess.run([sys.executable, ROOT / "tools" / "asm8080" / "asm8080.py", HERE / "SCS1.ASM", loader], check=True, stdout=subprocess.DEVNULL) com = bytearray(loader.read_bytes()) if com[IMAGE_OFFSET : IMAGE_OFFSET + LENGTH] != bytes(LENGTH): sys.exit("SCS1.ASM's IMAGE area isn't where make.py expects it") com[IMAGE_OFFSET : IMAGE_OFFSET + LENGTH] = image out.write_bytes(com) print(f"Wrote {out} ({len(com)} bytes)") if __name__ == "__main__": main()