"""Drive the assembler toolchain (GNU binutils for m68k, z80asm) that lives in WSL. Windows paths are translated to ``/mnt//...`` and every tool is run through ``wsl -e``. Each step is a plain subprocess so a failure shows the tool's own message. """ from __future__ import annotations import subprocess from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def to_wsl(path: Path) -> str: path = path.resolve() drive = path.drive.rstrip(":").lower() rest = path.as_posix()[len(path.drive) :] return f"/mnt/{drive}{rest}" def wsl(*args: str, cwd: Path | None = None) -> None: cmd = ["wsl", "-e"] if cwd is not None: cmd += ["bash", "-lc", f"cd {to_wsl(cwd)} && " + " ".join(args)] else: cmd += list(args) subprocess.run(cmd, check=True) def assemble_68k(source: Path, out_bin: Path, listing: Path | None = None, include_dirs: tuple[Path, ...] = (), defines: dict[str, int] | None = None) -> None: # fmt: skip """Assemble one GNU-syntax 68000 source file to a flat big-endian binary at origin 0 (the cartridge's own address space).""" out_bin.parent.mkdir(parents=True, exist_ok=True) obj = out_bin.with_suffix(".o") as_args = ["m68k-linux-gnu-as", "-m68000", "--register-prefix-optional", "-I", to_wsl(source.parent)] for name, value in (defines or {}).items(): as_args += ["--defsym", f"{name}={value}"] for inc in include_dirs: as_args += ["-I", to_wsl(inc)] if listing is not None: as_args.append("-a=" + to_wsl(listing)) as_args += ["-o", to_wsl(obj), to_wsl(source)] wsl(*as_args, cwd=source.parent) # The linker fixes the origin and resolves the symbols, writing the flat binary directly. wsl("m68k-linux-gnu-ld", "-Ttext=0", "--oformat", "binary", "-o", to_wsl(out_bin), to_wsl(obj), cwd=source.parent) def assemble_z80(source: Path, out_bin: Path) -> None: out_bin.parent.mkdir(parents=True, exist_ok=True) wsl("z80asm", "-o", to_wsl(out_bin), to_wsl(source), cwd=source.parent)