Exemplo n.º 1
0
        public void RunNextInstruction()
        {
            string instStr = _programData[_pointer].ToString().PadLeft(5, '0');
            int    opcode  = Int32.Parse(instStr.Substring(instStr.Length - 2));

            ParamModes[] modes = new ParamModes[3];
            for (int i = 0; i < 3; i++)
            {
                modes[i] = (ParamModes)Int32.Parse(instStr.Substring(instStr.Length - 3 - i, 1));
            }

            switch (opcode)
            {
            case 1:
                Operation1(modes);
                break;

            case 2:
                Operation2(modes);
                break;

            case 3:
                Operation3(modes);
                break;

            case 4:
                Operation4(modes);
                break;

            case 5:
                Operation5(modes);
                break;

            case 6:
                Operation6(modes);
                break;

            case 7:
                Operation7(modes);
                break;

            case 8:
                Operation8(modes);
                break;

            case 9:
                Operation9(modes);
                break;

            case 99:
                Operation99(modes);
                break;

            default:
                throw new NotImplementedException($"Operation {opcode} is not yet implemented");
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates a new set of Modes from the given number
 /// </summary>
 /// <param name="modes">Value to extract the modes from</param>
 /// <exception cref="InvalidEnumArgumentException">If one of the parsed modes is not a valid member of the enum</exception>
 public Modes(int modes)
 {
     // ReSharper disable LocalVariableHidesMember
     (modes, int first)      = Math.DivRem(modes, 10);
     (int third, int second) = Math.DivRem(modes, 10);
     this.first  = (ParamModes)first;
     this.second = (ParamModes)second;
     this.third  = (ParamModes)third;
     // ReSharper restore LocalVariableHidesMember
 }
Exemplo n.º 3
0
        private Int64 GetOutputPosition(int paramNo, ParamModes mode)
        {
            Int64 addressValue = _pointer + paramNo;

            switch (mode)
            {
            case ParamModes.Position:
                return(GetAddressValueSafe(addressValue));

            case ParamModes.Immediate:
                return(addressValue);

            case ParamModes.Relative:
                return(GetAddressValueSafe(addressValue) + _relativeBase);
            }

            throw new NotImplementedException($"Mode {mode} not implemented.");
        }
Exemplo n.º 4
0
        public static IEnumerable <IOperation> CreateOperationsStream(IReadOnlyProcessor processor)
        {
            while (true)
            {
                var opcodeFull = processor.ReadMemory(0);
                var opcode     = (Opcode)(opcodeFull % 100);
                var paramModes = new ParamModes(opcodeFull);

                switch (opcode)
                {
                case Opcode.ADDITION:
                    yield return(new Addition(
                                     new Parameter(processor.ReadMemory(1), paramModes.GetNext()),
                                     new Parameter(processor.ReadMemory(2), paramModes.GetNext()),
                                     new Parameter(processor.ReadMemory(3), paramModes.GetNext())
                                     ));

                    break;

                case Opcode.MULTIPLICATION:
                    yield return(new Multiplication(
                                     new Parameter(processor.ReadMemory(1), paramModes.GetNext()),
                                     new Parameter(processor.ReadMemory(2), paramModes.GetNext()),
                                     new Parameter(processor.ReadMemory(3), paramModes.GetNext())
                                     ));

                    break;

                case Opcode.INPUT:
                    yield return(new Input(
                                     new Parameter(processor.ReadMemory(1), paramModes.GetNext())
                                     ));

                    break;

                case Opcode.OUTPUT:
                    yield return(new Output(
                                     new Parameter(processor.ReadMemory(1), paramModes.GetNext())
                                     ));

                    yield break;

                case Opcode.JUMP_IF_TRUE:
                    yield return(new JumpIfTrue(
                                     new Parameter(processor.ReadMemory(1), paramModes.GetNext()),
                                     new Parameter(processor.ReadMemory(2), paramModes.GetNext())
                                     ));

                    break;

                case Opcode.JUMP_IF_FALSE:
                    yield return(new JumpIfFalse(
                                     new Parameter(processor.ReadMemory(1), paramModes.GetNext()),
                                     new Parameter(processor.ReadMemory(2), paramModes.GetNext())
                                     ));

                    break;

                case Opcode.LESS_THAN:
                    yield return(new LessThan(
                                     new Parameter(processor.ReadMemory(1), paramModes.GetNext()),
                                     new Parameter(processor.ReadMemory(2), paramModes.GetNext()),
                                     new Parameter(processor.ReadMemory(3), paramModes.GetNext())
                                     ));

                    break;

                case Opcode.EQUALS:
                    yield return(new Equals(
                                     new Parameter(processor.ReadMemory(1), paramModes.GetNext()),
                                     new Parameter(processor.ReadMemory(2), paramModes.GetNext()),
                                     new Parameter(processor.ReadMemory(3), paramModes.GetNext())
                                     ));

                    break;

                case Opcode.RELATIVE_BASE_OFFSET:
                    yield return(new RelativeBaseOffset(
                                     new Parameter(processor.ReadMemory(1), paramModes.GetNext())
                                     ));

                    break;

                case Opcode.FINISH:
                    yield return(new Finish());

                    yield break;

                default:
                    throw new InvalidOperationException($"Unrecognized operation opcode: {opcode}");
                }
            }
        }