#!/usr/bin/env python3 """ Step 3: Generate .mcs configuration memory file using Vivado batch mode. Calls Vivado with step3_write_mcs.tcl (located in the same scripts/ directory) via -tclargs so no temporary script files are created. Usage: python gen_mcs.py \ --bit-file "E:/project/RU_golden/RU_golden.runs/impl_1/RU_golden_top.bit" \ --mcs-file "E:/project/RU_golden/RU_golden.runs/impl_1/RU_golden_smod.mcs" Exit codes: 0 = success ("write_cfgmem completed successfully" in output) 1 = failure """ import argparse import os import subprocess import sys from vivado_env import resolve_vivado_dir def norm(path: str) -> str: """Normalize path to forward slashes (Vivado on Windows accepts both).""" return path.replace("\\", "/") def main(): parser = argparse.ArgumentParser( description="Generate .mcs from .bit via Vivado write_cfgmem" ) parser.add_argument( "--vivado-dir", default=None, help="Vivado install root; defaults to VIVADO_ROOT from ~/skills_env.md", ) parser.add_argument( "--bit-file", required=True, help="Full path to the .bit file", ) parser.add_argument( "--mcs-file", required=True, help="Full path for the output .mcs file", ) args = parser.parse_args() vivado_dir = norm(resolve_vivado_dir(args.vivado_dir)) bit_file = norm(args.bit_file) mcs_file = norm(args.mcs_file) # Locate the TCL template alongside this script scripts_dir = os.path.dirname(os.path.abspath(__file__)) tcl_template = os.path.join(scripts_dir, "step3_write_mcs.tcl").replace("\\", "/") # Validate inputs vivado_exe = os.path.join(vivado_dir, "bin", "vivado.bat") if not os.path.isfile(vivado_exe): print(f"ERROR: Vivado not found: {vivado_exe}", file=sys.stderr) sys.exit(1) if not os.path.isfile(bit_file): print(f"ERROR: .bit file not found: {bit_file}", file=sys.stderr) sys.exit(1) if not os.path.isfile(tcl_template): print(f"ERROR: TCL template not found: {tcl_template}", file=sys.stderr) sys.exit(1) cmd = [ vivado_exe, "-mode", "batch", "-source", tcl_template, "-tclargs", bit_file, mcs_file, ] print(f"[gen_mcs] Vivado: {vivado_exe}") print(f"[gen_mcs] bit : {bit_file}") print(f"[gen_mcs] mcs : {mcs_file}") print() result = subprocess.run(cmd) sys.exit(result.returncode) if __name__ == "__main__": main()