Z80 CPU - A New Project






4.80/5 (2 votes)
Z80 CPU - A New Project
Inspired by Eric Lippert's series on creating a Z-Machine interpreter, I decided to create a Z80 CPU Emulator and write about it.
This will very much be a 'I'm just going to do it' and then learn from it, so expect me to make some very silly stupid mistakes as well as going back on decisions that I have made in the past.
I am in the enviable position of having no deadline and doing this just for the fun of it, so I will probably spend a lot of time re factoring; to make the code neater and easier to understand.
A CPU performs a set of instructions that have been supplied to it. Each instruction is specified by an instruction code specified in a byte; this is known as an opcode.
An opcode may require a number of operands, which supply extra information to the opcode. In the case of the Z80 CPU, an opcode can have 0, 1 or 2 operands each of which are also a byte.
There is a lot more to emulating a CPU than just performing operations; things such as timing, interrupts and pin usage needs to be configured but I'm going to start with operations and work my way up.
To begin with, I need to define some classes.
Since all of the opcodes and operations are definable as bytes, I could use the built in byte class but I don't really want to do that. So instead, I am going to define my own ZByte
class which is a collection of 8 bits.
So I need a Bit
which I am going to define as an enum
.
public enum Bit
{
Zero,
One
}
public class ZByte
{
private Bit[] _bits = new Bit[8];
public Bit this[ByteBitLocation location]
{
get
{
return _bits[(int)location];
}
set
{
_bits[(int)location] = value;
}
}
/* Other methods */
}
With ByteBitLocation
being defined as:
public enum ByteBitLocation : int
{
Zero = 0,
One = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7
}
There is an argument that this is more verbose than required and that using integers would be simpler and easier, but I want to take advantage of compile time checking as much as possible, so that means not using primitive types where possible.
Next week, we will start on our first operation.