module alu(A,B,ALUOp,C);
input [31:0] A; //The first value to participate in the ALU calculation
input [31:0] B; //The second value to participate in the ALU calculation
input [2:0] ALUOp; //ALU function selection signal
output reg [31:0] C; //Calculation result of ALU
//every signal assigned within an"always" block must be defined as reg
always@(*)//only ALUOp is wrong
begin
case(ALUOp)
//Add
3'b000:
begin
C=A+B;
end
//Sub
3'b001:
begin
C=A-B;
end
//AND
3'b010:
begin
C=A&B;
end
//OR
3'b011:
begin
C=A|B;
end
//srl
3'b100:
begin
C=A>>B;
end
//sra
3'b101:
begin
C=($signed(A))>>>B;
end
/*猜测:在算数右移中>>>,是多次重复调用“右移一次”,
当结束后需再次判断是有符号数,而非无符号数。
使用$signed(A)可直接将A转化为有符号数进行操作。*/
//unsigned comparison operation
3'b110:
begin
if(A>B)
C=1;
else
C=0;
end
//signed comparison operation
3'b111:
begin
if(A[31]==1&&B[31]==1&&A>B)
C=0;
else if(A[31]==1&&B[31]==0)
C=0;
else
C=1;
end
endcase
end
endmodule
猜测:在算数右移中>>>,是多次重复调用“右移一次”,当结束后需再次判断是有符号数,而非无符号数。使用$signed(A)可直接将A转化为有符号数进行操作。