Forth - the LEGO of Programming Languages
1/19/95
Forth, the programming language, is like LEGO blocks. LEGO blocks are simple, and they snap together or apart to facilitate experimentation. As programming languages go, Forth is as simple as they get. Unlike syntactic languages that require the use of a complex combination of syntactic structures to create an expression, Forth is a semantic language where the syntax is just words with spaces between them and the meaning is the defintion of the words. The words are like LEGO blocks, they snap together or pull apart to facilitate experimentation. They don't require elaborate typing or elaborate specification of what is passed between functions that reduce the modularity of other languges. This most modular and interactive of programming lanuages is well known for providing a development enviroment and methodology that is ideal for experimenting with small computers or new computerized equipment. Those seeking to experiment with programming robots should certainly consider using Forth to get the most from themselves and their robots.
Forth is Simple
If you designed the most simple computer you could you might have what is called the Forth virtual machine. It is so simple that one can understand it completely very quickly. Essentially the Forth virtual machine has two stacks, one for data and one for return addresses, and access to memory. One creates a dictionary of words, that have names and code that operates on this virtual machine. Forth provides some standard words to perform the most common functions on the Forth virtual machine. Everything about Forth is simple. The simplicity is unmatched in any other programming langugage, and this is why Forth is usually the first language implemented on new machines, is the language that boots up on Sun workstations and new "Open Boot Firmware" bus machines, can fit in the ROM of common microcontrollers, and can be easily implemented directly in hardare.
Why you didn't learn Forth in Computer Science 101
LISP originated in an academic environment and 'C' came through one. LISP came out of MIT and has been used in Artificial Intelligence research. Bell Labs released 'C' and UNIX and made them available to universities at greatly reduced costs where they became standard course material. With its clearly demonstrated power, 'C' and a generation of college educated 'C' programmers became the most commonly used high level language. Special computers were made to run LISP more effictively but were simply too expensive to gain a foothold outside of research and left people with the impression that specialized hardware was only academic.
Meanwhile Forth evolved as a tool to get the most productivity and performance in small computers. Many programmers forget that 98% of the microprocessors made go into embedded applications and that only 2% go into personal computers or workstations. I found a statement about Forth in the SFRSA BBS by their president Brad Smallridge that amazed me. Brad said that Forth was like LISP, only good for research, and that 'C' was the way to go in the real world.
While it is true that there are more jobs for 'C' programmers than for Forth programmers, Forth is still popular in embedded systems, real-time control, and robotics where it provides unique capabilities.
In addition to the language itself Forth provides a complete development environment. Forth may provide the compiler, the editor, the runtime library, your application's real-time operating system all in a few kilobytes of memory. Instead of going through a foreign operating system to access each step in the development process, design, coding, compiling, linking, debugging, each step is available in an integrated development environment in Forth.
With Forth at work I could write code to test a new device, run it, and show the results to the engineer who built it about as fast as he could describe what it was suppose to do. The first time I wrote a test program for the chief engineer and had it running before he could walk back to his office he told me, "You know the programmer that you replaced would have taken a week to do that!"
Forth is so simple that the virtual machine, the compiler, or any part of the system is easily understood. It may be so simplee that the complete source for the compiler, interpreter, and application may only take a few kilobytes of memory. Many programmers get distracted by the amazingly clear overview that you get in Forth and dive into experimenting with language design or compiler optimization. When I learned Forth I tried a few practical experiments and wrote programs to test the engineer's new hardware at work in record time. I tried some experiments and found it was easy to programs things like CAD systems and flight simulators even on very small computers in Forth. I moved on to voice and image recognition, expert systems, learning systems, multimedia, and parallel compiler design.
Greatest Strengths are Greatest Weaknesses
Forth is not as well known or widely used as 'C', more things are published in 'C', it is widely used in universities, and there are more jobs in 'C'. These things are all true and for these reasons some Forth programmers write their Forths in 'C'. This gives them the easiest way to access all the 'C' libraries while still being able to get the benefits of Forth programming. The fact that Forth is not the most popular of languages is seen as a weakness by many, but as a strength by some. Many companies consider their use of Forth to be a trade secret, they don't want their compeditors to reap the benefits of reduced development time and cost. Just as Forth was one of the secrets the Russians used to compete against the much more well funded United States.
You my have heard that Forth is a "write only" language, that it is hard to read or maintain. Forth has developed this reputation because Forth provides so much freedom to the programmer that they can do just about anything they want. If they don't pay attention to writing code that they or someone else can read later it can easily become unreadable. But Forth also has a history of many sucessful large projects involving teams where readability and maintenance were big issues. But the freedom to do something quick and dirty may be just what one needs to get that experimental widget working by tomorrow morning. If you need to rewrite the code next week and provide proper documentation that can be done.
The same practices that make it possible for 100 'C' programmers to all work on the same program also severly limit whan any individual programmer can do in his code. In big 'C' projects you may need to get 100 programmers to agree on all sorts of tiny details. The language needs to support this with built in blinders or management needs to enforce the practice on a given project.
There are also many public domain versions of Forth that are far below minimal completeness or professional quality. This too is both a strength and a weakness. If you want the cheapest tools, then Forth is very hard to beat.
Forth Compiler/Interpreter in ROM
Forth is so small that it can easily fit into the ROM (Read Only Memory) of some common microcontrollers. Forth is so small and simple that the Forth compiler and interpreter can fit in a few kilobytes of memory. All you need to develop programs is some kind of input/output device and the microcontroller itself. You will see other languages in chips, like the PIC Basic Stamp, but these require another complete computer to compile their source code and download the executable image to the stamp.
Tethered Forth Development
If you do have another computer to attach to your microcontroller a popular method for development today is tethered development. The hardware is no different than a system where the application is compiled on a PC or workstation then downloaded and run on the microcontroller. There are two computers and a cable. Rather than just downloading and running software the two systems can be more tightly coupled, or more precisely the microcontroller can be tethered. It is not unlike using the PC as a terminal to the running microcontroller board, and single stepping through the code, but much more can easily be done in Forth. It often means that no ICE or in circuit emulators are needed in debugging.
A window on the PC can easily display the source code synchronized to the executing object code on the tethered microcontroller. The fact that changing the inner workings of the system is like changing LEGO blocks makes it easy to provide sophisticated debuggging capabilities whenever they are needed.
Forth Hardware
A decade ago people realized that they could build the Forth vitual machine in hardware and that it would be incredibly small, simple and fast. In 1986 I bought a "Forth kit" from Chuck Moore, the inventor of Forth, with the first Forth Engine the Novix NC4000. This was a hobbiest's experimental board with lots of interesting application notes. It came with an optimzing metacompiler that could compile itself from about 30 kilobytes of source code in a tiny fraction of a second. For many things the Novix chip was an order of magnitude faster than the fastest Intel processor of its time yet was implemented in only 4000 gates. It was a remarkable little machine.
Most machines were using microcode so their machine language instructions would each require a number of clock cycles. Since the machine CODE words in Forth, that operate on the Forth virtual machine, are made of multiple machine language instructions a Forth word might take 10 instructions of 10 cycles each or 100 machine cycles. On a typcial old 1 Mhz microcoded microprocessor this would give you 10,000 Forth instructions per second.
The Novix chip was a little like a superscaler Forth engine in that it could execute up to five Forth instructions in a single clock cycle. The burst rate was 40,000,000 Forth instructions per second on an 8 Mhz chip. It could read a 16 bit I/O port, mask out a subset of bits, compare to another mask, and branch on the result of the operation all in a single clock cycle. The only Novix chip made had a bug in the interupt circuit and they never produced a correct chip. There was a PAL that one could use to make the Novix interupt more usable. Harris Semiconductor bought the design and produced the RTX-2000 series of Forth chips.
Last year I got a credit card sized board with Chuck's latest chip, MuP21. This chip will produced a video signal directly from the chip, a sort of built in video card, and will execute up to 100,000,000 Forth instructions per second. It is also potentially 100 times less expensive to mass produce than the old Novix chip.
I have a special interest in Forth. I decided to get a custom version of a Forth machine in silicon from Chuck Moore. I think of it as a sort of "toy" super computer. It will be as fast a second generation super computer, but as cheap and low power as the microcontrollers used in appliances today. It is designed to be a modular component of a high performance robot brain. It will provide several processors on a single chip for real-time processing of audio, video and digital I/O. This will make it possible for even small and inexpensive robots to have speech, speech recognition, machine vision, and to present a GUI to a user or an on-screen inteligent agent. It is designed to allow processors to snap together like LEGO blocks. I want robots that not only are inexpensive but also can see, hear, speak, and think well enough to be able to solve problems of sufficient complexity to make them useful. These are the kind of things that most people would expect would at least demand an expensive PC.
This paper was written on 1/19/95 and spruced up with some graphics and HTML links on 11/24/00. There was a nice article in Wired about LEGO Lego: A Hacker's Best Friend. With the popularity of the LEGO Mindstorm Robots and with Ralph Hemple's Forth system for these LEGO robots on the scene the old Forth and LEGO article seemed like an interesting historic artifact.
Jeff Fox