public static void TryWaitOnHandleAndDispose(ref System.Threading.WaitHandle handle) { if (handle == null) return; try { handle.WaitOne(); } catch (System.ObjectDisposedException) { return; } catch (System.Exception ex) { Media.Common.Extensions.Exception.ExceptionExtensions.TryRaiseTaggedException(handle, "An exception occured while waiting.", ex); } finally { if (handle != null) handle.Dispose(); } handle = null; }
public void ExecuteProgram(UInt16 programPosition, System.Threading.ManualResetEvent pauseEvent) { Debug.Assert(memory != null); InstructionPointer = programPosition; bool isProgramEnd = false; Running = CPUStatus.Running; while (!isProgramEnd) { var instruction = memory[InstructionPointer]; ++InstructionPointer; lock(mutexSpeed) { System.Threading.Thread.Sleep(Speed); } pauseEvent.WaitOne(System.Threading.Timeout.Infinite); switch (instruction) { case 0x01: //LDT R V { var registerID = (Register)memory[InstructionPointer]; var bytes = new byte[] { memory[(UInt16)(InstructionPointer + 1)], memory[(UInt16)(InstructionPointer + 2)] }; var fromAddress = System.BitConverter.ToUInt16(bytes, 0); InstructionPointer += 3; MemoryToRegister(registerID, fromAddress); if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x28: //LDR R R { var leftRegisterID = (Register)memory[InstructionPointer]; var rightRegisterID = (Register)memory[(UInt16)(InstructionPointer + 1)]; UInt16 fromAddress = ReadFromRegister(rightRegisterID); InstructionPointer += 2; MemoryToRegister(leftRegisterID, fromAddress); if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x02: //STT V R { var bytes = new byte[] { memory[(UInt16)(InstructionPointer)], memory[(UInt16)(InstructionPointer + 1)] }; var toAddress = System.BitConverter.ToUInt16(bytes, 0); var registerID = (Register)memory[(UInt16)(InstructionPointer + 2)]; InstructionPointer += 3; RegisterToMemory(registerID, toAddress); break; } case 0x27: //STT R R { var leftRegisterID = (Register)memory[InstructionPointer]; var rightRegisterID = (Register)memory[(UInt16)(InstructionPointer + 1)]; UInt16 toAddress = ReadFromRegister(leftRegisterID); InstructionPointer += 2; RegisterToMemory(rightRegisterID, toAddress); break; } case 0x03: //SET R V { var registerID = (Register)memory[InstructionPointer]; if (registerID == Register.AH || registerID == Register.AL) { if (registerID == Register.AH) AH = (memory[(UInt16)(InstructionPointer + 1)]); else AL = (memory[(UInt16)(InstructionPointer + 1)]); } else { var bytes = new byte[] { memory[(UInt16)(InstructionPointer + 1)], memory[(UInt16)(InstructionPointer + 2)] }; var value = System.BitConverter.ToUInt16(bytes, 0); SetToRegister(registerID, value); } InstructionPointer += 3; if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x04: //END ADDR InstructionPointer += 2; isProgramEnd = true; break; case 0x0c: //CMP V V { var leftValue = System.BitConverter.ToUInt16(memory.Segment(InstructionPointer, (UInt16)(InstructionPointer + 2)), 0); var rightValue = System.BitConverter.ToUInt16(memory.Segment((UInt16)(InstructionPointer + 2), (UInt16)(InstructionPointer + 4)), 0); Flags = 0; if (leftValue == rightValue) Flags = (byte)(Flags | 1); if (leftValue != rightValue) Flags = (byte)(Flags | 2); if (leftValue > rightValue) Flags = (byte)(Flags | 4); if (leftValue < rightValue) Flags = (byte)(Flags | 8); InstructionPointer += 4; if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x0d: //CMP V R { var leftValue = System.BitConverter.ToUInt16(memory.Segment(InstructionPointer, (UInt16)(InstructionPointer + 2)), 0); var registerID = (Register)memory[(UInt16)(InstructionPointer + 2)]; UInt16 rightValue = ReadFromRegister(registerID); Flags = 0; if (leftValue == rightValue) Flags = (byte)(Flags | 1); if (leftValue != rightValue) Flags = (byte)(Flags | 2); if (leftValue > rightValue) Flags = (byte)(Flags | 4); if (leftValue < rightValue) Flags = (byte)(Flags | 8); InstructionPointer += 3; if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x0e: //CMP R V { var registerID = (Register)memory[(UInt16)(InstructionPointer)]; var rightValue = System.BitConverter.ToUInt16(memory.Segment((UInt16)(InstructionPointer + 1), (UInt16)(InstructionPointer + 3)), 0); ; UInt16 leftValue = ReadFromRegister(registerID); Flags = 0; if (leftValue == rightValue) Flags = (byte)(Flags | 1); if (leftValue != rightValue) Flags = (byte)(Flags | 2); if (leftValue > rightValue) Flags = (byte)(Flags | 4); if (leftValue < rightValue) Flags = (byte)(Flags | 8); InstructionPointer += 3; if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x0f: //CMP R R { var leftRegisterID = (Register)memory[InstructionPointer]; var rightRegisterID = (Register)memory[(UInt16)(InstructionPointer + 1)]; var leftValue = ReadFromRegister(leftRegisterID); var rightValue = ReadFromRegister(rightRegisterID); Flags = 0; if (leftValue == rightValue) Flags = (byte)(Flags | 1); if (leftValue != rightValue) Flags = (byte)(Flags | 2); if (leftValue > rightValue) Flags = (byte)(Flags | 4); if (leftValue < rightValue) Flags = (byte)(Flags | 8); InstructionPointer += 4; if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x05: //JMP ADDR { var addr = System.BitConverter.ToInt16(memory.Segment(InstructionPointer, (UInt16)(InstructionPointer + 2)), 0); UInt16 absAddr = (UInt16)System.Math.Abs(addr); if (addr >= 0) InstructionPointer += absAddr; else InstructionPointer -= absAddr; if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x06: //JLE ADDR { var addr = System.BitConverter.ToUInt16(memory.Segment(InstructionPointer, (UInt16)(InstructionPointer + 2)), 0); UInt16 absAddr = (UInt16)System.Math.Abs(addr); if ((Flags & 8) == 8 || (Flags & 1) == 1) { if (addr >= 0) InstructionPointer += absAddr; else InstructionPointer -= absAddr; } else InstructionPointer += 2; if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x07: //JL ADDR { var addr = System.BitConverter.ToUInt16(memory.Segment(InstructionPointer, (UInt16)(InstructionPointer + 2)), 0); UInt16 absAddr = (UInt16)System.Math.Abs(addr); if ((Flags & 8) == 8) { if (addr >= 0) InstructionPointer += absAddr; else InstructionPointer -= absAddr; } else InstructionPointer += 2; if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x08: //JGE ADDR { var addr = System.BitConverter.ToUInt16(memory.Segment(InstructionPointer, (UInt16)(InstructionPointer + 2)), 0); UInt16 absAddr = (UInt16)System.Math.Abs(addr); if ((Flags & 4) == 4 || (Flags & 1) == 1) { if (addr >= 0) InstructionPointer += absAddr; else InstructionPointer -= absAddr; } else InstructionPointer += 2; if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x09: //JG ADDR { var addr = System.BitConverter.ToUInt16(memory.Segment(InstructionPointer, (UInt16)(InstructionPointer + 2)), 0); UInt16 absAddr = (UInt16)System.Math.Abs(addr); if ((Flags & 4) == 4) { if (addr >= 0) InstructionPointer += absAddr; else InstructionPointer -= absAddr; } else InstructionPointer += 2; if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x0a: //JE ADDR { var addr = System.BitConverter.ToUInt16(memory.Segment(InstructionPointer, (UInt16)(InstructionPointer + 2)), 0); UInt16 absAddr = (UInt16)System.Math.Abs(addr); if ((Flags & 1) == 1) { if (addr >= 0) InstructionPointer += absAddr; else InstructionPointer -= absAddr; } else InstructionPointer += 2; if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x0b: //JNE ADDR { var addr = System.BitConverter.ToUInt16(memory.Segment(InstructionPointer, (UInt16)(InstructionPointer + 2)), 0); UInt16 absAddr = (UInt16)System.Math.Abs(addr); if ((Flags & 2) == 2) { if (addr >= 0) InstructionPointer += absAddr; else InstructionPointer -= absAddr; } else InstructionPointer += 2; if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } case 0x11: //ADD R V { var leftRegisterID = (Register)memory[InstructionPointer]; var leftOperand = ReadFromRegister(leftRegisterID); var rightOperand = System.BitConverter.ToUInt16(memory.Segment((UInt16)(InstructionPointer + 1), (UInt16)(InstructionPointer + 3)), 0); var result = leftOperand + rightOperand; if (leftRegisterID == Register.AH || leftRegisterID == Register.AL) { if (result > 0xff) Flags = (byte)(Flags | 16); SetToRegister(leftRegisterID, (byte)result); } else { if (result > 0xffff) Flags = (byte)(Flags | 16); SetToRegister(leftRegisterID, (UInt16)result); } InstructionPointer += 3; if (RegisterStatusUpdateEvent != null) RegisterStatusUpdateEvent(); break; } default: throw new NotImplementedException(); } } Running = CPUStatus.Stop; }
private static Udbus.v4v.v4vConnection KeepTryingToConnectToV4V(Udbus.Serialization.UdbusDelegates.D_io_debug io_debug, System.Threading.ManualResetEvent stop, Udbus.Core.Logging.ILog log) { Udbus.v4v.v4vConnection connection = TryV4VConnection(io_debug, log); while (connection == null && stop.WaitOne(0) == false) { System.Threading.Thread.Sleep(ConnectionWaitMilliseconds); connection = TryV4VConnection(io_debug, log); } // Ends while failed to connect to V4V return connection; }