4 * ASSEMBLY SOURCE LISTING
8 * WITH COMPILER SECURITY
9 * AND VARIABLE LENGTH NAMES
12 * Adapted by Joel Matthew Rees
13 * from fig-FORTH for 6800 by Dave Lion, et. al.
15 * This free/libre/open source publication is provided
16 * through the courtesy of:
21 * and other interested parties.
24 * P.O. Box 8231 - San Jose, CA 95155 - (408) 277-0668
25 * URL: http://www.forth.org
26 * Further distribution must include this notice.
28 NAM Copyright: FORTH Interest Group, original authors, and Joel Matthew Rees
30 * filename fig-forth-auto6809opt.asm
31 * === FORTH-6809 {date} {time}
34 * Permission is hereby granted, free of charge, to any person obtaining a copy
35 * of this software and associated documentation files (the "Software"), to deal
36 * in the Software without restriction, including without limitation the rights
37 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38 * copies of the Software, and to permit persons to whom the Software is
39 * furnished to do so, subject to the following conditions:
41 * The above copyright notice and this permission notice shall be included in
42 * all copies or substantial portions of the Software.
44 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
52 * "Associated documentation" for this declaration of license
53 * shall be interpreted to include only the comments in this file,
54 * or, if the code is split into multiple files,
55 * all files containing the complete source.
57 * This is the MIT model license, as published by the Open Source Consortium,
58 * with associated documentation defined.
59 * It was chosen to reflect the spirit of the original
60 * terms of use, which used archaic legal terminology.
63 * Authors of the 6800 model:
64 * === Primary: Dave Lion,
68 * === The Forth Interest Group
70 * === San Carlos, CA 94070
72 * === Unbounded Computing
73 * === 1134-K Aster Ave.
74 * === Sunnyvale, CA 94086
76 NATWID EQU 2 ; bytes per natural integer/pointer
77 * The original version was developed on an AMI EVK 300 PROTO
78 * system using an ACIA for the I/O.
79 * This version is developed targeting the Tandy Color Computer.
82 * is done in three subroutines:
83 * PEMIT ( word # 182 )
87 * The FORTH words for disc related I/O follow the model
88 * of the FORTH Interest Group, but have not yet been
89 * tested using a real disc.
91 * Addresses in the 6800 implementation reflect the fact that,
92 * on the development system, it was convenient to
93 * write-protect memory at hex 1000, and leave the first
94 * 4K bytes write-enabled. As a consequence, code from
95 * location $1000 to lable ZZZZ could be put in ROM.
96 * Minor deviations from the model were made in the
97 * initialization and words ?STACK and FORGET
98 * in order to do this.
99 * Those deviations will be altered in this
100 * implementation for the 6809 -- Color Computer.
103 * MEMORY MAP for this 16K|32K system:
104 * ( delineated so that systems with 4k byte write-
105 * protected segments can write protect FORTH )
107 * addr. contents pointer init by
108 * **** ******************************* ******* ******
111 * ACIAC EQU $FBCE the ACIA control address and
112 * ACIAD EQU ACIAC+1 data address for PROTO
114 MEMT32 EQU $7FFF ; Theoretical absolute end of all ram
115 MEMT16 EQU $3FFF ; 16K is too tight until we no longer need disc emulation.
122 * substitute for disc mass memory
123 RAMSCR EQU 8 ; addresses calculate as 2 (Too much for 16K in RAM only.)
126 MASSLO EQU MASSHI-RAMSCR*SCRSZ+1
131 * "end" of "usable ram" (If disc mass memory emulation is removed, actual end.)
136 USERSZ EQU 256 ; (Addressable by DP, must be 256 on even boundary)
137 USER16 EQU 1 ; We can change these for ROMPACK or 64K.
138 USER32 EQU 2 ; maybe?
140 USERLO EQU MEMEND-USERSZ*USERCT
143 * user tables of variables
144 * registers & pointers for the virtual machine
145 * scratch area for potential use in something, maybe?
149 * This is a really awkward place to define the disk buffer records.
151 * 4 buffer sectors of VIRTUAL MEMORY
152 NBLK EQU 4 ; # of disc buffer blocks for virtual memory
153 * Should NBLK be SCRSZ/SECTSZ?
154 * each block is SECTSZ+SECTRL bytes in size,
155 * holding SECTSZ characters
157 SECTRL EQU 2*NATWID ; Currently held sector number, etc.
158 BUFSZ EQU (SECTSZ+SECTRL)*NBLK
159 BUFBAS EQU USERLO-BUFSZ
160 * *BUG* SECTRL is hard-wired into several definitions.
161 * It will take a bit of work to ferret them out.
162 * It is too small, and it should not be hard-wired.
163 * SECTSZ was also hard-wired into several definitions,
164 * will I find them all?
170 * Don't want one return too many to destroy the disc buffers.
173 * 32D8|71D8 <== RP RINIT
175 IRP EQU BUFBAS-RPBUMP
177 RSTK16 EQU $50*NATWID ; 80 max levels nesting calls
178 RSTK32 EQU $90*NATWID ; 144 max
183 SFTBND EQU IRP-RSTKSZ ; (false boundary between TIB and return stack)
185 * holds up to TIBSZ characters
186 * and is scanned upward by IN
189 ITIB EQU SFTBND-TIBSZ
191 * 3148|6FB8 <== IN TIB
193 * Don't want terminal input and parameter underflow collisions
198 * 3140|6FB0 <== SP SP0,SINIT
200 * | grows downward from 3140|6FB0
205 * I DICTIONARY grows upward
207 * >>>>>>--------Two words to start RAMmable dictionary--------<<<<<<
210 * ???? end of ram-dictionary. <== DICTPT DPINIT
213 * ???? "FORTH" ( a word ) <=, <== CONTEXT
215 * start of ram-dictionary.
217 * >>>>>> memory from here up must be in RAM area <<<<<<
220 * 6k of romable "FORTH" <== IP ABORT
222 * the VIRTUAL FORTH MACHINE
224 * 1208 initialization tables
225 * 1204 <<< WARM START ENTRY >>>
226 * 1200 <<< COLD START ENTRY >>>
227 * 1200 lowest address used by FORTH
232 * >>>>>> memory from here down left alone <<<<<<
233 * >>>>>> so we can safely call ROM routines <<<<<<
239 * CONVENTIONS USED IN THIS PROGRAM ARE AS FOLLOWS :
241 * IP (hardware Y) points to the current instruction ( pre-increment mode )
242 * RP (hardware S) points to last return address pushedin return stack
243 * SP (hardware U) points to last byte pushed in data stack
245 * Y must be IP when NEXT is entered (if using the inner loop).
247 * When A and B hold one 16 bit FORTH data word,
248 * A contains the high byte, B, the low byte.
250 * UP (hardware DP) is the base of per-task ("user") variables.
251 * (Be careful of the stray semantics of "user".)
253 * W (hardware X) is the pointer to the "code field" address of native CPU
254 * machine code to be executed for the definition of the dictionary word
255 * to be executed/currently executing.
256 * The following natural integer (word) begins any "parameter section"
257 * (body) -- similar to a "this" pointer, but not the same.
258 * It may be native CPU machine code, or it may be a global variable,
259 * or it may be a list of Forth definition words (addresses).
262 * This implementation uses the native subroutine architecture
263 * rather than a postponed-push call that the 6800 model VM uses
264 * to save code and time in leaf routines.
266 * This should allow directly calling many of the Forth words
267 * from assembly language code.
268 * (Be aware of the need for a valid W in some cases.)
269 * It won't allow mixing assembly language directly into Forth word lists.
273 * 0 is false, anything else is true.
274 * Most places in this model that set a boolean flag set true as 1.
275 * This is in contrast to many models that set a boolean flag as -1.
280 * This system is shown with one user (task),
281 * but additional users (tasks) may be added
282 * by allocating additional user tables:
286 UBASEX RMB USERSZ data table for extra users
288 * Some of this stuff gets initialized during
289 * COLD start and WARM start:
290 * [ names correspond to FORTH words of similar (no X) name ]
294 * A few useful VM variables
295 * Will be removed when they are no longer needed.
296 * All are replaced by 6809 registers.
298 N RMB 10 used as scratch by (FIND),ENCLOSE,CMOVE,EMIT,KEY,
299 * SP@,SWAP,DOES>,COLD
302 * These locations are used by the TRACE routine :
304 TRLIM RMB 1 the count for tracing without user intervention
305 TRACEM RMB 1 non-zero = trace mode
306 BRKPT RMB 2 the breakpoint address at which
307 * the program will go into trace mode
308 VECT RMB 2 vector to machine code
309 * (only needed if the TRACE routine is resident)
312 * Registers used by the FORTH virtual machine:
316 W RMB 2 the instruction register points to 6800 code
317 * This is not exactly accurate. Points to the definiton body,
318 * which is native CPU machine code when it is native CPU machine code.
319 * IP RMB 2 the instruction pointer points to pointer to 6800 code
320 * RP RMB 2 the return stack pointer
321 * UP RMB 2 the pointer to base of current user's 'USER' table
322 * ( altered during multi-tasking )
324 *UORIG RMB 6 3 reserved variables
325 RMB 6 3 reserved variables
326 XSPZER RMB 2 initial top of data stack for this user
327 XRZERO RMB 2 initial top of return stack
328 XTIB RMB 2 start of terminal input buffer
329 XWIDTH RMB 2 name field width
330 XWARN RMB 2 warning message mode (0 = no disc)
331 XFENCE RMB 2 fence for FORGET
332 XDICTP RMB 2 dictionary pointer
333 XVOCL RMB 2 vocabulary linking
334 XBLK RMB 2 disc block being accessed
335 XIN RMB 2 scan pointer into the block
336 XOUT RMB 2 cursor position
337 XSCR RMB 2 disc screen being accessed ( O=terminal )
338 XOFSET RMB 2 disc sector offset for multi-disc
339 XCONT RMB 2 last word in primary search vocabulary
340 XCURR RMB 2 last word in extensible vocabulary
341 XSTATE RMB 2 flag for 'interpret' or 'compile' modes
342 XBASE RMB 2 number base for I/O numeric conversion
343 XDPL RMB 2 decimal point place
345 XCSP RMB 2 current stack position, for compile checks
348 XDELAY RMB 2 carriage return delay count
349 XCOLUM RMB 2 carriage width
350 IOSTAT RMB 2 last acia status from write/read
361 * end of user table, start of common system variables
365 * These need to be moved to where they will be
366 * initialized globals in variable space, not in the USER table.
367 * Or, more accurately, need to be turned into monitored or semaphored resources.
373 * The FORTH program ( address $1200 to about $27FF ) will be written
374 * so that it can be in a ROM, or write-protected if desired,
375 * but right now we're just getting it running.
378 * ######>> screen 3 <<
380 ***************************
381 ** C O L D E N T R Y **
382 ***************************
386 ***************************
387 ** W A R M E N T R Y **
388 ***************************
390 * JMP WENT warm-start code, keeps current dictionary intact
391 LBSR WENT warm-start code, keeps current dictionary intact
395 ******* startup parmeters **************************
397 FDB $6809,0000 cpu & revision
398 FDB 0 topmost word in FORTH vocabulary
399 * BACKSP FDB $7F backspace character for editing
400 BACKSP FDB $08 backspace character for editing
401 UPINIT FDB UORIG initial user area
402 * UPINIT FDB UORIG initial user area
403 SINIT FDB ISP ; initial top of data stack
404 * SINIT FDB ORIG-$D0 initial top of data stack
405 RINIT FDB IRP ; initial top of return stack
406 * RINIT FDB ORIG-2 initial top of return stack
407 FDB ITIB ; terminal input buffer
408 * FDB ORIG-$D0 terminal input buffer
409 FDB 31 initial name field width
410 FDB 0 initial warning mode (0 = no disc)
411 FENCIN FDB REND initial fence
412 DPINIT FDB REND cold start value for DICTPT
413 BUFINT FDB BUFBAS Start of the disk buffers area
414 VOCINT FDB FORTH+4*NATWID
415 COLINT FDB TIBSZ initial terminal carriage width
416 DELINT FDB 4 initial carriage return delay
417 ****************************************************
421 * ######>> screen 13 <<
422 * These were of questionable use anyway,
423 * kept here now to satisfy the assembler and show hints.
424 * They're too much trouble to use with native subroutine call anyway.
425 * PULABX PULS A ; 24 cycles until 'NEXT'
427 * PULABX PULU A,B ; ?? cycles until 'NEXT'
428 * STABX STA 0,X 16 cycles until 'NEXT'
430 * STABX STD 0,X ; ?? cycles until 'NEXT'
432 * GETX LDA 0,X 18 cycles until 'NEXT'
434 * GETX LDD 0,X ?? cycles until 'NEXT'
435 * PUSHBA PSHS B ; 8 cycles until 'NEXT'
437 * PUSHBA PSHU A,B ; ?? cycles until 'NEXT'
441 * "NEXT" takes ?? cycles if TRACE is removed,
443 * and ?? cycles if trace is present and NOT tracing.
445 * = = = = = = = t h e v i r t u a l m a c h i n e = = = = =
447 * NEXT itself might just completely go away.
448 * About the only reason to keep it is to allowing executing a list
449 * which allows a cheap TRACE routine.
451 * NEXT is a loop which implements the Forth VM.
452 * It basically cycles through calling the code out of code lists,
454 * Using a native CPU return for this uses a few extra cycles per call,
455 * compared to simply jumping to each definition and jumping back
456 * to the known beginning of the loop,
457 * but the loop itself is really only there for convenience.
459 * This implementation uses the native subroutine call,
460 * to break the wall between Forth code and non-Forth code.
463 * LEAX 1,X ; pre-increment mode
466 NEXT ; IP is Y, push before using, pull before you come back here.
468 * NEXT2 LDX 0,X get W which points to CFA of word to be done
469 NEXT2 LDX ,Y++ get W which points to CFA of word to be done
472 * But NEXT2 is too much trouble to use with subroutine threading anyway.
474 NEXT3 ; W is X until you use X for something else. (TOS points back here.)
475 * But NEXT3 is too much trouble to use with subroutine threading anyway.
476 * LDX 0,X get VECT which points to executable code
478 * The next instruction could be patched to JMP TRACE =
479 * if a TRACE routine is available: =
483 JSR [,X] ; Saving the postinc cycles,
484 * ; but X must be bumped NATWID to the parameters.
486 * JMP TRACE ( an alternate for the above )
487 * BSR DBGREG ( an alternate for the above )
488 * In other words, with the call and the NOP,
489 * there is room to patch the call with a JMP to your TRACE
490 * routine, which you have to provide.
512 DBGNrt PULS CC,D,X,Y,PC
523 ADDB #$C7 ; ($40-$39)-$40
540 DBGREG PSHS U,Y,X,DP,B,A,CC
555 LDD 3*NATWID+4,S ; PC:505
562 LDD 2*NATWID+4,S ; U:50E
566 LDD 1*NATWID+4,S ; Y:513
568 LDD 0*NATWID+4,S ; X at 517
580 LDD [3*NATWID+4,S] ; PC
586 LDD [2*NATWID+4,S] ; U
590 LDD [1*NATWID+4,S] ; Y
592 LDD [0*NATWID+4,S] ; X
665 DBGRdU LDY 2*NATWID+4,S
675 DBGRrt PULS CC,A,B,DP,X,Y,U,PC
676 DBGRLB FCC 'DPCC PC S U Y X A B '
682 * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
689 * Pushes the following natural width integer from the instruction stream
690 * as a literal, or immediate value.
695 * FDB LITERAL-TO-BE-PUSHED
698 * In native processor code, there should be a better way, use that instead.
699 * More specifically, DO NOT CALL THIS from assembly language code.
700 * (Note that there is no compile-only flag in the fig model.)
702 * See (FIND), or PFIND , for layout of the header format.
705 FCC 'LI' ; 'LIT' : NOTE: this is different from LITERAL
706 FCB $D4 ; 'T'|'\x80' ; character code for T, with high bit set.
707 FDB 0 ; link of zero to terminate dictionary scan
708 LIT FDB *+NATWID ; Note also that LIT is meaningless in native code.
720 * ######>> screen 14 <<
723 * Pushes the following byte from the instruction stream
724 * as a literal, or immediate value.
729 * FCB LITERAL-TO-BE-PUSHED
732 * If this is kept, it should have a header for TRACE to read.
733 * If the data bus is wider than a byte, you don't want to do this.
734 * Byte shaving like this is often counter-productive anyway.
735 * Changing the name to LIT8, hoping that will be more understandable.
736 * Also, see comments for LIT.
737 * (Note that there is no compile-only flag in the fig model.)
739 FCC 'LIT' ; 'LIT8' : NOTE: this is different from LITERAL
742 LIT8 FDB *+NATWID (this was an invisible word, with no header)
743 LDB ,Y+ ; This also is meaningless in native code.
755 * off is offset in video buffer area.
757 FCC 'SHOWTO' ; 'SHOWTOS'
786 * Jump to address on stack. Used by the "outer" interpreter to
787 * interactively invoke routines.
788 * Might be useful to have EXECUTE test the pointer, as done in BIF-6809.
790 FCC 'EXECUT' ; 'EXECUTE'
794 PULU X ; Gotta have W anyway, just in case.
795 JMP [,X] ; Tail return.
797 * LDX 0,X get code field address (CFA)
798 * LEAS 1,S ; pop stack
802 * ######>> screen 15 <<
805 * Add the following word from the instruction stream to the
806 * instruction pointer (Y++). Causes a program branch in Forth code stream.
808 * In native processor code, there should be a better way, use that instead.
809 * More specifically, DO NOT CALL THIS from assembly language code.
810 * This is only for Forth code stream.
811 * Also, see comments for LIT.
813 FCC 'BRANC' ; 'BRANCH'
816 BRAN FDB ZBYES ; Go steal code in ZBRANCH
818 * Moving code around to optimize the branch taking case in 0BRANCH.
819 ZBNO LEAY NATWID,Y ; No branch.
823 * BRANCH if flag is zero.
825 * In native processor code, there should be a better way, use that instead.
826 * More specifically, DO NOT CALL THIS from assembly language code.
827 * This is only for Forth code stream.
828 * Also, see comments for LIT.
830 FCC '0BRANC' ; '0BRANCH'
837 LEAY D,Y ; IP is postinc
841 * PSHS B ; ** emulating ABA:
845 * ZBYES LDX IP Note: code is shared with BRANCH, (+LOOP), (LOOP)
853 * ZBNO LDX IP no branch. This code is shared with (+LOOP), (LOOP).
854 * LEAX 1,X ; jump over branch delta
859 * ######>> screen 16 <<
861 * ( --- ) ( limit index *** limit index+1) C
862 * ( limit index *** )
863 * Counting loop primitive. The counter and limit are the top two
864 * words on the return stack. If the updated index/counter does
865 * not exceed the limit, a branch occurs. If it does, the branch
866 * does not occur, and the index and limit are dropped from the
869 * In native processor code, there should be a better way, use that instead.
870 * More specifically, DO NOT CALL THIS from assembly language code.
871 * This is only for Forth code stream.
872 * Also, see comments for LIT.
874 FCC '(LOOP' ; '(LOOP)'
878 LDD #1 ; Borrowing from BIF-6809.
879 XLOOPA ADDD NATWID,S ; Dodge the return address.
882 BMI ZBYES ; pseudo-signed-unsigned
884 LDX ,S ; synthetic return
885 LEAS 3*NATWID,S ; Clean up the index and limit.
888 * LDB #1 get set to increment counter by 1 (Clears N.)
889 * BRA XPLOP2 go steal other guy's code!
892 * ( n --- ) ( limit index *** limit index+n ) C
893 * ( limit index *** )
894 * Loop with a variable increment. Terminates when the index
895 * crosses the boundary from one below the limit to the limit. A
896 * positive n will cause termination if the result index equals the
897 * limit. A negative n must cause the index to become less than
898 * the limit to cause loop termination.
900 * Note that the end conditions are not symmetric around zero.
902 * In native processor code, there should be a better way, use that instead.
903 * More specifically, DO NOT CALL THIS from assembly language code.
904 * This is only for Forth code stream.
905 * Also, see comments for LIT.
907 FCC '(+LOOP' ; '(+LOOP)'
910 XPLOOP FDB *+NATWID ; Borrowing from BIF-6809.
912 BPL XLOOPA ; Steal plain loop code for forward count.
913 ADDD NATWID,S ; Dodge the return address
916 BPL ZBYES ; pseudo-signed-unsigned
917 BRA XLOOPN ; This path might be less time-sensitive.
919 * This should work, but I want to use tested code.
920 * PULU A,B ; Get the increment.
921 * XPLOP2 PULS X ; Pre-clear the return stack.
922 * PSHU A ; Save the direction in high bit.
925 * SUBD NATWID,S ; Check limit.
927 ** I think this should work:
928 * EORA ,U+ ; dir < 0 and (count - limit) >= 0
929 * BPL XPLONO ; or dir >= 0 and (count - limit) < 0
931 * LEAY D,Y ; IP is postinc
933 * XPLONO LEAS 2*NATWID,S
934 * JMP ,X ; synthetic return
936 * This definitely should work:
937 * TST ,U+ ; Get the sign
942 * LEAY D,Y ; IP is postinc
944 * XPLOF CMPD NATWID,S
946 * XPLONO LEAS 2*NATWID,S
947 * JMP ,X ; synthetic return
949 * 6800 Probably could have used the exclusive-or method, too.:
950 * PULS A ; get increment
953 * BPL XPLOF forward looping
959 * BRA XPLONO fall through
963 * ADDB 3,X add it to counter
965 * STB 3,X store new counter value
974 * XPLONO LEAX 1,X ; done, don't branch back
979 * BRA ZBNO use ZBRAN to skip over unused delta
981 * ######>> screen 17 <<
983 * ( limit index --- ) ( *** limit index )
984 * Move the loop parameters to the return stack. Synonym for D>R.
989 XDO FDB *+NATWID This is the RUNTIME DO, not the COMPILING DO
990 LDX ,S ; Save the return address.
993 PULU A,B ; Maintain order.
995 JMP ,X ; synthetic return
1014 * ( --- index ) ( limit index *** limit index )
1015 * Copy the loop index from the return stack. Synonym for R.
1020 LDD NATWID,S ; Dodge return address.
1028 * ######>> screen 18 <<
1030 * ( c base --- false )
1031 * ( c base --- n true )
1032 * Translate C in base, yielding a translation valid flag. If the
1033 * translation is not valid in the specified base, only the false
1036 FCC 'DIGI' ; 'DIGIT'
1039 DIGIT FDB *+NATWID NOTE: legal input range is 0-9, A-Z
1040 LDD NATWID,U ; Check the whole thing.
1041 SUBD #$30 ; ascii zero
1042 BMI DIGIT2 IF LESS THAN '0', ILLEGAL
1044 BMI DIGIT0 IF '9' OR LESS
1046 BMI DIGIT2 if less than 'A'
1048 BPL DIGIT2 if greater than 'Z'
1049 SUBD #7 translate 'A' thru 'F'
1050 DIGIT0 CMPD ,U ; Check the base.
1051 BPL DIGIT2 if not less than the base
1052 STD NATWID,U ; Store converted digit. (High byte known zero.)
1053 LDD #1 ; set valid flag
1054 DIGIT1 STD ,U ; store the flag
1056 DIGIT2 LDD #0 ; set not valid flag
1057 LEAU NATWID,U ; pop base
1061 * SUBA #$30 ascii zero
1062 * BMI DIGIT2 IF LESS THAN '0', ILLEGAL
1064 * BMI DIGIT0 IF '9' OR LESS
1066 * BMI DIGIT2 if less than 'A'
1068 * BPL DIGIT2 if greater than 'Z'
1069 * SUBA #7 translate 'A' thru 'F'
1071 * BPL DIGIT2 if not less than the base
1073 * STA 3,X store digit
1074 * DIGIT1 STB 1,X store the flag
1078 * LEAS 1,S ; pop bottom number
1080 * STB 0,X make sure both bytes are 00
1083 * ######>> screen 19 <<
1085 * The word definition format in the dictionary:
1087 * (Symbol names are bracketed by bytes with the high bit set, rather than linked.)
1089 * NFA (name field address):
1090 * char-count + $80 Length of symbol name, flagged with high bit set.
1091 * char 1 Characters of symbol name.
1094 * char n + $80 symbol termination flag (char set < 128 code points)
1095 * LFA (link field address):
1096 * link high byte \___pointer to previous word in list
1097 * link low byte / -- Combined allocation/dictionary list. --
1098 * CFA (code field address):
1099 * CFA high byte \___pointer to native CPU machine code
1100 * CFA low byte / -- Consider this the characteristic code. --
1101 * PFA (parameter field address):
1102 * parameter fields -- Machine code for low-level native machine CPU code,
1103 * " instruction list for high-level Forth code,
1104 * " constant data for constants, pointers to per task variables,
1105 * " space for variables, for global variables, etc.
1107 * In the case of native CPU machine code, the address at CFA will be PFA.
1109 * Definition attributes:
1110 FIMMED EQU $40 ; Immediate word flag.
1111 FSMUDG EQU $20 ; Smudged => definition not ready.
1112 CTMASK EQU ($FF&(^($80|FIMMED))) ; For unmasking the length byte.
1113 * Note that the SMUDGE bit is not masked out.
1115 * But we really want more (Thinking for a new model, need one more byte):
1116 * FCOMPI EQU $10 ; Compile-time-only.
1117 * FASSEM EQU $08 ; Assembly-language code only.
1118 * F4THLV EQU $04 ; Must not be called from assembly language code.
1119 * These would require some significant adjustments to the model.
1120 * We also want to put the low-level VM stuff in its own vocabulary.
1123 * (FIND) ( name vocptr --- locptr length true )
1124 * ( name vocptr --- false )
1125 * Search vocabulary for a symbol called name.
1126 * name is a pointer to a high-bit bracket string with length head.
1127 * vocptr is a pointer to the NFA of the tail-end (LATEST) definition
1128 * in the vocabulary to be searched.
1129 * Hidden (SMUDGEd) definitions are lexically not equal to their name strings.
1131 FCC '(FIND' ; '(FIND)'
1135 PSHS Y ; Have to track two pointers.
1136 * Use the stack and registers instead of temp area N.
1137 PA0 EQU NATWID ; pointer to the length byte of name being searched against
1138 PD EQU 0 ; pointer to NFA of dict word being checked
1142 LDX PD,U ; Start in on the vocabulary (NFA).
1143 PFNDLP LDY PA0,U ; Point to the name to check against.
1144 LDB ,X+ ; get dict name length byte
1145 TFR B,A ; Save it in case it matches.
1148 CMPB ,Y+ ; Compare lengths
1152 TSTB ; ; Is high bit of character in dictionary entry set?
1156 ANDB #$7F ; Clear high bit from dictionary.
1157 CMPB ,Y+ ; Compare "last" characters.
1159 BEQ FOUND ; Matches even if dictionary actual length is shorter.
1160 PFNDLN LDX ,X++ ; Get previous link in vocabulary.
1162 BNE PFNDLP ; Continue if link not=0
1165 LEAU NATWID,U ; Return only false flag.
1172 PFNDCH CMPB ,Y+ ; Compare characters.
1176 PFNDSC LDB ,X+ ; scan forward to end of this name in dictionary
1184 FOUND LEAX 2*NATWID,X
1198 * NOP ; Probably leftovers from a debugging session.
1200 * PD EQU N ptr to dict word being checked
1206 * PFIND0 PULS A ; loop to get arguments
1213 * PFNDLP LDB 0,X get count dict count
1219 * LDA 0,X get count from arg
1221 * STX PA intialize PA
1222 * PSHS B ; ** emulating CBA:
1223 * CMPA ,S+ ; compare lengths
1233 * TSTB ; is dict entry neg. ?
1235 * ANDB #$7F clear sign
1236 * PSHS B ; ** emulating CBA:
1239 * PFNDLN LDX 0,X get new link
1240 * BNE PFNDLP continue if link not=0
1247 * PFNDCH PSHS B ; ** emulating CBA:
1251 * PFNDSC LDB 0,X scan forward to end of this name
1258 * FOUND LDA PD compute CFA
1271 * PSHS A ; Left over from a stray copy-paste, I guess.
1277 * ######>> screen 20 <<
1279 * ( buffer ch --- buffer symboloffset delimiteroffset scancount )
1280 * ( buffer ch --- buffer symboloffset nuloffset scancount ) ( Scan count == nuloffset )
1281 * ( buffer ch --- buffer nuloffset onepast scancount )
1282 * Scan buffer for a symbol delimited by ch or ASCII NUL,
1283 * return the length of the buffer region scanned,
1284 * the offset to the trailing delimiter,
1285 * and the offset of the first character of the symbol.
1286 * Leave the buffer on the stack.
1287 * Scancount is also offset to first character not yet looked at.
1288 * If no symbol in buffer, scancount and symboloffset point to NUL
1289 * and delimiteroffset points one beyond for some reason.
1290 * On trailing NUL, delimiteroffset == scancount.
1291 * (Buffer is the address of the buffer array to scan.)
1292 * (This is a bit too tricky, really.)
1294 FCC 'ENCLOS' ; 'ENCLOSE'
1298 LDA 1,U ; Delimiter character to match against in A.
1299 LDX NATWID,U ; Buffer to scan in.
1300 CLRB ; Initialize offset. (Buffer < 256 wide!)
1301 * Scan to a non-delimiter or a NUL
1302 ENCDEL TST B,X ; NUL ?
1304 CMPA B,X ; Delimiter?
1306 INCB ; count character
1308 * Found first character. Save the offset.
1309 ENC1ST STB 1,U ; Found first non-delimiter character --
1310 CLR ,U ; store the count, zero high byte.
1311 * Scan to a delimiter or a NUL
1312 ENCSYM TST B,X ; NUL ?
1314 CMPA B,X ; delimiter?
1318 * Found end of symbol. Push offset to delimiter found.
1319 ENCEND CLRA ; high byte -- buffer < 255 wide!
1320 PSHU A,B ; Offset to seen delimiter.
1321 * Advance and push address of next character to check.
1322 ADDD #1 ; In case offset was 255.
1325 * Found NUL before non-delimiter, therefore there is no word
1326 ENCNUL CLRA ; high byte -- buffer < 255 wide!
1327 STD ,U ; offset to NUL.
1328 ADDD #1 ; Point after NUL to allow (FIND) to match it.
1330 SUBD #1 ; Next is not passed NUL.
1331 PSHU A,B ; Stealing code will save only one byte.
1333 * Found NUL following the word instead of delimiter.
1338 PSHU A,B ; Save offset to first after symbol (NUL)
1340 PSHU A,B ; and count scanned.
1345 * FC means offset (bytes) to First Character of next word
1346 * EW " " to End of Word
1347 * NC " " to Next Character to start next enclose at
1348 * ENCLOS FDB *+NATWID
1350 * PULS B ; now, get the low byte, for an 8-bit delimiter
1354 * * wait for a non-delimiter or a NUL
1357 * PSHS B ; ** emulating CBA:
1358 * CMPA ,S+ ; CHECK FOR DELIM
1363 * * found first character. Push FC
1364 * ENC1ST LDA N found first char.
1368 * wait for a delimiter or a NUL
1371 * PSHS B ; ** emulating CBA:
1372 * CMPA ,S+ ; ckech for delim.
1377 * * found EW. Push it
1382 * * advance and push NC
1385 * found NUL before non-delimiter, therefore there is no word
1386 * ENCNUL LDB N found NUL
1390 * BRA ENC0TR+2 ; ********** POTENTIAL BUG HERE *******
1391 * ******** Should use labels in case opcodes change! ********
1392 * found NUL following the word instead of SPACE
1396 * ENCL8 LDB N save NC
1401 * ######>> screen 21 <<
1402 * The next 4 words call system dependant I/O routines
1403 * which are listed after word "-->" ( lable: "arrow" )
1404 * in the dictionary.
1408 * Write c to the output device (screen or printer).
1409 * ROM Uses the ECB device number at address $6F,
1410 * -2 is printer, 0 is screen.
1417 LBSR PEMIT ; PEMIT expects the character in D.
1426 * INC XOUT+1-UORIG,X
1428 * ****WARNING**** HARD OFFSET: *+4 ****
1435 * Wait for a key from the keyboard.
1436 * If the key is BREAK, set the high byte (result $FF03).
1442 LBSR PKEY ; PKEY leaves the key/break code in D.
1453 * Scan keyboard, but do not wait.
1454 * Return 0 if no key,
1455 * BREAK ($ff03) if BREAK is pressed,
1456 * or key currently pressed.
1458 FCC '?TERMINA' ; '?TERMINAL'
1462 LBSR PQTER ; PQTER leaves the flag/key in D.
1467 * JMP PUSHBA stack the flag
1471 * EMIT a Carriage Return (ASCII CR).
1477 LBRA PCR ; Nothing really to do here.
1481 * ######>> screen 22 <<
1483 * ( source target count --- )
1484 * Copy/move count bytes from source to target.
1485 * Moves ascending addresses,
1486 * so that overlapping only works if the source is above the destination.
1488 FCC 'CMOV' ; 'CMOVE' : source, destination, count
1492 * Another way ; takes ( 42+17*count+9*(count/256) cycles )
1494 SUBD ,U++ ; #2~9 ; invert the count
1505 CMOVEX PULS A,Y,PC ; #2~10
1525 * One way: ; takes ( 37+17*count+9*(count/256) cycles )
1526 * PSHS Y ; #2~7 ; Gotta have our pointers.
1529 * PULU D,X,Y ; #2~11
1530 * PSHS A ; #2~6 ; Gotta have our pointers.
1541 * BPL CMOVLP ; #2~3 ; If this actually works, it is limited to 32k here.
1543 * PULS A,Y,PC ; #2~10
1544 * Yet another way ; takes ( 37+29*count cycles )
1546 * LDX NATWID,U ; #2~6
1547 * LDY NATWID,U ; #3~7
1557 * LEAU 3*NATWID,U ; #2~5
1559 * Yet another way ; takes ( 44+24*odd+33*count/2 cycles )
1561 * LDX NATWID,U ; #2~6
1562 * LDY 2*NATWID,U ; #3~7
1580 * LEAU 3*NATWID,U ; #2~5
1582 * From the 6800 model:
1583 * CMOVE FDB *+2 takes ( 43+47*count cycles ) on 6800
1587 * STA 0,X move parameters to scratch area
1609 * ######>> screen 23 <<
1612 * Multiplies the top two unsigned integers,
1613 * yielding a double integer product.
1620 LDA 2*NATWID+1,U ; least
1624 LDA 2*NATWID,U ; most
1628 LDD 2*NATWID+1,U ; first inner (u2 lo, u1 hi)
1634 LDA 2*NATWID,U ; second inner (u2 hi)
1635 LDB 3*NATWID,U ; (u1 lo)
1652 * The following is a subroutine which
1653 * multiplies top 2 words on stack,
1654 * leaving 32-bit result: high order word in A,B
1655 * low order word in 2nd word of stack.
1657 * USTARS LDA #16 bits/word counter
1662 * USTAR2 ROR 5,X shift multiplier
1670 * RORB ; shift result
1672 * USTAR4 LEAS 1,S ; dump counter
1675 * ######>> screen 24 <<
1677 * ( ud u --- uremainder uquotient )
1678 * Divides the top unsigned integer
1679 * into the second and third words on the stack
1680 * as a single unsigned double integer,
1681 * leaving the remainder and quotient (quotient on top)
1682 * as unsigned integers.
1684 * The smaller the divisor, the more likely dropping the high word
1685 * of the quotient loses significant bits. See M/MOD .
1694 LDD NATWID,U ; dividend
1695 USLDIV CMPD ,U ; divisor
1697 ANDCC #~1 ; carry clear
1700 ORCC #1 ; quotient, (carry set)
1701 USLBIT ROL 2*NATWID+1,U ; save it
1713 PULS A,PC ; Avoiding a LEAS 1,S by discarding A.
1726 * USL2 ANDCC #~$01 ; CLC :
1744 * JMP SWAP+4 reverse quotient & remainder
1746 * ######>> screen 25 <<
1749 * Bitwise and the top two integers.
1769 * Bitwise or the top two integers.
1789 * Bitwise exclusive or the top two integers.
1807 * ######>> screen 26 <<
1810 * Fetch the parameter stack pointer (before it is pushed).
1811 * This points at whatever was on the top of stack before.
1821 * STX N scratch area
1826 * ( whatever --- nothing )
1827 * Initialize the parameter stack pointer from the USER variable S0.
1828 * Effectively clears the stack.
1837 * LDX XSPZER-UORIG,X
1838 * TFR X,S ; TXS : watch it ! X and S are not equal on 6800.
1841 * ( whatever *** nothing )
1842 * Initialize the return stack pointer from the initialization table
1843 * instead of the user variable R0, for some reason.
1844 * Quite possibly, this should be from R0.
1845 * Effectively aborts all in process definitions, except the active one.
1846 * An emergency measure, to be sure.
1847 * The routine that calls this must never execute a return.
1848 * So this should never be executed from the terminal, I guess.
1849 * This is another that should be compile-time only, and in a separate vocabulary.
1855 PULS X ; But this guy has to return to his caller.
1858 * LDX RINIT initialize from rom constant
1864 * Pop IP from return stack (return from high-level definition).
1865 * Can be used in a screen to force interpretion to terminate.
1866 * Must not be executed when temporaries are saved on top of the return stack.
1872 PULS D,Y ; return address in D, and saved IP in Y.
1873 TFR D,PC ; Synthetic return.
1880 * LDX 0,X get address we have just finished.
1881 * JMP NEXT+2 increment the return address & do next word
1883 * ######>> screen 27 <<
1885 * ( limit index *** index index )
1886 * Force the terminating condition for the innermost loop by
1887 * copying its index to its limit.
1888 * Termination is postponed until the next
1889 * LOOP or +LOOP instruction is executed.
1890 * The index remains available for use until
1891 * the LOOP or +LOOP instruction is encountered.
1892 * Note that the assumption is that the current count is the correct count
1893 * to end at, rather than pushing the count to the final count.
1895 FCC 'LEAV' ; 'LEAVE'
1899 LDD NATWID,S ; Dodge the return address.
1912 * Move top of parameter stack to top of return stack.
1920 STD ,S ; Put it where the return address was.
1935 * Move top of return stack to top of parameter stack.
1955 * Copy the top of return stack to top of parameter stack.
1967 * ######>> screen 28 <<
1970 * Logically invert top of stack;
1971 * or flag true if top is zero, otherwise false.
1981 * Logically invert top of stack;
1982 * or flag true if top is zero, otherwise false.
2000 *ZEQU2 TFR S,X ; TSX :
2005 * Flag true if top is negative (MSbit set), otherwise false.
2018 * LDA #$80 check the sign bit
2027 * ######>> screen 29 <<
2029 * ( n1 n2 --- n1+n2 )
2030 * Add top two words.
2047 * ( d1 d2 --- d1+d2 )
2048 * Add top two double integers.
2064 * ANDCC #~$01 ; CLC :
2080 * Negate (two's complement) top of stack.
2082 FCC 'MINU' ; 'MINUS'
2091 * from 6800 model code:
2102 * Negate (two's complement) top two words on stack as a double integer.
2104 FCC 'DMINU' ; 'DMINUS'
2109 SUBD NATWID,U ; #2~7
2129 * ######>> screen 30 <<
2131 * ( n1 n2 --- n1 n2 n1 )
2132 * Push a copy of the second word on stack.
2148 * Discard the top word on stack.
2161 * ( n1 n2 --- n2 n1 )
2162 * Swap the top two words on stack.
2186 * Push a copy of the top word on stack.
2201 * ######>> screen 31 <<
2204 * Add the second word on stack to the word at the adr on top of stack.
2219 * PULS A ; get stack data
2221 * ADDB 1,X add & store low byte
2223 * ADCA 0,X add & store hi byte
2229 * Exclusive or byte at adr with low byte of top word.
2231 FCC 'TOGGL' ; 'TOGGLE'
2239 * Using the model code would be less likely to introduce bugs,
2240 * but that would sort-of defeat my purposes here.
2241 * Anyway, I can borrow from theoretically known good bif-6809 code
2242 * and it's fewer bytes and much faster code this way.
2244 * FDB DOCOL,OVER,CAT,XOR,SWAP,CSTORE
2247 * ######>> screen 32 <<
2250 * Replace address on stack with the word at the address.
2259 * LDX 0,X get address
2266 * Replace address on top of stack with the byte at the address.
2267 * High byte of result is clear.
2289 * Store second word on stack at address on top of stack.
2299 * LDX 0,X get address
2306 * Store low byte of second word on stack at address on top of stack.
2307 * High byte is ignored.
2318 * LDX 0,X get address
2327 * ######>> screen 33 <<
2330 * { : name sundry-activities ; } typical input
2331 * If executing (not compiling),
2332 * record the data stack mark in CSP,
2333 * Set the CONTEXT vocabulary to CURRENT,
2335 * set state to compile,
2336 * and compile the call to the trailing native CPU machine code DOCOL.
2338 * This would not be hard to flatten to native code.
2339 * But that's not the purpose of a model.
2343 COLON FDB DOCOL,QEXEC,SCSP,CURENT,AT,CONTXT,STORE
2347 * Here is the IP pusher for allowing
2348 * nested words in the virtual machine:
2349 * ( ;S is the equivalent un-nester )
2352 * Characteristic of a colon (:) definition.
2353 * Begins execution of a high-level definition,
2354 * i. e., nests the definition and begins processing icodes.
2355 * Mechanically, it pushes the IP (Y register)
2356 * and loads the Parameter Field Address of the definition which
2357 * called it into the IP.
2358 DOCOL LDD ,S ; Save the return address.
2359 STY ,S ; Nest the old IP.
2360 LEAY NATWID,X ; W still in X, bump to parameters, load as new IP.
2361 TFR D,PC ; synthetic return to interpret.
2363 * DOCOL LDX RP make room in the stack
2369 * STA 2,X Store address of the high level word
2370 * STB 3,X that we are starting to execute
2371 * LDX W Get first sub-word of that definition
2372 * JMP NEXT+2 and execute it
2376 * { : name sundry-activities ; } typical input
2377 * ERROR check data stack against mark in CSP,
2379 * unSMUDGE LATEST definition,
2380 * and set state to interpretation.
2381 FCB $C1 ; imnediate code
2384 SEMI FDB DOCOL,QCSP,COMPIL,SEMIS,SMUDGE,LBRAK
2387 * ######>> screen 34 <<
2390 * { value CONSTANT name } typical input
2393 * compile the constant value,
2394 * and compile the call to the trailing native CPU machine code DOCON.
2396 FCC 'CONSTAN' ; 'CONSTANT'
2399 CON FDB DOCOL,CREATE,SMUDGE,COMMA,PSCODE
2401 * Characteristic of a CONSTANT.
2402 * A CONSTANT simply loads its value from its parameter field
2403 * and pushes it on the stack.
2404 DOCON LDD NATWID,X ; Get the first natural width word of the parameter field.
2409 * LDB 3,X A & B now contain the constant
2412 * Not in model, needed for abstraction:
2414 * The byte width of objects on stack.
2416 FCC 'NATWI' ; 'NATWID'
2422 * Not in model, needed for abstraction:
2423 * Note that this is not defined as an INCREMENTER!
2424 * Coded to increment by the exact constant returned by NATWID
2425 * ( n --- n+NATWID )
2432 ADDD NATWCV,PCR ; Looking ahead, does not have to be PCRelative.
2435 * How this might have been done for 6800 model:
2436 * CLRA ; We know the natural width is less than 255, LOL.
2445 * { init VARIABLE name } typical input
2446 * Use CONSTANT to CREATE a header and compile the initial value, init,
2447 * then overwrite the characteristic to point to DOVAR.
2449 FCC 'VARIABL' ; 'VARIABLE'
2452 VAR FDB DOCOL,CON,PSCODE
2454 * Characteristic of a VARIABLE.
2455 * A VARIABLE pushes its PFA address on the stack.
2456 * The parameter field of a VARIABLE is the actual allocation of the variable,
2457 * so that pushing its address allows its contents to be @ed (fetched).
2458 * Ordinary arrays and strings that do not subscript themselves
2459 * may be allocated by defining a variable
2460 * and immediately ALLOTting the remaining needed space.
2461 * VARIABLES are global to all users,
2462 * and thus should be hidden in resource monitors, but aren't.
2463 DOVAR LEAX NATWID,X ; Point to the first natural width word of the parameters.
2469 * ADCA #0 A,B now contain the address of the variable
2474 * { uboffset USER name } typical input
2475 * CREATE a header and compile the unsigned byte offset in the per-USER table,
2476 * then overwrite the header with a call to DOUSER.
2477 * The USER is entirely responsible for maintaining allocation!
2482 USER FDB DOCOL,CON,PSCODE
2484 * Characteristic of a per-USER variable.
2485 * USER variables are similiar to VARIABLEs,
2486 * but are allocated (by hand!) in the per-user table.
2487 * A USER variable's parameter field contains its offset in the per-user table.
2488 DOUSER TFR DP,A ; Make a pointer to the direct page.
2490 * See Alternative -- alternatives start from this point.
2491 ADDD NATWID,X ; Add it to the offset to the per-user variable.
2493 TFR D,X ; Cache the pointer in X for the caller.
2495 * Hey, the per-user table could actually be larger than 256 bytes!
2496 * But we knew that. It's just not as esthetic to calculate it this way.
2498 * LDX NATWID,X ; Keep the offset
2499 * EXG D,X ; Prepare for EA
2504 * PSHS Y ; Get Y free for calculations.
2505 * TFR D,Y ; Y points to the UP base
2506 * LDD NATWID,X ; Get the offset
2507 * LEAX D,Y ; Leave the pointer cached in X.
2511 * From the 6800 model:
2512 * DOUSER LDX W get offset into user's table
2515 * ADDB UP+1 add to users base address
2517 * JMP PUSHBA push address of user's variable
2519 * ######>> screen 35 <<
2554 * ASCII SPACE character
2559 BL FDB DOCON ascii blank
2563 * This really shouldn't be a CONSTANT.
2565 * The base of the disk buffer space.
2567 FCC 'FIRS' ; 'FIRST'
2572 * FDB MEMEND-528 (132 * NBLK)
2575 * This really shouldn't be a CONSTANT.
2577 * The limit of the disk buffer space.
2579 FCC 'LIMI' ; 'LIMIT' : ( the end of memory +1 )
2584 * In 6800 model, was
2588 * ( --- sectorsize )
2589 * The size, in bytes, of a buffer control region.
2591 FCC 'B/CT' ; 'B/CTL' : (bytes/control region)
2597 * ( --- sectorsize )
2598 * The size, in bytes, of a buffer.
2600 FCC 'B/BU' ; 'B/BUF' : (bytes/buffer)
2605 * Hardcoded in 6800 model:
2609 * ( --- blocksperscreen )
2610 * The size, in blocks, of a screen.
2611 * Should this be the same as NBLK, the number of block buffers maintained?
2613 FCC 'B/SC' ; 'B/SCR' : (blocks/screen)
2618 * Hardcoded in 6800 model as:
2620 * blocks/screen = 1024 / "B/BUF" = 8, if sectors are 128 bytes.
2624 * Calculate the address of entry (#n/2) in the boot-up parameter table.
2625 * (Adds the base of the boot-up table to n.)
2627 FCC '+ORIGI' ; '+ORIGIN'
2630 PORIG FDB DOCOL,LIT,ORIG,PLUS
2633 * ######>> screen 36 <<
2636 * This is the per-task variable recording the initial parameter stack pointer.
2646 * This is the per-task variable recording the initial return stack pointer.
2656 * Terminal Input Buffer address.
2657 * Note that this is a variable, so users may allocate their own buffers, but it must be @ed.
2666 * ( --- maxnamewidth )
2667 * This is the maximum width to which symbol names will be recorded.
2669 FCC 'WIDT' ; 'WIDTH'
2677 * Availability of error messages on disk.
2678 * Contains 1 if messages available,
2680 * -1 if a disk error has occurred.
2682 FCC 'WARNIN' ; 'WARNING'
2690 * Boundary for FORGET.
2692 FCC 'FENC' ; 'FENCE'
2700 * Dictionary pointer, fetched by HERE.
2702 FCC 'D' ; 'DP' : points to first free byte at end of dictionary
2709 * ( --- vadr ) ******* Need to check what this is!
2710 * Used in maintaining vocabularies.
2711 * I think it points to the "parent" vocabulary, but I'm not sure.
2712 * Or maybe this is the CONTEXT vocabulary. I'll have to come back here. *****
2714 FCC 'VOC-LIN' ; 'VOC-LINK'
2722 * Disk block being interpreted.
2723 * Zero refers to terminal.
2724 * ******** Should be made a 32 bit user variable! ********
2725 * But the base system needs to have full 32 bit support, div and mul, etc.
2726 * before we can do that.
2736 * Input buffer offset/cursor.
2738 FCC 'I' ; 'IN' : scan pointer for input line buffer
2746 * Output buffer offset/cursor.
2756 * Screen currently being edited, once we have an editor running.
2763 * ######>> screen 37 <<
2767 * Sector offset for LOADing screens,
2768 * set by DRIVE to make a new drive the default.
2769 * This should also be 32 bit or bigger.
2771 FCC 'OFFSE' ; 'OFFSET'
2779 * Current context of interpretation (vocabulary root).
2781 FCC 'CONTEX' ; 'CONTEXT' : points to pointer to vocab to search first
2789 * Current context of definition (vocabulary root).
2791 FCC 'CURREN' ; 'CURRENT' : points to ptr. to vocab being extended
2799 * Compiler/interpreter state.
2801 FCC 'STAT' ; 'STATE' : 1 if compiling, 0 if not
2809 * Numeric conversion base.
2811 FCC 'BAS' ; 'BASE' : number base for all input & output
2819 * Decimal point location for output.
2829 * Field width for I/O formatting.
2839 * Compiler stack mark for stack check.
2849 * Editing cursor location.
2859 * Pointer to last HELD character in PAD.
2867 * ======>> 82.5 <<== SPECIAL
2869 * Line width of active terminal.
2871 FCC 'COLUMN' ; 'COLUMNS' : line width of terminal
2877 * ######>> screen 38 <<
2879 ** An INCREMENTER probably should not be defined without a defined CONSTANT?
2881 ** Make an INCREMENTER compiling word (not in model):
2883 ** { n INCREMENTER name } typical input
2884 ** CREATE a header and compile the increment constant,
2885 ** then overwrite the header with a call to DOINC.
2887 * FCC 'INCREMENTE' ; 'INCREMENTER'
2890 * INCR FDB DOCOL,CON,PSCODE
2892 ** Characteristic of an INCREMENTER.
2893 ** This is too naive:
2895 * ADDD NATWID,X ; Add the increment.
2898 * Compiling word should check that it is compiling a CONSTANT.
2906 * Using the model keeps things semantically connected for other processors:
2907 ONEP FDB DOCOL,ONE,PLUS
2909 ** Greedy alternative:
2915 * Naive alternative:
2918 * Naive alternative:
2921 * ADDD #1 ; It's hard to imagine 1+ being other than 1.
2931 * Using the model keeps things semantically connected for other processors:
2932 TWOP FDB DOCOL,TWO,PLUS
2934 ** Greedy alternative:
2937 * ADDD TWOV,PCR ; See NAT+ (NATP)
2940 * Naive alternative:
2943 * Naive alternative:
2946 * ADDD #2 ; See NAT+ (NATP)
2952 * Get the DICTPT allocation, like a USER constant.
2953 * Should check the stack and heap for collision.
2958 HERE FDB DOCOL,DICTPT,AT
2963 * Increase/decrease heap (add n to DP),
2964 * Should ERROR check stack/heap.
2966 FCC 'ALLO' ; 'ALLOT'
2969 ALLOT FDB DOCOL,DICTPT,PSTORE
2974 * Store word n at DP++,
2975 * Should ERROR check stack/heap.
2979 COMMA FDB DOCOL,HERE,STORE,NATWC,ALLOT
2981 * COMMA FDB DOCOL,HERE,STORE,TWO,ALLOT
2986 * Store byte b at DP+,
2987 * Should ERROR check stack/heap.
2992 CCOMM FDB DOCOL,HERE,CSTORE,ONE,ALLOT
2996 * ( n1 n2 --- n1-n2 )
2997 * Subtract top two words.
3006 * SUB FDB DOCOL,MINUS,PLUS
3007 * FDB SEMIS ; Costs 6 bytes and lots of cycles.
3010 * ( n1 n2 --- n1==n2 )
3011 * Return flag true if n1 and n2 are equal, otherwise false.
3015 EQUAL FDB DOCOL,SUB,ZEQU
3019 * ( n1 n2 --- n1<n2 )
3020 * Return flag true if n1 is less than n2, otherwise false.
3041 * CMPB 1,X ; Why not sub, sbc, bge?
3051 * ( n1 n2 --- n1>n2 )
3052 * Return flag true if n1 is greater than n2, false otherwise.
3056 GREAT FDB DOCOL,SWAP,LESS
3060 * ( n1 n2 n3 --- n2 n3 n1 )
3061 * Rotate the top three words on stack,
3062 * bringing the third word to the top.
3073 * ROT FDB DOCOL,TOR,SWAP,FROMR,SWAP
3080 FCC 'SPAC' ; 'SPACE'
3083 SPACE FDB DOCOL,BL,EMIT
3087 * ( n0 n1 --- min(n0,n1) )
3088 * Leave the minimum of the top two integers.
3089 * Being too greedy here, but, whatever.
3100 * MIN FDB DOCOL,OVER,OVER,GREAT,ZBRAN
3107 * ( n0 n1 --- max(n0,n1) )
3108 * Leave the maximum of the top two integers.
3109 * Really should leave this as in the model.
3120 * MAX FDB DOCOL,OVER,OVER,LESS,ZBRAN
3139 * DDUP FDB DOCOL,DUP,ZBRAN
3140 * FDB DDUP2-*-NATWID
3144 * ######>> screen 39 <<
3149 * Change top integer to its sign.
3151 FCC 'SIGNU' ; 'SIGNUM'
3159 SIGNUP SEX ; Couldn't they have called SignEXtend EXT instead?
3160 STD ,U ; Am I too much of a prude?
3162 * 6800 model version should be something like this:
3173 * ( adr1 direction --- adr2 )
3174 * TRAVERSE the symbol name.
3175 * If direction is 1, find the end.
3176 * If direction is -1, find the beginning.
3178 FCC 'TRAVERS' ; 'TRAVERSE'
3182 BSR SIGNUE ; Convert negative to -, zero or positive to 1.
3183 LDD ,U++ ; Still in D, but we have to pop it anyway.
3184 LDX ,U ; If D is 1 or -1, so is B.
3186 TRAVLP LEAX B,X ; Don't look at the one we start at.
3187 CMPA ,X ; Not sure why we aren't just doing LDA ,X ; BPL.
3191 * Doing this in 6809 just because it can be done may be getting too greedy.
3192 * TRAV FDB DOCOL,SWAP
3193 * TRAV2 FDB OVER,PLUS,LIT8
3195 * FDB OVER,CAT,LESS,ZBRAN
3196 * FDB TRAV2-*-NATWID
3202 * Fetch CURRENT as a per-USER constant.
3204 FCC 'LATES' ; 'LATEST'
3207 LATEST FDB DOCOL,CURENT,AT,AT
3209 * LATEST FDB *+NATWID
3210 * Getting too greedy:
3215 * LDD CURENT+NATWID,PCR
3217 * PSHU X ; Leave the address in X.
3226 * Too greedy, too many smantic holes to fall through.
3227 * If the address at the CFA is made relative,
3228 * this is part of the code that would be affected
3229 * if it is in native CPU code.
3232 * Wanted to do these as INCREMENTERs,
3233 * but I need to stick with the model as much as possible,
3234 * (mostly, LOL) adding code only to make the model more clear.
3236 * Convert PFA to LFA, unchecked. (Bump back from contents to allocation link.)
3249 * Convert PFA to CFA, unchecked. (Bump back from contents to characterist code link.)
3254 * CFA FDB DOCOL,TWO,SUB
3255 CFA FDB DOCOL,NATWC,SUB
3260 * Convert PFA to NFA. (Bump back from contents to beginning of symbol name.)
3268 FDB SUB,ONE,MINUS,TRAV
3273 * Convert NFA to PFA. (Bump up from beginning of symbol name to contents.)
3278 PFA FDB DOCOL,ONE,TRAV,LIT8
3284 * ######>> screen 40 <<
3287 * Save the parameter stack pointer in CSP for compiler checks.
3292 SCSP FDB DOCOL,SPAT,CSP,STORE
3296 * ( 0 n --- ) ( *** )
3297 * ( true n --- IN BLK ) ( anything *** nothing )
3298 * If flag is false, do nothing.
3299 * If flag is true, issue error MESSAGE and QUIT or ABORT, via ERROR.
3300 * Leaves cursor position (IN)
3301 * and currently loading block number (BLK) on stack, for analysis.
3303 * This one is too important to be high-level Forth codes.
3304 * When we have an error, we want to disturb as little as possible.
3305 * But fixing that cascades through ERROR and MESSAGE
3306 * into the disk block system.
3307 * And we aren't ready for that yet.
3309 FCC '?ERRO' ; '?ERROR'
3317 ** this doesn't work anyway: QERROR LBR ERROR
3318 QERR FDB DOCOL,SWAP,ZBRAN
3326 * STATE is compiling:
3328 * STATE is compiling:
3329 * ( --- IN BLK ) ( anything *** nothing )
3330 * ERROR if not compiling.
3332 FCC '?COM' ; '?COMP'
3335 QCOMP FDB DOCOL,STATE,AT,ZEQU,LIT8
3341 * STATE is executing:
3343 * STATE is executing:
3344 * ( --- IN BLK ) ( anything *** nothing )
3345 * ERROR if not executing.
3347 FCC '?EXE' ; '?EXEC'
3350 QEXEC FDB DOCOL,STATE,AT,LIT8
3356 * ( n1 n1 --- ) ( *** )
3357 * ( n1 n2 --- IN BLK ) ( anything *** nothing )
3358 * ERROR if top two are unequal.
3359 * MESSAGE says compiled conditionals do not match.
3361 FCC '?PAIR' ; '?PAIRS'
3364 QPAIRS FDB DOCOL,SUB,LIT8
3370 * CSP and parameter stack are balanced (equal):
3372 * CSP and parameter stack are not balanced (unequal):
3373 * ( --- IN BLK ) ( anything *** nothing )
3374 * ERROR if return/control stack is not at same level as last !CSP.
3375 * Usually indicates that a definition has been left incomplete.
3380 QCSP FDB DOCOL,SPAT,CSP,AT,SUB,LIT8
3388 * No active BLK input:
3389 * ( --- IN BLK ) ( anything *** nothing )
3390 * ERROR if not loading, i. e., if BLK is zero.
3392 FCC '?LOADIN' ; '?LOADING'
3395 QLOAD FDB DOCOL,BLK,AT,ZEQU,LIT8
3400 * ######>> screen 41 <<
3403 * Compile an in-line literal value from the instruction stream.
3405 FCC 'COMPIL' ; 'COMPILE'
3408 * COMPIL FDB DOCOL,QCOMP,FROMR,TWOP,DUP,TOR,AT,COMMA
3409 * COMPIL FDB DOCOL,QCOMP,FROMR,NATP,DUP,TOR,AT,COMMA
3410 COMPIL FDB DOCOL,QCOMP,FROMR,DUP,NATP,TOR,AT,COMMA
3415 * Clear the compile state bit(s) (shift to interpret).
3419 LBRAK FDB DOCOL,ZERO,STATE,STORE
3426 * Set the compile state bit(s) (shift to compile).
3430 RBRAK FDB DOCOL,LIT8
3437 * Toggle SMUDGE bit of LATEST definition header,
3438 * to hide it until defined or reveal it after definition.
3440 FCC 'SMUDG' ; 'SMUDGE'
3443 SMUDGE FDB DOCOL,LATEST,LIT8
3450 * Set the conversion base to sixteen (b00010000).
3457 FCB 16 ; decimal sixteen
3463 * Set the conversion base to ten (b00001010).
3465 FCC 'DECIMA' ; 'DECIMAL'
3470 FCB 10 ; decimal ten
3474 * ######>> screen 42 <<
3476 * ( --- ) ( IP *** )
3477 * Pop the saved IP and use it to
3478 * compile the latest symbol as a reference to a ;CODE definition;
3479 * overwrite the code field of the symbol found by LATEST
3480 * with the address of the low-level characteristic code
3481 * provided in the defining definition.
3482 * Look closely at where things return, consider the operation of R> and >R .
3484 * The machine-level code which follows (;CODE) in the instruction stream
3485 * is not executed by the defining symbol,
3486 * but becomes the characteristic of the defined symbol.
3487 * This is the usual way to generate the characteristics of VARIABLEs,
3488 * CONSTANTs, COLON definitions, etc., when FORTH compiles itself.
3490 * Finally, note that, if code shifts from low level back to high
3491 * (native CPU machine code calling into a list of FORTH codes),
3492 * the low level code can't just call a high-level definition.
3493 * Leaf definitions can directly call other leaf definitions,
3494 * but not non-leafs.
3495 * It will need an anonymous list, probably embedded in the low-level code,
3496 * and Y and X will have to be set appropriately before entering the list.
3498 FCC '(;CODE' ; '(;CODE)'
3501 * PSCODE FDB DOCOL,FROMR,TWOP,LATEST,PFA,CFA,STORE
3502 PSCODE FDB DOCOL,FROMR ; Y/IP is post-inc, needs no adjustment.
3503 FDB LATEST,PFA,CFA,STORE
3508 * ?CSP to see if there are loose ends in the defining definition
3509 * before shifting to the assembler,
3510 * compile (;CODE) in the defining definition's instruction stream,
3511 * shift to interpreting,
3512 * make the ASSEMBLER vocabulary current,
3513 * and !CSP to mark the stack
3514 * in preparation for assembling low-level code.
3515 * Note that ;CODE, unlike DOES>, is IMMEDIATE,
3516 * and compiles (;CODE),
3517 * which will do the actual work of changing
3518 * the LATEST definition's characteristic when the defining word runs.
3519 * Assembly is done by the interpreter, rather than the compiler.
3520 * I could have avoided the anomalous three-byte code fields by
3522 * Note that the ASSEMBLER is not part of the model (at this time).
3523 * That means that, until the assembler is ready,
3524 * if you want to define low-level words,
3525 * you have to poke (comma) in hand-assembled stuff.
3528 FCC ';COD' ; ';CODE'
3531 SEMIC FDB DOCOL,QCSP,COMPIL,PSCODE,SMUDGE,LBRAK,QSTACK
3533 * note: "QSTACK" will be replaced by "ASSEMBLER" later
3535 * ######>> screen 43 <<
3538 * Make the word currently being defined
3539 * build a header for DOES> definitions.
3540 * Actually just compiles a CONSTANT zero
3541 * which can be overwritten later by DOES>.
3542 * Since the fig models were established, this technique has been deprecated.
3544 * Note that <BUILDS is not IMMEDIATE,
3545 * and therefore executes during a definition's run-time,
3546 * rather than its compile-time.
3547 * It is not intended to be used directly,
3548 * but rather so that one definition word can build another.
3549 * Also, note that nothing particularly special happens
3550 * in the defining definition until DOES> executes.
3551 * The name <BUILDS is intended to be a reminder of what is about to occur.
3553 * <BUILDS probably should have compiled an ERROR instead of a ZERO CONSTANT.
3555 FCC '<BUILD' ; '<BUILDS'
3558 BUILDS FDB DOCOL,ZERO,CON
3562 * ( --- ) ( IP *** ) C
3563 * Define run-time behavior of definitions compiled/defined
3564 * by a high-level defining definition --
3565 * the FORTH equivalent of a compiler-compiler.
3566 * DOES> assumes that the LATEST symbol table entry
3567 * has at least one word of parameter field,
3568 * which <BUILDS provides.
3569 * Note that DOES> is also not IMMEDIATE.
3571 * When the defining word containing DOES> executes the DOES> icode,
3572 * it overwrites the LATEST symbol's CFA with jsr <XDOES,
3573 * overwrites the first word of that symbol's parameter field with its own IP,
3574 * and pops the previous IP from the return stack.
3575 * The icodes which follow DOES> in the stream
3576 * do not execute at the defining word's run-time.
3578 * Examining XDOES in the virtual machine shows
3579 * that the defined word will execute those icodes
3580 * which follow DOES> at its own run-time.
3582 * The advantage of this kind of behaviour,
3583 * which you will also note in ;CODE,
3584 * is that the defined word can contain
3585 * both operations and data to be operated on.
3586 * This is how FORTH data objects define their own behavior.
3588 * Finally, note that the effective parameter field for DOES> definitions
3589 * starts two NATWID words after the CFA, instead of just one
3590 * (four bytes instead of two in a sixteen-bit addressing Forth).
3592 * VOCABULARYs will use this. See definition of word FORTH.
3594 FCC 'DOES' ; 'DOES>'
3597 * DOES FDB DOCOL,FROMR,TWOP,LATEST,PFA,STORE
3598 DOES FDB DOCOL,FROMR ; Y/IP is post-inc, needs no adjustment.
3599 FDB LATEST,PFA,STORE
3602 * ( --- PFA+NATWID ) ( *** IP )
3603 * Characteristic of a DOES> defined word.
3604 * The characteristics of DOES> definitions are written in high-level
3605 * Forth codes rather than native CPU machine level code.
3606 * The first parameter word points to the high-level characteristic.
3607 * This routine's job is to push the IP,
3608 * load the high level characteristic pointer in IP,
3609 * and leave the address following the characteristic pointer on the stack
3610 * so the parameter field can be accessed.
3611 DODOES LDD ,S ; Keep the return address.
3612 STY ,S ; Save/nest the current IP on the return stack.
3613 LDY NATWID,X ; First parameter is new IP.
3614 LEAX 2*NATWID,X ; Address of second parameter.
3616 TFR D,PC ; Synthetic return.
3618 * From the 6800 model:
3621 * LDX RP make room on return stack
3625 * STA 2,X push return address
3627 * LDX W get addr of pointer to run-time code
3630 * STX N stash it in scratch area
3631 * LDX 0,X get new IP
3633 * CLRA ; get address of parameter
3637 * PSHS B ; and push it on data stack
3641 * ######>> screen 44 <<
3643 * ( strptr --- strptr+1 count )
3644 * Convert counted string to string and count.
3645 * (Fetch the byte at strptr, post-increment.)
3647 FCC 'COUN' ; 'COUNT'
3650 COUNT FDB DOCOL,DUP,ONEP,SWAP,CAT
3654 * ( strptr count --- )
3655 * EMIT count characters at strptr.
3660 TYPE FDB DOCOL,DDUP,ZBRAN
3662 FDB OVER,PLUS,SWAP,XDO
3663 TYPE2 FDB I,CAT,EMIT,XLOOP
3671 * ( strptr count1 --- strptr count2 )
3672 * Supress trailing blanks (subtract count of trailing blanks from strptr).
3674 FCC '-TRAILIN' ; '-TRAILING'
3677 DTRAIL FDB DOCOL,DUP,ZERO,XDO
3678 DTRAL2 FDB OVER,OVER,PLUS,ONE,SUB,CAT,BL
3690 * TYPE counted string out of instruction stream (updating IP).
3695 * PDOTQ FDB DOCOL,R,TWOP,COUNT,DUP,ONEP
3696 * PDOTQ FDB DOCOL,R,NATP,COUNT,DUP,ONEP
3697 PDOTQ FDB DOCOL,R,COUNT,DUP,ONEP
3698 FDB FROMR,PLUS,TOR,TYPE
3703 * { ." something-to-be-printed " } typical input
3704 * Use WORD to parse to trailing quote;
3705 * if compiling, compile XDOTQ and string parsed,
3706 * otherwise, TYPE string.
3716 FDB COMPIL,PDOTQ,WORD
3717 FDB HERE,CAT,ONEP,ALLOT,BRAN
3719 DOTQ1 FDB WORD,HERE,COUNT,TYPE
3722 * ######>> screen 45 <<
3723 * ======>> 126 <<== MACHINE DEPENDENT
3725 * ( --- IN BLK ) ( anything *** nothing )
3726 * ERROR if parameter stack out of bounds.
3728 * But checking whether the stack is in bounds or not
3729 * really should not use the stack.
3730 * And there really should be a ?RSTACK, as well.
3732 FCC '?STAC' ; '?STACK'
3735 QSTACK FDB DOCOL,LIT8
3738 * But why use that instead of XSPZER (S0)?
3739 * Multi-user or multi-tasking would not want that.
3741 * FDB PORIG,AT,TWO,SUB,SPAT,LESS,ONE
3742 FDB PORIG,AT,SPAT,LESS,ONE ; Not post-decrement push.
3744 * prints 'empty stack'
3747 * Here, we compare with a value at least 128
3748 * higher than dict. ptr. (DICTPT)
3750 FCB $80 ; This is a rough check anyway, leave it as is.
3753 FDB TWO ; NOT the NATWID constant!
3755 * prints 'full stack'
3759 * ======>> 127 << this word's function
3760 * is done by ?STACK in this version
3765 *QFREE FDB DOCOL,SPAT,HERE,LIT8
3767 * FDB PLUS,LESS,TWO,QERR,SEMIS ; This TWO is not NATWID!
3769 * ######>> screen 46 <<
3772 * ***** Check that this is how it works here:
3773 * Get up to n-1 characters from the keyboard,
3774 * storing at buffer and echoing, with backspace editing,
3775 * quitting when a CR is read.
3776 * Terminate it with a NUL.
3778 FCC 'EXPEC' ; 'EXPECT'
3781 EXPECT FDB DOCOL,OVER,PLUS,OVER,XDO ; brace the buffer area
3782 * EXPEC2 FDB KEY,DUP,LIT8
3784 * FDB LIT,$1C,SHOTOS ; DBG
3787 FDB PORIG,AT,EQUAL,ZBRAN ; check for backspacing
3790 FCB 8 ( backspace character to emit )
3791 FDB OVER,I,EQUAL,DUP,FROMR,TWO,SUB,PLUS ; back I up TWO characters
3795 FCB $D ( carriage return )
3798 FDB LEAVE,DROP,BL,ZERO,BRAN ; I think this is the NUL terminator.
3801 EXPEC5 FDB I,CSTORE,ZERO,I,ONEP,STORE
3802 EXPEC6 FDB EMIT,XLOOP
3809 * EXPECT 128 (TWID) characters to TIB.
3811 FCC 'QUER' ; 'QUERY'
3814 QUERY FDB DOCOL,TIB,AT,COLUMS
3815 FDB AT,EXPECT,ZERO,IN,STORE
3820 * End interpretation of a line or screen, and/or prepare for a new block.
3821 * Note that the name of this definition is an empty string,
3822 * so it matches on the terminating NUL in the terminal or block buffer.
3823 FCB $C1 immediate < carriage return >
3826 NULL FDB DOCOL,BLK,AT,ZBRAN
3829 FDB ZERO,IN,STORE,BLK,AT,BSCR,MOD
3831 * check for end of screen
3834 FDB QEXEC,FROMR,DROP
3837 NULL2 FDB FROMR,DROP
3840 * ######>> screen 47 <<
3843 * Fill n bytes at adr with b.
3844 * This relies on CMOVE having a certain lack of parameter checking,
3845 * where overlapping regions are not properly inverted in copy.
3846 * And this really should be done in low-level.
3847 * None of the advantages of doing things in high-level apply to fill.
3852 FILL FDB DOCOL,SWAP,TOR,OVER,CSTORE,DUP,ONEP
3853 FDB FROMR,ONE,SUB,CMOVE
3858 * Fill n bytes with 0.
3860 FCC 'ERAS' ; 'ERASE'
3863 ERASE FDB DOCOL,ZERO,FILL
3868 * Fill n bytes with ASCII SPACE.
3870 FCC 'BLANK' ; 'BLANKS'
3873 BLANKS FDB DOCOL,BL,FILL
3878 * Format a character at the left of the HLD output buffer.
3883 HOLD FDB DOCOL,LIT,$FFFF,HLD,PSTORE,HLD,AT,CSTORE
3888 * Give the address of the output PAD buffer.
3889 * PAD points to the end of a 68 byte buffer for numeric conversion.
3894 PAD FDB DOCOL,HERE,LIT8
3899 * ######>> screen 48 <<
3902 * Scan a string terminated by the character c or ASCII NUL out of input;
3903 * store symbol at WORDPAD with leading count byte and trailing ASCII NUL.
3904 * Leading c are passed over, per ENCLOSE.
3905 * Scans from BLK, or from TIB if BLK is zero.
3906 * May overwrite the numeric conversion pad,
3907 * if really long (length > 31) symbols are scanned.
3912 WORD FDB DOCOL,BLK,AT,ZBRAN
3914 FDB BLK,AT,BLOCK,BRAN
3917 WORD3 FDB IN,AT,PLUS,SWAP,ENCLOS,HERE,LIT8
3919 FDB BLANKS,IN,PSTORE,OVER,SUB,TOR,R,HERE
3920 FDB CSTORE,PLUS,HERE,ONEP,FROMR,CMOVE
3923 * ######>> screen 49 <<
3925 * ( d1 string --- d2 adr )
3926 * Convert the text at string into a number, accumulating the result into d1,
3927 * leaving adr pointing to the first character not converted.
3928 * If DPL is non-negative at entry,
3929 * accumulates the number of characters converted into DPL.
3931 FCC '(NUMBER' ; '(NUMBER)'
3935 PNUMB2 FDB ONEP,DUP,TOR,CAT,BASE,AT,DIGIT,ZBRAN
3937 FDB SWAP,BASE,AT,USTAR,DROP,ROT,BASE
3938 FDB AT,USTAR,DPLUS,DPL,AT,ONEP,ZBRAN
3941 PNUMB3 FDB FROMR,BRAN
3948 * Convert text at ctstr to a double integer,
3949 * taking the 0 ERROR if the conversion is not valid.
3950 * If a decimal point is present,
3951 * accumulate the count of digits to the decimal point's right into DPL
3952 * (negative DPL at exit indicates single precision).
3953 * ctstr is a counted string
3954 * -- the first byte at ctstr is the length of the string,
3955 * but NUMBER ignores the count and expects a NUL terminator instead.
3957 FCC 'NUMBE' ; 'NUMBER'
3960 NUMB FDB DOCOL,ZERO,ZERO,ROT,DUP,ONEP,CAT,LIT8
3962 FDB EQUAL,DUP,TOR,PLUS,LIT,$FFFF
3963 NUMB1 FDB DPL,STORE,PNUMB,DUP,CAT,BL,SUB
3968 FDB SUB,ZERO,QERR,ZERO,BRAN
3970 NUMB2 FDB DROP,FROMR,ZBRAN
3976 * ( --- locptr length true ) { -FIND name } typical input
3978 * Parse a word, then FIND,
3979 * first in the definition vocabulary,
3980 * then in the CONTEXT (interpretation) vocabulary, if necessary.
3981 * Returns what (FIND) returns, flag and optional location and length.
3983 FCC '-FIN' ; '-FIND'
3986 DFIND FDB DOCOL,BL,WORD,HERE,CONTXT,AT,AT
3987 FDB PFIND,DUP,ZEQU,ZBRAN
3989 FDB DROP,HERE,LATEST,PFIND
3992 * ######>> screen 50 <<
3994 * ( anything --- nothing ) ( anything *** nothing )
3995 * An indirection for ABORT, for ERROR,
3996 * which may be modified carefully.
3998 FCC '(ABORT' ; '(ABORT)'
4001 PABORT FDB DOCOL,ABORT
4006 FCC 'ERRO' ; 'ERROR'
4009 * This really should not be high level, according to best practices.
4010 * But fixing that cascades through MESSAGE,
4011 * requiring re-architecting the disk block system.
4012 * First, we need to get this transliteration running.
4013 ERROR FDB DOCOL,WARN,AT,ZLESS
4018 * 0 to print error #
4019 * and 1 to print error message from disc
4021 ERROR2 FDB HERE,COUNT,TYPE,PDOTQ
4024 FDB MESS,SPSTOR,IN,AT,BLK,AT,QUIT
4029 * Mask byte at adr with n.
4030 * Not in FIG, don't need it for 8 bit characters after all.
4032 * FCC 'CMAS' ; 'CMASK'
4035 * CMASK FDB *+NATWID
4043 * Mask high bit of tail of name in PAD buffer.
4044 * Not in FIG, need it for 8 bit characters.
4046 FCC 'IDFLA' ; 'IDFLAT'
4051 LDB ,X ; get the count
4053 LDA B,X ; point to the tail
4054 ANDA #$7F ; Clear the EndOfName flag bit.
4059 * Print definition's name from its NFA.
4064 IDDOT FDB DOCOL,PAD,LIT8
4067 FCB $5F ( underline )
4068 FDB FILL,DUP,PFA,LFA,OVER,SUB,PAD
4069 * FDB SWAP,CMOVE,PAD,COUNT,LIT8
4077 * ######>> screen 51 <<
4079 * ( --- ) { CREATE name } input
4080 * Parse a name (length < 32 characters) and create a header,
4081 * reporting first duplicate found in either the defining vocabulary
4082 * or the context (interpreting) vocabulary.
4083 * Install the header in the defining vocabulary
4084 * with CFA dangerously pointing to the parameter field.
4085 * Leave the name SMUDGEd.
4087 FCC 'CREAT' ; 'CREATE'
4090 CREATE FDB DOCOL,DFIND,ZBRAN
4099 CREAT2 FDB HERE,DUP,CAT,WIDTH,AT,MIN
4100 FDB ONEP,ALLOT,DUP,LIT8
4101 FCB ($80|FSMUDG) ; Bracket the name.
4102 FDB TOGGLE,HERE,ONE,SUB,LIT8
4104 FDB TOGGLE,LATEST,COMMA,CURENT,AT,STORE
4105 * FDB HERE,TWOP,COMMA
4109 * ######>> screen 52 <<
4112 * { [COMPILE] name } typical use
4113 * -DFIND next WORD and COMPILE it, literally;
4114 * used to compile immediate definitions into words.
4116 FCC '[COMPILE' ; '[COMPILE]'
4119 BCOMP FDB DOCOL,DFIND,ZEQU,ZERO,QERR,DROP,CFA,COMMA
4123 * ( n --- ) if compiling. P
4124 * ( n --- n ) if interpreting.
4125 * Compile n as a literal, if compiling.
4127 FCC 'LITERA' ; 'LITERAL'
4130 LITER FDB DOCOL,STATE,AT,ZBRAN
4132 FDB COMPIL,LIT,COMMA
4136 * ( d --- ) if compiling. P
4137 * ( d --- d ) if interpreting.
4138 * Compile d as a double literal, if compiling.
4140 FCC 'DLITERA' ; 'DLITERAL'
4143 DLITER FDB DOCOL,STATE,AT,ZBRAN
4145 FDB SWAP,LITER,LITER ; Just two literals in the right order.
4148 * ######>> screen 53 <<
4151 * Interpret or compile, according to STATE.
4152 * Searches words parsed in dictionary first, via -FIND,
4153 * then checks for valid NUMBER.
4154 * Pushes or COMPILEs double literal if NUMBER leaves DPL non-negative.
4155 * ERROR checks the stack via ?STACK before returning to its caller.
4157 FCC 'INTERPRE' ; 'INTERPRET'
4161 INTER2 FDB DFIND,ZBRAN
4171 INTER5 FDB HERE,NUMB,DPL,AT,ONEP,ZBRAN
4175 INTER6 FDB DROP,LITER
4176 INTER7 FDB QSTACK,BRAN
4178 * FDB SEMIS never executed
4181 * ######>> screen 54 <<
4184 * Toggle precedence bit of LATEST definition header.
4185 * During compiling, most symbols scanned are compiled.
4186 * IMMEDIATE definitions execute whenever the outer INTERPRETer scans them,
4187 * but may be compiled via ' (TICK).
4189 FCC 'IMMEDIAT' ; 'IMMEDIATE'
4192 IMMED FDB DOCOL,LATEST,LIT8
4198 * ( --- ) { VOCABULARY name } input
4199 * Create a vocabulary entry with a flag for terminating vocabulary searches.
4200 * Store the current search context in it for linking.
4201 * At run-time, VOCABULARY makes itself the CONTEXT vocabulary.
4203 FCC 'VOCABULAR' ; 'VOCABULARY'
4206 VOCAB FDB DOCOL,BUILDS,LIT,$81A0,COMMA,CURENT,AT,CFA
4207 FDB COMMA,HERE,VOCLIN,AT,COMMA,VOCLIN,STORE,DOES
4208 * DOVOC FDB TWOP,CONTXT,STORE
4209 DOVOC FDB NATP,CONTXT,STORE
4214 * Note: FORTH does not go here in the rom-able dictionary,
4215 * since FORTH is a type of variable.
4217 * (Should make a proper architecture for this at some point.)
4222 * Makes the current interpretation CONTEXT vocabulary
4223 * also the CURRENT defining vocabulary.
4225 FCC 'DEFINITION' ; 'DEFINITIONS'
4228 DEFIN FDB DOCOL,CONTXT,AT,CURENT,STORE
4233 * Parse out a comment and toss it away.
4234 * Leaves the first 32 characters in WORDPAD, which may or may not be useful.
4238 PAREN FDB DOCOL,LIT8
4243 * ######>> screen 55 <<
4245 * ( anything *** nothing )
4246 * Clear return stack.
4247 * Then INTERPRET and, if not compiling, prompt with OK,
4253 QUIT FDB DOCOL,ZERO,BLK,STORE
4256 * Here is the outer interpretter
4257 * which gets a line of input, does it, prints " OK"
4259 QUIT2 FDB RPSTOR,CR,QUERY,INTERP,STATE,AT,ZEQU
4267 * FDB SEMIS ( never executed )
4270 * ( anything --- nothing ) ( anything *** nothing )
4271 * Clear parameter stack,
4272 * set STATE to interpret and BASE to DECIMAL,
4273 * return to input from terminal,
4274 * restore DRIVE OFFSET to 0,
4275 * print out "Forth-68",
4276 * set interpret and define vocabularies to FORTH,
4277 * and finally, QUIT.
4278 * Used to force the system to a known state
4279 * and return control to the initial INTERPRETer.
4281 FCC 'ABOR' ; 'ABORT'
4284 ABORT FDB DOCOL,SPSTOR,DEC,QSTACK,DRZERO,CR,PDOTQ
4286 FCC "fig-Forth-6809(RTS)"
4289 * FDB SEMIS never executed
4292 * ######>> screen 56 <<
4293 * bootstrap code... moves rom contents to ram :
4300 * Ultimately, we want position indepence,
4301 * so I'm using PCR where it seems reasonable.
4302 CENT LDS SINIT,PCR ; Get a useable return stack, at least.
4303 LDA #IUPDP ; This is not relative to PC.
4304 TFR A,DP ; And a useable direct page, too.
4305 SETDP IUPDP ; (For good measure.)
4307 * We'll keep this here for the time being.
4308 * There are better ways to do this, of course.
4309 * Re-architect, re-architect.
4310 LEAX ERAM,PCR ; end of stuff to move
4311 STX <XFENCE ; Borrow this variable for a loop terminator.
4312 LDY #RBEG ; bottom of open-ended destination
4313 LEAX RAM,PCR ; bottom of stuff to move
4315 STA ,Y+ ; move TASK & FORTH to ram
4318 * Leaves USE and PREV uninitialized.
4323 * STX <XFENCE ; Borrow this variable for a loop terminator.
4324 * LEAY REND,PCR ; top of destination (included XUSE and XPREV)
4325 * LEAX ERAM,PCR ; top of stuff to move (included initializers for XUSE and XPREV)
4327 * STA ,-Y ; move TASK & FORTH to ram
4331 * CENT LDS #REND-1 top of destination
4332 * LDX #ERAM top of stuff to move
4335 * PSHS A ; move TASK & FORTH to ram
4339 * LDS #XFENCE-1 put stack at a safe place for now
4340 * But that is taken care of.
4362 WENT LDS SINIT,PCR ; Get a useable return stack, at least.
4363 LDA #IUPDP ; This is not relative to PC.
4364 TFR A,DP ; And a useable direct page, too.
4365 SETDP IUPDP ; (For good measure.)
4368 PSHS X ; for loop termination
4369 CLRB ; Yes, I'm being a little ridiculous. Only a little.
4371 LEAY XFENCE-UORIG,Y ; top of destination
4372 LEAX FENCIN,PCR ; top of stuff to move
4373 WARM2 LDD ,--X ; All entries are 16 bit.
4377 LEAS 2,S ; But we'll reset the return stack shortly, anyway.
4378 LDU <XSPZER ; So we can clear the hole above the TOS
4379 * WENT LDS #XFENCE-1 top of destination
4380 * LDX #FENCIN top of stuff to move
4388 * S is already there.
4390 * STX UP init user ram pointer
4391 * UP is already there (DP).
4394 LEAY ABORT+NATWID,PCR ; IP never points to DOCOL!
4396 NOP Here is a place to jump to special user
4397 NOP initializations such as I/0 interrups
4400 * For systems with TRACE:
4402 STX ,U The hole above the parameter stack
4403 * STX TRLIM clear trace mode
4404 STX <TRLIM clear trace mode (both bytes)
4406 * STX BRKPT clear breakpoint address
4407 STX <BRKPT clear breakpoint address
4408 * JMP RPSTOR+2 start the virtual machine running !
4409 LBSR RPSTOR+NATWID start the virtual machine running !
4410 LEAX WENT,PCR ; But we must also give RP! someplace to return.
4411 STX ,S ; This rail might get walked on by (DO).
4413 * RP! sets up the return stack pointer, then Y references abort.
4415 * Here is the stuff that gets copied to ram :
4416 * (not * at address $140:)
4417 * at an appropriate address:
4419 * RAM FDB $3000,$3000,0,0
4420 * RAM FDB BUFBAS,BUFBAS,0,0 ; ... except the direct page has moved.
4421 * These initialization values for USE and PREV were here to help pack the code.
4422 * They don't belong here unless we move the USER table
4423 * back below the writable dictionary,
4424 * *and* move these USER variables to the end of the direct page --
4425 * *or* let these definitions exist in the USER table.
4430 * Makes FORTH the current interpretation vocabulary.
4431 * In order to make this ROMmable, this entry is set up as the tail-end,
4432 * and copied to RAM in the start-up code.
4433 * We want a more elegant solution to this, too. Greedy, maybe.
4435 FCC 'FORT' ; 'FORTH'
4437 FDB NOOP-7 ; Note that this does not link to COLD!
4438 RFORTH FDB DODOES,DOVOC,$81A0,TASK-7
4440 FCC "Copyright 1979 Forth Interest Group, David Lion,"
4442 FCC "Parts Copyright 2019 Joel Matthew Rees"
4448 RTASK FDB DOCOL,SEMIS
4450 ERAMSZ EQU *-RAM ; So we can get a look at it.
4453 * ######>> screen 57 <<
4456 * Sign extend n0 to a double integer.
4460 FDB COLD-7 ; Note that this does not link to FORTH (RFORTH)!
4461 STOD FDB DOCOL,DUP,ZLESS,MINUS
4467 * ( multiplier multiplicand --- product )
4468 * Signed word multiply.
4473 LBSR USTAR+NATWID ; or [USTAR,PCR]?
4474 LEAU NATWID,U ; Drop high word.
4482 * ( dividend divisor --- remainder quotient )
4483 * M/ in word-only form, i. e., signed division of 2nd word by top word,
4484 * yielding signed word quotient and remainder.
4485 * Except *BUG* it isn't signed.
4490 SLMOD FDB DOCOL,TOR,STOD,FROMR,USLASH
4494 * ( dividend divisor --- quotient )
4495 * Signed word divide without remainder.
4496 * Except *BUG* it isn't signed.
4500 SLASH FDB DOCOL,SLMOD,SWAP,DROP
4504 * ( dividend divisor --- remainder )
4505 * Remainder function, result takes sign of dividend.
4510 MOD FDB DOCOL,SLMOD,DROP
4514 * ( multiplier multiplicand divisor --- remainder quotient )
4515 * Signed precise division of product:
4516 * multiply 2nd and 3rd words on stack
4517 * and divide the 31-bit product by the top word,
4518 * leaving both quotient and remainder.
4519 * Remainder takes sign of product.
4520 * Guaranteed not to lose significant bits in 16 bit integer math.
4522 FCC '*/MO' ; '*/MOD'
4525 SSMOD FDB DOCOL,TOR,USTAR,FROMR,USLASH
4529 * ( multiplier multiplicand divisor --- quotient )
4530 * */MOD without remainder.
4535 SSLASH FDB DOCOL,SSMOD,SWAP,DROP
4539 * ( ud1 u1 --- u2 ud2 )
4540 * U/ with an (unsigned) double quotient.
4541 * Guaranteed not to lose significant bits in 32 bit / 16 bit bit integer math,
4542 * if you are prepared to deal with the extra 16 bits of result.
4544 FCC 'M/MO' ; 'M/MOD'
4547 MSMOD FDB DOCOL,TOR,ZERO,R,USLASH
4548 FDB FROMR,SWAP,TOR,USLASH,FROMR
4554 * Convert the top of stack to its absolute value.
4559 ABS FDB DOCOL,DUP,ZLESS,ZBRAN
4567 * Convert the top double to its absolute value.
4572 DABS FDB DOCOL,DUP,ZLESS,ZBRAN
4577 * ######>> screen 58 <<
4581 * Least Recently Used buffer.
4582 * Really should be with FIRST and LIMIT in the per-task table.
4591 * Most Recently Used buffer.
4592 * Really should be with FIRST and LIMIT in the per-task table.
4600 * ( buffer1 --- buffer2 f )
4601 * Bump to next buffer,
4602 * flag false if result is PREVious buffer,
4603 * otherwise flag true.
4604 * Used in the LRU allocation routines.
4609 * PBUF FDB DOCOL,LIT8
4610 * FCB $84 ; This was a hard-wiring bug.
4611 PBUF FDB DOCOL,BBUF,BCTL,PLUS ; Size of the buffer record.
4612 * FDB PLUS,DUP,LIMIT,EQUAL,ZBRAN
4613 FDB PLUS,DUP,LIMIT,LESS,ZEQU,OVER,FIRST,LESS,OR,ZBRAN
4614 FDB PBUF2-*-NATWID ; Use defensive programming.
4616 PBUF2 FDB DUP,PREV,AT,SUB
4621 * Flag to mark a buffer dirty, in need of being written out.
4622 * This flag limits the max number of sectors in a disk to ((256^NATWID)/2)-1.
4623 * It also hard-codes an implicit test which is used elsewhere.
4625 FCC 'UPDATE-BI' ; 'UPDATE-BIT'
4632 * Mark PREVious buffer dirty, in need of being written out.
4634 FCC 'UPDAT' ; 'UPDATE'
4637 * UPDATE FDB DOCOL,PREV,AT,AT,LIT,$8000,OR,PREV,AT,STORE
4638 UPDATE FDB DOCOL,PREV,AT,AT,UPDBIT,OR,PREV,AT,STORE
4643 * Mark the buffer addressed as empty.
4644 * Have to add code to avoid block 0 appearing to be in a buffer from COLD.
4645 * Usually, there is no sector 0 (?), but the RAM buffers are too simple.
4646 * Note that without this block number being made illegal,
4647 * about 8 binaryMegabytes (256 bytes/block) of disk can be addressed total.
4648 * With this block number made illegal, the max is 1 block less,
4649 * still about 8 biMeg.
4651 FCC 'KILL-BUFFE' ; 'KILL-BUFFER'
4654 KILBUF FDB *+NATWID ; DOCOL,UPDBIT,ONE,SUB,SWAP,STORE
4656 LDD UPDBIT+NATWID,PCR
4663 * Mark all buffers empty.
4665 FCC 'KILL-BUFFER' ; 'KILL-BUFFERS'
4671 LDD FIRST+NATWID,PCR
4679 ADDD BBUF+NATWID,PCR
4680 ADDD BCTL+NATWID,PCR
4691 * Erase and mark all buffers empty.
4692 * Standard method of discarding changes.
4694 FCC 'EMPTY-BUFFER' ; 'EMPTY-BUFFERS'
4697 MTBUF FDB DOCOL,FIRST,LIMIT,OVER,SUB,ERASE
4698 * FDB FIRST,DUP,KILBUF,PBUF,DROP,DUP,KILBUF
4699 * FDB PBUF,DROP,DUP,KILBUF,PBUF,DROP,KILBUF
4705 * Clear the current offset to the block numbers in the drive interface.
4706 * The drives need to be re-architected.
4707 * Would be cool to have RAM and ROM drives supported
4708 * in addition to regular physical persistent store.
4713 DRZERO FDB DOCOL,ZERO,OFSET,STORE
4716 * ======>> 174 <<== system dependant word
4718 * Set the current offset in the drive interface to reference the second drive.
4719 * The hard-coded number in there needs to be in a table.
4724 DRONE FDB DOCOL,LIT,$07D0,OFSET,STORE
4725 ; **** hard-codes the size of the disc !!!!
4728 * ######>> screen 59 <<
4731 * Get a free buffer,
4732 * assign it to block n,
4733 * return buffer address.
4734 * Will free a buffer by writing it, if necessary.
4735 * Does not actually read the block.
4736 * A bug in the fig LRU algorithm, which I have not fixed,
4737 * gives the PREVious buffer if USE gets set to PREVious.
4738 * (The bug is that USE sometimes gets set to PREVious.)
4739 * This bug sometimes causes sector moves to become sector fills.
4741 FCC 'BUFFE' ; 'BUFFER'
4744 BUFFER FDB DOCOL,USE,AT,DUP,TOR
4745 BUFFR2 FDB PBUF,ZBRAN
4747 FDB USE,STORE,R,AT,ZLESS
4750 * FDB R,TWOP,R,AT,LIT,$7FFF,AND,ZERO,RW
4751 FDB R,NATP,R,AT,UPDBIT,LNOT,AND,ZERO,RW
4752 * BUFFR3 FDB R,STORE,R,PREV,STORE,FROMR,TWOP
4753 BUFFR3 FDB R,STORE,R,PREV,STORE,FROMR,NATP
4756 * ######>> screen 60 <<
4759 * Get BUFFER containing block n, relative to OFFSET.
4760 * If block n is not in a buffer, bring it in.
4761 * Returns buffer address.
4763 FCC 'BLOC' ; 'BLOCK'
4766 BLOCK FDB DOCOL,OFSET,AT,PLUS,TOR
4767 FDB PREV,AT,DUP,AT,R,SUB,DUP,PLUS,ZBRAN
4769 BLOCK3 FDB PBUF,ZEQU,ZBRAN
4771 * FDB DROP,R,BUFFER,DUP,R,ONE,RW,TWO,SUB
4772 FDB DROP,R,BUFFER,DUP,R,ONE,RW,NATWC,SUB
4773 BLOCK4 FDB DUP,AT,R,SUB,DUP,PLUS,ZEQU,ZBRAN
4776 * BLOCK5 FDB FROMR,DROP,TWOP
4777 BLOCK5 FDB FROMR,DROP,NATP
4780 * ######>> screen 61 <<
4782 * ( line screen --- buffer C/L)
4783 * Bring in the sector containing the specified line of the specified screen.
4784 * Returns the buffer address and the width of the screen.
4785 * Screen number is relative to OFFSET.
4786 * The line number may be beyond screen 4,
4787 * (LINE) will get the appropriate screen.
4789 FCC '(LINE' ; '(LINE)'
4792 PLINE FDB DOCOL,TOR,LIT8
4794 FDB BBUF,SSMOD,FROMR,BSCR,STAR,PLUS,BLOCK,PLUS,LIT8
4799 * ( line screen --- )
4800 * Print the line of the screen as found by (LINE), suppress trailing BLANKS.
4802 FCC '.LIN' ; '.LINE'
4805 DLINE FDB DOCOL,PLINE,DTRAIL,TYPE
4810 * If WARNING is 0, print "MESSAGE #n";
4811 * otherwise, print line n relative to screen 4,
4812 * the line number may be negative.
4813 * Uses .LINE, but counter-adjusts to be relative to the real drive 0.
4815 FCC 'MESSAG' ; 'MESSAGE'
4818 MESS FDB DOCOL,WARN,AT,ZBRAN
4824 FDB OFSET,AT,BSCR,SLASH,SUB,DLINE,BRAN
4828 FCC 'err # ' ; 'err # '
4834 * Begin interpretation of screen (block) n.
4835 * See also ARROW, SEMIS, and NULL.
4837 FCC 'LOA' ; 'LOAD' : input:scr #
4840 LOAD FDB DOCOL,BLK,AT,TOR,IN,AT,TOR,ZERO,IN,STORE
4841 FDB BSCR,STAR,BLK,STORE
4842 FDB INTERP,FROMR,IN,STORE,FROMR,BLK,STORE
4847 * Continue interpreting source code on the next screen.
4852 ARROW FDB DOCOL,QLOAD,ZERO,IN,STORE,BSCR
4853 FDB BLK,AT,OVER,MOD,SUB,BLK,PSTORE
4858 * ######>> screen 63 <<
4859 * The next 4 subroutines are machine dependent, and are
4860 * called by words 13 through 16 in the dictionary.
4862 * ======>> 182 << code for EMIT
4863 * ( --- ) No parameter stack effect.
4864 * Interfaces directly with ROM. Expects output character in D (therefore, B).
4865 * Output using rom CHROUT: redirectable to a printer on Coco.
4866 * Outputs the character on stack (low byte of 1 bit word/cell).
4867 PEMIT PSHS Y,U,DP ; Save everything important! (For good measure, only.)
4868 TFR B,A ; Coco ROM wants it in A.
4870 TFR B,DP ; Give the ROM its direct page.
4871 JSR [$A002] ; Output the character in A.
4873 * PEMIT STB N save B
4876 * BITB #2 check ready bit
4877 * BEQ PEMIT+4 if not ready for more data
4880 * STB IOSTAT-UORIG,X
4881 * LDB N recover B & X
4883 * RTS only A register may change
4884 * PEMIT JMP $E1D1 for MIKBUG
4885 * PEMIT FCB $3F,$11,$39 for PROTO
4886 * PEMIT JMP $D286 for Smoke Signal DOS
4888 * ======>> 183 << code for KEY
4889 * ( --- ) No parameter stack effect.
4890 * Returns character or break flag in D, since this interfaces with Coco ROM.
4891 * Wait for key from POLCAT on Coco.
4892 * Returns the character code for the key pressed.
4893 PKEY PSHS Y,U,DP ; Must save everything important for this one.
4894 LDA #$CF ; a cursor of sorts
4906 PKEYR CLRB ; for the break flag, shares code with PQTER
4909 COMB ; for the break flag
4910 PKEYGT EXG A,B ; Leave it in D for return.
4911 PULS Y,U,DP,PC ; Shares exit with PQTER
4917 * BCC PKEY+4 no incoming data yet
4919 * ANDA #$7F strip parity bit
4921 * STB IOSTAT+1-UORIG,X
4925 * PKEY JMP $E1AC for MIKBUG
4926 * PKEY FCB $3F,$14,$39 for PROTO
4927 * PKEY JMP $D289 for Smoke Signal DOS
4929 * ######>> screen 64 <<
4930 * ======>> 184 << code for ?TERMINAL
4931 * ( --- f ) Should change this to no stack effect.
4932 * check break key using POLCAT
4933 * Returns a flag to tell whether the break key was pressed or not.
4937 JSR [$A000] ; Look but don't wait.
4939 * PQTER LDA ACIAC Test for 'break' condition
4940 * ANDA #$11 mask framing error bit and
4943 * LDA ACIAD clear input buffer
4950 * ======>> 185 << code for CR
4951 * ( --- ) No stack effect.
4952 * Interfaces directly with ROM.
4953 * For Coco just output a CR.
4954 * Also subject to redirection in Coco BASIC ROM.
4956 BRA PEMIT ; Just steal the code.
4957 * PCR LDA #$D carriage return
4963 * LDB XDELAY+1-UORIG,X
4965 * BMI PQTER2 return if minus
4966 * PSHS B ; save counter
4967 * BSR PEMIT print RUBOUTs to delay.....
4974 * ######>> screen 66 <<
4977 * Query the disk, I suppose.
4978 * Not sure what the model had in mind for this stub.
4980 FCC '?DIS' ; '?DISC'
4986 * ######>> screen 67 <<
4989 * Write one block of data to disk.
4990 * Parameters unspecified in model. Stub in model.
4992 FCC 'BLOCK-WRIT' ; 'BLOCK-WRITE'
4998 * ######>> screen 68 <<
5001 * Read one block of data from disk.
5002 * Parameters unspecified in model. Stub in model.
5004 FCC 'BLOCK-REA' ; 'BLOCK-READ'
5010 *The next 3 words are written to create a substitute for disc
5011 * mass memory,located between MASSLO & MASSHI in ram --
5012 * ($3210 and $3fff in the 6800 model).
5019 FDB MEMEND a system dependent equate at front
5027 FDB MEMTOP ( $3FFF or $7FFF in this version )
5029 * ######>> screen 69 <<
5031 * ( buffer sector f --- )
5032 * Read or Write the specified (absolute -- ignores OFFSET) sector
5033 * from or to the specified buffer.
5034 * A zero flag specifies write,
5035 * non-zero specifies read.
5036 * Sector is an unsigned integer,
5037 * buffer is the buffer's address.
5038 * Will need to use the CoCo ROM disk routines.
5039 * For now, provides a virtual disk in RAM.
5044 RW FDB DOCOL,TOR,BBUF,STAR,LO,PLUS,DUP,HI,GREAT,ZBRAN
5048 FCC ' Range ?' ; ' Range ?'
5058 * LDY $C006 control table
5059 * LDX #DROFFS+7 ; This is BIF's table of drive sizes.
5061 * RWD SUBD ,X++ sectors
5063 * BVC RWR table end?
5067 * RWR ADDD ,--X back one
5070 * LDD #18 sectors/track
5080 * PULS D table entry
5093 * JSR [$C004] ROM handles timeout
5094 * PULS Y,U,DP if IRQ enabled
5097 * LDB 6,X coco status
5107 * ######>> screen 72 <<
5109 * ( --- ) compiling P
5110 * ( --- adr ) interpreting
5112 * Parse a symbol name from input and search the dictionary for it, per -FIND;
5113 * compile the address as a literal if compiling,
5114 * otherwise just push it.
5118 TICK FDB DOCOL,DFIND,ZEQU,ZERO,QERR,DROP,LITER
5122 * ( --- ) { FORGET name } input
5123 * Parse out name of definition to FORGET to, -DFIND it,
5124 * then lop it and everything that follows out of the dictionary.
5125 * In fig Forth, CURRENT and CONTEXT have to be the same to FORGET.
5127 FCC 'FORGE' ; 'FORGET'
5130 FORGET FDB DOCOL,CURENT,AT,CONTXT,AT,SUB,LIT8
5132 FDB QERR,TICK,DUP,FENCE,AT,LESS,LIT8
5134 FDB QERR,DUP,ZERO,PORIG,GREAT,LIT8
5136 FDB QERR,DUP,NFA,DICTPT,STORE,LFA,AT,CONTXT,AT,STORE
5139 * ######>> screen 73 <<
5142 * Calculate a back reference from HERE and compile it.
5147 * BACK FDB DOCOL,HERE,SUB,COMMA
5148 BACK FDB DOCOL,HERE,NATP,SUB,COMMA
5153 * typical use: BEGIN code-loop test UNTIL
5154 * typical use: BEGIN code-loop AGAIN
5155 * typical use: BEGIN code-loop test WHILE code-true REPEAT
5156 * ( --- adr n ) compile time P,C
5157 * Push HERE for BACK reference for general (non-counting) loops,
5158 * with BEGIN construct flag.
5159 * A better flag: $4245 (ASCII for 'BE').
5161 FCC 'BEGI' ; 'BEGIN'
5164 BEGIN FDB DOCOL,QCOMP,HERE,ONE ; ONE is a flag for BEGIN loops.
5169 * typical use: test IF code-true ELSE code-false ENDIF
5170 * ENDIF is just a sort of intersection piece,
5171 * marking where execution resumes after both branches.
5172 * ( adr n --- ) compile time
5173 * Check the mark and resolve the IF.
5174 * A better flag: $4846 (ASCII for 'IF').
5176 FCC 'ENDI' ; 'ENDIF'
5179 ENDIF FDB DOCOL,QCOMP,TWO,QPAIRS,HERE ; This TWO is a flag for IF.
5180 FDB OVER,NATP,SUB,SWAP,STORE
5185 * typical use: test IF code-true ELSE code-false ENDIF
5192 THEN FDB DOCOL,ENDIF
5196 * ( limit index --- ) runtime
5197 * typical use: DO code-loop LOOP
5198 * typical use: DO code-loop increment +LOOP
5199 * Counted loop, index is initial value of index.
5200 * Will loop until index equals (positive going)
5201 * or passes (negative going) limit.
5202 * ( --- adr n ) compile time P,C
5203 * Compile (DO), push HERE for BACK reference,
5204 * and push DO control construct flag.
5205 * A better flag: $444F (ASCII for 'DO').
5210 DO FDB DOCOL,COMPIL,XDO,HERE,THREE ; THREE is a flag for DO loops.
5215 * typical use: DO code-loop LOOP
5216 * Increments the index by one and branches back to beginning of loop.
5217 * Will loop until index equals limit.
5218 * ( adr n --- ) compile time P,C
5219 * Check the mark and compile (LOOP), fill in BACK reference.
5220 * A better flag: $444F (ASCII for 'DO').
5225 LOOP FDB DOCOL,THREE,QPAIRS,COMPIL,XLOOP,BACK ; THREE for DO loops.
5230 * typical use: DO code-loop increment +LOOP
5231 * Increments the index by n and branches back to beginning of loop.
5232 * Will loop until index equals (positive going)
5233 * or passes (negative going) limit.
5234 * ( adr n --- ) compile time P,C
5235 * Check the mark and compile (+LOOP), fill in BACK reference.
5236 * A better flag: $444F (ASCII for 'DO').
5238 FCC '+LOO' ; '+LOOP'
5241 PLOOP FDB DOCOL,THREE,QPAIRS,COMPIL,XPLOOP,BACK ; THREE for DO loops.
5246 * typical use: BEGIN code-loop test UNTIL
5247 * Will loop until UNTIL tests true.
5248 * ( adr n --- ) compile time P,C
5249 * Check the mark and compile (0BRANCH), fill in BACK reference.
5250 * A better flag: $4245 (ASCII for 'BE').
5252 FCC 'UNTI' ; 'UNTIL' : ( same as END )
5255 UNTIL FDB DOCOL,ONE,QPAIRS,COMPIL,ZBRAN,BACK ; ONE for BEGIN loops.
5258 * ######>> screen 74 <<
5261 * typical use: BEGIN code-loop test END
5273 * typical use: BEGIN code-loop AGAIN
5275 * (or until something uses R> DROP to force the current definition to die,
5276 * or perhaps ABORT or ERROR or some such other drastic means stops things).
5277 * ( adr n --- ) compile time P,C
5278 * Check the mark and compile (0BRANCH), fill in BACK reference.
5279 * A better flag: $4245 (ASCII for 'BE').
5281 FCC 'AGAI' ; 'AGAIN'
5284 AGAIN FDB DOCOL,ONE,QPAIRS,COMPIL,BRAN,BACK ; ONE for BEGIN loops.
5289 * typical use: BEGIN code-loop test WHILE code-true REPEAT
5290 * Will loop until WHILE tests false, skipping code-true on end.
5291 * REPEAT marks where execution resumes after the WHILE find a false flag.
5292 * ( aadr1 n1 adr2 n2 --- ) compile time P,C
5293 * Check the marks for WHILE and BEGIN,
5294 * compile BRANCH and BACK fill adr1 reference,
5295 * FILL-IN 0BRANCH reference at adr2.
5296 * Better flags: $4245 (ASCII for 'BE') and $5747 (ASCII for 'WH').
5298 FCC 'REPEA' ; 'REPEAT'
5301 REPEAT FDB DOCOL,TOR,TOR,AGAIN,FROMR,FROMR ; ONE for BEGIN loops.
5302 FDB TWO,SUB,ENDIF ; TWO is for IF, 4 is for WHILE.
5307 * typical use: test IF code-true ELSE code-false ENDIF
5308 * Will pass execution to the true part on a true flag
5309 * and to the false part on a false flag.
5310 * ( --- adr n ) compile time P,C
5311 * Compile a 0BRANCH and dummy offset
5312 * and push IF reference to fill in and
5313 * IF control construct flag.
5314 * A better flag: $4946 (ASCII for 'IF').
5319 IF FDB DOCOL,COMPIL,ZBRAN,HERE,ZERO,COMMA,TWO ; TWO is a flag for IF.
5324 * typical use: test IF code-true ELSE code-false ENDIF
5325 * ELSE is just a sort of intersection piece,
5326 * marking where execution resumes on a false branch.
5327 * ( adr1 n --- adr2 n ) compile time P,C
5329 * compile BRANCH with dummy offset,
5330 * resolve IF reference,
5331 * and leave reference to BRANCH for ELSE.
5332 * A better flag: $4946 (ASCII for 'IF').
5337 ELSE FDB DOCOL,TWO,QPAIRS,COMPIL,BRAN,HERE
5338 FDB ZERO,COMMA,SWAP,TWO,ENDIF,TWO ; TWO is a flag for IF.
5343 * typical use: BEGIN code-loop test WHILE code-true REPEAT
5344 * Will loop until WHILE tests false, skipping code-true on end.
5345 * ( --- adr n ) compile time P,C
5346 * Compile 0BRANCH with dummy offset (using IF),
5347 * push WHILE reference.
5348 * BEGIN flag will sit underneath this.
5349 * Better flags: $4245 (ASCII for 'BE') and $5747 (ASCII for 'WH').
5351 FCC 'WHIL' ; 'WHILE'
5354 WHILE FDB DOCOL,IF,TWOP ; TWO is a flag for IF, 4 is for WHILE.
5357 * ######>> screen 75 <<
5360 * EMIT count spaces, for non-zero, non-negative counts.
5362 FCC 'SPACE' ; 'SPACES'
5365 SPACES FDB DOCOL,ZERO,MAX,DDUP,ZBRAN
5368 SPACE2 FDB SPACE,XLOOP
5374 * Initialize HLD for converting a double integer.
5375 * Stores the PAD address in HLD.
5380 BDIGS FDB DOCOL,PAD,HLD,STORE
5384 * ( d --- string length )
5385 * Terminate numeric conversion,
5386 * drop the number being converted,
5387 * leave the address of the conversion string and the length, ready for TYPE.
5392 EDIGS FDB DOCOL,DROP,DROP,HLD,AT,PAD,OVER,SUB
5397 * Put sign of n (as a flag) at the head of the conversion string.
5398 * Drop the sign flag.
5403 SIGN FDB DOCOL,ROT,ZLESS,ZBRAN
5412 * Generate next most significant digit in the conversion BASE,
5413 * putting the digit at the head of the conversion string.
5417 DIG FDB DOCOL,BASE,AT,MSMOD,ROT,LIT8
5431 * Convert d to a numeric string using # until the result is zero.
5432 * Leave the double result on the stack for #> to drop.
5438 DIGS2 FDB DIG,OVER,OVER,OR,ZEQU,ZBRAN
5442 * ######>> screen 76 <<
5445 * Print n on the output device in the current conversion base,
5447 * right aligned in a field at least width wide.
5452 DOTR FDB DOCOL,TOR,STOD,FROMR,DDOTR
5457 * Print d on the output device in the current conversion base,
5459 * right aligned in a field at least width wide.
5464 DDOTR FDB DOCOL,TOR,SWAP,OVER,DABS,BDIGS,DIGS,SIGN
5465 FDB EDIGS,FROMR,OVER,SUB,SPACES,TYPE
5470 * Print d on the output device in the current conversion base,
5472 * in free format with trailing space.
5477 DDOT FDB DOCOL,ZERO,DDOTR,SPACE
5482 * Print n on the output device in the current conversion base,
5484 * in free format with trailing space.
5488 DOT FDB DOCOL,STOD,DDOT
5493 * Print signed word at adr, per DOT.
5497 QUEST FDB DOCOL,AT,DOT
5500 * ######>> screen 77 <<
5503 * Print out screen n as a field of ASCII,
5504 * with line numbers in decimal.
5505 * Needs a console more than 70 characters wide.
5510 LIST FDB DOCOL,DEC,CR,DUP,SCR,STORE,PDOTQ
5516 LIST2 FDB CR,I,THREE
5517 FDB DOTR,SPACE,I,SCR,AT,DLINE,XLOOP
5524 * Print comment lines (line 0, and line 1 if C/L < 41) of screens
5525 * from start to end.
5526 * Needs a console more than 70 characters wide.
5528 FCC 'INDE' ; 'INDEX'
5531 INDEX FDB DOCOL,CR,ONEP,SWAP,XDO
5532 INDEX2 FDB CR,I,THREE
5533 FDB DOTR,SPACE,ZERO,I,DLINE
5543 * List a printer page full of screens.
5544 * Line and screen number are in current base.
5545 * Needs a console more than 70 characters wide.
5547 FCC 'TRIA' ; 'TRIAD'
5550 TRIAD FDB DOCOL,THREE,SLASH,THREE,STAR
5551 FDB THREE,OVER,PLUS,SWAP,XDO
5553 FDB LIST,QTERM,ZBRAN
5563 * ######>> screen 78 <<
5566 * Alphabetically list the definitions in the current vocabulary.
5567 * Expects to output to printer, not TRS80 Color Computer screen.
5569 FCC 'VLIS' ; 'VLIST'
5572 VLIST FDB DOCOL,LIT8
5574 FDB OUT,STORE,CONTXT,AT,AT
5575 VLIST1 FDB OUT,AT,COLUMS,AT,LIT8
5579 FDB CR,ZERO,OUT,STORE
5580 VLIST2 FDB DUP,IDDOT,SPACE,SPACE,PFA,LFA,AT
5581 FDB DUP,ZEQU,QTERM,OR,ZBRAN
5586 * Need some utility stuff that isn't in the fig FORTH:
5588 * Emit dot if c is less than blank, else emit c
5590 FCC 'BEMI' ; 'BEMIT'
5594 FDB DUP,BL,LESS,ZBRAN
5602 * Output n in hexadecimal field width.
5608 FDB BASE,AT,TOR,HEX,DOTR,FROMR,BASE,STORE
5612 * Dump a line of 4 bytes in memory, in hex and as characters.
5614 FCC 'BLIN' ; 'BLINE'
5621 BLINEX FDB I,CAT,THREE,XDOTR,XLOOP
5627 BLINEC FDB I,CAT,BEMIT,XLOOP
5632 * Dump 4 byte lines from start to end.
5634 FCC 'BDUM' ; 'BDUMP'
5652 * Mostly for place holding (fig Forth).
5659 * Without the RTS, would misalign the stack.
5660 * NOOP NEXT a useful no-op
5661 ZZZZ FDB 0,0,0,0,0,0,0,0 end of rom program
5664 * These things, up through the lable 'REND', are overwritten
5665 * at time of cold load and should have the same contents
5668 * This can be moved whereever the bottom of the
5669 * user's dictionary is going to be put.
5673 FCC 'FORT' ; 'FORTH'
5676 FORTH FDB DODOES,DOVOC,$81A0,TASK-7
5679 FCC "Copyright 1979 Forth Interest Group, David Lion,"
5681 FCC "Parts Copyright 2019 Joel Matthew Rees"
5688 TASK FDB DOCOL,SEMIS
5690 REND EQU * ( first empty location in dictionary )
5691 RSIZE EQU *-RBEG ; So we can look at it.
5695 * "0 1 2 3 4 5 6 " ;
5696 * "0123456789012345678901234567890123456789012345678901234567890123" ;
5697 FCC " 0) Index page " ; 0
5698 FCC " 1) empty line on line 1 of screen 0 block 0 " ; 1
5699 FCC " 2) Title and copyright " ; 2
5700 FCC " 3) empty line on line 3 of screen 0 block 0 " ; 3
5701 FCC " 4) Error messages 1st screen " ; 4
5702 FCC " 5) Error messages 2nd screen " ; 5
5703 FCC " 6) empty line 3 screen 0 block 1 " ; 6
5704 FCC " 7) empty line 4 " ; 7
5705 FCC " 8) and line 1 of block 2 " ; 8
5706 FCC " 9) line 2 of block 2 screen 0 is pretty much empty too " ; 9
5707 FCC " 10) listen to this. Line three of block two is too " ; 10
5708 FCC " 11) and so is line 4 4 4 4 4 4 4 4 4 4 b2s0 " ; 11
5709 FCC " 12) screen zero block three first line " ; 12
5710 FCC " 13) second line fourth block (block three) screen 0 " ; 13
5711 FCC " 14) block three screen zero line 3 3 3 3 3 3 3 3 3 " ; 14
5712 FCC " 15) fourth line block three screen 0 0 0 0 0 0 0 0 0 0 " ; 15
5713 * "0 1 2 3 4 5 6 " ;
5714 * "0123456789012345678901234567890123456789012345678901234567890123" ;
5715 FCC " test 10 b0s1 aaaa " ; 0
5716 FCC " test 11 b0s1 ee ee ee ee " ; 1
5717 FCC " test 12 b0s1 oo oo oo oo oo " ; 2
5718 FCC " test 13 b0s1 eh ehe he eh eh " ; 3
5719 FCC " ( block 1 ) b1s1 oh ohoo oh oh oh " ; 4
5720 FCC " 15 test b1s1 " ; 5
5721 FCC " 16 test b1s1 " ; 6
5722 FCC " 17 test b1s1 " ; 7
5723 FCC " 18 test b2s1 " ; 8
5724 FCC " 19 test b2s1 " ; 9
5725 FCC " 1A test b2s1 " ; 10
5726 FCC " 1B test b2ws1 " ; 11
5727 FCC " 1C test b3s1 " ; 12
5728 FCC " 1D test b3s1 " ; 13
5729 FCC " 1e this completes our second screen b3s1 " ; 14
5730 FCC " 1F test b3s1 " ; 15
5731 * "0 1 2 3 4 5 6 " ;
5732 * "0123456789012345678901234567890123456789012345678901234567890123" ;
5734 FCC " fig Forth High Level Model Code " ; 1
5736 FCC " Copyright 2018 Joel Matthew Rees " ; 3
5737 FCC " ( block 2 ) " ; 4
5749 * "0 1 2 3 4 5 6 " ;
5750 * "0123456789012345678901234567890123456789012345678901234567890123" ;
5755 FCC " ( block 3 ) " ; 4
5767 * "0 1 2 3 4 5 6 " ;
5768 * "0123456789012345678901234567890123456789012345678901234567890123" ;
5773 FCC " ( block 4 ) " ; 4
5785 * "0 1 2 3 4 5 6 " ;
5786 * "0123456789012345678901234567890123456789012345678901234567890123" ;
5787 FCC " ( ERROR MESSAGES ) " ; 0
5788 FCC " DATA STACK UNDERFLOW " ; 1
5789 FCC " DICTIONARY FULL " ; 2
5790 FCC " ADDRESS RESOLUTION ERROR " ; 3
5791 FCC " HIDES DEFINITION IN " ; 4
5803 * "0 1 2 3 4 5 6 " ;
5804 * "0123456789012345678901234567890123456789012345678901234567890123" ;
5805 FCC " more test data 2 3 4 5 6 " ; 0
5806 FCC "0123456789012345678901234567890123456789012345678901234567890123" ; 1
5807 FCC "Test data for the RAM disc emulator buffers. " ; 2
5809 FCC " ( block 6 ) " ; 4