.KEYWORD quartus
.FLYINGHEAD PROGRAMMING POWER
.TITLE Palm programming with Quartus Forth
.DEPT
.SUMMARY Have you ever wanted to program your Palm device right on your Palm device? Well, now’s your chance. Quartus Forth is a faithful implementation of the somewhat funky Forth programming environment that runs right on your Palm handheld. To learn more, read this month’s Programming Power column by Mark Lawson
.AUTHOR Mark Lawson
When most people start looking at programming their Palm devices, they think of programming in the C or C++ programming languages. However, there are alternatives for the adventurous or curious, or those like me, who never really got along with C. I’ve spent the last few weeks looking at doing a project using Quartus Forth, an ANSI compliant, on-board, Forth compiler for Palm devices. It’s a strange language by normal standards, but has proven itself well worth the look. In this article I can only really scratch the surface, but I hope to give you a taste for what it can do.
By the way, when we say "onboard", this means you can actually program in Quartus Forth right on your Palm device.
.H1 Bits of history
Charles Moore invented Forth. He named it FORTH because, in his mind, it represented the fourth generation of programming languages. So why, you might ask, wasn’t it named FOURTH? Was it just that Moore couldn’t spell? Actually, the reason gives an interesting insight into computer history. FORTH was so named (without the U) because the operating system Moore was using at the time only supported five characters in file names. Adding the U would have pushed it to a sixth character. And so it because FORTH instead of FOURTH.
Unlike most computer languages of the time, the name FORTH wasn’t an acronym. It was its own word. As such, rather than spelling it out in all upper case, FORTH was eventually treated like other proper names and presented as "Forth".
.H1 Getting started
Quartus Forth is shareware; the evaluation and registered versions differ in the fact that the registered version allows you to generate stand-alone executable .PRC files that don’t require a separate runtime library.
To get started, you’ll need to download Quartus Forth either from Quartus or one of the normal Palm download sites (see Product Availability and Resources at the end of this article). The download includes a copy of RsrcEdit for onboard resource creation, which you need to install as well.
Between them the two programs will use under 200kB of memory on your Palm device. You’ll also find the library memos in MemoPad archive format, and you’ll need to install them, too. More about these memos later in this article.
Here’s a nice programming safety hint: As with all Palm programming, if you’re not sure what you’re doing, install to the POSE (Portable Operating System Environment) emulator on your PC rather developing on your Palm device.
Once installed, you should see a screen such as that in Figure A.
.FIG A Here’s the Quartus Forth workspace.
.H1 The Forth world
Most common programming languages today are based on systems in which previously declared functions are passed variables of a particular type. These are glued together with programming logic like If-Then-Else and For-Next keywords that are integral parts of the language itself. You effectively program in a language that has few words but a rich syntax. Forth uses a completely opposite system; you program in a language that has a simple syntax but is rich in words. This is achieved through two mechanisms, the stack and the interpreter.
The stack is a last-on-first-off queue used by the system to store numbers temporarily before they’re worked on by words. The classic visual for this is one of those spring-loaded dish racks you see in a restaurant. The first dish goes on the rack. Then the next dish goes on top, pushing the first dish down. And so forth. When a dish is removed, it’s the top dish (the most recently placed on the rack, or stack in computer terms). Finally, the last dish removed is the first that was ever placed on the dish rack.
The Forth interpreter reads your program as you input it, either as a full program or line-by-line. It uses spaces to delimit words, and as it comes across each word, it looks up the word’s definition in a dictionary and executes the definition. Absolutely anything can be a word in Forth, including numbers and punctuation. If the interpreter finds a number that has no definition, it pushes it onto the stack.
Forth uses postfix notation for arithmetic. Postfix is a little different than normal programming. For example, to add the numbers 5 and 4 together you would enter "4 5 +" (remember the spaces). This makes sense because the interpreter will see 4 and then 5 and push them onto the stack. Next it will see +, which is a word with the definition "take the top two values from the stack, add them, and push back the answer." If we use the Forth language word ‘.’ we can print out the top stack value — 9, in this case. To create new words, we use the words ‘:’ and ‘;’ which start and stop the compiler respectively and put our definition between them. We can now create a word "hello" which prints out "hello world" using the ‘.’ word as in:
.BEGIN_CODE
: hello . “Hello world” ;
.END_CODE
By entering some of this into your Palm device, you can get Quartus Forth to perform operations. Figure B shows some of this in action.
.FIG B With simple words and arithmetic you can make your own definitions in this program.
As you can imagine, there’s more to it than that, but that’s the basic mechanism. A Forth program consists of word definitions. Don’t think of these as traditional computer functions; you’ll only get a headache. A word can be almost anything, including punctuation. Words often act on values on the stack, but not always, consuming them and putting results back on the stack.
Now let’s look at how Quartus Forth works with your Palm device.
.H1 Palm programming in Forth
Quartus Forth programs are created using memo records. The distribution contains a library of about 60 memo records that define useful word collections for MemoDB access, reading .DOC format files, or handling IDs and events. To create a new program, simply create a new memo and start it with a backslash, a space, and the name of the program followed by optional comments.
Here’s a listing building on our hello word earlier:
.BEGIN_CODE
\ helloworld ML 1-Feb-00
: hello ( –) .” Hello World” ;
: go
MainForm
hello
begin ekey drop again ;
.END_CODE
To run this program, create the memo, then go into Quartus Forth and write "include helloworld." If you get an ok, then write "go". The screen should clear and "Hello World" should appear.
The "go" word definition consists of MainForm, a word which reloads a blank form, effectively clearing the screen. Next, it includes "hello", our Hello World printer routine we wrote earlier. Finally, the "go" definition includes a loop which reads events from the queue and simply drops the value from the stack (i.e., it does nothing but sit there).
As the state of the stack is so important, most words include a stack diagram which shows what’s taken off the stack and anything that’s put on, in the form ( off — on ). The left bracket is a comment word terminated with the right bracket — remember to leave a space. In a lot of cases you’ll find no comments to a word, just the diagram, so it’s worth getting used to reading them. As an example, here’s the stack diagram for the DmCloseDatabase OS call:
.BEGIN_CODE
DmCloseDatabase ( dbP. — Err )
.END_CODE
The above line shows the call expects a double cell (i.e., a 32-bit pointer value, as input and leaves a numeric error flag as output). If you’ve installed Quartus Forth, you’ll find the rest of the Palm OS calls and a key to the types in stackdia.txt file in the docs directory.
.H1 Handling the event loop
In our original program, we simply dropped any events we were sent. Events are things that happen on the Palm device, like a screen tap, while the programming is running. Now we’ll do something with them. This time we’ll pick up the penDown event and print our message at that location. For this we need some more words:
.BEGIN_LIST
.BULLET The event names so we know to enter penDownEvent. These come from the Events collection;
.END_LIST
.BEGIN_LIST
.BULLET The current co-ordinates of the pen. This is in Events, too, as the word coords\@ ( — y x);
.END_LIST
.BEGIN_LIST
.BULLET A word to move the print position for the ‘s’ word to the pen position. This needs the at ( y x –) word in the graphics collection.
.END_LIST
Adding these pieces gives the following listing:
.BEGIN_CODE
\ helloworld ML 1-Feb-00
needs events
needs graphics
: hello ( –) .” Hello World” ;
: handler ( ekey–)
dup penDownEvent = if
coords\@ at hello
else drop then ;
: go
MainForm
hello
begin ekey handler again ;
.END_CODE
The "needs" word means the program uses the words from the library referenced. It’s like #include in C. But what’s all this "dup … = if" stuff?
Let’s take it one step at a time:
.BEGIN_LIST
.BULLET ekey puts an event on the stack;
.END_LIST
.BEGIN_LIST
.BULLET handler needs to test it to see if the event is a key, but testing it will consume the event, so we need another copy, hence dup;
.END_LIST
.BEGIN_LIST
.BULLET penDownEvent puts the event code of a penDownEvent on the stack;
.END_LIST
.BEGIN_LIST
.BULLET ‘=’ takes the top two numbers off, i.e., the event and our code, and replaces them with true or false;
.END_LIST
.BEGIN_LIST
.BULLET if consumes the top value. If true then "coords\@ at hello" is executed, otherwise drop drops our remaining ekey off the stack.
.END_LIST
Figure C shows HelloWorld with these events being handled.
.FIG C Here is HelloWorld with events.
.H1 The next step
I hope in this brief introduction that I’ve given you a hint as to what you can do with Forth. I’d recommend you download Steve Donahue’s excellent .PDF tutorial on using resources in your program as your next step.
Although I’ve been frustrated at times, I’m going to keep going with Forth. If nothing else, it’s blown fresh air into my mind and started to loosen the ingrained habits of 15 years of programming in the mainstream.
It’s not easy, though. If you’re new to programming on Palm devices and new to Forth, then you’re fighting on two fronts. Having said that, you might find it pays off. For instance, you can get free development systems for the Mac, Windows, and Linux platforms, so it might be the only language you need.
.BEGIN_SIDEBAR
.H1 Product availability and resources
You can visit the Quartus home page at http://www.quartus.net. Registration is $69.95 USD and comes with 60 days of technical support via email. Quartus also hosts a contributor’s download section and a lively discussion area moderated by Neal Bridges, the developer.
Look in the download section at http://www.quartus.net for Tutorial 1.02, a great beginner’s guide by Steven Donahue.
I bought the two books on the must read list, Starting Forth and Thinking Forth, both by Leo Brodie. ISBN 0-13-843079-9 and 0-13-917568-7 respectively. I didn’t think much of Starting Forth — it’s a revamped 1983 edition and way out of touch. I’d recommend Thinking Forth though; it’s a good insight into Forth design philosophy. Visit http://www.amazon.com for these titles.
If you want to see the home of all things Forth, visit http://www.forth.com. This is where Charles Moore currently hangs his hat and you can read much about the language and its history.
UK readers should go to MicroProcessor Engineering Ltd. at http://www.mpeltd.demon.co.uk/. They’re Forth specialists, very friendly, and a great place for Forth books and tools.
.H1 Bulk reprints
Bulk reprints of this article (in quantities of 100 or more) are available for a fee from Reprint Services, a ZATZ business partner. Contact them at reprints@zatz.com or by calling 1-800-217-7874.
.END_SIDEBAR
.BIO Mark Lawson is MD of EM Connect Ltd., which specializes in collaboration and distance learning solutions. He can be reached at markl@emconnect.com.
.DISCUSS http://powerboards.zatz.com/cgi-bin/webx?13@@.ee6e0a4


