Adventures in FORTH

I have always wanted to run Forth on my Microtan. Well clearly not always… I don’t actually recall when or why I first decided it would be good, just that it always seemed a good idea. I do think FORTH is the perfect language for the Microtan, so much faster and more powerful than BASIC.

Having solved the hardware limitations of my system with my 64KRAM-R board I set about trying to get a working FORTH. As with all projects I started at Alan’s website. In discussions with him I discovered he only had a cassette version of TANFORTH, or the cassette or disk versions of TUG-FORTH. I really did not want to be playing with cassettes or audio files, so my only option was TUG-FORTH.

My first task was to learn FORTH. The definitive book to read to learn FORTH is ‘Starting FORTH’ by Lee Brodie. This is available as a free online edition and I found there was a kindle version. I also downloaded Gforth to start playing on my PC.

The Microtan FORTHs are all derived from the Fourth Interest Group’s figFORTH. The book above and Gforth are based on the much later ANS FORTH standard. The differences between this and figFORTH are remarkably few so it makes an acceptable learning environment, Just don’t get used to the modern file handling capabilities of ANS FORTH.

The disk version of TUGFORTH (called DFORTH) works OK has a few limitations in terms of its disk usage. It can only access side 0 of drive 0. Basic instructions for getting it going are available from Alan in a “getting started guide”. I set myself the task of making the breakout game provided with TANFORTH work under DFORTH. This required making the graphics extensions provided with TANFORTH work with DFORTH, and the challenge with that was to recreate the word CHUNKS, which is built into TANFORTH itself, to support use of the chunky graphics.

I discovered that the FORTH word ‘ returns the execution address of a word. So running TANFORTH and typing HEX ‘ CHUNKS . reveals the start address of the CHUNKS routine, which can then be disassembled using XBUG. It was at this point that I realised life would be so much easier if I was working with a terminal emulator. With the emulator I could capture the output of XBUG.

Next challenge was to get CHUNKS into DFORTH. One way to do this is to just take the hex code and create what is effectively a constant array, but this felt a very non portable way to do things. Breakout itself has a couple of routines done this way. DFORTH also has an assembler so I thought I would try that. The DFORTH assembler does not support the normal branch mnemonics like BEQ, instead you have to use words similar but not identical to the normal FORTH control structures. I got it working in the end, but it is not pretty. Also, as the DFORTH assembler seems to be unique, it is no more portable than just entering the machine code!

I then turned my attention to Breakout itself and once I had made and corrected several stupid mistakes all that was needed was to adjust the two machine code routines embedded in it. In FORTH every word ends with a JMP to one of a number of possible locations depending on what you want to happen to the stack. These are of course implementation dependent and so the JMP addresses embedded in the two routines needed to be changed.

The result of all this is a working version of Breakout for DFORTH, but perhaps more importantly the TANFORTH extensions. I’ve published all my code on this site as text files this should allow almost any code written to work with TANFORTH to work with DFORTH

At the end of these adventures I had somewhat unintentionally ended up with a pretty in depth knowledge of how FORTH works, and realised that the source code for the original figFORTH was widely available. How hard would it be to create a new port of figFORTH for the Microtan, overcoming the limitations of DFORTH and having a version with full source code so that it could be enhanced if required in the future? It turned out that figFORTH is incredibly well documented. The original port to 6502 is itself very well documented. The figFORTH model is available. Most of FORTH is written in FORTH and the model has documented source code. The real jewel is an excellent book called System Guide to figFORTH by DR C. H. Ting, Ph. D which explains how every forth word works.

So FORTH-R was born. In the spirit of the Microtan-R project a modern recreation of late 1970s technology. I have documented the complete toolchain used in the FORTH-R user manual, initial versions were tested on Geoff’s emulator, during this process I think I discovered that the 6502 emulation it uses does not have the JMP (xxFF) quirk that a real 6502 does. FORTH uses a lot of that type of JMP and so each time you assemble it has to be checked for routines that start at xxFF and a NOP added to shift everything down, which may of course then cause a problem further down the code!

Having moved from the emulator to real hardware I went about getting disk support working the way I wanted it to work, porting over the excellent Editor and Assembler from TANFORTH, and once again adjusted the assembly routines to make Breakout work.  I also found a number of documented bugs and published fixes for some on the unsigned and double-precision maths routines in the original figFORTH and incorporated these.

The final problem was how to distribute FORTH-R. With no cassette support, although the basic binary can be distributed as .wav not all the extra screens needed to make the complete package can. Having been doing a lot of the development by creating text files on my PC and then transferring them using a terminal emulator I decided to use this approach for distributing the rest of FORTH-R and so created a “script” which when sent via a terminal emulator creates all the required screens on a disk.

TANFORTH came with a Towers of Hanoi game that I did not have a copy of, so I adapted a version published on a Jupiter ace website to use the chunky graphics. FORTH_R behaved perfectly during the development of this, so although it has not been extensively tested I have decided to release it in case anybody else wants to play. FORTH-R can be found here, and Towers of Hanoi is here.