-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatapath.c
322 lines (272 loc) · 8.98 KB
/
datapath.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
#include <stdio.h>
#include <strings.h>
#include "globals.h"
extern ActivateAlu () ;
extern ActivateShifter () ;
extern FirstSubcycle () ;
extern SecondSubcycle () ;
extern ThirdSubcycle () ;
extern FourthSubcycle () ;
DataBusType ProgramCounter="0000000000000000" ;
DataBusType Accumulator="0000000000000000" ;
DataBusType StackPointer="0000000000000000" ;
DataBusType InstructionReg="0000000000000000" ;
DataBusType TempInstruction="0000000000000000" ;
DataBusType ZeroRegister ;
DataBusType PositiveOne ;
DataBusType NegativeOne ;
DataBusType Amask ;
DataBusType Smask ;
DataBusType ARegister="0000000000000000" ;
DataBusType BRegister="0000000000000000" ;
DataBusType CRegister="0000000000000000" ;
DataBusType DRegister="0000000000000000" ;
DataBusType ERegister="0000000000000000" ;
DataBusType FRegister="0000000000000000" ;
DataBusType ALatch ;
DataBusType BLatch ;
AddressBusType MAR ;
DataBusType MBR ;
DataBusType AluResult ;
DataBusType ShifterResult ;
extern int btoi();
DumpRegisters ()
{
printf ("\nProgramCounter : %s base 10: %6d\n", ProgramCounter,
btoi(ProgramCounter)) ;
printf ("Accumulator : %s base 10: %6d\n", Accumulator,
btoi(Accumulator)) ;
printf ("InstructionReg : %s base 10: %6d\n", InstructionReg,
btoi(InstructionReg)) ;
printf ("TempInstr : %s base 10: %6d\n", TempInstruction,
btoi(TempInstruction)) ;
printf ("StackPointer : %s base 10: %6d\n", StackPointer,
btoi(StackPointer)) ;
printf ("ARegister : %s base 10: %6d\n", ARegister,
btoi(ARegister)) ;
printf ("BRegister : %s base 10: %6d\n", BRegister,
btoi(BRegister)) ;
printf ("CRegister : %s base 10: %6d\n", CRegister,
btoi(CRegister)) ;
printf ("DRegister : %s base 10: %6d\n", DRegister,
btoi(DRegister)) ;
printf ("ERegister : %s base 10: %6d\n", ERegister,
btoi(ERegister)) ;
printf ("FRegister : %s base 10: %6d\n", FRegister,
btoi(FRegister)) ;
} /* END DumpRegisters */
LoadMar (DataLines)
DataBusType DataLines ;
{
int I ;
for (I = 0 ; I <= 11 ; I++)
MAR[I] = DataLines[I+4] ;
} /* END LoadMar */
SelectRegister (Lines)
DataBusType Lines ;
{
int I ;
int Register ;
Register = 16 ;
for (I = 0 ; I < DataWordSize ; I++)
if (Lines[I] == One)
Register = I ;
return (Register) ;
} /* END SelectRegister */
LoadALatch (ABits)
DataBusType ABits ;
{
int Register ;
Register = SelectRegister (ABits) ;
switch (Register)
{
case 0 : strcpy (ALatch, ProgramCounter) ;
break ;
case 1 : strcpy (ALatch, Accumulator) ;
break ;
case 2 : strcpy (ALatch, StackPointer) ;
break ;
case 3 : strcpy (ALatch, InstructionReg) ;
break ;
case 4 : strcpy (ALatch, TempInstruction) ;
break ;
case 5 : strcpy (ALatch, ZeroRegister) ;
break ;
case 6 : strcpy (ALatch, PositiveOne) ;
break ;
case 7 : strcpy (ALatch, NegativeOne) ;
break ;
case 8 : strcpy (ALatch, Amask) ;
break ;
case 9 : strcpy (ALatch, Smask) ;
break ;
case 10 : strcpy (ALatch, ARegister) ;
break ;
case 11 : strcpy (ALatch, BRegister) ;
break ;
case 12 : strcpy (ALatch, CRegister) ;
break ;
case 13 : strcpy (ALatch, DRegister) ;
break ;
case 14 : strcpy (ALatch, ERegister) ;
break ;
case 15 : strcpy (ALatch, FRegister) ;
break ;
}
} /* END LoadALatch */
LoadBLatch (BBits)
DataBusType BBits ;
{
int Register ;
Register = SelectRegister (BBits) ;
switch (Register)
{
case 0 : strcpy (BLatch, ProgramCounter) ;
break ;
case 1 : strcpy (BLatch, Accumulator) ;
break ;
case 2 : strcpy (BLatch, StackPointer) ;
break ;
case 3 : strcpy (BLatch, InstructionReg) ;
break ;
case 4 : strcpy (BLatch, TempInstruction) ;
break ;
case 5 : strcpy (BLatch, ZeroRegister) ;
break ;
case 6 : strcpy (BLatch, PositiveOne) ;
break ;
case 7 : strcpy (BLatch, NegativeOne) ;
break ;
case 8 : strcpy (BLatch, Amask) ;
break ;
case 9 : strcpy (BLatch, Smask) ;
break ;
case 10 : strcpy (BLatch, ARegister) ;
break ;
case 11 : strcpy (BLatch, BRegister) ;
break ;
case 12 : strcpy (BLatch, CRegister) ;
break ;
case 13 : strcpy (BLatch, DRegister) ;
break ;
case 14 : strcpy (BLatch, ERegister) ;
break ;
case 15 : strcpy (BLatch, FRegister) ;
break ;
}
} /* END LoadBLatch */
LoadRegisterBank (CBits, DataLines)
DataBusType CBits ;
DataBusType DataLines ;
{
int Register ;
Register = SelectRegister (CBits) ;
switch (Register)
{
case 0 : strcpy (ProgramCounter, DataLines) ;
break ;
case 1 : strcpy (Accumulator, DataLines) ;
break ;
case 2 : strcpy (StackPointer, DataLines) ;
break ;
case 3 : strcpy (InstructionReg, DataLines) ;
break ;
case 4 : strcpy (TempInstruction, DataLines) ;
break ;
case 10 : strcpy (ARegister, DataLines) ;
break ;
case 11 : strcpy (BRegister, DataLines);
break ;
case 12 : strcpy (CRegister, DataLines) ;
break ;
case 13 : strcpy (DRegister, DataLines) ;
break ;
case 14 : strcpy (ERegister, DataLines) ;
break ;
case 15 : strcpy (FRegister, DataLines) ;
break ;
case 16 : strcpy (DataLines, DataLines) ;
break ;
}
} /* END LoadRegisterBank */
ActivateDataPath (MarRegs, MbrRegs, ABits, BBits, CBits,
AmuxBit, AluBits, ShiftBits, MbrBit, MarBit,
NBit, ZBit)
AddressBusType MarRegs ;
DataBusType MbrRegs ;
DataBusType ABits ;
DataBusType BBits ;
DataBusType CBits ;
Bit AmuxBit ;
TwoBits AluBits ;
TwoBits ShiftBits ;
Bit MbrBit ;
Bit MarBit ;
Bit *NBit ;
Bit *ZBit ;
{
DataBusType LeftOperand ;
DataBusType RightOperand ;
strcpy (MAR, MarRegs) ;
strcpy (MBR, MbrRegs) ;
if (SecondSubcycle())
{
LoadALatch (ABits) ;
LoadBLatch (BBits) ;
}
if (ThirdSubcycle())
{
if (AmuxBit == One)
strcpy (LeftOperand, MBR) ;
else strcpy (LeftOperand, ALatch) ;
if (MarBit == One)
{
LoadMar (BLatch) ;
strcpy (MarRegs, MAR) ;
}
strcpy (RightOperand, BLatch) ;
ActivateAlu (LeftOperand, RightOperand, AluBits, AluResult,
NBit, ZBit) ;
ActivateShifter (AluResult, ShiftBits, ShifterResult) ;
}
if (FourthSubcycle())
{
LoadRegisterBank( CBits, ShifterResult) ;
if (MbrBit == One)
{
strcpy (MBR, ShifterResult) ;
strcpy (MbrRegs, ShifterResult) ;
}
}
} /* END ActivateDataPath */
void InitializePCandStackPointer (pc, sp)
int pc, sp;
{
char pc_str[17], sp_str[17];
int i, mask = 0x00008000;
if(pc < 0 || sp < 0 || pc > 2047 || sp > 4095){
strcpy (ProgramCounter, "0000000000000000") ;
strcpy (StackPointer, "0000111110000000") ;
}else{
for(i=0; i<16; i++){
if(pc & (mask >> i))
pc_str[i] = '1';
else
pc_str[i] = '0';
if(sp & (mask >> i))
sp_str[i] = '1';
else
sp_str[i] = '0';
}
pc_str[16] = '\0';
sp_str[16] = '\0';
strcpy (ProgramCounter, pc_str);
strcpy (StackPointer, sp_str);
fprintf(stderr,"Starting PC is : %s base 10: %6d\nStarting SP is : %s base 10: %6d\n\n",pc_str,btoi(pc_str),sp_str,btoi(sp_str));
}
strcpy (ZeroRegister, "0000000000000000") ;
strcpy (PositiveOne, "0000000000000001") ;
strcpy (NegativeOne, "1111111111111111") ;
strcpy (Amask, "0000111111111111") ;
strcpy (Smask, "0000000011111111") ;
} /* END InitializePCandStackPointer */