为什么这个Verilog函数中没有赋值语句?

发布于 2024-10-19 19:24:07 字数 742 浏览 6 评论 0原文

来自 C++ 背景的我开始学习 Verilog。该代码描述了进入两个与门的四个输入。这两个与门的输出进入或门。或门的输出是最终的输出。

// a user-defined AND gate
module my_and2 (in, out);
input [1:0] in;
output out;
assign out = in[1]&in[0];
endmodule

// a user-defined OR gate
module my_or2 (in, out);
input [1:0] in;
output out;
assign out = in[1]|in[0];
endmodule

// the AND-OR logic built on top of the user-defined AND and OR gates
module and_or (in_top, out_top);
input [3:0] in_top;
output out_top;
wire [1:0] sig;
// instantiate the gate-level modules
my_and2 U1 (.in(in_top[3:2]),.out(sig[1]));
my_and2 U2 (.in(in_top[1:0]),.out(sig[0]));
my_or2 U3 (.in(sig),.out(out_top));
endmodule

前两个模块对我来说很有意义。然而,最后一个却没有。前两个模块末尾有一个赋值语句,用于设置输出变量的值。然而,最后一个却没有。这是为什么?

Coming from a C++ background I'm starting to learn Verilog. This code describes four inputs going into two AND gates. The outputs from those two AND gates go into an OR gate. The output from the OR gate is the final output.

// a user-defined AND gate
module my_and2 (in, out);
input [1:0] in;
output out;
assign out = in[1]&in[0];
endmodule

// a user-defined OR gate
module my_or2 (in, out);
input [1:0] in;
output out;
assign out = in[1]|in[0];
endmodule

// the AND-OR logic built on top of the user-defined AND and OR gates
module and_or (in_top, out_top);
input [3:0] in_top;
output out_top;
wire [1:0] sig;
// instantiate the gate-level modules
my_and2 U1 (.in(in_top[3:2]),.out(sig[1]));
my_and2 U2 (.in(in_top[1:0]),.out(sig[0]));
my_or2 U3 (.in(sig),.out(out_top));
endmodule

The first two modules make sense to me. However, the last one doesn't. The first two modules have an assign statement at the end to set the value for the output variable. However, the last one doesn't. Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

活雷疯 2024-10-26 19:24:07

Verilog 是“事件驱动”的。编写 verilog 时,请根据敏感度列表进行思考。

在 AND 门的示例中,您有表达式 assign out = in[1]&in[0];。据说您的表达式对 in[0]in[1] 敏感。这意味着只要 in[0]in[1] 发生变化,表达式就会重新计算,并且 out 的值将是已更新。

因此,在顶级模块 and_or 中,您基本上构建了一个对前面表达式的输出敏感的大表达式树。当然,这棵树是使用模块连接构建的。因此,该顶级模块的输入之一的值的变化将波及其“逻辑锥”中的所有表达式。

要驱动输入,您需要将更高级别的测试平台模块驱动信号到您的 and_or 模块中。这将提供时间间隔的输入,这将触发 and_or 中及其下方的表达式。如果没有,您的 sim 将不会有任何事件,因此不会触发任何表达式,并且 sim 会在 0ps 时超时,因为它处于“事件匮乏”状态。

PS:对于您的 AND 门表达式,assign out = ∈ 也可以工作...(归约 AND 运算符)

Verilog is 'event driven'. When writing verilog, think in terms of sensitivity lists.

In your example of the AND gate, you've the expression assign out = in[1]&in[0];. Your expression is said to be sensitive to in[0] and in[1]. This means that any time in[0] or in[1] change, the expression will be recomputed, and the value of out will be updated.

So in your toplevel module and_or, you're basically building a big tree of expressions that are sensitive to the outputs of the preceding expressions. This tree is, of course, built using the module connections. So a change in the value of one of the inputs to this toplevel module will ripple through all expressions in its 'logic cone'.

To drive the inputs you'll need higher level testbench module driving signals into your and_or module. This will supply inputs spaced out in time which will trigger the expressions in and below and_or. If not, your sim will have no events, so no expressions will trigger and the sim will time-out at 0ps because it is 'event starved'.

PS: for your AND gate expression, assign out = ∈ will work too... (reduction AND operator)

梦与时光遇 2024-10-26 19:24:07

out_top 由 U3 实例输出驱动。

out_top is driven by the U3 instance output.

书间行客 2024-10-26 19:24:07

简而言之,我喜欢将实例化视为连接电线。

模块是数字电路块。与门和或门模块是魔法发生的地方。你已经明白那部分了。通过实例化这些模块,就像将顶层模块的输入线与两个块 AND 模块的输入连接起来一样。然后取出它们的输出并将它们粘贴到从 OR 块伸出的输入线上。最后,将 OR 块的输出连接到顶层的输出信号线。

To put things simply, I like to think instantiation as just connecting wires.

Modules are blocks of digital circuits. You AND and OR gate modules are where magic happens. You already understand that part. By instantiating those modules, it's like you're connecting the input wires of your top level module with inputs of two blocks AND module. Then taking the outputs of them and taping them to the input wire sticking out of your OR block. And finally you're connecting the output of OR block to the output signal wire of top level.

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