text
stringlengths
0
99.6k
WATCH
Another kind of debugging aid, very useful in some situations, is WATCH. WATCH will keep an eye on a memory location, and halt program execution when it changes, printing out all the words executed in that line of execution. This can be handy when trying to locate a word that is altering the value of a variable, or even destroying the dictionary! For example, if you have a variable PROBLEM.VAR , which is getting changed to a bad value at some point, and you can't find out where, first initialize the variable to the value you want it too have, and then type: PROBLEM.VAR WATCH . When the value of PROBLEM.VAR changes from what it was when you executed WATCH, the system will stop program execution, and execute UNRAVEL , which displays the higher level nesting of Forth words which occurred up to that point.
UNRAVEL
Essentially, UNRAVEL displays all words executed by Forth up to the point that UNRAVEL was executed. It does this by decoding the contents of the return stack. (For more information on this, see Starting Forth.) This allows you to see what words preceeded a given words execution. For example:
: FOO BAR ;
: BAR FOO2 ;
: FOO2 FOO3 ;
: FOO3 UNRAVEL ;
When FOO is executed, it will ultimately arrive at FOO3 , which will execute UNRAVEL. This will cause the following display at your terminal:
FOO FOO3 FOO2 BAR 7106 INTERPRET RUN
(the words 7106 etc, are system words which are responsible for executing your input. They will always appear, and can just be ignored.)
This can be handy in determining what words lie in a certain execution path. Note that if you store values on the return stack, or if you use a DO LOOP, the return stack will contain stuff that is not relevant. UNRAVEL will just print these out as numbers, but this in itself, can be a very useful clue to debugging an application.
?PARAMS
This word will give an UNRAVEL trace if there are not enough parameters on the return stack. One use of ?PARAMS is to add run time error checking to the system. No run time error checking is performed by Forth. This allows applications to run faster, but can make life harder when you are developing an application. Forth does allow you to add this, if you want. When the program is finished, you can remove it, and the application will then run at full speed. For example, you can redefine the following:
: DROP 1 ?PARAMS DROP ;
: 2DROP 2 ?PARAMS 2DROP ;
etc.
DROP and 2DROP will now check the stack for the proper number of arguments, and give an error message, and an UNRAVEL dump if there are not enough arguments. One very nice thing about this way of doing things is that you don't have to re-edit your application once it's finished. You can redefine all of FORTH's stack operators in this way, load them in, and then load your application. When it's debugged, simply loading your application without loading the redefined stack operators will remove the run-time error checking, and allow your application to run at full speed.
This is only one example - such runtime error checks can be very useful, and are limited only by your imagination and debugging skills.
.S
.S is a common and highly useful word. It non-destructively prints out the contents of the stack, or the message STACK EMPTY, if there is nothing on the stack. Note that this version of .S displays unsigned numbers, so, for example, a value of -1 will display as 65535.
DUMP
DUMP takes two arguments, a starting address, and a count. It will then dump the contents of the memory locations to the current output device (in hex) and also the symbolic ASCII contents of those addresses. A period will be displayed if there is no corresponding ASCII character. This can be extremely useful for monitoring memory arrays, or simply trying too figure out why a CODE defintion doesn't work. Note that DUMP includes PAUSE. See PAUSE for more information.
EXAMPLE:
2000 64 DUMP will display 64 memory locations starting with 2000.
DEFER and IS
These two words form a remarkably useful combination. DEFER is similar to the FORWARD declarations of other compilers. Ordinarily, you must compile a word before using it in another definition. DEFER lets you out of this obligation. EXAMPLE:
DEFER FOO ( postpone working on FOO)
: BAR FOO ;
Later in the application, you may define:
: (FOO) ." This is foo. " ;
At this point, FOO will simply give an error message, "UNSPECIFIED VECTOR". You must tell FOO what to do, like this:
' (FOO) IS FOO // make FOO do (FOO)
Note the tick (') - it's important! Now, typing FOO will result in the execution of (FOO).
Note that if you dislike the way a DEFERed word is performing, you can redefine it without recompiling the entire application. For example:
: NEWFOO ." This is new foo." ;
' NEWFOO IS FOO
FOO will now execute NEWFOO, instead of (FOO). This allows you the incredible freedom of altering an already compiled word's behaviour - this is, so far as I know, unique to Forth!
Note that the usefulness of DEFER IS isn't limited to development. IS may be used inside a definition, and it's a convenient way to handle vectored execution. (See Starting Forth for more on vectored execution.) For example:
DEFER MESSAGE
: HI ." Hi there." ;
: BYE ." See you later " ;
: SOLONG ['] BYE IS MESSAGE ;
: HELLO ['] HI IS MESSAGE ;
Executing SOLONG will cause MESSAGE to print "See you later" while executing HELLO will cause MESSAGE to print "Hi there". Note that when used inside a colon definition, you need to use ['] instead of ' .
*lk:blazin.doc2
*hd3:Blazin' Forth Documentation,System Information,-#-
*cn1;Input and Output*cn0
Blazin' Forth supports all the necessary words to handle any peripheral device on the serial bus, or on the IEEE bus, if you have an extension card like the BUSCARD II. I left certain definitions out deliberately, so that users who require them can define them as they like.
Higher Level Words.
The system supports I/O redirection of output to a printer. The words PRINTER and NOPRINTER will open a channel to a 1526 printer, connected as device # 4. After executing PRINTER , all output will be directed to the printer. NOPRINTER will redirect output to the screen. Both PRINTER and NOPRINTER are DEFERed (see DEFER for more information), to make it easy for you to alter them, if you need to, without having to recompile the system. #LP is a constant which contains the current device number of the systems line printer. As currently configured, PRINTER sends a control code to the printer which turns on paging, and NOPRINTER sends a control code which turns paging off (these codes are for the 1526 printer). If your printer uses different control codes, or doesn't require them, you will probably want to revector these words. I would suggest you examine the definitions of LP and NLP for an example of how to write such words in Forth. In particular, it is important to have PRINTER set the user variable PRINTING? to TRUE, and NOPRINTER should reset PRINTING? to false.
The word DOS" has been provided to allow you to send commands to the disk drive from Forth. Any command which is recognized by the 1541 or a compatible drive may be sent using this word. As an example:
DOS" N0:DISKNAME,ID"
will format a disk in drive 0. Note that there must be a space between the " and the command.
To read the disks error channel, you can use the word ?DISC . Note that ?DISC will only display the message if there is an error.
MOUNT
MOUNT initializes the virtual disk operating system of Forth. It *must* be used before any word which accesses the disk. Note that you only need to use this word once, at the start of a session, and not before every LIST or LOAD command. (You must reissue it if you use RESTART, or if you change disks during a session.) Note that the disk light will remain on while the system is in operation. This is normal, and should not cause you a second thought.
If you require information on accessing standard CBM disk files, please see the appendix. These are the only words required in normal usage of the system.
*fp0
*cn1;Extensions to the 83 Standard*cn0
BOOLEANS:
Blazin' Forth contains the following boolean tests which are not part of the 83 standard:
0<> Leaves a true flag if top of stack is non-zero.
0> Leaves a true flag if top of stack is positive.