Click here to Skip to main content
15,867,594 members
Articles / All Topics

Z80 CPU - 16 Bit Registers

Rate me:
Please Sign up or sign in to vote.
4.67/5 (4 votes)
2 Jun 2016CPOL1 min read 8.2K   3
The Z80 CPU contains 16 bit registers which come in two different types.

Most of our work so far has been using 8 bit registers but the Z80 CPU also contains 16 bit registers.

In fact, it contains 7 16 bit registers that are available at any one time.

It contains the IX and IY registers which are used for indexing into tables.

It contains the Stack Pointer which contains the address of the top of the stack (we will get into this more in a future post).

And it contains the Program Counter which is the address of the currently executing instruction.

But it also contains faux 16 bit registers made up of the 8 bit registers.

BC, DE and HL can each be treated as a 16 bit register despite actually being two 8 bit registers.

The first register contains the high order byte and the second contains the low order byte. So when loading a value into the BC register, the high order byte will be stored in the B register and the low order byte will be stored in the low order byte.

So from a coding point of view, I decided to treat these 16 bit registers as separate things. So I defined an interface to represent everything I needed out of a 16 bit register...

C++
interface I16BitRegister
   {
       ZByte HighOrderByte { get; set; }

       ZByte LowOrderByte { get; set; }
   }

...and then created two concrete classes for the two types of 16 bit register.

C++
public class SixteenBitRegister : I16BitRegister
    {
        public SixteenBitRegister()
        {
            HighOrderByte = new ZByte();
            LowOrderByte = new ZByte();
        }


        public ZByte HighOrderByte
        {
            get; set;
        }

        public ZByte LowOrderByte
        {
            get; set;
        }
    }

and:

C++
class Double8BitRegister : I16BitRegister
   {
       private EightBitRegister _firstRegister;
       private EightBitRegister _secondRegister;


       public Double8BitRegister(EightBitRegister first, EightBitRegister second)
       {
           _firstRegister = first;
           _secondRegister = second;
       }

       public ZByte HighOrderByte
       {
           get
           {
               return _firstRegister.ToByte();
           }

           set
           {
               _firstRegister.FromByte(value);
           }
       }

       public ZByte LowOrderByte
       {
           get
           {
               return _secondRegister.ToByte();
           }

           set
           {
               _secondRegister.FromByte(value);
           }
       }
   }

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

 
QuestionZ80 still alive ? Pin
Member 112713055-Jun-16 22:19
Member 112713055-Jun-16 22:19 
GeneralUndocumented Z80 instructions Pin
Matthew Barnett3-Jun-16 10:31
Matthew Barnett3-Jun-16 10:31 
GeneralRe: Undocumented Z80 instructions Pin
James Flanagan6-Jun-16 4:46
James Flanagan6-Jun-16 4:46 

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.