MyHDL Discourse

Changing testbench inputs during a simulation


#1

I’m trying to use a Python-Verilog interface to simulate a CPLD (or rather, a web interface for a CPLD) that allows a user who enters the website to be able to enter Verilog code for that CPLD, compile it, and simulate it while being able to change the inputs as they would with the CPLD itself.

I understand cosimulation would be the way to go, since users would be writing in Verilog and I would be using Python MyHDL to write the test bench. However from what I’ve read about MyHDL, the inputs to the simulation are fixed, i.e. they can’t be changed outside of a static test bench design.

My goal is for users to change inputs to the CPLD on my website, and reflect those changes in the simulation while it is running. How would I achieve that in MyHDL?


#2

I have used the pyparsing verilog parser to extract the ports from verilog files and dynamically create the port interface.

It has been awhile since I have used the code. You can see if it could be useful:

The prep_cosim is where the Cosimulation is instantiated, should be able to follow from there and see how the ports are extracted.


#3

Do you want to change the pins function (In, Out…) dynamically ? Or do you want to inject external signals(from web site) in the design being simulated ?


#4

Yes, the website has an interactive SVG of the CPLD attached to a bunch of switches, lights, character displays and buttons. By toggling the switches or buttons, a user gets to observe what kind of code they’ve implemented as if it was burned directly on to the CPLD.

As you said, I’m trying to do this dynamically, with the user able to set their inputs and see the expected output in real time via the simulator. I managed to get that working, so now I have a function that sets the next values for each of the signals based on global variables. The difficulty I’m facing right now is connecting my JavaScript code to the Python file in order to run the simulation and exchange I/O (send inputs, get outputs) as a stream.

I understand this is web dev/Python domain and not strictly a myHDL topic, but I was hoping there was something in the library that allows control of the processes in order to perform the I/O exchange from the webpage.


#5

Thank you! But I managed to get past that hurdle over the last few days. I’ve described my next issue below.


#6

The javascript part is run on the client machine. The Python part is run on the server machine. You need a way for both ends to communicate. What do you intend to use ? Ajax ? WebSocket ?


#7

I’ve been using Ajax for trials, but I’ve been advised to use WebSocket by my advisor on the project, but I’m not really familiar with it.
What I have realized though, is that I might need whichever one provides the best streaming capability to stream the output from the simulator to the website, and the input for vice versa. It seems like both are easy to implement, but which one would you pick?


#8

I would choose websockets.
Do you know Brython (http://brython.info/) ?
You can’t make MyHDL run inside Brython but you get a “full python” dev environment. Especially if you use a Python based web server.
I realized a full app with CherryPy+ws4py+Brython years ago. The websocket was used to transfer real time data (audio levels) from the server to the client.through a websocket. The actions on the client (button click) where transferred to the server through a websocket. The client interface was managed with Brython.
This was just a test case but it was working pretty well.


#9

I got it to work! Thank you so much!
I didn’t use Brython/CherryPy/ws4py (I got quite lost in the description), but I did use a package called websockets (not sure if this is the same thing) on Python to start a WebSocket server that the site would connect to (served by an Apache server) and create a WS connection to. I send the Verilog code to be compiled, which if successfully compiled by iverilog, is then simulated using myHDL. The successful state is sent to the state, which then starts transmitting inputs and getting back outputs from the server.

This works perfectly for one user. However, I now need to figure out how to make this threaded, i.e. create a new thread for simulation every time a new client connects. I’m currently rewriting the code to do this, but if you have some suggestions for me, that would be great! Thank you for your help so far!