-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALU.v
371 lines (319 loc) · 6.57 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//TOP MODULE
module ALU(
input Clk,
input Rst,
input [3:0] A,
input [3:0] B,
input [1:0] SL,
output wire [6:0]dig,
output wire [7:0]an,
output wire dc
);
reg [15:0]clkdiv;
reg clkd;
wire [3:0]q0,q1;
wire [3:0]R,Q;
wire [7:0]P;
wire DC;
wire [6:0] a,b,Q0,Q1,op1,op2;
wire [6:0] op3;
always@(posedge Clk, negedge Rst )
begin
if(!Rst)
clkdiv<=0;
else
begin
clkdiv <= clkdiv+1;
clkd <= clkdiv[15];
end
end
mult muiltiplictn(Clk, Rst, 1'b1, 1'b1, 1'b1, A, B, P);
div divisn(Clk, Rst, 1'b1, 1'b1, 1'b1, A, B, R, Q);
oprtn oprtn_cntr(SL, A, B, P, R, Q, q0, q1, DC);
seg seg7cnvrtA(A, a);
seg seg7cnvrtB(B, b);
seg seg7cnvrtQ0(q0, Q0);
seg seg7cnvrtQ1(q1, Q1);
sign sign1(SL, op1);
sign0 sign2(SL, op2);
assign op3 = 7'b1110110;
segcntr seg7cntr(clkd,Rst,a,b,DC,Q0,Q1,op1,op2,op3, dig, dc, an);
endmodule
//MULTIPLICATION
module mult(Clock, Resetn, s, LA, LB, DataA, DataB, P);
parameter n = 4;
input Clock, Resetn, LA, LB, s;
input [n-1:0] DataA, DataB;
output [n+n-1:0] P;
reg Done;
wire z;
reg [n+n-1:0] DataP;
wire [n+n-1:0] A, Sum;
reg [1:0] y, Y;
wire [n-1:0] B;
reg EA, EB, EP, Psel;
integer k;
// control circuit
parameter S1 = 2'b00, S2 = 2'b01, S3 = 2'b10;
always @(s, y, z)
begin: State_table
case (y)
S1: if (s == 0) Y = S1;
else Y = S2;
S2: if (z == 0) Y = S2;
else Y = S3;
S3: if (s == 1) Y = S3;
else Y = S1;
default: Y = 2'bxx;
endcase
end
always @(posedge Clock, negedge Resetn)
begin: State_flipflops
if (Resetn == 0)
y <= S1;
else
y <=Y;
end
always @(s, y, B[0])
begin: FSM_outputs
// defaults
EA = 0; EB = 0; EP = 0; Done = 0; Psel = 0;
case (y)
S1: EP = 1;
S2: begin
EA= 1; EB = 1; Psel = 1;
if (B[0])
EP = 1;
else EP = 0;
end
S3: Done = 1;
endcase
end
//datapath circuit
shiftrne ShiftB (DataB, LB, EB, 1'b0, Clock, B);
defparam ShiftB.n = 4;
shiftlne ShiftA ({{n{1'b0}}, DataA}, LA, EA, 1'b0, Clock, A);
defparam ShiftA.n = 8;
assign z = (B == 0);
assign Sum = A + P;
// define the 2n 2-to-1 multiplexers
always @(Psel, Sum)
for (k = 0; k < n+n; k = k+1)
DataP[k] = Psel ? Sum[k] : 1'b0;
regne RegP (DataP, Clock, Resetn, EP, P);
defparam RegP.n = 8;
endmodule
//DIVISION
module div(Clock, Resetn, s, LA, EB, DataA, DataB, R, Q);
parameter n = 4, logn = 2;
input Clock, Resetn, s, LA, EB;
input [n-1:0] DataA, DataB;
output [n-1:0] R, Q;
reg Done;
wire Cout, z, R0;
wire [n-1:0] DataR;
wire [n:0] Sum;
reg [1:0] y, Y;
wire [n-1:0] A, B;
wire [logn-1:0] Count;
reg EA, Rsel, LR, ER, ER0, LC, EC;
integer k;
// control circuit
parameter S1 = 2'b00, S2 = 2'b01, S3 = 2'b10;
always @(s, y, z)
begin: State_table
case (y)
S1: if (s == 0) Y = S1;
else Y = S2;
S2: if (z == 0) Y = S2;
else Y = S3;
S3: if (s == 1) Y = S3;
else Y = S1;
default: Y = 2'bxx;
endcase
end
always @(posedge Clock, negedge Resetn)
begin: State_flipflops
if (Resetn == 0)
y <= S1;
else
y <=Y;
end
always @(y, s, Cout, z)
begin: FSM_outputs
// defaults
LR = 0; ER = 0; ER0 = 0; LC = 0; EC = 0; EA= 0;
Rsel = 0; Done = 0;
case (y)
S1: begin
LC = 1; ER = 1;
if (s == 0)
begin
LR = 1; ER0 = 0;
end
else
begin
LR = 0; EA = 1; ER0 = 1;
end
end
S2: begin
Rsel = 1; ER = 1; ER0 = 1; EA= 1;
if (Cout) LR = 1;
else LR = 0;
if (z == 0) EC = 1;
else EC = 0;
end
S3: Done = 1;
endcase
end
regne RegB (DataB, Clock, Resetn, EB, B);
defparam RegB.n = n;
shiftlne ShiftR (DataR, LR, ER, R0, Clock, R);
defparam ShiftR.n = n;
muxdff FF_R0 (1'b0, A[n-1], ER0, Clock, R0);
shiftlne ShiftA (DataA, LA, EA, Cout, Clock, A);
defparam ShiftA.n = n;
assign Q =A;
downcount Counter (2'b11, Clock, EC, LC, Count);
defparam Counter.n = logn;
assign z = (Count == 0);
assign Sum = {1'b0, R[n-2:0], R0} + {1'b0, B} + 1;
assign Cout = Sum[n];
// define the n 2-to-1 multiplexers
assign DataR = Rsel ? Sum : 0;
endmodule
//OPERATION CONTROL
module oprtn(
input [1:0]SL,
input [3:0]a,
input [3:0]b,
input [7:0]p,
input [3:0]r,
input [3:0]q,
output reg [3:0]q0,
output reg [3:0]q1,
output reg dc
);
always@(*)
case(SL)
0 : begin dc = 1;
{q1,q0} = a + b;
end
1 : begin dc = 1;
{q1,q0} = p;
end
2 : begin dc = 0;
q1 = q;
q0 = r;
end
3 : begin dc = 1;
{q1,q0} = a - b;
end
endcase
endmodule
//HEXADECIMAL CONVERTOR
module seg(bcd,led);
input [3:0]bcd;
output reg [6:0]led;
always@(bcd)
case(bcd)
0 : led=7'b0000001;
1 : led=7'b1001111;
2 : led=7'b0010010;
3 : led=7'b0000110;
4 : led=7'b1001100;
5 : led=7'b0100100;
6 : led=7'b0100000;
7 : led=7'b0001111;
8 : led=7'b0000000;
9 : led=7'b0000100;
10: led=7'b0001000;
11: led=7'b1100000;
12: led=7'b0110001;
13: led=7'b1000010;
14: led=7'b0110000;
15: led=7'b0111000;
endcase
endmodule
//PERTRN GENERATOR
module sign(
input [1:0]SL,
output reg [6:0]op
);
always@(*)
case(SL)
0 : op = 7'b1001110;
2 : op = 7'b1111110;
3 : op = 7'b1100111;
1 : op = 7'b0000111;
default op = 7'b1111111;
endcase
endmodule
module sign0(
input [1:0]SL,
output reg [6:0]op
);
always@(*)
case(SL)
0 : op = 7'b1111110;
3 : op = 7'b1111110;
2 : op = 7'b0111101;
1 : op = 7'b0110001;
default op = 7'b1111111;
endcase
endmodule
//INTASIATED MODULES
module shiftrne(R, L, E, w, Clock, Q);
parameter n = 4;
input [n-1:0] R;
input L, E, w, Clock;
output reg [n- 1:0] Q;
integer k;
always @(posedge Clock)
begin
if (L)
Q <= R;
else if (E)
begin
Q[n-1] <= w;
for (k = n-2; k >= 0; k = k-1)
Q[k] <= Q[k+ 1];
end
end
endmodule
module shiftlne(R, L, E, w, Clock, Q);
parameter n = 4;
input [n- 1:0] R;
input L, E, w, Clock;
output reg [n- 1:0] Q;
integer k;
always @(posedge Clock)
begin
if (L)
Q <= R;
else if (E)
begin
Q[0] <= w;
for (k =1 ; k >n-1 ; k = k+1)
Q[k+1] <= Q[k];
end
end
module regne(R, Clock, Resetn, E, Q);
parameter n = 8;
input [n-1:0] R;
input Clock, Resetn, E;
output reg [n-1:0] Q;
always @(posedge Clock, negedge Resetn)
if (Resetn == 0)
Q <= 0;
else if (E)
Q <= R;
endmodule
module muxdff(D0, D1, Sel, Clock, Q);
input D0, D1, Sel, Clock;
output reg Q;
wire D;
assign D = Sel ? D1 : D0;
always @(posedge Clock)
Q <= D;
endmodule