Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native python functional model #289

Merged
merged 70 commits into from
Jan 12, 2021
Merged

Add native python functional model #289

merged 70 commits into from
Jan 12, 2021

Conversation

Kuree
Copy link
Collaborator

@Kuree Kuree commented Nov 21, 2020

Usage example:

DUT:

class dut(m.Circuit):
    io = m.IO(A=m.In(m.Bits[4]), B=m.In(m.Bits[4]), O=m.Out(m.Bits[4]))
    io.O @= io.A + io.B

Functional model (used for demonstrate class constructor and method call):

class Model:
    @sv(b=DataType.UByte)
    def __init__(self, b):
        self.b = b
 
   @sv(a=DataType.UByte)
    def add(self, a):
        return a + self.b

Test code:

a_value = 1
tester = fault.Tester(dut)
tester.poke(tester.circuit.A, a_value)

model = tester.Var("model", Model)
tester.poke(model, tester.make_call(Model, a_value))

# start testing. using a loop
loop = tester.loop(10)
loop.poke(tester.circuit.B, loop.index)
loop.eval()
loop.expect(tester.circuit.O, tester.make_call(model.add, loop.index))

Nee functions/class introduced

  1. Add make_call to tester class
  2. Add CallExpression to expression
  3. Extend Var to support var.foo, where var is declared tester.Var, and foo is a function name. Compile time check to ensure correctness

Code refactored

  1. Refactor template the support start and exit code region. This can be used for simulator environment setup and teardown.

Simulator tested

  • Xcelium/Incisive
  • VCS
  • Verilator

Discussion

This should solve #280 fundamentally by allowing arbitrary Python code execution inside a simulator. It also enables runtime model check with values unknown during compilation time.

Most of the heavy lifting is done via the pysv package. Currently there are some limitation on how the code should rewritten. Notably how the class and functions import are handled. If there is enough use case for that I can spend some time to solve that issue.

Roadmap

  • I will continue work on bringing the support for arrays. Ideally it should not introduce any changes to fault.
  • I also talked to @sgherbst and he suggested that it might also be possible to do something similar with SPICE simulator. I will look into that.

@Kuree
Copy link
Collaborator Author

Kuree commented Nov 22, 2020

Oddly the tests passed for fpga on buildkite but not the normal one. It seems like conda has some weird python system path setup. I will take a look into it. One workaround is to use venv instead of conda on cadence7, which I've successfully tested.

Edit: Can't reproduce it on cadence7 either. Is there anyway to reproduce the error?

@leonardt
Copy link
Owner

The conda env is setup with the following commands:

conda create -p ./conda_env -m -y pip numpy python=3.7.3
export PATH=$PWD/conda_env/bin:$PATH

so it creates a local environment in the current working directory. I'm guessing we need to configure pysv to use the correspond library load path? Is there a way we can point it to a custom directory, e.g. with an environment variable? I guess this is similar to how venv works with a special directory, so is there anything special done to configure that?

Could it be that pysv is building/linking with the wrong python? How does it find out which one to use? Since we're only editing PATH, maybe we're not setting the right environment variable like PYTHONHOME?

@leonardt
Copy link
Owner

@Kuree
Copy link
Collaborator Author

Kuree commented Nov 30, 2020

For libraries, pysv uses the sys.path of whatever Python it is running with and then have the compiled runtime load that, see here.

As for linking, it uses pybind11's built in capability to detect currently python executable and libraries. The error seems to look like library loading issue.

@Kuree
Copy link
Collaborator Author

Kuree commented Dec 1, 2020

@leonardt I tried to prune unnecessary imports from pysv's side to avoid switching sys.path, still can't reproduce the error.

Here is my working directory on cadence7: /tmp/fault-pysv/

pytest tests/test_pysv.py -v

Conda is created at /tmp/fault-pysv/conda_env using the exact same command.

Tried with several Linux distro with conda and still can't reproduce the error. Would be nice if all the tests are containerized and easy to reproduce.

Copy link
Owner

@leonardt leonardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, finally got CI passing, some minor comments to address before merging

fault/system_verilog_target.py Outdated Show resolved Hide resolved
fault/system_verilog_target.py Outdated Show resolved Hide resolved
fault/verilator_target.py Outdated Show resolved Hide resolved
@leonardt leonardt merged commit 7a7a18f into master Jan 12, 2021
@leonardt leonardt deleted the pysv branch January 12, 2021 17:30
@leonardt leonardt mentioned this pull request Jan 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants