Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
;
; Copyright (c) 2018 Yosuke Sugahara. All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions
; are met:
; 1. Redistributions of source code must retain the above copyright
;    notice, this list of conditions and the following disclaimer.
; 2. Redistributions in binary form must reproduce the above copyright
;    notice, this list of conditions and the following disclaimer in the
;    documentation and/or other materials provided with the distribution.
;
; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
; BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
; AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
; SUCH DAMAGE.
;
;
; LUNA XP multiplexed device firmware
;
; used language:
;  zasm 4.1
;  http://k1.spdns.de/Develop/Projects/zasm
;
; XP memory map
;
; type : SH, PR, IN, NC
;     SH: host shared memory, 64kB, PA 00000 - 0FFFF
;     PR: private memory, 32kB, PA 28000-2FFFF
;     IN: HD647180 internal 512 bytes memory
;     NC: not connected (00 or FF or image readable, maybe)
;
; start end type desc
;  0000 00FF SH RESET/RST etc.
;  0100 01FF SH shared variables
;  0200 0FFF SH resident program
;  1000 7FFF SH PAM/PCM buffer 28K
;  8000 8FFF SH PSG buffer 4K
;  9000 9FFF SH LPR buffer 4K
;  A000 DFFF SH FDC buffer 16K
;  E000 EFFF PR program/stack
;  F000 FDFF NC bus error (00 or FF)
;  FE00 FFDF IN PAM player
;  FFE0 FFFF IN interrupt vector
;
; shared variable area
;  0100    XPBUS
;  0110    TIME
;  0120    PAM
;  0130    PCM
;  0140    PSG
;  0150    SPK
;  0160    LPR
;  0170    FDC
;  0180    SIO0
;  0190    SIO1
; device ID = bit 7-4
;
; XP internal device usage
;  PRT0  device dispatcher/TIME
;  PRT1  PCM
;  PT2   unused
;  ASCI0 SIO0
;  ASCI1 SIO1	本体表記の関係で、入れ替える?
;
; READY-CMD-RESULT-RUN プロトコル
; XP 視点
; READY
;   コマンドを受け付けできるとき != 0
;   受付できないとき 0
; CMD
;   ホスト側が送信してくるコマンド
;   コマンドなしは 0
;   XP がコマンドを受け付けると、READY=0 CMD=0 の順で、XP が 0 にする
; RESULT
;   コマンド実行結果。
;   RESULT=x READY=1 の順で書き込む。
;   ハードリセット、またはホストが 0 クリアする。
; RUN
;   コマンド実行中は != 0
;   実行していないときは 0
;   実行完了時、RESULT=x RUN=0 READY=1 の順で書き込む。
;
; 通常シーケンス
;  READY を上げる
;  CMD の立ち上がりを待つ
;  READY を下げる
;  RUN を上げる
;  CMD を下げる
;  実行
;  RESULT を書く
;  RUN を下げる
;  READY を上げる
;
; ホスト視点
; 実行完了を待つとき
;  while (READY == 0);	// 受付可能待ち
;  RESULT=0;		// 結果クリア
;  CMD=x;		// コマンド送信
;  while (RESULT == 0);	// 実行完了待ち
;  if (RESULT==ERROR) error();	// エラー確認
;

;
; XPBUS
;  +0.b READY
;  +1.b CMD
;  +2.b RESULT
;  +3.b RUN
;
;  +4.b STAT_RESET
;        ファームウェア転送で 0 にされる。
;        リセット位置の実行のたびに +1 
;        すなわち何もなければ 1 になっている。
;  +5.3 align
;  +8.w PRT0_TIMER
;        ==256(1200Hz)
;  +A.w INTR1_DEV
;        bitmap of INTR1 device ID
;  +C.w INTR5_DEV
;        bitmap of INTR5 device ID
;
; TIME
;  +0.b READY
;  +1.b CMD
;  +2.b RESULT
;  +3.b RUN
;
;  +4.w TIMECOUNTER
;
; PAM
;  +0.b READY
;  +1.b CMD
;  +2.b RESULT
;  +3.b RUN
;
;  +4.b ENC
;        エンコードフォーマット識別子。
;  +5.b REPT
;        REPT 回数。
;  +6.w CYCLE_CLK
;        基準クロック数
;        クエリで返される。
;  +8.b REPT_CLK
;        1 REPT あたりのクロック数。
;        クエリで返される。
;  +9.b REPT_MAX
;        REPT に設定できる最大値。
;        クエリで返される。
;
;  +E.w STAT_PTR
;
; PCM
;  +0.b READY
;  +1.b CMD
;  +2.b RESULT
;  +3.b RUN
;  +4.b ENC
;  +6.w PRT1_TIMER
;        PCM >=10(30.72kHz,200clk)
;
;  +E.w STAT_PTR
;
; PSG
;  +0.b READY
;  +1.b CMD
;  +2.b RESULT
;  +3.b RUN
;
; SPK
;  +0.b READY
;  +1.b CMD
;  +2.b RESULT
;  +3.b RUN
;
;  +4.b VOL
;        PSG ボリュームレジスタ値。
;  +6.w FREQ
;        PSG FREQ レジスタ値。
;  +8.w TIME
;        1200Hz 単位の持続時間。
;  +A.w REMAIN
;        内部変数:残り時間。
;
; LPR
;  TBD.
; FDC
;  TBD.
;
; SIO0
;  +0.b READY
;  +1.b CMD
;  +2.b RESULT
;  +3.b RUN
;				; 送受信のバッファリングも検討したが
;				; わりに合わない
;  +4.b TXCMD
;  +5.b TXSTAT
;  +6.b TX
;  +A.b RXCMD
;  +B.b RXSTAT
;  +C.b RX
;
; SIO1
;  +0.b READY
;  +1.b CMD
;  +2.b RESULT
;  +3.b RUN
;				; 送受信のバッファリングも検討したが
;				; わりに合わない
;  +4.b TXCMD
;  +5.b TXSTAT
;  +6.b TX
;  +A.b RXCMD
;  +B.b RXSTAT
;  +C.b RX

	.Z180

; ######## device ID

#define DEVID_XPBUS	0
#define DEVID_TIME	1
#define DEVID_PAM	2
#define DEVID_PCM	3
#define DEVID_PSG	4
#define DEVID_SPK	5
#define DEVID_LPR	6
#define DEVID_FDC	7
#define DEVID_SIO0	8
#define DEVID_SIO1	9
; ######## define

#define PAM_CMD_START	1
#define PAM_CMD_QUERY	2

#define PAM_ENC_PAM2A	1
#define PAM_ENC_PAM2B	2
#define PAM_ENC_PAM3A	3
#define PAM_ENC_PAM3B	4
#define PAM_ENC_PAM1P	5

#define PCM_CMD_START	1

#define PCM_ENC_PCM1	1
#define PCM_ENC_PCM2	2
#define PCM_ENC_PCM3	3

#define SPK_CMD_START	1
#define SPK_CMD_STOP	2
#define SPK_CMD_KEEP	3


; #### RESULT
#define XPLX_R_OK		1
#define XPLX_R_ERROR_PARAM	254
#define XPLX_R_UNKNOWN_CMD	255


; ######## switch
; 0 = USE STAT_PTR for userland test mode
; 1 = USE HOSTINTR for kernel (normal)
#define USE_INTR	1

; ######## constants
; xp to host level 1 interrupt port
HOSTINTR1	.EQU	0B0H
; xp to host level 5 interrupt port
HOSTINTR5	.EQU	0A0H

; PAM use HOSTINTR5
PAM_HOSTINTR	.EQU	HOSTINTR5
; PCM use HOSTINTR5
PCM_HOSTINTR	.EQU	HOSTINTR5

; I/O PORT
TMDR0L	.EQU	0CH
TMDR0H	.EQU	0DH
RLDR0L	.EQU	0EH
RLDR0H	.EQU	0FH
TCR	.EQU	10H
TMDR1L	.EQU	14H
TMDR1H	.EQU	15H
RLDR1L	.EQU	16H
RLDR1H	.EQU	17H

PSG_ADR	.EQU	83H		; PSG address (out)
PSG_DAT	.EQU	82H		; data output
PSG_IN	.EQU	83H		; data input (in)

INITIAL_SP:	.EQU	01000H
PRIVATE_SP:	.EQU	0F000H

; ######## macros

ADD_HL_A:	.MACRO
	ADD	A,L
	LD	L,A
	JR	NC,$ + 3
	INC	H
	.ENDM

WAIT3	.MACRO
	NOP
	.ENDM

WAIT4	.MACRO
	LD	A,A
	.ENDM

WAIT6	.MACRO
	NOP
	NOP
	.ENDM

WAIT7	.MACRO
	LD	A,A			; 4+3=7
	NOP
	.ENDM

WAIT8	.MACRO
	LD	A,A			; 4*2=8
	LD	A,A
	.ENDM

WAIT9	.MACRO
	NOP				; 3*3=9
	NOP
	NOP
	.ENDM

WAIT10	.MACRO
	LD	A,A			; 4+3*2=10
	NOP
	NOP
	.ENDM

WAIT11	.MACRO
	LD	A,A			; 4*2+3=11
	LD	A,A
	NOP
	.ENDM

WAIT12	.MACRO
	LD	A,A			; 4*3=12
	LD	A,A
	LD	A,A
	.ENDM

WAIT13	.MACRO
	LD	A,A			; 4+3*3=13
	NOP
	NOP
	NOP
	.ENDM

WAIT16	.MACRO
	LD	A,A
	LD	A,A
	LD	A,A
	LD	A,A
	.ENDM

WAIT17	.MACRO
	LD	A,A			; 4*2+3*3=17
	LD	A,A
	NOP
	NOP
	NOP
	.ENDM

WAIT19	.MACRO
	LD	A,A			; 4*4+3=19
	LD	A,A
	LD	A,A
	LD	A,A
	NOP
	.ENDM

; ######## RESET/RST
	.ORG	0000H
RESET:
	JP	ENTRY

	.ORG	0038H
INT0:
	JP	INTR_INT0

	.ORG	0066H
NMI:
	RETN

	.ORG	0080H
DEBUG0::	.DB	0
DEBUG1::	.DB	0
DEBUG2::	.DB	0
DEBUG3::	.DB	0
DEBUG4::	.DB	0
DEBUG5::	.DB	0
DEBUG6::	.DB	0
DEBUG7::	.DB	0
DEBUG8::	.DB	0
DEBUG9::	.DB	0
DEBUG10::	.DB	0

	.ORG	00FCH
XPLX_MAGIC::			; MAGIC
	.DB	"XPLX"

; ######## shared variables
; XPBUS
	.ORG	0100H
XPLX_VAR_BASE::
XPBUS_READY::
	.DB	0
XPBUS_CMD::
	.DB	0
XPBUS_RESULT::
	.DB	0
XPBUS_RUN::
	.DB	0

XPBUS_STAT_RESET::		; reset count
	.DB	0
	.DB	0,0,0		; reserved

XPBUS_PRT0_TIMER::		; PRT0 TIMER TLDR (devices dispatch)
	.DW	256
XPBUS_INTR1_DEV::		; HOSTINTR1 device
	.DW	0
XPBUS_INTR5_DEV::		; HOSTINTR5 device
	.DW	0

; TIME
	.ORG	0110H
TIME_READY::
	.DB	0
TIME_CMD::
	.DB	0
TIME_RESULT::
	.DB	0
TIME_RUN::
	.DB	0
TIME_TIMECOUNTER::		; timecounter (TBD.)
	.DW	0

; PAM
	.ORG	0120H
PAM_READY::
	.DB	0
PAM_CMD::
	.DB	0
PAM_RESULT::
	.DB	0
PAM_RUN::
	.DB	0

PAM_ENC::
	.DB	0
PAM_REPT::
	.DB	0
PAM_CYCLE_CLK::
	.DW	0
PAM_REPT_CLK::
	.DB	0
PAM_REPT_MAX::
	.DB	0

	.DB	0,0,0,0		; reserved
PAM_STAT_PTR::
	.DW	0

; PCM
	.ORG	0130H
PCM_READY::
	.DB	0
PCM_CMD::
	.DB	0
PCM_RESULT::
	.DB	0
PCM_RUN::
	.DB	0

PCM_ENC::
	.DB	0
	.DB	0		; reserved
PCM_PRT1_TIMER::			; PRT1 TIMER TLDR (PCM)
	.DW	0

	.DB	0,0,0,0,0,0	; reserved
PCM_STAT_PTR::
	.DW	0

; PSG
	.ORG	0140H
PSG_READY::
	.DB	0
PSG_CMD::
	.DB	0
PSG_RESULT::
	.DB	0
PSG_RUN::
	.DB	0

; SPK
	.ORG	0150H
SPK_READY::
	.DB	0
SPK_CMD::
	.DB	0
SPK_RESULT::
	.DB	0
SPK_RUN::
	.DB	0

SPK_VOL::
	.DB	0
	.DB	0		; reserved
SPK_FREQ::
	.DW	0
SPK_TIME::
	.DW	0
SPK_REMAIN::
	.DW	0

; LPR
	.ORG	0160H
LPR_READY::
	.DB	0
LPR_CMD::
	.DB	0
LPR_RESULT::
	.DB	0
LPR_RUN::
	.DB	0
	; TBD.

LPR_CMD_START	.EQU	1

; FDC
	.ORG	0170H
FDC_READY::
	.DB	0
FDC_CMD::
	.DB	0
FDC_RESULT::
	.DB	0
FDC_RUN::
	.DB	0
; TBD.

FDC_CMD_START	.EQU	1

; SIO0
	.ORG	0180H
SIO0_READY::
	.DB	0
SIO0_CMD::
	.DB	0
SIO0_RESULT::
	.DB	0
SIO0_RUN::
	.DB	0

SIO0_TXCMD::
	.DB	0
SIO0_TXSTAT::
	.DB	0
SIO0_TX::
	.DB	0
	.DS	3
SIO0_RXCMD::
	.DB	0
SIO0_RXSTAT::
	.DB	0
SIO0_RX::
	.DB	0

; SIO1
	.ORG	0190H
SIO1_READY::
	.DB	0
SIO1_CMD::
	.DB	0
SIO1_RESULT::
	.DB	0
SIO1_RUN::
	.DB	0

SIO1_TXCMD::
	.DB	0
SIO1_TXSTAT::
	.DB	0
SIO1_TX::
	.DB	0
	.DS	3
SIO1_RXCMD::
	.DB	0
SIO1_RXSTAT::
	.DB	0
SIO1_RX::
	.DB	0


; ######## Bootstrap program
	.ORG	0200H
ENTRY:
	DI
	LD	SP,INITIAL_SP

				; inc reset count
	LD	HL, XPBUS_STAT_RESET
	INC	(HL)

				; initial devices
				; READY=0
	XOR	A
	LD	(XPBUS_READY),A
	LD	(TIME_READY),A
	LD	(PAM_READY),A
	LD	(PCM_READY),A
	LD	(PSG_READY),A
	LD	(SPK_READY),A
	LD	(LPR_READY),A
	LD	(FDC_READY),A
	LD	(SIO0_READY),A
	LD	(SIO1_READY),A

	LD	A,1
	LD	(DEBUG0),A

				; init XP internal devices
				; internal I/O address = 00H - 3FH
	LD	A,00H		; IOA7[7]=0 IOSTP[5]=0
ICR	.EQU	3FH
	OUT0	(ICR),A

				; memory wait = 0
				; I/O wait = 3
				; no DMA
	LD	A,20H		; MWI[76]=0 IWI[54]=2(3wait) DMS[32]=0 DIM[10]=0
DCNTL	.EQU	32H
	OUT0	(DCNTL),A
				; disable refresh
	LD	A,03H		; REFE[7]=0 REFW[6]=0 CYC[10]=3(80state)
RCR	.EQU	36H
	OUT0	(RCR),A

	LD	A,2
	LD	(DEBUG0),A

				; prepare memory map
				; MMU
CBR	.EQU	38H
BBR	.EQU	39H
CBAR	.EQU	3AH
				; Common0: VA=0000H -> PA=00000H SH
				; Bank   : VA=E000H -> PA=28000H PR
				; Common1: VA=F000H -> PA=FF000H IN
	LD	A,0FEH
	OUT0	(CBAR),A
	LD	A,0F0H
	OUT0	(CBR),A
	LD	A,1AH
	OUT0	(BBR),A

	LD	A,3
	LD	(DEBUG0),A

				; internal RAM addressing
				; for no-wait access
				; PA=FxxxxH にしたらノーウェイトになった。
				; PA=0xxxxH だとウェイトが入った。
				; ほかのアドレスは未調査。
				; built-in RAM VA=FE00H PA=FFE00H
	LD	A,0F0H
RMCR	.EQU	51H
	OUT0	(RMCR),A
				; disable external interrupt
				; TODO: if use "Host to XP" interrupt, change here
	LD	A,00H		; TRAP[7]=0 ITE2[2]=0 ITE1[1]=0 ITE0[0]=0
ITC	.EQU	34H
	OUT0	(ITC),A
				; Interrupt Vector Low = E
				; I = FFH
				; Interrupt Vector Address = FFE0H
	LD	A,0E0H
IL	.EQU	33H
	OUT0	(IL),A
	LD	A,0FFH
	LD	I,A
				; interrupt mode 1
	IM	1

	LD	A,4
	LD	(DEBUG0),A

	CALL	INIT_PSG

	; TODO
	; INIT FDC
	; INIT LPR
	; INIT SIO

				; INIT PRT0,1
				; TIE1[5]=TIE0[4]=0
				; TOC1[3]=TOC0[2]=0
				; TDE1[1]=TDE0[0]=0
	LD	A,00H
	OUT0	(TCR),A
				; prepare PRT0
	LD	HL,(XPBUS_PRT0_TIMER)
	OUT0	(RLDR0L),L
	OUT0	(TMDR0L),L
	OUT0	(RLDR0H),H
	OUT0	(TMDR0H),H
				; TIE0, TID0 ON
				; TIE0[4]=1 TDE0[0]=1
	LD	A,11H
	OUT0	(TCR),A

				; copy to private memory
	LD	HL,PROG_ORG
	LD	DE,PRIVATE_RAM
	LD	BC,PROG_ORG_LEN
	LDIR
				; interrupt vector copy to internal memory
	LD	HL,VECTOR_ORG
	LD	DE,VECTOR
	LD	BC,VECTOR_ORG_LEN
	LDIR

	LD	A,5
	LD	(DEBUG0),A
				; jump to XPBUS
	JP	XPBUS

; initialize PSG registers
; break all regs
INIT_PSG:
				; init PSG
				; PSG R0-R6 All 00H
	LD	A,0
	LD	B,7
	LD	C,PSG_DAT
	LD	D,0
PSG_CLEAR_06:
	OUT	(PSG_ADR),A
	OUT	(C),D
	INC	A
	DJNZ	PSG_CLEAR_06
				; PSG mixer
				; tone = off, noise = off
				; IOA, IOB = output
	LD	A,7
	LD	D,0FFH
	OUT	(PSG_ADR),A
	OUT	(C),D
				; PSG volume and envelope
				; PSG R8-R15 all 0
	LD	A,8
	LD	B,8
	LD	D,0
PSG_CLEAR_8F:
	OUT	(PSG_ADR),A
	OUT	(C),D
	INC	A
	DJNZ	PSG_CLEAR_8F
				; TODO: PSG I/O Port
	RET

; ######## buffers
	.PHASE	1000H
PAM_BUF::
PCM_BUF::
	.DEPHASE
	.PHASE 08000H
PAM_BUF_LEN::	.EQU	$-PAM_BUF
PCM_BUF_LEN::	.EQU	$-PCM_BUF
PSG_BUF::
	.DEPHASE
	.PHASE 09000H
PSG_BUF_LEN::	.EQU	$-PSG_BUF
LPR_BUF::
	.DEPHASE
	.PHASE 0A000H
LPR_BUF_LEN::	.EQU	$-LPR_BUF
FDC_BUF::
	.DEPHASE

; ######## private memory program
	.PHASE 0E000H
FDC_BUF_LEN::	.EQU	$-FDC_BUF

PROG_ORG:	.EQU	$$
PRIVATE_RAM:

XPBUS:
	LD	A,6
	LD	(DEBUG0),A

	LD	SP,PRIVATE_SP

				; devices READY=1
	LD	A,1
	LD	(XPBUS_READY),A
	LD	(TIME_READY),A
	LD	(PAM_READY),A
	LD	(PCM_READY),A
	LD	(PSG_READY),A
	LD	(SPK_READY),A
	LD	(LPR_READY),A
	LD	(FDC_READY),A
	LD	(SIO0_READY),A
	LD	(SIO1_READY),A

				; wait for PRT0
	EI
XPBUS_LOOP:
	HALT
	JR	XPBUS_LOOP

INTR_PRT0:
; #### Periodic devices
; 1200Hz
; ここから呼び出される DISPATCH ルーチンは、
; o. A にコマンドが入っている
; o. AF, HL は破壊して良い。
; o. EI 状態で呼ばれることに注意。
; o. EI 状態でリターンすること。
; o. 裏レジスタは PCM 専用。
; o. PAM 以外、0.83 msec 以内にリターンすること。

	PUSH	AF
	PUSH	HL

	LD	A,7
	LD	(DEBUG0),A
				; reset PRT0 interrupt
	IN0	F,(TCR)
	IN0	F,(TMDR0L)
				; first EI, for PRT1
	EI

TIMECOUNTER_INCR:
				; timecounter
	LD	HL,(TIME_TIMECOUNTER)
	INC	HL
	LD	(TIME_TIMECOUNTER),HL

; #### XPBUS devices dispatcher

DEVICES_DISPATCH:
	LD	A,(XPBUS_CMD)
	OR	A
	CALL	NZ,XPBUS_DISPATCH

	LD	A,(PAM_CMD)
	OR	A
	CALL	NZ,PAM_DISPATCH

	LD	A,(PCM_CMD)
	OR	A
	CALL	NZ,PCM_DISPATCH

	LD	A,(PSG_CMD)
	OR	A
	CALL	NZ,PSG_DISPATCH

	LD	A,(SPK_CMD)
	OR	A
	CALL	NZ,SPK_DISPATCH

	LD	A,(LPR_CMD)
	OR	A
	CALL	NZ,LPR_DISPATCH

	LD	A,(FDC_CMD)
	OR	A
	CALL	NZ,FDC_DISPATCH

	LD	A,(SIO0_CMD)
	OR	A
	CALL	NZ,SIO0_DISPATCH

	LD	A,(SIO1_CMD)
	OR	A
	CALL	NZ,SIO1_DISPATCH

	LD	A,8
	LD	(DEBUG0),A

	POP	HL
	POP	AF
	RETI

; #### XPBUS

XPBUS_DISPATCH:
	; not implemented
	XOR	A
	LD	(XPBUS_CMD),A
	LD	A,XPLX_R_UNKNOWN_CMD
	LD	(XPBUS_RESULT),A
	RET

; #### TIME

TIME_DISPATCH:
	; not implemented
	XOR	A
	LD	(TIME_CMD),A
	LD	A,XPLX_R_UNKNOWN_CMD
	LD	(TIME_RESULT),A
	RET

; #### PAM は末尾

; #### PCM driver core

; PCM 割り込みは裏レジスタを専有します。
; メインルーチン側では裏レジスタを使用してはいけません。

; #### PCM play start
PCM_DISPATCH:
	CP	PCM_CMD_START
	JR	Z,PCM_START

	LD	A,XPLX_R_UNKNOWN_CMD
PCM_ERROR:
	LD	(PCM_RESULT),A
	RET

PCM_START:
				; if READY==0 return
	LD	A,(PCM_READY)
	OR	A
	RET	Z
				; check ENC
	LD	A,(PCM_ENC)
	DEC	A
	JR	Z,PCM_START_OK	; PCM1 = 1
	DEC	A
	JR	Z,PCM_START_OK	; PCM2 = 2
	DEC	A
	JR	Z,PCM_START_OK	; PCM3 = 3

	LD	A,XPLX_R_ERROR_PARAM
	JR	PCM_ERROR

PCM_START_OK:
				; A = 0
	LD	(PCM_READY),A
	LD	(PCM_CMD),A


				; prepare vector
	DI
				; set PRT1 vector
	LD	HL,PCM_INTR
	LD	(VEC_PRT1),HL
				; prepare register
	EXX

	CALL	INIT_PSG

				; make interrupt handler
	LD	A,(PCM_ENC)
	DEC	A
	JR	Z,PCM_SET_PCM1
	DEC	A
	JR	Z,PCM_SET_PCM2
PCM_SET_PCM3:
	LD	HL,PCM3
	JR	PCM_SET
PCM_SET_PCM2:
	LD	HL,PCM2
	JR	PCM_SET
PCM_SET_PCM1:
	LD	HL,PCM1
PCM_SET:
	LD	(PCM_INTR_JMP),HL

	LD	HL,PCM_BUF
	LD	BC,0800H + PSG_ADR
	LD	DE,0709H

	EXX

				; TIE1, TDE1 OFF
	IN0	A,(TCR)
	AND	0DDH		; TIE1[5]=0 TDE1[1]=0
	OUT0	(TCR),A
				; prepare PRT1
	LD	HL,(PCM_PRT1_TIMER)
	OUT0	(RLDR1L),L
	OUT0	(RLDR1H),H
	OUT0	(TMDR1L),L
	OUT0	(TMDR1H),H
				; TIE1, TID1 ON
	OR	22H		; TIE1[5]=1 TDE1[5]=1
	OUT0	(TCR),A

	EI

	LD	A,1
	LD	(PCM_RUN),A

	RET



; #### PCM interrupt handler

PCM_INTR:
				; PRT1 interrupt
	EX	AF,AF
	EXX
				; interrupt acknowledge
				; reset PRT1 Interrupt
	IN0	F,(TCR)
	IN0	F,(TMDR1L)

				; ジャンプ先は書き換えられる
PCM_INTR_JMP:	.EQU	$+1
	JP	PCM1

PCM_INTR_NEXT:
	RLCA
	JR	C,PCM_RELOAD
				; inc ptr after reload check
	INC	HL
	RLCA
	JR	C,PCM_STAT
	RLCA
	JR	NC,PCM_NORMAL

; PCM RESET attention
; in: HL = EXIT address
PCM_RESET:
				; PRT1 intr stop
	IN0	A,(TCR)
				; TIE1,TDE1 OFF
	AND	0DDH		; TIE1[5]=0 TDE1[1]=0
	OUT0	(TCR),A
				; PLAY STOP
	XOR	A
	LD	(PCM_RUN),A
	LD	A,XPLX_R_OK
	LD	(PCM_RESULT),A
	LD	(PCM_READY),A

	JR	PCM_EXIT

; PCM common code

PCM_RELOAD:
	LD	HL,PCM_BUF
PCM_STAT:
#if USE_INTR
	OUT	(PCM_HOSTINTR),A
#else
	LD	(PCM_STAT_PTR),HL
#endif
PCM_NORMAL:
PCM_EXIT:
	EXX
	EX	AF,AF
	EI
	RETI

; #### PCM core code

PCM1:
				; PSG REG=8
	OUT	(C),B
				; read attention or CH0
	LD	A,(HL)
	OUT	(PSG_DAT),A
	JP	PCM_INTR_NEXT

PCM2:
	LD	D,(HL)
	INC	HL
	LD	A,(HL)

	OUT	(C),B
	OUT0	(PSG_DAT),D
	OUT	(C),E
	OUT	(PSG_DAT),A
	JP	PCM_INTR_NEXT

PCM3:
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	INC	HL
	LD	A,(HL)

	PUSH	HL
	LD	HL,090AH
	OUT	(C),B
	OUT0	(PSG_DAT),E
	OUT	(C),H
	OUT0	(PSG_DAT),D
	OUT	(C),L
	OUT	(PSG_DAT),A
	POP	HL
	JP	PCM_INTR_NEXT

; #### SPK
SPK_DISPATCH:
	CP	SPK_CMD_START
	JR	Z,SPK_START
	CP	SPK_CMD_STOP
	JR	Z,SPK_STOP
	CP	SPK_CMD_KEEP
	JR	Z,SPK_KEEP

	LD	A,XPLX_R_UNKNOWN_CMD
	LD	(SPK_RESULT),A
	RET

SPK_START:
	LD	A,(SPK_READY)
	OR	A
	RET	Z

	XOR	A
	LD	(SPK_READY),A
				; next to CMD_KEEP
	LD	A,SPK_CMD_KEEP
	LD	(SPK_CMD),A
	LD	A,1
	LD	(SPK_RUN),A

				; set REMAIN
	LD	HL,(SPK_TIME)
	LD	(SPK_REMAIN),HL

	DI
				; PSG CH3 FREQ
	LD	HL,(SPK_FREQ)
	LD	A,4
	OUT0	(PSG_ADR),A
	OUT0	(PSG_DAT),L
	LD	A,5
	OUT0	(PSG_ADR),A
	OUT0	(PSG_DAT),H
				; PSG CH3 VOL
	LD	A,10
	OUT	(PSG_ADR),A
	LD	A,(SPK_VOL)
	OUT	(PSG_DAT),A
				; save PSG R7
	LD	A,7
	OUT0	(PSG_ADR),A
	IN	A,(PSG_IN)
	LD	(SPK_PSGR7),A
				; PSG CH3 TONE ON
	AND	0FBH
	OUT	(PSG_DAT),A

	JR	SPK_EXIT

SPK_STOP:
	LD	A,(SPK_READY)
	OR	A
	RET	Z

SPK_STOP_CORE:
	XOR	A
	LD	(SPK_READY),A
	LD	(SPK_CMD),A

	DI
				; restore PSG R7
	LD	A,7
	OUT	(PSG_ADR),A
	LD	A,(SPK_PSGR7)
	OUT	(PSG_DAT),A
				; PSG CH3 VOL=0
	LD	A,10
	OUT	(PSG_ADR),A
	XOR	A
	OUT	(PSG_DAT),A

	LD	(SPK_RUN),A

	JR	SPK_EXIT

SPK_KEEP:
				; REMAIN == 0, then stop
	LD	HL,(SPK_REMAIN)
	LD	A,H
	OR	L
	JR	Z,SPK_STOP_CORE

	DEC	HL
	LD	(SPK_REMAIN),HL

SPK_EXIT:
	EI
	LD	A,XPLX_R_OK
	LD	(SPK_RESULT),A
	LD	(SPK_READY),A
	RET

SPK_PSGR7:
	.DB	0

; ######## PSG
PSG_DISPATCH:
	; not implemented
	XOR	A
	LD	(PSG_CMD),A
	LD	A,XPLX_R_UNKNOWN_CMD
	LD	(PSG_RESULT),A
	RET
; ######## LPR
LPR_DISPATCH:
	; not implemented
	XOR	A
	LD	(LPR_CMD),A
	LD	A,XPLX_R_UNKNOWN_CMD
	LD	(LPR_RESULT),A
	RET
; ######## FDC
FDC_DISPATCH:
	; not implemented
	XOR	A
	LD	(FDC_CMD),A
	LD	A,XPLX_R_UNKNOWN_CMD
	LD	(FDC_RESULT),A
	RET

; ######## SIO
SIO0_DISPATCH:
	; not implemented
	XOR	A
	LD	(SIO0_CMD),A
	LD	A,XPLX_R_UNKNOWN_CMD
	LD	(SIO0_RESULT),A
	RET

SIO1_DISPATCH:
	; not implemented
	XOR	A
	LD	(SIO1_CMD),A
	LD	A,XPLX_R_UNKNOWN_CMD
	LD	(SIO1_RESULT),A
	RET

INTR_INT0:
INTR_ASCI0:
INTR_ASCI1:
				; TBD
	EI
	RETI

; #### PAM play start

PAM_DISPATCH:
	CP	PAM_CMD_START
	JR	Z,PAM_START
	CP	PAM_CMD_QUERY
	JR	Z,PAM_QUERY

	XOR	A
	LD	(PAM_CMD),A
	LD	A,XPLX_R_UNKNOWN_CMD
	LD	(PAM_RESULT),A
	RET

; PAM ENC -> PAM Driver MAP address
; OUT: HL = MAP address
; if error, direct return to main routine
PAM_ENC_MAP:
	LD	A,(PAM_ENC)
	OR	A
	JR	Z,PAM_ERROR_ENC
	DEC	A

	CP	PAM_DRIVER_MAP_LEN / 16		; 16 bytes / entry
	JP	NC,PAM_ERROR_ENC

	ADD	A,A		; A *= 16
	ADD	A,A
	ADD	A,A
	ADD	A,A

	LD	HL,PAM_DRIVER_MAP
	ADD_HL_A
	RET

PAM_ERROR_ENC:
	POP	HL		; discard caller PC
PAM_ERROR_PARAM:
	LD	A,XPLX_R_ERROR_PARAM
	LD	(PAM_RESULT),A
	RET			; return to main

PAM_QUERY:
	CALL	PAM_ENC_MAP	; get ENC to MAP

	LD	A,(PAM_READY)
	OR	A
	RET	Z

	XOR	A
	LD	(PAM_READY),A
	LD	(PAM_CMD),A

	PUSH	BC
	PUSH	DE

	LD	BC,12		; MAP offset 12 = CYCLE_CLK
	ADD	HL,BC

				; CYCLE_CLK, REPT_CLK, REPT_MAX
	LD	DE,PAM_CYCLE_CLK
	LD	BC,4
	LDIR

	POP	DE
	POP	BC

	LD	A,XPLX_R_OK
	LD	(PAM_RESULT),A
	LD	(PAM_READY),A
	RET


PAM_START:
	CALL	PAM_ENC_MAP	; get ENC to MAP

	LD	A,15
	ADD_HL_A		; HL points REPT_MAX

	LD	A,(PAM_REPT)
	CP	(HL)
	JR	Z,PAM_START_OK	; == OK
	JR	C,PAM_START_OK	; < OK
	JR	PAM_ERROR_PARAM

PAM_START_OK:
	LD	A,(PAM_READY)
	OR	A
	RET	Z

	XOR	A
	LD	(PAM_READY),A
	LD	(PAM_CMD),A

				; never normal return
				; PAM never EI
	DI
	CALL	INIT_PSG

	CALL	PAM_ENC_MAP	; re- get ENC to MAP

				; copy to internal RAM
	LD	DE,PAM_DRIVER

	LD	SP,HL		; SP = top of Map entry
	POP	HL		; HEAD
	POP	BC		; HEAD_LEN
	LDIR

	LD	A,(PAM_REPT)
	INC	A		; DEC is not change CY


PAM_REPT_LOOP:
	POP	HL		; REPT
	POP	BC		; REPT_LEN

	DEC	A		; DEC is not change CY
	JR	Z,PAM_REPT_END

	LDIR

	DEC	SP
	DEC	SP
	DEC	SP
	DEC	SP
	JR	PAM_REPT_LOOP
PAM_REPT_END:

	POP	HL		; TAIL
	POP	BC		; TAIL_LEN
	LDIR

				; buffer pointer
	LD	HL,PAM_BUF
#if USE_INTR
#else
	LD	(PAM_STAT_PTR),HL
#endif
				; prefetch
	LD	SP,HL			; 4
	POP	DE

; I/O WAIT 3 -> 2
; PSG の address / write 時間 は 300ns なので、
; 1.8432 clock あればよいので 1 wait から設定できるはずだが、
; 2 wait に設定すると out 命令が 12 clock となり、
; 共有メモリに対する POP の 9+3=12 clock と一致して
; クロック整合を取りやすくなるため、2 wait に設定する。
; なお PSG の read は 400ns 必要なため、2 wait だとあやしい。
; また HOSTINTR 用の I/O への out で wait を満足するかどうかは
; 未確定だけど、HOSTINTR 機構はデータバスの値ではなく
; アドレスへの出力で動作するため、ウェイトに関係なく動作すると
; 期待してみる。
	LD	A,10H		; IWI[54]=1(2wait)
	OUT0	(DCNTL),A

	LD	A,1
	LD	(PAM_RUN),A

	LD	A,8
	OUT	(PSG_ADR),A
	LD	C,PSG_DAT

	JP	PAM_DRIVER

PAM_RESET:
				; XPBUS に制御を戻す
	LD	SP,PRIVATE_SP

; I/O WAIT 2 -> 3
	LD	A,20H		; IWI[54]=2(3wait)
	OUT0	(DCNTL),A

	CALL	INIT_PSG

	XOR	A
	LD	(PAM_RUN),A

	LD	A,XPLX_R_OK
	LD	(PAM_RESULT),A
	LD	(PAM_READY),A

	JP	XPBUS

PAM_DRIVER_MAP:
				; 16 bytes / entry
	DW	PAM2A_HEAD_ORG
	DW	PAM2A_HEAD_LEN
	DW	PAM2A_REPT_ORG
	DW	PAM2A_REPT_LEN
	DW	PAM2A_TAIL_ORG
	DW	PAM2A_TAIL_LEN
	DW	204		;CYCLE_CLK
	DB	36		;REPT_CLK
	DB	37		;REPT_MAX

	DW	PAM2B_HEAD_ORG
	DW	PAM2B_HEAD_LEN
	DW	PAM2B_REPT_ORG
	DW	PAM2B_REPT_LEN
	DW	PAM2B_TAIL_ORG
	DW	PAM2B_TAIL_LEN
	DW	152		;CYCLE_CLK
	DB	24		;REPT_CLK
	DB	57		;REPT_MAX

	DW	PAM3A_HEAD_ORG
	DW	PAM3A_HEAD_LEN
	DW	PAM3A_REPT_ORG
	DW	PAM3A_REPT_LEN
	DW	PAM3A_TAIL_ORG
	DW	PAM3A_TAIL_LEN
	DW	298		;CYCLE_CLK
	DB	51		;REPT_CLK
	DB	24		;REPT_MAX

	DW	PAM3B_HEAD_ORG
	DW	PAM3B_HEAD_LEN
	DW	PAM3B_REPT_ORG
	DW	PAM3B_REPT_LEN
	DW	PAM3B_TAIL_ORG
	DW	PAM3B_TAIL_LEN
	DW	136		;CYCLE_CLK
	DB	36		;REPT_CLK
	DB	38		;REPT_MAX
	


PAM_DRIVER_MAP_LEN:	.EQU	$-PAM_DRIVER_MAP

	.DEPHASE



; ######## PAM drivers
	.PHASE 0FE00H
				; all PAM drivers have same address=0FE00H
PAM_DRIVER:
	.DEPHASE

; #### PAM2A

	.PHASE 0FE00H
PAM2A_HEAD_ORG:	.EQU	$$
PAM2A_HEAD:
PAM2A:
				; PAM2A
				; 12+0:12+12 = 1:2 PAM
				; PAM 36clk 170.667kHz
				; output PAM wave = normal 5 + antinoise 1

				; 1 PAM cycle = 204 clk

				; 6.144E6 / (204 + 36*n)

				; sampling freqs:
				;  0: 30118
				; 37:  4000

				; no STAT for first time
	JP	PAM2A_LOOP

PAM2A_RELOAD:
	OUT	(C),E
	OUT	(C),D
	LD	SP,PAM_BUF		;9
	WAIT3

PAM2A_STAT:
#if USE_INTR
	OUT	(C),E
	OUT	(C),D
	OUT	(PAM_HOSTINTR),A		;10+2
#else
				; STAT_PTR モードでの遅れはしょうがない
	OUT	(C),E
	OUT	(C),D
	LD	(PAM_STAT_PTR),SP		;19+3
#endif

PAM2A_NORMAL:
	OUT	(C),E
	OUT	(C),D
				; prefetch
	POP	DE			;9+3

	OUT	(C),L
	OUT	(C),H
				; うまくいくかはわからない
				; 本来 wait 12 だが PAM 遷移ノイズを
				; 低減するため待たない
PAM2A_LOOP:
				; prefetched DE
	OUT	(C),E
	OUT	(C),D
				; HL = DE for save current sample
	LD	L,E			;4
	LD	H,D			;4
				; A = attention
	LD	A,E			;4

PAM2A_HEAD_LEN:	.EQU	$-PAM2A_HEAD

PAM2A_REPT_ORG:	.EQU	$$
PAM2A_REPT:
	OUT	(C),E
	OUT	(C),D
	WAIT12
PAM2A_REPT_LEN:	.EQU	$-PAM2A_REPT

PAM2A_TAIL_ORG:	.EQU	$$
PAM2A_TAIL:
				; このブロックは動的再配置されるので
				; このブロック"への"ジャンプは困難
				; "からの"ジャンプは可能。
	OUT	(C),E
	OUT	(C),D
	RLCA
				; attention bit
				; bit7=1, reload
				; must be JP
	JP	C,PAM2A_RELOAD		; jump=9 no=6

	WAIT3
	OUT	(C),E
	OUT	(C),D
	RLCA				; 3
				; bit6=1, stat
				; must be JP
	JP	C,PAM2A_STAT		; jump=9 no=6

	WAIT3
	OUT	(C),E
	OUT	(C),D
	RLCA				; 3
				; bit5=0, normal
				; must be JP
	JP	NC,PAM2A_NORMAL		; jump=9 no=6
				; attention=001, reset
	JP	PAM_RESET
PAM2A_TAIL_LEN:	.EQU	$-PAM2A_TAIL

				; cycle
				; 5 * (12*3) + 12*2 = 204

	.DEPHASE

; #### PAM2B

	.PHASE 0FE00H
				; all PAM drivers have same address=0FE00H
PAM2B_HEAD_ORG:	.EQU	$$
PAM2B_HEAD:
PAM2B:
				; PAM2B
				; averaged 1:1 PAM
				; wait (4,7), (3,9), (9,12), (12,0)
				; phase wait 28:28
				; clk  35, 36, 45, 36
				; PAM 176, 171, 137, 171 kHz
				; output PAM wave = 4

				; 1 PAM cycle = 152 clk

				; 6.144E6 / (152 + 24*n)

				; sampling freqs:
				;  0: 40421
				; 57:  4042

				; no STAT for first time
	JP	PAM2B_LOOP

PAM2B_RELOAD:
	OUT	(C),E
	LD	SP,PAM_BUF		;9

PAM2B_STAT:
#if USE_INTR
	OUT	(C),D
	OUT	(PAM_HOSTINTR),A		;10+2
#else
				; STAT_PTR モードでの遅れはしょうがない
	OUT	(C),D
	LD	(PAM_STAT_PTR),SP		;19+3
#endif

PAM2B_NORMAL:
	OUT	(C),E
				; prefetch
	POP	DE			;9+3
	OUT	(C),B
PAM2B_LOOP:
				; prefetched DE
	OUT	(C),E
				; A = attention
	LD	A,E			;4
	OUT	(C),D
				; B = save D
	LD	B,D			;4
	WAIT3

PAM2B_HEAD_LEN:	.EQU	$-PAM2B_HEAD

PAM2B_REPT_ORG:	.EQU	$$
PAM2B_REPT:
	OUT	(C),E
	OUT	(C),D
PAM2B_REPT_LEN:	.EQU	$-PAM2B_REPT

PAM2B_TAIL_ORG:	.EQU	$$
PAM2B_TAIL:
				; このブロックは動的再配置されるので
				; このブロック"への"ジャンプは困難
				; "からの"ジャンプは可能。
	OUT	(C),E
	RLCA				;3
	OUT	(C),D
				; attention bit
				; bit7=1, reload
				; must be JP
	JP	C,PAM2B_RELOAD		; jump=9 no=6

	RLCA				; 3
	OUT	(C),E
				; bit6=1, stat
				; must be JP
	JP	C,PAM2B_STAT		; jump=9 no=6

	RLCA				; 3
	OUT	(C),D
	WAIT3
				; bit5=0, normal
				; must be JP
	JP	NC,PAM2B_NORMAL		; jump=9 no=6
				; attention=001, reset
	JP	PAM_RESET
PAM2B_TAIL_LEN:	.EQU	$-PAM2B_TAIL

				; cycle
				; 4 * 12*2 + (4+7 + 3+9 + 9+12 + 12+0) = 152

	.DEPHASE

; #### PAM3A

	.PHASE 0FE00H
PAM3A_HEAD_ORG:	.EQU	$$
PAM3A_HEAD:
PAM3A:
				; PAM3A
				; 12+0:12+3:12+12 = 4:5:8 PAM
				; PAM 51clk 120.471kHz
				; output PAM wave = normal 5 + antinoise 1

				; 1 PAM cycle = 298 clk

				; 6.144E6 / (298 + 51*n)

				; sampling freqs:
				; 0: 20617
				; 24: 4037

				; prefetch
	POP	AF
	LD	B,A
				; no STAT for first time
	JP	PAM3A_LOOP

PAM3A_RELOAD:
	OUT	(C),L
	OUT	(C),H
	WAIT3
	OUT	(C),B
	LD	SP,PAM_BUF		;9
	WAIT3

PAM3A_STAT:
#if USE_INTR
	OUT	(C),L
	OUT	(C),H
	WAIT3
	OUT	(C),B
	OUT	(PAM_HOSTINTR),A		;10+2
#else
				; STAT_PTR モードでの遅れはしょうがない
	OUT	(C),L
	OUT	(C),H
	WAIT3
	OUT	(C),B
	LD	(PAM_STAT_PTR),SP		;19+3
#endif

PAM3A_NORMAL:
	OUT	(C),L
	OUT	(C),H
	WAIT3
	OUT	(C),B
				; prefetch
	POP	DE			;9+3

	OUT	(C),L
	OUT	(C),H
	WAIT3
	OUT	(C),B
				; prefetch
	POP	AF			;9+3

	OUT	(C),L
	OUT	(C),H
	WAIT3
	OUT	(C),B
				; うまくいくかはわからない
				; 本来 wait 12 だが PAM 遷移ノイズを
				; 低減するのも含めて4clkだけ消費する
	LD	B,A			;4
PAM3A_LOOP:
				; prefetched DE, A=B

PAM3A_HEAD_LEN:	.EQU	$-PAM3A_HEAD

PAM3A_REPT_ORG:	.EQU	$$
PAM3A_REPT:
	OUT	(C),E
	OUT	(C),D
	WAIT3
	OUT	(C),B
	WAIT12
PAM3A_REPT_LEN:	.EQU	$-PAM3A_REPT

PAM3A_TAIL_ORG:	.EQU	$$
PAM3A_TAIL:
				; このブロックは動的再配置されるので
				; このブロック"への"ジャンプは困難
				; "からの"ジャンプは可能。
	OUT	(C),E
	OUT	(C),D
	EX	DE,HL			;3
	OUT	(C),B
	RLCA
				; attention bit
				; bit7=1, reload
				; must be JP
	JP	C,PAM3A_RELOAD		; jump=9 no=6

	WAIT3
	OUT	(C),L
	OUT	(C),H
	WAIT3
	OUT	(C),B
	RLCA				; 3
				; bit6=1, stat
				; must be JP
	JP	C,PAM3A_STAT		; jump=9 no=6

	WAIT3
	OUT	(C),L
	OUT	(C),H
	WAIT3
	OUT	(C),B
	RLCA				; 3
				; bit5=0, normal
				; must be JP
	JP	NC,PAM3A_NORMAL		; jump=9 no=6
				; attention=001, reset
	JP	PAM_RESET
PAM3A_TAIL_LEN:	.EQU	$-PAM3A_TAIL

				; cycle
				; 5 * (12*3+3+12) + (12*3+3+4) = 298

	.DEPHASE

; #### PAM3B

	.PHASE 0FE00H
PAM3B_HEAD_ORG:	.EQU	$$
PAM3B_HEAD:
PAM3B:
				; PAM3B
				; approx 1:1:1
				; wait (9,9,12), (12,12,10)
				; phase wait 21:21:22
				; clk 66, 70
				; PAM 93, 88 kHz
				; output PAM wave = 2

				; 1 PAM cycle = 136 clk

				; 6.144E6 / (136 + 36*n)

				; sampling freqs:
				; 0: 45176
				; 38: 4085

				; prefetch
	POP	AF
	LD	B,A
	RLCA
				; no STAT for first time
	JP	PAM3B_LOOP

PAM3B_RELOAD:
	OUT	(C),D
	LD	SP,PAM_BUF		;9

PAM3B_STAT:
#if USE_INTR
	OUT	(C),B
	OUT	(PAM_HOSTINTR),A		;10+2
#else
				; STAT_PTR モードでの遅れはしょうがない
	OUT	(C),B
	LD	(PAM_STAT_PTR),SP		;19+3
#endif

PAM3B_NORMAL:
	OUT	(C),E
				; prefetch
	POP	HL			;9+3

	OUT	(C),D
				; prefetch
	POP	AF			;9+3

	OUT	(C),B
	EX	DE,HL			;3
	LD	B,A			;4
	RLCA				;3
PAM3B_LOOP:
				; prefetched DE,B A=RLCA-ed flag

PAM3B_HEAD_LEN:	.EQU	$-PAM3B_HEAD

PAM3B_REPT_ORG:	.EQU	$$
PAM3B_REPT:
	OUT	(C),E
	OUT	(C),D
	OUT	(C),B
PAM3B_REPT_LEN:	.EQU	$-PAM3B_REPT

PAM3B_TAIL_ORG:	.EQU	$$
PAM3B_TAIL:
				; このブロックは動的再配置されるので
				; このブロック"への"ジャンプは困難
				; "からの"ジャンプは可能。
	OUT	(C),E
				; attention bit
				; bit7=1, reload
				; must be JP
	JP	C,PAM3B_RELOAD		; jump=9 no=6

	RLCA				; 3
	OUT	(C),D
				; bit6=1, stat
				; must be JP
	JP	C,PAM3B_STAT		; jump=9 no=6

	RLCA				; 3
	OUT	(C),B
	WAIT3
				; bit5=0, normal
				; must be JP
	JP	NC,PAM3B_NORMAL		; jump=9 no=6
				; attention=001, reset
	JP	PAM_RESET
PAM3B_TAIL_LEN:	.EQU	$-PAM3B_TAIL


	.DEPHASE

; #### PAM1P

	.PHASE	0FE00H
PAM1P_HEAD_ORG:	.EQU	$$
PAM1P_HEAD:
PAM1P:
				; PAM1P
				; PAM1P は正確にはPCMだが
				; 動作方式はPAMに近いためこちら。
				; Polyphase PCM

				; 1 cycle = 87 clk
				; 6.144E6 / (87 + 3*n)

				; sampling freqs:
				; 0: 70621
				; 255: 7420

	LD	HL,PAM_BUF		;9

	LD	C,PSG_ADR
				; initial CH0
	LD	A,8
	OUT	(PSG_ADR),A
				; rotated next CH
	LD	B,9
	LD	DE,080AH

				; no STAT for first time
	JP	PAM1P_LOOP

PAM1P_RELOAD:
	LD	HL,PAM_BUF		;9

PAM1P_STAT:
#if USE_INTR
	OUT	(PAM_HOSTINTR),A		;10+2
#else
				; STAT_PTR モードでの遅れはしょうがない
	LD	(PAM_STAT_PTR),HL		;16+3
#endif

PAM1P_NORMAL:
				; rotate B,E,D
	LD	A,B			;4
	LD	B,E			;4
	LD	E,D			;4
	LD	D,A			;4
	OUT	(C),B			;10+2

PAM1P_LOOP:

	LD	A,(HL)			;6+3
	INC	HL			;4

	OUT	(PSG_DAT),A			;10+2

PAM1P_HEAD_LEN:	.EQU	$-PAM1P_HEAD

PAM1P_REPT_ORG:	.EQU	$$
PAM1P_REPT:
	WAIT3
PAM1P_REPT_LEN:	.EQU	$-PAM1P_REPT

PAM1P_TAIL_ORG:	.EQU	$$
PAM1P_TAIL:
				; このブロックは動的再配置されるので
				; このブロック"への"ジャンプは困難
				; "からの"ジャンプは可能。
	RLCA				;3
				; attention bit
				; bit7=1, reload
				; must be JP
	JP	C,PAM1P_RELOAD		; jump=9 no=6

	RLCA				; 3
				; bit6=1, stat
				; must be JP
	JP	C,PAM1P_STAT		; jump=9 no=6

	RLCA				; 3
	WAIT3
				; bit5=0, normal
				; must be JP
	JP	NC,PAM1P_NORMAL		; jump=9 no=6
				; attention=001, reset
	JP	PAM_RESET
PAM1P_TAIL_LEN:	.EQU	$-PAM1P_TAIL

				; cycle
				; 63 + 12 + 12 = 87

	.DEPHASE

PROG_ORG_LEN:	.EQU	$$-PROG_ORG

; #### interrupt vector
	.PHASE	0FFE0H
VECTOR_ORG:	.EQU	$$
VECTOR:

VEC_INT1:
	DW	INTR_IGN
VEC_INT2:
	DW	INTR_IGN
VEC_PRT0:
	DW	INTR_PRT0
VEC_PRT1:
	DW	INTR_IGN
VEC_DMAC0:
	DW	INTR_IGN
VEC_DMAC1:
	DW	INTR_IGN
VEC_SIO:
	DW	INTR_IGN
VEC_ASCI0:
	DW	INTR_ASCI0
VEC_ASCI1:
	DW	INTR_ASCI1
VEC_PT2IN:
	DW	INTR_IGN
VEC_PT2OUT:
	DW	INTR_IGN
VEC_PT2OVF:
	DW	INTR_IGN
			; 本当はここはベクタテーブルだが
			; 使われることはないので押し込む。
INTR_IGN:
	EI
	RETI

VECTOR_ORG_LEN:	.EQU	$$-VECTOR_ORG

	.DEPHASE
XPLX_FIRMWARE_LEN::	.EQU	$