【问题标题】:Implementing ALU实施 ALU
【发布时间】:2015-10-25 16:12:42
【问题描述】:

我正在尝试实现给定特定功能代码的 ALU。

由于某种原因,下面的代码无法运行,并且根据编译器有错误。

错误(可抑制):alu.v(61): (vlog-2388) 'result' 已在此范围 (alu) 中声明。
错误(可抑制):alu.v(67): (vlog-2388) 'operand0' 已在此范围 (alu) 中声明。
错误(可抑制):alu.v(67): (vlog-2388) 'operand1' 已在此范围 (alu) 中声明。
错误(可抑制):alu.v(68): (vlog-2388) 'control' 已在此范围 (alu) 中声明。
错误:(vlog-13069)alu.v(71):靠近“ 错误:alu.v(71): (vlog-13205) 在“结果”之后的范围内发现语法错误。是否缺少'::'?

如果我将 result、operand0、operand1 和 control 的声明删除为线和 regs,我仍然会收到错误消息,指出“结果”超出范围或无法访问。我真的对这部分感到困惑,任何帮助将不胜感激。

我觉得问题出在注册表和电线的某个地方,但我不确定。

module alu 
(
//--------------------------
// Input Ports
//--------------------------
input   [31:0]  operand0, 
input   [31:0]  operand1, 
input   [3:0]   control,
//--------------------------
// Output Ports
//--------------------------
output  [31:0]  result,
output          zero,
output          overflow
);



// Signal Declarations: local params




// Signal Declarations: reg





// Signal Declarations: wire


always @(*)
begin
case(control) 
4'b0000: result= operand0 | operand1; // OR 

4'b0001: begin
        result= operand0 & operand1; // AND
     end  
default: result = 4'b0;
endcase
end


endmodule 

我将上面的代码更改为修改后的版本。我仍然得到错误:

现在它说: (vlog-2110) 非法引用网络“结果”。 3次

【问题讨论】:

    标签: verilog alu


    【解决方案1】:

    您的代码几乎没有问题。有两种声明输入和输出的方法:

      module alu (
             input   [31:0]  operand0, 
             input   [31:0]  operand1, 
             input   [3:0]   control,
    
             output reg [31:0]  result,
             output          zero,
             output          overflow
                 );
    

    第二种方法是:

       module (operand0,operand1,control,result,zero,overflow);
       input [31:0]  operand0; 
       input   [31:0]  operand1;
       input   [3:0]   control;
       output reg [31:0]  result;
       output          zero;
       output          overflow;
    

    我希望你能看到不同之处。因此,在您的代码中,您重新声明了输入和输出。

    还有非阻塞赋值,即 通常用于顺序逻辑。对于组合逻辑阻塞分配是首选,即=

    如果您想将输出定义为 reg,则必须在 always 块内使用,否则将其定义为线。

    你的代码可以写成如下:

    module alu 
    (
    input   [31:0]  operand0, 
    input   [31:0]  operand1, 
    input   [3:0]   control,
    
    output reg [31:0]  result,
    output          zero,
    output          overflow
    );
    
    
    always @(*)
    begin
     case(control) 
      4'b0000: result= operand0 | operand1; // OR 
    
      4'b0001: begin
               result= operand0 & operand1; // AND
               end  
     default : result =4'b0;
    endcase
    end
     endmodule 
    

    【讨论】:

    • 非常感谢。你的解释很清楚!现在,我收到错误“非法引用网络“结果”3 次。我可以不将变量等同于案例中的结果吗?我编辑了上面问题中的语句。我也做了你提到的更改。
    • 默认情况下有错字。我错误地使用了 而不是 = 。我现在已经更正了。
    • 您不能将变量等同于案例中的结果,因为只要输入操作数 0 和操作数 1 改变,输出就会改变。因此,必须将它们添加到敏感度列表中。所以总是@(*) 意味着所有输入都包含在敏感列表中,这将影响输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2019-12-16
    • 2022-07-16
    • 2011-05-04
    • 2010-10-03
    • 2015-10-03
    相关资源
    最近更新 更多