Пример #1
0
        private void OutputDelegate(long value)
        {
            OutputQueue.Enqueue(value);

            switch (OutputState)
            {
            case 0:
                XPos = value;
                OutputState++;
                break;

            case 1:
                YPos = value;
                OutputState++;
                break;

            case 2:
                TileID = value;
                if (GamePlayHasStarted)
                {
                    ProcessGameInstruction();
                }
                OutputState = 0;
                break;

            default:
                throw new ArgumentException("OutputState wonky");
            }
        }
Пример #2
0
            public static void SendRawMessage([NotNull] string line)
            {
                if (line == null)
                {
                    throw new ArgumentNullException("line");
                }

                OutputQueue.Enqueue(line);
            }
Пример #3
0
        private int ExecuteOutput(int[] workingSet, int position, int input)
        {
            var outputPosition = position + 1;
            var x = GetParameter(workingSet, outputPosition, GetParameterMode(input, 0));

            OutputQueue.Enqueue(x);
            OutputProduced?.Invoke(this, new OutputProducedEventArgs(x));
            return(position + 2);
        }
Пример #4
0
 public void HandleFromChild(IViewNet.Common.Models.Packet Message)
 {
     if (Message.Code == 7777) // SetDetectedFrame
     {
         short  FrameIndex  = BitConverter.ToInt16(Message.Content, 0);
         byte[] BitmapBytes = Message.Content.Skip(2).ToArray();
         Output.Enqueue(new Frame(BitmapBytes, FrameIndex));
     }
     else
     {
         Logger.Log(new Log("Invalid packet code: " + Message.Code, ConsoleColor.Red));
     }
 }
Пример #5
0
            public ReturnCode RunUntilBlockOrComplete()
            {
                var rawParams      = new long [ParameterCountsByOpCode.Values.Max()];
                var resolvedParams = new long [ParameterCountsByOpCode.Values.Max()];

                ReturnCode?returnCode = null;

                while (myPos >= 0 && returnCode == null)
                {
                    var(opCode, parameterModes) = ParseInstruction((int)myMemory [myPos]);
                    ResolveParams(myPos, parameterModes, ref rawParams, ref resolvedParams);

                    switch (opCode)
                    {
                    case 1:
                        myMemory [rawParams [2]] = resolvedParams [0] + resolvedParams [1];
                        break;

                    case 2:
                        myMemory [rawParams [2]] = resolvedParams [0] * resolvedParams [1];
                        break;

                    case 3:
                        if (InputQueue.Count == 0)
                        {
                            return(ReturnCode.WaitingForInput);
                        }                                                                 // Read cannot be completed, return and repeat the same command on continue.
                        myMemory [rawParams [0]] = InputQueue.Dequeue();
                        break;

                    case 4:
                        OutputQueue.Enqueue(resolvedParams [0]);
                        returnCode = ReturnCode.WrittenOutput;
                        break;

                    case 5:
                        if (resolvedParams [0] != 0)
                        {
                            myPos = resolvedParams [1]; continue;
                        }
                        break;

                    case 6:
                        if (resolvedParams [0] == 0)
                        {
                            myPos = resolvedParams [1]; continue;
                        }
                        break;

                    case 7:
                        myMemory [rawParams [2]] = resolvedParams [0] < resolvedParams [1] ? 1 : 0;
                        break;

                    case 8:
                        myMemory [rawParams [2]] = resolvedParams [0] == resolvedParams [1] ? 1 : 0;
                        break;

                    case 9:
                        myRelativeBase += resolvedParams [0];
                        break;

                    case 99:
                        returnCode = ReturnCode.Completed;
                        break;

                    default:
                        throw new InvalidOperationException($"Unknown operator: {opCode}");
                    }

                    myPos += parameterModes.Length + 1;
                }

                return(returnCode ?? ReturnCode.Error);
            }
Пример #6
0
        void Program4(long[] intCode, long param1)
        {
            var outputValue = param1;

            OutputQueue.Enqueue(outputValue);
        }
Пример #7
0
        /*
         *  Execute a single instruction at the current instruction pointer, m_ip.
         *
         *  Returns:
         *
         *  1 if correctly executed,
         *  2 if program halted pending input not available yet,
         *  0 if program terminated normally,
         *  -1 or other value if something went wrong.
         */
        private int ExecuteInstruction()
        {
            var instruction = Read(mIp++);
            // Base opcode of the instruction.
            int opcode = instruction % 100;
            // Parameter modes for this instruction.
            int pmodeArg1 = (instruction / 100) % 10;
            int pmodeArg2 = (instruction / 1000) % 10;

            int retcode = 1;
            int arg1, arg2, destination, result;

            switch (opcode)
            {
            case 1:
                // Add two arguments and store in a third position.
                (arg1, arg2) = GrabTwoArguments(mIp, pmodeArg1, pmodeArg2);
                mIp         += 2;
                result       = arg1 + arg2;
                destination  = Read(mIp++);
                Write(destination, result);
                break;

            case 2:
                // Multiply two arguments and store in a third position.
                (arg1, arg2) = GrabTwoArguments(mIp, pmodeArg1, pmodeArg2);
                mIp         += 2;
                result       = arg1 * arg2;
                destination  = Read(mIp++);
                Write(destination, result);
                break;

            case 3:
                // Input to a location in memory.
                if (InputQueue.Count == 0)
                {
                    mIp--;
                    retcode = 2;
                    break;
                }
                result      = InputQueue.Dequeue();
                destination = Read(mIp++);
                if (pmodeArg1 != 0)
                {
                    throw new Exception($"Illegal parameter mode {pmodeArg1} for opcode {opcode}.");
                }
                Write(destination, result);
                break;

            case 4:
                // Output from a location in memory.
                arg1   = Read(mIp++);
                result = (pmodeArg1 == 1) ? arg1 : Read(arg1);
                OutputQueue.Enqueue(result);
                break;

            case 5:
                // Jump if true.
                (arg1, arg2) = GrabTwoArguments(mIp, pmodeArg1, pmodeArg2);
                mIp         += 2;
                if (arg1 != 0)
                {
                    mIp = arg2;
                }

                break;

            case 6:
                // Jump if false.
                (arg1, arg2) = GrabTwoArguments(mIp, pmodeArg1, pmodeArg2);
                mIp         += 2;
                if (arg1 == 0)
                {
                    mIp = arg2;
                }

                break;

            case 7:
                // Less than.
                (arg1, arg2) = GrabTwoArguments(mIp, pmodeArg1, pmodeArg2);
                mIp         += 2;
                destination  = Read(mIp++);
                Write(destination, (arg1 < arg2) ? 1 : 0);
                break;

            case 8:
                // Equals.
                (arg1, arg2) = GrabTwoArguments(mIp, pmodeArg1, pmodeArg2);
                mIp         += 2;
                destination  = Read(mIp++);
                Write(destination, (arg1 == arg2) ? 1 : 0);
                break;

            case 99:
                // Normal program termination - return 0.
                retcode = 0;
                break;

            default:
                throw new Exception($"Error executing instruction {instruction} at position {mIp - 1}. Unrecognised opcode.");
            }

            return(retcode);
        }