forked from jamesbowman/8051forth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamel51.asm
More file actions
4303 lines (3995 loc) · 110 KB
/
camel51.asm
File metadata and controls
4303 lines (3995 loc) · 110 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
; ===============================================
; CamelForth for the Intel 8051
; (c) 1994,1997,1999 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 bj@camelforth.com
;
; ===============================================
; CAMEL51.ASM: Code Primitives
; Source code is for the A51 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.
;
; Subroutine-Threaded Forth model for Intel 8051
; 16 bit cell, 8 bit char, 8 bit (byte) adrs unit
; split Code & Data spaces
; 8051 PC = Forth IP Interpreter Pointer
; SP = RSP Return Stack Pointer low
; RSP high byte = 0
; R0 = PSP Parameter Stack Ptr low
; PSP high = UP
; reg 08 = P2 = UP User area Pointer high
; (and PSP high), UP low = 0
; DPTR = TOS (top Param. Stack item)
; A,B,R1-R5 = temporaries
; (no W register is defined)
; R6,R7 = loop index
; reg 09-7F = return stack
; ===============================================
; REVISION HISTORY
; $Log: Camel51.asm,v $
; v1.6 18 Aug 99 Fixed FM/MOD (again).
;
; Revision 1.5 1997/05/28 23:03:52 brad
; v1.5 Added multitasker words. 28 Mar 97
; Corrected memory map comments.
; Fixed UM* bug.
; Fixed FM/MOD bug, per Ed Smeda (thanks!)
; Fixed >BODY to return Data adrs, per ANS.
; Moved SWAP from REPEAT to WHILE, per ANS.
; ABORT" ?ABORT now use IS" and ITYPE.
; Fixed WORDS to ignore smudge bit.
; Fixed IWORD to return Code address.
; Renamed ROMDICT,RAMDICT to CODERAM,DATARAM
; v1.4 changed QUIT to type CR even 9 Nov 96
; if STATE<>0, to support downloading.
; Added example I/O.
; v1.3 changed PLUSLOOPSENSE to 17 Mar 96
; PLUSLPSENSE (for some A51 assemblers)
; v1.2 fixed names of KEY? and S>D
; v1.1 bug fixes for split I & D mem 14 Mar 95
; in COLD: changed CMOVE to I->D
; in XISQUOTE: changed COUNT to ICOUNT
; in LIT: uses MOVC instead of MOVX
; in FIND: changed NFA>LFA @ to NFA>LFA I@
; in QUIT: changed TYPE to ITYPE
; v1.0 alpha test version, 12 Dec 94
; ===============================================
; Forth linkage
.equ link,0
.equ IMMED,1 ; flag for Immediate word
; 8051 EQUATES
.equ dr2,0x02 ; r2-r5 accessed as
.equ dr3,0x03 ; direct registers;
.equ dr4,0x04 ; required for PUSH and
.equ dr5,0x05 ; POP instructions.
.equ dr6,0x06 ; Assumes register bank 0
.equ dr7,0x07 ; is selected.
.equ UP,0x08
; FORTH MEMORY MAP EQUATES
; Memory map:
; regs 8-7Fh Return stack, 120 bytes, grows up
; 0000h Forth kernel
; E000h Forth dictionary (program)
; F000h Forth dictionary (user RAM)
; UAREA-100h Task Save Area, 128 bytes
; UAREA-80h Terminal Input Buffer, 128 bytes
; UAREA=FE00h User area, 128 bytes
; UAREA+80h Parameter stack, 128B, grows down
; UAREA+100h HOLD area, 40 bytes, grows down
; UAREA+128h PAD buffer, 88 bytes
; UAREA+180h Leave stack, 128 bytes, grows up
; 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.
; Initial RAM & ROM pointers for CamelForth.
.equ coderam,0x0e000 ; where new code goes
.equ dataram,0x0f000 ; where data goes
.equ UPHI,0xFE ; Uarea at FE00 hex
; RESET AND INTERRUPT VECTORS ===================
ljmp reset
; ljmp ie0
; ie0: reti
; .skip 4
; ljmp clock ; ljmp tf0
; tf0: reti
; .skip 4
; ljmp ie1
; ie1: reti
; .skip 4
; ljmp tf1
; tf1: reti
; .skip 4
; ljmp riti
; riti: reti
; .skip 4
; ljmp tf2
; tf2: reti
;
reset: mov ie,#0 ; disable all irpts
mov pcon,#0 ; T1 baudrate not doubled
mov tmod,#0x20 ; T1 mode 2, T0 mode 0
mov th1,#0xfd ; 9600 baud @ 11.0592 MHz
setb tcon.6 ; enable timer 1
mov scon,#0x52 ; UART mode 1 (8-bit)
mov UP,#UPHI
mov p2,UP ; user area at FE00,
mov r0,#0xff ; param stack at FEFF
mov sp,#0x8 ; ret stack at bottom
ljmp COLD ; enter Forth interpreter
; SERIAL I/O ====================================
;C EMIT c -- output character to console
.drw link
.set link,*+1
.db 0,4,"EMIT"
EMIT: ; jnb scon.1,EMIT ; await Tx interrupt flag
; clr scon.1 ; clear flag
mov sbuf,dpl ; output TOS char to UART
ajmp poptos ; pop new TOS
;C KEY -- c get character from keyboard
.drw link
.set link,*+1
.db 0,3,"KEY"
KEY: ; jnb scon.0,KEY ; await Rx interrupt flag
; clr scon.0
; dec r0 ; push old TOS
; mov a,dph
; movx @r0,a
; dec r0
; mov a,dpl
; movx @r0,a
; mov dpl,sbuf ; get new char in TOS
; mov dph,#0
acall DUP
mov dpl,sbuf ; get new char in TOS
mov dph,#0
ret
;X KEY? -- f return true if char waiting
.drw link
.set link,*+1
.db 0,4,"KEY?"
QUERYKEY: dec r0 ; push old TOS
mov a,dph
movx @r0,a
dec r0
mov a,dpl
movx @r0,a
mov a,scon ; get rx flag in carry
rrc a
ajmp cyprop ; propagate that thru TOS
; INTERPRETER LOGIC =============================
; NEXT and ENTER are not needed for Subroutine
; Threading. EXIT may be used in high level code.
;C EXIT -- exit a colon definition
.drw link
.set link,*+1
.db 0,4,"EXIT"
EXIT: dec sp ; discard ret adrs in caller
dec sp
ret ; return to caller's caller
;Z LIT -- x fetch inline literal to stack
.drw link
.set link,*+1
.db 0,3,"LIT"
LIT: dec r0 ; push old TOS
mov a,dph
movx @r0,a
dec r0
mov a,dpl
movx @r0,a
pop dph ; get return address
pop dpl
clr a
movc a,@a+dptr ; get literal low byte
inc dptr
mov r2,a
clr a
movc a,@a+dptr ; get literal high byte
inc dptr
push dpl ; restore updated ret adr
push dph
mov dph,a ; put literal in TOS
mov dpl,r2
ret
;C EXECUTE i*x xt -- j*x execute Forth word
;C at 'xt'
.drw link
.set link,*+1
.db 0,7,"EXECUTE"
EXECUTE: push dpl ; push addr onto r.stack,
push dph ; then pop new TOS->DPTR
; 'ret' in poptos will then execute
; desired word; its 'ret' will return to
; EXECUTE's caller.
ajmp poptos
; DEFINING WORDS ================================
;C VARIABLE -- define a Forth VARIABLE
; CREATE CELL ALLOT ;
; Action of ROMable variable is that of CONSTANT;
; the constant holds the RAM address.
.drw link
.set link,*+1
.db 0,8,"VARIABLE"
VARIABLE: lcall CREATE
acall CELL
ljmp ALLOT
;C CONSTANT -- define a Forth constant
; CREATE CELL NEGATE IALLOT I, Harvard model
; DOES> (machine code fragment)
; Note that the constant is stored in Code space.
.drw link
.set link,*+1
.db 0,8,"CONSTANT"
CONSTANT: lcall CREATE
lcall CELL
lcall NEGATE
lcall IALLOT
lcall ICOMMA
lcall XDOES
; DOCON, code action of CONSTANT,
; entered by CALL DOCON
docon: ; -- x exec action of constant
dovar: ; -- a-addr exec action of ROMable var
docreate: ; -- a-addr exec action of Harv.CREATE
dec r0 ; push old TOS
mov a,dph
movx @r0,a
dec r0
mov a,dpl
movx @r0,a
pop dph ; get addr of param field
pop dpl ; (in Code memory!)
ajmp IFETCH ; go fetch its contents
;Z USER n -- define user variable 'n'
; CONSTANT DOES> (machine code fragment)
; Note that this version allows a full 16-bit
; offset from the user pointer.
.drw link
.set link,*+1
.db 0,4,"USER"
USER: acall CONSTANT
lcall XDOES
; DOUSER, code action of USER,
; entered by CALL DOUSER
douser: acall pushtos ; push old TOS
pop dph ; get addr of param field
pop dpl ; (in Code memory!)
acall IFETCH ; go fetch its contents
add a,UP ; add UP:00 to offset
mov dph,a ; NB. IFETCH leaves A=DPH
ret
; DOCREATE's action is for a table in RAM.
; DOROM is the code action for a table in ROM;
; it returns the address of the parameter field.
; Entered by CALL DOROM
dorom: acall pushtos ; push old TOS
pop dph ; param field adrs -> TOS
pop dpl
ret
; DODOES, code action of DOES> clause
; (internal code fragment, not a Forth word)
; entered by LCALL fragment
; address of data
; ...
; fragment: LCALL DODOES
; high-level thread
; Enters high-level thread with address of
; data on top of stack. HARVARD MODEL: the data
; (in Data space) does NOT follow LCALL fragment
; (in Code space); instead, the address of the
; data is appended after LCALL fragment.
dodoes: ; -- a-addr support routine for DOES>
dec r0 ; push old TOS
mov a,dph
movx @r0,a
dec r0
mov a,dpl
movx @r0,a
pop dr5 ; addr of DOES> clause
pop dr4 ; Forth code
pop dph ; addr of defined word's
pop dpl ; Param. field
push dr4 ; restore Forth code addr
push dr5
ajmp IFETCH ; fetch adrs from P.field
; & go do the DOES> code
; STACK OPERATIONS ==============================
;C DUP x -- x x duplicate top of stack
.drw link
.set link,*+1
.db 0,3,"DUP"
DUP:
pushtos: dec r0 ; push hi byte of TOS
mov a,dph
movx @r0,a
dec r0 ; push lo byte of TOS
mov a,dpl
movx @r0,a
ret
;C ?DUP x -- 0 | x x DUP if nonzero
.drw link
.set link,*+1
.db 0,4,"?DUP"
QDUP: mov a,dph
orl a,dpl
jnz pushtos
ret
;C DROP x -- drop top of stack
.drw link
.set link,*+1
.db 0,4,"DROP"
DROP:
poptos: movx a,@r0 ; pop lo byte -> TOS
mov dpl,a
inc r0
movx a,@r0 ; pop hi byte -> TOS
mov dph,a
inc r0
ret
;C SWAP x1 x2 -- x2 x1 swap top two items
.drw link
.set link,*+1
.db 0,4,"SWAP"
;; SWOP: movx a,@r0 ; pop lo byte -> X
;; mov r2,a
;; inc r0
;; movx a,@r0 ; pop hi byte -> X
;; mov r3,a
;; ; inc r0
;; ; dec r0 ; push hi byte of TOS
SWOP:
movx a,@r0
xch a,dpl
movx @r0,a
inc r0
movx a,@r0
xch a,dph
movx @r0,a
dec r0
ret
halfover: mov a,dph
movx @r0,a
dec r0 ; push lo byte of TOS
mov a,dpl
movx @r0,a
mov dph,r3 ; old 2nd item -> TOS
mov dpl,r2
ret
;C OVER x1 x2 -- x1 x2 x1 per stack diagram
.drw link
.set link,*+1
.db 0,4,"OVER"
OVER:
; movx a,@r0 ; pop lo byte -> X
; mov r2,a
; inc r0
; movx a,@r0 ; pop hi byte -> X
; mov r3,a
; dec r0 ; restore stack pointer
; dec r0 ; predecrement for 'halfover'
; sjmp halfover ; push TOS, then copy X to TOS
movx a,@r0 ; r2:r3 is x1
mov r3,a
inc r0
movx a,@r0
mov r2,a
dec r0
dec r0 ; push hi byte of TOS
mov a,dph
movx @r0,a
dec r0 ; push lo byte of TOS
mov a,dpl
movx @r0,a
mov dph,r2
mov dpl,r3
ret
;C ROT x1 x2 x3 -- x2 x3 x1 per stack diagram
.drw link
.set link,*+1
.db 0,3,"ROT"
ROT: ; x3 is in TOS
movx a,@r0 ; pop x2 -> r5:r4
mov r4,a
inc r0
movx a,@r0
mov r5,a
inc r0
movx a,@r0 ; pop x1 -> r3:r2
mov r2,a
inc r0
movx a,@r0
mov r3,a
; inc r0
; dec r0 ; push x2
mov a,r5
movx @r0,a
dec r0
mov a,r4
movx @r0,a
dec r0 ; predecr. for 'halfover'
sjmp halfover ; push x3 (TOS), then
; copy x1 to TOS
;C >R x -- R: -- x push to return stack
.drw link
.set link,*+1
.db 0,2,">R"
TOR: pop dr3 ; save ret addr in r3:r2
pop dr2
push dpl ; push lo byte*
push dph ; push hi byte*
push dr2 ; restore ret addr
push dr3
sjmp poptos ; pop new TOS
;* NB. stored lo:hi in regs because SP increments
;C R> -- x R: x -- pop from return stack
.drw link
.set link,*+1
.db 0,2,"R>"
RFROM: dec r0 ; push old TOS
mov a,dph
movx @r0,a
dec r0
mov a,dpl
movx @r0,a
pop dr3 ; save ret addr in r3:r2
pop dr2
pop dph ; pop hi byte
pop dpl ; pop lo byte
push dr2 ; restore return address
push dr3
ret
;C R@ -- x R: x -- x fetch from rtn stack
.drw link
.set link,*+1
.db 0,2,"R@"
RFETCH: dec r0 ; push old TOS
mov a,dph
movx @r0,a
dec r0
mov a,dpl
movx @r0,a
mov r1,sp ; get copy of SP
dec r1 ; skip return address
dec r1
mov dph,@r1 ; fetch 2nd return stack item
dec r1
mov dpl,@r1
ret
;Z SP@ -- a-addr get data stack pointer
.drw link
.set link,*+1
.db 0,3,"SP@"
SPFETCH: dec r0 ; push old TOS
mov a,dph
movx @r0,a
dec r0
mov a,dpl
movx @r0,a
mov dph,UP ; 16-bit pointer P2:R0
mov dpl,r0
ret
;Z SP! a-addr -- set data stack pointer
; Note: only the low 8 bits are affected!
.drw link
.set link,*+1
.db 0,3,"SP!"
SPSTORE: mov r0,dpl ; set stack pointer
ajmp poptos ; get new TOS
;Z RP@ -- a-addr get return stack pointer
.drw link
.set link,*+1
.db 0,3,"RP@"
RPFETCH: dec r0 ; push old TOS
mov a,dph
movx @r0,a
dec r0
mov a,dpl
movx @r0,a
mov dph,#0 ; 16-bit pointer 00:SP
mov dpl,sp
ret
;Z RP! a-addr -- set return stack pointer
; Note: only the low 8 bits are significant!
.drw link
.set link,*+1
.db 0,3,"RP!"
RPSTORE: pop dr3 ; save ret addr in r3:r2
pop dr2
mov sp,dpl ; set new stack pointer
push dr2 ; restore ret addr
push dr3
ajmp poptos ; get new TOS
;X NIP x1 x2 -- x2 per stack diagram
.drw link
.set link,*+1
.db 0,3,"NIP"
NIP: inc r0
inc r0
ret
;X TUCK x1 x2 -- x2 x1 x2 per stack diagram
.drw link
.set link,*+1
.db 0,4,"TUCK"
TUCK: acall SWOP
ajmp OVER
; MEMORY OPERATIONS =============================
;C ! x a-addr -- store cell in Data mem
; Byte order is lo,hi.
.drw link
.set link,*+1
.db 0,1,"!"
STORE: movx a,@r0 ; low byte of X
inc r0
movx @dptr,a
inc dptr
movx a,@r0 ; high byte of X
inc r0
movx @dptr,a
ajmp poptos ; pop new TOS
;C C! c c-addr -- store char in Data mem
.drw link
.set link,*+1
.db 0,2,"C!"
CSTORE: movx a,@r0 ; low byte is char
inc r0
movx @dptr,a
inc r0 ; skip high byte
ajmp poptos ; pop new TOS
;C @ a-addr -- x fetch cell from Data mem
; Byte order is lo,hi.
.drw link
.set link,*+1
.db 0,1,"@"
FETCH: movx a,@dptr ; low byte
mov r2,a ; ..temporary stash
inc dptr
movx a,@dptr ; high byte
mov dpl,r2 ; copy to TOS (DPTR)
mov dph,a
ret
;C C@ c-addr -- c fetch char from Data mem
.drw link
.set link,*+1
.db 0,2,"C@"
CFETCH: movx a,@dptr
mov dpl,a
mov dph,#0
ret
;C I! x a-addr -- store cell in Code mem
; On 8051, the only way to store to Code memory
; is to have it also appear in Data space.
; So, I! is identical to !, and IC! to C!.
.drw link
.set link,*+1
.db 0,2,"I!"
ISTORE: sjmp STORE
;C IC! c c-addr -- store char in Code mem
.drw link
.set link,*+1
.db 0,3,"IC!"
ICSTORE: sjmp CSTORE
;Z I@ a-addr -- x fetch cell from Code mem
; Byte order is lo,hi.
.drw link
.set link,*+1
.db 0,2,"I@"
IFETCH: clr a
movc a,@a+dptr ; low byte
mov r2,a ; ..temporary stash
mov a,#1
movc a,@a+dptr ; high byte
mov dpl,r2 ; copy to TOS (DPTR)
mov dph,a
ret
; Note! USER expects IFETCH to leave A=DPH!
;Z IC@ a-addr -- x fetch char from Code mem
.drw link
.set link,*+1
.db 0,2,"IC@"
ICFETCH: clr a
movc a,@a+dptr ; low byte
mov dpl,a
mov dph,#0
ret
; ARITHMETIC AND LOGICAL OPERATIONS =============
;C + n1/u1 n2/u2 -- n3/u3 add n1+n2
.drw link
.set link,*+1
.db 0,1,"+"
PLUS: movx a,@r0 ; low byte
inc r0
add a,dpl
mov dpl,a
movx a,@r0 ; high byte
inc r0
addc a,dph
mov dph,a
ret
;Z M+ d n -- d add single to double
.drw link
.set link,*+1
.db 0,2,"M+"
MPLUS: movx a,@r0 ; pop d.high -> r3:r2
mov r2,a
inc r0
movx a,@r0
mov r3,a
inc r0
movx a,@r0 ; d.low, low byte
add a,dpl
movx @r0,a
inc r0
movx a,@r0 ; d.low, high byte
addc a,dph
movx @r0,a
dec r0
clr a
addc a,r2 ; d.high, low byte
mov dpl,a
clr a
addc a,r3 ; d.high, high byte
mov dph,a
ret
;C - n1/u1 n2/u2 -- n3/u3 subtract n1-n2
.drw link
.set link,*+1
.db 0,1,"-"
MINUS: movx a,@r0 ; low byte
inc r0
clr c
subb a,dpl
mov dpl,a
movx a,@r0 ; high byte
inc r0
subb a,dph
mov dph,a
ret
;C AND x1 x2 -- x3 logical AND
.drw link
.set link,*+1
.db 0,3,"AND"
AND: movx a,@r0 ; low byte
inc r0
anl a,dpl
mov dpl,a
movx a,@r0 ; high byte
inc r0
anl a,dph
mov dph,a
ret
;C OR x1 x2 -- x3 logical OR
.drw link
.set link,*+1
.db 0,2,"OR"
OR: movx a,@r0 ; low byte
inc r0
orl a,dpl
mov dpl,a
movx a,@r0 ; high byte
inc r0
orl a,dph
mov dph,a
ret
;C XOR x1 x2 -- x3 logical XOR
.drw link
.set link,*+1
.db 0,3,"XOR"
XOR: movx a,@r0 ; low byte
inc r0
xrl a,dpl
mov dpl,a
movx a,@r0 ; high byte
inc r0
xrl a,dph
mov dph,a
ret
;C INVERT x1 -- x2 bitwise inversion
.drw link
.set link,*+1
.db 0,6,"INVERT"
INVERT: xrl dpl,#0xff
xrl dph,#0xff
ret
;C NEGATE x1 -- x2 two's complement
.drw link
.set link,*+1
.db 0,6,"NEGATE"
NEGATE: xrl dpl,#0xff
xrl dph,#0xff
inc dptr
ret
;C 1+ n1/u1 -- n2/u2 add 1 to TOS
.drw link
.set link,*+1
.db 0,2,"1+"
ONEPLUS: inc dptr
ret
;C 1- n1/u1 -- n2/u2 subtract 1 from TOS
.drw link
.set link,*+1
.db 0,2,"1-"
ONEMINUS: mov a,dpl
jnz dphok
dec dph ; if dpl=0, decr. affects dph
dphok: dec dpl
ret
;Z >< x1 -- x2 swap bytes (not ANSI)
.drw link
.set link,*+1
.db 0,2,"><"
swapbytes: mov a,dpl
mov dpl,dph
mov dph,a
ret
;C 2* x1 -- x2 arithmetic left shift
.drw link
.set link,*+1
.db 0,2,"2*"
TWOSTAR: mov a,dpl ; lo byte, left shift
add a,dpl
mov dpl,a
mov a,dph ; hi byte, left rot w/cy
rlc a
mov dph,a
ret
;C 2/ x1 -- x2 arithmetic right shift
.drw link
.set link,*+1
.db 0,2,"2/"
TWOSLASH: mov a,dph ; get msb of TOS into cy
rlc a
mov a,dph ; high byte, right rotate
rrc a
mov dph,a
mov a,dpl ; low byte, right rotate
rrc a
mov dpl,a
ret
;C LSHIFT x1 u -- x2 logical left shift
.drw link
.set link,*+1
.db 0,6,"LSHIFT"
LSHIFT: mov r4,dpl ; r4 = loop counter
movx a,@r0 ; pop x1 -> DPTR
mov dpl,a
inc r0
movx a,@r0
mov dph,a
inc r0
inc r4 ; test for r4=0 case
sjmp lshtest
lshloop: mov a,dpl ; shift left
add a,dpl
mov dpl,a
mov a,dph
rlc a
mov dph,a
lshtest: djnz r4,lshloop
ret
;C RSHIFT x1 u -- x2 logical right shift
.drw link
.set link,*+1
.db 0,6,"RSHIFT"
RSHIFT: mov r4,dpl ; r4 = loop counter
movx a,@r0 ; pop x1 -> DPTR
mov dpl,a
inc r0
movx a,@r0
mov dph,a
inc r0
inc r4 ; test for r4=0 case
sjmp rshtest
rshloop: clr c ; clear carry
mov a,dph ; shift right
rrc a
mov dph,a
mov a,dpl
rrc a
mov dpl,a
rshtest: djnz r4,rshloop
ret
;C +! n/u a-addr -- add cell to Data mem
.drw link
.set link,*+1
.db 0,2,"+!"
PLUSSTORE: movx a,@r0 ; low byte of n
inc r0
mov r2,a
movx a,@dptr ; low byte of memory
add a,r2
movx @dptr,a
inc dptr
movx a,@r0 ; high byte of n
inc r0
mov r2,a
movx a,@dptr ; high byte of memory
addc a,r2
movx @dptr,a
ajmp poptos ; pop new TOS
; COMPARISON OPERATIONS =========================
;X <> x1 x2 -- flag test not equal
.drw link
.set link,*+1
.db 0,2,"<>"
NOTEQUAL: acall EQUAL
sjmp ZEROEQUAL
;C 0= n/u -- flag return true if TOS=0
.drw link
.set link,*+1
.db 0,2,"0="
ZEROEQUAL: mov a,dph
zequ1: orl a,dpl ; A = z or nz, per DPTR
clr c
subb a,#1 ; cy set if A was 0
cyprop: subb a,acc ; -1 if A was 0, else 0
mov dph,a
mov dpl,a
ret ; NB! A=0 iff TOS=0
;C 0< n -- flag true if TOS negative
.drw link
.set link,*+1
.db 0,2,"0<"
ZEROLESS: mov a,dph
rlc a ; cy set if A negative
sjmp cyprop ; propagate cy thru TOS
;C = x1 x2 -- flag test x1=x2
.drw link
.set link,*+1
.db 0,1,"="
EQUAL: acall MINUS ; x1-x2 in TOS, A=DPH
sjmp zequ1
;C < n1 n2 -- flag test n1<n2, signed
.drw link
.set link,*+1
.db 0,1,"<"
LESS: acall MINUS ; n1-n2 in TOS, A=DPH,
; CY and OV valid
; if result negative (MSB=1) & not OV, n1<n2
; neg. & OV => n1 +ve, n2 -ve, result -ve, n1>n2
; if result positive (MSB=0) & not OV, n1>=n2
; pos. & OV => n1 -ve, n2 +ve, result +ve, n1<n2
; thus OV reverses the sense of the sign bit
jnb psw.2,msbok ; jump if overflow clear
cpl a ; OV set: invert msb
msbok: rlc a ; put msb (sign) in cy
sjmp cyprop ; & propagate thru TOS
;C > n1 n2 -- flag test n1>n2, signed
.drw link
.set link,*+1
.db 0,1,">"
GREATER: acall SWOP
sjmp LESS
;C U< u1 u2 -- flag test n1<n2, unsigned
.drw link
.set link,*+1
.db 0,2,"U<"
ULESS: acall MINUS ; TOS=u1-u2, cy set if u1<u2
sjmp cyprop ; propagate cy thru TOS
;X U> u1 u2 -- flag test u1>u2, unsigned
.drw link
.set link,*+1
.db 0,2,"U>"
UGREATER: acall SWOP
sjmp ULESS
; LOOP AND BRANCH OPERATIONS ====================
; branch and ?branch are done with sjmp and jz,
; respectively, using the following routines
; which leave a value in A. Typical use:
; lcall zerosense, jz destadr
; lcall loopsense, jz destadr, lcall unloop
; LEAVE may exit loop by branching ^--here
.drw link
.set link,*+1
.db 0,7,"?BRANCH"
qbranch:
zerosense: ; n -- leave zero in A if TOS=0
movx a,@r0 ; new TOS in a:r2
mov r2,a
inc r0
movx a,@r0
inc r0
xch a,dph ; DPH=new TOS hi, A=old DPH
orl a,dpl ; A=0 if old TOS was zero
mov dpl,r2 ; new TOS lo in DPL
ret
; LOOP and +LOOP are done with jz, using the
; following routines which leave a value in A.
; If the loop terminates, (index crosses 8000h),
; a nonzero value is left in A. A=0 to loop.
; Typical use:
; lcall loopsense, jz destadr, lcall unloop
; LEAVE may exit loop by branching ^-here
; The topmost loop index is in regs r7:r6.
.drw link