-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALU_microprocessor.v
More file actions
280 lines (248 loc) · 9.78 KB
/
ALU_microprocessor.v
File metadata and controls
280 lines (248 loc) · 9.78 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company : Self-made
// Engineer : Muhammad Abdur-Rehman Siddiqui
//
// Create Date : 15:55:51 06/12/2020
// Design Name : 32-bit Advanced Microprocessor Design
// Module Name : ALU_microprocessor
// Project Name : Single cycle 16-bit ARM Microprocessor
// Target Devices: Any FPGA family
// Tool versions : ISE Design Suite 15.7
// Description : ALU module for the processor
//
// Dependencies : None
//
// Revision : 0.1
// Revision 0.01 - File Created
// Additional Comments:
//
// An advanced working ALU module which can perform several mathematical and
// logical operations on the operands
//////////////////////////////////////////////////////////////////////////////////
module ALU_microprocessor (
input [ 5:0] alu_ctrl ,
input [31:0] in_1 ,
input [31:0] in_2 ,
input alu_clk ,
output reg [31:0] alu_rslt ,
output [ 4:0] alu_checks // Flag values
);
reg N; // Sign bit (to see if result is negative(1) or not(0))
reg Z; // Zero bit (to see if result is zero(1) or not(0))
reg C; // Carry bit (to see if result generates carry(1) or not(0))
reg V; // Overflow bit (to see if overflow occurs(1) or not(0))
reg P; // Parity bit (to see if odd parity exists(1) or not(0))
/*
==============================================================================================
ARITHMETIC OPERATIONS FOR THE ALU DESIGN
In arithmetic operations, flag bits greatly effect by the
results generated
==============================================================================================
*/
always @(posedge alu_clk) begin
case(alu_ctrl)
5'd0 : begin
{C,alu_rslt} = in_1 + in_2; // ADD operation
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = in_1[31]&&(in_2[31])&&(!alu_rslt[31]) || (!in_1[31])&&(!in_2[31])&&alu_rslt[31];
P = ^alu_rslt;
end
5'd1 : begin
{C,alu_rslt} = in_1 + (-in_2); // SUB operation
C = !C;
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = in_1[31]&&(!in_2[31])&&(!alu_rslt[31]) || (!in_1[31])&&(in_2[31])&&alu_rslt[31];
P = ^alu_rslt;
end
5'd2 : begin
alu_rslt = in_1; // Passing first input as it is on output
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0;
C = 0;
P = ^alu_rslt;
end
5'd3 : begin
alu_rslt = in_2; // Passing second input as it is on output
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0;
C = 0;
P = ^alu_rslt;
end
5'd4 : begin
{C,alu_rslt} = in_1 + 1; // Incrementing first input by 1
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0;
P = ^alu_rslt;
end
5'd5 : begin
{C,alu_rslt} = in_2 + 1; // Incrementing second input by 1
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0;
P = ^alu_rslt;
end
5'd6 : begin
alu_rslt = in_1 - 1; // Decrementing first input by 1
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0;
C = alu_rslt[31];
P = ^alu_rslt;
end
5'd7 : begin
alu_rslt = in_2 - 1; // Decrementing second input by 1
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0;
C = alu_rslt[31];
P = ^alu_rslt;
end
/*
==============================================================================================
LOGICAL OPERATIONS FOR THE ALU DESIGN
In logical operations, flag bits usually does not have any effect by the
results generated
==============================================================================================
*/
5'd8 : begin
alu_rslt = in_1 & in_2; // AND operation
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
C = 0; // Don't care
V = 0; // Don't care
P = ^alu_rslt;
end
5'd9 : begin
alu_rslt = in_1 | in_2; // OR operation
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0; // Don't care
C = 0; // Don't care
P = ^alu_rslt;
end
5'd10: begin
alu_rslt = ~(in_1 & in_2); // NAND operation
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0; // Don't care
C = 0; // Don't care
P = ^alu_rslt;
end
5'd11: begin
alu_rslt = ~(in_1 | in_2); // NOR operation
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0; // Don't care
C = 0; // Don't care
P = ^alu_rslt;
end
5'd12: begin
alu_rslt = ~(in_1 ^ in_2); // XNOR operation
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0; // Don't care
C = 0; // Don't care
P = ^alu_rslt;
end
5'd13: begin
alu_rslt = in_1 ^ in_2; // XOR operation
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0; // Don't care
C = 0; // Don't care
P = ^alu_rslt;
end
5'd14: begin
alu_rslt = ~in_1; // NOT operation on first input
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0; // Don't care
C = 0; // Don't care
P = ^alu_rslt;
end
5'd15: begin
alu_rslt = ~in_2; // NOT operation on second input
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0; // Don't care
C = 0; // Don't care
P = ^alu_rslt;
end
5'd16: begin
alu_rslt = in_1 << 1; // Left shift operation on first input
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0; // Don't care
C = alu_rslt[31];
P = ^alu_rslt;
end
5'd17: begin
alu_rslt = in_2 << 1; // Left shift operation on second input
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0; // Don't care
C = alu_rslt[31];
P = ^alu_rslt;
end
5'd18: begin
alu_rslt = in_1 >> 1; // Right shift operation on first input
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0; // Don't care
C = alu_rslt[0];
P = ^alu_rslt;
end
5'd19: begin
alu_rslt = in_1 >> 1; // Right shift operation on second input
Z = (alu_rslt==32'b0);
N = alu_rslt[31];
V = 0; // Don't care
C = alu_rslt[0];
P = ^alu_rslt;
end
5'd20: begin
alu_rslt = {in_1[30:0], in_1[31]}; // Left Circular Rotate (LCR) or shift on first input
Z = (alu_rslt==32'b0);
N = 0; // Don't care
V = 0; // Don't care
C = 0; // Don't care
P = ^alu_rslt;
end
5'd21: begin
alu_rslt = {in_2[30:0], in_2[31]}; // Left Circular Rotate (LCR) or shift on second input
Z = (alu_rslt==32'b0);
N = 0; // Don't care
V = 0; // Don't care
C = 0; // Don't care
P = ^alu_rslt;
end
5'd22: begin
alu_rslt = {in_1[0], in_1[31:1]}; // Right Circular Rotate (RCR) or shift on first input
Z = (alu_rslt==32'b0);
N = 0; // Don't care
V = 0; // Don't care
C = 0; // Don't care
P = ^alu_rslt;
end
5'd23: begin
alu_rslt = {in_2[0], in_2[31:1]}; // Right Circular Rotate (RCR) or shift on second input
Z = (alu_rslt==32'b0);
N = 0; // Don't care
V = 0; // Don't care
C = 0; // Don't care
P = ^alu_rslt;
end
default: begin // In default case, everything should be zero
alu_rslt = 0;
{P,N,Z,C,V}= 5'b00100; // Zero flag bit should be 1 to indicate the default case
end
endcase //case structure
end //always block
assign alu_checks = {P,V,Z,C,N}; // Assigning Flag values to alu_checks
endmodule //ALU_microprocessor