File size: 1,501 Bytes
e3ca86e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
"""
Generate a PNG netlist diagram from one or more Verilog files using yosys show.

Usage:
    python scripts/visualize_netlist.py design.v [extra.v ...] [-o output.png] [-m top_module]

Requires: yosys + graphviz (dot) installed.
"""

import argparse
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

from server.evaluator import VerilogEvaluator


def main():
    parser = argparse.ArgumentParser(description="Visualize Verilog netlist as PNG")
    parser.add_argument("files", nargs="+", help="Verilog source files")
    parser.add_argument("-o", "--output", default="netlist.png", help="Output PNG path (default: netlist.png)")
    parser.add_argument("-m", "--module", default=None, help="Top module name (optional)")
    args = parser.parse_args()

    sources = {}
    for fpath in args.files:
        p = Path(fpath)
        if not p.exists():
            print(f"ERROR: file not found: {fpath}", file=sys.stderr)
            sys.exit(1)
        sources[p.name] = p.read_text()

    evaluator = VerilogEvaluator()
    result = evaluator.visualize(sources, top_module=args.module)

    if not result.success:
        print(f"Visualization failed:\n{result.stderr}", file=sys.stderr)
        sys.exit(1)

    import base64
    png_bytes = base64.b64decode(result.image_b64)
    out = Path(args.output)
    out.write_bytes(png_bytes)
    print(f"Netlist diagram saved to: {out.resolve()}")


if __name__ == "__main__":
    main()