protected void AssertFlagsFalse(CPUResults results)
 {
     Assert.False(results.Flags.Sign);
     Assert.False(results.Flags.Zero);
     Assert.False(results.Flags.HalfCarry);
     Assert.False(results.Flags.ParityOverflow);
     Assert.False(results.Flags.Subtract);
     Assert.False(results.Flags.Carry);
 }
 protected void AssertFlagsSame(CPUConfig initialState, CPUResults results)
 {
     Assert.Equal(initialState.Flags.Sign, results.Flags.Sign);
     Assert.Equal(initialState.Flags.Zero, results.Flags.Zero);
     Assert.Equal(initialState.Flags.HalfCarry, results.Flags.HalfCarry);
     Assert.Equal(initialState.Flags.ParityOverflow, results.Flags.ParityOverflow);
     Assert.Equal(initialState.Flags.Subtract, results.Flags.Subtract);
     Assert.Equal(initialState.Flags.Carry, results.Flags.Carry);
 }
        /**
         * Used to start a unit test run with the given CPU instance. The instance is expected to
         * have a memory implementation loaded with a ROM and registers (program counter etc) set
         * to appropriate values. After the execution completes, a results object will be returned
         * which contains the state of the CPU that can be used for assertions.
         */
        protected CPUResults Execute(CPU cpu)
        {
            // Record the number of iterations (instructions), CPU cycles, and the address of the
            // program counter after each instruction is executed. This allows tests to assert each
            // of these values in addition to the CPU state.
            var iterations  = 0;
            var cycles      = 0;
            var pcAddresses = new List <UInt16>();

            while (!cpu.Halted)
            {
                // Ensure we don't have a run away program.
                if (iterations > 100)
                {
                    throw new Exception("More than 100 iterations occurred.");
                }

                pcAddresses.Add(cpu.Registers.PC);

                cycles += cpu.Step();

                iterations++;
            }

            // HACK: Originally the 8080 implementation did not increment the PC after a HALT.
            // This was copied forward to the Z80 codebase, and all of the existing unit tests
            // assert this value after a HALT. However, the Z80 actually increments the PC after
            // a HALT. I don't want to fix hundreds of tests, so I'm hacking this here...
            cpu.Registers.PC = (UInt16)(cpu.Registers.PC - Opcodes.HALT.Size);

            // Return the state of the CPU so tests can do verification.

            var results = new CPUResults()
            {
                Iterations = iterations,
                Cycles     = cycles,
                ProgramCounterAddresses = pcAddresses,
                Memory            = (cpu.Memory as SimpleMemory).Memory,
                Registers         = cpu.Registers,
                Flags             = cpu.Flags,
                ProgramCounter    = cpu.Registers.PC,
                StackPointer      = cpu.Registers.SP,
                InterruptsEnabled = cpu.InterruptsEnabled,
                InterruptsEnabledPreviousValue = cpu.InterruptsEnabledPreviousValue,
                InterruptMode = cpu.InterruptMode,
            };

            return(results);
        }