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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
|
#include <signal.h>
#include "sysdep.h"
#include "bfd.h"
#include "callback.h"
#include "remote-sim.h"
#include "d10v_sim.h"
#include "sim-d10v.h"
enum _leftright { LEFT_FIRST, RIGHT_FIRST };
static char *myname;
static SIM_OPEN_KIND sim_kind;
int d10v_debug;
/* Set this to true to get the previous segment layout. */
int old_segment_mapping;
host_callback *d10v_callback;
unsigned long ins_type_counters[ (int)INS_MAX ];
uint16 OP[4];
static int init_text_p = 0;
/* non-zero if we opened prog_bfd */
static int prog_bfd_was_opened_p;
bfd *prog_bfd;
asection *text;
bfd_vma text_start;
bfd_vma text_end;
static long hash PARAMS ((long insn, int format));
static struct hash_entry *lookup_hash PARAMS ((uint32 ins, int size));
static void get_operands PARAMS ((struct simops *s, uint32 ins));
static void do_long PARAMS ((uint32 ins));
static void do_2_short PARAMS ((uint16 ins1, uint16 ins2, enum _leftright leftright));
static void do_parallel PARAMS ((uint16 ins1, uint16 ins2));
static char *add_commas PARAMS ((char *buf, int sizeof_buf, unsigned long value));
extern void sim_set_profile PARAMS ((int n));
extern void sim_set_profile_size PARAMS ((int n));
static INLINE uint8 *map_memory (unsigned phys_addr);
#ifdef NEED_UI_LOOP_HOOK
/* How often to run the ui_loop update, when in use */
#define UI_LOOP_POLL_INTERVAL 0x14000
/* Counter for the ui_loop_hook update */
static long ui_loop_hook_counter = UI_LOOP_POLL_INTERVAL;
/* Actual hook to call to run through gdb's gui event loop */
extern int (*ui_loop_hook) PARAMS ((int signo));
#endif /* NEED_UI_LOOP_HOOK */
#ifndef INLINE
#if defined(__GNUC__) && defined(__OPTIMIZE__)
#define INLINE __inline__
#else
#define INLINE
#endif
#endif
#define MAX_HASH 63
struct hash_entry
{
struct hash_entry *next;
uint32 opcode;
uint32 mask;
int size;
struct simops *ops;
};
struct hash_entry hash_table[MAX_HASH+1];
INLINE static long
hash(insn, format)
long insn;
int format;
{
if (format & LONG_OPCODE)
return ((insn & 0x3F000000) >> 24);
else
return((insn & 0x7E00) >> 9);
}
INLINE static struct hash_entry *
lookup_hash (ins, size)
uint32 ins;
int size;
{
struct hash_entry *h;
if (size)
h = &hash_table[(ins & 0x3F000000) >> 24];
else
h = &hash_table[(ins & 0x7E00) >> 9];
while ((ins & h->mask) != h->opcode || h->size != size)
{
if (h->next == NULL)
{
(*d10v_callback->printf_filtered)
(d10v_callback, "ERROR: Illegal instruction %x at PC %x\n", ins, PC);
State.exception = SIGILL;
return NULL;
}
h = h->next;
}
return (h);
}
INLINE static void
get_operands (struct simops *s, uint32 ins)
{
int i, shift, bits, flags;
uint32 mask;
for (i=0; i < s->numops; i++)
{
shift = s->operands[3*i];
bits = s->operands[3*i+1];
flags = s->operands[3*i+2];
mask = 0x7FFFFFFF >> (31 - bits);
OP[i] = (ins >> shift) & mask;
}
/* FIXME: for tracing, update values that need to be updated each
instruction decode cycle */
State.trace.psw = PSW;
}
bfd_vma
decode_pc ()
{
asection *s;
if (!init_text_p && prog_bfd != NULL)
{
init_text_p = 1;
for (s = prog_bfd->sections; s; s = s->next)
if (strcmp (bfd_get_section_name (prog_bfd, s), ".text") == 0)
{
text = s;
text_start = bfd_get_section_vma (prog_bfd, s);
text_end = text_start + bfd_section_size (prog_bfd, s);
break;
}
}
return (PC << 2) + text_start;
}
static void
do_long (ins)
uint32 ins;
{
struct hash_entry *h;
#ifdef DEBUG
if ((d10v_debug & DEBUG_INSTRUCTION) != 0)
(*d10v_callback->printf_filtered) (d10v_callback, "do_long 0x%x\n", ins);
#endif
h = lookup_hash (ins, 1);
if (h == NULL)
return;
get_operands (h->ops, ins);
State.ins_type = INS_LONG;
ins_type_counters[ (int)State.ins_type ]++;
(h->ops->func)();
}
static void
do_2_short (ins1, ins2, leftright)
uint16 ins1, ins2;
enum _leftright leftright;
{
struct hash_entry *h;
enum _ins_type first, second;
#ifdef DEBUG
if ((d10v_debug & DEBUG_INSTRUCTION) != 0)
(*d10v_callback->printf_filtered) (d10v_callback, "do_2_short 0x%x (%s) -> 0x%x\n",
ins1, (leftright) ? "left" : "right", ins2);
#endif
if (leftright == LEFT_FIRST)
{
first = INS_LEFT;
second = INS_RIGHT;
ins_type_counters[ (int)INS_LEFTRIGHT ]++;
}
else
{
first = INS_RIGHT;
second = INS_LEFT;
ins_type_counters[ (int)INS_RIGHTLEFT ]++;
}
/* Issue the first instruction */
h = lookup_hash (ins1, 0);
if (h == NULL)
return;
get_operands (h->ops, ins1);
State.ins_type = first;
ins_type_counters[ (int)State.ins_type ]++;
(h->ops->func)();
/* Issue the second instruction (if the PC hasn't changed) */
if (!State.pc_changed && !State.exception)
{
/* finish any existing instructions */
SLOT_FLUSH ();
h = lookup_hash (ins2, 0);
if (h == NULL)
return;
get_operands (h->ops, ins2);
State.ins_type = second;
ins_type_counters[ (int)State.ins_type ]++;
ins_type_counters[ (int)INS_CYCLES ]++;
(h->ops->func)();
}
else if (!State.exception)
ins_type_counters[ (int)INS_COND_JUMP ]++;
}
static void
do_parallel (ins1, ins2)
uint16 ins1, ins2;
{
struct hash_entry *h1, *h2;
#ifdef DEBUG
if ((d10v_debug & DEBUG_INSTRUCTION) != 0)
(*d10v_callback->printf_filtered) (d10v_callback, "do_parallel 0x%x || 0x%x\n", ins1, ins2);
#endif
ins_type_counters[ (int)INS_PARALLEL ]++;
h1 = lookup_hash (ins1, 0);
if (h1 == NULL)
return;
h2 = lookup_hash (ins2, 0);
if (h2 == NULL)
return;
if (h1->ops->exec_type == PARONLY)
{
get_operands (h1->ops, ins1);
State.ins_type = INS_LEFT_COND_TEST;
ins_type_counters[ (int)State.ins_type ]++;
(h1->ops->func)();
if (State.exe)
{
ins_type_counters[ (int)INS_COND_TRUE ]++;
get_operands (h2->ops, ins2);
State.ins_type = INS_RIGHT_COND_EXE;
ins_type_counters[ (int)State.ins_type ]++;
(h2->ops->func)();
}
else
ins_type_counters[ (int)INS_COND_FALSE ]++;
}
else if (h2->ops->exec_type == PARONLY)
{
get_operands (h2->ops, ins2);
State.ins_type = INS_RIGHT_COND_TEST;
ins_type_counters[ (int)State.ins_type ]++;
(h2->ops->func)();
if (State.exe)
{
ins_type_counters[ (int)INS_COND_TRUE ]++;
get_operands (h1->ops, ins1);
State.ins_type = INS_LEFT_COND_EXE;
ins_type_counters[ (int)State.ins_type ]++;
(h1->ops->func)();
}
else
ins_type_counters[ (int)INS_COND_FALSE ]++;
}
else
{
get_operands (h1->ops, ins1);
State.ins_type = INS_LEFT_PARALLEL;
ins_type_counters[ (int)State.ins_type ]++;
(h1->ops->func)();
if (!State.exception)
{
get_operands (h2->ops, ins2);
State.ins_type = INS_RIGHT_PARALLEL;
ins_type_counters[ (int)State.ins_type ]++;
(h2->ops->func)();
}
}
}
static char *
add_commas(buf, sizeof_buf, value)
char *buf;
int sizeof_buf;
unsigned long value;
{
int comma = 3;
char *endbuf = buf + sizeof_buf - 1;
*--endbuf = '\0';
do {
if (comma-- == 0)
{
*--endbuf = ',';
comma = 2;
}
*--endbuf = (value % 10) + '0';
} while ((value /= 10) != 0);
return endbuf;
}
void
sim_size (power)
int power;
{
int i;
for (i = 0; i < IMEM_SEGMENTS; i++)
{
if (State.mem.insn[i])
free (State.mem.insn[i]);
}
for (i = 0; i < DMEM_SEGMENTS; i++)
{
if (State.mem.data[i])
free (State.mem.data[i]);
}
for (i = 0; i < UMEM_SEGMENTS; i++)
{
if (State.mem.unif[i])
free (State.mem.unif[i]);
}
/* Always allocate dmem segment 0. This contains the IMAP and DMAP
registers. */
State.mem.data[0] = calloc (1, SEGMENT_SIZE);
}
/* For tracing - leave info on last access around. */
static char *last_segname = "invalid";
static char *last_from = "invalid";
static char *last_to = "invalid";
enum
{
IMAP0_OFFSET = 0xff00,
DMAP0_OFFSET = 0xff08,
DMAP2_SHADDOW = 0xff04,
DMAP2_OFFSET = 0xff0c
};
static void
set_dmap_register (int reg_nr, unsigned long value)
{
uint8 *raw = map_memory (SIM_D10V_MEMORY_DATA
+ DMAP0_OFFSET + 2 * reg_nr);
WRITE_16 (raw, value);
#ifdef DEBUG
if ((d10v_debug & DEBUG_MEMORY))
{
(*d10v_callback->printf_filtered)
(d10v_callback, "mem: dmap%d=0x%04lx\n", reg_nr, value);
}
#endif
}
static unsigned long
dmap_register (int reg_nr)
{
uint8 *raw = map_memory (SIM_D10V_MEMORY_DATA
+ DMAP0_OFFSET + 2 * reg_nr);
return READ_16 (raw);
}
static void
set_imap_register (int reg_nr, unsigned long value)
{
uint8 *raw = map_memory (SIM_D10V_MEMORY_DATA
+ IMAP0_OFFSET + 2 * reg_nr);
WRITE_16 (raw, value);
#ifdef DEBUG
if ((d10v_debug & DEBUG_MEMORY))
{
(*d10v_callback->printf_filtered)
(d10v_callback, "mem: imap%d=0x%04lx\n", reg_nr, value);
}
#endif
}
static unsigned long
imap_register (int reg_nr)
{
uint8 *raw = map_memory (SIM_D10V_MEMORY_DATA
+ IMAP0_OFFSET + 2 * reg_nr);
return READ_16 (raw);
}
enum
{
HELD_SPI_IDX = 0,
HELD_SPU_IDX = 1
};
static unsigned long
spu_register (void)
{
if (PSW_SM)
return GPR (SP_IDX);
else
return HELD_SP (HELD_SPU_IDX);
}
static unsigned long
spi_register (void)
{
if (!PSW_SM)
return GPR (SP_IDX);
else
return HELD_SP (HELD_SPI_IDX);
}
static void
set_spi_register (unsigned long value)
{
if (!PSW_SM)
SET_GPR (SP_IDX, value);
SET_HELD_SP (HELD_SPI_IDX, value);
}
static void
set_spu_register (unsigned long value)
{
if (PSW_SM)
SET_GPR (SP_IDX, value);
SET_HELD_SP (HELD_SPU_IDX, value);
}
/* Given a virtual address in the DMAP address space, translate it
into a physical address. */
unsigned long
sim_d10v_translate_dmap_addr (unsigned long offset,
int nr_bytes,
unsigned long *phys,
unsigned long (*dmap_register) (int reg_nr))
{
short map;
int regno;
last_from = "logical-data";
if (offset >= DMAP_BLOCK_SIZE * SIM_D10V_NR_DMAP_REGS)
{
/* Logical address out side of data segments, not supported */
return 0;
}
regno = (offset / DMAP_BLOCK_SIZE);
offset = (offset % DMAP_BLOCK_SIZE);
if ((offset % DMAP_BLOCK_SIZE) + nr_bytes > DMAP_BLOCK_SIZE)
{
/* Don't cross a BLOCK boundary */
nr_bytes = DMAP_BLOCK_SIZE - (offset % DMAP_BLOCK_SIZE);
}
map = dmap_register (regno);
if (regno == 3)
{
/* Always maps to data memory */
int iospi = (offset / 0x1000) % 4;
int iosp = (map >> (4 * (3 - iospi))) % 0x10;
last_to = "io-space";
*phys = (SIM_D10V_MEMORY_DATA + (iosp * 0x10000) + 0xc000 + offset);
}
else
{
int sp = ((map & 0x3000) >> 12);
int segno = (map & 0x3ff);
switch (sp)
{
case 0: /* 00: Unified memory */
*phys = SIM_D10V_MEMORY_UNIFIED + (segno * DMAP_BLOCK_SIZE) + offset;
last_to = "unified";
break;
case 1: /* 01: Instruction Memory */
*phys = SIM_D10V_MEMORY_INSN + (segno * DMAP_BLOCK_SIZE) + offset;
last_to = "chip-insn";
break;
case 2: /* 10: Internal data memory */
*phys = SIM_D10V_MEMORY_DATA + (segno << 16) + (regno * DMAP_BLOCK_SIZE) + offset;
last_to = "chip-data";
break;
case 3: /* 11: Reserved */
return 0;
}
}
return nr_bytes;
}
/* Given a virtual address in the IMAP address space, translate it
into a physical address. */
unsigned long
sim_d10v_translate_imap_addr (unsigned long offset,
int nr_bytes,
unsigned long *phys,
unsigned long (*imap_register) (int reg_nr))
{
short map;
int regno;
int sp;
int segno;
last_from = "logical-insn";
if (offset >= (IMAP_BLOCK_SIZE * SIM_D10V_NR_IMAP_REGS))
{
/* Logical address outside of IMAP segments, not supported */
return 0;
}
regno = (offset / IMAP_BLOCK_SIZE);
offset = (offset % IMAP_BLOCK_SIZE);
if (offset + nr_bytes > IMAP_BLOCK_SIZE)
{
/* Don't cross a BLOCK boundary */
nr_bytes = IMAP_BLOCK_SIZE - offset;
}
map = imap_register (regno);
sp = (map & 0x3000) >> 12;
segno = (map & 0x007f);
switch (sp)
{
case 0: /* 00: unified memory */
*phys = SIM_D10V_MEMORY_UNIFIED + (segno << 17) + offset;
last_to = "unified";
break;
case 1: /* 01: instruction memory */
*phys = SIM_D10V_MEMORY_INSN + (IMAP_BLOCK_SIZE * regno) + offset;
last_to = "chip-insn";
break;
case 2: /*10*/
/* Reserved. */
return 0;
case 3: /* 11: for testing - instruction memory */
offset = (offset % 0x800);
*phys = SIM_D10V_MEMORY_INSN + offset;
if (offset + nr_bytes > 0x800)
/* don't cross VM boundary */
nr_bytes = 0x800 - offset;
last_to = "test-insn";
break;
}
return nr_bytes;
}
unsigned long
sim_d10v_translate_addr (unsigned long memaddr,
int nr_bytes,
unsigned long *targ_addr,
unsigned long (*dmap_register) (int reg_nr),
unsigned long (*imap_register) (int reg_nr))
{
unsigned long phys;
unsigned long seg;
unsigned long off;
last_from = "unknown";
last_to = "unknown";
seg = (memaddr >> 24);
off = (memaddr & 0xffffffL);
/* However, if we've asked to use the previous generation of segment
mapping, rearrange the segments as follows. */
if (old_segment_mapping)
{
switch (seg)
{
case 0x00: /* DMAP translated memory */
seg = 0x10;
break;
case 0x01: /* IMAP translated memory */
seg = 0x11;
break;
case 0x10: /* On-chip data memory */
seg = 0x02;
break;
case 0x11: /* On-chip insn memory */
seg = 0x01;
break;
case 0x12: /* Unified memory */
seg = 0x00;
break;
}
}
switch (seg)
{
case 0x00: /* Physical unified memory */
last_from = "phys-unified";
last_to = "unified";
phys = SIM_D10V_MEMORY_UNIFIED + off;
if ((off % SEGMENT_SIZE) + nr_bytes > SEGMENT_SIZE)
nr_bytes = SEGMENT_SIZE - (off % SEGMENT_SIZE);
break;
case 0x01: /* Physical instruction memory */
last_from = "phys-insn";
last_to = "chip-insn";
phys = SIM_D10V_MEMORY_INSN + off;
if ((off % SEGMENT_SIZE) + nr_bytes > SEGMENT_SIZE)
nr_bytes = SEGMENT_SIZE - (off % SEGMENT_SIZE);
break;
case 0x02: /* Physical data memory segment */
last_from = "phys-data";
last_to = "chip-data";
phys = SIM_D10V_MEMORY_DATA + off;
if ((off % SEGMENT_SIZE) + nr_bytes > SEGMENT_SIZE)
nr_bytes = SEGMENT_SIZE - (off % SEGMENT_SIZE);
break;
case 0x10: /* in logical data address segment */
nr_bytes = sim_d10v_translate_dmap_addr (off, nr_bytes, &phys,
dmap_register);
break;
case 0x11: /* in logical instruction address segment */
nr_bytes = sim_d10v_translate_imap_addr (off, nr_bytes, &phys,
imap_register);
break;
default:
return 0;
}
*targ_addr = phys;
return nr_bytes;
}
/* Return a pointer into the raw buffer designated by phys_addr. It
is assumed that the client has already ensured that the access
isn't going to cross a segment boundary. */
uint8 *
map_memory (unsigned phys_addr)
{
uint8 **memory;
uint8 *raw;
unsigned offset;
int segment = ((phys_addr >> 24) & 0xff);
switch (segment)
{
case 0x00: /* Unified memory */
{
memory = &State.mem.unif[(phys_addr / SEGMENT_SIZE) % UMEM_SEGMENTS];
last_segname = "umem";
break;
}
case 0x01: /* On-chip insn memory */
{
memory = &State.mem.insn[(phys_addr / SEGMENT_SIZE) % IMEM_SEGMENTS];
last_segname = "imem";
break;
}
case 0x02: /* On-chip data memory */
{
if ((phys_addr & 0xff00) == 0xff00)
{
phys_addr = (phys_addr & 0xffff);
if (phys_addr == DMAP2_SHADDOW)
{
phys_addr = DMAP2_OFFSET;
last_segname = "dmap";
}
else
last_segname = "reg";
}
else
last_segname = "dmem";
memory = &State.mem.data[(phys_addr / SEGMENT_SIZE) % DMEM_SEGMENTS];
break;
}
default:
/* OOPS! */
last_segname = "scrap";
return State.mem.fault;
}
if (*memory == NULL)
{
*memory = calloc (1, SEGMENT_SIZE);
if (*memory == NULL)
{
(*d10v_callback->printf_filtered) (d10v_callback, "Malloc failed.\n");
return State.mem.fault;
}
}
offset = (phys_addr % SEGMENT_SIZE);
raw = *memory + offset;
return raw;
}
/* Transfer data to/from simulated memory. Since a bug in either the
simulated program or in gdb or the simulator itself may cause a
bogus address to be passed in, we need to do some sanity checking
on addresses to make sure they are within bounds. When an address
fails the bounds check, treat it as a zero length read/write rather
than aborting the entire run. */
static int
xfer_mem (SIM_ADDR virt,
unsigned char *buffer,
int size,
int write_p)
{
int xfered = 0;
while (xfered < size)
{
uint8 *memory;
unsigned long phys;
int phys_size;
phys_size = sim_d10v_translate_addr (virt, size,
&phys,
dmap_register,
imap_register);
if (phys_size == 0)
return xfered;
memory = map_memory (phys);
#ifdef DEBUG
if ((d10v_debug & DEBUG_INSTRUCTION) != 0)
{
(*d10v_callback->printf_filtered)
(d10v_callback,
"sim_%s %d bytes: 0x%08lx (%s) -> 0x%08lx (%s) -> 0x%08lx (%s)\n",
(write_p ? "write" : "read"),
phys_size, virt, last_from,
phys, last_to,
(long) memory, last_segname);
}
#endif
if (write_p)
{
memcpy (memory, buffer, phys_size);
}
else
{
memcpy (buffer, memory, phys_size);
}
virt += phys_size;
buffer += phys_size;
xfered += phys_size;
}
return size;
}
int
sim_write (sd, addr, buffer, size)
SIM_DESC sd;
SIM_ADDR addr;
unsigned char *buffer;
int size;
{
/* FIXME: this should be performing a virtual transfer */
return xfer_mem( addr, buffer, size, 1);
}
int
sim_read (sd, addr, buffer, size)
SIM_DESC sd;
SIM_ADDR addr;
unsigned char *buffer;
int size;
{
/* FIXME: this should be performing a virtual transfer */
return xfer_mem( addr, buffer, size, 0);
}
SIM_DESC
sim_open (kind, callback, abfd, argv)
SIM_OPEN_KIND kind;
host_callback *callback;
struct _bfd *abfd;
char **argv;
{
struct simops *s;
struct hash_entry *h;
static int init_p = 0;
char **p;
sim_kind = kind;
d10v_callback = callback;
myname = argv[0];
old_segment_mapping = 0;
/* NOTE: This argument parsing is only effective when this function
is called by GDB. Standalone argument parsing is handled by
sim/common/run.c. */
for (p = argv + 1; *p; ++p)
{
if (strcmp (*p, "-oldseg") == 0)
old_segment_mapping = 1;
#ifdef DEBUG
else if (strcmp (*p, "-t") == 0)
d10v_debug = DEBUG;
else if (strncmp (*p, "-t", 2) == 0)
d10v_debug = atoi (*p + 2);
#endif
else
(*d10v_callback->printf_filtered) (d10v_callback, "ERROR: unsupported option(s): %s\n",*p);
}
/* put all the opcodes in the hash table */
if (!init_p++)
{
for (s = Simops; s->func; s++)
{
h = &hash_table[hash(s->opcode,s->format)];
/* go to the last entry in the chain */
while (h->next)
h = h->next;
if (h->ops)
{
h->next = (struct hash_entry *) calloc(1,sizeof(struct hash_entry));
if (!h->next)
perror ("malloc failure");
h = h->next;
}
h->ops = s;
h->mask = s->mask;
h->opcode = s->opcode;
h->size = s->is_long;
}
}
/* reset the processor state */
if (!State.mem.data[0])
sim_size (1);
sim_create_inferior ((SIM_DESC) 1, NULL, NULL, NULL);
/* Fudge our descriptor. */
return (SIM_DESC) 1;
}
void
sim_close (sd, quitting)
SIM_DESC sd;
int quitting;
{
if (prog_bfd != NULL && prog_bfd_was_opened_p)
{
bfd_close (prog_bfd);
prog_bfd = NULL;
prog_bfd_was_opened_p = 0;
}
}
void
sim_set_profile (n)
int n;
{
(*d10v_callback->printf_filtered) (d10v_callback, "sim_set_profile %d\n",n);
}
void
sim_set_profile_size (n)
int n;
{
(*d10v_callback->printf_filtered) (d10v_callback, "sim_set_profile_size %d\n",n);
}
uint8 *
dmem_addr (uint16 offset)
{
unsigned long phys;
uint8 *mem;
int phys_size;
/* Note: DMEM address range is 0..0x10000. Calling code can compute
things like ``0xfffe + 0x0e60 == 0x10e5d''. Since offset's type
is uint16 this is modulo'ed onto 0x0e5d. */
phys_size = sim_d10v_translate_dmap_addr (offset, 1, &phys,
dmap_register);
if (phys_size == 0)
{
mem = State.mem.fault;
}
else
mem = map_memory (phys);
#ifdef DEBUG
if ((d10v_debug & DEBUG_MEMORY))
{
(*d10v_callback->printf_filtered)
(d10v_callback,
"mem: 0x%08x (%s) -> 0x%08lx %d (%s) -> 0x%08lx (%s)\n",
offset, last_from,
phys, phys_size, last_to,
(long) mem, last_segname);
}
#endif
return mem;
}
uint8 *
imem_addr (uint32 offset)
{
unsigned long phys;
uint8 *mem;
int phys_size = sim_d10v_translate_imap_addr (offset, 1, &phys, imap_register);
if (phys_size == 0)
{
return State.mem.fault;
}
mem = map_memory (phys);
#ifdef DEBUG
if ((d10v_debug & DEBUG_MEMORY))
{
(*d10v_callback->printf_filtered)
(d10v_callback,
"mem: 0x%08x (%s) -> 0x%08lx %d (%s) -> 0x%08lx (%s)\n",
offset, last_from,
phys, phys_size, last_to,
(long) mem, last_segname);
}
#endif
return mem;
}
static int stop_simulator = 0;
int
sim_stop (sd)
SIM_DESC sd;
{
stop_simulator = 1;
return 1;
}
/* Run (or resume) the program. */
void
sim_resume (sd, step, siggnal)
SIM_DESC sd;
int step, siggnal;
{
uint32 inst;
uint8 *iaddr;
/* (*d10v_callback->printf_filtered) (d10v_callback, "sim_resume (%d,%d) PC=0x%x\n",step,siggnal,PC); */
State.exception = 0;
if (step)
sim_stop (sd);
do
{
iaddr = imem_addr ((uint32)PC << 2);
if (iaddr == State.mem.fault)
{
State.exception = SIGBUS;
break;
}
inst = get_longword( iaddr );
State.pc_changed = 0;
ins_type_counters[ (int)INS_CYCLES ]++;
switch (inst & 0xC0000000)
{
case 0xC0000000:
/* long instruction */
do_long (inst & 0x3FFFFFFF);
break;
case 0x80000000:
/* R -> L */
do_2_short ( inst & 0x7FFF, (inst & 0x3FFF8000) >> 15, RIGHT_FIRST);
break;
case 0x40000000:
/* L -> R */
do_2_short ((inst & 0x3FFF8000) >> 15, inst & 0x7FFF, LEFT_FIRST);
break;
case 0:
do_parallel ((inst & 0x3FFF8000) >> 15, inst & 0x7FFF);
break;
}
/* If the PC of the current instruction matches RPT_E then
schedule a branch to the loop start. If one of those
instructions happens to be a branch, than that instruction
will be ignored */
if (!State.pc_changed)
{
if (PSW_RP && PC == RPT_E)
{
/* Note: The behavour of a branch instruction at RPT_E
is implementation dependant, this simulator takes the
branch. Branching to RPT_E is valid, the instruction
must be executed before the loop is taken. */
if (RPT_C == 1)
{
SET_PSW_RP (0);
SET_RPT_C (0);
SET_PC (PC + 1);
}
else
{
SET_RPT_C (RPT_C - 1);
SET_PC (RPT_S);
}
}
else
SET_PC (PC + 1);
}
/* Check for a breakpoint trap on this instruction. This
overrides any pending branches or loops */
if (PSW_DB && PC == IBA)
{
SET_BPC (PC);
SET_BPSW (PSW);
SET_PSW (PSW & PSW_SM_BIT);
SET_PC (SDBT_VECTOR_START);
}
/* Writeback all the DATA / PC changes */
SLOT_FLUSH ();
#ifdef NEED_UI_LOOP_HOOK
if (ui_loop_hook != NULL && ui_loop_hook_counter-- < 0)
{
ui_loop_hook_counter = UI_LOOP_POLL_INTERVAL;
ui_loop_hook (0);
}
#endif /* NEED_UI_LOOP_HOOK */
}
while ( !State.exception && !stop_simulator);
if (step && !State.exception)
State.exception = SIGTRAP;
}
int
sim_trace (sd)
SIM_DESC sd;
{
#ifdef DEBUG
d10v_debug = DEBUG;
#endif
sim_resume (sd, 0, 0);
return 1;
}
void
sim_info (sd, verbose)
SIM_DESC sd;
int verbose;
{
char buf1[40];
char buf2[40];
char buf3[40];
char buf4[40];
char buf5[40];
unsigned long left = ins_type_counters[ (int)INS_LEFT ] + ins_type_counters[ (int)INS_LEFT_COND_EXE ];
unsigned long left_nops = ins_type_counters[ (int)INS_LEFT_NOPS ];
unsigned long left_parallel = ins_type_counters[ (int)INS_LEFT_PARALLEL ];
unsigned long left_cond = ins_type_counters[ (int)INS_LEFT_COND_TEST ];
unsigned long left_total = left + left_parallel + left_cond + left_nops;
unsigned long right = ins_type_counters[ (int)INS_RIGHT ] + ins_type_counters[ (int)INS_RIGHT_COND_EXE ];
unsigned long right_nops = ins_type_counters[ (int)INS_RIGHT_NOPS ];
unsigned long right_parallel = ins_type_counters[ (int)INS_RIGHT_PARALLEL ];
unsigned long right_cond = ins_type_counters[ (int)INS_RIGHT_COND_TEST ];
unsigned long right_total = right + right_parallel + right_cond + right_nops;
unsigned long unknown = ins_type_counters[ (int)INS_UNKNOWN ];
unsigned long ins_long = ins_type_counters[ (int)INS_LONG ];
unsigned long parallel = ins_type_counters[ (int)INS_PARALLEL ];
unsigned long leftright = ins_type_counters[ (int)INS_LEFTRIGHT ];
unsigned long rightleft = ins_type_counters[ (int)INS_RIGHTLEFT ];
unsigned long cond_true = ins_type_counters[ (int)INS_COND_TRUE ];
unsigned long cond_false = ins_type_counters[ (int)INS_COND_FALSE ];
unsigned long cond_jump = ins_type_counters[ (int)INS_COND_JUMP ];
unsigned long cycles = ins_type_counters[ (int)INS_CYCLES ];
unsigned long total = (unknown + left_total + right_total + ins_long);
int size = strlen (add_commas (buf1, sizeof (buf1), total));
int parallel_size = strlen (add_commas (buf1, sizeof (buf1),
(left_parallel > right_parallel) ? left_parallel : right_parallel));
int cond_size = strlen (add_commas (buf1, sizeof (buf1), (left_cond > right_cond) ? left_cond : right_cond));
int nop_size = strlen (add_commas (buf1, sizeof (buf1), (left_nops > right_nops) ? left_nops : right_nops));
int normal_size = strlen (add_commas (buf1, sizeof (buf1), (left > right) ? left : right));
(*d10v_callback->printf_filtered) (d10v_callback,
"executed %*s left instruction(s), %*s normal, %*s parallel, %*s EXExxx, %*s nops\n",
size, add_commas (buf1, sizeof (buf1), left_total),
normal_size, add_commas (buf2, sizeof (buf2), left),
parallel_size, add_commas (buf3, sizeof (buf3), left_parallel),
cond_size, add_commas (buf4, sizeof (buf4), left_cond),
nop_size, add_commas (buf5, sizeof (buf5), left_nops));
(*d10v_callback->printf_filtered) (d10v_callback,
"executed %*s right instruction(s), %*s normal, %*s parallel, %*s EXExxx, %*s nops\n",
size, add_commas (buf1, sizeof (buf1), right_total),
normal_size, add_commas (buf2, sizeof (buf2), right),
parallel_size, add_commas (buf3, sizeof (buf3), right_parallel),
cond_size, add_commas (buf4, sizeof (buf4), right_cond),
nop_size, add_commas (buf5, sizeof (buf5), right_nops));
if (ins_long)
(*d10v_callback->printf_filtered) (d10v_callback,
"executed %*s long instruction(s)\n",
size, add_commas (buf1, sizeof (buf1), ins_long));
if (parallel)
(*d10v_callback->printf_filtered) (d10v_callback,
"executed %*s parallel instruction(s)\n",
size, add_commas (buf1, sizeof (buf1), parallel));
if (leftright)
(*d10v_callback->printf_filtered) (d10v_callback,
"executed %*s instruction(s) encoded L->R\n",
size, add_commas (buf1, sizeof (buf1), leftright));
if (rightleft)
(*d10v_callback->printf_filtered) (d10v_callback,
"executed %*s instruction(s) encoded R->L\n",
size, add_commas (buf1, sizeof (buf1), rightleft));
if (unknown)
(*d10v_callback->printf_filtered) (d10v_callback,
"executed %*s unknown instruction(s)\n",
size, add_commas (buf1, sizeof (buf1), unknown));
if (cond_true)
(*d10v_callback->printf_filtered) (d10v_callback,
"executed %*s instruction(s) due to EXExxx condition being true\n",
size, add_commas (buf1, sizeof (buf1), cond_true));
if (cond_false)
(*d10v_callback->printf_filtered) (d10v_callback,
"skipped %*s instruction(s) due to EXExxx condition being false\n",
size, add_commas (buf1, sizeof (buf1), cond_false));
if (cond_jump)
(*d10v_callback->printf_filtered) (d10v_callback,
"skipped %*s instruction(s) due to conditional branch succeeding\n",
size, add_commas (buf1, sizeof (buf1), cond_jump));
(*d10v_callback->printf_filtered) (d10v_callback,
"executed %*s cycle(s)\n",
size, add_commas (buf1, sizeof (buf1), cycles));
(*d10v_callback->printf_filtered) (d10v_callback,
"executed %*s total instructions\n",
size, add_commas (buf1, sizeof (buf1), total));
}
SIM_RC
sim_create_inferior (sd, abfd, argv, env)
SIM_DESC sd;
struct _bfd *abfd;
char **argv;
char **env;
{
bfd_vma start_address;
/* reset all state information */
memset (&State.regs, 0, (int)&State.mem - (int)&State.regs);
if (argv)
{
/* a hack to set r0/r1 with argc/argv */
/* some high memory that won't be overwritten by the stack soon */
bfd_vma addr = 0x7C00;
int p = 20;
int i = 0;
while (argv[i])
{
int size = strlen (argv[i]) + 1;
SW (addr + 2*i, addr + p);
sim_write (sd, addr + 0, argv[i], size);
p += size;
i++;
}
SET_GPR (0, addr);
SET_GPR (1, i);
}
/* set PC */
if (abfd != NULL)
start_address = bfd_get_start_address (abfd);
else
start_address = 0xffc0 << 2;
#ifdef DEBUG
if (d10v_debug)
(*d10v_callback->printf_filtered) (d10v_callback, "sim_create_inferior: PC=0x%lx\n", (long) start_address);
#endif
SET_CREG (PC_CR, start_address >> 2);
/* cpu resets imap0 to 0 and imap1 to 0x7f, but D10V-EVA board
initializes imap0 and imap1 to 0x1000 as part of its ROM
initialization. */
if (old_segment_mapping)
{
/* External memory startup. This is the HARD reset state. */
set_imap_register (0, 0x0000);
set_imap_register (1, 0x007f);
set_dmap_register (0, 0x2000);
set_dmap_register (1, 0x2000);
set_dmap_register (2, 0x0000); /* Old DMAP */
set_dmap_register (3, 0x0000);
}
else
{
/* Internal memory startup. This is the ROM intialized state. */
set_imap_register (0, 0x1000);
set_imap_register (1, 0x1000);
set_dmap_register (0, 0x2000);
set_dmap_register (1, 0x2000);
set_dmap_register (2, 0x0000); /* Old DMAP, Value is not 0x2000 */
set_dmap_register (3, 0x0000);
}
SLOT_FLUSH ();
return SIM_RC_OK;
}
void
sim_set_callbacks (p)
host_callback *p;
{
d10v_callback = p;
}
void
sim_stop_reason (sd, reason, sigrc)
SIM_DESC sd;
enum sim_stop *reason;
int *sigrc;
{
/* (*d10v_callback->printf_filtered) (d10v_callback, "sim_stop_reason: PC=0x%x\n",PC<<2); */
switch (State.exception)
{
case SIG_D10V_STOP: /* stop instruction */
*reason = sim_exited;
*sigrc = 0;
break;
case SIG_D10V_EXIT: /* exit trap */
*reason = sim_exited;
*sigrc = GPR (0);
break;
default: /* some signal */
*reason = sim_stopped;
if (stop_simulator && !State.exception)
*sigrc = SIGINT;
else
*sigrc = State.exception;
break;
}
stop_simulator = 0;
}
int
sim_fetch_register (sd, rn, memory, length)
SIM_DESC sd;
int rn;
unsigned char *memory;
int length;
{
int size;
if (rn < 0)
size = 0;
else if (rn >= SIM_D10V_R0_REGNUM
&& rn < SIM_D10V_R0_REGNUM + SIM_D10V_NR_R_REGS)
{
WRITE_16 (memory, GPR (rn - SIM_D10V_R0_REGNUM));
size = 2;
}
else if (rn >= SIM_D10V_CR0_REGNUM
&& rn < SIM_D10V_CR0_REGNUM + SIM_D10V_NR_CR_REGS)
{
WRITE_16 (memory, CREG (rn - SIM_D10V_CR0_REGNUM));
size = 2;
}
else if (rn >= SIM_D10V_A0_REGNUM
&& rn < SIM_D10V_A0_REGNUM + SIM_D10V_NR_A_REGS)
{
WRITE_64 (memory, ACC (rn - SIM_D10V_A0_REGNUM));
size = 8;
}
else if (rn == SIM_D10V_SPI_REGNUM)
{
/* PSW_SM indicates that the current SP is the USER
stack-pointer. */
WRITE_16 (memory, spi_register ());
size = 2;
}
else if (rn == SIM_D10V_SPU_REGNUM)
{
/* PSW_SM indicates that the current SP is the USER
stack-pointer. */
WRITE_16 (memory, spu_register ());
size = 2;
}
else if (rn >= SIM_D10V_IMAP0_REGNUM
&& rn < SIM_D10V_IMAP0_REGNUM + SIM_D10V_NR_IMAP_REGS)
{
WRITE_16 (memory, imap_register (rn - SIM_D10V_IMAP0_REGNUM));
size = 2;
}
else if (rn >= SIM_D10V_DMAP0_REGNUM
&& rn < SIM_D10V_DMAP0_REGNUM + SIM_D10V_NR_DMAP_REGS)
{
WRITE_16 (memory, dmap_register (rn - SIM_D10V_DMAP0_REGNUM));
size = 2;
}
else
size = 0;
return size;
}
int
sim_store_register (sd, rn, memory, length)
SIM_DESC sd;
int rn;
unsigned char *memory;
int length;
{
int size;
if (rn < 0)
size = 0;
else if (rn >= SIM_D10V_R0_REGNUM
&& rn < SIM_D10V_R0_REGNUM + SIM_D10V_NR_R_REGS)
{
SET_GPR (rn - SIM_D10V_R0_REGNUM, READ_16 (memory));
size = 2;
}
else if (rn >= SIM_D10V_CR0_REGNUM
&& rn < SIM_D10V_CR0_REGNUM + SIM_D10V_NR_CR_REGS)
{
SET_CREG (rn - SIM_D10V_CR0_REGNUM, READ_16 (memory));
size = 2;
}
else if (rn >= SIM_D10V_A0_REGNUM
&& rn < SIM_D10V_A0_REGNUM + SIM_D10V_NR_A_REGS)
{
SET_ACC (rn - SIM_D10V_A0_REGNUM, READ_64 (memory) & MASK40);
size = 8;
}
else if (rn == SIM_D10V_SPI_REGNUM)
{
/* PSW_SM indicates that the current SP is the USER
stack-pointer. */
set_spi_register (READ_16 (memory));
size = 2;
}
else if (rn == SIM_D10V_SPU_REGNUM)
{
set_spu_register (READ_16 (memory));
size = 2;
}
else if (rn >= SIM_D10V_IMAP0_REGNUM
&& rn < SIM_D10V_IMAP0_REGNUM + SIM_D10V_NR_IMAP_REGS)
{
set_imap_register (rn - SIM_D10V_IMAP0_REGNUM, READ_16(memory));
size = 2;
}
else if (rn >= SIM_D10V_DMAP0_REGNUM
&& rn < SIM_D10V_DMAP0_REGNUM + SIM_D10V_NR_DMAP_REGS)
{
set_dmap_register (rn - SIM_D10V_DMAP0_REGNUM, READ_16(memory));
size = 2;
}
else
size = 0;
SLOT_FLUSH ();
return size;
}
void
sim_do_command (sd, cmd)
SIM_DESC sd;
char *cmd;
{
(*d10v_callback->printf_filtered) (d10v_callback, "sim_do_command: %s\n",cmd);
}
SIM_RC
sim_load (sd, prog, abfd, from_tty)
SIM_DESC sd;
char *prog;
bfd *abfd;
int from_tty;
{
extern bfd *sim_load_file (); /* ??? Don't know where this should live. */
if (prog_bfd != NULL && prog_bfd_was_opened_p)
{
bfd_close (prog_bfd);
prog_bfd_was_opened_p = 0;
}
prog_bfd = sim_load_file (sd, myname, d10v_callback, prog, abfd,
sim_kind == SIM_OPEN_DEBUG,
1/*LMA*/, sim_write);
if (prog_bfd == NULL)
return SIM_RC_FAIL;
prog_bfd_was_opened_p = abfd == NULL;
return SIM_RC_OK;
}
|