long[] Program3(long[] intCode, long pos) { var input = InputQueue.Dequeue(); intCode[pos] = input; //InputQueue.Enqueue(2); return(intCode); }
int[] Program3(int[] intCode, int pos, Parameter p) { var input = InputQueue.Dequeue(); var inputValue = intCode[pos]; intCode[inputValue] = input; return(intCode); }
public ConsoleKeyInfo ReadKey() { while (KeyAvailable == false) { Thread.Sleep(10); } return(InputQueue.Dequeue()); }
private long InputDelegate() { long value = 0; GamePlayHasStarted = true; if (InputQueue.Count > 0) { value = InputQueue.Dequeue(); } return(value); }
protected bool Dequeue(TimeSpan timeout, out TDisposable item) { this.ThrowIfNotOpened(); bool dequeued = inputQueue.Dequeue(timeout, out item); if (item == null) { this.ThrowIfFaulted(); this.ThrowIfAborted(); } return(dequeued); }
public bool RunCode() { for (; LineNum < prog.Length; LineNum++) { string line = prog[LineNum]; string[] parts = prog[LineNum].Split(' '); switch (prog[LineNum][1]) { case 'n': // snd OnSend(GetValue(parts[1])); break; case 'e': // set Register(parts[1]) = GetValue(parts[2]); break; case 'd': // add Register(parts[1]) += GetValue(parts[2]); break; case 'u': // mul Register(parts[1]) *= GetValue(parts[2]); break; case 'o': // mod Register(parts[1]) %= GetValue(parts[2]); break; case 'c': // rcv if (InputQueue.Count == 0) { return(false); } Register(parts[1]) = InputQueue.Dequeue(); break; case 'g': // jgz if (GetValue(parts[1]) <= 0) { break; } LineNum += GetValue(parts[2]) - 1; break; } } return(true); }
// private static object message_queue_lock = new object(); /// <summary> /// Returns the current GenericInput for processing. /// </summary> /// <returns>A GenericInput that needs to be process, null if the InputQueque is empty.</returns> public static GenericInput Update() { lock (input_queue_lock) { if (InputQueue.Count == 0) { return(null); } else { GenericInput gi = InputQueue.Dequeue(); if (gi is MouseGenericInput) { MouseGenericInput mgi = (MouseGenericInput)gi; if (MouseGenericInputType.MousePressed == mgi.MouseInputType) { IsMouseDown = true; } else if (MouseGenericInputType.MouseReleased == mgi.MouseInputType) { if (IsMouseDown) { MouseGenericInput mouse_click_event = new MouseGenericInput(mgi.X, mgi.Y); mouse_click_event.MouseInputType = MouseGenericInputType.MouseClick; InputManager.AddInputItem(mouse_click_event); } IsMouseDown = false; } } if (gi is GenericKeyboardInput) { GenericKeyboardInput ki = (GenericKeyboardInput)gi; IsWKeyPress = ki.IsWKeyPress; IsAKeyPress = ki.IsAKeyPress; IsSKeyPress = ki.IsSKeyPress; IsDKeyPress = ki.IsDKeyPress; } return(gi); } } }
protected override void DoWorkImpl() { while (true) { var block = InputQueue.Dequeue(); if (block == null) { break; } using (var inStream = new MemoryStream(block.Bytes)) using (var gzipStream = new GZipStream(inStream, CompressionMode.Decompress)) using (var outStream = new MemoryStream()) { gzipStream.CopyTo(outStream); var decompressedBlock = new ByteBlock(block.ID, outStream.ToArray()); ResultQueue.Enqueue(decompressedBlock, true); } } }
protected override void DoWorkImpl() { while (true) { var block = InputQueue.Dequeue(); if (block == null) { break; } using (var memoryStream = new MemoryStream()) { using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress)) { gzipStream.Write(block.Bytes, 0, block.Bytes.Length); } var compressedArray = memoryStream.ToArray(); var compressedBlock = new ByteBlock(block.ID, compressedArray); ResultQueue.Enqueue(compressedBlock, true); } } }
/// <summary> /// This method is constantly beeing executed by the main thread of the framework.<br/> /// Dont call this method directly. Use the Init and FinishMainThread methods. /// </summary> //TODO: Think about implement something which does really check on value changes on tableelements or triggered effects before setting update required. private void MainThreadDoIt() { //ThreadInfoList.HeartBeat("DirectOutput"); try { while (KeepMainThreadAlive) { bool UpdateRequired = false; DateTime Start = DateTime.Now; //Consume the tableelement data delivered from the calling application while (InputQueue.Count > 0 && (DateTime.Now - Start).Milliseconds <= MaxInputDataProcessingTimeMs && KeepMainThreadAlive) { TableElementData D; D = InputQueue.Dequeue(); try { //Log.Write("Pinball.MainThreadDoIt...d.name="+D.Name+", value="+D.Value+", d="+D.Number+", d="+D); Table.UpdateTableElement(D); UpdateRequired |= true; } catch (Exception E) { Log.Exception("A unhandled exception occured while processing data for table element {0} {1} with value {2}".Build(D.TableElementType, D.Number, D.Value), E); //ThreadInfoList.RecordException(E); } } if (KeepMainThreadAlive) { try { //Executed all alarms which have been scheduled for the current time UpdateRequired |= Alarms.ExecuteAlarms(DateTime.Now.AddMilliseconds(1)); } catch (Exception E) { Log.Exception("A unhandled exception occured while executing timer events.", E); //ThreadInfoList.RecordException(E); } } //Call update on output controllers if necessary if (UpdateRequired && KeepMainThreadAlive) { try { Cabinet.Update(); } catch (Exception E) { Log.Exception("A unhandled exception occured while updating the output controllers", E); //ThreadInfoList.RecordException(E); } } if (KeepMainThreadAlive) { //ThreadInfoList.HeartBeat(); //Sleep until we get more input data and/or a timer expires. DateTime NextAlarm = Alarms.GetNextAlarmTime(); lock (MainThreadLocker) { while (InputQueue.Count == 0 && NextAlarm > DateTime.Now && !MainThreadDoWork && KeepMainThreadAlive) { int TimeOut = ((int)(NextAlarm - DateTime.Now).TotalMilliseconds).Limit(1, 50); Monitor.Wait(MainThreadLocker, TimeOut); // Lock is released while we’re waiting //ThreadInfoList.HeartBeat(); } } MainThreadDoWork = false; } } } catch (Exception E) { Log.Exception("A unexpected exception occured in the DirectOutput MainThread", E); //ThreadInfoList.RecordException(E); } //ThreadInfoList.ThreadTerminates(); }
private void ReadInput() { Memory[Memory[_iPointer + 3]] = InputQueue.Dequeue(); _iPointer += 2; }
private AsciiKey GetNextInput() => InputQueue.Dequeue();
public long RunNext() { string fullOpcode = Memory[InstructionPtr].ToString().PadLeft(5, '0'); long opcode = long.Parse(fullOpcode.Substring(3, 2)); long param1Mode = long.Parse(fullOpcode.Substring(2, 1)); long param2Mode = long.Parse(fullOpcode.Substring(1, 1)); long param3Mode = long.Parse(fullOpcode.Substring(0, 1)); long param1 = 0; long param2 = 0; long param3 = 0; long param1Key = 0; long param2Key = 0; long param3Key = 0; if (new long[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.Contains(opcode)) { if (!Memory.ContainsKey(InstructionPtr + 1)) { Memory.Add(InstructionPtr + 1, 0); } switch (param1Mode) { case 0: param1Key = Memory[InstructionPtr + 1]; if (!Memory.ContainsKey(param1Key)) { Memory.Add(param1Key, 0); } param1 = Memory[param1Key]; break; case 1: param1 = Memory[InstructionPtr + 1]; break; case 2: param1Key = RelativeBase + Memory[InstructionPtr + 1]; if (!Memory.ContainsKey(param1Key)) { Memory.Add(param1Key, 0); } param1 = Memory[param1Key]; break; } } if (new long[] { 1, 2, 5, 6, 7, 8 }.Contains(opcode)) { if (!Memory.ContainsKey(InstructionPtr + 2)) { Memory.Add(InstructionPtr + 2, 0); } switch (param2Mode) { case 0: param2Key = Memory[InstructionPtr + 2]; if (!Memory.ContainsKey(param2Key)) { Memory.Add(param2Key, 0); } param2 = Memory[param2Key]; break; case 1: param2 = Memory[InstructionPtr + 2]; break; case 2: param2Key = RelativeBase + Memory[InstructionPtr + 2]; if (!Memory.ContainsKey(param2Key)) { Memory.Add(param2Key, 0); } param2 = Memory[param2Key]; break; } } if (new long[] { 1, 2, 7, 8 }.Contains(opcode)) { if (!Memory.ContainsKey(InstructionPtr + 3)) { Memory.Add(InstructionPtr + 3, 0); } switch (param3Mode) { case 0: param3Key = Memory[InstructionPtr + 3]; if (!Memory.ContainsKey(param3Key)) { Memory.Add(param3Key, 0); } param3 = Memory[param3Key]; break; case 1: param3 = Memory[InstructionPtr + 3]; break; case 2: param3Key = RelativeBase + Memory[InstructionPtr + 3]; if (!Memory.ContainsKey(param3Key)) { Memory.Add(param3Key, 0); } param3 = Memory[param3Key]; break; } } switch (opcode) { case 1: Memory[param3Key] = param1 + param2; InstructionPtr += 4; break; case 2: Memory[param3Key] = param1 * param2; InstructionPtr += 4; break; case 3: bool success = false; long parsed = long.MinValue; while (!success) { string input; if (InputQueue.Any()) { input = InputQueue.Dequeue().ToString(); if (PrintOutput) { Console.WriteLine($"Input: {input}"); } } else if (AsciiInputMode) { string line = Console.ReadLine(); AddAsciiInput(line + "\n"); input = ""; } else { Console.Write("Input: "); input = Console.ReadLine(); } success = long.TryParse(input.Trim(), out parsed); } Memory[param1Key] = parsed; InstructionPtr += 2; break; case 4: if (PrintOutput) { Console.WriteLine($"Output: {param1}"); } OutputQueue.Enqueue(param1); InstructionPtr += 2; break; case 5: if (param1 != 0) { InstructionPtr = param2; } else { InstructionPtr += 3; } break; case 6: if (param1 == 0) { InstructionPtr = param2; } else { InstructionPtr += 3; } break; case 7: Memory[param3Key] = param1 < param2 ? 1 : 0; InstructionPtr += 4; break; case 8: Memory[param3Key] = param1 == param2 ? 1 : 0; InstructionPtr += 4; break; case 9: RelativeBase += param1; InstructionPtr += 2; break; case 99: return(Memory[0]); default: throw new Exception($"Unknown opcode: {opcode} at {InstructionPtr}"); } return(long.MinValue); }
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); }
//public static void IntCodeComputerMain(int[] intCode, Func<int> inputFunc, Action<int> outputAct) //{ // intCode = RunProgram(intCode, 0, inputFunc, outputAct); //} public int[] RunProgram(int[] intCode) { return(RunProgram(intCode, InputQueue.Dequeue())); }
public long[] RunProgram(long[] intCode) { RelativeBase = 0; return(RunProgram(intCode, InputQueue.Dequeue())); }
/* * 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); }
//public Queue<long> Compute() public void Compute() { long pc = 0; //program counter long rb = 0; //relative base while (pc < memory.Length) { var instruction = memory[pc].ToString("00000"); long opcode = int.Parse(instruction.Substring(instruction.Length - 2)); char p1Mode = instruction[2]; char p2Mode = instruction[1]; char p3Mode = instruction[0]; if (opcode == 99) { //halt break; } else if (opcode == 1) { //add SetParameter(pc + 3, p3Mode, rb, GetParameter(pc + 1, p1Mode, rb) + GetParameter(pc + 2, p2Mode, rb)); pc += 4; } else if (opcode == 2) { //mult SetParameter(pc + 3, p3Mode, rb, GetParameter(pc + 1, p1Mode, rb) * GetParameter(pc + 2, p2Mode, rb)); pc += 4; } else if (opcode == 3) { //read input from inputQueue bool spinlock = true; while (spinlock) { lock (InputQueue){ if (InputQueue.Any()) { SetParameter(pc + 1, p1Mode, rb, InputQueue.Dequeue()); spinlock = false; } } } pc += 2; } else if (opcode == 4) { //write output var output = GetParameter(pc + 1, p1Mode, rb); if (OutputToConsole) { Console.WriteLine(output); } lock (OutputQueue) { OutputQueue.Enqueue(output); } pc += 2; } else if (opcode == 5) { //jump if true if (GetParameter(pc + 1, p1Mode, rb) != 0) { pc = GetParameter(pc + 2, p2Mode, rb); } else { pc += 3; } } else if (opcode == 6) { //jump if false if (GetParameter(pc + 1, p1Mode, rb) == 0) { pc = GetParameter(pc + 2, p2Mode, rb); } else { pc += 3; } } else if (opcode == 7) { //less than if (GetParameter(pc + 1, p1Mode, rb) < GetParameter(pc + 2, p2Mode, rb)) { SetParameter(pc + 3, p3Mode, rb, 1); } else { SetParameter(pc + 3, p3Mode, rb, 0); } pc += 4; } else if (opcode == 8) { //equals if (GetParameter(pc + 1, p1Mode, rb) == GetParameter(pc + 2, p2Mode, rb)) { SetParameter(pc + 3, p3Mode, rb, 1); } else { SetParameter(pc + 3, p3Mode, rb, 0); } pc += 4; } else if (opcode == 9) { rb += GetParameter(pc + 1, p1Mode, rb); pc += 2; } else { Console.WriteLine("ERR!!!!!"); } } //return OutputQueue; }