4 * ASSEMBLY SOURCE LISTING
8 * WITH COMPILER SECURITY
9 * AND VARIABLE LENGTH NAMES
11 * Adapted by Joel Matthew Rees
12 * from fig-FORTH for 6800 by Dave Lion, et. al.
14 * This free/libre/open source publication is provided
15 * through the courtesy of:
20 * and other interested parties.
23 * P.O. Box 8231 - San Jose, CA 95155 - (408) 277-0668
24 * URL: http://www.forth.org
25 * Further distribution must include this notice.
27 NAM Copyright: FORTH Interest Group, original authors, and Joel Matthew Rees
29 * filename fig-forth-auto6809opt.asm
30 * === FORTH-6809 {date} {time}
33 * Permission is hereby granted, free of charge, to any person obtaining a copy
34 * of this software and associated documentation files (the "Software"), to deal
35 * in the Software without restriction, including without limitation the rights
36 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
37 * copies of the Software, and to permit persons to whom the Software is
38 * furnished to do so, subject to the following conditions:
40 * The above copyright notice and this permission notice shall be included in
41 * all copies or substantial portions of the Software.
43 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
46 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
48 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
51 * "Associated documentation" for this declaration of license
52 * shall be interpreted to include only the comments in this file,
53 * or, if the code is split into multiple files,
54 * all files containing the complete source.
56 * This is the MIT model license, as published by the Open Source Consortium,
57 * with associated documentation defined.
58 * It was chosen to reflect the spirit of the original
59 * terms of use, which used archaic legal terminology.
62 * Authors of the 6800 model:
63 * === Primary: Dave Lion,
67 * === The Forth Interest Group
69 * === San Carlos, CA 94070
71 * === Unbounded Computing
72 * === 1134-K Aster Ave.
73 * === Sunnyvale, CA 94086
75 NATWID EQU 2 ; bytes per natural integer/pointer
76 * The original version was developed on an AMI EVK 300 PROTO
77 * system using an ACIA for the I/O.
78 * This version is developed targeting the Tandy Color Computer.
81 * is done in three subroutines:
82 * PEMIT ( word # 182 )
86 * The FORTH words for disc related I/O follow the model
87 * of the FORTH Interest Group, but have not yet been
88 * tested using a real disc.
90 * Addresses in the 6800 implementation reflect the fact that,
91 * on the development system, it was convenient to
92 * write-protect memory at hex 1000, and leave the first
93 * 4K bytes write-enabled. As a consequence, code from
94 * location $1000 to lable ZZZZ could be put in ROM.
95 * Minor deviations from the model were made in the
96 * initialization and words ?STACK and FORGET
97 * in order to do this.
98 * Those deviations will be altered in this
99 * implementation for the 6809 -- Color Computer.
102 * MEMORY MAP for this 16K|32K system:
103 * ( delineated so that systems with 4k byte write-
104 * protected segments can write protect FORTH )
106 * addr. contents pointer init by
107 * **** ******************************* ******* ******
110 * ACIAC EQU $FBCE the ACIA control address and
111 * ACIAD EQU ACIAC+1 data address for PROTO
113 MEMT32 EQU $7FFF ; Theoretical absolute end of all ram
114 MEMT16 EQU $3FFF ; 16K is too tight until we no longer need disc emulation.
121 * substitute for disc mass memory
122 RAMSCR EQU 8 ; addresses calculate as 2 (Too much for 16K in RAM only.)
125 MASSLO EQU MASSHI-RAMSCR*SCRSZ+1
130 * "end" of "usable ram" (If disc mass memory emulation is removed, actual end.)
135 USERSZ EQU 256 ; (Addressable by DP, must be 256 on even boundary)
136 USER16 EQU 1 ; We can change these for ROMPACK or 64K.
137 USER32 EQU 2 ; maybe?
139 USERLO EQU MEMEND-USERSZ*USERCT
142 * user tables of variables
143 * registers & pointers for the virtual machine
144 * scratch area for potential use in something, maybe?
148 * This is a really awkward place to define the disk buffer records.
150 * 4 buffer sectors of VIRTUAL MEMORY
151 NBLK EQU 4 ; # of disc buffer blocks for virtual memory
152 * Should NBLK be SCRSZ/SECTSZ?
153 * each block is SECTSZ+SECTRL bytes in size,
154 * holding SECTSZ characters
156 SECTRL EQU 2*NATWID ; Currently held sector number, etc.
157 BUFSZ EQU (SECTSZ+SECTRL)*NBLK
158 BUFBAS EQU USERLO-BUFSZ
159 * *BUG* SECTRL is hard-wired into several definitions.
160 * It will take a bit of work to ferret them out.
161 * It is too small, and it should not be hard-wired.
162 * SECTSZ was also hard-wired into several definitions,
163 * will I find them all?
169 * Don't want one return too many to destroy the disc buffers.
172 * 32D8|71D8 <== RP RINIT
174 IRP EQU BUFBAS-RPBUMP
176 RSTK16 EQU $50*NATWID ; 80 max levels nesting calls
177 RSTK32 EQU $90*NATWID ; 144 max
182 SFTBND EQU IRP-RSTKSZ ; (false boundary between TIB and return stack)
184 * holds up to TIBSZ characters
185 * and is scanned upward by IN
188 ITIB EQU SFTBND-TIBSZ
190 * 3148|6FB8 <== IN TIB
192 * Don't want terminal input and parameter underflow collisions
197 * 3140|6FB0 <== SP SP0,SINIT
199 * | grows downward from 3140|6FB0
204 * I DICTIONARY grows upward
206 * >>>>>>--------Two words to start RAMmable dictionary--------<<<<<<
209 * ???? end of ram-dictionary. <== DICTPT DPINIT
212 * ???? "FORTH" ( a word ) <=, <== CONTEXT
214 * start of ram-dictionary.
216 * >>>>>> memory from here up must be in RAM area <<<<<<
219 * 6k of romable "FORTH" <== IP ABORT
221 * the VIRTUAL FORTH MACHINE
223 * 1208 initialization tables
224 * 1204 <<< WARM START ENTRY >>>
225 * 1200 <<< COLD START ENTRY >>>
226 * 1200 lowest address used by FORTH
231 * >>>>>> memory from here down left alone <<<<<<
232 * >>>>>> so we can safely call ROM routines <<<<<<
238 * CONVENTIONS USED IN THIS PROGRAM ARE AS FOLLOWS :
240 * IP (hardware Y) points to the current instruction ( pre-increment mode )
241 * RP (hardware S) points to last return address pushedin return stack
242 * SP (hardware U) points to last byte pushed in data stack
244 * Y must be IP when NEXT is entered (if using the inner loop).
246 * When A and B hold one 16 bit FORTH data word,
247 * A contains the high byte, B, the low byte.
249 * UP (hardware DP) is the base of per-task ("user") variables.
250 * (Be careful of the stray semantics of "user".)
252 * W (hardware X) is the pointer to the "code field" address of native CPU
253 * machine code to be executed for the definition of the dictionary word
254 * to be executed/currently executing.
255 * The following natural integer (word) begins any "parameter section"
256 * (body) -- similar to a "this" pointer, but not the same.
257 * It may be native CPU machine code, or it may be a global variable,
258 * or it may be a list of Forth definition words (addresses).
261 * This implementation uses the native subroutine architecture
262 * rather than a postponed-push call that the 6800 model VM uses
263 * to save code and time in leaf routines.
265 * This should allow directly calling many of the Forth words
266 * from assembly language code.
267 * (Be aware of the need for a valid W in some cases.)
268 * It won't allow mixing assembly language directly into Forth word lists.
272 * 0 is false, anything else is true.
273 * Most places in this model that set a boolean flag set true as 1.
274 * This is in contrast to many models that set a boolean flag as -1.
279 * This system is shown with one user (task),
280 * but additional users (tasks) may be added
281 * by allocating additional user tables:
285 UBASEX RMB USERSZ data table for extra users
287 * Some of this stuff gets initialized during
288 * COLD start and WARM start:
289 * [ names correspond to FORTH words of similar (no X) name ]
293 * A few useful VM variables
294 * Will be removed when they are no longer needed.
295 * All are replaced by 6809 registers.
297 N RMB 10 used as scratch by (FIND),ENCLOSE,CMOVE,EMIT,KEY,
298 * SP@,SWAP,DOES>,COLD
301 * These locations are used by the TRACE routine :
303 TRLIM RMB 1 the count for tracing without user intervention
304 TRACEM RMB 1 non-zero = trace mode
305 BRKPT RMB 2 the breakpoint address at which
306 * the program will go into trace mode
307 VECT RMB 2 vector to machine code
308 * (only needed if the TRACE routine is resident)
311 * Registers used by the FORTH virtual machine:
315 W RMB 2 the instruction register points to 6800 code
316 * This is not exactly accurate. Points to the definiton body,
317 * which is native CPU machine code when it is native CPU machine code.
318 * IP RMB 2 the instruction pointer points to pointer to 6800 code
319 * RP RMB 2 the return stack pointer
320 * UP RMB 2 the pointer to base of current user's 'USER' table
321 * ( altered during multi-tasking )
323 *UORIG RMB 6 3 reserved variables
324 RMB 6 3 reserved variables
325 XSPZER RMB 2 initial top of data stack for this user
326 XRZERO RMB 2 initial top of return stack
327 XTIB RMB 2 start of terminal input buffer
328 XWIDTH RMB 2 name field width
329 XWARN RMB 2 warning message mode (0 = no disc)
330 XFENCE RMB 2 fence for FORGET
331 XDICTP RMB 2 dictionary pointer
332 XVOCL RMB 2 vocabulary linking
333 XBLK RMB 2 disc block being accessed
334 XIN RMB 2 scan pointer into the block
335 XOUT RMB 2 cursor position
336 XSCR RMB 2 disc screen being accessed ( O=terminal )
337 XOFSET RMB 2 disc sector offset for multi-disc
338 XCONT RMB 2 last word in primary search vocabulary
339 XCURR RMB 2 last word in extensible vocabulary
340 XSTATE RMB 2 flag for 'interpret' or 'compile' modes
341 XBASE RMB 2 number base for I/O numeric conversion
342 XDPL RMB 2 decimal point place
344 XCSP RMB 2 current stack position, for compile checks
347 XDELAY RMB 2 carriage return delay count
348 XCOLUM RMB 2 carriage width
349 IOSTAT RMB 2 last acia status from write/read
360 * end of user table, start of common system variables
369 * The FORTH program ( address $1200 to about $27FF ) will be written
370 * so that it can be in a ROM, or write-protected if desired,
371 * but right now we're just getting it running.
374 * ######>> screen 3 <<
376 ***************************
377 ** C O L D E N T R Y **
378 ***************************
382 ***************************
383 ** W A R M E N T R Y **
384 ***************************
386 * JMP WENT warm-start code, keeps current dictionary intact
387 LBSR WENT warm-start code, keeps current dictionary intact
391 ******* startup parmeters **************************
393 FDB $6809,0000 cpu & revision
394 FDB 0 topmost word in FORTH vocabulary
395 * BACKSP FDB $7F backspace character for editing
396 BACKSP FDB $08 backspace character for editing
397 UPINIT FDB UORIG initial user area
398 * UPINIT FDB UORIG initial user area
399 SINIT FDB ISP ; initial top of data stack
400 * SINIT FDB ORIG-$D0 initial top of data stack
401 RINIT FDB IRP ; initial top of return stack
402 * RINIT FDB ORIG-2 initial top of return stack
403 FDB ITIB ; terminal input buffer
404 * FDB ORIG-$D0 terminal input buffer
405 FDB 31 initial name field width
406 FDB 0 initial warning mode (0 = no disc)
407 FENCIN FDB REND initial fence
408 DPINIT FDB REND cold start value for DICTPT
409 BUFINT FDB BUFBAS Start of the disk buffers area
410 VOCINT FDB FORTH+4*NATWID
411 COLINT FDB TIBSZ initial terminal carriage width
412 DELINT FDB 4 initial carriage return delay
413 ****************************************************
417 * ######>> screen 13 <<
418 * These were of questionable use anyway,
419 * kept here now to satisfy the assembler and show hints.
420 * They're too much trouble to use with native subroutine call anyway.
421 * PULABX PULS A ; 24 cycles until 'NEXT'
423 * PULABX PULU A,B ; ?? cycles until 'NEXT'
424 * STABX STA 0,X 16 cycles until 'NEXT'
426 * STABX STD 0,X ; ?? cycles until 'NEXT'
428 * GETX LDA 0,X 18 cycles until 'NEXT'
430 * GETX LDD 0,X ?? cycles until 'NEXT'
431 * PUSHBA PSHS B ; 8 cycles until 'NEXT'
433 * PUSHBA PSHU A,B ; ?? cycles until 'NEXT'
437 * "NEXT" takes ?? cycles if TRACE is removed,
439 * and ?? cycles if trace is present and NOT tracing.
441 * = = = = = = = t h e v i r t u a l m a c h i n e = = = = =
443 * NEXT itself might just completely go away.
444 * About the only reason to keep it is to allowing executing a list
445 * which allows a cheap TRACE routine.
447 * NEXT is a loop which implements the Forth VM.
448 * It basically cycles through calling the code out of code lists,
450 * Using a native CPU return for this uses a few extra cycles per call,
451 * compared to simply jumping to each definition and jumping back
452 * to the known beginning of the loop,
453 * but the loop itself is really only there for convenience.
455 * This implementation uses the native subroutine call,
456 * to break the wall between Forth code and non-Forth code.
459 * LEAX 1,X ; pre-increment mode
462 NEXT ; IP is Y, push before using, pull before you come back here.
464 * NEXT2 LDX 0,X get W which points to CFA of word to be done
465 NEXT2 LDX ,Y++ get W which points to CFA of word to be done
468 * But NEXT2 is too much trouble to use with subroutine threading anyway.
470 NEXT3 ; W is X until you use X for something else. (TOS points back here.)
471 * But NEXT3 is too much trouble to use with subroutine threading anyway.
472 * LDX 0,X get VECT which points to executable code
474 * The next instruction could be patched to JMP TRACE =
475 * if a TRACE routine is available: =
479 JSR [,X] ; Saving the postinc cycles,
480 * ; but X must be bumped NATWID to the parameters.
482 * JMP TRACE ( an alternate for the above )
483 BSR DBGREG ( an alternate for the above )
484 * In other words, with the call and the NOP,
485 * there is room to patch the call with a JMP to your TRACE
486 * routine, which you have to provide.
508 DBGNrt PULS CC,D,X,Y,PC
519 ADDB #$C7 ; ($40-$39)-$40
536 DBGREG PSHS U,Y,X,DP,B,A,CC
551 LDD 3*NATWID+4,S ; PC:505
558 LDD 2*NATWID+4,S ; U:50E
562 LDD 1*NATWID+4,S ; Y:513
564 LDD 0*NATWID+4,S ; X at 517
576 LDD [3*NATWID+4,S] ; PC
582 LDD [2*NATWID+4,S] ; U
586 LDD [1*NATWID+4,S] ; Y
588 LDD [0*NATWID+4,S] ; X
633 DBGRdU LDY 2*NATWID+4,S
641 DBGRrt PULS CC,A,B,DP,X,Y,U,PC
642 DBGRLB FCC 'DPCC PC S U Y X A B '
648 * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
655 * Pushes the following natural width integer from the instruction stream
656 * as a literal, or immediate value.
661 * FDB LITERAL-TO-BE-PUSHED
664 * In native processor code, there should be a better way, use that instead.
665 * More specifically, DO NOT CALL THIS from assembly language code.
666 * (Note that there is no compile-only flag in the fig model.)
668 * See (FIND), or PFIND , for layout of the header format.
671 FCC 'LI' ; 'LIT' : NOTE: this is different from LITERAL
672 FCB $D4 ; 'T'|'\x80' ; character code for T, with high bit set.
673 FDB 0 ; link of zero to terminate dictionary scan
674 LIT FDB *+NATWID ; Note also that LIT is meaningless in native code.
686 * ######>> screen 14 <<
689 * Pushes the following byte from the instruction stream
690 * as a literal, or immediate value.
695 * FCB LITERAL-TO-BE-PUSHED
698 * If this is kept, it should have a header for TRACE to read.
699 * If the data bus is wider than a byte, you don't want to do this.
700 * Byte shaving like this is often counter-productive anyway.
701 * Changing the name to LIT8, hoping that will be more understandable.
702 * Also, see comments for LIT.
703 * (Note that there is no compile-only flag in the fig model.)
705 FCC 'LIT' ; 'LIT8' : NOTE: this is different from LITERAL
708 LIT8 FDB *+NATWID (this was an invisible word, with no header)
709 LDB ,Y+ ; This also is meaningless in native code.
721 * off is offset in video buffer area.
723 FCC 'SHOWTO' ; 'SHOWTOS'
752 * Jump to address on stack. Used by the "outer" interpreter to
753 * interactively invoke routines.
754 * Might be useful to have EXECUTE test the pointer, as done in BIF-6809.
756 FCC 'EXECUT' ; 'EXECUTE'
760 PULU X ; Gotta have W anyway, just in case.
761 JMP [,X] ; Tail return.
763 * LDX 0,X get code field address (CFA)
764 * LEAS 1,S ; pop stack
768 * ######>> screen 15 <<
771 * Add the following word from the instruction stream to the
772 * instruction pointer (Y++). Causes a program branch in Forth code stream.
774 * In native processor code, there should be a better way, use that instead.
775 * More specifically, DO NOT CALL THIS from assembly language code.
776 * This is only for Forth code stream.
777 * Also, see comments for LIT.
779 FCC 'BRANC' ; 'BRANCH'
782 BRAN FDB ZBYES ; Go steal code in ZBRANCH
784 * Moving code around to optimize the branch taking case in 0BRANCH.
785 ZBNO LEAY NATWID,Y ; No branch.
789 * BRANCH if flag is zero.
791 * In native processor code, there should be a better way, use that instead.
792 * More specifically, DO NOT CALL THIS from assembly language code.
793 * This is only for Forth code stream.
794 * Also, see comments for LIT.
796 FCC '0BRANC' ; '0BRANCH'
803 LEAY D,Y ; IP is postinc
807 * PSHS B ; ** emulating ABA:
811 * ZBYES LDX IP Note: code is shared with BRANCH, (+LOOP), (LOOP)
819 * ZBNO LDX IP no branch. This code is shared with (+LOOP), (LOOP).
820 * LEAX 1,X ; jump over branch delta
825 * ######>> screen 16 <<
827 * ( --- ) ( limit index *** limit index+1) C
828 * ( limit index *** )
829 * Counting loop primitive. The counter and limit are the top two
830 * words on the return stack. If the updated index/counter does
831 * not exceed the limit, a branch occurs. If it does, the branch
832 * does not occur, and the index and limit are dropped from the
835 * In native processor code, there should be a better way, use that instead.
836 * More specifically, DO NOT CALL THIS from assembly language code.
837 * This is only for Forth code stream.
838 * Also, see comments for LIT.
840 FCC '(LOOP' ; '(LOOP)'
844 LDD #1 ; Borrowing from BIF-6809.
845 XLOOPA ADDD NATWID,S ; Dodge the return address.
850 LDX ,S ; synthetic return
851 LEAS 3*NATWID,S ; Clean up the index and limit.
854 * LDB #1 get set to increment counter by 1 (Clears N.)
855 * BRA XPLOP2 go steal other guy's code!
858 * ( n --- ) ( limit index *** limit index+n ) C
859 * ( limit index *** )
860 * Loop with a variable increment. Terminates when the index
861 * crosses the boundary from one below the limit to the limit. A
862 * positive n will cause termination if the result index equals the
863 * limit. A negative n must cause the index to become less than
864 * the limit to cause loop termination.
866 * Note that the end conditions are not symmetric around zero.
868 * In native processor code, there should be a better way, use that instead.
869 * More specifically, DO NOT CALL THIS from assembly language code.
870 * This is only for Forth code stream.
871 * Also, see comments for LIT.
873 FCC '(+LOOP' ; '(+LOOP)'
876 XPLOOP FDB *+NATWID ; Borrowing from BIF-6809.
878 BPL XLOOPA ; Steal plain loop code for forward count.
879 ADDD NATWID,S ; Dodge the return address
883 BRA XLOOPN ; This path is less time-sensitive.
885 * This should work, but I want to use tested code.
886 * PULU A,B ; Get the increment.
887 * XPLOP2 PULS X ; Pre-clear the return stack.
888 * PSHU A ; Save the direction in high bit.
891 * SUBD NATWID,S ; Check limit.
893 ** I think this should work:
894 * EORA ,U+ ; dir < 0 and (count - limit) >= 0
895 * BPL XPLONO ; or dir >= 0 and (count - limit) < 0
897 * LEAY D,Y ; IP is postinc
899 * XPLONO LEAS 2*NATWID,S
900 * JMP ,X ; synthetic return
902 * This definitely should work:
903 * TST ,U+ ; Get the sign
908 * LEAY D,Y ; IP is postinc
910 * XPLOF CMPD NATWID,S
912 * XPLONO LEAS 2*NATWID,S
913 * JMP ,X ; synthetic return
915 * 6800 Probably could have used the exclusive-or method, too.:
916 * PULS A ; get increment
919 * BPL XPLOF forward looping
925 * BRA XPLONO fall through
929 * ADDB 3,X add it to counter
931 * STB 3,X store new counter value
940 * XPLONO LEAX 1,X ; done, don't branch back
945 * BRA ZBNO use ZBRAN to skip over unused delta
947 * ######>> screen 17 <<
949 * ( limit index --- ) ( *** limit index )
950 * Move the loop parameters to the return stack. Synonym for D>R.
955 XDO FDB *+NATWID This is the RUNTIME DO, not the COMPILING DO
956 LDX ,S ; Save the return address.
959 PULU A,B ; Maintain order.
961 JMP ,X ; synthetic return
980 * ( --- index ) ( limit index *** limit index )
981 * Copy the loop index from the return stack. Synonym for R.
986 LDD NATWID,S ; Dodge return address.
994 * ######>> screen 18 <<
996 * ( c base --- false )
997 * ( c base --- n true )
998 * Translate C in base, yielding a translation valid flag. If the
999 * translation is not valid in the specified base, only the false
1002 FCC 'DIGI' ; 'DIGIT'
1005 DIGIT FDB *+NATWID NOTE: legal input range is 0-9, A-Z
1006 LDD NATWID,U ; Check the whole thing.
1007 SUBD #$30 ; ascii zero
1008 BMI DIGIT2 IF LESS THAN '0', ILLEGAL
1010 BMI DIGIT0 IF '9' OR LESS
1012 BMI DIGIT2 if less than 'A'
1014 BPL DIGIT2 if greater than 'Z'
1015 SUBD #7 translate 'A' thru 'F'
1016 DIGIT0 CMPD ,U ; Check the base.
1017 BPL DIGIT2 if not less than the base
1018 STD NATWID,U ; Store converted digit. (High byte known zero.)
1019 LDD #1 ; set valid flag
1020 DIGIT1 STD ,U ; store the flag
1022 DIGIT2 LDD #0 ; set not valid flag
1023 LEAU NATWID,U ; pop base
1027 * SUBA #$30 ascii zero
1028 * BMI DIGIT2 IF LESS THAN '0', ILLEGAL
1030 * BMI DIGIT0 IF '9' OR LESS
1032 * BMI DIGIT2 if less than 'A'
1034 * BPL DIGIT2 if greater than 'Z'
1035 * SUBA #7 translate 'A' thru 'F'
1037 * BPL DIGIT2 if not less than the base
1039 * STA 3,X store digit
1040 * DIGIT1 STB 1,X store the flag
1044 * LEAS 1,S ; pop bottom number
1046 * STB 0,X make sure both bytes are 00
1049 * ######>> screen 19 <<
1051 * The word definition format in the dictionary:
1053 * (Symbol names are bracketed by bytes with the high bit set, rather than linked.)
1055 * NFA (name field address):
1056 * char-count + $80 Length of symbol name, flagged with high bit set.
1057 * char 1 Characters of symbol name.
1060 * char n + $80 symbol termination flag (char set < 128 code points)
1061 * LFA (link field address):
1062 * link high byte \___pointer to previous word in list
1063 * link low byte / -- Combined allocation/dictionary list. --
1064 * CFA (code field address):
1065 * CFA high byte \___pointer to native CPU machine code
1066 * CFA low byte / -- Consider this the characteristic code. --
1067 * PFA (parameter field address):
1068 * parameter fields -- Machine code for low-level native machine CPU code,
1069 * " instruction list for high-level Forth code,
1070 * " constant data for constants, pointers to per task variables,
1071 * " space for variables, for global variables, etc.
1073 * In the case of native CPU machine code, the address at CFA will be PFA.
1075 * Definition attributes:
1076 FIMMED EQU $40 ; Immediate word flag.
1077 FSMUDG EQU $20 ; Smudged => definition not ready.
1078 CTMASK EQU ($FF&(^($80|FIMMED))) ; For unmasking the length byte.
1079 * Note that the SMUDGE bit is not masked out.
1081 * But we really want more (Thinking for a new model, need one more byte):
1082 * FCOMPI EQU $10 ; Compile-time-only.
1083 * FASSEM EQU $08 ; Assembly-language code only.
1084 * F4THLV EQU $04 ; Must not be called from assembly language code.
1085 * These would require some significant adjustments to the model.
1086 * We also want to put the low-level VM stuff in its own vocabulary.
1089 * (FIND) ( name vocptr --- locptr length true )
1090 * ( name vocptr --- false )
1091 * Search vocabulary for a symbol called name.
1092 * name is a pointer to a high-bit bracket string with length head.
1093 * vocptr is a pointer to the NFA of the tail-end (LATEST) definition
1094 * in the vocabulary to be searched.
1095 * Hidden (SMUDGEd) definitions are lexically not equal to their name strings.
1097 FCC '(FIND' ; '(FIND)'
1101 PSHS Y ; Have to track two pointers.
1102 * Use the stack and registers instead of temp area N.
1103 PA0 EQU NATWID ; pointer to the length byte of name being searched against
1104 PD EQU 0 ; pointer to NFA of dict word being checked
1108 LDX PD,U ; Start in on the vocabulary (NFA).
1109 PFNDLP LDY PA0,U ; Point to the name to check against.
1110 LDB ,X+ ; get dict name length byte
1111 TFR B,A ; Save it in case it matches.
1114 CMPB ,Y+ ; Compare lengths
1118 TSTB ; ; Is high bit of character in dictionary entry set?
1122 ANDB #$7F ; Clear high bit from dictionary.
1123 CMPB ,Y+ ; Compare "last" characters.
1125 BEQ FOUND ; Matches even if dictionary actual length is shorter.
1126 PFNDLN LDX ,X++ ; Get previous link in vocabulary.
1128 BNE PFNDLP ; Continue if link not=0
1131 LEAU NATWID,U ; Return only false flag.
1138 PFNDCH CMPB ,Y+ ; Compare characters.
1142 PFNDSC LDB ,X+ ; scan forward to end of this name in dictionary
1150 FOUND LEAX 2*NATWID,X
1164 * NOP ; Probably leftovers from a debugging session.
1166 * PD EQU N ptr to dict word being checked
1172 * PFIND0 PULS A ; loop to get arguments
1179 * PFNDLP LDB 0,X get count dict count
1185 * LDA 0,X get count from arg
1187 * STX PA intialize PA
1188 * PSHS B ; ** emulating CBA:
1189 * CMPA ,S+ ; compare lengths
1199 * TSTB ; is dict entry neg. ?
1201 * ANDB #$7F clear sign
1202 * PSHS B ; ** emulating CBA:
1205 * PFNDLN LDX 0,X get new link
1206 * BNE PFNDLP continue if link not=0
1213 * PFNDCH PSHS B ; ** emulating CBA:
1217 * PFNDSC LDB 0,X scan forward to end of this name
1224 * FOUND LDA PD compute CFA
1237 * PSHS A ; Left over from a stray copy-paste, I guess.
1243 * ######>> screen 20 <<
1245 * ( buffer ch --- buffer symboloffset delimiteroffset scancount )
1246 * ( buffer ch --- buffer symboloffset nuloffset scancount ) ( Scan count == nuloffset )
1247 * ( buffer ch --- buffer nuloffset onepast scancount )
1248 * Scan buffer for a symbol delimited by ch or ASCII NUL,
1249 * return the length of the buffer region scanned,
1250 * the offset to the trailing delimiter,
1251 * and the offset of the first character of the symbol.
1252 * Leave the buffer on the stack.
1253 * Scancount is also offset to first character not yet looked at.
1254 * If no symbol in buffer, scancount and symboloffset point to NUL
1255 * and delimiteroffset points one beyond for some reason.
1256 * On trailing NUL, delimiteroffset == scancount.
1257 * (Buffer is the address of the buffer array to scan.)
1258 * (This is a bit too tricky, really.)
1260 FCC 'ENCLOS' ; 'ENCLOSE'
1264 LDA 1,U ; Delimiter character to match against in A.
1265 LDX NATWID,U ; Buffer to scan in.
1266 CLRB ; Initialize offset. (Buffer < 256 wide!)
1267 * Scan to a non-delimiter or a NUL
1268 ENCDEL TST B,X ; NUL ?
1270 CMPA B,X ; Delimiter?
1272 INCB ; count character
1274 * Found first character. Save the offset.
1275 ENC1ST STB 1,U ; Found first non-delimiter character --
1276 CLR ,U ; store the count, zero high byte.
1277 * Scan to a delimiter or a NUL
1278 ENCSYM TST B,X ; NUL ?
1280 CMPA B,X ; delimiter?
1284 * Found end of symbol. Push offset to delimiter found.
1285 ENCEND CLRA ; high byte -- buffer < 255 wide!
1286 PSHU A,B ; Offset to seen delimiter.
1287 * Advance and push address of next character to check.
1288 ADDD #1 ; In case offset was 255.
1291 * Found NUL before non-delimiter, therefore there is no word
1292 ENCNUL CLRA ; high byte -- buffer < 255 wide!
1293 STD ,U ; offset to NUL.
1294 ADDD #1 ; Point after NUL to allow (FIND) to match it.
1296 SUBD #1 ; Next is not passed NUL.
1297 PSHU A,B ; Stealing code will save only one byte.
1299 * Found NUL following the word instead of delimiter.
1304 PSHU A,B ; Save offset to first after symbol (NUL)
1306 PSHU A,B ; and count scanned.
1311 * FC means offset (bytes) to First Character of next word
1312 * EW " " to End of Word
1313 * NC " " to Next Character to start next enclose at
1314 * ENCLOS FDB *+NATWID
1316 * PULS B ; now, get the low byte, for an 8-bit delimiter
1320 * * wait for a non-delimiter or a NUL
1323 * PSHS B ; ** emulating CBA:
1324 * CMPA ,S+ ; CHECK FOR DELIM
1329 * * found first character. Push FC
1330 * ENC1ST LDA N found first char.
1334 * wait for a delimiter or a NUL
1337 * PSHS B ; ** emulating CBA:
1338 * CMPA ,S+ ; ckech for delim.
1343 * * found EW. Push it
1348 * * advance and push NC
1351 * found NUL before non-delimiter, therefore there is no word
1352 * ENCNUL LDB N found NUL
1356 * BRA ENC0TR+2 ; ********** POTENTIAL BUG HERE *******
1357 * ******** Should use labels in case opcodes change! ********
1358 * found NUL following the word instead of SPACE
1362 * ENCL8 LDB N save NC
1367 * ######>> screen 21 <<
1368 * The next 4 words call system dependant I/O routines
1369 * which are listed after word "-->" ( lable: "arrow" )
1370 * in the dictionary.
1374 * Write c to the output device (screen or printer).
1375 * ROM Uses the ECB device number at address $6F,
1376 * -2 is printer, 0 is screen.
1383 LBSR PEMIT ; PEMIT expects the character in D.
1392 * INC XOUT+1-UORIG,X
1394 * ****WARNING**** HARD OFFSET: *+4 ****
1401 * Wait for a key from the keyboard.
1402 * If the key is BREAK, set the high byte (result $FF03).
1408 LBSR PKEY ; PKEY leaves the key/break code in D.
1419 * Scan keyboard, but do not wait.
1420 * Return 0 if no key,
1421 * BREAK ($ff03) if BREAK is pressed,
1422 * or key currently pressed.
1424 FCC '?TERMINA' ; '?TERMINAL'
1428 LBSR PQTER ; PQTER leaves the flag/key in D.
1433 * JMP PUSHBA stack the flag
1437 * EMIT a Carriage Return (ASCII CR).
1443 LBRA PCR ; Nothing really to do here.
1447 * ######>> screen 22 <<
1449 * ( source target count --- )
1450 * Copy/move count bytes from source to target.
1451 * Moves ascending addresses,
1452 * so that overlapping only works if the source is above the destination.
1454 FCC 'CMOV' ; 'CMOVE' : source, destination, count
1477 * One way: ; takes ( 37+17*count+9*(count/256) cycles )
1478 * PSHS Y ; #2~7 ; Gotta have our pointers.
1481 * PULU D,X,Y ; #2~11
1482 * PSHS A ; #2~6 ; Gotta have our pointers.
1495 * PULS A,Y,PC ; #2~10
1496 * Another way ; takes ( 42+17*count+9*(count/256) cycles )
1498 * SUBD ,U++ ; #2~9 ; invert the count
1510 * PULS A,Y,PC ; #2~10
1511 * Yet another way ; takes ( 37+29*count cycles )
1513 * LDX NATWID,U ; #2~6
1514 * LDY NATWID,U ; #3~7
1524 * LEAU 3*NATWID,U ; #2~5
1526 * Yet another way ; takes ( 44+24*odd+33*count/2 cycles )
1528 * LDX NATWID,U ; #2~6
1529 * LDY 2*NATWID,U ; #3~7
1547 * LEAU 3*NATWID,U ; #2~5
1549 * From the 6800 model:
1550 * CMOVE FDB *+2 takes ( 43+47*count cycles ) on 6800
1554 * STA 0,X move parameters to scratch area
1576 * ######>> screen 23 <<
1579 * Multiplies the top two unsigned integers,
1580 * yielding a double integer product.
1587 LDA 2*NATWID+1,U ; least
1591 LDA 2*NATWID,U ; most
1595 LDD 2*NATWID+1,U ; first inner (u2 lo, u1 hi)
1601 LDA 2*NATWID,U ; second inner (u2 hi)
1602 LDB 3*NATWID,U ; (u1 lo)
1619 * The following is a subroutine which
1620 * multiplies top 2 words on stack,
1621 * leaving 32-bit result: high order word in A,B
1622 * low order word in 2nd word of stack.
1624 * USTARS LDA #16 bits/word counter
1629 * USTAR2 ROR 5,X shift multiplier
1637 * RORB ; shift result
1639 * USTAR4 LEAS 1,S ; dump counter
1642 * ######>> screen 24 <<
1644 * ( ud u --- uremainder uquotient )
1645 * Divides the top unsigned integer
1646 * into the second and third words on the stack
1647 * as a single unsigned double integer,
1648 * leaving the remainder and quotient (quotient on top)
1649 * as unsigned integers.
1651 * The smaller the divisor, the more likely dropping the high word
1652 * of the quotient loses significant bits. See M/MOD .
1661 LDD NATWID,U ; dividend
1662 USLDIV CMPD ,U ; divisor
1664 ANDCC #~1 ; carry clear
1667 ORCC #1 ; quotient, (carry set)
1668 USLBIT ROL 2*NATWID+1,U ; save it
1680 PULS A,PC ; Avoiding a LEAS 1,S by discarding A.
1693 * USL2 ANDCC #~$01 ; CLC :
1711 * JMP SWAP+4 reverse quotient & remainder
1713 * ######>> screen 25 <<
1716 * Bitwise and the top two integers.
1736 * Bitwise or the top two integers.
1756 * Bitwise exclusive or the top two integers.
1774 * ######>> screen 26 <<
1777 * Fetch the parameter stack pointer (before it is pushed).
1778 * This points at whatever was on the top of stack before.
1788 * STX N scratch area
1793 * ( whatever --- nothing )
1794 * Initialize the parameter stack pointer from the USER variable S0.
1795 * Effectively clears the stack.
1804 * LDX XSPZER-UORIG,X
1805 * TFR X,S ; TXS : watch it ! X and S are not equal on 6800.
1808 * ( whatever *** nothing )
1809 * Initialize the return stack pointer from the initialization table
1810 * instead of the user variable R0, for some reason.
1811 * Quite possibly, this should be from R0.
1812 * Effectively aborts all in process definitions, except the active one.
1813 * An emergency measure, to be sure.
1814 * The routine that calls this must never execute a return.
1815 * So this should never be executed from the terminal, I guess.
1816 * This is another that should be compile-time only, and in a separate vocabulary.
1822 PULS X ; But this guy has to return to his caller.
1825 * LDX RINIT initialize from rom constant
1831 * Pop IP from return stack (return from high-level definition).
1832 * Can be used in a screen to force interpretion to terminate.
1833 * Must not be executed when temporaries are saved on top of the return stack.
1839 PULS D,Y ; return address in D, and saved IP in Y.
1840 TFR D,PC ; Synthetic return.
1847 * LDX 0,X get address we have just finished.
1848 * JMP NEXT+2 increment the return address & do next word
1850 * ######>> screen 27 <<
1852 * ( limit index *** index index )
1853 * Force the terminating condition for the innermost loop by
1854 * copying its index to its limit.
1855 * Termination is postponed until the next
1856 * LOOP or +LOOP instruction is executed.
1857 * The index remains available for use until
1858 * the LOOP or +LOOP instruction is encountered.
1859 * Note that the assumption is that the current count is the correct count
1860 * to end at, rather than pushing the count to the final count.
1862 FCC 'LEAV' ; 'LEAVE'
1866 LDD NATWID,S ; Dodge the return address.
1879 * Move top of parameter stack to top of return stack.
1887 STD ,S ; Put it where the return address was.
1902 * Move top of return stack to top of parameter stack.
1922 * Copy the top of return stack to top of parameter stack.
1934 * ######>> screen 28 <<
1937 * Logically invert top of stack;
1938 * or flag true if top is zero, otherwise false.
1956 *ZEQU2 TFR S,X ; TSX :
1961 * Flag true if top is negative (MSbit set), otherwise false.
1974 * LDA #$80 check the sign bit
1983 * ######>> screen 29 <<
1985 * ( n1 n2 --- n1+n2 )
1986 * Add top two words.
2003 * ( d1 d2 --- d1+d2 )
2004 * Add top two double integers.
2020 * ANDCC #~$01 ; CLC :
2036 * Negate (two's complement) top of stack.
2038 FCC 'MINU' ; 'MINUS'
2047 * from 6800 model code:
2058 * Negate (two's complement) top two words on stack as a double integer.
2060 FCC 'DMINU' ; 'DMINUS'
2065 SUBD NATWID,U ; #2~7
2085 * ######>> screen 30 <<
2087 * ( n1 n2 --- n1 n2 n1 )
2088 * Push a copy of the second word on stack.
2104 * Discard the top word on stack.
2117 * ( n1 n2 --- n2 n1 )
2118 * Swap the top two words on stack.
2142 * Push a copy of the top word on stack.
2157 * ######>> screen 31 <<
2160 * Add the second word on stack to the word at the adr on top of stack.
2175 * PULS A ; get stack data
2177 * ADDB 1,X add & store low byte
2179 * ADCA 0,X add & store hi byte
2185 * Exclusive or byte at adr with low byte of top word.
2187 FCC 'TOGGL' ; 'TOGGLE'
2195 * Using the model code would be less likely to introduce bugs,
2196 * but that would sort-of defeat my purposes here.
2197 * Anyway, I can borrow from theoretically known good bif-6809 code
2198 * and it's fewer bytes and much faster code this way.
2200 * FDB DOCOL,OVER,CAT,XOR,SWAP,CSTORE
2203 * ######>> screen 32 <<
2206 * Replace address on stack with the word at the address.
2215 * LDX 0,X get address
2222 * Replace address on top of stack with the byte at the address.
2223 * High byte of result is clear.
2245 * Store second word on stack at address on top of stack.
2255 * LDX 0,X get address
2262 * Store low byte of second word on stack at address on top of stack.
2263 * High byte is ignored.
2274 * LDX 0,X get address
2283 * ######>> screen 33 <<
2286 * { : name sundry-activities ; } typical input
2287 * If executing (not compiling),
2288 * record the data stack mark in CSP,
2289 * Set the CONTEXT vocabulary to CURRENT,
2291 * set state to compile,
2292 * and compile the call to the trailing native CPU machine code DOCOL.
2294 * This would not be hard to flatten to native code.
2295 * But that's not the purpose of a model.
2299 COLON FDB DOCOL,QEXEC,SCSP,CURENT,AT,CONTXT,STORE
2303 * Here is the IP pusher for allowing
2304 * nested words in the virtual machine:
2305 * ( ;S is the equivalent un-nester )
2308 * Characteristic of a colon (:) definition.
2309 * Begins execution of a high-level definition,
2310 * i. e., nests the definition and begins processing icodes.
2311 * Mechanically, it pushes the IP (Y register)
2312 * and loads the Parameter Field Address of the definition which
2313 * called it into the IP.
2314 DOCOL LDD ,S ; Save the return address.
2315 STY ,S ; Nest the old IP.
2316 LEAY NATWID,X ; W still in X, bump to parameters, load as new IP.
2317 TFR D,PC ; synthetic return to interpret.
2319 * DOCOL LDX RP make room in the stack
2325 * STA 2,X Store address of the high level word
2326 * STB 3,X that we are starting to execute
2327 * LDX W Get first sub-word of that definition
2328 * JMP NEXT+2 and execute it
2332 * { : name sundry-activities ; } typical input
2333 * ERROR check data stack against mark in CSP,
2335 * unSMUDGE LATEST definition,
2336 * and set state to interpretation.
2337 FCB $C1 ; imnediate code
2340 SEMI FDB DOCOL,QCSP,COMPIL,SEMIS,SMUDGE,LBRAK
2343 * ######>> screen 34 <<
2346 * { value CONSTANT name } typical input
2349 * compile the constant value,
2350 * and compile the call to the trailing native CPU machine code DOCON.
2352 FCC 'CONSTAN' ; 'CONSTANT'
2355 CON FDB DOCOL,CREATE,SMUDGE,COMMA,PSCODE
2357 * Characteristic of a CONSTANT.
2358 * A CONSTANT simply loads its value from its parameter field
2359 * and pushes it on the stack.
2360 DOCON LDD NATWID,X ; Get the first natural width word of the parameter field.
2365 * LDB 3,X A & B now contain the constant
2368 * Not in model, needed for abstraction:
2370 * The byte width of objects on stack.
2372 FCC 'NATWI' ; 'NATWID'
2378 * Not in model, needed for abstraction:
2379 * Note that this is not defined as an INCREMENTER!
2380 * Coded to increment by the exact constant returned by NATWID
2381 * ( n --- n+NATWID )
2388 ADDD NATWCV,PCR ; Looking ahead, does not have to be PCRelative.
2391 * How this might have been done for 6800 model:
2392 * CLRA ; We know the natural width is less than 255, LOL.
2401 * { init VARIABLE name } typical input
2402 * Use CONSTANT to CREATE a header and compile the initial value, init,
2403 * then overwrite the characteristic to point to DOVAR.
2405 FCC 'VARIABL' ; 'VARIABLE'
2408 VAR FDB DOCOL,CON,PSCODE
2410 * Characteristic of a VARIABLE.
2411 * A VARIABLE pushes its PFA address on the stack.
2412 * The parameter field of a VARIABLE is the actual allocation of the variable,
2413 * so that pushing its address allows its contents to be @ed (fetched).
2414 * Ordinary arrays and strings that do not subscript themselves
2415 * may be allocated by defining a variable
2416 * and immediately ALLOTting the remaining needed space.
2417 * VARIABLES are global to all users,
2418 * and thus should be hidden in resource monitors, but aren't.
2419 DOVAR LEAX NATWID,X ; Point to the first natural width word of the parameters.
2425 * ADCA #0 A,B now contain the address of the variable
2430 * { uboffset USER name } typical input
2431 * CREATE a header and compile the unsigned byte offset in the per-USER table,
2432 * then overwrite the header with a call to DOUSER.
2433 * The USER is entirely responsible for maintaining allocation!
2438 USER FDB DOCOL,CON,PSCODE
2440 * Characteristic of a per-USER variable.
2441 * USER variables are similiar to VARIABLEs,
2442 * but are allocated (by hand!) in the per-user table.
2443 * A USER variable's parameter field contains its offset in the per-user table.
2444 DOUSER TFR DP,A ; Make a pointer to the direct page.
2446 * See Alternative -- alternatives start from this point.
2447 ADDD NATWID,X ; Add it to the offset to the per-user variable.
2449 TFR D,X ; Cache the pointer in X for the caller.
2451 * Hey, the per-user table could actually be larger than 256 bytes!
2452 * But we knew that. It's just not as esthetic to calculate it this way.
2454 * LDX NATWID,X ; Keep the offset
2455 * EXG D,X ; Prepare for EA
2460 * PSHS Y ; Get Y free for calculations.
2461 * TFR D,Y ; Y points to the UP base
2462 * LDD NATWID,X ; Get the offset
2463 * LEAX D,Y ; Leave the pointer cached in X.
2467 * From the 6800 model:
2468 * DOUSER LDX W get offset into user's table
2471 * ADDB UP+1 add to users base address
2473 * JMP PUSHBA push address of user's variable
2475 * ######>> screen 35 <<
2510 * ASCII SPACE character
2515 BL FDB DOCON ascii blank
2519 * This really shouldn't be a CONSTANT.
2521 * The base of the disk buffer space.
2523 FCC 'FIRS' ; 'FIRST'
2528 * FDB MEMEND-528 (132 * NBLK)
2531 * This really shouldn't be a CONSTANT.
2533 * The limit of the disk buffer space.
2535 FCC 'LIMI' ; 'LIMIT' : ( the end of memory +1 )
2540 * In 6800 model, was
2544 * ( --- sectorsize )
2545 * The size, in bytes, of a buffer control region.
2547 FCC 'B/CTL' ; 'B/CTL' : (bytes/control region)
2553 * ( --- sectorsize )
2554 * The size, in bytes, of a buffer.
2556 FCC 'B/BU' ; 'B/BUF' : (bytes/buffer)
2561 * Hardcoded in 6800 model:
2565 * ( --- blocksperscreen )
2566 * The size, in blocks, of a screen.
2567 * Should this be the same as NBLK, the number of block buffers maintained?
2569 FCC 'B/SC' ; 'B/SCR' : (blocks/screen)
2574 * Hardcoded in 6800 model as:
2576 * blocks/screen = 1024 / "B/BUF" = 8, if sectors are 128 bytes.
2580 * Calculate the address of entry (#n/2) in the boot-up parameter table.
2581 * (Adds the base of the boot-up table to n.)
2583 FCC '+ORIGI' ; '+ORIGIN'
2586 PORIG FDB DOCOL,LIT,ORIG,PLUS
2589 * ######>> screen 36 <<
2592 * This is the per-task variable recording the initial parameter stack pointer.
2602 * This is the per-task variable recording the initial return stack pointer.
2612 * Terminal Input Buffer address.
2613 * Note that this is a variable, so users may allocate their own buffers, but it must be @ed.
2622 * ( --- maxnamewidth )
2623 * This is the maximum width to which symbol names will be recorded.
2625 FCC 'WIDT' ; 'WIDTH'
2633 * Availability of error messages on disk.
2634 * Contains 1 if messages available,
2636 * -1 if a disk error has occurred.
2638 FCC 'WARNIN' ; 'WARNING'
2646 * Boundary for FORGET.
2648 FCC 'FENC' ; 'FENCE'
2656 * Dictionary pointer, fetched by HERE.
2658 FCC 'D' ; 'DP' : points to first free byte at end of dictionary
2665 * ( --- vadr ) ******* Need to check what this is!
2666 * Used in maintaining vocabularies.
2667 * I think it points to the "parent" vocabulary, but I'm not sure.
2668 * Or maybe this is the CONTEXT vocabulary. I'll have to come back here. *****
2670 FCC 'VOC-LIN' ; 'VOC-LINK'
2678 * Disk block being interpreted.
2679 * Zero refers to terminal.
2680 * ******** Should be made a 32 bit user variable! ********
2681 * But the base system needs to have full 32 bit support, div and mul, etc.
2682 * before we can do that.
2692 * Input buffer offset/cursor.
2694 FCC 'I' ; 'IN' : scan pointer for input line buffer
2702 * Output buffer offset/cursor.
2712 * Screen currently being edited, once we have an editor running.
2719 * ######>> screen 37 <<
2723 * Sector offset for LOADing screens,
2724 * set by DRIVE to make a new drive the default.
2725 * This should also be 32 bit or bigger.
2727 FCC 'OFFSE' ; 'OFFSET'
2735 * Current context of interpretation (vocabulary root).
2737 FCC 'CONTEX' ; 'CONTEXT' : points to pointer to vocab to search first
2745 * Current context of definition (vocabulary root).
2747 FCC 'CURREN' ; 'CURRENT' : points to ptr. to vocab being extended
2755 * Compiler/interpreter state.
2757 FCC 'STAT' ; 'STATE' : 1 if compiling, 0 if not
2765 * Numeric conversion base.
2767 FCC 'BAS' ; 'BASE' : number base for all input & output
2775 * Decimal point location for output.
2785 * Field width for I/O formatting.
2795 * Compiler stack mark for stack check.
2805 * Editing cursor location.
2815 * Pointer to last HELD character in PAD.
2823 * ======>> 82.5 <<== SPECIAL
2825 * Line width of active terminal.
2827 FCC 'COLUMN' ; 'COLUMNS' : line width of terminal
2833 * ######>> screen 38 <<
2835 ** An INCREMENTER probably should not be defined without a defined CONSTANT?
2837 ** Make an INCREMENTER compiling word (not in model):
2839 ** { n INCREMENTER name } typical input
2840 ** CREATE a header and compile the increment constant,
2841 ** then overwrite the header with a call to DOINC.
2843 * FCC 'INCREMENTE' ; 'INCREMENTER'
2846 * INCR FDB DOCOL,CON,PSCODE
2848 ** Characteristic of an INCREMENTER.
2849 ** This is too naive:
2851 * ADDD NATWID,X ; Add the increment.
2854 * Compiling word should check that it is compiling a CONSTANT.
2862 * Using the model keeps things semantically connected for other processors:
2863 ONEP FDB DOCOL,ONE,PLUS
2865 ** Greedy alternative:
2871 * Naive alternative:
2874 * Naive alternative:
2877 * ADDD #1 ; It's hard to imagine 1+ being other than 1.
2887 * Using the model keeps things semantically connected for other processors:
2888 TWOP FDB DOCOL,TWO,PLUS
2890 ** Greedy alternative:
2893 * ADDD TWOV,PCR ; See NAT+ (NATP)
2896 * Naive alternative:
2899 * Naive alternative:
2902 * ADDD #2 ; See NAT+ (NATP)
2908 * Get the DICTPT allocation, like a USER constant.
2909 * Should check the stack and heap for collision.
2914 HERE FDB DOCOL,DICTPT,AT
2919 * Increase/decrease heap (add n to DP),
2920 * Should ERROR check stack/heap.
2922 FCC 'ALLO' ; 'ALLOT'
2925 ALLOT FDB DOCOL,DICTPT,PSTORE
2930 * Store word n at DP++,
2931 * Should ERROR check stack/heap.
2935 COMMA FDB DOCOL,HERE,STORE,NATWC,ALLOT
2937 * COMMA FDB DOCOL,HERE,STORE,TWO,ALLOT
2942 * Store byte b at DP+,
2943 * Should ERROR check stack/heap.
2948 CCOMM FDB DOCOL,HERE,CSTORE,ONE,ALLOT
2952 * ( n1 n2 --- n1-n2 )
2953 * Subtract top two words.
2962 * SUB FDB DOCOL,MINUS,PLUS
2963 * FDB SEMIS ; Costs 6 bytes and lots of cycles.
2966 * ( n1 n2 --- n1==n2 )
2967 * Return flag true if n1 and n2 are equal, otherwise false.
2971 EQUAL FDB DOCOL,SUB,ZEQU
2975 * ( n1 n2 --- n1<n2 )
2976 * Return flag true if n1 is less than n2, otherwise false.
2997 * CMPB 1,X ; Why not sub, sbc, bge?
3007 * ( n1 n2 --- n1>n2 )
3008 * Return flag true if n1 is greater than n2, false otherwise.
3012 GREAT FDB DOCOL,SWAP,LESS
3016 * ( n1 n2 n3 --- n2 n3 n1 )
3017 * Rotate the top three words on stack,
3018 * bringing the third word to the top.
3029 * ROT FDB DOCOL,TOR,SWAP,FROMR,SWAP
3036 FCC 'SPAC' ; 'SPACE'
3039 SPACE FDB DOCOL,BL,EMIT
3043 * ( n0 n1 --- min(n0,n1) )
3044 * Leave the minimum of the top two integers.
3045 * Being too greedy here, but, whatever.
3056 * MIN FDB DOCOL,OVER,OVER,GREAT,ZBRAN
3063 * ( n0 n1 --- max(n0,n1) )
3064 * Leave the maximum of the top two integers.
3065 * Really should leave this as in the model.
3076 * MAX FDB DOCOL,OVER,OVER,LESS,ZBRAN
3095 * DDUP FDB DOCOL,DUP,ZBRAN
3096 * FDB DDUP2-*-NATWID
3100 * ######>> screen 39 <<
3105 * Change top integer to its sign.
3107 FCC 'SIGNU' ; 'SIGNUM'
3115 SIGNUP SEX ; Couldn't they have called SignEXtend EXT instead?
3116 STD ,U ; Am I too much of a prude?
3118 * 6800 model version should be something like this:
3129 * ( adr1 direction --- adr2 )
3130 * TRAVERSE the symbol name.
3131 * If direction is 1, find the end.
3132 * If direction is -1, find the beginning.
3134 FCC 'TRAVERS' ; 'TRAVERSE'
3138 BSR SIGNUE ; Convert negative to -, zero or positive to 1.
3139 LDD ,U++ ; Still in D, but we have to pop it anyway.
3140 LDX ,U ; If D is 1 or -1, so is B.
3142 TRAVLP LEAX B,X ; Don't look at the one we start at.
3143 CMPA ,X ; Not sure why we aren't just doing LDA ,X ; BPL.
3147 * Doing this in 6809 just because it can be done may be getting too greedy.
3148 * TRAV FDB DOCOL,SWAP
3149 * TRAV2 FDB OVER,PLUS,LIT8
3151 * FDB OVER,CAT,LESS,ZBRAN
3152 * FDB TRAV2-*-NATWID
3158 * Fetch CURRENT as a per-USER constant.
3160 FCC 'LATES' ; 'LATEST'
3163 LATEST FDB DOCOL,CURENT,AT,AT
3165 * LATEST FDB *+NATWID
3166 * Getting too greedy:
3171 * LDD CURENT+NATWID,PCR
3173 * PSHU X ; Leave the address in X.
3182 * Too greedy, too many smantic holes to fall through.
3183 * If the address at the CFA is made relative,
3184 * this is part of the code that would be affected
3185 * if it is in native CPU code.
3188 * Wanted to do these as INCREMENTERs,
3189 * but I need to stick with the model as much as possible,
3190 * (mostly, LOL) adding code only to make the model more clear.
3192 * Convert PFA to LFA, unchecked. (Bump back from contents to allocation link.)
3205 * Convert PFA to CFA, unchecked. (Bump back from contents to characterist code link.)
3210 * CFA FDB DOCOL,TWO,SUB
3211 CFA FDB DOCOL,NATWC,SUB
3216 * Convert PFA to NFA. (Bump back from contents to beginning of symbol name.)
3224 FDB SUB,ONE,MINUS,TRAV
3229 * Convert NFA to PFA. (Bump up from beginning of symbol name to contents.)
3234 PFA FDB DOCOL,ONE,TRAV,LIT8
3240 * ######>> screen 40 <<
3243 * Save the parameter stack pointer in CSP for compiler checks.
3248 SCSP FDB DOCOL,SPAT,CSP,STORE
3252 * ( 0 n --- ) ( *** )
3253 * ( true n --- IN BLK ) ( anything *** nothing )
3254 * If flag is false, do nothing.
3255 * If flag is true, issue error MESSAGE and QUIT or ABORT, via ERROR.
3256 * Leaves cursor position (IN)
3257 * and currently loading block number (BLK) on stack, for analysis.
3259 * This one is too important to be high-level Forth codes.
3260 * When we have an error, we want to disturb as little as possible.
3261 * But fixing that cascades through ERROR and MESSAGE
3262 * into the disk block system.
3263 * And we aren't ready for that yet.
3265 FCC '?ERRO' ; '?ERROR'
3273 ** this doesn't work anyway: QERROR LBR ERROR
3274 QERR FDB DOCOL,SWAP,ZBRAN
3282 * STATE is compiling:
3284 * STATE is compiling:
3285 * ( --- IN BLK ) ( anything *** nothing )
3286 * ERROR if not compiling.
3288 FCC '?COM' ; '?COMP'
3291 QCOMP FDB DOCOL,STATE,AT,ZEQU,LIT8
3297 * STATE is executing:
3299 * STATE is executing:
3300 * ( --- IN BLK ) ( anything *** nothing )
3301 * ERROR if not executing.
3303 FCC '?EXE' ; '?EXEC'
3306 QEXEC FDB DOCOL,STATE,AT,LIT8
3312 * ( n1 n1 --- ) ( *** )
3313 * ( n1 n2 --- IN BLK ) ( anything *** nothing )
3314 * ERROR if top two are unequal.
3315 * MESSAGE says compiled conditionals do not match.
3317 FCC '?PAIR' ; '?PAIRS'
3320 QPAIRS FDB DOCOL,SUB,LIT8
3326 * CSP and parameter stack are balanced (equal):
3328 * CSP and parameter stack are not balanced (unequal):
3329 * ( --- IN BLK ) ( anything *** nothing )
3330 * ERROR if return/control stack is not at same level as last !CSP.
3331 * Usually indicates that a definition has been left incomplete.
3336 QCSP FDB DOCOL,SPAT,CSP,AT,SUB,LIT8
3344 * No active BLK input:
3345 * ( --- IN BLK ) ( anything *** nothing )
3346 * ERROR if not loading, i. e., if BLK is zero.
3348 FCC '?LOADIN' ; '?LOADING'
3351 QLOAD FDB DOCOL,BLK,AT,ZEQU,LIT8
3356 * ######>> screen 41 <<
3359 * Compile an in-line literal value from the instruction stream.
3361 FCC 'COMPIL' ; 'COMPILE'
3364 * COMPIL FDB DOCOL,QCOMP,FROMR,TWOP,DUP,TOR,AT,COMMA
3365 * COMPIL FDB DOCOL,QCOMP,FROMR,NATP,DUP,TOR,AT,COMMA
3366 COMPIL FDB DOCOL,QCOMP,FROMR,DUP,NATP,TOR,AT,COMMA
3371 * Clear the compile state bit(s) (shift to interpret).
3375 LBRAK FDB DOCOL,ZERO,STATE,STORE
3382 * Set the compile state bit(s) (shift to compile).
3386 RBRAK FDB DOCOL,LIT8
3393 * Toggle SMUDGE bit of LATEST definition header,
3394 * to hide it until defined or reveal it after definition.
3396 FCC 'SMUDG' ; 'SMUDGE'
3399 SMUDGE FDB DOCOL,LATEST,LIT8
3406 * Set the conversion base to sixteen (b00010000).
3413 FCB 16 ; decimal sixteen
3419 * Set the conversion base to ten (b00001010).
3421 FCC 'DECIMA' ; 'DECIMAL'
3426 FCB 10 ; decimal ten
3430 * ######>> screen 42 <<
3432 * ( --- ) ( IP *** )
3433 * Pop the saved IP and use it to
3434 * compile the latest symbol as a reference to a ;CODE definition;
3435 * overwrite the code field of the symbol found by LATEST
3436 * with the address of the low-level characteristic code
3437 * provided in the defining definition.
3438 * Look closely at where things return, consider the operation of R> and >R .
3440 * The machine-level code which follows (;CODE) in the instruction stream
3441 * is not executed by the defining symbol,
3442 * but becomes the characteristic of the defined symbol.
3443 * This is the usual way to generate the characteristics of VARIABLEs,
3444 * CONSTANTs, COLON definitions, etc., when FORTH compiles itself.
3446 * Finally, note that, if code shifts from low level back to high
3447 * (native CPU machine code calling into a list of FORTH codes),
3448 * the low level code can't just call a high-level definition.
3449 * Leaf definitions can directly call other leaf definitions,
3450 * but not non-leafs.
3451 * It will need an anonymous list, probably embedded in the low-level code,
3452 * and Y and X will have to be set appropriately before entering the list.
3454 FCC '(;CODE' ; '(;CODE)'
3457 * PSCODE FDB DOCOL,FROMR,TWOP,LATEST,PFA,CFA,STORE
3458 PSCODE FDB DOCOL,FROMR ; Y/IP is post-inc, needs no adjustment.
3459 FDB LATEST,PFA,CFA,STORE
3464 * ?CSP to see if there are loose ends in the defining definition
3465 * before shifting to the assembler,
3466 * compile (;CODE) in the defining definition's instruction stream,
3467 * shift to interpreting,
3468 * make the ASSEMBLER vocabulary current,
3469 * and !CSP to mark the stack
3470 * in preparation for assembling low-level code.
3471 * Note that ;CODE, unlike DOES>, is IMMEDIATE,
3472 * and compiles (;CODE),
3473 * which will do the actual work of changing
3474 * the LATEST definition's characteristic when the defining word runs.
3475 * Assembly is done by the interpreter, rather than the compiler.
3476 * I could have avoided the anomalous three-byte code fields by
3478 * Note that the ASSEMBLER is not part of the model (at this time).
3479 * That means that, until the assembler is ready,
3480 * if you want to define low-level words,
3481 * you have to poke (comma) in hand-assembled stuff.
3484 FCC ';COD' ; ';CODE'
3487 SEMIC FDB DOCOL,QCSP,COMPIL,PSCODE,SMUDGE,LBRAK,QSTACK
3489 * note: "QSTACK" will be replaced by "ASSEMBLER" later
3491 * ######>> screen 43 <<
3494 * Make the word currently being defined
3495 * build a header for DOES> definitions.
3496 * Actually just compiles a CONSTANT zero
3497 * which can be overwritten later by DOES>.
3498 * Since the fig models were established, this technique has been deprecated.
3500 * Note that <BUILDS is not IMMEDIATE,
3501 * and therefore executes during a definition's run-time,
3502 * rather than its compile-time.
3503 * It is not intended to be used directly,
3504 * but rather so that one definition word can build another.
3505 * Also, note that nothing particularly special happens
3506 * in the defining definition until DOES> executes.
3507 * The name <BUILDS is intended to be a reminder of what is about to occur.
3509 * <BUILDS probably should have compiled an ERROR instead of a ZERO CONSTANT.
3511 FCC '<BUILD' ; '<BUILDS'
3514 BUILDS FDB DOCOL,ZERO,CON
3518 * ( --- ) ( IP *** ) C
3519 * Define run-time behavior of definitions compiled/defined
3520 * by a high-level defining definition --
3521 * the FORTH equivalent of a compiler-compiler.
3522 * DOES> assumes that the LATEST symbol table entry
3523 * has at least one word of parameter field,
3524 * which <BUILDS provides.
3525 * Note that DOES> is also not IMMEDIATE.
3527 * When the defining word containing DOES> executes the DOES> icode,
3528 * it overwrites the LATEST symbol's CFA with jsr <XDOES,
3529 * overwrites the first word of that symbol's parameter field with its own IP,
3530 * and pops the previous IP from the return stack.
3531 * The icodes which follow DOES> in the stream
3532 * do not execute at the defining word's run-time.
3534 * Examining XDOES in the virtual machine shows
3535 * that the defined word will execute those icodes
3536 * which follow DOES> at its own run-time.
3538 * The advantage of this kind of behaviour,
3539 * which you will also note in ;CODE,
3540 * is that the defined word can contain
3541 * both operations and data to be operated on.
3542 * This is how FORTH data objects define their own behavior.
3544 * Finally, note that the effective parameter field for DOES> definitions
3545 * starts two NATWID words after the CFA, instead of just one
3546 * (four bytes instead of two in a sixteen-bit addressing Forth).
3548 * VOCABULARYs will use this. See definition of word FORTH.
3550 FCC 'DOES' ; 'DOES>'
3553 * DOES FDB DOCOL,FROMR,TWOP,LATEST,PFA,STORE
3554 DOES FDB DOCOL,FROMR ; Y/IP is post-inc, needs no adjustment.
3555 FDB LATEST,PFA,STORE
3558 * ( --- PFA+NATWID ) ( *** IP )
3559 * Characteristic of a DOES> defined word.
3560 * The characteristics of DOES> definitions are written in high-level
3561 * Forth codes rather than native CPU machine level code.
3562 * The first parameter word points to the high-level characteristic.
3563 * This routine's job is to push the IP,
3564 * load the high level characteristic pointer in IP,
3565 * and leave the address following the characteristic pointer on the stack
3566 * so the parameter field can be accessed.
3567 DODOES LDD ,S ; Keep the return address.
3568 STY ,S ; Save/nest the current IP on the return stack.
3569 LDY NATWID,X ; First parameter is new IP.
3570 LEAX 2*NATWID,X ; Address of second parameter.
3572 TFR D,PC ; Synthetic return.
3574 * From the 6800 model:
3577 * LDX RP make room on return stack
3581 * STA 2,X push return address
3583 * LDX W get addr of pointer to run-time code
3586 * STX N stash it in scratch area
3587 * LDX 0,X get new IP
3589 * CLRA ; get address of parameter
3593 * PSHS B ; and push it on data stack
3597 * ######>> screen 44 <<
3599 * ( strptr --- strptr+1 count )
3600 * Convert counted string to string and count.
3601 * (Fetch the byte at strptr, post-increment.)
3603 FCC 'COUN' ; 'COUNT'
3606 COUNT FDB DOCOL,DUP,ONEP,SWAP,CAT
3610 * ( strptr count --- )
3611 * EMIT count characters at strptr.
3616 TYPE FDB DOCOL,DDUP,ZBRAN
3618 FDB OVER,PLUS,SWAP,XDO
3619 TYPE2 FDB I,CAT,EMIT,XLOOP
3627 * ( strptr count1 --- strptr count2 )
3628 * Supress trailing blanks (subtract count of trailing blanks from strptr).
3630 FCC '-TRAILIN' ; '-TRAILING'
3633 DTRAIL FDB DOCOL,DUP,ZERO,XDO
3634 DTRAL2 FDB OVER,OVER,PLUS,ONE,SUB,CAT,BL
3646 * TYPE counted string out of instruction stream (updating IP).
3651 * PDOTQ FDB DOCOL,R,TWOP,COUNT,DUP,ONEP
3652 * PDOTQ FDB DOCOL,R,NATP,COUNT,DUP,ONEP
3653 PDOTQ FDB DOCOL,R,COUNT,DUP,ONEP
3654 FDB FROMR,PLUS,TOR,TYPE
3659 * { ." something-to-be-printed " } typical input
3660 * Use WORD to parse to trailing quote;
3661 * if compiling, compile XDOTQ and string parsed,
3662 * otherwise, TYPE string.
3672 FDB COMPIL,PDOTQ,WORD
3673 FDB HERE,CAT,ONEP,ALLOT,BRAN
3675 DOTQ1 FDB WORD,HERE,COUNT,TYPE
3678 * ######>> screen 45 <<
3679 * ======>> 126 <<== MACHINE DEPENDENT
3681 * ( --- IN BLK ) ( anything *** nothing )
3682 * ERROR if parameter stack out of bounds.
3684 * But checking whether the stack is in bounds or not
3685 * really should not use the stack.
3686 * And there really should be a ?RSTACK, as well.
3688 FCC '?STAC' ; '?STACK'
3691 QSTACK FDB DOCOL,LIT8
3694 * But why use that instead of XSPZER (S0)?
3695 * Multi-user or multi-tasking would not want that.
3697 * FDB PORIG,AT,TWO,SUB,SPAT,LESS,ONE
3698 FDB PORIG,AT,SPAT,LESS,ONE ; Not post-decrement push.
3700 * prints 'empty stack'
3703 * Here, we compare with a value at least 128
3704 * higher than dict. ptr. (DICTPT)
3706 FCB $80 ; This is a rough check anyway, leave it as is.
3709 FDB TWO ; NOT the NATWID constant!
3711 * prints 'full stack'
3715 * ======>> 127 << this word's function
3716 * is done by ?STACK in this version
3721 *QFREE FDB DOCOL,SPAT,HERE,LIT8
3723 * FDB PLUS,LESS,TWO,QERR,SEMIS ; This TWO is not NATWID!
3725 * ######>> screen 46 <<
3728 * ***** Check that this is how it works here:
3729 * Get up to n-1 characters from the keyboard,
3730 * storing at buffer and echoing, with backspace editing,
3731 * quitting when a CR is read.
3732 * Terminate it with a NUL.
3734 FCC 'EXPEC' ; 'EXPECT'
3737 EXPECT FDB DOCOL,OVER,PLUS,OVER,XDO ; brace the buffer area
3738 * EXPEC2 FDB KEY,DUP,LIT8
3740 FDB LIT,$1C,SHOTOS ; DBG
3743 FDB PORIG,AT,EQUAL,ZBRAN ; check for backspacing
3746 FCB 8 ( backspace character to emit )
3747 FDB OVER,I,EQUAL,DUP,FROMR,TWO,SUB,PLUS ; back I up TWO characters
3751 FCB $D ( carriage return )
3754 FDB LEAVE,DROP,BL,ZERO,BRAN ; I think this is the NUL terminator.
3757 EXPEC5 FDB I,CSTORE,ZERO,I,ONEP,STORE
3758 EXPEC6 FDB EMIT,XLOOP
3765 * EXPECT 128 (TWID) characters to TIB.
3767 FCC 'QUER' ; 'QUERY'
3770 QUERY FDB DOCOL,TIB,AT,COLUMS
3771 FDB AT,EXPECT,ZERO,IN,STORE
3776 * End interpretation of a line or screen, and/or prepare for a new block.
3777 * Note that the name of this definition is an empty string,
3778 * so it matches on the terminating NUL in the terminal or block buffer.
3779 FCB $C1 immediate < carriage return >
3782 NULL FDB DOCOL,BLK,AT,ZBRAN
3785 FDB ZERO,IN,STORE,BLK,AT,BSCR,MOD
3787 * check for end of screen
3790 FDB QEXEC,FROMR,DROP
3793 NULL2 FDB FROMR,DROP
3796 * ######>> screen 47 <<
3799 * Fill n bytes at adr with b.
3800 * This relies on CMOVE having a certain lack of parameter checking,
3801 * where overlapping regions are not properly inverted in copy.
3802 * And this really should be done in low-level.
3803 * None of the advantages of doing things in high-level apply to fill.
3808 FILL FDB DOCOL,SWAP,TOR,OVER,CSTORE,DUP,ONEP
3809 FDB FROMR,ONE,SUB,CMOVE
3814 * Fill n bytes with 0.
3816 FCC 'ERAS' ; 'ERASE'
3819 ERASE FDB DOCOL,ZERO,FILL
3824 * Fill n bytes with ASCII SPACE.
3826 FCC 'BLANK' ; 'BLANKS'
3829 BLANKS FDB DOCOL,BL,FILL
3834 * Format a character at the left of the HLD output buffer.
3839 HOLD FDB DOCOL,LIT,$FFFF,HLD,PSTORE,HLD,AT,CSTORE
3844 * Give the address of the output PAD buffer.
3845 * PAD points to the end of a 68 byte buffer for numeric conversion.
3850 PAD FDB DOCOL,HERE,LIT8
3855 * ######>> screen 48 <<
3858 * Scan a string terminated by the character c or ASCII NUL out of input;
3859 * store symbol at WORDPAD with leading count byte and trailing ASCII NUL.
3860 * Leading c are passed over, per ENCLOSE.
3861 * Scans from BLK, or from TIB if BLK is zero.
3862 * May overwrite the numeric conversion pad,
3863 * if really long (length > 31) symbols are scanned.
3868 WORD FDB DOCOL,BLK,AT,ZBRAN
3870 FDB BLK,AT,BLOCK,BRAN
3873 WORD3 FDB IN,AT,PLUS,SWAP,ENCLOS,HERE,LIT8
3875 FDB BLANKS,IN,PSTORE,OVER,SUB,TOR,R,HERE
3876 FDB CSTORE,PLUS,HERE,ONEP,FROMR,CMOVE
3879 * ######>> screen 49 <<
3881 * ( d1 string --- d2 adr )
3882 * Convert the text at string into a number, accumulating the result into d1,
3883 * leaving adr pointing to the first character not converted.
3884 * If DPL is non-negative at entry,
3885 * accumulates the number of characters converted into DPL.
3887 FCC '(NUMBER' ; '(NUMBER)'
3891 PNUMB2 FDB ONEP,DUP,TOR,CAT,BASE,AT,DIGIT,ZBRAN
3893 FDB SWAP,BASE,AT,USTAR,DROP,ROT,BASE
3894 FDB AT,USTAR,DPLUS,DPL,AT,ONEP,ZBRAN
3897 PNUMB3 FDB FROMR,BRAN
3904 * Convert text at ctstr to a double integer,
3905 * taking the 0 ERROR if the conversion is not valid.
3906 * If a decimal point is present,
3907 * accumulate the count of digits to the decimal point's right into DPL
3908 * (negative DPL at exit indicates single precision).
3909 * ctstr is a counted string
3910 * -- the first byte at ctstr is the length of the string,
3911 * but NUMBER ignores the count and expects a NUL terminator instead.
3913 FCC 'NUMBE' ; 'NUMBER'
3916 NUMB FDB DOCOL,ZERO,ZERO,ROT,DUP,ONEP,CAT,LIT8
3918 FDB EQUAL,DUP,TOR,PLUS,LIT,$FFFF
3919 NUMB1 FDB DPL,STORE,PNUMB,DUP,CAT,BL,SUB
3924 FDB SUB,ZERO,QERR,ZERO,BRAN
3926 NUMB2 FDB DROP,FROMR,ZBRAN
3932 * ( --- locptr length true ) { -FIND name } typical input
3934 * Parse a word, then FIND,
3935 * first in the definition vocabulary,
3936 * then in the CONTEXT (interpretation) vocabulary, if necessary.
3937 * Returns what (FIND) returns, flag and optional location and length.
3939 FCC '-FIN' ; '-FIND'
3942 DFIND FDB DOCOL,BL,WORD,HERE,CONTXT,AT,AT
3943 FDB PFIND,DUP,ZEQU,ZBRAN
3945 FDB DROP,HERE,LATEST,PFIND
3948 * ######>> screen 50 <<
3950 * ( anything --- nothing ) ( anything *** nothing )
3951 * An indirection for ABORT, for ERROR,
3952 * which may be modified carefully.
3954 FCC '(ABORT' ; '(ABORT)'
3957 PABORT FDB DOCOL,ABORT
3962 FCC 'ERRO' ; 'ERROR'
3965 * This really should not be high level, according to best practices.
3966 * But fixing that cascades through MESSAGE,
3967 * requiring re-architecting the disk block system.
3968 * First, we need to get this transliteration running.
3969 ERROR FDB DOCOL,WARN,AT,ZLESS
3974 * 0 to print error #
3975 * and 1 to print error message from disc
3977 ERROR2 FDB HERE,COUNT,TYPE,PDOTQ
3980 FDB MESS,SPSTOR,IN,AT,BLK,AT,QUIT
3985 * Mask byte at adr with n.
3986 * Not in FIG, don't need it for 8 bit characters after all.
3988 * FCC 'CMAS' ; 'CMASK'
3991 * CMASK FDB *+NATWID
3999 * Mask high bit of tail of name in PAD buffer.
4000 * Not in FIG, need it for 8 bit characters.
4002 FCC 'IDFLA' ; 'IDFLAT'
4007 LDB ,X ; get the count
4009 LDA B,X ; point to the tail
4010 ANDA #$7F ; Clear the EndOfName flag bit.
4015 * Print definition's name from its NFA.
4020 IDDOT FDB DOCOL,PAD,LIT8
4023 FCB $5F ( underline )
4024 FDB FILL,DUP,PFA,LFA,OVER,SUB,PAD
4025 * FDB SWAP,CMOVE,PAD,COUNT,LIT8
4033 * ######>> screen 51 <<
4035 * ( --- ) { CREATE name } input
4036 * Parse a name (length < 32 characters) and create a header,
4037 * reporting first duplicate found in either the defining vocabulary
4038 * or the context (interpreting) vocabulary.
4039 * Install the header in the defining vocabulary
4040 * with CFA dangerously pointing to the parameter field.
4041 * Leave the name SMUDGEd.
4043 FCC 'CREAT' ; 'CREATE'
4046 CREATE FDB DOCOL,DFIND,ZBRAN
4055 CREAT2 FDB HERE,DUP,CAT,WIDTH,AT,MIN
4056 FDB ONEP,ALLOT,DUP,LIT8
4057 FCB ($80|FSMUDG) ; Bracket the name.
4058 FDB TOGGLE,HERE,ONE,SUB,LIT8
4060 FDB TOGGLE,LATEST,COMMA,CURENT,AT,STORE
4061 * FDB HERE,TWOP,COMMA
4065 * ######>> screen 52 <<
4068 * { [COMPILE] name } typical use
4069 * -DFIND next WORD and COMPILE it, literally;
4070 * used to compile immediate definitions into words.
4072 FCC '[COMPILE' ; '[COMPILE]'
4075 BCOMP FDB DOCOL,DFIND,ZEQU,ZERO,QERR,DROP,CFA,COMMA
4079 * ( n --- ) if compiling. P
4080 * ( n --- n ) if interpreting.
4081 * Compile n as a literal, if compiling.
4083 FCC 'LITERA' ; 'LITERAL'
4086 LITER FDB DOCOL,STATE,AT,ZBRAN
4088 FDB COMPIL,LIT,COMMA
4092 * ( d --- ) if compiling. P
4093 * ( d --- d ) if interpreting.
4094 * Compile d as a double literal, if compiling.
4096 FCC 'DLITERA' ; 'DLITERAL'
4099 DLITER FDB DOCOL,STATE,AT,ZBRAN
4101 FDB SWAP,LITER,LITER ; Just two literals in the right order.
4104 * ######>> screen 53 <<
4107 * Interpret or compile, according to STATE.
4108 * Searches words parsed in dictionary first, via -FIND,
4109 * then checks for valid NUMBER.
4110 * Pushes or COMPILEs double literal if NUMBER leaves DPL non-negative.
4111 * ERROR checks the stack via ?STACK before returning to its caller.
4113 FCC 'INTERPRE' ; 'INTERPRET'
4117 INTER2 FDB DFIND,ZBRAN
4127 INTER5 FDB HERE,NUMB,DPL,AT,ONEP,ZBRAN
4131 INTER6 FDB DROP,LITER
4132 INTER7 FDB QSTACK,BRAN
4134 * FDB SEMIS never executed
4137 * ######>> screen 54 <<
4140 * Toggle precedence bit of LATEST definition header.
4141 * During compiling, most symbols scanned are compiled.
4142 * IMMEDIATE definitions execute whenever the outer INTERPRETer scans them,
4143 * but may be compiled via ' (TICK).
4145 FCC 'IMMEDIAT' ; 'IMMEDIATE'
4148 IMMED FDB DOCOL,LATEST,LIT8
4154 * ( --- ) { VOCABULARY name } input
4155 * Create a vocabulary entry with a flag for terminating vocabulary searches.
4156 * Store the current search context in it for linking.
4157 * At run-time, VOCABULARY makes itself the CONTEXT vocabulary.
4159 FCC 'VOCABULAR' ; 'VOCABULARY'
4162 VOCAB FDB DOCOL,BUILDS,LIT,$81A0,COMMA,CURENT,AT,CFA
4163 FDB COMMA,HERE,VOCLIN,AT,COMMA,VOCLIN,STORE,DOES
4164 * DOVOC FDB TWOP,CONTXT,STORE
4165 DOVOC FDB NATP,CONTXT,STORE
4170 * Note: FORTH does not go here in the rom-able dictionary,
4171 * since FORTH is a type of variable.
4173 * (Should make a proper architecture for this at some point.)
4178 * Makes the current interpretation CONTEXT vocabulary
4179 * also the CURRENT defining vocabulary.
4181 FCC 'DEFINITION' ; 'DEFINITIONS'
4184 DEFIN FDB DOCOL,CONTXT,AT,CURENT,STORE
4189 * Parse out a comment and toss it away.
4190 * Leaves the first 32 characters in WORDPAD, which may or may not be useful.
4194 PAREN FDB DOCOL,LIT8
4199 * ######>> screen 55 <<
4201 * ( anything *** nothing )
4202 * Clear return stack.
4203 * Then INTERPRET and, if not compiling, prompt with OK,
4209 QUIT FDB DOCOL,ZERO,BLK,STORE
4212 * Here is the outer interpretter
4213 * which gets a line of input, does it, prints " OK"
4215 QUIT2 FDB RPSTOR,CR,QUERY,INTERP,STATE,AT,ZEQU
4223 * FDB SEMIS ( never executed )
4226 * ( anything --- nothing ) ( anything *** nothing )
4227 * Clear parameter stack,
4228 * set STATE to interpret and BASE to DECIMAL,
4229 * return to input from terminal,
4230 * restore DRIVE OFFSET to 0,
4231 * print out "Forth-68",
4232 * set interpret and define vocabularies to FORTH,
4233 * and finally, QUIT.
4234 * Used to force the system to a known state
4235 * and return control to the initial INTERPRETer.
4237 FCC 'ABOR' ; 'ABORT'
4240 ABORT FDB DOCOL,SPSTOR,DEC,QSTACK,DRZERO,CR,PDOTQ
4245 * FDB SEMIS never executed
4248 * ######>> screen 56 <<
4249 * bootstrap code... moves rom contents to ram :
4256 * Ultimately, we want position indepence,
4257 * so I'm using PCR where it seems reasonable.
4258 CENT LDS SINIT,PCR ; Get a useable return stack, at least.
4259 LDA #IUPDP ; This is not relative to PC.
4260 TFR A,DP ; And a useable direct page, too.
4261 SETDP IUPDP ; (For good measure.)
4263 * We'll keep this here for the time being.
4264 * There are better ways to do this, of course.
4265 * Re-architect, re-architect.
4266 LEAX ERAM,PCR ; end of stuff to move
4267 STX <XFENCE ; Borrow this variable for a loop terminator.
4268 LDY #RBEG ; bottom of open-ended destination
4269 LEAX RAM,PCR ; bottom of stuff to move
4271 STA ,Y+ ; move TASK & FORTH to ram
4274 * Leaves USE and PREV uninitialized.
4279 * STX <XFENCE ; Borrow this variable for a loop terminator.
4280 * LEAY REND,PCR ; top of destination (included XUSE and XPREV)
4281 * LEAX ERAM,PCR ; top of stuff to move (included initializers for XUSE and XPREV)
4283 * STA ,-Y ; move TASK & FORTH to ram
4287 * CENT LDS #REND-1 top of destination
4288 * LDX #ERAM top of stuff to move
4291 * PSHS A ; move TASK & FORTH to ram
4295 * LDS #XFENCE-1 put stack at a safe place for now
4296 * But that is taken care of.
4318 WENT LDS SINIT,PCR ; Get a useable return stack, at least.
4319 LDA #IUPDP ; This is not relative to PC.
4320 TFR A,DP ; And a useable direct page, too.
4321 SETDP IUPDP ; (For good measure.)
4324 PSHS X ; for loop termination
4325 CLRB ; Yes, I'm being a little ridiculous. Only a little.
4327 LEAY XFENCE-UORIG,Y ; top of destination
4328 LEAX FENCIN,PCR ; top of stuff to move
4329 WARM2 LDD ,--X ; All entries are 16 bit.
4333 LEAS 2,S ; But we'll reset the return stack shortly, anyway.
4334 LDU <XSPZER ; So we can clear the hole above the TOS
4335 * WENT LDS #XFENCE-1 top of destination
4336 * LDX #FENCIN top of stuff to move
4344 * S is already there.
4346 * STX UP init user ram pointer
4347 * UP is already there (DP).
4350 LEAY ABORT+NATWID,PCR ; IP never points to DOCOL!
4352 NOP Here is a place to jump to special user
4353 NOP initializations such as I/0 interrups
4356 * For systems with TRACE:
4358 STX ,U The hole above the parameter stack
4359 * STX TRLIM clear trace mode
4360 STX <TRLIM clear trace mode (both bytes)
4362 * STX BRKPT clear breakpoint address
4363 STX <BRKPT clear breakpoint address
4364 * JMP RPSTOR+2 start the virtual machine running !
4365 LBSR RPSTOR+NATWID start the virtual machine running !
4366 LEAX WENT,PCR ; But we must also give RP! someplace to return.
4367 STX ,S ; This rail might get walked on by (DO).
4369 * RP! sets up the return stack pointer, then Y references abort.
4371 * Here is the stuff that gets copied to ram :
4372 * (not * at address $140:)
4373 * at an appropriate address:
4375 * RAM FDB $3000,$3000,0,0
4376 * RAM FDB BUFBAS,BUFBAS,0,0 ; ... except the direct page has moved.
4377 * These initialization values for USE and PREV were here to help pack the code.
4378 * They don't belong here unless we move the USER table
4379 * back below the writable dictionary,
4380 * *and* move these USER variables to the end of the direct page --
4381 * *or* let these definitions exist in the USER table.
4386 * Makes FORTH the current interpretation vocabulary.
4387 * In order to make this ROMmable, this entry is set up as the tail-end,
4388 * and copied to RAM in the start-up code.
4389 * We want a more elegant solution to this, too. Greedy, maybe.
4391 FCC 'FORT' ; 'FORTH'
4393 FDB NOOP-7 ; Note that this does not link to COLD!
4394 RFORTH FDB DODOES,DOVOC,$81A0,TASK-7
4396 FCC "Copyright 1979 Forth Interest Group, David Lion,"
4398 FCC "Parts Copyright 2019 Joel Matthew Rees"
4404 RTASK FDB DOCOL,SEMIS
4406 ERAMSZ EQU *-RAM ; So we can get a look at it.
4409 * ######>> screen 57 <<
4412 * Sign extend n0 to a double integer.
4416 FDB COLD-7 ; Note that this does not link to FORTH (RFORTH)!
4417 STOD FDB DOCOL,DUP,ZLESS,MINUS
4423 * ( multiplier multiplicand --- product )
4424 * Signed word multiply.
4429 LBSR USTAR+NATWID ; or [USTAR,PCR]?
4430 LEAU NATWID,U ; Drop high word.
4438 * ( dividend divisor --- remainder quotient )
4439 * M/ in word-only form, i. e., signed division of 2nd word by top word,
4440 * yielding signed word quotient and remainder.
4441 * Except *BUG* it isn't signed.
4446 SLMOD FDB DOCOL,TOR,STOD,FROMR,USLASH
4450 * ( dividend divisor --- quotient )
4451 * Signed word divide without remainder.
4452 * Except *BUG* it isn't signed.
4456 SLASH FDB DOCOL,SLMOD,SWAP,DROP
4460 * ( dividend divisor --- remainder )
4461 * Remainder function, result takes sign of dividend.
4466 MOD FDB DOCOL,SLMOD,DROP
4470 * ( multiplier multiplicand divisor --- remainder quotient )
4471 * Signed precise division of product:
4472 * multiply 2nd and 3rd words on stack
4473 * and divide the 31-bit product by the top word,
4474 * leaving both quotient and remainder.
4475 * Remainder takes sign of product.
4476 * Guaranteed not to lose significant bits in 16 bit integer math.
4478 FCC '*/MO' ; '*/MOD'
4481 SSMOD FDB DOCOL,TOR,USTAR,FROMR,USLASH
4485 * ( multiplier multiplicand divisor --- quotient )
4486 * */MOD without remainder.
4491 SSLASH FDB DOCOL,SSMOD,SWAP,DROP
4495 * ( ud1 u1 --- u2 ud2 )
4496 * U/ with an (unsigned) double quotient.
4497 * Guaranteed not to lose significant bits in 32 bit / 16 bit bit integer math,
4498 * if you are prepared to deal with the extra 16 bits of result.
4500 FCC 'M/MO' ; 'M/MOD'
4503 MSMOD FDB DOCOL,TOR,ZERO,R,USLASH
4504 FDB FROMR,SWAP,TOR,USLASH,FROMR
4510 * Convert the top of stack to its absolute value.
4515 ABS FDB DOCOL,DUP,ZLESS,ZBRAN
4523 * Convert the top double to its absolute value.
4528 DABS FDB DOCOL,DUP,ZLESS,ZBRAN
4533 * ######>> screen 58 <<
4537 * Least Recently Used buffer.
4538 * Really should be with FIRST and LIMIT in the per-task table.
4547 * Most Recently Used buffer.
4548 * Really should be with FIRST and LIMIT in the per-task table.
4556 * ( buffer1 --- buffer2 f )
4557 * Bump to next buffer,
4558 * flag false if result is PREVious buffer,
4559 * otherwise flag true.
4560 * Used in the LRU allocation routines.
4565 * PBUF FDB DOCOL,LIT8
4566 * FCB $84 ; This was a hard-wiring bug.
4567 PBUF FDB DOCOL,BBUF,BCTL,PLUS ; Size of the buffer record.
4568 FDB PLUS,DUP,LIMIT,EQUAL,ZBRAN
4571 PBUF2 FDB DUP,PREV,AT,SUB
4576 * Mark PREVious buffer dirty, in need of being written out.
4578 FCC 'UPDAT' ; 'UPDATE'
4581 UPDATE FDB DOCOL,PREV,AT,AT,LIT,$8000,OR,PREV,AT,STORE
4586 * Mark all buffers empty.
4587 * Standard method of discarding changes.
4589 FCC 'EMPTY-BUFFER' ; 'EMPTY-BUFFERS'
4592 MTBUF FDB DOCOL,FIRST,LIMIT,OVER,SUB,ERASE
4597 * Clear the current offset to the block numbers in the drive interface.
4598 * The drives need to be re-architected.
4599 * Would be cool to have RAM and ROM drives supported
4600 * in addition to regular physical persistent store.
4605 DRZERO FDB DOCOL,ZERO,OFSET,STORE
4608 * ======>> 174 <<== system dependant word
4610 * Set the current offset in the drive interface to reference the second drive.
4611 * The hard-coded number in there needs to be in a table.
4616 DRONE FDB DOCOL,LIT,$07D0,OFSET,STORE
4619 * ######>> screen 59 <<
4622 * Get a free buffer,
4623 * assign it to block n,
4624 * return buffer address.
4625 * Will free a buffer by writing it, if necessary.
4626 * Does not actually read the block.
4627 * A bug in the fig LRU algorithm, which I have not fixed,
4628 * gives the PREVious buffer if USE gets set to PREVious.
4629 * (The bug is that USE sometimes gets set to PREVious.)
4630 * This bug sometimes causes sector moves to become sector fills.
4632 FCC 'BUFFE' ; 'BUFFER'
4635 BUFFER FDB DOCOL,USE,AT,DUP,TOR
4636 BUFFR2 FDB PBUF,ZBRAN
4638 FDB USE,STORE,R,AT,ZLESS
4641 * FDB R,TWOP,R,AT,LIT,$7FFF,AND,ZERO,RW
4642 FDB R,NATP,R,AT,LIT,$7FFF,AND,ZERO,RW
4643 * BUFFR3 FDB R,STORE,R,PREV,STORE,FROMR,TWOP
4644 BUFFR3 FDB R,STORE,R,PREV,STORE,FROMR,NATP
4647 * ######>> screen 60 <<
4650 * Get BUFFER containing block n, relative to OFFSET.
4651 * If block n is not in a buffer, bring it in.
4652 * Returns buffer address.
4654 FCC 'BLOC' ; 'BLOCK'
4657 BLOCK FDB DOCOL,OFSET,AT,PLUS,TOR
4658 FDB PREV,AT,DUP,AT,R,SUB,DUP,PLUS,ZBRAN
4660 BLOCK3 FDB PBUF,ZEQU,ZBRAN
4662 * FDB DROP,R,BUFFER,DUP,R,ONE,RW,TWO,SUB
4663 FDB DROP,R,BUFFER,DUP,R,ONE,RW,NATWC,SUB
4664 BLOCK4 FDB DUP,AT,R,SUB,DUP,PLUS,ZEQU,ZBRAN
4667 * BLOCK5 FDB FROMR,DROP,TWOP
4668 BLOCK5 FDB FROMR,DROP,NATP
4671 * ######>> screen 61 <<
4673 * ( line screen --- buffer C/L)
4674 * Bring in the sector containing the specified line of the specified screen.
4675 * Returns the buffer address and the width of the screen.
4676 * Screen number is relative to OFFSET.
4677 * The line number may be beyond screen 4,
4678 * (LINE) will get the appropriate screen.
4680 FCC '(LINE' ; '(LINE)'
4683 PLINE FDB DOCOL,TOR,LIT8
4685 FDB BBUF,SSMOD,FROMR,BSCR,STAR,PLUS,BLOCK,PLUS,LIT8
4690 * ( line screen --- )
4691 * Print the line of the screen as found by (LINE), suppress trailing BLANKS.
4693 FCC '.LIN' ; '.LINE'
4696 DLINE FDB DOCOL,PLINE,DTRAIL,TYPE
4701 * If WARNING is 0, print "MESSAGE #n";
4702 * otherwise, print line n relative to screen 4,
4703 * the line number may be negative.
4704 * Uses .LINE, but counter-adjusts to be relative to the real drive 0.
4706 FCC 'MESSAG' ; 'MESSAGE'
4709 MESS FDB DOCOL,WARN,AT,ZBRAN
4715 FDB OFSET,AT,BSCR,SLASH,SUB,DLINE,BRAN
4719 FCC 'err # ' ; 'err # '
4725 * Begin interpretation of screen (block) n.
4726 * See also ARROW, SEMIS, and NULL.
4728 FCC 'LOA' ; 'LOAD' : input:scr #
4731 LOAD FDB DOCOL,BLK,AT,TOR,IN,AT,TOR,ZERO,IN,STORE
4732 FDB BSCR,STAR,BLK,STORE
4733 FDB INTERP,FROMR,IN,STORE,FROMR,BLK,STORE
4738 * Continue interpreting source code on the next screen.
4743 ARROW FDB DOCOL,QLOAD,ZERO,IN,STORE,BSCR
4744 FDB BLK,AT,OVER,MOD,SUB,BLK,PSTORE
4749 * ######>> screen 63 <<
4750 * The next 4 subroutines are machine dependent, and are
4751 * called by words 13 through 16 in the dictionary.
4753 * ======>> 182 << code for EMIT
4754 * ( --- ) No parameter stack effect.
4755 * Interfaces directly with ROM. Expects output character in D (therefore, B).
4756 * Output using rom CHROUT: redirectable to a printer on Coco.
4757 * Outputs the character on stack (low byte of 1 bit word/cell).
4758 PEMIT PSHS Y,U,DP ; Save everything important! (For good measure, only.)
4759 TFR B,A ; Coco ROM wants it in A.
4761 TFR B,DP ; Give the ROM its direct page.
4762 JSR [$A002] ; Output the character in A.
4764 * PEMIT STB N save B
4767 * BITB #2 check ready bit
4768 * BEQ PEMIT+4 if not ready for more data
4771 * STB IOSTAT-UORIG,X
4772 * LDB N recover B & X
4774 * RTS only A register may change
4775 * PEMIT JMP $E1D1 for MIKBUG
4776 * PEMIT FCB $3F,$11,$39 for PROTO
4777 * PEMIT JMP $D286 for Smoke Signal DOS
4779 * ======>> 183 << code for KEY
4780 * ( --- ) No parameter stack effect.
4781 * Returns character or break flag in D, since this interfaces with Coco ROM.
4782 * Wait for key from POLCAT on Coco.
4783 * Returns the character code for the key pressed.
4784 PKEY PSHS Y,U,DP ; Must save everything important for this one.
4785 LDA #$CF ; a cursor of sorts
4797 PKEYR CLRB ; for the break flag, shares code with PQTER
4800 COMB ; for the break flag
4801 PKEYGT EXG A,B ; Leave it in D for return.
4802 PULS Y,U,DP,PC ; Shares exit with PQTER
4808 * BCC PKEY+4 no incoming data yet
4810 * ANDA #$7F strip parity bit
4812 * STB IOSTAT+1-UORIG,X
4816 * PKEY JMP $E1AC for MIKBUG
4817 * PKEY FCB $3F,$14,$39 for PROTO
4818 * PKEY JMP $D289 for Smoke Signal DOS
4820 * ######>> screen 64 <<
4821 * ======>> 184 << code for ?TERMINAL
4822 * ( --- f ) Should change this to no stack effect.
4823 * check break key using POLCAT
4824 * Returns a flag to tell whether the break key was pressed or not.
4828 JSR [$A000] ; Look but don't wait.
4830 * PQTER LDA ACIAC Test for 'break' condition
4831 * ANDA #$11 mask framing error bit and
4834 * LDA ACIAD clear input buffer
4841 * ======>> 185 << code for CR
4842 * ( --- ) No stack effect.
4843 * Interfaces directly with ROM.
4844 * For Coco just output a CR.
4845 * Also subject to redirection in Coco BASIC ROM.
4847 BRA PEMIT ; Just steal the code.
4848 * PCR LDA #$D carriage return
4854 * LDB XDELAY+1-UORIG,X
4856 * BMI PQTER2 return if minus
4857 * PSHS B ; save counter
4858 * BSR PEMIT print RUBOUTs to delay.....
4865 * ######>> screen 66 <<
4868 * Query the disk, I suppose.
4869 * Not sure what the model had in mind for this stub.
4871 FCC '?DIS' ; '?DISC'
4877 * ######>> screen 67 <<
4880 * Write one block of data to disk.
4881 * Parameters unspecified in model. Stub in model.
4883 FCC 'BLOCK-WRIT' ; 'BLOCK-WRITE'
4889 * ######>> screen 68 <<
4892 * Read one block of data from disk.
4893 * Parameters unspecified in model. Stub in model.
4895 FCC 'BLOCK-REA' ; 'BLOCK-READ'
4901 *The next 3 words are written to create a substitute for disc
4902 * mass memory,located between $3210 & $3FFF in ram.
4909 FDB MEMEND a system dependent equate at front
4917 FDB MEMTOP ( $3FFF or $7FFF in this version )
4919 * ######>> screen 69 <<
4921 * ( buffer sector f --- )
4922 * Read or Write the specified (absolute -- ignores OFFSET) sector
4923 * from or to the specified buffer.
4924 * A zero flag specifies write,
4925 * non-zero specifies read.
4926 * Sector is an unsigned integer,
4927 * buffer is the buffer's address.
4928 * Will need to use the CoCo ROM disk routines.
4929 * For now, provides a virtual disk in RAM.
4934 RW FDB DOCOL,TOR,BBUF,STAR,LO,PLUS,DUP,HI,GREAT,ZBRAN
4938 FCC ' Range ?' ; ' Range ?'
4948 * LDY $C006 control table
4949 * LDX #DROFFS+7 ; This is BIF's table of drive sizes.
4951 * RWD SUBD ,X++ sectors
4953 * BVC RWR table end?
4957 * RWR ADDD ,--X back one
4960 * LDD #18 sectors/track
4970 * PULS D table entry
4983 * JSR [$C004] ROM handles timeout
4984 * PULS Y,U,DP if IRQ enabled
4987 * LDB 6,X coco status
4997 * ######>> screen 72 <<
4999 * ( --- ) compiling P
5000 * ( --- adr ) interpreting
5002 * Parse a symbol name from input and search the dictionary for it, per -FIND;
5003 * compile the address as a literal if compiling,
5004 * otherwise just push it.
5008 TICK FDB DOCOL,DFIND,ZEQU,ZERO,QERR,DROP,LITER
5012 * ( --- ) { FORGET name } input
5013 * Parse out name of definition to FORGET to, -DFIND it,
5014 * then lop it and everything that follows out of the dictionary.
5015 * In fig Forth, CURRENT and CONTEXT have to be the same to FORGET.
5017 FCC 'FORGE' ; 'FORGET'
5020 FORGET FDB DOCOL,CURENT,AT,CONTXT,AT,SUB,LIT8
5022 FDB QERR,TICK,DUP,FENCE,AT,LESS,LIT8
5024 FDB QERR,DUP,ZERO,PORIG,GREAT,LIT8
5026 FDB QERR,DUP,NFA,DICTPT,STORE,LFA,AT,CONTXT,AT,STORE
5029 * ######>> screen 73 <<
5032 * Calculate a back reference from HERE and compile it.
5037 * BACK FDB DOCOL,HERE,SUB,COMMA
5038 BACK FDB DOCOL,HERE,NATP,SUB,COMMA
5043 * typical use: BEGIN code-loop test UNTIL
5044 * typical use: BEGIN code-loop AGAIN
5045 * typical use: BEGIN code-loop test WHILE code-true REPEAT
5046 * ( --- adr n ) compile time P,C
5047 * Push HERE for BACK reference for general (non-counting) loops,
5048 * with BEGIN construct flag.
5049 * A better flag: $4245 (ASCII for 'BE').
5051 FCC 'BEGI' ; 'BEGIN'
5054 BEGIN FDB DOCOL,QCOMP,HERE,ONE ; ONE is a flag for BEGIN loops.
5059 * typical use: test IF code-true ELSE code-false ENDIF
5060 * ENDIF is just a sort of intersection piece,
5061 * marking where execution resumes after both branches.
5062 * ( adr n --- ) compile time
5063 * Check the mark and resolve the IF.
5064 * A better flag: $4846 (ASCII for 'IF').
5066 FCC 'ENDI' ; 'ENDIF'
5069 ENDIF FDB DOCOL,QCOMP,TWO,QPAIRS,HERE ; This TWO is a flag for IF.
5070 FDB OVER,NATP,SUB,SWAP,STORE
5075 * typical use: test IF code-true ELSE code-false ENDIF
5082 THEN FDB DOCOL,ENDIF
5086 * ( limit index --- ) runtime
5087 * typical use: DO code-loop LOOP
5088 * typical use: DO code-loop increment +LOOP
5089 * Counted loop, index is initial value of index.
5090 * Will loop until index equals (positive going)
5091 * or passes (negative going) limit.
5092 * ( --- adr n ) compile time P,C
5093 * Compile (DO), push HERE for BACK reference,
5094 * and push DO control construct flag.
5095 * A better flag: $444F (ASCII for 'DO').
5100 DO FDB DOCOL,COMPIL,XDO,HERE,THREE ; THREE is a flag for DO loops.
5105 * typical use: DO code-loop LOOP
5106 * Increments the index by one and branches back to beginning of loop.
5107 * Will loop until index equals limit.
5108 * ( adr n --- ) compile time P,C
5109 * Check the mark and compile (LOOP), fill in BACK reference.
5110 * A better flag: $444F (ASCII for 'DO').
5115 LOOP FDB DOCOL,THREE,QPAIRS,COMPIL,XLOOP,BACK ; THREE for DO loops.
5120 * typical use: DO code-loop increment +LOOP
5121 * Increments the index by n and branches back to beginning of loop.
5122 * Will loop until index equals (positive going)
5123 * or passes (negative going) limit.
5124 * ( adr n --- ) compile time P,C
5125 * Check the mark and compile (+LOOP), fill in BACK reference.
5126 * A better flag: $444F (ASCII for 'DO').
5128 FCC '+LOO' ; '+LOOP'
5131 PLOOP FDB DOCOL,THREE,QPAIRS,COMPIL,XPLOOP,BACK ; THREE for DO loops.
5136 * typical use: BEGIN code-loop test UNTIL
5137 * Will loop until UNTIL tests true.
5138 * ( adr n --- ) compile time P,C
5139 * Check the mark and compile (0BRANCH), fill in BACK reference.
5140 * A better flag: $4245 (ASCII for 'BE').
5142 FCC 'UNTI' ; 'UNTIL' : ( same as END )
5145 UNTIL FDB DOCOL,ONE,QPAIRS,COMPIL,ZBRAN,BACK ; ONE for BEGIN loops.
5148 * ######>> screen 74 <<
5151 * typical use: BEGIN code-loop test END
5163 * typical use: BEGIN code-loop AGAIN
5165 * (or until something uses R> DROP to force the current definition to die,
5166 * or perhaps ABORT or ERROR or some such other drastic means stops things).
5167 * ( adr n --- ) compile time P,C
5168 * Check the mark and compile (0BRANCH), fill in BACK reference.
5169 * A better flag: $4245 (ASCII for 'BE').
5171 FCC 'AGAI' ; 'AGAIN'
5174 AGAIN FDB DOCOL,ONE,QPAIRS,COMPIL,BRAN,BACK ; ONE for BEGIN loops.
5179 * typical use: BEGIN code-loop test WHILE code-true REPEAT
5180 * Will loop until WHILE tests false, skipping code-true on end.
5181 * REPEAT marks where execution resumes after the WHILE find a false flag.
5182 * ( aadr1 n1 adr2 n2 --- ) compile time P,C
5183 * Check the marks for WHILE and BEGIN,
5184 * compile BRANCH and BACK fill adr1 reference,
5185 * FILL-IN 0BRANCH reference at adr2.
5186 * Better flags: $4245 (ASCII for 'BE') and $5747 (ASCII for 'WH').
5188 FCC 'REPEA' ; 'REPEAT'
5191 REPEAT FDB DOCOL,TOR,TOR,AGAIN,FROMR,FROMR ; ONE for BEGIN loops.
5192 FDB TWO,SUB,ENDIF ; TWO is for IF, 4 is for WHILE.
5197 * typical use: test IF code-true ELSE code-false ENDIF
5198 * Will pass execution to the true part on a true flag
5199 * and to the false part on a false flag.
5200 * ( --- adr n ) compile time P,C
5201 * Compile a 0BRANCH and dummy offset
5202 * and push IF reference to fill in and
5203 * IF control construct flag.
5204 * A better flag: $4946 (ASCII for 'IF').
5209 IF FDB DOCOL,COMPIL,ZBRAN,HERE,ZERO,COMMA,TWO ; TWO is a flag for IF.
5214 * typical use: test IF code-true ELSE code-false ENDIF
5215 * ELSE is just a sort of intersection piece,
5216 * marking where execution resumes on a false branch.
5217 * ( adr1 n --- adr2 n ) compile time P,C
5219 * compile BRANCH with dummy offset,
5220 * resolve IF reference,
5221 * and leave reference to BRANCH for ELSE.
5222 * A better flag: $4946 (ASCII for 'IF').
5227 ELSE FDB DOCOL,TWO,QPAIRS,COMPIL,BRAN,HERE
5228 FDB ZERO,COMMA,SWAP,TWO,ENDIF,TWO ; TWO is a flag for IF.
5233 * typical use: BEGIN code-loop test WHILE code-true REPEAT
5234 * Will loop until WHILE tests false, skipping code-true on end.
5235 * ( --- adr n ) compile time P,C
5236 * Compile 0BRANCH with dummy offset (using IF),
5237 * push WHILE reference.
5238 * BEGIN flag will sit underneath this.
5239 * Better flags: $4245 (ASCII for 'BE') and $5747 (ASCII for 'WH').
5241 FCC 'WHIL' ; 'WHILE'
5244 WHILE FDB DOCOL,IF,TWOP ; TWO is a flag for IF, 4 is for WHILE.
5247 * ######>> screen 75 <<
5250 * EMIT count spaces, for non-zero, non-negative counts.
5252 FCC 'SPACE' ; 'SPACES'
5255 SPACES FDB DOCOL,ZERO,MAX,DDUP,ZBRAN
5258 SPACE2 FDB SPACE,XLOOP
5264 * Initialize HLD for converting a double integer.
5265 * Stores the PAD address in HLD.
5270 BDIGS FDB DOCOL,PAD,HLD,STORE
5274 * ( d --- string length )
5275 * Terminate numeric conversion,
5276 * drop the number being converted,
5277 * leave the address of the conversion string and the length, ready for TYPE.
5282 EDIGS FDB DOCOL,DROP,DROP,HLD,AT,PAD,OVER,SUB
5287 * Put sign of n (as a flag) at the head of the conversion string.
5288 * Drop the sign flag.
5293 SIGN FDB DOCOL,ROT,ZLESS,ZBRAN
5302 * Generate next most significant digit in the conversion BASE,
5303 * putting the digit at the head of the conversion string.
5307 DIG FDB DOCOL,BASE,AT,MSMOD,ROT,LIT8
5321 * Convert d to a numeric string using # until the result is zero.
5322 * Leave the double result on the stack for #> to drop.
5328 DIGS2 FDB DIG,OVER,OVER,OR,ZEQU,ZBRAN
5332 * ######>> screen 76 <<
5335 * Print n on the output device in the current conversion base,
5337 * right aligned in a field at least width wide.
5342 DOTR FDB DOCOL,TOR,STOD,FROMR,DDOTR
5347 * Print d on the output device in the current conversion base,
5349 * right aligned in a field at least width wide.
5354 DDOTR FDB DOCOL,TOR,SWAP,OVER,DABS,BDIGS,DIGS,SIGN
5355 FDB EDIGS,FROMR,OVER,SUB,SPACES,TYPE
5360 * Print d on the output device in the current conversion base,
5362 * in free format with trailing space.
5367 DDOT FDB DOCOL,ZERO,DDOTR,SPACE
5372 * Print n on the output device in the current conversion base,
5374 * in free format with trailing space.
5378 DOT FDB DOCOL,STOD,DDOT
5383 * Print signed word at adr, per DOT.
5387 QUEST FDB DOCOL,AT,DOT
5390 * ######>> screen 77 <<
5393 * Print out screen n as a field of ASCII,
5394 * with line numbers in decimal.
5395 * Needs a console more than 70 characters wide.
5400 LIST FDB DOCOL,DEC,CR,DUP,SCR,STORE,PDOTQ
5406 LIST2 FDB CR,I,THREE
5407 FDB DOTR,SPACE,I,SCR,AT,DLINE,XLOOP
5414 * Print comment lines (line 0, and line 1 if C/L < 41) of screens
5415 * from start to end.
5416 * Needs a console more than 70 characters wide.
5418 FCC 'INDE' ; 'INDEX'
5421 INDEX FDB DOCOL,CR,ONEP,SWAP,XDO
5422 INDEX2 FDB CR,I,THREE
5423 FDB DOTR,SPACE,ZERO,I,DLINE
5433 * List a printer page full of screens.
5434 * Line and screen number are in current base.
5435 * Needs a console more than 70 characters wide.
5437 FCC 'TRIA' ; 'TRIAD'
5440 TRIAD FDB DOCOL,THREE,SLASH,THREE,STAR
5441 FDB THREE,OVER,PLUS,SWAP,XDO
5443 FDB LIST,QTERM,ZBRAN
5453 * ######>> screen 78 <<
5456 * Alphabetically list the definitions in the current vocabulary.
5457 * Expects to output to printer, not TRS80 Color Computer screen.
5459 FCC 'VLIS' ; 'VLIST'
5462 VLIST FDB DOCOL,LIT8
5464 FDB OUT,STORE,CONTXT,AT,AT
5465 VLIST1 FDB OUT,AT,COLUMS,AT,LIT8
5469 FDB CR,ZERO,OUT,STORE
5470 VLIST2 FDB DUP,IDDOT,SPACE,SPACE,PFA,LFA,AT
5471 FDB DUP,ZEQU,QTERM,OR,ZBRAN
5476 * Need some utility stuff that isn't in the fig FORTH:
5478 * Emit dot if c is less than blank, else emit c
5480 FCC 'BEMI' ; 'BEMIT'
5484 FDB DUP,BL,LESS,ZBRAN
5492 * Output n in hexadecimal field width.
5498 FDB BASE,AT,TOR,HEX,DOTR,FROMR,BASE,STORE
5502 * Dump a line of 4 bytes in memory, in hex and as characters.
5504 FCC 'BLIN' ; 'BLINE'
5511 BLINEX FDB I,CAT,THREE,XDOTR,XLOOP
5517 BLINEC FDB I,CAT,BEMIT,XLOOP
5522 * Dump 4 byte lines from start to end.
5524 FCC 'BDUM' ; 'BDUMP'
5542 * Mostly for place holding (fig Forth).
5549 * Without the RTS, would misalign the stack.
5550 * NOOP NEXT a useful no-op
5551 ZZZZ FDB 0,0,0,0,0,0,0,0 end of rom program
5554 * These things, up through the lable 'REND', are overwritten
5555 * at time of cold load and should have the same contents
5558 * This can be moved whereever the bottom of the
5559 * user's dictionary is going to be put.
5563 FCC 'FORT' ; 'FORTH'
5566 FORTH FDB DODOES,DOVOC,$81A0,TASK-7
5569 FCC "Copyright 1979 Forth Interest Group, David Lion,"
5571 FCC "Parts Copyright 2019 Joel Matthew Rees"
5578 TASK FDB DOCOL,SEMIS
5580 REND EQU * ( first empty location in dictionary )
5581 RSIZE EQU *-RBEG ; So we can look at it.
5585 * "0 1 2 3 4 5 6 " ;
5586 * "0123456789012345678901234567890123456789012345678901234567890123" ;
5587 FCC "HEX ( THIS IS SOME TEST STUFF. ) " ; 0
5588 FCC ": STAR 42 EMIT ; ( With some randome comments. ) " ; 1
5589 FCC ": STARS 0 DO I EMIT LOOP ; ;S " ; 2
5603 * "0 1 2 3 4 5 6 " ;
5604 * "0123456789012345678901234567890123456789012345678901234567890123" ;
5605 FCC " more test data 2 3 4 5 6 " ; 0
5606 FCC "0123456789012345678901234567890123456789012345678901234567890123" ; 1
5607 FCC "Test data for the RAM disc emulator buffers. " ; 2