MyHDL使用yield
語句來傳達的,當其中一人是True
,將恢復發電機的運行條件的列表。例如,當時鍾從低電平轉換到高電平(0到1)時,發生器可能產生條件clock.posedge
- 當時鍾發生此轉換時,發生器將恢復。
爲了模擬(大致)是如何工作的,在這裏是一個Python發生器,其被恢復時的其條件之一(參數是由3整除或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)
輸出:
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
更新 - 一些我使用的語法的更詳細一點:
我傾向於使用[next(gen, [default])
]功能,因爲它比調用gen.next()
更靈活。例如,如果您通過default
參數,當發電機耗盡時,它將返回default
而不是raising StopIteration
。
var conds
指向包含條件的tuple。在這種情況下,它指向包含由process
返回的2 lambda anonymous functions的tuple。
語法:
if any(cond(i) for cond in conds):
conds = next(gen)
調用any
built-in function,傳遞一個generator expression其循環遍歷conds
和評估if cond(i) is True
。它是寫作的簡寫:
for cond in conds:
if cond(i):
conds = next(gen)
break
@samplebias:非常感謝您回覆......但是請您在下面的代碼中解釋以下內容:1)conds = next(gen)意味着conds = gen.next()..對嗎? 2)你能解釋一下「cond cond in conds」是什麼意思...... conds不是我能解釋的列表。那麼我們如何迭代它呢?非常感謝! – Adwaitvedant 2011-05-19 08:33:28
@samplebias:謝謝你的回覆!..忽略我最後的帖子...我仔細檢查了你的例子,並正確理解它!但是我仍然沒有得到答案...在Python中我們必須明確地調用next(conds )當我們想再次調用函數...在myhdl那不是這種情況....當yield語句中的對象「回火」時,它會自動觸發自己(這是他們在文檔中說過的)。這是如何實施的? – Adwaitvedant 2011-05-19 15:51:31
@Adwait我相信他們正在使用Python代碼來生成其行爲不同的HDL。將一些[MyHDL Python源代碼](http://www.myhdl.org/doku.php/cookbook:sinecomp#design)與相應的[生成的Verilog代碼](http://www.myhdl.org/doku.php) /食譜:sinecomp#automatic_conversion_to_verilog)。請注意'yield clock.posedge'如何創建'always(..)'指令。 – samplebias 2011-05-19 16:10:13