Code Monkey home page Code Monkey logo

pysvinst's Introduction

pysvinst

License: MIT Actions Status codecov PyPI version Join the chat at https://gitter.im/sgherbst/pysvinst

This Python library examines SystemVerilog files to determine what modules are defined and what modules are instantiated. The backend uses sv-parser, which has good support of SystemVerilog 2017.

Purpose

The Verilog language has contains features for defining configs and libraries. However, these features are not well-supported by open-source tools, and even some commercial synthesis tools. By extracting a list of modules defined and instantiated in a file, a user can work around this problem by constructing their own design hierarchy outside of Verilog, and then passing that list of files back into the simulator / synthesis tool.

Installation

This package can be installed via pip:

> pip install svinst

Alternatively, you can clone the repository and build the package yourself. This requires that Rust is installed.

> git clone https://github.com/sgherbst/pysvinst.git
> cd pysvinst
> pip install -e .

Usage

The main functionality of this package is provided through the function get_defs. In this first example, a list of module definitions is returned, each one containing a list module instantiations (if any) contained in that module definition.

>>> from svinst import get_defs
>>> defs = get_defs('tests/verilog/test.sv')
>>> _ = [print(str(def_)) for def_ in defs]
ModDef("A", [
])
ModDef("B", [
])
ModDef("C", [
  ModInst("A", "I0"),
  ModInst("B", "I1")
])
ModDef("D", [
  ModInst("X", "I0"),
  ModInst("Y", "I1")
])

It is also possible to add define variables and include directory paths, since both of these can change the modules that get defined and instantiated:

>>> get_defs('tests/verilog/inc_test.sv', includes=['tests/verilog'])
>>> get_defs('tests/verilog/def_test.sv',
             defines={'MODULE_NAME': 'module_name_from_define', 'EXTRA_INSTANCE': None})

If there is a syntax error, an error message is printed and an Exception is raised.

>>> get_defs('tests/verilog/broken.sv')
parse failed: "tests/verilog/broken.sv"
 tests/verilog/broken.sv:5:10
  |
5 | endmodule
  |  

Finally, the user can get a full syntax tree for advanced processing, using the command get_syntax_tree. That command also allows the arguments includes and defines.

>>> from svinst import get_syntax_tree
>>> tree = get_syntax_tree('tests/verilog/simple.sv')
>>> _ = [print(elem) for elem in tree]
SyntaxNode("SourceText", [
  SyntaxNode("Description", [
    SyntaxNode("ModuleDeclaration", [
      SyntaxNode("ModuleDeclarationAnsi", [
        SyntaxNode("ModuleAnsiHeader", [
          SyntaxNode("ModuleKeyword", [
            SyntaxNode("Keyword", [
              SyntaxToken("module", line=1)
            ])
          ]),
          SyntaxNode("ModuleIdentifier", [
            SyntaxNode("Identifier", [
              SyntaxNode("SimpleIdentifier", [
                SyntaxToken("A", line=1)
              ])
            ])
          ]),
          SyntaxNode("Symbol", [
            SyntaxToken(";", line=1)
          ])
        ]),
        SyntaxNode("Keyword", [
          SyntaxToken("endmodule", line=2)
        ])
      ])
    ])
  ])
])

pysvinst's People

Contributors

gitter-badger avatar sgherbst avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pysvinst's Issues

Instances within generate block

It seems that generate blocks are ignored in svinst/pysvinst. If you save the SV code as top.sv and the Python code as test.py, then python test.py will prove the concept. There are 16 instances described in top.sv, but only one identified by pysvinst.

// top.sv
module top (
  input logic i_clk,
  output logic [15:0] o_clks
);

  genvar i;
  generate 
    for (i=0; i < 16; i++) begin : generate_test
      my_module my_module_inst (
        .i_clk(i_clk),
        .o_clk(o_clks[i])
      );
    end : generate_test
  endgenerate

endmodule
# test.py
defs = svinst.get_defs("top.sv")
print("len(defs):", len(defs))
print("len(defs[0].insts):", len(defs[0].insts))
print("defs[0].insts[0]:", defs[0].insts[0])
# result:
"""
len(defs): 1
len(defs[0].insts): 1
defs[0].insts[0]: ModInst("my_module", "my_module_inst")
"""

It might be a little tricky to add such functionality but I don't think it would necessitate a full elaboration of the design. All that would be needed is to keep track of elab-time constants (defines/parameters/localparams/literal constants/etc...) in order to interpret a generate block's behavior accordingly. The only two constructs that would steer a generate block that I can think of are if/else (something is either instantiated or it isn't) and for loops (something is instantiated N times).

I haven't looked at the Rust code in svinst so I don't know how difficult it would be to add this functionality, but what's your guess? I'm no Rust aficionado but I'm happy to help if you reckon it ought to be within the scope of this project.

Instance name defined in a text macro doesn't work

Example:

`define MOD_INST u_mysubmod
module mymod;
mysubmod `MOD_INST (); 
endmodule

Output:

files:
  - file_name: "some_file.sv"
    defs:
      - mod_name: "mymod"
        insts:

Expected:

files:
  - file_name: "some_file.sv"
    defs:
      - mod_name: "mymod"
        insts:
          - mod_name: "mysubmod"
            inst_name: "u_mysubmod"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.