-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiplc-sim.c
More file actions
726 lines (619 loc) · 24.2 KB
/
iplc-sim.c
File metadata and controls
726 lines (619 loc) · 24.2 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/***********************************************************************/
/***********************************************************************
Pipeline Cache Simulator
***********************************************************************/
/***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#define MAX_CACHE_SIZE 10240
#define CACHE_MISS_DELAY 10 // 10 cycle cache miss penalty
#define MAX_STAGES 5
// init the simulator
void iplc_sim_init(int index, int blocksize, int assoc);
// Cache simulator functions
void iplc_sim_LRU_replace_on_miss(int index, int tag);
void iplc_sim_LRU_update_on_hit(int index, int assoc);
int iplc_sim_trap_address(unsigned int address);
// Pipeline functions
unsigned int iplc_sim_parse_reg(char *reg_str);
void iplc_sim_parse_instruction(char *buffer);
void iplc_sim_push_pipeline_stage();
void iplc_sim_process_pipeline_rtype(char *instruction, int dest_reg,
int reg1, int reg2_or_constant);
void iplc_sim_process_pipeline_lw(int dest_reg, int base_reg, unsigned int data_address);
void iplc_sim_process_pipeline_sw(int src_reg, int base_reg, unsigned int data_address);
void iplc_sim_process_pipeline_branch(int reg1, int reg2);
void iplc_sim_process_pipeline_jump();
void iplc_sim_process_pipeline_syscall();
void iplc_sim_process_pipeline_nop();
// Outout performance results
void iplc_sim_finalize();
typedef struct cache_line
{
int *vdbt;
int *tag;
int asso;
int *replace;
// Your data structures for implementing your cache should include:
// a valid bit
// a tag
// a method for handling varying levels of associativity
// a method for selecting which item in the cache is going to be replaced
} cache_line_t;
cache_line_t *cache=NULL;
int cache_index=0;
int cache_blocksize=0;
int cache_blockoffsetbits = 0;
int cache_assoc=0;
long cache_miss=0;
long cache_access=0;
long cache_hit=0;
char instruction[16];
char reg1[16];
char reg2[16];
char offsetwithreg[16];
unsigned int data_address=0;
unsigned int instruction_address=0;
unsigned int pipeline_cycles=0; // how many cycles did you pipeline consume
unsigned int instruction_count=0; // home many real instructions ran thru the pipeline
unsigned int branch_predict_taken=0;
unsigned int branch_count=0;
unsigned int correct_branch_predictions=0;
unsigned int debug=0;
unsigned int dump_pipeline=1;
enum instruction_type {NOP, RTYPE, LW, SW, BRANCH, JUMP, JAL, SYSCALL};
typedef struct rtype
{
char instruction[16];
int reg1;
int reg2_or_constant;
int dest_reg;
} rtype_t;
typedef struct load_word
{
unsigned int data_address;
int dest_reg;
int base_reg;
} lw_t;
typedef struct store_word
{
unsigned int data_address;
int src_reg;
int base_reg;
} sw_t;
typedef struct branch
{
int reg1;
int reg2;
} branch_t;
typedef struct jump
{
char instruction[16];
} jump_t;
typedef struct pipeline
{
enum instruction_type itype;
unsigned int instruction_address;
union
{
rtype_t rtype;
lw_t lw;
sw_t sw;
branch_t branch;
jump_t jump;
}
stage;
} pipeline_t;
enum pipeline_stages {FETCH, DECODE, ALU, MEM, WRITEBACK};
pipeline_t pipeline[MAX_STAGES];
/************************************************************************************************/
/* Cache Functions ******************************************************************************/
/************************************************************************************************/
/*
* Correctly configure the cache.
*/
void iplc_sim_init(int index, int blocksize, int assoc)
{
int i=0, j=0;
unsigned long cache_size = 0;
cache_index = index;
cache_blocksize = blocksize;
cache_assoc = assoc;
cache_blockoffsetbits =
(int) rint((log( (double) (blocksize * 4) )/ log(2)));
/* Note: rint function rounds the result up prior to casting */
cache_size = assoc * ( 1 << index ) * ((32 * blocksize) + 33 - index - cache_blockoffsetbits);
printf("Cache Configuration \n");
printf(" Index: %d bits or %d lines \n", cache_index, (1<<cache_index) );
printf(" BlockSize: %d \n", cache_blocksize );
printf(" Associativity: %d \n", cache_assoc );
printf(" BlockOffSetBits: %d \n", cache_blockoffsetbits );
printf(" CacheSize: %lu \n", cache_size );
if (cache_size > MAX_CACHE_SIZE ) {
printf("Cache too big. Great than MAX SIZE of %d .... \n", MAX_CACHE_SIZE);
exit(-1);
}
cache = (cache_line_t *) malloc((sizeof(cache_line_t) * 1<<index));
// Dynamically create our cache based on the information the user entered
for (i = 0; i < (1<<index); i++) {
cache[i].vdbt = (int *)malloc((sizeof(int) * assoc));
cache[i].tag = (int *)malloc((sizeof(int) * assoc));
cache[i].replace = (int *)malloc((sizeof(int) * assoc));
for(j = 0; j < assoc; j++){
cache[i].vdbt[j] = 0;
cache[i].tag[j] = 0;
cache[i].replace[j] = j;
}
}
// init the pipeline -- set all data to zero and instructions to NOP
for (i = 0; i < MAX_STAGES; i++) {
// itype is set to O which is NOP type instruction
bzero(&(pipeline[i]), sizeof(pipeline_t));
}
}
/*
* iplc_sim_trap_address() determined this is not in our cache. Put it there
* and make sure that is now our Most Recently Used (MRU) entry.
*/
void iplc_sim_LRU_replace_on_miss(int index, int tag)
{
int repl = cache[index].replace[0];
//replace everything at least recently used
cache[index].vdbt[repl] = 1;
cache[index].tag[repl] = tag;
cache[index].replace[cache_assoc - 1] = repl;
for(int j = 0; j < cache_assoc - 1; j++){
//update lru
cache[index].replace[j] = cache[index].replace[j + 1];
}
}
/*
* iplc_sim_trap_address() determined the entry is in our cache. Update its
* information in the cache.
*/
void iplc_sim_LRU_update_on_hit(int index, int assoc_entry)
{
int update = 0;
//find update index
while(cache[index].replace[update] != assoc_entry){
update++;
}
//from there, update the lru
for(int i = update + 1; i < cache_assoc; i++){
cache[index].replace[i - 1] = cache[index].replace[i];
}
cache[index].replace[cache_assoc - 1] = assoc_entry;
}
/*
* Check if the address is in our cache. Update our counter statistics
* for cache_access, cache_hit, etc. If our configuration supports
* associativity we may need to check through multiple entries for our
* desired index. In that case we will also need to call the LRU functions.
*/
int iplc_sim_trap_address(unsigned int address)
{
int i=0, index=0;
int tag=0;
int hit=0;
unsigned int mask;
//Set up a bit mask of 1s the size of cache_index
mask = 1 << (cache_blockoffsetbits + cache_index);
mask--;
//Find the index bits of the address, then remove unnecessary bits
index = address & mask;
index = index >> (cache_blockoffsetbits);
//Set up a bit mask for finding tag (16 is a constant here)
mask = 1 << (cache_blockoffsetbits + cache_index + cache_assoc + 16);
mask--;
//Find the index bits of the address, then remove unecessary bits
tag = address & mask;
tag = tag >> (cache_blockoffsetbits + cache_index);
//Check to see if tag is in our cache already by going through our tag array
for(i = 0; i < cache_assoc; i++){
if ((cache[index].vdbt[i] == 1) && (cache[index].tag[i] == tag))
//If it is, stop here because we need i
hit = 1;
break;
}
printf("Address %x: Tag= %x, Index= %x\n", address,tag,index);
cache_access++;//Cache will always be accessed whether hit or miss
// Call the appropriate function for a miss or hit
if (hit == 1){
cache_hit++;
//Give the location where we found our value
iplc_sim_LRU_update_on_hit(index,i);
}
else{
cache_miss++;
//If not found give tag where we want to put our value in cache
iplc_sim_LRU_replace_on_miss(index,tag);
}
/* expects you to return 1 for hit, 0 for miss */
return hit;
}
/*
* Just output our summary statistics.
*/
void iplc_sim_finalize()
{
/* Finish processing all instructions in the Pipeline */
while (pipeline[FETCH].itype != NOP ||
pipeline[DECODE].itype != NOP ||
pipeline[ALU].itype != NOP ||
pipeline[MEM].itype != NOP ||
pipeline[WRITEBACK].itype != NOP) {
iplc_sim_push_pipeline_stage();
}
printf(" Cache Performance \n");
printf("\t Number of Cache Accesses is %ld \n", cache_access);
printf("\t Number of Cache Misses is %ld \n", cache_miss);
printf("\t Number of Cache Hits is %ld \n", cache_hit);
printf("\t Cache Miss Rate is %f \n\n", (double)cache_miss / (double)cache_access);
printf("Pipeline Performance \n");
printf("\t Total Cycles is %u \n", pipeline_cycles);
printf("\t Total Instructions is %u \n", instruction_count);
printf("\t Total Branch Instructions is %u \n", branch_count);
printf("\t Total Correct Branch Predictions is %u \n", correct_branch_predictions);
printf("\t CPI is %f \n\n", (double)pipeline_cycles / (double)instruction_count);
}
/************************************************************************************************/
/* Pipeline Functions ***************************************************************************/
/************************************************************************************************/
/*
* Dump the current contents of our pipeline.
*/
void iplc_sim_dump_pipeline()
{
int i;
for (i = 0; i < MAX_STAGES; i++) {
switch(i) {
case FETCH:
printf("(cyc: %u) FETCH:\t %d: 0x%x \t", pipeline_cycles, pipeline[i].itype, pipeline[i].instruction_address);
break;
case DECODE:
printf("DECODE:\t %d: 0x%x \t", pipeline[i].itype, pipeline[i].instruction_address);
break;
case ALU:
printf("ALU:\t %d: 0x%x \t", pipeline[i].itype, pipeline[i].instruction_address);
break;
case MEM:
printf("MEM:\t %d: 0x%x \t", pipeline[i].itype, pipeline[i].instruction_address);
break;
case WRITEBACK:
printf("WB:\t %d: 0x%x \n", pipeline[i].itype, pipeline[i].instruction_address);
break;
default:
printf("DUMP: Bad stage!\n" );
exit(-1);
}
}
}
/*
* Check if various stages of our pipeline require stalls, forwarding, etc.
* Then push the contents of our various pipeline stages through the pipeline.
*/
void iplc_sim_push_pipeline_stage()
{
/* 1. Count WRITEBACK stage is "retired" -- This I'm giving you */
if (pipeline[WRITEBACK].instruction_address) {
instruction_count++;
if (debug)
printf("DEBUG: Retired Instruction at 0x%x, Type %d, at Time %u \n",
pipeline[WRITEBACK].instruction_address, pipeline[WRITEBACK].itype, pipeline_cycles);
}
//now to count stalls and misses
int nopes = 0;
int misses = 0;
int dhit = 1;
/* 2. Check for BRANCH and correct/incorrect Branch Prediction */
if (pipeline[DECODE].itype == BRANCH) {
int branch_taken = 0;
branch_count++;
if(pipeline[FETCH].instruction_address >
(pipeline[DECODE].instruction_address + 4)){
branch_taken = 1;
}
if(branch_predict_taken == branch_taken){
correct_branch_predictions++;
}else{
nopes++;
}
}
/* 3. Check for LW delays due to use in ALU stage and if data hit/miss
* add delay cycles if needed.
*/
if (pipeline[MEM].itype == LW) {
//must check for stalls if lw is loading branch or rtype
//rtype:
if (pipeline[ALU].itype == RTYPE &&
(pipeline[MEM].stage.lw.dest_reg ==
pipeline[ALU].stage.rtype.reg1 ||
pipeline[MEM].stage.lw.dest_reg ==
pipeline[ALU].stage.rtype.reg2_or_constant)) {
nopes = 1;
}
if (pipeline[ALU].itype == BRANCH &&
(pipeline[MEM].stage.lw.dest_reg ==
pipeline[ALU].stage.branch.reg1 ||
pipeline[MEM].stage.lw.dest_reg ==
pipeline[ALU].stage.branch.reg2)){
nopes = 1;
}
//now check for hits or misses
dhit = iplc_sim_trap_address(pipeline[MEM].stage.lw.data_address);
if(dhit){
//this is a hit
printf("DATA HIT:\t Address 0x%x \n", pipeline[MEM].stage.lw.data_address);
}else{
//miss
printf("DATA MISS:\t Address 0x%x \n", pipeline[MEM].stage.lw.data_address);
//miss penalty
pipeline_cycles += CACHE_MISS_DELAY;
misses++;
}
}
/* 4. Check for SW mem acess and data miss .. add delay cycles if needed */
if (pipeline[MEM].itype == SW) {
//another hit miss check
dhit = dhit && iplc_sim_trap_address(pipeline[MEM].stage.lw.data_address);
if(dhit){
//hit
printf("DATA HIT:\t Address 0x%x \n", pipeline[MEM].stage.lw.data_address);
}else{
//miss
printf("DATA MISS:\t Address 0x%x \n", pipeline[MEM].stage.lw.data_address);
//penalties
pipeline_cycles += CACHE_MISS_DELAY;
misses++;
}
}
/* 5. Increment pipe_cycles 1 cycle for normal processing */
if(misses == 0){
pipeline_cycles += (nopes + 1);
}
/* 6. push stages thru MEM->WB, ALU->MEM, DECODE->ALU, FETCH->ALU */
for (int i = MAX_STAGES - 1; i > 0; i--) {
pipeline[i].itype = pipeline[i-1].itype;
pipeline[i].instruction_address = pipeline[i-1].instruction_address;
pipeline[i].stage = pipeline[i-1].stage;
}
// 7. This is a give'me -- Reset the FETCH stage to NOP via bezero */
bzero(&(pipeline[FETCH]), sizeof(pipeline_t));
}
/*
* This function is fully implemented. You should use this as a reference
* for implementing the remaining instruction types.
*/
void iplc_sim_process_pipeline_rtype(char *instruction, int dest_reg, int reg1, int reg2_or_constant)
{
/* This is an example of what you need to do for the rest */
iplc_sim_push_pipeline_stage();
pipeline[FETCH].itype = RTYPE;
pipeline[FETCH].instruction_address = instruction_address;
strcpy(pipeline[FETCH].stage.rtype.instruction, instruction);
pipeline[FETCH].stage.rtype.reg1 = reg1;
pipeline[FETCH].stage.rtype.reg2_or_constant = reg2_or_constant;
pipeline[FETCH].stage.rtype.dest_reg = dest_reg;
}
void iplc_sim_process_pipeline_lw(int dest_reg, int base_reg, unsigned int data_address)
{
iplc_sim_push_pipeline_stage();
pipeline[FETCH].itype = LW;
pipeline[FETCH].instruction_address = instruction_address;
pipeline[FETCH].stage.lw.data_address = data_address;
pipeline[FETCH].stage.lw.dest_reg = dest_reg;
pipeline[FETCH].stage.lw.base_reg = base_reg;
/* You must implement this function */
}
void iplc_sim_process_pipeline_sw(int src_reg, int base_reg, unsigned int data_address)
{
iplc_sim_push_pipeline_stage();
pipeline[FETCH].itype = SW;
pipeline[FETCH].instruction_address = instruction_address;
pipeline[FETCH].stage.sw.data_address = data_address;
pipeline[FETCH].stage.sw.src_reg = src_reg;
pipeline[FETCH].stage.sw.base_reg = base_reg;
/* You must implement this function */
}
void iplc_sim_process_pipeline_branch(int reg1, int reg2)
{
iplc_sim_push_pipeline_stage();
pipeline[FETCH].itype = BRANCH;
pipeline[FETCH].instruction_address = instruction_address;
pipeline[FETCH].stage.branch.reg1 = reg1;
pipeline[FETCH].stage.branch.reg2 = reg2;
/* You must implement this function */
}
void iplc_sim_process_pipeline_jump(char *instruction)
{
iplc_sim_push_pipeline_stage();
pipeline[FETCH].itype = JUMP;
pipeline[FETCH].instruction_address = instruction_address;
strcpy(pipeline[FETCH].stage.jump.instruction, instruction);
/* You must implement this function */
}
void iplc_sim_process_pipeline_syscall()
{
iplc_sim_push_pipeline_stage();
pipeline[FETCH].itype = SYSCALL;
pipeline[FETCH].instruction_address = instruction_address;
/* You must implement this function */
}
void iplc_sim_process_pipeline_nop()
{
iplc_sim_push_pipeline_stage();
pipeline[FETCH].itype = NOP;
pipeline[FETCH].instruction_address = 0;
/* You must implement this function */
}
/************************************************************************************************/
/* parse Function *******************************************************************************/
/************************************************************************************************/
/*
* Don't touch this function. It is for parsing the instruction stream.
*/
unsigned int iplc_sim_parse_reg(char *reg_str)
{
int i;
// turn comma into \n
if (reg_str[strlen(reg_str)-1] == ',')
reg_str[strlen(reg_str)-1] = '\n';
if (reg_str[0] != '$')
return atoi(reg_str);
else {
// copy down over $ character than return atoi
for (i = 0; i < strlen(reg_str); i++)
reg_str[i] = reg_str[i+1];
return atoi(reg_str);
}
}
/*
* Don't touch this function. It is for parsing the instruction stream.
*/
void iplc_sim_parse_instruction(char *buffer)
{
int instruction_hit = 0;
int i=0, j=0;
int src_reg=0;
int src_reg2=0;
int dest_reg=0;
char str_src_reg[16];
char str_src_reg2[16];
char str_dest_reg[16];
char str_constant[16];
if (sscanf(buffer, "%x %s", &instruction_address, instruction ) != 2) {
printf("Malformed instruction \n");
exit(-1);
}
instruction_hit = iplc_sim_trap_address( instruction_address );
// if a MISS, then push current instruction thru pipeline
if (!instruction_hit) {
// need to subtract 1, since the stage is pushed once more for actual instruction processing
// also need to allow for a branch miss prediction during the fetch cache miss time -- by
// counting cycles this allows for these cycles to overlap and not doubly count.
printf("INST MISS:\t Address 0x%x \n", instruction_address);
for (i = pipeline_cycles, j = pipeline_cycles; i < j + CACHE_MISS_DELAY - 1; i++)
iplc_sim_push_pipeline_stage();
}
else
printf("INST HIT:\t Address 0x%x \n", instruction_address);
// Parse the Instruction
if (strncmp( instruction, "add", 3 ) == 0 ||
strncmp( instruction, "sll", 3 ) == 0 ||
strncmp( instruction, "ori", 3 ) == 0) {
if (sscanf(buffer, "%x %s %s %s %s",
&instruction_address,
instruction,
str_dest_reg,
str_src_reg,
str_src_reg2 ) != 5) {
printf("Malformed RTYPE instruction (%s) at address 0x%x \n",
instruction, instruction_address);
exit(-1);
}
dest_reg = iplc_sim_parse_reg(str_dest_reg);
src_reg = iplc_sim_parse_reg(str_src_reg);
src_reg2 = iplc_sim_parse_reg(str_src_reg2);
iplc_sim_process_pipeline_rtype(instruction, dest_reg, src_reg, src_reg2);
}
else if (strncmp( instruction, "lui", 3 ) == 0) {
if (sscanf(buffer, "%x %s %s %s",
&instruction_address,
instruction,
str_dest_reg,
str_constant ) != 4 ) {
printf("Malformed RTYPE instruction (%s) at address 0x%x \n",
instruction, instruction_address );
exit(-1);
}
dest_reg = iplc_sim_parse_reg(str_dest_reg);
src_reg = -1;
src_reg2 = -1;
iplc_sim_process_pipeline_rtype(instruction, dest_reg, src_reg, src_reg2);
}
else if (strncmp( instruction, "lw", 2 ) == 0 ||
strncmp( instruction, "sw", 2 ) == 0 ) {
if ( sscanf( buffer, "%x %s %s %s %x",
&instruction_address,
instruction,
reg1,
offsetwithreg,
&data_address ) != 5) {
printf("Bad instruction: %s at address %x \n", instruction, instruction_address);
exit(-1);
}
if (strncmp(instruction, "lw", 2 ) == 0) {
dest_reg = iplc_sim_parse_reg(reg1);
// don't need to worry about base regs -- just insert -1 values
iplc_sim_process_pipeline_lw(dest_reg, -1, data_address);
}
if (strncmp( instruction, "sw", 2 ) == 0) {
src_reg = iplc_sim_parse_reg(reg1);
// don't need to worry about base regs -- just insert -1 values
iplc_sim_process_pipeline_sw( src_reg, -1, data_address);
}
}
else if (strncmp( instruction, "beq", 3 ) == 0) {
// don't need to worry about getting regs -- just insert -1 values
iplc_sim_process_pipeline_branch(-1, -1);
}
else if (strncmp( instruction, "jal", 3 ) == 0 ||
strncmp( instruction, "jr", 2 ) == 0 ||
strncmp( instruction, "j", 1 ) == 0 ) {
iplc_sim_process_pipeline_jump( instruction );
}
else if (strncmp( instruction, "jal", 3 ) == 0 ||
strncmp( instruction, "jr", 2 ) == 0 ||
strncmp( instruction, "j", 1 ) == 0 ) {
/*
* Note: no need to worry about forwarding on the jump register
* we'll let that one go.
*/
iplc_sim_process_pipeline_jump(instruction);
}
else if ( strncmp( instruction, "syscall", 7 ) == 0) {
iplc_sim_process_pipeline_syscall( );
}
else if ( strncmp( instruction, "nop", 3 ) == 0) {
iplc_sim_process_pipeline_nop( );
}
else {
printf("Do not know how to process instruction: %s at address %x \n",
instruction, instruction_address );
exit(-1);
}
}
/************************************************************************************************/
/* MAIN Function ********************************************************************************/
/************************************************************************************************/
int main()
{
char trace_file_name[1024];
FILE *trace_file = NULL;
char buffer[80];
int index = 10;
int blocksize = 1;
int assoc = 1;
printf("Please enter the tracefile: ");
scanf("%s", trace_file_name);
trace_file = fopen(trace_file_name, "r");
if ( trace_file == NULL ) {
printf("fopen failed for %s file\n", trace_file_name);
exit(-1);
}
printf("Enter Cache Size (index), Blocksize and Level of Assoc \n");
scanf( "%d %d %d", &index, &blocksize, &assoc );
printf("Enter Branch Prediction: 0 (NOT taken), 1 (TAKEN): ");
scanf("%d", &branch_predict_taken );
iplc_sim_init(index, blocksize, assoc);
while (fgets(buffer, 80, trace_file) != NULL) {
iplc_sim_parse_instruction(buffer);
if (dump_pipeline)
iplc_sim_dump_pipeline();
}
iplc_sim_finalize();
return 0;
}