python和MyHDL中yield语句的区别

发布于 2024-11-08 01:11:41 字数 589 浏览 1 评论 0原文

我目前正在为我的夏季项目学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪异←思 2024-11-15 01:11:41

首先也是最重要的一点:记住 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.

回首观望 2024-11-15 01:11:41

MyHDL 使用 yield 语句来传达一系列条件,当其中一个为 True 时,将恢复生成器的执行。例如,当时钟从低电平转换到高电平(0 到 1)时,生成器可能会产生条件 clock.ownedge ——当时钟进行此转换时,生成器将恢复。

为了(粗​​略地)模拟其工作原理,这里有一个 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 而不是 引发 StopIteration

var conds 指向 元组 包含条件。在上面的例子中,它指向一个 元组 包含 返回的 2 个 lambda 匿名函数过程

语法:

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

调用 any 内置函数 ,向其传递一个在 conds生成器表达式 > 并评估 if cond(i) 为 True。它是写作的简写:

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

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
桃扇骨 2024-11-15 01:11:41

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文