About Archive Tags RSS Feed


Building a computer - part 2

18 July 2019 13:01

My previous post on the subject of building a Z80-based computer briefly explained my motivation, and the approach I was going to take.

This post describes my progress so far:

  • On the hardware side, zero progress.
  • On the software-side, lots of fun.

To recap I expect to wire a Z80 microprocessor to an Arduino (mega). The arduino will generate a clock-signal which will make the processor "tick". It will also react to read/write attempts that the processor makes to access RAM, and I/O devices.

The Z80 has a neat system for requesting I/O, via the use of the IN and OUT instructions which allow the processor to read/write a single byte to one of 255 connected devices.

To experiment, and for a memory recap I found a Z80 assembler, and a Z80 disassembler, both packaged for Debian. I also found a Z80 emulator, which I forked and lightly-modified.

With the appropriate tools available I could write some simple code. I implemented two I/O routines in the emulator, one to read a character from STDIN, and one to write to STDOUT:

IN A, (1)   ; Read a character from STDIN, store in A-register.
OUT (1), A  ; Write the character in A-register to STDOUT

With those primitives implemented I wrote a simple script:

;  Simple program to upper-case a string
org 0
   ; show a prompt.
   ld a, '>'
   out (1), a
   ; read a character
   in a,(1)
   ; eof?
   cp -1
   jp z, quit
   ; is it lower-case?  If not just output it
   cp 'a'
   jp c,output
   cp 'z'
   jp nc, output
   ; convert from lower-case to upper-case.  yeah.  math.
   sub a, 32
   ; output the character
   out (1), a
   ; repeat forever.
   jr start
   ; terminate

With that written it could be compiled:

 $ z80asm ./sample.z80 -o ./sample.bin

Then I could execute it:

 $ echo "Hello, world" | ./z80emulator ./sample.bin
 Testing "./sample.bin"...

 1150 cycle(s) emulated.

And that's where I'll leave it for now. When I have the real hardware I'll hookup some fake-RAM containing this program, and code a similar I/O handler to allow reading/writing to the arduino's serial-console. That will allow the same code to run, unchanged. That'd be nice.

I've got a simple Z80-manager written, but since I don't have the chips yet I can only compile-test it. We'll see how well I did soon enough.



Comments on this entry

icon Filip Beijer at 06:20 on 18 July 2019

So, what are the changes to z80emu, care to share these

icon Steve Kemp at 06:39 on 18 July 2019

This is my repository:

  • http://github.com/skx/z80emulater/

This is the original, from which I forked it:

  • https://github.com/anotherlin/z80emu

I made a couple of changes, in a bit of a sloppy fashion due to whitespace/formatting updates. But in brief the original project contained a test-driver which was designed to load two binary files (hardwired) and run their contents. These test-files test the various opcode implementations.

I updated the code to instead load the file(s) specified upon the command-line, and load the code at address 0x0000 - instead of the previous default of 0x0100. (Since the z80 sets the instruction pointer to 0x0000 when it boots).

I changed the name of the generated driver from zextest to z80emulator to match the intended use, and I updated the z80user.h configuration file to allow port reading/writing, via address 1. The only other change I recall without running diff was to make the HLT instruction terminate the emulation, which better matches what a real Z80 would do.

icon Filip Beijer at 15:00 on 18 July 2019

thanks for the info about your version of z80emulator