I dug out an old (if not quite retro) system that has a Z80 at its core - the venerable Texas Instruments TI-83 calculator that my younger son was required to use for high school math classes. I fed it 4 new AAA batteries and a new CR1616 and it sprung to life after 20+ years in the drawer. Some teachers back then surely challenged their students to make this programmable calculator get PI to converge to some degree of precision, but all the programs I've seen online for the TI only calculate PI to 9 decimal places (given that TI BASIC's floating point precision is 10 significant figures). I had already coded two arbitrary precision PI programs in C based on the prior works of Paul Schaper and Dik Winter, and one of these had already been translated into Microsoft BASIC by Tom Lake, and implemented in some form or another on an emulated 8080 (AltairDuino), Z180 (SC131), ESP8266 (MakerPhone), and assorted Windows and Linux systems. I documented the SC131 project here:
https://groups.google.com/g/retro-comp/c/3LKv-TYwfZ0 .
Finding myself with a LOT of time on my hands due to the ongoing pandemic, I wanted to learn about this old TI-83. I first translated the second C program to MBASIC, then proceeded to translate them both to TI-BASIC 83 (which I discovered is VERY different from MBASIC) and hand-enter them on the TI-83 (I've since acquired a TI SilverLink USB GraphLink cable to simplify this). These two programs are based on different "spigot" algorithms, and they have different characteristics:
PI - Based on a FOCAL program by Paul Schaper written for the SOL20 using an
algorithm of unknown origin:
A more complex algorithm that is slower (than PIOL) but quite frugal with its
memory usage, able to compute up to 983 decimal places in TI-BASIC 83 (but it
will take ~36 hours, 52 minutes :)
PIOL - Based on a One-Line C program by Dik Winter using an algorithm attributed to
Rabinowitz & Wagon:
A simpler algorithm that is faster (than PI) but uses more memory, able to compute
up to only 28 decimal places in TI-BASIC 83.
The significant memory limitation in TI-BASIC 83 is not the total amount of memory on the calculator, which hovers around 26.8 KiB, but rather the maximum size of an array (List): 999 elements (each element using 9 bytes). Both algorithms utilize arrays with sizes proportional to the number of decimal places to be computed, thus the limits noted above.
The Z80, even at 6 MHz (three times the speed of the Altair), is no speed demon, and encumbered by an interpreted BASIC language, none of these programs computes PI very quickly. Here is a rundown of times to compute PI to 40 decimal places:
180 seconds - PI.BAS on an AltairDuino running MBASIC under CP/M
160 seconds - PIOL.BAS on an AltairDuino running MBASIC under CP/M
300 seconds - PI on the TI-83
85 seconds - PIOL on the TI-83
The big surprise is the 4th one - for some reason PIOL on the TI-83 runs (and correctly computes) much faster than might be expected looking at the other three timings. I have no explanation for this, nor for PI running quite a bit slower on the TI-83 than expected.
At the high end of things, PIOL correctly computes 283 decimal places (its max) in 52 minutes, 10 seconds, while PI requires nearly 37 hours to correctly compute 983 decimal places (its max). I didn't actually verify the latter on the TI-83, but rather on a TI-83 emulator (Vti by Rusty Wagner) at 60x real-time.
I've made a zip archive containing both MBASIC programs and both TI-BASIC 83 programs; you can download it from here:
TI-83 PI BASIC AND C.zip . For the TI BASIC programs, I've included both the ASCII source (.txt) files and the tokenized TI-83 program files (.83p). The source files can be used to hand-enter the programs or compile to .83p or other formats (e.g.: .8xp), while the latter can be directly sent to a TI-83 via a GraphLink cable or dropped onto the TI-83 emulator. The archive also contains the C program versions and Ion 1.6 (see next post). None of these tokenized program files (except for Ion 1.6) may be directly run on any later TI calculator (TI-83plus onward) for several reasons I'll get into later; they're strictly for the TI-83 (no "plus").
The TI-BASIC 83 programs both wait when finished for the ENTER key before clearing the screen and terminating; and they both can be interrupted by pressing the ON button, just like any other TI-BASIC 83 program.
IMPORTANT NOTE ABOUT PIOL SOURCE FILES: You will find 3 instances where subroutines (programs) PIOL2000, PIOL3000, and PIOL4000 are called; you will find a backslash character (\) before the 2, 3, and 4 (respectively) in these calls. You should ignore those \ characters if hand-entering these programs. If compiling these source files to tokenized TI-83 program files using a tool or IDE other than TokenIDE 0.11 by Merthsoft, you should probably delete these \ characters; they exist only because a bug in TokenIDE causes the L followed by a number 1-6 to be misinterpreted as an L1-L6 token, even in this context where a list would be inappropriate. The \ tells TokenIDE to take the following character literally, without interpretation.
The next obvious thing to do is compile the C programs to Z80 machine code and run them on the TI-83 (much faster). See the next post in this topic.