~rabbits/dexe

ref: 0695bf53f536dfd48a9feec7585a625d8a4dbb44 dexe/src/main.tal -rw-r--r-- 10.1 KiB
0695bf53neauoire Keyboard controls 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
(
	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 }
%8*   { #30 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 }

( 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

@scroll $2
@cursor 
	&x $2 &y $2 
@state
	&timer $1
	&trap $1
	&blink $1
@pointer
	&x $2 
	&y $2
	&last $1
@selection
	&from $2
	&to $2
	&scroll $2
	&nibble $1
@path 
	&length $1
	&name $20
@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 ) 
	;on-button .Controller/vector DEO2
	;on-mouse .Mouse/vector DEO2
	;on-frame    .Screen/vector DEO2

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

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

	.frame/x LDZ2 
		DUP2 .byte-frame/x STZ2 #00c0 ++ .byte-frame/x2 STZ2
	.frame/y LDZ2 
		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 #0110 ++
		DUP2 .menu-frame/y STZ2 #0010 ++ .menu-frame/y2 STZ2

	( load bytes )
	;untitled-txt .File/name DEO2
	#1000         .File/length DEO2
	;MEMORY       .File/load DEO2
	.File/success DEI2 ;MEMORY-LEN STA2

	#0000 .scroll STZ2
	;MEMORY ;select JSR2

BRK

@on-frame ( -> )
	
BRK

@on-button ( -> )

	.Controller/button DEI
	DUP #10 ! ,&no-up JCN
		.selection/from LDZ2 #0008 -- ;select JSR2
		&no-up
	DUP #20 ! ,&no-down JCN
		.selection/from LDZ2 #0008 ++ ;select JSR2
		&no-down
	DUP #40 ! ,&no-left JCN
		.selection/from LDZ2 #0001 -- ;select JSR2
		&no-left
	DUP #80 ! ,&no-right JCN
		.selection/from LDZ2 #0001 ++ ;select JSR2
		&no-right
	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
	POP2 POP2
	
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 ++ [ .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 ++ [ .scroll LDZ2 8** ++ ] ;select JSR2

BRK

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

	POP2 POP2

BRK

@select ( id* -- )

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

RTN

@follow ( -- )

	.selection/from LDZ2 ;MEMORY .scroll LDZ2 8** ++ << ,&up JCN
	.selection/to LDZ2 ;MEMORY #007f ++ .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

@scroll-to ( addr* -- )

	;MEMORY -- 8// .scroll STZ2

RTN

@is-selected ( id* -- flag )

	DUP2 .selection/from LDZ2 == ,&yes JCN
	STH2k .selection/from LDZ2 >> STH2r .selection/from LDZ2 << #0101 == ,&yes JCN
		#00
	RTN
	&yes
		POP2 #01 

RTN

@redraw ( -- )

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

	;draw-menu 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 ++ ;is-selected JSR2 #07 MUL #01 + 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 ++ ;is-selected JSR2 #07 MUL #01 + 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

@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
		#20 - TOS 10** ;font ++
	&resume
	.Screen/addr DEO2
	
	STHkr .Screen/sprite DEO
	STHr .Screen/sprite DEO

	#00 .Screen/auto DEO

RTN

@draw-menu ( -- )

	.menu-frame/x LDZ2 .menu-frame/x2 LDZ2 .menu-frame/y LDZ2 #0008 -- #03 ;line-hor JSR2

	( start byte )
	.selection/from LDZ2 ;MEMORY -- STH STH
	.menu-frame/x LDZ2 .menu-frame/y LDZ2 STHr #01 ;draw-byte JSR2
	.menu-frame/x LDZ2 #0010 ++ .menu-frame/y LDZ2 STHr #01 ;draw-byte JSR2

	( / )
	.menu-frame/x LDZ2 #0020 ++ .menu-frame/y LDZ2 LIT '/ #03 ;draw-char JSR2

	( end byte )
	;MEMORY-LEN LDA2 STH STH
	.menu-frame/x LDZ2 #0028 ++ .menu-frame/y LDZ2 STHr #01 ;draw-byte JSR2
	.menu-frame/x LDZ2 #0038 ++ .menu-frame/y LDZ2 STHr #01 ;draw-byte JSR2

RTN

@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

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

@scrollbar_icn
	aa55 aa55 aa55 aa55 ( bg )
	ffff ffff ffff ffff ( fg )
	0010 387c fe10 1010 ( up )
	0010 1010 fe7c 3810 ( down )

@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

@MEMORY-LEN $2

@MEMORY