PicForth: A Forth Compiler for the PIC17CXX Series of Microcontrollers

Welcome, embedded systems programmer, and thank you for your interest in PicForth. PicForth, like any high level language development system, was designed as a productivity aide that abstracts the low level internal mechanics of the processor, allowing you to more efficiently express higher level algorithms and logic. Your programs become more readable, easier to change, and more portable. If you are already familiar with the Forth language, you will find PicForth familiar territory. If you are new to Forth, I am sure you will find PicForth a useful tool.

Introduction to Forth

Forth is fundamentally different than the Algol/Pascal/C family of languages with which most programmers are familiar. It was developed in the late sixties and early seventies by Charles Moore, in order to improve his productivity over assembly language. One of the best descriptions of Forth comes from the forward of the ANS Standards document:

Forth is a language for direct communication between human beings and machines. Using natural-language diction and machine-oriented syntax, Forth provides an economical, productive environment for interactive compilation and execution of programs....

The statement of Forth in terms of human diction and machine syntax captures succinctly what is Forth's greatest strength. In terms of machine syntax, Forth statements almost universally use Reverse Polish (postfix) notation, which extremely simplifies or obviates the parsing and lexical analysis steps in compilation. There are no formal data structures, and there is absolutely no type checking.

In terms of its "human diction," Forth does not limit the programmer in his choice of words in any way, and words that would seem ridiculously out of place in most other programming languages find a comfortable home in Forth, some standard and well used Forth words being: @, !, ' , (, ), (even the comma is a Forth word.)

PicForth is Mostly Forth

PicForth is not a true interactive Forth system as described by the ANS standard, but rather is a cross compiler system which that standard specifically declines to cover. Nonetheless, PicForth complies as much as can be reasonably expected to that document. PicForth differs in that it is not extensible (it does not include does>,) and it is not interactive. Nevertheless, PicForth shares all the advantages of code structure, code re-use, and compact memory footprint that characterizes all Forth systems.

Why Forth?

The PIC17XX microcontroller has an awkward instruction set, and a small, paged, internal ram resource; and lacks any transparent means of addressing external ram. Most of the code examples from the Microchip application notes do not make any attempt at developing a consistent parameter passing method between functions, or a scalable method for allocating temporary storage space for function and procedure variables.

The PIC17CXX series of processors doubles the onboard program memory from earlier models. Larger and more complex projects become possible, and with it the need for better program modularity and design methodology. The allocation of temporary storage space for variables, and a consistent parameter passing method can be implemented with a parameter/data stack. The notion of a stack machine is best embodied in the Forth language, which is that of a two stack machine where stack parameters are operated upon within words (forth procedures) and results are pushed back onto the stack. Since this design encourages the use of very small and highly reusable words, program memory use is extremely conservative. Forth also implements a second stack, the return stack, which is used to hold return address values, temporary variables, and loop parameters.

Forth has some inherent advantages over other languages that make it ideal for microcontrolled systems development:

Additionally, PicForth molds itself well to the machine architecture of the PIC series of microcontrollers: