-
Notifications
You must be signed in to change notification settings - Fork 2
/
itc430hilvl4e.asm
1662 lines (1471 loc) · 51.1 KB
/
itc430hilvl4e.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
; ----------------------------------------------------------------------
; CamelForth for the Texas Instruments MSP430
; (c) 2009,2014 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]
; ----------------------------------------------------------------------
; itc430hilvl.asm - High Level Words - MSP430
; B. Rodriguez 4 Jan 09
; 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.
; ----------------------------------------------------------------------
; REVISION HISTORY
; 10 may 18 mk - added:
; - added 1MS to wait about 1 millisecond.
; - added MS to wait about n milliseconds.
; - added \ to have comments.
; - added BELL to send $07 (bell) to Terminal.
; - added ESC[ to start esc-sequence.
; - added PN to send parameter of esc-sequence.
; - added PN; to send delimiter ; followed by parameter.
; - added AT-XY to set cursor position in terminal.
; - added PAGE to "page" command to terminal to clear screen.
; - added .VER to print version
; - added .VER to print version.
; - Modified WORDS to stop&go.
; - CAPITALIZE in ' (tick)
; - modified QUIT to become 4e4th prompt.
; - CAPITALIZE in INTERPRET.
; - Checking stack underflow in INTERPRET.
; - added CAPITALIZE to capitalize string.
; - added UPC to capitalize character.
; - added ?STACK for checking stack underflow.
; - CAPITALIZE in IWORD for case INsensitiv interpretation.
; - Modified ACCEPT : backspace writing over characters in terminal.
; 1 mar 14 bjr - adapted from hilvl430.s43 for naken_asm.
; 22 dec 13 bjr - added XON/OFF logic to interpreter loop
; 27 nov 12 bjr - fixed S" IS" to use PARSE
; 13 nov 12 bjr - added PARSE, fixed ( to use PARSE.
; 12 nov 12 bjr - fixed FM/MOD.
; 17 jan 09 bjr - changed label _DP to DDP for compatibility with token
; naming convention. Now uses DEST macro to compute branch offsets.
; 11 jan 09 - modified QUIT for Xon/Xoff flow control
; 4 jan 09 - created from Camel86h.asm.
; SYSTEM VARIABLES & CONSTANTS ==================
;Z u0 -- a-addr current user area adrs
; 0 USER U0
HEADER(U0,2,"U0",DOUSER)
DW 0
;C >IN -- a-addr holds offset into TIB
; 2 USER >IN
HEADER(TOIN,3,">IN",DOUSER)
DW 2
;C BASE -- a-addr holds conversion radix
; 4 USER BASE
HEADER(BASE,4,"BASE",DOUSER)
DW 4
;C STATE -- a-addr holds compiler state
; 6 USER STATE
HEADER(STATE,5,"STATE",DOUSER)
DW 6
;Z dp -- a-addr holds dictionary ptr
; 8 USER DP
HEADER(DDP,2,"DP",DOUSER)
DW 8
;Z 'source -- a-addr two cells: len, adrs
; 10 USER 'SOURCE
HEADER(TICKSOURCE,7,"\'SOURCE",DOUSER)
DW 10
;Z latest -- a-addr last word in dict.
; 14 USER LATEST
HEADER(LATEST,6,"LATEST",DOUSER)
DW 14
;Z hp -- a-addr HOLD pointer
; 16 USER HP
HEADER(HP,2,"HP",DOUSER)
DW 16
;Z LP -- a-addr Leave-stack pointer
; 18 USER LP
HEADER(LP,2,"LP",DOUSER)
DW 18
;Z IDP -- a-addr ROM dictionary pointer
; 20 USER IDP
HEADER(IDP,3,"IDP",DOUSER)
DW 20
;Z NEWEST -- a-addr temporary LATEST storage
; 22 USER NEWEST
HEADER(NEWEST,6,"NEWEST",DOUSER)
DW 22
;Z XON/OFF -- a-addr XON and XOFF characters
; 24 USER XON/OFF
HEADER(XONOFF,7,"XON/OFF",DOUSER)
DW 24
;Z CAPS -- a-addr CAPS=true --> case sensitive ;mk
; 26 USER CAPS
HEADER(CAPS,4,"CAPS",DOUSER)
DW 26
; user variables 28,30 tbd ;mk
; user variables 26,28,30 tbd
;X PAD -- a-addr user PAD buffer
; = end of hold area!
HEADER(PAD,3,"PAD",DOUSER)
DW PADAREA-UAREA
;Z l0 -- a-addr bottom of Leave stack
HEADER(L0,2,"L0",DOUSER)
DW LSTACK-UAREA
;Z r0 -- a-addr end of return stack
HEADER(RZERO,2,"R0",DOUSER)
DW RSTACK-UAREA
;Z s0 -- a-addr end of parameter stack
HEADER(S0,2,"S0",DOUSER)
DW PSTACK-UAREA
;X tib -- a-addr Terminal Input Buffer
; HEX 80 USER TIB 8086: above user area
HEADER(TIB,3,"TIB",DOUSER)
DW TIBAREA-UAREA
;Z tibsize -- n size of TIB
HEADER(TIBSIZE,7,"TIBSIZE",DOCON)
DW TIB_SIZE-2 ; 2 chars safety zone
;C BL -- char an ASCII space
HEADER(BLANK,2,"BL",DOCON)
DW 20h
;Z uinit -- addr initial values for user area
; MSP430: we also use this to initialize the RAM interrupt
; vectors, which immediately follow the user area.
; Per init430f1611.s43, allocate 16 cells for user
; variables, followed by 30 cells for interrupt vectors.
HEADER(UINIT,5,"UINIT",DOROM)
DW 0,0,10,0 ; reserved,>IN,BASE,STATE
DW RAMDICT ; DP
DW 0,0 ; SOURCE init'd elsewhere
DW lastword ; LATEST
DW 0,0 ; HP,LP init'd elsewhere
DW ROMDICT ; IDP
DW 0 ; NEWEST not init'd
DW 0 ; XON/OFF initially disabled
DW 0FFFFH ; CAPS flag. 0 = case sensitive
DW 0,0 ; user variables TBD
; RAM interrupt vectors, 15 vectors of 2 cells each
MOV #nullirq,PC
MOV #nullirq,PC
MOV #nullirq,PC
MOV #nullirq,PC
MOV #nullirq,PC
MOV #nullirq,PC
MOV #nullirq,PC
MOV #nullirq,PC
MOV #nullirq,PC
MOV #nullirq,PC
MOV #nullirq,PC
MOV #nullirq,PC
MOV #nullirq,PC
MOV #nullirq,PC
MOV #nullirq,PC
;Z #init -- n #bytes of user area init data
HEADER(NINIT,5,"#INIT",DOCON)
DW (UAREA_SIZE+VECS_SIZE)*2 ; SIZEs given in cells
; ARITHMETIC OPERATORS ==========================
;C S>D n -- d single -> double prec.
; DUP 0< ;
HEADER(STOD,3,"S>D",DOCOLON)
DW DUP,ZEROLESS,EXIT
;Z ?NEGATE n1 n2 -- n3 negate n1 if n2 negative
; 0< IF NEGATE THEN ; ...a common factor
HEADER(QNEGATE,7,"?NEGATE",DOCOLON)
DW ZEROLESS,qbran
DEST(QNEG1)
DW NEGATE
QNEG1: DW EXIT
;C ABS n1 -- +n2 absolute value
; DUP ?NEGATE ;
HEADER(ABBS,3,"ABS",DOCOLON)
DW DUP,QNEGATE,EXIT
;X DNEGATE d1 -- d2 negate double precision
; SWAP INVERT SWAP INVERT 1 M+ ;
HEADER(DNEGATE,7,"DNEGATE",DOCOLON)
DW SWAP,INVERT,SWAP,INVERT,lit,1,MPLUS
DW EXIT
;Z ?DNEGATE d1 n -- d2 negate d1 if n negative
; 0< IF DNEGATE THEN ; ...a common factor
HEADER(QDNEGATE,8,"?DNEGATE",DOCOLON)
DW ZEROLESS,qbran
DEST(DNEG1)
DW DNEGATE
DNEG1: DW EXIT
;X DABS d1 -- +d2 absolute value dbl.prec.
; DUP ?DNEGATE ;
HEADER(DABS,4,"DABS",DOCOLON)
DW DUP,QDNEGATE,EXIT
;C M* n1 n2 -- d signed 16*16->32 multiply
; 2DUP XOR >R carries sign of the result
; SWAP ABS SWAP ABS UM*
; R> ?DNEGATE ;
HEADER(MSTAR,2,"M*",DOCOLON)
DW TWODUP,XORR,TOR
DW SWAP,ABBS,SWAP,ABBS,UMSTAR
DW RFROM,QDNEGATE,EXIT
;C SM/REM d1 n1 -- n2 n3 symmetric signed div
; 2DUP XOR >R sign of quotient
; OVER >R sign of remainder
; ABS >R DABS R> UM/MOD
; SWAP R> ?NEGATE
; SWAP R> ?NEGATE ;
; Ref. dpANS-6 section 3.2.2.1.
HEADER(SMSLASHREM,6,"SM/REM",DOCOLON)
DW TWODUP,XORR,TOR,OVER,TOR
DW ABBS,TOR,DABS,RFROM,UMSLASHMOD
DW SWAP,RFROM,QNEGATE,SWAP,RFROM,QNEGATE
DW EXIT
;C d1 n1 -- n2 n3 floored signed div'n
; courtesy of Ed Smeda
; DUP >R SM/REM 2DUP 1 < AND IF
; SWAP R@ + SWAP 1- THEN
; R> DROP ;
; Ref. dpANS-6 section 3.2.2.1.
; HEADER FMSLASHMOD,6,'FM/MOD',DOCOLON
; DW DUP,TOR,SMSLASHREM
; DW TWODUP,lit,1,LESS,ANDD,qbran
; DEST FMMOD1
; DW SWAP,RFETCH,PLUS,SWAP,ONEMINUS
;FMMOD1: DW RFROM,DROP,EXIT
; Fixed FM/MOD, added 12 nov 2012
;C FM/MOD d1 n1 -- n2 n3 floored signed div'n
; DUP >R divisor
; 2DUP XOR >R sign of quotient
; >R divisor
; DABS R@ ABS UM/MOD
; SWAP R> ?NEGATE SWAP apply sign to remainder
; R> 0< IF if quotient negative,
; NEGATE
; OVER IF if remainder nonzero,
; R@ ROT - SWAP 1- adjust rem,quot
; THEN
; THEN R> DROP ;
; Ref. dpANS-6 section 3.2.2.1.
HEADER(FMSLASHMOD,6,"FM/MOD",DOCOLON)
DW DUP,TOR,TWODUP,XORR,TOR,TOR
DW DABS,RFETCH,ABBS,UMSLASHMOD
DW SWAP,RFROM,QNEGATE,SWAP,RFROM,ZEROLESS,qbran
DEST(FMMOD1)
DW NEGATE,OVER,qbran
DEST(FMMOD2)
DW RFETCH,ROT,MINUS,SWAP,ONEMINUS
FMMOD2:
FMMOD1: DW RFROM,DROP,EXIT
;C * n1 n2 -- n3 signed multiply
; M* DROP ;
HEADER(STAR,1,"*",DOCOLON)
DW MSTAR,DROP,EXIT
;C /MOD n1 n2 -- n3 n4 signed divide/rem'dr
; >R S>D R> FM/MOD ;
HEADER(SLASHMOD,4,"/MOD",DOCOLON)
DW TOR,STOD,RFROM,FMSLASHMOD,EXIT
;C / n1 n2 -- n3 signed divide
; /MOD nip ;
HEADER(SLASH,1,"/",DOCOLON)
DW SLASHMOD,NIP,EXIT
;C MOD n1 n2 -- n3 signed remainder
; /MOD DROP ;
HEADER(MODD,3,"MOD",DOCOLON)
DW SLASHMOD,DROP,EXIT
;C */MOD n1 n2 n3 -- n4 n5 n1*n2/n3, rem"
; >R M* R> FM/MOD ;
HEADER(SSMOD,5,"*/MOD",DOCOLON)
DW TOR,MSTAR,RFROM,FMSLASHMOD,EXIT
;C */ n1 n2 n3 -- n4 n1*n2/n3
; */MOD nip ;
HEADER(STARSLASH,2,"*/",DOCOLON)
DW SSMOD,NIP,EXIT
;C MAX n1 n2 -- n3 signed maximum
; 2DUP < IF SWAP THEN DROP ;
HEADER(MAX,3,"MAX",DOCOLON)
DW TWODUP,LESS,qbran
DEST(MAX1)
DW SWAP
MAX1: DW DROP,EXIT
;C MIN n1 n2 -- n3 signed minimum
; 2DUP > IF SWAP THEN DROP ;
HEADER(MIN,3,"MIN",DOCOLON)
DW TWODUP,GREATER,qbran
DEST(MIN1)
DW SWAP
MIN1: DW DROP,EXIT
; DOUBLE OPERATORS ==============================
;C 2@ a-addr -- x1 x2 fetch 2 cells
; DUP CELL+ @ SWAP @ ;
; the lower address will appear on top of stack
HEADER(TWOFETCH,2,"2@",DOCOLON)
DW DUP,CELLPLUS,FETCH,SWAP,FETCH,EXIT
;C 2! x1 x2 a-addr -- store 2 cells
; SWAP OVER ! CELL+ ! ;
; the top of stack is stored at the lower adrs
HEADER(TWOSTORE,2,"2!",DOCOLON)
DW SWAP,OVER,STORE,CELLPLUS,STORE,EXIT
;C 2DROP x1 x2 -- drop 2 cells
; DROP DROP ;
HEADER(TWODROP,5,"2DROP",DOCOLON)
DW DROP,DROP,EXIT
;C 2DUP x1 x2 -- x1 x2 x1 x2 dup top 2 cells
; OVER OVER ;
HEADER(TWODUP,4,"2DUP",DOCOLON)
DW OVER,OVER,EXIT
;C 2SWAP x1 x2 x3 x4 -- x3 x4 x1 x2 per diagram
; ROT >R ROT R> ;
HEADER(TWOSWAP,5,"2SWAP",DOCOLON)
DW ROT,TOR,ROT,RFROM,EXIT
;C 2OVER x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2
; >R >R 2DUP R> R> 2SWAP ;
HEADER(TWOOVER,5,"2OVER",DOCOLON)
DW TOR,TOR,TWODUP,RFROM,RFROM
DW TWOSWAP,EXIT
; INPUT/OUTPUT ==================================
;C COUNT c-addr1 -- c-addr2 u counted->adr/len
; DUP CHAR+ SWAP C@ ;
HEADER(COUNT,5,"COUNT",DOCOLON)
DW DUP,CHARPLUS,SWAP,CFETCH,EXIT
;C CR -- output newline
; 0D EMIT 0A EMIT ;
HEADER(CR,2,"CR",DOCOLON)
DW lit,0dh,EMIT,lit,0ah,EMIT,EXIT
;C SPACE -- output a space
; BL EMIT ;
HEADER(SPACE,5,"SPACE",DOCOLON)
DW BLANK,EMIT,EXIT
;C SPACES n -- output n spaces
; BEGIN DUP WHILE SPACE 1- REPEAT DROP ;
HEADER(SPACES,6,"SPACES",DOCOLON)
SPCS1: DW DUP,qbran
DEST(SPCS2)
DW SPACE,ONEMINUS,bran
DEST(SPCS1)
SPCS2: DW DROP,EXIT
;Z umin u1 u2 -- u unsigned minimum
; 2DUP U> IF SWAP THEN DROP ;
HEADER(UMIN,4,"UMIN",DOCOLON)
DW TWODUP,UGREATER,qbran
DEST(UMIN1)
DW SWAP
UMIN1: DW DROP,EXIT
;Z umax u1 u2 -- u unsigned maximum
; 2DUP U< IF SWAP THEN DROP ;
HEADER(UMAX,4,"UMAX",DOCOLON)
DW TWODUP,ULESS,qbran
DEST(UMAX1)
DW SWAP
UMAX1: DW DROP,EXIT
;C ACCEPT c-addr +n -- +n' get line from term'l
; OVER + 1- OVER -- sa ea a
; BEGIN KEY -- sa ea a c
; DUP 0D <> WHILE
; DUP EMIT -- sa ea a c
; DUP 8 = IF DROP 1- >R OVER R> UMAX
; ELSE OVER C! 1+ OVER UMIN
; THEN -- sa ea a
; REPEAT -- sa ea a c
; DROP NIP SWAP - ;
HEADER(ACCEPT,6,"ACCEPT",DOCOLON)
DW OVER,PLUS,ONEMINUS,OVER
ACC1: DW KEY,DUP,lit,0Dh,NOTEQUAL,qbran
DEST(ACC5)
DW DUP,EMIT,DUP,lit,8,EQUAL,qbran
DEST(ACC3)
DW DROP,ONEMINUS,TOR,OVER,RFROM,UMAX
DW SPACE,lit,8,EMIT ;mk clear on BS
DW bran
DEST(ACC4)
ACC3: DW OVER,CSTORE,ONEPLUS,OVER,UMIN
ACC4: DW bran
DEST(ACC1)
ACC5: DW DROP,NIP,SWAP,MINUS,EXIT
;C TYPE c-addr +n -- type line to term'l
; ?DUP IF
; OVER + SWAP DO I C@ EMIT LOOP
; ELSE DROP THEN ;
HEADER(TYP,4,"TYPE",DOCOLON)
DW QDUP,qbran
DEST(TYP4)
DW OVER,PLUS,SWAP,xdo
TYP3: DW II,CFETCH,EMIT,xloop
DEST(TYP3)
DW bran
DEST(TYP5)
TYP4: DW DROP
TYP5: DW EXIT
; HARVARD MODEL EXTENSIONS (split Code & Data)
;Z ICOUNT c-addr1 -- c-addr2 u counted->adr/len
; DUP CHAR+ SWAP IC@ ; from Code space
HEADER(ICOUNT,6,"ICOUNT",DOCOLON)
DW DUP,CHARPLUS,SWAP,ICFETCH,EXIT
;Z ITYPE c-addr +n -- type line to term'l
; ?DUP IF from Code space
; OVER + SWAP DO I IC@ EMIT LOOP
; ELSE DROP THEN ;
HEADER(ITYPE,5,"ITYPE",DOCOLON)
DW QDUP,qbran
DEST(ITYP4)
DW OVER,PLUS,SWAP,xdo
ITYP3: DW II,ICFETCH,EMIT,xloop
DEST(ITYP3)
DW bran
DEST(ITYP5)
ITYP4: DW DROP
ITYP5: DW EXIT
;Z (IS") -- c-addr u run-time code for S"
; R> ICOUNT 2DUP + ALIGNED >R ;
; Harvard model, for string stored in Code space
; e.g. as used by ."
; HEADER(XISQUOTE,5,"(IS\")",DOCOLON)
DW link
DB 0FFh ; not immediate
.set link = $
DB 5,"(IS",'"',')'
.align 16
XISQUOTE: DW DOCOLON
DW RFROM,ICOUNT,TWODUP,PLUS,ALIGNED,TOR
DW EXIT
;Z (S") -- c-addr u run-time code for S"
; R@ I@ get Data address
; R> CELL+ DUP IC@ CHAR+ -- Dadr Radr+2 n+1
; 2DUP + ALIGNED >R -- Dadr Iadr n+1
; >R OVER R> I->D -- Dadr
; COUNT ;
; Harvard model, for string stored in Code space
; which is copied to Data space.
; HEADER(XSQUOTE,4,"(S")",DOCOLON)
DW link
DB 0FFh ; not immediate
.set link = $
DB 4,"(S",'"',')'
.align 16
XSQUOTE: DW DOCOLON
DW RFETCH,IFETCH
DW RFROM,CELLPLUS,DUP,ICFETCH,CHARPLUS
DW TWODUP,PLUS,ALIGNED,TOR
DW TOR,OVER,RFROM,ITOD,COUNT,EXIT
;Z IS" -- compile in-line string
; COMPILE (IS") [ HEX ]
; 22 PARSE ( -- c-addr n )
; DUP >R IC, IHERE R@ D->I
; R> IALLOT ALIGN ; IMMEDIATE
; Harvard model: string is stored in Code space
; IMMED(ISQUOTE,3,"IS"",DOCOLON)
DW link
DB 0FEh ; immediate
.set link = $
DB 3,"IS",'"'
.align 16
ISQUOTE: DW DOCOLON
DW lit,XISQUOTE,COMMAXT
DW lit,22h,PARSE
DW DUP,TOR,ICCOMMA,IHERE,RFETCH,DTOI
DW RFROM,IALLOT,ALIGNN,EXIT
;Z IS" -- compile in-line string OLD DEF'N
; COMPILE (IS") [ HEX ]
; 22 IWORD
; IC@ 1+ ALIGNED IALLOT ; IMMEDIATE
; Harvard model: string is stored in Code space
; IMMED ISQUOTE,3,"IS"',DOCOLON
; DW lit,XISQUOTE,COMMAXT
; DW lit,22H,IWORD
; DW ICFETCH,ONEPLUS,ALIGNED,IALLOT,EXIT
;C S" -- compile in-line string
; COMPILE (S") [ HEX ]
; HERE I, data address
; 22 PARSE ( -- c-addr n )
; DUP >R IC, IHERE R@ D->I
; R@ 1+ ALLOT reserve RAM space
; R> IALLOT ALIGN ; IMMEDIATE
; Harvard model: string is stored in Code space
; IMMED(SQUOTE,2,"S"",DOCOLON)
DW link
DB 0FEh ; immediate
.set link = $
DB 2,'S','"'
.align 16
SQUOTE: DW DOCOLON
DW lit,XSQUOTE,COMMAXT
DW HERE,ICOMMA,lit,22h,PARSE
DW DUP,TOR,ICCOMMA,IHERE,RFETCH,DTOI
DW RFETCH,ONEPLUS,ALLOT
DW RFROM,IALLOT,ALIGNN,EXIT
;C S" -- compile in-line string OLD DEF'N
; COMPILE (S") [ HEX ]
; HERE I, data address
; 22 IWORD
; IC@ 1+ ALIGNED
; DUP ALLOT IALLOT ; IMMEDIATE
; Harvard model: string is stored in Code space
; IMMED SQUOTE,2,'S"',DOCOLON
; DW lit,XSQUOTE,COMMAXT
; DW HERE,ICOMMA,lit,22H,IWORD
; DW ICFETCH,ONEPLUS,ALIGNED
; DW DUP,ALLOT,IALLOT,EXIT
;C ." -- compile string to print
; POSTPONE IS" POSTPONE ITYPE ; IMMEDIATE
; IMMED(DOTQUOTE,2,"."",DOCOLON)
DW link
DB 0FEh ; immediate
.set link = $
DB 2,'.','"'
.align 16
DOTQUOTE: DW DOCOLON
DW ISQUOTE
DW lit,ITYPE,COMMAXT
DW EXIT
;Z IWORD c -- c-addr WORD to Code space
; WORD
; IHERE TUCK OVER C@ CHAR+ D->I ;
HEADER(IWORD,5,"IWORD",DOCOLON)
; DW WORDD,IHERE,TUCK,OVER,CFETCH ;mk
DW WORDD,CAPITALIZE,IHERE,TUCK,OVER,CFETCH ;mk
DW CHARPLUS,DTOI,EXIT
; SEPARATE HEADER EXTENSIONS ARE NOT USED
#define HCOUNT ICOUNT
#define HTYPE ITYPE
#define HWORD IWORD
; NUMERIC OUTPUT ================================
; Numeric conversion is done l.s.digit first, so
; the output buffer is built backwards in memory.
; Some double-precision arithmetic operators are
; needed to implement ANSI numeric conversion.
;Z UD/MOD ud1 u2 -- u3 ud4 32/16->32 divide
; >R 0 R@ UM/MOD ROT ROT R> UM/MOD ROT ;
HEADER(UDSLASHMOD,6,"UD/MOD",DOCOLON)
DW TOR,lit,0,RFETCH,UMSLASHMOD,ROT,ROT
DW RFROM,UMSLASHMOD,ROT,EXIT
;Z UD* ud1 d2 -- ud3 32*16->32 multiply
; DUP >R UM* DROP SWAP R> UM* ROT + ;
HEADER(UDSTAR,3,"UD*",DOCOLON)
DW DUP,TOR,UMSTAR,DROP
DW SWAP,RFROM,UMSTAR,ROT,PLUS,EXIT
;C HOLD char -- add char to output string
; -1 HP +! HP @ C! ;
HEADER(HOLD,4,"HOLD",DOCOLON)
DW lit,-1,HP,PLUSSTORE
DW HP,FETCH,CSTORE,EXIT
;C <# -- begin numeric conversion
; PAD HP ! ; (initialize Hold Pointer)
HEADER(LESSNUM,2,"<#",DOCOLON)
DW PAD,HP,STORE,EXIT
;Z >digit n -- c convert to 0..9A..Z
; [ HEX ] DUP 9 > 7 AND + 30 + ;
HEADER(TODIGIT,6,">DIGIT",DOCOLON)
DW DUP,lit,9,GREATER,lit,7,ANDD,PLUS
DW lit,30h,PLUS,EXIT
;C # ud1 -- ud2 convert 1 digit of output
; BASE @ UD/MOD ROT >digit HOLD ;
HEADER(NUM,1,"#",DOCOLON)
DW BASE,FETCH,UDSLASHMOD,ROT,TODIGIT
DW HOLD,EXIT
;C #S ud1 -- ud2 convert remaining digits
; BEGIN # 2DUP OR 0= UNTIL ;
HEADER(NUMS,2,"#S",DOCOLON)
NUMS1: DW NUM,TWODUP,ORR,ZEROEQUAL,qbran
DEST(NUMS1)
DW EXIT
;C #> ud1 -- c-addr u end conv., get string
; 2DROP HP @ PAD OVER - ;
HEADER(NUMGREATER,2,"#>",DOCOLON)
DW TWODROP,HP,FETCH,PAD,OVER,MINUS,EXIT
;C SIGN n -- add minus sign if n<0
; 0< IF 2D HOLD THEN ;
HEADER(SIGN,4,"SIGN",DOCOLON)
DW ZEROLESS,qbran
DEST(SIGN1)
DW lit,2Dh,HOLD
SIGN1: DW EXIT
;C U. u -- display u unsigned
; <# 0 #S #> TYPE SPACE ;
HEADER(UDOT,2,"U.",DOCOLON)
DW LESSNUM,lit,0,NUMS,NUMGREATER,TYP
DW SPACE,EXIT
;C . n -- display n signed
; <# DUP ABS 0 #S ROT SIGN #> TYPE SPACE ;
HEADER(DOT,1,".",DOCOLON)
DW LESSNUM,DUP,ABBS,lit,0,NUMS
DW ROT,SIGN,NUMGREATER,TYP,SPACE,EXIT
;C DECIMAL -- set number base to decimal
; 10 BASE ! ;
HEADER(DECIMAL,7,"DECIMAL",DOCOLON)
DW lit,10,BASE,STORE,EXIT
;X HEX -- set number base to hex
; 16 BASE ! ;
HEADER(HEX,3,"HEX",DOCOLON)
DW lit,16,BASE,STORE,EXIT
; DICTIONARY MANAGEMENT =========================
;C HERE -- addr returns dictionary ptr
; DP @ ;
HEADER(HERE,4,"HERE",DOCOLON)
DW DDP,FETCH,EXIT
;C ALLOT n -- allocate n bytes in dict
; DP +! ;
HEADER(ALLOT,5,"ALLOT",DOCOLON)
DW DDP,PLUSSTORE,EXIT
;C , x -- append cell to dict
; HERE ! 1 CELLS ALLOT ;
HEADER(COMMA,1,02Ch,DOCOLON)
DW HERE,STORE,lit,1,CELLS,ALLOT,EXIT
;C C, char -- append char to dict
; HERE C! 1 CHARS ALLOT ;
; HEADER(CCOMMA,2,"C,",DOCOLON)
DW link
DB 0FFh ; not immediate
.set link = $
DB 2,"C,"
.align 16
CCOMMA: DW DOCOLON
DW HERE,CSTORE,lit,1,CHARS,ALLOT,EXIT
; The following additional words support the
; "Harvard" model, with separate address spaces
; for Instructions (Code) and Data. ANSI
; requires DP to manage the Data space, so a
; separate Instruction Dictionary Pointer, IDP,
; is added to manage the Code space. Also added:
; I@ IC@ I! IC! I->D D->I (in the primitives)
; ITYPE ICOUNT IWORD (above)
; IHERE IALLOT I, IC, (below)
; It should be possible to convert the Harvard
; implementation to a combined-code-and-data
; system, by equating these words to their
; Data-space counterparts.
;C IHERE -- addr returns Code dictionary ptr
; IDP @ ;
HEADER(IHERE,5,"IHERE",DOCOLON)
DW IDP,FETCH,EXIT
;C IALLOT n -- allocate n bytes in Code dict
; IDP +! ;
HEADER(IALLOT,6,"IALLOT",DOCOLON)
DW IDP,PLUSSTORE,EXIT
;C I, x -- append cell to Code dict
; IHERE I! 1 CELLS IALLOT ;
; HEADER(ICOMMA,2,"I,",DOCOLON)
DW link
DB 0FFh ; not immediate
.set link = $
DB 2,"I,"
.align 16
ICOMMA: DW DOCOLON
DW IHERE,ISTORE,lit,1,CELLS,IALLOT,EXIT
;C IC, char -- append char to Code dict
; IHERE IC! 1 CHARS IALLOT ;
; HEADER(ICCOMMA,3,"IC,",DOCOLON)
DW link
DB 0FFh ; not immediate
.set link = $
DB 3,"IC,"
.align 16
ICCOMMA: DW DOCOLON
DW IHERE,ICSTORE,lit,1,CHARS,IALLOT,EXIT
; SEPARATE HEADER EXTENSIONS ARE NOT USED
#define HHERE IHERE
#define HALLOT IALLOT
#define HCOMMA ICOMMA
#define HCCOMMA ICCOMMA
#define HCFETCH ICFETCH
#define HFETCH IFETCH
#define HCSTORE ICSTORE
#define HSTORE ISTORE
; INTERPRETER ===================================
; Note that NFA>LFA, NFA>CFA, IMMED?, and FIND
; are dependent on the structure of the Forth
; header. This may be common across many CPUs,
; or it may be different.
;mk -----------------------------------------------------------------
; ?STACK -- check stack underflow
; depth 0< abort" SUF" ;
HEADER(QSTACK,6,"?STACK",DOCOLON)
DW DEPTH,ZEROLESS,XISQUOTE
DB 3,"SUF"
DW QABORT,EXIT
;mk \----------------------------------------------------------------
;C SOURCE -- adr n current input buffer
; 'SOURCE 2@ ; length is at lower adrs
HEADER(SOURCE,6,"SOURCE",DOCOLON)
DW TICKSOURCE,TWOFETCH,EXIT
;X /STRING a u n -- a+n u-n trim string
; ROT OVER + ROT ROT - ;
HEADER(SLASHSTRING,7,"/STRING",DOCOLON)
DW ROT,OVER,PLUS,ROT,ROT,MINUS,EXIT
;Z >counted src n dst -- copy to counted str
; 2DUP C! CHAR+ SWAP CMOVE ;
HEADER(TOCOUNTED,8,">COUNTED",DOCOLON)
DW TWODUP,CSTORE,CHARPLUS,SWAP,CMOVE,EXIT
;Z ADR>IN c-addr' -- set >IN to offset to given adr
; SOURCE -- adr' adr n
; ROT ROT - -- n adr'-adr
; MIN 0 MAX -- n'
; >IN ! ;
HEADER(ADRTOIN,6,"ADR>IN",DOCOLON)
DW SOURCE,ROT,ROT,MINUS,MIN,lit,0,MAX
DW TOIN,STORE,EXIT
;X PARSE char -- c-addr n word delim'd by char
; SOURCE >IN @ /STRING -- c adr n
; OVER >R save adr of string start
; ROT SCAN -- adr" n"
; OVER SWAP IF CHAR+ THEN skip trailing delim. if any
; ADR>IN advance >IN -- adr"
; R> TUCK - ; -- adr n'
HEADER(PARSE,5,"PARSE",DOCOLON)
DW SOURCE,TOIN,FETCH,SLASHSTRING
DW OVER,TOR,ROT,SCAN
DW OVER,SWAP,qbran
DEST(PARSE1)
DW ONEPLUS ; char+
PARSE1: DW ADRTOIN
DW RFROM,TUCK,MINUS,EXIT
;C WORD char -- c-addr word delim'd by char
; DUP SOURCE >IN @ /STRING -- c c adr n
; ROT SKIP -- c adr' n'
; DROP ADR>IN PARSE -- adr" n"
; HERE >counted --
; HERE -- a
; BL OVER COUNT + C! ; append trailing blank
HEADER(WORDD,4,"WORD",DOCOLON)
DW DUP,SOURCE,TOIN,FETCH,SLASHSTRING
DW ROT,SKIP
DW DROP,ADRTOIN,PARSE
DW HERE,TOCOUNTED,HERE
DW BLANK,OVER,COUNT,PLUS,CSTORE,EXIT
;C WORD char -- c-addr word delim'd by char OLD DEF'N
; DUP SOURCE >IN @ /STRING -- c c adr n
; DUP >R ROT SKIP -- c adr' n'
; OVER >R ROT SCAN -- adr" n"
; DUP IF CHAR- THEN skip trailing delim.
; R> R> ROT - >IN +! update >IN offset
; TUCK - -- adr' N
; HERE >counted --
; HERE -- a
; BL OVER COUNT + C! ; append trailing blank
; HEADER WORDD,4,'WORD',DOCOLON
; DW DUP,SOURCE,TOIN,FETCH,SLASHSTRING
; DW DUP,TOR,ROT,SKIP
; DW OVER,TOR,ROT,SCAN
; DW DUP,qbran
; DEST WORD1
; DW ONEMINUS ; char-
;WORD1: DW RFROM,RFROM,ROT,MINUS,TOIN,PLUSSTORE
; DW TUCK,MINUS
; DW HERE,TOCOUNTED,HERE
; DW BLANK,OVER,COUNT,PLUS,CSTORE,EXIT
;Z NFA>LFA nfa -- lfa name adr -> link field
; 3 - ;
HEADER(NFATOLFA,7,"NFA>LFA",DOCOLON)
DW lit,3,MINUS,EXIT
;Z NFA>CFA nfa -- cfa name adr -> code field
; HCOUNT 7F AND + ALIGNED ; mask off 'smudge' bit
HEADER(NFATOCFA,7,"NFA>CFA",DOCOLON)
DW HCOUNT
DW lit,07Fh,ANDD,PLUS,ALIGNED,EXIT
;Z IMMED? nfa -- f fetch immediate flag
; 1- HC@ 1 AND 0= ; Flashable model, LSB=0 if immed
HEADER(IMMEDQ,6,"IMMED?",DOCOLON)
DW ONEMINUS,HCFETCH,lit,1,ANDD,ZEROEQUAL,EXIT
;C FIND c-addr -- c-addr 0 if not found
;C FIND c-addr -- xt 1 if immediate
;C FIND c-addr -- xt -1 if "normal"
; LATEST @ BEGIN -- a nfa
; 2DUP OVER C@ CHAR+ -- a nfa a nfa n+1
; N= -- a nfa f
; DUP IF
; DROP
; NFA>LFA H@ DUP -- a link link
; THEN
; 0= UNTIL -- a nfa OR a 0
; DUP IF
; NIP DUP NFA>CFA -- nfa xt
; SWAP IMMED? -- xt iflag
; 0= 1 OR -- xt 1/-1
; THEN ;
HEADER(FIND,4,"FIND",DOCOLON)
DW LATEST,FETCH
FIND1: DW TWODUP,OVER,CFETCH,CHARPLUS
DW NEQUAL,DUP,qbran
DEST(FIND2)
DW DROP,NFATOLFA,HFETCH,DUP
FIND2: DW ZEROEQUAL,qbran
DEST(FIND1)
DW DUP,qbran
DEST(FIND3)
DW NIP,DUP,NFATOCFA
DW SWAP,IMMEDQ,ZEROEQUAL,lit,1,ORR
FIND3: DW EXIT
;mk -----------------------------------------------------------------
;C UPC char -- char capitalize character
; DUP [CHAR] a < OVER [CHAR] z > OR IF EXIT THEN
; [ CHAR A CHAR a - ] LITERAL + ;
; HEADER UPC,3,'UPC',DOCOLON
HEADLESS(UPC,DOCOLON)
DW DUP,lit,"a",LESS,OVER,lit,"z",GREATER
DW ORR,qbran
DEST(UPC1)
DW EXIT
UPC1: DW lit,"A"-"a",PLUS
DW EXIT
;C CAPITALIZE c-addr -- c-addr capitalize string
; DUP COUNT OVER + SWAP ?DO I c@ upc I c! LOOP ;
; HEADER CAPITALIZE, 10, 'CAPITALIZE', DOCOLON
HEADLESS(CAPITALIZE,DOCOLON)
DW CAPS,FETCH,qbran
DEST(CAPS2)
DW DUP,COUNT,OVER,PLUS,SWAP,xdo
CAPS1: DW II,CFETCH,UPC,II,CSTORE
DW xloop
DEST(CAPS1)
CAPS2: DW EXIT
;mk \ ---------------------------------------------------------------
;C LITERAL x -- append numeric literal
; STATE @ IF ['] LIT ,XT I, THEN ; IMMEDIATE
; This tests STATE so that it can also be used
; interpretively. (ANSI doesn't require this.)
IMMED(LITERAL,7,"LITERAL",DOCOLON)
DW STATE,FETCH,qbran
DEST(LITER1)
DW lit,lit,COMMAXT,ICOMMA
LITER1: DW EXIT
;Z DIGIT? c -- n -1 if c is a valid digit
;Z DIGIT? c -- x 0 otherwise
; [ HEX ] DUP 39 > 100 AND + silly looking
; DUP 140 > 107 AND - 30 - but it works!
; DUP BASE @ U< ;
HEADER(DIGITQ,6,"DIGIT?",DOCOLON)
DW DUP,lit,39h,GREATER,lit,100h,ANDD,PLUS
DW DUP,lit,140h,GREATER,lit,107h,ANDD
DW MINUS,lit,30h,MINUS
DW DUP,BASE,FETCH,ULESS,EXIT
;Z ?SIGN adr n -- adr' n' f get optional sign
;Z ?SIGN advance adr/n if sign; return NZ if negative
; OVER C@ -- adr n c
; 2C - DUP ABS 1 = AND -- +=-1, -=+1, else 0
; DUP IF 1+ -- +=0, -=+2
; >R 1 /STRING R> -- adr' n' f
; THEN ;
HEADER(QSIGN,5,"?SIGN",DOCOLON)
DW OVER,CFETCH,lit,2Ch,MINUS,DUP,ABBS
DW lit,1,EQUAL,ANDD,DUP,qbran
DEST(QSIGN1)
DW ONEPLUS,TOR,lit,1,SLASHSTRING,RFROM
QSIGN1: DW EXIT
;C >NUMBER ud adr u -- ud' adr' u' convert string to number
; BEGIN
; DUP WHILE
; OVER C@ DIGIT?
; 0= IF DROP EXIT THEN
; >R 2SWAP BASE @ UD*
; R> M+ 2SWAP
; 1 /STRING
; REPEAT ;
HEADER(TONUMBER,7,">NUMBER",DOCOLON)
TONUM1: DW DUP,qbran
DEST(TONUM3)
DW OVER,CFETCH,DIGITQ
DW ZEROEQUAL,qbran
DEST(TONUM2)
DW DROP,EXIT
TONUM2: DW TOR,TWOSWAP,BASE,FETCH,UDSTAR
DW RFROM,MPLUS,TWOSWAP
DW lit,1,SLASHSTRING,bran
DEST(TONUM1)
TONUM3: DW EXIT
;Z ?NUMBER c-addr -- n -1 string->number
;Z ?NUMBER c-addr -- c-addr 0 if convert error
; DUP 0 0 ROT COUNT -- ca ud adr n