July 01, 2019

Writing software for a modern Forth cpu


In contrast to a common misconception, Forth is not a dead language but the community is strong and healthy. A typical sign is, that lots of new Forth related CPUs for Forth systems were developed in the last decade for example the J1 Forth CPU and the H2 Forth CPU. The VHDL files are available for free download and many expert users have tested out the design on their own FPGA board.
Forth has a small bottleneck which is the hardware side. The easy part is to get a self developed Forth CPU up and running, which means that the ok prompt is visible on the screen. Even the development of a new Forth system plus a new Forth system from scratch is a task which can be handled by single programmer. But what will happen after the ok prompt is blinking? If the situation is a bit different and the newly developed system is Linux compatible the next logical step is install some fancy software with entering apt-get install xwindow, latex, vim but under Forth no “apt-get install” command is available. What is the Forth way to overcome the software issue?
Forth is working a bit different from the Linux community. Forth doesn't has precompiled binary software and it lacks in providing written sourcecode. The only thing what the Forth community can provide are some handbooks which are describing the inner working of the stack and some demo programs for calculating fibonacci numbers. It's up to the programmer to program his own application and his own operating system with that knowledge.
Suppose the idea is to a use Forth system as a terminal, as a database, as a gaming console and for surfing in the internet, what is the best practice method in developing new software? The good news is that no additional compiler, linker nor assembly program is needed, because everything is built in into the forth system. That means, a software like GCC or a linker is useless. What is needed to create larger programs is a library similar to the standard-c library. The first thing before a pong game or even a 3d game can be programmed is to invent a library which provides the basic features for example, input of textstrings, putting pixels to the screen and so on.
A good introduction into the topic is the existing C standard library, but also some assembly library for MS-DOS helps to get the general idea. A typical library contains of 1000 and more subfunctions and all these features can be realized in Forth as well. The good news is, that after such a library is available the programming task will become more simpler. Programing in C, C++ or Java is easy, because lots of preinstalled libraries are available. Let me give an example:
Suppose the idea is to draw a sprite to the screen, and move the sprite from left to right. Without a graphics library this task is very complicated. But if a standard library is available the overall program is trivial. All what the programmer has to do is to identify the correct command for activiting the VGA mode, then he is using a different command for drawing the sprite, and then he is moving the object with a for loop. The resulting sourcecode in Forth looks very similar to what Python programmers typing in if they have access to the pygame library. That means, the overall mini game can be realized in less then 100 lines of code.
The only problem is that the programmer has to realize the library first. If he has no high level word for activating the graphics screen, he has to program the feature first.