Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Computing PI in BASIC and C on a quite different Z80 system ca. 1996

280 views
Skip to first unread message

Frank P.

unread,
Mar 27, 2021, 12:13:02 PM3/27/21
to retro-comp

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.

Frank P.

unread,
Mar 27, 2021, 12:15:25 PM3/27/21
to retro-comp
When I found out that Z88DK could compile for the TI-83, I tried compiling (unchanged) the same two C programs (piz88.c and pi-olz88.c) I had compiled for the SC131 to run under CP/M. Surprisingly, I only got one error implying that malloc() wasn't going to work on the TI-83. I modified the programs to statically size the arrays to be large enough to compute a reasonable number of decimal places, yet take up only enough RAM on the TI-83 so that both programs (and their BASIC equivalents) could be stored in RAM at the same time. I also removed the CP/M BIOS call to test for an interrupt. I ended up with PI being able to compute up to 2683 decimal places and PI-OL being able to compute up to 567 decimal places (remember that PI is much more frugal with memory, but runs slower and does not print any digits until the algorithm is complete).

Remarkably, the programs then ran just fine (using the TI-83 Shell program "Ion" to launch them), but neither the input nor the output was optimized for the TI-83's small display. I proceeded to do fix these things, but soon realized that the C standard I/O library functions (e.g.: gets(), putchar(), printf(), etc.) had two problems:
    1. Any output function that attempted to print in the last column of the last row would skip that column and display
       that character in column 1 of the last row after scrolling the rest up.
    2. Any input function that blocked waiting for a key would allow the auto power-down feature of the TI-83 (or the manual
       power-down initiated by pressing "2nd" followd by "ON") to power down the processor. At this point, pressing ON to
       power-up the processor again would always leave the program in various locked-up states.
So I used direct ROM calls to do all (non-blocking) input and any output where printing to the last column of the last row is possible. I also added a busy-wait loop at the end of the program so that the screen could be observed in its final state until "ENTER" is pressed; and also added the ability to interrupt the computation by pressing the "MODE/QUIT" key (the same key that Ion accepts to terminate itself).

Not surprisingly at all, the compiled C programs ran MUCH faster on the TI-83 than their BASIC counterparts. Here are some typical timings for computing PI to 40 decimal places:
   300 seconds - PI in TI-BASIC 83
    85 seconds - PIOL in TI-BASIC 83
   4.5 seconds - PI in C
     3 seconds = PIOL in C

At their upper limits, PI takes ~4.6 hours to correctly compute 2683 decimal places, and PI-OL takes 455 seconds to correctly compute 567 decimal places.

The zip archive from the previous post contains (in a subdirectory for each program) the C source code, the tokenized TI-83 program file, and the TI-83 shell program Ion 1.6 necessary to run them (with instructions for installing it.) Since these are all machine-code programs, they cannot be entered by hand into the TI-83, but require a GraphLink cable and TI Connect software on your PC in order to upload to a real TI-83. But you can easily upload them to an emulated TI-83 running on the Vti emulator via a simple drag-and-drop. In the next post, I'll detail the tools I used for all of this (including Vti, TI Connect, Z88DK, and TokenIDE) and where you can get them.

Frank P.

unread,
Mar 27, 2021, 12:20:23 PM3/27/21
to retro-comp
Tools and documents

1. First and foremost (since most people don't have a TI-83 just lying around) is the TI calculator emulator known as "Vti". You can download it here: https://www.ticalc.org/archives/files/fileinfo/84/8442.html . It does not come with ROM images; you can download ROM images here: https://www.theoldcomputer.com/roms/index.php?folder=Texas-Instruments/TI-83 . I'm using TI-83 version 1.02 though my son's calculator is 1.07; it doesn't seem to matter which version as long as it's not for the TI-83 Plus.

2. If you plan on trying the C versions of PI/PIOL on the emulator or a real TI-83, you'll need the Ion shell (included) which you can download here: https://www.ticalc.org/archives/files/fileinfo/130/13058.html . Follow the instructions in the .txt file for the TI-83. To exit the Ion shell, press the MODE/QUIT key.

3. If you want to upload any of the program files to a real TI-83, you'll need a GraphLink cable (for example, https://www.amazon.com/gp/product/B00006BXBS ) and the TI Connect software you can download here: https://education.ti.com/en/software/details/en/B59F6C83468C4574ABFEE93D2BC3F807/swticonnectsoftware . It's available for Windows and MacOS, but not Linux (for Linux there is a different package called TILP-II, or you can run TI Connect on a virtual Windows machine). The Windows version works on Windows 10 but is problematic on a 64-bit system (locks up), so I run mine on a virtualbox Windows XP machine.

4. If you want to convert a TI-BASIC 83 source file (.txt) to a tokenized program file (.83p), you'll need a tokenizer like TokenIDE 0.11 by Merthsoft. That one is a little buggy (which I'll explain later), but usable and can be downloaded here: https://www.ticalc.org/archives/files/fileinfo/433/43315.html .

5. If you want to compile the C source to a tokenized program file (.83p), many of you are familiar with Z88DK which you can download from here: https://z88dk.org/site/ . Use the command line "zcc +Ti83 -startup=1 -create-app -o <prog-name>.83p <prog-name>.c". You can obviously also use this to write and compile your own C programs for various TI calculators.

6. The TI-88 GUIDEBOOK (the manual) can be downloaded here: http://sites.science.oregonstate.edu/math/home/programs/undergrad/TI_Manuals/ti83Guidebook.pdf

7. There is precious little documentation on the TI-83 ROM routines, but there is a quite comprehensive such document for the TI-83 Plus which can be downloaded here: https://archive.org/details/83psdk/83psysroutines . In general you can find what you want in there and then extrapolate the routine name a little bit to find it in the ti83asm.inc file.

8. The general format of an .83p file is documented here: https://www.ticalc.org/pub/text/calcinfo/83pformat.txt

Frank P.

unread,
Mar 27, 2021, 12:22:51 PM3/27/21
to retro-comp
Final ruminations

This month-long project has been quite a learning experience. I didn't really need yet another machine to compute PI, I just wanted to see if the TI could do it. Here are a few last tidbits that should be documented lest they be forgotten.

The TI-83 is the product that got Texas Instruments calculators permanently ingrained into the math curricula at high schools and universities across the country. This was followed by the TI-83 Plus and all the successive generations (I guess we're up to TI-89 now). The TI-83 and the TI-83 Plus are incompatible in two important ways with respect to these PI programs:
    1. The encoding of tokens, and thus the format of the .83p vs. .8xp program files, is different.
    2. The ROM routines, while largely the same in functionality, are called in different ways:
        The TI-83 calls ROM routines directly using the call instruction to a ROM address.
        The TI-83 Plus calls ROM routines by using the rst 28h instruction followed by a 16-bit code (ROM address).
        For a given routine, the addresses are not the same on the TI-83 and TI-83 Plus.
Or course there are many other differences For example, the TI-83 Plus has flash memory; And it has new Asm(, AsmComp(, and AsmPrgm tokens that would allow the compiled C programs to be executed without needing a shell.

The TI-BASIC 83 programs can be upgraded for the TI-83 Plus (and possibly later versions) by generating tokenized program files in the .8xp format (TokenIDE can do that). The Z88DK C programs would need a command-line change to target the appropriate calculator, a change to the include file that enumerates the ROM routines, and a change to the assembly code functions to use the TI-83 Plus method for calling ROM routines. This is left as an "exercise for the student" :)

The TokenIDE program has a couple of annoying bugs:
    1. Any TI-BASIC 83 subroutine call containing an L followed by a number 1-6 must have a \ before the L or the number
        to prevent TokenIDE from tokenizing this as one of the 6 pre-defined list variables.

    2. In the .83p (I can't speak for the .8xp format) output file generated there are 3 errors that must be fixed manually
        if the file is to be uploaded to a real TI-83 (but need not be fixed to upload to the Vti emulator, as it is
        not as fussy):
        a. The little-endian 16-bit integer at $35-$36 must be reduced by 2
        b. Byte at $37 must be changed from $0D to $0B
        c. The little-endian checksum in last 2 bytes must be reduced by 2
       If you read https://www.ticalc.org/pub/text/calcinfo/83pformat.txt , you'll see how TokenIDE managed to screw this up.
Message has been deleted

Frank P.

unread,
Mar 27, 2021, 5:39:51 PM3/27/21
to retro-comp
Meant to include this pic in the first post but I forgot, and Google has deleted the ability to edit the post, sigh :(
TI-83small.jpg...

Phillip Stevens

unread,
Mar 28, 2021, 3:40:45 AM3/28/21
to retro-comp
Frank wrote:
Meant to include this pic in the first post but I forgot, and Google has deleted the ability to edit the post, sigh :(

And the other BS they’ve instituted is no posting from phone. Truly retrogressive measure for a forum.

Trying your exercise for students sounds interesting. I have a newer but similar TI calculator in the family.

P.
--
Sent from a Mobile Device. Replies may appear terse.

Frank P.

unread,
Mar 28, 2021, 6:48:43 AM3/28/21
to retro-comp
The TI-83 Plus through TI-86 still used the Z80. After that (e.g.: TI-89) they used the Motorola 6800.

Frank P.

unread,
Mar 28, 2021, 10:50:43 AM3/28/21
to retro-comp
Whoops, I meant 68000.

Frank P.

unread,
Apr 15, 2025, 6:40:01 PMApr 15
to retro-comp
I'm downsizing and rather than toss this fully-functional TI-83 and its companion TI SilverLink USB GraphLink cable into the electronics waste disposal pile, I'm offering this piece of computing history for free if you can pick it up in Lower Bucks County, Pennsylvania (just NE of Philadelphia). It still has both BASIC PI programs and both compiled C PI programs in its memory (just press PROG - the first program is a shell that invokes either of the compiled programs - the BASIC programs are down lower). Or you may just want a TI-83 to show your kids that all the cool kids took a Z80 to school in the 80s (where else can you find a calculator that rerquires you to adjust the display contrast as the batteries wear down). LMK if you can pick up (please reply to me directly).
Message has been deleted

Frank P.

unread,
Apr 22, 2025, 9:32:28 AMApr 22
to retro-comp
Claimed
Reply all
Reply to author
Forward
0 new messages