-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALU.v
82 lines (76 loc) · 2.6 KB
/
ALU.v
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*-----------------------------------------------------
-- Arithmetic Logic Unit
-- This arithmetic logic unit performs different operations
-- to input data and gives out an output and necessary flags.
-- Data width is given as a parameter to this module.
-- Performed operations are:
-- Addition, Subtraction, Bit Clear, And, Or, Exor and Exnor
-- Designer: Tuna Biçim
-----------------------------------------------------*/
module ALU(A, B, ALU_Control, ALU_Out, CO, OVF, N, Z);
parameter W = 8;
input [W-1:0] A,B; // ALU W-bit Inputs
input [2:0] ALU_Control;// ALU Selection
output [W-1:0] ALU_Out; // ALU W-bit Output
output CO; // Carry Out Flag
output OVF; // Overflow Flag
output N; // Negative Flag
output Z; // Zero Flag
reg [W-1:0] ALU_Result;
assign ALU_Out = ALU_Result; // ALU out
reg [W:0]Carry_Out; //Register to execute carry calculations
reg Overflow; // Overflow bit to determine if the condition occurred
assign CO = Carry_Out[W]; //If the first bit of the carry out is 1 the operation had a carry
assign OVF = Overflow;
assign N = ALU_Result[W-1];
assign Z = ~(|ALU_Result);
always @(*)
begin
case(ALU_Control)
3'b000: begin// Addition
ALU_Result = A + B ;
Overflow = ((~A[W-1])&(~B[W-1])&ALU_Result[W-1])|(A[W-1]&B[W-1]&(~ALU_Result[W-1]));
Carry_Out = {1'b0, A} + {1'b0, B};
end
3'b001: begin// SubtractionAB
ALU_Result = A - B ;
Overflow = ((~A[W-1])&B[W-1]&ALU_Result[W-1])|((A[W-1])&(~B[W-1])&(~ALU_Result[W-1]));
Carry_Out = ~({1'b0, A} + {1'b0, (-B)});
end
3'b010: begin // SubtractionBA
ALU_Result = B - A;
Overflow = ((~A[W-1])&B[W-1]&(~ALU_Result[W-1]))|((A[W-1])&(~B[W-1])&ALU_Result[W-1]);
Carry_Out = ~({1'b0, (-A)} + {1'b0, B});
end
3'b011: begin // Clear
ALU_Result = {W{1'b0}};
Overflow = 1'b0;
Carry_Out = {W+1{1'b0}};
end
3'b100: begin // Logical and
ALU_Result = A & B;
Overflow = 1'b0;
Carry_Out = {W+1{1'b0}};
end
3'b101: begin // Logical or
ALU_Result = A | B;
Overflow = 1'b0;
Carry_Out = {W{1'b0}};
end
3'b110: begin // Logical xor
ALU_Result = A ^ B;
Overflow = 1'b0;
Carry_Out = {W{1'b0}};
end
3'b111: begin // Logical xnor
ALU_Result = ~(A ^ B);
Overflow = 1'b0;
Carry_Out = {W{1'b0}};
end
default: begin ALU_Result = A + B ;
Overflow = 1'b0;
Carry_Out = {W{1'b0}};
end
endcase
end
endmodule