Z80.NET is a Z80 processor simulator that can be used as the core component for developing computer emulators (see for example NestorMSX), or to exercise pieces of Z80 code in custom test code. It is written in C# targetting the .NET Framework 4 Client Profile.
If you like Z80.NET you may want to take a look at ZWatcher too.
var z80 = new Z80Processor();
z80.AutoStopOnRetWithStackEmpty = true;
var program = new byte[] {
0x3E, 0x07, //LD A,7
0xC6, 0x04, //ADD A,4
0x3C, //INC A
0xC9 //RET
};
z80.Memory.SetContents(0, program);
z80.Start();
Debug.Assert(z80.Registers.A == 12);
Debug.Assert(z80.TStatesElapsedSinceStart == 28);
For your convenience, you can add Z80.NET to your project as a NuGet package if you want. In that case you may want to take a look at the release notes.
- Create an instance of the Z80Processor class.
- Optionally, plug your own implementations of one or more of the dependencies.
- Configure your instance as appropriate.
- Optionally, register one or more interrupt sources.
- Optionally, capture the memory access events and/or the instruction execution events.
- Start the simulated processor execution by using one of the execution control methods.
- Execution will stop (and the execution method invoked will then return) when one of the execution stop conditions is met. You can then check the processor state and, if desired, resume execution.
Execution is completely synchronous: one single thread is used for everything, including firing events. As seen in the Hello World example, you just invoke one of the starting methods and wait until it returns (there are means to force this to happen, see the execution stop conditions). If you want some kind of multithreading, you'll have to implement it by yourself, I just tried to keep things simple. :-)
Interaction of the processor with the hosting code and the outside world (memory and ports) can be achieved by handling the class events, by plugging custom implementations of the dependencies, or both at the same time. Interrupts can be generated by using interrupt sources.
Z80.NET implements all the documented Z80 behavior, plus all the undocumented instructions and flag effects as per The undocumented Z80 documented except for the following:
- The bit 3 and 5 flags are not modified by the BIT instruction
- The H, C and P/V flags are not modified by the INI, INIR, IND, INDR, OUTI, OTIR, OUTD and OTDR instructions
The processor class passes the ZEXDOC test fully, and the ZEXALL test fully except for the BIT instruction. You can try these tests yourself by running the ZexallTest project.
The following resources have been used to develop this project:
- Z80 official user manual
- The undocumented Z80 documented by Sean Young.
- Z80 instructions table at ClrHome.org
- Z80 technical reference at WorldOfSpectrum.org
- Complete Z80 instruction set from ticalc.org. The instruction tables in the code were automatically generated from a modified version of this file.
...if you like this project please consider donating! My kids need moar shoes!
I'm Konamiman, the MSX freak. No more, no less.