Click here to Skip to main content
15,885,435 members
Articles / All Topics

Z80 CPU - A New Project

Rate me:
Please Sign up or sign in to vote.
4.80/5 (2 votes)
26 Apr 2016CPOL2 min read 11.7K   2   9
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.

C#
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:

C#
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.

 

This article was originally posted at http://www.jflanagan.co.uk/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFinished Pin
jhuey6425-Jun-16 7:15
jhuey6425-Jun-16 7:15 
AnswerRe: Finished Pin
James Flanagan28-Jun-16 10:08
James Flanagan28-Jun-16 10:08 
QuestionReally - Z80? Pin
Sean McPoland13-May-16 22:02
Sean McPoland13-May-16 22:02 
While I understand the Z80 was a popular chip, personally I preferred the 6502. Popular with gaming consoles of the day (Galaxians, Space Invaders et al.) and the precursor to real home computers, the Commodore PET.

The only downside of course is that it made Apple...

I got my hands on my first one in 1977 when I built the schools first computer as part of a physics project.

Keep these articles coming, it's like going home.

regards
QuestionPerformance Pin
jpmik8-May-16 7:05
jpmik8-May-16 7:05 
AnswerRe: Performance Pin
James Flanagan11-May-16 6:06
James Flanagan11-May-16 6:06 
QuestionFun Pin
Herman Bakker27-Apr-16 22:55
Herman Bakker27-Apr-16 22:55 
GeneralZ80 emulation Pin
Member 990203627-Apr-16 7:12
Member 990203627-Apr-16 7:12 
PraiseZ80 Pin
SteveHolle26-Apr-16 9:43
SteveHolle26-Apr-16 9:43 
QuestionNice Pin
CarelAgain26-Apr-16 5:24
professionalCarelAgain26-Apr-16 5:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.