-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbootstrap.s
More file actions
1397 lines (1226 loc) · 22.6 KB
/
Copy pathbootstrap.s
File metadata and controls
1397 lines (1226 loc) · 22.6 KB
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
; This file bootstraps enough code to start a STC Forth
; for the 6502 system. It will allow programming NES programs
; using Forth. It's modelled using a NES-like memory
; map:
; 00-FF: zero page
; 100-1FF: hardware stack
; 200-7FF: User RAM
; 8000-FFFF: Cartridge space
;
; Unlike a NES cartridge, initially the cartridge space is
; RAM. The code will run in the simulator, and the user can
; interact with it, test functions, etc. Once they execute
; The word FREEZE, however, the contents of RAM from 8000-FFFF
; will be extracted and put into a .nes file with an appropriate
; header.
;
; The implementation loosely follows Jones Forth.
;
; Specifics:
;
; - The data stack is stored in the zero page with the x register
; as the stack pointer.
; - Words are compiled to a series of jsr instructions, or to inline
; code.
; - The words are relocatable. The entry point for each word is the same
; as the dictionary entry.
.macpack generic
.include "forth.inc"
.import DICT_END, TMP_DICT_END
.import CHERE_PERM_INIT, CHERE_TMP_INIT
.import VHERE_PERM_INIT, VHERE_TMP_INIT
; The simulator uses this address as the IO port.
; A write to this address will output that character
; to stdout, and a read will read the next character from
; stdin, possibly blocking, since the simulator reads line-by-
; line.
IO_PORT := $401C
; Flags for the dictionary entries.
; The word should execute immediately even in compile mode
F_IMMED = $80
; The word should not be found in a dictionary search.
F_HIDDEN = $20
; Always inline the word
F_INLINE = $40
; This is the last entry in the dictionary
F_END = $8000
; This is a single-byte val
F_SINGLE_BYTE = $4000
.segment "DICT"
; Reserve space to push the dictionary to the end of the memory
; space, since it now grows down.
.res $D13
DHERE_PERM_INIT:
.segment "TMP_DICT"
.res $671
DHERE_TMP_INIT:
.segment "ZEROPAGE": zeropage
TMP1: .res 1
TMP2: .res 1
TMP3: .res 1
TMP4: .res 1
TMP5: .res 1
TMP6: .res 1
TMP7: .res 1
TMP8: .res 1
Stack: .res 80
Stack_End: .res 8 ; buffer zone
ControlFlowSP: .res 1
ControlFlowStack: .res 32
ControlFlowStackEnd:
.segment "STACK"
; Reserve the hardware stack.
RStack: .res $100
defconsttmp "dict::impl", DictEntry::CodePtr, DictEntryCodePtr
defconsttmp "dict::len", DictEntry::Len, DictEntryLen
defconsttmp "dict::flags", DictEntry::Flags2, DictEntryFlags
defconsttmp "dict::name", DictEntry::Name, DictEntryName
defconst "c-sp", ControlFlowSP, C_SP
defconst "cstack", ControlFlowStack, CSTACK
; ( a -- )
defword "drop", F_INLINE, DROP
pop
rts
; ( a b -- b a )
defword "swap", 0, SWAP
lda Stack+0, x
ldy Stack+2, x
sty Stack+0, x
sta Stack+2, x
lda Stack+1, x
ldy Stack+3, x
sty Stack+1, x
sta Stack+3, x
rts
; ( a -- a a )
defword "dup", 0, DUP
dex
dex
lda Stack+2, x
sta Stack+0, x
lda Stack+3, x
sta Stack+1, x
rts
; ( a -- a + 1 )
defword "1+", 0, INCR
inc Stack, x
beq @hi
rts
@hi:
inc Stack+1, x
rts
defword "1-", 0, DECR
sec
lda Stack, x
sbc #1
sta Stack, x
lda Stack+1, x
sbc #0
sta Stack+1, x
rts
defword "+", 0, ADD
clc
lda Stack, x
adc Stack+2, x
sta Stack+2, x
lda Stack+1, x
adc Stack+3, x
sta Stack+3, x
pop
rts
; Divides a single byte by 3, truncating:
defword "3/", 0, DIV3
ldy #$FF
lda #$FD
:
clc
adc #3
iny
cpy #86
bge @done
cmp Stack, x
beq @exact
blt :-
@done:
dey
@exact:
sty Stack, x
lda #0
sta Stack+1, x
rts
defword "-", 0, SUB
sec
lda Stack+2, x
sbc Stack, x
sta Stack+2, x
lda Stack+3, x
sbc Stack+1, x
sta Stack+3, x
pop
rts
defword "!", 0, STORE
toTMP1
ldy #0
lda Stack+2, x
sta (TMP1), y
lda Stack+3, x
iny
sta (TMP1), y
pop
pop
rts
defword "c!", 0, CSTORE
lda Stack+2, x
sta (Stack, x)
pop
pop
rts
defword "@", 0, FETCH
toTMP1
ldy #0
lda (TMP1), y
sta Stack, x
iny
lda (TMP1), y
sta Stack+1, x
rts
defword "c@", 0, CFETCH
lda (Stack, x)
sta Stack, x
lda #0
sta Stack+1, x
rts
; ( ptr1 ptr2 n -- )
defword "cmove", 0, CMOVE
defwordtmp "key", 0, KEY
dex
dex
lda IO_PORT
ldy #0
sta Stack, x
sty Stack+1, x
rts
; ( -- str-ptr len )
defwordtmp "word", 0, WORD
lda #0
sta TMP1
jsr KEY
lda Stack, x
pop
cmp #'\'
beq @skipComment
cmp #' ' + 1
bcc WORD_IMPL
@loop:
ldy TMP1
sta word_buffer, y
inc TMP1
jsr KEY
lda Stack, x
pop
cmp #' ' + 1
bge @loop
ldy TMP1
; iny
lda #0
sta word_buffer, y ; zero terminate
push word_buffer
dex
dex
lda TMP1
sta Stack, x
lda #0
sta Stack+1, x
rts
@skipComment:
jsr KEY
lda Stack, x
pop
cmp #$A ; \n
bne @skipComment
beq WORD_IMPL ; bra
.segment "TMP_VARIABLES"
word_buffer: .res 32
defvartmp "base", 10, BASE
; ( str len -- parsed-number non-zero )
; or ( str len -- str len 0 ) on error
defwordtmp "number", 0, NUMBER
Str := TMP1
Result := TMP3
Len := TMP5
ResultAcc := TMP6
lda Stack, x
sta Len ; save string length
lda Stack+2, x
sta Str
lda Stack+3, x
sta Str+1 ; save str pointer
lda #0
sta Result
sta Result+1
txa
pha ; save the stack pointer
ldy #0
@eachDigit:
lda (Str), y
cmp #'A'
bcc @digit ; if ascii < 'A', treat is as a decimal digit
and #<~$20 ; convert to uppercase letter.
sbc #'A' - '9' - 1 ; move it to the range $3A+, right after '9'.
@digit:
sec
sbc #'0' ; now it should be in a range of 0-BASE-1
cmp BASE_VALUE
bcs @invalid ; if digit >= BASE
pha ; save the digit
ldx BASE_VALUE ; loop BASE times, to multiply Result by BASE using repeated addition
lda #0
sta ResultAcc
sta ResultAcc+1 ; reset ResultAcc
@multLoop:
clc
lda ResultAcc
adc Result
sta ResultAcc
lda ResultAcc+1
adc Result+1
sta ResultAcc+1 ; add Result to ResultAcc
dex
bne @multLoop
pla ; get the digit
clc
adc ResultAcc
sta Result
lda ResultAcc+1
adc #0
sta Result+1 ; Result = ResultAcc + Digit
iny
cpy Len
bne @eachDigit ; loop again for the next digit until we've exhausted the string.
pla
tax
pop ; drop the string length
lda Result
sta Stack, x
lda Result+1
sta Stack+1, x ; put parsed number of the stack.
push HANDLE_NUMBER
rts
@invalid:
pla
tax ; restore stack pointer
push 0
rts
defwordtmp "d:perm", 0, DICT_PERM
push DHERE_PERM
jsr FETCH
jmp DFIND
defwordtmp "d:tmp", 0, DICT_TMP
push DHERE_TMP
jsr FETCH
jmp DFIND
defwordtmp "hDict", 0, HANDLE_DICT
; now that we have the dictionary entry, check if it's immediate.
toTMP1
ldy #(DictEntry::Len)
lda (TMP1), y
and #F_IMMED
bne @execute
lda STATE_VALUE
beq @execute
@compiling:
lda (TMP1), y
and #F_INLINE
bne @inline
push JSR_OP
jsr CCOMMA
jmp COMMA
@inline: ; simple inlining -- just copy until you see an rts.
jmp INLINE
@execute:
pop ; drop dictionary pointer
jmp (TMP1) ; tailcall to the word, which will return to QUIT
; ( str-ptr len dict-start -- dictionary-pointer handler )
; or ( str-ptr len dict-start -- str-ptr len 0 ) if it wasn't found
; Searches the dictionary at the given start for the given word.
defwordtmp "dfind", 0, DFIND
LPointer := TMP1
MyStr := TMP3
lda Stack, x
sta LPointer
lda Stack+1, x
sta LPointer+1
inx
inx
sec
lda Stack+2, x
sbc #<(DictEntry::Name)
sta MyStr
lda Stack+3, x
sbc #>(DictEntry::Name)
sta MyStr+1 ; put the string pointer - name offset in MyStr
; that way we can use lda (MyStr), y to read
; MyStr using the same offset that we do from LPointer
; check if LPointer matches
@loop:
ldy #(DictEntry::Len)
lda (LPointer), y ; get length from dictionary entry
and #$1F | F_HIDDEN ; mask to get length, allowing hidden to change length.
cmp Stack, x ; compare to the length on the stack
bne @next ; if lengths don't match, follow the next pointer.
; now compare the strings.
lda Stack, x
clc
adc #(DictEntry::Name) - 1
tay ; Init y to check the end of the string
@strcmpLoop:
lda (LPointer), y
cmp (MyStr), y
bne @next
dey
cpy #(DictEntry::Len)
beq @found
bne @strcmpLoop ; bra
@next:
; Check if this is the last dictionary entry
ldy #(DictEntry::Flags2)
lda (LPointer), y
and #>F_END
bne @notFound
ldy #(DictEntry::Len)
lda (LPointer), y
and #$1F ; TODO - magic constant
clc
adc LPointer
sta LPointer
lda #0
adc LPointer + 1
sta LPointer + 1 ; add string length to LPointer
lda #DictEntry::Name
clc
adc LPointer
sta LPointer
lda #0
adc LPointer + 1
sta LPointer + 1 ; add the other bytes of the header.
clv
bvc @loop ; bra
@notFound:
dex
dex
lda #0
sta Stack, x
sta Stack+1, x
rts
@found:
pop
lda LPointer
sta Stack, x
lda LPointer+1
sta Stack+1, x
push HANDLE_DICT
rts
defwordtmp "hNum", 0, HANDLE_NUMBER
lda STATE_VALUE
beq @executeLiteral
@compileLiteral:
jsr ASM
.byte 1, DEX_OP ; DEX
jsr ASM
.byte 1, DEX_OP ; DEX
jsr DUP ; dup
jsr ASM
.byte 2, LDA_IMM_OP ; LDA.#
push Stack
jsr ASM
.byte 2, STA_ZP_X_OP ; stack STA.ZX
lda Stack+1, x
sta Stack, x ; >byte
jsr ASM
.byte 2, LDA_IMM_OP ; LDA.#
push Stack+1
jsr ASM
.byte 2, STA_ZP_X_OP ; stack 1+ STA.ZX
rts
@executeLiteral:
rts
PushTemplate:
dex
dex
PushTemplateLo := *+1
lda #0
sta Stack, x
PushTemplateHi := *+1
lda #0
sta Stack+1, x
EndPushTemplate:
defwordtmp "d:asm", 0, DICT_ASM
lda Stack+2, x
sta TMP1
lda Stack+3, x
sta TMP2
jsr ParseInstruction
lda Stack, x ; check the length of the instruction parsed.
beq :+
; Remove the string
lda Stack, x
sta Stack+4, x
lda Stack+1, x
sta Stack+5, x
lda Stack+2, x
sta Stack+6, x
lda Stack+3, x
sta Stack+7, x
pop
pop
jsr SWAP
jsr DUP
jsr DIV3
jsr ADD
jsr SWAP
push ASM_COMP
: rts
; ( str-ptr len -- dictionary-pointer )
; or ( str-ptr len -- str-ptr len 0 ) if it wasn't found
; Searches the dictionary for a definition of the given word.
defwordtmp "find", 0, FIND
jsr DICT_PERM
cmpTopZero
beq :+
pop
rts
: pop
jsr DICT_TMP
cmpTopZero
beq :+
pop
: rts
; Given a pointer, gets the name of the dictionary entry,
; or an empty string if it's not in the dictionary.
; ( addr -- addr len )
defwordtmp "rfind", 0, RFIND
@LPointer := TMP1
push DHERE_PERM
jsr FETCH
jsr @rfind_impl
lda Stack, x
ora Stack+1, x
bne @found
pop
push DHERE_TMP
jsr FETCH
jsr @rfind_impl
@found:
rts
@rfind_impl:
lda Stack, x
sta @LPointer
lda Stack+1, x
sta @LPointer+1
pop
@loop:
lda @LPointer
cmp Stack, x
bne @ne
lda @LPointer+1
cmp Stack+1, x
bne @ne
; We've found the dictionary entry, now return the string.
ldy #DictEntry::Len
lda (@LPointer), y
and #$1F ; mask just the length, no flags.
dex
dex
sta Stack, x
lda #0
sta Stack+1, x
lda @LPointer
clc
adc #DictEntry::Name
sta Stack+2, x
lda @LPointer+1
adc #0
sta Stack+3, x
rts
@ne:
ldy #DictEntry::Len
lda (@LPointer), y
and #31 ; TODO - magic number
clc
adc @LPointer
sta @LPointer
lda #0
adc @LPointer+1
sta @LPointer+1
lda #DictEntry::Name
clc
adc @LPointer
sta @LPointer
lda #0
adc @LPointer+1
sta @LPointer+1
lda @LPointer+1
cmp #>DICT_END ; assumes DICT_END is the last entry in the dictionary
blt @loop
lda @LPointer
cmp #<DICT_END
beq @loop
blt @loop
@notFound:
; Didn't find anything, so return 0 len string and the original address.
dex
dex
lda #0
sta Stack, x
sta Stack+1, x
rts
defconst "<perm>", HERE_PERM, PERM_LATEST
defconst "<tmp>", HERE_TMP, TMP_LATEST
defwordtmp "definitions:", 0, DEFINITIONS
jsr HERE
jsr STORE
rts
defword "dicts", 0, DICTS
push 0
push DHERE_TMP
jsr FETCH
push DHERE_PERM
jmp FETCH
; Points to a list of 3 RAM addresses, holding
; the current HERE pointers, representing where
; to place the next code, dictionary, or variable bytes.
defvartmp "here", HERE_PERM, HERE
; Gives the address of the next free byte of code
defwordtmp "chere", 0, CHERE
jsr HERE
jmp FETCH
; Gives the address of the next byte to add to the dictionary
defwordtmp "dhere", 0, DHERE
jsr HERE
jsr FETCH
push 2
jmp ADD
; Gives the address of the next free byte of RAM
defwordtmp "vhere", 0, VHERE
jsr HERE
jsr FETCH
push 4
jmp ADD
.segment "TMP_VARIABLES"
HERE_TMP:
CHERE_TMP: .word CHERE_TMP_INIT
DHERE_TMP: .word DHERE_TMP_INIT
VHERE_TMP: .word VHERE_TMP_INIT
HERE_PERM:
CHERE_PERM: .word CHERE_PERM_INIT
DHERE_PERM: .word DHERE_PERM_INIT
VHERE_PERM: .word VHERE_PERM_INIT
JMP_OP = $4C
; ( str-ptr len -- )
; Creates a new dictionary entry
defwordtmp "create", 0, CREATE
; The dict entry needs str-len + DictEntry::Name bytes of space.
; Subtract from DHERE to allocate the space.
jsr DUP ; str length
push DictEntry::Name
jsr ADD
jsr DHERE
jsr FETCH
jsr SWAP
jsr SUB
jsr DHERE
jsr STORE
; Update LATEST to the new entry
;jsr DHERE
;jsr FETCH
;jsr LATEST
;jsr STORE
; ( stack is now string-ptr len )
; Store length in new entry
; DUP DHERE Len + C!
jsr DUP
jsr DHERE
jsr FETCH
push DictEntry::Len
jsr ADD
jsr CSTORE
; Write jmp CHERE to the dictionary entry.
; JMP_OP dhere @ Jmp + c!
push JMP_OP
jsr DHERE
jsr FETCH
push DictEntry::Jmp
jsr ADD
jsr CSTORE
; chere @ dhere @ codeptr + !
jsr CHERE
jsr FETCH
jsr DHERE
jsr FETCH
push DictEntry::CodePtr
jsr ADD
jsr STORE
; Get DHERE value
jsr DHERE
jsr FETCH
; Add the offset to the name part
clc
lda #<(DictEntry::Name)
adc Stack, x
sta TMP3
lda #>(DictEntry::Name)
adc Stack+1, x
sta TMP4
pop ; Drop DHERE value
; now we need to copy the name string.
lda Stack, x ; get length
tay
dey
lda Stack+2, x
sta TMP1
lda Stack+3, x
sta TMP2 ; copy str pointer into TMP1
@loop:
lda (TMP1), y
sta (TMP3), y
dey
bpl @loop
pop
pop
rts
defwordtmp ",", 0, COMMA
jsr CHERE
jsr FETCH
jsr STORE
jsr CHERE
jsr FETCH
jsr INCR
jsr INCR
jsr CHERE
jmp STORE
defwordtmp "c,", 0, CCOMMA
jsr CHERE
jsr FETCH
jsr CSTORE
jsr CHERE
jsr FETCH
jsr INCR
jsr CHERE
jmp STORE
defvartmp "state", 0, STATE
defwordtmp "[", F_IMMED, LSQUARE
lda #0
sta STATE_VALUE
sta STATE_VALUE+1
rts
defwordtmp "]", 0, RSQUARE
lda #1
sta STATE_VALUE
lda #0
sta STATE_VALUE+1
rts
defwordtmp ":", 0, COLON
jsr WORD
jsr CREATE ; create the dictionary entry
jsr DHERE
jsr FETCH
jsr HIDDEN ; toggle hidden in the entry
jmp RSQUARE ; enter compile mode
RTS_OP = $60
defwordtmp ";", F_IMMED, SEMICOLON
; append rts to code
jsr ASM
.byte 1, RTS_OP
jsr DHERE
jsr FETCH
jsr HIDDEN ; toggle hidden flag
jmp LSQUARE ; go back to immediate mode
; ( dict-ptr -- )
; Marks the dictionary entry as hidden
defwordtmp "hidden", 0, HIDDEN
toTMP1
ldy #(DictEntry::Len)
lda #F_HIDDEN
eor (TMP1), y
sta (TMP1), y
pop
rts
defwordtmp "c-val-tog", 0, C_VAL_TOGGLE
toTMP1
ldy #(DictEntry::Flags2)
lda #>F_SINGLE_BYTE
eor (TMP1), y
sta (TMP1), y
pop
rts
defwordtmp "hide", 0, HIDE
jsr WORD
jsr FIND
jmp HIDDEN
JSR_OP = $20
defwordtmp "[asm]", 0, ASM
pla
sta TMP1
pla
sta TMP2
lda TMP1
clc
adc #2
tay
lda TMP2
adc #0
pha ; add 2 to the return address because there are 2 bytes of parameters.
tya
pha
; Move the two parmeters to the stack.
ldy #2
lda (TMP1), y
dex
dex
sta Stack, x
lda #0
sta Stack+1, x
dey
lda (TMP1), y
dex
dex
sta Stack, x
lda #0
sta Stack+1, x
jmp AsmCompExec
; ( arg? instruction n-bytes -- )
; Given an optional argument, the number
; of bytes in the instruction, and the instruction
; number, compiles the instruction and arg to
; the code space.
defwordtmp "asm-comp", 0, ASM_COMP
lda STATE_VALUE
beq AsmCompExec
@compile:
push JSR_OP
jsr CCOMMA
push ASM
jsr COMMA
jsr CCOMMA
jsr CCOMMA
rts
AsmCompExec:
lda Stack, x
pha
pop
jsr CCOMMA ; write instruction byte
pla
cmp #2
beq @two
blt @one
@three:
jmp COMMA
@two:
jmp CCOMMA
@one:
rts
defwordtmp "quit", 0, QUIT
stx TMP1
cpx #Stack_End - Stack
bge @underflow
ldx #$FF
txs
ldx TMP1
jsr INTERPRET
jmp QUIT
@underflow:
jsr DODOTQUOTE
.asciiz "Stack underflow detected!"
ldx #Stack_End - Stack - 1
jmp QUIT
; ( xt -- )
; Inlines the code of the execution token,
; stopping when it reaches an RTS instruction.
INLINE:
push DictEntry::CodePtr
jsr ADD
jsr FETCH
@loop2:
jsr DUP
jsr CFETCH
lda Stack, x
cmp #$60
beq @doneInlining2
jsr CCOMMA
jsr INCR
clv
bvc @loop2
@doneInlining2:
pop ; drop the two stack values
pop
rts
DEX_OP = $CA
LDA_IMM_OP = $A9
STA_ZP_X_OP = $95
defword "execute", 0, EXECUTE
toTMP1
pop
jmp (TMP1)
defwordtmp "interpret", 0, INTERPRET
.macro tryDict dict
jsr dict
cmpTopZero
bne @found
pop
.endmacro
jsr WORD
tryDict DICT_PERM
tryDict DICT_TMP
tryDict NUMBER
tryDict DICT_ASM
@notFound: