summaryrefslogtreecommitdiffstats
path: root/src/usr/pore/poreve/pore_model/ibuf/pore_model.c
blob: a69b087b0ccaa48d5a04fd4f89c8ef0162913e66 (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
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/pore/poreve/pore_model/ibuf/pore_model.c $            */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2012,2014              */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
// $Id: pore_model.c,v 1.26 2013/11/27 15:52:41 thi Exp $
/******************************************************************************
 *
 * Virtual PORe Engine
 *
 *****************************************************************************/

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "pore_regs.h"
#include "pore_ibuf.h"
#include "pore_model.h"
#include "pore_inline_decode.h"

static int fetch(pore_model_t p);
static int decode(pore_model_t p);
static int execute(pore_model_t p);
static int finishBrokenInstruction(pore_model_t p);
static int __pore_flush_reset(pore_model_t p);

#define PORE_GET_BITS(X,S,N)						\
	((((~(0xFFFFFFFFFFFFFFFFull >> (N))) >> (S))			\
	  & (X)) >> (64 - (S)-(N)))
#define PORE_SET_BITS(Y,X,S,N,V)					\
	Y = (((X) & (~((~((~0x0)<<(N))) << (64-(S)-(N))))) |		\
	     (((~(0xFFFFFFFFFFFFFFFFull << (N))) & (V)) << (64 - (S)-(N))))

#define PORE_GET_BIT(X,S)     PORE_GET_BITS((X),(S),1)
#define PORE_SET_BIT(Y,X,S,V) PORE_SET_BITS(Y,(X),(S),1,(V))

/* Exception handling ********************************************************/

/// Set the PC to the Nth combined vector
static int
pore_vectorThroughTable(pore_model_t p, unsigned vector)
{
	uint32_t offset;

	if (vector >= (PORE_ERROR_VECTORS + PORE_EXE_VECTORS)) {
		BUG();
		return PORE_ERR_BUG_INVALID_VECTOR;
	}
	offset = p->table_base_addr.table_base_address +
		(vector * PORE_VECTOR_SIZE);

	p->status.pc = ((uint64_t)p->table_base_addr.memory_space << 32) +
		offset;

	dprintf(p, "%s: Jumping to vector %d @ %016llx\n",
		__func__, vector, (long long)p->status.pc);

	return PORE_SUCCESS;
}

/// Set the PC to an error vector
static inline int
pore_errorVector(pore_model_t p, unsigned error)
{
	return pore_vectorThroughTable(p, error);
}

/// Set the PC to an EXE vector
static int
pore_exeVector(pore_model_t p, unsigned vector)
{
	return pore_vectorThroughTable(p, PORE_ERROR_VECTORS + vector);
}

/* Support read/write registers with side effects ****************************/

static inline int
pore_stack0_reg_write(pore_model_t p, uint64_t val, uint64_t mask)
{
	int me = PORE_SUCCESS;
	int newSp;
	pore_pc_stack0_reg pps0;

	val &= PORE_PC_STACK0_VALID_BITS;
	p->pc_stack[0].val = ((val		  &  mask) |
			      (p->pc_stack[0].val & ~mask));
	p->pc_stack[0].set_stack_pointer = 0;
	p->pc_stack[0].new_stack_pointer = 0;

	pps0.val = val & mask;
	if (pps0.set_stack_pointer) {
		newSp = pps0.new_stack_pointer;
		if ((newSp != 1) &&
		    (newSp != 2) &&
		    (newSp != 4) &&
		    (newSp != 8)) {
			me = PORE_ERR_INVALID_STACK_POINTER;
		} else {
			p->status.stack_pointer = newSp;
		}
	}
	return me;
}

// NB: Technically, if the PORE is being held in reset no other
// register updates should go through or have effect. We don't model
// that, however, and instead just catch cases where firmware tries to
// execute PORE programs while PORE or the OCI master are in reset.

static inline int
pore_reset_reg_write(pore_model_t p, uint64_t val, uint64_t mask)
{
	pore_reset_reg reset;

	dprintf(p, "%s: val=%016llx mask=%016llx\n", __func__,
		(long long)val, (long long)mask);

	val &= PORE_RESET_VALID_BITS;
	reset.val = ((val	   &  mask) |
		     (p->reset.val & ~mask));

	if (mask & PORE_BITS_0_31) {
		if (reset.fn_reset)
			return pore_flush_reset(p);
		if (reset.oci_reset)
			return poreb_reset(p->mem);
		if (reset.restart_sbe_trigger) {
			/* FIXME SBE will reset and restart initial
			 * boot code execution
			 */
			return pore_flush_reset(p);
		}
	}

	/* The bits of this register are self clearing after the
	 * resets have been performed. Don't write bits back.
	 */
	return PORE_SUCCESS;
}

static inline void
pore_status_reg_print(pore_status_reg *status)
{
	aprintf("  PORE_STATUS %016llx\n"
		"      cur_state     = %02x freeze_action = %x\n"
		"      stack_pointer = %x  pc            = %llx\n",
		(long long)status->val,
		(unsigned int)status->cur_state,
		(unsigned int)status->freeze_action,
		(unsigned int)status->stack_pointer,
		(long long)status->pc);
}

static inline void
pore_control_reg_print(pore_control_reg *poreControl)
{
	aprintf("  PORE_CONTROL %016llx\n"
		"      start_stop    = %x continue_step = %x\n"
		"      skip	    = %x set_pc        = %x\n"
		"      lock_exe_trig = %x trap_enable   = %x\n"
		"      pc_brk_pt	    = %llx\n",
		(long long)poreControl->val,
		(unsigned int)poreControl->start_stop,
		(unsigned int)poreControl->continue_step,
		(unsigned int)poreControl->skip,
		(unsigned int)poreControl->set_pc,
		(unsigned int)poreControl->lock_exe_trig,
		(unsigned int)poreControl->trap_enable,
		(long long)poreControl->pc_brk_pt);
}


/// Control register write side effects
///
/// \bug This model is not correct (does not implement) all cases
/// regarding stopping/starting/continue/breakpoints/traps. This is a
/// low priority since OCC firmware is not expected to use these
/// features.

static inline int
pore_control_reg_write(pore_model_t p, uint64_t val, uint64_t mask)
{
	int me = PORE_SUCCESS;
	int squashWrite = 0;
	pore_control_reg poreControl;

	poreControl.val = val;

	if (p->trace_flags & PORE_TRACE_IBUF)
		pore_control_reg_print(&poreControl);

	if (p->reset.fn_reset == 1) {
		return PORE_ERR_IN_RESET;
	}

	// Move the value to a local base register for field decoding,
	// then process control actions.
	if (mask & PORE_BITS_0_31) {

		// Processing Set PC. This only happens on 64-bit
		// writes!!! And if it happens, everything else is
		// ignored. It is considered a firmware error to set
		// the PC when the hardware is not in the wait state.
		if (poreControl.set_pc && (mask & PORE_BITS_32_63)) {
			squashWrite = 1;
			if ((p->control.start_stop != 1) ||
			    (p->status.cur_state != PORE_STATE_WAIT)) {
				me = PORE_ERR_NOT_STOPPED_WAIT;
			} else {
				p->status.pc = poreControl.pc_brk_pt;
			}
		} else {
			// Processing START/STOP bit.  If this bit is
			// set the PORE stops in the WAIT state.  If
			// we were in the ABR state we have to finish
			// the instruction first.

			if (poreControl.start_stop) {
				p->status.cur_state = PORE_STATE_WAIT;
			} else if (p->status.cur_state == PORE_STATE_WAIT) {
				p->status.cur_state = PORE_STATE_EXEC;
			}

			// Hitting the continue bit will restart a
			// machine in the ABR (address breakpoint)
			// state. If in any other state we punt.

			if (poreControl.continue_step) {
				if (p->status.cur_state == PORE_STATE_ABR) {
					p->status.cur_state = PORE_STATE_EXEC;
					finishBrokenInstruction(p);

				} else {
					me = PORE_ERR_BEHAVIOR_NOT_MODELED;
				}
			}

			if (poreControl.skip) {
				me = PORE_ERR_BEHAVIOR_NOT_MODELED;
			}
		}
	}

	if (!me && !squashWrite) {

		// Clear any 'pulse-mode' bits in the register before writing
		poreControl.continue_step = 0;
		poreControl.skip = 0;
		poreControl.set_pc = 0;

		p->control.val = ((poreControl.val &  mask) | /* new val */
				  (p->control.val  & ~mask)); /* old val */

		dprintf(p, "  new control reg val=%016llx\n",
			(long long)p->control.val);
	}

	return me;
}

/// EXE_Trigger Register Write Function
///
/// This function writes to the Exe trigger register and also schedules
/// the instruction for execution when LO order word is written. No side
/// effect when HI order word is written.

static inline int
pore_exe_trigger_reg_write(pore_model_t p, uint64_t val, uint64_t mask)
{
	val &= PORE_EXE_TRIGGER_VALID_BITS;
	if (p->reset.fn_reset == 1) {
		return PORE_ERR_IN_RESET;

	}

	if (p->control.lock_exe_trig &&
	    !p->control.interrupt_sequencer_enabled) {
		return PORE_ERR_REGISTER_LOCKED;
	}

	if (mask == PORE_BITS_32_63) {
		p->exe_trigger.val = ((val		  &  mask) |
				      (p->exe_trigger.val & ~mask));
		return PORE_SUCCESS;
	}

	if (p->control.interrupt_sequencer_enabled) {
		if (p->control.pore_interruptible == 0) {
			/* enque request, remember desired exe_trigger_reg */
			p->pore_interrupt_request = 1;
			p->dbg0.interrupt_counter = 0x00;
			p->exe_trigger.val = ((val		  &  mask) |
					      (p->exe_trigger.val & ~mask));

			return PORE_SUCCESS;
		} else {
			/* else do the trigger, and wipe out the request */
			p->pore_interrupt_request = 0;
		}
	}

	p->exe_trigger.val = ((val		  &  mask) |
			      (p->exe_trigger.val & ~mask));

	/* Lock the EXE_TRIGGER register, compute the new starting PC,
	 * and begin execution.
	 */
	p->control.lock_exe_trig = 1;
	p->control.start_stop = 0;
	pore_exeVector(p, p->exe_trigger.start_vector);

	return PORE_SUCCESS;
}

/// write method for IBUF_01 register. Upon write to the ibuf_0 part
/// it executes the instruction.
/// The PORE IBUF0,1,2 registers are read-only, when Instruction
/// engine is in automatic run-mode (Control Register(0) = 0).

static int
pore_ibuf_01_reg_write(pore_model_t p, uint64_t val, uint64_t mask)
{
	int me = PORE_SUCCESS;

	if (p->reset.fn_reset == 1) {
		return PORE_ERR_IN_RESET;
	}
	if (p->control.start_stop != 1) {
		return PORE_ERR_NOT_STOPPED;
	}

	p->ibuf_01.val = ((val		  &  mask) |
			  (p->ibuf_01.val & ~mask));

	if (mask & PORE_BITS_0_31) {
		me = decode(p);
		if (me != PORE_SUCCESS)
			return me;

		me = execute(p);
		if (me != PORE_SUCCESS)
			return me;

		/* Extension for interruptible operation of SLW instance */
		if (p->control.interrupt_sequencer_enabled &&
		    p->pore_interrupt_request) {

			if (p->control.pore_interruptible) {
				__pore_flush_reset(p);
				pore_exe_trigger_reg_write(p,
							   p->exe_trigger.val,
							   PORE_BITS_0_63);
			} else {
				if (p->dbg0.interrupt_counter != 0xff)
					p->dbg0.interrupt_counter++;
			}
		}

		/* FIXME In case of stuffing instructions I strongly
		   assume that the PC must not be upated. */
	}
	return me;
}

void pore_set_trace(pore_model_t pm, uint64_t trace)
{
	pm->trace_flags = trace;
}

uint64_t pore_get_trace(pore_model_t pm)
{
	return pm->trace_flags;
}

int pore_instrHook(pore_model_t p, uint64_t addr,
		   uint32_t im24, uint64_t im64)
{
	if (!p->instrHook)
		return 0;
	return p->instrHook(p, addr, im24, im64);
}

int pore_readHook(pore_model_t p, uint64_t addr)
{
	if (!p->readHook)
		return 0;
	return p->readHook(p, addr);
}

int pore_writeHook(pore_model_t p, uint64_t addr)
{
	if (!p->writeHook)
		return 0;
	return p->writeHook(p, addr);
}

int pore_fetchHook(pore_model_t p, uint64_t addr)
{
	if (!p->fetchHook)
		return 0;
	return p->fetchHook(p, addr);
}

static int pore_decodeHook(pore_model_t p, uint8_t *instr, unsigned int size)
{
	if (!p->decodeHook)
		return 0;
	return p->decodeHook(p, instr, size);
}

static void pore_waitCallback(pore_model_t p, uint32_t delay)
{
	if (!p->waitCallback)
		return;
	p->waitCallback(p, delay);
}

static void incrPc(pore_model_t p)
{
	p->status.pc += p->opcode_len;
}

static void setJmpTarget(int offset, _PoreAddress *next_pc)
{
	next_pc->offset += offset * 4;
}

/**
 * Implements Add Operation with Status Flag Set.
 * This function received the pre constructed operands for ADD and calculates
 * ALU Status flags. It also updates the id_flags register.
 * \param op1      operand 1
 * \param op2      operand 2
 * \param result   result of operaton
 * \param opcode   opcode which was executed
 *                 (e.g. SUBx will change g* and s* flags)
 * \retval         PORE_SUCCESS
 * \todo Make match VHDL model - several issues to resolve.
 *
 * See also: Table 4.7: Ibuf internal register: ALU flags and Ibuf-ID.
 */
static int setAluFlags(pore_model_t p, int64_t op1, int64_t op2,
		       int64_t result, int opcode)
{
	PoreInlineDecode *dis = &p->dis;
	pore_id_flags_reg *id_flags = &p->id_flags;

	/* Flags are updated only when target is scr1 or src2 */
	if (dis->tR != PORE_SCRATCH1_ENC && dis->tR != PORE_SCRATCH2_ENC) {
		return PORE_SUCCESS;
	}

	/* N: bit 0 of result goes to N; is result negative? */
	id_flags->n = PORE_GET_BIT(result, 0);

	/* Z: set zero bit */
	if (result == 0) {
		id_flags->z = 1;
	} else {
		id_flags->z = 0;
	}

	/* O: check for overflows */
	if ((~((op1 ^ op2) >> 63) & 0x1ull) &
	    (((result ^ op1) >> 63) & 0x1ull)) {
		id_flags->o = 1;
	} else {
		id_flags->o = 0;
	}

	/* C: set carry bit */
	if( op2 == 0) {
	  id_flags->c = 1;
	} else if (((op1 & 0x7fffffffffffffffull) +
	            (op2 & 0x7FFFFFFFFFFFFFFFull)) & (0x1ull << 63)) {

		if ((PORE_GET_BIT(op1,0) + PORE_GET_BIT(op2,0) + 1 ) >> 1) {
			id_flags->c = 1;
		} else {
			id_flags->c = 0;
		}
	} else {
		if ((PORE_GET_BIT(op1,0) + PORE_GET_BIT(op2,0) ) >> 1) {
			id_flags->c = 1;
		} else {
			id_flags->c = 0;
		}

	}

	/* UGT, ULT, SGT, SLT */
	if ((opcode == PORE_OPCODE_SUB) ||
	    (opcode == PORE_OPCODE_SUBI)) {
		/* UGT = C AND !Z */
		id_flags->ugt = ((id_flags->c  & ~id_flags->z) & 0x1);

		/* ULT = !C AND !Z */
		id_flags->ult = ((~id_flags->c & ~id_flags->z) & 0x1);

		/* SGT = (N AND V AND !Z) OR (!N AND !V AND !Z)
		 */
		id_flags->sgt = (((id_flags->n &
				   id_flags->o &
				   ~id_flags->z) & 0x1) |
				 ((~id_flags->n &
				   ~id_flags->o &
				   ~id_flags->z) & 0x1));

		/* SLT = (N AND !V) OR (!N AND V) */
		id_flags->slt = (((id_flags->n & ~id_flags->o) & 0x1 ) |
				 ((~id_flags->n & id_flags->o) & 0x1 ));
	}
	/* If the instruction is an ADD, then by specification the
	 * UGT/ULT/SGT/SLT bits are cleared.
	 */
	else if ((opcode == PORE_OPCODE_ADD) ||
		 (opcode == PORE_OPCODE_ADDI)) {
		id_flags->ugt = 0;
		id_flags->ult = 0;
		id_flags->sgt = 0;
		id_flags->slt = 0;
	}

	return 0;
}

/*** Address Decoding ********************************************************/

/// Relocate a memory address if it's in the relocation region
void pore_relocateAddress(pore_model_t p, uint64_t *addr)
{
	_PoreAddress address;

	address.val = *addr;

	if (p->memory_reloc.memory_reloc_region == (address.offset >> 30)) {
		address.offset += (p->memory_reloc.memory_reloc_base << 12);
	}
	*addr = address.val;
}

/// Compute OCI effective and real addresses and handle hooks. The
/// returned address is the real (physical) _PoreAddress.
static int computeOciDataAddress(pore_model_t p, uint32_t ima24,
				 _PoreAddress *address, int read_not_write)
{
	uint32_t base_select = ima24 & 0x400000;
	pore_oci_base_address_reg oci_base;

	// First compute an effective address.  The 'oci_mem_route' is
	// in the form of a memory space descriptor, minus the
	// high-order bit. This memory route is only used in the
	// PORE-SBE.

	if (base_select) {
		oci_base.val = p->oci_base[1].val;
	} else {
		oci_base.val = p->oci_base[0].val;
	}

	address->memorySpace = oci_base.oci_mem_route | 0x8000;
	address->offset = oci_base.oci_base_address + (ima24 & 0x003FFFFF);

	if (pore_get_enableAddressHooks(p)) {
		if (read_not_write) {
			pore_readHook(p, address->val);
		} else {
			pore_writeHook(p, address->val);
		}
	}

	// Return the relocated address
	pore_relocateAddress(p, &address->val);
	return 0;
}

/* Internal register encodings ***********************************************/

static uint64_t __readReg(pore_model_t p, pore_internal_reg_t reg)
{
	switch (reg) {
	case PORE_PRV_BASE_ADDR0_ENC:	return p->prv_base[0].val >> 32;
	case PORE_PRV_BASE_ADDR1_ENC:	return p->prv_base[1].val >> 32;
	case PORE_OCI_MEMORY_BASE_ADDR0_ENC: return p->oci_base[0].val;
	case PORE_OCI_MEMORY_BASE_ADDR1_ENC: return p->oci_base[1].val;
	case PORE_TABLE_BASE_ADDR_ENC:	return p->table_base_addr.val;
	case PORE_EXE_TRIGGER_ENC:	return p->exe_trigger.val;
	case PORE_SCRATCH0_ENC:		return p->scratch0.val >> 32;
	case PORE_SCRATCH1_ENC:		return p->scratch1;
	case PORE_SCRATCH2_ENC:		return p->scratch2;
	case PORE_DATA0_ENC:		return p->data0 >> 32;
	case PORE_ERROR_MASK_ENC:	return p->error_mask.val;
	case PORE_PC_ENC:		return p->status.pc;
	case PORE_ALU_IBUF_ID_ENC:	return p->id_flags.val;
	}

	eprintf(p, "%s: err: illegal reg %x\n", __func__, reg);
	return PORE_ERR_INVALID_PARAM;
}

static int __writeReg(pore_model_t p, pore_internal_reg_t reg, uint64_t val)
{
	switch (reg) {
	case PORE_PRV_BASE_ADDR0_ENC:
		p->prv_base[0].val =
			(val << 32) & PORE_PRV_BASE_ADDRESS_VALID_BITS;
		break;
	case PORE_PRV_BASE_ADDR1_ENC:
		p->prv_base[1].val =
			(val << 32) & PORE_PRV_BASE_ADDRESS_VALID_BITS;
		break;

	case PORE_OCI_MEMORY_BASE_ADDR0_ENC:
		p->oci_base[0].val =
			val & PORE_OCI_BASE_ADDRESS_VALID_BITS;
		break;
	case PORE_OCI_MEMORY_BASE_ADDR1_ENC:
		p->oci_base[1].val =
			val & PORE_OCI_BASE_ADDRESS_VALID_BITS;
		break;

	case PORE_TABLE_BASE_ADDR_ENC:
		p->table_base_addr.val = val;
		break;

	case PORE_EXE_TRIGGER_ENC:
		/**
		 * A COPY instruction to the EXE_Trigger register will
		 * only update the upper half of the register and not
		 * trigger a new start vector execution.
		 */
		p->exe_trigger.mc_chiplet_select_mask = val & 0xffffffff;
		break;

	case PORE_SCRATCH0_ENC:
		p->scratch0.val = (val << 32) & PORE_SCRATCH0_VALID_BITS;
		break;

	case PORE_SCRATCH1_ENC:
		p->scratch1 = val;
		break;
	case PORE_SCRATCH2_ENC:
		p->scratch2 = val;
		break;

	case PORE_DATA0_ENC:
		p->data0 = (val << 32) & PORE_DATA0_VALID_BITS;
		break;
	case PORE_ERROR_MASK_ENC:
		p->error_mask.val = val & PORE_ERROR_MASK_VALID_BITS;
		break;
	case PORE_PC_ENC:		p->status.pc = val; break;
	case PORE_ALU_IBUF_ID_ENC:
		p->id_flags.val = val & PORE_ID_FLAGS_VALID_BITS;
		break;
	default:
		eprintf(p, "%s: err: illegal reg %x\n", __func__, reg);
		return PORE_ERR_INVALID_PARAM;
	}
	return PORE_SUCCESS;
}

/*** Stack operations ********************************************************/

/**
 * Stack push. Only in the event of an error push do we allow the 3rd
 * level to be used.
 *
 * 0001: Stack empty
 * 0010: Stack filled up to level 1
 * 0100: Stack filled up to level 2
 * 1000: Stack filled up to level 3 (error handler only)
 */
static int push(pore_model_t p, uint64_t next_pc, int error)
{
	int rc = PORE_SUCCESS;
	pore_pc_stack0_reg pc_stack0;

	if (p->status.stack_pointer == 0x8) {
		BUG();
		p->dbg1.pc_stack_ovflw_undrn_err = 1;
		p->control.start_stop = 1; /* FIXME This is brute force */
		return PORE_ERR_STACK_OVERFLOW;
	}

	if ((p->status.stack_pointer == 0x4) && !error) {
		p->dbg1.pc_stack_ovflw_undrn_err = 1;
		rc = pore_handleErrEvent(p, 2, PORE_ERR_STACK_OVERFLOW);
		if (rc < 0) {
			return rc;
		}
	}

	pc_stack0.val = p->pc_stack[0].val;

	switch (p->status.stack_pointer) {
	case 0x1:
	case 0x2:
	case 0x4:
		p->pc_stack[2].pc_stack = p->pc_stack[1].pc_stack;
		p->pc_stack[1].pc_stack = p->pc_stack[0].pc_stack;
		pc_stack0.pc_stack	= next_pc;
		pore_stack0_reg_write(p, pc_stack0.val, PORE_BITS_0_63);

		p->status.stack_pointer = (p->status.stack_pointer << 1);
		break;
	default:
		return PORE_ERR_INVALID_STACK_POINTER;
	}

	return PORE_SUCCESS;
}

/**
 * Stack pop.
 *
 * 0001: Stack empty
 * 0010: Stack filled up to level 1
 * 0100: Stack filled up to level 2
 * 1000: Stack filled up to level 3 (error handler only)
 */
static int pop(pore_model_t p, _PoreAddress *next_pc)
{
	int rc;
	pore_pc_stack0_reg pc_stack0;

	pc_stack0.val = p->pc_stack[0].val;
	switch (p->status.stack_pointer) {
	case 0x1:
		p->dbg1.pc_stack_ovflw_undrn_err = 1;
		rc = pore_handleErrEvent(p, 2, PORE_ERR_STACK_UNDERFLOW);
		if (rc < 0)
			return rc;

	case 0x2:
	case 0x4:
	case 0x8:
		next_pc->val		= pc_stack0.pc_stack;
		pc_stack0.pc_stack	= p->pc_stack[1].pc_stack;
		pore_stack0_reg_write(p, pc_stack0.val, PORE_BITS_0_63);
		p->pc_stack[1].pc_stack = p->pc_stack[2].pc_stack;
		p->status.stack_pointer = p->status.stack_pointer >> 1;
		break;
	default:
		return PORE_ERR_INVALID_STACK_POINTER;
	}

	return PORE_SUCCESS;
}

/* Error handling ************************************************************/

/// Take Action Based on Error event and Error Mask setting.  If PORE
/// is programmed to stop on the error and d_errorBreak is true (the
/// default), we break simulation.

static void signalError(pore_model_t p)
{
	dprintf(p, "%s()\n", __func__);

	if (!p->errorCallback)
		return;
	p->errorCallback(p);
}

static void signalFatalError(pore_model_t p)
{
	dprintf(p, "%s()\n", __func__);

	if (!p->fatalErrorCallback)
		return;
	return p->fatalErrorCallback(p);
}

/**
 * Error event 0: During instruction execution phase, a non-zero
 * return code was received from PORE's pervasive PIB Master interface
 * or parity error detected in PIB read data.  The SBE will also use
 * this error event in case of a Fast I2C protocol hang (poll count
 * for SEEPROM/Flash/OTPROM access completion reached threshold as
 * defined in).  If error mask register bit20 is set, then the
 * return-code chiplet offline (0b010) does not trigger this error
 * event.
 *
 * Error event 1: During instruction execution phase, a non-zero
 * return code was received from PORE's OCI Master interface or parity
 * error detected in OCI read data (PORE_SBE has a pervasive PIB
 * Master IF only. So this error event is unused.)
 *
 * Error event 2: Error during instruction fetching or decode, which
 * is either:
 *  - bad instruction parity, if instruction parity checking is enabled
 *  - invalid instruction code
 *  - invalid start vector (SV) trigger
 *  - invalid instruction path (I2CM parameter miss)
 *  - invalid instruction operand (any other register selected than
 *    documented in (Table 4.8))
 *  - instruction counter (PC) overflow or underrun in relative addressing
 *  - PC stack overflow/underrun: BSR/BSRD to a full stack/RET on empty stack
 *  - instruction fetching error: a non-zero return-code was received
 *    during instruction fetching (single for short instruction or up to
 *    three times for long operations)
 *  - SBE only: Fast I2C protocol hang (poll count for SEEPROM/Flash/OTPROM
 *    access completion reached threshold during an instruction fetch).
 *
 * Each error cause is indicated by a corresponding bit in DBG
 * Register1.  Since errors on this event are fatal for PORE, it is
 * highly recommended to configure the actions âStop IBUF execution
 * and IBUF fatal error in the IBUF Error Mask register.  This is
 * the default setting.
 *
 * Error event 3: Internal data error during instruction execution:
 * Detected bad data consistency checking, e.g. bad scan-data CRC
 *
 * Error event 4: Error-on-Error. Any error occurred when DBG Regs0-1
 * are already locked: DBG_Reg1(63) = 1.
 *
 * Error events 2 and 3 will always freeze the DBG registers while
 * error event 0 and 1 will only freeze the DBG registers if the
 * corresponding Error Handler is enabled in the Error_Mask register.
 */
int
pore_handleErrEvent(pore_model_t p, int error_in, int model_err)
{
	int me, pc_offs;
	int error = error_in;

	// Check for error on error. If yes change to err event
	// 4. Else lock the debug registers for events 2, 3 and
	// 0, 1 only if err_handler is enabled.

	if (p->dbg1.debug_regs_locked == 1)
		error = 4;

	pc_offs = p->opcode_len;
	if ((pc_offs != 0) && (pc_offs != 4) && (pc_offs != 12))
		eprintf(p, "%s: err: illegal pc_offs=%d\n", __func__, pc_offs);

	switch (error) {
	case 0:
		if ((p->error_mask.enable_err_output0  == 0) &&
		    (p->error_mask.enable_fatal_error0 == 0) &&
		    (p->error_mask.stop_exe_on_error0  == 0) &&
		    (p->error_mask.enable_err_handler0 == 0))
			return PORE_ERROR_IGNORED;

		if (p->error_mask.enable_err_output0)
			signalError(p);
		if (p->error_mask.enable_fatal_error0)
			signalFatalError(p);
		if (p->error_mask.stop_exe_on_error0) {
			p->control.start_stop = 1;
		}
		else if (p->error_mask.enable_err_handler0) {
			p->dbg1.debug_regs_locked = 1;

			/* push pc onto stack, only if there is space */
			me = push(p, p->status.pc + pc_offs, 1);
			if (me < 0) {
				model_err = me;
				break;
			}
			pore_errorVector(p, 0);
			model_err = PORE_ERR_HANDLED_BY_CODE;
		}
		break;
	case 1:
		if ((p->error_mask.enable_err_output1  == 0) &&
		    (p->error_mask.enable_fatal_error1 == 0) &&
		    (p->error_mask.stop_exe_on_error1  == 0) &&
		    (p->error_mask.enable_err_handler1 == 0))
			return PORE_ERROR_IGNORED;

		if (p->error_mask.enable_err_output1)
			signalError(p);
		if (p->error_mask.enable_fatal_error1)
			signalFatalError(p);
		if (p->error_mask.stop_exe_on_error1) {
			p->control.start_stop = 1;
		}
		else if (p->error_mask.enable_err_handler1) {
			p->dbg1.debug_regs_locked = 1;

			/* push pc onto stack, only if there is space */
			me = push(p, p->status.pc + pc_offs, 1);
			if (me < 0) {
				model_err = me;
				break;
			}
			pore_errorVector(p, 1);
			model_err = PORE_ERR_HANDLED_BY_CODE;
		}
		break;
	case 2:
		p->dbg1.debug_regs_locked = 1;

		if ((p->error_mask.enable_err_output2  == 0) &&
		    (p->error_mask.enable_fatal_error2 == 0) &&
		    (p->error_mask.stop_exe_on_error2  == 0) &&
		    (p->error_mask.enable_err_handler2 == 0))
			return PORE_ERROR_IGNORED;

		if (p->error_mask.enable_err_output2)
			signalError(p);
		if (p->error_mask.enable_fatal_error2)
			signalFatalError(p);
		if (p->error_mask.stop_exe_on_error2) {
			p->control.start_stop = 1;
		}
		else if (p->error_mask.enable_err_handler2) {
			/* push pc onto stack, only if there is space */
			me = push(p, p->status.pc + pc_offs, 1);
			if (me < 0) {
				model_err = me;
				break;
			}
			pore_errorVector(p, 2);
			model_err = PORE_ERR_HANDLED_BY_CODE;
		}
		break;
	case 3:
		p->dbg1.debug_regs_locked = 1;

		if ((p->error_mask.enable_err_output3  == 0) &&
		    (p->error_mask.enable_fatal_error3 == 0) &&
		    (p->error_mask.stop_exe_on_error3  == 0) &&
		    (p->error_mask.enable_err_handler3 == 0))
			return PORE_ERROR_IGNORED;

		if (p->error_mask.enable_err_output3)
			signalError(p);
		if (p->error_mask.enable_fatal_error3)
			signalFatalError(p);
		if (p->error_mask.stop_exe_on_error3) {
			p->control.start_stop = 1;
		}
		else if (p->error_mask.enable_err_handler3) {
			/* push pc onto stack, only if there is space */
			me = push(p, p->status.pc + pc_offs, 1);
			if (me < 0) {
				model_err = me;
				break;
			}
			pore_errorVector(p, 3);
			model_err = PORE_ERR_HANDLED_BY_CODE;
		}
		break;
	case 4:
		if ((p->error_mask.enable_err_output4  == 0) &&
		    (p->error_mask.enable_fatal_error4 == 0) &&
		    (p->error_mask.stop_exe_on_error4  == 0) &&
		    (p->error_mask.enable_err_handler4 == 0))
			return PORE_ERROR_IGNORED;

		if (p->error_mask.enable_err_output4)
			signalError(p);
		if (p->error_mask.enable_fatal_error4)
			signalFatalError(p);
		if (p->error_mask.stop_exe_on_error4) {
			p->control.start_stop = 1;
		}
		else if (p->error_mask.enable_err_handler4) {
			me = push(p, p->status.pc + pc_offs, 1);
			if (me < 0) {
				model_err = me;
				break;
			}
			pore_errorVector(p, 4);
			model_err = PORE_ERR_HANDLED_BY_CODE;
		}
		break;
	default:
		return PORE_ERR_NOT_IMPLEMENTED;
	}
	return model_err;
}

/* External register encodings ***********************************************/

uint64_t pore_readReg(pore_model_t p, pore_reg_t reg, uint64_t msk)
{
	uint64_t val;

	switch (reg) {
	case PORE_R_STATUS:		val = p->status.val; break;
	case PORE_R_CONTROL:		val = p->control.val; break;
	case PORE_R_RESET:		val = p->reset.val; break;
	case PORE_R_ERROR_MASK:		val = p->error_mask.val; break;
	case PORE_R_PRV_BASE_ADDR0:	val = p->prv_base[0].val; break;
	case PORE_R_PRV_BASE_ADDR1:	val = p->prv_base[1].val; break;
	case PORE_R_OCI_MEMORY_BASE_ADDR0: val = p->oci_base[0].val; break;
	case PORE_R_OCI_MEMORY_BASE_ADDR1: val = p->oci_base[1].val; break;
	case PORE_R_TABLE_BASE_ADDR:	val = p->table_base_addr.val; break;
	case PORE_R_EXE_TRIGGER:	val = p->exe_trigger.val; break;
	case PORE_R_SCRATCH0:		val = p->scratch0.val; break;
	case PORE_R_SCRATCH1:		val = p->scratch1; break;
	case PORE_R_SCRATCH2:		val = p->scratch2; break;
	case PORE_R_IBUF_01:		val = p->ibuf_01.val; break;
	case PORE_R_IBUF_2:		val = p->ibuf_2.val; break;
	case PORE_R_DBG0:		val = p->dbg0.val; break;
	case PORE_R_DBG1:		val = p->dbg1.val; break;
	case PORE_R_PC_STACK0:		val = p->pc_stack[0].val; break;
	case PORE_R_PC_STACK1:		val = p->pc_stack[1].val; break;
	case PORE_R_PC_STACK2:		val = p->pc_stack[2].val; break;
	case PORE_R_ID_FLAGS:		val = p->id_flags.val; break;
	case PORE_R_DATA0:		val = p->data0; break;
	case PORE_R_MEM_RELOC:		val = p->memory_reloc.val; break;
	case PORE_R_I2C_E0_PARAM:	val = p->i2c_e_param[0].val; break;
	case PORE_R_I2C_E1_PARAM:	val = p->i2c_e_param[1].val; break;
	case PORE_R_I2C_E2_PARAM:	val = p->i2c_e_param[2].val; break;
	default:
		eprintf(p, "%s: err: illegal reg %x\n", __func__, reg);
		return PORE_ERR_INVALID_PARAM;
	}

	dprintf(p, "<== %s:  reg=%02x val=%016llx mask=%016llx\n",
		__func__, reg, (long long)val, (long long)msk);

	return val & msk;
}

uint64_t pore_readRegRaw(pore_model_t p, pore_reg_t reg,
			 uint64_t msk __attribute__((unused)))
{
	return pore_readReg(p, reg, msk);
}

static void write_under_mask(uint64_t *reg, uint64_t n_val, uint64_t mask)
{
	uint64_t o_val = *reg;
	*reg = ((n_val &  mask) | /* new val */
		(o_val & ~mask)); /* old val */
}

/**
 * Writing any data to either PIBMS_DBG Reg0 or Reg1 unsets their lock
 * and resets PIBMS_DBG1(63).
 */
int pore_writeReg(pore_model_t p, pore_reg_t reg, uint64_t val, uint64_t msk)
{
	dprintf(p, "==> %s: reg=%02x val=%016llx mask=%016llx\n",
		__func__, reg, (long long)val, (long long)msk);

	switch (reg) {
	case PORE_R_STATUS:
		write_under_mask(&p->status.val, val, msk);
		break;
	case PORE_R_CONTROL:				/* SIDE EFFECTS */
		return pore_control_reg_write(p, val, msk);

	case PORE_R_RESET:                              /* SIDE EFFECTS */
                return pore_reset_reg_write(p, val, msk);

	case PORE_R_ERROR_MASK:
		write_under_mask(&p->error_mask.val,
				 val & PORE_ERROR_MASK_VALID_BITS, msk);
		break;

	case PORE_R_PRV_BASE_ADDR0:
		write_under_mask(&p->prv_base[0].val,
				 val & PORE_PRV_BASE_ADDRESS_VALID_BITS, msk);
		break;
	case PORE_R_PRV_BASE_ADDR1:
		write_under_mask(&p->prv_base[1].val,
				 val & PORE_PRV_BASE_ADDRESS_VALID_BITS, msk);
		break;

	case PORE_R_OCI_MEMORY_BASE_ADDR0:
		write_under_mask(&p->oci_base[0].val,
				 val & PORE_OCI_BASE_ADDRESS_VALID_BITS, msk);
		break;
	case PORE_R_OCI_MEMORY_BASE_ADDR1:
		write_under_mask(&p->oci_base[1].val,
				 val & PORE_OCI_BASE_ADDRESS_VALID_BITS, msk);
		break;

	case PORE_R_TABLE_BASE_ADDR:
		write_under_mask(&p->table_base_addr.val, val, msk);
		break;
	case PORE_R_EXE_TRIGGER:		/* SIDE EFFECTS */
		return pore_exe_trigger_reg_write(p, val, msk);
	case PORE_R_SCRATCH0:
		write_under_mask(&p->scratch0.val,
				 val & PORE_SCRATCH0_VALID_BITS, msk);
		break;
	case PORE_R_SCRATCH1:
		write_under_mask(&p->scratch1, val, msk);
		break;
	case PORE_R_SCRATCH2:
		write_under_mask(&p->scratch2, val, msk);
		break;
	case PORE_R_IBUF_01:			/* SIDE EFFECTS */
		return pore_ibuf_01_reg_write(p, val, msk);
	case PORE_R_IBUF_2:
		write_under_mask(&p->ibuf_2.val, val, msk);
		break;
	case PORE_R_DBG0:
		p->dbg1.debug_regs_locked = 0; /* Writing will unlock */
		write_under_mask(&p->dbg0.val, val& PORE_DBG0_VALID_BITS, msk);
		break;
	case PORE_R_DBG1:
		p->dbg1.debug_regs_locked = 0; /* Writing will unlock */
		write_under_mask(&p->dbg1.val, val& PORE_DBG1_VALID_BITS, msk);
		break;
	case PORE_R_PC_STACK0:			/* SIDE EFFECTS */
		return pore_stack0_reg_write(p, val, msk);
	case PORE_R_PC_STACK1:
		write_under_mask(&p->pc_stack[1].val,
				 val & PORE_PC_STACK1_VALID_BITS, msk);
		break;
	case PORE_R_PC_STACK2:
		write_under_mask(&p->pc_stack[2].val,
				 val & PORE_PC_STACK2_VALID_BITS, msk);
		break;
	case PORE_R_ID_FLAGS:
		write_under_mask(&p->id_flags.val,
				 val & PORE_ID_FLAGS_VALID_BITS, msk);
		break;
	case PORE_R_DATA0:
		write_under_mask(&p->data0,
				 val & PORE_DATA0_VALID_BITS, msk);
		break;
	case PORE_R_MEM_RELOC:
		write_under_mask(&p->memory_reloc.val,
				 val & PORE_MEMORY_RELOC_VALID_BITS, msk);
		break;
	case PORE_R_I2C_E0_PARAM:
		write_under_mask(&p->i2c_e_param[0].val,
				 val & PORE_I2C_E0_PARAM_VALID_BITS, msk);
		break;
	case PORE_R_I2C_E1_PARAM:
		write_under_mask(&p->i2c_e_param[1].val,
				 val & PORE_I2C_E1_PARAM_VALID_BITS, msk);
		break;
	case PORE_R_I2C_E2_PARAM:
		write_under_mask(&p->i2c_e_param[2].val,
				 val & PORE_I2C_E2_PARAM_VALID_BITS, msk);
		break;
	default:
		eprintf(p, "%s: err: illegal reg %x\n", __func__, reg);
		return PORE_ERR_INVALID_PARAM;
	}
	return PORE_SUCCESS;
}

int pore_writeRegRaw(pore_model_t p, pore_reg_t reg, uint64_t val,
		     uint64_t msk)
{
	switch (reg) {
	case PORE_R_CONTROL:
		write_under_mask(&p->control.val, val, msk);
		break;
	case PORE_R_EXE_TRIGGER:
		write_under_mask(&p->exe_trigger.val,
				 val & PORE_EXE_TRIGGER_VALID_BITS, msk);
		break;
	case PORE_R_IBUF_01:
		write_under_mask(&p->ibuf_01.val, val, msk);
		break;
	case PORE_R_PC_STACK0:
		write_under_mask(&p->pc_stack[0].val, val, msk);
		break;
	case PORE_R_DBG0:
		write_under_mask(&p->dbg0.val, val& PORE_DBG0_VALID_BITS, msk);
		break;
	case PORE_R_DBG1:
		write_under_mask(&p->dbg1.val, val& PORE_DBG1_VALID_BITS, msk);
		break;

	default:
		return pore_writeReg(p, reg, val, msk);
	}
	return PORE_SUCCESS;
}

/*** PIB Bus with correct address translation using PORe offset regs *********/

/**
 * See Table 4.53: PRV address arithmetic.
 */
static int
computeDirectPibDataAddress(pore_model_t p, uint32_t ima24,
			    uint32_t *pibAddress, int read_not_write)
{
	int me = 0;
	_PoreAddress address;
	uint32_t baseSelect, localAddress, chipletId, mc, port;
	pore_prv_base_address_reg *prv_base;

	if (!p)
		return PORE_ERR_INVALID_PARAM;

	localAddress = ima24 & 0xffff;
	port = (ima24 >> 16) & 0xf;
	address.val = 0;
	address.offset = localAddress << 2;

	baseSelect = ima24 & 0x400000;
	if (baseSelect) {
		prv_base = &p->prv_base[1];
	} else {
		prv_base = &p->prv_base[0];
	}

	chipletId = prv_base->chiplet_id;
	mc = prv_base->mc;			/* multicast */
	address.memorySpace = (mc << 14) | (chipletId << 8) | port;

	if (pore_get_enableAddressHooks(p)) {
		if (read_not_write) {
			pore_readHook(p, address.val);
		} else {
			pore_writeHook(p, address.val);
		}
	}

	*pibAddress = (address.memorySpace << 16) | localAddress;
	return me;
}

int pore_pib_write(pore_model_t p, uint64_t addr, const uint8_t *buf,
		   unsigned int len, int *err_code)
{
	int rc;
	struct pore_bus *pib = p->pib;

	if (!pib)
		return PORE_ERR_UNCONNECTED_BUS;
	if (len != 8) {
		BUG();
		return PORE_ERR_INVALID_PARAM;
	}

	rc = poreb_write(pib, addr, buf, len, err_code);

	if (p->dbg1.debug_regs_locked == 0) {
		p->dbg0.last_completed_address = addr & 0xFFFFFFFF;
		p->dbg0.last_ret_code_prv = *err_code;
	}

	p->id_flags.pib_status = *err_code;
	p->id_flags.pib_parity_fail = 0;

	pib_printf(p, "  putScom: addr=%016llx data=%016llx err_code=%x\n",
		   (long long)addr, *(long long *)buf, *err_code);

	return rc;
}

int pore_pib_read(pore_model_t p, uint64_t addr, uint8_t *buf,
		  unsigned int len, int *err_code)
{
	int rc;
	struct pore_bus *pib = p->pib;

	if (!pib)
		return PORE_ERR_UNCONNECTED_BUS;
	if (len != 8) {
		BUG();
		return PORE_ERR_INVALID_PARAM;
	}

	rc = poreb_read(pib, addr, buf, len, err_code);
	if (p->dbg1.debug_regs_locked == 0) {
		p->dbg0.last_completed_address = addr & 0xFFFFFFFF;
		p->dbg0.last_ret_code_prv = *err_code;
	}

	p->id_flags.pib_status = *err_code;
	p->id_flags.pib_parity_fail = 0;

	pib_printf(p, "  getScom: addr=%016llx data=%016llx err_code=%x\n",
		   (long long)addr, *(long long *)buf, *err_code);

	return rc;
}

/// Fetch an instruction word using PIB direct addressing. This is
/// somewhat analogous to the OCI fetch buffer case in that we fetch 8
/// bytes but only return 4, however for some reason they don't buffer
/// PIB fetches.

static int
fetchInstructionWordPibDirect(pore_model_t p, _PoreAddress *pc,
			      uint32_t *word, int *err_code)
{
	int rc = 0;
	uint64_t data;
	uint32_t offset = pc->offset;
	uint32_t pibAddress = (pc->memorySpace << 16) | (offset >> 3);
	struct pore_bus *pib = p->pib;

	if (!pib)
		return PORE_ERR_UNCONNECTED_BUS;

	/* always fetch 8 byte on the PIB */
	rc = poreb_read(pib, pibAddress, (uint8_t *)&data, sizeof(data),
			err_code);

	if (p->dbg1.debug_regs_locked == 0) {
		p->dbg0.last_completed_address = pibAddress & 0xFFFFFFFF;
		p->dbg0.last_ret_code_prv = *err_code;
	}

	p->id_flags.pib_status = *err_code;
	p->id_flags.pib_parity_fail = 0;

	pib_printf(p, "  getScom: addr=%016llx data=%016llx err_code=%x\n",
		   (long long)pibAddress, (long long)data, *err_code);

	if (rc != sizeof(data)) {
		return PORE_ERR_FETCH;
	}

	*word = ((offset & 0x7) == 0) ?	data >> 32 : data & 0xffffffff;
	return rc;
}

static int pore_pib_fetch(pore_model_t p, uint64_t _pc,
			  uint64_t *ibuf_01,
			  uint64_t *ibuf_2,
			  unsigned int *size,
			  int *err_code)
{
	int me;
	uint32_t i_word;
	_PoreAddress pc;

	*ibuf_01 = *ibuf_2 = 0;
	pc.val = _pc;
	me = fetchInstructionWordPibDirect(p, &pc, &i_word, err_code);
	if (me < 0)
		return me;

	*ibuf_01 = (uint64_t)i_word << 32;
	*size = 4;
	if (i_word & 0x80000000) {
		pc.offset += 4;
		me = fetchInstructionWordPibDirect(p, &pc, &i_word, err_code);
		if (me < 0)
			return me;
		*size += 4;
		*ibuf_01 |= i_word;
		pc.offset += 4;
		me = fetchInstructionWordPibDirect(p, &pc, &i_word, err_code);
		if (me < 0)
			return me;
		*size += 4;
		*ibuf_2 = (uint64_t)i_word << 32;
	}
	return PORE_SUCCESS;
}

/*****************************************************************************/

/// The Instruction Fetch Routine
///
/// This function computes the effective address and fetches the
/// instructions from the fetch buffer to IBUF.

static int fetch(pore_model_t p)
{
	int rc;
	int recursion_stop = 0;

 redo_fetch:
	if (recursion_stop == 2)
		return PORE_ERR_INVALID_PARAM; /* STOP calling myself */

	if (!p)
		return PORE_ERR_INVALID_PARAM;

	if (p->enableAddressHooks && p->fetchHook) {
		int _rc;

		/* FIXME There is some more done in the existing model
		 * e.g. setting forchedBranchMode ...  Need to
		 * understand it before I will reintegrate.
		 */
		p->forcedBranch = 0;
		p->forcedBranchMode = FORCED_BRANCH_FETCH_HOOK;
		_rc = pore_fetchHook(p, p->status.pc);
		p->forcedBranchMode = FORCED_BRANCH_DISALLOWED;

		/* Model was modified from outside!!! */
		if (!_rc) {
			if (p->forcedBranch) {
				p->status.pc = p->forcedPc;
				recursion_stop++;
				goto redo_fetch;
			}
		}
	}

	/* zero the input buffers, such that we see 0s even on errors */
	p->ibuf_01.val = 0;
	p->ibuf_2.val = 0;

	if (p->status.pc & 0x0000800000000000ull) {
		rc = poreb_fetch(p->mem, p->status.pc,
				 &p->ibuf_01.val, &p->ibuf_2.val,
				 &p->opcode_len, &p->err_code);
	} else {
		rc = pore_pib_fetch(p, p->status.pc,
				    &p->ibuf_01.val, &p->ibuf_2.val,
				    &p->opcode_len, &p->err_code);

		/* Error occured during transaction try to handle it */
		if ((rc < 0) && (p->id_flags.pib_status != PORE_PCB_SUCCESS)) {
			dprintf(p, "PCB ERROR %x occured\n",
				(unsigned int)p->id_flags.pib_status);

			if ((p->id_flags.pib_status != PORE_PCB_CHIPLET_OFFLINE) ||
			    (p->error_mask.gate_chiplet_offline_err == 0)) {
				rc = pore_handleErrEvent(p, 2, rc);
			} else {
				/* HW190098: PORE Model not handling
				   mask of offline cores correctly */
				rc = PORE_SUCCESS;
			}
		}
	}

	if (p->dbg1.debug_regs_locked == 0)
		p->dbg1.pc_last_access = p->status.pc;
	if (rc < 0) {
		if (p->dbg1.debug_regs_locked == 0)
			p->dbg1.instruction_fetch_error = 1;
		rc = pore_handleErrEvent(p, 2, rc);
		return rc;
	}
	return rc;
}

static int decode(pore_model_t p)
{
	uint32_t *q;
	PoreInlineDecode *dis;

	if (!p)
		return PORE_ERR_INVALID_PARAM;

	dis = &p->dis;
	vpore_inline_decode_instruction(dis, p->ibuf_01.ibuf0);
	vpore_inline_decode_imd64(dis, ((uint64_t)p->ibuf_01.ibuf1 << 32 |
					(uint64_t)p->ibuf_2.ibuf2));

	p->opcode_len = 4;
	if (dis->long_instruction)
		p->opcode_len = 12;

	if (p->decodeHook) {
		unsigned int i;
		uint8_t instr[12];

		/* data was temporarily turned into the host format */
		q = (uint32_t *)instr;
		q[0] = htobe32(p->ibuf_01.ibuf0);
		q[1] = htobe32(p->ibuf_01.ibuf1);
		q[2] = htobe32(p->ibuf_2.ibuf2);

		dprintf(p, "  IBUF[0..2]=");
		for (i = 0; i < p->opcode_len; i++)
			dprintf(p, "%02x", instr[i]);

		dprintf(p, "\n"
			"  STAT %016llx CONT %016llx\n"
			"  D0   %016llx D1   %016llx CTR  %016llx\n"
			"  P0   %016llx P1   %016llx EMR  %016llx\n"
			"  A0   %016llx A1   %016llx ETR  %016llx\n"
			"  I2C0 %016llx I2C0 %016llx I2C0 %016llx\n"
			"  DBG0 %016llx DBG1 %016llx\n",
			(long long)p->status.val,
			(long long)p->control.val,

			(long long)p->scratch1,
			(long long)p->scratch2,
			(long long)p->scratch0.val,

			(long long)p->prv_base[0].val,
			(long long)p->prv_base[1].val,
			(long long)p->error_mask.val,

			(long long)p->oci_base[0].val,
			(long long)p->oci_base[1].val,
			(long long)p->exe_trigger.val,

			(long long)p->i2c_e_param[0].val,
			(long long)p->i2c_e_param[1].val,
			(long long)p->i2c_e_param[2].val,

			(long long)p->dbg0.val,
			(long long)p->dbg1.val);

		pore_decodeHook(p, instr, p->opcode_len);
	}
	return 0;
}

static int inExeInterfaceWrite(pore_model_t p, uint64_t write_data)
{
	int rc;
	PoreInlineDecode *dis = &p->dis;
	uint32_t addr_space, pib_addr;
	_PoreAddress address;
	uint64_t addr = dis->ima24;

	/* OCI/MEM Address Space *********************************************/
	addr_space = addr & 0x800000;
	if (addr_space) {
		address.val = 0;
		computeOciDataAddress(p, addr & 0xFFFFFF, &address, 0);
		rc = poreb_write(p->mem, address.val, (uint8_t *)&write_data,
				 sizeof(write_data), &p->err_code);

		if (p->dbg1.debug_regs_locked == 0) {
			p->dbg1.oci_master_rd_parity_err = 0;
			p->dbg1.last_ret_code_oci = p->err_code;
		}
		mem_printf(p, "  putMem:  addr=%016llx data=%016llx "
			   "err_code=%x\n", (long long)address.val,
			   (long long)write_data, p->err_code);

		if (rc == sizeof(write_data))
			return PORE_SUCCESS;

		rc = pore_handleErrEvent(p, 1, rc);
		return rc;
	}

	/* PIB Address Space *************************************************/
	address.val = 0;
	computeDirectPibDataAddress(p, addr, &pib_addr, 0);
	rc = pore_pib_write(p, pib_addr, (uint8_t *)&write_data,
			    sizeof(write_data), &p->err_code);
	if (rc == sizeof(write_data))
		rc = PORE_SUCCESS;

	/* Error occured during transaction try to handle it */
	if ((rc < 0) && (p->id_flags.pib_status != PORE_PCB_SUCCESS)) {
		if ((p->id_flags.pib_status != PORE_PCB_CHIPLET_OFFLINE) ||
		    (p->error_mask.gate_chiplet_offline_err == 0)) {

			rc = pore_handleErrEvent(p, 0, rc);
		} else {
			/* HW190098: PORE Model not handling mask of
			   offline cores correctly */
			rc = PORE_SUCCESS;
		}
	}
	return rc;
}

static int inExeInterfaceRead(pore_model_t p, uint64_t *read_data)
{
	int rc;
	PoreInlineDecode *dis = &p->dis;
	uint32_t addr_space, pib_addr;
	_PoreAddress address;
	uint64_t addr = dis->ima24;

	*read_data = 0;

	/* OCI/MEM Address Space *********************************************/
	addr_space = addr & 0x800000;
	if (addr_space) {
		address.val = 0;
		computeOciDataAddress(p, addr & 0xFFFFFF, &address, 1);
		rc = poreb_read(p->mem, address.val, (uint8_t *)read_data,
				sizeof(*read_data), &p->err_code);

		if (p->dbg1.debug_regs_locked == 0) {
			p->dbg1.oci_master_rd_parity_err = 0;
			p->dbg1.last_ret_code_oci = p->err_code;
		}

		mem_printf(p, "  getMem:  addr=%016llx data=%016llx "
			   "err_code=%x\n", (long long)address.val,
			   (long long)*read_data, p->err_code);

		if (rc == sizeof(*read_data))
			return PORE_SUCCESS;

		rc = pore_handleErrEvent(p, 1, rc);
		return rc;
	}

	/* PIB Address Space *************************************************/
	address.val = 0;
	computeDirectPibDataAddress(p, addr, &pib_addr, 1);
	rc = pore_pib_read(p, pib_addr, (uint8_t *)read_data,
			   sizeof(*read_data), &p->err_code);
	if (rc == sizeof(*read_data))
		rc = PORE_SUCCESS;

	/* Error occured during transaction try to handle it */
	if ((rc < 0) && (p->id_flags.pib_status != PORE_PCB_SUCCESS)) {
		if ((p->id_flags.pib_status != PORE_PCB_CHIPLET_OFFLINE) ||
		    (p->error_mask.gate_chiplet_offline_err == 0)) {
			rc = pore_handleErrEvent(p, 0, rc);
		} else {
			/* HW190098: PORE Model not handling mask of
			   offline cores correctly */
			rc = PORE_SUCCESS;
		}
	}
	return rc;
}


/**
 * Example how to do scan a chain via the FSI Scan engine.
 *
 * General approach:
 *
 * 1. Write the length of the scan chain to be shifted (in this case
 * '1') and the control bits into the Front End Length Register
 * putcfam pu 0c02 00000001
 *
 * you might also want to set bit2 (setpulse) or bit4 (header check)
 * ... but that depends.
 *
 * 2. Setup the Command Register with the address of the scan chain
 * and the desired operation
 *
 * Now it gets a little more tricky: Bit 0 of the Command Register is
 * a write/not_read bit. We will take the write case. The following
 * 31 bits is the address of the scan chain.
 *
 * There are three easy ways to find that:
 *   - CRONUS: do a getringdump to that ring, and see what is in
 *     0c01 => getcfam pu 0c01
 *   - CRONUS: do a getringdump pu RINGNAME -debug5.15.f and check
 *     for the mentioned address take a look at the scandef
 *
 * 3. Write to the FIFO with the scan info.
 *   putcfam pu 0c00 00000000
 *
 * Repeat step 3. for the length of the scan chain ( => length/32 times ).
 *
 * 4. Check the status (not mandatory :-) )
 * getcfam pu 0c07
 *
 * Example: Scan of ex_dpll_gptr chain on P7
 *
 * # scan in
 * putcfam pu 0c02 00000147
 * putcfam pu 0c01 88030402
 *
 * putcfam pu 0c00 DEADBEEF
 * putcfam pu 0c00 00000000
 * putcfam pu 0c00 00000000
 * putcfam pu 0c00 00000000
 * putcfam pu 0c00 00000000
 * putcfam pu 0c00 00000000
 * putcfam pu 0c00 00000000
 * putcfam pu 0c00 00000000
 * putcfam pu 0c00 00000000
 * putcfam pu 0c00 00000000
 * putcfam pu 0c00 00000000
 *
 * # scan out
 * putcfam pu 0c02 00000147
 * putcfam pu 0c01 08030402
 *
 * getcfam pu 0c00
 * getcfam pu 0c00
 * getcfam pu 0c00
 * getcfam pu 0c00
 * getcfam pu 0c00
 * getcfam pu 0c00
 * getcfam pu 0c00
 * getcfam pu 0c00
 * getcfam pu 0c00
 * getcfam pu 0c00
 * getcfam pu 0c00
 *
 * Here and example doing the scan via SCOM commands:
 * 1.) Configure Clock Control of respective chiplet
 * 2.) Write data packets
 *
 * Example of EN_LBST Chain with manual 'Headercheck' (DEADBEEF as Header)
 *
 * en_lbst_scan
 * putscom pu 03030007 5E000800
 *
 * putscom pu 03038020 deadbeef
 * putscom pu 03038020 11112222
 * putscom pu 03038020 33334444
 * putscom pu 03038020 55556666
 * putscom pu 03038020 77778888
 * putscom pu 03038020 9999aaaa
 * putscom pu 03038020 bbbbcccc
 * putscom pu 03038020 ddddeeee
 * putscom pu 03038020 ffff1212
 * putscom pu 03038020 23233434
 * putscom pu 03038020 FE000000
 * putscom pu 03038020 23233434
 * putscom pu 03038020 23233434
 * putscom pu 03038020 23233434
 * putscom pu 03038020 23233434
 * putscom pu 0303801C 23233434
 *
 * echo "HEADERCHECK:"
 * getscom pu 03038000
 *
 * putscom pu 03038020 deadbeef
 * getscom pu 03038000
 * getscom pu 03038020
 * getscom pu 03038020
 * getscom pu 03038020
 * getscom pu 03038020
 * getscom pu 03038020
 * getscom pu 03038020
 * getscom pu 03038020
 * getscom pu 03038020
 * getscom pu 03038020
 * getscom pu 03038020
 * getscom pu 03038020
 * getscom pu 03038020
 * getscom pu 03038020
 * getscom pu 03038020
 * getscom pu 0303801C
 *
 * Binary to Unary Translation of scan_sel:
 *  WHEN 11 scantype_allf => result := "1101.1100.1110.0000";
 *  WHEN 12 scantype_ccvf => result := "0010.1000.0000.0000";
 *  WHEN 13 scantype_lbcm => result := "0000.1000.0010.0000";
 *  WHEN 14 scantype_abfa => result := "0000.0100.0100.0000";
 *  WHEN 15 scantype_fure => result := "1001.0000.0000.0000";
 */
static const uint16_t bin_2_unary[] = {
	/* unary                binary */
	0x8000,			/* 0x0  0 */
	0x4000,			/* 0x1  1 */
	0x2000,			/* 0x2  2 */
	0x1000,			/* 0x3  3 */
	0x0800,			/* 0x4  4 */
	0x0400,			/* 0x5  5 */
	0x0200,			/* 0x6  6 */
	0x0100,			/* 0x7  7 */
	0x0080,			/* 0x8  8 */
	0x0040,			/* 0x9  9 */
	0x0020,			/* 0xa 10 */
	0xdce0,			/* 0xb 11 */
	0x2800,			/* 0xc 12 */
	0x0820,			/* 0xd 13 */
	0x0440,			/* 0xe 14 */
	0x9000,			/* 0xf 15 */
};

static int pore_scand_read(pore_model_t p, _PoreAddress *addr, uint32_t *word)
{
	int rc;
	uint64_t data;

	if (addr->val & 0x0000800000000000ull) {
		uint64_t memAddress;

		memAddress = addr->val;
		pore_relocateAddress(p, &memAddress);
		rc = poreb_read(p->mem, memAddress, (uint8_t *)word,
				sizeof(*word), &p->err_code);

		if (p->dbg1.debug_regs_locked == 0) {
			p->dbg1.oci_master_rd_parity_err = 0;
			p->dbg1.last_ret_code_oci = p->err_code;
		}

		mem_printf(p, "  getMem:  addr=%016llx data=%016llx "
			   "err_code=%x\n", (long long)memAddress,
			   (long long)*word, p->err_code);

		if (rc != sizeof(*word))
			return rc;

	} else {
		uint32_t pibAddress = ((addr->memorySpace << 16) |
				       (addr->offset >> 3));

		/* always read 8 byte on the PIB */
		rc = poreb_read(p->pib, pibAddress, (uint8_t *)&data,
				sizeof(data), &p->err_code);

		if (p->dbg1.debug_regs_locked == 0) {
			p->dbg0.last_completed_address = pibAddress;
			p->dbg0.last_ret_code_prv = p->err_code;
		}

		mem_printf(p, "  getScom: addr=%016llx data=%016llx "
			   "err_code=%x\n", (long long)pibAddress,
			   (long long)data, p->err_code);

		if (rc != sizeof(data)) {
			return rc;
		}
		*word = ((addr->offset & 0x7) == 0) ?
			data >> 32 : data & 0xffffffff;
	}
	return PORE_SUCCESS;
}

static int pore_scand(pore_model_t p)
{
	int bits, rem, rc;
	PoreInlineDecode *dis = &p->dis;
	pore_scratch0_reg *scr0 = &p->scratch0;
	uint32_t scan_data = 0, crc32_data = 0;
	uint64_t write_data;
	_PoreAddress addr;
	PibAddress pib_addr;
	shift_eng_cmd_reg scan_sel;
	scan_type_select_reg scan_type_sel;

	pib_addr.val = 0;
	scan_sel.val = dis->scan_select;

	addr.val = p->status.pc;
	addr.offset += dis->scan_offset * 4;

	/**
	 * Setup scan region register to select correct chains,
	 * e.g. write scom 0x02030007 0x0100000800000000 (chiplet 2,
	 * non-vital region 3, type bndy)
	 */
	pib_addr.mc	    = scan_sel.bc;
	pib_addr.chiplet_id = scan_sel.chiplet_select;
	pib_addr.prv_port   = scan_sel.port;
	pib_addr.local_addr = SCAN_REGION_OFFSET;

	scan_type_sel.val = 0;
	scan_type_sel.region_select = scan_sel.region_select;
	scan_type_sel.type_sel_unary = bin_2_unary[scan_sel.type_sel_bin];
	write_data = scan_type_sel.val;

	rc = pore_pib_write(p, pib_addr.val, (uint8_t *)&write_data,
			    sizeof(write_data), &p->err_code);
	if (rc < 0)
		return rc;

	for (bits = 0, rem = dis->scan_length,
		     scr0->scratch0 = dis->scan_length / 32;
	     bits < dis->scan_length;
	     bits += 32, rem -= 32,
	     scr0->scratch0--) {

		int num_bits = rem > 32 ? 32 : rem;

		rc = pore_scand_read(p, &addr, &scan_data);
		if (rc < 0)
			return rc;

		/**
		 * Send PCB scan packets which have the following
		 * layout:
		 *   Address is 0x0<chiplet_id>0x0380<num_bits>
		 *   Data is the left aligned values to get scanned in
		 *   (up to 32 bit).
		 *
		 * The last scan packet should have the updateDR bit
		 * set in the address, i.e. 0x03A0 instead of 0x380.
		 *
		 * This will clear the scan region register if scan
		 * protection is active (default).  E.g. write scom
		 * 0x02038020 0xFFFFFFFF00000000 (scan 32 bit in
		 * chiplet 2, all '1')).
		 *
		 * cDR: See JTAG standard, capture data from scanlatch
		 * uDR:          "         update data to scanlatch
		 * Is used for PLL settings.
		 */

		if (scr0->scratch0 == dis->scan_length / 32) {
			/* Need to set cDR on 1st data access */
			pib_addr.local_addr =
				SCAN_DATA_OFFSET(dis->capture, 0, num_bits);

		} else if (scr0->scratch0 == 0) {
			/* Need to set uDR on last data access */
			pib_addr.local_addr =
				SCAN_DATA_OFFSET(0, dis->update, num_bits);
		} else {
			pib_addr.local_addr =
				SCAN_DATA_OFFSET(0, 0, num_bits);
		}

		write_data = (uint64_t)scan_data << 32;
		rc = pore_pib_write(p, pib_addr.val, (uint8_t *)&write_data,
				    sizeof(write_data), &p->err_code);
		if (rc < 0)
			return rc;

		addr.offset += 4;
	}
	rc = pore_scand_read(p, &addr, &crc32_data);
	if (rc < 0)
		return rc;
	p->scratch1 = crc32_data;

	return PORE_SUCCESS;
}

static int execute(pore_model_t p)
{
	int me = PORE_SUCCESS;
	int next_state = PORE_STATE_EXEC;
	int trapBreakpoint = 0;
	int addressBreakpoint = 0;
	PoreInlineDecode *dis;
	uint64_t write_data, read_data = 0;
	int64_t op1, op2;
	_PoreAddress next_pc;

	if (!p)
		return PORE_ERR_INVALID_PARAM;

	dis = &p->dis;
	next_pc.val = p->status.pc;
	p->branchTaken = 0;

	switch (dis->opcode) {

	case PORE_OPCODE_NOP:
		break;

	case PORE_OPCODE_TRAP:
		// PORE hardware trap leaves the machine in the
		// address breakpoint state.
		if (p->control.trap_enable) {
			trapBreakpoint = 1;
			next_state = PORE_STATE_ABR;
		}
		break;

	case PORE_OPCODE_HOOK:
		if (p->enableHookInstruction) {
			int _rc;

			p->forcedBranch = 0;
			p->forcedBranchMode = FORCED_BRANCH_HOOK_INSTRUCTION;
			_rc = pore_instrHook(p, p->status.pc, dis->imd24,
					     dis->imd64);
			p->forcedBranchMode = FORCED_BRANCH_DISALLOWED;
			if (_rc) {
				me = PORE_ERR_HOOK_FAILED;
				break;
			}
			if (p->forcedBranch) {
				next_pc.val = p->forcedPc;
				p->branchTaken = 1;
			}
		}
		break;

	case PORE_OPCODE_WAIT:
		if (p->dis.imd24 == 0) {
			// Invalidate fetch buffer for OCI access
			/// \bug Buffer is invalidated on reset
			p->oci_fetchBufferValid = 0;
			p->oci_fetchBufferCursor = 0;
			p->oci_fetchBuffer = 0;

			// This is the stop command. Raise the
			// pore_stopped signal
			p->control.start_stop = 1;

			//unlockRegisters();
			p->control.lock_exe_trig = 0;
		}
		pore_waitCallback(p, dis->imd24);
		break;

		/* Branch Instructions */
	case PORE_OPCODE_BRA:
		p->branchTaken = 1;
		setJmpTarget(dis->impco24, &next_pc);
		break;

	case PORE_OPCODE_BRAD:
		if (dis->tR != PORE_SCRATCH1_ENC &&
		    dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		p->branchTaken = 1;
		next_pc.val = __readReg(p, (pore_internal_reg_t)dis->tR);
		break;

	case PORE_OPCODE_BRAZ:
		if (dis->tR != PORE_SCRATCH0_ENC &&
		    dis->tR != PORE_SCRATCH1_ENC &&
		    dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		if (__readReg(p, (pore_internal_reg_t)dis->tR) == 0) {
			p->branchTaken = 1;
			setJmpTarget(dis->impco20, &next_pc);
		}
		break;

	case PORE_OPCODE_BRANZ:
		if (dis->tR != PORE_SCRATCH0_ENC &&
		    dis->tR != PORE_SCRATCH1_ENC &&
		    dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		if (__readReg(p, (pore_internal_reg_t)dis->tR) != 0) {
			p->branchTaken = 1;
			setJmpTarget(dis->impco20, &next_pc);
		}
		break;

	case PORE_OPCODE_BRAI:
		p->branchTaken = 1;
		next_pc.val = dis->impc48;
		break;

	case PORE_OPCODE_BSR:
		me = push(p, p->status.pc + 4, 0);
		if (me < 0) {
			break;
		}

		p->branchTaken = 1;
		setJmpTarget(dis->impco24 , &next_pc);
		break;

	case PORE_OPCODE_BSRD:
		if (dis->tR != PORE_SCRATCH1_ENC &&
		    dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		me = push(p, p->status.pc + 4, 0);
		if (me < 0)
			break;

		p->branchTaken = 1;
		next_pc.val = __readReg(p, (pore_internal_reg_t)dis->tR);
		break;

	case PORE_OPCODE_RET:
		me = pop(p, &next_pc);
		if (me < 0)
			break;

		p->branchTaken = 1;
		break;

	case PORE_OPCODE_CMPBRA:
		if (p->scratch1 == dis->imd64) {
			p->branchTaken = 1;
			setJmpTarget(dis->impco24, &next_pc);
		}
		break;

	case PORE_OPCODE_CMPNBRA:
		if (p->scratch1 != dis->imd64) {
			p->branchTaken = 1;
			setJmpTarget(dis->impco24, &next_pc);
		}
		break;

	case PORE_OPCODE_CMPBSR:
		if (p->scratch1 == dis->imd64) {
			me = push(p, p->status.pc + 12, 0);
			if (me < 0)
				break;

			p->branchTaken = 1;
			setJmpTarget(dis->impco24, &next_pc);
		}
		break;

	case PORE_OPCODE_LOOP:
		if (p->scratch0.scratch0 > 0) {
			p->scratch0.scratch0 = p->scratch0.scratch0 - 1;
			p->branchTaken = 1;
			setJmpTarget(dis->impco24, &next_pc);
		}
		break;

		/* ALU Instructions */
	case PORE_OPCODE_ANDI:
		if (dis->sR != PORE_SCRATCH1_ENC &&
		    dis->sR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		if (dis->tR != PORE_SCRATCH1_ENC &&
		    dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		__writeReg(p, (pore_internal_reg_t)dis->tR,
			   __readReg(p, (pore_internal_reg_t)
				     dis->sR) & dis->imd64);
	    break;

	case PORE_OPCODE_ORI:
		if (dis->sR != PORE_SCRATCH1_ENC &&
		    dis->sR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		if (dis->tR != PORE_SCRATCH1_ENC &&
		    dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		__writeReg(p, (pore_internal_reg_t)dis->tR,
			   __readReg(p, (pore_internal_reg_t)dis->sR) |
			   dis->imd64);
		break;

	case PORE_OPCODE_XORI:
		if (dis->sR != PORE_SCRATCH1_ENC &&
		    dis->sR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		if (dis->tR != PORE_SCRATCH1_ENC &&
		    dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		__writeReg(p, (pore_internal_reg_t)dis->tR,
			   __readReg(p, (pore_internal_reg_t)dis->sR) ^
			   dis->imd64);
		break;

	case PORE_OPCODE_AND:
		if (dis->tR != PORE_SCRATCH1_ENC &&
		    dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		__writeReg(p, (pore_internal_reg_t)dis->tR,
			   p->scratch1 & p->scratch2);
		break;

	case PORE_OPCODE_OR:
		if (dis->tR != PORE_SCRATCH1_ENC &&
		    dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		__writeReg(p, (pore_internal_reg_t)dis->tR,
			   p->scratch1 | p->scratch2);
		break;

	case PORE_OPCODE_XOR:
		if (dis->tR != PORE_SCRATCH1_ENC &&
		    dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		__writeReg(p, (pore_internal_reg_t)dis->tR,
			   p->scratch1 ^ p->scratch2);
		break;

	case PORE_OPCODE_ADDI: {
		int64_t add_result;

		if (dis->tR == PORE_EXE_TRIGGER_ENC ||
		    dis->tR == PORE_ALU_IBUF_ID_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}

		op1 = __readReg(p, (pore_internal_reg_t)dis->tR);
		op2 = dis->imd16;
		add_result = op1 + op2;

		setAluFlags(p, op1, op2, add_result, dis->opcode);
		__writeReg(p, (pore_internal_reg_t)dis->tR, add_result);
		break;
	}
	case PORE_OPCODE_SUBI: {
		int64_t sub_result;

		if (dis->tR == PORE_EXE_TRIGGER_ENC ||
		    dis->tR == PORE_ALU_IBUF_ID_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}

		op1 = __readReg(p, (pore_internal_reg_t)dis->tR);
		op2 = dis->imd16;
		op2 = ~op2 + 1;
		sub_result = op1 + op2;

		setAluFlags(p, op1, op2, sub_result, dis->opcode);
		__writeReg(p, (pore_internal_reg_t)dis->tR, sub_result);
		break;
	}
	case PORE_OPCODE_ADD: {
		int64_t add_result;

		if (dis->tR != PORE_SCRATCH1_ENC &&
		    dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}

		op1 = p->scratch1;
		op2 = p->scratch2;
		add_result = op1 + op2;

		setAluFlags(p, op1, op2, add_result, dis->opcode);
		__writeReg(p, (pore_internal_reg_t)dis->tR, add_result);
		break;
	}
	case PORE_OPCODE_SUB: {
		int64_t sub_result;

		if (dis->tR != PORE_SCRATCH1_ENC &&
		    dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}

		op1 = p->scratch1;
		op2 = p->scratch2;
		op2 = ~op2 + 1;
		sub_result = op1 + op2;
		setAluFlags(p, op1, op2, sub_result, dis->opcode);
		__writeReg(p, (pore_internal_reg_t)dis->tR, sub_result);
		break;
	}
	case PORE_OPCODE_NEG: {
		int64_t neg_result;

		if (dis->sR != PORE_SCRATCH1_ENC &&
		    dis->sR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		if (dis->tR != PORE_SCRATCH1_ENC &&
		    dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}

		op1 = __readReg(p, (pore_internal_reg_t)dis->sR);
		neg_result = ~op1 + 1;
		__writeReg(p, (pore_internal_reg_t)dis->tR, neg_result);
		break;
	}
	case PORE_OPCODE_COPY:
		__writeReg(p, (pore_internal_reg_t)dis->tR,
			   __readReg(p, (pore_internal_reg_t)dis->sR));
		break;

	case PORE_OPCODE_ROL: {
		uint16_t rmask = 0;
		uint64_t result = 0;
		uint64_t src = 0;

		// Operand Check
		rmask = dis->imd16;
		if (rmask != 0x1 && rmask != 0x4 && rmask != 0x8
		    && rmask != 0x10 && rmask != 0x20 ) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;

		} else if (
			   dis->sR != PORE_SCRATCH1_ENC &&
			   dis->sR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;

		} else if (dis->tR != PORE_SCRATCH1_ENC &&
			   dis->tR != PORE_SCRATCH2_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;

		} else {
			src = __readReg(p, (pore_internal_reg_t)dis->sR);
			result = ( src << rmask ) |
				(((~(0xFFFFFFFFFFFFFFFFull >> rmask)) & src )
				 >> (64 - rmask));
		}

		__writeReg(p, (pore_internal_reg_t)dis->tR, result);
		break;
	}
		/* Load/Store Instructions */
	case PORE_OPCODE_LOAD20: {
		if (dis->tR == PORE_EXE_TRIGGER_ENC ||
		    dis->tR == PORE_ALU_IBUF_ID_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}
		/**
		 * ImD20 will be signed extended to 64b (on MSB side)
		 * and then truncated to the width of <tR>.
		 */
		__writeReg(p, (pore_internal_reg_t)dis->tR, dis->imd20);
		break;
	}
	case PORE_OPCODE_LOAD64:
		if (dis->tR == PORE_PRV_BASE_ADDR0_ENC ||
		    dis->tR == PORE_PRV_BASE_ADDR1_ENC ||
		    dis->tR == PORE_EXE_TRIGGER_ENC ||
		    dis->tR == PORE_ALU_IBUF_ID_ENC) {
			BUG();
			if (p->dbg1.debug_regs_locked == 0)
				p->dbg1.invalid_instruction_operand = 1;
			me = pore_handleErrEvent(p, 2,
						 PORE_ERR_INVALID_OPERAND);
			break;
		}

		__writeReg(p, (pore_internal_reg_t)dis->tR, dis->imd64);
		break;

	case PORE_OPCODE_SCR1RD: {
		me = inExeInterfaceRead(p, &read_data);
		if (me < 0)
			break;
		p->scratch1 = read_data;
		break;
	}
	case PORE_OPCODE_SCR2RD: {
		me  = inExeInterfaceRead(p, &read_data);
		if (me < 0)
			break;
		p->scratch2 = read_data;
		break;
	}
	case PORE_OPCODE_SCR1RDA: {
		// Read DATA from OCI/PRV Address Space using effective addr
		me = inExeInterfaceRead(p, &read_data);
		if (me < 0)
			break;
		// Apply imd64 AND mask
		read_data &= dis->imd64;
		p->scratch1 = read_data;
		break;
	}
	case PORE_OPCODE_SCR2RDA: {
		// Read DATA from OCI/PRV Address Space using effective addr
		me = inExeInterfaceRead(p, &read_data);
		if (me < 0)
			break;
		// Apply imd64 AND mask
		read_data &= dis->imd64;
		p->scratch2 = read_data;
		break;
	}
	case PORE_OPCODE_WRI: {
		write_data = dis->imd64;
		// Write DATA to OCI/PRV Address Space using effective addr
		me = inExeInterfaceWrite(p, write_data);
		if (me < 0)
			break;
		break;
	}
	case PORE_OPCODE_BS: {
		// Read OCI/PRV space from effective address to read_data
		me = inExeInterfaceRead(p, &read_data);
		if (me < 0)
			break;
		// Apply OR mask to read_data
		read_data |= dis->imd64;
		// update scratch register
		p->scratch1 = read_data;
		// Write the data back to effective address in OCI/PRV
		me = inExeInterfaceWrite(p, read_data);
		if (me < 0)
			break;
		break;
	}
	case PORE_OPCODE_BC: {
		// Read OCI/PRV space from effective address to read_data
		me = inExeInterfaceRead(p, &read_data);
		if (me < 0)
			break;
		// Apply Inverted AND mask to read_data
		read_data &= ~(dis->imd64);
		// Update scratch register
		p->scratch1 = read_data;
		// Write the data back to effective address in OCI/PRV
		me = inExeInterfaceWrite(p, read_data);
		if (me < 0)
			break;
		break;
	}
	case PORE_OPCODE_SCR1WR: {
		write_data = p->scratch1;
		// Write DATA to OCI/PRV Address Space using effective addr
		me = inExeInterfaceWrite(p, write_data);
		if (me < 0)
			break;
		break;
	}
	case PORE_OPCODE_SCR2WR: {
		write_data = p->scratch2;
		// Write DATA to OCI/PRV Address Space using effective addr
		me = inExeInterfaceWrite(p, write_data);
		if (me < 0)
			break;
		break;
	}
	case PORE_OPCODE_SCAND:
		me = pore_scand(p);
		if (me < 0) {
			me = pore_handleErrEvent(p, 2, me);
			break;
		}
		break;

	default:
		/* Disabled printout as requested in HW218122 */
		/* eprintf(p, "err: Invalid Instruction Encoding: "
			"     %08x %08x %08x\n",
			(unsigned int)p->ibuf_01.ibuf0,
			(unsigned int)p->ibuf_01.ibuf0,
			(unsigned int)p->ibuf_2.ibuf2); */

		if (p->dbg1.debug_regs_locked == 0)
			p->dbg1.invalid_instr_code = 1;
		me = pore_handleErrEvent(p, 2, PORE_ERR_INVALID_OPCODE);
		break;
	}

	// Check for address, trap, magic breakpoints and single-step.
	// Note that we do the dumps

	addressBreakpoint = (p->status.pc == p->control.pc_brk_pt);

	if (addressBreakpoint) {
		pore_dump(p);
		next_state = PORE_STATE_ABR;
		p->broken = 1;
		dprintf(p, "PORE Address Breakpoint @ 0x%012llx\n",
			(long long)p->status.pc);
	} else if (trapBreakpoint) {
		pore_dump(p);
		next_state = PORE_STATE_ABR;
		p->broken = 1;
		dprintf(p, "PORE Trap Breakpoint @ 0x%012llx\n",
			(long long)p->status.pc);
	}

	// Branches have already updated the PC so the assignment to
	// the PC is done here. Otherwise we increment the PC for the
	// next step unless we're broken.
	//
	// In case of me < 0 the pore_handeErrEvent() function has set
	// a new PC by jumping into the appropriate Error Vector. In
	// this case we do not update the current PC instead it was
	// pushed onto the stack. After the error handler has tidied
	// up, it can jump back to the failing instruction.

	if (me >= 0) {
		if (p->branchTaken) {
			p->status.pc = next_pc.val;
		} else if (!p->broken) {
			incrPc(p);
		}
	}

	/**
	 * The vPORE has been setup to handle this error via the
	 * exception handler code. Therefore ignore this failure and
	 * continue.
	 */
	if (me == PORE_ERR_HANDLED_BY_CODE) {
		me = PORE_ERROR_IGNORED;
	}

	// Step mode. don't update state and don't schedule next step
	// Condition can occur when we are hitting an error during
	// instruction execution, e.g. a pib_read failed.

	if (p->control.start_stop == 1) {
		dprintf(p, "PORE was stopped during execution!\n");
		p->status.cur_state = PORE_STATE_WAIT;

	} else if (p->reset.fn_reset) {
		dprintf(p, "PORE is being held in reset!\n");
		me = PORE_ERR_IN_RESET;

	} else{
		p->status.cur_state = next_state;
	}

	return me;
}

/// If the instruction was broken, finish it.
///
/// If the PORE is an a 'broken' state, then we first need to update
/// the PC by re-decoding the current instruction.  Taken branches
/// have already updated the PC.
///
/// The situation occurs if we have hit a breakpoint. In this case the
/// PC has still the value of the breakpoint instruction, but needs to
/// be updated when the user writes 1 to control.continue_step.

static int finishBrokenInstruction(pore_model_t p)
{
	if (!p)
		return PORE_ERR_INVALID_PARAM;

	if (p->broken && !p->branchTaken) {
		decode(p);
		incrPc(p);
	}

	p->branchTaken = 0;
	p->broken = 0;
	return PORE_SUCCESS;
}

int pore_step(pore_model_t p)
{
	int rc = PORE_SUCCESS;

	if (!p)
		return PORE_ERR_INVALID_PARAM;

	dprintf(p, "(0) ensure we are in correct state to do a step ...\n");
	if (p->reset.fn_reset == 1) {
		dprintf(p, "  no: engine is in reset\n");
		return PORE_IN_RESET;
	}
	if (p->control.start_stop == 1) {
		dprintf(p, "  no: engine is stopped\n");
		return PORE_STOPPED;
	}
	if (p->status.cur_state == PORE_STATE_ABR) {
		dprintf(p, "  no: engine has hit a breakpoint\n");
		return PORE_BREAKPOINT_HIT;
	}

	p->branchTaken = 0;	/* reset internal state */
	p->broken = 0;

	dprintf(p, "(1) fetch @ %016llx ...\n", (long long)p->status.pc);
	rc = fetch(p);
	if (rc < 0) {
		eprintf(p, "%s: err: fetch rc=%d\n", __func__, rc);
		return rc;
	}

	dprintf(p, "(2) decode ...\n");
	rc = decode(p);
	if (rc < 0) {
		eprintf(p, "%s: err: decode rc=%d\n", __func__, rc);
		return rc;
	}

	dprintf(p, "(3) execute ...\n");
	rc = execute(p);
	if (rc < 0) {
		eprintf(p, "%s: err: execute rc=%d\n", __func__, rc);
		return rc;
	}

	/* Extension for interruptible operation of SLW instance */
	if (p->control.interrupt_sequencer_enabled &&
	    p->pore_interrupt_request) {

		if (p->control.pore_interruptible) {
			__pore_flush_reset(p);
			pore_exe_trigger_reg_write(p, p->exe_trigger.val,
						   PORE_BITS_0_63);
		} else {
			if (p->dbg0.interrupt_counter != 0xff)
				p->dbg0.interrupt_counter++;
		}
	}

	return rc;
}

void pore_dump(pore_model_t p)
{
	_PoreAddress a0, a1;
	_PoreAddress sp0, sp1, sp2;
	int sp;
	uint32_t p0, p1;

	sp0.val = p->pc_stack[0].pc_stack;
	sp1.val = p->pc_stack[1].pc_stack;
	sp2.val = p->pc_stack[2].pc_stack;

	a0.val = __readReg(p, PORE_OCI_MEMORY_BASE_ADDR0_ENC);
	a1.val = __readReg(p, PORE_OCI_MEMORY_BASE_ADDR1_ENC);
	p0 = __readReg(p, PORE_PRV_BASE_ADDR0_ENC);
	p1 = __readReg(p, PORE_PRV_BASE_ADDR1_ENC);

	aprintf("-------------------------------------"
		"-------------------------------------\n"
		"%s : PORE Id %d.\n"
		"-------------------------------------"
		"-------------------------------------\n"
		"  IBUF: %08llx %08x.%08x.%08x\n"
		"-------------------------------------"
		"-------------------------------------\n"
		"  D0: %016llx   D1: %016llx\n"
		"  A0: %04x:%08x      A1: %04x:%08x\n"
		"  P0: %02x P1: %02x          CTR: %06x\n"
		"-------------------------------------"
		"-------------------------------------\n",
		__func__,
		(uint32_t)(p->id_flags.ibuf_id),
		(long long)p->status.pc,
		p->ibuf_01.ibuf0,
		p->ibuf_01.ibuf1,
		p->ibuf_2.ibuf2,
		(long long)p->scratch1,
		(long long)p->scratch2,
		(unsigned int)a0.memorySpace, (unsigned int)a0.offset,
		(unsigned int)a1.memorySpace, (unsigned int)a1.offset,
		p0, p1,	(uint32_t)p->scratch0.scratch0);

	sp = p->status.stack_pointer;
	/* pore_status_reg_print(&p->status); */
	/* pore_control_reg_print(&p->control); */

	if (sp == 0) {
		aprintf("The stack is empty\n");
	} else {
		aprintf("Stack Trace\n"
			"  Level 0 : %04x:%08x\n",
			(unsigned int)sp0.memorySpace,
			(unsigned int)sp0.offset);
		if (sp > 0x1) {
			aprintf("  Level 1 : %04x:%08x\n",
				(unsigned int)sp1.memorySpace,
				(unsigned int)sp1.offset);
		}
		if (sp > 0x2) {
			aprintf("  Level 2 : %04x:%08x\n",
				(unsigned int)sp2.memorySpace,
				(unsigned int)sp2.offset);
		}
	}

	aprintf("-------------------------------------"
		"-------------------------------------\n"
		"Last pervasive access @ %08x returned error code %d\n"
		"-------------------------------------"
		"-------------------------------------\n",
		(uint32_t)p->dbg0.last_completed_address,
		(int)p->dbg0.last_ret_code_prv);

	aprintf("  PORe Registers\n"
		"    STATUS:       %016llx CONTROL:	 %016llx\n"
		"    RESET:        %016llx ERR_MASK:	 %016llx\n"
		"    PVR_BASE0/P0: %016llx PVR_BASE1/P1: %016llx\n"
		"    OCI_BASE0/A0: %016llx OCI_BASE1/A1: %016llx\n"
		"    TBL_BASE:     %016llx EXE_TRIGGER:	 %016llx\n"
		"    SCR0/CTR:     %016llx SCR1/D0:	 %016llx\n"
		"    SCR2/D1:      %016llx IBUF012: %08llx.%08llx.%08llx\n"
		"    DBG0:         %016llx DBG1:         %016llx\n"
		"    PC_STACK0:    %016llx PC_STACK1:    %016llx\n"
		"    PC_STACK2:    %016llx ID_FLAGS:     %016llx\n"
		"    DATA0:        %016llx MEM_RELOC:	 %016llx\n"
		"    I2C_E0:       %016llx I2C_E1:	 %016llx\n"
		"    I2C_E2:       %016llx PC:		 %016llx\n"
		"  Internal State\n"
		"    branchTaken/broken:       %lld/%lld\n"
		"    pore_interrupt_request:   %lld\n"
		"    oci_fetchBufValid/Cursor: %lld/%lld\n"
		"    oci_fetchBuf:             %016llx\n"
		"-------------------------------------"
		"-------------------------------------\n",
		(long long)p->status.val,	(long long)p->control.val,
		(long long)p->reset.val,	(long long)p->error_mask.val,
		(long long)p->prv_base[0].val,	(long long)p->prv_base[1].val,
		(long long)p->oci_base[0].val,	(long long)p->oci_base[1].val,
		(long long)p->table_base_addr.val,
		(long long)p->exe_trigger.val,
		(long long)p->scratch0.val,	(long long)p->scratch1,
		(long long)p->scratch2,		(long long)p->ibuf_01.ibuf0,
		(long long)p->ibuf_01.ibuf1,	(long long)p->ibuf_2.ibuf2,
		(long long)p->dbg0.val,		(long long)p->dbg1.val,
		(long long)p->pc_stack[0].val,	(long long)p->pc_stack[1].val,
		(long long)p->pc_stack[2].val,	(long long)p->id_flags.val,
		(long long)p->data0,		(long long)p->memory_reloc.val,
		(long long)p->i2c_e_param[0].val,
		(long long)p->i2c_e_param[1].val,
		(long long)p->i2c_e_param[2].val,
		(long long)p->status.pc,
		(long long)p->branchTaken, (long long)p->broken,
		(long long)p->pore_interrupt_request,
		(long long)p->oci_fetchBufferValid,
		(long long)p->oci_fetchBufferCursor,
		(long long)p->oci_fetchBuffer);
}

/* Model Creation and Reset **************************************************/

void pore_model_destroy(pore_model_t p)
{
	poreb_destroy(p->pib);
	poreb_destroy(p->mem);
	free(p);
}

int pore_registerHooks (pore_model_t p,
			instrHook_f  instrHook,
			readHook_f   readHook,
			writeHook_f  writeHook,
			fetchHook_f  fetchHook,
			decodeHook_f decodeHook)
{
	p->instrHook = instrHook;
	p->readHook = readHook;
	p->writeHook = writeHook;
	p->fetchHook = fetchHook;
	p->decodeHook = decodeHook;
	return 0;
}

int pore_registerCallbacks(pore_model_t p,
			   waitCallback_f waitCallback,
			   errorCallback_f errorCallback,
			   errorCallback_f fatalErrorCallback)
{
	p->waitCallback = waitCallback;
	p->errorCallback = errorCallback;
	p->fatalErrorCallback = fatalErrorCallback;
	return 0;
}

void pore_setpriv(pore_model_t p, void *priv)
{
	p->priv = priv;
}

void *pore_getpriv(pore_model_t p)
{
	return p->priv;
}

/* recursive function to setup the pore pointer in all bus attachements */
static void poreb_set_pore(struct pore_bus *b, pore_model_t p)
{
	unsigned int i;

	if (b == NULL)
		return;
	b->pore = p;
	for (i = 0; i < PORE_MAX_BUS; i++)
		poreb_set_pore(b->slaves[i], p);
}

int pore_attach_mem(pore_model_t p, struct pore_bus *b)
{
	p->mem = b;
	poreb_set_pore(b, p);
	return 0;
}

int pore_attach_pib(pore_model_t p, struct pore_bus *b)
{
	p->pib = b;
	poreb_set_pore(b, p);
	return 0;
}

int pore_extractState(pore_model_t p, struct pore_state *s)
{
	memcpy(s, p, sizeof(*s));
	return 0;
}

int pore_installState(pore_model_t p, const struct pore_state *s)
{
	memcpy(p, s, sizeof(*s));
	return 0;
}

void pore_set_enableHookInstr(pore_model_t p, int enabled)
{
	p->enableHookInstruction = enabled;
}

int pore_get_enableHookInstr(pore_model_t p)
{
	return p->enableHookInstruction;
}

void pore_set_enableAddressHooks(pore_model_t p, int enabled)
{
	p->enableAddressHooks = enabled;
}

int pore_get_enableAddressHooks(pore_model_t p)
{
	return p->enableAddressHooks;
}

int
pore_forceBranch(pore_model_t p, uint64_t addr)
{
	switch (p->forcedBranchMode) {

	case FORCED_BRANCH_DISALLOWED:
		return PORE_ERR_ILLEGAL_FORCED_BRANCH;

	case FORCED_BRANCH_FETCH_HOOK:
	case FORCED_BRANCH_HOOK_INSTRUCTION:
		p->forcedPc = addr;
		p->forcedBranch = 1;
		break;
	}
	return PORE_SUCCESS;
}

int pore_stop(pore_model_t p)
{
	uint64_t control;
	control = pore_readReg(p, PORE_R_CONTROL, PORE_BITS_0_63);
	/* set start_stop bit[0] */
	pore_writeReg(p, PORE_R_CONTROL, control | BE64_BIT(0),
		      PORE_BITS_0_63);
	return PORE_SUCCESS;
}

int pore_start(pore_model_t p)
{
	uint64_t control;
	control = pore_readReg(p, PORE_R_CONTROL, PORE_BITS_0_63);
	/* clear start_stop bit[0] */
	pore_writeReg(p, PORE_R_CONTROL, control & ~BE64_BIT(0),
		      PORE_BITS_0_63);
	return PORE_SUCCESS;
}

int pore_setPc(pore_model_t p, uint64_t pc)
{
	pore_stop(p);
	pore_writeReg(p, PORE_R_CONTROL, BE64_BIT(3) |
		      (pc & BE64_MASK(16, 63)), PORE_BITS_0_63);
	return PORE_SUCCESS;
}

int pore_setBreakpoint(pore_model_t p, uint64_t bp)
{
	uint64_t control;

	control = pore_readReg(p, PORE_R_CONTROL, PORE_BITS_0_63);
	control &= ~BE64_MASK(16, 63);
	control |= (bp & BE64_MASK(16, 63));
	pore_writeReg(p, PORE_R_CONTROL, control, PORE_BITS_0_63);
	return PORE_SUCCESS;
}

int pore_enableTrap(pore_model_t p, int enable)
{
    uint64_t control;

    control = pore_readReg(p, PORE_R_CONTROL, PORE_BITS_0_63);
    if (enable) {
	    control |= BE64_BIT(11);
    } else {
	    control &= ~BE64_BIT(11);
    }
    pore_writeReg(p, PORE_R_CONTROL, control, PORE_BITS_0_63);
    return PORE_SUCCESS;
}

static int __fake_read(struct pore_bus *b, uint64_t addr,
		       uint8_t *buf, unsigned int len,
		       int *err_code __attribute__((unused)))
{
	unsigned int i;

	memset(buf, 0xff, len);
	dprintf(b->pore, "  %-12s: %s(%p, 0x%08llx, ...)\n	 read:	",
		b->name, __func__, b, (long long)addr);

	for (i = 0; i < len; i++)
		dprintf(b->pore, "%02x ", buf[i]);
	dprintf(b->pore, "\n");

	return len;
}

static int __fake_write(struct pore_bus *b, uint64_t addr,
			const uint8_t *buf, unsigned int len,
			int *err_code __attribute__((unused)))
{
	unsigned int i;

	dprintf(b->pore, "  %-12s: %s(%p, 0x%08llx, ...)\n	 write:	 ",
		b->name, __func__, b, (long long)addr);
	for (i = 0; i < len; i++)
		dprintf(b->pore, "%02x ", buf[i]);
	dprintf(b->pore, "\n");

	return len;
}

static int __fake_fetch(struct pore_bus *b, uint64_t pc,
			uint64_t *ibuf_01 __attribute__((unused)),
			uint64_t *ibuf_2  __attribute__((unused)),
			unsigned int *size,
			int *err_code __attribute__((unused)))
{
	dprintf(b->pore, "  %-12s: %s(%p, 0x%08llx, ...)\n	 fetch: ",
		b->name, __func__, b, (long long)pc);

	*size = 4;
	return *size;
}

static int __fake_reset(struct pore_bus *b)
{
	dprintf(b->pore, "  %s: %s(%p)\n", b->name, __func__, b);
	return 0;
}

/**
 * @brief Here we are setting the engine type and those registers
 * which are only setup on engine creation. Also model internal states
 * are reset to their start values.
 */
pore_model_t pore_model_create(const char *name)
{
	pore_model_t p;

	p = (pore_model_t)malloc(sizeof(*p));
	if (!p)
		return NULL;
	memset(p, 0, sizeof(*p));


	/* Some registers must only be set at model creation time */
	/* Setup depends on model type ------------------------------------- */
	p->name = name;

	/* ERROR_MASK ... only at 1st init */
	/* page 74: IBUF Err Mask, not altered during PORE reset. */
	p->error_mask.val = 0x00BFF00000000000ull;

	/* I2C_PARAM0/1/2 ... only at 1st init */
	p->i2c_e_param[0].val = 0x0;
	p->i2c_e_param[1].val = 0x0;
	p->i2c_e_param[2].val = 0x0;

	if	  (strcmp(p->name, "SBE")  == 0) {
		p->id_flags.ibuf_id = PORE_IBUF_ID_SBE;

		/* I2C_PARAM0/1/2 only at 1st init */
		p->i2c_e_param[0].i2c_engine_speed = 0x0f;

	} else if (strcmp(p->name, "SLW")  == 0) {
		p->id_flags.ibuf_id = PORE_IBUF_ID_SLW;
	} else if (strcmp(p->name, "GPE0") == 0) {
		p->id_flags.ibuf_id = PORE_IBUF_ID_GPE0;
	} else if (strcmp(p->name, "GPE1") == 0) {
		p->id_flags.ibuf_id = PORE_IBUF_ID_GPE1;
	}

	/* Setup register default values */
	pore_flush_reset(p);

	p->enableHookInstruction = 0;
	p->enableAddressHooks = 0;
	p->trace_flags = PORE_TRACE_ERR;
	return p;
}

/**
 * @brief The interruptible PORe implementation does not reset all
 * registers to their default state. Therefore this function only
 * resets those, which must be reset in that specific case.
 */
static int __pore_flush_reset(pore_model_t p)
{
	/* --------------- always ------------------------------------------ */

	/* STATUS */
	p->status.val = 0;
	p->status.cur_state = PORE_STATE_WAIT;
	p->status.stack_pointer = 0x1; /* HW218121 */
	p->status.pc = 0;

	/* OCI/MEM Route */
	p->prv_base[0].val = 0;
	p->prv_base[1].val = 0;
	p->oci_base[0].val = 0;
	p->oci_base[1].val = 0;

	/* SCR0, SRC1, IBUF01, IBUF2 */
	p->scratch0.val = 0;
	p->scratch1 = 0;
	p->scratch2 = 0;
	p->ibuf_01.val = 0;
	p->ibuf_2.val = 0;

	/* ID_FLAGS, DBG0, DBG1, PC_STACK0/1/2, DATA0 */
	p->id_flags.val = p->id_flags.ibuf_id; /* keep ibuf_id */
	p->dbg0.val = 0;
	p->dbg1.val = 0;
	p->pc_stack[0].val = 0; /* clears one bits on a write */
	p->pc_stack[1].val = 0;
	p->pc_stack[2].val = 0;
	p->data0 = 0;

	/* Internal model state */
	p->err_code = 0;
	p->branchTaken = 0;
	p->broken = 0;
	p->singleStep = 0;
	p->forcedBranchMode = FORCED_BRANCH_DISALLOWED;
	p->forcedPc = 0;

	/* Externally attached busses */
	poreb_reset(p->pib);	/* reset bus models, e.g. clear buffers */
	poreb_reset(p->mem);

	return 0;
}

/**
 * @brief This is the regular PORe reset case. Some registers depend
 * on the type of the engine, other don't. Note that there are even
 * registers which are not reset here, but only in case of engine
 * instance creation. See specifiction.
 */
int pore_flush_reset(pore_model_t p)
{
	dprintf(p, "%s: %s\n", __func__, p->name);

	/* --------------- exclude those regs for interruptible PORE case -- */
	/* EXE_TRIGGER */
	p->exe_trigger.val = 0;

	/* CONTROL */
	p->control.val = 0;
	p->control.start_stop = 1;
	p->control.lock_exe_trig = 0;
	p->control.check_parity = 0;
	p->control.prv_parity = 0;
	p->control.pc_brk_pt = 0xffffffffffffull;

	/* TABLE_BASE */
	p->table_base_addr.val = 0;

	/* ERROR_MASK ... only at 1st init */
	/* page 74: IBUF Err Mask, not altered during functional PORE reset. */

	/* MEMORY_RELOC */
	p->memory_reloc.val = 0;

	/* I2C_PARAM0/1/2 ... only at 1st init, see pore_model_create() */

	/* Setup depends on model type ------------------------------------- */
	if	  (strcmp(p->name, "SBE")  == 0) {
		p->id_flags.ibuf_id = PORE_IBUF_ID_SBE;

		/* CONTROL */
		p->control.interrupt_sequencer_enabled = 0;
		p->control.pore_interruptible = 0;

		/* The 0x00040000 in the SBE address space translates
		 * to the 0x00008000 (>>3) on the PIB bus.
		 */
		/* TABLE_BASE */
		p->table_base_addr.memory_space = 0x00001; /* SBE */
		p->table_base_addr.table_base_address = 0x00040028; /* SBE */

	} else {
		/* TABLE_BASE */
		p->table_base_addr.memory_space = 0x0; /* SLW/GPE */
		p->table_base_addr.table_base_address = 0x0; /* SLW/GPE */
	}

	if (strcmp(p->name, "SLW")  == 0) {
		p->id_flags.ibuf_id = PORE_IBUF_ID_SLW;

		/* CONTROL */
		p->control.interrupt_sequencer_enabled = 1; /* SLW */
		p->control.pore_interruptible = 1; /* SLW */

	} else if (strcmp(p->name, "GPE0") == 0) {
		p->id_flags.ibuf_id = PORE_IBUF_ID_GPE0;

	} else if (strcmp(p->name, "GPE1") == 0) {
		p->id_flags.ibuf_id = PORE_IBUF_ID_GPE1;
	}

	__pore_flush_reset(p);
	return 0;
}

pore_model_t
pore_sbe_create(pore_bus_t pib)
{
	pore_model_t p;
	pore_bus_t fi2cm[3], pore2fi2cm;

	p = pore_model_create("SBE");
	if (!p)
		return NULL;

	/* setup reference for the case it is not yet done. */
	if (pib)
		pib->pore = p;

	/* underlying busses which finally implement functionality */
	if (!pib)
		pib = poreb_create("PIB", 0, __fake_read, __fake_write,
				   __fake_fetch, __fake_reset);

	fi2cm[0] = poreb_create_fi2cm(p, "FI2CM0", pib, &p->i2c_e_param[0]);
	fi2cm[1] = poreb_create_fi2cm(p, "FI2CM1", pib, &p->i2c_e_param[1]);
	fi2cm[2] = poreb_create_fi2cm(p, "FI2CM2", pib, &p->i2c_e_param[2]);

	/* view of PORe engine which goes through remappings
	   before the underlying busses are accessed */

	/* SBE: 3 FI2C masters are servicing this memory range */
	pore2fi2cm = poreb_create_pore2fi2c(p, "PORE2FI2C",
					    fi2cm[0], fi2cm[1], fi2cm[2]);
	if (!pore2fi2cm)
		return NULL;

	pore_attach_mem(p, pore2fi2cm);
	pore_attach_pib(p, pib);

	return p;
}

static pore_model_t
__slw_gpe_create(const char *name, pore_bus_t pib, pore_bus_t oci)
{
	pore_model_t p;
	struct pore_bus *pore2oci_b;

	p = pore_model_create(name);
	if (!p)
		return NULL;

	/* setup reference for the case it is not yet done. */
	if (pib)
		pib->pore = p;
	if (oci)
		oci->pore = p;

	/* underlying busses which finally implement functionality */
	if (!pib)
		pib = poreb_create("PIB", 0, __fake_read, __fake_write,
				   __fake_fetch, __fake_reset);

	if (!oci)
		oci = poreb_create("OCI", 0, __fake_read, __fake_write,
				   __fake_fetch, __fake_reset);

	/* view of PORe engine which goes through remappings
	   before the underlying busses are accessed */

	/* SLW/GPE0/GPE1: OCI is servicing this memory range */
	pore2oci_b = poreb_create_pore2oci(p, "PORE2OCI", oci);
	if (!pore2oci_b)
		return NULL;

	pore_attach_mem(p, pore2oci_b);
	pore_attach_pib(p, pib);
	return p;
}

pore_model_t
pore_slw_create(pore_bus_t pib, pore_bus_t oci)
{
	return __slw_gpe_create("SLW", pib, oci);
}

pore_model_t
pore_gpe0_create(pore_bus_t pib, pore_bus_t oci)
{
	return __slw_gpe_create("GPE0", pib, oci);
}

pore_model_t
pore_gpe1_create(pore_bus_t pib, pore_bus_t oci)
{
	return __slw_gpe_create("GPE1", pib, oci);
}
OpenPOWER on IntegriCloud