forked from jkotlinski/durexforth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
durexforth.asm
executable file
·1752 lines (1530 loc) · 26 KB
/
durexforth.asm
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
;{{{ The MIT License
;
;Copyright (c) 2008-2013 Johan Kotlinski, Mats Andren
;
;Permission is hereby granted, free of charge, to any person obtaining a copy
;of this software and associated documentation files (the "Software"), to deal
;in the Software without restriction, including without limitation the rights
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;copies of the Software, and to permit persons to whom the Software is
;furnished to do so, subject to the following conditions:
;
;The above copyright notice and this permission notice shall be included in
;all copies or substantial portions of the Software.
;
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;THE SOFTWARE. }}}
; ACME assembler
!cpu 6510
!to "durexforth.prg", cbm ; set output file and format
* = $801
!byte $b, $08, $a, 0, $9E, $32, $30, $36, $31, 0, 0, 0 ; basic header
F_IMMED = $80
F_HIDDEN = $40
F_NO_TAIL_CALL_ELIMINATION = $20 ; when set, skips tailcall elimination
STRLEN_MASK = $1f
TIB = $200
; zeropage
X_INIT = 0
MSB = $73 ; msb stack is [$3b, $72]
LSB = $3b ; lsb stack is [3, $3a]
W = $8b ; rnd seed
W2 = $8d ; rnd seed
W3 = $9e ; tape error log
OP_NOP = $ea
OP_JMP = $4c
OP_JSR = $20
OP_RTS = $60
OP_INX = $e8
OP_LDA_I = $a9
OP_STA_AX = $9d
GETCHR = $ffe4 ; get char from kbd
PUTCHR = $ffd2 ; put char
CURSOR_COLOR = $286
K_F1 = $85
K_F3 = $86
K_BACKSPACE = $14
K_RETURN = $d
K_CLRSCR = $93
K_SPACE = ' '
K_REVERSE = $c7
K_DOWN = $11
K_RIGHT = $1d
K_UP = $91
K_LEFT = $9d
C_YELLOW = 7
!ct pet
; -------- program start
jsr quit_reset
jsr PAGE
lda #%00010110 ; lowercase
sta $d018
_START = * + 1
jsr load_base
restore_handler
cli
jmp QUIT
; ----------- macros
!set LINK = 0
!macro BACKLINK {
; it's tempting to add the string as a macro parameter,
; but this does not seem to be supported by ACME.
!word LINK
!set LINK = * - 2
}
+BACKLINK
!byte 6
!text "pushya"
pushya
dex
sta LSB, x
sty MSB, x
rts
!macro VALUE .word {
lda #<.word
ldy #>.word
jmp pushya
}
; ---------- words
; START - points to the code of the startup word.
+BACKLINK
!byte 5
!text "start"
+VALUE _START
+BACKLINK
!byte 2
!text "bl"
BL
+VALUE K_SPACE
+BACKLINK
!byte 3
!text "msb"
+VALUE MSB
+BACKLINK
!byte 3
!text "lsb"
+VALUE LSB
+BACKLINK
!byte 6
!text "source"
dex
dex
lda TIB_PTR
sta LSB+1, x
lda TIB_PTR + 1
sta MSB+1, x
lda TIB_SIZE
sta LSB, x
lda TIB_SIZE + 1
sta MSB, x
rts
TIB_PTR
!word 0
TIB_SIZE
!word 0
; DROP
+BACKLINK
!byte 4 | F_IMMED
!text "drop"
DROP
lda STATE
bne +
inx
rts
+ lda #OP_INX
jmp compile_a
; SWAP
+BACKLINK
!byte 4
!text "swap"
SWAP
ldy MSB, x
lda MSB + 1, x
sta MSB, x
sty MSB + 1, x
ldy LSB, x
lda LSB + 1, x
sta LSB, x
sty LSB + 1, x
rts
+BACKLINK
!byte 3
!text "dup"
DUP
dex
lda MSB + 1, x
sta MSB, x
lda LSB + 1, x
sta LSB, x
rts
+BACKLINK
!byte 4
!text "?dup"
QDUP
lda MSB, x
ora LSB, x
bne DUP
rts
; OVER
+BACKLINK
!byte 4
!text "over"
OVER
dex
lda MSB + 2, x
sta MSB, x
lda LSB + 2, x
sta LSB, x
rts
+BACKLINK
!byte 4
!text "2dup"
TWODUP
jsr OVER
jmp OVER
; 1+
+BACKLINK
!byte 2
!text "1+"
ONEPLUS
inc LSB, x
bne +
inc MSB, x
+ rts
; 1-
+BACKLINK
!byte 2
!text "1-"
ONEMINUS
lda LSB, x
bne +
dec MSB, x
+ dec LSB, x
rts
; +
+BACKLINK
!byte 1
!text "+"
PLUS
lda LSB, x
clc
adc LSB + 1, x
sta LSB + 1, x
lda MSB, x
adc MSB + 1, x
sta MSB + 1, x
inx
rts
!src "math.asm"
+BACKLINK
!byte 1
!text "="
EQUAL
ldy #0
lda LSB, x
cmp LSB + 1, x
bne +
lda MSB, x
cmp MSB + 1, x
bne +
dey
+ inx
sty MSB, x
sty LSB, x
rts
; 0=
+BACKLINK
!byte 2
!text "0="
ZEQU
ldy #0
lda MSB, x
bne +
lda LSB, x
bne +
dey
+ sty MSB, x
sty LSB, x
rts
; AND
+BACKLINK
!byte 3
!text "and"
lda MSB, x
and MSB + 1, x
sta MSB + 1, x
lda LSB, x
and LSB + 1, x
sta LSB + 1, x
inx
rts
; !
+BACKLINK
!byte 1
!text "!"
STORE
lda LSB, x
sta W
lda MSB, x
sta W + 1
ldy #0
lda LSB+1, x
sta (W), y
iny
lda MSB+1, x
sta (W), y
inx
inx
rts
; @
+BACKLINK
!byte 1
!text "@"
FETCH
lda LSB,x
sta W
lda MSB,x
sta W+1
ldy #0
lda (W),y
sta LSB,x
iny
lda (W),y
sta MSB,x
rts
; C!
+BACKLINK
!byte 2
!text "c!"
STOREBYTE
lda LSB,x
sta + + 1
lda MSB,x
sta + + 2
lda LSB+1,x
+ sta $cafe
inx
inx
rts
; C@
+BACKLINK
!byte 2
!text "c@"
FETCHBYTE
lda LSB,x
sta + + 1
lda MSB,x
sta + + 2
+ lda $cafe
sta LSB,x
lda #0
sta MSB,x
rts
; FILL ( start len char -- )
+BACKLINK
!byte 4
!text "fill"
FILL
lda LSB, x
tay
lda LSB + 2, x
sta .fdst
lda MSB + 2, x
sta .fdst + 1
lda LSB + 1, x
eor #$ff
sta W
lda MSB + 1, x
eor #$ff
sta W + 1
inx
inx
inx
-
inc W
bne +
inc W + 1
bne +
rts
+
.fdst = * + 1
sty $ffff ; overwrite
; advance
inc .fdst
bne -
inc .fdst + 1
jmp -
!src "move.asm"
; ---------- variables
; STATE - Is the interpreter executing code (0) or compiling a word (non-zero)?
+BACKLINK
!byte 5
!text "state"
+VALUE STATE
STATE
!word 0
+BACKLINK
!byte 9
!text "source-id"
SOURCE_ID_LSB = * + 1
SOURCE_ID_MSB = * + 3
; -1 : string (via evaluate)
; 0 : keyboard
; 1+ : file id
+VALUE 0
+BACKLINK
!byte 8
!text "evaluate" ; ( addr size -- )
EVALUATE
jsr SAVE_INPUT
lda LSB + 1, x
sta TIB_PTR
lda MSB + 1, x
sta TIB_PTR + 1
jsr PLUS
lda LSB, x
sta .bufend
lda MSB, x
sta .bufend + 1
inx
jsr evaluate_get_new_line
ldy #$ff
sty SOURCE_ID_LSB
sty SOURCE_ID_MSB
.eval_loop
lda TIB_PTR + 1
cmp .bufend + 1
bcc +
lda TIB_PTR
cmp .bufend
bcc +
jmp RESTORE_INPUT ; exit
+
jsr interpret_tib
jsr REFILL
jmp .eval_loop
evaluate_get_new_line
ldy #0
sty TO_IN
sty TO_IN + 1
; Determines TIB_SIZE.
lda TIB_PTR
sta W
lda TIB_PTR + 1
sta W + 1
.findtibsizeloop
lda .bufend + 1
cmp W + 1
bcc .foundeol
bne +
lda W
cmp .bufend
bcs .foundeol
+
ldy #0
lda (W),y
cmp #K_RETURN
beq .foundeol
inc W
bne +
inc W + 1
+
jmp .findtibsizeloop
.foundeol
lda W
sec
sbc TIB_PTR
sta TIB_SIZE
lda W + 1
sbc TIB_PTR + 1
sta TIB_SIZE + 1
rts
evaluate_consume_tib
lda TIB_PTR
clc
adc TIB_SIZE
sta TIB_PTR
lda TIB_PTR + 1
adc TIB_SIZE + 1
sta TIB_PTR + 1
inc TIB_PTR ; skip cr
bne +
inc TIB_PTR + 1
+ rts
.bufend
!word 0
SAVE_INPUT_STACK
!fill 9*4
SAVE_INPUT_STACK_DEPTH
!byte 0
push_input_stack
ldy SAVE_INPUT_STACK_DEPTH
sta SAVE_INPUT_STACK, y
inc SAVE_INPUT_STACK_DEPTH
rts
pop_input_stack
dec SAVE_INPUT_STACK_DEPTH
ldy SAVE_INPUT_STACK_DEPTH
lda SAVE_INPUT_STACK, y
rts
SAVE_INPUT
lda READ_EOF
jsr push_input_stack
lda #0
sta READ_EOF
lda TO_IN
jsr push_input_stack
lda TO_IN+1
jsr push_input_stack
lda SOURCE_ID_LSB
jsr push_input_stack
lda SOURCE_ID_MSB
jsr push_input_stack
lda TIB_PTR
jsr push_input_stack
lda TIB_PTR+1
jsr push_input_stack
lda TIB_SIZE
jsr push_input_stack
lda TIB_SIZE+1
jmp push_input_stack
RESTORE_INPUT
jsr pop_input_stack
sta TIB_SIZE+1
jsr pop_input_stack
sta TIB_SIZE
jsr pop_input_stack
sta TIB_PTR+1
jsr pop_input_stack
sta TIB_PTR
jsr pop_input_stack
sta SOURCE_ID_MSB
jsr pop_input_stack
sta SOURCE_ID_LSB
jsr pop_input_stack
sta TO_IN+1
jsr pop_input_stack
sta TO_IN
jsr pop_input_stack
sta READ_EOF
rts
; HERE - points to the next free byte of memory. When compiling, compiled words go here.
+BACKLINK
!byte 4
!text "here"
HERE
HERE_LSB = * + 1
HERE_MSB = * + 3
+VALUE _LATEST + 2
+BACKLINK
!byte 1
!text "0"
ZERO
lda #0
tay
jmp pushya
; ------------ i/o
; EMIT
+BACKLINK
!byte 4
!text "emit"
EMIT
lda LSB, x
inx
jmp PUTCHR
+BACKLINK
!byte 5
!text "count"
COUNT
jsr DUP
jsr ONEPLUS
jsr SWAP
jmp FETCHBYTE
+BACKLINK
!byte 4
!text "page"
PAGE
lda #K_CLRSCR
jmp PUTCHR
; TODO: Use HERE instead of a separate WORD_BUFFER?
WORD_BUFFER
WORD_BUFFER_LENGTH
!byte 0
MAX_WORD_LENGTH = 31
WORD_BUFFER_DATA
!fill MAX_WORD_LENGTH
tmp_x
!byte 0
+BACKLINK
!byte 4
!text "key?"
lda $c6 ; number of characters in keyboard buffer
beq +
.pushtrue
lda #$ff
+ tay
jmp pushya
+BACKLINK
!byte 3
!text "key"
- lda $c6
beq -
stx W
jsr $e5b4
ldx W
ldy #0
jmp pushya
+BACKLINK
!byte 4
!text "getc"
GETC
jsr GET_CHAR_FROM_TIB
bne +
jsr REFILL
lda #K_RETURN
+ ldy #0
jmp pushya
+BACKLINK
!byte 4
!text "char"
-
dex
lda #K_SPACE
sta LSB,x
jsr WORD
inx
lda WORD_BUFFER_LENGTH
bne +
jsr REFILL
jmp -
+
lda WORD_BUFFER_DATA
ldy #0
jmp pushya
GET_CHAR_BLOCKING
stx tmp_x
-
jsr CHRIN ; wastes x
pha
jsr READST
sta READ_EOF
pla
ora #0
beq -
ldx tmp_x
rts
+BACKLINK
!byte 3
!text ">in"
+VALUE TO_IN
TO_IN
!word 0
+BACKLINK
!byte 6
!text "refill" ; ( -- )
REFILL
READ_EOF = * + 1
lda #0
beq +
; handle EOF
stx tmp_x
lda SOURCE_ID_LSB
jsr CLOSE
dec SOURCE_ID_LSB
ldx SOURCE_ID_LSB
jsr CHKIN
ldx tmp_x
jmp RESTORE_INPUT ; exit
+
lda SOURCE_ID_MSB
beq +
jsr evaluate_consume_tib
jmp evaluate_get_new_line
+
ldy #0
sty TO_IN
sty TO_IN + 1
sty TIB_SIZE
sty TIB_SIZE + 1
lda #<TIB
sta TIB_PTR
lda #>TIB
sta TIB_PTR + 1
lda SOURCE_ID_LSB
beq .getLineFromConsole
lda SOURCE_ID_MSB
beq .getLineFromDisk
.getLineFromConsole
stx tmp_x
ldx #0
- jsr $e112 ; input char
cmp #$d
beq .gotReturn
sta TIB,x
cpx #$58
beq -
inx
jmp -
.gotReturn
jsr PUTCHR
; Set TIB_SIZE to number of chars fetched.
stx TIB_SIZE
ldx tmp_x
rts
.getLineFromDisk
jsr GET_CHAR_BLOCKING
cmp #K_RETURN
beq +
inc $d020
ldy TIB_SIZE
sta TIB,y
inc TIB_SIZE
dec $d020
jmp .getLineFromDisk
+
rts
GET_CHAR_FROM_TIB
lda TO_IN
cmp TIB_SIZE
bne +
lda TO_IN + 1
cmp TIB_SIZE + 1
bne +
lda #0
rts
+
clc
lda TIB_PTR
adc TO_IN
sta W
lda TIB_PTR + 1
adc TO_IN + 1
sta W + 1
ldy #0
lda (W),y
; pha
; jsr PUTCHR ; debug
; pla
inc TO_IN
bne +
inc TO_IN + 1
+ rts
; WORD ( delim -- strptr )
+BACKLINK
!byte 4
!text "word"
WORD
lda #0
sta WORD_BUFFER_LENGTH
; skips initial delimiters.
-
jsr GET_CHAR_FROM_TIB
beq .word_end
cmp LSB, x
beq -
jmp .append
.get_char
jsr GET_CHAR_FROM_TIB
beq .word_end
cmp LSB,x
beq .word_end
.append
ldy WORD_BUFFER_LENGTH
sta WORD_BUFFER_DATA,y
iny
sty WORD_BUFFER_LENGTH
tya
cmp #MAX_WORD_LENGTH
bne .get_char
.word_end
lda #<WORD_BUFFER
sta LSB, x
lda #>WORD_BUFFER
sta MSB, x
rts
FIND_BUFFER_SIZE = 31
FIND_BUFFER
!fill FIND_BUFFER_SIZE
+BACKLINK
!byte 4
!text "find"
FIND ; ( str -- str 0 | xt 1 | xt -1 )
txa
pha
lda MSB, x
sta W2 + 1
lda LSB, x
sta W2 ; TMP2 contains pointer to find string
ldy #0
lda (W2), y ; get length of find string
beq .find_failed
; store findlen
sta .findlen + 1
sta .findlen2 + 1
tay
- lda (W2), y
jsr CHAR_TO_LOWERCASE
sta FIND_BUFFER-1, y
dey
beq +
jmp -
+
ldx _LATEST
lda _LATEST + 1
.examine_word
sta W + 1
stx W
; W now contains new dictionary word.
ldy #2
lda (W), y ; get string length of dictionary word
and #STRLEN_MASK | F_HIDDEN ; include hidden flag... so we don't find the hidden words.
.findlen
cmp #0
beq .string_compare
.word_not_equal
; no match, advance the linked list.
ldy #0
lax (W), y
iny
lda (W), y
; Is word null? If not, examine it.
bne .examine_word
; It is null - give up.
.find_failed
pla
tax
dex
lda #0
sta LSB, x
sta MSB, x
rts
.string_compare ; y = 2
; equal strlen, now compare strings...
.findlen2
lda #0
sta .strlen
- iny
lda FIND_BUFFER-3, y ; find string
cmp (W), y ; dictionary string
bne .word_not_equal
dec .strlen
beq .word_is_equal
jmp -
.strlen !byte 0
.word_is_equal
; return address to dictionary word
pla
tax
lda W
sta LSB, x
lda W + 1
sta MSB, x
jsr TCFA
dex
ldy #2
lda (W), y
and #F_NO_TAIL_CALL_ELIMINATION | F_IMMED
sta FOUND_WORD_WITH_NO_TCE
lda (W), y ; a contains string length + mask
and #F_IMMED
beq .not_immed
dey
sty LSB, x ; 1
dey
sty MSB, x ; 0
rts
.not_immed
lda #$ff
sta LSB, x
sta MSB, x
rts
; >CFA
+BACKLINK
!byte 4
!text ">cfa"
TCFA
lda MSB, x
sta W + 1
lda LSB, x
sta W
; W contains pointer to word
ldy #2
lda (W), y ; a contains string length + mask
and #STRLEN_MASK
clc
adc #3 ; offset for link + string length
sta .addend + 1
lda LSB, x
.addend
adc #0
sta LSB, x
bcc +
inc MSB, x
+ rts
!src "number.asm"
+BACKLINK
!byte 7 | F_IMMED
!text "literal"
LITERAL
dex