C Code to calculate PI on my Z80

344 views
Skip to first unread message

Peter Onion

unread,
Sep 8, 2025, 11:40:39 AMSep 8
to RC2014-Z80
Spigot400.png
We've been playing with a bit of code on some of  the museum machines that calculates PI with a very odd algorithm, so I thought I should try it on my Z80.  It only needs integer arithmetic, but it does need 32 bit integers so it's not particularly fast, 3:25 for 400 digits.
PeterO

S P Dixon

unread,
Sep 8, 2025, 1:06:02 PMSep 8
to rc201...@googlegroups.com

I love this stuff, do you have details of the algorithm? (I'm just really curious about how it works.)

Shiela


--
You received this message because you are subscribed to the Google Groups "RC2014-Z80" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rc2014-z80+...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/rc2014-z80/1037874b-b0fb-4881-8b2b-690bc01a70d6n%40googlegroups.com.
<Spigot400.png>

Peter Onion

unread,
Sep 8, 2025, 1:26:49 PMSep 8
to RC2014-Z80
Shiela,

We got the original "obfuscated C" code from this paper : https://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/spigot.pdf

We were talking to some visitors to TNMOC about this last Saturday.  It turned out one of them knows the person that wrote that paper !  They commented "That's typical of the things he does!".

I've attached a version for modern C (tested on gcc)  For Hi-Tech C on the Z80 just change uint32_t to long and remove the second #include line.

Have fun,

PeterO
spigot.c

Martin Giese

unread,
Sep 9, 2025, 8:43:30 AMSep 9
to RC2014-Z80
This seems to be the original reference for the Rabinowitz and Wagon “spigot algorithm”:


Martin

Peter Onion

unread,
Sep 9, 2025, 9:45:44 AMSep 9
to rc201...@googlegroups.com
On Tue, 2025-09-09 at 05:43 -0700, Martin Giese wrote:
> This seems to be the original reference for the Rabinowitz and Wagon “spigot algorithm”:
> https://www.jstor.org/stable/2975006
>
> PDF here: http://stanleyrabinowitz.com/bibliography/spigot.pdf
>
> Martin

Yes, I have that paper in my "Downloads" directory. I also have a version of the PASCAL
code from there translated to ALGOL-60 to run on the ELLIOTT 803, but it is rather slow !

PeterO

PauldB

unread,
Sep 9, 2025, 10:39:41 AMSep 9
to RC2014-Z80
I agree, this stuff is a lot of fun.

Some years back during Covid, Frank P. posted a conversation where he was "experimenting with computing PI on the SC131 using various programming languages and algorithms". There's some interesting stuff there.

Paul

Message has been deleted

David Lochlin

unread,
Sep 24, 2025, 9:10:53 AMSep 24
to RC2014-Z80
Hi Peter,
 like Sheila, I like these types of programs that calculate important numbers. I copied your code but couldn't get it to compile( I know zero about C) so ChatGPT came to the rescue and now it runs fine on my SC126.

Cheers Dave
2025-09-24_23-09-24.jpg

John Sandlin

unread,
Sep 25, 2025, 1:24:30 AMSep 25
to RC2014-Z80
What is different about the working version - and which C did you compile it with?
Message has been deleted

David Lochlin

unread,
Sep 25, 2025, 9:00:34 AMSep 25
to RC2014-Z80
I used HI-TECH-Z80 V3.09-17 built into the RomWBW's C files. Looking at the code there are quite a few differences(which mean nothing to me). Here's ChatGPT's version attached.

Dave
picalc.c

Peter Onion

unread,
Sep 25, 2025, 9:58:25 AMSep 25
to rc201...@googlegroups.com
On Thu, 2025-09-25 at 06:00 -0700, David Lochlin wrote:
> I used HI-TECH-Z80 V3.09-17 built into the RomWBW's C files. Looking at the code there
> are quite a few differences(which mean nothing to me). Here's ChatGPT's version
> attached.

My HiTech C version.

PeterO

-------------------------------------------------------------------------------------

#include <stdio.h>

long a[4088],b,c,d,e,f,g,h;
int n,digits,four;



void main(void)
{
c = 4088;
f = 10000;
h = 0;
digits = 0 ;
n = 10;


for(;b=c-=14;)
{
for(e=d%=f;g=--b*2;d/=g)
{
d =d*b+f*(h?a[b]:2000);
a[b] = d%--g;
}
h = 1;

digits += 4;
four = (int) (e+(d/f));

printf("%04d",four);
if(n-- == 1)
{
printf(" %d\n",digits);
n = 10;
}
}
printf("\nPI to %d digits\n",digits);
}

r4r...@gmail.com

unread,
Sep 25, 2025, 11:16:20 AMSep 25
to RC2014-Z80
Hi,  

How about posting a compiled for Z80 version of the program?  



.

John Sandlin

unread,
Sep 25, 2025, 6:05:49 PMSep 25
to RC2014-Z80
I am using Hitech-C too. I updated my code to expand the 1e4 to 10000 and changed the int32_t to long, and it works now. 

Thanks. 

David Lochlin

unread,
Sep 26, 2025, 1:32:51 AMSep 26
to RC2014-Z80
Pardon my ignorance, but what is a compiled Z80 version of the program? Is it the .COM file the compiler produces. I can do that.
Dave
Message has been deleted

Peter Onion

unread,
Sep 26, 2025, 3:08:25 AMSep 26
to rc201...@googlegroups.com
On Thu, 2025-09-25 at 15:05 -0700, John Sandlin wrote:
> I am using Hitech-C too. I updated my code to expand the 1e4 to 10000 and changed the
> int32_t to long, and it works now. 

John,

I back up all my CP/M files on my Pi and it seems I originally posted the gcc version I
had written for my Pi by mistake !

Sorry.

PeterO


Peter Onion

unread,
Sep 26, 2025, 3:17:00 AMSep 26
to rc201...@googlegroups.com
On Thu, 2025-09-25 at 08:16 -0700, r4r...@gmail.com wrote:
> Hi,  
>
> How about posting a compiled for Z80 version of the program?  
>

Why not take this opportunity to learn how to use the Hi-Tech C compiler (which is already
included in RomWBW). Then you'll be able to compile the code yourself and also modify it
if you want to. I'm sure we can help you with doing that.

If you don't already know C then learning it is not hard as it's quite a small language.
At least it used to be small back in the 1980s :-)

PeterO


Message has been deleted

r4r...@gmail.com

unread,
Sep 26, 2025, 7:28:29 PMSep 26
to RC2014-Z80
Hi, 

Do you fly yourself across the country?  Why not go get a commercial/instrument license?  It's easy.  

IF the program was compiled for the Z80 computer, why do people feel the need to post non-Z80 code and not the final compiled program?  All that does is make it sound like a scam.  


.

r4r...@gmail.com

unread,
Sep 26, 2025, 7:28:29 PMSep 26
to RC2014-Z80
Hi, 

Yes, Rather than everybody needing to compile the program for their Z80 computer, it only takes one person with the tools to do it and post it for sharing.  I hope to try it on my ZAltair mini-FP and ZAltar Z80c computers.  


.

John Sandlin

unread,
Sep 26, 2025, 8:34:21 PMSep 26
to RC2014-Z80
The program does one thing. Displays 400 digits of Pi. It's all the compiled version will ever do. And if it is compiled for only the Z80, that is all it will ever run on. The advantage of having the source and a C compiler is you can change the number of digits it can show (limited by memory, of course). Also, for someone like me, it was a chance to learn a little more about writing C on a retro computer and specifically on the RC2014 in this instance. Also, seeing the algorithm for calculating Pi is illuminating, not that I fully understand it yet. It might work on the ZAltair as compiled, or it might not. No guarantees.

So we could post the compiled code and you get basically a once and done experience, if it works.

r4r...@gmail.com

unread,
Sep 26, 2025, 9:29:22 PMSep 26
to RC2014-Z80
Hi, 

That's exactly what I asked for in my first message here.  Just a simple Pi.COM program I can run on Z80 computers to see how it ports from machine to machine.  


.

Doug Jackson

unread,
Sep 26, 2025, 10:50:36 PMSep 26
to rc201...@googlegroups.com
The magic of CP/M is that porting from machine to machine is not necessary - It is just like WS.com - It doesn't need to be 'ported' between machines.  it just works.   What it does need is the terminal handling configuration adjusted so that it works with the infinite variety of terminals and printers in the world.

Kindest regards,

Doug Jackson

ph: 0414 986878




--
You received this message because you are subscribed to the Google Groups "RC2014-Z80" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rc2014-z80+...@googlegroups.com.
Message has been deleted

David Lochlin

unread,
Sep 26, 2025, 10:57:29 PMSep 26
to RC2014-Z80
Well I posted the .com file here easrlier but Google chose to delete it. Don't know what else I can do. Around 25% of my posts get deleted for some reason which makes it hard to have a decent conversation.

Dave

Peter Onion

unread,
Sep 27, 2025, 2:39:28 AMSep 27
to rc201...@googlegroups.com
On Fri, 2025-09-26 at 17:34 -0700, John Sandlin wrote:
> The program does one thing. Displays 400 digits of Pi. It's all the compiled version
> will ever do. And if it is compiled for only the Z80, that is all it will ever run on.
> The advantage of having the source and a C compiler is you can change the number of
> digits it can show (limited by memory, of course). Also, for someone like me, it was a
> chance to learn a little more about writing C on a retro computer and specifically on
> the RC2014 in this instance. Also, seeing the algorithm for calculating Pi is
> illuminating, not that I fully understand it yet. It might work on the ZAltair as
> compiled, or it might not. No guarantees.
>
> So we could post the compiled code and you get basically a once and done experience, if
> it works.

I agree. This is also about education, open-source philosophy, and security.

Here is a simple (as in not much code) example that does something interesting. Curious
people are looking at it and saying "How does that work ?" and then going on to learn
about the algorithm it uses. You can't do that with a .COM file.

It's called "open source" because it's the source code that is distributed. I would
imagine that many of the people on this list use at least some open-source, and some of us
use only open source. It promotes sharing of information and helping each other solve
problems.  

If you run a .COM file from some random person on the internet you are taking a big risk.
It would be very easy to insert only a few bytes of code to do something nefarious to your
system.  Do you still want to run a .COM file from me ? ;-)


PeterO
>

r4r...@gmail.com

unread,
Sep 27, 2025, 4:05:27 AMSep 27
to RC2014-Z80
Hi, 

But a program that just calculate Pi to 400 places that is compiled into a Pi400.COM program for Z80 CPUs should work in almost any CP/M or even DOS operating system.  

The program only needs to print back to the CP/M console.  This is why I don't understand why a .COM program cannot be posted for downloading rather than everybody needing to compile the same c program for the same Z80 CPUs.  


.

Tony Nicholson

unread,
Sep 27, 2025, 5:19:28 AMSep 27
to RC2014-Z80
On Saturday, September 27, 2025 at 6:05:27 PM UTC+10 r4r...@gmail.com wrote:
Hi, 

But a program that just calculate Pi to 400 places that is compiled into a Pi400.COM program for Z80 CPUs should work in almost any CP/M or even DOS operating system.  

A program binary compiled into Z80 machine code using standard CP/M BDOS calls will only run on a system that can execute Z80 machine code and is running a CP/M compatible operating system (CP/M 2.2, CP/M Plus or derivatives like ZPM3 or DOS-Plus).

It will not normally run under what you may mean by "DOS operating system" - which most people associate with MS-DOS or PC-DOS on an Intel x86 or x86_64 CPU) unless you are using a Z80 emulator (like yaze-ag, ZXCC or SIMH Altairz80).
 
The program only needs to print back to the CP/M console.  This is why I don't understand why a .COM program cannot be posted for downloading rather than everybody needing to compile the same c program for the same Z80 CPUs. 

Most e-mail systems now block the transfer of what they regard as binary executable programs.  Google now considers any COM or EXE file a risk - so they are blocked.

I've compiled the pi.c program posted earlier in this thread using HI-TECH C and then converted the Z80 CP/M PI.COM program binary into an Intel Absolute Loader format HEX file using

A>c -o pi.c
A:C        COM
Hi-Tech Z80 C Compiler (CP/M-80) V3.09-20
Copyright (C) 1984-87 HI-TECH SOFTWARE
Updated from https://github.com/agn453/HI-TECH-Z80-C

A>unload pi.com
A:UNLOAD   COM

UNLOAD ver 2.3
Done

A>

You'll need to transfer the PI.HEX file (attached) to your CP/M system (using XMODEM, Kermit or even PIP) and convert it back to the program binary using

A> pip pi.hex=con:[H]

(paste the PI.HEX text here and then press CTRL-Z to end tyhe transfer)

A>load pi.hex   (CP/M 2.2)

or

A>hexcom pi.hex    (under CP/M-Plus)

Tony 


PI.HEX

Peter Onion

unread,
Sep 27, 2025, 6:40:55 AMSep 27
to RC2014-Z80
What exactly are you trying to prove ?  There are already dozens of .com files on your CP/M machine that just "print to the CP/M console".

PeterO

r4r...@gmail.com

unread,
Sep 27, 2025, 6:50:57 AMSep 27
to RC2014-Z80
Hi, 

Thanks, Tony.  Now all I need to do is rename the file PI400.COM on my Windows PC, XMODEM it over to my ZAltair Z80, or my ZAltair mini-FP and run the program.  

No need to download, install, compile a tiny program on my Windows PC, then do the transfer.  

Some people don't seem to understand we're not all programmers with assemblers and compilers and cross-compilers etc ... we have other things going on in our lives, but like to try stuff once in awhile when we see it.  

Trying to force people to do things they don't want to do isn't nice.  


.

Message has been deleted

David Lochlin

unread,
Sep 27, 2025, 9:16:23 AMSep 27
to RC2014-Z80
Now I get why my message containing the .COM file got deleted. Probably got a 'security alert' tag against my name now, LOL 

r4r...@gmail.com

unread,
Sep 27, 2025, 10:44:56 AMSep 27
to RC2014-Z80
Hi, 

Because of hate speech, scammers, hackers etc.  Google is trying to protect itself from future class action lawsuits.  IF their A.I. was so good, it should be able to test a .COM program, see and understand what it's doing and then approve it for posting within a couple of seconds.  


.

Alan Cox

unread,
Sep 27, 2025, 11:35:02 AMSep 27
to rc201...@googlegroups.com


On Sat, 27 Sept 2025, 15:45 r4r...@gmail.com, <r4r...@gmail.com> wrote:
Hi, 

.. understand what it's doing and then approve it for posting within a couple of seconds.  

Mathematics says otherwise. It's a dumb insecurity hold over from the old days but you can't easily tell if random code is hostile.

It's btw possible to build a .com file that if on 8080/Z80 runs direct and on 8086 runs an emulator built into the .com file. Always a fun party trick.

In the early days of CP/M 86 and MessDOS people did actually start shipping .com files that errored politely on the wrong CPU for a while.

Alan

Peter Onion

unread,
Sep 27, 2025, 2:08:56 PMSep 27
to rc201...@googlegroups.com
On Thu, 2025-09-25 at 22:42 -0700, r4r...@gmail.com wrote:
> Hi, 
>
> Yes, Rather than everybody needing to compile the program for their Z80 computer, it
> only takes one person with the tools to do it and post it for sharing.  I hope to try it
> on my ZAltair mini-FP and ZAltar Z80c computers.  
>

So it seems the whole thread has been off topic for this list as these are not RC2014
systems.

PeterO


Tony Nicholson

unread,
Sep 27, 2025, 5:32:30 PMSep 27
to RC2014-Z80
On Saturday, September 27, 2025 at 11:16:23 PM UTC+10 David Lochlin wrote:
Now I get why my message containing the .COM file got deleted. Probably got a 'security alert' tag against my name now, LOL 

Instead of making claims about “security alert tags” - you could easily look up the facts about attachments on Google’s support help pages at the following URL -


Tony

r4r...@gmail.com

unread,
Sep 27, 2025, 6:09:28 PMSep 27
to RC2014-Z80
Hi, 

The discussion was compiling the program into a Z80 program to run on a Z80 computer.  


.

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages