Z80 CPU - 16 Bit Registers






4.67/5 (4 votes)
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...
interface I16BitRegister
{
ZByte HighOrderByte { get; set; }
ZByte LowOrderByte { get; set; }
}
...and then created two concrete classes for the two types of 16 bit register.
public class SixteenBitRegister : I16BitRegister
{
public SixteenBitRegister()
{
HighOrderByte = new ZByte();
LowOrderByte = new ZByte();
}
public ZByte HighOrderByte
{
get; set;
}
public ZByte LowOrderByte
{
get; set;
}
}
and:
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);
}
}
}