CpuThread.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using ChocolArm64.Memory;
  2. using ChocolArm64.State;
  3. using System;
  4. using System.Threading;
  5. namespace ChocolArm64
  6. {
  7. public class CpuThread
  8. {
  9. public CpuThreadState ThreadState { get; private set; }
  10. public MemoryManager Memory { get; private set; }
  11. private Translator _translator;
  12. public Thread Work;
  13. public event EventHandler WorkFinished;
  14. private int _isExecuting;
  15. public CpuThread(Translator translator, MemoryManager memory, long entryPoint)
  16. {
  17. _translator = translator;
  18. Memory = memory;
  19. ThreadState = new CpuThreadState();
  20. ThreadState.ExecutionMode = ExecutionMode.AArch64;
  21. ThreadState.Running = true;
  22. Work = new Thread(delegate()
  23. {
  24. translator.ExecuteSubroutine(this, entryPoint);
  25. memory.RemoveMonitor(ThreadState.Core);
  26. WorkFinished?.Invoke(this, EventArgs.Empty);
  27. });
  28. }
  29. public bool Execute()
  30. {
  31. if (Interlocked.Exchange(ref _isExecuting, 1) == 1)
  32. {
  33. return false;
  34. }
  35. Work.Start();
  36. return true;
  37. }
  38. public void StopExecution()
  39. {
  40. ThreadState.Running = false;
  41. }
  42. public void RequestInterrupt()
  43. {
  44. ThreadState.RequestInterrupt();
  45. }
  46. public bool IsCurrentThread()
  47. {
  48. return Thread.CurrentThread == Work;
  49. }
  50. }
  51. }