-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_control.c
425 lines (334 loc) · 15.8 KB
/
test_control.c
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <assert.h>
#include <stdio.h>
#include "circuits.h"
typedef char BIT;
#define TRUE 1
#define FALSE 0
#define UNDEF -1
BIT RegDst = FALSE;
BIT Jump = FALSE; BIT Branch = FALSE;
BIT MemToReg = FALSE;
BIT ALUOp[2] = {FALSE};
BIT MemWrite = FALSE;
BIT ALUImm = FALSE;
BIT RegWrite = FALSE;
BIT Zero = FALSE;
BIT Link = FALSE;
BIT JumpReg = FALSE;
BIT ALUControl[4] = {FALSE};
BIT not_gate(BIT A)
{
return (!A);
}
BIT or_gate(BIT A, BIT B)
{
return (A || B);
}
BIT or_gate3(BIT A, BIT B, BIT C)
{
return or_gate(A, or_gate(B, C));
}
BIT and_gate(BIT A, BIT B)
{
return (A && B);
}
BIT and_gate3(BIT A, BIT B, BIT C)
{
return and_gate(A, and_gate(B, C));
}
BIT xor_gate(BIT A, BIT B)
{
BIT nA = not_gate(A);
BIT nB = not_gate(B);
BIT x0 = and_gate(A, nB);
BIT x1 = and_gate(nA, B);
return or_gate(x0, x1);
}
BIT nor_gate(BIT A, BIT B)
{
return not_gate(or_gate(A, B));
}
BIT nand_gate(BIT A, BIT B)
{
return not_gate(and_gate(A, B));
}
void decoder2(BIT* I, BIT EN, BIT* O)
{
BIT nI1 = not_gate(I[1]);
BIT nI0 = not_gate(I[0]);
O[0] = and_gate(nI1, nI0);
O[1] = and_gate(nI1, I[0]);
O[2] = and_gate(I[1], nI0);
O[3] = and_gate(I[1], I[0]);
// Note use of EN (enable) line below
for (int i = 0; i < 4; ++i)
O[i] = and_gate(EN, O[i]);
return;
}
void decoder3(BIT* I, BIT EN, BIT* O)
{
O[0] = and_gate3(not_gate(I[2]), not_gate(I[1]), not_gate(I[0]));
O[1] = and_gate3(not_gate(I[2]), not_gate(I[1]), I[0]);
O[2] = and_gate3(not_gate(I[2]), I[1], not_gate(I[0]));
O[3] = and_gate3(not_gate(I[2]), I[1], I[0]);
O[4] = and_gate3(I[2], not_gate(I[1]), not_gate(I[0]));
O[5] = and_gate3(I[2], not_gate(I[1]), I[0]);
O[6] = and_gate3(I[2], I[1], not_gate(I[0]));
O[7] = and_gate3(I[2], I[1], I[0]);
for (int i = 0; i < 8; ++i)
O[i] = and_gate(EN, O[i]);
return;
}
void decoder5(BIT* I, BIT EN, BIT* O)
{
BIT EN_O[4] = {FALSE};
decoder2(&I[3], EN, EN_O);
decoder3(I, EN_O[3], &O[24]);
decoder3(I, EN_O[2], &O[16]);
decoder3(I, EN_O[1], &O[8]);
decoder3(I, EN_O[0], &O[0]);
for (int i = 0; i < 32; ++i)
O[i] = and_gate(EN, O[i]);
}
BIT multiplexor2(BIT S, BIT I0, BIT I1)
{
BIT nS = not_gate(S);
BIT x0 = and_gate(nS, I0);
BIT x1 = and_gate(S, I1);
return or_gate(x0, x1);
}
void multiplexor2_32(BIT S, BIT* I0, BIT* I1, BIT* Output)
{
for (int i = 0; i < 32; ++i) {
BIT nS = not_gate(S);
BIT x0 = and_gate(nS, I0[i]);
BIT x1 = and_gate(S, I1[i]);
Output[i] = or_gate(x0, x1);
}
}
BIT multiplexor4(BIT S0, BIT S1, BIT I0, BIT I1, BIT I2, BIT I3)
{
BIT x0 , x1, x2, x3 = FALSE;
BIT S[] = {S0, S1};
BIT O[] = {I0,I1,I2,I3};
decoder2(S,TRUE, O);
BIT y0 = and_gate(x0, I0);
BIT y1 = and_gate(x1, I1);
BIT y2 = and_gate(x2, I2);
BIT y3 = and_gate(x3, I3);
BIT z0 = or_gate(y0, y1);
BIT z1 = or_gate(y2, y3);
return or_gate(z0, z1);
}
void Control(BIT* OpCode) {
// TODO: Set control bits for everything
// Input: opcode field from the instruction
// OUtput: all control lines get set
// Note: Can use SOP or similar approaches to determine bits
MemToReg = MemToRegCircuit(OpCode);
MemWrite = MemWriteCircuit(OpCode);
Branch = BranchCircuit(OpCode);
RegDst = RegDstCircuit(OpCode);
Link = LinkCircuit(OpCode);
RegWrite = or_gate3(RegDst, Link, or_gate(MemToReg, AddiCircuit(OpCode)));
Jump = or_gate(JumpCircuit(OpCode), Link);
ALUOp[0] = or_gate3(AddiCircuit(OpCode), MemToReg, MemWrite);
ALUOp[1] = Branch;
ALUImm = ALUOp[0];
}
void updateAluControl(){
//ALUOp != {0,0} we have to update the alu contorl bits
BIT ADD[] = {0, 0, 1, 0};
BIT SUB[] = {0, 1, 1, 0};
for(int i=0; i<4; i++)
ALUControl[i] = multiplexor2(ALUOp[0], ALUControl[i], ADD[i]);
for(int i=0; i<4; i++)
ALUControl[i] = multiplexor2(ALUOp[1], ALUControl[i], SUB[i]);
}
void ALU_Control(BIT* funct)
{
JumpReg = and_gate3(JumpRegCircuit(funct), \
not_gate(or_gate(ALUOp[0], ALUOp[1])), RegDst);
//Set RegWrite to 0 if we are preforming a jr instruction
RegWrite = multiplexor2(JumpReg, RegWrite, 0);
ALUControl[3] = AluControl_Circuit0(funct);
ALUControl[2] = not_gate(or_gate(AluControl_Circuit1(funct), ALUControl[3]));
ALUControl[3] = or_gate(AluControl_Circuit0(funct), LessCircuit(funct));
ALUControl[1] = not_gate(or_gate(not_gate(ALUControl[2]), BinvertCircuit(funct)));
ALUControl[0] = 0;
updateAluControl();
}
int main(){
BIT lw [] = {1,1,0,0,0,1};
BIT sw [] = {1,1,0,1,0,1};
BIT beq [] = {0,0,1,0,0,0};
BIT addi[] = {0,0,0,1,0,0};
BIT R [] = {0,0,0,0,0,0};
BIT J [] = {0,1,0,0,0,0};
BIT JAL [] = {1,1,0,0,0,0};
BIT T_funct [] = {0,0,0,1,0,0};
/****************************************************************************************************************************/
/* TEST LOAD WORD*/
/****************************************************************************************************************************/
Control(lw);
ALU_Control(T_funct);
assert(!ALUOp[1] && ALUOp[0] && MemToReg && !RegDst && ALUImm \
&& !Branch && !Jump && !Link && RegWrite && !MemWrite);
assert(!ALUControl[0] && !ALUControl[1] && \
ALUControl[2] && !ALUControl[3]);
assert(!JumpReg);
printf("PASSED\n");
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/* TEST STORE WORD*/
/****************************************************************************************************************************/
RegDst = Jump = Branch = MemToReg = ALUOp[0] = ALUOp[1] = MemWrite = ALUImm = RegWrite = Link = FALSE;
Control(sw);
ALU_Control(T_funct);
assert(!ALUOp[1] && ALUOp[0] && !MemToReg && !RegDst && ALUImm \
&& !Branch && !Jump && !Link && !RegWrite && MemWrite);
assert(!ALUControl[0] && !ALUControl[1] && \
ALUControl[2] && !ALUControl[3]);
assert(!JumpReg);
printf("PASSED\n");
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/* TEST BEQ*/
/****************************************************************************************************************************/
RegDst = Jump = Branch = MemToReg = ALUOp[0] = ALUOp[1] = MemWrite = ALUImm = RegWrite = Link = FALSE;
Control(beq);
ALU_Control(T_funct);
assert(ALUOp[1] && !ALUOp[0] && !MemToReg && !RegDst && !ALUImm \
&& Branch && !Jump && !Link && !RegWrite && !MemWrite);
assert(!ALUControl[0] && ALUControl[1] && \
ALUControl[2] && !ALUControl[3]);
assert(!JumpReg);
printf("PASSED\n");
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/* TEST addi*/
/****************************************************************************************************************************/
RegDst = Jump = Branch = MemToReg = ALUOp[0] = ALUOp[1] = MemWrite = ALUImm = RegWrite = Link = FALSE;
Control(addi);
ALU_Control(T_funct);
assert(!ALUOp[1] && ALUOp[0] && !MemToReg && !RegDst && ALUImm \
&& !Branch && !Jump && !Link && RegWrite && !MemWrite);
assert(!ALUControl[0] && !ALUControl[1] && \
ALUControl[2] && !ALUControl[3]);
assert(!JumpReg);
printf("PASSED\n");
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/* TEST and*/
/****************************************************************************************************************************/
RegDst = Jump = Branch = MemToReg = ALUOp[0] = ALUOp[1] = MemWrite = ALUImm = RegWrite = Link = FALSE;
BIT and [] = {0,0,1,0,0,1};
Control(R);
ALU_Control(and);
assert(!ALUOp[1] && !ALUOp[0] && !MemToReg && RegDst && !ALUImm \
&& !Branch && !Jump && !Link && RegWrite && !MemWrite);
assert(!ALUControl[0] && !ALUControl[1] && \
!ALUControl[2] && !ALUControl[3]);
assert(!JumpReg);
printf("PASSED\n");
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/* Test or*/
/****************************************************************************************************************************/
RegDst = Jump = Branch = MemToReg = ALUOp[0] = ALUOp[1] = MemWrite = ALUImm = RegWrite = Link = FALSE;
BIT or [] = {1,0,1,0,0,1};
Control(R);
ALU_Control(or);
assert(!ALUOp[1] && !ALUOp[0] && !MemToReg && RegDst && !ALUImm \
&& !Branch && !Jump && !Link && RegWrite && !MemWrite);
assert(!ALUControl[0] && !ALUControl[1] && \
!ALUControl[2] && ALUControl[3]);
assert(!JumpReg);
printf("PASSED\n");
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/* Test add*/
/****************************************************************************************************************************/
RegDst = Jump = Branch = MemToReg = ALUOp[0] = ALUOp[1] = MemWrite = ALUImm = RegWrite = Link = FALSE;
BIT add [] = {0,0,0,0,0,1};
Control(R);
ALU_Control(add);
assert(!ALUOp[1] && !ALUOp[0] && !MemToReg && RegDst && !ALUImm \
&& !Branch && !Jump && !Link && RegWrite && !MemWrite);
assert(!ALUControl[0] && !ALUControl[1] && \
ALUControl[2] && !ALUControl[3]);
assert(!JumpReg);
printf("PASSED\n");
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/* Test sub*/
/****************************************************************************************************************************/
RegDst = Jump = Branch = MemToReg = ALUOp[0] = ALUOp[1] = MemWrite = ALUImm = RegWrite = Link = FALSE;
BIT sub [] = {0,1,0,0,0,1};
Control(R);
ALU_Control(sub);
assert(!ALUOp[1] && !ALUOp[0] && !MemToReg && RegDst && !ALUImm \
&& !Branch && !Jump && !Link && RegWrite && !MemWrite);
assert(!ALUControl[0] && ALUControl[1] && \
ALUControl[2] && !ALUControl[3]);
assert(!JumpReg);
printf("PASSED\n");
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/* Test slt*/
/****************************************************************************************************************************/
RegDst = Jump = Branch = MemToReg = ALUOp[0] = ALUOp[1] = MemWrite = ALUImm = RegWrite = Link = FALSE;
BIT slt [] = {0,1,0,1,0,1};
Control(R);
ALU_Control(slt);
assert(!ALUOp[1] && !ALUOp[0] && !MemToReg && RegDst && !ALUImm \
&& !Branch && !Jump && !Link && RegWrite && !MemWrite);
assert(!ALUControl[0] && ALUControl[1] && \
ALUControl[2] && ALUControl[3]);
assert(!JumpReg);
printf("PASSED\n");
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/* Test jr*/
/****************************************************************************************************************************/
RegDst = Jump = Branch = MemToReg = ALUOp[0] = ALUOp[1] = MemWrite = ALUImm = RegWrite = Link = FALSE;
BIT jr [] = {0,0,0,1,0,0};
Control(R);
ALU_Control(jr);
assert(!ALUOp[1] && !ALUOp[0] && !MemToReg && RegDst && !ALUImm \
&& !Branch && !Jump && !Link && !RegWrite && !MemWrite);
assert(JumpReg);
printf("PASSED\n");
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/* TEST jump*/
/****************************************************************************************************************************/
RegDst = Jump = Branch = MemToReg = ALUOp[0] = ALUOp[1] = MemWrite = ALUImm = RegWrite = Link = FALSE;
Control(J);
assert(!ALUOp[1] && !ALUOp[0] && !MemToReg && !RegDst && !ALUImm \
&& !Branch && Jump && !Link && !RegWrite && !MemWrite);
printf("PASSED\n");
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/****************************************************************************************************************************/
/* TEST jal*/
/****************************************************************************************************************************/
RegDst = Jump = Branch = MemToReg = ALUOp[0] = ALUOp[1] = MemWrite = ALUImm = RegWrite = Link = FALSE;
Control(JAL);
assert(!ALUOp[1] && !ALUOp[0] && !MemToReg && !RegDst && !ALUImm \
&& !Branch && Jump && Link && RegWrite && !MemWrite);
printf("PASSED\n");
/****************************************************************************************************************************/
/****************************************************************************************************************************/
return 0;
}