aboutsummaryrefslogtreecommitdiff
path: root/doc/casm.html
blob: aebdf15dc6d5861725b8a99fbe0bfeda321fb5c5 (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
<html>
<head>

<title>CASM - A simple, portable multi-pass assembler</title>

<style>

h1
{
    padding-top: 20px;
    padding-bottom: 10px;
}

h2
{
    padding-top: 20px;
    padding-bottom: 10px;
}

h3
{
    padding-top: 20px;
    padding-bottom: 10px;
}

h4
{
    padding-top: 20px;
    padding-bottom: 10px;
}

body
{
    background-color: white;
    color: black;
    font-family: sans-serif;
    font-weight: normal;
    width: 80%;
    margin: auto;
}

.legalise
{
    font-variant: small-caps;
    font-size: 80%;
    width: 45%;
}

.codeblock
{
    font-family: monospace;
    border: 1px solid black;
    width: 100%;
    background-color: #e0e0ff;
    padding: 5px;
}

li
{
  padding-bottom: 3px;
}

table
{
    padding-top: 10px;
    padding-bottom: 10px;
}

tr
{
    padding-bottom: 5px;
}

thead
{
    font-weight: bold;
    background-color: black;
    color: white;
    white-space: nowrap;
    font-variant: small-caps;
}

td.head
{
    vertical-align: top;
    padding: 10px;
}

td.cmd
{
    vertical-align: top;
    padding: 5px;
    font-family: monospace;
    font-weight: bold;
    background-color: #e0e0e0;
    white-space: nowrap;
}

td.def
{
    padding-left: 10px;
    padding-top: 5px;
    padding-bottom: 5px;
    vertical-align: top;
}

td.alias
{
    padding-left: 10px;
    padding-top: 5px;
    padding-bottom: 5px;
    vertical-align: top;
    font-family: monospace;
    background-color: #e0e0e0;
    white-space: nowrap;
}

h1
{
    border-top: 2px solid black;
}

h2
{
    border-top: 1px solid #888888;
}

</style>

</head>

<body>

<div class="legalise">
<p><b>casm</b> is a simple, portable multi-pass assembler</p>

<p>Copyright (C) 2003-2024  Ian Cowburn</p>

<p>This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.</p>

<p>This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.</p>

<p>You should have received a copy of the GNU General Public License
along with this program.  If not, see 
<a href="http://www.gnu.org/licenses/gpl-3.0.html">
http://www.gnu.org/licenses/gpl-3.0.html)</a></p>
</div>

<h1>Index</h1>

<p>
<a href="#casm">CASM</a> - General usage
</p>
<p>
<a href="#z80">Z80</a> - Z80 processor support.
</p>
<p>
<a href="#6502">6502</a> - 6502 processor support.
</p>
<p>
<a href="#gbcpu">Gameboy Z80</a> - The Gameboy Z80 derivative processor.
</p>
<p>
<a href="#65c816">65c816</a> - 65c816 processor support, as used in the SNES.
</p>
<p>
<a href="#spc700">SPC700</a> - SPC700 processor support, as used in the SNES
sound co-processor.
</p>


<h1 id="casm">CASM</h1>

<h2>Usage</h2>

<div class="codeblock">
casm <i>file</i>
</div>

<p>Assembles <i>file</i>, and places the resulting code in a file called
<b>output</b> by default.</p>

<p>Note that switches aren't used by <b>casm</b>.  Instead options are
controlled by commands in the source <i>file</i>.</p>

<p>If you type the command without an argument, usage, version and license
info is displayed.</p>


<h2>Memory Layout</h2>

<p>There is 64K of RAM that the assembler will generate output into.  Extra 64K
banks of RAM can be added by using the <b>bank</b> or <b>org</b> directives.
Banks are numbered from zero upwards.</p>


<h2>Source Code Layout</h2>

The source files follow this basic format:

<pre class="codeblock">
    ; Comments
    ;
    label1: equ     0xffff

            org     $4000;
            
            db      "Hello, World\n",0

    main    jp      local_label     ; Comments

    .local_label
            inc     a

    another:
            inc     b
            jp      local_label     ; Actually jumps to the following
                                    ; local_label.

    .local_label
            ret
</pre>


<p>The source files follow the following rules:</p>

<ul>

<li>Any text past a semicolon (;) is discarded as a comment (except when part
of a string constant).</li>

<li>Labels must start in column zero (the left hand most column).</li>

  <ul>
  <li>It is an error to redefine a global label that already exists.</li>

  <li>If the label ends with a colon (:) then the colon is removed.</li>

  <li>If the label doesn't start with a period (.) then it is assumed a global
  label.</li>

  <li>If the label starts with a period (.) then it is assumed to be a local
  label.  Local labels are associated with the preceding global label.  If
  a local label has the same name as a global variable the local value will be
  used on expansion.  If a local label already exists with the same name it
  is an error.</li>

  <li>Any label can be followed by an <code>equ</code> directive, in which case
  the label is set to that value rather than the current program counter.</li>

  <li>Labels are case-insensitive.</li>

</ul>

<li>Directives and opcodes must appear further along the line (anywhere else
other than the left hand column where labels live basically).</li>

<li>Strings can either be quoted with single or double quotes; this allows you
to put the other quote type inside the string.</li>

</ul>


<h2>Recognised directives</h2>

<p>The following directives are also recognised with an optional period (.) in
front of them, and are case insensitive.  Directives can also be used to
control the output of a program listing, and the output of the assembly
itself.  These are documented in subsequent sections.</p>

<table>

<thead><tr><td class="head">Directive</td>
<td class="head">Description</td></tr></thead>
<tr>

<td class="cmd">
processor <i>cpu</i>
</td>

<td class="def">
Sets the processor type to <i>CPU</i>.  If omitted then Z80 is the default. 
Note that this can appear multiple times in the same file.  See the later
sections on processors to see what values are supported.</p>
</td></tr>

<tr><td class="cmd">
option <i>setting</i>, <i>value</i>
</td>

<td class="def">
Set options.  Options are defined later on, and each CPU and output driver
can also have its own options.  For options that support booleans
(on/off/true/false),the <i>setting</i> can be prefixed with a plus or minus
character to switch it on or off respectively.
</td></tr>

<tr><td class="cmd">
equ <i>value</i></code>
</td>
<td class="def">
Sets the top level label to <i>value</i>.  Note this requires a label on the
same line as this directive.
</td></tr>

<tr><td class="cmd">
org <i>value</i>[, <i>bank</i>]
</td>
<td class="def">
Sets the program counter (PC) to <i>value</i>.  The PC defaults to zero on
initialisation.  If the optional second argument is passed the current memory  
bank in use is set to <i>bank</i>.  Note that it also possible to set the
bank by passing a 24-bit address.  This was added for convenience when using the
65c816 processor.
</td></tr>

<tr><td class="cmd">
bank <i>value</i>
</td>
<td class="def">
The current memory bank in use is set to <i>value</i>.
</td></tr>

<tr><td class="cmd">
ds <i>value</i>[, <i>fill</i>]
</td>
<td class="def">
Skips the program counter on <i>value</i> bytes.  If the optional <i>fill</i> is
provided then the bytes are filled with <i>fill</i>, otherwise they are filled
with zero.
</td>

<tr><td class="cmd">
db <i>value</i>[, <i>value</i>]
</td>
<td class="def">
Writes bytes represented by <i>value</i> to the current PC.  The values can be
constants, expressions, labels or strings which are expanded to a list of byte
values for each character in the string.
</td></tr>

<tr><td class="cmd">
dw <i>value</i>[, <i>value</i>]
</td>
<td class="def">
Writes words (16-bit values) represented by <i>value</i> to the current PC.
The values can be constants, expressions, labels or strings.  Strings are
written as 16-bit versions of their byte values, i.e. the high byte will be zero
and the low byte the character code.
</td></tr>

<tr><td class="cmd">
align <i>value</i>[, <i>fill</i>]
</td>
<td class="def">
Align the PC so that (PC modulus <i>value</i>) is zero.  Will error if
<i>value</i> is less than 2 or greater that 32768.  No values are written to
the skipped bytes unless the optional <i>fill</i> is supplied.
</td></tr>

<tr><td class="cmd">
import <i>filename</i>[, labels|all[, offset]]</code>
</td>
<td class="def">
Includes a simple library file as generated by casm.  See the <a href="#libout">section on simple library files</a> for more details.  If the optional second
argument is set to labels then only the label values are imported, otherwise
both memory and labels are imported.  If the optional <i>offset</i> is given
then that offset is added onto the load address and defined labels.
</td></tr>

<tr><td class="cmd">
include <i>filename</i></code>
</td>
<td class="def">
Includes the source file <i>filename</i> as if it was text entered at the
current location.
</td></tr>

<tr><td class="cmd">
incbin <i>filename</i>
</td>
<td class="def">
Includes the binary file <i>filename</i> at the current PC, as if it was a
sequence of <code>db</code> directives with all the bytes from the file.
</td></tr>

<tr><td class="cmd">
alias <i>command</i>, <i>replacement</i>
</td>
<td class="def">
Creates an alias so that whenever the command <i>command</i> is found in the
source it is replaced with <i>replacement</i>.  The idea of this is to make it
easier to import sources that use unknown directives, e.g.

<pre class="codeblock">
    alias setaddr,org
    alias ldreg,ld

    cpu         z80

    setaddr     $8000   ; These two are
    org         $8000   ; equivalent.

    ld          a,(hl)  ; These two are
    ldreg       a,(hl)  ; equivalent.
</pre>
</td></tr>

<tr><td class="cmd">
nullcmd
</td>
<td class="def">
Simply does nothing.  It's only real use is as an alias if you wished to
strip a directive from a foreign source file.
</td></tr>

<tr><td class="cmd">
end
</td>
<td class="def">
Terminates input processing.  Anything past the directive will be ignored.
</td></tr>
</table>

<h3>Built-in Aliases</h3>

The following are built-in aliases for the above directives.

<table>

<thead><tr><td class="head">Command</td>
<td class="head">Built-in Alias</td></tr></thead>

<tr><td class="cmd">processor</td>
<td class="alias">
proc<br>
arch<br>
cpu<br>
</td></tr>

<tr><td class="cmd">equ</td>
<td class="alias">
eq
</td></tr>

<tr><td class="cmd">ds</td>
<td class="alias">
defs
</td></tr>

<tr><td class="cmd">db</td>
<td class="alias">
defb<br>
byte<br>
text<br>
</td></tr>

<tr><td class="cmd">dw</td>
<td class="alias">
defw<br>
word<br>
</td></tr>

</table>

<h2>Options</h2>

<p>The core of CASM supports the following options that can be set via the
<b>option</b> command.
</p>

<table>

<thead><tr><td class="head">Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option address24, &lt;on|off&gt;
</td>
<td class="def">
Controls whether addresses have the current bank set in the high word, e.g.

<pre class="codeblock">
        org     $8000
        bank    7

        option  +address24
label1: ; Label1 == $078000
        
        option  -address24
label2: ; Label2 == $8000
</pre>

Note that the name is rather misleading; bank numbers are allowed to be greater
than 256.

</td></tr>

</table>

<h2>Expressions</h2>

<p>In any of the directives above, where a value is defined, an expression can
be entered.</p>

<p>Assembly instructions will also permit these expressions to be used where
applicable.  As many opcodes use parenthesis to indicate addressing modes,
remember that {} brackets can be used to alter expression precedence.</p>

<pre class="codeblock">
    ld  a,{8+2}*2               ; On the Z80 loads A with the value 20
    ld  a,({8+2}*2)             ; On the Z80 loads A with the value stored at
                                ; address 20
</pre>

<p>Note that the expression is evaluated using a standard C int, and then cast
to the appropriate size.</p>

<p>The following formats for constant numbers are supported:</p>

<table>

<thead><tr><td class="head">Format (regular expression)</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
"." <i>or</i> '.'
</td>
<td class="def">
A single quoted character will be converted into the appropriate character
code.
</td></tr>

<tr><td class="cmd">
[1-9][0-9]*
</td>
<td class="def">
A decimal number, e.g. 42.
</td></tr>

<tr><td class="cmd">
0[0-7]*
</td>
<td class="def">
An octal number, e.g. 052.
</td></tr>

<tr><td class="cmd">
0x[0-9a-fA-f]+
</td>
<td class="def">
A hex number, e.g. 0x2a.
</td></tr>

<tr><td class="cmd">
[0-9a-fA-f]+h
</td>
<td class="def">
A hex number, e.g. 2ah.
</td></tr>

<tr><td class="cmd">
$[0-9a-fA-f]+
</td>
<td class="def">
A hex number, e.g. $2a.
</td></tr>

<tr><td class="cmd">
[01]+b
</td>
<td class="def">
A binary number, e.g. 00101010b
</td></tr>

<tr><td class="cmd">
%[01]+
</td>
<td class="def">
A binary number, e.g. %00101010
</td></tr>

<tr><td class="cmd">
[a-zA-Z_0-9]+
</td>
<td class="def">
A label, e.g. `main_loop`.
</td></tr>

</table>

The following operators are understood.  The order here is the order of
precedence.

<table>

<thead><tr><td class="head">Arithmetic Operators</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
{ }
</td>
<td class="def">
Brackets used to alter the order of precedence.  Note normal parenthesis
aren't used as the assembly language may make use of them.
</td></tr>

<tr><td class="cmd">
~ + -
</td>
<td class="def">
Bitwise NOT/unary plus/unary minus.
</td></tr>

<tr><td class="cmd">
&lt;&lt; &gt;&gt;
</td>
<td class="def">
Shift left/shift right.
</td></tr>

<tr><td class="cmd">
/ *
</td>
<td class="def">
Division/multiplication
</td></tr>

<tr><td class="cmd">
+ -
</td>
<td class="def">
Addition/subtraction.
</td></tr>

</table>

All the following have the same precedence, and so will be done left to right.

<table>

<thead><tr><td class="head">Comparison Operators</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
==
</td>
<td class="def">
Equality.  Returns 1 if the arguments are equal, otherwise zero.
</td></tr>

<tr><td class="cmd">
!=
</td>
<td class="def">
Inequality.  Returns 1 if the arguments are unequal, otherwise zero.
</td></tr>

<tr><td class="cmd">
&lt; &lt;= &gt; &gt;=
</td>
<td class="def">
Less than/less than or equal/greater than/greater than or equal.  Returns 1
if the expression is true, otherwise zero.
</td></tr>

</table>

All the following have the same precedence, and so will be done left to right.

<table>

<thead><tr><td class="head">Boolean Operators</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
&amp;&amp; &amp;
</td>
<td class="def">
Boolean/bitwise AND.  For boolean operation arguments, zero is FALSE,
otherwise TRUE.
</td></tr>

<tr><td class="cmd">
|| |
</td>
<td class="def">
Boolean/bitwise OR.
</td></tr>

<tr><td class="cmd">
^
</td>
<td class="def">
Bitwise XOR.
</td></tr>

</table>

<h2>Character Sets</h2>

<p>The assembler has built-in support for a few different character sets.
These can be set by using the options `charset` or `codepage`, i.e.</p>

<pre class="codeblock">
    option codepage, <i>format</i>
    option charset, <i>format</i>
</pre>

<p>The following values can be used for <i>format</i>.</p>

<table>

<thead><tr><td class="head">Format</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
ascii
</td>
<td class="def">
7-bit ASCII.  This is the default.
</td></tr>

<tr><td class="cmd">
spectrum
</td>
<td class="def">
The character codes as used on the Sinclair ZX Spectrum.
</td></tr>

<tr><td class="cmd">
cbm
</td>
<td class="def">
PETSCII as used on the Commodore Business Machine's range from the
PET to the C128.  See <a href="https://en.wikipedia.org/wiki/PETSCII">
https://en.wikipedia.org/wiki/PETSCII</a> more details.
</td></tr>

<tr><td class="cmd">
c64
</td>
<td class="def">
The screen codes as used on the Commodore 64.  Note these codes are different
to PETSCII and are the values to be poked into screen memory.  The lower case
characters are the low part of the code -- these will appear onscreen either
in upper or lower case depending on the shift mode.  The upper case characters
are either graphics characters or capital letters depending on the shift mode.
</td></tr>

<tr><td class="cmd">
zx81
</td>
<td class="def">
The character codes as used on the Sinclair ZX81.  Lower case
letters are encoded as normal upper case letters and upper case
letter will be encoded as inverse upper case letters.  In addition the following
characters that have no corresponding ZX81 character are mapped as:
<table>

<tr><td class="cmd">
#
</td>
<td class="def">
The British Pound sign.
</td></tr>

<tr><td class="cmd">
&apos;
</td>
<td class="def">
Inverse double quotes.
</td></tr>

<tr><td class="cmd">
\
</td>
<td class="def">
Inverse slash.
</td></tr>

<tr><td class="cmd">
!
</td>
<td class="def">
Inverse question mark.
</td></tr>

<tr><td class="cmd">
|
</td>
<td class="def">
Inverse space.
</td></tr>

<tr><td class="cmd">
~
</td>
<td class="def">
Inverse minus sign.
</td></tr>

<tr><td class="cmd">
{ }
</td>
<td class="def">
Inverse round brackets.
</td></tr>

<tr><td class="cmd">
`
</td>
<td class="def">
The newline character.  Note that the newline is actually a HALT opcode used to
terminate the line.
</td></tr>

</table>
</td></tr>

</table>

<p>e.g.</p>

<pre class="codeblock">
    option  +list
    option  +list-hex

    option  charset,ascii
    db      "Hello",'A'
    ; $48 $65 $6C $6C $6F $41

    option  charset,zx81
    db      "Hello",'A'
    ; $AD $2A $31 $31 $34 $A6

    option  codepage,cbm
    db      "Hello",'A'
    ; $48 $45 $4C $4C $4F $41

    option  codepage,spectrum
    db      "Hello",'A'
    ; $48 $65 $6C $6C $6F $41
</pre>


<h2>Macros</h2>

<p>
Macros can be defined in one of two ways; either parameterless or with named
parameters.  Macro names are case-insensitive.  In the parameterless mode the
special identifier '*' can be used to expand all arguments, which will be
separated with commas.
</p>

<p>
When expanded the macro will have an internally generated top-level label
assigned to it, so local variables will work inside the macro.
</p>

<p>e.g.</p>

<pre class="codeblock">
macro1: macro

        ld a,\1
        ld b,\2
        ld hl,data
        call \3
        jr dataend
.data
        defb \*
.dataend

        endm

macro2: macro char,junk,interface

        ld a,@char
        ld b,@junk
        call @interface

        endm
</pre>

<p>
Note that trying to expand and unknown/missing argument will be replaced with
an empty string.  Also the two argument reference styles can be mixed, though
obviously the @ form only makes sense in a parameterised macro, e.g.
</p>

<pre class="codeblock">
mac:    macro char,junk,interface

        ld a,@char
        ld b,\2
        call @interface

        endm
</pre>

<p>
The at symbol (@) used for parameter expansion in named argument macros can
be replaced by using the following option, e.g.
</p>

<pre class="codeblock">
        option  macro-arg-char,&
</pre>

<p>
Note that this is enforced when the macro is <u>used</u> not when it is
defined.
Also the character must not be quoted, as that will be parsed as a string
holding the character code of the character.
</p>


<h2>Output Format</h2>

By default the assembled code is written to a file called <b>output</b> as raw
binary.  The generated output can be controlled with the following options.

<table>

<thead><tr><td class="head">Output Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option output-file, <i>file</i>
</td>
<td class="def">
Send the output to <i>file</i>.  Defaults to <b>output</b>.
</td></tr>

<tr><td class="cmd">
option output-bank, <i>printf formatted filename</i>
</td>
<td class="def">
Send the output if multiple banks to use to <i>printf formatted filename</i>.
It defaults to <b>output.%u</b> and accepts just  one argument in the
formatting string of an unsigned integer.
If more or a different format specifier is used the behaviour of the assembler
will be undefined.  How this is used depends on the output driver.
</td></tr>

<tr><td class="cmd">
option output-type, <i>format</i>
</td>
<td class="def">
Controls the format of the output file.  The following are the
supported output formats:

<table>

<tr><td class="cmd">
<a href="#rawout">raw</a>
</td>
<td class="def">
A raw binary image.
</td></tr>

<tr><td class="cmd">
<a href="#specout">spectrum</a>
</td>
<td class="def">
A Spectrum emulator TAP file.
</td></tr>

<tr><td class="cmd">
<a href="#zx81out">zx81</a>
</td>
<td class="def">
A ZX81 emulator P file.
</td></tr>

<tr><td class="cmd">
<a href="#t64out">t64</a>
</td>
<td class="def">
A Commodore 64 T64 tape file.
</td></tr>

<tr><td class="cmd">
<a href="#gameboyout">gameboy</a>
</td>
<td class="def">
A Nintendo Gameboy ROM file.
</td></tr>

<tr><td class="cmd">
<a href="#snesout">snes</a>
</td>
<td class="def">
A SNES ROM file.
</td></tr>

<tr><td class="cmd">
<a href="#libout">lib</a>
</td>
<td class="def">
A simple library format that allows the importing of pre-built memory and
labels.
</td></tr>

<tr><td class="cmd">
<a href="#nesout">nes</a>
</td>
<td class="def">
A NES ROM file.
</td></tr>

<tr><td class="cmd">
<a href="#cpcout">cpc</a>
</td>
<td class="def">
An Amstrad CPC CDT file.
</td></tr>

<tr><td class="cmd">
<a href="#prgout">prg</a>
</td>
<td class="def">
A Commodore 64 or VIC-20 PRG file.
</td></tr>

<tr><td class="cmd">
<a href="#hexout">hex</a>
</td>
<td class="def">
An Intel HEX format file.
</td></tr>

</table>

</td></tr>

</table>

The output formats are described in detail in the following sections.


<h3 id="rawout">RAW Output Format</h3>

In this mode the file is created covering the block of memory that the assembly
touched.  If memory banks have been used then the <b>output-bank</b> setting is
used to generate the output filename.


<h3 id="specout">Spectrum TAP Output Format</h3>
<p>
Generates a Spectrum TAP file for an emulator.  A TAP file is a simple binary
file holding the bytes that the real Spectrum would have written to a tape.
</p>

<p>The TAP file will be given the same name as the output filename, and the
internal code block will also be given the same name, unless memory banks
have been used, in which case each code file in the TAP file will use the
<b>output-bank</b> setting to generate the filename for each block.
</p>

<p>If the options are set the TAP file will contain a BASIC loader for the
machine code.</p>

<p>
Remember that TAP files can be concatenated, so the output could be appended to
another TAP file.
</p>

<h4>Spectrum TAP Output Format options</h4>

<p>The Spectrum TAP output driver supports the following settings that can be
set via an <b>option</b> command.
</p>

<table>

<thead><tr><td class="head">Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option spectrum-loader, &lt;on|off&gt;
</td>
<td class="def">
Whether the TAP file should contain a BASIC loader to load and run the machine
code.
</td></tr>

<tr><td class="cmd">
option spectrum-start, <i>address</i>
</td>
<td class="def">
The start address for the generated BASIC loader.  If left undefined defaults
to the start of written memory.
</td></tr>


</table>



<h3 id="zx81out">ZX81 .P Output Format</h3>

<p>
Generates a P-file for an emulator.  A ZX81 .P file is simply a dump of memory
from the system variables onwards.
</p>

<p>This format does not support memory blocks (the .P file is not a container
format) and so will only output anything in the first bank used, and using the 
<b>output-file</b> for the filename.
</p>

<p>The output file will be created as a BASIC program, containing a REM
statement holding the machine code, and a command to execute the code.  As
such the output will fail if the code in Bank 0 does not start at address 16514.
Your code must also support being executed from this address.
</p>

<p>Another important thing to note is about the display file.  In the ZX81
memory map the DFILE can move around depending on the size of the program.
The output driver will create a display file for you.  The easiest way to
reference this is to read the DFILE system variable when your program starts.
</p>

<p>Alternatively you can just as easily set up your own display file once
your program starts if you have special requirements, e.g. a display file for
pseudo hi-res.
</p>


<h4>ZX81 .P Output Format options</h4>

<p>The ZX81 output driver supports the following settings that can be set via
an <b>option</b> command.
</p>

<table>

<thead><tr><td class="head">Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option zx81-margin, &lt;pal|ntsc&gt;
</td>
<td class="def">
Sets the MARGIN system variable appropriately either for the PAL or NTSC
TV system.  Defaults to PAL.
</td></tr>

<tr><td class="cmd">
option zx81-autorun, &lt;on|off&gt;
</td>
<td class="def">
Whether the ZX81 should auto run the machine code.  Defaults to on.
</td></tr>

<tr><td class="cmd">
option zx81-collapse-dfile, &lt;on|off&gt;
</td>
<td class="def">
Whether the display file should be generated collapsed (e.g. for 1K mode).
Defaults to off.
</td></tr>


</table>


<h3 id="t64out">C64 T64 Tape Output Format</h3>
<p>
Generates a T64 tape file for an emulator.
</p>

<p>The tape file will be given the same name as the output filename, and the
internal code block will also be given the same name, unless memory banks
have been used, in which case then each entry in the tape file will use the
<b>output-bank</b> setting to generate the filename for each entry.
</p>

<p>The first (or only) bank will have a small BASIC program inserted as part of
the generated file.  For this reason the first bank should start near the BASIC
area (0x820 should be a safe place to start) unless you have a great
desire for a tape full of zero bytes. This BASIC will simply hold a SYS command
to start the machine code, e.g.
</p>

<pre class="codeblock">
10 SYS 2080
</pre>

<p>Any remaining blocks will be stored as-is without any basic loader.</p>

<h4>C64 T64 Tape Output Format options</h4>

<p>The C64 T64 output driver supports the following settings that can be
set via an <b>option</b> command.
</p>

<table>

<thead><tr><td class="head">Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option t64-start, <i>address</i>
</td>
<td class="def">
The start address for the generated BASIC loader.  If left undefined defaults
to the start of written memory in the first bank.
</td></tr>

</table>

<h3 id="gameboyout">Nintendo Gameboy ROM File Output Format</h3>

<p>
Generates a ROM file for a Gameboy emulator or hardware.  Note that large ROM
sizes have not been extensively checked and verified.
</p>

<p>If a single bank was used during the assembly then a simple 32K ROM is
assumed, and an error will be shown if the addresses used fall outside the range
0x150 to 0x7fff. </p>

<p>Similarly if multiple banks are used then it is assumed that the first bank
is only used in the range 0x150 to 0x3fff, and subsequent banks in the range
0x4000 to 0x7fff.  This is to fit in with the method the Gameboy uses to page
memory banks into the upper 16K portion of the normal 32K ROM address space.
</p>

<p>By default the output driver will try and fill in the ROM size and type in
the header properly, but these can be overridden using settings.</p>

<h4>Gameboy ROM Output Format options</h4>

<p>The Gameboy output driver supports the following settings that can be set via
an <b>option</b> command.
</p>

<table>

<thead><tr><td class="head">Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option gameboy-colour, &lt;on|off&gt;
</td>
<td class="def">
Whether this is a Gameboy Colour cartridge.  Defaults to <i>off</i>.
Note that <b>gameboy-color</b> can be used as a different spelling for this
setting.
</td></tr>

<tr><td class="cmd">
option gameboy-super, &lt;on|off&gt;
</td>
<td class="def">
Whether this is a Gameboy Super extended cartridge.  Defaults to <i>off</i>.
</td></tr>

<tr><td class="cmd">
option gameboy-cart-type, <i>number</i>
</td>
<td class="def">
Specifies the cartridge type.  Defaults to -1, which means the output driver
will try and pick the appropriate type.
</td></tr>

<tr><td class="cmd">
option gameboy-irq, <i>irq</i>, <i>address</i>
</td>
<td class="def">
Specifies an address where an IRQ routine is stored, and requests that the
IRQ be transferred to that address when it happens.  The IRQ routine must end
with a <b>reti</b> opcode.<br><br>
<i>irq</i> can be either <b>vbl</b>, <b>lcd</b>, <b>timer</b>, <b>serial</b>
or <b>joypad</b>.  If left at the default value of -1 then an IRQ handler is
installed with just a <b>reti</b> instruction.
</td></tr>

</table>

<h3 id="snesout">SNES ROM File Output Format</h3>

<p>
Generates a ROM file for a SNES emulator or hardware.  Note that large ROM
sizes have not been extensively checked and verified.
</p>

<p>If a single bank was used during the assembly then a simple 32K ROM is
assumed, and an error will be shown if the addresses used fall outside the range
0x150 to 0x7fff. </p>

<p>Similarly if multiple banks are used then it is assumed that the first bank
is only used in the range 0x150 to 0x3fff, and subsequent banks in the range
0x4000 to 0x7fff.  This is to fit in with the method the Gameboy uses to page
memory banks into the upper 16K portion of the normal 32K ROM address space.
</p>

<p>By default the output driver will try and fill in the ROM size and type in
the header properly, but these can be overridden using settings.</p>

<h4>SNES ROM Output Format options</h4>

<p>The SNES output driver supports the following settings that can be set via
an <b>option</b> command.
</p>

<table>

<thead><tr><td class="head">Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option snes-name, <i>name</i>
</td>
<td class="def">
Sets the name of the cartridge.  Defaults to "NONAME" if not set.
</td></tr>

<tr><td class="cmd">
option snes-rom-type, &lt;lorom|hirom|lorom-fast|hirom-fast&gt;
</td>
<td class="def">
Defines the memory mapping used for this cartridge.  A detailed explanation
is beyond this document, but note that when lorom is used then just the upper
32K of each bank is used to generate the ROM.  This is to make life easier,
as the real physical mapping would make the lower 32K of a ROM bank 
mapped into the upper 32K address space.  This way no hoops have to be jumped
through.
</td></tr>

<tr><td class="cmd">
option snes-irq, <i>irq</i>, <i>address</i>
</td>
<td class="def">
Specifies an address where an IRQ routine is stored, and sets the appropriate
vectors in emulation and native mode. <i>irq</i> can either by <b>vbl</b> for
the vertical blanking interrupt and <b>irq</b> which is used for the scan-line
interrupt.  Note that an error is generated if a <b>vbl</b> routine is not
supplied.
</td></tr>

<tr><td class="cmd">
option snes-ram-size, <i>size</i>
</td>
<td class="def">
Sets the RAM size in the ROM header.
</td></tr>

<tr><td class="cmd">
option snes-rom-size, <i>size</i>
</td>
<td class="def">
Sets the ROM size in the ROM header, overriding the calculated value the output
driver would generate.
</td></tr>

</table>

<h3 id="libout">Simple Library Output Format</h3>

<p>This mode produces a simple library file that can be imported using the
<b>import</b> command.  This is of use if you wish to split a large project
into smaller assembly jobs, for example, to stop you needing to assemble large
chunks of graphics data every time.  The library contains all the memory from
the assembled file and records the values of all global labels.  Note that
private macro variables and local labels aren't saved, but they'd be useless
anyway.</p>

<p>.e.g.</p>

<b>Makefile</b>
<pre class="codeblock">
game.sfc: game.lib gfx.lib
        casm link.asm

game.lib: game.asm
        casm game.asm

gfx.lib: gfx.asm
        casm gfx.asm
</pre>

<b>link.asm</b>
<pre class="codeblock">
        processor 65c816

        option  output-file,"game.sfc"
        option  output-format,snes

        ; These labels actually come from game.asm
        ;
        option  snes-irq,vbl,vbl_handler
        option  snes-irq,irq,irq_handler

        import  "game.lib"
        import  "gfx.lib"
</pre>

<b>game.asm</b>
<pre class="codeblock">
        processor 65c816

        org     $8000
        bank    0

        ;
        ; Lots of code....
        ;
</pre>


<b>gfx.asm</b>
<pre class="codeblock">
        processor 65c816

        org     $8000
        bank    1

        ;
        ; Lots of gfx data....
        ;
</pre>


<h3 id="nesout">NES ROM File Output Format</h3>

<p>
Generates a ROM file for a NES emulator or hardware.  Note that large ROM
sizes have not been extensively checked and verified.
</p>

<p>It searches for banks with code in the range 0x8000 to 0xffff, or just in the
range 0xc000 to 0xffff if there is just one ROM code bank. Video ROM (VROM)
banks are identified by just using addresses up to 0x1fff.</p>

<p>The output driver will output the appropriate number of ROM and VROM chunks
to the file depending on these, and will error if there are no ROM banks
or banks using illegal addresses.</p>

<h4>NES ROM Output Format options</h4>

<p>The NES output driver supports the following settings that can be set via
an <b>option</b> command.
</p>

<table>

<thead><tr><td class="head">Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option nes-vector, <i>vector</i>, <i>address</i>
</td>
<td class="def">
Specifies an address where an IRQ routine is stored, and sets the appropriate
vectors in the appropriate place in memory. <i>irq</i> can either by
<b>reset</b>, <b>nmi</b> or <b>brk</b>.<br><br>

<b>reset</b> is for the reset vector (i.e. where the ROM runs from).  If
undefined a warning is displayed and the vector will automatically be set to
either 0x8000 or 0xc000 according to the ROM size.<br><br>

<b>nmi</b> is the NMI vector used for the vertical blanking interrupt.  If
undefined a warning is issued.<br><br>

<b>brk</b> is the vector used for software interrupts from the brk command.
This can be left undefined if not needed.
</td></tr>

<tr><td class="cmd">
option nes-mapper, <i>number</i>
</td>
<td class="def">
Sets the mapper type in the output file header.  If undefined left at zero,
which is the correct one if you have 16 or 32K of code ROM and 8KB of video
ROM.
</td></tr>

<tr><td class="cmd">
option nes-tv-format, pal|ntsc
</td>
<td class="def">
Defines the TV format stored in the ROM header, defaulting to <b>pal</b>.
</td></tr>

<tr><td class="cmd">
option nes-mirror, horizontal|vertical|vram
</td>
<td class="def">
Defines whether nametables are mirrored horizontally, vertically or not at
all respectively.  Defaults to <b>horizontal</b>.
</td></tr>

<tr><td class="cmd">
option nes-battery-ram, on|off
</td>
<td class="def">
Defines whether there is battery backed RAM in the cartridge.  Defaults to 
<b>off</b>.
</td></tr>

</table>


<h3 id="cpcout">Amstrad CPC CDT Output Format</h3>
<p>
Generates an Amstrad CPC TAP file for an emulator.  
The file can also be run into a real CPC using TZX to wav file converters.
</p>

<p>The CDT file will be given the same name as the output filename, 
unless memory banks have been used, in which case each code file in the CDT
file will use the <b>output-bank</b> setting to generate the filename for
each block.
</p>

<h4>Amstrad CPC CDT Output Format options</h4>

<p>The Amstrad CPC CDT output driver supports the following settings that can be
set via an <b>option</b> command.
</p>

<table>

<thead><tr><td class="head">Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option cpc-start, <i>address</i>
</td>
<td class="def">
The start address for the tape file.  If left undefined defaults to the start
of written memory.
</td></tr>

<tr><td class="cmd">
option cpc-loader, &lt;on|off&gt;
</td>
<td class="def">
Whether the CDT file should contain a BASIC loader to load and run the machine
code.
</td></tr>


</table>


<h3 id="prgout">C64/VIC-20 PRG Output Format</h3>
<p>
Generates a PRG file for an emulator or real hardware.
</p>

<p>The tape file will be given the same name as the output filename, and the
internal code block will also be given the same name, unless memory banks
have been used, in which case then each entry in the tape file will use the
<b>output-bank</b> setting to generate the filename for each entry.
</p>

<p>All banks will have a small BASIC program inserted as part of
the generated file.  For this reason the bank should start near the BASIC
area (0x820 should be a safe place to start for the C64) unless you have a great
desire for a PRG full of zero bytes. This BASIC will simply hold a SYS command
to start the machine code, e.g.
</p>

<pre class="codeblock">
10 SYS2080
</pre>

<h4>C64/VIC-20 PRG Output Format options</h4>

<p>The PRG output driver supports the following settings that can be
set via an <b>option</b> command.
</p>

<table>

<thead><tr><td class="head">Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option prg-start, <i>address</i>
</td>
<td class="def">
The start address for the generated BASIC loader.  If left undefined defaults
to the start of written memory in the bank.
</td></tr>

<tr><td class="cmd">
option prg-system, &lt;c64|vic20|vic20+8k&gt;
</td>
<td class="def">
The system to generate the PRG file for, either a Commodore 64 (<b>c64</b>),
VIC-20 either unexpanded or with 3K expansion (<b>vic20</b>) or a VIC-20 with
an 8K or 16K expansion (<b>vic20+8k</b>).
</td></tr>

</table>


<h3 id="hexout">Intel HEX Output Format</h3>
<p>
Generates a Intel HEX file for an emulator or real hardware.  This format is
used by various tools and programmers.
</p>

<h4>Intel HEX Output Format options</h4>

<p>The Intel HEX output driver supports the following settings that can be
set via an <b>option</b> command.
</p>

<table>

<thead><tr><td class="head">Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option hex-null, <i>value</i>
</td>
<td class="def">
Sets the value that the output handler considers an empty byte that doesn't 
need to be output.  Defaults to zero.
</td></tr>

</table>


<h2>Listing</h2>

<p>
By default no output listing is generated.  This can be controlled by the
following options.
</p>

<table>

<thead><tr><td class="head">Listing Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option list, &lt;on|off&gt;
</td>
<td class="def">
Enables or disables listing.  The listing will go to stdout by default. 
Defaults to <i>off</i>.
</td></tr>

<tr><td class="cmd">
option list-file, <i>filename</i>
</td>
<td class="def">
Sends the listing to <i>filename</i>.  Note this should appear before enabling
the listing.
</td></tr>

<tr><td class="cmd">
option list-pc, &lt;on|off&gt;
</td>
<td class="def">
Control the output of the current PC in the as a comment preceding the
line (so that a listing could be reassembled with no editing).  Defaults
to <i>off</i>.
</td></tr>

<tr><td class="cmd">
option list-hex, &lt;on|off&gt;
</td>
<td class="def">
Control the output of the bytes generated by the source line in hex.
Defaults to <i>off</i>.  If <i>on</i> then the hex is output in a comment
preceding the line (possibly with the PC above), so that a listing is still
valid as input to the assembler.
</td></tr>

<tr><td class="cmd">
option list-labels, &lt;on|off|all&gt;
</td>
<td class="def">
Controls the listing of labels, either:

<table>
<tr><td class="cmd">
off
</td>
<td class="def">
The default; don't list anything.
</td></tr>

<tr><td class="cmd">
on
</td>
<td class="def">
List labels at the end of the listing.  The labels are output commented so that
the list could be used as input.
</td></tr>

<tr><td class="cmd">
all
</td>
<td class="def">
List all labels, including internally generated private labels for macros.
</td></tr>
</table>

</td></tr>

<tr><td class="cmd">
option list-macros, &lt;off|exec|dump|all&gt;
</td>
<td class="def">
Controls the listing of macro invocations, either:

<table>
<tr><td class="cmd">
off
</td>
<td class="def">
The default; don't list anything.
</td></tr>

<tr><td class="cmd">
exec
</td>
<td class="def">
List invocations of macros.
</td></tr>

<tr><td class="cmd">
dump
</td>
<td class="def">
Produce a list of macro definitions at the end of the listing.
</td></tr>

<tr><td class="cmd">
all
</td>
<td class="def">
Combine <i>exec</i> and <i>dump</i>.
</td></tr>
</table>
</td></tr>

<tr><td class="cmd">
option list-rm-blanks, &lt;on|off&gt;
</td>
<td class="def">
Defaults to <i>on</i>.  This option causes multiple blank lines to be collapsed
down to a single blank line in the listing.
</td></tr>
</table>


<h1 id="z80">Z80 CPU</h1>

<h2>Using the Z80</h2>

The Z80 processor can be selected by passing <b>z80</b> to the processor
directive, i.e.

<pre class="codeblock">
        processor z80
</pre>

<h2>Opcodes</h2>

<p>
The Z80 assembler uses the standard Zilog opcodes, and supports
undocumented instructions.
</p>

<p>
For instructions were the Accumulator can be assumed it can be omitted, and
EOR can be used the same as XOR:
</p>

<pre class="codeblock">
        xor     a,a         ; These are equivalent
        xor     a
        eor     a,a

        and     a,b         ; These are equivalent
        and     b
</pre>

<p>
For exchange opcodes with parameters the parameters can be reversed from their
official form:
</p>

<pre class="codeblock">
        ; The official forms
        ;
        ex      de,hl
        ex      af,af'
        ex      (sp),hl
        ex      (sp),ix
        ex      (sp),iy

        ; Also supported
        ;
        ex      hl,de
        ex      af',af
        ex      hl,(sp)
        ex      ix,(sp)
        ex      iy,(sp)
</pre>

<p>
Where the high/low register parts of the IX and IY registers are to be used,
simply use ixl, iyl, ixh and iyh.  Note that the assembler will accept
illegal pairings involving H and L, but these will be warned about:
</p>

<pre class="codeblock">
        ld  ixh,$e5
        ld  iyl,iyl

        ld  ixh,l           ; This will be turned into "ld ixh,ixl" and a
                            ; warning will be issued.

        ld  iyh,ixl         ; This will generate an error as the index registers
                            ; have been mixed.
</pre>

<p>
For the hidden bit manipulations that also can copied to a register, these can
be represented by adding the destination register as an extra parameter, e.g.
</p>

<pre class="codeblock">
        srl (iy-1),d
        set 3,(iy-1),a
        res 4,(iy-1),b
</pre>

<p>
For the hidden IN instruction using the flag register the following are all
equivalent:
</p>

<pre class="codeblock">
        in  (c)
        in  f,(c)
</pre>

<p>
For the hidden OUT instruction using the flag register, $00 or $ff depending
on where you're reading, the following are all equivalent, where <i>value</i>
can be any value at all:
</p>

<pre class="codeblock">
        out (c)
        out (c),f
        out (c),<i>value</i>
</pre>


<h2>Options</h2>

The Z80 assembler has no options.

<h1 id="6502">6502 CPU</h1>

<h2>Using the 6502</h2>

The 6502 processor can be selected by passing <b>6502</b> to the processor
directive, i.e.

<pre class="codeblock">
        processor 6502
</pre>

<h2>Opcodes</h2>

The 6502 assembler uses the standard Motorola opcodes.  Undocumented opcodes are
supported and use the opcodes as described at
<a href="https://www.masswerk.at/nowgobang/2021/6502-illegal-opcodes">
www.masswerk.at/nowgobang/2021/6502-illegal-opcodes</a>.


<h2>Options</h2>

The 6502 assembler has the following options.

<table>

<thead><tr><td class="head">6502 Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option zero-page, &lt;on|off|auto&gt;
</td>
<td class="def">
Controls the assumptions made regarding Zero Page address.  Defaults to
<i>auto</i>, and can be the following values:

<table>
<tr><td class="cmd">
off
</td>
<td class="def">
All addresses are assumed to be not on the Zero Page, regardless
of the value used.
</td></tr>

<tr><td class="cmd">
on
</td>
<td class="def">
Assumes all addresses are in the Zero Page, raising an error if any address is
not in the Zero Page.
</td></tr>

<tr><td class="cmd">
auto
</td>
<td class="def">
Treats addresses less than 256 as being in the Zero Page automatically.  This
mode also makes the assembler perform an extra pass to guard against the 
possibility of the calculation being fooled.
</td></tr>
</table>

e.g.

<pre class="codeblock">
        cpu     6502
        org     $8000

        lda     $0000,x     ; Produces $bd $00 $00
        option  +zero-page
        lda     $0000,x     ; Produces $b5 $00
        lda     $1234,x     ; Produces an error

        option  zero-page,auto
        lda     $00,x       ; Produces $b5 $00
        lda     $8000,x     ; Produces $bd $00 $80
</pre>

</td></tr>
</table>

<h1 id="gbcpu">Gameboy Z80 derivative CPU</h1>

<h2>Using the Gameboy Z80 derivative</h2>

The Gameboy Z80 derivative processor can be selected by passing <b>gameboy</b>
to the processor directive, i.e.

<pre class="codeblock">
        processor gameboy
</pre>

<h2>Opcodes</h2>

<p>
The Gameboy assembler uses the standard Z80 opcodes where applicable.
Note that the Gameboy processor has a reduced number of opcodes, flags
and no index registers, though it has some additional instructions and
addressing modes.
</p>

<p>
For instructions were the Accumulator can be assumed it can be omitted, and
EOR can be used the same as XOR:
</p>

<pre class="codeblock">
        xor     a,a         ; These are equivalent
        xor     a
        eor     a,a

        and     a,b         ; These are equivalent
        and     b
</pre>

<p>
The Gameboy CPU has a special addressing mode used for one opcode, where the
referenced address is stored as a single byte, and used as an offset into the
top page (0xff00).  This can be either triggered by using the special opcode, or
will automatically used whenever an address is accessed in the range 0xff00 to
0xffff:
</p>

<pre class="codeblock">
        ; These all will use the special addressing mode opcode, accessing
        ; memory location $ff34
        ;
label   equ     $ff34

        ldh     a,($34)
        ldh     a,($ff34)
        ld      a,($ff34)
        ld      a,(label)

        ld      (label),a
        ld      ($ff34),a
        ldh     ($34),a
        ldh     ($ff34),a
</pre>

<p>
The Gameboy CPU also supports incrementing or decrementing the HL register when
it is used as an address:
</p>

<pre class="codeblock">
        ; All these decrement HL after the value has been used.
        ;
        ld      a,(hl-)
        ld      a,(hld)
        ldd     a,(hl)
        ld      (hl-),a
        ld      (hld),a
        ldd     (hl),a

        ; All these increment HL after the value has been used.
        ;
        ld      a,(hl+)
        ld      a,(hli)
        ldi     a,(hl)
        ld      (hl+),a
        ld      (hli),a
        ldi     (hl),a
</pre>

<p>In addition the Gameboy CPU supports these extra instructions over the Z80:
</p>

<pre class="codeblock">
        ; Actually loads using the address $ff00 + C
        ;
        ld      a,(c)
        ld      (c),a

        ; Put the Gameboy into a low-power mode till a control is pressed.
        ; Note it is accepted practice to put a NOP afterwards.  This may be
        ; due to the stop replacing DJNZ, which may still be wired to expect
        ; an argument.  That is just a wild guess though.
        ;
        stop
        nop

        ; Swaps the low/high nibbles of the register
        ;
        swap    a
        swap    b
        swap    c
        swap    d
        swap    e
        swap    h
        swap    l
        swap    (hl)
</pre>


<h2>Options</h2>

The Gameboy CPU assembler has no options.


<h1 id="65c816">65c816 CPU</h1>

<h2>Using the 65c816 processor</h2>

The 65c816 processor can be selected by passing <b>65c816</b>
to the processor directive, i.e.

<pre class="codeblock">
        processor 65c816
</pre>

<h2>Opcodes</h2>

The 65c816 assembler uses the standard opcodes.  As an addition some implicit
opcodes are generally followed by a byte that can be used, e.g. in the case
of a BRK interrupt.  In these cases the following byte is automatically set to
zero unless a argument is supplied (in any addressing mode) and this is written
out as the following byte.  e.g.

<pre class="codeblock">
    processor 65c618

    brk          ; Produces the bytes 0x00 0x00
    brk $ff      ; Produces the bytes 0x00 0xff

    wdm          ; Produces the bytes 0x42 0x00
    wdm $80      ; Produces the bytes 0x42 0x80
</pre>


<h2>Additional Directives</h2>

The 65c618 assembler has some addition directives.  As with the built-in
directives, these can be preceded by an optional period (.) character.

<table>

<thead><tr><td class="head">Directive</td>
<td class="head">Description</td></tr></thead>
<tr>

<td class="cmd">
m8 
</td>

<td class="def">
Sets the assembler to produce 8-bit immediate values for the Accumulator in the
appropriate instructions.
</td></tr>

<td class="cmd">
m16 
</td>

<td class="def">
Sets the assembler to produce 16-bit immediate values for the Accumulator in the
appropriate instructions.
</td></tr>

<td class="cmd">
x8 
</td>

<td class="def">
Sets the assembler to produce 8-bit immediate values for the index registers X
and Y in the appropriate instructions.
</td></tr>

<td class="cmd">
x16 
</td>

<td class="def">
Sets the assembler to produce 16-bit immediate values for the index registers X
and Y in the appropriate instructions.
</td></tr>

<tr><td class="cmd">
mx <i>accumulator-size</i>, <i>index-size</i>
</td>

<td class="def">
Sets the Accumulator and index register sizes for immediate values to either
8 or 16 bits.
</td></tr>

</table>

<h2>Options</h2>

The 65c618 assembler has the following options.

<table>

<thead><tr><td class="head">65c618 Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option a16, &lt;on|off&gt;
</td>
<td class="def">
Switches on or off the generation of 16-bit absolute values for the Accumulator.
Note that this can also be done using the directives described in the previous
section.
</td></tr>

<tr><td class="cmd">
option i16, &lt;on|off&gt;
</td>
<td class="def">
Switches on or off the generation of 16-bit absolute values for the index
registers X and Y.
Note that this can also be done using the directives described in the previous
section.
</td></tr>

</table>

<h1 id="spc700">SPC700 CPU</h1>

<h2>Using the SPC700</h2>

The SPC700 processor can be selected by passing <b>spc700</b> to the processor
directive, i.e.

<pre class="codeblock">
        processor spc700
</pre>

<h2>Opcodes</h2>

I could find no official documentation on The SPC700, so the assembler uses what
seems to be the accepted opcode format.


<h2>Options</h2>

The SPC700 assembler has the following options.

<table>

<thead><tr><td class="head">SPC700 Option</td>
<td class="head">Description</td></tr></thead>

<tr><td class="cmd">
option direct-page, &lt;on|off|auto&gt;
</td>
<td class="def">
Controls the assumptions made regarding Direct Page address.  Defaults to
<i>auto</i>, and can be the following values:

<table>
<tr><td class="cmd">
off
</td>
<td class="def">
All addresses are assumed to be not on the Direct Page, regardless of the value
used.
</td></tr>

<tr><td class="cmd">
on
</td>
<td class="def">
Assumes all addresses are in the Direct Page, raising an error if any address is
not in the Direct Page.
</td></tr>

<tr><td class="cmd">
auto
</td>
<td class="def">
Treats addresses less than 256 as being in the Direct Page automatically.  This
mode also makes the assembler perform an extra pass to guard against the 
possibility of the calculation being fooled.
</td></tr>
</table>

</td></tr>
</table>


<!--  vim: ai sw=4 ts=8 expandtab spell
-->
</body>
</html>