forked from jamesbowman/camelforth-z80
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamel80.asm.m4
1033 lines (934 loc) · 29.1 KB
/
camel80.asm.m4
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
; Listing 2.
; ===============================================
; CamelForth for the Zilog Z80
; Copyright (c) 1994,1995 Bradford J. Rodriguez
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
; Commercial inquiries should be directed to the author at
; 115 First St., #105, Collingwood, Ontario L9Y 4W3 Canada
; or via email to [email protected]
;
; ===============================================
; CAMEL80.AZM: Code Primitives
; Source code is for the Z80MR macro assembler.
; Forth words are documented as follows:
;x NAME stack -- stack description
; where x=C for ANS Forth Core words, X for ANS
; Extensions, Z for internal or private words.
;
; Direct-Threaded Forth model for Zilog Z80
; 16 bit cell, 8 bit char, 8 bit (byte) adrs unit
; Z80 BC = Forth TOS (top Param Stack item)
; HL = W working register
; DE = IP Interpreter Pointer
; SP = PSP Param Stack Pointer
; IX = RSP Return Stack Pointer
; IY = UP User area Pointer
; A, alternate register set = temporaries
;
; Revision history:
; 19 Aug 94 v1.0
; 25 Jan 95 v1.01 now using BDOS function 0Ah
; for interpreter input; TIB at 82h.
; 02 Mar 95 v1.02 changed ALIGN to ALIGNED in
; S" (S"); changed ,BRANCH to ,XT in DO.
; ===============================================
; Macros to define Forth headers
; HEAD label,length,name,action
; IMMED label,length,name,action
; label = assembler name for this word
; (special characters not allowed)
; length = length of name field
; name = Forth's name for this word
; action = code routine for this word, e.g.
; DOCOLON, or DOCODE for code words
; IMMED defines a header for an IMMEDIATE word.
;
define(link, 0)
define(head, `
dw link
db 0
$1_link:
define(`link', `$1_link')
defm len($2), "patsubst($2, ", `",34,"')"
$1:
ifelse($3,docode,, call $3)
')
define(link, 0)
define(immed, `
dw link
db 1
$1_link:
define(`link', `$1_link')
defm len($2), "patsubst($2, ", `",34,"')"
$1:
ifelse($3,docode,, call $3)
')
; NEXTHL is used when the IP is already in HL.
define(nexthl, `
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ex de,hl
jp (hl)')
; The NEXT macro (7 bytes) assembles the 'next'
; code in-line in every Z80 CamelForth CODE word.
define(next, `
ex de,hl
nexthl
')
define(ex_sp_de, `
ex de,hl
ex (sp),hl
ex de,hl')
; RESET AND INTERRUPT VECTORS ===================
; RC2014 Entry point
org $9000
reset: ld hl,$fc00
dec h ; EM-100h
ld sp,hl ; = top of param stack
inc h ; EM
push hl
pop ix ; = top of return stack
dec h ; EM-200h
dec h
push hl
pop iy ; = bottom of user area
ld de,1 ; do reset if COLD returns
jp COLD ; enter top-level Forth word
; Memory map:
; 0080h Terminal Input Buffer, 128 bytes
; 0100h Forth kernel = start of CP/M TPA
; ? h Forth dictionary (user RAM)
; EM-200h User area, 128 bytes
; EM-180h Parameter stack, 128B, grows down
; EM-100h HOLD area, 40 bytes, grows down
; EM-0D8h PAD buffer, 88 bytes
; EM-80h Return stack, 128 B, grows down
; EM End of RAM = start of CP/M BDOS
; See also the definitions of U0, S0, and R0
; in the "system variables & constants" area.
; A task w/o terminal input requires 200h bytes.
; Double all except TIB and PAD for 32-bit CPUs.
; INTERPRETER LOGIC =============================
; See also "defining words" at end of this file
;C EXIT -- exit a colon definition
head(EXIT,EXIT,docode)
ld e,(ix+0) ; pop old IP from ret stk
inc ix
ld d,(ix+0)
inc ix
next
;Z lit -- x fetch inline literal to stack
; This is the primtive compiled by LITERAL.
head(lit,LIT,docode)
push bc ; push old TOS
ld a,(de) ; fetch cell at IP to TOS,
ld c,a ; advancing IP
inc de
ld a,(de)
ld b,a
inc de
next
;C EXECUTE i*x xt -- j*x execute Forth word
;C at 'xt'
head(EXECUTE,EXECUTE,docode)
ld h,b ; address of word -> HL
ld l,c
pop bc ; get new TOS
jp (hl) ; go do Forth word
; DEFINING WORDS ================================
; ENTER, a.k.a. DOCOLON, entered by CALL ENTER
; to enter a new high-level thread (colon def'n.)
; (internal code fragment, not a Forth word)
; N.B.: DOCOLON must be defined before any
; appearance of 'docolon' in a 'word' macro!
docolon: ; (alternate name)
enter: dec ix ; push old IP on ret stack
ld (ix+0),d
dec ix
ld (ix+0),e
pop hl ; param field adrs -> IP
nexthl
;C VARIABLE -- define a Forth variable
; CREATE 1 CELLS ALLOT ;
; Action of RAM variable is identical to CREATE,
; so we don't need a DOES> clause to change it.
head(VARIABLE,VARIABLE,docolon)
DW CREATE,lit,1,CELLS,ALLOT,EXIT
; DOVAR, code action of VARIABLE, entered by CALL
; DOCREATE, code action of newly created words
docreate:
dovar: ; -- a-addr
pop hl ; parameter field address
push bc ; push old TOS
ld b,h ; pfa = variable's adrs -> TOS
ld c,l
next
;C CONSTANT n -- define a Forth constant
; CREATE , DOES> (machine code fragment)
head(CONSTANT,CONSTANT,docolon)
DW CREATE,COMMA,XDOES
; DOCON, code action of CONSTANT,
; entered by CALL DOCON
docon: ; -- x
pop hl ; parameter field address
push bc ; push old TOS
ld c,(hl) ; fetch contents of parameter
inc hl ; field -> TOS
ld b,(hl)
next
;Z USER n -- define user variable 'n'
; CREATE , DOES> (machine code fragment)
head(USER,USER,docolon)
DW CREATE,COMMA,XDOES
; DOUSER, code action of USER,
; entered by CALL DOUSER
douser: ; -- a-addr
pop hl ; parameter field address
push bc ; push old TOS
ld c,(hl) ; fetch contents of parameter
inc hl ; field
ld b,(hl)
push iy ; copy user base address to HL
pop hl
add hl,bc ; and add offset
ld b,h ; put result in TOS
ld c,l
next
; DODOES, code action of DOES> clause
; entered by CALL fragment
; parameter field
; ...
; fragment: CALL DODOES
; high-level thread
; Enters high-level thread with address of
; parameter field on top of stack.
; (internal code fragment, not a Forth word)
dodoes: ; -- a-addr
dec ix ; push old IP on ret stk
ld (ix+0),d
dec ix
ld (ix+0),e
pop de ; adrs of new thread -> IP
pop hl ; adrs of parameter field
push bc ; push old TOS onto stack
ld b,h ; pfa -> new TOS
ld c,l
next
; TERMINAL I/O ==================================
;C API1 a c -- a execute API function
head(API1,API1,docode)
ex de,hl
ex (sp),hl ; parameter A is in L, DE in TOS
ld a,l
rst $30
ld b,0
ld c,a
pop de
next
;C API2 de a c -- a execute API function
head(API2,AP2,docode)
pop hl
ld a,l
ex_sp_de
rst $30
ld b,0
ld c,a
pop de
next
;C EMIT c -- output character to console
; 6 BDOS DROP ;
; warning: if c=0ffh, will read one keypress
head(EMIT,EMIT,docolon)
DW lit,2,API1,DROP,EXIT
;X KEY? -- f return true if char waiting
; 0FF 6 BDOS DUP SAVEKEY C! ; rtns 0 or key
; must use BDOS function 6 to work with KEY
head(QUERYKEY,KEY?,docolon)
DW lit,0,EXIT
;C KEY -- c get character from keyboard
; BEGIN SAVEKEY C@ 0= WHILE KEY? DROP REPEAT
; SAVEKEY C@ 0 SAVEKEY C! ;
; must use CP/M direct console I/O to avoid echo
; (BDOS function 6, contained within KEY?)
head(KEY,KEY,docolon)
DW lit,0,lit,1,API1,EXIT
dnl ;Z CPMACCEPT c-addr +n -- +n' get line of input
dnl ; SWAP 2 - TUCK C! max # of characters
dnl ; DUP 0A BDOS DROP CP/M Get Console Buffer
dnl ; 1+ C@ 0A EMIT ; get returned count
dnl ; Note: requires the two locations before c-addr
dnl ; to be available for use.
head(CPMACCEPT,CPMACCEPT,docolon)
DW lit,4,API2,EXIT
;X BYE i*x -- return to CP/M
head(BYE,BYE,docode)
jp 0
; STACK OPERATIONS ==============================
;C DUP x -- x x duplicate top of stack
head(DUP,DUP,docode)
pushtos: push bc
next
;C ?DUP x -- 0 | x x DUP if nonzero
head(QDUP,?DUP,docode)
ld a,b
or c
jr nz,pushtos
next
;C DROP x -- drop top of stack
head(DROP,DROP,docode)
poptos: pop bc
next
;C SWAP x1 x2 -- x2 x1 swap top two items
head(SWOP,SWAP,docode)
pop hl
push bc
ld b,h
ld c,l
next
;C OVER x1 x2 -- x1 x2 x1 per stack diagram
head(OVER,OVER,docode)
pop hl
push hl
push bc
ld b,h
ld c,l
next
;C ROT x1 x2 x3 -- x2 x3 x1 per stack diagram
head(ROT,ROT,docode)
; x3 is in TOS
pop hl ; x2
ex (sp),hl ; x2 on stack, x1 in hl
push bc
ld b,h
ld c,l
next
;X NIP x1 x2 -- x2 per stack diagram
head(NIP,NIP,docolon)
DW SWOP,DROP,EXIT
;X TUCK x1 x2 -- x2 x1 x2 per stack diagram
head(TUCK,TUCK,docolon)
DW SWOP,OVER,EXIT
;C >R x -- R: -- x push to return stack
head(TOR,>R,docode)
dec ix ; push TOS onto rtn stk
ld (ix+0),b
dec ix
ld (ix+0),c
pop bc ; pop new TOS
next
;C R> -- x R: x -- pop from return stack
head(RFROM,R>,docode)
push bc ; push old TOS
ld c,(ix+0) ; pop top rtn stk item
inc ix ; to TOS
ld b,(ix+0)
inc ix
next
;C R@ -- x R: x -- x fetch from rtn stk
head(RFETCH,R@,docode)
push bc ; push old TOS
ld c,(ix+0) ; fetch top rtn stk item
ld b,(ix+1) ; to TOS
next
;Z SP@ -- a-addr get data stack pointer
head(SPFETCH,SP@,docode)
push bc
ld hl,0
add hl,sp
ld b,h
ld c,l
next
;Z SP! a-addr -- set data stack pointer
head(SPSTORE,SP!,docode)
ld h,b
ld l,c
ld sp,hl
pop bc ; get new TOS
next
;Z RP@ -- a-addr get return stack pointer
head(RPFETCH,RP@,docode)
push bc
push ix
pop bc
next
;Z RP! a-addr -- set return stack pointer
head(RPSTORE,RP!,docode)
push bc
pop ix
pop bc
next
; MEMORY AND I/O OPERATIONS =====================
;C ! x a-addr -- store cell in memory
head(STORE,!,docode)
ld h,b ; address in hl
ld l,c
pop bc ; data in bc
ld (hl),c
inc hl
ld (hl),b
pop bc ; pop new TOS
next
;C C! char c-addr -- store char in memory
head(CSTORE,C!,docode)
ld h,b ; address in hl
ld l,c
pop bc ; data in bc
ld (hl),c
pop bc ; pop new TOS
next
;C @ a-addr -- x fetch cell from memory
head(FETCH,@,docode)
ld h,b ; address in hl
ld l,c
ld c,(hl)
inc hl
ld b,(hl)
next
;C C@ c-addr -- char fetch char from memory
head(CFETCH,C@,docode)
ld a,(bc)
ld c,a
ld b,0
next
;Z PC! char c-addr -- output char to port
head(PCSTORE,PC!,docode)
pop hl ; char in L
out (c),l ; to port (BC)
pop bc ; pop new TOS
next
;Z PC@ c-addr -- char input char from port
head(PCFETCH,PC@,docode)
in c,(c) ; read port (BC) to C
ld b,0
next
; ARITHMETIC AND LOGICAL OPERATIONS =============
;C + n1/u1 n2/u2 -- n3/u3 add n1+n2
head(PLUS,+,docode)
pop hl
add hl,bc
ld b,h
ld c,l
next
;X M+ d n -- d add single to double
head(MPLUS,M+,docode)
ex de,hl
pop de ; hi cell
ex (sp),hl ; lo cell, save IP
add hl,bc
ld b,d ; hi result in BC (TOS)
ld c,e
jr nc,mplus1
inc bc
mplus1: pop de ; restore saved IP
push hl ; push lo result
next
;C - n1/u1 n2/u2 -- n3/u3 subtract n1-n2
head(MINUS,-,docode)
pop hl
or a
sbc hl,bc
ld b,h
ld c,l
next
;C AND x1 x2 -- x3 logical AND
head(AND,AND,docode)
pop hl
ld a,b
and h
ld b,a
ld a,c
and l
ld c,a
next
;C OR x1 x2 -- x3 logical OR
head(OR,OR,docode)
pop hl
ld a,b
or h
ld b,a
ld a,c
or l
ld c,a
next
;C XOR x1 x2 -- x3 logical XOR
head(XOR,XOR,docode)
pop hl
ld a,b
xor h
ld b,a
ld a,c
xor l
ld c,a
next
;C INVERT x1 -- x2 bitwise inversion
head(INVERT,INVERT,docode)
ld a,b
cpl
ld b,a
ld a,c
cpl
ld c,a
next
;C NEGATE x1 -- x2 two's complement
head(NEGATE,NEGATE,docode)
ld a,b
cpl
ld b,a
ld a,c
cpl
ld c,a
inc bc
next
;C 1+ n1/u1 -- n2/u2 add 1 to TOS
head(ONEPLUS,1+,docode)
inc bc
next
;C 1- n1/u1 -- n2/u2 subtract 1 from TOS
head(ONEMINUS,1-,docode)
dec bc
next
;Z >< x1 -- x2 swap bytes (not ANSI)
head(swapbytes,><,docode)
ld a,b
ld b,c
ld c,a
next
;C 2* x1 -- x2 arithmetic left shift
head(TWOSTAR,2*,docode)
sla c
rl b
next
;C 2/ x1 -- x2 arithmetic right shift
head(TWOSLASH,2/,docode)
sra b
rr c
next
;C LSHIFT x1 u -- x2 logical L shift u places
head(LSHIFT,LSHIFT,docode)
ld b,c ; b = loop counter
pop hl ; NB: hi 8 bits ignored!
inc b ; test for counter=0 case
jr lsh2
lsh1: add hl,hl ; left shift HL, n times
lsh2: djnz lsh1
ld b,h ; result is new TOS
ld c,l
next
;C RSHIFT x1 u -- x2 logical R shift u places
head(RSHIFT,RSHIFT,docode)
ld b,c ; b = loop counter
pop hl ; NB: hi 8 bits ignored!
inc b ; test for counter=0 case
jr rsh2
rsh1: srl h ; right shift HL, n times
rr l
rsh2: djnz rsh1
ld b,h ; result is new TOS
ld c,l
next
;C +! n/u a-addr -- add cell to memory
head(PLUSSTORE,+!,docode)
pop hl
ld a,(bc) ; low byte
add a,l
ld (bc),a
inc bc
ld a,(bc) ; high byte
adc a,h
ld (bc),a
pop bc ; pop new TOS
next
; COMPARISON OPERATIONS =========================
;C 0= n/u -- flag return true if TOS=0
head(ZEROEQUAL,0=,docode)
ld a,b
or c ; result=0 if bc was 0
sub 1 ; cy set if bc was 0
sbc a,a ; propagate cy through A
ld b,a ; put 0000 or FFFF in TOS
ld c,a
next
;C 0< n -- flag true if TOS negative
head(ZEROLESS,0<,docode)
sla b ; sign bit -> cy flag
sbc a,a ; propagate cy through A
ld b,a ; put 0000 or FFFF in TOS
ld c,a
next
;C = x1 x2 -- flag test x1=x2
head(EQUAL,=,docode)
pop hl
or a
sbc hl,bc ; x1-x2 in HL, SZVC valid
jr z,tostrue
tosfalse: ld bc,0
next
;X <> x1 x2 -- flag test not eq (not ANSI)
head(NOTEQUAL,<>,docolon)
DW EQUAL,ZEROEQUAL,EXIT
;C < n1 n2 -- flag test n1<n2, signed
head(LESS,<,docode)
pop hl
or a
sbc hl,bc ; n1-n2 in HL, SZVC valid
; if result negative & not OV, n1<n2
; neg. & OV => n1 +ve, n2 -ve, rslt -ve, so n1>n2
; if result positive & not OV, n1>=n2
; pos. & OV => n1 -ve, n2 +ve, rslt +ve, so n1<n2
; thus OV reverses the sense of the sign bit
jp pe,revsense ; if OV, use rev. sense
jp p,tosfalse ; if +ve, result false
tostrue: ld bc,0ffffh ; if -ve, result true
next
revsense: jp m,tosfalse ; OV: if -ve, reslt false
jr tostrue ; if +ve, result true
;C > n1 n2 -- flag test n1>n2, signed
head(GREATER,>,docolon)
DW SWOP,LESS,EXIT
;C U< u1 u2 -- flag test u1<n2, unsigned
head(ULESS,U<,docode)
pop hl
or a
sbc hl,bc ; u1-u2 in HL, SZVC valid
sbc a,a ; propagate cy through A
ld b,a ; put 0000 or FFFF in TOS
ld c,a
next
;X U> u1 u2 -- flag u1>u2 unsgd (not ANSI)
head(UGREATER,U>,docolon)
DW SWOP,ULESS,EXIT
; LOOP AND BRANCH OPERATIONS ====================
;Z branch -- branch always
head(branch,branch,docode)
dobranch: ld a,(de) ; get inline value => IP
ld l,a
inc de
ld a,(de)
ld h,a
nexthl
;Z ?branch x -- branch if TOS zero
head(qbranch,?branch,docode)
ld a,b
or c ; test old TOS
pop bc ; pop new TOS
jr z,dobranch ; if old TOS=0, branch
inc de ; else skip inline value
inc de
next
;Z (do) n1|u1 n2|u2 -- R: -- sys1 sys2
;Z run-time code for DO
; '83 and ANSI standard loops terminate when the
; boundary of limit-1 and limit is crossed, in
; either direction. This can be conveniently
; implemented by making the limit 8000h, so that
; arithmetic overflow logic can detect crossing.
; I learned this trick from Laxen & Perry F83.
; fudge factor = 8000h-limit, to be added to
; the start value.
head(xdo,(do),docode)
ex de,hl
ex (sp),hl ; IP on stack, limit in HL
ex de,hl
ld hl,8000h
or a
sbc hl,de ; 8000-limit in HL
dec ix ; push this fudge factor
ld (ix+0),h ; onto return stack
dec ix ; for later use by 'I'
ld (ix+0),l
add hl,bc ; add fudge to start value
dec ix ; push adjusted start value
ld (ix+0),h ; onto return stack
dec ix ; as the loop index.
ld (ix+0),l
pop de ; restore the saved IP
pop bc ; pop new TOS
next
;Z (loop) R: sys1 sys2 -- | sys1 sys2
;Z run-time code for LOOP
; Add 1 to the loop index. If loop terminates,
; clean up the return stack and skip the branch.
; Else take the inline branch. Note that LOOP
; terminates when index=8000h.
head(xloop,(loop),docode)
exx
ld bc,1
looptst: ld l,(ix+0) ; get the loop index
ld h,(ix+1)
or a
adc hl,bc ; increment w/overflow test
jp pe,loopterm ; overflow=loop done
; continue the loop
ld (ix+0),l ; save the updated index
ld (ix+1),h
exx
jr dobranch ; take the inline branch
loopterm: ; terminate the loop
ld bc,4 ; discard the loop info
add ix,bc
exx
inc de ; skip the inline branch
inc de
next
;Z (+loop) n -- R: sys1 sys2 -- | sys1 sys2
;Z run-time code for +LOOP
; Add n to the loop index. If loop terminates,
; clean up the return stack and skip the branch.
; Else take the inline branch.
head(xplusloop,(+loop),docode)
pop hl ; this will be the new TOS
push bc
ld b,h
ld c,l
exx
pop bc ; old TOS = loop increment
jr looptst
;C I -- n R: sys1 sys2 -- sys1 sys2
;C get the innermost loop index
head(II,I,docode)
push bc ; push old TOS
ld l,(ix+0) ; get current loop index
ld h,(ix+1)
ld c,(ix+2) ; get fudge factor
ld b,(ix+3)
or a
sbc hl,bc ; subtract fudge factor,
ld b,h ; returning true index
ld c,l
next
;C J -- n R: 4*sys -- 4*sys
;C get the second loop index
head(JJ,J,docode)
push bc ; push old TOS
ld l,(ix+4) ; get current loop index
ld h,(ix+5)
ld c,(ix+6) ; get fudge factor
ld b,(ix+7)
or a
sbc hl,bc ; subtract fudge factor,
ld b,h ; returning true index
ld c,l
next
;C UNLOOP -- R: sys1 sys2 -- drop loop parms
head(UNLOOP,UNLOOP,docode)
inc ix
inc ix
inc ix
inc ix
next
; MULTIPLY AND DIVIDE ===========================
;C UM* u1 u2 -- ud unsigned 16x16->32 mult.
head(UMSTAR,UM*,docode)
push bc
exx
pop bc ; u2 in BC
pop de ; u1 in DE
ld hl,0 ; result will be in HLDE
ld a,17 ; loop counter
or a ; clear cy
umloop: rr h
rr l
rr d
rr e
jr nc,noadd
add hl,bc
noadd: dec a
jr nz,umloop
push de ; lo result
push hl ; hi result
exx
pop bc ; put TOS back in BC
next
;C UM/MOD ud u1 -- u2 u3 unsigned 32/16->16
head(UMSLASHMOD,UM/MOD,docode)
push bc
exx
pop bc ; BC = divisor
pop hl ; HLDE = dividend
pop de
ld a,16 ; loop counter
sla e
rl d ; hi bit DE -> carry
udloop: adc hl,hl ; rot left w/ carry
jr nc,udiv3
; case 1: 17 bit, cy:HL = 1xxxx
or a ; we know we can subtract
sbc hl,bc
or a ; clear cy to indicate sub ok
jr udiv4
; case 2: 16 bit, cy:HL = 0xxxx
udiv3: sbc hl,bc ; try the subtract
jr nc,udiv4 ; if no cy, subtract ok
add hl,bc ; else cancel the subtract
scf ; and set cy to indicate
udiv4: rl e ; rotate result bit into DE,
rl d ; and `next' bit of DE into cy
dec a
jr nz,udloop
; now have complemented quotient in DE,
; and remainder in HL
ld a,d
cpl
ld b,a
ld a,e
cpl
ld c,a
push hl ; push remainder
push bc
exx
pop bc ; quotient remains in TOS
next
; BLOCK AND STRING OPERATIONS ===================
;C FILL c-addr u char -- fill memory with char
head(FILL,FILL,docode)
ld a,c ; character in a
exx ; use alt. register set
pop bc ; count in bc
pop de ; address in de
or a ; clear carry flag
ld hl,0ffffh
adc hl,bc ; test for count=0 or 1
jr nc,filldone ; no cy: count=0, skip
ld (de),a ; fill first byte
jr z,filldone ; zero, count=1, done
dec bc ; else adjust count,
ld h,d ; let hl = start adrs,
ld l,e
inc de ; let de = start adrs+1
ldir ; copy (hl)->(de)
filldone: exx ; back to main reg set
pop bc ; pop new TOS
next
;X CMOVE c-addr1 c-addr2 u -- move from bottom
; as defined in the ANSI optional String word set
; On byte machines, CMOVE and CMOVE> are logical
; factors of MOVE. They are easy to implement on
; CPUs which have a block-move instruction.
head(CMOVE,CMOVE,docode)
push bc
exx
pop bc ; count
pop de ; destination adrs
pop hl ; source adrs
ld a,b ; test for count=0
or c
jr z,cmovedone
ldir ; move from bottom to top
cmovedone: exx
pop bc ; pop new TOS
next
;X CMOVE> c-addr1 c-addr2 u -- move from top
; as defined in the ANSI optional String word set
head(CMOVEUP,CMOVE>,docode)
push bc
exx
pop bc ; count
pop hl ; destination adrs
pop de ; source adrs
ld a,b ; test for count=0
or c
jr z,umovedone
add hl,bc ; last byte in destination
dec hl
ex de,hl
add hl,bc ; last byte in source
dec hl
lddr ; move from top to bottom
umovedone: exx
pop bc ; pop new TOS
next
;Z SKIP c-addr u c -- c-addr' u'
;Z skip matching chars
; Although SKIP, SCAN, and S= are perhaps not the
; ideal factors of WORD and FIND, they closely
; follow the string operations available on many
; CPUs, and so are easy to implement and fast.
head(skip,SKIP,docode)
ld a,c ; skip character
exx
pop bc ; count
pop hl ; address
ld e,a ; test for count=0
ld a,b
or c
jr z,skipdone
ld a,e
skiploop: cpi
jr nz,skipmis ; char mismatch: exit
jp pe,skiploop ; count not exhausted
jr skipdone ; count 0, no mismatch
skipmis: inc bc ; mismatch! undo last to
dec hl ; point at mismatch char
skipdone: push hl ; updated address
push bc ; updated count
exx
pop bc ; TOS in bc
next
;Z SCAN c-addr u c -- c-addr' u'
;Z find matching char
head(scan,SCAN,docode)
ld a,c ; scan character
exx
pop bc ; count
pop hl ; address
ld e,a ; test for count=0
ld a,b
or c
jr z,scandone
ld a,e
cpir ; scan 'til match or count=0
jr nz,scandone ; no match, BC & HL ok
inc bc ; match! undo last to
dec hl ; point at match char
scandone: push hl ; updated address
push bc ; updated count
exx
pop bc ; TOS in bc
next
;Z S= c-addr1 c-addr2 u -- n string compare
;Z n<0: s1<s2, n=0: s1=s2, n>0: s1>s2