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
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
|
/*
* Simulator for the Hitachi H8/300 architecture.
*
* Written by Steve Chamberlain of Cygnus Support. sac@cygnus.com
*
* This file is part of H8/300 sim
*
*
* THIS SOFTWARE IS NOT COPYRIGHTED
*
* Cygnus offers the following for use in the public domain. Cygnus makes no
* warranty with regard to the software or its performance and the user
* accepts the software "AS IS" with all faults.
*
* CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS
* SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "config.h"
#include <stdio.h>
#include <signal.h>
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include "ansidecl.h"
#include "bfd.h"
#include "callback.h"
#include "remote-sim.h"
#ifndef SIGTRAP
# define SIGTRAP 5
#endif
int debug;
host_callback *sim_callback;
static SIM_OPEN_KIND sim_kind;
static char *myname;
/* FIXME: Needs to live in header file.
This header should also include the things in remote-sim.h.
One could move this to remote-sim.h but this function isn't needed
by gdb. */
void sim_set_simcache_size PARAMS ((int));
#define X(op, size) op * 4 + size
#define SP (h8300hmode ? SL : SW)
#define SB 0
#define SW 1
#define SL 2
#define OP_REG 1
#define OP_DEC 2
#define OP_DISP 3
#define OP_INC 4
#define OP_PCREL 5
#define OP_MEM 6
#define OP_CCR 7
#define OP_IMM 8
#define OP_ABS 10
#define h8_opcodes ops
#define DEFINE_TABLE
#include "opcode/h8300.h"
#include "inst.h"
/* The rate at which to call the host's poll_quit callback. */
#define POLL_QUIT_INTERVAL 0x80000
#define LOW_BYTE(x) ((x) & 0xff)
#define HIGH_BYTE(x) (((x) >> 8) & 0xff)
#define P(X,Y) ((X << 8) | Y)
#define BUILDSR() cpu.ccr = (N << 3) | (Z << 2) | (V << 1) | C;
#define GETSR() \
c = (cpu.ccr >> 0) & 1;\
v = (cpu.ccr >> 1) & 1;\
nz = !((cpu.ccr >> 2) & 1);\
n = (cpu.ccr >> 3) & 1;
#ifdef __CHAR_IS_SIGNED__
#define SEXTCHAR(x) ((char) (x))
#endif
#ifndef SEXTCHAR
#define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff): x & 0xff)
#endif
#define UEXTCHAR(x) ((x) & 0xff)
#define UEXTSHORT(x) ((x) & 0xffff)
#define SEXTSHORT(x) ((short) (x))
static cpu_state_type cpu;
int h8300hmode = 0;
int h8300smode = 0;
static int memory_size;
static int
get_now ()
{
#ifndef WIN32
return time (0);
#endif
return 0;
}
static int
now_persec ()
{
return 1;
}
static int
bitfrom (x)
{
switch (x & SIZE)
{
case L_8:
return SB;
case L_16:
return SW;
case L_32:
return SL;
case L_P:
return h8300hmode ? SL : SW;
}
}
static unsigned int
lvalue (x, rn)
{
switch (x / 4)
{
case OP_DISP:
if (rn == 8)
{
return X (OP_IMM, SP);
}
return X (OP_REG, SP);
case OP_MEM:
return X (OP_MEM, SP);
default:
abort ();
}
}
static unsigned int
decode (addr, data, dst)
int addr;
unsigned char *data;
decoded_inst *dst;
{
int rs = 0;
int rd = 0;
int rdisp = 0;
int abs = 0;
int bit = 0;
int plen = 0;
struct h8_opcode *q;
int size = 0;
dst->dst.type = -1;
dst->src.type = -1;
/* Find the exact opcode/arg combo. */
for (q = h8_opcodes; q->name; q++)
{
op_type *nib = q->data.nib;
unsigned int len = 0;
while (1)
{
op_type looking_for = *nib;
int thisnib = data[len >> 1];
thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
if (looking_for < 16 && looking_for >= 0)
{
if (looking_for != thisnib)
goto fail;
}
else
{
if ((int) looking_for & (int) B31)
{
if (!(((int) thisnib & 0x8) != 0))
goto fail;
looking_for = (op_type) ((int) looking_for & ~(int) B31);
thisnib &= 0x7;
}
if ((int) looking_for & (int) B30)
{
if (!(((int) thisnib & 0x8) == 0))
goto fail;
looking_for = (op_type) ((int) looking_for & ~(int) B30);
}
if (looking_for & DBIT)
{
/* Exclude adds/subs by looking at bit 0 and 2, and
make sure the operand size, either w or l,
matches by looking at bit 1. */
if ((looking_for & 7) != (thisnib & 7))
goto fail;
abs = (thisnib & 0x8) ? 2 : 1;
}
else if (looking_for & (REG | IND | INC | DEC))
{
if (looking_for & REG)
{
/* Can work out size from the register. */
size = bitfrom (looking_for);
}
if (looking_for & SRC)
rs = thisnib;
else
rd = thisnib;
}
else if (looking_for & L_16)
{
abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
plen = 16;
if (looking_for & (PCREL | DISP))
{
abs = (short) (abs);
}
}
else if (looking_for & ABSJMP)
{
abs = (data[1] << 16) | (data[2] << 8) | (data[3]);
}
else if (looking_for & MEMIND)
{
abs = data[1];
}
else if (looking_for & L_32)
{
int i = len >> 1;
abs = (data[i] << 24)
| (data[i + 1] << 16)
| (data[i + 2] << 8)
| (data[i + 3]);
plen = 32;
}
else if (looking_for & L_24)
{
int i = len >> 1;
abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]);
plen = 24;
}
else if (looking_for & IGNORE)
{
;
}
else if (looking_for & DISPREG)
{
rdisp = thisnib & 0x7;
}
else if (looking_for & KBIT)
{
switch (thisnib)
{
case 9:
abs = 4;
break;
case 8:
abs = 2;
break;
case 0:
abs = 1;
break;
default:
goto fail;
}
}
else if (looking_for & L_8)
{
plen = 8;
if (looking_for & PCREL)
{
abs = SEXTCHAR (data[len >> 1]);
}
else if (looking_for & ABS8MEM)
{
plen = 8;
abs = h8300hmode ? ~0xff0000ff : ~0xffff00ff;
abs |= data[len >> 1] & 0xff;
}
else
{
abs = data[len >> 1] & 0xff;
}
}
else if (looking_for & L_3)
{
plen = 3;
bit = thisnib;
}
else if (looking_for == E)
{
dst->op = q;
/* Fill in the args. */
{
op_type *args = q->args.nib;
int hadone = 0;
while (*args != E)
{
int x = *args;
int rn = (x & DST) ? rd : rs;
ea_type *p;
if (x & DST)
p = &(dst->dst);
else
p = &(dst->src);
if (x & L_3)
{
p->type = X (OP_IMM, size);
p->literal = bit;
}
else if (x & (IMM | KBIT | DBIT))
{
p->type = X (OP_IMM, size);
p->literal = abs;
}
else if (x & REG)
{
/* Reset the size.
Some ops (like mul) have two sizes. */
size = bitfrom (x);
p->type = X (OP_REG, size);
p->reg = rn;
}
else if (x & INC)
{
p->type = X (OP_INC, size);
p->reg = rn & 0x7;
}
else if (x & DEC)
{
p->type = X (OP_DEC, size);
p->reg = rn & 0x7;
}
else if (x & IND)
{
p->type = X (OP_DISP, size);
p->reg = rn & 0x7;
p->literal = 0;
}
else if (x & (ABS | ABSJMP | ABS8MEM))
{
p->type = X (OP_DISP, size);
p->literal = abs;
p->reg = 8;
}
else if (x & MEMIND)
{
p->type = X (OP_MEM, size);
p->literal = abs;
}
else if (x & PCREL)
{
p->type = X (OP_PCREL, size);
p->literal = abs + addr + 2;
if (x & L_16)
p->literal += 2;
}
else if (x & ABSJMP)
{
p->type = X (OP_IMM, SP);
p->literal = abs;
}
else if (x & DISP)
{
p->type = X (OP_DISP, size);
p->literal = abs;
p->reg = rdisp & 0x7;
}
else if (x & CCR)
{
p->type = OP_CCR;
}
else
printf ("Hmmmm %x", x);
args++;
}
}
/* But a jmp or a jsr gets automagically lvalued,
since we branch to their address not their
contents. */
if (q->how == O (O_JSR, SB)
|| q->how == O (O_JMP, SB))
{
dst->src.type = lvalue (dst->src.type, dst->src.reg);
}
if (dst->dst.type == -1)
dst->dst = dst->src;
dst->opcode = q->how;
dst->cycles = q->time;
/* And a jsr to 0xc4 is turned into a magic trap. */
if (dst->opcode == O (O_JSR, SB))
{
if (dst->src.literal == 0xc4)
{
dst->opcode = O (O_SYSCALL, SB);
}
}
dst->next_pc = addr + len / 2;
return;
}
else
printf ("Don't understand %x \n", looking_for);
}
len++;
nib++;
}
fail:
;
}
/* Fell off the end. */
dst->opcode = O (O_ILL, SB);
}
static void
compile (pc)
{
int idx;
/* Find the next cache entry to use. */
idx = cpu.cache_top + 1;
cpu.compiles++;
if (idx >= cpu.csize)
{
idx = 1;
}
cpu.cache_top = idx;
/* Throw away its old meaning. */
cpu.cache_idx[cpu.cache[idx].oldpc] = 0;
/* Set to new address. */
cpu.cache[idx].oldpc = pc;
/* Fill in instruction info. */
decode (pc, cpu.memory + pc, cpu.cache + idx);
/* Point to new cache entry. */
cpu.cache_idx[pc] = idx;
}
static unsigned char *breg[18];
static unsigned short *wreg[18];
static unsigned int *lreg[18];
#define GET_B_REG(x) *(breg[x])
#define SET_B_REG(x,y) (*(breg[x])) = (y)
#define GET_W_REG(x) *(wreg[x])
#define SET_W_REG(x,y) (*(wreg[x])) = (y)
#define GET_L_REG(x) *(lreg[x])
#define SET_L_REG(x,y) (*(lreg[x])) = (y)
#define GET_MEMORY_L(x) \
(x < memory_size \
? ((cpu.memory[x+0] << 24) | (cpu.memory[x+1] << 16) \
| (cpu.memory[x+2] << 8) | cpu.memory[x+3]) \
: ((cpu.eightbit[(x+0) & 0xff] << 24) | (cpu.eightbit[(x+1) & 0xff] << 16) \
| (cpu.eightbit[(x+2) & 0xff] << 8) | cpu.eightbit[(x+3) & 0xff]))
#define GET_MEMORY_W(x) \
(x < memory_size \
? ((cpu.memory[x+0] << 8) | (cpu.memory[x+1] << 0)) \
: ((cpu.eightbit[(x+0) & 0xff] << 8) | (cpu.eightbit[(x+1) & 0xff] << 0)))
#define GET_MEMORY_B(x) \
(x < memory_size ? (cpu.memory[x]) : (cpu.eightbit[x & 0xff]))
#define SET_MEMORY_L(x,y) \
{ register unsigned char *_p; register int __y = y; \
_p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \
_p[0] = (__y)>>24; _p[1] = (__y)>>16; \
_p[2] = (__y)>>8; _p[3] = (__y)>>0;}
#define SET_MEMORY_W(x,y) \
{ register unsigned char *_p; register int __y = y; \
_p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \
_p[0] = (__y)>>8; _p[1] =(__y);}
#define SET_MEMORY_B(x,y) \
(x < memory_size ? (cpu.memory[(x)] = y) : (cpu.eightbit[x & 0xff] = y))
int
fetch (arg, n)
ea_type *arg;
{
int rn = arg->reg;
int abs = arg->literal;
int r;
int t;
switch (arg->type)
{
case X (OP_REG, SB):
return GET_B_REG (rn);
case X (OP_REG, SW):
return GET_W_REG (rn);
case X (OP_REG, SL):
return GET_L_REG (rn);
case X (OP_IMM, SB):
case X (OP_IMM, SW):
case X (OP_IMM, SL):
return abs;
case X (OP_DEC, SB):
abort ();
case X (OP_INC, SB):
t = GET_L_REG (rn);
t &= cpu.mask;
r = GET_MEMORY_B (t);
t++;
t = t & cpu.mask;
SET_L_REG (rn, t);
return r;
break;
case X (OP_INC, SW):
t = GET_L_REG (rn);
t &= cpu.mask;
r = GET_MEMORY_W (t);
t += 2;
t = t & cpu.mask;
SET_L_REG (rn, t);
return r;
case X (OP_INC, SL):
t = GET_L_REG (rn);
t &= cpu.mask;
r = GET_MEMORY_L (t);
t += 4;
t = t & cpu.mask;
SET_L_REG (rn, t);
return r;
case X (OP_DISP, SB):
t = GET_L_REG (rn) + abs;
t &= cpu.mask;
return GET_MEMORY_B (t);
case X (OP_DISP, SW):
t = GET_L_REG (rn) + abs;
t &= cpu.mask;
return GET_MEMORY_W (t);
case X (OP_DISP, SL):
t = GET_L_REG (rn) + abs;
t &= cpu.mask;
return GET_MEMORY_L (t);
case X (OP_MEM, SL):
t = GET_MEMORY_L (abs);
t &= cpu.mask;
return t;
case X (OP_MEM, SW):
t = GET_MEMORY_W (abs);
t &= cpu.mask;
return t;
default:
abort ();
}
}
static void
store (arg, n)
ea_type *arg;
int n;
{
int rn = arg->reg;
int abs = arg->literal;
int t;
switch (arg->type)
{
case X (OP_REG, SB):
SET_B_REG (rn, n);
break;
case X (OP_REG, SW):
SET_W_REG (rn, n);
break;
case X (OP_REG, SL):
SET_L_REG (rn, n);
break;
case X (OP_DEC, SB):
t = GET_L_REG (rn) - 1;
t &= cpu.mask;
SET_L_REG (rn, t);
SET_MEMORY_B (t, n);
break;
case X (OP_DEC, SW):
t = (GET_L_REG (rn) - 2) & cpu.mask;
SET_L_REG (rn, t);
SET_MEMORY_W (t, n);
break;
case X (OP_DEC, SL):
t = (GET_L_REG (rn) - 4) & cpu.mask;
SET_L_REG (rn, t);
SET_MEMORY_L (t, n);
break;
case X (OP_DISP, SB):
t = GET_L_REG (rn) + abs;
t &= cpu.mask;
SET_MEMORY_B (t, n);
break;
case X (OP_DISP, SW):
t = GET_L_REG (rn) + abs;
t &= cpu.mask;
SET_MEMORY_W (t, n);
break;
case X (OP_DISP, SL):
t = GET_L_REG (rn) + abs;
t &= cpu.mask;
SET_MEMORY_L (t, n);
break;
default:
abort ();
}
}
static union
{
short int i;
struct
{
char low;
char high;
}
u;
}
littleendian;
static void
init_pointers ()
{
static int init;
if (!init)
{
int i;
init = 1;
littleendian.i = 1;
if (h8300hmode)
memory_size = H8300H_MSIZE;
else
memory_size = H8300_MSIZE;
cpu.memory = (unsigned char *) calloc (sizeof (char), memory_size);
cpu.cache_idx = (unsigned short *) calloc (sizeof (short), memory_size);
cpu.eightbit = (unsigned char *) calloc (sizeof (char), 256);
/* `msize' must be a power of two. */
if ((memory_size & (memory_size - 1)) != 0)
abort ();
cpu.mask = memory_size - 1;
for (i = 0; i < 9; i++)
{
cpu.regs[i] = 0;
}
for (i = 0; i < 8; i++)
{
unsigned char *p = (unsigned char *) (cpu.regs + i);
unsigned char *e = (unsigned char *) (cpu.regs + i + 1);
unsigned short *q = (unsigned short *) (cpu.regs + i);
unsigned short *u = (unsigned short *) (cpu.regs + i + 1);
cpu.regs[i] = 0x00112233;
while (p < e)
{
if (*p == 0x22)
{
breg[i] = p;
}
if (*p == 0x33)
{
breg[i + 8] = p;
}
p++;
}
while (q < u)
{
if (*q == 0x2233)
{
wreg[i] = q;
}
if (*q == 0x0011)
{
wreg[i + 8] = q;
}
q++;
}
cpu.regs[i] = 0;
lreg[i] = &cpu.regs[i];
}
lreg[8] = &cpu.regs[8];
/* Initialize the seg registers. */
if (!cpu.cache)
sim_set_simcache_size (CSIZE);
}
}
static void
control_c (sig, code, scp, addr)
int sig;
int code;
char *scp;
char *addr;
{
cpu.state = SIM_STATE_STOPPED;
cpu.exception = SIGINT;
}
#define C (c != 0)
#define Z (nz == 0)
#define V (v != 0)
#define N (n != 0)
static int
mop (code, bsize, sign)
decoded_inst *code;
int bsize;
int sign;
{
int multiplier;
int multiplicand;
int result;
int n, nz;
if (sign)
{
multiplicand =
bsize ? SEXTCHAR (GET_W_REG (code->dst.reg)) :
SEXTSHORT (GET_W_REG (code->dst.reg));
multiplier =
bsize ? SEXTCHAR (GET_B_REG (code->src.reg)) :
SEXTSHORT (GET_W_REG (code->src.reg));
}
else
{
multiplicand = bsize ? UEXTCHAR (GET_W_REG (code->dst.reg)) :
UEXTSHORT (GET_W_REG (code->dst.reg));
multiplier =
bsize ? UEXTCHAR (GET_B_REG (code->src.reg)) :
UEXTSHORT (GET_W_REG (code->src.reg));
}
result = multiplier * multiplicand;
if (sign)
{
n = result & (bsize ? 0x8000 : 0x80000000);
nz = result & (bsize ? 0xffff : 0xffffffff);
}
if (bsize)
{
SET_W_REG (code->dst.reg, result);
}
else
{
SET_L_REG (code->dst.reg, result);
}
#if 0
return ((n == 1) << 1) | (nz == 1);
#endif
}
#define ONOT(name, how) \
case O(name, SB): \
{ \
int t; \
int hm = 0x80; \
rd = GET_B_REG (code->src.reg); \
how; \
goto shift8; \
} \
case O(name, SW): \
{ \
int t; \
int hm = 0x8000; \
rd = GET_W_REG (code->src.reg); \
how; \
goto shift16; \
} \
case O(name, SL): \
{ \
int t; \
int hm = 0x80000000; \
rd = GET_L_REG (code->src.reg); \
how; \
goto shift32; \
}
#define OSHIFTS(name, how1, how2) \
case O(name, SB): \
{ \
int t; \
int hm = 0x80; \
rd = GET_B_REG (code->src.reg); \
if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \
{ \
how1; \
} \
else \
{ \
how2; \
} \
goto shift8; \
} \
case O(name, SW): \
{ \
int t; \
int hm = 0x8000; \
rd = GET_W_REG (code->src.reg); \
if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \
{ \
how1; \
} \
else \
{ \
how2; \
} \
goto shift16; \
} \
case O(name, SL): \
{ \
int t; \
int hm = 0x80000000; \
rd = GET_L_REG (code->src.reg); \
if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \
{ \
how1; \
} \
else \
{ \
how2; \
} \
goto shift32; \
}
#define OBITOP(name,f, s, op) \
case O(name, SB): \
{ \
int m; \
int b; \
if (f) ea = fetch (&code->dst); \
m=1<< fetch(&code->src); \
op; \
if(s) store (&code->dst,ea); goto next; \
}
int
sim_stop (sd)
SIM_DESC sd;
{
cpu.state = SIM_STATE_STOPPED;
cpu.exception = SIGINT;
return 1;
}
void
sim_resume (sd, step, siggnal)
SIM_DESC sd;
{
static int init1;
int cycles = 0;
int insts = 0;
int tick_start = get_now ();
void (*prev) ();
int poll_count = 0;
int res;
int tmp;
int rd;
int ea;
int bit;
int pc;
int c, nz, v, n;
int oldmask;
init_pointers ();
prev = signal (SIGINT, control_c);
if (step)
{
cpu.state = SIM_STATE_STOPPED;
cpu.exception = SIGTRAP;
}
else
{
cpu.state = SIM_STATE_RUNNING;
cpu.exception = 0;
}
pc = cpu.pc;
/* The PC should never be odd. */
if (pc & 0x1)
abort ();
GETSR ();
oldmask = cpu.mask;
if (!h8300hmode)
cpu.mask = 0xffff;
do
{
int cidx;
decoded_inst *code;
top:
cidx = cpu.cache_idx[pc];
code = cpu.cache + cidx;
#define ALUOP(STORE, NAME, HOW) \
case O(NAME,SB): HOW; if(STORE)goto alu8;else goto just_flags_alu8; \
case O(NAME, SW): HOW; if(STORE)goto alu16;else goto just_flags_alu16; \
case O(NAME,SL): HOW; if(STORE)goto alu32;else goto just_flags_alu32;
#define LOGOP(NAME, HOW) \
case O(NAME,SB): HOW; goto log8;\
case O(NAME, SW): HOW; goto log16;\
case O(NAME,SL): HOW; goto log32;
#if ADEBUG
if (debug)
{
printf ("%x %d %s\n", pc, code->opcode,
code->op ? code->op->name : "**");
}
cpu.stats[code->opcode]++;
#endif
cycles += code->cycles;
insts++;
switch (code->opcode)
{
case 0:
/*
* This opcode is a fake for when we get to an
* instruction which hasnt been compiled
*/
compile (pc);
goto top;
break;
case O (O_SUBX, SB):
rd = fetch (&code->dst);
ea = fetch (&code->src);
ea = -(ea + C);
res = rd + ea;
goto alu8;
case O (O_ADDX, SB):
rd = fetch (&code->dst);
ea = fetch (&code->src);
ea = C + ea;
res = rd + ea;
goto alu8;
#define EA ea = fetch(&code->src);
#define RD_EA ea = fetch(&code->src); rd = fetch(&code->dst);
ALUOP (1, O_SUB, RD_EA;
ea = -ea;
res = rd + ea);
ALUOP (1, O_NEG, EA;
ea = -ea;
rd = 0;
res = rd + ea);
case O (O_ADD, SB):
rd = GET_B_REG (code->dst.reg);
ea = fetch (&code->src);
res = rd + ea;
goto alu8;
case O (O_ADD, SW):
rd = GET_W_REG (code->dst.reg);
ea = fetch (&code->src);
res = rd + ea;
goto alu16;
case O (O_ADD, SL):
rd = GET_L_REG (code->dst.reg);
ea = fetch (&code->src);
res = rd + ea;
goto alu32;
LOGOP (O_AND, RD_EA;
res = rd & ea);
LOGOP (O_OR, RD_EA;
res = rd | ea);
LOGOP (O_XOR, RD_EA;
res = rd ^ ea);
case O (O_MOV_TO_MEM, SB):
res = GET_B_REG (code->src.reg);
goto log8;
case O (O_MOV_TO_MEM, SW):
res = GET_W_REG (code->src.reg);
goto log16;
case O (O_MOV_TO_MEM, SL):
res = GET_L_REG (code->src.reg);
goto log32;
case O (O_MOV_TO_REG, SB):
res = fetch (&code->src);
SET_B_REG (code->dst.reg, res);
goto just_flags_log8;
case O (O_MOV_TO_REG, SW):
res = fetch (&code->src);
SET_W_REG (code->dst.reg, res);
goto just_flags_log16;
case O (O_MOV_TO_REG, SL):
res = fetch (&code->src);
SET_L_REG (code->dst.reg, res);
goto just_flags_log32;
case O (O_ADDS, SL):
SET_L_REG (code->dst.reg,
GET_L_REG (code->dst.reg)
+ code->src.literal);
goto next;
case O (O_SUBS, SL):
SET_L_REG (code->dst.reg,
GET_L_REG (code->dst.reg)
- code->src.literal);
goto next;
case O (O_CMP, SB):
rd = fetch (&code->dst);
ea = fetch (&code->src);
ea = -ea;
res = rd + ea;
goto just_flags_alu8;
case O (O_CMP, SW):
rd = fetch (&code->dst);
ea = fetch (&code->src);
ea = -ea;
res = rd + ea;
goto just_flags_alu16;
case O (O_CMP, SL):
rd = fetch (&code->dst);
ea = fetch (&code->src);
ea = -ea;
res = rd + ea;
goto just_flags_alu32;
case O (O_DEC, SB):
rd = GET_B_REG (code->src.reg);
ea = -1;
res = rd + ea;
SET_B_REG (code->src.reg, res);
goto just_flags_inc8;
case O (O_DEC, SW):
rd = GET_W_REG (code->dst.reg);
ea = -code->src.literal;
res = rd + ea;
SET_W_REG (code->dst.reg, res);
goto just_flags_inc16;
case O (O_DEC, SL):
rd = GET_L_REG (code->dst.reg);
ea = -code->src.literal;
res = rd + ea;
SET_L_REG (code->dst.reg, res);
goto just_flags_inc32;
case O (O_INC, SB):
rd = GET_B_REG (code->src.reg);
ea = 1;
res = rd + ea;
SET_B_REG (code->src.reg, res);
goto just_flags_inc8;
case O (O_INC, SW):
rd = GET_W_REG (code->dst.reg);
ea = code->src.literal;
res = rd + ea;
SET_W_REG (code->dst.reg, res);
goto just_flags_inc16;
case O (O_INC, SL):
rd = GET_L_REG (code->dst.reg);
ea = code->src.literal;
res = rd + ea;
SET_L_REG (code->dst.reg, res);
goto just_flags_inc32;
#define GET_CCR(x) BUILDSR();x = cpu.ccr
case O (O_ANDC, SB):
GET_CCR (rd);
ea = code->src.literal;
res = rd & ea;
goto setc;
case O (O_ORC, SB):
GET_CCR (rd);
ea = code->src.literal;
res = rd | ea;
goto setc;
case O (O_XORC, SB):
GET_CCR (rd);
ea = code->src.literal;
res = rd ^ ea;
goto setc;
case O (O_BRA, SB):
if (1)
goto condtrue;
goto next;
case O (O_BRN, SB):
if (0)
goto condtrue;
goto next;
case O (O_BHI, SB):
if ((C || Z) == 0)
goto condtrue;
goto next;
case O (O_BLS, SB):
if ((C || Z))
goto condtrue;
goto next;
case O (O_BCS, SB):
if ((C == 1))
goto condtrue;
goto next;
case O (O_BCC, SB):
if ((C == 0))
goto condtrue;
goto next;
case O (O_BEQ, SB):
if (Z)
goto condtrue;
goto next;
case O (O_BGT, SB):
if (((Z || (N ^ V)) == 0))
goto condtrue;
goto next;
case O (O_BLE, SB):
if (((Z || (N ^ V)) == 1))
goto condtrue;
goto next;
case O (O_BGE, SB):
if ((N ^ V) == 0)
goto condtrue;
goto next;
case O (O_BLT, SB):
if ((N ^ V))
goto condtrue;
goto next;
case O (O_BMI, SB):
if ((N))
goto condtrue;
goto next;
case O (O_BNE, SB):
if ((Z == 0))
goto condtrue;
goto next;
case O (O_BPL, SB):
if (N == 0)
goto condtrue;
goto next;
case O (O_BVC, SB):
if ((V == 0))
goto condtrue;
goto next;
case O (O_BVS, SB):
if ((V == 1))
goto condtrue;
goto next;
case O (O_SYSCALL, SB):
{
char c = cpu.regs[2];
sim_callback->write_stdout (sim_callback, &c, 1);
}
goto next;
ONOT (O_NOT, rd = ~rd; v = 0;);
OSHIFTS (O_SHLL,
c = rd & hm; v = 0; rd <<= 1,
c = rd & (hm >> 1); v = 0; rd <<= 2);
OSHIFTS (O_SHLR,
c = rd & 1; v = 0; rd = (unsigned int) rd >> 1,
c = rd & 2; v = 0; rd = (unsigned int) rd >> 2);
OSHIFTS (O_SHAL,
c = rd & hm; v = (rd & hm) != ((rd & (hm >> 1)) << 1); rd <<= 1,
c = rd & (hm >> 1); v = (rd & (hm >> 1)) != ((rd & (hm >> 2)) << 2); rd <<= 2);
OSHIFTS (O_SHAR,
t = rd & hm; c = rd & 1; v = 0; rd >>= 1; rd |= t,
t = rd & hm; c = rd & 2; v = 0; rd >>= 2; rd |= t | t >> 1 );
OSHIFTS (O_ROTL,
c = rd & hm; v = 0; rd <<= 1; rd |= C,
c = rd & hm; v = 0; rd <<= 1; rd |= C; c = rd & hm; rd <<= 1; rd |= C);
OSHIFTS (O_ROTR,
c = rd & 1; v = 0; rd = (unsigned int) rd >> 1; if (c) rd |= hm,
c = rd & 1; v = 0; rd = (unsigned int) rd >> 1; if (c) rd |= hm; c = rd & 1; rd = (unsigned int) rd >> 1; if (c) rd |= hm);
OSHIFTS (O_ROTXL,
t = rd & hm; rd <<= 1; rd |= C; c = t; v = 0,
t = rd & hm; rd <<= 1; rd |= C; c = t; v = 0; t = rd & hm; rd <<= 1; rd |= C; c = t);
OSHIFTS (O_ROTXR,
t = rd & 1; rd = (unsigned int) rd >> 1; if (C) rd |= hm; c = t; v = 0,
t = rd & 1; rd = (unsigned int) rd >> 1; if (C) rd |= hm; c = t; v = 0; t = rd & 1; rd = (unsigned int) rd >> 1; if (C) rd |= hm; c = t);
case O (O_JMP, SB):
{
pc = fetch (&code->src);
goto end;
}
case O (O_JSR, SB):
{
int tmp;
pc = fetch (&code->src);
call:
tmp = cpu.regs[7];
if (h8300hmode)
{
tmp -= 4;
SET_MEMORY_L (tmp, code->next_pc);
}
else
{
tmp -= 2;
SET_MEMORY_W (tmp, code->next_pc);
}
cpu.regs[7] = tmp;
goto end;
}
case O (O_BSR, SB):
pc = code->src.literal;
goto call;
case O (O_RTS, SN):
{
int tmp;
tmp = cpu.regs[7];
if (h8300hmode)
{
pc = GET_MEMORY_L (tmp);
tmp += 4;
}
else
{
pc = GET_MEMORY_W (tmp);
tmp += 2;
}
cpu.regs[7] = tmp;
goto end;
}
case O (O_ILL, SB):
cpu.state = SIM_STATE_STOPPED;
cpu.exception = SIGILL;
goto end;
case O (O_SLEEP, SN):
/* FIXME: Doesn't this break for breakpoints when r0
contains just the right (er, wrong) value? */
cpu.state = SIM_STATE_STOPPED;
/* The format of r0 is defined by target newlib. Expand
the macros here instead of looking for .../sys/wait.h. */
#define SIM_WIFEXITED(v) (((v) & 0xff) == 0)
#define SIM_WIFSIGNALED(v) (((v) & 0x7f) > 0 && (((v) & 0x7f) < 0x7f))
if (! SIM_WIFEXITED (cpu.regs[0]) && SIM_WIFSIGNALED (cpu.regs[0]))
cpu.exception = SIGILL;
else
cpu.exception = SIGTRAP;
goto end;
case O (O_BPT, SN):
cpu.state = SIM_STATE_STOPPED;
cpu.exception = SIGTRAP;
goto end;
OBITOP (O_BNOT, 1, 1, ea ^= m);
OBITOP (O_BTST, 1, 0, nz = ea & m);
OBITOP (O_BCLR, 1, 1, ea &= ~m);
OBITOP (O_BSET, 1, 1, ea |= m);
OBITOP (O_BLD, 1, 0, c = ea & m);
OBITOP (O_BILD, 1, 0, c = !(ea & m));
OBITOP (O_BST, 1, 1, ea &= ~m;
if (C) ea |= m);
OBITOP (O_BIST, 1, 1, ea &= ~m;
if (!C) ea |= m);
OBITOP (O_BAND, 1, 0, c = (ea & m) && C);
OBITOP (O_BIAND, 1, 0, c = !(ea & m) && C);
OBITOP (O_BOR, 1, 0, c = (ea & m) || C);
OBITOP (O_BIOR, 1, 0, c = !(ea & m) || C);
OBITOP (O_BXOR, 1, 0, c = (ea & m) != C);
OBITOP (O_BIXOR, 1, 0, c = !(ea & m) != C);
#define MOP(bsize, signed) \
mop (code, bsize, signed); \
goto next;
case O (O_MULS, SB):
MOP (1, 1);
break;
case O (O_MULS, SW):
MOP (0, 1);
break;
case O (O_MULU, SB):
MOP (1, 0);
break;
case O (O_MULU, SW):
MOP (0, 0);
break;
case O (O_DIVU, SB):
{
rd = GET_W_REG (code->dst.reg);
ea = GET_B_REG (code->src.reg);
if (ea)
{
tmp = (unsigned) rd % ea;
rd = (unsigned) rd / ea;
}
SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
n = ea & 0x80;
nz = ea & 0xff;
goto next;
}
case O (O_DIVU, SW):
{
rd = GET_L_REG (code->dst.reg);
ea = GET_W_REG (code->src.reg);
n = ea & 0x8000;
nz = ea & 0xffff;
if (ea)
{
tmp = (unsigned) rd % ea;
rd = (unsigned) rd / ea;
}
SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
goto next;
}
case O (O_DIVS, SB):
{
rd = SEXTSHORT (GET_W_REG (code->dst.reg));
ea = SEXTCHAR (GET_B_REG (code->src.reg));
if (ea)
{
tmp = (int) rd % (int) ea;
rd = (int) rd / (int) ea;
n = rd & 0x8000;
nz = 1;
}
else
nz = 0;
SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
goto next;
}
case O (O_DIVS, SW):
{
rd = GET_L_REG (code->dst.reg);
ea = SEXTSHORT (GET_W_REG (code->src.reg));
if (ea)
{
tmp = (int) rd % (int) ea;
rd = (int) rd / (int) ea;
n = rd & 0x80000000;
nz = 1;
}
else
nz = 0;
SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
goto next;
}
case O (O_EXTS, SW):
rd = GET_B_REG (code->src.reg + 8) & 0xff; /* Yes, src, not dst. */
ea = rd & 0x80 ? -256 : 0;
res = rd + ea;
goto log16;
case O (O_EXTS, SL):
rd = GET_W_REG (code->src.reg) & 0xffff;
ea = rd & 0x8000 ? -65536 : 0;
res = rd + ea;
goto log32;
case O (O_EXTU, SW):
rd = GET_B_REG (code->src.reg + 8) & 0xff;
ea = 0;
res = rd + ea;
goto log16;
case O (O_EXTU, SL):
rd = GET_W_REG (code->src.reg) & 0xffff;
ea = 0;
res = rd + ea;
goto log32;
case O (O_NOP, SN):
goto next;
case O (O_STM, SL):
{
int nregs, firstreg, i;
nregs = GET_MEMORY_B (pc + 1);
nregs >>= 4;
nregs &= 0xf;
firstreg = GET_MEMORY_B (pc + 3);
firstreg &= 0xf;
for (i = firstreg; i <= firstreg + nregs; i++)
{
cpu.regs[7] -= 4;
SET_MEMORY_L (cpu.regs[7], cpu.regs[i]);
}
}
goto next;
case O (O_LDM, SL):
{
int nregs, firstreg, i;
nregs = GET_MEMORY_B (pc + 1);
nregs >>= 4;
nregs &= 0xf;
firstreg = GET_MEMORY_B (pc + 3);
firstreg &= 0xf;
for (i = firstreg; i >= firstreg - nregs; i--)
{
cpu.regs[i] = GET_MEMORY_L (cpu.regs[7]);
cpu.regs[7] += 4;
}
}
goto next;
default:
cpu.state = SIM_STATE_STOPPED;
cpu.exception = SIGILL;
goto end;
}
abort ();
setc:
cpu.ccr = res;
GETSR ();
goto next;
condtrue:
/* When a branch works */
pc = code->src.literal;
goto end;
/* Set the cond codes from res */
bitop:
/* Set the flags after an 8 bit inc/dec operation */
just_flags_inc8:
n = res & 0x80;
nz = res & 0xff;
v = (rd & 0x7f) == 0x7f;
goto next;
/* Set the flags after an 16 bit inc/dec operation */
just_flags_inc16:
n = res & 0x8000;
nz = res & 0xffff;
v = (rd & 0x7fff) == 0x7fff;
goto next;
/* Set the flags after an 32 bit inc/dec operation */
just_flags_inc32:
n = res & 0x80000000;
nz = res & 0xffffffff;
v = (rd & 0x7fffffff) == 0x7fffffff;
goto next;
shift8:
/* Set flags after an 8 bit shift op, carry,overflow set in insn */
n = (rd & 0x80);
nz = rd & 0xff;
SET_B_REG (code->src.reg, rd);
goto next;
shift16:
/* Set flags after an 16 bit shift op, carry,overflow set in insn */
n = (rd & 0x8000);
nz = rd & 0xffff;
SET_W_REG (code->src.reg, rd);
goto next;
shift32:
/* Set flags after an 32 bit shift op, carry,overflow set in insn */
n = (rd & 0x80000000);
nz = rd & 0xffffffff;
SET_L_REG (code->src.reg, rd);
goto next;
log32:
store (&code->dst, res);
just_flags_log32:
/* flags after a 32bit logical operation */
n = res & 0x80000000;
nz = res & 0xffffffff;
v = 0;
goto next;
log16:
store (&code->dst, res);
just_flags_log16:
/* flags after a 16bit logical operation */
n = res & 0x8000;
nz = res & 0xffff;
v = 0;
goto next;
log8:
store (&code->dst, res);
just_flags_log8:
n = res & 0x80;
nz = res & 0xff;
v = 0;
goto next;
alu8:
SET_B_REG (code->dst.reg, res);
just_flags_alu8:
n = res & 0x80;
nz = res & 0xff;
c = (res & 0x100);
switch (code->opcode / 4)
{
case O_ADD:
v = ((rd & 0x80) == (ea & 0x80)
&& (rd & 0x80) != (res & 0x80));
break;
case O_SUB:
case O_CMP:
v = ((rd & 0x80) != (-ea & 0x80)
&& (rd & 0x80) != (res & 0x80));
break;
case O_NEG:
v = (rd == 0x80);
break;
}
goto next;
alu16:
SET_W_REG (code->dst.reg, res);
just_flags_alu16:
n = res & 0x8000;
nz = res & 0xffff;
c = (res & 0x10000);
switch (code->opcode / 4)
{
case O_ADD:
v = ((rd & 0x8000) == (ea & 0x8000)
&& (rd & 0x8000) != (res & 0x8000));
break;
case O_SUB:
case O_CMP:
v = ((rd & 0x8000) != (-ea & 0x8000)
&& (rd & 0x8000) != (res & 0x8000));
break;
case O_NEG:
v = (rd == 0x8000);
break;
}
goto next;
alu32:
SET_L_REG (code->dst.reg, res);
just_flags_alu32:
n = res & 0x80000000;
nz = res & 0xffffffff;
switch (code->opcode / 4)
{
case O_ADD:
v = ((rd & 0x80000000) == (ea & 0x80000000)
&& (rd & 0x80000000) != (res & 0x80000000));
c = ((unsigned) res < (unsigned) rd) || ((unsigned) res < (unsigned) ea);
break;
case O_SUB:
case O_CMP:
v = ((rd & 0x80000000) != (-ea & 0x80000000)
&& (rd & 0x80000000) != (res & 0x80000000));
c = (unsigned) rd < (unsigned) -ea;
break;
case O_NEG:
v = (rd == 0x80000000);
c = res != 0;
break;
}
goto next;
next:;
pc = code->next_pc;
end:
;
#if 0
if (cpu.regs[8])
abort ();
#endif
if (--poll_count < 0)
{
poll_count = POLL_QUIT_INTERVAL;
if ((*sim_callback->poll_quit) != NULL
&& (*sim_callback->poll_quit) (sim_callback))
sim_stop (sd);
}
}
while (cpu.state == SIM_STATE_RUNNING);
cpu.ticks += get_now () - tick_start;
cpu.cycles += cycles;
cpu.insts += insts;
cpu.pc = pc;
BUILDSR ();
cpu.mask = oldmask;
signal (SIGINT, prev);
}
int
sim_trace (sd)
SIM_DESC sd;
{
/* FIXME: Unfinished. */
abort ();
}
int
sim_write (sd, addr, buffer, size)
SIM_DESC sd;
SIM_ADDR addr;
unsigned char *buffer;
int size;
{
int i;
init_pointers ();
if (addr < 0)
return 0;
for (i = 0; i < size; i++)
{
if (addr < memory_size)
{
cpu.memory[addr + i] = buffer[i];
cpu.cache_idx[addr + i] = 0;
}
else
cpu.eightbit[(addr + i) & 0xff] = buffer[i];
}
return size;
}
int
sim_read (sd, addr, buffer, size)
SIM_DESC sd;
SIM_ADDR addr;
unsigned char *buffer;
int size;
{
init_pointers ();
if (addr < 0)
return 0;
if (addr < memory_size)
memcpy (buffer, cpu.memory + addr, size);
else
memcpy (buffer, cpu.eightbit + (addr & 0xff), size);
return size;
}
#define R0_REGNUM 0
#define R1_REGNUM 1
#define R2_REGNUM 2
#define R3_REGNUM 3
#define R4_REGNUM 4
#define R5_REGNUM 5
#define R6_REGNUM 6
#define R7_REGNUM 7
#define SP_REGNUM R7_REGNUM /* Contains address of top of stack */
#define FP_REGNUM R6_REGNUM /* Contains address of executing
* stack frame */
#define CCR_REGNUM 8 /* Contains processor status */
#define PC_REGNUM 9 /* Contains program counter */
#define CYCLE_REGNUM 10
#define INST_REGNUM 11
#define TICK_REGNUM 12
int
sim_store_register (sd, rn, value, length)
SIM_DESC sd;
int rn;
unsigned char *value;
int length;
{
int longval;
int shortval;
int intval;
longval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
shortval = (value[0] << 8) | (value[1]);
intval = h8300hmode ? longval : shortval;
init_pointers ();
switch (rn)
{
case PC_REGNUM:
cpu.pc = intval;
break;
default:
abort ();
case R0_REGNUM:
case R1_REGNUM:
case R2_REGNUM:
case R3_REGNUM:
case R4_REGNUM:
case R5_REGNUM:
case R6_REGNUM:
case R7_REGNUM:
cpu.regs[rn] = intval;
break;
case CCR_REGNUM:
cpu.ccr = intval;
break;
case CYCLE_REGNUM:
cpu.cycles = longval;
break;
case INST_REGNUM:
cpu.insts = longval;
break;
case TICK_REGNUM:
cpu.ticks = longval;
break;
}
return -1;
}
int
sim_fetch_register (sd, rn, buf, length)
SIM_DESC sd;
int rn;
unsigned char *buf;
int length;
{
int v;
int longreg = 0;
init_pointers ();
switch (rn)
{
default:
abort ();
case 8:
v = cpu.ccr;
break;
case 9:
v = cpu.pc;
break;
case R0_REGNUM:
case R1_REGNUM:
case R2_REGNUM:
case R3_REGNUM:
case R4_REGNUM:
case R5_REGNUM:
case R6_REGNUM:
case R7_REGNUM:
v = cpu.regs[rn];
break;
case 10:
v = cpu.cycles;
longreg = 1;
break;
case 11:
v = cpu.ticks;
longreg = 1;
break;
case 12:
v = cpu.insts;
longreg = 1;
break;
}
if (h8300hmode || longreg)
{
buf[0] = v >> 24;
buf[1] = v >> 16;
buf[2] = v >> 8;
buf[3] = v >> 0;
}
else
{
buf[0] = v >> 8;
buf[1] = v;
}
return -1;
}
void
sim_stop_reason (sd, reason, sigrc)
SIM_DESC sd;
enum sim_stop *reason;
int *sigrc;
{
#if 0 /* FIXME: This should work but we can't use it.
grep for SLEEP above. */
switch (cpu.state)
{
case SIM_STATE_EXITED : *reason = sim_exited; break;
case SIM_STATE_SIGNALLED : *reason = sim_signalled; break;
case SIM_STATE_STOPPED : *reason = sim_stopped; break;
default : abort ();
}
#else
*reason = sim_stopped;
#endif
*sigrc = cpu.exception;
}
/* FIXME: Rename to sim_set_mem_size. */
void
sim_size (n)
int n;
{
/* Memory size is fixed. */
}
void
sim_set_simcache_size (n)
{
if (cpu.cache)
free (cpu.cache);
if (n < 2)
n = 2;
cpu.cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n);
memset (cpu.cache, 0, sizeof (decoded_inst) * n);
cpu.csize = n;
}
void
sim_info (sd, verbose)
SIM_DESC sd;
int verbose;
{
double timetaken = (double) cpu.ticks / (double) now_persec ();
double virttime = cpu.cycles / 10.0e6;
(*sim_callback->printf_filtered) (sim_callback,
"\n\n#instructions executed %10d\n",
cpu.insts);
(*sim_callback->printf_filtered) (sim_callback,
"#cycles (v approximate) %10d\n",
cpu.cycles);
(*sim_callback->printf_filtered) (sim_callback,
"#real time taken %10.4f\n",
timetaken);
(*sim_callback->printf_filtered) (sim_callback,
"#virtual time taked %10.4f\n",
virttime);
if (timetaken != 0.0)
(*sim_callback->printf_filtered) (sim_callback,
"#simulation ratio %10.4f\n",
virttime / timetaken);
(*sim_callback->printf_filtered) (sim_callback,
"#compiles %10d\n",
cpu.compiles);
(*sim_callback->printf_filtered) (sim_callback,
"#cache size %10d\n",
cpu.csize);
#ifdef ADEBUG
/* This to be conditional on `what' (aka `verbose'),
however it was never passed as non-zero. */
if (1)
{
int i;
for (i = 0; i < O_LAST; i++)
{
if (cpu.stats[i])
(*sim_callback->printf_filtered) (sim_callback,
"%d: %d\n", i, cpu.stats[i]);
}
}
#endif
}
/* Indicate whether the cpu is an H8/300 or H8/300H.
FLAG is non-zero for the H8/300H. */
void
set_h8300h (flag)
int flag;
{
/* FIXME: Much of the code in sim_load can be moved to sim_open.
This function being replaced by a sim_open:ARGV configuration
option. */
h8300hmode = flag;
}
SIM_DESC
sim_open (kind, ptr, abfd, argv)
SIM_OPEN_KIND kind;
struct host_callback_struct *ptr;
struct _bfd *abfd;
char **argv;
{
/* FIXME: Much of the code in sim_load can be moved here. */
sim_kind = kind;
myname = argv[0];
sim_callback = ptr;
/* Fudge our descriptor. */
return (SIM_DESC) 1;
}
void
sim_close (sd, quitting)
SIM_DESC sd;
int quitting;
{
/* Nothing to do. */
}
/* Called by gdb to load a program into memory. */
SIM_RC
sim_load (sd, prog, abfd, from_tty)
SIM_DESC sd;
char *prog;
bfd *abfd;
int from_tty;
{
bfd *prog_bfd;
/* FIXME: The code below that sets a specific variant of the H8/300
being simulated should be moved to sim_open(). */
/* See if the file is for the H8/300 or H8/300H. */
/* ??? This may not be the most efficient way. The z8k simulator
does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
if (abfd != NULL)
prog_bfd = abfd;
else
prog_bfd = bfd_openr (prog, "coff-h8300");
if (prog_bfd != NULL)
{
/* Set the cpu type. We ignore failure from bfd_check_format
and bfd_openr as sim_load_file checks too. */
if (bfd_check_format (prog_bfd, bfd_object))
{
unsigned long mach = bfd_get_mach (prog_bfd);
set_h8300h (mach == bfd_mach_h8300h
|| mach == bfd_mach_h8300s);
}
}
/* If we're using gdb attached to the simulator, then we have to
reallocate memory for the simulator.
When gdb first starts, it calls fetch_registers (among other
functions), which in turn calls init_pointers, which allocates
simulator memory.
The problem is when we do that, we don't know whether we're
debugging an H8/300 or H8/300H program.
This is the first point at which we can make that determination,
so we just reallocate memory now; this will also allow us to handle
switching between H8/300 and H8/300H programs without exiting
gdb. */
if (h8300hmode)
memory_size = H8300H_MSIZE;
else
memory_size = H8300_MSIZE;
if (cpu.memory)
free (cpu.memory);
if (cpu.cache_idx)
free (cpu.cache_idx);
if (cpu.eightbit)
free (cpu.eightbit);
cpu.memory = (unsigned char *) calloc (sizeof (char), memory_size);
cpu.cache_idx = (unsigned short *) calloc (sizeof (short), memory_size);
cpu.eightbit = (unsigned char *) calloc (sizeof (char), 256);
/* `msize' must be a power of two. */
if ((memory_size & (memory_size - 1)) != 0)
abort ();
cpu.mask = memory_size - 1;
if (sim_load_file (sd, myname, sim_callback, prog, prog_bfd,
sim_kind == SIM_OPEN_DEBUG,
0, sim_write)
== NULL)
{
/* Close the bfd if we opened it. */
if (abfd == NULL && prog_bfd != NULL)
bfd_close (prog_bfd);
return SIM_RC_FAIL;
}
/* Close the bfd if we opened it. */
if (abfd == NULL && prog_bfd != NULL)
bfd_close (prog_bfd);
return SIM_RC_OK;
}
SIM_RC
sim_create_inferior (sd, abfd, argv, env)
SIM_DESC sd;
struct _bfd *abfd;
char **argv;
char **env;
{
if (abfd != NULL)
cpu.pc = bfd_get_start_address (abfd);
else
cpu.pc = 0;
return SIM_RC_OK;
}
void
sim_do_command (sd, cmd)
SIM_DESC sd;
char *cmd;
{
(*sim_callback->printf_filtered) (sim_callback,
"This simulator does not accept any commands.\n");
}
void
sim_set_callbacks (ptr)
struct host_callback_struct *ptr;
{
sim_callback = ptr;
}
|