Flashforth: Using the Three Values of Forth

3 minute read

Where I demonstrate the three values of Forth; speed, extensibility and interactive, to develop a better understanding of the ATmega328P.

Sources

Introduction

In describing Forth to others, I typically use what I call the “Three Values of Forth”. They are speed, both in execution and development, extensibility, the capability to easily add to the language and interactive, the ability to easily interact with Forth using the serial terminal. It is these three values which make Forth, a great language to use for programming microcontroller boards.

Video

This 7 min video will briefly demonstrate the three values. I recommend you view the video along with reading this blog entry to gain the greatest understanding.

A couple of errors in the video…

  1. Near the end, while reviewing the code, I mention line 25, it is actually line 245.
  2. The frequency for 24 CTC is 40kHz, not 40Hz.

Speed

The speed of Forth can be described in two dimensions, speed of development and speed of execution. The former lies in the ability to combine all three of Forth’s values to easily and quickly develop code. I’ll speak more on this aspect later in this entry. The latter can be easily seen by reading Comparing Board and Language Speeds. In the entry, Forth execution speed ranges from .5X compiled C to 5X compiled C. I typically make the assumption that Forth will run about the same speed as C.

Execution speed will be highly dependent on the application and board/controller. No matter the language, if execution speed is critical than assembly language becomes a standard response. Forth also allows inline assembly language, which reduces the effort required to develop the fastest code.

Extensibility

Both C and Python have the ability to add libraries of routines to extend the existing capabilities of the language. This isn’t as convenient nor as powerful as extending the language itself. Forth has extensibility, which is the ability to add native commands (words in Forth nomenclature) at no additional cost to speed or convenience.

Extensibility provides the ability to write a specific language for a specific project. This language can be a phenomenal user interface. For example, when developing Forth code to vary the intensity of a blue led, I created the words blue, off, dim, med, and max. Which allowed me to write off blue or dim blue, which is far more understandable than analogWrite(blue, dim) in C. [Note: The CTC word in the video became the word, speaker, as the word was used to change the frequency to a ultrasonic speaker(transducer).]

Interactive

The video is the best example of Forth’s ability to be interactive. C is prohibitively difficult to create immediate interactive programs, I won’t even spend time on it. While the REPL of python (CircuitPython or MicroPython) is handy, it pales in comparison to Forth.

Forth makes it easy because of its extensive use of the stack. In Python, one would have to print a request, read the request, store the request in a variable then call the routine to set the frequency. Some of that can be optimized, however, many times, it is more development time than it is worth.

Forth expects the number to be on the stack, so you simply put a number on the stack then “call the word”, which is even easier as you simply type the word to call it. Which means you end up with something as simple as 31 CTC to check the results of OCR1A being 31.

Comments powered by Talkyard.