var ram = new byte[65536];
// Load a ROM image
Array.Clear(ram, 0, ram.Length);
Array.Copy(File.ReadAllBytes("48.rom"), ram, 16384);
// Ports is something you supply to emulate I/O ports
var ports = new SamplePorts();
// Set up memory layout
var myZ80 = new Z80(new Memory(ram, 16384), ports);
// Run
while (!myZ80.Halted)
{
myZ80.Parse();
}
// Show the registers
Console.WriteLine(myZ80.DumpState());public void Main()
{
var asm = new byte[]{ 0x21, 0x00, 0x00 }; // LD HL, $0000
var z80 = new z80(asm);
z80.Parse();
z80.DumpState();
}[Test]
public void DoesLDHLWork()
{
var asm = new byte[]{ 0x21, 0x00, 0x00, 0x76 }; // LD HL, $0000 ; HALT
var testSystem = new TestSystem(asm);
testSystem.Run();
Assert.AreEqual(0, testSystem.HL);
}
[Test]
public void DoesLDIXWork()
{
var asm = new byte[]{ 0xDD, 0x21, 0x00, 0x00, 0x76 }; // LD IX, $0000 ; HALT
var testSystem = new TestSystem(asm);
testSystem.Run();
Assert.AreEqual(0, testSystem.IX);
}
// Moar[Test]
public void Test_LD_HL_nn()
{
asm.LoadReg16Val(2, 0x1942);
asm.Halt();
en.Run();
Assert.AreEqual(asm.Position, en.PC);
Assert.AreEqual(0x19, en.H);
Assert.AreEqual(0x42, en.L);
}
[Test]
public void Test_LD_DE_nn()
{
asm.LoadReg16Val(1, 0x1942);
asm.Halt();
en.Run();
Assert.AreEqual(asm.Position, en.PC);
Assert.AreEqual(0x19, en.D);
Assert.AreEqual(0x42, en.E);
}[Test]
[TestCase(0, 0x44, 0x11)]
[TestCase(0, 0x44, 0x0F)]
[TestCase(0, 0x44, 0xFF)]
[TestCase(0, 0x44, 0x01)]
[TestCase(0, 0xF4, 0x11)]
// Many, many more test cases
public void Test_ADD_A_r(byte reg, byte val, byte val2)
{
asm.LoadRegVal(7, val);
asm.LoadRegVal(reg, val2);
asm.AddAReg(reg);
asm.Halt();
en.Run();
Assert.AreEqual(asm.Position, en.PC);
var trueSum = (val + val2);
var byteSum = trueSum % 256;
var sbyteSum = (sbyte)byteSum;
Assert.AreEqual(byteSum, en.A);
Assert.AreEqual(sbyteSum < 0, en.FlagS, "Flag S contained the wrong value");
Assert.AreEqual(en.A == 0x00, en.FlagZ, "Flag Z contained the wrong value");
Assert.AreEqual((0x0F & val2 + 0x0F & val) > 0x0F, en.FlagH, "Flag H contained the wrong value");
// if both operands are positive and result is negative or if both are negative and result is positive
var overflow = (val < 0x7F == val2 < 0x7F) && (val < 0x7F == sbyteSum < 0);
Assert.AreEqual(overflow, en.FlagP, "Flag P contained the wrong value");
Assert.AreEqual(trueSum > 0xFF, en.FlagC, "Flag C contained the wrong value");
}z80 - a Z80 emulator that works in real time written in C#.
Z80 Emulator
Z80 Assembler backend
2270 tests (98% coverage)
https://github.com/sklivvz/z80