Search This Blog

Saturday, 21 December 2019

Z80 Arduino Using Mega as Debugger

Z80 Arduino Using Mega as Debugger

Ever since the 80's I've wanted to build a Z80 computer. I even bought the chips, but never used them. The reasons for this are varied. I never liked erasing and reprogramming EPROMs. My EPROM eraser died. I didn't have the money to make a PCB, or the software (or red and blue tape and a lightbox). In the 90's I did make a 6502 computer which was done with loads of wires on veroboard. Then the Arduinos came along and I didn't really need a Z80 computer as much.

I still wanted one though.

Then, in a flash, the scratch to my itch appeared: Why not make a Z80 computer with flash memory? Well, fine, but how do you program the flash memory? Aha! Why not use an Arduino to take over the bus and program the flash chip?

That should work, and once I had the idea of an Arduino to program the flash, it didn't take too long to think about using an Arduino to drive the Z80 clock and bus signals and provide flash data directly itself. This allows you to run the Z80 using the Arduino and read all the bus signals. You can single step your code at the resolution of a half T state (one clock transition). Once you are happy with your code, it can be burnt into flash. Once there, you can remove the debugger Arduino and use the board as a stand alone computer.

Of course, without IO and hardware bits attached to it, a computer like this isn't very interesting. So I added a couple of Z80 PIOs and the footprint for an Arduino shield. So you can plug shields in to the board and use whatever is on them. So you have a Z80 Arduino. Programmed in Z80 assembly (or C, or whatever you can get running on a Z80).

PCB manufacture is a lot cheaper these days so I had a few PCBs made up.
The board looks like this:



It is technically an Arduino shield as it plugs into the top of a Mega. It is also an Arduino itself, as shields (Uno format) plug into it. You run it either under Mega control where the Mega clocks the Z80 and emulates flash (the RAM is switched in as the mega runs the Z80, so you have 32K of RAM, more than the Mega can supply). Or you run it at 4.5MHz from the real hardware (with the Mega plugged in or not).

The specs aren't too bad either, there's 32K of SRAM and 512K of flash. The flash is bank switched, but needs a small piece of code in each bank at the reset vector. The Z80 runs at 4.5MHz.

The two PIOs have 32 GPIOs, each of which is bit addressable as input or output. I also put a Z80 CTC on there for timing things. This PCB uses through hole components apart from the flash chip, a 3V3 regulator and an A/D converter. There's some surface mount capacitors too. It's pretty easy to build, but does come out larger than an Uno, or Mega.

The mega plugs in underneath:


 The shield plugs in on top:


Under the shield there's an A/D converter that provides the analogue channels when operating as an Arduino. There's also a 3V3 regulator, just in case:


There's various test code in the sketch. This code writes to the LCD on the LCD/button shield:


I've got a script that embeds Z80 .lst file output into the Arduino sketch so you don't have to manually edit the file, you assemble it then run the script and it's put into your sketch which you then compile.

I've written a terminal emulator script that lets you upload .ihx files that have been built using the Z88SDK C compiler. The terminal emulator also has a register dump facility. This uses a small piece of code that is executed between each instruction when the Z80 is being clocked by the Mega. This code dumps the register contents in a non-disruptive way and the data is picked up by the terminal emulator and displayed.

This is very much a work in progress, but that's part of the fun isn't it?

The files are here on github, there's all the code and the PCB files. I use geda.

The PCBs I made are on Dirty PCBs here:

https://dirtypcbs.com/store/designer/details/9818/6402/z80-arduino-shield

A quick video of the shield:


5 comments:

Brian said...

Very cool!

Unknown said...

Could you post a schematic in ".pdf" format?

amen said...

I've uploaded a PDF schematic.

Alfa66 said...

Awesome

Unknown said...

Thank You for quickly posting the schematic. It took
me awhile to find it.