2018-02-04 18:08:20 -05:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using ChocolArm64.State;
|
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace ChocolArm64
|
|
|
|
{
|
2018-10-30 21:43:02 -04:00
|
|
|
public class CpuThread
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-10-30 21:43:02 -04:00
|
|
|
public CpuThreadState ThreadState { get; private set; }
|
|
|
|
public MemoryManager Memory { get; private set; }
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
private Translator _translator;
|
2018-02-13 21:43:08 -05:00
|
|
|
|
2018-09-18 19:36:43 -04:00
|
|
|
public Thread Work;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
public event EventHandler WorkFinished;
|
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
private int _isExecuting;
|
2018-02-13 21:43:08 -05:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
public CpuThread(Translator translator, MemoryManager memory, long entrypoint)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-10-30 21:43:02 -04:00
|
|
|
_translator = translator;
|
|
|
|
Memory = memory;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
ThreadState = new CpuThreadState();
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
ThreadState.ExecutionMode = ExecutionMode.AArch64;
|
2018-05-26 16:49:21 -04:00
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
ThreadState.Running = true;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
|
|
|
Work = new Thread(delegate()
|
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
translator.ExecuteSubroutine(this, entrypoint);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
memory.RemoveMonitor(ThreadState.Core);
|
2018-09-18 19:36:43 -04:00
|
|
|
|
|
|
|
WorkFinished?.Invoke(this, EventArgs.Empty);
|
|
|
|
});
|
2018-03-12 00:04:52 -04:00
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
public bool Execute()
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-10-30 21:43:02 -04:00
|
|
|
if (Interlocked.Exchange(ref _isExecuting, 1) == 1)
|
2018-02-13 21:43:08 -05:00
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
return false;
|
2018-02-13 21:43:08 -05:00
|
|
|
}
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
Work.Start();
|
2018-02-13 21:43:08 -05:00
|
|
|
|
|
|
|
return true;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-03-12 00:04:52 -04:00
|
|
|
|
2018-04-19 15:18:30 -04:00
|
|
|
public void StopExecution()
|
|
|
|
{
|
|
|
|
ThreadState.Running = false;
|
|
|
|
}
|
|
|
|
|
2018-09-18 19:36:43 -04:00
|
|
|
public void RequestInterrupt()
|
|
|
|
{
|
|
|
|
ThreadState.RequestInterrupt();
|
|
|
|
}
|
|
|
|
|
2018-04-19 15:18:30 -04:00
|
|
|
public bool IsCurrentThread()
|
|
|
|
{
|
|
|
|
return Thread.CurrentThread == Work;
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|