【问题标题】:Verilog - If ConditionVerilog - 如果条件
【发布时间】:2017-10-02 11:41:17
【问题描述】:

在 Quartus Prime V16.0 上编译时,我在 Verilog 中遇到了这个模块的问题。目标是如果 8 个总输入位中的任何一个位是 1,则返回一个特定的 8 位数字 [00011000] 的输出。这个 if 语句是否有效?如果没有,有什么更好的实施方法?运算符是否以这种方式返回 1?根据我的研究,它确实如此。

or 运算符的输入是 4 位 A (in[7:4]) 和 4 位 B (in[3:0])

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

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

case3 u3(
    .in(SW[7:0]),
    .out(wire3)
);

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

【问题讨论】:

  • 很难知道从哪里开始,但我建议回到你的教科书并复习 1)Verilog 原语的用法(你的“或”那里)2)赋值语句的用法 3)总是阻塞。最后,您的问题陈述不完整。当输入全为 0 时会发生什么?即使这是合法的语法,也存在其他逻辑问题。
  • 只是添加到 Brian 的评论中,请同时查看您的术语,case3 是不是函数。所以,首先开始阅读有关 verilog hdl 的概念。它不是通用编程语言。
  • 如果您的问题得到解决,请标记答案。

标签: if-statement binary switch-statement logic verilog


【解决方案1】:

您可以根据自己的目的执行以下代码。

module case3 (in, out);

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

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

endmodule

这里|in 将对in 的所有位进行oring,并将返回单个位输出。因此,如果in 的任何一位是1,那么|in 将返回1。

【讨论】:

    【解决方案2】:

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

    所以,假设你的规范是

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

    我认为这样的事情可以解决问题:

    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
    

    免责声明:我没有对此进行测试。 (我怎么可能?有点少了。)

    【讨论】:

    • @Serge out 是 reg,而不是电线。查看我的代码的第 4 行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多