python和MyHDL中yield语句的区别
我目前正在为我的夏季项目学习 MyHDL。 我在理解其中的yield 语句的功能时遇到问题。尽管 MyHDL 确实基于 python,但它以专门的方式使用它的yield 语句。 相同的链接是: http://www.myhdl.org/doc/current/manual /reference.html#myhdl.always
它指出: MyHDL 生成器是带有专门yield 语句的标准Python 生成器。在硬件描述语言中,等效的语句称为敏感度列表。 MyHDL 生成器中的yield 语句的一般格式是: 产量子句 [, 子句 ...] 当生成器执行yield 语句时,其执行将在此时暂停。同时,每个子句都是一个触发器对象,它定义了生成器应恢复的条件。然而,每次调用yield 语句时,生成器都会恢复一次,无论子句的数量如何。这种情况发生在第一次触发时。
我无法理解它。有人可以用简单的话解释一下吗?或者也许将我重定向到另一个来源?
如果您能提供帮助,我将不胜感激。 谢谢!
问候
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先也是最重要的一点:记住 MyHDL 实现是严格纯 Python 的。从这个意义上说,MyHDL 中的yield 语句和Python 中的yield 语句之间没有“区别”。
MyHDL 确实是一种将 Python 用作 HDL 的方法。部分地,这是通过在名为 myhdl 的纯 Python 包中实现一些特定于硬件设计的对象来完成的。例如,有一个 myhdl.Simulation 对象,它以适合硬件模拟的方式运行生成器。
另一部分只是以特定于硬件的方式解释某些 Python 功能。例如,硬件模块被建模为返回生成器的 Python 函数。另一个例子是“yield”语句被解释为“wait”功能。
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.
MyHDL 使用
yield
语句来传达一系列条件,当其中一个为True
时,将恢复生成器的执行。例如,当时钟从低电平转换到高电平(0 到 1)时,生成器可能会产生条件clock.ownedge
——当时钟进行此转换时,生成器将恢复。为了(粗略地)模拟其工作原理,这里有一个 Python 生成器,当满足其条件之一(参数可被 3 或 7 整除)时,它将恢复:
输出:
更新 - 更详细一点关于我使用的一些语法:
我倾向于使用 [
next(gen, [default])
] 函数,因为它比调用gen.next()
更灵活代码>.例如,如果您传递default
参数,当生成器耗尽时,它将返回default
而不是 引发StopIteration
。var
conds
指向 元组 包含条件。在上面的例子中,它指向一个 元组 包含返回的 2 个 lambda 匿名函数过程
。语法:
调用
any
内置函数 ,向其传递一个在conds
生成器表达式 > 并评估if cond(i) 为 True。它是写作的简写:
MyHDL is using the
yield
statement to communicate a list of conditions which, when one of them isTrue
, will resume execution of the generator. For example, a generator might yield conditionclock.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:
Output:
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 callinggen.next()
. For example, if you pass thedefault
arg, when the generator is exhausted it will returndefault
rather than raisingStopIteration
.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 byprocess
.The syntax:
Calls the
any
built-in function, passing it a generator expression which loops overconds
and evaluatesif cond(i) is True
. It is shorthand for writing:Yield 语句用于创建生成器。反过来,这些生成器可以与 MyHDL 中的 Simulation(...) 对象或转换器函数一起使用。换句话说,通过将描述硬件行为的所有生成器传递给模拟器,在 MyHDL 中使用生成器。 Simulation.run() 函数将使用“next()”。
在 MyHDL 手册的 RTL 建模部分中,http://www.myhdl .org/doc/current/manual/modeling.html#example 是一些如何创建 MyHDL 生成器并在模拟中使用它们的好示例。
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.