Example #1
0
        public void TestLd_addrHL_n()
        {
            //setup
            byte   testValue   = 0x0E;
            ushort testAddress = 0xF1;
            var    prog        = new byte[256];

            prog[0] = (byte)OpCode.LD_addrHL_n;
            prog[1] = testValue;
            var mmu = new SimpleMmu(prog);
            var cpu = new Cpu(mmu);

            //put nonsense in target address
            mmu.WriteByte(testAddress, 0xFF);
            //set HL to address to be copied to
            cpu.HL = testAddress;

            //expected outcome
            var cpuExpected = cpu.CopyState();

            cpuExpected.Cycles         = 12;
            cpuExpected.ProgramCounter = 2;
            cpuExpected.Mmu.WriteByte(testAddress, testValue);

            //execute & validate
            cpu.Step();
            CpuHelpers.ValidateState(cpuExpected, cpu);
        }
Example #2
0
        public void TestLd_R_addrHL(ChangeType targetRegister, OpCode opCode)
        {
            //setup
            byte   testValue   = 0x0E;
            ushort testAddress = 0xF1;
            var    prog        = new byte[256];

            prog[0] = (byte)opCode; //LD r,(HL)
            var mmu = new SimpleMmu(prog);
            var cpu = new Cpu(mmu);

            //set value to be copied to target register in memory
            mmu.WriteByte(testAddress, testValue);
            //put address to be copied from into HL
            cpu.HL = testAddress;

            //put some nonsense in target register to make sure it changes
            //H/L are special cases as they affect the target address, so can't be altered
            if (targetRegister != ChangeType.H && targetRegister != ChangeType.L)
            {
                CpuHelpers.SetValue(cpu, targetRegister, 0xFF);
            }

            //expected outcome
            var cpuExpected = cpu.CopyState();

            cpuExpected.Cycles         = 8;
            cpuExpected.ProgramCounter = 1;
            CpuHelpers.SetValue(cpuExpected, targetRegister, testValue);

            //execute & validate
            cpu.Step();
            CpuHelpers.ValidateState(cpuExpected, cpu);
        }
Example #3
0
        public void TestSetBit(ChangeType register, byte registerInitialVal, byte bitToSet, byte expectedRegisterVal)
        {
            //build opcode - 0b11<reg><bit>
            var opCode = 0b1100_0000 | (bitToSet << 3) | (byte)register;
            //setup
            var mem = new byte[256];

            mem[0] = 0xCB;
            mem[1] = (byte)opCode;
            var aHL = (byte)0xF1;
            var mmu = new SimpleMmu(mem);
            var cpu = new Cpu(mmu);

            cpu.F = 0;
            if (register == ChangeType.aHL)
            {
                cpu.HL = aHL;
                mmu.WriteByte(aHL, registerInitialVal);
            }
            else
            {
                CpuHelpers.SetValue(cpu, register, registerInitialVal);
            }

            //expected outcome
            var cpuExpected = cpu.CopyState();

            cpuExpected.Cycles         = (uint)(register == ChangeType.aHL ? 12 : 8);
            cpuExpected.ProgramCounter = 2;
            cpuExpected.ZeroFlag       = false;
            cpuExpected.HalfCarryFlag  = false;
            cpuExpected.SubFlag        = false;
            cpuExpected.CarryFlag      = false;
            if (register == ChangeType.aHL)
            {
                cpuExpected.Mmu.WriteByte(aHL, expectedRegisterVal);
            }
            else
            {
                CpuHelpers.SetValue(cpuExpected, register, expectedRegisterVal);
            }


            //execute & validate
            cpu.Step();
            CpuHelpers.ValidateState(cpuExpected, cpu);
        }
Example #4
0
        public void TestRotate(byte opCode, ChangeType register, byte registerInitialVal, bool carryFlagInitialVal, byte expectedRegisterVal, bool expectedCarryFlag, bool expectedZeroFlag)
        {
            //setup
            var mem = new byte[256];

            mem[0] = 0xCB;
            mem[1] = (byte)(opCode | (byte)register); //build opcode - 0bcccc_crrr
            var aHL = (byte)0xF1;
            var mmu = new SimpleMmu(mem);
            var cpu = new Cpu(mmu);

            cpu.F         = 0;
            cpu.CarryFlag = carryFlagInitialVal;
            if (register == ChangeType.aHL)
            {
                cpu.HL = aHL;
                mmu.WriteByte(aHL, registerInitialVal);
            }
            else
            {
                CpuHelpers.SetValue(cpu, register, registerInitialVal);
            }

            //expected outcome
            var cpuExpected = cpu.CopyState();

            cpuExpected.Cycles         = (uint)(register == ChangeType.aHL ? 12 : 8);
            cpuExpected.ProgramCounter = 2;
            cpuExpected.ZeroFlag       = expectedZeroFlag;
            cpuExpected.HalfCarryFlag  = false;
            cpuExpected.SubFlag        = false;
            cpuExpected.CarryFlag      = expectedCarryFlag;
            if (register == ChangeType.aHL)
            {
                cpuExpected.Mmu.WriteByte(aHL, expectedRegisterVal);
            }
            else
            {
                CpuHelpers.SetValue(cpuExpected, register, expectedRegisterVal);
            }

            //execute & validate
            cpu.Step();
            CpuHelpers.ValidateState(cpuExpected, cpu);
        }
Example #5
0
        public void TestLd_addrHL_R(ChangeType sourceRegister, OpCode opCode)
        {
            //setup
            byte testValue = 0x0E;

            //H/L are special cases as they hold the address AND the value
            if (sourceRegister == ChangeType.H)
            {
                testValue = 0x01;
            }
            if (sourceRegister == ChangeType.L)
            {
                testValue = 0xF1;
            }
            ushort testAddress = 0x01F1;
            var    prog        = new byte[512];

            prog[0] = (byte)opCode; //LD r,(HL)
            var mmu = new SimpleMmu(prog);
            var cpu = new Cpu(mmu);

            //put nonsense in target address
            mmu.WriteByte(testAddress, 0xFF);

            //set value in register to be copied
            CpuHelpers.SetValue(cpu, sourceRegister, testValue);
            //set HL to address to be copied to
            cpu.HL = testAddress;

            //expected outcome
            var cpuExpected = cpu.CopyState();

            cpuExpected.Cycles         = 8;
            cpuExpected.ProgramCounter = 1;
            cpuExpected.Mmu.WriteByte(testAddress, testValue);

            //execute & validate
            cpu.Step();
            CpuHelpers.ValidateState(cpuExpected, cpu);
        }