-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalu.sv
More file actions
53 lines (48 loc) · 1.61 KB
/
alu.sv
File metadata and controls
53 lines (48 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// alu
//
// code | operations
// ------------------
// 0000 | add x
// 0001 | sub x
// 0010 | sll
// 0011 | slt
// 0100 | sltu
// 0101 | xor x
// 0110 | srl
// 0111 | sra
// 1000 | or x
// 1001 | and x
// ------------------
module alu(
input logic [31:0] in1, in2,
input logic [3:0] op,
output logic [31:0] result,
output logic negative, zero
);
logic [31:0] sraResult;
logic [31:0] sltResult;
logic [31:0] sltuResult;
assign sraResult = $signed(in1) >>> in2;
assign sltResult = ($signed(in1) < $signed(in2)) ? 32'b1 : 32'b0;
assign sltuResult = (in1 < in2) ? 32'b1 : 32'b0;
assign result = (op == 4'b0000) ? (in1 + in2) : // plus
(op == 4'b0001) ? (in1 - in2) : // minus
(op == 4'b1001) ? (in1 & in2) : // and
(op == 4'b1000) ? (in1 | in2) : // or
(op == 4'b0101) ? (in1 ^ in2) : // xor
(op == 4'b0010) ? (in1 << in2) : // sll (shift left logical)
(op == 4'b0110) ? (in1 >> in2) : // srl (shift right logical)
(op == 4'b0111) ? sraResult : // sra (shift right arithmetic)
(op == 4'b0011) ? sltResult : // slt
(op == 4'b0100) ? sltuResult // sltu
: 32'hxxxxxxxx;
assign negative = result[31];
assign zero = ~|result;
// always @(*) begin
// $display("op %b", op);
// $display("in1 %b", in1);
// $display("in2 %b", in2);
// $display("result %b", result);
// $display("zero %b", zero);
// end
endmodule