# Calculating Pi on the 4004, and then on the 4040

A couple of really juicy Pi-related retrocomputation blog posts here, using early-1970s microprocessors with very limited RAM and ROM and no special augmentations.

In both cases the inspiration was from 1949:

Over the long Labor Day weekend in 1949, a team lead by George Reitwiesner at the US Army’s Ballistic Research Laboratory in Aberdeen, Maryland, used the ENIAC to compute the decimal expansion of pi out to 2,035 places, more than doubling the previous record of 808 digits.

First the 4004, configured to address 10k bits of nibble-wide memory (using 4 RAM chips)

I want to have bit width divisible by 4, 8-bit option gives us only 38 digits, but 12 bit allows us to calculate 255 digits of π. And we would have an extra 40 bits in memory for utility needs.

And as a follow-up, using the 4040 to try to match the ENIAC result but preferably faster than 70 hours elapsed

This time there’s 1280 bytes of RAM to play with!

But the good news is that we got some new features with 4040:

• Now we have AND/OR instructions. You read it right, 4004 didn’t have them. Unfortunatelly, these instructions are not perfect – they perform a binary operation with an accumulator and a specific register (`rr6` or `rr7` for the `AND` operation, for instance).
• The call stack grew from 3 nested subroutine calls to 7, so we can better organize our program. Still, it’s a pretty low value and at some point, I exceeded it.
• Increased the amount of general-purpose registers from 16 to 24. Intel added a second bank with 8 registers. Because the ISA is the basically same and the register index is still encoded as a 4-bit number, for access to extra registers we need to switch between index register banks by executing specific instructions: `SB0` / `SB1`, which adds some inconvenience.
• ROM space has been extended as well. Now we have 2 banks of ROM with 4KiB each. Again, switching between banks is not transparent, you need to call the `DB0` / `DB1` instruction at a specific moment.
3 Likes