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