Esempio n. 1
0
        public Instruction(int address, IntCodeCpuMemory memory)
        {
            const int opcodeSize = 1;

            Address = address;

            OperationCode = (int)memory.Read(address, ParameterMode.Immediate);
            Operation     = (Operations)(OperationCode % 100);

            Size           = InstructionSizeLookup(Operation);
            ParameterCount = ParameterCountLookup(Operation);
            Parameters     = new List <Parameter>(ParameterCount);

            for (var i = 0; i < ParameterCount; i++)
            {
                int parameterCodes     = OperationCode / 100;
                int parameterAddress   = i + (address + opcodeSize);
                int divider            = (int)Math.Pow(10, i);
                int parameterBit       = parameterCodes / divider;
                int parameterModeValue = parameterBit % 10;
                var parameterMode      = (ParameterMode)parameterModeValue;

                var parameter = new Parameter()
                {
                    Address       = parameterAddress,
                    Value         = memory[parameterAddress],
                    Mode          = parameterMode,
                    ResolvedValue = memory.Read(parameterAddress, parameterMode)
                };

                Parameters.Add(parameter);
            }
        }
Esempio n. 2
0
        public static BigInteger Read(this IntCodeCpuMemory memory, int address, ParameterMode mode)
        {
            if (mode == ParameterMode.Relative)
            {
                return(memory[(int)memory[address] + memory.RelativeBase]);
            }

            return(mode == ParameterMode.Position
                ? memory[(int)memory[address]]
                : memory[address]);
        }
Esempio n. 3
0
        public static BigInteger Write(this IntCodeCpuMemory memory, BigInteger value, int address, ParameterMode mode)
        {
            if (mode == ParameterMode.Relative)
            {
                return(memory[(int)memory[address] + memory.RelativeBase] = value);
            }

            return(mode == ParameterMode.Position
                ? memory[(int)memory[address]] = value
                : memory[address] = value);
        }
 public IntCodeCpu(ILogger logger, IntCodeCpuMemory memory)
 {
     this.logger = logger;
     this.memory = memory;
 }
Esempio n. 5
0
 public static BigInteger ReadOperand(this Instruction instruction, IntCodeCpuMemory memory, int index)
 {
     return(memory.Read(instruction.Parameters[index].Address, instruction.Parameters[index].Mode));
 }
Esempio n. 6
0
 public static void WriteOperand3(this Instruction instruction, BigInteger value, IntCodeCpuMemory memory)
 {
     WriteOperand(instruction, value, memory, 2);
 }
Esempio n. 7
0
 public static void WriteOperand(this Instruction instruction, BigInteger value, IntCodeCpuMemory memory, int index)
 {
     memory.Write(value, instruction.Parameters[index].Address, instruction.Parameters[index].Mode);
 }
Esempio n. 8
0
 public static BigInteger ReadOperand2(this Instruction instruction, IntCodeCpuMemory memory)
 {
     return(ReadOperand(instruction, memory, 1));
 }