public static void Check_Instruction_18_CALL_Address()
        {
            StringBuilder testProgram = new StringBuilder();
            testProgram.AppendLine("LD A,0");
            testProgram.AppendLine("CALL FUNC");
            testProgram.AppendLine("LD A,2");
            testProgram.AppendLine("ORG 100");
            testProgram.AppendLine("FUNC: LD A,1");
            testProgram.AppendLine("RET");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(5);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/CallAndReturn/Logs/Check_Instruction_18_CALL_Address"))
            {
                throw new Exception("Log compare failed");
            }
        }
        /// <summary>
        /// Utility method used to compare execution log with arithmetic operations results (8 bits arithmetic)
        /// </summary>
        private static void CompareExecutionResultsWithSample(string sampleProgramFileName, string sampleResultFileName, int instructionsPerLine, int accColumn)
        {
            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(sampleProgramFileName, Encoding.UTF8, false);

            Stream stream = PlatformSpecific.GetStreamForProjectFile(sampleResultFileName);
            using (StreamReader reader = new StreamReader(stream))
            {
                string line = null;
                while ((line = reader.ReadLine()) != null)
                {
                    testSystem.ExecuteInstructionCount(instructionsPerLine);

                    string[] columns = line.Split(';');
                    string AHexValue = columns[accColumn];
                    byte AByteValue = Convert.ToByte(AHexValue, 16);
                    string FBinValue = columns[accColumn + 1];
                    byte FByteValue = Convert.ToByte(FBinValue, 2);

                    if (testSystem.CPU.A != AByteValue)
                    {
                        throw new Exception(String.Format("Error line {0}, A = {1}", line, String.Format("{0:X}", testSystem.CPU.A)));
                    }
                    else if ((testSystem.CPU.F) != FByteValue)
                    {
                        throw new Exception(String.Format("Error line {0}, F = {1}", line, Convert.ToString(testSystem.CPU.F, 2)));
                    }
                }
            }
        }
        public static void Check_Instruction_65_LD_Register16_Register16()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "10 (4, 3, 3)"
            testProgram.AppendLine("LD HL,1547");
            // TStatesByMachineCycle = "1 (6)"
            testProgram.AppendLine("LD SP,HL");
            // TStatesByMachineCycle = "14 (4, 4, 3, 3)"
            testProgram.AppendLine("LD IY,3225");
            // TStatesByMachineCycle = "10 (4, 6)"
            testProgram.AppendLine("LD SP,IY");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(4);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/Load16Bit/Logs/Check_Instruction_65_LD_Register16_Register16"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_125_SBC_Register_Register16Address()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,10");
            // TStatesByMachineCycle = "10 (4, 3, 3)"
            testProgram.AppendLine("LD HL,100");
            // TStatesByMachineCycle = "10 (4, 3, 3)"
            testProgram.AppendLine("LD (HL),5");
            // TStatesByMachineCycle = 1 (4)
            testProgram.AppendLine("SCF");
            // TStatesByMachineCycle = 7 (4, 3)
            testProgram.AppendLine("SBC A,(HL)");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(5);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("ALU/Arithmetic_8bits/Logs/Check_Instruction_125_SBC_Register_Register16Address"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_FetchOpcode(bool simulateSlowMemoryAndDevices)
        {
            StringBuilder testProgram = new StringBuilder();
            testProgram.AppendLine("LD A,B");
            testProgram.AppendLine("LD R,A");

            TestSystem testSystem = new TestSystem(simulateSlowMemoryAndDevices);
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
               CPUStateLogger.CPUStateElements.InternalState |
               CPUStateLogger.CPUStateElements.Registers |
               CPUStateLogger.CPUStateElements.Buses |
               CPUStateLogger.CPUStateElements.ControlPins,
               new Z80CPU.LifecycleEventType[] {
                    Z80CPU.LifecycleEventType.HalfTState,
                    Z80CPU.LifecycleEventType.InstructionEnd,
                    Z80CPU.LifecycleEventType.InstructionStart,
                    Z80CPU.LifecycleEventType.MachineCycleEnd,
                    Z80CPU.LifecycleEventType.MachineCycleStart
                });
            testSystem.ExecuteInstructionCount(2);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("MachineCycles/Logs/Check_FetchOpcode" + GetSlowSuffix(simulateSlowMemoryAndDevices)))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_100_RL_Register()
        {
            StringBuilder testProgram = new StringBuilder();
            testProgram.AppendLine("LD B,00000001B");
            testProgram.AppendLine("RL B");
            testProgram.AppendLine("RL B");
            testProgram.AppendLine("RL B");
            testProgram.AppendLine("RL B");
            testProgram.AppendLine("RL B");
            testProgram.AppendLine("RL B");
            testProgram.AppendLine("RL B");
            testProgram.AppendLine("RL B");
            testProgram.AppendLine("RL B");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(10);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("ALU/RotateAndShift/Logs/Check_Instruction_100_RL_Register"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_26_CPDR()
        {
            StringBuilder testProgram = new StringBuilder();
            testProgram.AppendLine("LD HL,100");
            testProgram.AppendLine("LD (HL),1");
            testProgram.AppendLine("LD HL,101");
            testProgram.AppendLine("LD (HL),2");
            testProgram.AppendLine("LD HL,102");
            testProgram.AppendLine("LD (HL),3");
            testProgram.AppendLine("LD BC,3");
            testProgram.AppendLine("LD A,5");
            testProgram.AppendLine("CPDR");
            testProgram.AppendLine("LD HL,102");
            testProgram.AppendLine("LD BC,3");
            testProgram.AppendLine("LD A,2");
            testProgram.AppendLine("CPDR");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(17);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/ExchangeBlockTransferAndSearch/Logs/Check_Instruction_26_CPDR"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_55_JP_Register16Address()
        {
            StringBuilder testProgram = new StringBuilder();
            testProgram.AppendLine("LD A,0");
            testProgram.AppendLine("LD HL,100");
            testProgram.AppendLine("JP (HL)");
            testProgram.AppendLine("ORG 100");
            testProgram.AppendLine("LD IX,110");
            testProgram.AppendLine("JP (IX)");
            testProgram.AppendLine("ORG 110");
            testProgram.AppendLine("LD IY,120");
            testProgram.AppendLine("JP (IY)");
            testProgram.AppendLine("ORG 120");
            testProgram.AppendLine("LD A,1");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(8);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/Jump/Logs/Check_Instruction_55_JP_Register16Address"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_20_CCF()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "1 (4)"
            testProgram.AppendLine("CCF");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,0");
            // TStatesByMachineCycle = "1 (4)"
            testProgram.AppendLine("CCF");
            // TStatesByMachineCycle = "1 (4)"
            testProgram.AppendLine("CCF");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(4);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("ALU/ArithmeticGeneralPurpose/Logs/Check_Instruction_20_CCF"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_60_LD_Register_Register()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD C,10");
            // TStatesByMachineCycle = "1 (4)"
            testProgram.AppendLine("LD D,C");
            // TStatesByMachineCycle = "8 (4,4)"
            testProgram.AppendLine("LD IXh,D");
            // TStatesByMachineCycle = "8 (4,4)"
            testProgram.AppendLine("LD E,IXh");
            // TStatesByMachineCycle = "8 (4,4)"
            testProgram.AppendLine("LD IXl,IXh");
            // TStatesByMachineCycle = "11 (4,4,3)"
            testProgram.AppendLine("LD IYh,32");
            // TStatesByMachineCycle = "8 (4,4)"
            testProgram.AppendLine("LD IYl,IYh");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,55");
            // TStatesByMachineCycle = "9 (4, 5)"
            testProgram.AppendLine("LD R,A");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,0");
            // TStatesByMachineCycle = "9 (4, 5)"
            testProgram.AppendLine("LD A,R");
            // TStatesByMachineCycle = "9 (4, 5)"
            testProgram.AppendLine("LD A,I");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(12);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/Load_8Bit/Logs/Check_Instruction_60_LD_Register_Register"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_122_RST_ResetAddress()
        {
            StringBuilder testProgram = new StringBuilder();
            testProgram.AppendLine("LD A,0");
            testProgram.AppendLine("RST 08H");
            testProgram.AppendLine("ORG 08H");
            testProgram.AppendLine("LD A,1");
            testProgram.AppendLine("RST 10H");
            testProgram.AppendLine("ORG 10H");
            testProgram.AppendLine("LD A,2");
            testProgram.AppendLine("RST 18H");
            testProgram.AppendLine("ORG 18H");
            testProgram.AppendLine("LD A,3");
            testProgram.AppendLine("RST 20H");
            testProgram.AppendLine("ORG 20H");
            testProgram.AppendLine("LD A,4");
            testProgram.AppendLine("RST 28H");
            testProgram.AppendLine("ORG 28H");
            testProgram.AppendLine("LD A,5");
            testProgram.AppendLine("RST 30H");
            testProgram.AppendLine("ORG 30H");
            testProgram.AppendLine("LD A,6");
            testProgram.AppendLine("RST 38H");
            testProgram.AppendLine("ORG 38H");
            testProgram.AppendLine("LD A,7");
            testProgram.AppendLine("RST 00H");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(17);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/CallAndReturn/Logs/Check_Instruction_122_RST_ResetAddress"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_59_LD_Register_Number8()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD H,30");
            // TStatesByMachineCycle = "11 (4,4,3)"
            testProgram.AppendLine("LD IXl,55");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(2);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/Load_8Bit/Logs/Check_Instruction_59_LD_Register_Number8"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_131_SET_Bit_IndexedAddress()
        {
            StringBuilder testProgram = new StringBuilder();
            testProgram.AppendLine("LD IY,100");
            testProgram.AppendLine("LD (IY+1),0");
            testProgram.AppendLine("SET 7,(IY+1)");
            testProgram.AppendLine("LD A,(IY+1)");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(4);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("ALU/BitSetResetAndTest/Logs/Check_Instruction_131_SET_Bit_IndexedAddress"))
            {
                throw new Exception("Log compare failed");
            }
        }
        /// <summary>
        /// Utility method used to compare execution log with arithmetic operations results (16 bits arithmetic)
        /// </summary>
        private static void Compare16bitsExecutionResultsWithSample(string sampleProgramFileName, string sampleResultFileName, int instructionsPerLine)
        {
            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(sampleProgramFileName, Encoding.UTF8, false);

            Stream stream = PlatformSpecific.GetStreamForProjectFile(sampleResultFileName);
            using (StreamReader reader = new StreamReader(stream))
            {
                string line = null;
                while ((line = reader.ReadLine()) != null)
                {
                    testSystem.ExecuteInstructionCount(instructionsPerLine);

                    string[] columns = line.Split(';');
                    string HHexValue = columns[3];
                    byte HByteValue = Convert.ToByte(HHexValue, 16);
                    string LHexValue = columns[4];
                    byte LByteValue = Convert.ToByte(LHexValue, 16);
                    string FBinValue = columns[5];
                    byte FByteValue = Convert.ToByte(FBinValue, 2);

                    if (testSystem.CPU.L != LByteValue)
                    {
                        throw new Exception(String.Format("Error line {0}, L = {1}", line, String.Format("{0:X}", testSystem.CPU.L)));
                    }
                    else if (testSystem.CPU.H != HByteValue)
                    {
                        throw new Exception(String.Format("Error line {0}, H = {1}", line, String.Format("{0:X}", testSystem.CPU.H)));
                    }
                    // Ignore undocumented flags 3 and 5 : 11010111 = 215
                    else if ((testSystem.CPU.F & 215) != (FByteValue & 215))
                    {
                        throw new Exception(String.Format("Error line {0}, F = {1}", line, Convert.ToString(testSystem.CPU.F, 2)));
                    }
                }
            }
        }
        public static void Check_Instruction_36_DJNZ_RelativeDisplacement()
        {
            StringBuilder testProgram = new StringBuilder();
            testProgram.AppendLine("LD A,0");
            testProgram.AppendLine("LD B,3");
            testProgram.AppendLine("INC A");
            testProgram.AppendLine("DJNZ -1");
            testProgram.AppendLine("LD A,0FFH");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(9);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/Jump/Logs/Check_Instruction_36_DJNZ_RelativeDisplacement"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_39_EX_Register16Address_Register16()
        {
            StringBuilder testProgram = new StringBuilder();
            testProgram.AppendLine("LD BC,1000");
            testProgram.AppendLine("LD HL,2000");
            testProgram.AppendLine("LD IX,3000");
            testProgram.AppendLine("LD IY,4000");
            testProgram.AppendLine("PUSH BC");
            testProgram.AppendLine("EX (SP),HL");
            testProgram.AppendLine("EX (SP),IX");
            testProgram.AppendLine("EX (SP),IY");
            testProgram.AppendLine("POP BC");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(9);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/ExchangeBlockTransferAndSearch/Logs/Check_Instruction_39_EX_Register16Address_Register16"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_72_LD_IndexedAddress_Register()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "14 (4, 4, 3, 3)"
            testProgram.AppendLine("LD IX,110");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD B,20");
            // TStatesByMachineCycle = "19 (4, 4, 3,5,3)"
            testProgram.AppendLine("LD (IX-10),B");
            // TStatesByMachineCycle = "13 (4, 3, 3, 3)"
            testProgram.AppendLine("LD A,(100)");
            // TStatesByMachineCycle = "14 (4, 4, 3, 3)"
            testProgram.AppendLine("LD IY,95");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD L,30");
            // TStatesByMachineCycle = "19 (4, 4, 3,5,3)"
            testProgram.AppendLine("LD (IY+5),L");
            // TStatesByMachineCycle = "13 (4, 3, 3, 3)"
            testProgram.AppendLine("LD A,(100)");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(8);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/Load_8Bit/Logs/Check_Instruction_72_LD_IndexedAddress_Register"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_82_OR_IndexedAddress()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,10101010B");
            // TStatesByMachineCycle = "14 (4, 4, 3, 3)"
            testProgram.AppendLine("LD IX,97");
            // TStatesByMachineCycle = "19 (4, 4, 3,5,3)"
            testProgram.AppendLine("LD (IX+3),10001001B");
            // TStatesByMachineCycle = "19 (4, 4, 3, 5, 3)"
            testProgram.AppendLine("OR (IX+3)");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(4);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("ALU/Arithmetic_8bits/Logs/Check_Instruction_82_OR_IndexedAddress"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_7_ADD_Register_Register()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,5");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD B,252");
            // TStatesByMachineCycle = 1 (4)
            testProgram.AppendLine("ADD A,B");
            // TStatesByMachineCycle = "11 (4,4,3)"
            testProgram.AppendLine("LD IXh,13");
            // TStatesByMachineCycle = "8 (4, 4)"
            testProgram.AppendLine("ADD A,IXh");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(5);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("ALU/Arithmetic_8bits/Logs/Check_Instruction_7_ADD_Register_Register"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_StackMemoryReadAndWrite()
        {
            StringBuilder testProgram = new StringBuilder();
            testProgram.AppendLine("LD A,10");
            testProgram.AppendLine("PUSH AF");
            testProgram.AppendLine("POP BC");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
               CPUStateLogger.CPUStateElements.InternalState |
               CPUStateLogger.CPUStateElements.Registers |
               CPUStateLogger.CPUStateElements.Buses |
               CPUStateLogger.CPUStateElements.ControlPins,
               new Z80CPU.LifecycleEventType[] {
                    Z80CPU.LifecycleEventType.HalfTState,
                    Z80CPU.LifecycleEventType.InstructionEnd,
                    Z80CPU.LifecycleEventType.InstructionStart,
                    Z80CPU.LifecycleEventType.MachineCycleEnd,
                    Z80CPU.LifecycleEventType.MachineCycleStart
                });
            testSystem.ExecuteInstructionCount(3);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("MachineCycles/Logs/Check_StackMemoryReadAndWrite"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_127_SBC_Register16_Register16()
        {
            string sampleProgramFileName = "ALU/Arithmetic16bits/Samples/testsbc16.asm";
            string sampleResultFileName = "ALU/Arithmetic16bits/Samples/resultsbc16.csv";
            Compare16bitsExecutionResultsWithSample(sampleProgramFileName, sampleResultFileName, 4);

            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "10 (4,3,3)"
            testProgram.AppendLine("LD BC,1");
            // TStatesByMachineCycle = "10 (4,3,3)"
            testProgram.AppendLine("LD DE,2");
            // TStatesByMachineCycle = "10 (4,3,3)"
            testProgram.AppendLine("LD HL,10");
            // TStatesByMachineCycle = "10 (4,3,3)"
            testProgram.AppendLine("LD SP,4");
            // TStatesByMachineCycle = "4"
            testProgram.AppendLine("SCF");
            // TStatesByMachineCycle = "11 (4,4,3)"
            testProgram.AppendLine("SBC HL,BC");
            // TStatesByMachineCycle = "4"
            testProgram.AppendLine("SCF");
            // TStatesByMachineCycle = "11 (4,4,3)"
            testProgram.AppendLine("SBC HL,DE");
            // TStatesByMachineCycle = "4"
            testProgram.AppendLine("SCF");
            // TStatesByMachineCycle = "11 (4,4,3)"
            testProgram.AppendLine("SBC HL,HL");
            // TStatesByMachineCycle = "4"
            testProgram.AppendLine("SCF");
            // TStatesByMachineCycle = "11 (4,4,3)"
            testProgram.AppendLine("SBC HL,SP");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(12);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("ALU/Arithmetic16bits/Logs/Check_Instruction_127_SBC_Register16_Register16"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_66_LD_Register16_Address()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,12");
            // TStatesByMachineCycle = "13 (4, 3, 3, 3)"
            testProgram.AppendLine("LD (4253),A");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,120");
            // TStatesByMachineCycle = "13 (4, 3, 3, 3)"
            testProgram.AppendLine("LD (4254),A");
            // TStatesByMachineCycle = "16 (4, 3, 3, 3, 3)"
            testProgram.AppendLine("LD HL,(4253)");
            // TStatesByMachineCycle = "20 (4, 4, 3, 3, 3, 3)"
            testProgram.AppendLine("LD DE,(4253)");
            // TStatesByMachineCycle = "20 (4, 4, 3, 3, 3, 3)"
            testProgram.AppendLine("LD IX,(4253)");
            // TStatesByMachineCycle = "20 (4, 4, 3, 3, 3, 3)"
            testProgram.AppendLine("LD SP,(4253)");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(8);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/Load16Bit/Logs/Check_Instruction_66_LD_Register16_Address"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_42_IM_InterruptMode()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "8 (4,4)"
            testProgram.AppendLine("IM 2");
            // TStatesByMachineCycle = "8 (4,4)"
            testProgram.AppendLine("IM 1");
            // TStatesByMachineCycle = "8 (4,4)"
            testProgram.AppendLine("IM 0");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(3);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/CPUControl/Logs/Check_Instruction_42_IM_InterruptMode"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_91_PUSH_Register16()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,21");
            // TStatesByMachineCycle = "10 (4, 3, 3)"
            testProgram.AppendLine("LD BC,4370");
            // TStatesByMachineCycle = "14 (4, 4, 3, 3)"
            testProgram.AppendLine("LD IY,4884");
            // TStatesByMachineCycle = "11 (5, 3, 3)"
            testProgram.AppendLine("PUSH AF");
            // TStatesByMachineCycle = "11 (5, 3, 3)"
            testProgram.AppendLine("PUSH BC");
            // TStatesByMachineCycle = "15 (4, 5, 3, 3)"
            testProgram.AppendLine("PUSH IY");
            // TStatesByMachineCycle = "10 (4, 3, 3)",
            testProgram.AppendLine("POP DE");
            // TStatesByMachineCycle = "10 (4, 3, 3)",
            testProgram.AppendLine("POP DE");
            // TStatesByMachineCycle = "10 (4, 3, 3)",
            testProgram.AppendLine("POP DE");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(9);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/Load16Bit/Logs/Check_Instruction_91_PUSH_Register16"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_90_POP_Register16()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,17");
            // TStatesByMachineCycle = "13 (4, 3, 3, 3)"
            testProgram.AppendLine("LD (4253),A");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,18");
            // TStatesByMachineCycle = "13 (4, 3, 3, 3)"
            testProgram.AppendLine("LD (4252),A");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,19");
            // TStatesByMachineCycle = "13 (4, 3, 3, 3)"
            testProgram.AppendLine("LD (4251),A");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,20");
            // TStatesByMachineCycle = "13 (4, 3, 3, 3)"
            testProgram.AppendLine("LD (4250),A");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,21");
            // TStatesByMachineCycle = "13 (4, 3, 3, 3)"
            testProgram.AppendLine("LD (4249),A");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,22");
            // TStatesByMachineCycle = "13 (4, 3, 3, 3)"
            testProgram.AppendLine("LD (4248),A");
            // TStatesByMachineCycle = "10 (4, 3, 3)"
            testProgram.AppendLine("LD SP,4248");
            // TStatesByMachineCycle = "10 (4, 3, 3)",
            testProgram.AppendLine("POP AF");
            // TStatesByMachineCycle = "10 (4, 3, 3)",
            testProgram.AppendLine("POP HL");
            // TStatesByMachineCycle = "14 (4, 4, 3, 3)"
            testProgram.AppendLine("POP IX");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(16);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/Load16Bit/Logs/Check_Instruction_90_POP_Register16"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_63_LD_Register_IndexedAddress()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "14 (4, 4, 3, 3)"
            testProgram.AppendLine("LD IX,90");
            // TStatesByMachineCycle = "14 (4, 4, 3, 3)"
            testProgram.AppendLine("LD IY,105");
            // TStatesByMachineCycle = "10 (4,3,3)"
            testProgram.AppendLine("LD HL,100");
            // TStatesByMachineCycle = "10 (4, 3, 3)"
            testProgram.AppendLine("LD (HL),32");
            // TStatesByMachineCycle = "19 (4, 4, 3, 5, 3)"
            testProgram.AppendLine("LD A,(IX+10)");
            // TStatesByMachineCycle = "19 (4, 4, 3, 5, 3)"
            testProgram.AppendLine("LD D,(IY-5)");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(6);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/Load_8Bit/Logs/Check_Instruction_63_LD_Register_IndexedAddress"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_70_LD_Register16Address_Register()
        {
            StringBuilder testProgram = new StringBuilder();
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD E,30");
            // TStatesByMachineCycle = "10 (4,3,3)"
            testProgram.AppendLine("LD HL,100");
            // TStatesByMachineCycle = "7 (4, 3)"
            testProgram.AppendLine("LD (HL),E");
            // TStatesByMachineCycle = "13 (4, 3, 3, 3)"
            testProgram.AppendLine("LD A,(100)");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,40");
            // TStatesByMachineCycle = "10 (4,3,3)"
            testProgram.AppendLine("LD BC,110");
            // TStatesByMachineCycle = "7 (4, 3)"
            testProgram.AppendLine("LD (BC),A");
            // TStatesByMachineCycle = "7 (4,3)"
            testProgram.AppendLine("LD A,0");
            // TStatesByMachineCycle = "13 (4, 3, 3, 3)"
            testProgram.AppendLine("LD A,(110)");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(9);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/Load_8Bit/Logs/Check_Instruction_70_LD_Register16Address_Register"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_16_BIT_Bit_Register16Address()
        {
            StringBuilder testProgram = new StringBuilder();
            testProgram.AppendLine("LD HL,10100000000000B");
            testProgram.AppendLine("LD BC,1");
            testProgram.AppendLine("ADD HL,BC");
            testProgram.AppendLine("LD (HL),00000001B");
            testProgram.AppendLine("BIT 0,(HL)");
            testProgram.AppendLine("LD (HL),10000000B");
            testProgram.AppendLine("BIT 7,(HL)");
            testProgram.AppendLine("BIT 4,(HL)");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(8);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("ALU/BitSetResetAndTest/Logs/Check_Instruction_16_BIT_Bit_Register16Address"))
            {
                throw new Exception("Log compare failed");
            }
        }
        public static void Check_Instruction_58_JR_FlagCondition_RelativeDisplacement()
        {
            StringBuilder testProgram = new StringBuilder();
            testProgram.AppendLine("LD A,0");
            testProgram.AppendLine("JR Z,NEAR");
            testProgram.AppendLine("ORG 10");
            testProgram.AppendLine("NEAR: JR NZ,FAR");
            testProgram.AppendLine("LD A,1");
            testProgram.AppendLine("ORG 100");
            testProgram.AppendLine("FAR: LD A,2");

            TestSystem testSystem = new TestSystem();
            testSystem.LoadProgramInMemory(testProgram.ToString());

            CPUStateLogger logger = new CPUStateLogger(testSystem.CPU);
            logger.StartLogging(
                CPUStateLogger.CPUStateElements.InternalState | CPUStateLogger.CPUStateElements.Registers,
                new Z80CPU.LifecycleEventType[] { Z80CPU.LifecycleEventType.MachineCycleEnd });
            testSystem.ExecuteInstructionCount(4);
            logger.StopLogging();

            if (!logger.CompareWithSavedLog("CPU/Jump/Logs/Check_Instruction_58_JR_FlagCondition_RelativeDisplacement"))
            {
                throw new Exception("Log compare failed");
            }
        }