1
0
Fork 0
2023-fpga-computer/src/sap2/alu.v

65 lines
849 B
Verilog

module alu(
input clk,
input[7:0] a,
input[2:0] op,
input load,
input en,
inout[15:0] bus
);
reg[7:0] alu = 0;
reg[7:0] tmp = 0;
localparam OP_ADD = 0;
localparam OP_SUB = 1;
localparam OP_AND = 2;
localparam OP_OR = 3;
localparam OP_XOR = 4;
localparam OP_CMA = 5;
localparam OP_RAL = 6;
localparam OP_RAR = 7;
always @(posedge clk) begin
if (load) begin
tmp <= bus[7:0];
end
end
always @(*) begin
case (op)
OP_ADD: begin
alu = a + tmp;
end
OP_SUB: begin
alu = a - tmp;
end
OP_AND: begin
alu = a & tmp;
end
OP_OR: begin
alu = a | tmp;
end
OP_XOR: begin
alu = a ^ tmp;
end
OP_CMA: begin
alu = ~a;
end
OP_RAL: begin
alu = a << 1;
alu[0] = a[7];
end
OP_RAR: begin
alu = a >> 1;
end
default: begin
alu = 0;
end
endcase
end
assign bus = (en) ? alu : 16'bz;
endmodule