Engineer Programs Four-Bit 4040 Microprocessor to Calculate Pi Faster Than the ENIAC

With a 180,000% improvement in calculation time from initial to final implementation.

James Lewis
2 years agoRetro Tech

In 1949, ENIAC was the first programmable computer to calculate a decimal expansion of π. It took ENIAC 70 hours to determine 2,035 digits. Mark Ablovatskii wondered if a 1974-era four-bit Intel 4040 microprocessor could calculate digits of Pi faster than the ENIAC. The results were surprising!

The four-bit Intel 4040 succeeded the original 4004 microprocessor. It ran at 740 kilohertz and could achieve 92,500 instructions per second. The 4040 included a few improvements that helped Ablovatskii's goal, like AND/OR instructions, a slightly larger stack, eight new registers, and support for two banks of ROM. However, it did not have multiplication, division, or shift instructions!

For comparison, the ENIAC ran at 100 kilohertz and performed about 5,000 additions or 50 multiplications per second. But its accumulators were also 10 digits! The 25-year gap may make this project look like a mismatched contest. However, Ablovatskii's first effort would have the 4040 calculating digits for years!

Several methods exist to calculate the digits of Pi. For example, a spigot algorithm keeps an array of numbers in memory. The trade-off with this method is that you need an amount of memory proportional to the number of digits you intend to calculate. Since the 4040 only has 1,280 bytes of RAM, it would not be possible to get 2,035 digits of Pi using this type of algorithm.

Overall, Ablovatskii considered seven methods before choosing an algorithm from Fabrice Bellard. This one is a practical implementation that can calculate π to the millionth position.

Before developing the code to do the calculations, Ablovatskii had to create a toolchain and emulator. Both efforts were extensions of Ablovatskii's previous work on a 4004-based project. (Also, a Pi calculator!) The toolchain consists of a pre-processor, compiler, and linker. The emulator implementation used JavaScript.

This project centered around the 4040 microprocessor. However, a modern STM32 provided an emulated memory device to the vintage processor and a USB interface back to the PC so we could see the resulting digits.

With tools in hand, Ablovatskii's first effort yielded 2,035 digits of Pi! However, completing the task would take an estimated 14.5 years or approximately 127,000 hours at the 4040's actual clock speed. It does not take in-depth math knowledge to realize that time is slightly longer than ENIAC's 70-hour benchmark.

Through an extensive iterative cycle of profiling and optimizing code, Ablovatskii eventually reduced the computation time to 69 hours, 28 minutes, and 31 seconds — an improvement of 180,000%!

Check out Ablovatskii's blog post on the project for detailed breakdowns of the math behind this achievement!

James Lewis
Electronics enthusiast, Bald Engineer, AddOhms on YouTube and KN6FGY.
Latest articles
Sponsored articles
Related articles
Latest articles
Read more
Related articles