-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathforth.s
More file actions
1518 lines (1205 loc) · 41.2 KB
/
forth.s
File metadata and controls
1518 lines (1205 loc) · 41.2 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
| subroutine threaded forth for the megadrive
| a6 points to the second to top value on the parameter stack and grows downward
| d0 contains the top value on the parameter stack
| a7 points to the top value on the return stack (native stack) and grows downward
| DEBUG EQU 1
.equ F_IMMED, 0x80
.equ F_HIDDEN, 0x20
.equ F_LENMASK, 0x1F
.equ F_HIDDENLENMASK, 0x3F
.equ PARAM_STACK_SIZE, 32
.equ SHADOW_STACK_OFFSET, PARAM_STACK_SIZE
.set LAST_WORD, 0
.set LAST_WORD_2, 0
.set USER_VAR_OFFSET, 0
.set USER_VAR_COUNT, 0
.set RSTACK_END, 0x00FFFE00
| pushes a value onto the return stack
| which is buffered to have the top value in d0
| this macro also does bounds checking
| the stack grows down, so we use pre-decrement addressing
.macro PUSH reg
cmpa.l #ps_overflow, %a6
blt stackOverflow
move.l %d0, -(%a6)
move.l \reg, %d0
.endm
| same as above, but in the reverse order
| moves the top value of the stack in d0
| to the given register
| and pops the a6 register up by 4 bytes, placing what it previously pointed to in d0
.macro POP reg
cmpa.l #ps_end, %a6
bgt stackUnderflow
move.l %d0, \reg
move.l (%a6)+, %d0
.endm
| convenience macros for pushing and popping onto the native (return) stack
.macro RPOP reg
move.l (%a7)+, \reg
.endm
.macro RPUSH reg
move.l \reg, -(%a7)
.endm
.macro HALT
halt_loop:
bra halt_loop
.endm
.macro DEFWORD name, nameLen, label, flags, doc
.set DOC_START, .
.ascii "\doc"
.set DOC_LEN, .-DOC_START
.balign 2
dc.w DOC_LEN
dc.l DOC_START
dc.l LAST_WORD
.set LAST_WORD, .-4
.byte \nameLen+\flags
.ascii "\name"
.balign 2
\label:
.endm
.macro DEFSYSVAR name, nameLen, label, flags, val, doc
loc_\label:
dc.l \val
DEFWORD \name, \nameLen, \label, \flags, "\doc"
PUSH #loc_\label
rts
.endm
.macro DEFUSERVAR name, nameLen, label, flags, val, doc
DEFWORD \name, \nameLen, \label, \flags, "\doc"
add.l %a5, %a4 | offset by user task pointer
PUSH %a4 | push result onto stack
rts
| increment offset pointer
.set USER_VAR_OFFSET, (USER_VAR_OFFSET+4)
.set USER_VAR_COUNT, USER_VAR_COUNT+1
.endm
.macro DEFCONST name, nameLen, label, flags, val, doc
DEFWORD \name, \nameLen, \label, \flags, "\doc"
PUSH \val
rts
.endm
.macro SAVE_REGS
movem.l %d0/%a5/%a6/%a7, -(%a7)
.endm
.macro RESTORE_REGS
movem.l (%a7)+, %d0/%a5/%a6/%a7
.endm
.text
.globl main, get_line_of_input, buffer, bufftop, curkey, loc_latest
.extern stackOverflowError, stackPointer, rStackPointer, tosDump, torsDump, printChar, initIO, printNewline, printStrn, printStrNewline
| ENTRY POINT, start of ROM
main:
| well, we don't really need ROM so jump to RAM
jmp ram_entry_point
| start of ram
.data
| place all system vars at the beginning
DEFSYSVAR "CP",2,cp,0,0,"Location of the current dictionary pointer."
DEFSYSVAR "LATEST",6,latest,0,0,"Location of the last compiled word."
DEFSYSVAR "UOFF",4,user_var_offset,0,0,"Offset to user data."
DEFSYSVAR "UCNT",4,user_var_count,0,0,"# of user variables defined."
DEFSYSVAR "STATE",5,state,0,0,"State of the interpreter (1==compile,0==interpreter)."
DEFSYSVAR "BASE",4,base,0,0xA,"Number base for parser/output words."
| 1 == case-sensitive, 0 == case-insensitive
| DEFSYSVAR "CASE",4,case,0,"Case sensitive? (1 == yes,0==no)"
DEFCONST "S0",2,szero, 0, #ps_end, "Location of the top of the stack (grows downward)."
DEFCONST "SS0",3,sszero, 0, #shadow_stack, "Location of the top of the shadow stack (grows downward)."
| hundredths of versions
DEFCONST "VERSION", 7, version, 0, #03,"Current version."
DEFCONST "R0", 2, rzero, 0, #RSTACK_END,"Top of return stack. Uses the native stack so grows downward."
DEFCONST "F_IMMED", 7, fimmed, 0, #F_IMMED,"Flag for marking words as immediate."
DEFCONST "F_HIDDEN", 8, fhidden, 0, #F_HIDDEN,"Flag for marking words as hidden."
DEFCONST "F_LENMASK", 9, flenmask, 0, #F_LENMASK,"Masks off the length of a word from the flags+length field."
welcome_message:
.ascii "Mega Forth"
version_message:
.ascii "Version: "
.balign 2
ram_entry_point:
| initialize I/O stuff (in io.c)
jsr initIO
| set up parameter stack
move.l (ps_end), %a6
bsr init_latest
bsr init_cp
bsr init_uvo
bsr init_up
moveq.l #0, %d0
PUSH #welcome_message
PUSH #10
bsr telln
bsr cr
PUSH #version_message
PUSH #9
bsr telln
bsr version
| divu #10, %d0
| andi.l #0x0000FFFF, %d0
| bsr dot
PUSH #'.'
bsr emit
bsr version
divu #10, %d0
andi.l #0xFFFF0000, %d0
asr.l #8, %d0
asr.l #8, %d0
bsr dot
bra quit
.balign 4
.space 16
ps_overflow:
| param stack grows down, with the pointer pointing at the top value
.space 4*PARAM_STACK_SIZE, 'A'
ps_end:
ps_end_var:
dc.l ps_end
.space 4*PARAM_STACK_SIZE, 0
shadow_stack:
.balign 2
| word buffer
curWordSlot:
dc.l wordBuffer
wordBuffer:
.space 256, ' '
wordBufTop:
| ptr in key buffer
curkey:
dc.l buffer
| ptr to end of input in key buffer
bufftop:
dc.l buffer
.balign 2
| input buffer
buffer:
.space 38, ' ' | 38 character buffer (screen width w/ 1 char margin on each side)
stackOverflow:
move.l %d0, (tosDump) | tos in d0
move.l (%a7), (torsDump) | tors in d1
move.l %a6, (stackPointer)
move.l %a7, (rStackPointer)
jmp stackOverflowError
stackUnderflow:
move.l %d0, (tosDump) | tos in d0
move.l (%a7), (torsDump) | tors in d1
move.l %a6, (stackPointer)
move.l %a7, (rStackPointer)
jmp stackUnderflowError
DEFWORD "DROP",4,drop,0, "( a -- )"
move.l (%a6)+, %d0
rts
DEFWORD "DUP",3,dup,0,"( a -- a a )"
move.l %d0, -(%a6)
rts
DEFWORD "OVER",4,over,0,"( a b -- a b a )"
| ( a b -- a b a )
move.l (%a6), %d1
move.l %d0, -(%a6)
move.l %d1, %d0
rts
DEFWORD "SWAP",4,swap,0,"( a b -- b a )"
| ( a b -- b a )
move.l %d0, %d1
move.l (%a6), %d0
move.l %d1, (%a6)
rts
DEFWORD "ROT",3,rot,0, "( a b c -- b c a )"
| ( a b c -- b c a )
| swap +4 and +0
move.l (%a6)+, %d1
move.l (%a6)+, %d2
| now push them back in the right order
PUSH %d1
PUSH %d0
move.l %d2, %d0
rts
DEFWORD "-ROT",4,nrot,0, "( a b c -- c a b )"
| ( a b c -- c a b )
move.l (%a6)+, %d1
move.l (%a6)+, %d2
move.l %d0, -(%a6)
move.l %d2, -(%a6)
move.l %d1, %d0
rts
DEFWORD "2DUP",4,tdup,0, "( a b -- a b a b )"
| ( a b -- a b a b )
move.l (%a6), %d1
move.l %d0, -(%a6)
move.l %d1, -(%a6)
rts
DEFWORD "2DROP",5,tdrop,0, "( a b c d -- a b )"
| ( a b c d -- a b )
addq.l #4, %a6
POP %d0
rts
DEFWORD "2SWAP",5,tswap,0, "( a b c d -- c d a b )"
| ( 1 2 3 4 -- 3 4 1 2 )
move.l %d0, %d3
move.l (%a6)+, %d2
move.l (%a6)+, %d1
move.l (%a6)+, %d0
movem.l %d1/%d2/%d3, -(%a6)
rts
DEFWORD "?DUP",4,qdup,0, "( a == 0 -- a a ), ( a != 0 -- )"
| ( 0 -- )
| or
| ( nonzero -- nonzero nonzero )
tst.l %d0
bne qdEnd
move.l %d0, -(%a6)
qdEnd:
rts
DEFWORD ">R",2,rto,0, "( a -- ) r: ( -- a )"
| p:( val -- ) r:( -- val)
move.l (%a7), %a0
| pop top of stack into rstack slot
pop (%a7)
jmp (%a0)
DEFWORD "R>",2,rfrom,0, "( -- a ) r: ( a -- )"
| p:( -- val ) r:( val -- )
move.l %d0, -(%a6)
move.l (%a7)+, %a0
move.l (%a7)+, %d0
jmp (%a0)
DEFWORD "1+",2,inc,0, "( a -- a+1 )"
| ( val -- val+1 )
addq.l #1, %d0
rts
DEFWORD "1-",2,dec,0, "( a -- a-1 )"
| ( val -- val-1 )
subq.l #1, %d0
rts
DEFWORD "4+",2,finc,0, "( a -- a+4 )"
| ( val -- val+4 )
addq.l #4, %d0
rts
DEFWORD "4-",2,fdec,0, "( a -- a-4 )"
| ( val -- val-4 )
subq.l #4, %d0
rts
DEFWORD "+",1,add,0, "( a b -- a+b )"
| ( x y -- x+y )
add.l (%a6)+, %d0
rts
DEFWORD "-",1,sub,0, "( a b -- a-b )"
| ( x y -- x-y )
sub.l (%a6)+, %d0
neg.l %d0
rts
| *, /, /mod, and mod
| only really handle bytes i think....
| should add full word multiplication and division
DEFWORD "*",1,mul,0, "( a b -- a*b )"
| ( x y -- x*y )
move.l (%a6)+, %d1
muls.w %d1, %d0
rts
DEFWORD "/MOD",4,divmod,0, "( a b -- a%b a/b )"
| ( x y -- x%y x/y )
divs (%a6),%d0
move.l %d0, %d1
rol.l #8, %d1
andi.l #0x0000FFFF, %d1
move.w %d1, (%a6)
rts
DEFWORD "/",1,div,0, "( a b -- a/b )"
| ( x y -- x/y )
divs (%a6)+,%d0
andi.l #0x0000FFFF, %d0
rts
DEFWORD "MOD",3,mod,0, "( a b -- a%b )"
| ( x y -- x%y )
divs (%a6)+,%d0
rol.l #8, %d0
andi.l #0x0000FFFF, %d0
rts
push_true:
| ( -- 1 )
PUSH #1
rts
push_false:
| ( -- 0 )
PUSH #0
rts
DEFWORD "=",1,eq,0, "( a b -- a==b?1:0 )"
| ( x y == 1 ) when x == y
| ( x y == 0 ) when x != y
POP %d1
POP %d2
cmp.l %d1,%d2
beq push_true
bra push_false
DEFWORD "!=",2,neq,0, "( a b -- a!=b?1:0 )"
POP %d1
POP %d2
cmp.l %d1,%d2
beq push_false
bra push_true
DEFWORD "0=",2,zeq,0, "( a -- a==0?1:0 )"
POP %d1
| status flags are set on the moved data, not the address
| so we can pop a value off the stack,
| and the flags are set for that value alfetchy
| tst.l %d1
beq push_true
bra push_false
DEFWORD "0<",2,zlt,0, "( a -- a>0?1:0 )"
POP %d1
blt push_true
bra push_false
DEFWORD "0<=",3,zle,0, "( a -- a>=0?1:0 )"
POP %d1
ble push_true
bra push_false
DEFWORD "0>",2,zgt,0, "( a -- a<0?1:0 )"
POP %d1
bgt push_true
bra push_false
DEFWORD "0>=",2,zge,0, "( a -- a<=0?1:0 )"
POP %d1
bge push_true
bra push_false
DEFWORD "0!=",3,zneq,0, "( a -- a!=0?1:0 )"
POP %d1
bne push_true
bra push_false
DEFWORD "<",1,lt,0, "( a b -- a<b?1:0 )"
POP %d2
POP %d1
cmp.l %d1, %d2
blt push_true
bra push_false
DEFWORD ">",1,gt,0, "( a b -- a>b?1:0 )"
POP %d2
POP %d1
cmp %d1, %d2
bgt push_true
bra push_false
DEFWORD "<=",2,le,0, "( a b -- a<=b?1:0 )"
POP %d2
POP %d1
cmp %d1, %d2
ble push_true
bra push_false
DEFWORD ">=",2,ge,0, "( a b -- a>=b?1:0 )"
POP %d2
POP %d1
cmp %d1, %d2
bge push_true
bra push_false
DEFWORD "BYE",3,bye,0, "( -- ) Exits interpreter."
PUSH #'E'
PUSH #'Y'
PUSH #'B'
bsr emit
bsr emit
bsr emit
bye_loop:
bra bye_loop
DEFWORD "RESTART",7,restart,0, "( -- ) Restarts system."
PUSH #SYS_reset
bra ccall1
DEFWORD "!",1,store,0, "( a b -- ) Stores a at the address given by b."
move.l %d0, %a0
move.l (%a6)+, (%a0)
move.l (%a6)+, %d0
rts
DEFWORD "!+",2,incMem,0, "( b -- ) Increments the longword value at b."
POP %a0
addq.l #1, (%a0)
move.l (%a6)+, %d0
rts
DEFWORD "!-",2,decMem,0, "( b -- ) Decrements the longword value at b."
move.l %d0, %a0
subq.l #1, (%a0)
move.l (%a6)+, %d0
rts
DEFWORD "@",1,fetch,0, "( a -- b ) Fetches the longword value b at a."
move.l %d0, %a0
move.l (%a0), %d0
rts
DEFWORD "@+",2,fetchInc,0, "( a -- b a+4 ) Fetches longword value b from a, returning the incremented address."
move.l %d0, %a0
move.l (%a0), %d1
move.l %d1, -(%a6)
addq.l #4, %d0
rts
DEFWORD "@-",2,fetchDec,0, "( a -- b a-4 ) Fetches longword value b from a, returning the decremented address."
move.l %d0, %a0
move.l (%a0), %d1
move.l %d1, -(%a6)
subq.l #4, %d0
rts
DEFWORD "W!",2,wstore,0, "( a b -- ) Stores first lower word of a at address b."
move.l %d0, %a0
move.l (%a6)+, %d1
move.w %d1, (%a0)
move.l (%a6)+, %d0
rts
DEFWORD "W@",2,wfetch,0, "( a -- b ) Fetches word b at a."
move.l %d0, %a0
move.w (%a0), %d0
andi.w #0xFFFF, %d0
rts
DEFWORD "C!",2,cstore,0, "( a b -- ) Stores low byte of a at address b."
move.l %d0, %a0
move.l (%a6)+, %d1
move.b %d1, (%a0) | store just a byte
move.l (%a6)+, %d0
rts
DEFWORD "C@",2,cfetch,0, "( a -- b ) Fetches byte b from address a."
move.l %d0, %a0
move.b (%a0), %d0
andi.l #0x000000FF, %d0 | just keep the byte fetch
rts
DEFWORD "C@C!",4,ccopy,0, "( src dest -- src dest ) Copies a byte from src to dest."
move.l %d0, %a1 | get dest ptr
move.l (%a6), %a0 | get src ptr
move.b (%a0), (%a1) | copy byte
addq.l #1, %d0 | increment dest ptr
addq.l #1, (%a6) | increment src ptr
rts
DEFWORD "CMOVE",5,cmove,0, "( src dest num -- ) Copies num bytes from src to dest."
| unrolled loop
| move longwords all at once
pop %d1 | numBytes in %d1
pop %a1 | dest in %a1
pop %a0 | src in %a0
tst.l %d1
ble cMoveEnd
| get number of longwords to copy, along with remainder
lsr.l #2, %d1 | divide by 4 (faster and correct because %d1 can't be negative)
move.l %d1, %d2 | dividend in %d2
andi.l #0x0000FFFF, %d2
rol.l #8, %d1
andi.l #0x0000FFFF,%d1 | remainder in %d1
tst.l %d1
beq cMoveLongLoop
cMoveByte:
move.b (%a0)+, (%a1)+
dblt %d1, cMoveByte | count down the remainder, max 3 bytes copied individually
cMoveLong: | copy four words at a time
tst.l %d2
beq cMoveEnd
cMoveLongLoop:
move.l (%a0)+, (%a1)+
dblt %d2, cMoveLongLoop
cMoveEnd:
rts
DEFWORD ".",1,dot,0, "( a -- ) Prints the top object on the stack. (decimal only)"
moveq #0, %d1
| %d1 is counter ( rest integer )
POP %d2
| ( rest of stack )
dotLoop:
move.b %d2, %d3
andi.l #0xFFFF, %d3 | get word?
divs #10, %d3
lsr.l #8, %d3
lsr.l #8, %d3
add.l #0x30, %d3
PUSH %d3 | save char on stack ( rest of stack firstChar )
addq.l #1, %d1
divu #10, %d2
andi.l #0xFFFF, %d2
bgt dotLoop
subq.l #1, %d1
dotPrintLoop:
| ( rest of stack chars )
| we need to save %d1
PUSH %d1
bsr swap
bsr emit
POP %d1
dbeq %d1, dotPrintLoop
PUSH #0x20
bra emit
rts
DEFWORD "DEPTH",5,depth,0, "( -- depth ) Returns current depth of stack."
| ( -- depth )
move.l %a6, %d1
move.l #ps_end, %d2
sub.l %d1, %d2
lsl #2, %d2
PUSH %d2
rts
DEFWORD "RDEPTH",6,rdepth,0, "( -- depth ) Returns current depth of return stack"
move.l %a7, %d1
move.l RSTACK_END, %d2
sub.l %d1, %d2
lsl #2, %d2
PUSH %d2
rts
DEFWORD "GOTO",4,goto,0, "( a -- ) Transfer control to address a."
move.l %d0, %a0
move.l (%a6)+, %d0
jmp (%a0)
DEFWORD "AND",3,and,0, "( a b -- a & b)"
and.l (%a6)+, %d0
rts
DEFWORD "OR",2,or,0, "( a b -- a | b)"
or.l (%a6)+, %d0
rts
DEFWORD "XOR",3,xor,0, "( a b -- a xor b)"
move.l (%a6)+, %d1
eor.l %d1, %d0
rts
DEFWORD "NEGATE", 6, negate,0, "( a -- -a )"
negx.l %d0
rts
DEFWORD "NOT", 3, not, 0, "( a -- ~a )"
not.l %d0
rts
| DEFWORD "LIT", 3, lit, 0
| move.l (%a7), %a0
| push (%a0)
| addq.l #4, (%a7)
| rts
| won't work inlined!
DEFWORD "RSP@",4,rspfetch,0, "( -- a ) Fetches return stack pointer a."
| r ( something return-addr )
PUSH %sp
addq #4, %d0
rts
| won't work inlined!
DEFWORD "RSP!",4,rspstore,0, "( a -- ) Sets return stack pointer to a."
move.l (%a7), %a0
POP %a7
jmp (%a0)
| won't work inlined!
DEFWORD "RDROP",5,rdrop,0, "r: ( a -- ) Drops the top object off the return stack."
move.l (%a7)+, (%a7)
rts
DEFWORD "DSP@",4,dspfetch,0, "( -- a ) Fetches stack pointer a."
move.l %a6, %d1
PUSH %d1
rts
DEFWORD "DSP!",4,dspstore,0, "( a -- ) Sets stack pointer to a."
move.l %d0, %a6
move.l (%a6)+, %d0
rts
DEFWORD "KEY",3,key,0, "( -- key ) Reads a character from input."
_key:
| is buffer empty?
move.l curkey, %a1
move.l bufftop, %a2
cmp.l %a2, %a1
| if curkey == end of buffer, get new line of input
bge get_new_input
| otherwise grab the next character from the buffer
move.b (%a1), %d1
andi.l #0x000000FF, %d1
PUSH %d1
addq.l #1, (curkey)
rts
get_new_input:
PUSH #get_line_of_input
bsr ccall0
| go back to the top and try to grab character
bra _key
DEFWORD "CCALL0",6,ccall0,0, "( a -- ) Calls a c-function at addr a."
POP %a0 | address
SAVE_REGS
jsr (%a0)
RESTORE_REGS
rts
DEFWORD "CCALL1",6,ccall1,0, "( a b -- ) Calls a c-function at addr b with operand a."
| ( p1 addr -- )
POP %a0 | get address
POP %d1 | get param
SAVE_REGS
RPUSH %d1
jsr (%a0)
addq.l #4, %a7
RESTORE_REGS
rts
DEFWORD "CCALL2",6,ccall2,0, "( a b c -- ) Calls a c-function at addr c with operands a and b."
| ( p1 p2 addr -- )
POP %a0
POP %d2
POP %d1
SAVE_REGS
RPUSH %d2
RPUSH %d1
jsr (%a0)
addq.l #8, %a7
RESTORE_REGS
rts
DEFWORD "CCALL3",6,ccall3,0, "( a b c d ) Calls a c-function at addr d with operands a, b, and c."
| ( p1 p2 p3 addr -- )
POP %a0
POP %d3
POP %d2
POP %d1
SAVE_REGS
RPUSH %d3
RPUSH %d2
RPUSH %d1
jsr (%a0)
add.l #12, %a7
RESTORE_REGS
rts
DEFWORD "EMIT",4,emit,0, "( a -- ) Prints a as a character."
PUSH #printChar
bra ccall1
DEFWORD "CR",2,cr,0, "( -- ) Prints a newline."
PUSH #'\n'
bra emit
DEFWORD "WORD",4,word,0, "( -- wordAddr wordLen ) Reads a word from input."
bra get_first_char
after_get_word:
| start address in a0
| length in d1
| move.l #wordBuffer, %d0 | get rid of space char
move.l %a0, (curWordSlot)
move.l %a2, %d0
PUSH %d1
rts
get_first_char:
| get key on top of stack
bsr key
cmpi.b #' ', %d0
bne start_store_chars
POP %d2
bra get_first_char
start_store_chars:
| #curWordSlot
| #wordBuffer
move.l (curWordSlot), %a0
move.l %a0, %a2 | start of word
move.l #wordBufTop, %a1
cmp.l %a0, %a1
beq flush_word_buffer
moveq.l #0, %d1
store_chars:
POP %d2
move.b %d2, (%a0)+
addq.b #1, %d1
RPUSH %a0
RPUSH %a2
RPUSH %d1
bsr key | get next char
RPOP %d1
RPOP %a2
RPOP %a0
cmpi.b #' ', %d0
| if not space keep grabbing chars
bne store_chars
| if found space return address of character and number of characters
bra after_get_word
flush_word_buffer:
move.l #wordBuffer, (curWordSlot)
bra start_store_chars
DEFWORD "TELL",4,tell,0, "( a b -- ) Prints string at a with length b."
| just print a whole string
| ( strAddr strLen -- )
push #printStrn
bra ccall2
DEFWORD "TELLN",5,telln,0, "( a b -- ) Prints string at a with length b. Starts with a freshline is string is too long."
push #printStrNewline
bra ccall2
DEFWORD "NUMBER",6,number,0, "( strAddr strLen -- number #unparsedChars ) Parses a string, returning a number and the number of unparsed characters (only nonzero in case of error)."
bra _number
after_get_number:
PUSH %d3 | parsed number
PUSH %d1 | number of unparsed characters
rts
_number:
| move.b (base), %d4 | get base
bsr base
bsr fetch
POP %d4 | place base in d4
POP %d1 | place length in d1
POP %a0 | place addr in a0
moveq.l #0, %d3 | cmover register for result
moveq.l #0, %d5 | cmover register for temporary characters
| check if first char is '-'
move.b (%a0)+, %d5 | first char in d5
PUSH #0 | push 0 on stack
cmp.b #'-', %d5
bne number_conv_char
move.l #-1, %d0 | push something !=0 to indicate negative number
subq #1, %d1 | if zero, the only character is '-'
bne fetch_digit_loop
moveq.l #0, %d3 | set number to 0
POP %d5 | pop -1 off stack
move.l #1, %d1 | and set unparsed characters to 1
bra after_get_number
fetch_digit_loop:
muls.w %d4, %d3 | multiply by base
move.b (%a0)+, %d5 | fetch character into d5
number_conv_char:
sub.b #'0', %d5 | subtract '0' char to get number
blt number_negate | less than 0?, jump to negate
cmp #10, %d5 | <= 9
blt number_cmp_base
sub.b #17, %d5 | < 'A' (16 is 'A'-'0')
blt number_negate
add.b #10, %d5
number_cmp_base:
cmp %d4, %d5 | >= base?
bge number_negate
add.l %d5, %d3
subq.l #1, %d1
bne fetch_digit_loop
number_negate:
POP %d6
tst.l %d6
beq number_end
neg.l %d3
number_end:
bra after_get_number
DEFWORD "IS-IMMEDIATE",12,qimmediate,0, "( wordAddr -- isImmediate ) Returns 1 if given word is an immediate word, 0 otherwise."
| ( dictEntryAddr -- t/f )
addq #4, %d0
move.l %d0, %a0
move.b (%a0), %d0
andi.l #F_IMMED, %d0
rts
DEFWORD "MARK",4,mark,0, "( -- ) Places a mark on the shadow stack."
move.l #1, (SHADOW_STACK_OFFSET, %a6) | mark this location in the shadow stack
rts
DEFWORD "UNMARK",6,unmark,0, "( -- ) Removes a mark from the shadow stack."
move.l #0, (SHADOW_STACK_OFFSET, %a6)
rts
DEFWORD "MARKED?",7,markedp,0,"( -- ) Is the current location on the shadow stack marked?"
move.l (SHADOW_STACK_OFFSET, %a6), %d1 | move shadow stack value to stack (shadow stack should only contain 0s and 1s
move.l %d0, -(%a6)
move.l %d1, %d0
rts
DEFWORD "CLEAR-TO-MARK",13,clearToMark,0, "( -- ) Clear stack until we reach a marked slot."
move.l %a6, %a0
add.l #SHADOW_STACK_OFFSET, %a0
ctmLoop:
move.l (%a0)+, %d1
bne ctmDone
POP %d2
bra ctmLoop
ctmDone:
rts
DEFWORD "COUNT-TO-MARK",13,countToMark,0, "( -- #objects ) Count number of items on stack above the last mark."
move.l #0, %d2
move.l %a6, %a0
add.l #SHADOW_STACK_OFFSET, %a0
cntLoop:
move.l (%a0)+, %d1
bne cntDone
addq.l #1, %d2
bra cntLoop
cntDone:
PUSH %d2
rts
DEFWORD "FIND",4,find,0, "( strAddr strLen -- wordAddr ) Finds address of word corresponding to string. Returns 0 if no word exists."
POP %d1 | length of string in d1
POP %a0 | address of string in a0
bra _find_start
after_find:
PUSH %a1 | dictionary address will be in a1
rts