June 16, 2018

Why Forth is used by homebrew computer enthusiasts


Forth seems to be a remarkable language, because it is so different from mainstream programming language. The main feature is its stack and the reverse polish notation. The reason why Forth is a great programming language can be understand if we focus on a special sub-discipline in computing: the homebrew computer scene. The experts there are creating his own CPUs and his own operating systems from scratch. That means, they are ignoring standard x86 CPUs from Intel, they are not interested in overclocking raspberry PI hardware and they are rejecting even Open source operating systems like Linux. The reason is, because “it was not invented here”, which is a term for understanding a system from scratch. For example, the Linux kernel consists of 100 MB sourcecode, written by others, not by the homebrew computer scene, so it is not ok to use the software on the own system.
Let us go a bit into details. Suppose, we have created out of 2000 transistors a simple computer in 4 bit style. What comes next? Right, we need a programming language and an operating system. The simplest way to program a programming language from scratch is called Reverse polish notation. The general idea is given by a calculator program which is able to parse and execute calculator statements: https://rosettacode.org/wiki/Parsing/RPN_calculator_algorithm#C.2B.2B
As an input, the system gets a string like “3 4 2 *” and and then it prints out the result. The reason, why the RPN notation is used is because the resulting sourcecode is very small. The examples from the rosetta-code challange are not longer then 100 lines of code. No matter which programming language was used (C++, Python or whetever) it is very easy to write a RPN parser. If we want not only parse calculator statements but complete computercode the situation is equal. Writing a parser for a RPN programming language can be done in less lines of code, then writing a normal parser which can parse expressions in the correct linear order.
The constraints under which the homebrew computer scene operates has to do with limitations. They need the smallest possible computer, and the smallest possible operating system. They are not interested in 64bit Intel CPU with billions of transistors, they are not interested in an operating system which with a size of 10 gb, but they want a tiny 4 bit cpu, made of simple transistors and an operating system which is smaller then 1 kb overall.
Does this make sense? Yes and no. On the one hand, RPN based programming languages, and parsers are small and powerful at the same time. On the other hand, it is not possible to use a homebrew computer for any useful purposes. For example, it is not possible to watch a youtube video on such a cpu, or using Forth for programming object-oriented sourcecode which has a huge amount of lines of code. That is the reason, why homebrew cpu and RPN notation is forgotten by the mainstream. It is something which was replaced by more advanced systems. For example, a modern CPU is faster, then a 4bit cpu, and Linux is more advanced then a self-made Forth OS in under 1 kb.
In theory it is possible to extend a Forth system into a much larger operating system. For example by adding new features like TCP/IP and a graphical user interface. In reality, such projects are not existing. That means, if somebody is interested in a powerful hardware-software combination, he is preferring the C programming language, together with a COTS CPU.
We can assume, that homebrew cpus and Forth will never die. Because if complexity of mainstream hardware/software will raise, on the same time there is a need for a low complexity system for educational purposes. That means, from a teaching perspective a 4bit homebrew cpu together with 1 kb Forth operating system is a good starting point to describe a computer from scratch.
Without any doubt, Forth is the simplest programming language to parse. That means a software which is able to execute a Forth program needs less discspace then an interpreter for C++. On the other hand, Forth is not the language of the future. That means, modern software will not be written in Forth, because it is not comfortable to use. A so called high-level programming language like C++ together with hundreds of library is very complicated to parse. For example, to get a system which is able to execute a hello World GTK+ program we need at least a computer with 4 GB of RAM and 10 GB of harddisk. The needed amount of manhour to program a C++ compiler together with the GTK+ libraries is huge. But, using C++ in daily life is a easier then to use Forth. That means, the software industry is investing the effort, to produce something which is more powerful then Forth. Not on a theoretical level, because any problem can be solved in Forth too, but from the community aspect, that real users need real computer software.
Let us describe the situation from the consumer perspective. Suppose, company A is delivering a Forth computer. The system has 5 kb of RAM, has an integrated Forth system which is errorfree and tested, and a light user manual which looks very similar to the handbook of the Jupiter ACE homecomputer. In contrast, company B is delivering a 64bit computer with lots of RAM, a preinstalled Fedora operating system and software which has lots of bugs and needs an update every day. Which product will be more interested for the home-user? Right, the second one. It has faster hardware, more software and is more advanced.
If Forth and 4bit microcontroller is so great, why did we see a software-crisis since the 1970s? Let us go back to that date. In principle, a 4bit cpu together with Forth is able to run any program. It is a working computer and it is possible to create error free software for it. In reality, such an attempt will fail. The mismatch between the demand of the enduser for advanced software and the inability of the software industry to deliver such a product is called software-crisis. That means, if a company can only deliver a 4bit microcontroller plus 3 kb of RAM which has to be programmed in Forth, this is a good example for a software-crisis. The enduser isn't interested in learning Forth, he want's to play games in high-resolution, watch videos and typing in text in a GUI. The software-crisis in the 1970s was solved with better hardware and software. That means, with cpu who have 32bit or even 64bit, and with software like a c-compiler, graphical user interface operating systems and object-oriented programming. Sure, we can ask if we need really a 64bit cpu and if we need really a mouse on a computer to scroll through the internet, but this would ignore the problem called software crisis. The argument is, that with a 4bit microcontroller and a RPN parser is everything great, but only the consumer is the problem, because he doesn't understand it.
Microsoft and Forth
Between Forth and Microsoft are many similarities. It may be a bit surprising, but most Forth enthusiasts are using MS-Windows as their main operating system to program their custom build Forth CPUs. To answer the reason why we must go back to the early day of Microsoft. The company was founded with the idea to bring homebrew computers to the masses. Early Apple II and the Altair computers are example for low-cost hardware which was extended with Microsoft software to a Personal computer. The motivation behind Microsoft in the 1970s and behind many homebrew CPU projects today is the same: to build a simple computer, write a compiler and ship it to the customer.
On the first hand, Microsoft software and the Forth programming language solves a lot problems. For example the problem of how to boot the computer. That means, the hardware is powered on, the user see a prompt and can enter a short BASIC program or a short Forth program. Every homebrew CPU is proving something. It proves, that it is possible to build a computer and run software on it. Usually, the demonstration that a new homebrew project was success is to run a hello world program on it. In most cases some prime number algorithm or a mini Tetris like game. If this demonstration is done, the work of the homebrew cpu enthusiast was done, he is out of the loop. He has build up the 8bit cpu from transistors, he has programmed the BIOS and provides also software for programming the device.
What today's homebrew cpu fans are doing is to replicate the work of Microsoft in the 1970's. The assumption is, that a homecomputer is not available, and that the task is to build such a system from scratch which needs a certain amount of knowledge in electronics and also a bit knowledge about compilers.
There is only one problem. Microsoft and other homebrew cpu projects are leaving out one important point. What comes after the project? The example with a homebrew Forth cpu and also with a computer who runs Microsoft Windows shows, that it is not enough to simply build and deliver a home computer. In theory, it is possible to use such a device for something useful, in reality the more demanding problem is to write additional software for it. That means, even the homebrew CPU project was a great success and the system works (including the Forth interpreter), it is not possible to use such a device as a database server, a LAMP server or a graphics workstation.
In an earlier paragraph I introduced the term software crisis. Homebrew CPUs and Microsoft have produces the software crisis. A software crisis is a situation in which the hardware works, and the computer is printing out the hello world prompt, but there is missing something else, what the user needs. A software crisis means not, that the computer is broken. No, the hardware works, and the Forth interpreter too. A software crisis means, that ontop of this minimal system no other software is available. And this is the reason why Linux is something different from Forth.
Linux is not about building a homebrew CPU or writing the BIOS system. Linux and especially the GNU movement is about creating high-level software, which implies end-user application like a word-processor, databases, programming languages compilers and so on. The precondition of a GNU system is, that the computer works. That means, the hardware must boot up, and some kind of simple BIOS must be there. Linux is not able to answer the problem of how to build a computer out of transistors, nor it answers the question of how to implement a BASIC on that system. Linux is about how to solve the software crisis, that means to provide high-level software.
Surprisingly, this task was never fulfilled by the homebrew computer scene. The answer of Microsoft to the customer was that he should buy for a word-processor or a webserver if he needs such software. And the answer of the Forth community is the same. That means, Microsoft or the Forth community doesn't provide high-level software for free to the customer. They are not solving the software crisis.
Microsoft and Forth are both technical motivated. The idea is to realize a working computer system. And this knowledge is used for a commercial model to write high-level software. The consequence is, that the current iforth distribution costs money and that the MS-Word version costs money. In contrast, the GNU movement is a non-technical ideology. The idea is, that the sourcecode should be free and in which language the sourcecode was written is not important. The Linux kernel was written in C, some highlevel programs like Google Chrome are written in C++ and the LaTeX system was written in another language. To be honest, the GNU movement has no favorite. Because the GNU movement isn't interested in technology. They want to solve the software crisis.
Let us make a concrete example. We are taking an Altair 8800 computer from the museum and booting the system until the BASIC prompt. From a technical point of view, the system works great. That means, the current flows through the wire, the BASIC prompt shows that everything is ready and if the user enters a small Hello world program he can see the result. So we have a wonderful computer on the desk and the Microsoft BASIC is working great. What's next? And exactly this is the problem. There is nothing the user can do with the machine. On the first hand, the machine itself is great. From a technical point of view, the system is error free. On the hardware and on the software level everything works fine. And this is the best example why the problem can be called “software crisis”. Because there is no software available. Only the microcode in the ALTAIR and only the BASIC interpreter is there. That means, the amount of software in total is around 2 kb, perhaps 4 kb.
Can we use the amount of 4kb for the software for doing something useful with the machine, for example typing in a LaTeX document? No, the 4kb BIOS system only provides an interface to the hardware, to use the computer for any purpose we must program the machine first. If we are not able to program it, if we have no preprogrammed software on magnetic tape, and if we have no money to buy such software in the store we have the software crisis life and in color. That means, a working Altair computer with minimalistic operating system is a solution for one problem and the precondition for the next problem.
Let us describe what the problem not is. The problem is not how to wire transistors together to a CPU. The problem is not to program a Forth interpreter as a BIOS, the problem is not to booting the machine. All of these problems were solved. The problem is, that we as a consumer need something more apart from a Forth, a Microsoft Basic and a working homebrew CPU.
Now we can describe better, what a possible answer to the software crisis is. A possible answer would be in case of the Altair computer a box full of magnet tapes which are labeled with “wordprocessor”, “database application”, “game 1”, “Mario game”, “Tetris game”, “webserver”, “Pascal compiler”, “spreadsheet application” and so forth. The similarity between the examples is, that these kind of products have nothing to do with computing itself, but with legal aspects. Software is usually copyright protected, and the software can't be created out of nothing. To create such software a company is needed and many man hours too.That means, the magnetic tape with the Tetris game has nothing to do with reverse polish notation, 8-bit assembly instructions or a graphics card, but it has to do with the question who is the author of the software, which software company has programmed the game, what is the price to buy such a software, what is the license, which features has the software, and so on.
A homebrew Forth CPU can be called a technical movement, and the GNU manifesto can be called a social movement. A Forth CPU has to do with stacks, adress spaces and memory consumption; while the GNU movement has to with the GPL license, making the world better and patents.