'如果'与“何时”相比用于制作多路复用器
我被告知使用“when”语句来制作多路复用器,但不要使用“if”语句,因为它会导致计时错误... 我不明白这个... 那么“如果”和“何时”有什么区别?它们在硬件中映射到相同的东西吗?
i have been told to use 'when' statement to make multiplexer but not use 'if' statement as it will cause timing errors...
i don't understand this ...
so what is the difference between 'if' and 'when' ? and do they map to the same thing in hardware ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,让我们首先讨论一下 if 和when 语句之间的区别:
When 语句
if 语句
并且您知道多路复用器是一个不需要进程块的组件,因为它的行为不会随着输入的改变而改变,所以它将在进程之外,所以你必须使用when语句来编写它,因为它是并发语句。如果你用if语句编写它,可能会出现计时错误。此外,所有参考资料以及 Xilinx 帮助(如果您使用的是 Xilinx)都使用 when 语句而不是 if 语句编写多路复用器块
参考:数字设计原则与方法《实践》,约翰·F·韦克利,第三版
OK, lets discuss some points at first on the difference between if and when statements:
when statement
if statement
And you know multiplexer is a component don't need process block, as its behavior doesn't change with changing its input, so it will be outside process, so you have to write it using when statement as it is concurrent statement.. And if you wrote it with if statement, timing errors may occur. Also all the references and also Xilinx help (if you are using Xilinx) are writing the Multiplexer block using when statement not if statement
Reference: Digital Design Priciples & Practices, John F. Wakerly, 3rd Edition
看看这些:
when.
if.
基本上,
if
是顺序的,when
是并发的。它们不会映射到硬件中的同一事物... 此页面描述,在底部,合成if
语句所需的一些特殊注意事项。See these:
when
.if
.Basically,
if
is sequential, andwhen
is concurrent. They do not map to the same thing in hardware... This page describes, at the bottom, some of the special considerations needed to synthesize anif
statement.两种编码风格都是完全有效的。
让我们回忆一下一些元素。从HDL开始,综合分两个主要步骤完成:
这两个步骤清楚地将 RTL 功能需求(控制逻辑、计算)与技术意外事件(时序等)分开。
让我们回到第一步(RTL):
关于多路复用器,可以使用多种编码风格:
使用并发分配:
y<= a1 when cond1 else a2 when cond2 else cond3;
在流程中使用 if 语句:
进程(a1,a2,a3,cond1,cond2)
开始
如果(条件1)那么
y≤a1;
elsif(cond2) 那么
y≤a2;
别的
y≤a3;
结束如果;
end;
使用另一个并发分配
形式,适合通用
描述:如果 sel 是一个整数
并 muxin 一个信号数组,然后:
muxout <= muxin(sel); 推断多路复用器
请注意,3 种编码风格始终有效。还要注意,它们比简单的多路复用器“多一点”,因为编码风格强制存在优先级编码(if elsif,when else),这不是基于简单方程的多路复用器的情况,真正对称。
使用案例陈述
进程(a1,a2,a3,cond1,cond2)
变量 cond : std_logic(1 降到 0);
开始
条件 := 条件2 &条件1;
情况条件是
当“01”=>时y<=a1;
当“10”=>时y≤a2;
当别人=> y≤a3;
最终情况;
end;
使用 select 语句(在我们的
例如,两个并发作业
需要):
sel <= cond2 &条件1;
带选择
y <= a1 当“01”时,
a2 当“10”时,
a3 当其他人时;
最后一点是关于抽象的兴起,即使对于 RTL 设计也是如此:合成器现在已经非常成熟了。例如,看看 Jiri Gaisler 的 LEON2 开源处理器编码风格,以及他的编码风格 (参见此处)。他采用了一种与经典书籍截然不同但完全有效的方法。
您应该始终了解 RTL 合成器将推断出什么。
相反,行为综合可以让您(部分)忘记合成器将推断出的内容。但那是另一个故事了。
Both coding styles are totally valid.
Let's recall some elements. Starting from HDL, synthesis is done in two main steps :
These two steps clearly separates RTL functional needs (steering logic, computations) from technology contingencies (timing etc).
Let's come back to the first step (RTL) :
Concerning multiplexers, several coding styles are possible :
using concurrent assignement :
y<= a1 when cond1 else a2 when cond2 else cond3;
using if statement within a process :
process(a1,a2,a3,cond1,cond2)
begin
if(cond1) then
y<=a1;
elsif(cond2) then
y<=a2;
else
y<=a3;
end if;
end;
using another concurrent assignment
form, suitable for generic
descriptions : if sel is an integer
and muxin an array of signals, then :
muxout <= muxin(sel); --will infer a mux
Note that the 3 coding styles always work. Note also that they are "a bit more" than simple multiplexer as the coding style force the presence of a priority encoding (if elsif, when else), which is not the case of a simple equation-based multiplexer, really symmetric.
using a case statement
process(a1,a2,a3,cond1,cond2)
variable cond : std_logic(1 downto 0);
begin
cond := cond2 & cond1;
case cond is
when "01" => y<= a1;
when "10" => y<= a2;
when others => y<=a3;
end case;
end;
using a select statement (in our
example, two concurrent assignements
needed) :
sel <= cond2 & cond1;
WITH sel SELECT
y <= a1 WHEN "01",
a2 WHEN "10",
a3 WHEN OTHERS;
A final remark is about the rising of abstraction, even for RTL design : the synthesizers are now really mature. Have a look at Jiri Gaisler coding styles for LEON2 open source processor for example, as well as his coding styles (see here). He prones a very different approach, yet perfectly valid, from classical books.
You should always understand what the RTL synthesizer will infer.
In the contrary, behavioral synthesis allows you to forget (partially) what the synthesizer will infer. But that's another story.