MinForth 


a MINimalistic*) but complete FORTH system
in C and Forth94
for DOS, Windows and Linux operating systems
mailto:minforth@arcor.de


Last update: 12-05-2009 (minor corrections)

Warning # 1 :-)
The language used in this document and in the source code is Genglish (German-flavoured English).

Warning # 2:
MinForth is rather stable but like any software it may have bugs. Therefore use MinForth on your own risk.

1.     Introduction

Have a look at MinForth running in an Ubuntu Linux terminal window:

MinForth is greet you shamelessly with claiming to be a minimalistic but complete Forth system. What does it mean:

Forth: if you don't know about Forth, please go there.

Minimalistic *): MinForth consists of a very simple virtual machine and only few low-level words, written in simple C. All the rest is built in high-level Forth.

Complete: practicallly all words defined by the ANS/ISO Forth94 standard are provided. Thus for hardcore minimalists MinForth is a fat system. :-) 

*)  Minimalistic means: small extensible kernel, small memory footprint, reasonably few C-coded words, practically only standard words are provided. You may perhaps miss: multimedia, communications, graphic user interface, object-orientation, multi-tasking, manuals .... ;-)


Features

Portability: MinForth runs on PCs. The sources can be compiled without modification for DOS, Windows or Linux operating systems. Images can run on any operating system.

Extensible: It is very easy to add new low-level routines in C, e.g. for accessing platform-specific hardware or for direct OS commands

Crash-proof: Robustness goes before speed. All low-level words include maximum runtime checking. Dangerous conditions generate a high-level exception.

Flat memory: MinForth uses a flat memory space with 32-bit-wide addresses. Word headers are separated from code.

Floating-point math: MinForth uses 64-bit IEEE floating-point numbers.

ANSI terminal: Support for ANSI terminals is provided including colour and command-line history functions.

Debugger: A simple classic single-stepping debugger is integrated in the virtual machine.

Goodies: Private headerless word definitions, easy-to-use locals, vocabularies (like in "good old" F83), etc.

2. Downloads
Free downloads of MinForth software:

In fact there is only one common set of source files, regardless of the target operating system. The different packages offered below differ only in their precompiled executable files, the source files are 100% identical, in C and in Forth.

Please select your favourite package:

mfwinzip  -  zipped package for Windows XP/2000 systems

mflinux.tar.gz  -  gzipped tar-file for Linux systems

mfdos.zip  -  zipped package for 16-bit DOS (and Windows) systems

Note: To switch ANSI escape sequences on (off), just give the command: TERMINAL ON (OFF).

Free downloads of MinForth documentation:

mfintro.pdf  -  pdf-file with MinForth documentation

Free downloads of standard Forth documentation:

ansforth.pdf  -  pdf-file with standard Forth documentation

dpANS.zip  -  zipped html-files with standard Forth documentation

(Note: for copyright reasons these files are only 'old' working documents i.e. so-called draft proposals)

3. Making MinForth

The steps are simple
1. compile the C sources if you don't want the downloaded executables
2. start metacomp to build the kernel system
3. start the extend "script" to build the MinForth system (you will be asked once if you have ANSI terminal capability)

That's all (if everything went well).

  Compiling MinForth for 16-bit "old" DOS

You can compile MinForth with the Turbo C 2.0 compiler. Turbo C is available in the web for free. The following link once worked for me:
   community.borland.com/article/images/20841/tc201.zip

   Please contact Borland directly for any license questions.

Download the MinForth package and extract it into a separate directory, e.g. C:\MinForth. Make sure that you have loaded ANSI.SYS in your config.sys. The simplest way to compile the programs is by using the batch file tcmake.bat. Edit tcmake.bat and adapt the TCPATH variable to your configuration. Then use the following commands:
   C:\MinForth> tcmake metacomp (make metacomp.exe)
   C:\MinForth  metacomp        (make kernel.i)
   C:\MinForth> tcmake decomp   (make decomp.exe)
   C:\MinForth> decomp          (make kernel.d)
   C:\MinForth> tcmake mf       (make mf.exe)
   C:\MinForth> extend          (make mf.i)
   C:\MinForth> mf              (start new MinForth)

If you prefer to use the Turbo-IDE instead of tcmake please don't forget to select the "huge" compiler model option.

  Compiling MinForth for 32-bit DOS

You can compile MinForth with the djgpp compiler which is an adaptation of the gcc to work with DOS. Gcc is also the compiler that is used for Linux system programming. You can get djgpp from its homepage:
   www.delorie.com/djgpp/

Programs made with djgpp require a DPMI memory extender to run and at least a 386 processor. The advantage is that you can address nearly all of your RAM as one flat memory space. The easiest way is to use cwsdpmi that comes along with djgpp. You could either include cwsdpmi -p in your autoexec.bat file or start cwsdpmi (without paramerters) before each program that had been made with djgpp. But if you use the DOS version of Windows 9x upwards you can run the program directly, because DPMI services are already there.

Making MinForth is as easy as with the Turbo C compiler above. Make sure that you have loaded ANSI.SYS in your config.sys. Edit djmake.bat and adapt the DJPATH variable to your configuration. Then do as shown above.

Of course you can also use RHIDE instead of djmake.

  Compiling MinForth for Windows
Getting a free C compiler

To compile MinForth for Windows you can get many good free C compilers from the web.*)

But perhaps the best is the MinGW compiler. MinGW stands for "Minimalist GNU for Windows", which explains that it is an adaptation of the gcc compiler to cooperate with Windows libraries. Gcc is also the same compiler that is used for Linux system programming. You can get MinGW via its homepage:
   www.mingw.org/.

*) f. ex. lcc-win32, bcc, PellesC, tinycc
Compiling MinForth

The compilation is as simple as explained for 16-bit DOS environments. Edit the batch file gcmake.bat / bcmake.bat / lmake.bat and adapt the variable GCPATH / LCPATH. Then run the batch file, e.g. like
   gcmake metacomp
   gcmake decomp
   metacomp
   gcmake mf
   extend.

Note: pure Windows NT/2000/XP consoles do not support ANSI.SYS, even when the config.nt file is set properly, i.e. if it contains the line device=%SystemRoot%\system32\ansi.sys. That is an old Windows bug. If you like to have colours and command line editing functions, then you must remake MinForth.

 

  CompilingMinForth for Linux

Making MinForth for Linux is as simple as for DOS or for Windows. Untar all files into a subdirectory of your home directory, say ~/minforth. If necessary you must set the correct file modes for the little "scripts" lxmake and extend, e.g. by chmod 755. Then just do as before:
   ./lxmake metacomp
   ./lxmake decomp
   ./metacomp
   ./lxmake mf
   ./extend.

The source files are identical for whatever operating system you use! The source file facility.mf that is included during extend is just responsible for correct terminal support. Terminal support programming for Linux or Unix can be a nightmare. Therefore the control sequences used in facility.mf correspond to a standard Linux text console only. If you use a different terminal you must probably adapt facility.mf.

By the way my favourite Linux tool is Midnight Commander. Its integrated editor is even clever enough to work with DOS style or Unix style text files. You don't need duconv to convert text files from CR-LF to LF line terminations.

4. Design

Simplicity and portability

On important simplification over traditional Forth systems is that MinForth has no assembler. Its language for creating low-level words and system functions is C, for some strong reasons:
- Processors and assemblers change so often that it is completely uneconomic to maintain assembler software sources.
- ANSI-C is the "lingua franca" in the world of system programming since long. It is relatively easy to port C programs from one machine to the next. It is difficult and often impossible to port assembler programs.
- Practically every manufacturer of programmable electronic equipment, e.g. controller boards, delivers C libraries for system programming of his products.
- Compiler makers are not stupid. They will probably have spent much more effort to understand the machine architecture than you will ever like to spend, and their code will probably be very efficient. Why shouldn't we stand on their shoulders? We'll test our compiled functions thoroughly later anyhow.

MinForth has few low-level words (primitives). Most words are defined in Forth. Once you have made your primitives work on your machine, the rest will function automatically.

The core program (mf or mf.exe) loads and interprets image files that contain 32-bit-wide execution tokens and data. One can exchange image files directly between different machines (they must use the same token numbers, of course). With 32-bit addresses one does also not have to fight with memory address calculations (C does this ugly job for us).

MinForth has a weak link to terminals. It uses ANSI-like character terminals, e.g. as provided by the ANSI.SYS device driver in DOS systems. That makes the porting job relatively easy. (One could think of using the wide-spread ncurses library though, but ...)


Stability

With MinForth robustness comes before speed (most other Forths favour speed). A MinForth program may crash or get stuck, but it should never plunge the machine into disaster. If you really need execution speed, create an extra low-level word in C or use a different Forth system or a faster hardware.

Therefore all MinForth primitives do extensive run-time parameter checking before they do anything. If an erroneous condition is detected an appropriate high-level exception is raised (by the Throw() function in mf.c).

If your program gets stuck or runs in an endless loop, typing CTRL-C will give control back to you.

MinForth does not even rely on the memory access violation monitoring offered by most C compilers for Pentium processors. It uses its own monitoring functions instead e.g. for stack over/underflow checks.

These stability features are realized with the signalling mechanisms available in C. This explains also the limitations: whenever an exception is caused by hardware or OS failures, you cannot blame MinForth for not being able to catch it.


Virtual Machine

The MinForth "motor" is reallly trivial (see the loop in the main() function of mf.c): it executes tokens that are just indices into a jump-table (a C function-pointer array) which contains addresses of low-level functions. E.g. token 16 leads to the execution of the pAT() function providing the runtime behavior for the Forth word @.

There is only one exception: if a high-level word starts with large token number > 256 (in its cfa) and if this number is a valid address, then this address is processed as its runtime behavior (of a word made with DOES>).

Forth with only low-level words were not very flexible without the important functions pNEST() and pUNNEST(). pNEST() cares for pushing the instruction pointer IP onto the return stack when a high-level word is called. pUNNEST() pops IP back and the execution continues where the calling level had been left. Thus the virtual machine processes high-evel words by continuous nesting and unnesting.

5. What the heck is Forth?

There are so many good sites in the web where you can learn about Forth. The authors explained it much better than I could ever do. For starters I recommend the Forth Programming Webring:
   t.webring.com/hub?ring=forth

And there is the standard Forth documentation (see above downloads): Print out the dpANS document and spend a rainy weekend with reading. If you are new to Forth you should start with Appendix C.

And you can also participate in the always active Usenet forum:
   comp.lang.forth.

If you are interested in trying out other free Forth systems or if you want a Forth for a different hardware platform, visit
   www.forth.org/compilers.html

6. Copyleft

MinForth is free software. MinForth comes without absolutely no direct or indirect guarantee or warranty and you use the software on your own risk. You can use MinForth according to the world-wide used GNU General Public License. You can obtain latest copies of the GPL license from the web-site:
   www.gnu.org

The original MinForth download packages also contain a text file COPYINGof the license.


(C) Andreas Kochenburger 2009