Difference between yield statement in python and MyHDL

I am currently studying MyHDL for my summer project. I have a problem with the functioning of the statement of profitability in it. Although it is true that MyHDL is python based, it uses its yield statement in a special way. link to the same: http://www.myhdl.org/doc/current/manual/reference.html#myhdl.always

it says: MyHDL generators are standard Python generators with special output operators. In hardware description languages, equivalent statements are called sensitivity lists. The general format of profitability operators in MyHDL generators is: yield, [...] When the generator executes the yield statement, its execution is paused at this point. At the same time, each sentence is a trigger object that defines the condition under which the generator should be resumed. However, when the yield statement is called, the generator resumes exactly once, regardless of the number of sentences. This happens the first time the trigger fires.

I canโ€™t figure it out. Can anyone explain this in simple words? or maybe redirect me to another source?

I would be grateful if you could help. Thanks!

Hi

+6
source share
3 answers

First of all, remember that the MyHDL implementation is strictly pure Python. In this sense, there is no โ€œdifferenceโ€ between the yield statement in MyHDL and Python.

MyHDL is really a way to use Python as an HDL. This is partly done by implementing some specific hardware design objects in a clean Python package called myhdl. For example, there is a myhdl.Simulation object that runs the generators in a way that is suitable for hardware simulations.

The other part is simply interpreting certain Python functions in a hardware-specific way. For example, a hardware module is modeled as a Python function that returns generators. Another example is that the yield statement is interpreted as a wait function.

+3
source

MyHDL uses the yield to pass a list of conditions that, when one of them is True , resumes the generator. For example, the generator can give the condition clock.posedge , when the clock goes from low to high (from 0 to 1) - when the clock makes this transition, the generator will resume.

To mimic (approximately) how it works, here is a Python generator that resumes when one of its conditions is met (the argument is evenly divided by 3 or 7):

 def process(): j = 0 while True: yield (lambda x: x % 3 == 0, lambda x: x % 7 == 0) j += 1 print 'process j=', j gen = process() conds = next(gen) for i in range(1, 11): print 'i=', i if any(cond(i) for cond in conds): conds = next(gen) 

Conclusion:

 i= 1 i= 2 i= 3 process j= 1 i= 4 i= 5 i= 6 process j= 2 i= 7 process j= 3 i= 8 i= 9 process j= 4 i= 10 

Refresh . A bit more about some of the syntaxes I used:

I tend to use the function [ next(gen, [default]) ], because it is a little more flexible than calling gen.next() . For example, if you pass the argument default when the generator is exhausted, it will return default , rather than raising StopIteration .

var conds points to a tuple containing conditions. In this case, above it points to a tuple containing 2 lambda anonymous functions returned by process .

Syntax:

 if any(cond(i) for cond in conds): conds = next(gen) 

Calls any built-in function , passing it a generator expression that passes through conds and evaluates if cond(i) is True . This is a shorthand for writing:

 for cond in conds: if cond(i): conds = next(gen) break 
+2
source

The yield statement is used to create generators. In turn, these generators can be used with a Simulation (...) object in MyHDL or with converter functions. In other words, the generators are used in MyHDL, passing all the generators that describe the hardware behavior for the simulator. The Simulation.run () function will use "next ()".

The RTL modeling section of the MyHDL manual http://www.myhdl.org/doc/current/manual/modeling.html#example provides some examples of how to create MyHDL generators and use them in a simulation.

+2
source

Source: https://habr.com/ru/post/888415/


All Articles