Announcing z80, my Zilog z80 emulator

WOPR

The Zilog Z80 is an 8-bit microprocessor designed by Zilog and sold from July 1976 onwards. It was widely used both in desktop and embedded computer designs as well as for military purposes. The Z80 and its derivatives and clones made up one of the most commonly used CPU families of all time, and, along with the MOS Technology 6502 family, dominated the eight-bit microcomputer market from the late 1970s to the mid-1980s.

z80 (zee-eighty) is an open source project I built in the past few months in my spare time. It emulates my favorite CPU of all time, the Zilog Z80.

Star or fork on GitHub

Why, oh why?

Because the Z80 is awesome and it made world-changing machines possible: the WOPR, the Sinclair ZX Spectrum, the Nintendo Game Boy and Galaga.

Because I learnt how to code on it, it was my first Assembly dialect and, whilst I even hand-compiled it at times—and stored it via POKE in memory—I never felt I mastered it completely. I though writing an emulator for it was good enough to give me the grounding I needed.

The project

z80 works in real time written in C#.

These are are its features:

  • Z80 Emulator (Z80), emulates all official opcodes with correct timings, and also implements some undocumented features.
  • Z80 Assembler backend (Z80Asm), basically a bunch of functions corresponding to Assembly instructions which write the op-codes to memory
  • Zilog-based Z80 tests (z80.Tests), a whole bunch of them, more than 2,000 presently

The tests are a translation of the documentation, the assembler backend is needed to write tests and stay sane and the emulator is the whole point.

There's a very basic step debugger in the tests (TestSystem) which basically came for free with them.

If you find any issues report them on github

Usage example

var ram = new byte[65536];

// Load a ROM image
Array.Clear(ram, 0, ram.Length);
Array.Copy(File.ReadAllBytes("48.rom"), ram, 16384);

// Ports is something you supply to emulate I/O ports
var ports = new SamplePorts();

// Set up memory layout
var myz80 = new z80(new Memory(ram, 16384), ports);

// Run
while (!myz80.Halted)
{
    myz80.Parse();
}

// Show the registers
Console.WriteLine(myz80.DumpState());

Plans for the future

  • Finish the undocumented features
  • A full z80 assembler and disassembler

Bibliography

The following resources have been useful documentation:

License

The project is open source licensed under a BSD 3 clause license.

(discuss on HN)


Hi, I'm Marco Cecconi. I am the founder of Intelligent Hack, developer, hacker, blogger, conference lecturer. Bio: ex Stack Overflow core team, ex Toptal EM.

Read more

Newest Posts

What can Stack Overflow learn from ChatGPT?

Stack Overflow could benefit from adopting a using conversational AI to provide specific answers

Read more
Fan mail

Multiple people with my name use my email address and I can read their email, chaos ensues!

Read more
Intelligent Trip

After years of building, our top-notch consultancy to help start-ups and scale-ups create great, scalable products, I think it is high time I added an update to how it is going and what's next for us.

Read more
Guest blog: Building, in partnership with communities by Shog9

A lesson in building communities by Stack Overflow's most prominent community manager emeritus, Shog9

Read more
Can you migrate a company from on-premise to remote-only?

Some lessons learned over the past 8 years of remote work in some of the best remote companies on the planet

Read more

Gleanings

No, I Don’t Want to Subscribe to Your Newsletter
The Bureau of Programming • Feb 25, 2017

No, I don’t want to subscribe to your newsletter. No, I don’t want to complete a short survey. And no, I don’t want to become a member.

Read more…