-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsim.c
1610 lines (1383 loc) · 43.4 KB
/
psim.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
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************
* psim.c - Pipelined Y86-64 simulator
*
* Copyright (c) 2010, 2015. Bryant and D. O'Hallaron, All rights reserved.
* May not be used, modified, or copied without permission.
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include "isa.h"
#include "pipeline.h"
#include "stages.h"
#include "sim.h"
#define MAXBUF 1024
#define DEFAULTNAME "Y86-64 Simulator: "
#ifdef HAS_GUI
#define USE_INTERP_RESULT
#include <tk.h>
#endif /* HAS_GUI */
#define MAXARGS 128
#define MAXBUF 1024
#define TKARGS 3
/***************
* Begin Globals
***************/
/* Simulator name defined and initialized by the compiled HCL file */
/* according to the -n argument supplied to hcl2c */
extern char simname[];
/* Parameters modifed by the command line */
int gui_mode = FALSE; /* Run in GUI mode instead of TTY mode? (-g) */
char *object_filename; /* The input object file name. */
FILE *object_file; /* Input file handle */
bool_t verbosity = 2; /* Verbosity level [TTY only] (-v) */
word_t instr_limit = 10000; /* Instruction limit [TTY only] (-l) */
bool_t do_check = FALSE; /* Test with ISA simulator? [TTY only] (-t) */
/*************
* End Globals
*************/
/***************************
* Begin function prototypes
***************************/
word_t sim_run_pipe(word_t max_instr, word_t max_cycle, byte_t *statusp, cc_t *ccp);
static void usage(char *name); /* Print helpful usage message */
static void run_tty_sim(); /* Run simulator in TTY mode */
#ifdef HAS_GUI
void addAppCommands(Tcl_Interp *interp); /* Add application-dependent commands */
#endif /* HAS_GUI */
/*************************
* End function prototypes
*************************/
/*******************************************************************
* Part 1: This part is the initial entry point that handles general
* initialization. It parses the command line and does any necessary
* setup to run in either TTY or GUI mode, and then starts the
* simulation.
*******************************************************************/
/*
* sim_main - main simulator routine. This function is called from the
* main() routine in the HCL file.
*/
int sim_main(int argc, char **argv)
{
int i;
int c;
char *myargv[MAXARGS];
/* Parse the command line arguments */
while ((c = getopt(argc, argv, "htgl:v:")) != -1) {
switch(c) {
case 'h':
usage(argv[0]);
break;
case 'l':
instr_limit = atoll(optarg);
break;
case 'v':
verbosity = atoi(optarg);
if (verbosity < 0 || verbosity > 2) {
printf("Invalid verbosity %d\n", verbosity);
usage(argv[0]);
}
break;
case 't':
do_check = TRUE;
break;
case 'g':
gui_mode = TRUE;
break;
default:
printf("Invalid option '%c'\n", c);
usage(argv[0]);
break;
}
}
/* Do we have too many arguments? */
if (optind < argc - 1) {
printf("Too many command line arguments:");
for (i = optind; i < argc; i++)
printf(" %s", argv[i]);
printf("\n");
usage(argv[0]);
}
/* The single unflagged argument should be the object file name */
object_filename = NULL;
object_file = NULL;
if (optind < argc) {
object_filename = argv[optind];
object_file = fopen(object_filename, "r");
if (!object_file) {
fprintf(stderr, "Couldn't open object file %s\n", object_filename);
exit(1);
}
}
/* Run the simulator in GUI mode (-g flag) */
if (gui_mode) {
#ifndef HAS_GUI
printf("To run in GUI mode, you must recompile with the HAS_GUI constant defined.\n");
exit(1);
#endif /* HAS_GUI */
/* In GUI mode, we must specify the object file on command line */
if (!object_file) {
printf("Missing object file argument in GUI mode\n");
usage(argv[0]);
}
/* Build the command line for the GUI simulator */
for (i = 0; i < TKARGS; i++) {
if ((myargv[i] = malloc(MAXBUF*sizeof(char))) == NULL) {
perror("malloc error");
exit(1);
}
}
strcpy(myargv[0], argv[0]);
strcpy(myargv[1], "pipe.tcl");
strcpy(myargv[2], object_filename);
myargv[3] = NULL;
/* Start the GUI simulator */
#ifdef HAS_GUI
Tk_Main(TKARGS, myargv, Tcl_AppInit);
#endif /* HAS_GUI */
exit(0);
}
/* Otherwise, run the simulator in TTY mode (no -g flag) */
run_tty_sim();
exit(0);
}
/*
* run_tty_sim - Run the simulator in TTY mode
*/
static void run_tty_sim()
{
word_t icount = 0;
byte_t run_status = STAT_AOK;
cc_t result_cc = 0;
word_t byte_cnt = 0;
mem_t mem0, reg0;
state_ptr isa_state = NULL;
/* In TTY mode, the default object file comes from stdin */
if (!object_file) {
object_file = stdin;
}
if (verbosity >= 2)
sim_set_dumpfile(stdout);
sim_init();
/* Emit simulator name */
if (verbosity >= 2)
printf("%s\n", simname);
byte_cnt = load_mem(mem, object_file, 1);
if (byte_cnt == 0) {
fprintf(stderr, "No lines of code found\n");
exit(1);
} else if (verbosity >= 2) {
printf("%lld bytes of code read\n", byte_cnt);
}
fclose(object_file);
if (do_check) {
isa_state = new_state(0);
free_mem(isa_state->r);
free_mem(isa_state->m);
isa_state->m = copy_mem(mem);
isa_state->r = copy_mem(reg);
isa_state->cc = cc;
}
mem0 = copy_mem(mem);
reg0 = copy_mem(reg);
icount = sim_run_pipe(instr_limit, 5*instr_limit, &run_status, &result_cc);
if (verbosity > 0) {
printf("%lld instructions executed\n", icount);
printf("Status = %s\n", stat_name(run_status));
printf("Condition Codes: %s\n", cc_name(result_cc));
printf("Changed Register State:\n");
diff_reg(reg0, reg, stdout);
printf("Changed Memory State:\n");
diff_mem(mem0, mem, stdout);
}
if (do_check) {
byte_t e = STAT_AOK;
word_t step;
bool_t match = TRUE;
for (step = 0; step < instr_limit && e == STAT_AOK; step++) {
e = step_state(isa_state, stdout);
}
if (diff_reg(isa_state->r, reg, NULL)) {
match = FALSE;
if (verbosity > 0) {
printf("ISA Register != Pipeline Register File\n");
diff_reg(isa_state->r, reg, stdout);
}
}
if (diff_mem(isa_state->m, mem, NULL)) {
match = FALSE;
if (verbosity > 0) {
printf("ISA Memory != Pipeline Memory\n");
diff_mem(isa_state->m, mem, stdout);
}
}
if (isa_state->cc != result_cc) {
match = FALSE;
if (verbosity > 0) {
printf("ISA Cond. Codes (%s) != Pipeline Cond. Codes (%s)\n",
cc_name(isa_state->cc), cc_name(result_cc));
}
}
if (match) {
printf("ISA Check Succeeds\n");
} else {
printf("ISA Check Fails\n");
}
}
/* Emit CPI statistics */
{
double cpi = instructions > 0 ? (double) cycles/instructions : 1.0;
printf("CPI: %lld cycles/%lld instructions = %.2f\n",
cycles, instructions, cpi);
}
}
/*
* usage - print helpful diagnostic information
*/
static void usage(char *name)
{
printf("Usage: %s [-htg] [-l m] [-v n] file.yo\n", name);
printf("file.yo arg required in GUI mode, optional in TTY mode (default stdin)\n");
printf(" -h Print this message\n");
printf(" -g Run in GUI mode instead of TTY mode (default TTY)\n");
printf(" -l m Set instruction limit to m [TTY mode only] (default %lld)\n", instr_limit);
printf(" -v n Set verbosity level to 0 <= n <= 2 [TTY mode only] (default %d)\n", verbosity);
printf(" -t Test result against ISA simulator [TTY mode only]\n");
exit(0);
}
/*********************************************************
* Part 2: This part contains the core simulator routines.
*********************************************************/
/*****************
* Part 2 Globals
*****************/
/* Performance monitoring */
/* How many cycles have been simulated? */
word_t cycles = 0;
/* How many instructions have passed through the WB stage? */
word_t instructions = 0;
/* Has simulator gotten past initial bubbles? */
static int starting_up = 1;
/* Both instruction and data memory */
mem_t mem;
word_t minAddr = 0;
word_t memCnt = 0;
/* Register file */
mem_t reg;
/* Condition code register */
cc_t cc;
/* Status code */
stat_t status;
/* Pending updates to state */
word_t cc_in = DEFAULT_CC;
word_t wb_destE = REG_NONE;
word_t wb_valE = 0;
word_t wb_destM = REG_NONE;
word_t wb_valM = 0;
word_t mem_addr = 0;
word_t mem_data = 0;
bool_t mem_write = FALSE;
/* EX Operand sources */
mux_source_t amux = MUX_NONE;
mux_source_t bmux = MUX_NONE;
/* Current and next states of all pipeline registers */
pc_ptr pc_curr;
if_id_ptr if_id_curr;
id_ex_ptr id_ex_curr;
ex_mem_ptr ex_mem_curr;
mem_wb_ptr mem_wb_curr;
pc_ptr pc_next;
if_id_ptr if_id_next;
id_ex_ptr id_ex_next;
ex_mem_ptr ex_mem_next;
mem_wb_ptr mem_wb_next;
/* Intermediate values */
word_t f_pc;
byte_t imem_icode;
byte_t imem_ifun;
bool_t imem_error;
bool_t instr_valid;
word_t d_regvala;
word_t d_regvalb;
word_t e_vala;
word_t e_valb;
bool_t e_bcond;
bool_t dmem_error;
/* The pipeline state */
pipe_ptr pc_state, if_id_state, id_ex_state, ex_mem_state, mem_wb_state;
/* Simulator operating mode */
sim_mode_t sim_mode = S_FORWARD;
/* Log file */
FILE *dumpfile = NULL;
/*****************************************************************************
* reporting code
*****************************************************************************/
#ifdef HAS_GUI
/* used for formatting instructions */
static char status_msg[128];
static char *format_pc(pc_ptr state)
{
char pstring[17];
wstring(state->pc, 4, 64, pstring);
sprintf(status_msg, "%s %s", stat_name(state->status), pstring);
return status_msg;
}
static char *format_if_id(if_id_ptr state)
{
char valcstring[17];
char valpstring[17];
wstring(state->valc, 4, 64, valcstring);
wstring(state->valp, 4, 64, valpstring);
sprintf(status_msg, "%s %s %s %s %s %s",
stat_name(state->status),
iname(HPACK(state->icode,state->ifun)),
reg_name(state->ra),
reg_name(state->rb),
valcstring,
valpstring);
return status_msg;
}
static char *format_id_ex(id_ex_ptr state)
{
char valcstring[17];
char valastring[17];
char valbstring[17];
wstring(state->valc, 4, 64, valcstring);
wstring(state->vala, 4, 64, valastring);
wstring(state->valb, 4, 64, valbstring);
sprintf(status_msg, "%s %s %s %s %s %s %s %s %s",
stat_name(state->status),
iname(HPACK(state->icode, state->ifun)),
valcstring,
valastring,
valbstring,
reg_name(state->deste),
reg_name(state->destm),
reg_name(state->srca),
reg_name(state->srcb));
return status_msg;
}
static char *format_ex_mem(ex_mem_ptr state)
{
char valestring[17];
char valastring[17];
wstring(state->vale, 4, 64, valestring);
wstring(state->vala, 4, 64, valastring);
sprintf(status_msg, "%s %s %c %s %s %s %s",
stat_name(state->status),
iname(HPACK(state->icode, state->ifun)),
state->takebranch ? 'Y' : 'N',
valestring,
valastring,
reg_name(state->deste),
reg_name(state->destm));
return status_msg;
}
static char *format_mem_wb(mem_wb_ptr state)
{
char valestring[17];
char valmstring[17];
wstring(state->vale, 4, 64, valestring);
wstring(state->valm, 4, 64, valmstring);
sprintf(status_msg, "%s %s %s %s %s %s",
stat_name(state->status),
iname(HPACK(state->icode, state->ifun)),
valestring,
valmstring,
reg_name(state->deste),
reg_name(state->destm));
return status_msg;
}
#endif /* HAS_GUI */
/* Report system state */
static void sim_report()
{
#ifdef HAS_GUI
if (gui_mode) {
report_pc(f_pc, pc_curr->status != STAT_BUB,
if_id_curr->stage_pc, if_id_curr->status != STAT_BUB,
id_ex_curr->stage_pc, id_ex_curr->status != STAT_BUB,
ex_mem_curr->stage_pc, ex_mem_curr->status != STAT_BUB,
mem_wb_curr->stage_pc, mem_wb_curr->status != STAT_BUB);
report_state("F", 0, format_pc(pc_next));
report_state("F", 1, format_pc(pc_curr));
report_state("D", 0, format_if_id(if_id_next));
report_state("D", 1, format_if_id(if_id_curr));
report_state("E", 0, format_id_ex(id_ex_next));
report_state("E", 1, format_id_ex(id_ex_curr));
report_state("M", 0, format_ex_mem(ex_mem_next));
report_state("M", 1, format_ex_mem(ex_mem_curr));
report_state("W", 0, format_mem_wb(mem_wb_next));
report_state("W", 1, format_mem_wb(mem_wb_curr));
/* signal_sources(); */
show_cc(cc);
show_stat(status);
show_cpi();
}
#endif
}
/*****************************************************************************
* pipeline control
* These functions can be used to handle hazards
*****************************************************************************/
/* bubble stage (has effect at next update) */
void sim_bubble_stage(stage_id_t stage)
{
switch (stage)
{
case IF_STAGE : pc_state->op = P_BUBBLE; break;
case ID_STAGE : if_id_state->op = P_BUBBLE; break;
case EX_STAGE : id_ex_state->op = P_BUBBLE; break;
case MEM_STAGE: ex_mem_state->op = P_BUBBLE; break;
case WB_STAGE : mem_wb_state->op = P_BUBBLE; break;
}
}
/* stall stage (has effect at next update) */
void sim_stall_stage(stage_id_t stage) {
switch (stage)
{
case IF_STAGE : pc_state->op = P_STALL; break;
case ID_STAGE : if_id_state->op = P_STALL; break;
case EX_STAGE : id_ex_state->op = P_STALL; break;
case MEM_STAGE: ex_mem_state->op = P_STALL; break;
case WB_STAGE : mem_wb_state->op = P_STALL; break;
}
}
static int initialized = 0;
void sim_init()
{
/* Create memory and register files */
initialized = 1;
mem = init_mem(MEM_SIZE);
reg = init_reg();
/* create 5 pipe registers */
pc_state = new_pipe(sizeof(pc_ele), (void *) &bubble_pc);
if_id_state = new_pipe(sizeof(if_id_ele), (void *) &bubble_if_id);
id_ex_state = new_pipe(sizeof(id_ex_ele), (void *) &bubble_id_ex);
ex_mem_state = new_pipe(sizeof(ex_mem_ele), (void *) &bubble_ex_mem);
mem_wb_state = new_pipe(sizeof(mem_wb_ele), (void *) &bubble_mem_wb);
/* connect them to the pipeline stages */
pc_next = pc_state->next;
pc_curr = pc_state->current;
if_id_next = if_id_state->next;
if_id_curr = if_id_state->current;
id_ex_next = id_ex_state->next;
id_ex_curr = id_ex_state->current;
ex_mem_next = ex_mem_state->next;
ex_mem_curr = ex_mem_state->current;
mem_wb_next = mem_wb_state->next;
mem_wb_curr = mem_wb_state->current;
sim_reset();
clear_mem(mem);
}
void sim_reset()
{
if (!initialized)
sim_init();
clear_pipes();
clear_mem(reg);
minAddr = 0;
memCnt = 0;
starting_up = 1;
cycles = instructions = 0;
cc = DEFAULT_CC;
status = STAT_AOK;
#ifdef HAS_GUI
if (gui_mode) {
signal_register_clear();
create_memory_display();
}
#endif
amux = bmux = MUX_NONE;
cc = cc_in = DEFAULT_CC;
wb_destE = REG_NONE;
wb_valE = 0;
wb_destM = REG_NONE;
wb_valM = 0;
mem_addr = 0;
mem_data = 0;
mem_write = FALSE;
sim_report();
}
/* Update state elements */
/* May need to disable updating of memory & condition codes */
static void update_state(bool_t update_mem, bool_t update_cc)
{
/* Writeback(s):
If either register is REG_NONE, write will have no effect .
Order of two writes determines semantics of
popl %rsp. According to ISA, %rsp will get popped value
*/
if (wb_destE != REG_NONE) {
sim_log("\tWriteback: Wrote 0x%llx to register %s\n",
wb_valE, reg_name(wb_destE));
set_reg_val(reg, wb_destE, wb_valE);
}
if (wb_destM != REG_NONE) {
sim_log("\tWriteback: Wrote 0x%llx to register %s\n",
wb_valM, reg_name(wb_destM));
set_reg_val(reg, wb_destM, wb_valM);
}
/* Memory write */
if (mem_write && !update_mem) {
sim_log("\tDisabled write of 0x%llx to address 0x%llx\n", mem_data, mem_addr);
}
if (update_mem && mem_write) {
if (!set_word_val(mem, mem_addr, mem_data)) {
sim_log("\tCouldn't write to address 0x%llx\n", mem_addr);
} else {
sim_log("\tWrote 0x%llx to address 0x%llx\n", mem_data, mem_addr);
#ifdef HAS_GUI
if (gui_mode) {
if (mem_addr % 8 != 0) {
/* Just did a misaligned write.
Need to display both words */
word_t align_addr = mem_addr & ~0x3;
word_t val;
get_word_val(mem, align_addr, &val);
set_memory(align_addr, val);
align_addr+=8;
get_word_val(mem, align_addr, &val);
set_memory(align_addr, val);
} else {
set_memory(mem_addr, mem_data);
}
}
#endif
}
}
if (update_cc)
cc = cc_in;
}
/* Text representation of status */
void tty_report(word_t cyc) {
sim_log("\nCycle %lld. CC=%s, Stat=%s\n", cyc, cc_name(cc), stat_name(status));
sim_log("F: predPC = 0x%llx\n", pc_curr->pc);
sim_log("D: instr = %s, rA = %s, rB = %s, valC = 0x%llx, valP = 0x%llx, Stat = %s\n",
iname(HPACK(if_id_curr->icode, if_id_curr->ifun)),
reg_name(if_id_curr->ra), reg_name(if_id_curr->rb),
if_id_curr->valc, if_id_curr->valp,
stat_name(if_id_curr->status));
sim_log("E: instr = %s, valC = 0x%llx, valA = 0x%llx, valB = 0x%llx\n srcA = %s, srcB = %s, dstE = %s, dstM = %s, Stat = %s\n",
iname(HPACK(id_ex_curr->icode, id_ex_curr->ifun)),
id_ex_curr->valc, id_ex_curr->vala, id_ex_curr->valb,
reg_name(id_ex_curr->srca), reg_name(id_ex_curr->srcb),
reg_name(id_ex_curr->deste), reg_name(id_ex_curr->destm),
stat_name(id_ex_curr->status));
sim_log("M: instr = %s, Cnd = %d, valE = 0x%llx, valA = 0x%llx\n dstE = %s, dstM = %s, Stat = %s\n",
iname(HPACK(ex_mem_curr->icode, ex_mem_curr->ifun)),
ex_mem_curr->takebranch,
ex_mem_curr->vale, ex_mem_curr->vala,
reg_name(ex_mem_curr->deste), reg_name(ex_mem_curr->destm),
stat_name(ex_mem_curr->status));
sim_log("W: instr = %s, valE = 0x%llx, valM = 0x%llx, dstE = %s, dstM = %s, Stat = %s\n",
iname(HPACK(mem_wb_curr->icode, mem_wb_curr->ifun)),
mem_wb_curr->vale, mem_wb_curr->valm,
reg_name(mem_wb_curr->deste), reg_name(mem_wb_curr->destm),
stat_name(mem_wb_curr->status));
}
/* Run pipeline for one cycle */
/* Return status of processor */
/* Max_instr indicates maximum number of instructions that
want to complete during this simulation run. */
static byte_t sim_step_pipe(word_t max_instr, word_t ccount)
{
byte_t wb_status = mem_wb_curr->status;
byte_t mem_status = mem_wb_next->status;
/* How many instructions are ahead of one in wb / ex? */
int ahead_mem = (wb_status != STAT_BUB);
int ahead_ex = ahead_mem + (mem_status != STAT_BUB);
bool_t update_mem = ahead_mem < max_instr;
bool_t update_cc = ahead_ex < max_instr;
/* Update program-visible state */
update_state(update_mem, update_cc);
/* Update pipe registers */
update_pipes();
tty_report(ccount);
if (pc_state->op == P_ERROR)
pc_curr->status = STAT_PIP;
if (if_id_state->op == P_ERROR)
if_id_curr->status = STAT_PIP;
if (id_ex_state->op == P_ERROR)
id_ex_curr->status = STAT_PIP;
if (ex_mem_state->op == P_ERROR)
ex_mem_curr->status = STAT_PIP;
if (mem_wb_state->op == P_ERROR)
mem_wb_curr->status = STAT_PIP;
/* Need to do decode after execute & memory stages,
and memory stage before execute, in order to propagate
forwarding values properly */
do_if_stage();
do_mem_stage();
do_ex_stage();
do_id_wb_stages();
do_stall_check();
#if 0
/* This doesn't seem necessary */
if (id_ex_curr->status != STAT_AOK
&& id_ex_curr->status != STAT_BUB) {
if_id_state->op = P_BUBBLE;
id_ex_state->op = P_BUBBLE;
}
#endif
/* Performance monitoring */
if (mem_wb_curr->status != STAT_BUB && mem_wb_curr->icode != I_POP2) {
starting_up = 0;
instructions++;
cycles++;
} else {
if (!starting_up)
cycles++;
}
sim_report();
return status;
}
/*
Run pipeline until one of following occurs:
- An error status is encountered in WB.
- max_instr instructions have completed through WB
- max_cycle cycles have been simulated
Return number of instructions executed.
if statusp nonnull, then will be set to status of final instruction
if ccp nonnull, then will be set to condition codes of final instruction
*/
word_t sim_run_pipe(word_t max_instr, word_t max_cycle, byte_t *statusp, cc_t *ccp)
{
word_t icount = 0;
word_t ccount = 0;
byte_t run_status = STAT_AOK;
while (icount < max_instr && ccount < max_cycle) {
run_status = sim_step_pipe(max_instr-icount, ccount);
if (run_status != STAT_BUB)
icount++;
if (run_status != STAT_AOK && run_status != STAT_BUB)
break;
ccount++;
}
if (statusp)
*statusp = run_status;
if (ccp)
*ccp = cc;
return icount;
}
/* If dumpfile set nonNULL, lots of status info printed out */
void sim_set_dumpfile(FILE *df)
{
dumpfile = df;
}
/*
* sim_log dumps a formatted string to the dumpfile, if it exists
* accepts variable argument list
*/
void sim_log( const char *format, ... ) {
if (dumpfile) {
va_list arg;
va_start( arg, format );
vfprintf( dumpfile, format, arg );
va_end( arg );
}
}
/*************************************************************
* Part 3: This part contains support for the GUI simulator
*************************************************************/
#ifdef HAS_GUI
/**********************
* Begin Part 3 globals
**********************/
/* Hack for SunOS */
/* WZC 2018.12.18
* Change of matherr & tclDummyMathPtr
*/
//extern int matherr();
//int *tclDummyMathPtr = (int *) matherr;
static char tcl_msg[256];
/* Keep track of the TCL Interpreter */
static Tcl_Interp *sim_interp = NULL;
static mem_t post_load_mem;
/**********************
* End Part 3 globals
**********************/
/******************************************************************************
* function declarations
******************************************************************************/
int simResetCmd(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[]);
int simLoadCodeCmd(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[]);
int simLoadDataCmd(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[]);
int simRunCmd(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[]);
int simModeCmd(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[]);
void addAppCommands(Tcl_Interp *interp);
/******************************************************************************
* tcl command definitions
******************************************************************************/
/* Implement command versions of the simulation functions */
int simResetCmd(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
sim_interp = interp;
if (argc != 1) {
interp->result = "No arguments allowed";
return TCL_ERROR;
}
sim_reset();
if (post_load_mem) {
free_mem(mem);
mem = copy_mem(post_load_mem);
}
interp->result = stat_name(STAT_AOK);
return TCL_OK;
}
int simLoadCodeCmd(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
FILE *code_file;
word_t code_count;
sim_interp = interp;
if (argc != 2) {
interp->result = "One argument required";
return TCL_ERROR;
}
code_file = fopen(argv[1], "r");
if (!code_file) {
sprintf(tcl_msg, "Couldn't open code file '%s'", argv[1]);
interp->result = tcl_msg;
return TCL_ERROR;
}
sim_reset();
code_count = load_mem(mem, code_file, 0);
post_load_mem = copy_mem(mem);
sprintf(tcl_msg, "%lld", code_count);
interp->result = tcl_msg;
fclose(code_file);
return TCL_OK;
}
int simLoadDataCmd(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
FILE *data_file;
word_t word_count = 0;
interp->result = "Not implemented";
return TCL_ERROR;
sim_interp = interp;
if (argc != 2) {
interp->result = "One argument required";
return TCL_ERROR;
}
data_file = fopen(argv[1], "r");
if (!data_file) {
sprintf(tcl_msg, "Couldn't open data file '%s'", argv[1]);
interp->result = tcl_msg;
return TCL_ERROR;
}
sprintf(tcl_msg, "%lld", word_count);
interp->result = tcl_msg;
fclose(data_file);
return TCL_OK;
}
int simRunCmd(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
word_t cycle_limit = 1;
byte_t status;
cc_t cc;
sim_interp = interp;
if (argc > 2) {
interp->result = "At most one argument allowed";
return TCL_ERROR;
}
if (argc >= 2 &&
(sscanf(argv[1], "%lld", &cycle_limit) != 1 ||
cycle_limit < 0)) {
sprintf(tcl_msg, "Cannot run for '%s' cycles!", argv[1]);
interp->result = tcl_msg;
return TCL_ERROR;
}
sim_run_pipe(cycle_limit + 5, cycle_limit, &status, &cc);
interp->result = stat_name(status);
return TCL_OK;
}
int simModeCmd(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
sim_interp = interp;
if (argc != 2) {
interp->result = "One argument required";
return TCL_ERROR;
}
interp->result = argv[1];
if (strcmp(argv[1], "wedged") == 0)
sim_mode = S_WEDGED;
else if (strcmp(argv[1], "stall") == 0)
sim_mode = S_STALL;
else if (strcmp(argv[1], "forward") == 0)
sim_mode = S_FORWARD;
else {
sprintf(tcl_msg, "Unknown mode '%s'", argv[1]);
interp->result = tcl_msg;
return TCL_ERROR;
}
return TCL_OK;
}
/******************************************************************************
* registering the commands with tcl
******************************************************************************/
void addAppCommands(Tcl_Interp *interp)
{
sim_interp = interp;
Tcl_CreateCommand(interp, "simReset", (Tcl_CmdProc *) simResetCmd,
(ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
Tcl_CreateCommand(interp, "simCode", (Tcl_CmdProc *) simLoadCodeCmd,
(ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
Tcl_CreateCommand(interp, "simData", (Tcl_CmdProc *) simLoadDataCmd,
(ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
Tcl_CreateCommand(interp, "simRun", (Tcl_CmdProc *) simRunCmd,
(ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
Tcl_CreateCommand(interp, "setSimMode", (Tcl_CmdProc *) simModeCmd,
(ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
}
/******************************************************************************
* tcl functionality called from within C
******************************************************************************/
/* Provide mechanism for simulator to update register display */
void signal_register_update(reg_id_t r, word_t val) {
int code;
sprintf(tcl_msg, "setReg %d %lld 1", (int) r, (word_t) val);
code = Tcl_Eval(sim_interp, tcl_msg);
if (code != TCL_OK) {
fprintf(stderr, "Failed to signal register set\n");
fprintf(stderr, "Error Message was '%s'\n", sim_interp->result);