summaryrefslogtreecommitdiff
path: root/cpu/mep-core.cpu
blob: accf00ef838eb2fc79ce3e350d8982fbbab8af05 (plain)
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
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
; Toshiba MeP Media Engine architecture description.  -*- Scheme -*-
; Copyright 2011 Free Software Foundation, Inc.
;
; Contributed by Red Hat Inc;
;
; This file is part of the GNU Binutils.
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
; MA 02110-1301, USA.

(include "simplify.inc")

(define-pmacro isa-enum ()
  (isas mep 
; begin-isa-enum
	ext_core1 ext_cop1_16 ext_cop1_32 ext_cop1_48 ext_cop1_64
; end-isa-enum
  )
)

(define-arch
  (name mep)
  (comment "Toshiba MeP Media Engine")
  (insn-lsb0? #f) ;; work around cgen limitation
  (machs mep h1 c5)
  isa-enum
)

(define-isa
  (name mep)
  (comment "MeP core instruction set")
  (default-insn-word-bitsize 32)
  (default-insn-bitsize 32)
  (base-insn-bitsize 32)
)

; begin-isas
(define-isa
  (name ext_core1)
  (comment "MeP core extension instruction set")
  (default-insn-word-bitsize 32)
  (default-insn-bitsize 32)
  (base-insn-bitsize 32)
)

(define-isa
  (name ext_cop1_16)
  (comment "MeP coprocessor instruction set")
  (default-insn-word-bitsize 32)
  (default-insn-bitsize 32)
  (base-insn-bitsize 32)
)

(define-isa
  (name ext_cop1_32)
  (comment "MeP coprocessor instruction set")
  (default-insn-word-bitsize 32)
  (default-insn-bitsize 32)
  (base-insn-bitsize 32)
)

(define-isa
  (name ext_cop1_48)
  (comment "MeP coprocessor instruction set")
  (default-insn-word-bitsize 32)
  (default-insn-bitsize 32)
  (base-insn-bitsize 32)
)

(define-isa
  (name ext_cop1_64)
  (comment "MeP coprocessor instruction set")
  (default-insn-word-bitsize 32)
  (default-insn-bitsize 32)
  (base-insn-bitsize 32)
)

(define-pmacro all-mep-isas () (ISA mep,ext_core1,ext_cop1_16,ext_cop1_32,ext_cop1_48,ext_cop1_64))

(define-pmacro all-mep-core-isas () (ISA mep,ext_core1,ext_cop1_32))

(define-pmacro all-core-isa-list () mep,ext_core1)
; end-isas

(define-cpu
  (name mepf)
  (comment "MeP family")
  (endian either)
  (insn-chunk-bitsize 16)
  (word-bitsize 32)
)

(define-mach
  (name mep)
  (comment "MeP media engine")
  (cpu mepf)
  isa-enum
)

(define-mach
  (name h1)
  (comment "H1 media engine")
  (cpu mepf)
  isa-enum
)

(define-mach
  (name c5)
  (comment "C5 media engine")
  (cpu mepf)
  isa-enum
)

(define-model
  (name mep)
  (comment "MeP media engine processor")
  (mach c5) ; mach gets changed by MeP-Integrator

  (unit u-exec "execution unit" ()
	1 1 ; issue done
	() () () ())

  ; Branch unit
  (unit u-branch "Branch Unit" ()
	0 0 ; issue done
	() ; state
	() ; inputs
	((pc)) ; outputs
	() ; profile action (default)
	)

  ; Multiply unit
  (unit u-multiply "Multiply Unit" ()
	0 0 ; issue done
	() ; state
	() ; inputs
	() ; outputs
	() ; profile action (default)
	)

  ; Divide unit
  (unit u-divide "Divide Unit" ()
	0 0 ; issue done
	() ; state
	() ; inputs
	() ; outputs
	() ; profile action (default)
	)

  ; Stcb unit
  (unit u-stcb "stcb Unit" ()
	0 0 ; issue done
	() ; state
	() ; inputs
	() ; outputs
	() ; profile action (default)
	)

  ; Ldcb unit
  (unit u-ldcb "ldcb Unit" ()
	0 0 ; issue done
	() ; state
	() ; inputs
	() ; outputs
	() ; profile action (default)
	)

  ; Load gpr unit
  (unit u-load-gpr "Load into GPR Unit" ()
	0 0 ; issue done
	() ; state
	() ; inputs
	((loadreg INT -1)) ; outputs
	() ; profile action (default)
	)

  (unit u-ldcb-gpr "Ldcb into GPR Unit" ()
	0 0 ; issue done
	() ; state
	() ; inputs
	((loadreg INT -1)) ; outputs
	() ; profile action (default)
	)

  ; Multiply into GPR unit
  (unit u-mul-gpr "Multiply into GPR Unit" ()
	0 0 ; issue done
	() ; state
	() ; inputs
	((resultreg INT -1)) ; outputs
	() ; profile action (default)
	)

  ; Use gpr unit -- stalls if GPR not ready
  (unit u-use-gpr "Use GPR Unit" ()
	0 0 ; issue done
	() ; state
	((usereg INT -1)) ; inputs
	() ; outputs
	() ; profile action (default)
	)

  ; Use ctrl-reg unit -- stalls if CTRL-REG not ready
  (unit u-use-ctrl-reg "Use CTRL-REG Unit" ()
	0 0 ; issue done
	() ; state
	((usereg INT -1)) ; inputs
	() ; outputs
	() ; profile action (default)
	)

  ; Store ctrl-reg unit -- stalls if CTRL-REG not ready
  (unit u-store-ctrl-reg "Store CTRL-REG Unit" ()
	0 0 ; issue done
	() ; state
	() ; inputs
	((storereg INT -1)) ; outputs
	() ; profile action (default)
	)
)

; Hardware elements.

(dnh h-pc "program counter" (PC PROFILE all-mep-isas) (pc) () () ())

(define-hardware
  (name h-gpr)
  (comment "General purpose registers")
  (attrs all-mep-isas CACHE-ADDR PROFILE)
  (type register SI (16))
  (indices keyword "$"
	   (("0" 0) ("1" 1) ("2" 2) ("3" 3) ("4" 4) ("5" 5)
	    ("6" 6) ("7" 7) ("8" 8) ("9" 9) ("10" 10) ("11" 11)
	    ; "$8" is the preferred name for register 8, but "$tp", "$gp"
	    ; and "$sp" are preferred for their respective registers.
	    (fp  8) (tp 13) (gp 14) (sp 15)
	    ("12" 12) ("13" 13) ("14" 14) ("15" 15)))
)

(define-hardware
  (name h-csr)
  (comment "Control/special registers")
  (attrs all-mep-isas PROFILE)
  (type register SI (32))
  (indices keyword "$"
	   ((pc 0)   (lp 1)   (sar 2)   (rpb  4) (rpe 5)   (rpc 6)
	    (hi 7)   (lo 8)   (mb0 12)  (me0 13) (mb1 14)  (me1 15)
	    (psw 16) (id 17)  (tmp 18)  (epc 19) (exc 20)  (cfg 21)
	    (npc 23) (dbg 24) (depc 25) (opt 26) (rcfg 27) (ccfg 28)
; begin-extra-csr-registers
	    (vid 22)
; end-extra-csr-registers
  ))
  (get (index) (c-call SI "cgen_get_csr_value" index))
  (set (index newval) (c-call VOID "cgen_set_csr_value" index newval))
)

(define-pmacro (-reg-pair n) ((.sym n) n))
(define-hardware
  (name h-cr64)
  (comment "64-bit coprocessor registers")
  (attrs all-mep-isas)
  ; This assumes that the data path of the co-pro is 64 bits.
  (type register DI (32))
  (indices keyword "$c" (.map -reg-pair (.iota 32)))
  (set (index newval) (c-call VOID "h_cr64_queue_set" index newval))
)
(define-hardware
  (name h-cr64-w)
  (comment "64-bit coprocessor registers, pending writes")
  (attrs all-mep-isas)
  ; This assumes that the data path of the co-pro is 64 bits.
  (type register DI (32))
)

(define-hardware
  (name h-cr)
  (comment "32-bit coprocessor registers")
  (attrs all-mep-isas VIRTUAL)
  (type register SI (32))
  (indices keyword "$c" (.map -reg-pair (.iota 32)))
  (set (index newval) (c-call VOID "h_cr64_set" index (ext DI newval)))
  (get (index) (trunc SI (c-call DI "h_cr64_get" index)))
)

;; Given a coprocessor control register number N, expand to a
;; name/index pair: ($ccrN N)
(define-pmacro (-ccr-reg-pair n) ((.sym "$ccr" n) n))

(define-hardware
  (name h-ccr)
  (comment "Coprocessor control registers")
  (attrs all-mep-isas)
  (type register SI (64))
  (indices keyword "" (.map -ccr-reg-pair (.iota 64)))
  (set (index newval) (c-call VOID "h_ccr_queue_set" index newval))
)
(define-hardware
  (name h-ccr-w)
  (comment "Coprocessor control registers, pending writes")
  (attrs all-mep-isas)
  (type register SI (64))
)


; Instruction fields.  Bit numbering reversed.

; Conventions:
;
; N = number of bits in value
; A = alignment (2 or 4, omit for 1)
; B = leftmost (i.e. closest to zero) bit position
;
; -- Generic Fields (f-*) --
; N		number of bits in *value* (1-24)
; [us]		signed vs unsigned
; B		position of left-most bit (4-16)
; aA		opt. alignment (2=drop 1 lsb, 4=drop 2 lsbs, etc)
; n		opt. for noncontiguous fields
; f-foo-{hi,lo}	msb/lsb parts of field f-foo
;
; -- Operands --
; pcrelNaA	PC-relative branch target (signed)
; pcabsNaA	Absolute branch target (unsigned)
;
; [us]dispNaA	[un]signed displacement
; [us]immN	[un]signed immediate value
; addrNaA	absolute address (unsigned)
;
; Additional prefixes may be used for special cases.

(dnf f-major   "major opcode"            (all-mep-core-isas)    0  4)

(dnf f-rn      "register n"              (all-mep-core-isas)    4  4)
(dnf f-rn3     "register 0-7"            (all-mep-core-isas)    5  3)
(dnf f-rm      "register m"              (all-mep-core-isas)    8  4)
(dnf f-rl      "register l"              (all-mep-core-isas)   12  4)
(dnf f-sub2    "sub opcode (2 bits)"     (all-mep-core-isas)   14  2)
(dnf f-sub3    "sub opcode (3 bits)"     (all-mep-core-isas)   13  3)
(dnf f-sub4    "sub opcode (4 bits)"     (all-mep-core-isas)   12  4)
(dnf f-ext     "extended field"          (all-mep-core-isas)   16  8)
(dnf f-ext4    "extended field 16:4"     (all-mep-core-isas)   16  4)
(dnf f-ext62   "extended field 20:2"     (all-mep-core-isas)   20  2)
(dnf f-crn     "copro register n"        (all-mep-core-isas)    4  4)

(df f-csrn-hi "cr hi 1u15" (all-mep-core-isas) 15 1 UINT #f #f)
(df f-csrn-lo "cr lo 4u8"  (all-mep-core-isas)  8 4 UINT #f #f)
(define-multi-ifield
  (name f-csrn)
  (comment "control reg")
  (attrs all-mep-core-isas)
  (mode UINT)
  (subfields f-csrn-hi f-csrn-lo)
  (insert (sequence ()
		    (set (ifield f-csrn-lo) (and (ifield f-csrn) #xf))
		    (set (ifield f-csrn-hi) (srl (ifield f-csrn) 4))))
  (extract (set (ifield f-csrn)
		(or (sll (ifield f-csrn-hi) 4) (ifield f-csrn-lo))))
  )

(df f-crnx-hi "crx hi 1u28" (all-mep-core-isas) 28 1 UINT #f #f)
(df f-crnx-lo "crx lo 4u4"  (all-mep-core-isas)  4 4 UINT #f #f)
(define-multi-ifield
  (name f-crnx)
  (comment "copro register n (0-31)")
  (attrs all-mep-core-isas)
  (mode UINT)
  (subfields f-crnx-hi f-crnx-lo)
  (insert (sequence ()
		    (set (ifield f-crnx-lo) (and (ifield f-crnx) #xf))
		    (set (ifield f-crnx-hi) (srl (ifield f-crnx) 4))))
  (extract (set (ifield f-crnx)
		(or (sll (ifield f-crnx-hi) 4) (ifield f-crnx-lo))))
  )

; Miscellaneous fields.

(define-pmacro (dnfb n)
  (dnf (.sym f- n) (.str "bit " n) (all-mep-isas) n 1))

; Define small fields used throughout the instruction set description.
; Each field (eg. `f-N') is at single bit field at position N.

(dnfb  0)
(dnfb  1)
(dnfb  2)
(dnfb  3)
(dnfb  4)
(dnfb  5)
(dnfb  6)
(dnfb  7)
(dnfb  8)
(dnfb  9)
(dnfb  10)
(dnfb  11)
(dnfb  12)
(dnfb  13)
(dnfb  14)
(dnfb  15)
(dnfb  16)
(dnfb  17)
(dnfb  18)
(dnfb  19)
(dnfb  20)
(dnfb  21)
(dnfb  22)
(dnfb  23)
(dnfb  24)
(dnfb  25)
(dnfb  26)
(dnfb  27)
(dnfb  28)
(dnfb  29)
(dnfb  30)
(dnfb  31)

; Branch/Jump target addresses

(df f-8s8a2 "pc-rel addr (8 bits)"    (all-mep-core-isas PCREL-ADDR)  8  7 INT
    ((value pc) (sra SI (sub SI value    pc) 1))
    ((value pc) (add SI (sll SI value 1) pc)))

(df f-12s4a2 "pc-rel addr (12 bits)"  (all-mep-core-isas PCREL-ADDR)  4 11 INT
    ((value pc) (sra SI (sub SI value    pc) 1))
    ((value pc) (add SI (sll SI value 1) pc)))

(df f-17s16a2 "pc-rel addr (17 bits)" (all-mep-core-isas PCREL-ADDR) 16 16 INT
    ((value pc) (sra SI (sub SI value    pc) 1))
    ((value pc) (add SI (sll SI value 1) pc)))

(df f-24s5a2n-hi "24s5a2n hi 16s16" (all-mep-core-isas PCREL-ADDR) 16 16  INT #f #f)
(df f-24s5a2n-lo "24s5a2n lo 7s5a2" (all-mep-core-isas PCREL-ADDR)  5  7 UINT #f #f)
(define-multi-ifield
  (name f-24s5a2n)
  (comment "pc-rel addr (24 bits align 2)")
  (attrs all-mep-core-isas PCREL-ADDR)
  (mode INT)
  (subfields f-24s5a2n-hi f-24s5a2n-lo)
  (insert (sequence ()
		    (set (ifield f-24s5a2n)
			 (sub (ifield f-24s5a2n) pc))
		    (set (ifield f-24s5a2n-lo)
			 (srl (and (ifield f-24s5a2n) #xfe) 1))
		    (set (ifield f-24s5a2n-hi)
			 (sra INT (ifield f-24s5a2n) 8))))
  (extract (set (ifield f-24s5a2n)
		(add SI (or (sll (ifield f-24s5a2n-hi) 8)
			    (sll (ifield f-24s5a2n-lo) 1))
		     pc)))
  )

(df f-24u5a2n-hi "24u5a2n hi 16u16" (all-mep-core-isas) 16 16 UINT #f #f)
(df f-24u5a2n-lo "24u5a2n lo 7u5a2" (all-mep-core-isas)  5  7 UINT #f #f)
(define-multi-ifield
  (name f-24u5a2n)
  (comment "abs jump target (24 bits, alignment 2)")
  (attrs all-mep-core-isas ABS-ADDR)
  (mode UINT)
  (subfields f-24u5a2n-hi f-24u5a2n-lo)
  (insert (sequence ()
		    (set (ifield f-24u5a2n-lo)
			 (srl (and (ifield f-24u5a2n) #xff) 1))
		    (set (ifield f-24u5a2n-hi)
			 (srl (ifield f-24u5a2n) 8))
		    ))
  (extract (set (ifield f-24u5a2n)
		(or (sll (ifield f-24u5a2n-hi) 8)
		    (sll (ifield f-24u5a2n-lo) 1))))
  )

; Displacement fields.

(df f-2u6     "SAR offset (2 bits)"    (all-mep-core-isas)  6  2 UINT #f #f)
(df f-7u9     "tp-rel b (7 bits)"      (all-mep-core-isas)  9  7 UINT #f #f)
(df f-7u9a2   "tp-rel h (7 bits)"      (all-mep-core-isas)  9  6 UINT
    ((value pc) (srl SI value 1))
    ((value pc) (sll SI value 1)))
(df f-7u9a4   "tp/sp-rel w (7 bits)"   (all-mep-core-isas)  9  5 UINT
    ((value pc) (srl SI value 2))
    ((value pc) (sll SI value 2)))
(df f-16s16   "general 16-bit s-val"   (all-mep-core-isas) 16 16  INT #f #f)

; Immediate fields.

(df f-2u10   "swi level (2 bits)"      (all-mep-core-isas) 10  2 UINT #f #f)
(df f-3u5    "bit offset (3 bits)"     (all-mep-core-isas)  5  3 UINT #f #f)
(df f-4u8    "bCC const (4 bits)"      (all-mep-core-isas)  8  4 UINT #f #f)
(df f-5u8    "slt & shifts (5 bits)"   (all-mep-core-isas)  8  5 UINT #f #f)
(df f-5u24   "clip immediate (5 bits)" (all-mep-core-isas) 24  5 UINT #f #f)
(df f-6s8    "add immediate (6 bits)"  (all-mep-core-isas)  8  6  INT #f #f)
(df f-8s8    "add imm (8 bits)"        (all-mep-core-isas)  8  8  INT #f #f)
(df f-16u16  "general 16-bit u-val"    (all-mep-core-isas) 16 16 UINT #f #f)
(df f-12u16  "cmov fixed 1"            (all-mep-core-isas) 16 12 UINT #f #f)
(df f-3u29   "cmov fixed 2"            (all-mep-core-isas) 29  3 UINT #f #f)


; These are all for the coprocessor opcodes

; The field is like IJKiiiiiii where I and J are toggled if K is set,
; for compatibility with older cores.
(define-pmacro (compute-cdisp10 val)
  (cond SI
	((and SI (cond SI ((and SI val #x80) (xor SI val #x300)) (else val)) #x200)
	 (sub (cond SI ((and SI val #x80) (xor SI val #x300)) (else val)) #x400))
	(else
	 (cond SI ((and SI val #x80) (xor SI val #x300)) (else val)))
	)
  )
(define-pmacro (extend-cdisp10 val)
  (cond SI
	((and SI (compute-cdisp10 val) #x200)
	 (sub (and SI (compute-cdisp10 val) #x3ff) #x400))
	(else
	 (and SI (compute-cdisp10 val) #x3ff))
	)
  )

(df f-cdisp10    "cop imm10"          (all-mep-core-isas)   22  10 INT
    ((value pc) (extend-cdisp10 value))
    ((value pc) (extend-cdisp10 value))
    )

; Non-contiguous fields.

(df f-24u8a4n-hi "24u8a4n hi 16u16" (all-mep-core-isas) 16 16 UINT #f #f)
(df f-24u8a4n-lo "24u8a4n lo 8u8a4" (all-mep-core-isas)  8  6 UINT #f #f)
(define-multi-ifield
  (name f-24u8a4n)
  (comment "absolute 24-bit address")
  (attrs all-mep-core-isas)
  (mode UINT)
  (subfields f-24u8a4n-hi f-24u8a4n-lo)
  (insert (sequence ()
		    (set (ifield f-24u8a4n-hi) (srl (ifield f-24u8a4n) 8))
		    (set (ifield f-24u8a4n-lo) (srl (and (ifield f-24u8a4n) #xfc) 2))))
  (extract (set (ifield f-24u8a4n)
		(or (sll (ifield f-24u8a4n-hi) 8)
		    (sll (ifield f-24u8a4n-lo) 2))))
  )

(df f-24u8n-hi "24u8n hi 16u16" (all-mep-core-isas) 16 16 UINT #f #f)
(df f-24u8n-lo "24u8n lo  8u8"  (all-mep-core-isas)  8  8 UINT #f #f)
(define-multi-ifield
  (name f-24u8n)
  (comment "24-bit constant")
  (attrs all-mep-core-isas)
  (mode UINT)
  (subfields f-24u8n-hi f-24u8n-lo)
  (insert (sequence ()
		    (set (ifield f-24u8n-hi) (srl (ifield f-24u8n) 8))
		    (set (ifield f-24u8n-lo) (and (ifield f-24u8n) #xff))))
  (extract (set (ifield f-24u8n)
		(or (sll (ifield f-24u8n-hi) 8)
		    (ifield f-24u8n-lo))))
  )

(df f-24u4n-hi "24u4n hi  8u4"  (all-mep-core-isas)  4  8 UINT #f #f)
(df f-24u4n-lo "24u4n lo 16u16" (all-mep-core-isas) 16 16 UINT #f #f)
(define-multi-ifield
  (name f-24u4n)
  (comment "coprocessor code")
  (attrs all-mep-core-isas)
  (mode UINT)
  (subfields f-24u4n-hi f-24u4n-lo)
  (insert (sequence ()
		    (set (ifield f-24u4n-hi) (srl (ifield f-24u4n) 16))
		    (set (ifield f-24u4n-lo) (and (ifield f-24u4n) #xffff))))
  (extract (set (ifield f-24u4n)
		(or (sll (ifield f-24u4n-hi) 16)
		    (ifield f-24u4n-lo))))
  )

(define-multi-ifield
  (name f-callnum)
  (comment "system call number field")
  (attrs all-mep-core-isas)
  (mode UINT)
  (subfields f-5 f-6 f-7 f-11)
  (insert (sequence ()
		    (set (ifield f-5)  (and (srl (ifield f-callnum) 3) 1))
		    (set (ifield f-6)  (and (srl (ifield f-callnum) 2) 1))
		    (set (ifield f-7)  (and (srl (ifield f-callnum) 1) 1))
		    (set (ifield f-11) (and (ifield f-callnum) 1))))
  (extract (set (ifield f-callnum)
		(or (sll (ifield f-5) 3)
		    (or (sll (ifield f-6) 2)
			(or (sll (ifield f-7) 1)
			    (ifield f-11))))))
  )

(df f-ccrn-hi "ccrn hi  2u28" (all-mep-core-isas) 28 2 UINT #f #f)
(df f-ccrn-lo "ccrn lo  4u4"  (all-mep-core-isas)  4 4 UINT #f #f)
(define-multi-ifield
  (name f-ccrn)
  (comment "Coprocessor register number field")
  (attrs all-mep-core-isas)
  (mode UINT)
  (subfields f-ccrn-hi f-ccrn-lo)
  (insert (sequence ()
		    (set (ifield f-ccrn-hi)  (and (srl (ifield f-ccrn) 4) #x3))
		    (set (ifield f-ccrn-lo)  (and (ifield f-ccrn) #xf))))
  (extract (set (ifield f-ccrn)
		(or (sll (ifield f-ccrn-hi) 4)
		    (ifield f-ccrn-lo))))
  )

; Operands.

;; Only LABEL, REGNUM, FMAX_FLOAT and FMAX_INT are now relevant for correct
;; operation.  The others are mostly kept for backwards compatibility,
;; although they do affect the dummy prototypes in
;; gcc/config/mep/intrinsics.h.
(define-attr
  (type enum)
  (for operand)
  (name CDATA)
  (comment "datatype to use for C intrinsics mapping")
  (values LABEL REGNUM FMAX_FLOAT FMAX_INT
	  POINTER LONG ULONG SHORT USHORT CHAR UCHAR CP_DATA_BUS_INT)
  (default LONG))

(define-attr
  (type enum)
  (for insn)
  (name CPTYPE)
  (comment "datatype to use for coprocessor values")
  (values CP_DATA_BUS_INT VECT V2SI V4HI V8QI V2USI V4UHI V8UQI)
  (default CP_DATA_BUS_INT))

(define-attr
  (type enum)
  (for insn)
  (name CRET)
  ;; VOID - all arguments are passed as parameters; if any are written, pointers to them are passed.
  ;; FIRST - the first argument is the return value.
  ;; FIRSTCOPY - the first argument is the return value, but a copy is also the first parameter.
  (values VOID FIRST FIRSTCOPY)
  (default VOID)
  (comment "Insn's intrinsic returns void, or the first argument rather than (or in addition to) passing it."))

(define-attr
  (type integer)
  (for operand)
  (name ALIGN)
  (comment "alignment of immediate operands")
  (default 1))

(define-attr
  (for operand)
  (type boolean)
  (name RELOC_IMPLIES_OVERFLOW)
  (comment "Operand should not be considered as a candidate for relocs"))

(define-attr
  (for hardware)
  (type boolean)
  (name IS_FLOAT)
  (comment "Register contains a floating point value"))

(define-pmacro (dpop name commment attrib hwr field func)
  (define-full-operand name comment attrib
    hwr DFLT field ((parse func)) () ()))
(define-pmacro (dprp name commment attrib hwr field pafunc prfunc)
  (define-full-operand name comment attrib
    hwr DFLT field ((parse pafunc) (print prfunc)) () ()))

(dnop r0        "register 0"              (all-mep-core-isas) h-gpr   0)
(dnop rn        "register Rn"             (all-mep-core-isas) h-gpr   f-rn)
(dnop rm        "register Rm"             (all-mep-core-isas) h-gpr   f-rm)
(dnop rl        "register Rl"             (all-mep-core-isas) h-gpr   f-rl)
(dnop rn3       "register 0-7"            (all-mep-core-isas) h-gpr   f-rn3)

;; Variants of RM/RN with different CDATA attributes.  See comment above
;; CDATA for more details.

(dnop rma       "register Rm holding pointer"          (all-mep-core-isas (CDATA POINTER)) h-gpr   f-rm)

(dnop rnc       "register Rn holding char"             (all-mep-core-isas (CDATA LONG))    h-gpr   f-rn)
(dnop rnuc      "register Rn holding unsigned char"    (all-mep-core-isas (CDATA LONG))    h-gpr   f-rn)
(dnop rns       "register Rn holding short"            (all-mep-core-isas (CDATA LONG))    h-gpr   f-rn)
(dnop rnus      "register Rn holding unsigned short"   (all-mep-core-isas (CDATA LONG))    h-gpr   f-rn)
(dnop rnl       "register Rn holding long"             (all-mep-core-isas (CDATA LONG))    h-gpr   f-rn)
(dnop rnul      "register Rn holding unsigned  long"   (all-mep-core-isas (CDATA ULONG))   h-gpr   f-rn)

(dnop rn3c       "register 0-7 holding unsigned char"    (all-mep-core-isas (CDATA LONG))  h-gpr   f-rn3)
(dnop rn3uc      "register 0-7 holding byte"             (all-mep-core-isas (CDATA LONG))  h-gpr   f-rn3)
(dnop rn3s       "register 0-7 holding unsigned short"   (all-mep-core-isas (CDATA LONG))  h-gpr   f-rn3)
(dnop rn3us      "register 0-7 holding short"            (all-mep-core-isas (CDATA LONG))  h-gpr   f-rn3)
(dnop rn3l       "register 0-7 holding unsigned long"    (all-mep-core-isas (CDATA LONG))  h-gpr   f-rn3)
(dnop rn3ul      "register 0-7 holding long"             (all-mep-core-isas (CDATA ULONG)) h-gpr   f-rn3)


(dnop lp        "link pointer"            (all-mep-core-isas) h-csr   1)
(dnop sar       "shift amount register"   (all-mep-core-isas) h-csr   2)
(dnop hi        "high result"             (all-mep-core-isas) h-csr   7)
(dnop lo        "low result"              (all-mep-core-isas) h-csr   8)
(dnop mb0       "modulo begin register 0" (all-mep-core-isas) h-csr  12)
(dnop me0       "modulo end register 0"   (all-mep-core-isas) h-csr  13)
(dnop mb1       "modulo begin register 1" (all-mep-core-isas) h-csr  14)
(dnop me1       "modulo end register 1"   (all-mep-core-isas) h-csr  15)
(dnop psw       "program status word"     (all-mep-core-isas) h-csr  16)
(dnop epc	"exception prog counter"  (all-mep-core-isas) h-csr  19)
(dnop exc       "exception cause"         (all-mep-core-isas) h-csr  20)
(dnop npc       "nmi program counter"     (all-mep-core-isas) h-csr  23)
(dnop dbg       "debug register"          (all-mep-core-isas) h-csr  24)
(dnop depc      "debug exception pc"      (all-mep-core-isas) h-csr  25)
(dnop opt       "option register"         (all-mep-core-isas) h-csr  26)
(dnop r1        "register 1"              (all-mep-core-isas) h-gpr   1)
(dnop tp        "tiny data area pointer"  (all-mep-core-isas) h-gpr  13)
(dnop sp        "stack pointer"           (all-mep-core-isas) h-gpr  15)
(dprp tpr       "TP register"             (all-mep-core-isas) h-gpr  13       "tpreg" "tpreg")
(dprp spr       "SP register"             (all-mep-core-isas) h-gpr  15       "spreg" "spreg")

(define-full-operand
  csrn "control/special register" (all-mep-core-isas (CDATA REGNUM)) h-csr
  DFLT f-csrn ((parse "csrn")) () ()
)

(dnop csrn-idx  "control/special reg idx" (all-mep-core-isas) h-uint  f-csrn)
(dnop crn64     "copro Rn (64-bit)"       (all-mep-core-isas (CDATA CP_DATA_BUS_INT)) h-cr64  f-crn)
(dnop crn       "copro Rn (32-bit)"       (all-mep-core-isas (CDATA CP_DATA_BUS_INT)) h-cr    f-crn)
(dnop crnx64    "copro Rn (0-31, 64-bit)" (all-mep-core-isas (CDATA CP_DATA_BUS_INT)) h-cr64  f-crnx)
(dnop crnx      "copro Rn (0-31, 32-bit)" (all-mep-core-isas (CDATA CP_DATA_BUS_INT)) h-cr    f-crnx)
(dnop ccrn      "copro control reg CCRn"  (all-mep-core-isas (CDATA REGNUM)) h-ccr   f-ccrn)
(dnop cccc      "copro flags"             (all-mep-core-isas) h-uint  f-rm)

(dprp pcrel8a2  "pc-rel addr (8 bits)"    (all-mep-core-isas (CDATA LABEL) RELAX) h-sint  f-8s8a2   "mep_align" "address")
(dprp pcrel12a2 "pc-rel addr (12 bits)"   (all-mep-core-isas (CDATA LABEL) RELAX) h-sint  f-12s4a2  "mep_align" "address")
(dprp pcrel17a2 "pc-rel addr (17 bits)"   (all-mep-core-isas (CDATA LABEL) RELAX) h-sint  f-17s16a2 "mep_align" "address")
(dprp pcrel24a2 "pc-rel addr (24 bits)"   (all-mep-core-isas (CDATA LABEL))       h-sint  f-24s5a2n "mep_align" "address")
(dprp pcabs24a2 "pc-abs addr (24 bits)"   (all-mep-core-isas (CDATA LABEL))       h-uint  f-24u5a2n "mep_alignu" "address")

(dpop sdisp16   "displacement (16 bits)"  (all-mep-core-isas) h-sint  f-16s16    "signed16")
(dpop simm16    "signed imm (16 bits)"    (all-mep-core-isas) h-sint  f-16s16    "signed16")
(dpop uimm16    "unsigned imm (16 bits)"  (all-mep-core-isas) h-uint  f-16u16    "unsigned16")
(dnop code16    "uci/dsp code (16 bits)"  (all-mep-core-isas) h-uint  f-16u16)

(dnop udisp2    "SSARB addend (2 bits)"   (all-mep-core-isas) h-sint  f-2u6)
(dnop uimm2     "interrupt (2 bits)"      (all-mep-core-isas) h-uint  f-2u10)

(dnop simm6     "add const (6 bits)"      (all-mep-core-isas) h-sint  f-6s8)
(dnop simm8     "mov const (8 bits)"      (all-mep-core-isas RELOC_IMPLIES_OVERFLOW) 
                                             h-sint  f-8s8)

(dpop addr24a4  "sw/lw addr (24 bits)"    (all-mep-core-isas (ALIGN 4)) h-uint  f-24u8a4n  "mep_alignu")
(dnop code24    "coprocessor code"        (all-mep-core-isas) h-uint  f-24u4n)

(dnop callnum   "system call number"      (all-mep-core-isas) h-uint  f-callnum)
(dnop uimm3     "bit immediate (3 bits)"  (all-mep-core-isas) h-uint  f-3u5)
(dnop uimm4     "bCC const (4 bits)"      (all-mep-core-isas) h-uint  f-4u8)
(dnop uimm5     "bit/shift val (5 bits)"  (all-mep-core-isas) h-uint  f-5u8)

(dpop udisp7    "tp-rel b (7 bits)"       (all-mep-core-isas)           h-uint  f-7u9      "unsigned7")
(dpop udisp7a2  "tp-rel h (7 bits)"       (all-mep-core-isas (ALIGN 2)) h-uint  f-7u9a2    "unsigned7")
(dpop udisp7a4  "tp/sp-rel w (7 bits)"    (all-mep-core-isas (ALIGN 4)) h-uint  f-7u9a4    "unsigned7")
(dpop uimm7a4   "sp w-addend (7 bits)"    (all-mep-core-isas (ALIGN 4)) h-uint  f-7u9a4    "mep_alignu")

(dnop uimm24    "immediate (24 bits)"     (all-mep-core-isas) h-uint  f-24u8n)

(dnop cimm4     "cache immed'te (4 bits)" (all-mep-core-isas) h-uint  f-rn)
(dnop cimm5     "clip immediate (5 bits)" (all-mep-core-isas) h-uint  f-5u24)

(dpop cdisp10   "copro addend (8/10 bits)" (all-mep-core-isas) h-sint  f-cdisp10  "cdisp10")
(dpop cdisp10a2 "copro addend (8/10 bits)" (all-mep-core-isas) h-sint  f-cdisp10  "cdisp10")
(dpop cdisp10a4 "copro addend (8/10 bits)" (all-mep-core-isas) h-sint  f-cdisp10  "cdisp10")
(dpop cdisp10a8 "copro addend (8/10 bits)" (all-mep-core-isas) h-sint  f-cdisp10  "cdisp10")

; Special operand representing the various ways that the literal zero can be
; specified.
(define-full-operand
  zero "Zero operand" (all-mep-core-isas) h-sint DFLT f-nil
  ((parse "zero")) () ()
)

; Attributes.

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_BIT_INSN)
  (comment "optional bit manipulation instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_MUL_INSN)
  (comment "optional 32-bit multiply instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_DIV_INSN)
  (comment "optional 32-bit divide instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_DEBUG_INSN)
  (comment "optional debug instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_LDZ_INSN)
  (comment "optional leading zeroes instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_ABS_INSN)
  (comment "optional absolute difference instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_AVE_INSN)
  (comment "optional average instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_MINMAX_INSN)
  (comment "optional min/max instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_CLIP_INSN)
  (comment "optional clipping instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_SAT_INSN)
  (comment "optional saturation instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_UCI_INSN)
  (comment "optional UCI instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_DSP_INSN)
  (comment "optional DSP instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_CP_INSN)
  (comment "optional coprocessor-related instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_CP64_INSN)
  (comment "optional coprocessor-related 64 data bit instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name OPTIONAL_VLIW64)
  (comment "optional vliw64 mode (vliw32 is default)"))

(define-attr
  (for insn)
  (type enum)
  (name STALL)
  (attrs META)
  (values NONE SHIFTI INT2 LOAD STORE LDC STC LDCB STCB SSARB FSFT RET
	  ADVCK MUL MULR DIV)
  (default NONE)
  (comment "gcc stall attribute"))

(define-attr
  (for insn)
  (type string)
  (name INTRINSIC)
  (attrs META)
  (comment "gcc intrinsic name"))

(define-attr
  (for insn)
  (type enum)
  (name SLOT)
  (attrs META)
  (values NONE C3 V1 V3 P0S P0 P1)
  (default NONE)
  (comment "coprocessor slot type"))

(define-attr
  (for insn)
  (type boolean)
  (name MAY_TRAP)
  (comment "instruction may generate an exception"))

; Attributes for scheduling restrictions in vliw mode

(define-attr
  (for insn)
  (type boolean)
  (name VLIW_ALONE)
  (comment "instruction can be scheduled alone in vliw mode"))

(define-attr
  (for insn)
  (type boolean)
  (name VLIW_NO_CORE_NOP)
  (comment "there is no corresponding nop core instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name VLIW_NO_COP_NOP)
  (comment "there is no corresponding nop coprocessor instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name VLIW64_NO_MATCHING_NOP)
  (comment "there is no corresponding nop coprocessor instruction"))
(define-attr
  (for insn)
  (type boolean)
  (name VLIW32_NO_MATCHING_NOP)
  (comment "there is no corresponding nop coprocessor instruction"))

(define-attr
  (for insn)
  (type boolean)
  (name VOLATILE)
  (comment "Insn is volatile."))

(define-attr
  (for insn)
  (type integer)
  (name LATENCY)
  (comment "The latency of this insn, used for scheduling as an intrinsic in gcc")
  (default 0))

; The MeP config tool will edit this.
(define-attr
  (type enum)
  (for insn)
  (name CONFIG)
  (values NONE ; config-attr-start
	default
	  ) ; config-attr-end
)


; Enumerations.

(define-normal-insn-enum major "major opcodes" (all-mep-core-isas) MAJ_
  f-major
  (.map .str (.iota 16))
)


(define-pmacro (dni-isa xname xcomment xattrs xsyntax xformat xsemantics xtiming isa)
  (define-insn          
    (name xname)        
    (comment xcomment)
    (.splice attrs (.unsplice xattrs) (ISA isa))
    (syntax xsyntax)
    (format xformat)
    (semantics xsemantics)
    (.splice timing (.unsplice xtiming))
    )
) 
  
(define-pmacro (dnmi-isa xname xcomment xattrs xsyntax xemit isa)
  (dnmi xname xcomment (.splice (.unsplice xattrs) (ISA isa)) xsyntax xemit)
)

; For making profiling calls and dynamic configuration
(define-pmacro (cg-profile caller callee)
  (c-call "cg_profile" caller callee)
)
; For dynamic configuration only
(define-pmacro (cg-profile-jump caller callee)
  (c-call "cg_profile_jump" caller callee)
)

; For defining Core Instructions
(define-pmacro (dnci xname xcomment xattrs xsyntax xformat xsemantics xtiming)
  (dni-isa xname xcomment xattrs xsyntax xformat xsemantics xtiming all-core-isa-list)
)
(define-pmacro (dncmi xname xcomment xattrs xsyntax xemit)
  (dnmi-isa xname xcomment xattrs xsyntax xemit all-core-isa-list)
)

; For defining Coprocessor Instructions
;(define-pmacro (dncpi xname xcomment xattrs xsyntax xformat xsemantics xtiming)  (dni-isa xname xcomment xattrs xsyntax xformat xsemantics xtiming cop)
;)

;; flag setting macro
(define-pmacro (set-bit xop xbitnum xval) 
  (set xop (or 
	    (and xop (inv (sll 1 xbitnum)))
	    (and (sll 1 xbitnum) (sll xval xbitnum)))))

;; some flags we commonly use in vliw reasoning / mode-switching etc.
(define-pmacro (get-opt.vliw64) (and (srl opt 6) 1))
(define-pmacro (get-opt.vliw32) (and (srl opt 5) 1))
(define-pmacro (get-rm.lsb) (and rm 1))
(define-pmacro (get-psw.om) (and (srl psw 12) 1))
(define-pmacro (get-psw.nmi) (and (srl psw 9) 1))
(define-pmacro (get-psw.iep) (and (srl psw 1) 1))
(define-pmacro (get-psw.ump) (and (srl psw 3) 1))
(define-pmacro (get-epc.etom) (and epc 1))
(define-pmacro (get-npc.ntom) (and npc 1))
(define-pmacro (get-lp.ltom) (and lp 1))

(define-pmacro (set-psw.om zval) (set-bit (raw-reg h-csr 16) 12 zval))
(define-pmacro (set-psw.nmi zval) (set-bit (raw-reg h-csr 16) 9 zval))
(define-pmacro (set-psw.umc zval) (set-bit (raw-reg h-csr 16) 2 zval))
(define-pmacro (set-psw.iec zval) (set-bit (raw-reg h-csr 16) 0 zval))
(define-pmacro (set-rpe.elr zval) (set-bit (raw-reg h-csr 5) 0 zval))


;; the "3 way switch" depending on our current operating mode and vliw status flags
(define-pmacro (core-vliw-switch core-rtl vliw32-rtl vliw64-rtl) 
  (cond
   ((andif (get-psw.om) (get-opt.vliw64)) vliw64-rtl)
   ((andif (get-psw.om) (get-opt.vliw32)) vliw32-rtl)
   (else core-rtl)))

;; the varying-pcrel idiom
(define-pmacro (set-vliw-modified-pcrel-offset xtarg xa xb xc)
  (core-vliw-switch (set xtarg (add pc xa))
		    (set xtarg (add pc xb))
		    (set xtarg (add pc xc))))

;; the increasing-alignment idiom in branch displacements
(define-pmacro (set-vliw-alignment-modified xtarg zaddr)
  (core-vliw-switch (set xtarg (and zaddr (inv 1)))
		    (set xtarg (and zaddr (inv 3)))
		    (set xtarg (and zaddr (inv 7)))))

;; the increasing-alignment idiom in option-only form
(define-pmacro (set-vliw-aliignment-modified-by-option xtarg zaddr)
  (if (get-opt.vliw32)
      (set xtarg (and zaddr (inv 3)))
      (set xtarg (and zaddr (inv 7)))))



; pmacros needed for coprocessor modulo addressing.

; Taken from supplement ``The operation of the modulo addressing'' in
; Toshiba documentation rev 2.2, p. 34.

(define-pmacro (compute-mask0)
  (sequence SI ((SI temp))
    (set temp (or mb0 me0))
    (srl (const SI -1) (c-call SI "do_ldz" temp))))

(define-pmacro (mod0 immed)
  (sequence SI ((SI modulo-mask))
	    (set modulo-mask (compute-mask0))
	    (if SI (eq (and rma modulo-mask) me0)
		(or (and rma (inv modulo-mask)) mb0)
		(add rma (ext SI immed)))))

(define-pmacro (compute-mask1)
  (sequence SI ((SI temp))
    (set temp (or mb1 me1))
    (srl (const SI -1) (c-call SI "do_ldz" temp))))

(define-pmacro (mod1 immed)
  (sequence SI ((SI modulo-mask))
	    (set modulo-mask (compute-mask1))
	    (if SI (eq (and rma modulo-mask) me1)
		(or (and rma (inv modulo-mask)) mb1)
		(add rma (ext SI immed)))))


; Instructions.

; A pmacro for use in semantic bodies of unimplemented insns.
(define-pmacro (unimp mnemonic) (nop))

; Core specific instructions
; (include "mep-h1.cpu") ; -- exposed by MeP-Integrator
(include "mep-c5.cpu") ; -- exposed by MeP-Integrator

; Load/store instructions.

(dnci sb "store byte (register indirect)" ((STALL STORE))
     "sb $rnc,($rma)"
     (+ MAJ_0 rnc rma (f-sub4 8))
     (sequence ()
	       (c-call VOID "check_write_to_text" rma)
	       (set (mem UQI rma) (and rnc #xff)))
     ((mep (unit u-use-gpr (in usereg rnc))
	   (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci sh "store half-word (register indirect)" ((STALL STORE))
     "sh $rns,($rma)"
     (+ MAJ_0 rns rma (f-sub4 9))
     (sequence ()
	       (c-call VOID "check_write_to_text" (and rma (inv 1)))
	       (set (mem UHI (and rma (inv 1))) (and rns #xffff)))
     ((mep (unit u-use-gpr (in usereg rns))
	   (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci sw "store word (register indirect)" ((STALL STORE))
     "sw $rnl,($rma)"
     (+ MAJ_0 rnl rma (f-sub4 10))
     (sequence ()
	       (c-call VOID "check_write_to_text" (and rma (inv 3)))
	       (set (mem USI (and rma (inv 3))) rnl))
     ((mep (unit u-use-gpr (in usereg rnl))
	   (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lb "load byte (register indirect)" ((STALL LOAD) (LATENCY 2))
     "lb $rnc,($rma)"
     (+ MAJ_0 rnc rma (f-sub4 12))
     (set rnc (ext SI (mem QI rma)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rnc)))))

(dnci lh "load half-word (register indirect)" ((STALL LOAD) (LATENCY 2))
     "lh $rns,($rma)"
     (+ MAJ_0 rns rma (f-sub4 13))
     (set rns (ext SI (mem HI (and rma (inv 1)))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rns)))))

(dnci lw "load word (register indirect)" ((STALL LOAD) (LATENCY 2))
     "lw $rnl,($rma)"
     (+ MAJ_0 rnl rma (f-sub4 14))
     (set rnl (mem SI (and rma (inv 3))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rnl)))))

(dnci lbu "load unsigned byte (register indirect)" ((STALL LOAD) (LATENCY 2))
     "lbu $rnuc,($rma)"
     (+ MAJ_0 rnuc rma (f-sub4 11))
     (set rnuc (zext SI (mem UQI rma)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rnuc)))))

(dnci lhu "load unsigned half-word (register indirect)" ((STALL LOAD) (LATENCY 2))
     "lhu $rnus,($rma)"
     (+ MAJ_0 rnus rma (f-sub4 15))
     (set rnus (zext SI (mem UHI (and rma (inv 1)))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rnus)))))

(dnci sw-sp "store word (sp relative)" ((STALL STORE))
     "sw $rnl,$udisp7a4($spr)"
     (+ MAJ_4 rnl (f-8 0) udisp7a4 (f-sub2 2))
     (sequence ()
	       (c-call VOID "check_write_to_text" (and (add udisp7a4 sp) (inv 3)))
	       (set (mem SI (and (add udisp7a4 sp) (inv 3))) rnl))
     ((mep (unit u-use-gpr (in usereg rnl))
	   (unit u-use-gpr (in usereg sp))
	   (unit u-exec))))


(dnci lw-sp "load word (sp relative)" ((STALL LOAD) (LATENCY 2))
     "lw $rnl,$udisp7a4($spr)"
     (+ MAJ_4 rnl (f-8 0) udisp7a4 (f-sub2 3))
     (set rnl (mem SI (and (add udisp7a4 sp) (inv 3))))
     ((mep (unit u-use-gpr (in usereg sp))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rnl)))))

(dnci sb-tp "store byte (tp relative)" ((STALL STORE))
     "sb $rn3c,$udisp7($tpr)"
     (+ MAJ_8 (f-4 0) rn3c (f-8 0) udisp7)
     (sequence ()
	       (c-call VOID "check_write_to_text" (add (zext SI udisp7) tp))
	       (set (mem QI (add (zext SI udisp7) tp)) (and rn3c #xff)))
     ((mep (unit u-use-gpr (in usereg rn3c))
	   (unit u-use-gpr (in usereg tp))
	   (unit u-exec))))

(dnci sh-tp "store half-word (tp relative)" ((STALL STORE))
     "sh $rn3s,$udisp7a2($tpr)"
     (+ MAJ_8 (f-4 0) rn3s (f-8 1) udisp7a2 (f-15 0))
     (sequence ()
	       (c-call VOID "check_write_to_text" (and (add (zext SI udisp7a2) tp) (inv 1)))
	       (set (mem HI (and (add (zext SI udisp7a2) tp) (inv 1))) (and rn3s #xffff)))
     ((mep (unit u-use-gpr (in usereg rn3s))
	   (unit u-use-gpr (in usereg tp))
	   (unit u-exec))))

(dnci sw-tp "store word (tp relative)" ((STALL STORE))
     "sw $rn3l,$udisp7a4($tpr)"
     (+ MAJ_4 (f-4 0) rn3l (f-8 1) udisp7a4 (f-sub2 2))
     (sequence ()
	       (c-call VOID "check_write_to_text" (and (add (zext SI udisp7a4) tp) (inv 3)))
	       (set (mem SI (and (add (zext SI udisp7a4) tp) (inv 3))) rn3l))
     ((mep (unit u-use-gpr (in usereg rn3l))
	   (unit u-use-gpr (in usereg tp))
	   (unit u-exec))))

(dnci lb-tp "load byte (tp relative)" ((STALL LOAD) (LATENCY 2))
     "lb $rn3c,$udisp7($tpr)"
     (+ MAJ_8 (f-4 1) rn3c (f-8 0) udisp7)
     (set rn3c (ext SI (mem QI (add (zext SI udisp7) tp))))
     ((mep (unit u-use-gpr (in usereg tp))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rn3c)))))

(dnci lh-tp "load half-word (tp relative)" ((STALL LOAD) (LATENCY 2))
     "lh $rn3s,$udisp7a2($tpr)"
     (+ MAJ_8 (f-4 1) rn3s (f-8 1) udisp7a2 (f-15 0))
     (set rn3s (ext SI (mem HI (and (add (zext SI udisp7a2) tp) (inv 1)))))
     ((mep (unit u-use-gpr (in usereg tp))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rn3s)))))

(dnci lw-tp "load word (tp relative)" ((STALL LOAD) (LATENCY 2))
     "lw $rn3l,$udisp7a4($tpr)"
     (+ MAJ_4 (f-4 0) rn3l (f-8 1) udisp7a4 (f-sub2 3))
     (set rn3l (mem SI (and (add (zext SI udisp7a4) tp) (inv 3))))
     ((mep (unit u-use-gpr (in usereg tp))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rn3l)))))

(dnci lbu-tp "load unsigned byte (tp relative)" ((STALL LOAD) (LATENCY 2))
     "lbu $rn3uc,$udisp7($tpr)"
     (+ MAJ_4 (f-4 1) rn3uc (f-8 1) udisp7)
     (set rn3uc (zext SI (mem QI (add (zext SI udisp7) tp))))
     ((mep (unit u-use-gpr (in usereg tp))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rn3uc)))))

(dnci lhu-tp "load unsigned half-word (tp relative)" ((STALL LOAD) (LATENCY 2))
     "lhu $rn3us,$udisp7a2($tpr)"
     (+ MAJ_8 (f-4 1) rn3us (f-8 1) udisp7a2 (f-15 1))
     (set rn3us (zext SI (mem HI (and (add (zext SI udisp7a2) tp) (inv 1)))))
     ((mep (unit u-use-gpr (in usereg tp))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rn3us)))))

(dnci sb16 "store byte (16 bit displacement)" ((STALL STORE))
     "sb $rnc,$sdisp16($rma)"
     (+ MAJ_12 rnc rma (f-sub4 8) sdisp16)
     (sequence ()
	       (c-call VOID "check_write_to_text" (add rma (ext SI sdisp16)))
	       (set (mem QI (add rma (ext SI sdisp16))) (and rnc #xff)))
     ((mep (unit u-use-gpr (in usereg rnc))
	   (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci sh16 "store half-word (16 bit displacement)" ((STALL STORE))
     "sh $rns,$sdisp16($rma)"
     (+ MAJ_12 rns rma (f-sub4 9) sdisp16)
     (sequence ()
	       (c-call VOID "check_write_to_text" (and (add rma (ext SI sdisp16)) (inv 1)))
	       (set (mem HI (and (add rma (ext SI sdisp16)) (inv 1))) (and rns #xffff)))
     ((mep (unit u-use-gpr (in usereg rns))
	   (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci sw16 "store word (16 bit displacement)" ((STALL STORE))
     "sw $rnl,$sdisp16($rma)"
     (+ MAJ_12 rnl rma (f-sub4 10) sdisp16)
     (sequence ()
	       (c-call "check_write_to_text" (and (add rma (ext SI sdisp16)) (inv 3)))
	       (set (mem SI (and (add rma (ext SI sdisp16)) (inv 3))) rnl))
     ((mep (unit u-use-gpr (in usereg rnl))
	   (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lb16 "load byte (16 bit displacement)" ((STALL LOAD) (LATENCY 2))
     "lb $rnc,$sdisp16($rma)"
     (+ MAJ_12 rnc rma (f-sub4 12) sdisp16)
     (set rnc (ext SI (mem QI (add rma (ext SI sdisp16)))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rnc)))))

(dnci lh16 "load half-word (16 bit displacement)" ((STALL LOAD) (LATENCY 2))
     "lh $rns,$sdisp16($rma)"
     (+ MAJ_12 rns rma (f-sub4 13) sdisp16)
     (set rns (ext SI (mem HI (and (add rma (ext SI sdisp16)) (inv 1)))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rns)))))

(dnci lw16 "load word (16 bit displacement)" ((STALL LOAD) (LATENCY 2))
     "lw $rnl,$sdisp16($rma)"
     (+ MAJ_12 rnl rma (f-sub4 14) sdisp16)
     (set rnl (mem SI (and (add rma (ext SI sdisp16)) (inv 3))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rnl)))))

(dnci lbu16 "load unsigned byte (16 bit displacement)" ((STALL LOAD) (LATENCY 2))
     "lbu $rnuc,$sdisp16($rma)"
     (+ MAJ_12 rnuc rma (f-sub4 11) sdisp16)
     (set rnuc (zext SI (mem QI (add rma (ext SI sdisp16)))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rnuc)))))

(dnci lhu16 "load unsigned half-word (16 bit displacement)" ((STALL LOAD) (LATENCY 2))
     "lhu $rnus,$sdisp16($rma)"
     (+ MAJ_12 rnus rma (f-sub4 15) sdisp16)
     (set rnus (zext SI (mem HI (and (add rma (ext SI sdisp16)) (inv 1)))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec)
	   (unit u-load-gpr (out loadreg rnus)))))

(dnci sw24 "store word (24 bit absolute addressing)" ((STALL STORE))
     "sw $rnl,($addr24a4)"
     (+ MAJ_14 rnl addr24a4 (f-sub2 2))
     (sequence ()
	       (c-call VOID "check_write_to_text" (zext SI addr24a4))
	       (set (mem SI (zext SI addr24a4)) rnl))
     ((mep (unit u-use-gpr (in usereg rnl))
	   (unit u-exec))))

(dnci lw24 "load word (24 bit absolute addressing)" ((STALL LOAD) (LATENCY 2))
     "lw $rnl,($addr24a4)"
     (+ MAJ_14 rnl addr24a4 (f-sub2 3))
     (set rnl (mem SI (zext SI addr24a4)))
     ((mep (unit u-exec)
	   (unit u-load-gpr (out loadreg rnl)))))


; Extension instructions.

(dnci extb "sign extend byte" ()
     "extb $rn"
     (+ MAJ_1 rn (f-rm 0) (f-sub4 13))
     (set rn (ext SI (and QI rn #xff)))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci exth "sign extend half-word" ()
     "exth $rn"
     (+ MAJ_1 rn (f-rm 2) (f-sub4 13))
     (set rn (ext SI (and HI rn #xffff)))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci extub "zero extend byte" ()
     "extub $rn"
     (+ MAJ_1 rn (f-rm 8) (f-sub4 13))
     (set rn (zext SI (and rn #xff)))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci extuh "zero extend half-word" ()
     "extuh $rn"
     (+ MAJ_1 rn (f-rm 10) (f-sub4 13))
     (set rn (zext SI (and rn #xffff)))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))


; Shift amount manipulation instructions.

(dnci ssarb "set sar to bytes" ((STALL SSARB) VOLATILE)
     "ssarb $udisp2($rm)"
     (+ MAJ_1 (f-4 0) (f-5 0) udisp2 rm (f-sub4 12))
     (if (c-call BI "big_endian_p")
         (set sar (zext SI (mul (and (add udisp2 rm) 3) 8)))
         (set sar (sub 32 (zext SI (mul (and (add udisp2 rm) 3) 8)))))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))


; Move instructions.

(dnci mov "move" ()
     "mov $rn,$rm"
     (+ MAJ_0 rn rm (f-sub4 0))
     (set rn rm)
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci movi8 "move 8-bit immediate" ()
     "mov $rn,$simm8"
     (+ MAJ_5 rn simm8)
     (set rn (ext SI simm8))
     ())

(dnci movi16 "move 16-bit immediate" ()
     "mov $rn,$simm16"
     (+ MAJ_12 rn (f-rm 0) (f-sub4 1) simm16)
     (set rn (ext SI simm16))
     ())

(dnci movu24 "move 24-bit unsigned immediate" ()
     "movu $rn3,$uimm24"
     (+ MAJ_13 (f-4 0) rn3 uimm24)
     (set rn3 (zext SI uimm24))
     ())

(dnci movu16 "move 16-bit unsigned immediate" ()
     "movu $rn,$uimm16"
     (+ MAJ_12 rn (f-rm 1) (f-sub4 1) uimm16)
     (set rn (zext SI uimm16))
     ())

(dnci movh "move high 16-bit immediate" ()
     "movh $rn,$uimm16"
     (+ MAJ_12 rn (f-rm 2) (f-sub4 1) uimm16)
     (set rn (sll uimm16 16))
     ())


; Arithmetic instructions.

(dnci add3 "add three registers" ()
     "add3 $rl,$rn,$rm"
     (+ MAJ_9 rn rm rl)
     (set rl (add rn rm))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci add "add" ()
     "add $rn,$simm6"
     (+ MAJ_6 rn simm6 (f-sub2 0))
     (set rn (add rn (ext SI simm6)))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci add3i "add two registers and immediate" ()
     "add3 $rn,$spr,$uimm7a4"
     (+ MAJ_4 rn (f-8 0) uimm7a4 (f-sub2 0))
     (set rn (add sp (zext SI uimm7a4)))
     ((mep (unit u-use-gpr (in usereg sp))
	   (unit u-exec))))

(dnci advck3 "add overflow check" ((STALL ADVCK))
     "advck3 \\$0,$rn,$rm"
     (+ MAJ_0 rn rm (f-sub4 7))
     (if (add-oflag rn rm 0)
	 (set r0 1)
	 (set r0 0))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci sub "subtract" ()
     "sub $rn,$rm"
     (+ MAJ_0 rn rm (f-sub4 4))
     (set rn (sub rn rm))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm)))))

(dnci sbvck3 "subtraction overflow check" ((STALL ADVCK))
     "sbvck3 \\$0,$rn,$rm"
     (+ MAJ_0 rn rm (f-sub4 5))
     (if (sub-oflag rn rm 0)
	 (set r0 1)
	 (set r0 0))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci neg "negate" ()
     "neg $rn,$rm"
     (+ MAJ_0 rn rm (f-sub4 1))
     (set rn (neg rm))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci slt3 "set if less than" ()
     "slt3 \\$0,$rn,$rm"
     (+ MAJ_0 rn rm (f-sub4 2))
     (if (lt rn rm)
	 (set r0 1)
	 (set r0 0))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci sltu3 "set less than unsigned" ()
     "sltu3 \\$0,$rn,$rm"
     (+ MAJ_0 rn rm (f-sub4 3))
     (if (ltu rn rm)
	 (set r0 1)
	 (set r0 0))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci slt3i "set if less than immediate" ()
     "slt3 \\$0,$rn,$uimm5"
     (+ MAJ_6 rn uimm5 (f-sub3 1))
     (if (lt rn (zext SI uimm5))
	 (set r0 1)
	 (set r0 0))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci sltu3i "set if less than unsigned immediate" ()
     "sltu3 \\$0,$rn,$uimm5"
     (+ MAJ_6 rn uimm5 (f-sub3 5))
     (if (ltu rn (zext SI uimm5))
	 (set r0 1)
	 (set r0 0))
     ())

(dnci sl1ad3 "shift left one and add" ((STALL INT2))
     "sl1ad3 \\$0,$rn,$rm"
     (+ MAJ_2 rn rm (f-sub4 6))
     (set r0 (add (sll rn 1) rm))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci sl2ad3 "shift left two and add" ((STALL INT2))
     "sl2ad3 \\$0,$rn,$rm"
     (+ MAJ_2 rn rm (f-sub4 7))
     (set r0 (add (sll rn 2) rm))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci add3x "three operand add (extended)" ()
     "add3 $rn,$rm,$simm16"
     (+ MAJ_12 rn rm (f-sub4 0) simm16)
     (set rn (add rm (ext SI simm16)))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci slt3x "set if less than (extended)" ()
     "slt3 $rn,$rm,$simm16"
     (+ MAJ_12 rn rm (f-sub4 2) simm16)
     (if (lt rm (ext SI simm16))
	 (set rn 1)
	 (set rn 0))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci sltu3x "set if less than unsigned (extended)" ()
     "sltu3 $rn,$rm,$uimm16"
     (+ MAJ_12 rn rm (f-sub4 3) uimm16)
     (if (ltu rm (zext SI uimm16))
	 (set rn 1)
	 (set rn 0))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))


; Logical instructions.

(dnci or "bitwise or" ()
     "or $rn,$rm"
     (+ MAJ_1 rn rm (f-sub4 0))
     (set rn (or rn rm))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci and "bitwise and" ()
     "and $rn,$rm"
     (+ MAJ_1 rn rm (f-sub4 1))
     (set rn (and rn rm))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci xor "bitwise exclusive or" ()
     "xor $rn,$rm"
     (+ MAJ_1 rn rm (f-sub4 2))
     (set rn (xor rn rm))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci nor "bitwise negated or" ()
     "nor $rn,$rm"
     (+ MAJ_1 rn rm (f-sub4 3))
     (set rn (inv (or rn rm)))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci or3 "or three operand" ()
     "or3 $rn,$rm,$uimm16"
     (+ MAJ_12 rn rm (f-sub4 4) uimm16)
     (set rn (or rm (zext SI uimm16)))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci and3 "and three operand" ()
     "and3 $rn,$rm,$uimm16"
     (+ MAJ_12 rn rm (f-sub4 5) uimm16)
     (set rn (and rm (zext SI uimm16)))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci xor3 "exclusive or three operand" ()
     "xor3 $rn,$rm,$uimm16"
     (+ MAJ_12 rn rm (f-sub4 6) uimm16)
     (set rn (xor rm (zext SI uimm16)))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))


; Shift instructions.

(dnci sra "shift right arithmetic" ((STALL INT2))
     "sra $rn,$rm"
     (+ MAJ_2 rn rm (f-sub4 13))
     (set rn (sra rn (and rm #x1f)))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci srl "shift right logical" ((STALL INT2))
     "srl $rn,$rm"
     (+ MAJ_2 rn rm (f-sub4 12))
     (set rn (srl rn (and rm #x1f)))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci sll "shift left logical" ((STALL INT2))
     "sll $rn,$rm"
     (+ MAJ_2 rn rm (f-sub4 14))
     (set rn (sll rn (and rm #x1f)))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))

(dnci srai "shift right arithmetic (immediate)" ((STALL SHIFTI))
     "sra $rn,$uimm5"
     (+ MAJ_6 rn uimm5 (f-sub3 3))
     (set rn (sra rn uimm5))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci srli "shift right logical (immediate)" ((STALL SHIFTI))
     "srl $rn,$uimm5"
     (+ MAJ_6 rn uimm5 (f-sub3 2))
     (set rn (srl rn uimm5))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci slli "shift left logical (immediate)" ((STALL SHIFTI))
     "sll $rn,$uimm5"
     (+ MAJ_6 rn uimm5 (f-sub3 6))
     (set rn (sll rn uimm5))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci sll3 "three-register shift left logical" ((STALL INT2))
     "sll3 \\$0,$rn,$uimm5"
     (+ MAJ_6 rn uimm5 (f-sub3 7))
     (set r0 (sll rn uimm5))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci fsft "field shift" ((STALL FSFT) VOLATILE)
     "fsft $rn,$rm"
     (+ MAJ_2 rn rm (f-sub4 15))
     (sequence ((DI temp) (QI shamt))
	       (set shamt (and sar #x3f))
	       (set temp (sll (or (sll (zext DI rn) 32) (zext DI rm)) shamt))
	       (set rn (subword SI (srl temp 32) 1)))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))


; Branch/jump instructions.

(dnci bra "branch" (RELAXABLE)
     "bra $pcrel12a2"
     (+ MAJ_11 pcrel12a2 (f-15 0))
     (set-vliw-alignment-modified pc pcrel12a2)
     ((mep (unit u-branch)
	   (unit u-exec))))

(dnci beqz "branch if equal zero" (RELAXABLE)
     "beqz $rn,$pcrel8a2"
     (+ MAJ_10 rn pcrel8a2 (f-15 0))
     (if (eq rn 0)
	 (set-vliw-alignment-modified pc pcrel8a2))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec)
	   (unit u-branch))))

(dnci bnez "branch if not equal zero" (RELAXABLE)
     "bnez $rn,$pcrel8a2"
     (+ MAJ_10 rn pcrel8a2 (f-15 1))
     (if (ne rn 0)
	 (set-vliw-alignment-modified pc pcrel8a2))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec)
	   (unit u-branch))))

(dnci beqi "branch equal immediate" (RELAXABLE)
     "beqi $rn,$uimm4,$pcrel17a2"
     (+ MAJ_14 rn uimm4 (f-sub4 0) pcrel17a2)
     (if (eq rn (zext SI uimm4))
	 (set-vliw-alignment-modified pc pcrel17a2))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec)
	   (unit u-branch))))

(dnci bnei "branch not equal immediate" (RELAXABLE)
     "bnei $rn,$uimm4,$pcrel17a2"
     (+ MAJ_14 rn uimm4 (f-sub4 4) pcrel17a2)
     (if (ne rn (zext SI uimm4))
	 (set-vliw-alignment-modified pc pcrel17a2))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec)
	   (unit u-branch))))

(dnci blti "branch less than immediate" (RELAXABLE)
     "blti $rn,$uimm4,$pcrel17a2"
     (+ MAJ_14 rn uimm4 (f-sub4 12) pcrel17a2)
     (if (lt rn (zext SI uimm4))
	 (set-vliw-alignment-modified pc pcrel17a2))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec)
	   (unit u-branch))))

(dnci bgei "branch greater than immediate" (RELAXABLE)
     "bgei $rn,$uimm4,$pcrel17a2"
     (+ MAJ_14 rn uimm4 (f-sub4 8) pcrel17a2)
     (if (ge rn (zext SI uimm4))
	 (set-vliw-alignment-modified pc pcrel17a2))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec)
	   (unit u-branch))))

(dnci beq "branch equal" ()
     "beq $rn,$rm,$pcrel17a2"
     (+ MAJ_14 rn rm (f-sub4 1) pcrel17a2)
     (if (eq rn rm)
	 (set-vliw-alignment-modified pc pcrel17a2))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-branch))))

(dnci bne "branch not equal" ()
     "bne $rn,$rm,$pcrel17a2"
     (+ MAJ_14 rn rm (f-sub4 5) pcrel17a2)
     (if (ne rn rm)
	 (set-vliw-alignment-modified pc pcrel17a2))
     ((mep (unit u-use-gpr (in usereg rn))
           (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-branch))))

(dnci bsr12 "branch to subroutine (12 bit displacement)" (RELAXABLE)
     "bsr $pcrel12a2"
     (+ MAJ_11 pcrel12a2 (f-15 1))
     (sequence ()
	       (cg-profile pc pcrel12a2)
	       (set-vliw-modified-pcrel-offset lp 2 4 8)
	       (set-vliw-alignment-modified pc pcrel12a2))
     ((mep (unit u-exec)
	   (unit u-branch))))

(dnci bsr24 "branch to subroutine (24 bit displacement)" ()
     "bsr $pcrel24a2"
     (+ MAJ_13 (f-4 1) (f-sub4 9) pcrel24a2)
     (sequence ()
	       (cg-profile pc pcrel24a2)
	       (set-vliw-modified-pcrel-offset lp 4 4 8)
	       (set-vliw-alignment-modified pc pcrel24a2))
     ((mep (unit u-exec)
	   (unit u-branch))))

(dnci jmp "jump" ()
     "jmp $rm"
     (+ MAJ_1 (f-rn 0) rm (f-sub4 14))
     (sequence ()
	       (if (eq (get-psw.om) 0)
		   ;; core mode
		   (if (get-rm.lsb)
		       (sequence ()
				 (set-psw.om 1) ;; enter VLIW mode
				 (set-vliw-aliignment-modified-by-option pc rm))
		       (set pc (and rm (inv 1))))
		   ;; VLIW mode
		   (if (get-rm.lsb)
		       (sequence ()
				 (set-psw.om 0) ;; enter core mode
				 (set pc (and rm (inv 1))))
		       (set-vliw-aliignment-modified-by-option pc rm)))
	       (cg-profile-jump pc rm))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-branch))))

(dnci jmp24 "jump (24 bit target)" ()
     "jmp $pcabs24a2"
     (+ MAJ_13 (f-4 1) (f-sub4 8) pcabs24a2)
     (sequence ()
	       (set-vliw-alignment-modified pc (or (and pc #xf0000000) pcabs24a2))
	       (cg-profile-jump pc pcabs24a2))
     ((mep (unit u-exec)
	   (unit u-branch))))

(dnci jsr "jump to subroutine" ()
     "jsr $rm"
     (+ MAJ_1 (f-rn 0) rm (f-sub4 15))
     (sequence ()
	       (cg-profile pc rm)
	       (set-vliw-modified-pcrel-offset lp 2 4 8)
	       (set-vliw-alignment-modified pc rm))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-branch))))

(dnci ret "return from subroutine" ((STALL RET))
     "ret"
     (+ MAJ_7 (f-rn 0) (f-rm 0) (f-sub4 2))
     (sequence ()
	       (if (eq (get-psw.om) 0)
		   ;; core mode
		   (if (get-lp.ltom) ;; link-pointer "toggle mode" bit
		       (sequence ()
				 (set-psw.om 1) ;; enter VLIW mode
				 (set-vliw-aliignment-modified-by-option pc lp))
		       (set pc (and lp (inv 1))))
		   ;; VLIW mode
		   (if (get-lp.ltom) ;; link-pointer "toggle mode" bit
		       (sequence ()
				 (set-psw.om 0) ;; enter VLIW mode
				 (set pc (and lp (inv 1))))
		       (set-vliw-aliignment-modified-by-option pc lp)))
	       (c-call VOID "notify_ret" pc))
     ((mep (unit u-exec)
	   (unit u-branch))))


; Repeat instructions.

(dnci repeat "repeat specified repeat block" ()
     "repeat $rn,$pcrel17a2"
     (+ MAJ_14 rn (f-rm 0) (f-sub4 9) pcrel17a2)
     (sequence ()
	       (set-vliw-modified-pcrel-offset (reg h-csr 4) 4 4 8)
	       (set-vliw-alignment-modified (reg h-csr 5) pcrel17a2)
	       (set (reg h-csr 6) rn))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci erepeat "endless repeat" ()
     "erepeat $pcrel17a2"
     (+ MAJ_14 (f-rn 0) (f-rm 1) (f-sub4 9) pcrel17a2)
     (sequence ()
	       (set-vliw-modified-pcrel-offset (reg h-csr 4) 4 4 8)
	       (set-vliw-alignment-modified (reg h-csr 5) pcrel17a2)
	       (set-rpe.elr 1)
	       ; rpc may be undefined for erepeat
	       ; use 1 to trigger repeat logic in the sim's main loop
	       (set (reg h-csr 6) 1))
     ())


; Control instructions.

;; special store variants

(dnci stc_lp "store to control register lp" ((STALL STC))
      "stc $rn,\\$lp" 
      (+ MAJ_7 rn (f-csrn-lo 1) (f-csrn-hi 0) (f-12 1) (f-13 0) (f-14 0))  
      (set lp rn)     
      ((mep (unit u-use-gpr (in usereg rn))
	    (unit u-store-ctrl-reg (out storereg lp))
	    (unit u-exec))))

(dnci stc_hi "store to control register hi" ((STALL STC))
      "stc $rn,\\$hi" 
      (+ MAJ_7 rn (f-csrn-lo 7) (f-csrn-hi 0) (f-12 1) (f-13 0) (f-14 0))  
      (set hi rn)     
      ((mep (unit u-use-gpr (in usereg rn))
	    (unit u-store-ctrl-reg (out storereg hi))
	    (unit u-exec))))

(dnci stc_lo "store to control register lo" ((STALL STC))
      "stc $rn,\\$lo" 
      (+ MAJ_7 rn (f-csrn-lo 8) (f-csrn-hi 0) (f-12 1) (f-13 0) (f-14 0))  
      (set lo rn)    
      ((mep (unit u-use-gpr (in usereg rn))
	    (unit u-store-ctrl-reg (out storereg lo))
	    (unit u-exec))))

;; general store

(dnci stc "store to control register" (VOLATILE (STALL STC))
     "stc $rn,$csrn"
     (+ MAJ_7 rn csrn (f-12 1) (f-13 0) (f-14 0))
     (set csrn rn)
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-store-ctrl-reg (out storereg csrn))
	   (unit u-exec))))

;; special load variants 

(dnci ldc_lp "load from control register lp" ((STALL LDC))
      "ldc $rn,\\$lp"    
      (+ MAJ_7 rn (f-csrn-lo 1) (f-csrn-hi 0) (f-12 1) (f-13 0) (f-14 1))     
      (set rn lp)     
      ((mep (unit u-use-ctrl-reg (in usereg lp))
	    (unit u-exec)
	    (unit u-load-gpr (out loadreg rn)))))
       

(dnci ldc_hi "load from control register hi" ((STALL LDC))
      "ldc $rn,\\$hi"    
      (+ MAJ_7 rn (f-csrn-lo 7) (f-csrn-hi 0) (f-12 1) (f-13 0) (f-14 1))
      (set rn hi)
      ((mep (unit u-use-ctrl-reg (in usereg hi))
	    (unit u-exec)
	    (unit u-load-gpr (out loadreg rn)))))

(dnci ldc_lo "load from control register lo" ((STALL LDC))
      "ldc $rn,\\$lo"
      (+ MAJ_7 rn (f-csrn-lo 8) (f-csrn-hi 0) (f-12 1) (f-13 0) (f-14 1))     
      (set rn lo)
      ((mep (unit u-use-ctrl-reg (in usereg lo))
	    (unit u-exec)
	    (unit u-load-gpr (out loadreg rn)))))

;; general load

(dnci ldc "load from control register" (VOLATILE (STALL LDC) (LATENCY 2))
     "ldc $rn,$csrn"
     (+ MAJ_7 rn csrn (f-12 1) (f-13 0) (f-14 1))
     (if (eq (ifield f-csrn) 0) 
	 ;; loading from the pc
	 (set-vliw-modified-pcrel-offset rn 2 4 8)
	 ;; loading from something else
	 (set rn csrn))
      ((mep (unit u-use-ctrl-reg (in usereg csrn))
	    (unit u-exec)
	    (unit u-load-gpr (out loadreg rn)))))

(dnci di "disable interrupt" (VOLATILE)
     "di"
     (+ MAJ_7 (f-rn 0) (f-rm 0) (f-sub4 0))
     ; clear psw.iec
     (set psw (sll (srl psw 1) 1)) 
     ())

(dnci ei "enable interrupt" (VOLATILE)
     "ei"
     (+ MAJ_7 (f-rn 0) (f-rm 1) (f-sub4 0))
     ; set psw.iec
     (set psw (or psw 1))
     ())

(dnci reti "return from interrupt" ((STALL RET))
     "reti"
     (+ MAJ_7 (f-rn 0) (f-rm 1) (f-sub4 2))
     (if (eq (get-psw.om) 0)
	 ;; core operation mode
	 (if (get-psw.nmi)
	     ;; return from NMI
	     (if (get-npc.ntom)
		 ;; return in VLIW operation mode
		 (sequence ()
			   (set-psw.om 1)
			   (set-vliw-aliignment-modified-by-option pc npc)
			   (set-psw.nmi 0))
		 ;; return in core mode
		 (sequence ()
			   (set pc (and npc (inv 1)))
			   (set-psw.nmi 0)))
	     ;; return from non-NMI
	     (if (get-epc.etom)
		 ;; return in VLIW mode
		 (sequence () 
			   (set-psw.om 1)
			   (set-vliw-aliignment-modified-by-option pc epc)
			   (set-psw.umc (get-psw.ump))
			   (set-psw.iec (get-psw.iep)))
		 ;; return in core mode
		 (sequence ()
			   (set pc (and epc (inv 1)))
			   (set-psw.umc (get-psw.ump))
			   (set-psw.iec (get-psw.iep)))))
	 ;; VLIW operation mode
	 ;; xxx undefined
	 (nop))
     ((mep (unit u-exec)
	   (unit u-branch))))

(dnci halt "halt pipeline" (VOLATILE)
     "halt"
     (+ MAJ_7 (f-rn 0) (f-rm 2) (f-sub4 2))
     ; set psw.halt
     (set (raw-reg h-csr 16) (or psw (sll 1 11)))
     ())

(dnci sleep "sleep pipeline" (VOLATILE)
     "sleep"
     (+ MAJ_7 (f-rn 0) (f-rm 6) (f-sub4 2))
     (c-call VOID "do_sleep")
     ())

(dnci swi "software interrupt" (MAY_TRAP VOLATILE)
     "swi $uimm2"
     (+ MAJ_7 (f-rn 0) (f-8 0) (f-9 0) uimm2 (f-sub4 6))
     (cond
      ((eq uimm2 0) (set exc (or exc (sll 1 4))))
      ((eq uimm2 1) (set exc (or exc (sll 1 5))))
      ((eq uimm2 2) (set exc (or exc (sll 1 6))))
      ((eq uimm2 3) (set exc (or exc (sll 1 7)))))
     ())

(dnci break "break exception" (MAY_TRAP VOLATILE)
     "break"
     (+ MAJ_7 (f-rn 0) (f-rm 3) (f-sub4 2))
     (set pc (c-call USI "break_exception" pc))
     ((mep (unit u-exec)
	   (unit u-branch))))

(dnci syncm "synchronise with memory" (VOLATILE)
     "syncm"
     (+ MAJ_7 (f-rn 0) (f-rm 1) (f-sub4 1))
     (unimp "syncm")
     ())

(dnci stcb "store in control bus space" (VOLATILE (STALL STCB))
     "stcb $rn,$uimm16"
     (+ MAJ_15 rn (f-rm 0) (f-sub4 4) uimm16)
     (c-call VOID "do_stcb" rn uimm16)
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec)
	   (unit u-stcb))))

(dnci ldcb "load from control bus space" (VOLATILE (STALL LDCB) (LATENCY 3))
     "ldcb $rn,$uimm16"
     (+ MAJ_15 rn (f-rm 1) (f-sub4 4) uimm16)
     (set rn (c-call SI "do_ldcb" uimm16))
      ((mep (unit u-ldcb)
	    (unit u-exec)
	    (unit u-ldcb-gpr (out loadreg rn)))))


; Bit manipulation instructions.
; The following instructions become the reserved instruction when the
; bit manipulation option is off.

(dnci bsetm "set bit in memory" (OPTIONAL_BIT_INSN)
     "bsetm ($rma),$uimm3"
     (+ MAJ_2 (f-4 0) uimm3 rma (f-sub4 0))
     (sequence ()
	       (c-call "check_option_bit" pc)
	       (set (mem UQI rma) (or (mem UQI rma) (sll 1 uimm3))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci bclrm "clear bit in memory" (OPTIONAL_BIT_INSN)
     "bclrm ($rma),$uimm3"
     (+ MAJ_2 (f-4 0) uimm3 rma (f-sub4 1))
     (sequence ()
	       (c-call "check_option_bit" pc)
	       (set (mem UQI rma) (and (mem UQI rma) (inv (sll 1 uimm3)))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci bnotm "toggle bit in memory" (OPTIONAL_BIT_INSN)
     "bnotm ($rma),$uimm3"
     (+ MAJ_2 (f-4 0) uimm3 rma (f-sub4 2))
     (sequence ()
	       (c-call "check_option_bit" pc)
	       (set (mem UQI rma) (xor (mem UQI rma) (sll 1 uimm3))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci btstm "test bit in memory" (OPTIONAL_BIT_INSN)
     "btstm \\$0,($rma),$uimm3"
     (+ MAJ_2 (f-4 0) uimm3 rma (f-sub4 3))
     (sequence ()
	       (c-call "check_option_bit" pc)
	       (set r0 (zext SI (and UQI (mem UQI rma) (sll 1 uimm3)))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci tas "test and set" (OPTIONAL_BIT_INSN)
     "tas $rn,($rma)"
     (+ MAJ_2 rn rma (f-sub4 4))
     (sequence ((SI result))
	       (c-call "check_option_bit" pc)
	       (set result (zext SI (mem UQI rma)))
	       (set (mem UQI rma) 1)
	       (set rn result))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))


; Data cache instruction.

(dnci cache "cache operations" (VOLATILE)
     "cache $cimm4,($rma)"
     (+ MAJ_7 cimm4 rma (f-sub4 4))
     (c-call VOID "do_cache" cimm4 rma pc)
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))


; Multiply instructions.
; These instructions become the RI when the 32-bit multiply
; instruction option is off.

(dnci mul "multiply" (OPTIONAL_MUL_INSN (STALL MUL))
     "mul $rn,$rm"
     (+ MAJ_1 rn rm (f-sub4 4))
     (sequence ((DI result))
	       (c-call "check_option_mul" pc)
	       (set result (mul (ext DI rn) (ext DI rm)))
	       (set hi (subword SI result 0))
	       (set lo (subword SI result 1)))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-multiply))))

(dnci mulu "multiply unsigned" (OPTIONAL_MUL_INSN (STALL MUL))
     "mulu $rn,$rm"
     (+ MAJ_1 rn rm (f-sub4 5))
     (sequence ((DI result))
	       (c-call "check_option_mul" pc)
	       (set result (mul (zext UDI rn) (zext UDI rm)))
	       (set hi (subword SI result 0))
	       (set lo (subword SI result 1)))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-multiply))))

(dnci mulr "multiply, lo -> reg" (OPTIONAL_MUL_INSN (STALL MULR) (LATENCY 3))
     "mulr $rn,$rm"
     (+ MAJ_1 rn rm (f-sub4 6))
     (sequence ((DI result))
	       (c-call "check_option_mul" pc)
	       (set result (mul (ext DI rn) (ext DI rm)))
	       (set hi (subword SI result 0))
	       (set lo (subword SI result 1))
	       (set rn (subword SI result 1)))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-multiply)
	   (unit u-mul-gpr (out resultreg rn)))))

(dnci mulru "multiply unsigned, lo -> reg" (OPTIONAL_MUL_INSN (STALL MULR) (LATENCY 3))
     "mulru $rn,$rm"
     (+ MAJ_1 rn rm (f-sub4 7))
     (sequence ((DI result))
	       (c-call "check_option_mul" pc)
	       (set result (mul (zext UDI rn) (zext UDI rm)))
	       (set hi (subword SI result 0))
	       (set lo (subword SI result 1))
	       (set rn (subword SI result 1)))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-multiply)
	   (unit u-mul-gpr (out resultreg rn)))))

(dnci madd "multiply accumulate" (OPTIONAL_MUL_INSN (STALL MUL))
     "madd $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 #x3004))
     (sequence ((DI result))
	       (c-call "check_option_mul" pc)
	       (set result (or (sll (zext DI hi) 32) (zext DI lo)))
	       (set result (add result (mul (ext DI rn) (ext DI rm))))
	       (set hi (subword SI result 0))
	       (set lo (subword SI result 1)))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-multiply))))

(dnci maddu "multiply accumulate unsigned" (OPTIONAL_MUL_INSN (STALL MUL))
     "maddu $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 #x3005))
     (sequence ((DI result))
	       (c-call "check_option_mul" pc)
	       (set result (or (sll (zext DI hi) 32) (zext DI lo)))
	       (set result (add result (mul (zext UDI rn) (zext UDI rm))))
	       (set hi (subword SI result 0))
	       (set lo (subword SI result 1)))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-multiply))))


(dnci maddr "multiply accumulate, lo -> reg" (OPTIONAL_MUL_INSN (STALL MULR) (LATENCY 3))
     "maddr $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 #x3006))
     (sequence ((DI result))
	       (c-call "check_option_mul" pc)
	       (set result (or (sll (zext DI hi) 32) (zext DI lo)))
	       (set result (add result (mul (ext DI rn) (ext DI rm))))
	       (set hi (subword SI result 0))
	       (set lo (subword SI result 1))
	       (set rn (subword SI result 1)))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-multiply)
	   (unit u-mul-gpr (out resultreg rn)))))

(dnci maddru "multiple accumulate unsigned, lo -> reg" (OPTIONAL_MUL_INSN (STALL MULR) (LATENCY 3))
     "maddru $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 #x3007))
     (sequence ((DI result))
	       (c-call "check_option_mul" pc)
	       (set result (or (sll (zext DI hi) 32) (zext DI lo)))
	       (set result (add result (mul (zext UDI rn) (zext UDI rm))))
	       (set hi (subword SI result 0))
	       (set lo (subword SI result 1))
	       (set rn (subword SI result 1)))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-multiply)
	   (unit u-mul-gpr (out resultreg rn)))))


; Divide instructions.
; These instructions become the RI when the 32-bit divide instruction
; option is off.

(dnci div "divide" (OPTIONAL_DIV_INSN (STALL DIV) (LATENCY 34) MAY_TRAP)
     "div $rn,$rm"
     (+ MAJ_1 rn rm (f-sub4 8))
     (sequence ()
	       (c-call "check_option_div" pc)
	       (if (eq rm 0)
		   (set pc (c-call USI "zdiv_exception" pc))
		   ; Special case described on p. 76.
		   (if (and (eq rn #x80000000)
			    (eq rm #xffffffff))
		       (sequence ()
				 (set lo #x80000000)
				 (set hi 0))
		       (sequence ()
				 (set lo (div rn rm))
				 (set hi (mod rn rm))))))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-divide)
           (unit u-branch))))

(dnci divu "divide unsigned" (OPTIONAL_DIV_INSN (STALL DIV) (LATENCY 34) MAY_TRAP)
     "divu $rn,$rm"
     (+ MAJ_1 rn rm (f-sub4 9))
     (sequence ()
	       (c-call "check_option_div" pc)
	       (if (eq rm 0)
		   (set pc (c-call USI "zdiv_exception" pc))
		   (sequence ()
			     (set lo (udiv rn rm))
			     (set hi (umod rn rm)))))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-divide)
           (unit u-branch))))


; Debug functions.
; These instructions become the RI when the debug function option is
; off.

(dnci dret "return from debug exception" (OPTIONAL_DEBUG_INSN)
     "dret"
     (+ MAJ_7 (f-rn 0) (f-rm 1) (f-sub4 3))
     (sequence ()
	       (c-call "check_option_debug" pc)
	       ; set DBG.DM.
	       (set dbg (and dbg (inv (sll SI 1 15))))
	       (set pc depc))
     ((mep (unit u-exec)
	   (unit u-branch))))

(dnci dbreak "generate debug exception" (OPTIONAL_DEBUG_INSN MAY_TRAP VOLATILE)
     "dbreak"
     (+ MAJ_7 (f-rn 0) (f-rm 3) (f-sub4 3))
     (sequence ()
	       (c-call "check_option_debug" pc)
	       ; set DBG.DPB.
	       (set dbg (or dbg 1)))
     ())


; Leading zero instruction.

(dnci ldz "leading zeroes" (OPTIONAL_LDZ_INSN (STALL INT2))
     "ldz $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 0))
     (sequence ()
	       (c-call "check_option_ldz" pc)
	       (set rn (c-call SI "do_ldz" rm)))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-exec))))


; Absolute difference instruction.

(dnci abs "absolute difference" (OPTIONAL_ABS_INSN (STALL INT2))
     "abs $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 3))
     (sequence ()
	       (c-call "check_option_abs" pc)
	       (set rn (abs (sub rn rm))))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))


; Average instruction.

(dnci ave "average" (OPTIONAL_AVE_INSN (STALL INT2))
     "ave $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 2))
     (sequence ()
	       (c-call "check_option_ave" pc)
	       (set rn (sra (add (add rn rm) 1) 1)))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))


; MIN/MAX instructions.

(dnci min "minimum" (OPTIONAL_MINMAX_INSN (STALL INT2))
     "min $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 4))
     (sequence ()
	       (c-call "check_option_minmax" pc)
	       (if (gt rn rm)
		   (set rn rm)))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci max "maximum" (OPTIONAL_MINMAX_INSN (STALL INT2))
     "max $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 5))
     (sequence ()
	       (c-call "check_option_minmax" pc)
	       (if (lt rn rm)
		   (set rn rm)))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci minu "minimum unsigned" (OPTIONAL_MINMAX_INSN (STALL INT2))
     "minu $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 6))
     (sequence ()
	       (c-call "check_option_minmax" pc)
	       (if (gtu rn rm)
		   (set rn rm)))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci maxu "maximum unsigned" (OPTIONAL_MINMAX_INSN (STALL INT2))
     "maxu $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 7))
     (sequence ()
	       (c-call "check_option_minmax" pc)
	       (if (ltu rn rm)
		   (set rn rm)))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))


; Clipping instruction.

(dnci clip "clip" (OPTIONAL_CLIP_INSN (STALL INT2))
     "clip $rn,$cimm5"
     (+ MAJ_15 rn (f-rm 0) (f-sub4 1) (f-ext #x10) cimm5 (f-29 0) (f-30 0) (f-31 0))
     (sequence ((SI min) (SI max))
	       (c-call "check_option_clip" pc)
	       (set max (sub (sll 1 (sub cimm5 1)) 1))
	       (set min (neg (sll 1 (sub cimm5 1))))
	       (cond
		((eq cimm5 0) (set rn 0))
		((gt rn max) (set rn max))
		((lt rn min) (set rn min))))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci clipu "clip unsigned" (OPTIONAL_CLIP_INSN (STALL INT2))
     "clipu $rn,$cimm5"
     (+ MAJ_15 rn (f-rm 0) (f-sub4 1) (f-ext #x10) cimm5 (f-29 0) (f-30 0) (f-31 1))
     (sequence ((SI max))
	       (c-call "check_option_clip" pc)
	       (set max (sub (sll 1 cimm5) 1))
	       (cond
		((eq cimm5 0) (set rn 0))
		((gt rn max) (set rn max))
		((lt rn 0) (set rn 0))))
     ((mep (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))


; Saturation instructions.

(dnci sadd "saturating addition" (OPTIONAL_SAT_INSN (STALL INT2))
     "sadd $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 8))
     (sequence ()
	       (c-call "check_option_sat" pc)
	       (if (add-oflag rn rm 0)
		   (if (nflag rn)
		       ; underflow
		       (set rn (neg (sll 1 31)))
		       ; overflow
		       (set rn (sub (sll 1 31) 1)))
		   (set rn (add rn rm))))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci ssub "saturating subtraction" (OPTIONAL_SAT_INSN (STALL INT2))
     "ssub $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 10))
     (sequence ()
	       (c-call "check_option_sat" pc)
	       (if (sub-oflag rn rm 0)
		   (if (nflag rn)
		       ; underflow
		       (set rn (neg (sll 1 31)))
		       ; overflow
		       (set rn (sub (sll 1 31) 1)))
		   (set rn (sub rn rm))))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci saddu "saturating unsigned addition" (OPTIONAL_SAT_INSN (STALL INT2))
     "saddu $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 9))
     (sequence ()
	       (c-call "check_option_sat" pc)
	       (if (add-cflag rn rm 0)
		   (set rn (inv 0))
		   (set rn (add rn rm))))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))

(dnci ssubu "saturating unsigned subtraction" (OPTIONAL_SAT_INSN (STALL INT2))
     "ssubu $rn,$rm"
     (+ MAJ_15 rn rm (f-sub4 1) (f-16u16 11))
     (sequence ()
	       (c-call "check_option_sat" pc)
	       (if (sub-cflag rn rm 0)
		   (set rn 0)
		   (set rn (sub rn rm))))
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-use-gpr (in usereg rn))
	   (unit u-exec))))


; UCI and DSP options are defined in an external file.
; See `mep-sample-ucidsp.cpu' for a sample.


; Coprocessor instructions.

(dnci swcp "store word coprocessor" (OPTIONAL_CP_INSN (STALL STORE))
     "swcp $crn,($rma)"
     (+ MAJ_3 crn rma (f-sub4 8))
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call VOID "check_write_to_text" (and rma (inv SI 3)))
	       (set (mem SI (and rma (inv SI 3))) crn))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lwcp "load word coprocessor" (OPTIONAL_CP_INSN (STALL LOAD))
     "lwcp $crn,($rma)"
     (+ MAJ_3 crn rma (f-sub4 9))
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (set crn (mem SI (and rma (inv SI 3)))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci smcp "smcp" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN (STALL STORE))
     "smcp $crn64,($rma)"
     (+ MAJ_3 crn64 rma (f-sub4 10))
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call "check_option_cp64" pc)
	       (c-call VOID "check_write_to_text" rma)
	       (c-call "do_smcp" rma crn64 pc))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lmcp "lmcp" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN (STALL LOAD))
     "lmcp $crn64,($rma)"
     (+ MAJ_3 crn64 rma (f-sub4 11))
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call "check_option_cp64" pc)
	       (set crn64 (c-call DI "do_lmcp" rma pc)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci swcpi "swcp (post-increment)" (OPTIONAL_CP_INSN (STALL STORE))
     "swcpi $crn,($rma+)"
     (+ MAJ_3 crn rma (f-sub4 0))
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call VOID "check_write_to_text" (and rma (inv SI 3)))
	       (set (mem SI (and rma (inv SI 3))) crn)
	       (set rma (add rma 4)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lwcpi "lwcp (post-increment)" (OPTIONAL_CP_INSN (STALL LOAD))
     "lwcpi $crn,($rma+)"
     (+ MAJ_3 crn rma (f-sub4 1))
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (set crn (mem SI (and rma (inv SI 3))))
	       (set rma (add rma 4)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci smcpi "smcp (post-increment)" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN (STALL STORE))
     "smcpi $crn64,($rma+)"
     (+ MAJ_3 crn64 rma (f-sub4 2))
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call "check_option_cp64" pc)
	       (c-call VOID "check_write_to_text" rma)
	       (c-call "do_smcpi" (index-of rma) crn64 pc)
	       (set rma rma)) ; reference as output for intrinsic generation
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lmcpi "lmcp (post-increment)" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN (STALL LOAD))
     "lmcpi $crn64,($rma+)"
     (+ MAJ_3 crn64 rma (f-sub4 3))
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call "check_option_cp64" pc)
	       (set crn64 (c-call DI "do_lmcpi" (index-of rma) pc))
	       (set rma rma)) ; reference as output for intrinsic generation
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci swcp16 "swcp (16-bit displacement)" (OPTIONAL_CP_INSN (STALL STORE))
     "swcp $crn,$sdisp16($rma)"
     (+ MAJ_15 crn rma (f-sub4 12) sdisp16)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (set (mem SI (and (add rma sdisp16) (inv SI 3))) crn))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lwcp16 "lwcp (16-bit displacement)" (OPTIONAL_CP_INSN (STALL LOAD))
     "lwcp $crn,$sdisp16($rma)"
     (+ MAJ_15 crn rma (f-sub4 13) sdisp16)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (set crn (mem SI (and (add rma sdisp16) (inv SI 3)))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci smcp16 "smcp (16-bit displacement)" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN (STALL STORE))
     "smcp $crn64,$sdisp16($rma)"
     (+ MAJ_15 crn64 rma (f-sub4 14) sdisp16)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call "check_option_cp64" pc)
	       (c-call "do_smcp16" rma sdisp16 crn64 pc))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lmcp16 "lmcp (16-bit displacement)" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN (STALL LOAD))
     "lmcp $crn64,$sdisp16($rma)"
     (+ MAJ_15 crn64 rma (f-sub4 15) sdisp16)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call "check_option_cp64" pc)
	       (set crn64 (c-call DI "do_lmcp16" rma sdisp16 pc)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci sbcpa "store byte coprocessor" (OPTIONAL_CP_INSN (STALL STORE))
     "sbcpa $crn,($rma+),$cdisp10"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 0) (f-ext62 0) cdisp10)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call VOID "check_write_to_text" rma)
	       (set (mem QI rma) (and crn #xff))
	       (set rma (add rma (ext SI cdisp10))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lbcpa "load byte coprocessor" (OPTIONAL_CP_INSN (STALL LOAD))
     "lbcpa $crn,($rma+),$cdisp10"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x4) (f-ext62 #x0) cdisp10)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (set crn (ext SI (mem QI rma)))
	       (set rma (add rma (ext SI cdisp10))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci shcpa "store half-word coprocessor" (OPTIONAL_CP_INSN (STALL STORE))
     "shcpa $crn,($rma+),$cdisp10a2"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x1) (f-ext62 #x0) cdisp10a2)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call VOID "check_write_to_text" (and rma (inv SI 1)))
	       (set (mem HI (and rma (inv SI 1))) (and crn #xffff))
	       (set rma (add rma (ext SI cdisp10a2))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lhcpa "load half-word coprocessor" (OPTIONAL_CP_INSN (STALL LOAD))
     "lhcpa $crn,($rma+),$cdisp10a2"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x5) (f-ext62 #x0) cdisp10a2)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (set crn (ext SI (mem HI (and rma (inv SI 1)))))
	       (set rma (add rma (ext SI cdisp10a2))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci swcpa "store word coprocessor" (OPTIONAL_CP_INSN (STALL STORE))
     "swcpa $crn,($rma+),$cdisp10a4"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x2) (f-ext62 #x0) cdisp10a4)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call VOID "check_write_to_text" (and rma (inv SI 3)))
	       (set (mem SI (and rma (inv SI 3))) crn)
	       (set rma (add rma (ext SI cdisp10a4))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lwcpa "load word coprocessor" (OPTIONAL_CP_INSN (STALL LOAD))
     "lwcpa $crn,($rma+),$cdisp10a4"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x6) (f-ext62 #x0) cdisp10a4)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (set crn (mem SI (and rma (inv SI 3))))
	       (set rma (add rma (ext SI cdisp10a4))))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci smcpa "smcpa" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN (STALL STORE))
     "smcpa $crn64,($rma+),$cdisp10a8"
     (+ MAJ_15 crn64 rma (f-sub4 5) (f-ext4 #x3) (f-ext62 #x0) cdisp10a8)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call "check_option_cp64" pc)
	       (c-call VOID "check_write_to_text" rma)
	       (c-call "do_smcpa" (index-of rma) cdisp10a8 crn64 pc)
	       (set rma rma)) ; reference as output for intrinsic generation
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lmcpa "lmcpa" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN (STALL LOAD))
     "lmcpa $crn64,($rma+),$cdisp10a8"
     (+ MAJ_15 crn64 rma (f-sub4 5) (f-ext4 #x7) (f-ext62 #x0) cdisp10a8)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call "check_option_cp64" pc)
	       (set crn64 (c-call DI "do_lmcpa" (index-of rma) cdisp10a8 pc))
	       (set rma rma)) ; reference as output for intrinsic generation
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))


(dnci sbcpm0 "sbcpm0" (OPTIONAL_CP_INSN)
     "sbcpm0 $crn,($rma+),$cdisp10"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x0) (f-ext62 #x2) cdisp10)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call VOID "check_write_to_text" rma)
	       (set (mem QI rma) (and crn #xff))
	       (set rma (mod0 cdisp10)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lbcpm0 "lbcpm0" (OPTIONAL_CP_INSN)
     "lbcpm0 $crn,($rma+),$cdisp10"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x4) (f-ext62 #x2) cdisp10)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (set crn (ext SI (mem QI rma)))
	       (set rma (mod0 cdisp10)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci shcpm0 "shcpm0" (OPTIONAL_CP_INSN)
     "shcpm0 $crn,($rma+),$cdisp10a2"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x1) (f-ext62 #x2) cdisp10a2)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call VOID "check_write_to_text" (and rma (inv SI 1)))
	       (set (mem HI (and rma (inv SI 1))) (and crn #xffff))
	       (set rma (mod0 cdisp10a2)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lhcpm0 "lhcpm0" (OPTIONAL_CP_INSN)
     "lhcpm0 $crn,($rma+),$cdisp10a2"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x5) (f-ext62 #x2) cdisp10a2)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (set crn (ext SI (mem HI (and rma (inv SI 1)))))
	       (set rma (mod0 cdisp10a2)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci swcpm0 "swcpm0" (OPTIONAL_CP_INSN)
     "swcpm0 $crn,($rma+),$cdisp10a4"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x2) (f-ext62 #x2) cdisp10a4)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call VOID "check_write_to_text" (and rma (inv SI 3)))
	       (set (mem SI (and rma (inv SI 3))) crn)
	       (set rma (mod0 cdisp10a4)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lwcpm0 "lwcpm0" (OPTIONAL_CP_INSN)
     "lwcpm0 $crn,($rma+),$cdisp10a4"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x6) (f-ext62 #x2) cdisp10a4)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (set crn (mem SI (and rma (inv SI 3))))
	       (set rma (mod0 cdisp10a4)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci smcpm0 "smcpm0" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN)
     "smcpm0 $crn64,($rma+),$cdisp10a8"
     (+ MAJ_15 crn64 rma (f-sub4 5) (f-ext4 #x3) (f-ext62 #x2) cdisp10a8)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call "check_option_cp64" pc)
	       (c-call VOID "check_write_to_text" rma)
	       (c-call "do_smcp" rma crn64 pc)
	       (set rma (mod0 cdisp10a8)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lmcpm0 "lmcpm0" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN)
     "lmcpm0 $crn64,($rma+),$cdisp10a8"
     (+ MAJ_15 crn64 rma (f-sub4 5) (f-ext4 #x7) (f-ext62 #x2) cdisp10a8)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call "check_option_cp64" pc)
	       (set crn64 (c-call DI "do_lmcp" rma pc))
	       (set rma (mod0 cdisp10a8)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci sbcpm1 "sbcpm1" (OPTIONAL_CP_INSN)
     "sbcpm1 $crn,($rma+),$cdisp10"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x0) (f-ext62 #x3) cdisp10)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call VOID "check_write_to_text" rma)
	       (set (mem QI rma) (and crn #xff))
	       (set rma (mod1 cdisp10)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lbcpm1 "lbcpm1" (OPTIONAL_CP_INSN)
     "lbcpm1 $crn,($rma+),$cdisp10"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x4) (f-ext62 #x3) cdisp10)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (set crn (ext SI (mem QI rma)))
	       (set rma (mod1 cdisp10)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci shcpm1 "shcpm1" (OPTIONAL_CP_INSN)
     "shcpm1 $crn,($rma+),$cdisp10a2"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x1) (f-ext62 #x3) cdisp10a2)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call VOID "check_write_to_text" (and rma (inv SI 1)))
	       (set (mem HI (and rma (inv SI 1))) (and crn #xffff))
	       (set rma (mod1 cdisp10a2)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lhcpm1 "lhcpm1" (OPTIONAL_CP_INSN)
     "lhcpm1 $crn,($rma+),$cdisp10a2"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x5) (f-ext62 #x3) cdisp10a2)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (set crn (ext SI (mem HI (and rma (inv SI 1)))))
	       (set rma (mod1 cdisp10a2)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci swcpm1 "swcpm1" (OPTIONAL_CP_INSN)
     "swcpm1 $crn,($rma+),$cdisp10a4"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x2) (f-ext62 #x3) cdisp10a4)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call VOID "check_write_to_text" (and rma (inv SI 3)))
	       (set (mem SI (and rma (inv SI 3))) crn)
	       (set rma (mod1 cdisp10a4)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lwcpm1 "lwcpm1" (OPTIONAL_CP_INSN)
     "lwcpm1 $crn,($rma+),$cdisp10a4"
     (+ MAJ_15 crn rma (f-sub4 5) (f-ext4 #x6) (f-ext62 #x3) cdisp10a4)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (set crn (ext SI (mem SI (and rma (inv SI 3)))))
	       (set rma (mod1 cdisp10a4)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci smcpm1 "smcpm1" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN)
     "smcpm1 $crn64,($rma+),$cdisp10a8"
     (+ MAJ_15 crn64 rma (f-sub4 5) (f-ext4 #x3) (f-ext62 #x3) cdisp10a8)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call "check_option_cp64" pc)
	       (c-call "do_smcp" rma crn64 pc)
	       (c-call VOID "check_write_to_text" rma)
	       (set rma (mod1 cdisp10a8)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnci lmcpm1 "lmcpm1" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN)
     "lmcpm1 $crn64,($rma+),$cdisp10a8"
     (+ MAJ_15 crn64 rma (f-sub4 5) (f-ext4 #x7) (f-ext62 #x3) cdisp10a8)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (c-call "check_option_cp64" pc)
	       (set crn64 (c-call DI "do_lmcp" rma pc))
	       (set rma (mod1 cdisp10a8)))
     ((mep (unit u-use-gpr (in usereg rma))
	   (unit u-exec))))

(dnop cp_flag       "branch condition register"  (all-mep-isas) h-ccr   1)

(dnci bcpeq "branch coprocessor equal" (OPTIONAL_CP_INSN RELAXABLE)
     "bcpeq $cccc,$pcrel17a2"
     (+ MAJ_13 (f-rn 8) cccc (f-sub4 4) pcrel17a2)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (if (eq (xor cccc cp_flag) 0)
	       (set-vliw-alignment-modified pc pcrel17a2)))
     ())

(dnci bcpne "branch coprocessor not equal" (OPTIONAL_CP_INSN RELAXABLE)
     "bcpne $cccc,$pcrel17a2"
     (+ MAJ_13 (f-rn 8) cccc (f-sub4 5) pcrel17a2)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (if (ne (xor cccc cp_flag) 0)
	       (set-vliw-alignment-modified pc pcrel17a2)))
     ())

(dnci bcpat "branch coprocessor and true" (OPTIONAL_CP_INSN RELAXABLE)
     "bcpat $cccc,$pcrel17a2"
     (+ MAJ_13 (f-rn 8) cccc (f-sub4 6) pcrel17a2)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (if (ne (and cccc cp_flag) 0)
	       (set-vliw-alignment-modified pc pcrel17a2)))
     ())

(dnci bcpaf "branch coprocessor and false" (OPTIONAL_CP_INSN RELAXABLE)
     "bcpaf $cccc,$pcrel17a2"
     (+ MAJ_13 (f-rn 8) cccc (f-sub4 7) pcrel17a2)
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (if (eq (and cccc cp_flag) 0)
	       (set-vliw-alignment-modified pc pcrel17a2)))
     ())

(dnci synccp "synchronise with coprocessor" (OPTIONAL_CP_INSN)
     "synccp"
     (+ MAJ_7 (f-rn 0) (f-rm 2) (f-sub4 1))
     (sequence ()
	       (c-call "check_option_cp" pc)
	       (unimp "synccp"))
     ())

(dnci jsrv "jump to vliw subroutine " (OPTIONAL_CP_INSN)
     "jsrv $rm"
     (+ MAJ_1 (f-rn 8) rm (f-sub4 15))
     (sequence ()
	       (cg-profile pc rm)
	       (c-call "check_option_cp" pc)
	       (core-vliw-switch

		;; in core operating mode
		(sequence ()
			  (set lp (or (add pc 2) 1))
			  (set-vliw-aliignment-modified-by-option pc rm)
			  (set-psw.om 1)) ;; to VLIW operation mode

		;; in VLIW32 operating mode
		(sequence ()
			  (set lp (or (add pc 4) 1))
			  (set pc (and rm (inv 1)))
			  (set-psw.om 0)) ;; to core operation mode

		;; in VLIW64 operating mode
		(sequence ()
			  (set lp (or (add pc 8) 1))
			  (set pc (and rm (inv 1)))
			  (set-psw.om 0)))) ;; to core operation mode
     ((mep (unit u-use-gpr (in usereg rm))
	   (unit u-exec)
	   (unit u-branch))))

(dnci bsrv "branch to vliw subroutine" (OPTIONAL_CP_INSN)
     "bsrv $pcrel24a2"
     (+ MAJ_13 (f-4 1) (f-sub4 11) pcrel24a2)
     (sequence ()
	       (cg-profile pc pcrel24a2)
	       (c-call "check_option_cp" pc)
	       (core-vliw-switch

		;; in core operating mode
		(sequence ()
			  (set lp (or (add pc 4) 1))
			  (set-vliw-aliignment-modified-by-option pc pcrel24a2)
			  (set-psw.om 1)) ;; to VLIW operation mode

		;; in VLIW32 operating mode
		(sequence ()
			  (set lp (or (add pc 4) 1))
			  (set pc (and pcrel24a2 (inv 1)))
			  (set-psw.om 0)) ;; to core operation mode

		;; in VLIW64 operating mode
		(sequence ()
			  (set lp (or (add pc 8) 1))
			  (set pc (and pcrel24a2 (inv 1)))
			  (set-psw.om 0)))) ;; to core operation mode
     ((mep (unit u-exec)
	   (unit u-branch))))


; An instruction for test instrumentation.
; Using a reserved opcode.

(dnci sim-syscall "simulator system call" ()
     "--syscall--"
     (+ MAJ_7 (f-4 1) callnum (f-8 0) (f-9 0) (f-10 0) (f-sub4 0))
     (c-call "do_syscall" pc callnum)
     ())

(define-pmacro (dnri n major minor)
  (dnci (.sym ri- n) "reserved instruction" ()
	"--reserved--"
	(+ major rn rm (f-sub4 minor))
	(set pc (c-call USI "ri_exception" pc))
	((mep (unit u-exec)
	      (unit u-branch)))))

(dnri 0  MAJ_0   6)
(dnri 1  MAJ_1  10)
(dnri 2  MAJ_1  11)
(dnri 3  MAJ_2   5)
(dnri 4  MAJ_2   8)
(dnri 5  MAJ_2   9)
(dnri 6  MAJ_2  10)
(dnri 7  MAJ_2  11)
(dnri 8  MAJ_3   4)
(dnri 9  MAJ_3   5)
(dnri 10 MAJ_3   6)
(dnri 11 MAJ_3   7)
(dnri 12 MAJ_3  12)
(dnri 13 MAJ_3  13)
(dnri 14 MAJ_3  14)
(dnri 15 MAJ_3  15)
(dnri 17 MAJ_7   7)
(dnri 20 MAJ_7  14)
(dnri 21 MAJ_7  15)
(dnri 22 MAJ_12  7)
(dnri 23 MAJ_14 13)
;(dnri 24 MAJ_15  3)
(dnri 26 MAJ_15  8)
; begin core-specific reserved insns
; end core-specific reserved insns


; Macro instructions.

(dnmi nop "nop"
      ()
      "nop"
      (emit mov (rn 0) (rm 0)))

; Emit the 16 bit form of these 32 bit insns when the displacement is zero.
;
(dncmi sb16-0 "store byte (explicit 16 bit displacement of zero)" (NO-DIS)
     "sb $rnc,$zero($rma)"
     (emit sb rnc rma))

(dncmi sh16-0 "store half (explicit 16 bit displacement of zero)" (NO-DIS)
     "sh $rns,$zero($rma)"
     (emit sh rns rma))

(dncmi sw16-0 "store word (explicit 16 bit displacement of zero)" (NO-DIS)
     "sw $rnl,$zero($rma)"
     (emit sw rnl rma))

(dncmi lb16-0 "load byte (explicit 16 bit displacement of zero)" (NO-DIS)
     "lb $rnc,$zero($rma)"
     (emit lb rnc rma))

(dncmi lh16-0 "load half (explicit 16 bit displacement of zero)" (NO-DIS)
     "lh $rns,$zero($rma)"
     (emit lh rns rma))

(dncmi lw16-0 "load word (explicit 16 bit displacement of zero)" (NO-DIS)
     "lw $rnl,$zero($rma)"
     (emit lw rnl rma))

(dncmi lbu16-0 "load unsigned byte (explicit 16 bit displacement of zero)" (NO-DIS)
     "lbu $rnuc,$zero($rma)"
     (emit lbu rnuc rma))

(dncmi lhu16-0 "load unsigned half (explicit 16 bit displacement of zero)" (NO-DIS)
     "lhu $rnus,$zero($rma)"
     (emit lhu rnus rma))

(dncmi swcp16-0 "swcp (explicit 16-bit displacement of zero)" (OPTIONAL_CP_INSN NO-DIS)
     "swcp $crn,$zero($rma)"
     (emit swcp crn rma))

(dncmi lwcp16-0 "lwcp (explicit 16-bit displacement of zero)" (OPTIONAL_CP_INSN NO-DIS)
     "lwcp $crn,$zero($rma)"
     (emit lwcp crn rma))

(dncmi smcp16-0 "smcp (explicit 16-bit displacement of zero)" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN NO-DIS)
     "smcp $crn64,$zero($rma)"
     (emit smcp crn64 rma))

(dncmi lmcp16-0 "lmcp (explicit 16-bit displacement of zero)" (OPTIONAL_CP_INSN OPTIONAL_CP64_INSN NO-DIS)
     "lmcp $crn64,$zero($rma)"
     (emit lmcp crn64 rma))