Cosmos/source/Cosmos.Core.Plugs/System/DateTimeImpl.cs
Unknown6656 1fd7a0367c Added ASM instructions and basic DateTime-implementation
Added the x86-instructions 'LEA', 'RDTSC' and 'RDMSR'.
Added a simplistic plug for 'system::datetime', which now contains the
method for fetching the current date/time
2016-08-14 06:06:30 +02:00

66 lines
1.5 KiB
C#

using System;
using Cosmos.IL2CPU.Plugs;
namespace Cosmos.Core.Plugs.System
{
[Plug(Target = typeof(DateTime))]
public class DateTimeImpl
{
public DateTime get_Now()
{
int[] raw = GetRawDate();
return new DateTime(
100 * BCDtoBIN(raw[10]) + BCDtoBIN(raw[9]), //YEAR
BCDtoBIN(raw[8]), //MONTH
BCDtoBIN(raw[7]), //DAY
BCDtoBIN(raw[4]), //HOUR
BCDtoBIN(raw[2]), //MINUTE
BCDtoBIN(raw[0]) //SECOND
);
}
public DateTime get_UtcNow()
{
// TODO: get timezone
return get_Now();
}
private static int[] GetRawDate()
{
IOPort p70 = new IOPort(0x70);
IOPort p71 = new IOPort(0x71);
int[] raw = new int[0x0b];
p70.Byte = 0x0b;
p71.Byte = 0x02; //24h format + BCD encoding
do
p70.Byte = 0x0a;
while ((p71.Byte & 0x80) == 0);
for (byte i = 0; i < 0x0a; i++)
{
p70.Byte = i;
raw[i] = p71.Word;
}
p70.Byte = 0x32;
raw[0x0a] = p71.Word;
return raw;
}
/// <summary>
/// Converts BCD-encoded numbers to `normal` (binary-encoded) numbers
/// </summary>
private static int BCDtoBIN(int bcd)
{
return ((bcd & 0xf0) >> 1) + ((bcd & 0xf0) >> 3) + (bcd & 0xf);
}
}
}