CamelForth for the RC2014 leads to π
This Sunday morning was a bit cold here, so I spent it porting Brad Rodriguez' Z80 CamelForth to the RC2014. The RC2014 is a Z80 homebrew kit computer. It has a Z80, 32K of RAM, and 8K ROM. Using jumpers, you can select between two ROM images: MS BASIC or a Z80 monitor called SCM.

My CamelForth port is here, with a Makefile for z88dk. It includes the pre-built hex file, which is all you need to run it on on your very own RC2014.
On reset, SCM writes its banner to the serial console:
Small Computer Monitor - RC2014 *
An easy hack for loading code into the RC2014 is to paste the hex file from another window. There's no visual confirmation for about a second, and then the monitor writes:
* Ready
Did it really load? It's easy to check by inspecting memory (things I'm typing here are in green):
*m 9000 9000: 21 00 FC 25 F9 24 E5 DD E1 25 25 E5 FD E1 11 01 !..%.$...%%..... 9010: 00 C3 8A A5 00 00 00 04 45 58 49 54 DD 5E 00 DD ........EXIT.^.. 9020: 23 DD 56 00 DD 23 EB 5E 23 56 23 EB E9 17 90 00 #.V..#.^#V#..... 9030: 03 4C 49 54 C5 1A 4F 13 1A 47 13 EB 5E 23 56 23 .LIT..O..G..^#V#
Looks like code. Let's run it by jumping to address 9000:
*G 9000 Z80 CamelForth v1.01 25 Jan 1995
Score! OK, let's get to the pi.
Like almost all of the early Forths, CamelForth is 16-bit, so the largest integer it can handle is 32767. But Forth has quite extensive double-precision math, called DOUBLE in the standard. You can do quite fancy arithmetic using a few well thought-out building blocks.
Here's how it works. First let's make one hundred million. The M* word multiples numbers and produces a double result. D. prints it:
10000 10000 M* D. 100000000
Huge. Here's the good bit. The word M*/ multiples a double by a fraction. So to check that 2/3 of a hundred million is 66 million:
ok 10000 10000 M* 2 3 M*/ D. 66666666
Now this page lists some fractional approximations to pi. The most famous is 355/113. Tasty.
ok 10000 10000 M* 355 113 M*/ D. 314159292
That's pretty good - 7 significant figure accuracy from one easy to remember fraction. You can bake it into a Forth word PI* like so:
ok : PI* 355 113 M*/ ; ok 10000 10000 M* PI* D. 314159292 ok
What's not immediately obvious is that M*/ is actually doing 48-bit arithmetic so it can avoid overflows. I still find it amazing that a language running on an ancient Z80 gets you to this point in just 5K.
In the fantasy world where I have lots of time to do things that don't earn any money, I'd like to get this Forth into a slightly more polished state then distribute it as an optional 8K ROM image. The RC2014 has 6 vacant slots, so CamelForth would make a nice addition. If anyone in the RC2014 community - specifically someone who has an EPROM programmer - is interested in working with me on this, please hit reply and let me know.
Thanks for reading.