"""Build CP/M Kermit-80 4.11 for an IMSAI 8080 with its modem on the IMSAI MIO's serial port. Kermit isn't in the repository: pass the Kermit Project's cpm80.zip (the Kermit-80 4.11 sources, with LASM and MLOAD as .HEX files). The script adds an "imsai" system to the system-dependent sources, then builds the way the Kermit manual says, with Kermit's own tools running on the emulator's 8080 under tools/CpmRun: LASM CPXTYP the IMSAI overlay, CPXTYP.HEX, at 7000H MLOAD KERMIT=CPSKER,CPXTYP merged with the prebuilt system-independent part The ports and status bits default to the MIO's test arrangement (data at 42H, status at 43H, TRDY on bit 0 and RRDY on bit 1, high when ready); change them to match how a board is jumpered. The MIO's baud rate is a jumper, so Kermit's SET SPEED isn't offered. """ from __future__ import annotations import argparse import pathlib import re import shutil import subprocess import sys import tempfile import zipfile HERE = pathlib.Path(__file__).resolve().parent ROOT = HERE.parent.parent CPMRUN = ROOT / "tools" / "CpmRun" TERMINALS = ("crt", "adm3a", "adm22", "h1500", "smrtvd", "soroq", "am230", "tvi912", "tvi925", "vt52", "vt100", "wyse") def hex_byte(value: int) -> str: return f"0{value:02X}H" def patch(text: str, anchor: str, addition: str, name: str) -> str: """Insert addition after the one line matching anchor (a regex for the whole line).""" matches = list(re.finditer(rf"^{anchor}[^\r\n]*\r\n", text, re.M)) if len(matches) != 1: sys.exit(f"{name}: expected one line matching {anchor!r}, found {len(matches)}") end = matches[0].end() return text[:end] + addition.replace("\n", "\r\n") + text[end:] def select(text: str, symbol: str) -> str: text, count = re.subn(rf"^({re.escape(symbol)}\s+EQU\s+)FALSE", r"\1TRUE", text, flags=re.M | re.I) if count != 1: sys.exit(f"CPXTYP.ASM: no '{symbol} EQU FALSE' to select") return text def add_imsai(work: pathlib.Path, args: argparse.Namespace) -> None: def edit(name: str, change) -> None: path = work / name path.write_bytes(change(path.read_bytes().decode("latin-1")).encode("latin-1")) edit("CPXTYP.ASM", lambda t: select(select(patch(patch(t, r"s1008\s+EQU\s+FALSE", "imsai\tEQU\tFALSE\t\t; IMSAI 8080 with a modem on its MIO's serial port (terminal required)\n", "CPXTYP.ASM"), r"ENDIF ; genie OR trsm4 OR ampro", "IF imsai\ninout\tSET\tTRUE\t\t; the MIO's UART is reached with IN and OUT\nENDIF ; imsai\n", "CPXTYP.ASM"), "imsai"), args.terminal)) edit("CPXSWT.ASM", lambda t: patch(t, r"ENDIF\t;s1008 \[29\]", "\nIF imsai\n.printx\t* Assembling KERMIT-80 for the IMSAI 8080 with an MIO *\nENDIF\t;imsai\n", "CPXSWT.ASM")) trdy, rrdy = 1 << args.trdy, 1 << args.rrdy edit("CPXSYS.ASM", lambda t: patch(t, r"ENDIF;s1008 \[29\]", "\nIF imsai\n" f"mnport\tEQU\t{hex_byte(args.data)}\t\t;MIO serial data port\n" f"mnprts\tEQU\t{hex_byte(args.status)}\t\t;MIO control port, read for its status inputs\n" f"output\tEQU\t{hex_byte(trdy)}\t\t;TRDY: the transmitter is ready (status bit {args.trdy})\n" f"input\tEQU\t{hex_byte(rrdy)}\t\t;RRDY: a character has come in (status bit {args.rrdy})\n" "z80\tSET\tFALSE\t\t;an 8080\n" "ENDIF;imsai\n", "CPXSYS.ASM")) edit("CPXSY2.ASM", lambda t: patch(patch(t, r"ENDIF;mmdI OR osi OR cpm3 OR S1008 \[29\]", "IF imsai\nspdtbl\tEQU\t0\t\t; SET SPEED not supported: the MIO's baud rate is a jumper\nsphtbl\tEQU\t0\nENDIF;imsai\n", "CPXSY2.ASM"), r"ENDIF ;s1008 \[29\]", "\nIF imsai\nsysver:\tdb\t'IMSAI 8080 with MIO serial port$'\nENDIF ;imsai\n", "CPXSY2.ASM")) def lasm_errors(listing: str) -> list[str]: # LASM flags a line with a letter in the first column; S marks .printx messages, which aren't errors. # Split on line feeds only: the sources have form feeds in their comments. return [line for line in listing.split("\n") if line[:1].isalpha() and not line.startswith("S ")] def main() -> None: parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("zip", type=pathlib.Path, help="the Kermit-80 4.11 sources, cpm80.zip") parser.add_argument("--data", type=lambda s: int(s, 16), default=0x42, help="serial data port, hex (42)") parser.add_argument("--status", type=lambda s: int(s, 16), default=0x43, help="status (control) port, hex (43)") parser.add_argument("--trdy", type=int, default=0, choices=range(8), help="status bit for TRDY (0)") parser.add_argument("--rrdy", type=int, default=1, choices=range(8), help="status bit for RRDY (1)") parser.add_argument("--terminal", default="crt", choices=TERMINALS, help="the console terminal, for Kermit's screen (crt: no cursor addressing)") parser.add_argument("--out", type=pathlib.Path, default=HERE / "KERMIT.COM") parser.add_argument("--work", type=pathlib.Path, help="build in this folder and keep it (default: a temporary one)") args = parser.parse_args() subprocess.run(["dotnet", "build", str(CPMRUN), "-c", "Release", "-v", "q", "-nologo"], check=True, stdout=subprocess.DEVNULL) cpmrun = CPMRUN / "bin" / "Release" / "net10.0" / "CpmRun.dll" with tempfile.TemporaryDirectory() as temp: work = args.work or pathlib.Path(temp) work.mkdir(parents=True, exist_ok=True) with zipfile.ZipFile(args.zip) as archive: for entry in archive.infolist(): name = pathlib.PurePosixPath(entry.filename).name.upper() if not entry.is_dir() and name.endswith((".ASM", ".HEX")) and name.startswith(("CPS", "CPX", "LASM", "MLOAD")): data = archive.read(entry) if name.endswith(".ASM"): data = data.replace(b"\r\n", b"\n").replace(b"\n", b"\r\n") # CP/M text (work / name).write_bytes(data) if not (work / "CPSKER.HEX").exists() or not (work / "LASM.HEX").exists(): sys.exit(f"{args.zip} doesn't look like the Kermit-80 4.11 sources (no CPSKER.HEX or LASM.HEX)") add_imsai(work, args) def cpm(*command: str) -> str: result = subprocess.run(["dotnet", str(cpmrun), "--dir", str(work), *command], stdin=subprocess.DEVNULL, capture_output=True, text=True) if result.returncode != 0: sys.exit(f"{' '.join(command)} failed:\n{result.stdout}{result.stderr}") return result.stdout cpm("LASM.HEX", "CPXTYP") errors = lasm_errors((work / "CPXTYP.PRN").read_text("latin-1")) if errors: sys.exit("LASM reported errors:\n" + "\n".join(errors)) cpm("MLOAD.HEX", "KERMIT=CPSKER,CPXTYP") kermit = work / "KERMIT.COM" if not kermit.exists(): sys.exit("MLOAD didn't write KERMIT.COM") shutil.copyfile(kermit, args.out) print(f"Wrote {args.out} ({args.out.stat().st_size} bytes): data {args.data:02X}H, status {args.status:02X}H, " f"TRDY bit {args.trdy}, RRDY bit {args.rrdy}, {args.terminal} terminal") if __name__ == "__main__": main()