Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between yield statement in python and MyHDL

I am currently learning MyHDL for my summer project. I have a problem grasping the functioning of yield statement in it. Though its true that the MyHDL is based upon python, it uses its yield statement in a specialized way. the link for the same is : http://www.myhdl.org/doc/current/manual/reference.html#myhdl.always

it states: MyHDL generators are standard Python generators with specialized yield statements. In hardware description languages, the equivalent statements are called sensitivity lists. The general format of yield statements in in MyHDL generators is: yield clause [, clause ...] When a generator executes a yield statement, its execution is suspended at that point. At the same time, each clause is a trigger object which defines the condition upon which the generator should be resumed. However, per invocation of a yield statement, the generator resumes exactly once, regardless of the number of clauses. This happens on the first trigger that occurs.

I am not able to comprehend it. Could someone please explain it in simple words? or perhaps redirect me to another source?

I'll be grateful if you could help. Thanks!

Regards

like image 725
Adwaitvedant Avatar asked May 18 '11 12:05

Adwaitvedant


3 Answers

First and foremost: remember that the MyHDL implementation is strictly pure Python. In that sense there is no "difference" between a yield statement in MyHDL and in Python.

MyHDL really is a way to use Python as a HDL. Partially, this is done by implementing some hardware design specific objects in a pure Python package called myhdl. For example, there is a myhdl.Simulation object that runs generators in a way suited for hardware simulation.

The other part is simply interpreting certain Python features 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" functionality.

like image 160
Jan Decaluwe Avatar answered Oct 27 '22 04:10

Jan Decaluwe


MyHDL is using the yield statement to communicate a list of conditions which, when one of them is True, will resume execution of the generator. For example, a generator might yield condition clock.posedge when the clock transitions from low to high (0 to 1) -- when the clock makes this transition, the generator will be resumed.

To simulate (roughly) how this works, here is a Python generator which is resumed when one of its conditions (argument is evenly divisible by 3 or 7) are met:

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)

Output:

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

Update - A bit more detail on some of the syntax I used:

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

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

The syntax:

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

Calls the any built-in function, passing it a generator expression which loops over conds and evaluates if cond(i) is True. It is shorthand for writing:

for cond in conds:
    if cond(i):
        conds = next(gen)
        break
like image 2
samplebias Avatar answered Oct 27 '22 03:10

samplebias


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

In the RTL modeling section of the MyHDL manual, http://www.myhdl.org/doc/current/manual/modeling.html#example, are some good examples how to create the MyHDL generators and use these in a simulation.

like image 2
Christopher Felton Avatar answered Oct 27 '22 05:10

Christopher Felton