-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraspforth.c
1931 lines (1657 loc) · 59.2 KB
/
graspforth.c
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
//----------------------------------------------------------------------------
//
// graspForth
//
// A Gcc/Retargetable/fAst/Small/Portable ITC FORTH written in C.
// Ver 0.85 (C) Copyright Bernard Mentink ........ 2004
// This code is released for free "personal" use, any commercial interest
// should be directed to the following address:
// <bmentink-at-gmail-dot-com>
//
// Please keep this header intact.
// TODO:
// Need to add the DOES> construct word for CREATE.
// Need to add a ROM load function, that loads and boots a ROM application.
//
// Ver 0.80 Initial version 2004
//
// Ver 0.85 Bug Fixes and added features Sept 2006
// Fixed bugs in "0=" "TRUE" "FALSE" words
// Strings now correct for BIG ENDIAN and LITTLE ENDIAN
// Fixed seg_fault problem in FILE word
// ACKNOWLEDGEMENTS
// eForth 1.0 by Bill Muench and C. H. Ting, 1990;
// Rolf Schroedter for endian fix.
// Please send any improvements/bug fixes to the above address, I will
// incorporate and periodically publish a new release.
// NOTES:
// When using primitives with DOLIT, use underscore names ie _DROP, not DROP.
// --------------------------------- Includes --------------------------------
#include "platform.h"
#include "forthdef.h"
//-------------------------------- Defines ----------------------------------
#define TTRUE -1
#define FFALSE 0
#define CELL sizeof(INT)
#define COMPO 0x40 // lexicon compile only bit
#define IMEDD 0x80 // lexicon immediate bit
#define MASK 0x1F // mask above bits
#define ERR 27
// Size of kernel name field (fixed width, by necessecity)
#define NAME_S 11
// ------------------------------- typedef's ---------------------------------
// NOTE: int and *int must be same size!
typedef int INT;
typedef unsigned int UINT;
typedef int * PTR;
typedef unsigned int * UPTR;
typedef unsigned char *CPTR;
// The dictionary Structure.
// Note. Fixed name field for internal words, user words are variable width.
typedef struct {
INT cfa; // code-field-address
INT lfa; // link-field-address
unsigned char count;
char name[NAME_S];
} HEADS;
// The ROM header, followed by rom image.
typedef struct {
INT magic_no; // start of header
INT boot_vector; // address of user application word. 0 if interpreter
INT rom_start;
INT rom_end;
INT ram_start; // address of rom image and where to put it.
INT cp_; // Forth pointers
INT np_;
INT vp_;
INT last;
} ROM_HEADER; // rom image follows
#define MAGIC_NO 0x12345678 // First word of the rom header
// ------------------------------ Virtual Machine Registers ------------------
#define IP mem[0] // Instruction Pointer, address of current memory cell
#define W mem[1] // Word Pointer, address of next code to execute
#define R0 mem[2] // Address of bottom of address stack
#define S0 mem[3] // Address of bottom of parameter stack
#define RP mem[4] // RP and SP, return stack pointer and stack pointer
#define SP mem[5]
#define X mem[6] // Temporary register
// -------------------------- System and User Variables ----------------------
// These are initialized in Main()
#define tqkey mem[7] // '?key
#define temit mem[8] // 'emit
#define texpect mem[9] // 'expect
#define ttap mem[10] // 'tap
#define techo mem[11] // 'echo
#define tprompt mem[12] // 'prompt
#define teval mem[13] // 'eval
#define tnumber mem[14] // 'number
#define tboot mem[15] // 'boot
#define base mem[16] // base
#define span mem[17] // span
#define in mem[18] // >in
#define ntib mem[19] // #tib
#define hld mem[20] // hld
#define handler mem[21] // handler
#define cp mem[22] // cp
#define np mem[23] // np
#define vp mem[24] // vp
#define llast mem[25] // last
#define vfrth mem[26] // forth
#define context mem[27] // context pointer
// vocab storage
#define context_end mem[35]
#define current mem[36] // current link
// vocab link
#define current_end mem[37]
#define tmp mem[38]
#define csp mem[39]
#define up mem[40]
#define user mem[41] // user area
#define tib mem[41+USER_S] // tib buffer
#define vm mem[41+USER_S+TIB_S] // variable area
#define pad_start mem[41+USER_S+TIB_S+VM_S] // text buffer
#define pad_end mem[41+USER_S+TIB_S+VM_S+PAD_S]
// ----------------------------------------- Macros --------------------------
#define INC(x) (x += CELL)
#define DEC(x) (x -= CELL)
#define DDEC(x) (x -= CELL*2)
#define DINC(x) (x += CELL*2)
#define TOS (*(PTR)SP) // top of stack
#define UTOS (*(UPTR)SP) // top of stack
#define NOS (*(PTR)(SP + CELL)) // next on stack
#define UNOS (*(UPTR)(SP + CELL)) // next on stack
#define TOR (*(PTR)RP) // top of return stack
#define FORTHWORDS static const INT
#define FORTHDICT static const HEADS
// ------------------------------------ Globals ------------------------------
INT mem[MEMSIZE]; // The VM memory
// ------------------------------------- Boot ROM image ----------------------
// If a Forth rom exists, try and boot it.
//
void boot_rom (void)
{
#ifdef ROM_PRESENT
INT *src,*dst,*end;
static volatile ROM_HEADER *rom=(ROM_HEADER *)ROM;
// Check is we have a rom image with the correct header
if(rom->magic_no == MAGIC_NO)
{
// Copy rom to ram
src = (INT *)rom->rom_start;
end = (INT *)rom->rom_end;
dst = (INT *)rom->ram_start;
for( ; src <= end ; src++, dst++)
*dst = *src;
// restore forth pointers
cp = rom->cp_;
np = rom->np_;
vp = rom->vp_;
vfrth = llast = rom->last;
// restore boot vector if non-zero
if(rom->boot_vector)
tboot = rom->boot_vector;
}
#endif
}
// --------------------------------------- Main ------------------------------
int main(void)
{
INT *adr,*adr2;
unsigned char count,*badr; // used by NAME?
INT i,j;
HEADS *head;
// Initial values for system & user variables
FORTHWORDS sys_var[] = {
_QRX,_TXSTORE,/*accept*/0,/*KTAP*/0,_TXSTORE,
/*DOTOK*/0,/*INTERP*/0,/*NUMBQ*/0,
0,10,0,0,0,0,0,0,0,0,0,0
};
// ---------------------------------- Kernel Primitives ----------------------
// .. all "_words" are actual code address's
FORTHWORDS cfa [] = {
_NEXT,_DOCOL,_SEMI,_DOLIT,_DOCON,_DOCASE,_EXECUTE,_ATEXEC,
_BRANCH,_ZBRANCH,_STORE, _PSTORE,_FETCH,_CSTORE,_CFETCH,
_RTO,_RFROM,_RFETCH,_RPZ,_RPFETCH,_RPSTORE,_SWAP,_DROP,_DUP,
_QDUP,_SPZ,_NIP,_SPFETCH,_SPSTORE,_OVER,_ROT,_TXSTORE,_QRX,_ADD,
_SUB,_UADD,_USUB,_MUL,_DIV,_UMUL,_UDIV,_MULDIV,_UMULDIV,_TWOMUL,
_TWODIV,_LSHIFT,_RSHIFT,_ZERO,_ONE,_TWO,_THREE,_NONE,_NTWO,
_NTHREE,_INVERT,_NEGATE,_MOD,_UMOD,_TRUEE,_FALSEE,
_EQ,_ZEQ,_LT,_ULT,_ZLT,_GT,_WITHIN,_MAX,_MIN,_AND_,_OR,_XOR,_NOT,
_NAMEQ,_DONEXT,
// -------------------------------- Colon words ------------------------------
// .. all colon words (except cold) start with a _DOCOL word and
// end in SEMI. Number is index into cfa array.
// -------------------------- Sytem & user variables--------------------------
// 75 TQKEY ( -- a)
_DOCOL,DOLIT,_TQKEY,SEMI,
// 79 TEMIT ( -- a)
_DOCOL,DOLIT,_TEMIT,SEMI,
// 83 TEXPECT ( -- a)
_DOCOL,DOLIT,_TEXPECT,SEMI,
// 87 TTAP ( -- a)
_DOCOL,DOLIT,_TTAP,SEMI,
// 91 TECHO ( -- a)
_DOCOL,DOLIT,_TECHO,SEMI,
// 95 TPROMPT ( -- a)
_DOCOL,DOLIT,_TPROMPT,SEMI,
// 99 TEVAL ( -- a)
_DOCOL,DOLIT,_TEVAL,SEMI,
// 103 TNUMBER ( -- a)
_DOCOL,DOLIT,_TNUMBER,SEMI,
// 107 BASE ( -- a)
_DOCOL,DOLIT,_BASE,SEMI,
// 111 SPAN ( -- a)
_DOCOL,DOLIT,_SPAN,SEMI,
// 115 IN ( -- a)
_DOCOL,DOLIT,_IN,SEMI,
// 119 NTIB ( -- a)
_DOCOL,DOLIT,_NTIB,SEMI,
// 123 HLD ( -- a)
_DOCOL,DOLIT,_HLD,SEMI,
// 127 HANDLER ( -- a)
_DOCOL,DOLIT,_HANDLER,SEMI,
// 131 CP ( -- a)
_DOCOL,DOLIT,_CP,SEMI,
// 135 NP ( -- a)
_DOCOL,DOLIT,_NP,SEMI,
// 139 VP ( -- a)
_DOCOL,DOLIT,_VP,SEMI,
// 143 LAST ( -- a)
_DOCOL,DOLIT,_LAST,SEMI,
// 147 VFRTH ( -- a)
_DOCOL,DOLIT,_VFRTH,SEMI,
// 151 CURRENT ( -- a)
_DOCOL,DOLIT,_CURRENT/*_END*/,SEMI,
// 155 UP ( -- a)
_DOCOL,DOLIT,_UP,SEMI,
// 159 TIB ( -- a)
_DOCOL,DOLIT,_TIB,SEMI,
// ------------ High level Colon Words (psuedo forth) ------------------------
// 163 EMIT ( c -- ) Send a character to the output device.
_DOCOL,TEMIT,ATEXEC,SEMI,
// 167 QKEY ( -- c T | F ) wait for key, FALSE if not ready
_DOCOL,TQKEY,ATEXEC,SEMI,
// 171 KEY ( -- c )
_DOCOL,
/*172*/ QKEY,ZBRANCH,&cfa[172],SEMI,
// 176 ABS ( n -- n ) Return the absolute value of n
_DOCOL,DUP,ZLT,
ZBRANCH,&cfa[182],
NEGATE,
/*182*/ SEMI,
// 183 CELLP ( a -- a ) Add cell size in byte to address.
_DOCOL,DOLIT,CELL,ADD,SEMI,
// 188 CELLM ( a -- a ) Subtract cell size in byte to address.
_DOCOL,DOLIT,0-CELL,ADD,SEMI,
// 193 CELLS ( a -- a ) Multiply by cell size.
_DOCOL,DOLIT,CELL,MUL,SEMI,
// 198 ALIGNED ( b -- a ) Align address to the cell boundary.
_DOCOL,DUP,DOLIT,CELL,
MOD,DUP,
ZBRANCH,&cfa[210],
DOLIT,CELL,SWAP,SUB,
/*210*/ ADD,SEMI,
// 212 BLANK ( -- 32 ) Return 32, the blank character.
_DOCOL,DOLIT,32,SEMI,
// 216 >CHAR ( c -- c ) Filter non-printing characters.
_DOCOL,DOLIT,0x7F,AND,DUP, //mask msb
BLANK,DOLIT,127,WITHIN,NOT, //check for printable
ZBRANCH,&cfa[231],
DROP,DOLIT,0x5F, //replace non-printables '_'
/*231*/ SEMI,
// 232 DEPTH ( -- n ) Return the depth of the data stack.
_DOCOL,SPFETCH,SPZ,NOP,SWAP,SUB,
DOLIT,CELL,DIV,SEMI,
// 242 PICK ( +n -- w ) Copy the nth stack item to tos.
_DOCOL,ONE,ADD,CELLS,
SPFETCH,ADD,FETCH,SEMI,
// 250 COUNT ( b -- b +n ) Return count byte of a string and add 1 to byte address.
_DOCOL,DUP,ONE,ADD,
SWAP,CFETCH,SEMI,
// 257 HERE ( -- a ) Return the top of the code dictionary.
_DOCOL,CP,FETCH,SEMI,
// 261 PAD ( -- a ) return address of text buffer
_DOCOL,DOLIT,_PAD,SEMI,
// 265 CMOVE ( b1 b2 u -- ) Copy u bytes from b1 to b2.
_DOCOL,RTO,
BRANCH,&cfa[279],
/*269*/ RTO,DUP,CFETCH,
RFETCH,CSTORE,
ONE,ADD,
RFROM,ONE,ADD,
/*279*/ DONEXT,&cfa[269],
DDROP,SEMI,
// 283 FILL ( b u c -- ) Fill u bytes of character c to area beginning at b.
_DOCOL,SWAP,RTO,SWAP,
BRANCH,&cfa[293],
/*289*/ DDUP,CSTORE,ONE,ADD,
/*293*/ DONEXT,&cfa[289],
DROP,SEMI,
// 297 -TRAILING ( b u -- b u ) Adjust the count to eliminate trailing white space.
_DOCOL,RTO,
BRANCH,&cfa[313],
/*301*/ BLANK,OVER,RFETCH,ADD,CFETCH,LT,
ZBRANCH,&cfa[313],
RFROM,ONE,ADD,SEMI,
/*313*/ DONEXT,&cfa[301],
ZERO,SEMI,
// 317 PACK$ ( b u a -- a )
_DOCOL,ALIGNED,DUP,RTO, //strings only on cell boundary
OVER,DUP,
DOLIT,CELL,MOD, //count mod cell
SUB,OVER,ADD,
ZERO,SWAP,STORE, //null fill cell
DDUP,CSTORE,ONE,ADD, //save count
SWAP,CMOVE,RFROM,SEMI, //move string
// 340 DIGIT ( u -- c ) Convert digit u to a character.
_DOCOL,DOLIT,9,OVER,LT,
DOLIT,7,AND,ADD,
DOLIT,0x30,ADD,SEMI,
// 353 U/MOD UDIVMOD ( u1 u2 -- ur uq )division of u1 / u2
// returning rem & quot
_DOCOL,OVER,OVER, // (u1 u2 u1 u2 --)
UMOD,ROT,ROT,UDIV,SEMI,
// 361 EXTRACT ( n base -- n c ) Extract the least significant digit from n.
_DOCOL,UDIVMOD,
SWAP,DIGIT,SEMI,
// 366 <# BDIGS ( -- ) Initiate the numeric output process.
_DOCOL,PAD,HLD,STORE,SEMI,
// 371 HOLD ( c -- ) Insert a character into the numeric output string.
_DOCOL,HLD,FETCH,ONE,SUB,
DUP,HLD,STORE,CSTORE,SEMI,
// 381 # DIG ( u -- u ) Extract one digit from u and append the digit to output string.
_DOCOL,BASE,FETCH,UDIVMOD,
SWAP,DOLIT,9,OVER,
LT,ZBRANCH,&cfa[395],DOLIT,7,ADD,
/*395*/ DOLIT,'0',ADD,HOLD,SEMI,
// 400 #S ( u -- 0 0 ) Convert u until all digits are added to the output string.
_DOCOL,
/*401*/ DIG,DUP,
ZBRANCH,&cfa[407],
BRANCH,&cfa[401],
/*407*/ SEMI,
// 408 SIGN ( n -- ) Add a minus sign to the numeric output string.
_DOCOL,ZLT,
ZBRANCH,&cfa[415],
DOLIT,'-',HOLD,
/*415*/ SEMI,
// 416 #> ( u -- b u ) Prepare the output string to be TYPE'd.
_DOCOL,DROP,HLD,FETCH,
PAD,OVER,SUB,SEMI,
// 424 str ( w -- b u ) Convert a signed integer to a numeric string.
_DOCOL,DUP,RTO,ABS,
BDIGS,DIGS,RFROM,
SIGN,EDIGS,SEMI,
// 434 HEX ( -- ) Use radix 16 as base for numeric conversions.
_DOCOL,DOLIT,16,BASE,STORE,SEMI,
// 440 DECIMAL ( -- ) Use radix 10 as base for numeric conversions.
_DOCOL,DOLIT,10,BASE,STORE,SEMI,
// 446 DIGIT? ( c base -- u t )Convert a character to its numeric value.
_DOCOL,RTO,DOLIT,'0',SUB,
DOLIT,9,OVER,LT,
ZBRANCH,&cfa[465],
DOLIT,7,SUB,
DUP,DOLIT,10,LT,OR,
/*465*/ DUP,RFROM,ULT,SEMI,
// 469 DDROP ( n n -- ) drop two items off stack
_DOCOL,DROP,DROP,SEMI,
// 473 NUMBER? ( a -- n T | a F ) Convert a number string to integer.
_DOCOL,BASE,FETCH,RTO,ZERO,OVER,COUNT,
OVER,CFETCH,DOLIT,'$',EQ,
ZBRANCH,&cfa[496],
HEX,SWAP,DOLIT,1,ADD,
SWAP,DOLIT,1,SUB,
/*496*/ OVER,CFETCH,DOLIT,'-',EQ,RTO,
SWAP,RFETCH,SUB,SWAP,RFETCH,ADD,QDUP,
ZBRANCH,&cfa[550],
DOLIT,1,SUB,RTO,
/*515*/ DUP,RTO,CFETCH,BASE,FETCH,DIGITQ,
ZBRANCH,&cfa[543],
SWAP,BASE,FETCH,MUL,ADD,RFROM,
DOLIT,1,ADD,
DONEXT,&cfa[515],
RFETCH,SWAP,DROP,
ZBRANCH,&cfa[540],
NEGATE,
/*540*/ SWAP,
BRANCH,&cfa[549],
/*543*/ RFROM,RFROM,DDROP,DDROP,DOLIT,0,
/*549*/ DUP,
/*550*/ RFROM,DDROP,
RFROM,BASE,STORE,SEMI,
// 556 CR ( -- ) Output a carriage return and a line feed.
_DOCOL,DOLIT,'\r',EMIT,
DOLIT,'\n',EMIT,SEMI,
// 564 NUF? ( -- t ) Return false if no input, else pause and if CR return true.
_DOCOL,QKEY,DUP,
ZBRANCH,&cfa[574],
DDROP,KEY,DOLIT,LINEFEED,EQ,
/*574*/ SEMI,
// 575 PACE ( -- ) Send a pace character for the file downloading process.
_DOCOL,DOLIT,11,EMIT,SEMI,
// 580 SPACE ( -- ) Send the blank character to the output device.
_DOCOL,BLANK,EMIT,SEMI,
// 584 SPACES ( +n -- ) Send n spaces to the output device.
_DOCOL,ZERO,MAX,RTO,
BRANCH,&cfa[591],
/*590*/ SPACE,
/*591*/ DONEXT,&cfa[590],
SEMI,
// 594 TYPE ( b u -- ) Output u characters from b.
_DOCOL,RTO,
BRANCH,&cfa[603],
/*598*/ DUP,CFETCH,EMIT,
ONE,ADD,
/*603*/ DONEXT,&cfa[598],
DROP,SEMI,
// 607 do$ ( -- a ) Return the address of a compiled string.
_DOCOL,RFROM,RFETCH,RFROM,COUNT,ADD,
ALIGNED,RTO,SWAP,RTO,SEMI,
// 618 $"| ( -- a ) Run time routine compiled by $".
// Return address of a compiled string.
_DOCOL,DOSTR,SEMI, // force a call to do$
// 621 ."| ( -- ) Run time routine of ." . Output a compiled string.
_DOCOL,DOSTR,COUNT,TYPES,SEMI,
// 626 .R ( n +n -- ) Display an integer in a field of n columns, right justified.
_DOCOL,RTO,STR,RFROM,OVER,SUB,
SPACES,TYPES,SEMI,
// 635 U.R ( u +n -- ) Display an unsigned integer in n column, right justified.
_DOCOL,RTO,BDIGS,DIGS,EDIGS,
RFROM,OVER,SUB,
SPACES,TYPES,SEMI,
// 646 U. ( u -- ) Display an unsigned integer in free format.
_DOCOL,BDIGS,DIGS,EDIGS,
SPACE,TYPES,SEMI,
// 653 _TYPE ( b u -- ) Display a string. Filter non-printing characters.
_DOCOL,RTO, //start count down loop
BRANCH,&cfa[664], //skip first pass
/*657*/ DUP,CFETCH,TOCHAR,EMIT, //display only printable
DOLIT,1,ADD, //increment address
/*664*/ DONEXT,&cfa[657], //loop till done
DROP,SEMI,
// 668 . ( w -- ) Display an integer in free format, preceeded by a space.
_DOCOL,BASE,FETCH,DOLIT,10,XOR, //?decimal
ZBRANCH,&cfa[678],
UDOT,SEMI, //no, display unsigned
/*678*/ STR,SPACE,TYPES,SEMI, //yes, display signed
// 682 ? ( a -- ) Display the contents in a memory cell.
_DOCOL,FETCH,DOT,SEMI,
// 686 parse ( b u c -- b u delta ) Scan string delimited by c
// Return found string and its offset.
_DOCOL,TEMP,STORE,OVER,RTO,DUP,
ZBRANCH,&cfa[763],
DOLIT,1,SUB,TEMP,FETCH,BLANK,EQ,
ZBRANCH,&cfa[724],
RTO,
/*704*/ BLANK,OVER,CFETCH, // skip leading blanks ONLY
SUB,ZLT,INVERT,
ZBRANCH,&cfa[723],
DOLIT,1,ADD,
DONEXT,&cfa[704],
RFROM,DROP,DOLIT,0,DUP,SEMI,
/*723*/ RFROM,
/*724*/ OVER,SWAP,
RTO,
/*727*/ TEMP,FETCH,OVER,CFETCH,SUB, //scan for delimiter
TEMP,FETCH,BLANK,EQ,
ZBRANCH,&cfa[739],
ZLT,
/*739*/ ZBRANCH,&cfa[750],
DOLIT,1,ADD,
DONEXT,&cfa[727],
DUP,RTO,
BRANCH,&cfa[757],
/*750*/ RFROM,DROP,DUP,
DOLIT,1,ADD,RTO,
/*757*/ OVER,SUB,
RFROM,RFROM,SUB,SEMI,
/*763*/ OVER,RFROM,SUB,SEMI,
// 767 PARSE ( c -- b u) Scan input stream and return counted string delimited by c.
_DOCOL,RTO,TIB,IN,FETCH,ADD, //current input buffer pointer
NTIB,FETCH,IN,FETCH,SUB, //remaining count
RFROM,PARS,IN,PSTORE,SEMI,
// 783 .( ( -- ) Output following string up to next ) .
_DOCOL,DOLIT,')',PARSE,TYPES,SEMI,
// 789 ( ( -- ) Ignore following string up to next ) . A comment.
_DOCOL,DOLIT,')',PARSE,DDROP,SEMI,
// 795 \ ( -- ) Ignore following text till the end of line.
_DOCOL,NTIB,FETCH,IN,STORE,SEMI,
// 801 CHAR ( -- c ) Parse next word and return its first character.
_DOCOL,BLANK,PARSE,DROP,CFETCH,SEMI,
// 807 TOKEN ( -- a ) Parse a word from input stream and copy it to name dictionary.
_DOCOL,BLANK,PARSE,DOLIT,31,MIN,
NP,FETCH,OVER,SUB,CELLM,
PACKS,SEMI,
// 820 WORD ( c -- a ) Parse a word from input stream and copy it to code dictionary.
_DOCOL,PARSE,HERE,PACKS,SEMI,
// 825 NAME> ( na -- ca ) Return a code address given a name address.
_DOCOL,CELLM,CELLM,FETCH,NOP,NOP,NOP,NOP,SEMI,
// 834 ^H ( bot eot cur -- bot eot cur ) Backup the cursor by one character.
_DOCOL,RTO,OVER,RFROM,SWAP,OVER,XOR,
ZBRANCH,&cfa[856],
DOLIT,8,TECHO,ATEXEC,ONE,SUB,
BLANK,TECHO,ATEXEC,
DOLIT,8,TECHO,ATEXEC,
/*856*/ SEMI,
// 857 TAP ( bot eot cur c -- bot eot cur ) Accept and echo the key stroke and bump the cursor.
_DOCOL,DUP,TECHO,ATEXEC,
OVER,CSTORE,ONE,ADD,SEMI,
// 866 kTAP ( bot eot cur c -- bot eot cur ) Process a key stroke, CR or backspace.
_DOCOL,DUP,DOLIT,LINEFEED,XOR,
ZBRANCH,&cfa[883],
DOLIT,8,XOR,
ZBRANCH,&cfa[881],
BLANK,TAP,SEMI,
/*881*/ BKSP,SEMI,
/*883*/ DROP,SWAP,DROP,DUP,SEMI,
// 888 accept ( b u -- b u ) Accept characters to input buffer.
// Return with actual count.
_DOCOL,OVER,ADD,OVER,
/*892*/ DDUP,XOR,
ZBRANCH,&cfa[911],
KEY,DUP,
BLANK,DOLIT,127,WITHIN,
ZBRANCH,&cfa[907],
TAP,
BRANCH,&cfa[909],
/*907*/ TTAP,ATEXEC,
/*909*/ BRANCH,&cfa[892],
/*911*/ DROP,OVER,SUB,SEMI,
// 915 EXPECT ( b u -- ) Accept input stream and store count in SPAN.
_DOCOL,TEXPECT,ATEXEC,SPAN,STORE,DROP,SEMI,
// 922 QUERY ( -- ) Accept input stream to terminal input buffer.
_DOCOL,TIB,DOLIT,80,TEXPECT,ATEXEC,NTIB,STORE,
DROP,ZERO,IN,STORE,SEMI,
// 935 CATCH ( ca -- 0 | err# ) Execute word at ca and set up an error frame for it.
_DOCOL,SPFETCH,RTO,HANDLER,FETCH,RTO,//save error frame
RPFETCH,HANDLER,STORE,EXECUTE,//execute
RFROM,HANDLER,STORE,//restore error frame
RFROM,DROP,ZERO,SEMI,//no error
// 952 THROW ( err# -- err# ) Reset system to current local error frame,
_DOCOL,HANDLER,FETCH,RPSTORE,//restore return stack
RFROM,HANDLER,STORE,//restore handler frame
RFROM,SWAP,RTO,SPSTORE,//restore data stack
DROP,RFROM,SEMI,
// 966 doVAR ( -- a ) Run time routine for CREATE.
_DOCOL,RFROM,SEMI,
// 969 doVRAM ( -- a ) Run time routine for VARIABLE & CONSTANT.
_DOCOL,RFROM,FETCH,SEMI,
// 973 NULL$ ( -- a ) Return address of a null string with zero count.
_DOCOL,DOVAR, // emulate CREATE
0,
#if (ENDIAN == BIG)
0x636f796f,0x74650000, // ASCII "coyote"
#else
0x6f796f63,0x00006574, // DC.B 99,111,121,111,116,101 ENDIANESS?
#endif
// 978 ABORT ( -- ) Reset data stack and jump to QUIT.
_DOCOL,NULLS,THROW,
// 981 (abort") ( f -- ) Run time routine of ABORT" . Abort with a message.
_DOCOL,ZBRANCH,&cfa[986], //text flag
DOSTR,THROW, //pass error string
/*986*/ DOSTR,DROP,SEMI, //drop error
// 989 $INTERPRET ( a -- ) Interpret a word. If failed, try to convert it to an integer.
_DOCOL,NAMEQ,QDUP, // ?defined
ZBRANCH,&cfa[1005],
CFETCH,DOLIT,COMPO,AND, //?compile only lexicon bits
//DROP,NOP,NOP,NOP,NOP,
ABORQ,
#if (ENDIAN == BIG)
0x0d20636f, 0x6d70696c, 0x6e6f2065, 0x6c790000, //13,' compile only'
#else
0x6f63200d, 0x6c69706d, 0x6e6f2065, 0x0000796c, //13,' compile only'
#endif
EXECUTE,SEMI, //execute defined word
/*1005*/ TNUMBER,ATEXEC, //convert a number
ZBRANCH,&cfa[1010],
SEMI,
/*1010*/ THROW, //error
// 1011 [ ( -- ) Start the text interpreter.
_DOCOL,DOLIT,INTERP,TEVAL,STORE,SEMI,
// 1017 .OK ( -- ) Display 'ok' only while interpreting.
_DOCOL,DOLIT,INTERP,TEVAL,FETCH,EQ,
ZBRANCH,&cfa[1027],
DOTQP,
#if (ENDIAN == BIG)
0x03206f6b, // 3," ok"
#else
0x6b6f2003, // 3," ok"
#endif
/*1027*/ CR,SEMI,
// 1029 ?STACK ( -- ) Abort if the data stack underflows.
_DOCOL,DEPTH,ZLT, //check only for underflow
ABORQ,
#if (ENDIAN == BIG)
0x0a20756e, 0x64657266, 0x6c6f7700, // 10," undeflow"
#else
0x6e75200a, 0x66726564, 0x00776f6c, // 10,' underflow'
#endif
NOP/*0*/, SEMI,
// 1038 EVAL ( -- ) Interpret the input stream.
_DOCOL,
/*1039*/ TOKEN,DUP,CFETCH, //?input stream empty
ZBRANCH,&cfa[1049],
TEVAL,ATEXEC,QSTACK, //evaluate input, check stack
BRANCH,&cfa[1039],
/*1049*/ DROP,TPROMPT,ATEXEC,SEMI, //prompt
// 1053 PRESET ( -- ) Reset data stack pointer and the terminal input buffer.
_DOCOL,SPZ,SPSTORE,
DOLIT,TIB,NTIB,CELLP,STORE,SEMI,
// 1062 2! ( d a -- ) Store the double integer to address a.
_DOCOL,SWAP,OVER,STORE,
CELLP,STORE,SEMI,
// 1069 2@ ( a -- d ) Fetch double integer from address a.
_DOCOL,DUP,CELLP,FETCH,
SWAP,FETCH,SEMI,
// 1076 xio ( a a a -- ) Reset the I/O vectors 'EXPECT, 'TAP, 'ECHO and 'PROMPT.
_DOCOL,DOLIT,ACCEPT,TEXPECT,DSTORE,
TECHO,DSTORE,SEMI,
// 1084 FILE ( -- ) Select I/O vectors for file download.
_DOCOL,DOLIT,PACE,DOLIT,_DROP,
DOLIT,KTAP,XIO,SEMI,
// 1093 HAND ( -- ) Select I/O vectors for terminal interface.
_DOCOL,DOLIT,DOTOK,DOLIT,EMIT,
DOLIT,KTAP,XIO,SEMI,
// 1102 I/O ( -- a ) Array to store default I/O vectors.
_DOCOL,DOVAR, //emulate CREATE
_QRX,_TXSTORE, // default I/O vectors
// 1106 CONSOLE ( -- ) Initiate terminal interface.
_DOCOL,ISLO,DFETCH,TQKEY,DSTORE, //restore default I/O device
HAND,SEMI, //keyboard input
// 1113 QUIT ( -- ) Reset return stack pointer and start text interpreter.
_DOCOL,RPZ,RPSTORE, //reset return stack pointer
/*1116*/ LBRAC, // start interpretation
/*1117*/ QUERY, // get input
DOLIT,EVAL,CATCH,QDUP, //evaluate input
ZBRANCH,&cfa[1117], //continue till error
TPROMPT,FETCH,RTO, //save input device
CONSOLE,NULLS,OVER,XOR, //?display error message
ZBRANCH,&cfa[1138],
SPACE,COUNT,TYPES, //error message
DOTQP,
#if (ENDIAN == BIG)
0x03203f20, // 3," ? "
#else
0x203f2003, // 3,' ? ' ;error prompt
#endif
/*1138*/ RFROM,DOLIT,DOTOK,XOR, //?file input
ZBRANCH,&cfa[1147],
DOLIT,ERR,EMIT, //;file error, tell host
/*1147*/ PRESET, //some cleanup
BRANCH,&cfa[1116],
// 1150 2DUP ( n1 n2 -- n1 n2 n1 n2)
_DOCOL,OVER,OVER,SEMI,
// 1154 NOP --
_DOCOL,SEMI,
// 1156 TEMP ( -- a ) return address of temp variable
_DOCOL,DOLIT,_TMP,SEMI,
// 1160 ' ( -- ca )Search context vocabularies for the next word in input stream.
_DOCOL,TOKEN,NAMEQ, //?defined
ZBRANCH,&cfa[1166],
SEMI, //yes, push code address
/*1166*/ THROW, //no, error
// 1167 ALLOT ( n -- ) Allocate n bytes to the variable memory.
_DOCOL,VP,PSTORE,SEMI, //adjust code pointer
// 1171 , ( w -- ) Compile an integer into the code dictionary.
_DOCOL,HERE,DUP,CELLP, //cell boundary
CP,STORE,STORE,SEMI, //adjust code pointer and compile
// 1179 [COMPILE] ( -- ; <string> ) Compile the next immediate word
// into code dictionary.
_DOCOL,TICK,COMMA,SEMI,
// 1183 COMPILE ( -- ) Compile the next address in colon list to code dictionary.
_DOCOL,RFROM,DUP,FETCH,COMMA, //compile address
CELLP,RTO,SEMI, //adjust return address
// 1191 LITERAL ( w -- ) Compile tos to code dictionary as an integer literal.
_DOCOL,COMPILE,DOLIT,COMMA,SEMI,
// 1196 $," ( -- ) Compile a literal string up to next " .
_DOCOL,DOLIT,0x22,WORDD, // '"' ,move string to code dictionary
COUNT,ADD,ALIGNED, //calculate aligned end of string
CP,STORE,SEMI, //adjust the code pointer
// 1206 RECURSE ( -- ) Make the current word available for compilation.
_DOCOL,LAST,FETCH,NAMET,COMMA,SEMI,
// 1212 FOR ( -- a ) Start a FOR-NEXT loop structure in a colon definition.
_DOCOL,COMPILE,RTO,HERE,SEMI,
// 1217 BEGIN ( -- a ) Start an infinite or indefinite loop structure.
_DOCOL,HERE,SEMI,
// 1220 NEXT ( a -- ) Terminate a FOR-NEXT loop structure.
_DOCOL,COMPILE,DONEXT,COMMA,SEMI,
// 1225 UNTIL ( a -- ) Terminate a BEGIN-UNTIL indefinite loop structure.
_DOCOL,COMPILE,ZBRANCH,COMMA,SEMI,
// 1230 AGAIN ( a -- ) Terminate a BEGIN-AGAIN infinite loop structure.
_DOCOL,COMPILE,BRANCH,COMMA,SEMI,
// 1235 IF ( -- A ) Begin a conditional branch structure.
_DOCOL,COMPILE,ZBRANCH,HERE,
ZERO,COMMA,SEMI,
// 1242 AHEAD ( -- A ) Compile a forward branch instruction.
_DOCOL,COMPILE,BRANCH,HERE,ZERO,COMMA,SEMI,
// 1249 REPEAT ( A a -- ) Terminate a BEGIN-WHILE-REPEAT indefinite loop.
_DOCOL,AGAIN,HERE,SWAP,STORE,SEMI,
// 1255 THEN ( A -- ) Terminate a conditional branch structure.
_DOCOL,HERE,SWAP,STORE,SEMI,
// 1260 AFT ( a -- a A ) Jump to THEN in a FOR-AFT-THEN-NEXT
// loop the first time through.
_DOCOL,DROP,AHEAD,BEGIN,SWAP,SEMI,
// 1266 ELSE ( A -- A ) Start the false clause in an IF-ELSE-THEN structure.
_DOCOL,AHEAD,SWAP,THENN,SEMI,
// 1271 WHILE ( a -- A a ) Conditional branch out of a BEGIN-WHILE-REPEAT loop.
_DOCOL, IFF,SWAP,SEMI,
// 1275 ABORT" ( -- ; <string> ) Conditional abort with an error message.
_DOCOL,COMPILE,ABORQ,STRCQ,SEMI,
// 1280 $" ( -- ; <string> ) Compile an inline string literal.
_DOCOL,COMPILE,STRQP,STRCQ,SEMI,
// 1285 ." ( -- ; <string> ) Compile an inline string literal to be typed out at run time.
_DOCOL,COMPILE,DOTQP,STRCQ,SEMI,
// 1290 ?UNIQUE ( a -- a ) Display a warning message if the word already exists.
_DOCOL,DUP,NAMEQ, //?name exists
ZBRANCH,&cfa[1301],
DOTQP, // redefinitions are OK
#if (ENDIAN == BIG)
0x07207265, 0x44656620, // 7," reDef "
#else
0x65722007, 0x20666544, // 7,' reDef ' but the user should be warned
#endif
OVER,COUNT,TYPES, // just in case its not planned
/*1301*/ DROP,SEMI,
// 1303 $,n ( na -- ) Build a new dictionary name using the string at na.
_DOCOL,DUP,CFETCH, //?null input
ZBRANCH,&cfa[1327],
UNIQUE, //?redefinition
DUP,LAST,STORE, //save na for vocabulary link
HERE,ALIGNED,SWAP, //align code address
CELLM, //link address
CURRENT,FETCH,FETCH,OVER,STORE, // save link to previous word
CELLM,
DUP,NP,STORE, //adjust name pointer
STORE,SEMI, //save code pointer
/*1327*/ STRQP,
#if (ENDIAN == BIG)
0x05206e61, 0x6d650000, // 5,' name' ;null input
#else
0x616e2005, 0x0000656d, // 5,' name' ;null input
#endif
THROW,
// FORTH compiler
// 1331 $COMPILE ( a -- ) Compile next word to code dictionary as a token or literal.
_DOCOL,NAMEQ,QDUP, //?defined
ZBRANCH,&cfa[1346],
CFETCH,DOLIT,IMEDD,AND, //?immediate
ZBRANCH,&cfa[1344],
EXECUTE,SEMI, //its immediate, execute
/*1344*/ COMMA,SEMI, //its not immediate, compile
/*1346*/ TNUMBER,ATEXEC, //try to convert to number
ZBRANCH,&cfa[1352],
LITERAL,SEMI, //compile number as integer
/*1352*/ THROW, //error
// 1353 OVERT ( -- ) Link a new word into the current vocabulary.
_DOCOL,LAST,FETCH,CURRENT,FETCH,STORE,SEMI,
// 1360 ; ( -- ) Terminate a colon definition.
_DOCOL,COMPILE,SEMI,LBRAC,OVERT,SEMI,
// 1366 ] ( -- ) Start compiling the words in the input stream.
_DOCOL,DOLIT,SCOMP,TEVAL,STORE,SEMI,
// 1372 : ( -- ; <string> ) Start a new colon definition using next word as its name.
_DOCOL,TOKEN,SNAME,DOLIT,_DOCOL,
COMMA,RBRAC,SEMI,
// 1380 IMMEDIATE ( -- ) Make the last compiled word an immediate word.
_DOCOL,DOLIT,IMEDD,LAST,FETCH,CFETCH,OR,
LAST,FETCH,CSTORE,SEMI,
// 1391 COMPILE-ONLY ( -- ) Make the last compiled word a compile-only word.
_DOCOL,DOLIT,COMPO,LAST,FETCH,CFETCH,OR,
LAST,FETCH,CSTORE,SEMI,
// Defining words
// 1402 HEADER ( -- ; <string> ) Compile a new header.
_DOCOL,TOKEN,SNAME,OVERT,
DOLIT,_DOCOL,COMMA,SEMI,
// 1410 USER ( u -- ; <string> )Compile a new user variable.
_DOCOL,HEADER,
DOLIT,DOUSER,COMMA,
COMMA,SEMI,
// 1417 CREATE ( -- ; <string> ) Compile a new dict entry without
// allocating code space. Added NOP for DOES> word use.
_DOCOL,HEADER,COMPILE,NOP,
COMPILE,DOVAR,SEMI,
// 1424 VARIABLE ( -- ; <string> ) Compile a new variable in RAM .
_DOCOL,HEADER,
DOLIT,DOVRAM,COMMA,
VP,FETCH,COMMA, //Store address of variable pointer
TWO,VP,PSTORE,SEMI, //increment the pointer
// 1436 CONSTANT ( u -- ; <string> ) Compile a constant in code space.
_DOCOL,HEADER,
DOLIT,DOVRAM,COMMA,
COMMA,SEMI,
// Tools
// 1443 _TYPE ( b u -- ) Display a string. Filter non-printing characters.
_DOCOL,RTO, //start count down loop
BRANCH,&cfa[1453], //skip first pass
/*1447*/ DUP,CFETCH,TOCHAR,EMIT, //display only printable
ONE,ADD, //increment address
/*1453*/ DONEXT,&cfa[1447], //loop till done
DROP,SEMI,
// 1457 dm+ ( a u -- a ) Dump u bytes from , leaving a+u on the stack.
_DOCOL,OVER,DOLIT,4,UDOTR, //display address
SPACE,RTO, //start count down loop
BRANCH,&cfa[1473], //skip first pass
/*1466*/ DUP,CFETCH,DOLIT,3,UDOTR, //display numeric data
ONE,ADD, //increment address
/*1473*/ DONEXT,&cfa[1466], //loop till done
SEMI,
// 1476 DUMP ( a u -- ) Dump u bytes from a, in a formatted manner.
_DOCOL,BASE,FETCH,RTO,HEX, //save radix, set hex
DOLIT,16,DIV, //change count to lines
RTO, //start count down loop
/*1485*/ CR,DOLIT,16,DDUP,DUMPP, //display numeric
ROT,ROT,
DOLIT,2,SPACES,UTYPE, //display printable characters
NUFQ,INVERT, //user control
ZBRANCH,&cfa[1504],
DONEXT,&cfa[1485], //loop till done
BRANCH,&cfa[1506],
/*1504*/ RFROM,DROP, //cleanup loop stack, early exit
/*1506*/ DROP,RFROM,BASE,STORE, //restore radix
SEMI,
// 1511 .S ( -- ) Display the contents of the data stack.
_DOCOL,CR,DEPTH, //stack depth
RTO, //start count down loop
BRANCH,&cfa[1520], //skip first pass
/*1517*/ RFETCH,PICK,DOT, //index stack, display contents
/*1520*/ DONEXT,&cfa[1517], //loop till done
DOTQP,
#if (ENDIAN == BIG)
0x04203c73, 0x70000000, // 4," <sp",0
#else
0x733c2004, 0x00000070, // 4,' <sp',0
#endif
SEMI,
// 1526 !CSP ( -- ) Save stack pointer in CSP for error checking.
_DOCOL,SPFETCH,CSP,STORE,SEMI, //save pointer
// 1531 ?CSP ( -- ) Abort if stack pointer differs from that saved in CSP.
_DOCOL,SPFETCH,CSP,FETCH,XOR, //compare pointers
ABORQ, //abort if different
#if (ENDIAN == BIG)
0x06737461, 0x636b7300, // 6,"stacks",0
#else
0x61747306, 0x00736b63, // 6,'stacks',0
#endif