Zilog Z80 Arduino Mega project

Zilog Z80 Arduino Mega I found an interesting project on the Internet of a Z80 processor on a breadboard that was connected with wires to an Arduino Mega board. It had some code with it, it was far from complete but it triggered me. I adopted the idea and improved it, rewrote the Arduino code and improved the hardware design. The Z80 processor in this project is running at 200 Khz, clocked from the Arduino and the Arduino emulates 8 kb of ROM and 6 kb of RAM and serial input and output. With this simple and cheap configuration you have a complete working Z80 based computer. With some adjustments I even got a complete Basic interpreter running on this hardware with the serial I/O of the Arduino as interface. But it is also nice to write your own assembler code and see it not only running in an emulator, but also on a real Z80 processor. Just put your generated HEX code in the memory.h file and recompile the Arduino sketch and upload it to the Mega board. On reset the Z80 will execute the code in the ROM section of the MEGA. Have fun!
Regards, Hein Pragt.

I also have a Facebook Group on Retro Computing and Electronics, feel frtee to join!

Building a Z80 Arduino Mega computer

What do you need for this project? Well at first an Arduino Mega board, then a development shield, a 40 pin socket for the Z80 processor, some breadboard wires and two rows of header pins. At least, that’s how I build it and the parts are all re-usable. You can also put the Z80 on a breadboard beside the Arduino Mega and use longer wires. Longer wires will effectively have some influence on the reliability and the speed. I soldered the socker on the development shield and also soldered the two rows of header pins alongside the socket. For a more permanent solution you could also solder all the connections from the Z80 socket to the header pins of the Arduino. There are many possibilities, its up to you how to make this project. The wiring diagram is below.

I noticed that the below configuration will only work with a NMOS version of the Z80 processor, I used a AB1 version!

z80 with Arduino Mega schema

The pins on the Arduino are chosen to pair the data and address lines to fixed 8 bit ports of the Arduino Mega so its easy to process them. The data lines are connected to pins 42-49 and this is the port L register of the Arduino Mega. The lower 8 address lines are connected to pin 22-29 and this is the port A register of the Arduino Mega. The upper address lines are connected to pins 32-37 and this is the port C register of the Arduino Mega. The INT, RESET and WAIT pins are outputs of the Arduino Mega and inputs on the Z80. The M1, IQREQ and MREQ are input pins on the Arduino and output pins on the Z80. These are control signals and the pins on the Arduino Mega are not dedicated. The CLK line is, this is connected from pin 10 of the Arduino Mega to the Z80 and pin 10 is the output pin of one of the internal timers of the Arduino Mega, to generate the clock signal for the Z80. You can program the speed by programming the divider in the Arduino. The most important lines are the RD and WR lines of the Z80, we connect them to the Arduino pins 18 and 19 so they can be used to generate interrupts on the Arduino Mega for the read cycle and the write cycle of the Z80 processor. On the Z80 some unused pins must be connected to +5V and the ground pin must be connected. That’s all.

z80 cycles

The RD and the WR are used to trigger interrupts on the Arduino Mega, so all the read and write actions will be processed in the background. The triggers od the interrupts are on the falling edge of the signals, that is de start of the RD and WR signal. On read we will first read the address from the address lines and if its below 2000h we will read the flash memory array (8K) of our Arduino (our Rom) and it its above 2000h we will read the memory array (6K) of the Arduino (out Ram) and put this data on the data bus to be read by the Z80. On WR we also read the address pins and also read the data pins, then we will store it in the memory array inside our Arduino memory. You cannot write to Rom so we do not need to handle that. It seems quit simple.

But we also need to process IO requests, so at the start of the interrupt routines we first check the MREQ pin. If this is active it is a memory request and we do the above. Then we test the IOREQ pin, if this is active it is an IO request. We now read the lower part of the address register (the Z80 only uses the low bytes) to determine the port. If it is an IO read, we read the data and if its port 1 we will send the data to the serial port of the Arduino. If it’s a IO read we check the lower address line for the port number and then its important to activate the WAIT l ine. Because we are going to do some serial IO things on the Arduino Mega, this will take more time on the Arduino Mega than the length of the read cycle of the Z80. The Z80 has a feature for slow IO devices that’s called the WAIT pin. When this pin is active the Z80 waits the IO operation until the device has the data ready and deactivates the WAIT pin. We now have enough time to poll the Serial buffer of the Arduino Mega and return the data (character received or the buffer status) on the data pins for the Z80 to read.

z80 with Arduino Mega photo

In basic we now have a complete emulation of Rom, Ram and IO device to the Z80 processor. At the start of the Arduino sketch, I first declare the pins of the Z80 to map them to Arduino registers. I also have some macros to read the state of pins or set the state of pins. This makes it easier to use in the code. Then I set the correct lines to input and output, its important to use the internal pullups on the address lines. Then initialize the serial port of the Arduino to a high speed and activate the RESET pin of the Z80 processor. This will start running and executing the instructions that I reads from our Arduino Flash memory array and read / write data to the ram memory array inside our Arduino Mega. We can start the serial terminal of the Arduino to see the serial output and send characters to the Arduino Mega and thereby to the Z80. You can also use any other terminal program. I included a version of the (little modified code) of the basic interpreter I got form the website of Grant Searle, a great electronics designer.

You can use my Z80 IDE to develop programs for this device, just set the rom area to 0000-2000 and the ram to max 3800 and then you can write, test and debug your Z80 assembly code. Do not forget toe initialize the SP register somehere in the Ram area. When its working, just export it to a HEX C style array (files menu) and cut and paste that code to the memory.h file of the Arduino sketch. Recompile the sketch and send it to the Arduino Mega, on reset the Z80 program will run on a REAL Z80 processor. I think that is cool.

You can expand the Arduino sketch, currently there is no code in the loop(), you can add your own code here, f.i. to read sensors or and SD card, but you can also use the ports of the Arduino as digital or analog input / outputs (just add an IO port section) to interface the Z80 processor. It’s up to your own creativity. Be aware that in this setup I tested that the maximum reliable clock speed of the Z80 is about 200 kilohertz, so that’s rather slow. You cannot speed this up too much, the Arduino will not respond in time on the Z80 requests and the wires will also cause a loss in signal quality. But is is a nice experiment if you want to play around with the old Z80 processor, I had a lot of fun building and developing this. Have fun!

The code and schematics

Freeware Z80 IDE (Editor/assembler/emulator/debugger)

HTTP Health monitor To be able to program for the z80 you need an assembler or compiler. I personally still like to write in Z80 assembler but I could no longer find tools for Windows 10. There were still some programs to download but most of them worked at DOS level. After some searching I decided that it was time for a new hobby project, building a complete integrated Z80 development environment. I found a pretty good assembler and good portable C code from an emulator and the rest I had somewhere in my code library. The result is Z80 workbench, a portable x64 Windows program that includes an editor, assembler, disassembler, emulator, single step debugger, Intel hex read / write function a terminal window, an MPF-1 compatible seven segment display with 8 LEDs and keyboard support. One of the unique features of the Z80-PlO is that all data transfer between the peripheral device and the CPU can be accomplished under total interrupt control even with nested interrupts. The the PlO can be programmed to interrupt if any specified peripheral alarm conditions should occur. This interrupt capability reduces the amount of time that the processor has to spend in polling peripheral status.
Download the program on this page

Z80 related documents and webpages

Last update: 18-12-2021

Disclaimer: All pages on this Web site are copyrighted by Hein Pragt, unless otherwise noted. I strive for accuracy but cannot be held responsible for any errors in the content. For questions about the content of this site or persmission to copy you can contact me at: (email: heinpragt@outlook.com). Heinpragt.com is registered under KvK number: 73839426.