首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Verilog - If条件

Verilog - If条件
EN

Stack Overflow用户
提问于 2017-10-02 19:41:17
回答 2查看 3.2K关注 0票数 0

在Quartus Prime V16.0上编译时,我在Verilog中遇到了这个模块的问题。目标是如果总共8个输入位中的任何一个位是1,则返回一个特定的8位数00011000的输出。这个if语句有效吗?如果不是,什么是更好的实现方法?运算符在返回1时是这样工作的吗?从我对它的研究来看。

or运算符的输入是4位A (in7:4)和4位B (in3:0)

代码语言:javascript
复制
module case3 (in, out);

input [7:0] in;
output [7:0] out;
wire x, y;

assign x = 1;

if (x == or(y, in[7:4], in[3:0]))
    assign out[7:0] = 8'b00011000;

endmodule

我的主模块中的函数调用如下所示:

代码语言:javascript
复制
case3 u3(
    .in(SW[7:0]),
    .out(wire3)
);

Wire3是定义为从Case3模块检索输出以供进一步使用的连接变量。

EN

回答 2

Stack Overflow用户

发布于 2017-10-04 14:03:12

您可以根据自己的目的执行类似以下代码的操作。

代码语言:javascript
复制
module case3 (in, out);

input [7:0] in;
output [7:0] out;

assign out[7:0] = (|in) ? 8'b00011000 : <Else Case>;

endmodule

在这里,|in将对in的所有位执行or,并将返回单个位输出。因此,如果in的任何位为1,那么|in将返回1

票数 1
EN

Stack Overflow用户

发布于 2017-10-03 00:08:59

正如上面的评论所说,甚至很难知道从哪里开始。所以,我将回答这个问题,希望通过我的回答,你会得到一些关于你应该去哪里的指导。如果这是家庭作业,那么恭喜你!-我已经为你做了家庭作业。不管是不是,如果你的意图是学习Verilog,那么你需要试着理解为什么它是这样写的。

因此,假设您的规范是

目标是如果总共8个输入位中的任何一个位是1,则返回一个特定的8位数00011000的输出。

我认为像这样的东西将会起到作用:

代码语言:javascript
复制
module case3 (in, out);  // This is very old-fashioned syntax. Since 2001, we've been writing
  input [7:0] in;        //   module case3 (input [7:0] in, output reg [7:0] out);
  output [7:0] out;      // 'out' must be a 'reg'. By default outputs are wires and it is 
  reg[7:0] out;          // illegal to assign to a wire from an 'always' block

  always @(*)            // 'if' is illegal outside an 'initial' block or an 'always' block. 
                         // Verilog is NOT a programming language, it is a HARDWARE DESCRIPTION
                         // language. Lines inside a 'module' are NOT executed sequentially,
                         // but concurrently, ie all at once, just like hardware.
                         // Lines inside an 'initial' or 'always' block are executed sequentially:
                         // think of an 'always' block is "a little bit of software that models
                         // a little bit of hardware".

    if (in)              // if 'in' is non-zero, Verilog considers it to be 'true'

      out = 8'b00011000; // don't say 'assign' - it is legal here, but means something weird
                         // and there is no need to say out[7:0]
    else
      out =              // who knows? You didn't say.

endmodule

免责声明:我没有测试过这一点。(我怎么能呢?其中有一点缺失。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46524785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档