mirror of
https://github.com/danbulant/Cosmos
synced 2026-05-19 04:18:43 +00:00
Revert "Added Stopwatch and Thread.Sleep implementation + added some cpuid-functions"
This reverts commit 1d53deb45e.
This commit is contained in:
parent
1d53deb45e
commit
19bc57bf7f
8 changed files with 11 additions and 418 deletions
|
|
@ -1,106 +0,0 @@
|
|||
using System;
|
||||
|
||||
using Cosmos.Assembler;
|
||||
using Cosmos.Assembler.x86;
|
||||
using Cosmos.IL2CPU;
|
||||
using Cosmos.IL2CPU.Plugs;
|
||||
|
||||
namespace Cosmos.Core.Plugs
|
||||
{
|
||||
[Plug(Target = typeof(CPUID))]
|
||||
public static unsafe class CPUIDImpl
|
||||
{
|
||||
private static int* __vendortargetptr; // I declare this as an extra field due to reflection -- don't like it, but can't change it :/
|
||||
|
||||
[Inline]
|
||||
internal static void fetchcpuvendor(int* target)
|
||||
{
|
||||
/*
|
||||
* lea esi, target
|
||||
* xor eax, eax
|
||||
* cpuid
|
||||
* mov [esi], ebx
|
||||
* mov [esi + 4], edx
|
||||
* mov [esi + 8], ecx
|
||||
* ret
|
||||
*/
|
||||
__vendortargetptr = target;
|
||||
|
||||
string intname = LabelName.GetFullName(typeof(CPUImpl).GetField(nameof(__vendortargetptr)));
|
||||
|
||||
ElementReference targ = ElementReference.New(intname);
|
||||
new Lea
|
||||
{
|
||||
DestinationReg = RegistersEnum.ESI,
|
||||
SourceRef = targ,
|
||||
};
|
||||
new CpuId();
|
||||
new Mov
|
||||
{
|
||||
DestinationReg = RegistersEnum.ESI,
|
||||
DestinationIsIndirect = true,
|
||||
SourceReg = RegistersEnum.EBX,
|
||||
};
|
||||
new Mov
|
||||
{
|
||||
DestinationReg = RegistersEnum.ESI,
|
||||
DestinationIsIndirect = true,
|
||||
DestinationDisplacement = 4,
|
||||
SourceReg = RegistersEnum.EDX,
|
||||
};
|
||||
new Mov
|
||||
{
|
||||
DestinationReg = RegistersEnum.ESI,
|
||||
DestinationIsIndirect = true,
|
||||
DestinationDisplacement = 8,
|
||||
SourceReg = RegistersEnum.ECX,
|
||||
};
|
||||
new Return();
|
||||
}
|
||||
|
||||
[Inline]
|
||||
internal static int canreadcpuid()
|
||||
{
|
||||
/*
|
||||
* pushfd
|
||||
* pushfd
|
||||
* xor dword [esp], 00200000h
|
||||
* popfd
|
||||
* pushfd
|
||||
* pop eax
|
||||
* xor eax, [esp]
|
||||
* and eax, 00200000h
|
||||
* ret
|
||||
*/
|
||||
new Pushfd();
|
||||
new Pushfd();
|
||||
new Xor
|
||||
{
|
||||
DestinationReg = RegistersEnum.ESP,
|
||||
DestinationIsIndirect = true,
|
||||
SourceValue = 0x00200000
|
||||
};
|
||||
new Popfd();
|
||||
new Pushfd();
|
||||
new Pop
|
||||
{
|
||||
DestinationReg = RegistersEnum.EAX,
|
||||
};
|
||||
new Xor
|
||||
{
|
||||
DestinationReg = RegistersEnum.EAX,
|
||||
SourceReg = RegistersEnum.ESP,
|
||||
SourceIsIndirect = true
|
||||
};
|
||||
new Popfd();
|
||||
new And
|
||||
{
|
||||
DestinationReg = RegistersEnum.EAX,
|
||||
SourceValue = 0x00200000
|
||||
};
|
||||
new Return();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,8 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Cosmos.Assembler;
|
||||
using Cosmos.Assembler.x86;
|
||||
using Cosmos.IL2CPU.Plugs;
|
||||
using XSharp.Compiler;
|
||||
using Assembler = Cosmos.Assembler.Assembler;
|
||||
|
|
@ -11,9 +9,9 @@ using CPUx86 = Cosmos.Assembler.x86;
|
|||
|
||||
namespace Cosmos.Core.Plugs {
|
||||
[Plug(Target = typeof(Core.CPU))]
|
||||
public unsafe class CPUImpl {
|
||||
public class CPUImpl {
|
||||
[PlugMethod(Assembler = typeof(UpdateIDT))]
|
||||
public static void UpdateIDT(CPU aThis, bool aEnableInterruptsImmediately) {
|
||||
public static void UpdateIDT(CPU aThis, bool aEnableInterruptsImmediately) {
|
||||
}
|
||||
|
||||
public class GetAmountOfRAMAsm : AssemblerMethod {
|
||||
|
|
@ -138,195 +136,14 @@ namespace Cosmos.Core.Plugs {
|
|||
}
|
||||
}
|
||||
|
||||
// [PlugMethod(Assembler = typeof(P2.Assemblers.Interrupt30))]
|
||||
// public static void Interrupt30(ref uint aEAX, ref uint aEBX, ref uint aECX, ref uint aEDX) {
|
||||
// aEAX = 0;
|
||||
// }
|
||||
// [PlugMethod(Assembler = typeof(P2.Assemblers.Interrupt30))]
|
||||
// public static void Interrupt30(ref uint aEAX, ref uint aEBX, ref uint aECX, ref uint aEDX) {
|
||||
// aEAX = 0;
|
||||
// }
|
||||
|
||||
// [PlugMethod(Assembler = typeof(P2.Assemblers.GetMBIAddress))]
|
||||
// public static uint GetMBIAddress() {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
#region CPU Cycle + Frequency count
|
||||
|
||||
private static int* __cyclesrdtscptr; // I declare this as an extra field due to reflection -- don't like it, but can't change it :/
|
||||
private static int* __raterdmsrptr; // I declare this as an extra field due to reflection -- don't like it, but can't change it :/
|
||||
private static long __ticktate = -1;
|
||||
|
||||
public static long GetCycleCount()
|
||||
{
|
||||
int[] val = new int[2];
|
||||
|
||||
fixed (int* ptr = val)
|
||||
__cyclesrdtsc(ptr);
|
||||
|
||||
return ((long)val[0] << 32) | (uint)val[1];
|
||||
}
|
||||
|
||||
public static long GetCycleRate()
|
||||
{
|
||||
if (__ticktate == -1)
|
||||
{
|
||||
int[] raw = new int[4];
|
||||
|
||||
fixed (int* ptr = raw)
|
||||
__raterdmsr(ptr);
|
||||
|
||||
ulong l1 = ((ulong)raw[0] << 32) | (uint)raw[1];
|
||||
ulong l2 = ((ulong)raw[2] << 32) | (uint)raw[3];
|
||||
|
||||
__ticktate = (long)((double)l2 / (double)l1); // * cpu_rate
|
||||
}
|
||||
|
||||
return __ticktate;
|
||||
}
|
||||
|
||||
[Inline]
|
||||
private static void __cyclesrdtsc(int* target)
|
||||
{
|
||||
/*
|
||||
* push eax
|
||||
* push ecx
|
||||
* push edx
|
||||
* lea esi, target
|
||||
* rdtsc
|
||||
* mov [esi+4], eax
|
||||
* mov [esi], edx
|
||||
* pop edx
|
||||
* pop ecx
|
||||
* pop eax
|
||||
* ret
|
||||
*/
|
||||
__cyclesrdtscptr = target;
|
||||
|
||||
string intname = LabelName.GetFullName(typeof(CPUImpl).GetField(nameof(__cyclesrdtscptr)));
|
||||
|
||||
ElementReference targ = ElementReference.New(intname);
|
||||
new Push
|
||||
{
|
||||
DestinationReg = RegistersEnum.EAX
|
||||
};
|
||||
new Push
|
||||
{
|
||||
DestinationReg = RegistersEnum.ECX
|
||||
};
|
||||
new Push
|
||||
{
|
||||
DestinationReg = RegistersEnum.EDX
|
||||
};
|
||||
new Lea
|
||||
{
|
||||
DestinationReg = RegistersEnum.ESI,
|
||||
SourceRef = targ,
|
||||
};
|
||||
new Rdtsc();
|
||||
new Mov
|
||||
{
|
||||
DestinationReg = RegistersEnum.ESI,
|
||||
DestinationIsIndirect = true,
|
||||
DestinationDisplacement = 4,
|
||||
SourceReg = RegistersEnum.EAX,
|
||||
};
|
||||
new Mov
|
||||
{
|
||||
DestinationReg = RegistersEnum.ESI,
|
||||
DestinationIsIndirect = true,
|
||||
SourceReg = RegistersEnum.EDX,
|
||||
};
|
||||
new Pop
|
||||
{
|
||||
DestinationReg = RegistersEnum.EDX
|
||||
};
|
||||
new Pop
|
||||
{
|
||||
DestinationReg = RegistersEnum.ECX
|
||||
};
|
||||
new Pop
|
||||
{
|
||||
DestinationReg = RegistersEnum.EAX
|
||||
};
|
||||
new Return();
|
||||
}
|
||||
|
||||
[Inline]
|
||||
private static void __raterdmsr(int* target)
|
||||
{
|
||||
/*
|
||||
* ; esi register layout: (mperf_hi, mperf_lo, aperf_hi, aperf_lo)
|
||||
* ;
|
||||
* ; int* ptr = new int[4];
|
||||
* ;
|
||||
* lea esi, ptr ;equivalent with `mov esi, &ptr`
|
||||
* mov ecx, e7h
|
||||
* rdmsr
|
||||
* mov [esi + 4], eax
|
||||
* mov [esi], edx
|
||||
* mov ecx, e8h
|
||||
* rdmsr
|
||||
* mov [esi + 12], eax
|
||||
* mov [esi + 8], edx
|
||||
* xor eax, eax ;reset to zero
|
||||
* ret
|
||||
*/
|
||||
__raterdmsrptr = target;
|
||||
|
||||
string intname = LabelName.GetFullName(typeof(CPUImpl).GetField(nameof(__raterdmsrptr)));
|
||||
|
||||
ElementReference targ = ElementReference.New(intname);
|
||||
new Lea
|
||||
{
|
||||
DestinationReg = RegistersEnum.ESI,
|
||||
SourceRef = targ,
|
||||
};
|
||||
new Mov
|
||||
{
|
||||
DestinationReg = RegistersEnum.ECX,
|
||||
SourceValue = 0xe7,
|
||||
};
|
||||
new Rdmsr();
|
||||
new Mov
|
||||
{
|
||||
SourceReg = RegistersEnum.EAX,
|
||||
DestinationReg = RegistersEnum.ESI,
|
||||
DestinationIsIndirect = true,
|
||||
DestinationDisplacement = 4,
|
||||
};
|
||||
new Mov
|
||||
{
|
||||
SourceReg = RegistersEnum.EDX,
|
||||
DestinationReg = RegistersEnum.ESI,
|
||||
DestinationIsIndirect = true,
|
||||
DestinationDisplacement = 0,
|
||||
};
|
||||
new Mov
|
||||
{
|
||||
DestinationReg = RegistersEnum.ECX,
|
||||
SourceValue = 0xe8,
|
||||
};
|
||||
new Rdmsr();
|
||||
new Mov
|
||||
{
|
||||
SourceReg = RegistersEnum.EAX,
|
||||
DestinationReg = RegistersEnum.ESI,
|
||||
DestinationIsIndirect = true,
|
||||
DestinationDisplacement = 12,
|
||||
};
|
||||
new Mov
|
||||
{
|
||||
SourceReg = RegistersEnum.EDX,
|
||||
DestinationReg = RegistersEnum.ESI,
|
||||
DestinationIsIndirect = true,
|
||||
DestinationDisplacement = 8,
|
||||
};
|
||||
new Xor
|
||||
{
|
||||
SourceReg = RegistersEnum.EAX,
|
||||
DestinationReg = RegistersEnum.EAX,
|
||||
};
|
||||
new Return();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
// [PlugMethod(Assembler = typeof(P2.Assemblers.GetMBIAddress))]
|
||||
// public static uint GetMBIAddress() {
|
||||
// return 0;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,7 +93,6 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="CPUIDImpl.cs" />
|
||||
<Compile Include="CPUImpl.cs" />
|
||||
<Compile Include="IOPortImpl.cs" />
|
||||
<Compile Include="System\ArrayImpl.cs" />
|
||||
|
|
@ -108,7 +107,6 @@
|
|||
<Compile Include="System\Buffer.cs" />
|
||||
<Compile Include="System\DateTimeImpl.cs" />
|
||||
<Compile Include="System\DelegateImpl.cs" />
|
||||
<Compile Include="System\Diagnostics\StopwatchImpl.cs" />
|
||||
<Compile Include="System\IO\PathHelperImpl.cs" />
|
||||
<Compile Include="System\MulticastDelegateImpl.cs" />
|
||||
<Compile Include="System\NormalDelegateImpl.cs" />
|
||||
|
|
@ -122,7 +120,6 @@
|
|||
<Compile Include="System\Text\DecoderFallbackImpl.cs" />
|
||||
<Compile Include="System\Text\EncoderFallbackImpl.cs" />
|
||||
<Compile Include="System\Text\InternalDecoderBestFitFallbackBufferImpl.cs" />
|
||||
<Compile Include="System\Threading\ThreadImpl.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Cosmos.Assembler\Cosmos.Assembler.csproj">
|
||||
|
|
|
|||
|
|
@ -1,21 +0,0 @@
|
|||
using Cosmos.Assembler;
|
||||
using Cosmos.Assembler.x86;
|
||||
using Cosmos.IL2CPU.Plugs;
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Cosmos.Core.Plugs.System.Diagnostics
|
||||
{
|
||||
[Plug(Target = typeof(global::System.Diagnostics.Stopwatch))]
|
||||
public static class StopwatchImpl
|
||||
{
|
||||
public static long GetTimestamp()
|
||||
{
|
||||
if (Stopwatch.IsHighResolution)
|
||||
return (long)(CPU.GetCycleCount() / (double)CPU.GetCycleRate() * 1000000d); // see https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx
|
||||
else
|
||||
return DateTime.UtcNow.Ticks;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
using Cosmos.IL2CPU.Plugs;
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cosmos.Core.Plugs.System.Threading
|
||||
{
|
||||
[Plug(Target = typeof(global::System.Threading.Thread))]
|
||||
public static class ThreadImpl
|
||||
{
|
||||
public static void SleepInternal(int ms)
|
||||
{
|
||||
// Implementation of http://referencesource.microsoft.com/#mscorlib/system/threading/thread.cs,6a577476abf2f437,references
|
||||
// See URL for further details
|
||||
|
||||
if ((ms > 0) && (ms != Timeout.Infinite))
|
||||
{
|
||||
double ticks = ms / 1000d * Stopwatch.Frequency + CPU.GetCycleCount() / (double)0;
|
||||
|
||||
while (ticks < CPU.GetCycleCount() / (double)0)
|
||||
new Action(() => { }).Invoke(); // execute an empty operation
|
||||
}
|
||||
else if (ms < 0)
|
||||
throw new ThreadInterruptedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -60,19 +60,5 @@ namespace Cosmos.Core {
|
|||
mInterruptsEnabled = false;
|
||||
return xResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of CPU cycles since startup of the current CPU core
|
||||
/// </summary>
|
||||
/// <returns>Number of CPU cycles since startup</returns>
|
||||
[PlugMethod(PlugRequired = true)]
|
||||
public static long GetCycleCount() { return 0; } //plugged
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of CPU cycles per seconds
|
||||
/// </summary>
|
||||
/// <returns>Number of CPU cycles per seconds</returns>
|
||||
[PlugMethod(PlugRequired = true)]
|
||||
public static long GetCycleRate() { return 0; } //plugged
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
using System;
|
||||
|
||||
using Cosmos.IL2CPU.Plugs;
|
||||
|
||||
namespace Cosmos.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages basic CPU ID functions
|
||||
/// </summary>
|
||||
public static unsafe class CPUID
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the CPU vendor name
|
||||
/// </summary>
|
||||
/// <returns>CPU vendor name</returns>
|
||||
public static string GetVendorName()
|
||||
{
|
||||
if (canreadcpuid() > 0)
|
||||
{
|
||||
int[] raw = new int[3];
|
||||
|
||||
fixed (int* ptr = raw)
|
||||
fetchcpuvendor(ptr);
|
||||
|
||||
return new string(new char[] {
|
||||
(char)(raw[0] >> 24),
|
||||
(char)((raw[0] >> 16) & 0xff),
|
||||
(char)((raw[0] >> 8) & 0xff),
|
||||
(char)(raw[0] & 0xff),
|
||||
(char)(raw[1] >> 24),
|
||||
(char)((raw[1] >> 16) & 0xff),
|
||||
(char)((raw[1] >> 8) & 0xff),
|
||||
(char)(raw[1] & 0xff),
|
||||
(char)(raw[2] >> 24),
|
||||
(char)((raw[2] >> 16) & 0xff),
|
||||
(char)((raw[2] >> 8) & 0xff),
|
||||
(char)(raw[2] & 0xff),
|
||||
});
|
||||
}
|
||||
else
|
||||
return "\0";
|
||||
}
|
||||
|
||||
[PlugMethod(PlugRequired = true)]
|
||||
internal static extern int canreadcpuid(); // plugged;
|
||||
|
||||
[PlugMethod(PlugRequired = true)]
|
||||
internal static extern void fetchcpuvendor(int* target); // plugged;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -95,7 +95,6 @@
|
|||
<ItemGroup>
|
||||
<Compile Include="BaseIOGroups.cs" />
|
||||
<Compile Include="Bootstrap.cs" />
|
||||
<Compile Include="CPUID.cs" />
|
||||
<Compile Include="IOGroup\COM.cs" />
|
||||
<Compile Include="IOGroup\Network\AMDPCNetIIIOGroup.cs" />
|
||||
<Compile Include="IOGroup\PCSpeaker.cs" />
|
||||
|
|
|
|||
Loading…
Reference in a new issue