~rabbits/orca-toy

orca-toy/src/main.tal -rw-r--r-- 35.2 KiB
3aa928f2neauoire Fixed issue with numbers 22 days 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
( Orca

	space - toggle play/pause
	backspace/delete - erase
	escape - deselect
	arrow - move
	shift+arrow - scale selection
	ctrl+, - speed down
	ctrl+. - speed up
	ctrl+arrow - drag selection
	ctrl+n - new
	ctrl+s - save
	ctrl+r - rename
	ctrl+o - open
	ctrl+c - copy
	ctrl+v - paste
	ctrl+x - cut
	alt - dpad controls
	ctrl+escape - toggle play/pause

	TODO
		- Catch ports that overflow out of grid
		- Display on-screen guide )

~src/utils.tal

%DATA-CELLS { #b000 }
%DATA-LOCKS { #c000 }
%DATA-TYPES { #d000 }

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

%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 }

%IS-CHAR-KEY { STHk #20 > STHr #7b < AND }

%SET-STATE   { #01 .state/changed STZ ;draw-state JSR2 }
%RESET-STATE { #00 .state/changed STZ ;draw-state JSR2 }
%RESET-SELECTION { .selection/x1 LDZ2 ;set-selection-from JSR2 }
%RESET-INSERT { #00 .selection/insert STZ }

%GET-INDEX { #00 SWP #00 .grid/width LDZ ** ROT #00 SWP ++ } ( x y -- index )
%GET-CHAR  { #24 MOD #00 SWP ;b36clc ++ LDA } ( b36 -- char )
%GET-VALUE { #20 - #00 SWP ;values ++ LDA } ( char -- b36 )
%GET-NOTE  { DUP GET-VALUE SWP IS-UC #24 * + TOS ;lc-notes ++ LDA } ( char -- midi )
%GET-CHAR-ADDR { #20 - TOS 10** ;font ++ }

%GET-CELL  { GET-INDEX DATA-CELLS ++ LDA } ( x y -- char )
%SET-CELL  { ROT ROT GET-INDEX DATA-CELLS ++ STA } ( x y char -- )
%GET-TYPE  { GET-INDEX DATA-TYPES ++ LDA } ( x y -- type )
%SET-TYPE  { ROT ROT GET-INDEX DATA-TYPES ++ STA } ( x y type -- )
%GET-LOCK  { GET-INDEX DATA-LOCKS ++ LDA } ( x y -- type )
%SET-LOCK  { ROT ROT GET-INDEX DATA-LOCKS ++ STA } ( x y type -- )
%GET-PORT-LEFT  { DUP2 PORTEL-TYPE SET-TYPE GET-CELL GET-VALUE } ( x y -- char )
%GET-PORT-RIGHT { DUP2 PORTER-TYPE SET-TYPE DUP2 #01 SET-LOCK GET-CELL GET-VALUE } ( x y -- char )
%GET-PORT-RIGHT-RAW { DUP2 PORTER-TYPE SET-TYPE DUP2 #01 SET-LOCK GET-CELL } ( x y -- char )
%SET-PORT-OUTPUT { STH DUP2 OUTPUT-TYPE SET-TYPE DUP2 #01 SET-LOCK STHr SET-CELL } ( x y char -- )
%LOAD-CASE { GET-CELL IS-UC } ( x y -- uc )
%SAVE-CASE { DUP #60 > STHr 20* * - } ( char uc -- char )

%GET-CASE { DUP IS-UC STH }
%SET-CASE { DUP #60 > STHr 20* * - }

( devices )

|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
|b0 @DateTime   &year   $2 &month    $1 &day    $1 &hour  $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1

( variables )

|0000

@dpad $1 
	&last $1
@timer
	&beat $1 &speed $1 &playing $1 &frame $2 &seed $2
@state
	&timer $1 &blink $1 &changed $1
@path
	&name $20
@selection
	&x1 $1 &y1 $1
	&x2 $1 &y2 $1
	&insert $1
@cursor
	&x $2 &y $2
	&last $1
@grid
	&x1 $2 &y1 $2
	&x2 $2 &y2 $2
	&width $1 &height $1
@toolbar
	&x1 $2 &y1 $2
	&x2 $2 &y2 $2
@variables $24

|0100

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

	( gobal yaddr )
	AUTO-YADDR

	( synths )
	#dd .Audio0/volume DEO  #0118 .Audio0/adsr DEO2 #0100 .Audio0/length DEO2
	#ce .Audio1/volume DEO  #0334 .Audio1/adsr DEO2 #0100 .Audio1/length DEO2
	#ec .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

	( vectors )
	;untrap JSR2

	( set grid size )
	.Screen/width DEI2
		DUP2 8// NIP #03 - .grid/width STZ
		DUP2 2// .grid/width LDZ 2/ INC TOS 8** -- #0004 ++ .grid/x1 STZ2
		2// .grid/width LDZ 2/ TOS 8** ++ #0002 ++ .grid/x2 STZ2
	.Screen/height DEI2
		DUP2 10// NIP #03 - .grid/height STZ
		DUP2 2// .grid/height LDZ 2/ INC TOS 10** -- #0004 -- .grid/y1 STZ2
		2// .grid/height LDZ 2/ TOS 10** ++ #0008 -- .grid/y2 STZ2

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

	( init random )
	;prng-init JSR2
	( blank file )
	;new-file JSR2
	( theme support )
	;load-theme JSR2
	( default speed )
	#08 ;set-speed JSR2
	( start )
	.timer/playing TOGGLE

BRK

@untrap ( -- )

	( vectors )
	;on-console .Console/vector DEO2
	;on-button .Controller/vector DEO2
	;on-mouse .Mouse/vector DEO2
	;on-frame .Screen/vector DEO2

	( filepath )
	#00 .state/blink STZ
	#01 ;draw-filepath JSR2
	RELEASE-MOUSE

RTN

@trap ( -- )

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

	( filepath )
	#01 .state/blink STZ
	#01 ;draw-filepath JSR2
	RELEASE-MOUSE

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

RTN

@on-console ( -> )

	.Console/read DEI
	#11 !~ ,&no-u JCN #00ff #00 ;edit-selection JSR2 &no-u
	#12 !~ ,&no-d JCN #0001 #00 ;edit-selection JSR2 &no-d
	#13 !~ ,&no-l JCN #ff00 #00 ;edit-selection JSR2 &no-l
	#14 !~ ,&no-r JCN #0100 #00 ;edit-selection JSR2 &no-r
	DUP IS-CHAR-KEY #00 = ,&no-key JCN 
		STHk .selection LDZ2 STHr SET-CELL 
		&no-key
	POP

BRK

@on-frame ( -> )

	( paused ) 
	.timer/playing LDZ JMP BRK
	( inc beat )
	.timer/beat LDZk INC SWP STZ
	( on beat )
	.timer LDZ2 ! ,&skip JCN
		;run JSR2
		#00 .timer/beat STZ
		&skip

BRK

@on-frame-trap ( -> )

	.state/timer LDZ INC
	DUP 8MOD ,&no-blink JCN
		.state/blink LDZ #00 =
			DUP 8* INC ;draw-filepath JSR2
			.state/blink STZ
		&no-blink
	.state/timer STZ

BRK

@on-button ( -> )

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

	( normal handlers )
	DUP #f0 AND ;&arrows JCN2
	DUP #01 AND ;&control JCN2
	DUP #09 ! ,&no-ctrl-escape JCN
		.timer/playing TOGGLE
		&no-ctrl-escape
	POP

	( default )
	.Controller/key DEI
	DUP #00 ! ,&no-null JCN
		POP BRK
		&no-null
	DUP #20 ! ,&no-space JCN
		( insert mode )
		.selection/insert LDZ #00 = ,&no-space-insert JCN
			#01 #00 #00 ;edit-selection JSR2
			POP BRK
			&no-space-insert
		.timer/playing TOGGLE
		;redraw JSR2 POP BRK
		&no-space
	DUP #08 ! OVR #7f ! AND ,&no-backspace JCN
		( insert mode )
		.selection/insert LDZ #00 = ,&no-backspace-insert JCN
			#ff #00 #00 ;edit-selection JSR2
			&no-backspace-insert
		CHAR-DOT ;fill-selection JSR2
		;redraw JSR2 POP BRK
		&no-backspace
	DUP #1b ! ,&no-escape JCN
		RESET-SELECTION
		RESET-INSERT
		;load-theme JSR2 POP BRK
		&no-escape
	DUP IS-CHAR-KEY #00 = ,&no-key JCN
		.Controller/key DEI ;fill-selection JSR2
		( insert mode )
		.selection/insert LDZ #00 = ,&no-key-insert JCN
			#01 #00 #00 ;edit-selection JSR2
			&no-key-insert
		SET-STATE
		;redraw JSR2 POP BRK
		&no-key
	POP BRK

	&control ( button -- )
		POP
		.Controller/key DEI
		DUP [ LIT 'a ] ! ,&no-expand JCN
			#0000 .grid/width LDZ2 ;set-selection JSR2
			POP BRK
			&no-expand
		DUP [ LIT 'c ] ! ,&no-copy JCN
			;copy-snarf JSR2
			;redraw JSR2 POP BRK
			&no-copy
		DUP [ LIT 'v ] ! ,&no-paste JCN
			;paste-snarf JSR2 RESET-SELECTION
			;redraw JSR2 POP BRK
			&no-paste
		DUP [ LIT 'x ] ! ,&no-cut JCN
			;copy-snarf JSR2 CHAR-DOT ;fill-selection JSR2 RESET-SELECTION
			;redraw JSR2 POP BRK
			&no-cut
		DUP [ LIT 'o ] ! ,&no-open JCN
			;load-file JSR2
			POP BRK
			&no-open
		DUP [ LIT 'r ] ! ,&no-name JCN
			#00 ;draw-filepath JSR2
			;trap JSR2
			POP BRK
			&no-name
		DUP [ LIT 'R ] ! ,&no-rename JCN
			#00 ;draw-filepath JSR2
			;path/name #0020 ;mclr JSR2
			;trap JSR2
			POP BRK
			&no-rename
		DUP [ LIT 's ] ! ,&no-save JCN
			;save-file JSR2
			;redraw JSR2 POP BRK
			&no-save
		DUP [ LIT 'n ] ! ,&no-new JCN
			;new-file JSR2
			POP BRK
			&no-new
		DUP [ LIT 'i ] ! ,&no-insert JCN
			.selection/insert TOGGLE
			;redraw JSR2 POP BRK
			&no-insert
		DUP #2c ! ,&no-slow JCN
			.timer/speed LDZ #01 - ;set-speed JSR2
			;redraw JSR2 POP BRK
			&no-slow
		DUP #2e ! ,&no-fast JCN
			.timer/speed LDZ INC ;set-speed
			;redraw JSR2 POP BRK
			&no-fast
		POP
	BRK

	&arrows ( button -> )
		DUP #f0 AND #10 ! ,&no-u JCN #00ff ROT ,&move JMP &no-u
		DUP #f0 AND #20 ! ,&no-d JCN #0001 ROT ,&move JMP &no-d
		DUP #f0 AND #40 ! ,&no-l JCN #ff00 ROT ,&move JMP &no-l
		DUP #f0 AND #80 ! ,&no-r JCN #0100 ROT ,&move JMP &no-r
		POP BRK
		&move
		;edit-selection JSR2
		;redraw JSR2
	BRK

BRK

@on-button-trap ( -> )

	.Controller/key DEI
	DUP ,&continue JCN
		POP BRK
		&continue
	( backspace ) #08 !~ ,&no-edit-backspace JCN
		( clamp ) ;path/name ;slen JSR2 NIP #00 = ,&edit-end JCN
		#00 ;draw-filepath JSR2
		;path/name ;spop JSR2
		#01 ;draw-filepath JSR2
		POP BRK
		&no-edit-backspace
	( special ) DUP IS-VALID ,&no-edit-enter JCN
		;untrap JSR2
		#00 .state/blink STZ
		#01 ;draw-filepath JSR2
		POP BRK
		&no-edit-enter
	( append ) ;path/name ;slen JSR2 NIP #1f = ,&edit-end JCN
	DUP ;path/name ROT ;sput JSR2
	#01 ;draw-filepath JSR2
	&edit-end
	POP

BRK

@on-mouse-trap ( -> )

	( release trap on touch )
	.Mouse/state DEI BRK?

	;untrap JSR2

BRK

@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 ! 2* ] + .Screen/sprite DEO

	( route )
	.Mouse/y DEI2 .toolbar/y1 LDZ2 -- 10// #0000 ==
		;on-mouse-toolbar JCN2
	.Mouse/x DEI2 .Mouse/y DEI2 .grid ;within-rect JSR2
		;on-mouse-grid JCN2

	( release-record ) #0000 .Mouse/state DEO .cursor/last STZ

BRK

@on-mouse-grid ( -> )

	.Mouse/state DEI DUP .cursor/last LDZ
		DUP2 #0000 == ,&end JCN
		( on down )
		DUP2 #0100 !! ,&no-down JCN
			.Mouse/x DEI2 .grid/x1 LDZ2 -- 8// NIP
			.Mouse/y DEI2 .grid/y1 LDZ2 -- 10// NIP
				;set-selection-from JSR2
			,&end JMP
			&no-down
		( on release )
		.Mouse/x DEI2 .grid/x1 LDZ2 -- 8// NIP
		.Mouse/y DEI2 .grid/y1 LDZ2 -- 10// NIP
			;set-selection-to JSR2
	&end
	POP2
	.cursor/last STZ

BRK

@on-mouse-toolbar ( -> )

	( skip ) .Mouse/state DEI BRK?

	( left-side )
	.Mouse/x DEI2 .grid/x1 LDZ2 -- 8// NIP
	DUP #05 > ,&no-insert JCN
		.selection/insert TOGGLE
		RELEASE-MOUSE
		;redraw JSR2 POP BRK
		&no-insert
	DUP #09 > ,&no-pause JCN
		.timer/playing TOGGLE
		RELEASE-MOUSE
		;redraw JSR2 POP BRK
		&no-pause
	DUP #0d > ,&no-speed JCN
		.timer/speed LDZ #01 +
			[ .Controller/button DEI #01 = #02 * - ]
			;set-speed JSR2
		RELEASE-MOUSE
		;redraw JSR2 POP BRK
		&no-speed
	DUP #0f > OVR .grid/width LDZ SWP - #05 > #0101 !! ,&no-rename JCN
		#00 ;draw-filepath JSR2
		;trap JSR2
		&no-rename
	POP

	( right-side )
	.grid/x2 LDZ2 .Mouse/x DEI2 -- 8// NIP
	DUP ,&no-save JCN
		;save-file JSR2
		&no-save
	DUP #02 ! ,&no-load JCN
		;load-file JSR2
		&no-load
	DUP #03 ! ,&no-name JCN
		;new-file JSR2
		&no-name
	POP

	RELEASE-MOUSE

BRK

( general )

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

	DUP2 ,set-selection JSR

RTN

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

	.selection/x1 LDZ2 SWP2 ,set-selection JSR

RTN

@set-selection ( x y x2 y2 -- )

	( keep to over from )
	OVR2 OVR2 ROT MAX ROT ROT MAX SWP NIP2
	( changed ) LITr 00
	,clamp-position JSR 
		DUP2 .selection/x2 LDZ2 !! JMP INCr 
		.selection/x2 STZ2
	,clamp-position JSR 
		DUP2 .selection/x1 LDZ2 !! JMP INCr
		.selection/x1 STZ2
	STHr #00 = ,&no-redraw JCN
		;redraw JSR2
		&no-redraw

RTN

@clamp-position ( x y -- x y )

	.grid/height LDZ #01 - MIN SWP .grid/width LDZ #01 - MIN SWP

RTN

@fill-selection ( char -- )

	STH
	.selection/y2 LDZ INC .selection/y1 LDZ
	&ver
		.selection/x2 LDZ INC .selection/x1 LDZ
		&hor
			( write char ) GET-ITER STHkr SET-CELL
			INC GTHk ,&hor JCN
		POP2
		INC GTHk ,&ver JCN
	POP2
	POPr

	SET-STATE

RTN

@edit-selection ( x y mod -- )

	( set modifiers )
	DUP
		#01 AND #00 ! ;&drag STA
		#04 AND #00 ! 2* ;&scale STA
	STH2

	;&drag LDA #00 = ,&no-drag-start JCN
		;copy-snarf JSR2
		CHAR-DOT ;fill-selection JSR2
		&no-drag-start

	( y )
	STHkr #00 = ,&no-ver JCN
		( clamp ) STHkr #ff = .selection/y1 [ ,&scale LDR + ] LDZ #00 = AND ,&no-ver JCN
		.selection/y1 [ ,&scale LDR + ] LDZ STHkr +
			.selection/y1 [ ,&scale LDR + ] STZ
		,&scale LDR ,&no-ver JCN
			.selection/y2 LDZ STHkr + .selection/y2 STZ
		&no-ver POPr

	( x )
	STHkr #00 = ,&no-hor JCN
		( clamp ) STHkr #ff = .selection/x1 [ ,&scale LDR + ] LDZ #00 = AND ,&no-hor JCN
		.selection/x1 [ ,&scale LDR + ] LDZ STHkr +
			.selection/x1 [ ,&scale LDR + ] STZ
		,&scale LDR ,&no-hor JCN
			.selection/x2 LDZ STHkr + .selection/x2 STZ
		&no-hor POPr

	( clamp result )
	.selection/x1 LDZ2 .selection/x2 LDZ2 ;set-selection JSR2

	,&drag LDR #00 = ,&no-drag-end JCN
		;paste-snarf JSR2
		&no-drag-end

RTN
	&drag $1
	&scale $1

@set-speed ( speed -- )

	( clamp )
	#1f AND [ #04 MAX ] .timer/speed STZ
	( reset timer )
	#00 .timer/beat STZ

RTN

( special )

@dpad-input ( -> )

	&start ( button* -> )
		POP
		#20 .dpad STZ
		;&save JMP2
	&end ( button* -> )
		POP
		.dpad LDZ #7f > ,&save JCN
		.dpad LDZ #20 = ,&erase JCN
		.selection LDZ2 .dpad LDZ SET-CELL
		SET-STATE
		RESET-SELECTION
		#00 .dpad STZ
		,&save JMP
	&add ( button* -> )
		#02 ! ,&save JCN
		DUP #04 SFT .dpad LDZ + #7f AND .dpad STZ
		,&save JMP
	&erase ( -> )
		.selection LDZ2 CHAR-DOT SET-CELL SET-STATE
		#00 .dpad STZ
	&save ( -> )
		.dpad/last STZ
		;redraw JSR2
	BRK

BRK

@run-char ( x y char -- )

	( skip dot )
	DUP CHAR-DOT NEQ ,&not-dot JCN
		POP POP2 RTN
		&not-dot

	( skip locked )
	ROT ROT DUP2 GET-LOCK #00 = ,&not-locked JCN
		POP POP2 RTN
		&not-locked
	ROT

	( skip numbers )
	DUP #30 < ,&no-num JCN
	DUP #39 > ,&no-num JCN
		POP POP2 RTN
		&no-num

	( lowercase )
	DUP #61 < ,&no-lc JCN
	DUP #7a > ,&no-lc JCN
		STH DUP2 ;get-bang JSR2 ,&run JCN
		POPr POP2 RTN
		&no-lc

	( uppercase )
	DUP #41 < ,&no-uc JCN
	DUP #5a > ,&no-uc JCN
		STH DUP2 OPERATOR-TYPE SET-TYPE &run STHr
		DUP GET-VALUE #0a - 2* TOS ;operations ++ LDA2 JMP2
		&no-uc

	( special )
	CHAR-BANG =~ ;op-bang JCN2
	CHAR-HASH =~ ;op-comment JCN2
	CHAR-SEMI =~ ;op-note JCN2
	CHAR-EQUAL =~ ;op-synth JCN2
	CHAR-COLON =~ ;op-midi JCN2
	CHAR-SLASH =~ ;op-byte JCN2

	( erase )
	POP CHAR-DOT SET-CELL

RTN

@run ( -- )

	( clear lock/types )
	#00 .grid/height LDZ #00 .grid/width LDZ ** STH2
		DATA-LOCKS STH2kr ;mclr JSR2
		DATA-TYPES STH2r ;mclr JSR2
	( do )
	.grid/height LDZ #00
	&ver
		.grid/width LDZ #00
		&hor
			GET-ITER
			DUP2 GET-CELL ;run-char JSR2
			INC GTHk ,&hor JCN
		POP2
		INC GTHk ,&ver JCN
	POP2
	( timer )
	.timer/frame LDZ2k INC2 ROT STZ2
	( redraw )
	;redraw JSR2

RTN

( operations )

@get-bang ( x y -- bang )

	DUP2 INC GET-CELL CHAR-BANG = ,&bang JCN
	DUP2 #01 - GET-CELL CHAR-BANG = ,&bang JCN
	DUP2 SWP INC SWP GET-CELL CHAR-BANG = ,&bang JCN
	DUP2 SWP #01 - SWP GET-CELL CHAR-BANG = ,&bang JCN
	POP2 #00 RTN
	&bang
		POP2 #01

RTN

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

	( set type ) DUP2 PORTEL-TYPE ROT ROT DATA-TYPES ++ STA
	( get data ) DATA-CELLS ++ LDA

RTN

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

	( set type ) DUP2 PORTEL-TYPE ROT ROT DATA-TYPES ++ STA
	( get data ) DATA-CELLS ++ LDA GET-VALUE

RTN

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

	( set lock ) DUP2 #01 ROT ROT DATA-LOCKS ++ STA
	( set type ) DUP2 PORTER-TYPE ROT ROT DATA-TYPES ++ STA
	( get data ) DATA-CELLS ++ LDA GET-VALUE

RTN

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

	( set lock ) DUP2 #01 ROT ROT DATA-LOCKS ++ STA
	( set type ) DUP2 OUTPUT-TYPE ROT ROT DATA-TYPES ++ STA
	( set data ) DATA-CELLS ++ STA

RTN

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

	( set lock ) DUP2 #01 ROT ROT DATA-LOCKS ++ STA
	( set type ) DUP2 #00 ROT ROT DATA-TYPES ++ STA
	( set data ) DATA-CELLS ++ STA

RTN

( operators )

@operations
	: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-a ( x y char -- )

	POP
	GET-INDEX STH2k
	( get a ) DEC2 ;get-port-left JSR2
	( get b ) STH2kr INC2
		( set lock ) DUP2 #01 ROT ROT DATA-LOCKS ++ STA
		( set type ) DUP2 PORTER-TYPE ROT ROT DATA-TYPES ++ STA
		( get data ) DATA-CELLS ++ LDA GET-CASE GET-VALUE
	( a b + ) +
	( apply case ) GET-CHAR SET-CASE
	( output ) STH2r #00 .grid/width LDZ ++ ;set-port-output JSR2

RTN

@op-b ( x y char -- )

	POP
	GET-INDEX STH2k
	( get a ) DEC2 ;get-port-left JSR2
	( get b ) STH2kr INC2
		( set lock ) DUP2 #01 ROT ROT DATA-LOCKS ++ STA
		( set type ) DUP2 PORTER-TYPE ROT ROT DATA-TYPES ++ STA
		( get data ) DATA-CELLS ++ LDA GET-CASE GET-VALUE
	( a b -abs ) - DUP #80 < ,&bounce JCN #24 SWP - &bounce
	( apply case ) GET-CHAR SET-CASE
	( output ) STH2r #00 .grid/width LDZ ++ ;set-port-output JSR2

RTN

@op-c ( x y char -- )

	POP
	GET-INDEX STH2k
	( get rate ) DEC2 ;get-port-left JSR2 1MIN
	( get mod ) STH2kr INC2
		( set lock ) DUP2 #01 ROT ROT DATA-LOCKS ++ STA
		( set type ) DUP2 PORTER-TYPE ROT ROT DATA-TYPES ++ STA
		( get data ) DATA-CELLS ++ LDA GET-CASE GET-VALUE 1MIN
	( timer rate / mod % ) TOS ROT TOS .timer/frame LDZ2 SWP2 // SWP2 MOD2 NIP
	( apply case ) GET-CHAR SET-CASE
	( output ) STH2r #00 .grid/width LDZ ++ ;set-port-output JSR2

RTN

@op-d ( x y char -- )

	POP
	GET-INDEX STH2k
	( get rate ) DEC2 ;get-port-left JSR2 1MIN
	( get mod ) STH2kr INC2 ;get-port-right JSR2 1MIN
	( rate mod * 0 = ) * TOS .timer/frame LDZ2 SWP2 MOD2 #0000 ==
	( bang on equal ) #fc * CHAR-DOT +
	( output ) STH2r #00 .grid/width LDZ ++ ;set-port-output JSR2

RTN

@op-e ( x y char -- )

	STH
	OVR STH GET-INDEX STHr
	( hit edge ) .grid/width LDZ #01 - = ,&collide JCN
	( hit cell ) INC2k DATA-CELLS ++ LDA CHAR-DOT ! ,&collide JCN
	( write new ) STH2k ROTr STHr STH2r INC2 ;set-port-raw JSR2
	( erase old ) STH2 CHAR-DOT STH2r ;set-port-raw JSR2
	RTN
	&collide
		POPr
		STH2 CHAR-BANG STH2r ;set-port-output JSR2

RTN

@op-f ( x y char -- )

	POP
	GET-INDEX STH2k
	( get rate ) DEC2 ;get-port-left JSR2
	( get mod ) STH2kr INC2 ;get-port-right JSR2
	( bang on equal ) = [ #fc * CHAR-DOT +  ]
	( output ) STH2r #00 .grid/width LDZ ++ ;set-port-output JSR2

RTN

@op-g ( x y char -- )

	POP
	( get x ) DUP2 [ SWP #03 - SWP ] GET-PORT-LEFT STH
	( get y ) DUP2 [ SWP #02 - SWP ] GET-PORT-LEFT STH
	( get len ) DUP2 [ SWP #01 - SWP ] GET-PORT-LEFT 1MIN
	#00
	&loop
		( x+i+1,y ) STHk OVR2 STHr ROT + INC SWP
		DUP2 GET-PORT-RIGHT-RAW STH
		( unstash x,y ) ROTr STHkr ROTr STHkr ROTr
		( x0+x1-1,y0+y1+1 ) ROT + STH + #01 - STHr INC
		STHr SET-PORT-OUTPUT
		INC GTHk ;&loop JCN2
	POP2
	( clean ) POP2 POP2r

RTN

@op-h ( x y char -- )

	POP INC
	GET-INDEX
	( set lock ) DUP2 #01 ROT ROT DATA-LOCKS ++ STA
	( set type ) PORTER-TYPE ROT ROT DATA-TYPES ++ STA

RTN

@op-i ( x y char -- )

	POP
	GET-INDEX STH2k
	( get rate ) DEC2 ;get-port-left JSR2 1MIN
	( get output ) STH2kr #00 .grid/width LDZ ++ DATA-CELLS ++ LDA GET-VALUE
	( rate output + ) +
	( get mod ) STH2kr INC2
		( set lock ) DUP2 #01 ROT ROT DATA-LOCKS ++ STA
		( set type ) DUP2 PORTER-TYPE ROT ROT DATA-TYPES ++ STA
		( get data ) DATA-CELLS ++ LDA GET-CASE GET-VALUE 1MIN
	( result % ) MOD
	( apply case ) GET-CHAR SET-CASE
	( output ) STH2r #00 .grid/width LDZ ++ ;set-port-output JSR2

RTN

@op-j ( x y char -- )

	POP ( TODO: Wiring )
	GET-INDEX STH2k
	( get above ) #00 .grid/width LDZ -- ;get-port-raw JSR2
	( set below ) STH2r #00 .grid/width LDZ ++ ;set-port-output JSR2

RTN

@op-k ( x y char -- )

	POP
	( get len ) DUP2 [ SWP #01 - SWP ] GET-PORT-LEFT 1MIN
	#00
	&loop
		( x+i+1,y ) STHk OVR2 STHr ROT + INC SWP
		( get variable ) DUP2 GET-PORT-RIGHT
		DUP #00 = ,&skip JCN
			.variables + LDZ STH
			( incr y ) INC
			DUP2 STHkr SET-PORT-OUTPUT
			STHr
			&skip
		POP POP2
		INC GTHk ;&loop JCN2
	POP2
	POP2

RTN

@op-l ( x y char -- )

	POP
	GET-INDEX STH2k
	( get rate ) DEC2 ;get-port-left JSR2
	( get mod ) STH2kr INC2
		( set lock ) DUP2 #01 ROT ROT DATA-LOCKS ++ STA
		( set type ) DUP2 PORTER-TYPE ROT ROT DATA-TYPES ++ STA
		( get data ) DATA-CELLS ++ LDA GET-CASE GET-VALUE
	( min ) LTHk JMP SWP POP
	( apply case ) GET-CHAR SET-CASE
	( output ) STH2r #00 .grid/width LDZ ++ ;set-port-output JSR2

RTN

@op-m ( x y char -- )

	POP
	GET-INDEX STH2k
	( get rate ) DEC2 ;get-port-left JSR2
	( get mod ) STH2kr INC2
		( set lock ) DUP2 #01 ROT ROT DATA-LOCKS ++ STA
		( set type ) DUP2 PORTER-TYPE ROT ROT DATA-TYPES ++ STA
		( get data ) DATA-CELLS ++ LDA GET-CASE GET-VALUE
	( mul ) *
	( apply case ) GET-CHAR SET-CASE
	( output ) STH2r #00 .grid/width LDZ ++ ;set-port-output JSR2

RTN

@op-n ( x y char -- )

	STH
	STHk GET-INDEX STHr
	( hit edge ) #00 = ,&collide JCN
	( hit cell ) DUP2 #00 .grid/width LDZ -- DATA-CELLS ++ LDA CHAR-DOT ! ,&collide JCN
	( write new ) STH2k ROTr STHr STH2r #00 .grid/width LDZ -- ;set-port-raw JSR2
	( erase old ) STH2 CHAR-DOT STH2r ;set-port-raw JSR2
	RTN
	&collide
		POPr
		STH2 CHAR-BANG STH2r ;set-port-output JSR2

RTN

@op-o ( x y char -- )

	POP
	( get x ) DUP2 [ SWP #02 - SWP ] GET-PORT-LEFT STH
	( get y ) DUP2 [ SWP #01 - SWP ] GET-PORT-LEFT STH
	( get value ) DUP2 [ SWP INC SWP ] STH2r ++ GET-PORT-RIGHT-RAW STH
	( incr y ) INC
	STHr SET-PORT-OUTPUT

RTN

@op-p ( x y char -- )

	POP
	( get key ) DUP2 [ SWP #02 - SWP ] GET-PORT-LEFT STH
	( get len ) DUP2 [ SWP #01 - SWP ] GET-PORT-LEFT 1MIN STH
	( get input ) DUP2 [ SWP INC SWP ] GET-PORT-RIGHT-RAW STH
	OVRr STHr #00
	&loop
		( x+i+1,y ) STHk OVR2 STHr ROT + SWP INC
		( lock ) DUP2 #01 SET-LOCK
		( type ) LOCKED-TYPE SET-TYPE
		INC GTHk ,&loop JCN
	POP2
	( write )
	( x-offset ) ROTr ROTr STH2r MOD
	( x,y ) SWP INC STH + STHr
	STHr SET-PORT-OUTPUT

RTN

@op-q ( x y char -- )

	POP
	( get x ) DUP2 [ SWP #03 - SWP ] GET-PORT-LEFT STH
	( get y ) DUP2 [ SWP #02 - SWP ] GET-PORT-LEFT STH
	( get len ) DUP2 [ SWP #01 - SWP ] GET-PORT-LEFT 1MIN
	#00
	&loop
		( x+i+1,y ) STHk OVR2 STHr ROT + INC SWP
		( x+x0,y+y0 ) STHkr + SWP OVRr STHr + SWP
		GET-PORT-RIGHT-RAW STH
		( x-i-len ) DUP2  - STH
		( x,y ) OVR2 SWP STHr - INC SWP INC
		STHr SET-PORT-OUTPUT
		 INC
		GTHk ;&loop JCN2
	POP2
	( clean ) POP2 POP2r

RTN

@op-r ( x y char -- )

	POP
	( get case ) DUP2 [ SWP INC SWP ] LOAD-CASE STH
	( get min ) DUP2 [ SWP #01 - SWP ] GET-PORT-LEFT STH
	( get max ) DUP2 [ SWP INC SWP ] GET-PORT-RIGHT 1MIN STH
	( unstash min,max ) STH2r
	( real max ) LTHk JMP SWP
	( no equal ) NEQk #04 JCN [ #01 - SWP ]
	( stash min,max ) STH2
	( incr y ) INC
	( get key ) ;prng JSR2 +
	( key % max + min ) STH2kr SWP - MOD POPr STHr +
	GET-CHAR SAVE-CASE SET-PORT-OUTPUT

RTN

@op-s ( x y char -- )

	STH
	STHk GET-INDEX STHr
	( hit edge ) .grid/height LDZ #01 - = ,&collide JCN
	( hit cell ) DUP2 #00 .grid/width LDZ ++ DATA-CELLS ++ LDA CHAR-DOT ! ,&collide JCN
	( write new ) STH2k ROTr STHr STH2r #00 .grid/width LDZ ++ ;set-port-raw JSR2
	( erase old ) STH2 CHAR-DOT STH2r ;set-port-raw JSR2
	RTN
	&collide
		POPr
		STH2 CHAR-BANG STH2r ;set-port-output JSR2

RTN

@op-t ( x y char -- )

	POP
	( get key ) DUP2 [ SWP #02 - SWP ] GET-PORT-LEFT STH
	( get len ) DUP2 [ SWP #01 - SWP ] GET-PORT-LEFT 1MIN STHk
	#00
	&loop
		( x+i+1,y ) STHk OVR2 STHr ROT + INC SWP
		( lock ) DUP2 #01 SET-LOCK
		( type ) LOCKED-TYPE SET-TYPE
		INC GTHk ,&loop JCN
	POP2
	( read )
	( x-offset ) DUP2 SWP INC SWP STH2r MOD ROT + SWP
	( x,y ) GET-PORT-RIGHT-RAW STH INC
	STHr SET-PORT-OUTPUT

RTN

@op-u ( x y char -- )

	POP
	( get step ) DUP2 [ SWP #01 - SWP ] GET-PORT-LEFT STH
	( get max ) DUP2 [ SWP INC SWP ] GET-PORT-RIGHT 1MIN STH
	( incr y ) INC
	( frame + max - 1 ) .timer/frame LDZ2 STHkr TOS ++ #0001 --
	( * step ) OVRr STHr TOS **
	( % max ) STHkr TOS MOD2
	( + step ) SWPr STHr TOS ++
	( bucket >= max ) STHr TOS << #01 !
	( bang if equal ) #fc * CHAR-DOT +
	SET-PORT-OUTPUT

RTN

@op-v ( x y char -- )

	POP
	( get write ) DUP2 [ SWP #01 - SWP ] GET-PORT-LEFT STH
	( get read ) DUP2 [ SWP INC SWP ] GET-PORT-RIGHT-RAW STH
	OVRr STHr ,&write JCN
	( incr y ) INC
	( load ) STHr GET-VALUE .variables + LDZ SET-PORT-OUTPUT
	POPr
	RTN
	&write
	( save ) STH2r SWP .variables + STZ POP2

RTN

@op-w ( x y char -- )

	STH
	OVR STH GET-INDEX STHr
	( hit edge ) #00 = ,&collide JCN
	( hit cell ) DUP2 #0001 -- DATA-CELLS ++ LDA CHAR-DOT ! ,&collide JCN
	( write new ) STH2k ROTr STHr STH2r #0001 -- ;set-port-raw JSR2
	( erase old ) STH2 CHAR-DOT STH2r ;set-port-raw JSR2
	RTN
	&collide
		POPr
		STH2 CHAR-BANG STH2r ;set-port-output JSR2

RTN

@op-x ( x y char -- )

	POP
	( get value ) DUP2 [ SWP INC SWP ] GET-PORT-RIGHT-RAW STH
	( get x ) DUP2 [ SWP #02 - SWP ] GET-PORT-LEFT STH
	( get y ) DUP2 [ SWP #01 - SWP ] GET-PORT-LEFT STH
	( incr y ) STH2r INC ++ STHr
	SET-PORT-OUTPUT

RTN

@op-y ( x y char -- )

	POP ( TODO: Wiring )
	GET-INDEX STH2k
	( get left ) DEC2 ;get-port-raw JSR2
	( set right ) STH2r INC2 ;set-port-output JSR2

RTN

@op-z ( x y char -- )

	POP
	( get case ) DUP2 [ SWP INC SWP ] LOAD-CASE STH
	( get rate ) DUP2 [ SWP #01 - SWP ] GET-PORT-LEFT STH
	( get target ) DUP2 [ SWP INC SWP ] GET-PORT-RIGHT STH
	( incr y ) INC
	( get val ) DUP2 GET-CELL GET-VALUE STH
	EQUkr STHr ,&end JCN
	LTHkr STHr #00 ! ,&no-below JCN
		( val rate + ) STHr OVRr STH SWPr ADDr
		( clamp ) GTHkr STHr SWPr?
		,&end JMP
		&no-below
	GTHkr STHr #00 ! ,&no-above JCN
		( val rate - ) STHr OVRr STH SWPr SUBr
		( clamp ) LTHkr STHr SWPr?
		&no-above
	&end
	STHr GET-CHAR POP2r SAVE-CASE SET-PORT-OUTPUT

RTN

@op-bang ( x y char -- )

	POP
	CHAR-DOT SET-CELL

RTN

@op-comment ( x y char -- )

	POP
	STH
	.grid/width LDZ SWP INC
	&loop
		DUP STHkr
		( lock )  DUP2 #01 SET-LOCK
		( close ) DUP2 GET-CELL CHAR-HASH = ,&end JCN
		( type )  LOCKED-TYPE SET-TYPE
		INC GTHk ,&loop JCN
	POP2 POPr
	RTN
	&end
	POP2 POP2 POPr

RTN

@op-synth ( x y char -- )

	POP
	( get channel ) DUP2 [ SWP INC SWP ] GET-PORT-RIGHT STH
	( get octave ) DUP2 [ SWP #02 + SWP ] GET-PORT-RIGHT [ #0c * ] STH
	( get note ) DUP2 [ SWP #03 + SWP ] GET-PORT-RIGHT-RAW
		( req note ) DUP CHAR-DOT ! ,&is-active JCN [ POP POP2 POP2r RTN ] &is-active GET-NOTE STH ADDr
	( req bang ) DUP2 ;get-bang JSR2 ,&is-bang [ JCN POP2 POP2r RTN ] &is-bang
	STHr .Audio0/pitch STHr 4MOD 10* + DEO
	IO-TYPE SET-TYPE

RTN

@op-midi ( x y char -- )

	POP
	( get channel ) DUP2 [ SWP INC SWP ] GET-PORT-RIGHT STH
	( get octave ) DUP2 [ SWP #02 + SWP ] GET-PORT-RIGHT [ #0c * ] STH
	( get note ) DUP2 [ SWP #03 + SWP ] GET-PORT-RIGHT-RAW
		( req note ) DUP CHAR-DOT ! ,&is-active JCN [ POP POP2 POP2r RTN ] &is-active GET-NOTE STH
	( req bang ) DUP2 ;get-bang JSR2 ,&is-bang JCN [ POP2 POP2r POPr RTN ] &is-bang
	IO-TYPE SET-TYPE
	ADDr
	( note on ) OVRr STHr .Console/write DEO STHkr .Console/write DEO #7f .Console/write DEO
	( note off ) OVRr STHr .Console/write DEO STHkr .Console/write DEO #00 .Console/write DEO
	POP2r

RTN

@op-note ( x y char -- )

	POP
	DUP2
		;get-bang JSR2 STH
		GET-INDEX STH2k
	( get a ) INC2 ;get-port-right JSR2
	( get b ) STH2kr INC2 INC2 ;get-port-right JSR2
	( req bang ) ROTr STHr ,&is-bang JCN [ POP2 POP2r RTN ] &is-bang
	( set type ) STH2r IO-TYPE ROT ROT DATA-TYPES ++ STA
	( send note ) SWP #0c * + .Console/write DEO

RTN

@op-byte ( x y char -- )

	POP
	DUP2
		;get-bang JSR2 STH
		GET-INDEX STH2k
	( get a ) INC2 ;get-port-right JSR2
	( get b ) STH2kr INC2 INC2 ;get-port-right JSR2
	( req bang ) ROTr STHr ,&is-bang JCN [ POP2 POP2r RTN ] &is-bang
	( set type ) STH2r IO-TYPE ROT ROT DATA-TYPES ++ STA
	( write byte ) #0f AND SWP #0f AND #40 SFT + .Console/write DEO

RTN

@draw-toolbar ( -- )

	( skip on small screens )
	.toolbar/y1 LDZ2 .Screen/height DEI2 << RTN?

	.toolbar/y1 LDZ2 .Screen/y DEO2

	( Positions )
	.grid/x1 LDZ2 .Screen/x DEO2
	.selection/x1 LDZ #01 ;draw-byte JSR2
	.grid/x1 LDZ2 #0010 ++ .Screen/x DEO2
	.selection/y1 LDZ #01 ;draw-byte JSR2

	.grid/x1 LDZ2 #0020 ++ .Screen/x DEO2
	;font/selector .selection/insert LDZ TOS 10** ++
		#02 .selection/x1 LDZ2 .selection/x2 LDZ2 == +
		;draw-sprite JSR2

	( Frame )
	.grid/x1 LDZ2 #0030 ++ .Screen/x DEO2
	.timer/frame INC LDZ #01 ;draw-byte JSR2

	.grid/x1 LDZ2 #0040 ++ .Screen/x DEO2
	;font/beat
		#03 .timer/frame INC LDZ #07 AND #00 = -
		;draw-sprite JSR2

	( d-pad )
	.dpad LDZ DUP #20 < ,&no-dpad JCN
		.grid/x1 LDZ2 #0050 ++ STH2k .Screen/x DEO2
		DUP #04 ;draw-byte JSR2
		STH2kr #0010 ++ .Screen/x DEO2
		;font #00 ;draw-sprite JSR2
		STH2r #0018 ++ .Screen/x DEO2
		.dpad LDZ #01 ;draw-char JSR2
		,&continue JMP
		&no-dpad
	( Speed )
	.grid/x1 LDZ2 #0050 ++ .Screen/x DEO2
	.timer/speed LDZ #01 ;draw-byte JSR2
	.grid/x1 LDZ2 #0060 ++ .Screen/x DEO2
	( t ) LIT 't #03 ;draw-char JSR2
	.grid/x1 LDZ2 #0068 ++ .Screen/x DEO2
	( h ) LIT 'h #03 ;draw-char JSR2
	&continue
	POP

	( File )
	.grid/x2 LDZ2 STH2k #0018 -- .Screen/x DEO2
	;font/load #01 ;draw-sprite JSR2
	STH2r #0020 -- .Screen/x DEO2
	;font/make #01 ;draw-sprite JSR2

	( filepath )
	#03 ;draw-filepath JSR2

RTN

@draw-state ( -- )

	.toolbar/x2 LDZ2 #0008 -- .Screen/x DEO2
	.toolbar/y1 LDZ2 .Screen/y DEO2
	;font/save #01 .state/changed LDZ + ;draw-sprite JSR2

RTN

@draw-filepath ( color -- )

	STH
	.toolbar/y1 LDZ2 .Screen/y DEO2
	.toolbar/x1 LDZ2 #0078 ++ .Screen/x DEO2
	;path/name STHr ;draw-str JSR2

RTN

@redraw ( -- )

	.grid/height LDZ #00
	&ver
		( x ) .grid/x1 LDZ2 .Screen/x DEO2
		( y ) DUP #00 SWP 10** [ .grid/y1 LDZ2 ++ ] .Screen/y DEO2
		.grid/width LDZ #00
		&hor
			GET-ITER STH2k ,get-char JSR STH2r ,get-color JSR ;draw-char JSR2
			INC GTHk ,&hor JCN
		POP2
		INC GTHk ,&ver JCN
	POP2
	;draw-toolbar JSR2

RTN

@get-color ( x y -- type )

	STH2k GET-TYPE #06 SWP
	STH2r ,is-selected JSR JMP SWP POP
		TOS ;cell-styles ++ LDA

RTN

@get-char ( x y -- char )

	DUP2 GET-CELL
	DUP CHAR-DOT ! ,&no-bar JCN
		POP
		DUP2 8MOD SWP 10MOD #0000 == ,&cross JCN
		DUP2 2MOD SWP 4MOD #0000 == ,&dot JCN
		DUP2 ,is-selected JSR ,&dot JCN
		DUP2 GET-TYPE ,&dot JCN
		POP2 #20 RTN
	&no-bar
	ROT ROT POP2

RTN
	&cross POP2 LIT '+ RTN
	&dot POP2 LIT '. RTN

@is-selected ( x y -- bool )

	OVR .selection/x1 LDZ < ,&end JCN
	OVR .selection/x2 LDZ > ,&end JCN
	DUP .selection/y1 LDZ < ,&end JCN
	DUP .selection/y2 LDZ > ,&end JCN
		POP2 #01 RTN
	&end
	POP2 #00

RTN

@draw-str ( str* color -- )

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

RTN

@draw-byte ( byte color -- )

	STH
	DUP #04 SFT GET-CHAR STHkr ,draw-char JSR
	#0f AND GET-CHAR STHr ,draw-char JSR

RTN

@draw-char ( char color -- )

	STH #20 - TOS 10** ;font ++ STHr ,draw-sprite JSR

RTN

@draw-sprite ( addr* color -- )
	
	STH .Screen/addr DEO2
	.Screen/y DEI2
	STHr .Screen/sprite DEOk DEO
	.Screen/y DEO2
	.Screen/x DEI2k #0008 ++ ROT DEO2

RTN

( file )

@new-file ( default* -- )

	#00 ;draw-filepath JSR2 
	( clear variables )
	;variables #0024 ;mclr JSR2
	( clear cells/locks/types )
	#00 .grid/height LDZ #00 .grid/width LDZ ** STH2
		DATA-CELLS STH2kr ;mclr JSR2
		DATA-LOCKS STH2kr ;mclr JSR2
		DATA-TYPES STH2r ;mclr JSR2
	( rename to untitled.txt )
	;path/name #0020 ;mclr JSR2
	;untitled-txt ;path/name #000d ;mcpy JSR2
	;redraw JSR2
	RESET-STATE

RTN

@load-file ( -- )

	;path/name .File/name DEO2
	#0001 .File/length DEO2
	( x,y ) LIT2r 0000
	&stream
		;&b .File/read DEO2
		( write )
		;&b LDA IS-CHAR-KEY #00 = ,&invalid JCN
			OVRr STHr STHkr ;&b LDA SET-CELL
			&invalid
		( incr-x ) SWPr INCr SWPr
		( incr-y )
		;&b LDA #0a ! ,&no-lb JCN
			INCr NIPr LITr 00 SWPr
			&no-lb
		.File/success DEI2 #0000 !! ,&stream JCN
	POP2r
	;redraw JSR2
	RESET-STATE

RTN
	&b $1

@save-file ( -- )

	;path/name .File/name DEO2
	#0001 .File/length DEO2
	.grid/height LDZ #00
	&ver
		.grid/width LDZ #00
		&hor
			GET-ITER GET-INDEX DATA-CELLS ++ .File/write DEO2
			INC GTHk ,&hor JCN
		POP2
		( linebreak ) ;&lb .File/write DEO2
		INC GTHk ,&ver JCN
	POP2

RTN
	&lb 0a

( random )

@prng-init ( -- )

	( 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

RTN

@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

RTN

( theme )

@theme-txt ".theme $1

@load-theme ( -- )

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

RTN

( snarf )

@snarf-txt ".snarf $1

@copy-snarf ( -- )

	;snarf-txt .File/name DEO2
	#0001 .File/length DEO2
	.selection/y2 LDZ INC .selection/y1 LDZ
	&ver
		.selection/x2 LDZ INC .selection/x1 LDZ
		&hor
			GET-ITER GET-INDEX DATA-CELLS ++ .File/write DEO2
			INC GTHk ,&hor JCN
		POP2
		( linebreak ) ;&lb .File/write DEO2
		INC GTHk ,&ver JCN
	POP2

RTN
	&lb 0a

@paste-snarf ( -- )

	;snarf-txt .File/name DEO2
	#0001 .File/length DEO2
	( x,y ) LIT2r 0000
	&stream
		;&b .File/read DEO2
		( write )
		;&b LDA IS-CHAR-KEY #00 = ,&invalid JCN
			.selection/x1 LDZ OVRr STHr +
			.selection/y1 LDZ STHkr +
				;&b LDA SET-CELL
			&invalid
		( incr-x ) SWPr INCr SWPr
		( incr-y )
		;&b LDA #0a ! ,&no-lb JCN
			INCr NIPr LITr 00 SWPr
			&no-lb
		.File/success DEI2 #0000 !! ,&stream JCN
	POP2r

RTN
	&b $1

( string generics )

@slen ( str* -- len* )

	DUP2 ,scap JSR SWP2 --

RTN

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

	LDAk #00 ! JMP RTN
	&while INC2 LDAk ,&while JCN

RTN

@sput ( str* char -- )

	ROT ROT ,scap JSR STA

RTN

@spop ( str* -- )

	( clamp ) LDAk #00 ! JMP RTN
	#00 ROT ROT ,scap JSR #0001 -- STA

RTN

( memory generics )

@mclr ( addr* len* -- )

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

RTN

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

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

RTN

( generics )

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

	STH
	( y < rect.y1 ) DUP2 STHkr #02 + LDZ2 << ,&skip JCN
	( y > rect.y2 ) DUP2 STHkr #06 + LDZ2 >> ,&skip JCN
	SWP2
	( x < rect.x1 ) DUP2 STHkr LDZ2 << ,&skip JCN
	( x > rect.x2 ) DUP2 STHkr #04 + LDZ2 >> ,&skip JCN
	POP2 POP2 POPr
	#01
RTN
	&skip
	POP2 POP2 POPr
	#00

RTN

@print-hex ( value* -- )
	
	SWP ,&byte JSR 
	&byte ( byte -- )
		STHk #04 SFT ,&parse JSR #18 DEO
		STHr #0f AND ,&parse JSR #18 DEO
	RTN
	&parse ( byte -- char ) DUP #09 GTH ,&above JCN #30 + RTN 
	&above #57 + RTN

RTN

@untitled-txt  "untitled.orca $1

@lc-notes
	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-notes
	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

~src/assets.tal