I've written a pretty simple FORTH environment for the Arduino platform. So far, I've tested it only on a ATMega168 Arduino, and simple things, like the LED blinker example, works fine. I'm releasing the source code under GPL2 and putting it on my Github account here: http://github.com/lpereira/finf
; so please fork it and help me improve it, specially if you're able to cut down the memory usage
Called FINF (FINF Is Not Forth), it weighs at the moment of writing, 6.5kB of program memory, and was written using the standard Arduino IDE. It is different from amforth however because words are not saved to the program memory; they stay in RAM and a simple virtual machine takes care of executing it.
As for the LED blinker example, try pasting this into Arduino's Serial Monitor:1 13 pinmode
: led 13 pinwrite;
: on 1 led;
: off 0 led;
: w 100 delay;
: blink 0 begin on w off w 1 + dup 10 = negate until;
This will blink the LED on pin 13 ten times, in 100ms intervals. What this does is, line by line:
1. Set pin 13 to digital output
2. Create a new word "led" that writes the value on stack on pin 13
3. Create a new word "on" that pushes "1" on the stack and call the word "led"
4. Does the same, except it pushes "0" on the stack
5. Creates a new word "w" that delays program execution by 100ms
6. Creates a new word "blink" that calls "on", "off", and "w", 10 times
7. Call the word "blink" -- the LED on your Arduino should be blinking now
It may look weird, but FORTH is pretty fun actually. You have some pre-defined words (type "words " on the Monitor to see what you've defined and what comes with FINF; type "dis " to see the disassembly of user-defined words) and end up creating your program by defining new words and gluing them together.
There are a few things missing from FINF, however:
- Ability to save/load things from a non-volatile memory
- Testing (specially conditionals and loops)
- Arrays bound checks, etc
- Did I mention stability?
Well, anyway. Give it a try and shout here.