MyHDL as of now provides a few powerful conversion features through AST (abstract syntax tree) parsing. So Python code modules can be parsed ‘inline’ and emit VHDL Verilog, or my own XML based concoction XHDL (work in progress).
Simple counter example
Let’s have a look at a MyHDL code snippet. This is a simple counter, that increments when ce is true (high). When it hits a certain value, the dout output is asserted to a specific value.
@block def test_counter(clk, ce, reset, dout, debug): counter = Signal(modbv(0)[8:]) d = Signal(intbv(3)[2:]) @always_seq(clk.posedge, reset) def worker(): if ce: counter.next = counter + 1 @always_comb def assign(): if counter == 14: dout.next = 1 debug.next = 1 elif counter == 16: debug.next = 1 dout.next = 3 else: debug.next = 0 dout.next = 0 return instances()
When we run a simple test bench which provides a clock signal clk, and some pseudo random assertion of the ce pin, we get this:
Now, how do we pass this simple logic on to yosys for synthesis?
Pyosys python wrapper
The pyosys module is a generated python wrapper, covering almost all functionality from the RTLIL yosys API. In short, it allows us to instanciate a design and hardware modules and add hardware primitives. It’s like wiring up 74xx TTL logic chips, but the abstract and virtual way.
Means, we don’t have to create Verilog or VHDL from Python and run it through the classic yosys passes, we can emit synthesizeable structures directly from the self-parsing HDL.
Way to synthesis
Now, how do we get to synthesizeable hardware, and how can we control it?
We do have a signal representation after running the analysis routines of MyHDL. Like we used to convert to the desired transfer language, we convert to a design, like:
a = test_counter(clk, ce, reset, dout, debug) a.convert("verilog") a.convert("yosys_module")
The yosys specific convert function, as of now, calls the pyosys interface to populate a design with logic and translates the pre-analysed MyHDL signals into yosys Wire objects and Signals, that are finally needed to create the fully functional chain of the logic zoo. The powerful ‘dot’ output allows us to look at what’s being created from the above counter example (right click on image and choose ‘view’ to see it in full size):
You might recognize the primitives from the hardware description. A compare node
if counter == 14 translates directly to the $eq primitive with ID $2. A Data flip flop ($dff) however is generated somewhat implicit by the @always_seq decorator from the output of a multiplexer. And note: This $dff is only emitted, because we have declared the reset signal as synchronous from the top level definition. Otherwise, a specific asynchronous reset $adff would be instanciated.
The multiplexers finally are those nasty omnipresent elements that route signals or represent decisions made upon a state variable, etc.
You can see a $mux instanciated for the reset circuit of the worker() function, appended to another $mux taking the decision for the ce pin whether to keep the counter at its present value or whether to increment it ($11). The $pmux units are parallel editions that cover multiple cases of an input signal. Together with the $eq elements, they actually convert well to a lookup table — the actual basic hardware element of the FPGA.
Now, how would we verify if the synthesized output from the MyHDL snippet works correctly? We could do that using yosys’ formal verification, but there’s a step we can take beforehand: Co-Simulation from within MyHDL against a known working reference, like a Verilog simulation.
In short, this is what’s happening:
- Functional MyHDL simulation of the unit under test with random stimulation
- Generation of Verilog code of the synthesized result
- Comparison of the MyHDL model output against the Verilog simulation output by the cycle-synchronous Co-Simulation functionality of MyHDL
There are some advantages to this approach:
- We can verify the basic correctness of direct Python HDL to yosys synthesis
- We can match against a known good reference of a Verilog simulator by emitting Verilog code via MyHDL
- Likewise, we can also verify against emitted VHDL code
- Set up more complex test suite and coverage scenarios (github ‘yosys’ myhdl fork in preparation)
- Run a really big project through it
- Acquire alpha testers and developers
- [ Extensions/Changes of MyHDL to modularize for large projects ]