~rabbits/orca-toy

ref: a9a2128f0d991dd09e9d8c6dc08f05ee700727ca orca-toy/src/orca.tal -rw-r--r-- 48.3 KiB
a9a2128fneauoire Extended grid size 3 months ago
                                                                                
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
( Orca )

%CHAR-NULL  { #00 } %CHAR-LINE  { #0a }
%CHAR-HASH  { #23 } %CHAR-BANG  { #2a }
%CHAR-DOT   { #2e } %CHAR-SLASH { #2f }
%CHAR-COLON { #3a } %CHAR-EQUAL { #3d }
%CHAR-SEMI  { #3b }

%LOCKED-TYPE   { #01 } %PORTEL-TYPE   { #02 }
%OPERATOR-TYPE { #03 } %PORTER-TYPE   { #04 }
%OUTPUT-TYPE   { #05 } %IO-TYPE       { #07 }

( helpers )

|00 @System     &vector $2 &pad      $6 &r      $2 &g     $2 &b      $2
|10 @Console    &vector $2 &read     $1 &pad    $5 &write $1
|20 @Screen     &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1
|30 @Audio0     &vector $2 &position $2 &output $1 &pad   $3 &adsr   $2 &length $2 &addr  $2 &volume $1 &pitch $1
|40 @Audio1     &vector $2 &position $2 &output $1 &pad   $3 &adsr   $2 &length $2 &addr  $2 &volume $1 &pitch $1
|50 @Audio2     &vector $2 &position $2 &output $1 &pad   $3 &adsr   $2 &length $2 &addr  $2 &volume $1 &pitch $1
|60 @Audio3     &vector $2 &position $2 &output $1 &pad   $3 &adsr   $2 &length $2 &addr  $2 &volume $1 &pitch $1
|80 @Controller &vector $2 &button   $1 &key    $1
|90 @Mouse      &vector $2 &x        $2 &y      $2 &state $1 &chord  $1
|a0 @File       &vector $2 &success  $2 &stat   $2 &delete $1 &append $1 &name  $2 &length $2 &read $2 &write $2
|c0 @DateTime   &year   $2 &month    $1 &day    $1 &hour  $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1

|0000

@dpad $1 &last $1
@timer &beat $1 &speed $1 &playing $1 &frame $1 &frame-lb $1
@state &timer $1 &changed $1
@guide $1
@filepath $40
@grid &x1 $2 &y1 $2 &x2 $2 &y2 $2 &size &width $1 &height $1 &length $2
@selection &from &x1 $1 &y1 $1 &to &x2 $1 &y2 $1
@cursor &x $2 &y $2
@toolbar &x1 $2 &y1 $2 &x2 $2 &y2 $2
@head &x $1 &y $1 &addr $2
@variables $24
@signal &midi $1
@voices $20

|0100 ( -> )

	( theme )
	#0f38 .System/r DEO2
	#0fc8 .System/g DEO2
	#0f98 .System/b DEO2

	( drawing mode )
	#15 .Screen/auto DEO

	( size )
	#0320 .Screen/width DEO2
	#01c0 .Screen/height DEO2

	( synths )
	#dd .Audio0/volume DEO  #0118 .Audio0/adsr DEO2 #0100 .Audio0/length DEO2
	#ef .Audio1/volume DEO  #0334 .Audio1/adsr DEO2 #0100 .Audio1/length DEO2
	#fe .Audio2/volume DEO  #1414 .Audio2/adsr DEO2 #0100 .Audio2/length DEO2
	#dd .Audio3/volume DEO  #222c .Audio3/adsr DEO2 #0100 .Audio3/length DEO2

	;sin-pcm .Audio0/addr DEO2
	;tri-pcm .Audio1/addr DEO2
	;saw-pcm .Audio2/addr DEO2
	;sqr-pcm .Audio3/addr DEO2

	( set grid size )
	.Screen/width DEI2
		DUP2 #03 SFT2 NIP #03 SUB .grid/width STZ
		DUP2 #01 SFT2 .grid/width LDZ #01 SFT INC #00 SWP #30 SFT2 SUB2 #0004 ADD2 .grid/x1 STZ2
		#01 SFT2 .grid/width LDZ #01 SFT #00 SWP #30 SFT2 ADD2 #0004 ADD2 .grid/x2 STZ2
	.Screen/height DEI2
		DUP2 #04 SFT2 NIP #03 SUB .grid/height STZ
		DUP2 #01 SFT2 .grid/height LDZ #01 SFT INC #00 SWP #40 SFT2 SUB2 #0004 SUB2 .grid/y1 STZ2
		#01 SFT2 .grid/height LDZ #01 SFT #00 SWP #40 SFT2 ADD2 #0008 SUB2 .grid/y2 STZ2
	#00 .grid/height LDZ #00 .grid/width LDZ MUL2 .grid/length STZ2

	( set toolbar size )
	.grid/x1 LDZ2 .toolbar/x1 STZ2
	.grid/y2 LDZ2 #0010 ADD2 .toolbar/y1 STZ2
	.grid/x2 LDZ2 .toolbar/x2 STZ2
	.toolbar/y1 LDZ2 #0008 ADD2 .toolbar/y2 STZ2

	( vectors )
	;untrap JSR2

	( theme support )
	;load-theme JSR2
	( draw once )
	.grid/x2 LDZ2 #0020 SUB2 .Screen/x DEO2
	.toolbar/y1 LDZ2 .Screen/y DEO2
	;font/load #01 ;draw-sprite JSR2
	;font/make #01 ;draw-sprite JSR2
	( init random )
	;init-prng JSR2
	( blank file )
	;init-file JSR2
	( display guide )
	;toggle-guide JSR2
	( draw position )
	;draw-position JSR2
	( start )
	.timer/playing LDZk #00 EQU SWP STZ

BRK

@untrap ( -- )

	( vectors )
	;on-console .Console/vector DEO2
	;on-button .Controller/vector DEO2
	;on-mouse .Mouse/vector DEO2
	;on-frame .Screen/vector DEO2
	#01 ;draw-filepath JSR2
	#00 .Mouse/state DEO

JMP2r

@trap ( -- )

	( vectors )
	;on-button-trap .Controller/vector DEO2
	;on-mouse-trap .Mouse/vector DEO2
	;on-frame-trap .Screen/vector DEO2
	#00 .Mouse/state DEO

	( clear cursor )
	.cursor/x LDZ2 .Screen/x DEO2
	.cursor/y LDZ2 .Screen/y DEO2
	#40 .Screen/sprite DEO

JMP2r

@on-console ( -> )

	.Console/read DEI
	[ #11 ] NEQk NIP ,&no-u JCN #00ff #00 ;mod-sel JSR2 &no-u
	[ #12 ] NEQk NIP ,&no-d JCN #0001 #00 ;mod-sel JSR2 &no-d
	[ #13 ] NEQk NIP ,&no-l JCN #ff00 #00 ;mod-sel JSR2 &no-l
	[ #14 ] NEQk NIP ,&no-r JCN #0100 #00 ;mod-sel JSR2 &no-r
	DUP ;ci-key JSR2 #00 EQU ,&no-key JCN
		STHk .selection LDZ2 STHr ;set-cell JSR2
		&no-key
	POP

BRK

@on-frame-trap ( -> )

	.state/timer LDZ
	DUP #07 AND ,&no-blink JCN
		DUP #03 SFT #01 AND #30 SFT INC ;draw-filepath JSR2
		&no-blink
	INC .state/timer STZ

BRK

@on-button-trap ( -> )

	#00 ;draw-filepath JSR2
	.Controller/key DEI DUP #0d EQU #03 MUL SUB ,capture-trap JSR
	#01 ;draw-filepath JSR2

BRK

@capture-trap ( button -- )

	DUP ,&no-null JCN POP JMP2r &no-null
	[ #08 ] NEQk NIP ,&no-pop JCN ;filepath ;spop JSR2 POP JMP2r &no-pop
	[ #0a ] NEQk NIP ,&no-load JCN ;load-file JSR2 &no-load
	[ #7f ] NEQk NIP ,&no-delete JCN ;filepath #0040 ;mclr JSR2 POP JMP2r &no-delete
	[ #20 ] GTHk NIP ,&no-special JCN ;untrap JSR2 POP JMP2r &no-special
	;filepath ;slen JSR2 NIP #3f EQU ,&no-push JCN
		DUP ;filepath ROT ;sput JSR2
		&no-push
	POP

JMP2r

@on-mouse-trap ( -> )

	( release trap on touch )
	.Mouse/state DEI #00 NEQ JMP BRK
	;untrap JSR2

BRK

@on-frame ( -> )

	( paused )
	.timer/playing LDZ JMP BRK
	( on beat )
	.timer LDZ2 NEQ ,&skip JCN
		;manage-voices JSR2
		;run JSR2
		.timer/frame LDZ2k INC2 ROT STZ2
		#00 .timer/beat STZ
		&skip
	( inc beat )
	.timer/beat LDZk INC SWP STZ

BRK

@on-button ( -> )

	( d-pad handler )
	.Controller/button DEI .dpad/last LDZ
	DUP2 #0200 EQU2 ;dpad-input/start JCN2
	DUP2 #0002 EQU2 ;dpad-input/end JCN2
	DUP #0f AND #02 EQU ;dpad-input/add JCN2
	POP ( pop last )
	.dpad/last STZ

	( modifier handlers )
	.Controller/button DEI #f0 AND ;on-button-arrow JCN2
	.Controller/button DEI #01 AND ;on-button-ctrl JCN2

	( default )
	.Controller/key DEI
	[ #00 ] EQUk NIP ,&end JCN
	[ #1b ] NEQk NIP ,&no-esc JCN .selection/from LDZ2 ;set-sel-from JSR2 POP BRK &no-esc
	[ #20 ] NEQk NIP ,&no-spc JCN ;toggle-play JSR2 POP BRK &no-spc
	[ #08 ] NEQk NIP OVR #7f NEQ AND ,&no-bks JCN CHAR-DOT ;fill-sel JSR2 POP BRK &no-bks
	DUP ;ci-key JSR2 #00 EQU ,&no-key JCN .Controller/key DEI ;fill-sel JSR2 &no-key
	&end
	POP

BRK

@on-button-insert ( -> )

	.Controller/key DEI
	[ #00 ] EQUk NIP ,&end JCN
	[ #1b ] NEQk NIP ,&no-esc JCN ;unset-insert JSR2 POP BRK &no-esc
	[ #20 ] NEQk NIP ,&no-spc JCN #01 #00 #00 ;mod-sel JSR2 POP BRK &no-spc
	[ #08 ] NEQk NIP ,&no-bks JCN #ff #00 #00 ;mod-sel JSR2 CHAR-DOT ;fill-sel JSR2 POP BRK &no-bks
	DUP ;ci-key JSR2 #00 EQU ,&no-key JCN .Controller/key DEI ;fill-sel JSR2 #01 #00 #00 ;mod-sel JSR2 &no-key
	&end
	POP

BRK

@on-button-ctrl ( -> )

	.Controller/key DEI
	( copy/paste/cut )
	[ LIT 'c ] NEQk NIP ,&no-copy JCN ;copy-snarf JSR2 &no-copy
	[ LIT 'v ] NEQk NIP ,&no-paste JCN ;paste-snarf JSR2 &no-paste
	[ LIT 'x ] NEQk NIP ,&no-cut JCN ;cut-snarf JSR2 &no-cut
	( new/rename/open/save )
	[ LIT 'n ] NEQk NIP ,&no-new JCN ;init-file JSR2 &no-new
	[ LIT 'r ] NEQk NIP ,&no-name JCN ;trap JSR2 &no-name
	[ LIT 'o ] NEQk NIP ,&no-open JCN ;load-file JSR2 &no-open
	[ LIT 's ] NEQk NIP ,&no-save JCN ;save-file JSR2 &no-save
	( select-all/insert )
	[ LIT 'a ] NEQk NIP ,&no-a JCN ;set-sel-all JSR2 &no-a
	[ LIT 'i ] NEQk NIP ,&no-i JCN ;set-insert JSR2 &no-i
	[ LIT 'h ] NEQk NIP ,&no-h JCN ;toggle-guide JSR2 &no-h
	( tempo )
	[ LIT ', ] NEQk NIP ,&no-slow JCN #ff ;mod-speed JSR2 &no-slow
	[ LIT '. ] NEQk NIP ,&no-fast JCN #01 ;mod-speed &no-fast
	POP

BRK

@on-button-arrow ( -> )

	( capture )
	.Controller/button DEI
	DUP #0f AND ,&mod STR
	#04 SFT #00 OVR #03 AND ;&vec ADD2 LDA ,&y STR
	#02 SFT #00 SWP #03 AND ;&vec ADD2 LDA ,&x STR
	[ LIT &x $1 ] [ LIT &y $1 ] [ LIT &mod $1 ] ;mod-sel JSR2

BRK
	&vec 00 ff 01 00

@on-mouse ( -> )

	( clear last cursor )
	.cursor/x LDZ2 .Screen/x DEO2
	.cursor/y LDZ2 .Screen/y DEO2
	#40 .Screen/sprite DEO
	( draw new cursor )
	.Mouse/x DEI2 DUP2 .cursor/x STZ2 .Screen/x DEO2
	.Mouse/y DEI2 DUP2 .cursor/y STZ2 .Screen/y DEO2
	;cursor-icn .Screen/addr DEO2
	#41 [ .Mouse/state DEI #00 NEQ #10 SFT ] ADD .Screen/sprite DEO
	( route )
	.Mouse/y DEI2 .toolbar/y1 LDZ2 SUB2 #04 SFT2 #0000 EQU2 ;on-mouse-toolbar JCN2
	.Mouse/x DEI2 .Mouse/y DEI2 .grid ;within-rect JSR2 ,on-mouse-grid JCN

BRK

@on-mouse-grid ( -> )

	.Mouse/state DEI [ LIT &last $1 ]
		DUP2 #0000 EQU2 ,&end JCN
		( on down )
		DUP2 #0100 NEQ2 ,&no-down JCN
			.Mouse/x DEI2 .grid/x1 LDZ2 SUB2 #03 SFT2 NIP
			.Mouse/y DEI2 .grid/y1 LDZ2 SUB2 #04 SFT2 NIP
				;set-sel-from JSR2
			,&end JMP
			&no-down
		( on release )
		.Mouse/x DEI2 .grid/x1 LDZ2 SUB2 #03 SFT2 NIP
		.Mouse/y DEI2 .grid/y1 LDZ2 SUB2 #04 SFT2 NIP
			;set-sel-to JSR2
	&end
	POP ,&last STR

BRK

@on-mouse-toolbar ( -> )

	( skip ) .Mouse/state DEI #01 JCN BRK

	( left-side )
	.Mouse/x DEI2 .grid/x1 LDZ2 SUB2 #03 SFT2 NIP
	[ #05 ] GTHk NIP ,&no-insert JCN ;toggle-insert JSR2 POP BRK &no-insert
	[ #09 ] GTHk NIP ,&no-pause JCN ;toggle-play JSR2 POP BRK &no-pause
	[ #0d ] GTHk NIP ,&no-speed JCN [ .Mouse/state DEI #01 EQU #10 SFT #01 SUB ] ;mod-speed JSR2 #00 .Mouse/state DEO POP BRK &no-speed
	[ #0e ] GTHk NIP OVR .grid/width LDZ SWP SUB #06 GTH #0101 NEQ2 ,&no-rename JCN ;trap JSR2 &no-rename
	POP
	( right-side )
	.grid/x2 LDZ2 .Mouse/x DEI2 SUB2 #03 SFT2 NIP
	[ #00 ] NEQk NIP ,&no-save JCN ;save-file JSR2 &no-save
	[ #02 ] NEQk NIP ,&no-load JCN ;load-file JSR2 &no-load
	[ #03 ] NEQk NIP ,&no-name JCN ;init-file JSR2 &no-name
	[ #05 ] NEQk NIP ,&no-guide JCN ;toggle-guide JSR2 &no-guide
	POP
	#00 .Mouse/state DEO

BRK

( selection )

@mod-sel ( x y mod -- )

	DUP #04 NEQ ,&no-scale JCN
		POP
		.selection/to LDZ2 ,&add-pos JSR ;set-sel-to JSR2
		JMP2r
		&no-scale
	DUP #01 NEQ ,&no-drag JCN
		POP
		;cut-snarf JSR2
		STH2k .selection/from LDZ2 ,&add-pos JSR
		STH2r .selection/to LDZ2 ,&add-pos JSR
			;set-sel-range JSR2
		;paste-snarf JSR2
		JMP2r
		&no-drag
	POP
	( default )
	STH2k .selection/from LDZ2 ,&add-pos JSR
	STH2r .selection/to LDZ2 ,&add-pos JSR
		;set-sel-range JSR2

JMP2r
	&add-pos ROT ADD STH ADD STHr JMP2r

@set-sel-all ( -- )

	#0000 .grid/size LDZ2 ,set-sel-range JSR

JMP2r

@set-sel-from ( x y -- )

	DUP2 ,set-sel-range JSR

JMP2r

@set-sel-to ( x y -- )

	.selection/from LDZ2 SWP2

@set-sel-range ( from* to* -- )

	( clamp top-left )
	OVR2 #ff NEQ SWP #ff NEQ AND ,&no-tl JCN
		POP2 POP2 JMP2r
		&no-tl
	( clamp bottom-right )
	OVR2 .grid/height LDZ LTH SWP .grid/width LDZ LTH AND ,&no-br JCN
		POP2 POP2 JMP2r
		&no-br
	( from )
	SWP2 DUP2 .selection/from LDZ2 NEQ2 STH .selection/from STZ2
	( to )
	.selection/y1 LDZ GTHk JMP SWP POP .grid/height LDZ #01 SUB LTHk JMP SWP POP STH
	.selection/x1 LDZ GTHk JMP SWP POP .grid/width LDZ #01 SUB LTHk JMP SWP POP STHr
	DUP2 .selection/to LDZ2 NEQ2 STH .selection/to STZ2
	( skip redraw when unchanged )
	ADDr STHr #01 JCN JMP2r
	( redraw )
	;draw-grid JSR2
	;draw-position JSR2
	( hide guide )
	.guide LDZ #00 EQU ,&no-guide JCN ;toggle-guide JSR2 &no-guide

JMP2r

@fill-sel ( char -- )

	,&c STR
	.selection/y2 LDZ INC .selection/y1 LDZ
	&ver
		STHk
		.selection/x2 LDZ INC .selection/x1 LDZ
		&hor
			[ LIT &c $1 ] OVR STHkr ;get-cell JSR2 ;data/cells ADD2 STA
			INC GTHk ,&hor JCN
		POP2 POPr
		INC GTHk ,&ver JCN
	POP2
	#01 .state/changed STZ ;draw-state JSR2

JMP2r

@mod-speed ( mod -- )

	.timer/speed LDZ ADD

@set-speed ( speed -- )

	#1f AND [ #04 GTHk JMP SWP POP ] .timer/speed STZ
	#00 .timer/beat STZ
	;draw-speed JSR2

JMP2r

@toggle-insert ( -- )

	.Controller/vector DEI2 ;on-button-insert EQU2 ,unset-insert JCN

@set-insert ( -- )

	;on-button-insert .Controller/vector DEO2
	;draw-position JSR2
	#00 .Mouse/state DEO

JMP2r

@unset-insert ( -- )

	;on-button .Controller/vector DEO2
	;draw-position JSR2
	#00 .Mouse/state DEO

JMP2r

@toggle-play ( -- )

	.timer/playing LDZk #00 EQU SWP STZ
	#00 .Mouse/state DEO
	;draw-timer JSR2

JMP2r

@toggle-guide ( -- )

	.guide LDZk #00 EQU SWP STZ
	;draw-grid JSR2
	.toolbar/y1 LDZ2 .Screen/y DEO2
	.grid/x2 LDZ2 #0030 SUB2 .Screen/x DEO2
	;font/help [ #00 .guide LDZ #40 SFT2 ] ADD2 #01 ;draw-sprite JSR2

JMP2r

( special )

@dpad-input ( -> )

	&start ( button* -> )
		POP
		#20 .dpad STZ
		;&save JMP2
	&end ( button* -> )
		POP
		.dpad LDZ #7f GTH ,&save JCN
		.dpad LDZ ;fill-sel JSR2
		.selection/from LDZ2 ;set-sel-from JSR2
		#00 .dpad STZ
		.dpad/last STZ
		;draw-speed JSR2
		BRK
		,&save JMP
	&add ( button* -> )
		#02 NEQ ,&save JCN
		DUP #04 SFT .dpad LDZ ADD #7f AND .dpad STZ
		,&save JMP
	&save ( -> )
		.dpad/last STZ
		;draw-dpad JSR2
	BRK

BRK

@init ( -- )

	;data/cells .grid/length LDZ2 ;mclr JSR2
	&grid
	;data/locks .grid/length LDZ2 STH2k ;mclr JSR2
	;data/types STH2r ;mclr JSR2
	#00 .signal/midi STZ

@init-variables ( -- )

	#2400
	&loop
		STHk #2e2e .variables STHr ADD STZ2
		INC INC GTHk ,&loop JCN
	POP2

JMP2r

@manage-voices ( -> )

	( iterate thru channels )

	#10 #00 &while EQUk ,&end JCN
		( note ) DUP #10 SFT .voices ADD LDZk
		( remaining length ) SWP INC LDZ
		( next channel if already 0 ) DUP #00 EQU ,&next-chan JCN

		( update remaining length ) #01 SUB ROTk #10 SFT .voices ADD INC STZ POP
		( send note-off when length reaches 0 )
		#00 NEQ ,&no-off JCN
			( channel ) OVR .Console/write DEO
			( note ) DUP .Console/write DEO
			( off ) #00 .Console/write DEO
		&no-off
		POP
		INC
	,&while JMP &end POP2 JMP2r

	&next-chan POP2 INC
	,&while JMP

@run ( -- )

	,init/grid JSR
	( reset head ) LIT2r 0000
	.grid/height LDZ #00
	&ver
		DUP .head/y STZ
		.grid/width LDZ #00
		&hor
			DUP .head/x STZ
			STH2kr .head/addr STZ2
			STH2kr ;data/cells ADD2 LDA ,run-char JSR
			INC2r
			INC GTHk ,&hor JCN
		POP2
		INC GTHk ,&ver JCN
	POP2
	POP2r
	;draw-grid JSR2
	;draw-timer JSR2

JMP2r

@run-char ( x y char -- )

	( skip dot )
	DUP CHAR-DOT NEQ ,&no-dot JCN
		POP JMP2r
		&no-dot
	( skip numbers )
	DUP #30 LTH ,&no-num JCN
	DUP #39 GTH ,&no-num JCN
		POP JMP2r
		&no-num
	( skip locked )
	.head/addr LDZ2 ;data/locks ADD2 LDA #00 EQU ,&no-locked JCN
		POP JMP2r
		&no-locked
	( lowercase )
	DUP #61 LTH ,&no-lc JCN
	DUP #7a GTH ,&no-lc JCN
		;get-bang JSR2 ,&run JCN
		POP JMP2r
		&no-lc
	( uppercase )
	DUP #41 LTH ,&no-uc JCN
	DUP #5a GTH ,&no-uc JCN
		&run
		.head/addr LDZ2 STH2k
		( set type ) OPERATOR-TYPE STH2r ;data/types ADD2 STA
		( run ) ROT ;chrb36 JSR2 #0a SUB #10 SFT #00 SWP ;op-table/func ADD2 LDA2 JMP2
		&no-uc
	( special )
	[ LIT '* ] EQUk NIP ;op-bang/func JCN2
	[ LIT '# ] EQUk NIP ;op-comment/func JCN2
	[ LIT '= ] EQUk NIP ;op-synth/func JCN2
	[ LIT '; ] EQUk NIP ;op-pitch/func JCN2
	[ LIT ': ] EQUk NIP ;op-midi/func JCN2
	[ LIT '/ ] EQUk NIP ;op-byte/func JCN2
	[ LIT '$ ] EQUk NIP ;op-self/func JCN2
	POP
	( erase )
	CHAR-DOT .head/addr LDZ2 ;data/cells ADD2 STA

JMP2r

( operations )

@b36chr ( b36 -- char ) #24 DIVk MUL SUB #00 SWP ;b36clc ADD2 LDA JMP2r
@chrb36 ( char -- b36 ) #20 SUB #00 SWP ;values ADD2 LDA JMP2r
@chrmid ( char -- midi ) DUP ,chrb36 JSR SWP ;ciuc JSR2 #24 MUL ADD #00 SWP ;notes ADD2 LDA JMP2r

@set-cell ( x y c -- ) ROT ROT ,get-cell JSR ;data/cells ADD2 STA JMP2r
@get-cell ( x y -- addr* ) #00 SWP #00 .grid/width LDZ MUL2 ROT #00 SWP ADD2 JMP2r

@get-bang ( -- bang )

	.head/addr LDZ2 ;data/cells ADD2 STH2
	STH2kr #0001 SUB2 LDA CHAR-BANG EQU ,&bang JCN
	STH2kr INC2 LDA CHAR-BANG EQU ,&bang JCN
	STH2kr #00 .grid/width LDZ SUB2 LDA CHAR-BANG EQU ,&bang JCN
	STH2kr #00 .grid/width LDZ ADD2 LDA CHAR-BANG EQU ,&bang JCN
	POP2r #00 JMP2r
	&bang POP2r #01

JMP2r

@lerp ( rate target val -- val )

	DUP2 GTHk JMP SWP SUB STH
	( if rate GTH target )
	ROT DUP STHr LTH ,&skip JCN
		POP2 JMP2r
		&skip
	( target val rate )
	STH
	GTHk ,&no-below JCN
		NIP STHr SUB JMP2r
		&no-below
	NIP STHr ADD
	
JMP2r

( drawing )

@draw-dpad ( -- )

	.grid/x1 LDZ2 #0050 ADD2 .Screen/x DEO2
	.toolbar/y1 LDZ2 .Screen/y DEO2
	( value )
	.dpad LDZ #04 ;draw-byte JSR2
	( space )
	;draw-sprite/blank JSR2
	( icon )
	.dpad LDZ #01 ;draw-char JSR2
	
JMP2r

@draw-position ( -- )

	.grid/x1 LDZ2 .Screen/x DEO2
	.toolbar/y1 LDZ2 .Screen/y DEO2
	( draw size )
	.selection/from LDZ2 .selection/to LDZ2 EQU2k ,&normal JCN
		SWP2 SUB2 DUP2
		&normal
	( value )
	POP2 #01 ;draw-short JSR2
	( icon )
	;font/selector #00 [ .Controller/vector DEI2 ;on-button-insert EQU2 ] #40 SFT2 ADD2
	#02 .selection/from LDZ2 .selection/to LDZ2 EQU2 ADD
		;draw-sprite JSR2

JMP2r

@draw-timer ( -- )

	.toolbar/y1 LDZ2 .Screen/y DEO2
	.grid/x1 LDZ2 #0030 ADD2 .Screen/x DEO2
	( value )
	.timer/frame-lb LDZ STHk #03 .timer/playing LDZ #10 SFT SUB ;draw-byte JSR2
	( icon )
	;font/beat #03 STHr #07 AND #00 EQU SUB ;draw-sprite JSR2

JMP2r

@draw-speed ( -- )

	.grid/x1 LDZ2 #0050 ADD2 .Screen/x DEO2
	.toolbar/y1 LDZ2 .Screen/y DEO2
	( value )
	.timer/speed LDZ #01 ;draw-byte JSR2
	( th )
	;&th-txt #03 ;draw-str JSR2

JMP2r
	&th-txt "th $1

@draw-state ( -- )

	.toolbar/x2 LDZ2 #0008 SUB2 .Screen/x DEO2
	.toolbar/y1 LDZ2 .Screen/y DEO2
	( icon )
	;font/save #01 .state/changed LDZ ADD ;draw-sprite JSR2

JMP2r

@draw-filepath ( color -- )

	.toolbar/y1 LDZ2 .Screen/y DEO2
	.toolbar/x1 LDZ2 #0078 ADD2 .Screen/x DEO2
	( icon )
	;filepath ROT ;draw-str JSR2

JMP2r

@draw-grid ( -- )

	( reset head ) LIT2r 0000
	.grid/height LDZ #00
	&ver
		DUP .head/y STZ
		( x ) .grid/x1 LDZ2 .Screen/x DEO2
		( y ) #00 OVR #40 SFT2 .grid/y1 LDZ2 ADD2 .Screen/y DEO2
		.grid/width LDZ #00
		&hor
			DUP .head/x STZ
			STH2kr .head/addr STZ2
			STH2kr ,get-char-at-addr JSR ,get-color JSR ;draw-char JSR2
			INC2r
			INC GTHk ,&hor JCN
		POP2
		INC GTHk ,&ver JCN
	POP2
	POP2r
	( draw meter )
	;draw-meter JSR2
	( draw guide )
	.guide LDZ JMP JMP2r ;draw-guide JSR2

JMP2r

@get-color ( -- type )

	.head LDZ2 ;is-selected JSR2 ,&selected JCN
		#00 .head/addr LDZ2 ;data/types ADD2 LDA ;cell-styles ADD2 LDA JMP2r
	&selected
		#09

JMP2r

@get-char-at-addr ( addr* -- char )

	;data/cells ADD2 LDA
	DUP CHAR-DOT NEQ ,&no-bar JCN
		POP .head LDZ2
		DUP2 #07 AND SWP #0f AND #0000 EQU2 ,&cross JCN
		DUP2 #01 AND SWP #03 AND #0000 EQU2 ,&dot JCN
		DUP2 ,is-selected JSR ,&dot JCN
		.head/addr LDZ2 ;data/types ADD2 LDA ,&dot JCN
		POP2 #20
	&no-bar
	
JMP2r
	&cross POP2 LIT '+ JMP2r
	&dot POP2 LIT '. JMP2r

@get-word ( addr* -- word* )

	;&word #0020 ;mclr JSR2
	&while
		INC2 DUP2 ;data/cells ADD2 LDA
			DUP LIT '. EQU ,&skip JCN
				DUP ;&word ROT ;sput JSR2
				&skip
			LIT '. NEQ ,&while JCN
	POP2
	;&word

JMP2r
	&word $20

@is-selected ( x y -- bool )

	DUP .selection/y1 LDZ LTH ,&end JCN
	DUP .selection/y2 LDZ GTH ,&end JCN
	OVR .selection/x1 LDZ LTH ,&end JCN
	OVR .selection/x2 LDZ GTH ,&end JCN
		POP2 #01 JMP2r
	&end
	POP2 #00

JMP2r

@draw-meter ( -- )

	.toolbar/y1 LDZ2 .Screen/y DEO2
	.grid/x2 LDZ2 #0040 SUB2 .Screen/x DEO2
	.signal/midi LDZ #07 LTHk JMP SWP POP STH
	;meter-icn #00 STHkr #40 SFT2 ADD2 .Screen/addr DEO2
	#01 STHr #07 EQU ADD .Screen/sprite DEO

JMP2r

@draw-guide ( -- )

	#0021 #0000
	&loop
		( x ) DUP2 #84 SFT2 .grid/x1 LDZ2 ADD2 #0020 ADD2 .Screen/x DEO2
		( y ) DUP2 #000f AND2 #40 SFT2 .grid/y1 LDZ2 ADD2 #0020 ADD2 .Screen/y DEO2
		DUP2 #10 SFT2 ;op-table/docs ADD2 LDA2
		( glyph ) LDAk #08 ;draw-char JSR2
		( space ) ;draw-sprite/blank JSR2
		( text ) INC2 #01 ,draw-str JSR
		INC2 GTH2k ,&loop JCN
	POP2 POP2

JMP2r

@draw-str ( str* color -- )

	STH
	&while
		LDAk STHkr ,draw-char JSR
		INC2 LDAk ,&while JCN
	POP2
	POPr

JMP2r

@draw-short ( short* color -- )

	STH SWP STHkr ,draw-byte JSR STHr

@draw-byte ( byte color -- )

	STH DUP #04 SFT STHkr ,draw-hex JSR STHr

@draw-hex ( byte color -- )

	STH #0f AND ;b36chr JSR2 STHr

@draw-char ( char color -- )

	OVR #20 LTH ,draw-unknown JCN
	STH #20 SUB #00 SWP #40 SFT2 ;font ADD2 STHr

@draw-sprite ( addr* color -- )
	
	STH .Screen/addr DEO2
	STHr .Screen/sprite DEO

JMP2r
	&blank #00 .Screen/sprite DEO JMP2r

@draw-unknown ( char color -- )

	NIP ;unknown-icn ROT ,draw-sprite JMP

JMP2r

( file )

@init-file ( default* -- )

	;init JSR2

	( rename to untitled.txt )
	#00 ;draw-filepath JSR2
	;filepath #0040 ;mclr JSR2
	;untitled-txt ;filepath #000d ;mcpy JSR2
	#01 ;draw-filepath JSR2

	( default speed )
	#0b ;set-speed JSR2

	;run JSR2
	#00 .state/changed STZ ;draw-state JSR2

JMP2r

@load-file ( -- )

	#0000 ;filepath ,inject-file JSR
	;draw-grid JSR2
	#00 .state/changed STZ ;draw-state JSR2

JMP2r

@inject-file ( x y path* -- )

	.File/name DEO2
	#0001 .File/length DEO2
	OVR ,&anchor-x STR
	&stream
		;&b .File/read DEO2
		( write )
		;&b LDA ;ci-key JSR2 #00 EQU ,&invalid JCN
			DUP2 ;&b LDA ;set-cell JSR2
			&invalid
		( inc x ) SWP INC SWP
		;&b LDA #0a NEQ ,&no-lb JCN
			( inc y ) INC
			( reset x ) [ LIT &anchor-x $1 ] ROT POP SWP
			&no-lb
		.File/success DEI2 ORA ,&stream JCN
	POP2

JMP2r
	&b $1

@save-file ( -- )

	;filepath .File/name DEO2
	#0001 .File/length DEO2
	.grid/height LDZ #00
	&ver
		.grid/width LDZ #00
		&hor
			OVR2 NIP OVR SWP ;get-cell JSR2 ;data/cells ADD2 .File/write DEO2
			INC GTHk ,&hor JCN
		POP2
		( linebreak ) ;&lb .File/write DEO2
		INC GTHk ,&ver JCN
	POP2
	#00 .state/changed STZ ;draw-state JSR2

JMP2r
	&lb 0a

( random )

@init-prng ( -- )

	( seed )
	#00 .DateTime/second DEI
	#00 .DateTime/minute DEI #60 SFT2 EOR2
	#00 .DateTime/hour   DEI #c0 SFT2 EOR2 ,prng/x STR2
	#00 .DateTime/hour   DEI #04 SFT2
	#00 .DateTime/day    DEI #10 SFT2 EOR2
	#00 .DateTime/month  DEI #60 SFT2 EOR2
		.DateTime/year  DEI2 #a0 SFT2 EOR2 ,prng/y STR2

JMP2r

@prng ( -- number* )

	LIT2 &x $2
	DUP2 #50 SFT2 EOR2
	DUP2 #03 SFT2 EOR2
	LIT2 &y $2 DUP2 ,&x STR2
	DUP2 #01 SFT2 EOR2 EOR2
	,&y STR2k POP

JMP2r

( theme )

@theme-txt ".theme $1

@load-theme ( -- )

	;theme-txt .File/name DEO2
	#0006 .File/length DEO2
	#fffa .File/read DEO2
	.File/success DEI2 #0006 NEQ2 ,&ignore JCN
		#fffa LDA2 .System/r DEO2
		#fffc LDA2 .System/g DEO2
		#fffe LDA2 .System/b DEO2
		&ignore

JMP2r

( snarf )

@snarf-txt ".snarf $1

@cut-snarf ( -- )

	,copy-snarf JSR
	CHAR-DOT ;fill-sel JSR2
	
JMP2r

@copy-snarf ( -- )

	;snarf-txt .File/name DEO2
	#0001 .File/length DEO2
	.selection/y2 LDZ INC .selection/y1 LDZ
	&ver
		STHk
		.selection/x2 LDZ INC .selection/x1 LDZ
		&hor
			DUP STHkr ;get-cell JSR2 ;data/cells ADD2 .File/write DEO2
			INC GTHk ,&hor JCN
		POP2 POPr
		( linebreak ) ;&lb .File/write DEO2
		INC GTHk ,&ver JCN
	POP2

JMP2r
	&lb 0a

@paste-snarf ( -- )

	.selection LDZ2 ;snarf-txt ;inject-file JSR2
	;draw-grid JSR2

JMP2r

( orca/opcodes )

@op-table
	:op-a :op-b :op-c :op-d :op-e :op-f :op-g :op-h
	:op-i :op-j :op-k :op-l :op-m :op-n :op-o :op-p
	:op-q :op-r :op-s :op-t :op-u :op-v :op-w :op-x
	:op-y :op-z
	:op-bang :op-comment :op-synth :op-midi :op-pitch :op-byte :op-self
	&docs
	:op-a/docs :op-b/docs :op-c/docs :op-d/docs :op-e/docs :op-f/docs :op-g/docs :op-h/docs
	:op-i/docs :op-j/docs :op-k/docs :op-l/docs :op-m/docs :op-n/docs :op-o/docs :op-p/docs
	:op-q/docs :op-r/docs :op-s/docs :op-t/docs :op-u/docs :op-v/docs :op-w/docs :op-x/docs
	:op-y/docs :op-z/docs
	:op-bang/docs :op-comment/docs :op-synth/docs :op-midi/docs :op-pitch/docs :op-byte/docs :op-self/docs
	&func
	:op-a/func :op-b/func :op-c/func :op-d/func :op-e/func :op-f/func :op-g/func :op-h/func
	:op-i/func :op-j/func :op-k/func :op-l/func :op-m/func :op-n/func :op-o/func :op-p/func
	:op-q/func :op-r/func :op-s/func :op-t/func :op-u/func :op-v/func :op-w/func :op-x/func
	:op-y/func :op-z/func

@op-a "add $1
	&docs 'A "Outputs 20 "sum 20 "of 20 "inputs $1
	&func ( addr* -- )

	STH2k
	( a-val ) #0001 SUB2 ;get-port-left-value JSR2
	( b-raw ) STH2kr INC2 ;get-port-right-raw JSR2
		( get case ) DUP ;ciuc JSR2 ,&case STR
		( to value ) ;chrb36 JSR2
	( res ) ADD
	( set case ) ;b36chr JSR2 DUP #60 GTH [ LIT &case $1 ] AND #50 SFT SUB
	( output ) STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2

JMP2r

@op-b "subtract $1
	&docs 'B "Outputs 20 "difference 20 "of 20 "inputs $1
	&func ( addr* -- )

	STH2k
	( get a ) #0001 SUB2 ;get-port-left-value JSR2
	( get b ) STH2kr INC2 ;get-port-right-raw JSR2
		( get case ) DUP ;ciuc JSR2 ,&case STR
		( to value ) ;chrb36 JSR2
	( res ) SUB DUP #80 LTH ,&bounce JCN #24 SWP SUB &bounce
	( set case ) ;b36chr JSR2 DUP #60 GTH [ LIT &case $1 ] AND #50 SFT SUB
	( output ) STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2

JMP2r

@op-c "clock $1
	&docs 'C "Outputs 20 "modulo 20 "of 20 "frame $1
	&func ( addr* -- )

	STH2k
	( get rate ) #0001 SUB2 ;get-port-left-value JSR2 DUP #00 EQU ADD
	( get mod ) STH2kr INC2 ;get-port-right-raw JSR2
		( get case ) DUP ;ciuc JSR2 ,&case STR
		( to value ) ;chrb36 JSR2 DUP #00 EQU ADD
	( res ) #00 SWP ROT #00 SWP .timer/frame LDZ2 SWP2 DIV2 SWP2 DIV2k MUL2 SUB2 NIP
	( set case ) ;b36chr JSR2 DUP #60 GTH [ LIT &case $1 ] AND #50 SFT SUB
	( output ) STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2

JMP2r

@op-d "delay $1
	&docs 'D "Bangs 20 "on 20 "modulo 20 "of 20 "frame $1
	&func ( addr* -- )

	STH2k
	( get rate ) #0001 SUB2 ;get-port-left-value JSR2 DUP #00 EQU ADD
	( get mod ) STH2kr INC2 ;get-port-right-value JSR2 DUP #00 EQU ADD
	( res ) MUL #00 SWP .timer/frame LDZ2 SWP2 DIV2k MUL2 SUB2 #0000 EQU2
	( bang on equal ) #fc MUL CHAR-DOT ADD
	( output ) STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2

JMP2r

@op-e "east $1
	&docs 'E "Moves 20 "eastward 20 "or 20 "bangs $1
	&func ( addr* -- )

	STH2k ;data/cells ADD2 LDA ,&self STR
	( wall ) .head/x LDZ INC .grid/width LDZ EQU ,&collide JCN
	( cell ) STH2kr INC2 ;data/cells ADD2 LDA CHAR-DOT NEQ ,&collide JCN
	( write new ) [ LIT &self $1 ] STH2kr INC2 ;set-port-raw JSR2
	( erase old ) CHAR-DOT STH2r ;set-port-raw JSR2
	JMP2r
	&collide
	( output ) CHAR-BANG STH2r ;set-port-output JSR2

JMP2r

@op-f "if $1
	&docs 'F "Bangs 20 "if 20 "inputs 20 "are 20 "equal $1
	&func ( addr* -- )

	STH2k
	( get a ) #0001 SUB2 ;get-port-left-raw JSR2
	( get b ) STH2kr INC2 ;get-port-right-raw JSR2
	( bang on equal ) EQU [ #fc MUL CHAR-DOT ADD  ]
	( output ) STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2

JMP2r

@op-g "generator $1
	&docs 'G "Writes 20 "operands 20 "with 20 "offset $1
	&func ( addr* -- )

	STH2k
	( x ) STH2kr #0003 SUB2 ;get-port-left-value JSR2
		( load ) #00 SWP ADD2
	( y ) STH2kr #0002 SUB2 ;get-port-left-value JSR2
		( load ) #00 SWP INC2 [ #00 .grid/width LDZ MUL2 ] ADD2
	,&save STR2
	( len ) STH2kr #0001 SUB2 ;get-port-left-value JSR2 DUP #00 EQU ADD
	#00
	&loop
		( load ) DUP #00 SWP STH2kr INC2 ADD2 ;get-port-right-raw JSR2
		( save ) OVR #00 SWP [ LIT2 &save $2 ] ADD2 ;set-port-output JSR2
		INC GTHk ,&loop JCN
	POP2
	POP2r

JMP2r

@op-h "hold $1
	&docs 'H "Holds 20 "southward 20 "operand $1
	&func ( addr* -- )

	#00 .grid/width LDZ ADD2
	( set lock ) DUP2 #01 ROT ROT ;data/locks ADD2 STA
	( set type ) OUTPUT-TYPE ROT ROT ;data/types ADD2 STA

JMP2r

@op-i "increment $1
	&docs 'I "Increments 20 "southward 20 "operand $1
	&func ( addr* -- )

	STH2k
	( step ) #0001 SUB2 ;get-port-left-value JSR2
	( mod ) STH2kr INC2 ;get-port-right-raw JSR2
		( get case ) DUP ;ciuc JSR2 ,&case STR
		( to value ) ;chrb36 JSR2 DUP #00 EQU ADD
	( res ) SWP STH2kr #00 .grid/width LDZ ADD2 [ ;data/cells ADD2 LDA ;chrb36 JSR2 ] ADD SWP DIVk MUL SUB
	( set case ) ;b36chr JSR2 DUP #60 GTH [ LIT &case $1 ] AND #50 SFT SUB
	( output ) STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2

JMP2r

@op-j "jumper $1
	&docs 'J "Outputs 20 "northward 20 "operand $1
	&func ( addr* -- )

	STH2k
	( get above ) #00 .grid/width LDZ SUB2 ;get-port-left-raw JSR2
	( ignore cable )
	DUP ;chrb36 JSR2 #13 NEQ ,&no-wire JCN
		POP POP2r JMP2r &no-wire
	( skip down )
	STH2r
	&while
		#00 .grid/width LDZ ADD2 DUP2 ;data/cells ADD2 LDA ;chrb36 JSR2 #13 EQU
		,&while JCN
	( set below ) ;set-port-output JSR2

JMP2r

@op-k "konkat $1
	&docs 'K "Reads 20 "multiple 20 "variables $1
	&func ( addr* -- )

	STH2k
	#0001 SUB2 ;get-port-left-value JSR2 #00
	&loop
		DUP #00 SWP STH2kr INC2 ADD2 STH2k ;get-port-right-raw JSR2
		DUP CHAR-DOT EQU ,&skip JCN
			( load ) DUP ;chrb36 JSR2 .variables ADD LDZ
			( save ) STH2kr #00 .grid/width LDZ ADD2 ;set-port-output JSR2
			&skip
		POP
		POP2r
		INC GTHk ;&loop JCN2
	POP2
	POP2r

JMP2r

@op-l "lesser $1
	&docs 'L "Outputs 20 "smallest 20 "of 20 "inputs $1
	&func ( addr* -- )

	STH2k
	( get a ) #0001 SUB2 ;get-port-left-value JSR2
	( get b ) STH2kr INC2 ;get-port-right-raw JSR2
		( get case ) DUP ;ciuc JSR2 ,&case STR
		( to value ) ;chrb36 JSR2
	( res ) LTHk JMP SWP POP
	( set case ) ;b36chr JSR2 DUP #60 GTH [ LIT &case $1 ] AND #50 SFT SUB
	( output ) STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2

JMP2r

@op-m "multiply $1
	&docs 'M "Outputs 20 "product 20 "of 20 "inputs $1
	&func ( addr* -- )

	STH2k
	( get a ) #0001 SUB2 ;get-port-left-value JSR2
	( get b ) STH2kr INC2 ;get-port-right-raw JSR2
		( get case ) DUP ;ciuc JSR2 ,&case STR
		( to value ) ;chrb36 JSR2
	( res ) MUL
	( set case ) ;b36chr JSR2 DUP #60 GTH [ LIT &case $1 ] AND #50 SFT SUB
	( output ) STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2

JMP2r

@op-n "north $1
	&docs 'N "Moves 20 "Northward 20 "or 20 "bangs $1
	&func ( addr* -- )

	STH2k ;data/cells ADD2 LDA ,&self STR
	( wall ) .head/y LDZ #01 SUB #ff EQU ,&collide JCN
	( cell ) STH2kr #00 .grid/width LDZ SUB2 ;data/cells ADD2 LDA CHAR-DOT NEQ ,&collide JCN
	( write new ) [ LIT &self $1 ] STH2kr #00 .grid/width LDZ SUB2 ;set-port-raw JSR2
	( erase old ) CHAR-DOT STH2r ;set-port-raw JSR2
	JMP2r
	&collide
	( output ) CHAR-BANG STH2r ;set-port-output JSR2

JMP2r

@op-o "read $1
	&docs 'O "Reads 20 "operand 20 "with 20 "offset $1
	&func ( addr* -- )

	STH2k
	( x ) STH2kr #0002 SUB2 ;get-port-left-value JSR2 INC #00 SWP ADD2
	( y ) STH2kr #0001 SUB2 ;get-port-left-value JSR2 #00 SWP #00 .grid/width LDZ MUL2 ADD2
	( val ) ;get-port-right-raw JSR2
	( output ) STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2

JMP2r

@op-p "push $1
	&docs 'P "Writes 20 "eastward 20 "operand $1
	&func ( addr* -- )

	STH2k
	( key ) #0002 SUB2 ;get-port-left-value JSR2
	( len ) STH2kr #0001 SUB2 ;get-port-left-value JSR2 DUP #00 EQU ADD
	#00
	&loop
		#00 OVR STH2kr #00 .grid/width LDZ ADD2 ADD2 STH2
		( lock ) #01 STH2kr ;data/locks ADD2 STA
		( type ) LOCKED-TYPE STH2r ;data/types ADD2 STA
		INC GTHk ,&loop JCN
	POP
	( read ) STH2kr INC2 ;get-port-right-raw JSR2
	( output ) ROT ROT DIVk MUL SUB #00 SWP STH2r #00 .grid/width LDZ ADD2 ADD2 ;set-port-output JSR2

JMP2r

@op-q "query $1
	&docs 'Q "Reads 20 "operands 20 "with 20 "offset $1
	&func ( addr* -- )

	STH2k
	( x ) STH2kr #0003 SUB2 ;get-port-left-value JSR2
		( load ) #00 SWP INC2 ADD2
	( y ) STH2kr #0002 SUB2 ;get-port-left-value JSR2
		( load ) #00 SWP [ #00 .grid/width LDZ MUL2 ] ADD2
	,&load STR2
	( len ) STH2kr #0001 SUB2 ;get-port-left-value JSR2 DUP #00 EQU ADD
		( save ) DUP #00 SWP STH2kr #00 .grid/width LDZ ADD2 SWP2 SUB2 INC2 ,&save STR2
	#00
	&loop
		( load ) DUP #00 SWP [ LIT2 &load $2 ] ADD2 ;get-port-right-raw JSR2
		( save ) OVR #00 SWP [ LIT2 &save $2 ] ADD2 ;set-port-output JSR2
		INC GTHk ,&loop JCN
	POP2
	POP2r

JMP2r

@op-r "random $1
	&docs 'R "Outputs 20 "random 20 "value $1
	&func ( addr* -- )

	STH2k
	( a-min ) #0001 SUB2 ;get-port-left-value JSR2
	( b-max ) STH2kr INC2 ;get-port-right-raw JSR2
		( get case ) DUP ;ciuc JSR2 ,&case STR
		( to value ) ;chrb36 JSR2 DUP #00 EQU ADD
	( mod ) OVR SUB ;prng JSR2 ADD SWP DUP #00 EQU ADD DIVk MUL SUB ADD
	( set case ) ;b36chr JSR2 DUP #60 GTH [ LIT &case $1 ] AND #50 SFT SUB
	( output ) STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2

JMP2r

@op-s "south $1
	&docs 'S "Moves 20 "southward 20 "or 20 "bangs $1
	&func ( addr* -- )

	STH2k ;data/cells ADD2 LDA ,&self STR
	( wall ) .head/y LDZ INC .grid/height LDZ EQU ,&collide JCN
	( cell ) STH2kr #00 .grid/width LDZ ADD2 ;data/cells ADD2 LDA CHAR-DOT NEQ ,&collide JCN
	( write new ) [ LIT &self $1 ] STH2kr #00 .grid/width LDZ ADD2 ;set-port-raw JSR2
	( erase old ) CHAR-DOT STH2r ;set-port-raw JSR2
	JMP2r
	&collide
	( output ) CHAR-BANG STH2r ;set-port-output JSR2

JMP2r

@op-t "track $1
	&docs 'T "Reads 20 "eastward 20 "operand $1
	&func ( addr* -- )

	STH2k
	( key ) #0002 SUB2 ;get-port-left-value JSR2
	( len ) STH2kr #0001 SUB2 ;get-port-left-value JSR2 DUP #00 EQU ADD
	#00
	&loop
		#00 OVR STH2kr INC2 ADD2 STH2
		( lock ) #01 STH2kr ;data/locks ADD2 STA
		( type ) LOCKED-TYPE STH2r ;data/types ADD2 STA
		INC GTHk ,&loop JCN
	POP
	( read ) DIVk MUL SUB #00 SWP STH2kr INC2 ADD2 ;get-port-right-raw JSR2
	STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2

JMP2r

@op-u "Uclid $1
	&docs 'U "Bangs 20 "on 20 "Euclidean 20 "rhythm $1
	&func ( addr* -- )

	STH2k
	( step ) #0001 SUB2 ;get-port-left-value JSR2
	( max ) STH2kr INC2 ;get-port-right-value JSR2 DUP #00 EQU ADD STH2
	( frame ADD max SUB 1 ) .timer/frame LDZ2 STHkr #00 SWP ADD2 #0001 SUB2
	( MUL step ) OVRr STHr #00 SWP MUL2
	( % max ) STHkr #00 SWP DIV2k MUL2 SUB2
	( ADD step ) SWPr STHr #00 SWP ADD2
	( bucket GTH= max ) STHr #00 SWP LTH2 #01 NEQ
	( bang if equal ) #fc MUL CHAR-DOT ADD
	STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2

JMP2r

@op-v "variable $1
	&docs 'V "Reads 20 "and 20 "writes 20 "variable $1
	&func ( addr* -- )

	STH2k
	( key ) #0001 SUB2 ;get-port-left-raw JSR2
	( val ) STH2kr INC2 ;get-port-right-raw JSR2
	DUP CHAR-DOT EQU ,&idle JCN
	OVR ;chrb36 JSR2 ,&save JCN
	( load )
		NIP ;chrb36 JSR2 .variables ADD LDZ STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2 JMP2r
	&save
		SWP ;chrb36 JSR2 .variables ADD STZ POP2r JMP2r
	&idle
		POP2 POP2r

JMP2r

@op-w "west $1
	&docs 'W "Moves 20 "westward 20 "or 20 "bangs $1
	&func ( addr* -- )

	STH2k ;data/cells ADD2 LDA ,&self STR
	( wall ) .head/x LDZ #01 SUB #ff EQU ,&collide JCN
	( cell ) STH2kr #0001 SUB2 ;data/cells ADD2 LDA CHAR-DOT NEQ ,&collide JCN
	( write new ) [ LIT &self $1 ] STH2kr #0001 SUB2 ;set-port-raw JSR2
	( erase old ) CHAR-DOT STH2r ;set-port-raw JSR2
	JMP2r
	&collide
	( output ) CHAR-BANG STH2r ;set-port-output JSR2

JMP2r

@op-x "write $1
	&docs 'X "Writes 20 "operand 20 "with 20 "offset $1
	&func ( addr* -- )

	STH2k
	( x ) STH2kr #0002 SUB2 ;get-port-left-value JSR2 #00 SWP ADD2
	( y ) STH2kr #0001 SUB2 ;get-port-left-value JSR2 INC #00 SWP #00 .grid/width LDZ MUL2 ADD2
	( val ) STH2r INC2 ;get-port-right-raw JSR2
	( output ) ROT ROT ;set-port-output JSR2

JMP2r

@op-y "yumper $1
	&docs 'Y "Outputs 20 "westward 20 "operand $1
	&func ( addr* -- )

	STH2k
	( get above ) #0001 SUB2 ;get-port-left-raw JSR2
	( ignore cable )
	DUP ;chrb36 JSR2 #22 NEQ ,&no-wire JCN
		POP POP2r JMP2r &no-wire
	( skip down )
	STH2r
	&while
		INC2 DUP2 ;data/cells ADD2 LDA ;chrb36 JSR2 #22 EQU
		,&while JCN
	( set below ) ;set-port-output JSR2

JMP2r

@op-z "lerp $1
	&docs 'Z "Transitions 20 "operand 20 "to 20 "input $1
	&func ( addr* -- )

	STH2k
	( rate ) #0001 SUB2 ;get-port-left-value JSR2
	( target ) STH2kr INC2 ;get-port-right-raw JSR2
		( get case ) DUP ;ciuc JSR2 ,&case STR
		( to value ) ;chrb36 JSR2
	( val ) STH2kr #00 .grid/width LDZ ADD2 [ ;data/cells ADD2 LDA ;chrb36 JSR2 ]
	( res ) ;lerp JSR2
	( set case ) ;b36chr JSR2 DUP #60 GTH [ LIT &case $1 ] AND #50 SFT SUB
	( output ) STH2r #00 .grid/width LDZ ADD2 ;set-port-output JSR2

JMP2r

( special )

@op-bang "bang $1
	&docs '* "Bangs 20 "neighboring 20 "operands $1
	&func ( char -- )

	POP
	CHAR-DOT .head/addr LDZ2 ;data/cells ADD2 STA

JMP2r

@op-comment "comment $1
	&docs '# "Comments 20 "a 20 "line $1
	&func ( char -- )

	POP
	.head/addr LDZ2 STH2k
	( bounds )
	#00 .grid/width LDZ .head/x LDZ SUB ADD2
	STH2r INC2
	&loop
		( set lock ) DUP2 #01 ROT ROT ;data/locks ADD2 STA
		( set type if unset )
		DUP2 ;data/types ADD2 LDA ,&skip JCN
			( set type ) DUP2 LOCKED-TYPE ROT ROT ;data/types ADD2 STA
			&skip
		( stop at hash ) DUP2 ;data/cells ADD2 LDA CHAR-HASH EQU ,&end JCN
		INC2 GTH2k ,&loop JCN
	&end
	POP2 POP2

JMP2r

@op-synth "synth $1
	&docs '= "Play 20 "note 20 "with 20 "uxn 20 "synth $1
	&func ( char -- )

	POP
	.head/addr LDZ2 STH2k
	( channel ) INC2 ;get-port-right-value JSR2 [ ,&ch STR ]
	( octave ) STH2kr #0002 ADD2 ;get-port-right-value JSR2
	( note ) STH2kr #0003 ADD2 ;get-port-right-raw JSR2
	( has note ) DUP CHAR-DOT NEQ ,&has-note JCN [ POP2 POP2r JMP2r ] &has-note
	( has bang ) ;get-bang JSR2 ,&is-bang JCN [ POP2 POP2r JMP2r ] &is-bang
	( animate ) IO-TYPE STH2r ;data/types ADD2 STA
	( get note ) ;chrmid JSR2 SWP [ #0c MUL ] ADD
	( play ) .Audio0/pitch [ LIT &ch $1 ] #03 AND #40 SFT ADD DEO

JMP2r

@op-midi "midi $1
	&docs ': "Send 20 "a 20 "midi 20 "note $1
	&func ( char -- )

	POP
	.head/addr LDZ2 STH2k
	( channel ) INC2 ;get-port-right-value JSR2 [ ,&ch STR ]
	( octave ) STH2kr #0002 ADD2 ;get-port-right-value JSR2
	( note ) STH2kr #0003 ADD2 ;get-port-right-raw JSR2
	( velocity ) STH2kr #0004 ADD2 ;get-port-right-raw JSR2 [ ,&vel STR ]
	( length ) STH2kr #0005 ADD2 ;get-port-right-value JSR2

	( has note ) OVR CHAR-DOT NEQ ,&has-note JCN [ POP POP2 POP2r JMP2r ] &has-note
	( has bang ) ;get-bang JSR2 ,&is-bang JCN [ POP POP2 POP2r JMP2r ] &is-bang

	( store length ) .voices ,&ch LDR #10 SFT ADD INC STZk POP [ ,&len STR ]

	( animate ) IO-TYPE STH2r ;data/types ADD2 STA

	( get note ) ;chrmid JSR2 SWP [ #0c MUL ] ADD
	( store note ) DUP .voices ,&ch LDR #10 SFT ADD STZ
	( get velocity ) [ LIT &vel $1 ]
		DUP CHAR-DOT NEQ ,&normalize JCN
			( default to max ) POP #7f ,&continue JMP
		&normalize
		;raw-to-b128 JSR2 &continue SWP
	( get channel ) [ LIT &ch $1 ]

	( note on )
	( channel ) DUP .Console/write DEO
	( note ) OVR .Console/write DEO
	( velocity ) ROT .Console/write DEO

	.signal/midi LDZk INC SWP STZ

	( note off immediately if 0 length )
	[ LIT &len $1 ] #00 NEQ ,&done JCN
		( channel ) .Console/write DEO
		( note ) .Console/write DEO
		( off ) #00 .Console/write DEO
		JMP2r
	&done
	POP2
	
JMP2r

@op-pitch "pitch $1
	&docs '; "Send 20 "a 20 "raw 20 "pitch 20 "byte $1
	&func ( char -- )

	POP
	.head/addr LDZ2 STH2k
	( octave ) INC2 ;get-port-right-value JSR2
	( note ) STH2kr #0002 ADD2 ;get-port-right-raw JSR2
	( has note ) DUP CHAR-DOT NEQ ,&has-note JCN [ POP2 POP2r JMP2r ] &has-note
	( has bang ) ;get-bang JSR2 ,&is-bang JCN [ POP2 POP2r JMP2r ] &is-bang
	( animate ) IO-TYPE STH2r ;data/types ADD2 STA
	( get note ) ;chrmid JSR2 SWP [ #0c MUL ] ADD .Console/write DEO

JMP2r

@op-byte "byte $1
	&docs '/ "Send 20 "a 20 "raw 20 "hexadecimal 20 "byte $1
	&func ( char -- )

	POP
	.head/addr LDZ2 STH2k
	( hn ) INC2 ;get-port-right-value JSR2
	( ln ) STH2kr #0002 ADD2 ;get-port-right-value JSR2
	( has bang ) ;get-bang JSR2 ,&is-bang JCN [ POP2 POP2r JMP2r ] &is-bang
	( animate ) IO-TYPE STH2r ;data/types ADD2 STA
	#0f AND SWP #0f AND #40 SFT ADD .Console/write DEO

JMP2r

@op-self "self $1
	&docs '$ "Load 20 "orca 20 "file $1
	&func ( char -- )

	POP
	.head/addr LDZ2 STH2k
	&while
		INC2 DUP2 ;get-port-right-raw JSR2 LIT '. NEQ ,&while JCN
	POP2
	( has bang ) ;get-bang JSR2 ,&is-bang JCN [ POP2r JMP2r ] &is-bang
	.head LDZ2 INC STH2kr ;get-word JSR2 ;inject-file JSR2
	( animate ) IO-TYPE STH2r ;data/types ADD2 STA

JMP2r

( helpers )

@raw-to-b128 ( raw -- b128 )

	;chrb36 JSR2
	#00 SWP #007f MUL2 #0023 DIV2 SWP POP

JMP2r

@set-port-output ( value addr* -- )

	( set lock ) DUP2 #01 ROT ROT ;data/locks ADD2 STA
	( set type ) DUP2 OUTPUT-TYPE ROT ROT ;data/types ADD2 STA
	( set data ) ;data/cells ADD2 STA

JMP2r

@set-port-raw ( value addr* -- )

	( set lock ) DUP2 #01 ROT ROT ;data/locks ADD2 STA
	( set type ) DUP2 #00 ROT ROT ;data/types ADD2 STA
	( set data ) ;data/cells ADD2 STA

JMP2r

@get-port-left-raw ( addr* -- value )

	( set type ) DUP2 PORTEL-TYPE ROT ROT ;data/types ADD2 STA
	( get data ) ;data/cells ADD2 LDA

JMP2r

@get-port-left-value ( addr* -- value )

	,get-port-left-raw JSR ;chrb36 JSR2

JMP2r

@get-port-right-raw ( addr* -- value )

	( set lock ) DUP2 #01 ROT ROT ;data/locks ADD2 STA
	( set type ) DUP2 PORTER-TYPE ROT ROT ;data/types ADD2 STA
	( get data ) ;data/cells ADD2 LDA

JMP2r

@get-port-right-value ( addr* -- value )

	,get-port-right-raw JSR ;chrb36 JSR2

JMP2r

@ciuc ( char -- bool ) DUP #40 GTH SWP #5b LTH AND JMP2r
@ci-key ( char -- bool ) DUP #20 GTH SWP #7b LTH AND JMP2r

( standards )

@slen ( str* -- len* )

	DUP2 ,scap JSR SWP2 SUB2

JMP2r

@scap ( str* -- str-end* )

	LDAk #00 NEQ JMP JMP2r
	&while INC2 LDAk ,&while JCN

JMP2r

@sput ( str* char -- )

	ROT ROT ,scap JSR STA

JMP2r

@spop ( str* -- )

	LDAk ,&no-null JCN
		POP2 JMP2r &no-null
	#00 ROT ROT ,scap JSR #0001 SUB2 STA

JMP2r

( memory generics )

@mclr ( addr* len* -- )

	OVR2 ADD2 SWP2
	&loop
		STH2k #00 STH2r STA
		INC2 GTH2k ,&loop JCN
	POP2 POP2

JMP2r

@mcpy ( src* dst* len* -- )

	SWP2 STH2
	OVR2 ADD2 SWP2
	&loop
		LDAk STH2kr STA INC2r
		INC2 GTH2k ,&loop JCN
	POP2 POP2
	POP2r

JMP2r

( generics )

@within-rect ( x* y* rect -- flag )

	STH
	( y LTH rect.y1 ) DUP2 STHkr #02 ADD LDZ2 LTH2 ,&skip JCN
	( y GTH rect.y2 ) DUP2 STHkr #06 ADD LDZ2 GTH2 ,&skip JCN
	SWP2
	( x LTH rect.x1 ) DUP2 STHkr LDZ2 LTH2 ,&skip JCN
	( x GTH rect.x2 ) DUP2 STHkr #04 ADD LDZ2 GTH2 ,&skip JCN
	POP2 POP2 POPr
	#01
JMP2r
	&skip
	POP2 POP2 POPr
	#00

JMP2r

( orca/assets )

@untitled-txt  "untitled.orca $1

@notes
	( lc )
	00 00 00 00 00 00 00 00
	00 00
	0a 0c 01 03 05 06 08 0a
	0c 0d 0f 11 12 14 16 18
	19 1b 1d 1e 20 22 24 25
	27 29
	( uc )
	00 00 00 00 00 00 00 00
	00 00
	09 0b 00 02 04 05 07 09
	0b 0c 0e 10 11 13 15 17
	18 1a 1c 1d 1f 21 23 24
	26 28

@values ( char to b36 )
	00 00 00 00 00 00 00 00
	00 00 00 00 00 00 00 00
	00 01 02 03 04 05 06 07
	08 09 00 00 00 00 00 00
	00 0a 0b 0c 0d 0e 0f 10
	11 12 13 14 15 16 17 18
	19 1a 1b 1c 1d 1e 1f 20
	21 22 23 00 00 00 00 00
	00 0a 0b 0c 0d 0e 0f 10
	11 12 13 14 15 16 17 18
	19 1a 1b 1c 1d 1e 1f 20
	21 22 23 00 00 00 00 00

@b36clc ( b36 to char-lc )
	30 31 32 33 34 35 36 37
	38 39 61 62 63 64 65 66
	67 68 69 6a 6b 6c 6d 6e
	6f 70 71 72 73 74 75 76
	77 78 79 7a

@cell-styles
	03 ( 0 normal )
	03 ( 1 locked )
	01 ( 2 port-unlocked )
	04 ( 3 operator )
	02 ( 4 port-locked )
	08 ( 5 port-output )
	09 ( 6 selected )
	0c ( 7 io )

@cursor-icn
	80c0 e0f0 f8e0 1000 0000 0000 0000 0000
@unknown-icn
	aa55 aa55 aa55 aa55 aa55 aa55 aa55 aa55
@meter-icn
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 00ff 0000
	0000 0000 0000 0000 0000 00ff 00ff 0000
	0000 0000 0000 0000 00ff 00ff 00ff 0000
	0000 0000 0000 00ff 00ff 00ff 00ff 0000
	0000 0000 00ff 00ff 00ff 00ff 00ff 0000
	0000 00ff 00ff 00ff 00ff 00ff 00ff 0000
	00ff 00ff 00ff 00ff 00ff 00ff 00ff 0000
	007c fefe feee c682 c6ee fefe fefe 7c00

@font ( orca.816 )
	0000 0000 0000 0000 0000 0000 0000 0000
	0010 1010 1010 1010 1010 1010 0010 0000
	0028 2800 0000 0000 0000 0000 0000 0000
	0024 247e 2424 2424 2424 247e 2424 0000
	0010 3854 5050 5038 1414 1454 3810 0000
	0022 5222 0404 0808 1010 2024 4a44 0000
	0010 2828 2828 1030 504a 4a44 443a 0000
	0000 1020 0000 0000 0000 0000 0000 0000
	0008 1010 1010 1010 1010 1010 1008 0000
	0010 0808 0808 0808 0808 0808 0810 0000
	0000 0010 5454 5438 5454 5410 0000 0000
	0000 0000 0010 107c 1010 0000 0000 0000
	0000 0000 0000 0000 0000 0000 1010 0000
	0000 0000 0000 007c 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0010 0000
	0002 0202 0404 0808 1010 2020 4040 0000
	0018 2442 4242 4242 4242 4242 2418 0000
	0030 5010 1010 1010 1010 1010 1010 0000
	0018 2442 4202 0408 1020 2040 407e 0000
	0018 2442 4202 0418 0402 4242 2418 0000
	0006 0a0a 1212 2222 427e 0202 0202 0000
	007e 4040 4038 0402 0202 4242 2418 0000
	0018 2442 4240 4058 6442 4242 2418 0000
	003e 4242 0204 0404 0808 0810 1010 0000
	0018 2442 4242 2418 2442 4242 2418 0000
	0018 2442 4242 261a 0202 0202 0202 0000
	0000 0000 0000 1000 0010 0000 0000 0000
	0000 0000 0000 1000 0010 1010 2000 0000
	0000 0204 0810 2040 2010 0804 0200 0000
	0000 0000 0000 7c00 007c 0000 0000 0000
	0000 4020 1008 0402 0408 1020 4000 0000
	0018 2442 4202 0204 0408 1010 0010 0000
	0018 2442 4a56 5252 5252 524c 201c 0000
	0018 2442 4242 427e 4242 4242 4242 0000
	0078 4442 4242 4478 4442 4242 4478 0000
	0018 2442 4240 4040 4040 4242 2418 0000
	0078 4442 4242 4242 4242 4242 4478 0000
	007e 4040 4040 4078 4040 4040 407e 0000
	007e 4040 4040 4078 4040 4040 4040 0000
	0018 2442 4040 404e 4242 4242 2418 0000
	0042 4242 4242 427e 4242 4242 4242 0000
	007c 1010 1010 1010 1010 1010 107c 0000
	001e 0202 0202 0202 0202 0242 2418 0000
	0042 4244 4448 4870 4848 4444 4242 0000
	0040 4040 4040 4040 4040 4040 407e 0000
	0074 4a4a 4a4a 4a4a 4a4a 4a4a 4a4a 0000
	0062 5252 5252 5252 4a4a 4a4a 4a46 0000
	0018 2442 4242 4242 4242 4242 2418 0000
	0078 4442 4242 4478 4040 4040 4040 0000
	0018 2442 4242 4242 4242 425a 241a 0000
	0078 4442 4242 4478 4442 4242 4242 0000
	0018 2442 4240 2018 0402 4242 2418 0000
	007c 1010 1010 1010 1010 1010 1010 0000
	0042 4242 4242 4242 4242 4242 241a 0000
	0042 4242 4242 4242 2424 2424 2418 0000
	004a 4a4a 4a4a 4a4a 4a4a 4a4a 4a76 0000
	0042 4242 2424 2418 2424 2442 4242 0000
	0042 4242 4242 261a 0202 4242 2418 0000
	007e 0204 0408 0810 1020 2040 407e 0000
	0018 1010 1010 1010 1010 1010 1018 0000
	aa55 aa55 aa55 aa55 aa55 aa55 aa55 aa55
	0018 0808 0808 0808 0808 0808 0818 0000
	0010 2844 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 007e 0000
	0010 1008 0000 0000 0000 0000 0000 0000
	0000 0000 0000 003c 4202 3e42 463a 0000
	0000 0040 4040 4078 4442 4242 4478 0000
	0000 0000 0000 003c 4240 4040 423c 0000
	0000 0002 0202 021e 2242 4242 261a 0000
	0000 0000 0000 003c 4242 7c40 423c 0000
	0000 0c12 1010 1038 1010 1010 1020 0000
	0000 0000 0000 003e 4242 3c40 3c42 3c00
	0000 0040 4040 4058 6442 4242 4242 0000
	0000 0000 0010 0010 3010 1010 1038 0000
	0000 0000 0008 0008 0808 0808 0848 3000
	0000 0040 4040 4042 4244 7844 4242 0000
	0000 0010 1010 1010 1010 1010 100c 0000
	0000 0000 0000 0074 4a4a 4a4a 4a4a 0000
	0000 0000 0000 0058 6442 4242 4242 0000
	0000 0000 0000 0018 2442 4242 2418 0000
	0000 0000 0000 007c 4242 427c 4040 4000
	0000 0000 0000 003e 4242 463a 0202 0200
	0000 0000 0000 005c 6240 4040 4040 0000
	0000 0000 0000 003c 4240 3c02 423c 0000
	0000 0010 1010 1038 1010 1010 120c 0000
	0000 0000 0000 0042 4242 4242 463a 0000
	0000 0000 0000 0042 4224 2424 2418 0000
	0000 0000 0000 004a 4a4a 4a4a 4a74 0000
	0000 0000 0000 0042 4224 1824 4242 0000
	0000 0000 0000 0042 4246 3a02 0242 3c00
	0000 0000 0000 007e 0204 1820 407e 0000
	0008 1010 1010 1010 2010 1010 1008 0000
	0010 1010 1010 1010 1010 1010 1010 1000
	0010 0808 0808 0808 0408 0808 0810 0000
	007e 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 183c 3c18 0000 0000 0000
	aa55 aa55 aa55 aa55 aa55 aa55 aa55 aa55
&selector
	0000 0010 1010 1010 ee10 1010 1010 0000
	0000 006c 1010 1010 1010 1010 106c 0000
&beat
	0010 1028 2844 4482 8244 4428 2810 1000
&make
	00fe 8282 8282 8282 8282 8282 848a f400
&load
	00fe d6aa d6aa d6aa d6aa d6aa d4aa f400
&save
	0010 1092 9254 5428 d628 5454 9292 1000
&help
	0000 0000 0000 0000	8244 3800 0000 0000
	0000 0000 0000 3844 9228 1000 0000 0000

@sin-pcm
	8083 8689 8c8f 9295 989b 9ea1 a4a7 aaad
	b0b3 b6b9 bbbe c1c3 c6c9 cbce d0d2 d5d7
	d9db dee0 e2e4 e6e7 e9eb ecee f0f1 f2f4
	f5f6 f7f8 f9fa fbfb fcfd fdfe fefe fefe
	fffe fefe fefe fdfd fcfb fbfa f9f8 f7f6
	f5f4 f2f1 f0ee eceb e9e7 e6e4 e2e0 dedb
	d9d7 d5d2 d0ce cbc9 c6c3 c1be bbb9 b6b3
	b0ad aaa7 a4a1 9e9b 9895 928f 8c89 8683
	807d 7a77 7471 6e6b 6865 625f 5c59 5653
	504d 4a47 4542 3f3d 3a37 3532 302e 2b29
	2725 2220 1e1c 1a19 1715 1412 100f 0e0c
	0b0a 0908 0706 0505 0403 0302 0202 0202
	0102 0202 0202 0303 0405 0506 0708 090a
	0b0c 0e0f 1012 1415 1719 1a1c 1e20 2225
	2729 2b2e 3032 3537 3a3d 3f42 4547 4a4d
	5053 5659 5c5f 6265 686b 6e71 7477 7a7d

@tri-pcm
	8082 8486 888a 8c8e 9092 9496 989a 9c9e
	a0a2 a4a6 a8aa acae b0b2 b4b6 b8ba bcbe
	c0c2 c4c6 c8ca ccce d0d2 d4d6 d8da dcde
	e0e2 e4e6 e8ea ecee f0f2 f4f6 f8fa fcfe
	fffd fbf9 f7f5 f3f1 efed ebe9 e7e5 e3e1
	dfdd dbd9 d7d5 d3d1 cfcd cbc9 c7c5 c3c1
	bfbd bbb9 b7b5 b3b1 afad aba9 a7a5 a3a1
	9f9d 9b99 9795 9391 8f8d 8b89 8785 8381
	7f7d 7b79 7775 7371 6f6d 6b69 6765 6361
	5f5d 5b59 5755 5351 4f4d 4b49 4745 4341
	3f3d 3b39 3735 3331 2f2d 2b29 2725 2321
	1f1d 1b19 1715 1311 0f0d 0b09 0705 0301
	0103 0507 090b 0d0f 1113 1517 191b 1d1f
	2123 2527 292b 2d2f 3133 3537 393b 3d3f
	4143 4547 494b 4d4f 5153 5557 595b 5d5f
	6163 6567 696b 6d6f 7173 7577 797b 7d7f

@saw-pcm
	8282 8183 8384 8685 8888 8889 8a8b 8c8c
	8e8e 8f90 9092 9193 9494 9596 9699 9899
	9b9a 9c9c 9c9d 9ea0 a1a0 a2a2 a3a5 a4a6
	a7a7 a9a8 a9aa aaac adad aeae b0b0 b1b3
	b2b4 b5b5 b6b7 b9b8 b9bb babc bdbc bdbe
	bfc1 bfc1 c3c1 c4c5 c5c6 c6c7 c9c7 cbca
	cbcc cdcd cfcf d2d0 d2d2 d2d5 d4d5 d6d7
	d8d8 d9dc d9df dadf dce1 dde5 dce6 dceb
	cb1f 1b1e 1c21 1c21 1f23 2025 2127 2329
	2529 2829 2a2b 2b2e 2d2f 302f 3231 3234
	3334 3536 3836 3939 3a3b 3b3d 3e3d 3f40
	4042 4242 4444 4646 4748 474a 4a4b 4d4c
	4e4e 4f50 5052 5252 5554 5557 5759 5959
	5b5b 5c5d 5d5f 5e60 6160 6264 6365 6566
	6867 6969 6a6c 6c6d 6d6e 706f 7071 7174
	7475 7576 7777 797a 7a7c 7b7c 7e7d 7f7f

@sqr-pcm
	ffff ffff ffff ffff ffff ffff ffff ffff
	ffff ffff ffff ffff ffff ffff ffff ffff
	ffff ffff ffff ffff ffff ffff ffff ffff
	ffff ffff ffff ffff ffff ffff ffff ffff
	ffff ffff ffff ffff ffff ffff ffff ffff
	ffff ffff ffff ffff ffff ffff ffff ffff
	ffff ffff ffff ffff ffff ffff ffff ffff
	ffff ffff ffff ffff ffff ffff ffff ffff
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000

@data
	&cells $4000
	&locks $4000
	&types $4000