Jump to content
  • entries
    4
  • comments
    13
  • views
    6,852

Lesson 1: Getting to know fbForth 2.0


Lee Stewart

1,855 views

blog-0640431001436417048.thumb.gif.b04e8533e3f0a7b16bd72a775a04ba1c.gifThis is the first of several tutorials to help those new to Forth, fbForth 2.0 in particular, to understand the language and its programming environment, as well as to gain some facility with it. fbForth is based on TI Forth, which was derived mostly from FIG-Forth with some influence from Forth-79. There are more recent Forth standards; but, compatibility with TI Forth was the prime concern.

 

The biggest difference between TI Forth and fbForth is that fbForth is a file-based system, whereas TI Forth reads/writes directly from/to disk sectors without regard to any file structure. This is dangerous for the health of the disk and the user, especially, if you were to inadvertently use a disk with files on it for other systems. It also makes it difficult to exchange programs. Not only does fbForth coexist with as many unrelated files as will fit on a disk, you can create many different blocks files on the same disk as long as there is room. A TI Forth disk cannot contain anything but Forth blocks.

 

I suppose that is more than sufficient for preamble. Let’s get on with learning fbForth.

 

To operate the fbForth 2.0 System, you must have the following equipment or equivalent:

  • TI-99/4A Console

  • Monitor

  • fbForth 2.0 Module (see this forum thread’s post #1 to get yours: fbForth—TI Forth with File-based Block I/O )

  • Peripheral Expansion Box (PEB) with

    • 32 KiB Memory Expansion

    • Disk Controller with 1 or more Disk Drives

    • RS232 Interface (optional)

  • Printer connected to RS232 interface (optional)

 

 

If you wish to work through these tutorials but do not have this equipment or equivalent (CF7+ or nanoPEB, which substitutes for a PEB with the first three PEB items above), all of the software and firmware are available in the above-referenced thread for the Classic99 and MAME emulators. I also can supply the same for CaDD Electronics’ PC99 emulator, if you need it.

 

 

It is a good idea to have a copy of fbForth 2.0: A File-Based Cartridge Implementation of TI Forth (the manual—available in the above forum thread) for reference, especially for looking up commands (Forth words—more below) in the glossary (Appendix D). Please note that the glossary is in ASCII order, which is listed at the bottom of every glossary page. Also, if you have a copy of the first edition of Leo Brodie’s excellent beginner’s book on Forth: Starting FORTH, Appendix C of the manual cross-references conflicts with fbForth 2.0.

 

After powering up:

 

blogentry-29677-0-61270900-1436452848_thumb.png

 

and selecting “2 FOR FBFORTH 2.0:12for 40-column text mode, say,

 

OptionScreen.png.65ef2c21e51310c52aff4f85139be409.png

 

If FBLOCKS is found, you will be presented with:

 

BootScreenFBLOCKS.png.80392a66634dc7fcb221bfe3fc5c01d1.png

 

followed by (after your color choice):

 

BootScreenFBLOCKS2.png.2cb28721af38afa332de571ad8958b8f.png

 

If FBLOCKS cannot be found, you will see:

 

BootScreenNoFBLOCKS.png.8b3045de72705da7cc31b931d57d3ef7.png

 

 

The system blocks file is FBLOCKS and must be present in DSK1 for the first series of screens to display. If fbForth does not find it there, the second screen displays. fbForth will still work just fine. You just won’t be able to display the menu of loadable utilities with MENU until you make FBLOCKS the current blocks file, which you can do by typing the following at the console’s flashing cursor if FBLOCKS is in DSK2, say:

USEBFL DSK2.FBLOCKS  1 LOAD

You can force fbForth to look for FBLOCKS on another disk at boot time if you hold down the number of that disk immediately following your startup-screen selection.

 

Notes about the welcome screen:

  • The first two lines and the last line of the welcome screen appear regardless of the presence of FBLOCKS.

  • The version number of the cartridge includes the revision number after the ‘:’.

  • The line beginning with “FBLOCKS mod:” comes from block #1 of FBLOCKS and will always reflect the current date of the system blocks file, FBLOCKS, which is always kept up to date in the above forum thread.

 

Commands in Forth are called “words”. You will note that Forth words included in the normal text of these tutorials appear in boldface and are surrounded by spaces. This may look awkward when the space after a word precedes a comma, period or similar punctuation mark; but, since those punctuation marks are also Forth words, this practice avoids ambiguity.

 

Speaking of spaces around words, that is how the Forth text interpreter ( INTERPRET ) knows it has the next word to look up in its dictionary (linked list of already defined words). It searches the dictionary from the most recently defined word to the very first word defined. In fbForth, that word is EXECUTE . See what I did there? EXECUTE has a space after it and it’s before a period. You are in the hands of the Forth text interpreter in two places, viz., at the console’s blinking cursor and when a block is loaded by the word LOAD . The input stream is viewed by the interpreter as a series of tokens separated by one or more spaces.

 

If the interpreter finds the word, it executes the word and gets the next token.

 

If the interpreter cannot find the token as a word in the dictionary, it checks to see if the token can be converted to a number in the current radix (number base). If it can, it pushes that number onto the parameter stack, which is often termed the data stack or, simply, the stack. The parameter stack, by the way, consists of a stack of cells, much as a stack of plates in a cafeteria, with the same restriction: You can only readily remove (pop) the top plate, i.e., the last cell on the stack is the most accessible and thus the first one popped off. This Last-In-First-Out situation is known as LIFO. Furthermore, in fbForth, a cell is 16 bits or 2 bytes wide. In computer parlance, 2 bytes constitutes a word; but, to avoid confusion with talking about Forth commands as words, we will generally use “cell” instead of “word” to mean “2 bytes”.

 

Finally, if the token is not a word in the dictionary and it cannot be converted to a number, the interpreter gives up and issues an error message that repeats the word it could not find followed by a question mark. It also clears the stacks (parameter stack and return stack, about which more later) and, if loaded from a blocks file, leaves two numbers on the parameter stack to aid in finding where in the input stream the error occurred. These numbers are the contents of user variables IN (the position in the input stream immediately following the token causing the error) and BLK (the block number being interpreted). When loading a block that aborts with the error report just described, you can type WHERE to put you into the editor with the cursor at the error. We will talk more about the editor in another lesson. Otherwise, you may just want to type SP! (stack pointer store) to clear the parameter stack.

 

After you finish entering one or more successfully interpreted words and/or converted numbers with <ENTER>, the interpreter will display “ ok:n” to let you know its success. The ‘n’ after the colon is the depth of the parameter stack, i.e., how many numbers are currently on the stack. Here are a few lines typed at the console:

 

blogentry-29677-0-56407800-1436500746_thumb.png

 

The first line is from just tapping <ENTER>. Everything is OK with nothing on the stack. The second line pushes ‘4’ onto the stack and indicates all is well with one number on the stack. The third line pops and prints ( . ) the number, showing the stack as now empty. The last line obviously was not understood by the interpreter, hence the error message.

 

Let’s wind this lesson up with showing you the most common way to define a new word in Forth. The defining word we will use is : . : starts a high-level Forth definition, which is terminated with ; . The first token that must follow : is the name for the new word. fbForth is case-sensitive. HELLO is different from hello . In our definition, we will use the word .” , which means “print string”. ." accepts any characters into the string except for " , which is the terminator. As soon as it sees the " , it prints the string:

 

blogentry-29677-0-47490800-1436504627_thumb.png

 

We will now define the word HELLO and add CR to the definition before and after the print-string code. This will put the cursor at the beginning of the next line each time it is executed. Typing the newly defined word, HELLO , will execute its contents:

 

blogentry-29677-0-58181500-1436503516_thumb.png

 

Remember that Forth words must be separated by spaces. The Forth Interpreter looks in the input stream for the next word until it finds a space or the end of the input stream. Upon finding a space, it then looks for the next word that starts with the next, non-space character. There are six words used in our definition of HELLO above, which are:

:
HELLO        <---the word we are defining
CR
.”
CR
;

That’s all for now.

 

  • Like 8

6 Comments


Recommended Comments

Professor Stewart,

This looks great! I printed off your first lesson, and since I have some time available next week, I'll go over every inch of it and see where I end up!

 

I also plan to get your manual printed up and bound at Office Despot next week as well.

  • Like 1
Link to comment

And sometimes it helps to verbalize some of these commands... ." , I always refer to as "dot quote" and by doing so, I know it is the start of a printed string... " is simply "quote" for me.

 

I think you mentioned it above, but ! is "store".

 

Another thing that helps me out (as we discussed in the SHIFT838 newsletter) is how your highest level colon definition can read like a sentence. :)

 

Im sure youll get there in your tutorials... Just excited for the next installment... Or 7.

  • Like 1
Link to comment

Woo Hoo, I'm gonna have a whole three hours worth of time to myself later this afternoon. Sooooo, I'm going to flip some switches on the 'ol TI, sit down with your lesson plan and have me some fun! I'll get back to ya!

  • Like 1
Link to comment

Just got done with lesson one, and I find lesson two is already out!

Wow, I'll have to print it out after work tonight. Thanks!

med_gallery_35324_1027_49498.jpg

  • Like 3
Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...