Tag Archives: z80

Z80 on Arduimo Mega

Z80 Mega05

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 free 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 Mega04

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 Mem 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 Mega06

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

Z80 related documents and webpages

More pages on embedded software

Z80 processor IDE

Z80 Header2

This is a page about my freeware Z80 IDE program which is an editor / emulator / debugger / assembler / disassembler for the Z80 all in one portable Windows executable. Many years ago when I was programming the Z80 professionally and at home, tools were very expensive. There was not much freeware or opensource, but trough my employer I had most of the tools I needed. When I was twenty years old in 1979 I bought my first real computer, the Tandy TRS-80 model I level II with a Z80 processor at its heart. I wrote a lot of programs in Z80 assembler for this computer and all computers I bought after that had a Z80 processor like the Sinclair Spectrum and later the MSX. My first IT job was developing hardware and software on embedded Z80 systems (cash register and machine control systems) in assembler and PL/Z. After that I started working at Micro Technology (an MSX hardware supplier) and there I was also programming a lot in Z80 assembler, bat I also learned to write in C and I had a good C compiler for the Z80. assembler. The Z80 was a genius design for its time and its funny that this processor is still used a lot in for instance the Nintendo Gameboy but also in the Texas Instrument scientific calculators. Also there is a growing group of computer hobbyist that keep the processor alive by creating new single board computers based on the Z80 processor. For development and debugging Z80 assembler code, this freeware IDE can be used. Regards, Hein Pragt.

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

Z80 workbensch editor / emulator / debugger

Z80 Workbench

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.

Installation

Download the zip file and unzip it at the place where you want to install it on your hard drive. Then create a shortcut to the program to the desktop and the program is ready for use. It is a so-called portable program which means that it does not use external sources or libraries and does not use the registry. After removing the directory from this program, it has also disappeared completely from your system.

First time use

To practice there is an example4.asm file in the example directory, which you can load after which it will be in the shown in the middle edit window. Now you can convert this into machine code by means of the assemble button, this will appear on the left of the screen and be put into the virtual memory of the emulator. The last (or first) .org statement will also be put in the program counter and by simply pressing the run button you can execute the program and follow it on the screen. This will not be very fast because every instruction will perform a screen update of all registers. If you want the program to run faster, you can disable the update of the registry block by means of the update checkbox. You can now modify the program, reassemble it and run it again.

The basic2.asm is an example of the basic assembler file from Grant Searle, I only added a modified BIOS file at the end and an .org 0000h at the end to set the start adresss.

The monitor.asm is an example of the sourcode of a simple monitor program to input hex bytes a an address, dump bytes of ans address to terminal and call an aadress in memory.

By means of the step button you can execute the program instruction by instruction and check the output and the registers. You can also enter the break address, the emulator will then stop at a run command at this address and display the registers of that moment. When 0000 is entered in the break field it will be ignored.

You can save the modified code as an asm file again but also save it as a hex file so that you can load the binary code in a circuit or ep(rom) for a real Z80 circuit. You can also read an Intel-Hex file, which will then be loaded into the internal emulator memory. You can also run tis code when you put the correct start address in the program counter. You can also disassemble this internal emulator memory code after which the source code will appears in the edit screen in the middle. This can then be changed and written back into the emulator memory by means of assemble button.

I/O map

The emulator has a standard number of I/O ports for input from the keyboard and output to the terminal screen or the seven segment displays / LEDs. In example4 there is an example of both.

  • In port 1: Returns the character of the keyboard in A register, when there is no sign a 0 character will be returned.
  • In port 2: Return the status of the keyboard, 0 no keystroke is available, unequal to 0 means a keystroke is available. The character must then be read on port 1.
  • Out port 1: Data for terminal or seven segment display. When Out port 2 is a 0, the data will go to the terminal. If port 2 is unequal to zero, the data will go to the seven segment display.
  • Out port 2: Selection terminal (0) or seven segment display. When a segment select bit (0..5) is 1, the display data will be send to each seven segment display that has its bit set to 1. It is therefore possible to address each display individually as well as multiple displays at the same time. To turn everything off, first set 0xff to port two and then a 0 to port 1. To switch back to the terminal, you have to write a 0 to port 2 again. (In contrast to the MPF-1, the data of the display is latched.)
  • Out port 3: This port is a latch to the eight LEDs where each bit controls an LED.

Credits: Credits go to Jorge Giner Cordero for letting me use and distribute his Z80 assembler.

Version 2.01 – 2.04

After version 1.07 I decided to add full support for a Z80-MBC2 emulator including CP/M 2.2 because I use this board and then I noticed some errors in the Z80 emulator code. So I decided to fix that and before I knew I was starting to rewrite all of the emulator code and now it is good and supports a lot of extra undocumented features of the Z80 processor. For CP/M 2 I needed a VT100 terminal, I had the code from 30 years ago when I write my own terminal when I was working with a DEC 750 VMS machine so I could reuse this code with some adjustments for Windows. When debugging the Z80 emulator I needed better breakpoint and tracing so I also added all these features. By this time I realized this would not just be version 1.08 but it is clearly a major upgrade so I decided to go for version 2.01. I included som CP/M disks and some old CP/M programs I still had as well. Just click Z80-MBC2 CP/M 2.2 in the main menu and then push the RUN button and you are on a Z80-MBC2 emulator machine with VT100 terminal and a few disk drives.I hope you like the new features and I will continue to improve this Z80 workbench, I have some new options in mind, like CP/M disk management and support for emulating more hardware and machines as well. In version 2.04 I added full support for hardware and software emulation of the famous MPF-1 board. I support the basic board, with an on screen keyboard, the 8255 I/O chip and the keyboard and display scan as well. I do not support tape load and tape save, these buttons link to the standard save hex and load hex as nice replacements. You can use the debugging solutions of the MPF-1 in combination with the debugging solutions of the IDE and you can use the assembler / disassembler together with the MPF-1. For the MPF-1 I also added support for the Z80 None Maskable Interrupt because the MPF-1 uses that for single stepping. And as usual I added one example program for the MPF-1.

Buy Z80 chips and support chips

I also have a webshop where you can buy all kind of Z80 chips and support chips to repair or create youre own Z80 board.

https://www.heinpragt.nl/?s=z80&post_type=product

Download

Here you can download the latest version if this IDE. This download is a zip file containing a portable x64 (and a x32 version) Windows exe programs, two example directories, a CP/M disks directory and an assembler directory and a MPF-1 directory. The zip file is just a container, only copy the “z80_workbenchdirectory from the zip file to a location on your harddrive.
There was a last minute bug in the 204 version, the SIMPLE mode did not work correct anymore, this is fixed in the 204A version.

https://www.heinpragt.nl/?product=z80-processor-ide

  • Version 2.04
    • Fixed seven segment display blanks and errors after heavly use.
    • Fixed seven segment display lines are wider now.
    • Fixed R register is now functioning like it should.
    • Added MPF-1 emulation, with original ROM and keyboard and display in true MPF-1 mode.
    • Added MPF-1 hardware emulation (8255) and ram / rom mapping.
    • Added mpf1 directory with hex code of ROM and example assembler file.
    • Added None Maskable Interrupt button and correct handling in emulator.
  • Version 2.03
    • Included latest version 1.10 of uz80as.exe in this project.
    • Added -u option to assembler to enable undocumented instructions.
    • Fixed false caret display in VT100 terminal.
    • Fixed error in VT100 terminal with 80th character on next line.
  • Version 2.02
    • Fixed error in VT100 handling that would cause a crash of the program.
    • Added new cursor handling in VT100 using the Windows caret.
  • Version 2.01
    • Complete rewrite of the Z80 emulator code, it now also supports all the undocumented features.
    • Added full Z80-MBC2 emulation mode (inluding CP/M 2.2), including R/W SP/M disk images from Z80-MBC2.
    • Added the CP/M drives from Z80-MBC2 and an extra drive H: that contains some old CP/M programs I had like Sargon Chess, an Aventure game and Zork.
    • Click Z80-MBC2 CP/M 2.2 in the main menu, push the RUN button and you are on a Z80-MBC2 machine with VT100 terminal and a few disk drives.
    • Changed the layout of the screen to fit a complete functional VT100 terminal emulation.
    • Added a notification window for errors and tracing.
    • Removed cycle counting.
    • Added new top menu items for window and terminal settings.
    • Added support for monitting and editing 4 memory locations realtime (Handy for memory mapped I/O).
    • Added support for breakpoint on memory access.
    • Added support for start of trace on breakpoint instead of stopping.
    • Added support for loading binary files (like roms) into memory.
    • Improved disassembler, better syntax and newlines after jumps and return statements.
    • Added realtime tracing in notification window with address and disassembled instruction.
    • Beside x64 there is also now a x32 version.
    • Added feature to write all terminal output to a file in the base directory.

Extentions to the IDE

George Kordogiannis wrote some nice additions to the IDE that I will include in the new version. For now I want to make the additions available to all existing users with this download. Just download the file and add (overwrite) the subdirectories of the z80_ide. It will also add a cpmtools directory.  F.i. it makes it possible to send HEX programs to your CP\M environment, and run them. Inside the cpmtools folder you will find 4 BAT files.

LS.BAT shows the contents of your CP\M folder

TRANSFER.BAT transfers the file you put inside the BAT file to your CP\M folder

With TRANSFER1.BAT it asks you to write the name of the HEX file you want to transfer.

With KILL.BAT we delete the given file from your CP\M folder

Finally in CP\M now with the LOAD command we can convert it to COM and run it.

This way you can write, save it in examples folder and develop a program on Z80 testing it directly in CP\M environment without having to transfer it to Z80-MBC2 which is a time-consuming process.

Download this zip file here: z80_wb_additions.zip

Z80 links, tips and documents

More pages on software development