-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALU_1bit.v
109 lines (77 loc) · 2.72 KB
/
ALU_1bit.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
`include "Utility_Modules.v"
//1-bit ALU module
module ALU_1b_Ordinary(a, b, Less, Ainvert, Binvert, CarryIn, Operation, Result, CarryOut);
input wire a, b, Less, Ainvert, Binvert, CarryIn;
input wire [1:0] Operation;
output wire Result, CarryOut;
//Mux to feed a or a'
wire [1:0] inputs2to1_0;
wire op1;
mux2to1 M0(.op (op1), .ip (inputs2to1_0), .select (Ainvert));
//Giving inputs as a or a'
assign inputs2to1_0[0] = a;
wire inverted_a;
not G0(inverted_a, a);
assign inputs2to1_0[1] = inverted_a;
//Mux to feed b or b'
wire [1:0] inputs2to1_1;
wire op2;
mux2to1 M1(.op (op2), .ip (inputs2to1_1), .select (Binvert));
//Giving inputs as b or b'
assign inputs2to1_1[0] = b;
wire inverted_b;
not G1(inverted_b, b);
assign inputs2to1_1[1] = inverted_b;
//Generating outputs for the final result 4:1 mux
//Mux to decide final output
wire [3:0] inputs4to1;
mux4to1 M2(.op (Result), .ip (inputs4to1), .select (Operation[1:0]));
//1. AND gate
and G2(inputs4to1[0], op1, op2);
//2. OR gate
or G3(inputs4to1[1], op1, op2);
//3. Full-adder
fullAdder A0(.a (op1), .b (op2), .Cin (CarryIn), .sum (inputs4to1[2]), .Cout (CarryOut));
//4. Less
assign inputs4to1[3] = Less;
endmodule
//Defining 1-bit ALU module for the MSB
module ALU_1bit_MSB(a, b, Less, Ainvert, Binvert, CarryIn, Operation, Result, CarryOut, Set, Overflow);
input wire a, b, Less, Ainvert, Binvert, CarryIn;
input wire [1:0] Operation;
output wire Result, CarryOut, Set, Overflow;
//Mux to feed a or a'
wire [1:0] inputs2to1_0;
wire op1;
mux2to1 M0(.op (op1), .ip (inputs2to1_0), .select (Ainvert));
//Giving inputs as a or a'
assign inputs2to1_0[0] = a;
wire inverted_a;
not G0(inverted_a, a);
assign inputs2to1_0[1] = inverted_a;
//Mux to feed b or b'
wire [1:0] inputs2to1_1;
wire op2;
mux2to1 M1(.op (op2), .ip (inputs2to1_1), .select (Binvert));
//Giving inputs as b or b'
assign inputs2to1_1[0] = b;
wire inverted_b;
not G1(inverted_b, b);
assign inputs2to1_1[1] = inverted_b;
//Generating outputs for the final result 4:1 mux
//Mux to decide final output
wire [3:0] inputs4to1;
mux4to1 M2(.op (Result), .ip (inputs4to1), .select (Operation[1:0]));
//1. AND gate
and G2(inputs4to1[0], op1, op2);
//2. OR gate
or G3(inputs4to1[1], op1, op2);
//3. Full-adder
fullAdder A0(.a (op1), .b (op2), .Cin (CarryIn), .sum (inputs4to1[2]), .Cout (CarryOut));
//4. Less
assign inputs4to1[3] = Less;
//Calculating whether overflow has occured
xor G4(Overflow, CarryIn, CarryOut);
//Checking SLT
xor G5(Set, Overflow, inputs4to1[2]); //Checks for a-b<0 for no overflow, or for negative overflow.
endmodule