-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathALU.v
More file actions
49 lines (37 loc) · 994 Bytes
/
ALU.v
File metadata and controls
49 lines (37 loc) · 994 Bytes
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
`timescale 10ns/1ns
module alu
// IO ports
(
input wire [31:0] a,
input wire [31:0] b,
input wire [3:0] alu_control,
output reg [31:0] alu_result,
output wire zero_flag
);
// ALU control symbolic representation
localparam [3:0]
AND = 4'b0000,
OR = 4'b0001,
ADD = 4'b0010,
SUB = 4'b0010,
SLT = 4'b1000;
// body
always @(*)
begin
case(alu_control)
AND:
alu_result = a & b; // BITWISE AND
OR:
alu_result = a | b; // BITWISE OR
ADD:
alu_result = a + b; // ADD
SUB:
alu_result = a - b; // SUBTRACT
SLT:
alu_result = (a < b) ? 32'b1 : 32'b0; // SET ON LESS THAN
default:
alu_result = 32'bx;
endcase
end
assign zero_flag = (alu_result == 32'b0) ? 1'b1 : 1'b0;
endmodule