繁体   English   中英

在Structural Verilog中创建32位ALU,但我不太确定如何实现操作码

[英]Creating a 32 bit ALU in Structural Verilog and I'm not quite sure how to implement opcodes

我必须使用AND(000),OR(001),ADD(010),SUB(110),SLT(111)和BEQ(100)的操作码在结构Verilog中创建32位ALU。 我了解这些功能是如何分别在门级工作的,我只是对如何使用操作码来获得所需的输出感到困惑。 例如,如果是高级语言,我会写一些类似的操作码,例如opcode == 100 {output = branch}。 如果这是一个愚蠢的问题,我感到抱歉,我对Verilog还是陌生的,许多在线答案都使用行为Verilog或非常令人困惑。

电路图

您可以使用在Verilog中描述的相同技术...来使用if语句,它们必须位于always块中,如下所示:

always @ (*) begin
  if (operation == 3'b000) begin
    alu_result = and_result;
  end else if (operation == 3'b001) begin
    alu_result = or_result;
  end else if (operation == 3'b010) begin
    alu_result = add_result;
  // ...repeat this pattern for the other operations except BEQ...
  end else begin
    alu_result = beq_result;
  end
end

在此示例中, *_result导线是各个操作的结果值。 该代码将综合到一个多路复用器,该多路复用器在各个结果值之间进行选择(取决于operation ),并驱动alu_result它是最终的ALU输出)。

对于该应用程序,与其使用if语句,不如使用case ,如下所示:

always @ (*) begin
  case (operation)
    3'b000: alu_result = and_result;
    3'b001: alu_result = or_result;
    3'b010: alu_result = add_result;
    // ...repeat this pattern for the other operations except BEQ...
    default: alu_result = beq_result;
  endcase
end

如您所见,这更加紧凑且易于阅读。 如果编写正确,则两个变体都应导致完全相同的多路复用器逻辑。 请注意,在这两个变种,该alu_result需要为类型reg [31:0]因为我们是一个内分配always块,但使用方式wire如果你喜欢:

alu_result = operation == 3'b000 ? and_result
           : operation == 3'b001 ? or_result
           : operation == 3'b010 ? add_result
           // ...repeat this pattern for the other operations except BEQ...
           : beq_result;

编辑

OP表示他需要一个位级的结构复用器代码。

可以通过AND,OR和NOT门创建非常简单的多路复用器。 例如,可以如下创建一个2路复用器:

not(select_inv,select);
and(selected_signal_a,signal_a,select_inv);
and(selected_signal_b,signal_b,select);
or(selected_result,selected_signal_a,selected_signal_b);

在这个例子中, select确定的哪一个signal_asignal_b获得通过到最终输出selected_result

通过将多个多路复用器串联在一起,可以将同一模式用于一个以上的选择位(提示:您需要三个)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM