Exemplo n.º 1
0
        private void InvokeOpcode_VAROP(Opcode aInstruction, int aOpcode, ushort aArg0, ushort aArg1, ushort aArg2, ushort aArg3, ushort aArg4, ushort aArg5, ushort aArg6, ushort aArg7, ushort aArgCount)
        {
            ZDebug.Output($"Invoking: {aOpcode.ToHex(2)} -> {aInstruction}:{aArgCount} -> {_invokeCount}");

            aInstruction.Execute(aArg0, aArg1, aArg2, aArg3, aArg4, aArg5, aArg6, aArg7, aArgCount);
            _invokeCount++;
        }
Exemplo n.º 2
0
Arquivo: CPU.cs Projeto: jenden0/KOS
        private bool ExecuteInstruction(ProgramContext context)
        {
            bool DEBUG_EACH_OPCODE = false;

            Opcode opcode = context.Program[context.InstructionPointer];

            if (DEBUG_EACH_OPCODE)
            {
                SafeHouse.Logger.Log("ExecuteInstruction.  Opcode number " + context.InstructionPointer + " out of " + context.Program.Count +
                                     "\n                   Opcode is: " + opcode.ToString());
            }

            if (!(opcode is OpcodeEOF || opcode is OpcodeEOP))
            {
                opcode.Execute(this);
                context.InstructionPointer += opcode.DeltaInstructionPointer;
                return(true);
            }
            if (opcode is OpcodeEOP)
            {
                BreakExecution(false);
                SafeHouse.Logger.Log("Execution Broken");
            }
            return(false);
        }
Exemplo n.º 3
0
        public static ISet <string> PossibleOpcodes(Sample s)
        {
            var set = new System.Collections.Generic.HashSet <string>();

            foreach ((string name, var codeFactory) in Opcode.Opcodes)
            {
                Opcode    opcode = codeFactory(s.A, s.B, s.C);
                Registers result = opcode.Execute(s.Before);
                if (result == s.After)
                {
                    set.Add(name);
                }
            }
            return(set);
        }
Exemplo n.º 4
0
        private bool ExecuteInstruction(ProgramContext context)
        {
            Opcode opcode = context.Program[context.InstructionPointer];

            if (!(opcode is OpcodeEOF || opcode is OpcodeEOP))
            {
                opcode.Execute(this);
                context.InstructionPointer += opcode.DeltaInstructionPointer;
                return(true);
            }
            if (opcode is OpcodeEOP)
            {
                BreakExecution(false);
                UnityEngine.Debug.LogWarning("kOS: Execution Broken");
            }
            return(false);
        }
Exemplo n.º 5
0
        public static int SolvePart2(
            IEnumerable <Sample> samples,
            IEnumerable <byte[]> instructions)
        {
            IReadOnlyDictionary <byte, string> opcodeMap = BuildOpcodeMap(samples);

            Registers r = default;

            foreach (byte[] instruction in instructions)
            {
                string operationName = opcodeMap[instruction[0]];
                Opcode op            = Opcode.Opcodes[operationName](instruction[1], instruction[2], instruction[3]);
                r = op.Execute(r);
            }

            return(r.R0);
        }
Exemplo n.º 6
0
 private void InvokeOpcode_2OP(Opcode aInstruction, int aOpcode, ushort aArg0, ushort aArg1)
 {
     ZDebug.Output($"Invoking 2OP: {aOpcode.ToHex(2)} -> {aInstruction} -> {_invokeCount}");
     aInstruction.Execute(aArg0, aArg1);
     _invokeCount++;
 }
Exemplo n.º 7
0
 private void InvokeOpcode_0OP(Opcode aInstruction, int aOpcode)
 {
     ZDebug.Output($"Invoking 0OP: {aOpcode.ToHex(2)} -> {aInstruction} -> {_invokeCount}");
     aInstruction.Execute();
     _invokeCount++;
 }
Exemplo n.º 8
0
        public void EmulateCycles(long cycleLimit)
        {
            long cycleCounter = cycleLimit;

            PerformanceTimer.Restart();

            while (cycleCounter-- > 0)
            {
                CycleTimer.Start();
                HandleTimers();
                PPU.ProcessCycle();
                APU.ProcessCycle();

                if (CycleCooldown > 0)
                {
                    if (ExecOpcode != null)
                    {
                        ExecOpcode.ExecuteTick();
                    }
                    CycleCooldown--;
                    if (CycleCooldown == 0 && ExecOpcode != null)
                    {
                        if (!HaltBug)
                        {
                            PC += (ushort)ExecOpcode.Length;
                        }
                        else
                        {
                            HaltBug = false;
                        }
                        ExecOpcode = null;
                    }
                    WaitForCycleFinish(CycleTimer, cycleCounter);
                    continue;
                }

                if (CheckForInterrupt())
                {
                    HandleInterrupt();
                    WaitForCycleFinish(CycleTimer, cycleCounter);
                    continue;
                }

                if (Halted)
                {
                    if (CycleCooldown == 0)
                    {
                        CycleCooldown += 4;
                    }
                    if (CycleCooldown > 0)
                    {
                        WaitForCycleFinish(CycleTimer, cycleCounter);
                        continue;
                    }
                }

                if (EINextInstruction)
                {
                    EINextInstruction = false;
                    InterruptsEnabled = true;
                }

                Opcode opcode = Decoder.DecodeOpcode(this, Memory[PC]);
                if (logTrace)
                {
                    LogTrace(opcode);
                }
                if (opcode.TickAccurate)
                {
                    ExecOpcode = opcode;
                }
                else
                {
                    ExecOpcode = null;
                }

                if (!opcode.TickAccurate)
                {
                    opcode.Execute();
                    if (!HaltBug)
                    {
                        PC += (ushort)opcode.Length;
                    }
                    else
                    {
                        HaltBug = false;
                    }
                }
                else
                {
                    ExecOpcode.ExecuteTick();
                }

                CycleCooldown = opcode.Cycles - 1;

                WaitForCycleFinish(CycleTimer, cycleCounter);
            }

            EndTime = PerformanceTimer.ElapsedTicks;
        }