23.10.2013 Views

FAST Forth Native-Language Embedded Computers

FAST Forth Native-Language Embedded Computers

FAST Forth Native-Language Embedded Computers

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

However, Fast<strong>Forth</strong>'s EXPECT is vectored, so program-<br />

mer utilities such as the assembler and editor may include<br />

a more extensive line editor capability. This allows exten-<br />

sive line editing on the target hardware during develop-<br />

ment without sacrificing nucleus size. The line editor is<br />

removed before the program is committed to ROM.<br />

The line editor uses a number of variables to track the<br />

size of the line being edited, the location of the cursor, and<br />

other characteristics. These are annotated in detail in the<br />

source code and in the glossary.<br />

A headerless string array is defined for the history<br />

capability. This array stores each string in the history in the<br />

traditional <strong>Forth</strong> count-and-string format. The starting<br />

address of each entry is stored in another array, STRARRAY.<br />

This is an array of pointers. A variable, NXTLN, points to<br />

the current string indirectly by pointing to the string array.<br />

This means that moving from each entry to the next<br />

consists of incrementing or decrementing NXTLN by the<br />

size of a pointer, and handling wrap-around correctly.<br />

The traditional <strong>Forth</strong> array was not used here because<br />

of speed considerations. This would have been defined<br />

something like this:<br />

: ARRAY CREATE STRINGS * ALLOT<br />

\ count --- I build array<br />

DOES> >R STRINGS * R> + ;<br />

\ entry# --- addr I<br />

This means that every access to the array would have<br />

involved a multiply. The double-indirection method elimi-<br />

nates this time-consuming operation. Furthermore, using<br />

the double-indirection method means that calculations are<br />

performed only at the time a new line is selected, never at<br />

access time.<br />

Using these variables and indirection, strings are loaded<br />

into the string array and copied from it to the edit buffer<br />

by always referring to the variable.<br />

A design decision was made that, when a line is copied<br />

from the history buffer, the cursor will appear on the left<br />

end of the line. This is due to experience with <strong>Forth</strong>. Often<br />

a line must be edited and re-executed because a vocabu-<br />

lary or other modifier was not given. These modifiers<br />

typically must be at the beginning of the line, not the right-<br />

hand end. Also, the Home key provides a quick method<br />

for the user to move to the right-hand end of the line,<br />

should he wish to.<br />

The Implementation<br />

The glossary indicates the function of each word. The<br />

listing should be read along with the coding descriptions<br />

of the words given below.<br />

The implementation starts with a loader screen, screen<br />

501. For development purposes, the debugger can be<br />

called. For inclusion in the utilities, this call to the<br />

debugger is commented out.<br />

The word TASK is used as a marker in the dictionary.<br />

During development, this is forgotten and recompiled<br />

below the application to allow the programmer to re-<br />

compile the application by entering the phrase RETRY<br />

<strong>Forth</strong> Dimensions<br />

TASK. Once an application is deemed complete and is to<br />

be added to the utilities, TASK is forgotten, the application<br />

is compiled, then TASK is redefined on top. Used in this<br />

manner, the word acts as a moveable marker to indicate<br />

where the utilities end and applications begin.<br />

The word BELL defines a word which emits an ASCII<br />

bell character. This is typically used as an alarm or an alert<br />

on some error.<br />

Line five of screen 501 forces the line input code to be<br />

(EXPECT) , the nucleus input working word. This is done<br />

at compile time to ensure (especially during debugging)<br />

that, in the event of a compile-time error, EXPECT will<br />

have a valid working word to execute.<br />

On line seven, we establish the vocabulary where most<br />

of the line editor will reside. This hides the bones of the<br />

application from the user once the project is completed.<br />

Line nine of screen 501 effects compilation of the next<br />

six screens. It is written this way because the word +BLK<br />

and its family may not exist when this code is compiled<br />

into the utilities.<br />

Screen 502 compiles a number of variables and constants.<br />

These are described in the glossary. The most<br />

important one to the user is the constant STRINGS, on line<br />

seven. It indicates the number of strings to be preserved<br />

in the string array. It is the equivalent of the Unix<br />

environment variable HI STORY. In order to effect changes<br />

in this constant, the line editor must be recompiled.<br />

On line 11, a most implementation-specific constant<br />

sets the maximum line length. This constant should<br />

specify the maximum line length that the user may enter<br />

into the terminal input buffer in the course of normal <strong>Forth</strong><br />

operations.<br />

The next screen, 503, shows the construction of the<br />

string array by the word MAKESTRS. This word allots<br />

memory for each string in the buffer, and stores its address<br />

in the array of string addresses, STRARRAY. This word is<br />

executed once, at compile time.<br />

The next two words handle the process of moving from<br />

one entry in the string array to the next. UP moves from<br />

a given string to the next higher address string, and<br />

handles wrap-around at the top. DN defines the process for<br />

moving down in the suing buffer. These two words turn<br />

the string array into a ring buffer for strings.<br />

The phrase NXTLN F@ F@ will show up often in the<br />

remainder of the code, as that phrase always returns the<br />

address of the current string buffer entry. In a traditional<br />

<strong>Forth</strong>, this phrase would probably be built into a word to<br />

make the code more readable. However, the Fast<strong>Forth</strong><br />

optimizing compiler turns this three-word phrase into two<br />

processor instructions. The implementor chose to go for<br />

faster code over programming elegance.<br />

Similar phrases based on variables, such as CURS F@<br />

and SIZE F@, resolve to one processor instruction, and so<br />

are not built into words that make pseudo-constants.<br />

HISTORY, and its alias HI ST, at the top of screen 504,<br />

show the ring string buffer at work. They exist to allow the<br />

user to view the contents of the string array. They are made<br />

generally available to the user by placing them in the FORTH<br />

vocabulary. HISTORY walks through the ring buffer, dis-<br />

11 March 1994 April

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!