forked from PicoPET/thumbulator-stm32f0x
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim_support.c
More file actions
1244 lines (1107 loc) · 39.4 KB
/
Copy pathsim_support.c
File metadata and controls
1244 lines (1107 loc) · 39.4 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
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
#include <stdlib.h>
#if MD5
#include <openssl/md5.h>
#endif
#include "sim_support.h"
#include "exmemwb.h"
#include "rsp-server.h"
u64 cycleCount = 0;
u64 last_insnCount = 0;
u64 insnCount = 0;
u64 wastedCycles = 0;
u32 cyclesSinceReset = 0;
u32 cyclesSinceCP = 0;
u32 resetAfterCycles = 0;
u32 addrOfCP = 0;
u32 addrOfRestoreCP = 0;
u32 do_reset = 0;
u32 wdt_seed = 0;
u32 wdt_val = 0;
u32 md5[4] = {0,0,0,0,0};
u32 PRINT_STATE_DIFF = PRINT_STATE_DIFF_INIT;
#if MEM_COUNT_INST
u32 store_count = 0;
u32 load_count = 0;
u32 cp_count = 0;
#endif
u32 ram[RAM_SIZE >> 2];
u32 flash[FLASH_SIZE >> 2];
// Performance counter start/stop PC values.
// The default value of 0 prevents the starting/stopping of traces
// without explicit --from-pc/--to-pc option setting since in Thumb
// mode only odd PC values are valid.
u32 trace_start_pc = 0;
u32 trace_stop_pc = 0;
// Event counters
u64 ram_data_reads = 0;
u64 ram_insn_reads = 0;
u64 ram_writes = 0;
u64 flash_data_reads = 0;
u64 flash_insn_reads = 0;
u64 flash_writes = 0;
u64 taken_branches = 0;
u64 nonword_branch_destinations = 0;
u64 ram_insn_prefetch_hits = 0;
u64 flash_insn_prefetch_hits = 0;
u64 nonword_taken_branches = 0;
u64 bl_insns = 0, blx_insns = 0, bx_insns = 0;
u64 last_bl_insns = 0, last_blx_insns = 0, last_bx_insns = 0;
bool branch_fetch_stall = 0;
u64 branch_fetch_stalls = 0;
bool ram_access = 0; // Boolean to mark a RAM request in the current decode cycle
bool flash_access = 0; // Boolean to mark a Flash request in the current decode cycle
u64 arbitration_conflicts = 0; // Count of potential RAM/Flash arbitration conflicts
// Last snapshot of event counters
u64 last_ram_data_reads = 0;
u64 last_ram_insn_reads = 0;
u64 last_ram_writes = 0;
u64 last_flash_data_reads = 0;
u64 last_flash_insn_reads = 0;
u64 last_flash_writes = 0;
u64 last_taken_branches = 0;
u64 last_nonword_branch_destinations = 0;
u64 last_ram_insn_prefetch_hits = 0;
u64 last_flash_insn_prefetch_hits = 0;
u64 last_nonword_taken_branches = 0;
u64 last_branch_fetch_stalls = 0;
u64 last_arbitration_conflicts = 0;
// Current/future data bus accesses
bool data_access_in_cur_cycle = 0;
bool data_access_in_next_cycle = 0;
bool data_access_in_two_cycles = 0;
bool data_access_in_three_cycles = 0;
// Current/PAST loads, stores, compares
bool load_in_cur_insn = 0;
bool load_in_prev_insn = 0;
bool store_in_cur_insn = 0;
bool store_in_prev_insn = 0;
char reg_loaded_in_cur_insn = -1;
char reg_loaded_in_prev_insn = -1;
bool cmp_in_cur_insn = 0;
// Count of back-to-banck mem operations.
u64 load_after_load = 0, last_load_after_load = 0;
u64 load_after_store = 0, last_load_after_store = 0;
u64 store_after_load = 0, last_store_after_load = 0;
u64 store_after_store = 0, last_store_after_store = 0;
u64 use_after_load_ld = 0, last_use_after_load_ld = 0;
u64 use_after_load_st = 0, last_use_after_load_st = 0;
u64 use_after_load_alu = 0, last_use_after_load_alu = 0;
u64 use_after_load_cmp = 0, last_use_after_load_cmp = 0;
u64 burst_loads = 0, last_burst_loads = 0, burst_stores = 0, last_burst_stores = 0;
u64 pop_high_regs = 0, pop_sp = 0, pop_pc = 0;
u64 last_pop_high_regs = 0, last_pop_sp = 0, last_pop_pc = 0;
u64 word_aligned_bl = 0, last_word_aligned_bl = 0;
bool use_after_load_seen = 0;
bool store_addr_reg_load_in_prev_insn = 0;
// Prefetch buffering: single word version for 32-bit memories
u32 last_fetched_address = 0xffffffff;
u32 last_fetched_word = 0xffffffff;
u32 prefetch_mode = PREFETCH_MODE_NONE;
u32 prefetch_addresses[3] = { 0xffffffff, 0xffffffff, 0xffffffff };
u32 prefetch_words[3] = { 0xffffffff, 0xffffffff, 0xffffffff };
// LRU implementation: use tail of MRU queue (prefetch_mru_q[2]).
u32 prefetch_mru_q[3] = { 0, 1, 2};
// Use tracing mode.
bool doTrace = 0;
bool useCSVoutput = 0;
// Trace or differential counters: 1 if in progress.
bool tracingActive = 0;
// Log all events even if not tracing the execution in any way: 1 if enabled.
bool logAllEvents = 0;
bool takenBranch = 0;
ADDRESS_LIST addressReadBeforeWriteList = {0, NULL};
ADDRESS_LIST addressWriteBeforeReadList = {0, NULL};
ADDRESS_LIST addressConflictList = {0, NULL};
int addressConflicts = 0;
int addressConflictsStack = 0;
int addressWrites = 0;
int addressReads = 0;
// Reserve a space inside the simulator for variables that GDB and python can use to control the simulator
// Essentially creates a new block of addresses on the bus of the processor that only the debug read and write commands can access
//MEMMAPIO mmio = {.cycleCountLSB = &cycleCount, .cycleCountMSB = &cycleCount+4,
// .cyclesSince = &cyclesSinceReset, .resetAfter = &resetAfterCycles};
u32* mmio[] = {&cycleCount, ((u32*)&cycleCount)+1, &wastedCycles, ((u32*)&wastedCycles)+1,
&cyclesSinceReset, &cyclesSinceCP, &addrOfCP, &addrOfRestoreCP,
&resetAfterCycles, &do_reset, &PRINT_STATE_DIFF, &wdt_seed,
&wdt_val, &(md5[0]), &(md5[1]), &(md5[2]),
&(md5[3]), &(md5[4])};
void printOpcodeCounts(u64 subcode_stats[], u32 count)
{
u32 i;
fprintf(stderr, ", [");
for (i = 0; i < count - 1; i++)
fprintf(stderr, "%ld, ", subcode_stats[i]);
fprintf(stderr, "%ld]\n", subcode_stats[i]);
}
void printOpcodeCountDeltas(u64 subcode_stats[], u64 last_subcode_stats[], u32 count)
{
u32 i;
fprintf(stderr, ", [");
for (i = 0; i < count - 1; i++)
fprintf(stderr, "%ld, ", subcode_stats[i] - last_subcode_stats[i]);
fprintf(stderr, "%ld]\n", subcode_stats[i], last_subcode_stats[i]);
}
void saveStats(void)
{
u32 i;
last_insnCount = insnCount;
last_ram_data_reads = ram_data_reads;
last_ram_insn_reads = ram_insn_reads;
last_ram_writes = ram_writes;
last_flash_data_reads = flash_data_reads;
last_flash_insn_reads =flash_insn_reads;
last_flash_writes = flash_writes;
last_taken_branches = taken_branches;
last_nonword_branch_destinations = nonword_branch_destinations;
last_ram_insn_prefetch_hits = ram_insn_prefetch_hits;
last_flash_insn_prefetch_hits = flash_insn_prefetch_hits;
last_nonword_taken_branches = nonword_taken_branches;
last_branch_fetch_stalls = branch_fetch_stalls;
last_arbitration_conflicts = arbitration_conflicts;
last_load_after_load = load_after_load;
last_store_after_load = store_after_load;
last_load_after_store = load_after_store;
last_store_after_store = store_after_store;
last_use_after_load_ld = use_after_load_ld;
last_use_after_load_st = use_after_load_st;
last_use_after_load_alu = use_after_load_alu;
last_use_after_load_cmp = use_after_load_cmp;
last_burst_loads = burst_loads;
last_burst_stores = burst_stores;
last_bl_insns = bl_insns;
last_blx_insns = blx_insns;
last_bx_insns = bx_insns;
last_pop_high_regs = pop_high_regs;
last_pop_sp = pop_sp;
last_pop_pc = pop_pc;
last_word_aligned_bl = word_aligned_bl;
memcpy(last_primary_opcode_stats, primary_opcode_stats, sizeof (primary_opcode_stats));
memcpy(last_opcode_stats, opcode_stats, sizeof(opcode_stats));
}
// Print execution statistics.
void printStats(void)
{
u32 i;
#if MEM_COUNT_INST
fprintf(stderr, "Loads: %u\nStores: %u\nCheckpoints: %u\n", load_count, store_count, cp_count);
#endif
fprintf(stderr, "Executed insns: %12ld\n", insnCount);
fprintf(stderr, "RAM data reads: %12ld\n", ram_data_reads);
fprintf(stderr, "RAM insn reads: %12ld\n", ram_insn_reads);
fprintf(stderr, "RAM writes: %12ld\n", ram_writes);
fprintf(stderr, "Flash data reads: %12ld\n", flash_data_reads);
fprintf(stderr, "Flash insn reads: %12ld\n", flash_insn_reads);
fprintf(stderr, "Flash writes: %12ld\n", flash_writes);
fprintf(stderr, "Taken branches: %12ld\n", taken_branches);
fprintf(stderr, "Nonword branch dsts: %12ld\n", nonword_branch_destinations);
fprintf(stderr, "Nonword taken branch:%12ld\n", nonword_taken_branches);
fprintf(stderr, "RAM prefetch hits: %12ld\n", ram_insn_prefetch_hits);
fprintf(stderr, "Flash prefetch hits: %12ld\n", flash_insn_prefetch_hits);
fprintf(stderr, "Branch fetch stalls: %12ld\n", branch_fetch_stalls);
fprintf(stderr, "Arbitration clashes: %12ld\n", arbitration_conflicts);
fprintf(stderr, "Load-after-load: %12ld\n", load_after_load);
fprintf(stderr, "Load-after-store: %12ld\n", load_after_store);
fprintf(stderr, "Store-after-load: %12ld\n", store_after_load);
fprintf(stderr, "Store-after-store: %12ld\n", store_after_store);
fprintf(stderr, "Use-after-load-LD: %12ld\n", use_after_load_ld);
fprintf(stderr, "Use-after-load-ST: %12ld\n", use_after_load_st);
fprintf(stderr, "Use-after-load-ALU: %12ld\n", use_after_load_alu);
fprintf(stderr, "Use-after-load-CMP: %12ld\n", use_after_load_cmp);
fprintf(stderr, "Burst loads: %12ld\n", burst_loads);
fprintf(stderr, "Burst stores: %12ld\n", burst_stores);
fprintf(stderr, "BL insns: %12ld\n", bl_insns);
fprintf(stderr, "BL word-aligned: %12ld\n", word_aligned_bl);
fprintf(stderr, "BLX insns: %12ld\n", blx_insns);
fprintf(stderr, "BX insns: %12ld\n", bx_insns);
fprintf(stderr, "PUSH/POP high regs: %12ld\n", pop_high_regs);
fprintf(stderr, "PUSH/POP SP: %12ld\n", pop_sp);
fprintf(stderr, "PUSH/POP PC/LR: %12ld\n", pop_pc);
fprintf(stderr, "Opcode statistics:\n");
for (i = 0; i < 64; i++)
{
fprintf(stderr, "%2d: %9ld", i, primary_opcode_stats[i]);
printOpcodeCounts(opcode_stats[i], 16);
}
}
void printStatsDelta(void)
{
u32 i;
#if MEM_COUNT_INST
fprintf(stderr, "Loads: %u\nStores: %u\nCheckpoints: %u\n", load_count, store_count, cp_count);
#endif
fprintf(stderr, "Executed insns: %12ld\n", insnCount - last_insnCount);
fprintf(stderr, "RAM data reads: %12ld\n", ram_data_reads - last_ram_data_reads);
fprintf(stderr, "RAM insn reads: %12ld\n", ram_insn_reads - last_ram_insn_reads);
fprintf(stderr, "RAM writes: %12ld\n", ram_writes - last_ram_writes);
fprintf(stderr, "Flash data reads: %12ld\n", flash_data_reads - last_flash_data_reads);
fprintf(stderr, "Flash insn reads: %12ld\n", flash_insn_reads - last_flash_insn_reads);
fprintf(stderr, "Flash writes: %12ld\n", flash_writes - last_flash_writes);
fprintf(stderr, "Taken branches: %12ld\n", taken_branches - last_taken_branches);
fprintf(stderr, "Nonword branch dsts: %12ld\n", nonword_branch_destinations - last_nonword_branch_destinations);
fprintf(stderr, "Nonword taken branch:%12ld\n", nonword_taken_branches - last_nonword_taken_branches);
fprintf(stderr, "RAM prefetch hits: %12ld\n", ram_insn_prefetch_hits - last_ram_insn_prefetch_hits);
fprintf(stderr, "Flash prefetch hits: %12ld\n", flash_insn_prefetch_hits - last_flash_insn_prefetch_hits);
fprintf(stderr, "Branch fetch stalls: %12ld\n", branch_fetch_stalls - last_branch_fetch_stalls);
fprintf(stderr, "Arbitration clashes: %12ld\n", arbitration_conflicts - last_arbitration_conflicts);
fprintf(stderr, "Load-after-load: %12ld\n", load_after_load - last_load_after_load);
fprintf(stderr, "Load-after-store: %12ld\n", load_after_store - last_load_after_store);
fprintf(stderr, "Store-after-load: %12ld\n", store_after_load - last_store_after_load);
fprintf(stderr, "Store-after-store: %12ld\n", store_after_store - last_store_after_store);
fprintf(stderr, "Use-after-load-LD: %12ld\n", use_after_load_ld - last_use_after_load_ld);
fprintf(stderr, "Use-after-load-ST: %12ld\n", use_after_load_st - last_use_after_load_st);
fprintf(stderr, "Use-after-load-ALU: %12ld\n", use_after_load_alu - last_use_after_load_alu);
fprintf(stderr, "Use-after-load-CMP: %12ld\n", use_after_load_cmp - last_use_after_load_cmp);
fprintf(stderr, "Burst loads: %12ld\n", burst_loads - last_burst_loads);
fprintf(stderr, "Burst stores: %12ld\n", burst_stores - last_burst_stores);
fprintf(stderr, "BL insns: %12ld\n", bl_insns - last_bl_insns);
fprintf(stderr, "BL word-aligned: %12ld\n", word_aligned_bl - last_word_aligned_bl);
fprintf(stderr, "BLX insns: %12ld\n", blx_insns - last_blx_insns);
fprintf(stderr, "BX insns: %12ld\n", bx_insns - last_bx_insns);
fprintf(stderr, "PUSH/POP high regs: %12ld\n", pop_high_regs - last_pop_high_regs);
fprintf(stderr, "PUSH/POP SP: %12ld\n", pop_sp - last_pop_sp);
fprintf(stderr, "PUSH/POP PC/LR: %12ld\n", pop_pc - last_pop_pc);
fprintf(stderr, "Opcode statistics:\n");
for (i = 0; i < 64; i++)
{
fprintf(stderr, "%2d: %9ld", i, primary_opcode_stats[i] - last_primary_opcode_stats[i]);
printOpcodeCountDeltas(opcode_stats[i], last_opcode_stats[i], 16);
}
}
void printOpcodeCountsCSV(FILE *f, u64 subcode_stats[], u32 count)
{
u32 i;
for (i = 0; i < count - 1; i++)
fprintf(f, "%ld, ", subcode_stats[i]);
fprintf(f, "%ld\n", subcode_stats[i]);
}
// Print execution statistics.
void printStatsCSV(void)
{
u32 i;
char filename[256];
char overallStatsCsv[500];
static int statsReportCounter = 0;
char *str1 = "simulationStats";
char *str2 = ".csv\0";
sprintf(filename,"%s%d%s", str1, statsReportCounter++, str2);
sprintf(overallStatsCsv, "%s%s%s", simulatingFilePath, "counters", ".csv");
FILE *f = fopen(filename, "w");
FILE *f1 = fopen(overallStatsCsv, "a");
if (f==NULL || f1==NULL){
fprintf(stderr, "File for writing stats can't be opened\n");
exit(-1);
}
#if MEM_COUNT_INST
fprintf(stderr, "Loads: %u\nStores: %u\nCheckpoints: %u\n", load_count, store_count, cp_count);
#endif
if (statsReportCounter == 1)
fprintf(f1, "RAM_data_reads, RAM_insn_reads, RAM_writes, Flash_data_reads, Flash_insn_reads, Flash_writes,"
" Taken_branches, Nonword_branch_targets, Nonword taken branches, RAM prefetch hits, Flash prefetch hits, Branch_fetch_stalls,"
" Arbitration_conflicts, Load after load, Load after store, Store after load, Store after store,"
" Use after load in LD, Use after load in ST, Use after load in ALU, Use after load CMP,"
" Burst loads, Burst stores, BL insns, BL word aligned, BLX insns, BX insns,"
" PUSH/POP high regs, PUSH/POP SP, PUSH/POP PC/LR\n");
fprintf(f1, "%12ld, %12ld, %12ld, %12ld, %12ld, %12ld,"
" %12ld, %12ld, %12ld, %12ld, %12ld, %12ld,"
" %12ld, %12ld, %12ld, %12ld, %12ld,"
" %12ld, %12ld, %12ld, %12ld,"
" %12ld, %12ld, %12ld, %12ld, %12ld, %12ld,"
" %12ld, %12ld, %12ld\n",
ram_data_reads, ram_insn_reads, ram_writes, flash_data_reads, flash_insn_reads, flash_writes,
taken_branches, nonword_branch_destinations, nonword_taken_branches, ram_insn_prefetch_hits, flash_insn_prefetch_hits, branch_fetch_stalls,
arbitration_conflicts, load_after_load, load_after_store, store_after_load, store_after_store,
use_after_load_ld, use_after_load_st, use_after_load_alu, use_after_load_cmp,
burst_loads, burst_stores, bl_insns, word_aligned_bl, blx_insns, bx_insns,
pop_high_regs, pop_sp, pop_pc);
fprintf(f, "Opcode, total_count, var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12, var13, var14, var15, var16\n");
for (i = 0; i < 64; i++)
{
fprintf(f, "%2d, %9ld,", i, primary_opcode_stats[i]);
printOpcodeCountsCSV(f, opcode_stats[i], 16);
}
fclose(f);
fclose(f1);
}
// Reset CPU state in accordance with B1.5.5 and B3.2.2
void cpu_reset(void)
{
// Initialize the special-purpose registers
cpu.apsr = 0; // No flags set
cpu.ipsr = 0; // No exception number
cpu.espr = ESPR_T; // Thumb mode
cpu.primask = 0; // No except priority boosting
cpu.control = 0; // Priv mode and main stack
cpu.sp_main = 0; // Stack pointer for exception handling
cpu.sp_process = 0; // Stack pointer for process
// Clear the general purpose registers
memset(cpu.gpr, 0, sizeof(cpu.gpr));
// Set the reserved GPRs
cpu.gpr[GPR_LR] = 0;
// May need to add logic to send writes and reads to the
// correct stack pointer
// Set the stack pointers
simLoadData(0, &cpu.sp_main);
cpu.sp_main &= 0xFFFFFFFC;
cpu.sp_process = 0;
cpu_set_sp(cpu.sp_main);
// Set the program counter to the address of the reset exception vector
u32 startAddr;
simLoadData(0x4, &startAddr);
cpu_set_pc(startAddr);
// No pending exceptions
cpu.exceptmask = 0;
// Check for attempts to go to ARM mode
if((cpu_get_pc() & 0x1) == 0)
{
printf("Error: Reset PC to an ARM address 0x%08X\n", cpu_get_pc());
sim_exit(1);
}
// Reset the systick unit
systick.control = 0x4;
systick.reload = 0x0;
systick.value = 0x0;
systick.calib = CPU_FREQ/100 | 0x80000000;
// Reset counters
wastedCycles += cyclesSinceCP;
cyclesSinceReset = 0;
cyclesSinceCP = 0;
wdt_val = 0;
}
void sim_command(void)
{
// Reset the CPU
if(do_reset != 0)
{
cpu_reset();
cpu_set_pc(cpu_get_pc() + 0x4);
do_reset = 0;
}
// Compute MD5 of memory
#if MD5
if(md5[0] != 0)
{
printf("Computing MD5!");
MD5_CTX c;
unsigned char digest[16];
int length;
char *ptr;
MD5_Init(&c);
// Do RAM first
length = RAM_SIZE-1;
ptr = (char*) ram;
while (length > 0) {
if (length > 512) {
MD5_Update(&c, ptr, 512);
} else {
MD5_Update(&c, ptr, length);
}
length -= 512;
ptr += 512;
}
// Do flash next
length = FLASH_SIZE-1;
ptr = (char*) flash;
while (length > 0) {
if (length > 512) {
MD5_Update(&c, ptr, 512);
} else {
MD5_Update(&c, ptr, length);
}
length -= 512;
ptr += 512;
}
//// Now low registers
//length = 8*4;
//ptr = (char*) cpu.gpr;
//MD5_Update(&c, ptr, length);
// PC, SP, LR
length = 3*4;
ptr = (char*) &(cpu.gpr[13]);
MD5_Update(&c, ptr, length);
MD5_Final((unsigned char*) &(md5[1]), &c);
md5[0]=0;
}
#endif
}
#if HOOK_GPR_ACCESSES
void do_nothing(void){;}
void report_sp(void)
{
#if 0 // This is a hard-coded value, applicable only to specific targets...
if(cpu_get_sp() < 0X40010000)
{
fprintf(stderr, "SP crosses heap: 0x%8.8X\n", cpu_get_sp());
fprintf(stderr, "PC: 0x%8.8X\n", cpu_get_pc());
}
#endif
}
void (* gprReadHooks[16])(void) = { \
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing\
};
void (* gprWriteHooks[16])(void) = { \
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
do_nothing,\
report_sp,\
do_nothing\
};
#endif
// Returns 1 if the passed list of addresses contains the passed
// address, returns 0 otherwise
char containsAddress(const ADDRESS_LIST *pList, const u32 pAddress)
{
if(pList->address == pAddress)
return 1;
if(pList->next == NULL)
return 0;
// Tail recursion FTW
return containsAddress(pList->next, pAddress);
}
// Adds the passed address to the end of the list
// Returns 0 if already present, 1 if added to end
char addAddress(const ADDRESS_LIST *pList, const u32 pAddress)
{
ADDRESS_LIST *temp = pList;
ADDRESS_LIST *temp_prev;
// Go to the end of the list
do
{
if(temp->address == pAddress)
return 0;
temp_prev = temp;
temp = temp->next;
} while(temp != NULL);
// Create a new entry and link to it
temp_prev->next = malloc(sizeof(ADDRESS_LIST));
temp_prev->next->address = pAddress;
temp_prev->next->next = NULL;
return 1;
}
// Clears the list
void clearList(ADDRESS_LIST *pList)
{
if(pList->next == NULL)
return;
clearList(pList->next);
free(pList->next);
pList->next = NULL;
}
// Called by branch and links
int insnsPerConflict = 0;
void reportAndReset(char pNumRegsPushed)
{
if(!REPORT_IDEM_BREAKS)
return;
insnsPerConflict = cycleCount - insnsPerConflict;
fprintf(stderr, "%d,%d,%d,%d,%d,%d,%d\n", addressWrites, addressReads, addressConflicts, addressConflicts - addressConflictsStack, insnsPerConflict, pNumRegsPushed, cpu_get_pc());
addressConflicts = 0;
addressConflictsStack = 0;
addressWrites = 0;
addressReads = 0;
insnsPerConflict = cycleCount;
clearList(&addressConflictList);
clearList(&addressReadBeforeWriteList);
clearList(&addressWriteBeforeReadList);
}
// MRU/LRU queue management: MRU always at index 0.
void prefetch_set_mru(u32 index)
{
int i;
if (prefetch_mru_q[0] == index)
// If index is already MRU, there's nothing to do.
return;
if (prefetch_mru_q[1] == index)
{
// Index in position 1: swap values at 0 and 1.
prefetch_mru_q[1] = prefetch_mru_q[0];
prefetch_mru_q[0] = index;
}
if (prefetch_mru_q[2] == index)
{
// Index in position 2: rotate the buffer to the right.
prefetch_mru_q[2] = prefetch_mru_q[1];
prefetch_mru_q[1] = prefetch_mru_q[0];
prefetch_mru_q[0] = index;
}
}
// Memory access functions assume that RAM has a higher address than Flash
char simLoadInsn(u32 address, u16 *value)
{
u32 fromMem;
if(address >= RAM_START)
{
if(address >= (RAM_START + RAM_SIZE))
{
fprintf(stderr, "Error: ILR Memory access out of range: 0x%8.8X, pc=%x\n", address, cpu_get_pc());
sim_exit(1);
}
if(REPORT_IDEM_BREAKS)
{
// Add addresses to the read list if they weren't written to first
if(!containsAddress(&addressWriteBeforeReadList, address))
addressReads += addAddress(&addressReadBeforeWriteList, address);
}
// TODO: Add support of RAM word buffer.
if (prefetch_mode == PREFETCH_MODE_WORD || prefetch_mode == PREFETCH_MODE_BUFFER)
{
if ((address & ~0x3) == last_fetched_address)
{
// Request address corresponds to the last fetched word.
fromMem = last_fetched_word;
if (tracingActive && logAllEvents)
ram_insn_prefetch_hits++;
}
else
{
// Request address does not match the last fetched word.
if (tracingActive && logAllEvents)
{
ram_insn_reads++;
ram_access = 1;
if (data_access_in_cur_cycle)
arbitration_conflicts++;
}
fromMem = ram[(address & RAM_ADDRESS_MASK) >> 2];
last_fetched_address = address & ~0x3;
last_fetched_word = fromMem;
}
}
else // Neither PREFETCH_MODE_WORD nor PREFETCH_MODE_BUFFER
{
// Original behavior: Plain load from RAM on every instruction.
fromMem = ram[(address & RAM_ADDRESS_MASK) >> 2];
if (tracingActive && logAllEvents)
{
ram_insn_reads++;
ram_access = 1;
if (data_access_in_cur_cycle)
arbitration_conflicts++;
}
}
}
else // Not a RAM access, so it should be inside Flash address range.
{
if(address >= (FLASH_START + FLASH_SIZE))
{
fprintf(stderr, "Error: ILF Memory access out of range: 0x%8.8X, pc=%x\n", address, cpu_get_pc());
sim_exit(1);
}
if (prefetch_mode == PREFETCH_MODE_WORD)
{
if ((address & ~0x3) == last_fetched_address)
{
// Request address corresponds to the last fetched word.
fromMem = last_fetched_word;
if (tracingActive && logAllEvents)
flash_insn_prefetch_hits++;
}
else
{
// Request address does not match the last fetched word.
if (tracingActive && logAllEvents)
{
flash_insn_reads++;
flash_access = 1;
if (data_access_in_cur_cycle)
arbitration_conflicts++;
}
fromMem = flash[(address & FLASH_ADDRESS_MASK) >> 2];
last_fetched_address = address & ~0x3;
last_fetched_word = fromMem;
}
}
else if (prefetch_mode == PREFETCH_MODE_BUFFER)
{
int i;
bool is_a_hit = 0;
// Compare address against the content of tags.
for (i = 0 ; i < 3; i++)
{
if ((address & ~0x3) == prefetch_addresses[i])
{
// Address was found in tags: use the corresponding PF buffer word
// update the MRU queue and the hit counter.
fromMem = prefetch_words[i];
prefetch_set_mru(i);
is_a_hit = 1;
if (tracingActive && logAllEvents)
flash_insn_prefetch_hits++;
break;
}
}
// If word is not in PF, we need to fetch it.
if (!is_a_hit)
{
// Evict the LRU entry from the PF buffer.
u32 victim = prefetch_mru_q[2];
prefetch_set_mru(victim);
fromMem = flash[(address & FLASH_ADDRESS_MASK) >> 2];
prefetch_words[victim] = fromMem;
prefetch_addresses[victim] = address & ~0x3;
if (tracingActive && logAllEvents)
{
flash_insn_reads++;
flash_access = 1;
if (data_access_in_cur_cycle)
arbitration_conflicts++;
}
}
}
else // Neither PREFETCH_MODE_WORD nor PREFETCH_MODE_BUFFER
{
// Original behavior: Plain load from flash on every instruction.
fromMem = flash[(address & FLASH_ADDRESS_MASK) >> 2];
if (tracingActive && logAllEvents)
{
flash_insn_reads++;
flash_access = 1;
if (data_access_in_cur_cycle)
arbitration_conflicts++;
}
}
}
// Data 32-bits, but instruction 16-bits
*value = ((address & 0x2) != 0) ? (u16)(fromMem >> 16) : (u16)fromMem;
return 0;
}
// Normal interface for a program to load data from memory
// Increments data load counter
char simLoadData(u32 address, u32 *value)
{
#if MEM_COUNT_INST
++load_count;
#endif
return simLoadData_internal(address, value, 0);
}
char simLoadData_internal(u32 address, u32 *value, u32 falseRead)
{
#if MEM_CHECKS
if((address & 0x3) != 0)
{
fprintf(stderr, "Unalinged data memory read: 0x%8.8X\n", address);
sim_exit(1);
}
#endif
if(address >= RAM_START)
{
if(address >= (RAM_START + RAM_SIZE))
{
// Check for UART
if(address == 0xE0000000)
{
*value = 0;
return 0;
}
// Check for systick
if((address >> 4) == 0xE000E01)
{
*value = ((u32 *)&systick)[(address >> 2) & 0x3];
if(address == 0xE000E010)
systick.control &= 0x00010000;
return 0;
}
// Check for general GPIO.
if (address >= MEMMAPIO_START && address < MEMMAPIO_START + MEMMAPIO_SIZE)
{
fprintf(stderr, "WARNING: arbitrary GPIO read: 0x%8.8X, pc=%x, returning 0...\n", address, cpu_get_pc());
*value = 0;
return 0;
}
// Check for general Cortex-M0+ peripheral access.
if (address >= M0PLUSPERIPHS_START && address < M0PLUSPERIPHS_START + M0PLUSPERIPHS_SIZE)
{
fprintf(stderr, "WARNING: arbitrary Cortex-M0+ peripherals read: 0x%8.8X, pc=%x, returning 0...\n", address, cpu_get_pc());
*value = 0;
return 0;
}
fprintf(stderr, "Error: LoadData_internal DLR Memory access out of range: 0x%8.8X, pc=%x\n", address, cpu_get_pc());
sim_exit(1);
}
// Implicitly (but confirmed by flow analysis) at this point we have 'address < (RAM_START + RAM_SIZE)'.
// Add addresses to the read list if they weren't written to first
if(REPORT_IDEM_BREAKS && !falseRead)
{
if(!containsAddress(&addressWriteBeforeReadList, address))
addressReads += addAddress(&addressReadBeforeWriteList, address);
}
if ((tracingActive && logAllEvents) && !falseRead)
{
ram_data_reads++;
ram_access = 1;
data_access_in_three_cycles = 1;
}
*value = ram[(address & RAM_ADDRESS_MASK) >> 2];
#if PRINT_MEM_OPS
if(!falseRead)
printf("%llu\t%llu\tR\t%8.8X\t%d\n", cycleCount, insnCount, address, *value);
#endif
#if PRINT_ALL_MEM
if(!falseRead)
fprintf(stderr, "%8.8X: Ram read at 0x%8.8X=0x%8.8X\n", cpu_get_pc()-4, address, *value);
#endif
}
else
{
// Here we assume FLASH_START < RAM_START...
if(address >= (FLASH_START + FLASH_SIZE))
{
fprintf(stderr, "Error: DLF Memory access out of range: 0x%8.8X, pc=%x\n", address, cpu_get_pc());
sim_exit(1);
}
if ((tracingActive && logAllEvents) && !falseRead)
{
flash_data_reads++;
flash_access = 1;
data_access_in_three_cycles = 1;
}
*value = flash[(address & FLASH_ADDRESS_MASK) >> 2];
#if PRINT_MEM_OPS
if(!falseRead)
printf("%llu\t%llu\tR\t%8.8X\t%d\n", cycleCount, insnCount, address, *value);
#endif
#if PRINT_ALL_MEM
if(!falseRead)
fprintf(stderr, "%8.8X: Flash read at 0x%8.8X=0x%8.8X\n", cpu_get_pc()-4, address, *value);
#endif
}
return 0;
}
char simStoreData(u32 address, u32 value)
{
unsigned int word;
#if MEM_CHECKS
if((address & 0x3) != 0) // Thumb-mode requires LSB = 1
{
fprintf(stderr, "Unalinged data memory write: 0x%8.8X, pc=%x\n", address, cpu_get_pc());
sim_exit(1);
}
#endif
if(address >= RAM_START)
{
if(address >= (RAM_START + RAM_SIZE))
{
// Check for UART
if(address == 0xE0000000)
{
#if !DISABLE_PROGRAM_PRINTING
printf("%c", value & 0xFF);
fflush(stdout);
#endif
return 0;
}
// Check for systick
if((address >> 4) == 0xE000E01 && address != 0xE000E01C)
{
if(address == 0xE000E010)
{
systick.control = (value & 0x1FFFD) | 0x4; // No external tick source, no interrupt
if(value & 0x2)
fprintf(stderr, "ERROR: SYSTICK interrupts not implemented...ignoring\n");
}
else if(address == 0xE000E014)
systick.reload = value & 0xFFFFFF;
else if(address == 0xE000E018)
systick.value = 0; // Reads clears current value
return 0;
}
// Check for cycle count
if(address >= MEMMAPIO_START && address <= (MEMMAPIO_START + MEMMAPIO_MAPPEDSIZE))
{
word = *(mmio[((address & 0xfffffffc)-MEMMAPIO_START >> 2)]);
word &= ~(0xff << (8*(address%4)));
word |= (value << (8*(address%4)));
*(mmio[((address & 0xfffffffc)-MEMMAPIO_START >> 2)]) = word;
// If the variable updated is a request to reset, then do it
if(do_reset != 0)
{
cpu_reset();
cpu_set_pc(cpu_get_pc() + 0x4);
do_reset = 0;
}
return 0;
}
// Shortcuts for GPIO control register offsets
#define GPIOB_BSRR 0x08000418
#define GPIOB_BRR 0x08000428
#define GPIOC_BSRR 0x08000818
#define GPIOC_BRR 0x08000828
if(address >= MEMMAPIO_START && address <= (MEMMAPIO_START + MEMMAPIO_SIZE))
{
// TeamPlay specific: Raising/clearing GPIOB[0]/GPIOC[0] starts/stops event counting
// and produces a cycle and insn count message on console.
// On CameraPill, the trigger pin is GPIOB[0].
// On F0-Discovery and Nucleo-F0 boards the trigger pin is GPIOC[0].
if ((value == 0x1 && address == (MEMMAPIO_START + GPIOB_BSRR))
|| (value == 0x1 && address == (MEMMAPIO_START + GPIOC_BSRR)))
{
// Write 0x1 to GPIOB_BSRR[0] (resp. GPIOC_BSRR[0]): Set the GPIOB[0] (resp. GPIOC[0]) pin.
fprintf(stderr, "TeamPlay: trigger pin raised at cycle %lld, insn count %lld, pc = %x\n", cycleCount, insnCount, cpu_get_pc());
// Start the logging of events.
if (logAllEvents)
{
tracingActive = 1;
if (useCSVoutput)
printStatsCSV();
else
saveStats();
}
return 0;
}
else if ((value == 0x1
&& (address == (MEMMAPIO_START + GPIOB_BRR)
|| address == (MEMMAPIO_START + GPIOC_BRR)))
|| (value == 0x00010000
&& (address == (MEMMAPIO_START + GPIOB_BSRR))
|| address == (MEMMAPIO_START + GPIOC_BSRR)))
{
// Write 0x1 to GPIOB_BRR[0]/GPIOC or 0x1 to GPIOB_BSRR[0]/GPIOC_BSRR[16]: clear the GPIOB[0]/GPIOC[0] pin.
fprintf(stderr, "TeamPlay: trigger pin cleared at cycle %lld, insn count %lld, pc = %x\n", cycleCount, insnCount, cpu_get_pc());
if (tracingActive && logAllEvents)
{
// Stop the logging of events.
tracingActive = 0;
if (useCSVoutput)
printStatsCSV();
else
// Print differential statistics.
printStatsDelta();
}
else if (logAllEvents)
printStats();
return 0;
}
fprintf(stderr, "WARNING: Writing to MMIO space: 0x%08x@0x%8.8X, pc=%x, operation IGNORED\n", value, address, cpu_get_pc());
return 0;
}
// Check for general Cortex-M0+ peripheral access.
if (address >= M0PLUSPERIPHS_START && address < M0PLUSPERIPHS_START + M0PLUSPERIPHS_SIZE)
{