实现 ALU

Implementing ALU

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

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

Error (suppressible): alu.v(61): (vlog-2388) 'result' already declared in this scope (alu).
Error (suppressible): alu.v(67): (vlog-2388) 'operand0' already declared in this scope (alu).
Error (suppressible): alu.v(67): (vlog-2388) 'operand1' already declared in this scope (alu).
Error (suppressible):alu.v(68): (vlog-2388) 'control' already declared in this scope (alu).
Error: (vlog-13069) alu.v(71): near "<=": syntax error, unexpected <=.
Error: alu.v(71): (vlog-13205) Syntax error found in the scope following 'result'. Is there a missing '::'?

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

我觉得问题出在寄存器和电线的某个地方,但我不确定。

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) 非法引用网络 "result"。 3次

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

  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 块中使用它,否则将其定义为 wire。

你的代码可以这样写:

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