The first operation I want to tackle in this series is the 'Load a value into Memory' operation.
The reason I picked this operation is because I will be using test driven development and this is the easiest operation to implement.
My first test is:
[TestMethod]
public void SavingConstantToMemoryLocation_0h()
{
var memory = new LastInputMemory();
var cpu = new CPU(memory);
var valueToLoad = new ZByte(Bit.Zero, Bit.Zero, Bit.One,
Bit.Zero, Bit.Zero, Bit.Zero, Bit.Zero, Bit.One);
cpu.PerformInstruction(LoadCommand.ConstantIntoMemory, valueToLoad, null);
Assert.AreEqual(valueToLoad, memory.Value);
}
LastInputMemory
is just a mock class for the IMemory
Interface:
public interface IMemory
{
void SetValue(SixteenBitAddress address, ZByte value);
ZByte GetValue(SixteenBitAddress address);
}
And the SixteenBitAddress
is just a collection of Bit
s with a length of 16, similar to the ZByte
class.
Naturally, when I first run this test, it fails; so it's time to write some production code.
To pass this test, the cpu.PerformInstruction
method just needs to take the valueToLoad
and pass it into the Memory.SetValue
.
But the next test for this operation requires a little bit more.
In this test, I want to save it to a specific memory location, which means I need to define which memory location I want to save it to.
Although you might think it would be a simple case of adding the address as an operand, that is not the case.
Instead, the memory location is defined based on the value of two of the registers in the CPU.
A register is a physical part of the CPU that can contain a series of Bit
s; since the Z80 CPU is an 8 bit CPU, it has 8 Bit
Registers (it also has 16 Bit
registers).
The Z80 CPU has 10 8-bit registers that are available at any given time. Each of the registers has a name.
Accumulator, Flags, B, C, D, E, H, L, Interrupt Vector (I) and Memory Refresh (R).
Sounds like an Enum
to me.
public enum EightBitRegisterNames
{
Accumulator,
Flags,
B,
C,
D,
E,
H,
L,
Interrupt,
Refresh
}
Now that I have the register names, I want to add them to the CPU. So I have added them as a Dictionary
into the CPU with the register Name being the Key.
However, now we reach a problem, we have no way of getting values into the registers so we can't define an address. So we are going to have to take a break from this operation and come back to it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.