Pyverilog

1.3.0 · active · verified Fri Apr 17

Pyverilog is a Python-based Hardware Design Processing Toolkit for Verilog HDL, providing a parser, dataflow analyzer, controlflow analyzer, and code generator. It allows users to parse Verilog code into an Abstract Syntax Tree (AST), analyze its structure, and extract design information. The current version is 1.3.0, and releases occur periodically, typically every few months, addressing bugs and adding new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a simple Verilog module into an Abstract Syntax Tree (AST) using `VerilogParser`, and then perform a basic dataflow analysis using `DataflowAnalyzer`. It prints key nodes from the AST and basic information from the dataflow graph. To visualize the dataflow graph as an image, you would also need to install Graphviz and use `DotGraph` to write a '.dot' file which can then be rendered externally.

import os
from pyverilog.vparser.parser import VerilogParser
import pyverilog.vparser.ast as vast
from pyverilog.dataflow.dataflow import DataflowAnalyzer
from pyverilog.dataflow.graph import DotGraph

# Create a dummy Verilog file for parsing
verilog_code = """
module test_module (input clk, input rst, output reg [7:0] count);
  always @(posedge clk or posedge rst) begin
    if (rst) begin
      count <= 8'h00;
    end else begin
      count <= count + 1;
    end
  end
endmodule
"""

with open('example.v', 'w') as f:
    f.write(verilog_code)

# 1. Parse the Verilog file into an AST
parser = VerilogParser()
ast = parser.parse(['example.v'])

print("--- Verilog AST --- ")
# ast.show() # Uncomment to print the full AST

# 2. Perform Dataflow Analysis
analyzer = DataflowAnalyzer()
analyzer.visit(ast)

# Get dataflow graph and print some nodes
dfg = analyzer.get_dfg()
print("\n--- Dataflow Analysis (some nodes) ---")
for node_id, node in dfg.items():
    if isinstance(node_id, vast.Port) or isinstance(node_id, vast.Reg):
        print(f"Node: {node_id.name}, Type: {type(node_id).__name__}, Defs: {len(node.definitions)}")

# 3. Generate a DOT graph (requires graphviz and dot command)
# try:
#     graph = DotGraph(dfg)
#     graph.write_dot('dfg.dot')
#     print("\nDataflow graph written to dfg.dot (requires Graphviz to visualize).")
# except Exception as e:
#     print(f"\nCould not generate DOT graph: {e} (Is Graphviz installed?)")

# Clean up the dummy file
os.remove('example.v')

view raw JSON →