~rabbits/dexe

ref: f1cf6890b06ed5f511993ed6c957c2702ab032ab dexe/src/main.tal -rw-r--r-- 16.9 KiB
f1cf6890neauoire Added bitpad 2 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
(
	app/dexe : hex editor
	
	arrows     - move selection
	enter      - insert byte
	backspace  - delete byte
	left-click - select bytes

	TODO:
		copy/paste
		scrollbar
)

%+  { ADD } %-   { SUB }  %*  { MUL } %/   { DIV }
%<  { LTH } %>   { GTH }  %=  { EQU } %!   { NEQ }
%++ { ADD2 } %-- { SUB2 } %** { MUL2 } %// { DIV2 }
%<< { LTH2 } %>> { GTH2 } %== { EQU2 } %!! { NEQ2 }

%DEBUG  { ;print-hex/byte JSR2 #0a .Console/write DEO }
%DEBUG2 { ;print-hex/short JSR2 #0a .Console/write DEO }

%RTN  { JMP2r }
%SWP2? { #01 JCN SWP }
%BRK? { #01 JCN BRK }

%2*   { #10 SFT }
%4*   { #20 SFT } %4/   { #02 SFT }
%8*   { #30 SFT } %8/   { #03 SFT }
%20*  { #50 SFT } 
%40*  { #60 SFT }
%2**  { #10 SFT2 } %2// { #01 SFT2 }
%4**  { #20 SFT2 } %4// { #02 SFT2 }
%8**  { #30 SFT2 } %8// { #03 SFT2 }
%10** { #40 SFT2 } %10// { #04 SFT2 }

%TOS  { #00 SWP } %TOB { SWP POP }
%INCR { SWP #01 + SWP }
%TOGGLE { DUP LDZ #00 = SWP STZ }
%INCR2 { SWP2 #0001 ++ SWP2 }
%DECR2 { SWP2 #0001 -- SWP2 }

%ASCII-FONT { TOS #0008 ** ;font-hex ++ }
%RELEASE-MOUSE { #00 .Mouse/state DEO }
%RESET-SELECTION { #0000 DUP2 .selection/from STZ2 .selection/to STZ2 }
%SET-STATE   { #01 .state/changed STZ ;draw-state JSR2 }
%RESET-STATE { #00 .state/changed STZ ;draw-state JSR2 }

%MEMORY { #8000 }
%MEMORY-SIZE { #8000 }

( 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
|80 @Controller &vector $2 &button  $1 &key    $1
|90 @Mouse      &vector $2 &x       $2 &y      $2 &state $1 &wheel $1
|a0 @File       &vector $2 &success $2 &offset $2 &pad   $2 &name  $2 &length $2 &load $2 &save $2

( variables )

|0000

@data
	&length $2
@cursor 
	&x $2 &y $2 
@state
	&timer $1
	&blink $1
	&changed $1
@path 
	&length $1
	&name $20
@selection
	&from $2
	&to $2
	&scroll $2
@frame
	&x $2 &y $2 &x2 $2 &y2 $2 
@file-frame
	&x $2 &y $2 &x2 $2 &y2 $2 
@byte-frame
	&x $2 &y $2 &x2 $2 &y2 $2 
@char-frame
	&x $2 &y $2 &x2 $2 &y2 $2 
@tile-frame
	&x $2 &y $2 &x2 $2 &y2 $2 
@menu-frame
	&x $2 &y $2 &x2 $2 &y2 $2 

( program )

|0100
	
	( theme )
	#0f77 .System/r DEO2 
	#0fc7 .System/g DEO2 
	#0f97 .System/b DEO2

	( vectors ) 
	;untrap JSR2

	( set size )
	#0160 .Screen/width DEO2
	#0180 .Screen/height DEO2

	( frame )
	.Screen/width DEI2 2// #0088 -- DUP2 .frame/x STZ2
		#0100 ++ .frame/x2 STZ2
	.Screen/height DEI2 2// #00a0 -- DUP2 .frame/y STZ2
		#0100 ++ .frame/y2 STZ2

	.frame/x LDZ2 
		DUP2 .file-frame/x STZ2 #0100 ++ .file-frame/x2 STZ2
	.frame/y LDZ2 
		DUP2 .file-frame/y STZ2 #0018 ++ .file-frame/y2 STZ2

	.frame/x LDZ2 
		DUP2 .byte-frame/x STZ2 #00c0 ++ .byte-frame/x2 STZ2
	.frame/y LDZ2 #0020 ++
		DUP2 .byte-frame/y STZ2 #0100 ++ .byte-frame/y2 STZ2

	.byte-frame/x2 LDZ2 
		DUP2 .char-frame/x STZ2 #0040 ++ .char-frame/x2 STZ2
	.byte-frame/y LDZ2 
		DUP2 .char-frame/y STZ2 #0100 ++ .char-frame/y2 STZ2

	.char-frame/x2 LDZ2 #0008 ++
		DUP2 .tile-frame/x STZ2 #0008 ++ .tile-frame/x2 STZ2
	.byte-frame/y LDZ2 
		DUP2 .tile-frame/y STZ2 #0100 ++ .tile-frame/y2 STZ2

	.frame/x LDZ2
		DUP2 .menu-frame/x STZ2 #0100 ++ .menu-frame/x2 STZ2
	.frame/y LDZ2 #0130 ++
		DUP2 .menu-frame/y STZ2 #0010 ++ .menu-frame/y2 STZ2

	;untitled-txt ;new-file JSR2
	MEMORY ;select JSR2

BRK

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

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

RTN

@trap ( -- )

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

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

RTN

@on-frame ( -> )
	
BRK

@on-frame-trap ( -> )

	( incr ) .state/timer LDZ INC DUP .state/timer STZ 

	#08 ! ,&no-toggle JCN
		.state/blink TOGGLE
		#01 .state/blink LDZ 8* + ;draw-filepath JSR2
		#00 .state/timer STZ 
		&no-toggle
	
BRK

@on-button ( -> )

	.Controller/button DEI
	DUP #44 ! ,&no-left-sel JCN
		.selection/to LDZ2 #0001 -- ;select-until JSR2 POP BRK
		&no-left-sel
	DUP #84 ! ,&no-right-sel JCN
		.selection/to LDZ2 #0001 ++ ;select-until JSR2 POP BRK
		&no-right-sel
	DUP #10 ! ,&no-up JCN
		.selection/from LDZ2 #0008 -- ;select JSR2 POP BRK
		&no-up
	DUP #20 ! ,&no-down JCN
		.selection/from LDZ2 #0008 ++ ;select JSR2 POP BRK
		&no-down
	DUP #40 ! ,&no-left JCN
		.selection/from LDZ2 #0001 -- ;select JSR2 POP BRK
		&no-left
	DUP #80 ! ,&no-right JCN
		.selection/from LDZ2 #0001 ++ ;select JSR2 POP BRK
		&no-right

	( ctrl )
	#01 AND ;on-control JCN2

	.Controller/key DEI
	DUP #1b ! ,&no-esc JCN
		;reset-selection JSR2 POP BRK
		&no-esc
	DUP #1f > OVR #7f < #0101 !! ,&no-char JCN
		DUP ;fill-selection JSR2
		&no-char
	DUP #0d ! ,&no-enter JCN
		.selection/from LDZ2 ;memsftr JSR2
		&no-enter
	DUP #08 ! ,&no-backspace JCN
		.selection/from LDZ2 ;memsftl JSR2
		&no-backspace
	POP

BRK

@on-control ( -> )

	.Controller/key DEI
	( file )
	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 
		#00 .Controller/key DEO
		#00 .path/length STZ
		#0000 .path/name STZ2
		;trap JSR2
		POP BRK
		&no-rename
	DUP [ LIT 'n ] ! ,&no-new JCN
		#25 ;draw-filepath JSR2
		;untitled-txt ;new-file JSR2
		;redraw JSR2 POP BRK &no-new
	DUP [ LIT 'o ] ! ,&no-open JCN
		;load-file JSR2
		;redraw JSR2 POP BRK &no-open
	DUP [ LIT 's ] ! ,&no-save JCN
		;path/name ;save-file JSR2 
		POP BRK  &no-save
	( snarf )
	DUP [ LIT 'c ] ! ,&no-copy JCN
		;copy-snarf JSR2 POP BRK
		&no-copy
	DUP [ LIT 'v ] ! ,&no-paste JCN
		;paste-snarf JSR2 POP BRK
		&no-paste
	POP 

BRK

@on-button-trap ( -> )

	.Controller/key DEI
	DUP ,&continue JCN
		POP BRK
		&continue
	DUP ( backspace ) #08 ! ,&no-edit-backspace JCN
		( clamp ) .path/length LDZ #00 = ,&edit-end JCN
		#00 ;draw-filepath JSR2
		.path/length LDZ #01 - STHk .path/length STZ
		#00 .path/name STHr + STZ
		#01 ;draw-filepath JSR2 POP BRK
		&no-edit-backspace
	DUP ( special ) #20 > ,&no-edit-enter JCN
		;untrap JSR2 POP BRK
		&no-edit-enter
	( clamp ) .path/length LDZ #1f = ,&edit-end JCN
	DUP .path/name .path/length LDZ STHk + STZ
	STHr INC STHk .path/length STZ
	#00 .path/name STHr + STZ
	#01 ;draw-filepath JSR2
	&edit-end
	POP

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
	.Mouse/state DEI #00 ! #41 + .Screen/sprite DEO

	.Mouse/wheel DEI 
	DUP #01 ! ,&no-up JCN
		.selection/from LDZ2 #0008 -- ;select JSR2
		&no-up
	DUP #ff ! ,&no-down JCN
		.selection/from LDZ2 #0008 ++ ;select JSR2
		&no-down
	POP

	.Mouse/state DEI BRK?

	.Mouse/x DEI2 .Mouse/y DEI2 
	( begin with canvas )
	OVR2 OVR2 .byte-frame ;within-rect JSR2
		;on-touch-byte JCN2
	OVR2 OVR2 .char-frame ;within-rect JSR2
		;on-touch-char JCN2
	OVR2 OVR2 .tile-frame ;within-rect JSR2
		;on-touch-tile JCN2
	OVR2 OVR2 .file-frame ;within-rect JSR2
		;on-touch-file JCN2
	OVR2 OVR2 .menu-frame ;within-rect JSR2
		;on-touch-menu JCN2
	POP2 POP2
	
BRK

@on-mouse-trap ( -> )

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

	;untrap JSR2
	
BRK

@on-touch-byte ( x* y* -> )

	.byte-frame/y LDZ2 -- 10// NIP 8* STH
	.byte-frame/x LDZ2 -- 8// NIP #03 DIV STHr +

	TOS MEMORY ++ [ .selection/scroll LDZ2 8** ++ ] ;select JSR2

BRK

@on-touch-char ( x* y* -> )

	.char-frame/y LDZ2 -- 10// NIP 8* STH
	.char-frame/x LDZ2 -- 8// NIP STHr +

	TOS MEMORY ++ [ .selection/scroll LDZ2 8** ++ ] ;select JSR2

BRK

@on-touch-tile ( x* y* -> )

	POP2 POP2

BRK

@on-touch-file ( x* y* -> )

	POP2 POP2
	#00 ;draw-filepath JSR2 
	;trap JSR2

BRK

@on-touch-menu ( x* y* -> )

	.menu-frame/y LDZ2 -- 8// NIP STH
	.menu-frame/x LDZ2 -- 8// NIP STHr

	( bitpad )
	OVR #20 < ;on-touch-menu-bitpad JCN2

	DEBUG2

BRK

@on-touch-menu-bitpad ( x* y* -> )

	4* + #07 SWP - #40 SFT #01 SWP SFT
	.selection/from LDZ2 LDA
	SWP EOR
	.selection/from LDZ2 STA
	;redraw JSR2
	RELEASE-MOUSE

BRK

@select ( id* -- )

	DUP2 .selection/from LDZ2 == ,&skip JCN
	DUP2 MEMORY << ,&skip JCN
	DUP2 MEMORY .data/length LDZ2 ++ #0001 -- >> ,&skip JCN
	( valid )
	DUP2 .selection/from STZ2
	.selection/to STZ2
	;follow-selection JSR2
	;redraw JSR2
	RTN
	&skip
	POP2

RTN

@select-until ( id* -- )

	DUP2 MEMORY .data/length LDZ2 ++ << ,&valid JCN
		POP2 RTN
		&valid

	.selection/to STZ2

	( clamp )
	.selection/to LDZ2 .selection/from LDZ2 >> ,&not-before JCN
		.selection/from LDZ2 .selection/to STZ2
		&not-before
	
	;follow-selection JSR2
	;redraw JSR2

RTN

@follow-selection ( -- )

	.selection/from LDZ2 MEMORY .selection/scroll LDZ2 8** ++ << ,&up JCN
	.selection/to LDZ2 MEMORY #007f ++ .selection/scroll LDZ2 8** ++  >> ,&down JCN
	RTN

	&up
		.selection/from LDZ2 #0001 -- ;scroll-to JSR2 
		RTN

	&down
		.selection/to LDZ2 #0080 -- #0008 ++ ;scroll-to JSR2

RTN

@fill-selection ( char -- )

	STH
	.selection/to LDZ2 INC2 .selection/from LDZ2
	&loop
		DUP2 STHkr ROT ROT STA
		INC2 GTH2k ,&loop JCN
	POP2 POP2 POPr
	;redraw JSR2

RTN

@reset-selection ( -- )

	.selection/from LDZ2 .selection/to STZ2
	;redraw JSR2

RTN

@scroll-to ( addr* -- )

	MEMORY -- 8// .selection/scroll STZ2

RTN

@is-selected ( id* -- flag )

	DUP2 .selection/from LDZ2 !! ,&continue JCN
		POP2 #01 RTN
		&continue
	DUP2 .selection/from LDZ2 >> ,&gt JCN
		POP2 #00 RTN
		&gt
	DUP2 .selection/to LDZ2 #0001 ++ << ,&lt JCN
		POP2 #00 RTN
		&lt
	POP2 #01

RTN

@memsftl ( addr* -- )

	DUP2 INC2 MEMORY .data/length LDZ2 ++ !! ,&can-shift JCN
		POP2 RTN
		&can-shift

	MEMORY .data/length LDZ2 ++ SWP2
	&loop
		( a ) INC2k LDA STH
		( b ) DUP2 STHr ROT ROT STA
		INC2 GTH2k ,&loop JCN
	POP2 POP2

	.data/length LDZ2 #0001 -- .data/length STZ2
	;redraw JSR2

RTN

@memsftr ( addr* -- )

	#0001 -- MEMORY .data/length LDZ2 ++
	&loop
		( a ) LDAk STH
		( b ) INC2k STHr ROT ROT STA
		#0001 -- LTH2k ,&loop JCN
	POP2 POP2

	.data/length LDZ2 INC2 .data/length STZ2
	;redraw JSR2

RTN

@redraw ( -- )

	#0010 #0000 
	&loop
		( addr ) DUP2 8** MEMORY ++ .selection/scroll LDZ2 8** ++ STH2
		( y ) DUP2 10** .byte-frame/y LDZ2 ++
		STH2r ;draw-row JSR2
		INC2 GTH2k ,&loop JCN
	POP2 POP2

	.file-frame/x LDZ2 .file-frame/x2 LDZ2 .file-frame/y2 LDZ2 #03 ;line-hor JSR2

	;draw-menu JSR2
	( 
	.file-frame #01 ;line-rect JSR2
	.byte-frame #01 ;line-rect JSR2
	.char-frame #02 ;line-rect JSR2
	.tile-frame #03 ;line-rect JSR2
	.menu-frame #03 ;line-rect JSR2
	 )

RTN

@draw-row ( y* id* -- )

	;&row STA2
	;&y STA2

	#08 #00
	&bytes-loop
		( color ) DUP TOS ;&row LDA2 ++ ;get-byte-color JSR2 STH
		( byte ) DUP TOS ;&row LDA2 ++ LDA STH
		( y ) ;&y LDA2 STH2
		( x-block ) DUP #03 MUL TOS 8** .frame/x LDZ2 ++
		STH2r STHr STHr ;draw-byte JSR2
		INC GTHk ,&bytes-loop JCN
	POP2

	#08 #00
	&chars-loop
		( color ) DUP TOS ;&row LDA2 ++ ;get-byte-color JSR2 STH
		( byte ) DUP TOS ;&row LDA2 ++ LDA STH
		( y ) ;&y LDA2 STH2
		( x-block ) DUP TOS 8** .frame/x LDZ2 ++ #00c0 ++
		STH2r STHr STHr ;draw-char JSR2
		INC GTHk ,&chars-loop JCN
	POP2

	.Screen/y DEI2 #0010 -- .Screen/y DEO2
	#0108 .frame/x LDZ2 ++ .Screen/x DEO2
	;&row LDA2 .Screen/addr DEO2
	#01 .Screen/sprite DEO

RTN
	&y $2 &row $2

@get-byte-color ( id* )

	DUP2 MEMORY .data/length LDZ2 ++ << ,&exists JCN
		POP2 #03 RTN
		&exists

	;is-selected JSR2 #07 MUL #01 +

RTN

@draw-byte ( x* y* byte color -- )

	#06 .Screen/auto DEO
	STH STH
	.Screen/y DEO2
	.Screen/x DEO2
	STHr
	DUP
		( hn )
		#04 SFT ,&parse JSR #18 - TOS 10** ;font-hex ++ .Screen/addr DEO2
		STHkr .Screen/sprite DEO
		STHkr .Screen/sprite DEO
	.Screen/x DEI2 #0008 ++ .Screen/x DEO2
	.Screen/y DEI2 #0010 -- .Screen/y DEO2
		( ln )
		#0f AND ,&parse JSR #18 - TOS 10** ;font-hex ++ .Screen/addr DEO2
		STHkr .Screen/sprite DEO
		STHr .Screen/sprite DEO
	#00 .Screen/auto DEO
	RTN
	&parse ( value -- char )
		DUP #09 > ,&above JCN 
			#30 + RTN 
		&above 
			#09 - #60 + RTN

RTN

@draw-char ( x* y* char color -- )

	#06 .Screen/auto DEO

	STH STH
	.Screen/y DEO2
	.Screen/x DEO2
	STHr

	DUP #1f > OVR #80 < #0101 == ,&visible JCN
		POP ;font #000e 10** ++ ,&resume JMP
	&visible
		;char-addr JSR2
	&resume
	.Screen/addr DEO2
	
	STHkr .Screen/sprite DEO
	STHr .Screen/sprite DEO

	#00 .Screen/auto DEO

RTN

@draw-menu ( -- )

	( filepath )
	#01 ;draw-filepath JSR2

	;draw-bitpad JSR2

RTN

@draw-bitpad ( -- )

	#08 #00
	&loop
		( state ) DUP #07 SWP - [ .selection/from LDZ2 LDA ] SWP SFT #01 AND STH
		( y ) DUP 4/ TOS 8** .menu-frame/y LDZ2 ++ STH2
		( x ) DUP #03 AND TOS 8** .menu-frame/x LDZ2 ++
		STH2r STHr #01 ,draw-bit JSR
		INC GTHk ,&loop JCN
	POP2

RTN

@draw-bit ( x* y* state color -- )

	STH
	( addr ) 8* TOS ;bit-icns ++ .Screen/addr DEO2 
	( y ) .Screen/y DEO2
	( x ) .Screen/x DEO2
	STHr .Screen/sprite DEO	

RTN

@draw-state ( -- )


RTN

@draw-filepath ( color -- )
	
	STH
	.file-frame/x LDZ2
	.file-frame/y LDZ2 
	;path/name STHr 
		;draw-text JSR2

	( start byte )
	.selection/from LDZ2 MEMORY -- STH STH
	.file-frame/x2 LDZ2 #0048 -- .file-frame/y LDZ2 STHr #01 ;draw-byte JSR2
	.file-frame/x2 LDZ2 #0038 -- .file-frame/y LDZ2 STHr #01 ;draw-byte JSR2
	( / )
	.file-frame/x2 LDZ2 #0028 -- .file-frame/y LDZ2 LIT '/ #03 ;draw-char JSR2
	( end byte )
	.data/length LDZ2 #0001 -- STH STH
	.file-frame/x2 LDZ2 #0020 -- .file-frame/y LDZ2 STHr #01 ;draw-byte JSR2
	.file-frame/x2 LDZ2 #0010 -- .file-frame/y LDZ2 STHr #01 ;draw-byte JSR2

RTN

@draw-text ( x* y* addr* color -- )

	#07 .Screen/auto DEO
	STH
	SWP2 .Screen/y DEO2
	SWP2 .Screen/x DEO2
	&while
		LDAk ;char-addr JSR2 .Screen/addr DEO2
		STHkr .Screen/sprite DEO
		.Screen/x DEI2 #0008 -- .Screen/x DEO2
		STHkr .Screen/sprite DEO
		.Screen/y DEI2 #0010 -- .Screen/y DEO2
		INC2 LDAk ,&while JCN
	POP2
	POPr
	#00 .Screen/auto DEO

RTN

@char-addr ( char -- addr* )

	#20 - TOS 10** ;font ++

RTN

@clear ( -- )

	MEMORY DUP2 MEMORY-SIZE ++ SWP2
	&loop
		DUP2 #00 ROT ROT STA
		INC2 GTH2k ,&loop JCN
	POP2 POP2

RTN

( file )

@new-file ( default* -- )

	( clear )
	;clear JSR2
	MEMORY ;select JSR2

	STH2
	#0c #00
	&loop
		DUP DUP TOS STH2kr ++ LDA
		SWP .path/name + STZ
		INC GTHk ,&loop JCN
	( end ) #00 SWP .path/name + STZ 
	.path/length STZ 
	POP2r
	RESET-STATE
	#0000 .selection/scroll STZ2
	#0008 .data/length STZ2
	;redraw JSR2

RTN

@load-file ( -- )

	( load bytes )
	;path/name .File/name DEO2
	MEMORY-SIZE         .File/length DEO2
	MEMORY       .File/load DEO2
	.File/success DEI2 .data/length STZ2
	RESET-STATE
	;redraw JSR2

RTN

@save-file ( -- )

	;path/name .File/name DEO2 
	.data/length LDZ2 .File/length DEO2 
	MEMORY .File/save DEO2 
	RESET-STATE

RTN

( snarf )

@snarf-txt ".snarf $1

@copy-snarf ( -- )

	;snarf-txt .File/name DEO2 
	.selection/to LDZ2 .selection/from LDZ2 -- INC2 .File/length DEO2 
	.selection/from LDZ2 .File/save DEO2

RTN

@paste-snarf ( -- )

	;snarf-txt .File/name DEO2
	MEMORY-SIZE .File/length DEO2
	.selection/from LDZ2 .File/load DEO2
	;redraw JSR2

RTN

( generics )

@line-hor ( x0* x1* y* color -- )

	STH .Screen/y DEO2 SWP2
	&loop
		( save ) DUP2 .Screen/x DEO2
		( draw ) STHkr .Screen/pixel DEO
		#0002 ++ GTH2k ,&loop JCN
	POP2 POP2 POPr

RTN

@line-rect ( rect color -- )

	STH STH
	( y2 ) STHkr #06 + LDZ2
	( y1 ) STHkr #02 + LDZ2 #0001 -- ( flip sign ) GTH2k SWP2?
	&ver
		( save ) DUP2 .Screen/y DEO2
		( x1 ) STHkr LDZ2 #0001 -- .Screen/x DEO2 
		OVRr STHr .Screen/pixel DEO
		( x2 ) STHkr #04 + LDZ2 .Screen/x DEO2 
		OVRr STHr .Screen/pixel DEO
		( incr )
		INC2 GTH2k ,&ver JCN
	POP2
	( x2 ) STHkr #04 + LDZ2
	( x1 ) STHkr LDZ2 #0001 -- ( flip sign ) GTH2k SWP2?
	&hor
		( save ) DUP2 .Screen/x DEO2
		( y1 ) STHkr #02 + LDZ2 #0001 -- .Screen/y DEO2 
		OVRr STHr .Screen/pixel DEO
		( y2 ) STHkr #06 + LDZ2 .Screen/y DEO2 
		OVRr STHr .Screen/pixel DEO
		( incr )
		INC2 GTH2k ,&hor JCN
	POP2
	POPr 
	.Screen/x DEO2
	.Screen/y DEO2 
	STHr .Screen/pixel DEO

RTN

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

RTN

@print-hex ( value* -- )
	
	&short ( value* -- )
		SWP ,&echo JSR 
	&byte ( value -- )
		,&echo JSR
	RTN

	&echo ( value -- )
	STHk #04 SFT ,&parse JSR .Console/write DEO
	STHr #0f AND ,&parse JSR .Console/write DEO
	RTN
	&parse ( value -- char )
		DUP #09 GTH ,&above JCN #30 + RTN &above #09 - #60 + RTN

RTN

@byte-spacing
	0002 0507 0a0c 0f11
	1416 191b 1e20 2325
@bit-icns
	3844 8282 8244 3800
	387c fefe fe7c 3800

@untitled-txt  "untitled.chr $1
@cursor-icn    80c0 e0f0 f8e0 1000

@font-hex
	007c 8282 8282 827c 0030 1010 1010 1010
	007c 8202 7c80 80fe 007c 8202 1c02 827c
	000c 1424 4484 fe04 00fe 8080 7c02 827c
	007c 8280 fc82 827c 007c 8202 1e02 0202
	007c 8282 7c82 827c 007c 8282 7e02 827c
	007c 8202 7e82 827e 00fc 8282 fc82 82fc
	007c 8280 8080 827c 00fc 8282 8282 82fc
	007c 8280 f080 827c 007c 8280 f080 8080 


include src/font.tal

@text-snarf $100