-
Notifications
You must be signed in to change notification settings - Fork 0
/
forth.S
1269 lines (1111 loc) · 20.4 KB
/
forth.S
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
; Need some way of having both SRAM words and PROGMEM words.
; If this is token-based, maybe signify in the LSB what kind of word the token
; is for?
#include <avr/io.h>
#include "macros.i"
#define CODEWORD_SIZE 2
; The interpreter pointer is r25:r24
#define IP_LREG 24
; The data stack pointer is Y (r28)
#define DS_LREG 28
#define RS0 0x100
#define DS0 0x180
; The dictionary address of the most recently defined word.
.set link,0
.text
; Interrupt vectors.
rjmp main ; RESET
rjmp main ; INT0
rjmp kb_clocked ; PCINT0
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
.global DOCOL
DOCOL:
PUSHRSP IP_LREG
; r23:22 contains the codeword's address.
movw IP_LREG, r22
; Skip past the codeword to get to the first token.
adiw IP_LREG, CODEWORD_SIZE
; Execute NEXT immediately.
.global NEXT
NEXT:
; Load the next token, which is an offset into the token table.
movw ZL, IP_LREG ; load the address of the next token
adiw IP_LREG, 1 ; increment the IP
; Load the token index into ZL. ZH is constant
lpm ZL, Z
ldi ZH, hi8(TOKEN_TABLE)
; Load the codeword's address from the token table. Keep it in r22 so DOCOL
; can read it later.
lpm r22, Z+
lpm r23, Z
movw ZL, r22
; The codeword contains the address of the code to run for this word, so we
; need to jump to the address stored at the codeword.
lpm r20, Z+
lpm r21, Z
movw ZL, r20
; Execute the codeword.
ijmp
.global main
main:
cli
; Intialize return stack
ldi r16, lo8(RS0)
out _SFR_IO_ADDR(SPL), r16
ldi r16, hi8(RS0)
out _SFR_IO_ADDR(SPH), r16
; Initialize data stack
ldi DS_LREG, lo8(DS0)
ldi DS_LREG+1, hi8(DS0)
; Initialize the interpreter pointer
ldi IP_LREG, lo8(forth_main)
ldi IP_LREG+1, hi8(forth_main)
; Call NEXT to kick things off.
rjmp NEXT
; The default program!
forth_main:
; Wait because the keyboard is noisy on the busy line (TODO: I think?)
lit 0x3FFF
fw WAIT
fw RAMINIT
fw RESETPGW
fw SERINIT
fw LCDINIT
fw KBINIT
fw TRUE
fw LEDSTORE
fw QUIT ; Never returns.
defcode "RAMINIT",,RAMINIT
ldi ZL, lo8(__data_load_start)
ldi ZH, hi8(__data_load_start)
ldi XL, lo8(__data_start)
ldi XH, hi8(__data_start)
1:
lpm r0, Z+
st X+, r0
ldi r16, lo8(__data_load_end)
ldi r17, hi8(__data_load_end)
sub r16, ZL
sbc r17, ZH
or r16, r17
brne 1b
rjmp NEXT
; == DEBUGGING TOOLS ==
; TICKS --
defword "WAIT",,WAIT
1:
fw DECR ; D: T-1
fw DUP ; D: T-1 T-1
fw ZEQU ; D: =0 T-1
zbranch 1b ; D: T-1
fw DROP ; D: empty
fw EXIT
defword "LED!",,LEDSTORE
fw LCD_RW_READ
fw SWAP
zbranch 1f
fw LCD_LED
fw OR
1:
fw TOSER
fw EXIT
; Blinks the LED if top of stack = 0, holds steady if != 0.
defword "TESTLED",,TESTLED
1:
fw FALSE
fw LEDSTORE
lit 0x0FFF
fw WAIT
fw TRUE
fw LEDSTORE
lit 0x0FFF
fw WAIT
fw DUP
zbranch 1b
1:
fw FALSE
zbranch 1b
; Blink indefinitely.
defword "BLINK",,BLINK
1:
fw LCD_LED
fw LCD_RW_READ
fw OR
fw TOSER
lit 0x0FFF
fw WAIT
fw LCD_RW_READ
fw TOSER
lit 0x0FFF
fw WAIT
fw FALSE
zbranch 1b
; == STACK MANIPULATION ==
defcode "DROP",,DROP
adiw Y, 2
rjmp NEXT
defcode "SWAP",,SWAP
POPDSP 0
POPDSP 2
PUSHDSP 0
PUSHDSP 2
rjmp NEXT
defcode "DUP",,DUP
; MSB is top of stack, LSB is next
ldd r0, Y+1
ld r1, Y
PUSHDSP 0
rjmp NEXT
defcode "OVER",,OVER
ldd r0, Y+3
ldd r1, Y+2
PUSHDSP 0
rjmp NEXT
defcode "ROT",,ROT
; Bring the third item to the top.
POPDSP 0
POPDSP 2
POPDSP 4
PUSHDSP 2
PUSHDSP 0
PUSHDSP 4
rjmp NEXT
defword "-ROT",,NROT
fw ROT
fw ROT
fw EXIT
defword "2DROP",,TWODROP
fw DROP
fw DROP
fw EXIT
defword "2DUP",,TWODUP
fw OVER
fw OVER
fw EXIT
; == RETURN STACK MANIPULATION ==
defcode ">R",,TOR
POPDSP 0
PUSHRSP 0
rjmp NEXT
defcode "R>",,FROMR
POPRSP 0
PUSHDSP 0
rjmp NEXT
defcode "RSP@",,RSPFETCH
in r0, _SFR_IO_ADDR(SPL)
in r1, _SFR_IO_ADDR(SPH)
PUSHDSP 0
rjmp NEXT
defcode "RSP!",,RSPSTORE
POPDSP 0
out _SFR_IO_ADDR(SPL), r0
out _SFR_IO_ADDR(SPH), r1
rjmp NEXT
defcode "RDROP",,RDROP
POPRSP 0
rjmp NEXT
defcode "DSP@",,DSPFETCH
; PUSHDSP isn't atomic, so need to buffer it via a different register.
movw r0, DS_LREG
PUSHDSP 0
rjmp NEXT
defcode "DSP!",,DSPSTORE
; POPDSP isn't atomic, so need to buffer it via a different register.
POPDSP 0
movw DS_LREG, r0
rjmp NEXT
; Relative branch.
defcode "0BRANCH",,ZBRANCH
; Push the next word onto the data stack.
movw ZL, IP_LREG
; TODO: This could just be a byte, but would require extending the byte into a
; word.
lpm r0, Z+
lpm r1, Z+
POPDSP 2
or r2, r3
brne 1f ; If top of stack is 0, we'll modify Z to branch.
add ZL, r0
adc ZH, r1
1:
movw IP_LREG, ZL
rjmp NEXT
; == ARITHMETIC ==
defcode "+",,ADD
POPDSP 0
POPDSP 2
add r0, r2
adc r1, r3
PUSHDSP 0
rjmp NEXT
defcode "-",,SUB
POPDSP 0
POPDSP 2
sub r0, r2
sbc r1, r3
PUSHDSP 0
rjmp NEXT
defword "1+",,INCR
lit 1
fw ADD
fw EXIT
defword "1-",,DECR
lit -1
fw ADD
fw EXIT
defcode "2*",,ASL
POPDSP 0
lsl r0 ; shift into carry
rol r1 ; shifts carry into low bit
PUSHDSP 0
rjmp NEXT
defcode "2/",,ASR
POPDSP 0
asr r1 ; leave high bit, shift rest, shift into carry
ror r0 ; shifts carry into high bit
PUSHDSP 0
rjmp NEXT
defcode "AND",,AND
POPDSP 0
POPDSP 2
and r0, r2
and r1, r3
PUSHDSP 0
rjmp NEXT
defcode "OR",,OR
POPDSP 0
POPDSP 2
or r0, r2
or r1, r3
PUSHDSP 0
rjmp NEXT
defcode "XOR",,XOR
POPDSP 0
POPDSP 2
eor r0, r2
eor r1, r3
PUSHDSP 0
rjmp NEXT
defcode "INVERT",,INVERT
POPDSP 0
com r0
com r1
PUSHDSP 0
rjmp NEXT
; Switch the high and low bytes.
defcode "SWIZZLE",,SWIZZLE
POPDSP 0
mov r2, r0
mov r0, r1
mov r1, r2
PUSHDSP 0
rjmp NEXT
defcode "0=",,ZEQU
POPDSP 30
ser r16
adiw r30, 0
breq 1f
clr r16 ; Clear all bits in r16 if the Z flag is cleared.
1:
mov r17, r16 ; Mirror into r16
PUSHDSP 16
rjmp NEXT
defcode "0<",,ZLT
POPDSP 30
ser r16
adiw r30, 0
brlt 1f
clr r16 ; Clear all bits in r16 if the Z flag is cleared.
1:
mov r17, r16 ; Mirror into r16
PUSHDSP 16
rjmp NEXT
defword "0>",,ZGT
fw DUP
fw ZEQU
fw ZEQU
fw SWAP
fw ZLT
fw ZEQU
fw AND
fw EXIT
defword "=",,EQU
fw SUB
fw ZEQU
fw EXIT
defword "<>",,NEQU
fw EQU
fw ZEQU
fw EXIT
defword ">",,GT
fw SUB
fw ZGT
fw EXIT
defword "<",,LT
fw SUB
fw ZLT
fw EXIT
; == MEMORY COMMANDS ==
defcode "C!",,STOREBYTE
POPDSP 30 ; address
POPDSP 0 ; data
st Z, r0
rjmp NEXT
defcode "C@",,FETCHBYTE
POPDSP 30 ; address
ld r16, Z
ldi r17, 0
PUSHDSP 16
rjmp NEXT
; (addr data -- )
defword "!",,STORE
; Store high byte.
fw OVER
fw SWIZZLE
fw OVER
fw INCR
fw STOREBYTE
; Store low byte.
fw STOREBYTE
fw EXIT
; (addr -- data)
defword "@",,FETCH
fw DUP
; Low byte
fw FETCHBYTE
fw SWAP
; High byte
fw INCR
fw FETCHBYTE
fw SWIZZLE
; Combine
fw OR
fw EXIT
defcode "ROMC@",,ROMFETCHBYTE
POPDSP 30
lpm r16, Z
ldi r17, 0
PUSHDSP 16
rjmp NEXT
defword "ROM@",,ROMFETCH
fw DUP
fw ROMFETCHBYTE
fw SWAP
fw INCR
fw ROMFETCHBYTE
fw SWIZZLE
fw OR
fw EXIT
; == FLAGS ==
; FLAGSVAR FLAG --
defword "SETFLAG",,SETFLAG
fw SWAP
fw OVER
fw FETCH
fw OR
fw SWAP
fw STORE
fw EXIT
; FLAGSVAR FLAG --
defword "CLRFLAG",,CLRFLAG
fw SWAP
fw INVERT
fw OVER
fw FETCH
fw AND
fw SWAP
fw STORE
fw EXIT
; FLAGSVAR FLAG -- IS_SET
defword "CHECKFLAG",,CHECKFLAG
fw FETCH
fw AND
fw ZEQU
fw ZEQU
fw EXIT
; DUPs the return address so that a copy of the LOOP point still exists even
; after EXITing from this word.
defword "LOOP",,LOOP
fw FROMR
fw DUP
fw TOR
fw TOR
fw EXIT
; pred a b -- pred ? a : b
defword "TERN",,TERN
fw DUP ; pred pred a b
fw ROT ; a pred pred b
fw AND ; a? pred b
fw NROT ; pred b a?
fw INVERT ; !pred b a?
fw AND ; b? a?
fw OR
fw EXIT
; Branches to the top of RSP if COND, else exits normally. Caller needs to RDROP
; the loop point after the loop terminates.
defword "WHILE",,WHILE
; D: COND R: NORMAL IFTRUE
fw FROMR ; NORMAL COND
fw FROMR ; IFTRUE NORMAL COND
fw DUP
fw TOR ; D: IFTRUE NORMAL COND R: IFTRUE
fw ROT ; D: COND IFTRUE NORMAL R: IFTRUE
fw TERN ; D: next R: IFTRUE
fw TOR
; Maybe conditionally RDROP so the caller doesn't have to?
fw EXIT
; Calculate the Nth triangle number with some awful stack manipulation.
; n -- the nth triangle number
defword "TRI",,TRI
lit 0 ; running sum
fw LOOP ; D: SUM N -- R: LOOP
fw OVER ; N SUM N
fw ADD ; NSUM N
fw SWAP ; N NSUM
fw DECR ; N-1 NSUM
fw SWAP ; NSUM N-1
fw OVER ; N-1 NSUM N-1
fw ZEQU ; ISZERO NSUM N-1
fw INVERT ; !ISZERO NSUM N-1
fw WHILE
fw RDROP
fw ADD ; Drop the arg (which is 0 now)
fw EXIT
; == I/O ==
#define SBUF_SIZE 0x20
defcode "SBUF",,SBUF
ldi r16, lo8(var_SBUF)
ldi r17, hi8(var_SBUF)
PUSHDSP 16
rjmp NEXT
.data
.balign SBUF_SIZE
var_SBUF:
.fill SBUF_SIZE, 1, 0
.text
defstring "GREETING",,GREETING,"Hello, world!"
; -- length addr
defword "WORD",,WORD
1:
fw KEY
fw DUP
lit ' '
fw EQU
zbranch 2f
fw DROP ; drop the space
fw FALSE
zbranch 1b
2:
fw SBUF ; Cursor.
fw SWAP
1: ; CHAR CURSOR --
fw OVER
fw STOREBYTE
fw INCR
fw KEY
fw DUP
lit ' '
fw EQU
zbranch 1b
fw DROP
fw SBUF
fw SWAP
fw SUB
fw SBUF
fw SWAP ; length on top
fw EXIT
defword "EMIT",,EMIT
fw SWIZZLE ; Put char in high byte.
fw LCD_RS_DR ; Data register.
fw OR
fw LCDCMD
fw EXIT
defword "WRITEWORD",,WRITEWORD
lit 4
1:
fw SWAP
fw DUP
; Move to bottom nibble
fw SWIZZLE
fw ASR
fw ASR
fw ASR
fw ASR
fw TOHEX
fw EMIT
; Shift left 4 bits to move to the next digit.
fw ASL
fw ASL
fw ASL
fw ASL
fw SWAP
fw DECR
fw DUP
fw ZEQU
zbranch 1b
fw TWODROP
fw EXIT
; LEN ADDR --
defword "TELL",,TELL
1:
fw DUP
zbranch 2f
fw DECR
fw SWAP
fw DUP
fw FETCHBYTE
fw EMIT
fw INCR
fw SWAP
fw FALSE
zbranch 1b
2:
fw TWODROP
fw EXIT
; == STRINGS ==
; Parses an ASCII value as a hex digit.
; DIGIT -- SUCCESS NUMBER
defword "DIGIT",,DIGIT
fw DUP
lit 'a'-1
fw LT
fw OVER
lit 'f'+1
fw GT
fw AND
zbranch 1f
lit 'a'-10
fw SWAP
fw SUB
fw TRUE
fw EXIT
1:
fw DUP
lit 'A'-1
fw LT
fw OVER
lit 'F'+1
fw GT
fw AND
zbranch 1f
lit 'A'-10
fw SWAP
fw SUB
fw TRUE
fw EXIT
1:
fw DUP
lit '0'-1
fw LT
fw OVER
lit '9'+1
fw GT
fw AND
zbranch 1f
lit '0'
fw SWAP
fw SUB
fw TRUE
fw EXIT
1:
fw FALSE
fw EXIT
; Parses a hex number. TODO: negatives, decimal+other bases
; LEN ADDR -- SUCCESS NUMBER
defword "NUMBER",,NUMBER
; Check for zero.
fw DUP
fw ZEQU
zbranch 1f
fw DROP
fw FALSE
fw EXIT
1: ; LEN INDEX
lit 0
fw TOR
2: ; LEN INDEX R: TOTAL
fw DECR
fw SWAP
fw DUP ; INDEX INDEX LEN
fw FETCHBYTE ; CHAR INDEX LEN
fw DIGIT ; SUCCESS NUMBER INDEX LEN
fw ZEQU ; NOTSUCCESS NUMBER INDEX LEN
zbranch 1f ; NUMBER INDEX LEN
fw RDROP
fw DROP ; INDEX GARBAGE
fw DROP ; GARBAGE
fw FALSE ; FALSE GARBAGE
fw EXIT
1: ; NUMBER INDEX LEN
fw FROMR
fw ASL
fw ASL
fw ASL
fw ASL
fw ADD
fw TOR
fw INCR
fw SWAP
fw DUP
fw ZEQU
zbranch 2b
fw TWODROP
fw FROMR
fw TRUE
fw EXIT
; Lowest 4 bits to hex digit
defword "TOHEX",,TOHEX
lit 0x000F
fw AND
fw DUP
lit 10
fw GT
zbranch 1f
lit '0'
fw ADD
fw EXIT
1:
lit 'A' - 10
fw ADD
fw EXIT
; TODO: Right now, ADDR1 must be in progmem. Maybe copy it into RAM?
; LEN1 ADDR1 LEN2 ADDR2 -- EQUAL
defword "STREQU",,STREQU
fw ROT
fw OVER ; LEN1 LEN2 LEN1 ADDR1 ADDR2
fw NEQU
zbranch 1f
; Not equal lengths.
fw DROP
fw DROP
fw DROP
fw FALSE
fw EXIT
1: ; LEN ADDR1 ADDR2
fw DUP
fw ZEQU
zbranch 2f
fw DROP
fw DROP
fw DROP
fw TRUE
fw EXIT
2:
fw DECR
fw NROT ; ADDR1 ADDR2 LEN-1
fw OVER
fw FETCHBYTE ; *ADDR2
fw OVER
fw ROMFETCHBYTE ; *ADDR1
fw NEQU ; NEQU ADDR1 ADDR2 LEN-1
zbranch 2f
fw DROP
fw DROP
fw DROP
fw FALSE
fw EXIT
2:
fw INCR
fw NROT
fw INCR
fw NROT
fw FALSE
zbranch 1b
; == INTERPRETING ==
defconst "R0",,RZ,RS0
defconst "F_IMMED",,F_IMMED,0x80
defconst "F_HIDDEN",,F_HIDDEN,0x20
defconst "F_LENMASK",,F_LENMASK,0x1f
defconst "TRUE",,TRUE,-1
defconst "FALSE",,FALSE,0
defvar "STATE",,STATE,value_FALSE
defvar "S0",,SZ,DS0
; Returns from current word and starts executing the top of the return stack.
defcode "EXIT",,EXIT
POPRSP IP_LREG
rjmp NEXT
defcode "LIT",,LIT
; Push the next word onto the data stack.
movw ZL, IP_LREG
adiw IP_LREG, 2
lpm r0, Z+
lpm r1, Z
PUSHDSP 0
rjmp NEXT
; LEN ADDR -- NAMEPTR
defword "FIND",,FIND
fw LATEST
fw FETCH ; *LATEST LEN ADDR
fw TOR
1: ; =LEN =ADDR R: THIS
fw TWODUP
fw FROMR
fw DUP
fw TOR ; THIS =LEN =ADDR =LEN =ADDR R: THIS
fw INCR
fw INCR
fw DUP
fw INCR ; ADDR &FLAGS =LEN =ADDR =LEN =ADDR
fw SWAP
fw ROMFETCHBYTE ; FLAGS ADDR =LEN =ADDR =LEN =ADDR
fw F_LENMASK
fw F_HIDDEN
fw OR
fw AND ; LEN|HIDDEN ADDR =LEN =ADDR =LEN =ADDR
fw STREQU
zbranch 2f
fw TWODROP
fw FROMR ; Found it!
fw EXIT
2:
fw FROMR
fw ROMFETCH ; NEXT =LEN =ADDR
fw DUP
fw ZEQU
zbranch 2f
fw NROT ; save the zero
fw TWODROP
fw EXIT
2:
fw TOR ; =LEN =ADDR R: NEXT
fw FALSE
zbranch 1b
defword ">CFA",,TOCFA
fw INCR
fw INCR
fw DUP
fw ROMFETCHBYTE
fw F_LENMASK
fw AND
fw INCR ; + 1 to also skip the FLAGS
fw ADD
fw INCR ; + 1 and AND to round to next word boundary
fw TRUE
fw DECR ; 0xFFFE
fw AND
fw EXIT
defword ">DFA",,TODFA
fw TOCFA
fw INCR
fw EXIT
defword "QUIT",,QUIT
fw RZ
fw RSPSTORE
1:
fw INTERPRET
fw FALSE
zbranch 1b
defcode "__EXE",,__EXE
POPDSP 30
; Move codeword address into r22:23 so it's accessible by DOCOL.
movw r22, Z
; Load the codeword.
lpm r20, Z+
lpm r21, Z
; Execute (jump to) the codeword. Eventually NEXT will be called and we'll
; return to EXECUTE, below.
movw ZL, r20
ijmp
; xt --
defword "EXECUTE",,EXECUTE
fw TOCFA
fw __EXE
fw EXIT
defword "INTERPRET",,INTERPRET
fw LCDDISABLE
fw KBENABLE
fw WORD
fw KBDISABLE
fw LCDCLEAR
fw TWODUP
fw TELL
fw TWODUP
fw FIND
fw DUP
zbranch 1f
; found word
fw NROT
fw TWODROP ; drop name
fw DUP ; xt xt
fw INCR
fw INCR
fw ROMFETCHBYTE ; Get len|flags
fw F_IMMED
fw AND
fw ZEQU ; !IMMED xt
fw STATE
fw FETCH
fw AND ; !IMMED&&COMPILING xt
zbranch 2f
; STATE = compiling
fw TOTOK
fw COMMABYTE
fw EXIT
2:
; STATE = executing
fw EXECUTE
fw LCDLINE
fw DSPFETCH
fw WRITEWORD
lit ' '
fw EMIT
fw DUP
fw WRITEWORD
fw EXIT
1:
; Name not found, try and parse number
fw DROP ; drop zero nameptr
fw NUMBER
zbranch 1f