public void Execute() { NextPointer = 0; var theEnd = false; while (!theEnd) { Watch?.Invoke(DebugOutput); var instructionPointer = NextPointer; var opcode = buffer[NextPointer++]; var instruction = (OpCode)(opcode % 100); switch (instruction) { case OpCode.END: theEnd = true; Log?.Invoke($"{instructionPointer,6} 99 END"); break; case OpCode.ADD: { NextPointer = RunInstruction_2_1(opcode, buffer, instructionPointer, NextPointer, (b1, b2) => b1 + b2); break; } case OpCode.MUL: { NextPointer = RunInstruction_2_1(opcode, buffer, instructionPointer, NextPointer, (b1, b2) => b1 * b2); break; } case OpCode.TEQ: { NextPointer = RunInstruction_2_1(opcode, buffer, instructionPointer, NextPointer, (b1, b2) => b1 == b2?1:0); break; } case OpCode.TLT: { NextPointer = RunInstruction_2_1(opcode, buffer, instructionPointer, NextPointer, (b1, b2) => b1 < b2?1:0); break; } case OpCode.JNZ: { NextPointer = RunInstruction_2_J(opcode, buffer, instructionPointer, NextPointer, (b1) => b1 != 0); break; } case OpCode.JZE: { NextPointer = RunInstruction_2_J(opcode, buffer, instructionPointer, NextPointer, (b1) => b1 == 0); break; } case OpCode.INP: { NextPointer = RunInstruction_0_1(opcode, buffer, instructionPointer, NextPointer, GetNextInput); break; } case OpCode.OUT: { NextPointer = RunInstruction_1_0(opcode, buffer, instructionPointer, NextPointer, x => SetOutput(x)); break; } default: throw new Exception($"Invalid opcode : {opcode}"); } } }
public void Execute() { NextPointer = 0; var theEnd = false; while (!theEnd) { Watch?.Invoke(DebugOutput); var opcode = buffer[NextPointer++]; switch (opcode) { case 99: theEnd = true; break; case 1: { var operand1 = buffer[NextPointer++]; var operand2 = buffer[NextPointer++]; var resultLocation = buffer[NextPointer++]; buffer[resultLocation] = buffer[operand1] + buffer[operand2]; break; } case 2: { var operand1 = buffer[NextPointer++]; var operand2 = buffer[NextPointer++]; var resultLocation = buffer[NextPointer++]; buffer[resultLocation] = buffer[operand1] * buffer[operand2]; break; } default: throw new Exception($"Invalid opcode : {opcode}"); } } }
public void Execute(Snapshot snapshot = null) { if (snapshot == null) { NextPointer = 0; BaseAddress = 0; additionnalMemory.Clear(); } else { buffer = snapshot.Buffer.ToArray(); additionnalMemory = snapshot.AdditionnalMemory.ToDictionary(x => x.Key, x => x.Value); BaseAddress = snapshot.BaseAddress; NextPointer = snapshot.NextPointer; } var theEnd = false; while (!theEnd) { Watch?.Invoke(DebugOutput); var instructionPointer = NextPointer; var opcode = (int)Memory[NextPointer++]; var instruction = (OpCode)(opcode % 100); switch (instruction) { case OpCode.END: theEnd = true; Log?.Invoke($"{instructionPointer,6} 99 END"); break; case OpCode.ADD: { NextPointer = RunInstruction_2_1(opcode, Memory, instructionPointer, NextPointer, (b1, b2) => b1 + b2); break; } case OpCode.MUL: { NextPointer = RunInstruction_2_1(opcode, Memory, instructionPointer, NextPointer, (b1, b2) => b1 * b2); break; } case OpCode.TEQ: { NextPointer = RunInstruction_2_1(opcode, Memory, instructionPointer, NextPointer, (b1, b2) => b1 == b2?1:0); break; } case OpCode.TLT: { NextPointer = RunInstruction_2_1(opcode, Memory, instructionPointer, NextPointer, (b1, b2) => b1 < b2?1:0); break; } case OpCode.JNZ: { NextPointer = RunInstruction_2_J(opcode, Memory, instructionPointer, NextPointer, (b1) => b1 != 0); break; } case OpCode.JZE: { NextPointer = RunInstruction_2_J(opcode, Memory, instructionPointer, NextPointer, (b1) => b1 == 0); break; } case OpCode.INP: { NextPointer = RunInstruction_0_1(opcode, Memory, instructionPointer, NextPointer, GetNextInput); break; } case OpCode.OUT: { NextPointer = RunInstruction_1_0(opcode, Memory, instructionPointer, NextPointer, x => SetOutput?.Invoke(x)); break; } case OpCode.ADB: { NextPointer = RunInstruction_1_0(opcode, Memory, instructionPointer, NextPointer, x => BaseAddress += x); break; } default: throw new Exception($"Invalid opcode : {opcode}"); } } }