-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforth.asm
2472 lines (2354 loc) · 46.6 KB
/
forth.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
.nolist
#include "ti83plus.inc"
#define ProgStart $9D95
#define B_CALL(xxxx) rst 28h \ .dw xxxx
#include forth.inc
.list
.org ProgStart - 2
.db t2ByteTok, tAsmCmp
;Entry point
;backup stack pointer
;initialize stacks
ld (OLD_SP), sp
ld sp, PS_LOCATION
ld ix, RS_LOCATION
ld bc, 0 ;initialize TOS
ld hl, 0 ;initialize W (working register)
B_CALL(_ClrLCDFull)
B_CALL(_ClrScrnFull)
B_CALL(_runindicoff)
;Display message
ld hl, WELCOME_MSG
B_CALL(_PutS)
B_CALL(_NewLine)
;ld hl, VERSION_MSG
;B_CALL(_PutS)
;B_CALL(_NewLine)
;B_CALL(_GetKey)
;B_CALL(_ClrLCDFull)
;user memory can be on Floating Point Stack.
;FPS as here crashes real calc, runs on emulator. IDK why protected memory
;ld hl, DICT_ALLOC
;B_CALL(_AllocFPS)
;ld hl, DICT_ALLOC
;B_CALL(_DeallocFPS)
;ld hl,(FPS)
;ld (HERE_VAL), hl
LOAD_START_TEXT:
;this code allows some startup text. Can be empty if not needed.
ld de, INBUF_LOCATION
ld hl, START_TEXT
ld bc, START_TEXT_END-START_TEXT
ldir
ld (BUFTOP), de
SETUP_BLOCK:
;user memory can be on BLOCK file. This file can be uploaded, but if it
;it not found it should be created to a default size. Also crashes real calc
;because ti cannot execute in file mem location
;ld hl, DICT_NAME
;rst 20h ;mov9toOP1
;push ix
;B_CALL(_ChkFindSym)
;pop ix
;jp nc, CONT_INIT
;ld hl, DICT_NAME
;rst 20h ;mov9toOP1
;ld hl, DICT_SIZE
;B_CALL(_CreateProg)
EDIT_MODE:
;ld de, END_PROG
;B_CALL(_EditProg)
CONT_INIT:
;inc de
;inc de
;inc de
;inc de
;ex de, hl
;ld (HERE_VAL), hl
call _CR
ld hl, MEM_MSG
B_CALL(_PutS)
B_CALL(_MemChk)
B_CALL(_DispHL)
B_CALL(_NewLine)
B_CALL(_CursorOn)
;enter QUIT to start up Forth system
ld de, COLD_START
NEXT
;shouldn't ever reach here
ret
;This can be used to load BLOCK file location to here, include high level
;word file, or any other startup stuff.
;remeber that the inbuf has up to 768 bytes before it overwrites stuff.
START_TEXT:
.db " : DEFER CREATE 0 , DOES> @ EXECUTE ; "
.db " : IS ` 3 + ! ; "
.db " : STACK CREATE 0 HERE @ ! 1+ CELLS ALLOT DOES> ; "
.db " : PUSH DUP @ 1+ 2DUP SWAP ! CELLS + ! ; "
.db " : POP DUP @ 0DUP UNLESS DROP EXIT THEN "
.db " 2DUP CELLS + @ -ROT 1- SWAP ! ; "
.db " : MAX 2DUP > IF DROP ELSE NIP THEN ;"
.db " : MIN 2DUP < IF DROP ELSE NIP THEN ;"
.db " : CASE: CREATE ] DOES> OVER + + @ EXECUTE ;"
.db " : [CHAR] POSTPONE LIT CHAR , ; IMMEDIATE "
.db " : (: 0 POSTPONE [ ; "
.db " : VALUE VARIABLE LATEST @ DFA >R 1+ ; "
.db " : :) 0 DO R> ! LOOP POSTPONE ] ; "
;todo make this stuff work.
.db " : DEPTH S0 DSP@ - 2 / ;"
;.db " : .S CR DSP@ DEPTH 0 DO I CELLS + @ . LOOP ;"
.db kEnter
START_TEXT_END:
.db " INCLUDE HLW ", kEnter;if rest of language is in HLW file
;Fragments
;Common DO definitions for words to jump to.
_DOCOL:
dec ix
ld (ix+0), d
dec ix
ld (ix+0), e
inc hl
inc hl
inc hl
SHORTNEXT
_DOCON:
push bc
inc hl
inc hl
inc hl
ld c, (hl)
inc hl
ld b, (hl)
NEXT
_DOVAR:
inc hl
inc hl
inc hl
push bc
ld b, h
ld c, l
NEXT
;"jp" to DOES> action but do a "call" to _DODOES to put addr on stack.
_DODOES:
dec ix
ld (ix+0), d
dec ix
ld (ix+0), e
ld d, b
ld e, c
inc hl
inc hl
inc hl
ld b, h
ld c, l
pop hl
push de
SHORTNEXT
Dictionary:
;Begin Definitions
;First link is 0
;last link is LATEST_DEF
;NAME_DEF:
; MAKEWORD(name, size, type, lastlink)
;NAME:
; Assembly definition
; NEXT
;or
;NAME_DEF:
; MAKEWORD(name, size, type, lastlink)
;NAME:
; jp _DOCOL
; .dw any number of
; .dw links to words
; .dw EXIT
;DOCOL compiles addr of _DOCOL. First word in the dictionary.
DOCOL_DEF:
MAKEWORD("DOCOL", 5, NORMAL, 0)
DOCOL:
push bc
ld hl, (HERE_VAL)
ld bc, _DOCOL
ld (hl), c
inc hl
ld (hl), b
inc hl
ld (HERE_VAL), hl
pop bc
NEXT
;Common EXIT code fragment to jump to pops off last return value into IP.
EXIT_DEF:
MAKEWORD("EXIT", 4, NORMAL, DOCOL_DEF)
EXIT:
ld e, (ix+0)
inc ix
ld d, (ix+0)
inc ix
NEXT
DOCON_DEF:
MAKEWORD("DOCON", 5, NORMAL, EXIT_DEF)
DOCON:
push bc
ld hl, (HERE_VAL)
ld bc, _DOCON
ld (hl), c
inc hl
ld (hl), b
inc hl
ld (HERE_VAL), hl
pop bc
NEXT
DOVAR_DEF:
MAKEWORD("DOVAR", 5, NORMAL, DOCON_DEF)
DOVAR:
push bc
ld hl, (HERE_VAL)
ld bc, _DOVAR
ld (hl), c
inc hl
ld (hl), b
inc hl
ld (HERE_VAL), hl
pop bc
NEXT
;DUP simply push TOS, so that TOS is now in the stack and in bc register.
DUP_DEF:
MAKEWORD("DUP", 3, NORMAL, DOVAR_DEF)
DUP:
push bc
NEXT
;0DUP simply push TOS, so that TOS is now in the stack and in bc register.
ZDUP_DEF:
MAKEWORD("0DUP", 4, NORMAL, DUP_DEF)
ZDUP:
ld a, c
cp 0
jp nz, ZDUP_DUP
ld a, b
cp 0
jp nz, ZDUP_DUP
jp ZDUP_END
ZDUP_DUP:
push bc
ZDUP_END:
NEXT
;DROP overwrites bc with actual TOS.
DROP_DEF:
MAKEWORD("DROP", 4, NORMAL, ZDUP_DEF)
DROP:
pop bc
NEXT
;SWAP exchanges (sp) register and sp.
SWAP_DEF:
MAKEWORD("SWAP", 4, NORMAL, DROP_DEF)
SWAP:
ld h, b
ld l, c
ex (sp), hl
ld b, h
ld c, l
NEXT
;OVER gets (sp) and pushes TOS register bc, then loads what was in (SP) into
;be, making it TOS
OVER_DEF:
MAKEWORD("OVER", 4, NORMAL, SWAP_DEF)
OVER:
pop hl
push hl
push bc
ld c,l
ld b,h
NEXT
;NIP just moves the stack pointer past the current value.
NIP_DEF:
MAKEWORD("NIP", 3, NORMAL, OVER_DEF)
NIP:
inc sp
inc sp
NEXT
;TUCK ( n m -- m n m )
TUCK_DEF:
MAKEWORD("TUCK", 4, NORMAL, NIP_DEF)
TUCK:
pop hl
push bc
push hl
NEXT
PICK_DEF:
MAKEWORD("PICK", 4, NORMAL, TUCK_DEF)
PICK:
jp _DOCOL
.dw INCR, CELLS
.dw DSPFETCH, ADD
.dw FETCH
.dw EXIT
;ROT ( a b c - b c a ) pops top two ideas of actual stack and pushes back two items, then
;loads into TOS register bc
ROT_DEF:
MAKEWORD("ROT", 3, NORMAL, PICK_DEF)
ROT:
pop af
pop hl
push af
push bc
ld b, h
ld c, l
NEXT
;ROT2 ( a b c -- c a b ) is the opposite of rot.
ROT2_DEF:
MAKEWORD("-ROT", 4, NORMAL, ROT_DEF)
ROT2:
pop hl
pop af
push bc
push af
ld b, h
ld c, l
;push hl
;pop bc
NEXT
;2DROP ( n m -- ) moves past the second value and overwrites TOS register.
TWODROP_DEF:
MAKEWORD("2DROP", 5, NORMAL, ROT2_DEF)
TWODROP:
inc sp
inc sp
pop bc
NEXT
;2DUP duplicates first two items on stack
;( n m -- n m n m )
TWODUP_DEF:
MAKEWORD("2DUP", 4, NORMAL, TWODROP_DEF)
TWODUP:
pop hl
push hl
push bc
push hl
NEXT
;2SWAP ( a b c d -- c d a b )
TWOSWAP_DEF:
MAKEWORD("2SWAP", 5, NORMAL, TWODUP_DEF)
TWOSWAP:
pop hl
pop af
ex (sp),hl
push bc
push hl
push af
pop bc
NEXT
;?DUP compare with bc and if not 0, push to stack
;leaving current value in bc.
QDUP_DEF:
MAKEWORD("?DUP", 4, NORMAL, TWOSWAP_DEF)
QDUP:
ld a, b
or c
jp z, QDUP_DONE
push bc
QDUP_DONE:
NEXT
;+1 increments TOS by 1
INCR_DEF:
MAKEWORD("1+", 2, NORMAL, QDUP_DEF)
INCR:
inc bc
NEXT
;-1 decrements TOS by 1
DECR_DEF:
MAKEWORD("1-", 2, NORMAL, INCR_DEF)
DECR:
dec bc
NEXT
;= checks equality with actual TOS and bc loads 1 if same, 0 if not use kEE
;for now for "=" sign.
EQUA_DEF:
MAKEWORD('=', 1, NORMAL, DECR_DEF)
EQUA:
pop hl
dec bc;for some reason this makes sbc work
sbc hl, bc
dec hl
jp z, EQUAL
ld bc, 0
NEXT
EQUAL:
ld bc, 1
NEXT
;0= ( n -- f ) f flag n == 0.
ZEQUA_DEF:
MAKEWORD("0=", 2, NORMAL, EQUA_DEF)
ZEQUA:
ld a, b
or c
jp nz, ZEQUA_NOT
ld bc, 1
NEXT
ZEQUA_NOT:
ld bc, 0
NEXT
TRUE_DEF:
MAKEWORD("TRUE", 4, NORMAL, ZEQUA_DEF)
TRUE:
push bc
ld bc, 1
NEXT
FALSE_DEF:
MAKEWORD("FALSE", 5, NORMAL, TRUE_DEF)
FALSE:
push bc
ld bc, 0
NEXT
;COMPILE compiles the next two bytes as an xt. Useful for defining words in asm
;and maybe some defining words.
COMPILE_DEF:
MAKEWORD("COMPILE", 7, NORMAL, FALSE_DEF)
COMPILE:
push bc
ex de, hl
ld e, (hl)
inc hl
ld d, (hl)
inc hl
ex de, hl
ld b, h
ld c, l
ld hl, (HERE_VAL)
ld (hl), c
inc hl
ld (hl), b
inc hl
ld (HERE_VAL), hl
pop bc
NEXT
;POSTPONE compiles the next word in the input stream, without regard to
;immediate words.
POSTPONE_DEF:
MAKEWORD("POSTPONE", 8, IMD_VAL, COMPILE_DEF)
POSTPONE:
jp _DOCOL
;.dw COMPILE
.dw TICK
.dw COMMA
.dw EXIT
MOD_DEF:
MAKEWORD("MOD", 3, NORMAL, POSTPONE_DEF)
MOD:
jp _DOCOL
.dw DIVMOD,SWAP, DROP
.dw EXIT
DIV_DEF:
MAKEWORD('/', 1, NORMAL, MOD_DEF)
DIV:
jp _DOCOL
.dw DIVMOD, DROP
.dw EXIT
BL_DEF:
MAKEWORD("BL", 2, NORMAL, DIV_DEF)
BL:
jp _DOCOL
.dw LIT, 32
.dw EXIT
SPACE_DEF:
MAKEWORD("SPACE", 5, NORMAL, BL_DEF)
SPACE:
jp _DOCOL
.dw BL, EMIT
.dw EXIT
CELLS_DEF:
MAKEWORD("CELLS", 5, NORMAL, SPACE_DEF)
CELLS:
jp _DOCOL
.dw TWOTIMES
.dw EXIT
CELLPLUS_DEF:
MAKEWORD("CELL+", 5, NORMAL, CELLS_DEF)
CELLPLUS:
inc bc
inc bc
NEXT
NEGATE_DEF:
MAKEWORD("NEGATE", 6, NORMAL, CELLPLUS_DEF)
NEGATE:
jp _DOCOL
.dw LIT, 0, SWAP, SUB
.dw EXIT
LITERAL_DEF:
MAKEWORD("LITERAL", 7, IMD_VAL, NEGATE_DEF)
LITERAL:
jp _DOCOL
;.dw TICK, LIT, COMMA, COMMA
.dw COMPILE, LIT, COMMA
.dw EXIT
IF_DEF:
MAKEWORD("IF", 2, IMD_VAL, LITERAL_DEF)
IF:
jp _DOCOL
.dw COMPILE, ZBRANCH
.dw HERE, FETCH
.dw ZERO, COMMA
.dw EXIT
THEN_DEF:
MAKEWORD("THEN", 4, IMD_VAL, IF_DEF)
THEN:
jp _DOCOL
.dw DUP, HERE, FETCH, SWAP, SUB
.dw SWAP, STORE
.dw EXIT
ELSE_DEF:
MAKEWORD("ELSE", 4, IMD_VAL, THEN_DEF)
ELSE:
jp _DOCOL
.dw COMPILE, BRANCH
.dw HERE, FETCH, ZERO, COMMA
.dw SWAP, DUP, HERE, FETCH, SWAP, SUB
.dw SWAP, STORE
.dw EXIT
BEGIN_DEF:
MAKEWORD("BEGIN", 5, IMD_VAL, ELSE_DEF)
BEGIN:
jp _DOCOL
.dw HERE, FETCH
.dw EXIT
UNTIL_DEF:
MAKEWORD("UNTIL", 5, IMD_VAL, BEGIN_DEF)
UNTIL:
jp _DOCOL
.dw COMPILE, ZBRANCH
.dw HERE, FETCH, SUB, COMMA
.dw EXIT
AGAIN_DEF:
MAKEWORD("AGAIN", 5, IMD_VAL, UNTIL_DEF)
AGAIN:
jp _DOCOL
.dw COMPILE, BRANCH
.dw HERE, FETCH, SUB, COMMA
.dw EXIT
WHILE_DEF:
MAKEWORD("WHILE", 5, IMD_VAL, AGAIN_DEF)
WHILE:
jp _DOCOL
.dw COMPILE, ZBRANCH
.dw HERE, FETCH, ZERO, COMMA
.dw EXIT
REPEAT_DEF:
MAKEWORD("REPEAT", 6, IMD_VAL, WHILE_DEF)
REPEAT:
jp _DOCOL
.dw COMPILE, BRANCH
.dw SWAP, HERE, FETCH, SUB, COMMA
.dw DUP, HERE, FETCH, SWAP, SUB, COMMA
.dw SWAP, STORE
.dw EXIT
UNLESS_DEF:
MAKEWORD("UNLESS", 6, IMD_VAL, REPEAT_DEF)
UNLESS:
jp _DOCOL
.dw COMPILE, NOT
.dw IF
.dw EXIT
;
;: DO COMPILE SWAP COMPILE 2>R
; HERE @ COMPILE 2R@
; COMPILE = COMPILE IF HERE @ 0 COMMA ;
DO_DEF:
MAKEWORD("DO", 2, IMD_VAL, UNLESS_DEF)
DO:
jp _DOCOL
.dw COMPILE, SWAP, COMPILE, TWOTORS
.dw HERE, FETCH, COMPILE, TWORSCOPY
.dw COMPILE, LST, IF
;.dw HERE, FETCH, LIT, 0, COMMA
.dw EXIT
;: LOOP COMPILE R> COMPILE 1+ COMPILE >R
; COMPILE BRANCH SWAP HERE @ - COMMA
; DUP HERE @ SWAP - SWAP !
; COMPILE 2RDROP ;
LOOP_DEF:
MAKEWORD("LOOP", 4, IMD_VAL, DO_DEF)
LOOP:
jp _DOCOL
.dw COMPILE, FROMRS, COMPILE, INCR, COMPILE, TORS
.dw COMPILE, BRANCH, SWAP, HERE, FETCH, SUB, COMMA
.dw THEN
;.dw DUP, HERE, FETCH, SWAP, SUB, SWAP, STORE
.dw COMPILE, TWORDROP
.dw EXIT
I_DEF:
MAKEWORD('I', 1, NORMAL, LOOP_DEF)
I:
jp _DOCOL
.dw TWORSCOPY, NIP
.dw EXIT
;doesn't work, must know about return stack
J_DEF:
MAKEWORD('J', 1, NORMAL, I_DEF)
J:
jp _DOCOL
;.dw TWOFROMRS, FROMRS, RCOPY, ROT2, TWOTORS
.dw EXIT
UNLOOP_DEF:
MAKEWORD("UNLOOP", 6, NORMAL, J_DEF)
UNLOOP:
jp _DOCOL
.dw 2RDROP
.dw EXIT
;FORGET does not work yet.
FORGET_DEF:
MAKEWORD("FORGET", 6, NORMAL, UNLOOP_DEF)
FORGET:
jp _DOCOL
.dw WORD, FIND
.dw DUP, FETCH, LATEST, STORE
.dw HERE, STORE
.dw EXIT
CONSTANT_DEF:
MAKEWORD("CONSTANT", 8, NORMAL, FORGET_DEF)
CONSTANT:
jp _DOCOL
.dw CREATE, LIT, 2, HERE, SUBSTORE
.dw DOCON
.dw COMMA, SEMICOLON
.dw EXIT
.dw LATEST, FETCH, CFA, LIT, 7, ADD
.dw LIT, FETCH, SWAP, STORE
.dw EXIT
;breaks dictionary for some reason
;COMMABYTE_DEF:
; MAKEWORD("C,", 2, NORMAL, CONSTANT_DEF)
;COMMABYTE:
; jp _DOCOL
; .dw HERE, FETCH, STOREBYTE
; .dw LIT, 1, ALLOT
; .dw EXIT
;
;RECURSE compiles the word currently being defined.
RECURSE_DEF:
MAKEWORD("RECURSE", 7, IMD_VAL, CONSTANT_DEF)
RECURSE:
jp _DOCOL
.dw NEXTLATEST, FETCH, CFA, COMMA
.dw EXIT
ALLOT_DEF:
MAKEWORD("ALLOT", 5, NORMAL, RECURSE_DEF)
ALLOT:
ld hl, (HERE_VAL)
add hl, bc
pop bc
ld (HERE_VAL), hl
NEXT
VARIABLE_DEF:
MAKEWORD("VARIABLE", 8, NORMAL, ALLOT_DEF)
VARIABLE:
jp _DOCOL
.dw CREATE, LIT, 2, HERE, SUBSTORE
.dw DOVAR
.dw LIT, 0, COMMA, SEMICOLON
.dw EXIT
LDSCRN_DEF
MAKEWORD("LDSCRN", 6, NORMAL, VARIABLE_DEF)
LDSCRN:
push de
B_CALL(_GrBufCpy)
pop de
NEXT
;CLRSCRN clears the screen by 0ing out the memory map of the screen.
CLRSCRN_DEF:
MAKEWORD("CLRSCRN", 7, NORMAL, LDSCRN_DEF)
CLRSCRN:
push de
push bc
B_CALL(_GrBufClr)
pop bc
pop de
NEXT
;DISPMEM displays an area of memory as 68 rows of 12 bytes. ( addr -- ).
DISPMEM_DEF:
MAKEWORD("DISPMEM", 7, NORMAL, CLRSCRN_DEF)
DISPMEM:
ld h, b
ld l, c
push de
B_CALL(_BufCpy)
pop de
pop bc
NEXT
;SCRN is a constant that puts plotSScreen in stack.
SCRN_DEF:
MAKEWORD("SCRN", 4, NORMAL, DISPMEM_DEF)
SCRN:
push bc
ld bc, plotSScreen
NEXT
;COMMENT ( begins comment and searchs for a ) on the same line.
COMMENT_DEF:
MAKEWORD('(', 1, NORMAL, SCRN_DEF)
COMMENT:
jp _DOCOL
.dw CHAR, LIT, ')', EQUA, ZBRANCH, -10
.dw EXIT
;NEXTLATEST puts the nextlatest variable on the stack. This could be used
;in the middle of a definition to get the address of the beginning of the
;current definition.
NEXTLATEST_DEF:
MAKEWORD("NEXTLATEST", 10, NORMAL, COMMENT_DEF)
NEXTLATEST:
push bc
ld bc, NEXT_LATEST
NEXT
;LIT takes next word and puts it on stack,
;then causes IP to skip over it
LIT_DEF:
MAKEWORD("LIT", 3, NORMAL, NEXTLATEST_DEF)
LIT:
push bc
ex de, hl
ld c, (hl)
inc hl
ld b, (hl)
inc hl
SHORTNEXT
;! stores actual top of stack to address in TOS
;register bc, and pops next value off to TOS
STORE_DEF:
MAKEWORD('!', 1, NORMAL, LIT_DEF)
STORE:
ld h, b
ld l, c
pop bc
ld (hl), c
inc hl
ld (hl), b
pop bc
NEXT
;@ fetches data from where TOS points and puts it in TOS.
FETCH_DEF:
MAKEWORD('@', 1, NORMAL, STORE_DEF)
FETCH:
ld h, b
ld l, c
ld c, (hl)
inc hl
ld b, (hl)
NEXT
ADDSTORE_DEF:
MAKEWORD("+!", 2, NORMAL, FETCH_DEF)
ADDSTORE:
jp _DOCOL
.dw DUP, FETCH, ROT, ADD, SWAP, STORE
.dw EXIT
SUBSTORE_DEF:
MAKEWORD("-!", 2, NORMAL, ADDSTORE_DEF)
SUBSTORE:
jp _DOCOL
.dw DUP, FETCH, ROT, SUB, SWAP, STORE
.dw EXIT
;C! stores only LS byte into stack.
STOREBYTE_DEF:
MAKEWORD("C!", 2, NORMAL, SUBSTORE_DEF)
STOREBYTE:
pop hl
ld a, l
ld (bc), a
pop bc
NEXT
FETCHBYTE_DEF:
MAKEWORD("C@", 2, NORMAL, STOREBYTE_DEF)
FETCHBYTE:
ld h, b
ld l, c
ld c, (hl)
ld b, 0
NEXT
HERE_DEF:
MAKEWORD("HERE", 4, NORMAL, FETCHBYTE_DEF)
HERE:
push bc
ld bc, HERE_VAL
NEXT
STATE_DEF:
MAKEWORD("STATE", 5, NORMAL, HERE_DEF)
STATE:
push bc
ld bc, STATE_VAL
NEXT
PAD_DEF:
MAKEWORD("PAD", 3, NORMAL, STATE_DEF)
PAD:
push bc
ld bc, PAD_VAL
NEXT
;S0 is a constant holding the bottom of the stack.
SZERO_DEF:
MAKEWORD("S0", 2, NORMAL, PAD_DEF)
SZERO:
push bc
ld bc, PS_LOCATION
dec bc
dec bc
dec bc
dec bc
NEXT
BASE_DEF:
MAKEWORD("BASE", 4, NORMAL, SZERO_DEF)
BASE:
push bc
ld bc, BASE_VAL
NEXT
INBUF_DEF:
MAKEWORD("INBUF", 5, NORMAL, BASE_DEF)
INBUF:
push bc
ld bc, INBUF_VAL
NEXT
;>IN is a variable holding the location of the next unprocessed cell in the
;input buffer.
INNEXT_DEF:
MAKEWORD(">IN", 3, NORMAL, INBUF_DEF)
INNEXT:
push bc
ld bc, KEY_NEXT
NEXT
DSPFETCH_DEF:
MAKEWORD("DSP@", 4, NORMAL, INNEXT_DEF)
DSPFETCH:
push bc
ld hl, 0
add hl, sp
ld b, h
ld c, l
;push bc
;ld (STACK_POS), sp
;ld hl, (STACK_POS)
;inc hl
;inc hl
;ld b, h
;ld c, l
NEXT
STACK_POS:
.dw 0
DSPSTORE_DEF:
MAKEWORD("DSP!", 4, NORMAL, DSPFETCH_DEF)
DSPSTORE:
ld h, b
ld l, c
ld sp, hl
pop bc
NEXT
RSPFETCH_DEF:
MAKEWORD("RSP@", 4, NORMAL, DSPSTORE_DEF)
push bc
;ld bc, ix TODO how does one load IX?
NEXT
RSPFETCH:
RSPSTORE_DEF:
MAKEWORD("RSP!", 4, NORMAL, RSPFETCH_DEF)
RSPSTORE:
;TODO implement by changing IX
NEXT
;TORS >R pop from parameter stack, put in return stack.
TORS_DEF:
MAKEWORD(">R", 2, NORMAL, RSPSTORE_DEF)
TORS:
dec ix
ld (ix+0), b
dec ix
ld (ix+0), c
pop bc
NEXT
;FROMRS R> pop from return stack onto parameter stack.
FROMRS_DEF:
MAKEWORD("R>", 2, NORMAL, TORS_DEF)
FROMRS:
push bc
ld c,(ix+0)
inc ix
ld b,(ix+0)
inc ix
NEXT
;R@ copies the first item from the return stack to the stack.
RCOPY_DEF:
MAKEWORD("R@", 2, NORMAL, FROMRS_DEF)
RCOPY:
push bc
ld c, (ix+0)
ld b, (ix+1)
NEXT
TWOTORS_DEF:
MAKEWORD("2>R", 3, NORMAL, RCOPY_DEF)
TWOTORS:
dec ix
ld (ix+0), b
dec ix
ld (ix+0), c
pop bc
dec ix
ld (ix+0), b
dec ix
ld (ix+0), c
pop bc
NEXT
TWOFROMRS_DEF:
MAKEWORD("2R>", 3, NORMAL, TWOTORS_DEF)
TWOFROMRS:
push bc
ld c,(ix+0)
inc ix
ld b,(ix+0)
inc ix
push bc
ld c,(ix+0)
inc ix
ld b,(ix+0)
inc ix
NEXT
TWORSCOPY_DEF:
MAKEWORD("2R@", 3, NORMAL, TWOFROMRS_DEF)
TWORSCOPY:
push bc
ld c, (ix+0)
ld b, (ix+1)
push bc
ld c, (ix+2)
ld b, (ix+3)
NEXT
RDROP_DEF:
MAKEWORD("RDROP", 5, NORMAL, TWORSCOPY_DEF)
RDROP:
inc ix
inc ix
NEXT
TWORDROP_DEF:
MAKEWORD("2RDROP", 6, NORMAL, RDROP_DEF)
TWORDROP:
inc ix
inc ix
inc ix
inc ix
NEXT
;GT greater than.
GRT_DEF:
MAKEWORD('>', 1, NORMAL, TWORDROP_DEF)
GRT:
pop hl
call HLSUBBC
ld bc, 0
ld a, h
or l
jp z, GRT_DONE
bit 7, h
jp nz, GRT_DONE
ld bc, 1
GRT_DONE:
NEXT
;LT less than.
LST_DEF:
MAKEWORD('<', 1, NORMAL, GRT_DEF)
LST:
pop hl
call HLSUBBC
ld bc, 0
ld a, h
or l
jp z, LST_DONE
bit 7, h
jp z, LST_DONE
ld bc, 1
LST_DONE:
NEXT
;ADD addition.
ADD_DEF:
MAKEWORD('+', 1, NORMAL, LST_DEF)
ADD:
pop hl
add hl, bc
ld c, l
ld b, h
NEXT