-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheForth431-msp430g2553-naken.asm
2229 lines (1985 loc) · 41.8 KB
/
eForth431-msp430g2553-naken.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; This is a 430eForth assembler listing based on the original script from
; Dr. Chen Hanson Ting as described in his book: Zen and the Forth Language:
; EFORTH for the MSP430 from Texas Instruments (Kindle Edition). It was first
; written by him in IAR Assembler, then transferred to the CCS and now adapted
; by Michael Kalus for the naken_asm.
;
; MIT License
; ----------------------------------------------------------------------------
; Copyright (c) 2014 Dr. Chen-Hanson Ting CCS Version 430eForth4.3
; (c) 2018 Michael Kalus Naken Version 430eForth4.3n
; (c) 2018 Manfred Mahlow Flash Tools 430eForth4.3n1
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
;furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in all
; copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; 7/7/2012 430eForth1.0, from eForth86.asm and 430uForth
; 7/4/2012 Move 430uForth2.1 from IAR to CCS 5.2
; 8/5/2014 Move 430eForth2.2 to CCS 6.0. Fix linkage of OVER.
; Software UART at 2400 baud.
; 8/10/2014 430eForth2.3 9600 baud, thanks to Dirk Bruehl and
; Michael Kalus of www.4e4th.org
; 8/10/2014 430eForth2.4 Restore ERASE and WRITE
; 8/20/2014 430eForth2.5 Test Segment D
; 8/25/2014 430eForth2.6 Turnkey
; 8/26/2014 430eForth2.7 Optimize
; 9/16/2014 430eForth3.1 Tail recursion, APP!
; 10/11/2014 430eForth4.1 Direct thread, more optimization
; 10/23/2014 430eForth4.2 Direct thread, pack lists
; 11/12/2014 430eForth4.2 Direct thread, final
;
; Build for and verified on MSP430G2 LaunchPad from TI
; Assembled with Code Composer Sudio 6.0 IDE
; Internal DCO at 8 MHz
; Hardware UART at 9600 baud. TXD and RXD must be crossed.
;CCS: ;ting
; .nolist
; .title "msp430 eForth 4.3"
; .cdecls C,LIST,"msp430g2553.h" ; Include device header file
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; 05/13/2018 Moved 430eForth4.3 from CSS to Michael Kohn's naken_asm (ver.
; 23 april 2018) - ok ;mk
; 20180624 $," - bug fixed. Thanks to Manfred Mahlow. ;mk
; 20180628 DIGIT? : bug fix, 0= 0> were handled as numbers ;MM
; 20180629 FSCAN added, sets CP to the lowest free flash addr at BOOT time.
; FSCAN is executed before COLD executes QUIT.
; Version string changed in HI from 43n to 43n1. ;MM
; 20180630 ERASE and WRITE renamed to IERASE IWRITE due to name conflict ;MM
; 20180701 LITERAL and ALIGNED revealed. ;MM
; 20180707 Flash Test QFLASH ( a -- a ) added. Aborts with message ?flash
; if a > EDM. (EDM = End of Dictionary Memory space in the flash) ;MM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;naken: ;mk
.msp430
.include "msp430g2553.inc" ; MCU-specific register equates for naken_asm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Direct Thread Model of eForth
; CCS: .equ ; naken: equ ;mk
;; CPU registers
tos equ R4
stack equ R5
ip equ R6
temp0 equ R7
temp1 equ R8
temp2 equ R9
temp3 equ R10
;; Macros
; CCS: <name> .macro ; naken: .macro <name> ;mk
.macro pops ;DROP
mov.w @stack+,tos
.endm
.macro pushs ;DUP
decd.w stack
mov.w tos,0(stack)
.endm;; Constants
.macro INEXT ;mk renamed (dollar)NEXT to INEXT - inline code for NEXT.
mov @ip+,pc ; fetch code address into PC
.endm
.macro INEST ;mk renamed (dollar)NEST to INEST - inline code for NEST.
.align 16 ; CCS: 2 bytes align ; naken: 16 bit align. mk
call #DOLST ; fetch code address into PC, W=PFA
.endm
.macro ICONST ;mk renamed (dollar)CONST to ICONST - inline code calling DOCON.
.align 16 ; CCS: 2 bytes align ; naken: 16 bit align. mk
call #DOCON ; fetch code address into PC, W=PFA
.endm
;; Assembler constants
COMPO equ 040H ;lexicon compile only bit
IMEDD equ 080H ;lexicon immediate bit
MASKK equ 07F1FH ;lexicon bit mask
CELLL equ 2 ;size of a cell
BASEE equ 10 ;default radix
VOCSS equ 8 ;depth of vocabulary stack
BKSPP equ 8 ;backspace
LF equ 10 ;line feed
CRR equ 13 ;carriage return
ERR equ 27 ;error escape
TIC equ 39 ;tick
CALLL equ 012B0H ;NOP CALL opcodes
UPP equ 200H
DPP equ 220H
SPP equ 378H ;data stack
TIBB equ 380H ;terminal input buffer
RPP equ 3F8H ;return stacl
CODEE equ 0C000H ;code dictionary
COLDD equ 0FFFEH ;cold start vector
EM equ 0FFFFH ;top of memory
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.list
;;;;
;; Main entry points and COLD start data
; .text ; CCS: Predefined Memory Segment Name : Main memory (flash or ROM)
.org 0C000H ; naken : MSP430G2553 main memory : C000-FFFF = 16KB flash ROM
; FFE0-FFFF = interrupt vectors
; .global main ; CCS: File Reference Directive
; unused in naken
main:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Device dependent I/O
; ?KEY ( -- F | c T )
; Return input character.
.dw 0
.db 4,"?KEY",0
QKEY:
pushs
QKEY1:
BIT.B #UCA0RXIFG,&IFG2
JZ FALSE ;return false flag
MOV.B &UCA0RXBUF,tos ; read character into TOS
pushs
jmp TRUE
; KEY ( -- c )
; Return input character.
.dw QKEY-6
.db 3,"KEY"
KEY: ;mk naken needs colon after label.
pushs
KEY1:
BIT.B #UCA0RXIFG,&IFG2
JZ KEY1
MOV.B &UCA0RXBUF,tos ; read character into TOS
INEXT
; EMIT ( c -- )
; Send character c to the output device.
.dw KEY-4
.db 4,"EMIT",0
EMIT:
EMIT1:
BIT.B #UCA0TXIFG,&IFG2
JZ EMIT1
MOV.B tos,&UCA0TXBUF
pops
INEXT
; !IO ( -- )
; Initialize the serial I/O devices.
; .dw EMIT-6
; .db 3,"!IO"
STOIO:
; 8MHz
mov.b &CALBC1_8MHZ, &BCSCTL1 ; Set DCO
mov.b &CALDCO_8MHZ, &DCOCTL ; to 8 MHz.
mov.b #006h, &P1SEL ; Use P1.1/P1.2 for USCI_A0
mov.b #006h, &P1SEL2 ; Use P1.1/P1.2 for USCI_A0
; Configure UART (Koch)
bis.b #UCSSEL_2,&UCA0CTL1 ;db2 SMCLK
mov.b #65,&UCA0BR0 ;db3 8MHz 9600 Insgesamt &833 = $341
mov.b #3,&UCA0BR1 ;db4 8MHz 9600
mov.b #UCBRS_2,&UCA0MCTL ;db5 Modulation UCBRSx = 2
bic.b #UCSWRST,&UCA0CTL1 ;db6 **Initialize USCI
INEXT ;called from COLD
;; The kernel
; doLIT ( -- w )
; Push an inline literal.
; .dw STOIO-4
; .db COMPO+5,"doLIT"
DOLIT:
pushs
mov @ip+,tos
INEXT
; doCON ( -- a )
; Run time routine for CONSTANT, VARIABLE and CREATE.
; .dw DOLIT-6
; .db COMPO+5,"doCON"
DOCON:
pushs
pop tos
mov @tos,tos
INEXT
; doLIST ( -- )
; Process colon list..
; .dw EMIT-6
; .db 6,"doLIST",0
DOLST:
mov ip,temp0 ;exchange pointers
pop ip ;push return stack
push temp0 ;restore the pointers
INEXT
; EXIT ( -- )
; Terminate a colon definition.
.dw EMIT-6
.db 4,"EXIT",0
EXIT:
mov @sp+,ip
INEXT
; EXECUTE ( ca -- )
; Execute the word at ca.
.dw EXIT-6
.db 7,"EXECUTE"
EXECU:
mov tos,temp0
pops
br temp0
; @EXECUTE ( a -- )
; Execute vector stored in address a.
.dw EXECU-8
.db 8,"@EXECUTE",0
ATEXE:
mov @tos,temp0
pops
br temp0
; branch ( -- )
; Branch to an inline address.
; .dw ATEXE-10
; .db COMPO+6,"branch",0
BRAN:
mov @ip+,ip
INEXT
; ?branch ( f -- )
; Branch if flag is zero.
; .dw BRAN-8
; .db COMPO+7,"?branch"
QBRAN:
tst tos
pops
jz BRAN
jmp SKIP
; next ( -- )
; Run time code for the single index loop.
; : next ( -- ) \ hilevel model
; r> r> dup if 1 - >r @ >r exit then drop cell+ >r ;
; .dw QBRAB-8
; .db COMPO+4,"next",0
DONXT:
dec 0(sp) ;decrement index
jge BRAN ;loop back
incd.w sp ;discard index
SKIP:
incd.w ip ;exit loop
INEXT
; ! ( w a -- )
; Pop the data stack to memory.
.dw ATEXE-10
.db 1,"!"
STORE:
mov.w @stack+,0(tos)
pops
INEXT
; @ ( a -- w )
; Push memory location to the data stack.
.dw STORE-2
.db 1,"@"
AT:
mov.w @tos,tos
INEXT
; C! ( c b -- )
; Pop the data stack to byte memory.
.dw AT-2
.db 2,"C!",0
CSTOR:
mov.b @stack+,0(tos)
inc stack
pops
INEXT
; C@ ( b -- c )
; Push byte memory location to the data stack.
.dw CSTOR-4
.db 2,"C@",0
CAT:
mov.b @tos,tos
INEXT
; RP! ( -- )
; init return stack pointer.
; .dw CAT-4
; .db 3,"RP!"
RPSTO:
mov #RPP,SP ;init return stack
INEXT
; R> ( -- w )
; Pop the return stack to the data stack.
.dw CAT-4
.db 2,"R",3EH,0
RFROM:
pushs
pop tos
INEXT
; R@ ( -- w )
; Copy top of return stack to the data stack.
.dw RFROM-4
.db 2,"R@",0
RAT:
pushs
mov 0(sp),tos
INEXT
; >R ( w -- )
; Push the data stack to the return stack.
.dw RAT-4
.db COMPO+2,">R",0
TOR:
push tos
pops
INEXT
; SP! ( -- )
; Init data stack pointer.
; .dw SPAT-4
; .db 3,"SP!"
SPSTO:
mov #SPP,stack ;init parameter stack
clr tos
INEXT
; DROP ( w -- )
; Discard top stack item.
.dw TOR-4
.db 4,"DROP",0
DROP:
pops
INEXT
; DUP ( w -- w w )
; Duplicate the top stack item.
.dw DROP-6
.db 3,"DUP"
DUPP:
pushs
INEXT
; SWAP ( w1 w2 -- w2 w1 )
; Exchange top two stack items.
.dw DUPP-4
.db 4,"SWAP",0
SWAP:
mov.w tos,temp0
mov.w @stack,tos
mov.w temp0,0(stack)
INEXT
; OVER ( w1 w2 -- w1 w2 w1 )
; Copy second stack item to top.
.dw SWAP-6
.db 4,"OVER",0
OVER:
mov.w @stack,temp0
pushs
mov.w temp0,tos
INEXT
; 0< ( n -- t )
; Return true if n is negative.
.dw OVER-6
.db 2,"0",3CH,0
ZLESS:
tst tos
jn TRUE
FALSE:
clr tos
INEXT
TRUE:
mov #0x-1,tos
INEXT
; AND ( w w -- w )
; Bitwise AND.
.dw ZLESS-4
.db 3,"AND"
ANDD:
and @stack+,tos
INEXT
; OR ( w w -- w )
; Bitwise inclusive OR.
.dw ANDD-4
.db 2,"OR",0
ORR:
bis @stack+,tos
INEXT
; XOR ( w w -- w )
; Bitwise exclusive OR.
.dw ORR-4
.db 3,"XOR"
XORR:
xor @stack+,tos
INEXT
; UM+ ( w w -- w cy )
; Add two numbers, return the sum and carry flag.
.dw XORR-4
.db 3,"UM+"
UPLUS:
add @stack,tos
mov tos,0(stack)
clr tos
rlc tos
INEXT
;; Common functions
; ?DUP ( w -- w w | 0 )
; Dup tos if its is not zero.
.dw UPLUS-4
.db 4,"?DUP",0
QDUP:
tst tos
jnz DUPP
INEXT
; ROT ( w1 w2 w3 -- w2 w3 w1 )
; Rot 3rd item to top.
.dw QDUP-6
.db 3,"ROT"
ROT:
mov.w 0(stack),temp0
mov.w tos,0(stack)
mov.w 2(stack),tos
mov.w temp0,2(stack)
INEXT
; 2DROP ( w w -- )
; Discard two items on stack.
.dw ROT-4
.db 5,"2DROP"
DDROP:
incd.w stack
pops
INEXT
; 2DUP ( w1 w2 -- w1 w2 w1 w2 )
; Duplicate top two items.
.dw DDROP-6
.db 4,"2DUP",0
DDUP:
mov.w @stack,temp0
pushs
decd.w stack
mov.w temp0,0(stack)
INEXT
; + ( w w -- sum )
; Add top two items.
.dw DDUP-6
.db 1,"+"
PLUS:
add @stack+,tos
INEXT
; D+ ( d d -- d )
; Double addition, as an example using UM+.
;
.dw PLUS-2
.db 2,"D+",0
DPLUS:
mov.w @stack+,temp0
mov.w @stack+,temp1
add.w temp0,0(stack)
addc temp1,tos
INEXT
; NOT ( w -- w )
; One's complement of tos.
.dw DPLUS-4
.db 3,"NOT"
INVER:
inv tos
INEXT
; NEGATE ( n -- -n )
; Two's complement of tos.
.dw INVER-4
.db 6,"NEGATE",0
NEGAT:
inv tos
inc tos
INEXT
; DNEGATE ( d -- -d )
; Two's complement of top double.
.dw NEGAT-8
.db 7,"DNEGATE"
DNEGA:
inv tos
inv 0(stack)
inc 0(stack)
addc #0,tos
INEXT
; - ( n1 n2 -- n1-n2 )
; Subtraction.
.dw DNEGA-8
.db 1,"-"
SUBB:
sub @stack+,tos
jmp NEGAT
; ABS ( n -- n )
; Return the absolute value of n.
.dw SUBB-2
.db 3,"ABS"
ABSS:
tst.w tos
jn NEGAT
INEXT
; = ( w w -- t )
; Return true if top two are equal.
.dw ABSS-4
.db 1,3DH
EQUAL:
xor @stack+,tos
jnz FALSE
jmp TRUE
; U< ( u u -- t )
; Unsigned compare of top two items.
.dw EQUAL-2
.db 2,"U",3CH,0
ULESS:
mov @stack+,temp0
cmp tos,temp0
subc tos,tos
INEXT
; < ( n1 n2 -- t )
; Signed compare of top two items.
.dw ULESS-4
.db 1,3CH
LESS:
cmp @stack+,tos
jz FALSE
jge TRUE
jmp FALSE
; > ( n1 n2 -- t )
; Signed compare of top two items.
.dw LESS-2
.db 1,3EH
GREAT:
cmp @stack+,tos
jge FALSE
jmp TRUE
; MAX ( n n -- n )
; Return the greater of two top stack items.
.dw GREAT-2
.db 3,"MAX"
MAX:
cmp 0(stack),tos
MAX1:
jl DROP
incd.w stack
INEXT
; MIN ( n n -- n )
; Return the smaller of top two stack items.
.dw MAX-4
.db 3,"MIN"
MIN:
cmp tos,0(stack)
jmp MAX1
;; Divide
; UM/MOD ( udl udh u -- ur uq )
; Unsigned divide of a double by a single. Return mod and quotient.
.dw MIN-4
.db 6,"UM/MOD",0
UMMOD:
mov tos,temp0
pops
mov #17,temp1
UMMOD2:
cmp temp0,tos
jnc UMMOD3
sub temp0,tos
setc
jmp UMMOD4
UMMOD3:
clrc
UMMOD4:
rlc 0(stack)
rlc tos
dec temp1
jnz UMMOD2
rra tos
mov tos,temp0
mov 0(stack),tos
mov temp0,0(stack)
INEXT
; M/MOD ( d n -- r q )
; Signed floored divide of double by single. Return mod and quotient.
.dw UMMOD-8
.db 5,"M/MOD"
MSMOD:
INEST
.dw DUPP,ZLESS,DUPP,TOR,QBRAN,MMOD1
.dw NEGAT,TOR,DNEGA,RFROM
MMOD1:
.dw TOR,DUPP,ZLESS,QBRAN,MMOD2
.dw RAT,PLUS
MMOD2:
.dw RFROM,UMMOD,RFROM,QBRAN,MMOD3
.dw SWAP,NEGAT,SWAP
MMOD3:
.dw EXIT
; /MOD ( n n -- r q )
; Signed divide. Return mod and quotient.
.dw MSMOD-6
.db 4,"/MOD",0
SLMOD:
INEST
.dw OVER,ZLESS,SWAP,MSMOD,EXIT
; MOD ( n n -- r )
; Signed divide. Return mod only.
.dw SLMOD-6
.db 3,"MOD"
MODD:
INEST
.dw SLMOD,DROP,EXIT
; / ( n n -- q )
; Signed divide. Return quotient only.
.dw MODD-4
.db 1,"/"
SLASH:
INEST
.dw SLMOD,SWAP,DROP,EXIT
;; Multiply
; UM* ( u u -- ud )
; Unsigned multiply. Return double product.
.dw SLASH-2
.db 3,"UM*"
UMSTA:
clr temp0
mov #16,temp1
UMSTA2:
bit #1,0(stack)
jz UMSTA3
add tos,temp0
jmp UMSTA4
UMSTA3:
clrc
UMSTA4:
rrc temp0
rrc 0(stack)
dec temp1
jnz UMSTA2
mov temp0,tos
INEXT
; * ( n n -- n )
; Signed multiply. Return single product.
.dw UMSTA-4
.db 1,"*"
STAR:
INEST
.dw UMSTA,DROP,EXIT
; M* ( n n -- d )
; Signed multiply. Return double product.
.dw STAR-2
.db 2,"M*",0
MSTAR:
INEST
.dw DDUP,XORR,ZLESS,TOR
.dw ABSS,SWAP,ABSS,UMSTA,RFROM
.dw QBRAN,MSTA1
.dw DNEGA
MSTA1:
.dw EXIT
; */MOD ( n1 n2 n3 -- r q )
; Multiply n1 and n2, then divide by n3. Return mod and quotient.
.dw MSTAR-4
.db 5,"*/MOD"
SSMOD:
INEST
.dw TOR,MSTAR,RFROM,MSMOD,EXIT
; */ ( n1 n2 n3 -- q )
; Multiply n1 by n2, then divide by n3. Return quotient only.
.dw SSMOD-6
.db 2,"*/",0
STASL:
INEST
.dw SSMOD,SWAP,DROP,EXIT
;; Miscellaneous
; 1+ ( a -- a+1 )
; Increment.
; .dw STASL-4
; .db 2,"1+",0
ONEP:
add #1,tos
INEXT
; 1- ( a -- a-1 )
; Decrement
; .dw ONEP-4
; .db 2,"1-",0
ONEM:
sub #1,tos
INEXT
; 2+ ( a -- a+2 )
; Add cell size in byte to address.
; .dw ONEM-4
; .db 2,"2+",0
CELLP:
add #2,tos
INEXT
; 2- ( a -- a-2 )
; Subtract cell size in byte from address.
; .dw CELLP-4
; .db 2,"2-",0
CELLM:
sub #2,tos
INEXT
; 2* ( n -- 2*n )
; Multiply tos by cell size in bytes.
.dw STASL-4
.db 2,"2*",0
CELLS:
rla tos
INEXT
; 2/ ( n -- n/2 )
; Divide tos by cell size in bytes.
.dw CELLS-4
.db 2,"2/",0
TWOSL:
rra tos
INEXT
; ALIGNED ( b -- a )
; Align address to the cell boundary.
.dw TWOSL-4
.db 7,"ALIGNED"
ALGND:
add #1,tos
bic #1,tos
INEXT
; >CHAR ( c -- c )
; Filter non-printing characters.
; .dw TWOSL-4
; .db 5,">CHAR"
TCHAR:
INEST
.dw BLANK,MAX ;mask msb
.dw DOLIT,126,MIN ;check for printable
.dw EXIT
; DEPTH ( -- n )
; Return the depth of the data stack.
; .dw TWOSL-4 ;MM180701
.dw ALGND-8 ;
.db 5,"DEPTH"
DEPTH:
mov stack,temp0
pushs
mov #SPP,tos
sub temp0,tos
rra tos
INEXT
; PICK ( ... +n -- ... w )
; Copy the nth stack item to tos.
.dw DEPTH-6
.db 4,"PICK",0
PICK:
rla tos
add stack,tos
mov @tos,tos
INEXT
;; Memory access
; +! ( n a -- )
; Add n to the contents at address a.
.dw PICK-6
.db 2,"+!",0
PSTOR:
add @stack+,0(tos)
pops
INEXT
; COUNT ( b -- b +n )
; Return count byte of a string and add 1 to byte address.
.dw PSTOR-4
.db 5,"COUNT"
COUNT:
mov.b @tos+,temp0
pushs
mov temp0,tos
INEXT
; CMOVE ( b1 b2 u -- )
; Copy u bytes from b1 to b2.
.dw COUNT-6
.db 5,"CMOVE"
CMOVE:
mov @stack+,temp0 ;destination
mov @stack+,temp1 ;source
jmp CMOVE2
CMOVE1:
mov.b @temp1+,0(temp0)
inc temp0
CMOVE2:
dec tos
jn CMOVE3 ;I need a jp. Oh, well.
jmp CMOVE1
CMOVE3:
JMP DROP
; FILL ( b u c -- )
; Fill u bytes of character c to area beginning at b.
.dw CMOVE-6
.db 4,"FILL",0
FILL:
mov @stack+,temp0 ;count
mov @stack+,temp1 ;destination
jmp FIL2
FIL1:
mov.b tos,0(temp1)
inc temp1
FIL2:
dec temp0
jn FIL3
jmp FIL1
FIL3:
JMP DROP
;; User variables
; 'BOOT ( -- a )
; The application startup vector.
.dw FILL-6
.db 5,"'BOOT"
TBOOT:
ICONST
.dw 200H
; BASE ( -- a )
; Storage of the radix base for numeric I/O.
.dw TBOOT-6
.db 4,"BASE",0
BASE:
ICONST
.dw 202H
; tmp ( -- a )
; A temporary storage location used in parse and find.
; .dw BASE-6
; .db COMPO+3,"tmp"
TEMP:
ICONST
.dw 204H
; #TIB ( -- a )
; Hold the character pointer while parsing input stream.
; .dw BASE-6
; .db 4,"#TIB",0
NTIB:
ICONST
.dw 206H
; >IN ( -- a )
; Hold the character pointer while parsing input stream.
; .dw NTIB-6
; .db 3,">IN"
INN:
ICONST
.dw 208H
; HLD ( -- a )
; Hold a pointer in building a numeric output string.
; .dw INN-4
; .db 3,"HLD"
HLD:
ICONST
.dw 20AH
; 'EVAL ( -- a )
; A area to specify vocabulary search order.
; .dw HLD-4
; .db 5,"'EVAL"
TEVAL:
ICONST
.dw 20CH
; CONTEXT ( -- a )
; A area to specify vocabulary search order.
; .dw TEVAL-6
; .db 7,"CONTEXT"
CNTXT:
ICONST
.dw 20EH
; CP ( -- a )
; Point to the top of the code dictionary.
.dw BASE-6
.db 2,"CP",0
CP:
ICONST
.dw 210H
; DP ( -- a )
; Point to the bottom of the free ram area.
.dw CP-4
.db 2,"DP",0
DP:
ICONST
.dw 212H
; LAST ( -- a )
; Point to the last name in the name dictionary.
; .dw DP-4
; .db 4,"LAST",0
LAST:
ICONST