module alu(x, y,instruction,overflow,result);
parameter bit_width=4;
input [bit_width-1:0]x,y;
input [2:0] instruction;
output overflow;
output [bit_width-1:0] result;
reg [bit_width:0] temp;
reg [bit_width-1:0] result;
reg overflow;
initial
overflow=0;
always@ (x or y or instruction)
begin case (instruction)
3'b001:begin temp = {x[bit_width-1], x}+{y[bit_width-1],y};
result <= temp[bit_width-1 :0];
overflow <= temp[bit_width] ^ temp[bit_width-1];
end // 當輸入為001的情況時的加功能為:result<=x+y;
/********** Begin *********/
3'b010:begin temp = {x[bit_width-1], x}-{y[bit_width-1],y};
result <= temp[bit_width-1:0];
overflow <= temp[bit_width] ^ temp[bit_width-1];
end
/********** End *********/ // 請補全上面為*的代碼,實現當輸入為010的情況時的減功能為:result<=x-y;
3'b011:begin result <= x&y; end
3'b100:begin result <= x|y; end
3'b101:begin result <= x^y; end
3'b110:begin result <= {1'b0,x[bit_width-1:1]}; end //實現邏輯右移1位
3'b111:begin result <= x << 1; end //補全該行代碼,實現邏輯左移1位。
default:begin result <= 0; overflow <=0; end
endcase
end
endmodule