[英]Creating a 32 bit ALU in Structural Verilog and I'm not quite sure how to implement opcodes
您可以使用在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_a
和signal_b
获得通过到最终输出selected_result
。
通过将多个多路复用器串联在一起,可以将同一模式用于一个以上的选择位(提示:您需要三个)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.