-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathforth850.asm
6339 lines (5390 loc) · 140 KB
/
forth850.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
;-------------------------------------------------------------------------------
;
; FFFFFFFF OOOOOO RRRRRRR TTTTTTTT HH HH 88888 55555555 0000
; FF OO OO RR RR TT HH HH 88 88 55 00 00
; FF OO OO RR RR TT HH HH 88 88 55 00 00
; FF OO OO RR RR TT HH HH 88 88 55 00 00
; FFFFFF OO OO RRRRRRR TT HHHHHHHH 88888 5555555 00 00
; FF OO OO RR RR TT HH HH 88 88 55 00 00
; FF OO OO RR RR TT HH HH 88 88 55 00 00
; FF OO OO RR RR TT HH HH 88 88 55 55 00 00
; FF OOOOOO RR RR TT HH HH 88888 555555 0000 v1.0
;
;
; Author:
; Dr. Robert van Engelen, Copyright 2022
;
;-------------------------------------------------------------------------------
;
; BSD 3-Clause License
;
; Copyright (c) 2022, Robert van Engelen
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;
; 1. Redistributions of source code must retain the above copyright notice, this
; list of conditions and the following disclaimer.
;
; 2. Redistributions in binary form must reproduce the above copyright notice,
; this list of conditions and the following disclaimer in the documentation
; and/or other materials provided with the distribution.
;
; 3. Neither the name of the copyright holder nor the names of its
; contributors may be used to endorse or promote products derived from
; this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
; CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;
; FORTH850 CPU REGISTERS AND RAM USAGE
;
;-------------------------------------------------------------------------------
;
; A unassigned, available as temp
; BC instruction pointer (IP)
; DE top of stack (TOS)
; HL unassigned, available as temp
; SP parameter stack pointer
; IX unassigned, available as temp
; IY address of the next routine used with jp (iy)
; [rp] return stack pointer in RAM
;
; A' BC' DE' HL' not used by an interrupt routine, available as temps
;
;-------------------------------------------------------------------------------
.title Forth850
.list (me)
.area PROGRAM (ABS)
.org 0x0100
FAST = 1 ; fast (1) or compact (0)
EDIT = 1 ; include a more capable line editor with replay back (1)
FULL = 1 ; include additional words (1)
MATH = 1 ; include floating point math words (1)
TEST = 0 ; include tests.asm (1)
;-------------------------------------------------------------------------------
;
; SCREEN
;
;-------------------------------------------------------------------------------
win_rows .equ 6
win_cols .equ 24
win_size .equ win_rows*win_cols
;-------------------------------------------------------------------------------
;
; PC-G850V(S) SYSCALLS - ENABLED WHEN USED
;
;-------------------------------------------------------------------------------
CLRLN .equ 0x84f7 ; clear bottom line
GETCHR .equ 0xbcc4 ; returns A with ascii key code
GETKEY .equ 0xbcfd ; returns A with key code
RDPSTR .equ 0xbd00 ; read pixel string HL size B at DE=xy
REGOUT .equ 0xbd03 ; display CPU registers, wait for key
PUTCHR .equ 0xbe62 ; put A=char at DE=xy, DE=xy unchanged
INSLN .equ 0xbe65 ; insert line at DE=xy
INKEY .equ 0xbe53 ; returns A with key code cf=1 or 0 cf=0
WRPSTR .equ 0xbfd0 ; draw pixel string HL size B at DE=xy
SCROLL .equ 0xbfeb ; scroll screen up, DE unchanged
REPCHR .equ 0xbfee ; put A=char at DE=xy B times
PUTSTR .equ 0xbff1 ; put string HL=addr length B at DE=xy
;-------------------------------------------------------------------------------
;
; CONFIGURATION
;
;-------------------------------------------------------------------------------
pad_size .equ 256 ; PAD size (do not change)
tib_size .equ 256 ; TIB size (do not change)
tmp_size .equ 256 ; TMP string buffer size (do not change)
r_size .equ 256 ; return stack size (adjustable)
s_size .equ 256 ; parameter stack size (adjustable)
h_size .equ 40 ; hold space size (adjustable)
;-------------------------------------------------------------------------------
;
; PC-G850V(S) SYSTEM POINTERS AND BUFFERS USED IN FORTH
;
;-------------------------------------------------------------------------------
USER .equ 0x7ffe ; MON USER+1 allocated space for FORTH
PAD0 .equ 0x7b00 ; PAD buffer
TIB0 .equ 0x7c00 ; terminal input buffer
TMP0 .equ 0x7d00 ; temporary string buffer 0 (base temp)
TMP1 .equ 0x7e00 ; temporary string buffer 1 (next temp)
;-------------------------------------------------------------------------------
;
; WORD CONTROL BITS
;
;-------------------------------------------------------------------------------
length_bits .equ 0x3f ; word length bitmask and max length
smudge_bit .equ 6 ; smudge bit, should be bit 6
smudge_bits .equ 1<<smudge_bit ; smudge bitmask
immediate_bit .equ 7 ; immediate bit, must be bit 7
immediate_bits .equ 1<<immediate_bit ; immediate bitmask
;-------------------------------------------------------------------------------
;
; FIG FORTH VOCABULARY KLUDGE
;
;-------------------------------------------------------------------------------
fig_kludge .equ 0x2001|smudge_bits ; a blank name with smudge bit set
;-------------------------------------------------------------------------------
;
; FORTH SYS CONSTANTS
;
;-------------------------------------------------------------------------------
colon_sys .equ 0xfdef
do_sys .equ 0xfedf
dest .equ 0xfdee
orig .equ 0xfeed
;-------------------------------------------------------------------------------
;
; ASSEMBLY MACROS
;
;-------------------------------------------------------------------------------
; Compile a code definition header
last_link = 0
.macro CODE name,label
link = last_link
last_link = .
.nchr len,^|name|
.dw link
.db len
.str ^|name|
label:
.endm
; Compile an immediate code definition header
.macro CODE_IMM name,label
link = last_link
last_link = .
.nchr len,^|name|
.dw link
.db len|immediate_bits
.str ^|name|
label:
.endm
; Compile a colon definition header
.macro COLON name,label
CODE ^|name|,label
call docol
.endm
; Compile an immediate colon definition header
.macro COLON_IMM name,label
CODE_IMM ^|name|,label
call docol
.endm
; Compile a user variable header
.macro VARIABLE name,label
CODE ^|name|,label
call dovar
.endm
; Compile a user constant header
.macro CONSTANT name,label
CODE ^|name|,label
call docon
.endm
; Compile a user value header
.macro VALUE name,label
CODE ^|name|,label
call doval
.endm
; Compile a user double value header
.macro TWOVALUE name,label
CODE ^|name|,label
call dotwoval
.endm
; Compile the next routine
.macro JP_NEXT
jp (iy) ; 8(46); jump to next routine
.endm
.macro NEXT
.if FAST
ld a,(bc) ; 7 ;
ld l,a ; 4 ;
inc bc ; 6 ;
ld a,(bc) ; 7 ;
ld h,a ; 4 ;
inc bc ; 6 ; [ip++] -> hl with xt
jp (hl) ; 4(38); jump to hl
.else
JP_NEXT ; 8(46); jump to next routine
.endif
.endm
; Compile an inline string literal
.macro SLIT string
.nchr len,^|string|
.dw doslit
.db len
.str ^|string|
.endm
;-------------------------------------------------------------------------------
;
; ENTRY POINT BOOT UP
;
;-------------------------------------------------------------------------------
boot:: ld (bsp),sp ; save BASIC sp to [bsp]
ld hl,(rp0) ;
ld (rp),hl ; set [rp0] -> [rp] FORTH RP
ld de,-r_size ;
add hl,de ; [rp0] - r_size -> hl
ld (sp0),hl ; set hl -> [sp0]
ld sp,hl ; set [sp0] -> sp FORTH parameter sp
ex de,hl ;
ld hl,-1 ;
sbc hl,sp ;
ld (sp1),hl ; -1 - [sp0] -> [sp1] sp for overflow check
ex de,hl ;
ld de,-s_size-h_size ;
add hl,de ; [sp0] - s_size - h_size -> hl
ld (top),hl ; hl -> [top]
ld iy,next ; set iy to next for jp (iy) in JP_NEXT
call docol ; (:) start interpreting
.dw decimal
.dw page
SLIT ^|FORTH|
.dw type
.dw unused,dolit,7,udotr
SLIT ^| bytes free|
.dw type
.dw repl
rp0 .equ USER ; rp0 = USER is top of (above) free memory
sp0: .dw 0 ; sp0
sp1: .dw 0 ; sp1 = -1 - sp0
top: .dw 0 ; dictionary top
bsp: .dw 0 ; saved BASIC stack pointer
;-------------------------------------------------------------------------------
;
; START OF THE DICTIONARY
;
;-------------------------------------------------------------------------------
; (:) -- ; R: -- ip
; call colon definition;
; runtime of the : compile-only word
CODE (:),docol
ld hl,(rp) ; 16 ; [rp] -> hl
dec hl ; 6 ;
ld (hl),b ; 7 ;
dec hl ; 6 ;
ld (hl),c ; 7 ; save bc -> [--rp] with caller ip on the return stack
ld (rp),hl ; 16 ; ip - 2 -> [rp]
pop bc ; 10(68); pop ip saved by call docol
cont: ; continue with ON/BREAK key check
in a,(0x1f) ; 11 ; port 0x1f bit 7 is set if ON/BREAK is depressed
add a ; 4 ; test ON/BREAK key
jr c,break ; 7(22); if ON/BREAK pressed then break
next: ; the next routine
ld a,(bc) ; 7 ;
ld l,a ; 4 ;
inc bc ; 6 ;
ld a,(bc) ; 7 ;
ld h,a ; 4 ;
inc bc ; 6 ; [ip++] -> hl with xt
jp (hl) ; 4(38); jump to hl
break: ; ON/BREAK key handling
call INKEY ; INKEY
jr c,break ; repeat INKEY while a key is depressed
ld a,-28 ;
jp throw_a ; throw -28 "user interrupt"
; (;) -- ; R: ip --
; return to caller from colon definition;
; runtime of the ; compile-only word
CODE ^|(;)|,doret
ld hl,(rp) ; 16 ; [rp] -> hl
ld c,(hl) ; 7 ;
inc hl ; 6 ;
ld b,(hl) ; 7 ;
inc hl ; 6 ;
ld (rp),hl ; 16(58); restore [rp++] -> bc with ip of the caller
NEXT ; continue
; (EXIT) -- ; R: ip --
; return to caller from colon definition;
; runtime of the EXIT compile-only word
CODE (EXIT),doexit
jr doret ; same as (;)
; (;CODE) -- ; R: ip --
; set LASTXT cfa to ip and return from colon definition;
; a runtime word compiled by the DOES> compile-only word
CODE ^|(;CODE)|,doscode
ld hl,(lastxt+3) ; LASTXT -> hl with last defined word xt
inc hl ;
ld (hl),c ;
inc hl ;
ld (hl),b ; ip -> [LASTXT+1] overwrite call address
jr doret ; (;) return to caller
; (DOES) addr -- addr ; R: -- ip
; calls the DOES> definition with pfa addr;
; a runtime word compiled by the DOES> compile-only word coded as call dodoes
CODE (DOES),dodoes
ld hl,(rp) ; 16 ; [rp] -> hl
dec hl ; 6 ;
ld (hl),b ; 7 ;
dec hl ; 6 ;
ld (hl),c ; 7 ;
ld (rp),hl ; 16 ; save bc -> [--rp] with old ip on the return stack
pop bc ; 10 ; pop bc with new ip of the DOES> routine saved by call dodoes
pop hl ; 10 ; pop pfa addr
push de ; 11 ; save TOS
ex de,hl ; 4(93); set new TOS to hl with pfa addr
NEXT ; continue
; (VAR) -- addr
; leave parameter field address (pfa) of variable;
; runtime word of a VARIABLE coded as call dovar
CODE (VAR),dovar
pop hl ; 10 ; pop hl with pfa addr saved by call dovar
push de ; 11 ; save TOS
ex de,hl ; 4(25); set new TOS to hl with pfa addr
NEXT ; continue
; (VAL) -- x
; fetch value;
; runtime word of a VALUE coded as call doval
CODE (VAL),doval
pop hl ; 10 ; pop hl with pfa addr saved by call doval
push_fetch: push de ; 11 ; save TOS
fetch_de: ld e,(hl) ; 7 ;
inc hl ; 6 ;
ld d,(hl) ; 7(41); set [hl] -> de as new TOS
NEXT ; continue
; (2VAL) -- dx
; fetch double value;
; runtime word of a 2VALUE coded as call dotwoval
CODE (2VAL),dotwoval
pop hl ; 10 ; pop hl with pfa addr saved by call dotwocon
push_twofetch: push de ; 11 ;
twofetch_de: inc hl ; 6 ;
inc hl ; 6 ;
ld e,(hl) ; 7 ;
inc hl ; 6 ;
ld d,(hl) ; 7 ;
dec hl ; 6 ;
dec hl ; 6 ;
dec hl ; 6 ;
jr push_fetch ; 31(102; save de as 2OS, set [hl] -> de as new TOS and continue
; (CON) -- x
; fetch constant;
; runtime word of a CONSTANT coded as call docon
CODE (CON),docon
jr doval ; same as (VAL)
; (2CON) -- x
; fetch double constant;
; runtime word of a 2CONSTANT coded as call dotwocon
CODE (2CON),dotwocon
jr dotwoval ; same as (2VAL)
; (DEF) --
; execute deferred word;
; runtime word of a DEFER coded as call dodef
CODE (DEF),dodef
pop hl ; 10 ; pop hl with pfa addr saved by call dodef
ld a,(hl) ; 7 ;
inc hl ; 6 ;
ld h,(hl) ; 7 ;
ld l,a ; 4 ; [hl] -> hl with execution token
jp (hl) ; 4(38); execute the execution token
; (LIT) -- x
; fetch literal;
; runtime word compiled by EVALUATE, INTERPRET and NUMBER
CODE (LIT),dolit
push de ; save TOS
ld a,(bc) ;
ld e,a ;
inc bc ;
ld a,(bc) ;
ld d,a ;
inc bc ; set [ip++] -> de as new TOS
NEXT ; continue
; (2LIT) -- x1 x2
; fetch double literal;
; runtime word compiled by EVALUATE, INTERPRET and NUMBER
CODE (2LIT),dotwolit
push de ; save TOS
ld a,(bc) ;
ld e,a ;
inc bc ;
ld a,(bc) ;
ld d,a ;
inc bc ; set [ip++] -> de as new TOS
ld a,(bc) ;
ld l,a ;
inc bc ;
ld a,(bc) ;
ld h,a ;
inc bc ; set [ip++] -> hl as new 2OS
push hl ; save hl as 2OS
JP_NEXT ; continue
; (SLIT) -- c-addr u
; fetch literal string;
; runtime word compiled by S" and ."
CODE (SLIT),doslit
push de ; save TOS
ld a,(bc) ;
inc bc ; [ip++] -> a with string length byte
push bc ; save bc = c-addr as new 2OS
ld e,a ;
ld d,0 ; set a -> de with u as new TOS
add c ;
ld c,a ;
ld a,d ; 0 -> a
adc b ;
ld b,a ; ip + u -> ip
JP_NEXT ; continue
;-------------------------------------------------------------------------------
;
; CONSTANTS
;
;-------------------------------------------------------------------------------
; 0 -- 0
; leave constant 0
;
; 0 CONSTANT 0
CODE 0,zero
push de ; save TOS
zero_next: ld de,0 ; set new TOS to 0
NEXT ; continue
false .equ zero ; alias
false_next .equ zero_next ; alias
; 1 -- 1
; leave constant 1
;
; 1 CONSTANT 1
CODE 1,one
push de ; save TOS
one_next: ld de,1 ; set new TOS to 1
NEXT ; continue
; -1 -- -1
; leave constant -1
;
; -1 CONSTANT -1
CODE -1,mone
push de ; save TOS
mone_next: ld de,-1 ; set new TOS to -1
NEXT ; continue
true .equ mone ; alias
true_next .equ mone_next ; alias
.if FULL
;+ FALSE -- 0
; leave 0
;
; 0 CONSTANT FALSE
CODE FALSE,false_
jr zero
;+ TRUE -- -1
; leave -1
;
; -1 CONSTANT TRUE
CODE TRUE,true_
jr mone
.endif
; BL -- 32
; leave constant 32 (space)
;
; #32 CONSTANT BL
CODE BL,bl
push de ; save TOS
ld de,0x20 ; set new TOS to 0x20
JP_NEXT ; continue
; PAD -- c-addr
; leave address of the PAD;
; the PAD is a free buffer space of 256 bytes not used by Forth850
CODE PAD,pad
push de ; save TOS
ld de,PAD0 ; set new TOS to PAD0
JP_NEXT ; continue
; TIB -- c-addr
; leave address of TIB;
; the terminal input buffer used by Forth850
CODE TIB,tib
push de ; save TOS
ld de,TIB0 ; set new TOS to TIB0
JP_NEXT ; continue
; TMP -- c-addr
; leave address of the next temp string buffer;
; switches between two string buffers of 256 free bytes each;
; used by S" to store a string when interpreting
CODE TMP,tmp
push de ; save TOS
ld hl,1$ ; 1$ -> hl
ld a,(hl) ; [1$] -> a with counter 0 or 1
xor 1 ; a ^ 1 -> a
ld (hl),a ; [1$] ^ 1 -> [1$]
ld de,TMP0 ;
add d ;
ld d,a ; set TMP0 + (a << 8) -> de as new TOS
JP_NEXT ; continue
1$: .db 1 ; previous tmp buffer 0 or 1
;-------------------------------------------------------------------------------
;
; STACK OPERATIONS
;
;-------------------------------------------------------------------------------
; DROP x --
; drop TOS
CODE DROP,drop
pop de ; 10 ; pop new TOS
NEXT ; continue
; DUP x -- x x
; duplicate TOS
CODE DUP,dup
push de ; 11 ; set new TOS
NEXT ; continue
; ?DUP x -- x x or 0 -- 0
; duplicate TOS if nonzero
CODE ?DUP,qdup
ld a,e ; 4 ;
or d ; 4 ; test de = 0
jr nz,dup ; 23/7 ; if de <> 0 then DUP
NEXT ; continue
; SWAP x1 x2 -- x2 x1
; swap TOS with 2OS
CODE SWAP,swap
pop hl ; 10 ; pop hl with 2OS
push de ; 11 ; save de as new 2OS
ex de,hl ; 4(25); set new TOS to hl
NEXT ; continue
; OVER x1 x2 -- x1 x2 x1
; copy 2OS over TOS
CODE OVER,over
pop hl ; 10 ; pop hl with 2OS
push hl ; 11 ; keep 2OS
push de ; 11 ; save TOS
ex de,hl ; 4(36); set new TOS to hl with old 2OS
NEXT ; continue
; ROT x1 x2 x3 -- x2 x3 x1
; rotate cells
;
; : ROT >R SWAP R> SWAP ;
CODE ROT,rot
pop hl ; 10 ; pop hl with 2OS
ex (sp),hl ; 19 ; save hl as new 3OS, set hl to old 3OS
push de ; 11 ; save TOS
ex de,hl ; 4(44); set new TOS to hl with old 3OS
NEXT ; continue
; -ROT x1 x2 x3 -- x3 x1 x2
; undo (or back, or left) rotate cells
;
; : -ROT ROT ROT ;
CODE -ROT,mrot
pop hl ; 10 ; pop hl with 2OS
ex de,hl ; 4 ; set de to 2OS, hl to TOS
ex (sp),hl ; 19 ; save hl as new 3OS, set hl to old 3OS
push hl ; 11(44); save hl as new 2OS
NEXT ; continue
; NIP x1 x2 -- x2
; nip 2OS
;
; : NIP SWAP DROP ;
CODE NIP,nip
pop hl ; 10 ; discard 2OS
NEXT ; continue
; TUCK x1 x2 -- x2 x1 x2
; tuck TOS under 2OS
;
; : TUCK SWAP OVER ;
CODE TUCK,tuck
pop hl ; 10 ; pop hl with 2OS
push de ; 11 ; save TOS as new 3OS
push hl ; 11(32); save hl as new 2OS
NEXT ; continue
; 2DROP xd1 xd2 -- xd1
; drop double TOS
;
; : 2DROP DROP DROP ;
CODE 2DROP,twodrop
pop de ; 10 ; discard 2OS
pop de ; 10(20); pop new TOS
NEXT ; continue
; 2DUP xd -- xd xd
; duplicate double TOS
;
; : 2DUP OVER OVER ;
CODE 2DUP,twodup
pop hl ; pop hl with 2OS
push hl ; keep 2OS as new 4OS
push de ; save de as new 3OS
push hl ; save hl as new 2OS
NEXT ; continue
; 2SWAP xd1 xd2 -- xd2 xd1
; swap double TOS with double 2OS
;
; : 2SWAP ROT >R ROT R> ;
; : 2SWAP 3 ROLL 3 ROLL ;
CODE 2SWAP,twoswap
pop ix ; pop ix with 2OS
pop hl ; pop hl with 3OS
ex (sp),ix ; save ix as new 4OS, ix with new 2OS
push de ; save de as new 3OS
push ix ; save ix as new 2OS
ex de,hl ; set new TOS to hl
NEXT ; continue
; 2OVER xd1 xd2 -- xd1 xd2 xd1
; copy double 2OS over double TOS
;
; : 2OVER >R >R 2DUP R> R> 2SWAP ;
; : 2OVER 3 PICK 3 PICK ;
CODE 2OVER,twoover
push de ; save TOS
ld hl,6 ;
add hl,sp ; sp + 6 -> hl
ld e,(hl) ;
inc hl ;
ld d,(hl) ;
push de ; save [sp+6] -> de new 2OS
dec hl ;
dec hl ;
ld d,(hl) ;
dec hl ;
ld e,(hl) ; set [sp+4] -> de as new TOS
NEXT ; continue
.if FULL
;+ 2ROT xd1 xd2 xd3 -- xd2 xd3 xd1
; rotate double cells
;
; : 2ROT 5 ROLL 5 ROLL ;
COLON 2ROT,tworot
.dw dolit,5,roll,dolit,5,roll
.dw doret
.endif
; DEPTH -- u
; parameter stack depth
;
; : DEPTH sp0 @ SP@ - 2- 2/ ;
CODE DEPTH,depth
push de ; save TOS
ld hl,(sp0) ; [sp0] -> hl
dec hl ;
scf ; 1 -> cf
sbc hl,sp ;
ex de,hl ; set [sp0] - sp - 2 -> de as TOS
jp twoslash ; 2/ divide TOS by 2
; CLEAR ... --
; purge parameter stack
;
; : CLEAR sp0 @ SP! ;
CODE CLEAR,clear
ld sp,(sp0) ; [sp0] -> sp
JP_NEXT ; continue
; .S --
; display parameter stack
;
; : .S DEPTH 0 ?DO sp0 @ I 2+ CELLS - ? LOOP ;
COLON .S,dots
.dw depth,zero,doqdo,2$
1$: .dw dolit,sp0,fetch,i,twoplus,cells,minus,question
.dw doloop,1$
2$: .dw doret
; SP@ -- addr
; fetch stack pointer
CODE SP@,spfetch
push de ; save TOS
ld hl,0 ;
add hl,sp ; sp -> hl
ex de,hl ; set new TOS to hl with sp
JP_NEXT ; continue
; SP! addr --
; store stack pointer
CODE SP!,spstore
ex de,hl ;
ld sp,hl ; addr -> sp
pop de ;
JP_NEXT ; continue
.if 0 ; unused, but perhaps useful later
;- N>R n*x n -- ; R: -- n*x n
; move n cells to the return stack
CODE N>R,ntor
push de ; save TOS
exx ; save bc with ip
pop bc ;
push bc ; TOS -> bc
inc bc ;
ld l,c ;
ld h,b ;
add hl,bc ;
ld c,l ;
ld b,h ; 2 * (bc + 1) -> bc
ld hl,(rp) ; [rp] -> hl
xor a ;
sbc hl,bc ; hl - bc -> hl
ld (rp),hl ; [rp] - bc -> rp
ex de,hl ; [rp] - bc -> de
ld l,a ;
ld h,a ; 0 -> hl asserted a = 0
add hl,sp ; sp -> hl
ldir ; [hl++] -> [de++] until --bc = 0
ld sp,hl ; hl -> sp
exx ; restore bc with ip
pop de ; pop new TOS
JP_NEXT ; continue
;- NR> R: n*x n -- ; -- n*x n
; move n cells from the return stack
CODE NR>,nrfrom
push de ; save TOS
exx ; save bc with ip
ld hl,(rp) ; [rp] -> hl
ld c,(hl) ;
inc hl ;
ld b,(hl) ;
dec hl ; [rp] -> bc
ex de,hl ; [rp] -> de
inc bc ;
ld l,c ;
ld h,b ;
add hl,bc ;
ld c,l ;
ld b,h ; 2 * (bc + 1) -> bc
ld hl,0 ;
add hl,sp ;
sbc hl,bc ;
ld sp,hl ; sp - bc -> sp,de
ex de,hl ; [rp] -> hl
ldir ; [hl++] -> [de++] until --bc = 0
ld (rp),hl ; hl -> rp
exx ; restore bc with ip
pop de ; pop new TOS
JP_NEXT ; continue
.endif
; >R x -- ; R: -- x
; move TOS to the return stack
CODE >R,tor
ld hl,(rp) ; 16 ; [rp] -> hl
dec hl ; 6 ;
ld (hl),d ; 7 ;
dec hl ; 6 ;
ld (hl),e ; 7 ; de -> [--rp]
ld (rp),hl ; 16 ;
pop de ; 10(58); pop new TOS
NEXT ; continue
; DUP>R x -- x ; R: -- x
; duplicate TOS to the return stack, a single word for DUP >R
CODE DUP>R,duptor
push de ; 11 ; save TOS
jr tor ; 60(71); >R
; R> R: x -- ; -- x
; move cell from the return stack
CODE R>,rfrom
push de ; 11 ; save TOS
ld hl,(rp) ; 16 ; [rp] -> hl
ld e,(hl) ; 7 ;
inc hl ; 6 ;
ld d,(hl) ; 7 ;
inc hl ; 6 ; set [rp++] -> de as new TOS
ld (rp),hl ; 16(59);
NEXT ; continue
; RDROP R: x -- ; --
; drop cell from the return stack, a single word for R> DROP
CODE RDROP,rdrop
ld hl,(rp) ; [rp] -> hl
inc hl ;
inc hl ;
ld (rp),hl ; rp + 2 -> rp
NEXT ; continue
; R@ R: x -- x ; -- x
; fetch cell from the return stack
CODE R@,rfetch
ld hl,(rp) ; [rp] -> hl
jp push_fetch ; set [hl] -> de as new TOS and continue
; 2>R x1 x2 -- ; R: -- x1 x2
; move double TOS to the return stack, a single word for SWAP >R >R
CODE 2>R,twotor
pop hl ; pop hl with 2OS
push de ; save TOS
ex de,hl ; 2OS -> de
ld hl,(rp) ; [rp] -> hl
dec hl ;
ld (hl),d ;
dec hl ;
ld (hl),e ; save de -> [--rp] with 2OS
ld (rp),hl ;
pop de ; restore TOS
jr tor ; >R
; 2R> R: x1 x2 -- ; -- x1 x2
; move double cell from the return stack, a single word for R> R> SWAP
CODE 2R>,tworfrom
push de ; save TOS
ld hl,(rp) ; [rp] -> hl
ld e,(hl) ;
inc hl ;
ld d,(hl) ;
inc hl ;
push de ; save [rp++] -> de as new 2OS
ld e,(hl) ;
inc hl ;
ld d,(hl) ;
inc hl ; [rp++] -> de as TOS
ld (rp),hl ;
jp swap ; SWAP
; 2R@ R: x1 x2 -- x1 x2 ; -- x1 x2
; fetch double cell from the return stack
CODE 2R@,tworfetch
push de ; save TOS
ld hl,(rp) ; [rp] -> hl
ld e,(hl) ;
inc hl ;
ld d,(hl) ;
inc hl ;
push de ; save [rp++] -> de as new 2OS
ld e,(hl) ;
inc hl ;
ld d,(hl) ;
jp swap ; SWAP
; RP@ -- addr