-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMIPSSim.c
More file actions
441 lines (395 loc) · 12.1 KB
/
MIPSSim.c
File metadata and controls
441 lines (395 loc) · 12.1 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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//
// This is the main soure file for MIPS Dissasembler
//
#include "FuncProto.h"
// Register List Definition
RegList RegLookUpTable =
{
{ 0x0, "R0", 0 },
{ 0x1, "R1", 0 },
{ 0x2, "R2", 0 },
{ 0x3, "R3", 0 },
{ 0x4, "R4", 0 },
{ 0x5, "R5", 0 },
{ 0x6, "R6", 0 },
{ 0x7, "R7", 0 },
{ 0x8, "R8", 0 },
{ 0x9, "R9", 0 },
{ 0xa, "R10", 0 },
{ 0xb, "R11", 0 },
{ 0xc, "R12", 0 },
{ 0xd, "R13", 0 },
{ 0xe, "R14", 0 },
{ 0xf, "R15", 0 },
{ 0x10, "R16", 0 },
{ 0x11, "R17", 0 },
{ 0x12, "R18", 0 },
{ 0x13, "R19", 0 },
{ 0x14, "R20", 0 },
{ 0x15, "R21", 0 },
{ 0x16, "R22", 0 },
{ 0x17, "R23", 0 },
{ 0x18, "R24", 0 },
{ 0x19, "R25", 0 },
{ 0x1a, "R26", 0 },
{ 0x1b, "R27", 0 },
{ 0x1c, "R28", 0 },
{ 0x1d, "R29", 0 },
{ 0x1e, "R30", 0 },
{ 0x1f, "R31", 0 },
{ 0x20, "Error#", 0 }
};
// printArguementUsage()
// This function prints the proper usage of the
// arguements to be entered by the user
void printArguementUsage()
{
printf("\n");
printf("\tMIPSsim inputfilename outputfilename [-Tm:n]\n\n");
printf("inputfilename : The file name of the binary input file\n");
printf("outputfilename : The file name to which to print the output\n");
printf("[-Tm:n] : Optional arguement to specify the start m and end n cycles of simulating trace output.\n");
printf("\n");
}
// parseArguements()
// This function parses the arguements and fills up
// the args structure to be used later
bool parseArguements(MipsSimContext *pContext, char *argv[])
{
int filenameLength = 0;
bool ReturnStatus = true;
char *startCycleNum = NULL;
char *endCycleNum = NULL;
UINT count = 0, i = 0;
UINT length = 0;
do
{
//get the file name of the input binary file
filenameLength = strlen(argv[1]);
if (filenameLength > 0)
{
pContext->Args.InputFilename = (char*)malloc(sizeof(filenameLength + 1));
strcpy(pContext->Args.InputFilename, argv[1]);
}
else
{
printf("Illegal Filename!!\n");
ReturnStatus = false;
break;
}
// get the filename of the output file
filenameLength = strlen(argv[2]);
if (filenameLength > 0)
{
pContext->Args.OutputFilename = (char*)malloc(sizeof(filenameLength + 1));
strcpy(pContext->Args.OutputFilename, argv[2]);
}
else
{
printf("Illegal Filename!!\n");
ReturnStatus = false;
break;
}
if (argv[3] != NULL)
{
// get the start and end cycle Num
for (count = 2; argv[3][count] != ':'; count++)
{
length++;
}
startCycleNum = (char*)malloc(sizeof(char) * (length + 1));
for (count = 2; argv[3][count] != ':'; count++)
{
startCycleNum[i++] = argv[3][count];
}
startCycleNum[i] = '\0';
i = 0; count++;
endCycleNum = (char*)malloc(sizeof(char)*(strlen(argv[3] - count) + 1));
for (count; count <= strlen(argv[3]); count++)
{
endCycleNum[i++] = argv[3][count];
}
pContext->Args.StartCycleNum = atoi(startCycleNum);
pContext->Args.EndCycleNum = atoi(endCycleNum);
if (pContext->Args.StartCycleNum == 0 && pContext->Args.EndCycleNum == 0)
{
pContext->Args.PrintFinalState = true;
}
}
else
{
pContext->Args.PrintAllCycles = true;
}
} while (false);
if (!ReturnStatus)
{
// free up memory
if (pContext->Args.InputFilename)
{
free(pContext->Args.InputFilename);
pContext->Args.InputFilename = NULL;
}
if (pContext->Args.OutputFilename)
{
free(pContext->Args.OutputFilename);
pContext->Args.OutputFilename = NULL;
}
}
return ReturnStatus;
}
// swapBytes()
// swaps the bytes 0 & 3, bytes 1 & 2 in a DWord
void swapBytes(char *Byte1, char* Byte2)
{
char temp;
// Swap Bytes
temp = *Byte1;
*Byte1 = *Byte2;
*Byte2 = temp;
}
// parseOpcode()
// Parses the Opcode of the Instruction and accordingly
// calls the decode function to create the string
void parseOpcode(MipsSimContext *pContext, Instruction *pInst)
{
// Get the opcode from Instruction Type
pInst->Opcode = (pInst->InstructionDWord.InstDwordValue & OpcodeMask) >> OpcodeShift;
switch (pInst->Opcode)
{
case OPCODE_SPECIAL:
switch (pInst->InstructionDWord.RType.Function)
{
case FUNC_SLL:
// This could be either SLL or NOP
// SLL R0, R0, 0 is expressed as NOP, will be checked within the SLL decoding
decodeSLL(pInst);
break;
case FUNC_SRL:
decodeSRL(pInst);
break;
case FUNC_SRA:
decodeSRA(pInst);
break;
case FUNC_BREAK:
decodeBREAK(pInst);
pContext->IsBreakInst = true;
break;
case FUNC_ADD:
decodeADD(pInst);
break;
case FUNC_ADDU:
decodeADDU(pInst);
break;
case FUNC_SUB:
decodeSUB(pInst);
break;
case FUNC_SUBU:
decodeSUBU(pInst);
break;
case FUNC_AND:
decodeAND(pInst);
break;
case FUNC_OR:
decodeOR(pInst);
break;
case FUNC_XOR:
decodeXOR(pInst);
break;
case FUNC_NOR:
decodeNOR(pInst);
break;
case FUNC_SLT:
decodeSLT(pInst);
break;
case FUNC_SLTU:
decodeSLTU(pInst);
break;
default:
// should not happen
break;
}
break;
case OPCODE_REGIMM:
switch (pInst->InstructionDWord.IType.Rt)
{
case RT_BLTZ:
decodeBLTZ(pInst);
break;
case RT_BGEZ:
decodeBGEZ(pInst);
break;
default:
// should not happen
break;
}
break;
case OPCODE_J:
decodeJ(pInst);
break;
case OPCODE_BEQ:
decodeBEQ(pInst);
break;
case OPCODE_BNE:
decodeBNE(pInst);
break;
case OPCODE_BLEZ:
decodeBLEZ(pInst);
break;
case OPCODE_BGTZ:
decodeBGTZ(pInst);
break;
case OPCODE_ADDI:
decodeADDI(pInst);
break;
case OPCODE_ADDIU:
decodeADDIU(pInst);
break;
case OPCODE_SLTI:
decodeSLTI(pInst);
break;
case OPCODE_SW:
decodeSW(pInst);
break;
case OPCODE_LW:
decodeLW(pInst);
break;
default:
// should not happen
break;
}
}
// addAllInstToContext()
// This function reads all the Instructions and adds them to the context
// and builds the linked list
void addAllInstToContext(MipsSimContext *pContext)
{
Instruction ReadInstruction = { 0 };
memset(&ReadInstruction, 0, sizeof(Instruction));
while (true)
{
memset(&ReadInstruction, 0, sizeof(Instruction));
// Read DWord from the Input file, build the Instruction structure
ReadInstruction.InstructionDWord.InstDwordValue = readUnsignedDWordFile(pContext->FileHandle.InputFileHandle);
pContext->NoOfDWordsRead++;
// Parse Opcode
parseOpcode(pContext, &ReadInstruction);
// Add memory address of the Instruction
ReadInstruction.InstAddress = pContext->CurrentAddress;
pContext->CurrentAddress += 4;
ReadInstruction.next = NULL;
// Add the instruction to the List
addInstToList(pContext, &ReadInstruction);
pContext->InstructionCount++;
// If EOF hit , set the flag
if (pContext->NoOfDWordsRead >= (pContext->InputFileLength >> 2))
{
pContext->IsEndOfFileHit = true;
}
// Exit the loop if break Inst hit or end of file
if (pContext->IsBreakInst)
{
break;
}
}
}
// addAllDataToContext()
// This function reads all the data segment and adds them to the context
// and builds the data linked list
void addAllDataToContext(MipsSimContext *pContext)
{
INT ReadDWord = 0;
while (true)
{
// Read DWord from the Input file
ReadDWord = readSignedDWordFile(pContext->FileHandle.InputFileHandle);
pContext->NoOfDWordsRead++;
// Start Adding the Data DWords if hit the Data Segment Start
if (pContext->CurrentAddress >= DATA_START_ADDRESS)
{
addDataToList(pContext, ReadDWord);
pContext->DataCount++;
}
// Increment the address
pContext->CurrentAddress += 4;
// Exit of EOF is hit
if (pContext->NoOfDWordsRead >= (pContext->InputFileLength >> 2))
{
break;
}
}
}
// parseInputFile()
// This function parses the input file and stores all the instructions
void parseInputFile(MipsSimContext *pContext)
{
// Read all the Instructions and add them to the context
addAllInstToContext(pContext);
// If EOF hit , done with file reading
if (!pContext->IsEndOfFileHit)
{
// Add all the data bytes to the file
addAllDataToContext(pContext);
}
}
// Main function !!
int main(int argc, char *argv[])
{
int ReturnStatus = 1;
MipsSimContext Context;
// Set all the context variables
// Set the Start memory address in the file for instruction fetch
Context.CurrentAddress = INST_START_ADDRESS;
Context.currentBufferPos = 0;
Context.InputFileLength = 0;
Context.IsBreakInst = false;
Context.IsEndOfFileHit = false;
Context.NoOfDWordsRead = 0;
memset(Context.Buffer, 0, sizeof(Context.Buffer));
Context.InstructionList = NULL;
Context.InstructionCount = 0;
Context.DataList = NULL;
Context.DataCount = 0;
Context.CurrentPC = INST_START_ADDRESS;
Context.CurrentCycle = 0;
Context.InstQueue = NULL;
memset(Context.ROB, 0, sizeof(Context.ROB));
memset(Context.RS, 0, sizeof(Context.RS));
memset(Context.RegisterStat, 0, sizeof(Context.RegisterStat));
Context.CurrentInstIssueNum = 0;
Context.BTB = NULL;
Context.RemoveInstFromQueue = false;
Context.Args.PrintFinalState = false;
Context.Args.PrintAllCycles = false;
do
{
// Check if the user has correctly entered the arguements
if (argc < 3 || argc > 4)
{
printArguementUsage();
break;
}
// Parse the program arguements and fill the parameters
if (!parseArguements(&Context, argv))
{
printf("Recheck Arguements!\n");
printArguementUsage();
ReturnStatus = -1;
break;
}
// checks Input and output files, creates the output file if not present
if (!verifyInOutFiles(&Context))
{
printf("Not able to read Input file or write to output file!\n");
ReturnStatus = -1;
break;
}
// SIMULATE TOMASULO ALGORITHM !!!!!
// Parse the input binary file to get all the instructions and Data added to the Context
parseInputFile(&Context);
// Start Simulation with Tomasulo Algorithm
simulateExTomasulo(&Context);
// close files
closeFiles(&Context);
} while (false);
return ReturnStatus;
}