mirror of
https://github.com/par274/sharpemu.git
synced 2026-07-26 04:39:17 +08:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0565d01744 | |||
| 56bf00e9a5 | |||
| 46b729c5b4 | |||
| 4b7df8623a | |||
| 3c2134474d | |||
| 298ef01809 | |||
| 2060dacaf1 |
@@ -1992,9 +1992,16 @@ public sealed unsafe partial class DirectExecutionBackend : INativeCpuBackend, I
|
||||
|
||||
byte* code = (byte*)ptr;
|
||||
int offset = 0;
|
||||
EmitByte(code, ref offset, 0x48); // sub rsp, 0x20
|
||||
// TlsGetValue returns its TLS pointer in RAX. Preserve the guest return value
|
||||
// above the 32-byte Windows shadow space while keeping the call site aligned.
|
||||
EmitByte(code, ref offset, 0x48); // sub rsp, 0x30
|
||||
EmitByte(code, ref offset, 0x83);
|
||||
EmitByte(code, ref offset, 0xEC);
|
||||
EmitByte(code, ref offset, 0x30);
|
||||
EmitByte(code, ref offset, 0x48); // mov [rsp+0x20], rax
|
||||
EmitByte(code, ref offset, 0x89);
|
||||
EmitByte(code, ref offset, 0x44);
|
||||
EmitByte(code, ref offset, 0x24);
|
||||
EmitByte(code, ref offset, 0x20);
|
||||
EmitByte(code, ref offset, 0xB9); // mov ecx, tlsIndex
|
||||
EmitUInt32(code, ref offset, _hostRspSlotTlsIndex);
|
||||
@@ -2004,13 +2011,21 @@ public sealed unsafe partial class DirectExecutionBackend : INativeCpuBackend, I
|
||||
offset += sizeof(ulong);
|
||||
EmitByte(code, ref offset, 0xFF); // call rax
|
||||
EmitByte(code, ref offset, 0xD0);
|
||||
EmitByte(code, ref offset, 0x48); // add rsp, 0x20
|
||||
EmitByte(code, ref offset, 0x49); // mov r11, rax
|
||||
EmitByte(code, ref offset, 0x89);
|
||||
EmitByte(code, ref offset, 0xC3);
|
||||
EmitByte(code, ref offset, 0x48); // mov rax, [rsp+0x20]
|
||||
EmitByte(code, ref offset, 0x8B);
|
||||
EmitByte(code, ref offset, 0x44);
|
||||
EmitByte(code, ref offset, 0x24);
|
||||
EmitByte(code, ref offset, 0x20);
|
||||
EmitByte(code, ref offset, 0x48); // add rsp, 0x30
|
||||
EmitByte(code, ref offset, 0x83);
|
||||
EmitByte(code, ref offset, 0xC4);
|
||||
EmitByte(code, ref offset, 0x20);
|
||||
EmitByte(code, ref offset, 0x48); // mov rsp, [rax]
|
||||
EmitByte(code, ref offset, 0x30);
|
||||
EmitByte(code, ref offset, 0x49); // mov rsp, [r11]
|
||||
EmitByte(code, ref offset, 0x8B);
|
||||
EmitByte(code, ref offset, 0x20);
|
||||
EmitByte(code, ref offset, 0x23);
|
||||
EmitHostNonvolatileXmmRestore(code, ref offset);
|
||||
EmitByte(code, ref offset, 0x41); EmitByte(code, ref offset, 0x5F);
|
||||
EmitByte(code, ref offset, 0x41); EmitByte(code, ref offset, 0x5E);
|
||||
|
||||
@@ -83,7 +83,6 @@ public sealed class SharpEmuRuntime : ISharpEmuRuntime
|
||||
ImportTraceLimit = Math.Max(0, options.ImportTraceLimit),
|
||||
};
|
||||
var moduleManager = new ModuleManager();
|
||||
moduleManager.RegisterFromAssembly(typeof(VideoOutExports).Assembly, Generation.Gen4 | Generation.Gen5, Aerolib.Instance);
|
||||
moduleManager.RegisterFromAssembly(typeof(KernelExports).Assembly, Generation.Gen4 | Generation.Gen5, Aerolib.Instance);
|
||||
moduleManager.Freeze();
|
||||
|
||||
|
||||
@@ -718,7 +718,7 @@ internal static class Gen5ShaderTranslator
|
||||
}
|
||||
|
||||
var src0 = word & 0x1FF;
|
||||
sizeDwords = opcode is 0x20 or 0x21 || src0 is 0xF9 or 0xFA or 0xFF ? 2u : 1u;
|
||||
sizeDwords = opcode is 0x20 or 0x21 or 0x2C or 0x2D || src0 is 0xF9 or 0xFA or 0xFF ? 2u : 1u;
|
||||
error = string.Empty;
|
||||
name = opcode switch
|
||||
{
|
||||
@@ -757,7 +757,9 @@ internal static class Gen5ShaderTranslator
|
||||
0x28 => "VAddcU32",
|
||||
0x29 => "VSubbU32",
|
||||
0x2A => "VSubbrevU32",
|
||||
0x2B => "VLdexpF32",
|
||||
0x2B => "VFmacF32",
|
||||
0x2C => "VFmamkF32",
|
||||
0x2D => "VFmaakF32",
|
||||
0x2F => "VCvtPkrtzF16F32",
|
||||
0x30 => "VCvtPkU16U32",
|
||||
0x31 => "VCvtPkI16I32",
|
||||
@@ -870,6 +872,7 @@ internal static class Gen5ShaderTranslator
|
||||
0x10F => "VMinF32",
|
||||
0x110 => "VMaxF32",
|
||||
0x11F => "VMacF32",
|
||||
0x12B => "VFmacF32",
|
||||
0x12F => "VCvtPkrtzF16F32",
|
||||
0x141 => "VMadF32",
|
||||
0x143 => "VMadU32U24",
|
||||
@@ -897,6 +900,7 @@ internal static class Gen5ShaderTranslator
|
||||
0x169 => "VMulLoU32",
|
||||
0x16A => "VMulHiU32",
|
||||
0x16B => "VMulLoI32",
|
||||
0x16C => "VMulHiI32",
|
||||
0x360 => "VMadU32U16",
|
||||
0x361 => "VMulLoU32",
|
||||
0x362 => "VLdexpF32",
|
||||
@@ -1379,7 +1383,7 @@ internal static class Gen5ShaderTranslator
|
||||
Gen5Operand.Source(word & 0x1FF, literal),
|
||||
Gen5Operand.Vector((word >> 9) & 0xFF),
|
||||
];
|
||||
if (opcode == "VMadMkF32" && literal.HasValue)
|
||||
if (opcode is "VMadMkF32" or "VFmamkF32" && literal.HasValue)
|
||||
{
|
||||
sources =
|
||||
[
|
||||
@@ -1388,7 +1392,7 @@ internal static class Gen5ShaderTranslator
|
||||
sources[1],
|
||||
];
|
||||
}
|
||||
else if (opcode == "VMadAkF32" && literal.HasValue)
|
||||
else if (opcode is "VMadAkF32" or "VFmaakF32" && literal.HasValue)
|
||||
{
|
||||
sources =
|
||||
[
|
||||
|
||||
@@ -255,6 +255,8 @@ internal static partial class Gen5SpirvTranslator
|
||||
case "VFmaF32":
|
||||
case "VMadMkF32":
|
||||
case "VMadAkF32":
|
||||
case "VFmamkF32":
|
||||
case "VFmaakF32":
|
||||
result = EmitFloatResult(
|
||||
instruction,
|
||||
Ext(
|
||||
@@ -265,6 +267,7 @@ internal static partial class Gen5SpirvTranslator
|
||||
GetFloatSource(instruction, 2)));
|
||||
break;
|
||||
case "VMacF32":
|
||||
case "VFmacF32":
|
||||
{
|
||||
var addend = Bitcast(_floatType, LoadV(destination));
|
||||
result = EmitFloatResult(
|
||||
@@ -334,6 +337,7 @@ internal static partial class Gen5SpirvTranslator
|
||||
result = EmitSubtractWithBorrow(instruction, reverse: true);
|
||||
break;
|
||||
case "VMulLoU32":
|
||||
case "VMulLoI32":
|
||||
case "VMulU32U24":
|
||||
result = EmitIntegerBinary(instruction, SpirvOp.IMul);
|
||||
break;
|
||||
@@ -369,6 +373,29 @@ internal static partial class Gen5SpirvTranslator
|
||||
_module.Constant64(_ulongType, 32)));
|
||||
break;
|
||||
}
|
||||
case "VMulHiI32":
|
||||
{
|
||||
var wideLeft = _module.AddInstruction(
|
||||
SpirvOp.SConvert,
|
||||
_longType,
|
||||
Bitcast(_intType, GetRawSource(instruction, 0)));
|
||||
var wideRight = _module.AddInstruction(
|
||||
SpirvOp.SConvert,
|
||||
_longType,
|
||||
Bitcast(_intType, GetRawSource(instruction, 1)));
|
||||
var product = _module.AddInstruction(
|
||||
SpirvOp.IMul,
|
||||
_longType,
|
||||
wideLeft,
|
||||
wideRight);
|
||||
result = _module.AddInstruction(
|
||||
SpirvOp.UConvert,
|
||||
_uintType,
|
||||
ShiftRightLogical64(
|
||||
Bitcast(_ulongType, product),
|
||||
_module.Constant64(_ulongType, 32)));
|
||||
break;
|
||||
}
|
||||
case "VMadU32U24":
|
||||
{
|
||||
var left = BitwiseAnd(
|
||||
|
||||
@@ -9,6 +9,189 @@ namespace SharpEmu.Libs.Rtc;
|
||||
public static class RtcExports
|
||||
{
|
||||
private const long DateTimeTicksPerMicrosecond = 10;
|
||||
private const ulong MicrosecondsPerSecond = 1_000_000UL;
|
||||
private const ulong MicrosecondsPerMinute = 60_000_000UL;
|
||||
private const ulong MicrosecondsPerHour = 3_600_000_000UL;
|
||||
private const ulong MicrosecondsPerDay = 86_400_000_000UL;
|
||||
private const ulong MicrosecondsPerWeek = 604_800_000_000UL;
|
||||
private const ulong UnixEpochTicks = 62_135_596_800_000_000UL;
|
||||
private const ulong Win32FileTimeEpochTicks = 50_491_123_200_000_000UL;
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "lPEBYdVX0XQ",
|
||||
ExportName = "sceRtcCheckValid",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcCheckValid(CpuContext ctx)
|
||||
{
|
||||
var timeAddress = ctx[CpuRegister.Rdi];
|
||||
if (timeAddress == 0)
|
||||
{
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
if (!TryReadRtcDateTime(ctx, timeAddress, out var time))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
return ValidateRtcDateTime(time);
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "fNaZ4DbzHAE",
|
||||
ExportName = "sceRtcCompareTick",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcCompareTick(CpuContext ctx)
|
||||
{
|
||||
var tick1Address = ctx[CpuRegister.Rdi];
|
||||
var tick2Address = ctx[CpuRegister.Rsi];
|
||||
if (tick1Address == 0 || tick2Address == 0)
|
||||
{
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
if (!ctx.TryReadUInt64(tick1Address, out var tick1) || !ctx.TryReadUInt64(tick2Address, out var tick2))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
return tick1 < tick2 ? -1 : tick1 > tick2 ? 1 : 0;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "8Yr143yEnRo",
|
||||
ExportName = "sceRtcConvertLocalTimeToUtc",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcConvertLocalTimeToUtc(CpuContext ctx)
|
||||
{
|
||||
var tickLocalAddress = ctx[CpuRegister.Rdi];
|
||||
var tickUtcAddress = ctx[CpuRegister.Rsi];
|
||||
if (tickLocalAddress == 0 || tickUtcAddress == 0)
|
||||
{
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
if (!ctx.TryReadUInt64(tickLocalAddress, out var tickLocal))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
if (!TryConvertTickToDateTime(tickLocal, out var localDateTime))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
DateTime utcDateTime;
|
||||
try
|
||||
{
|
||||
utcDateTime = TimeZoneInfo.ConvertTimeToUtc(localDateTime, TimeZoneInfo.Local);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (!TryWriteTickFromDateTime(ctx, tickUtcAddress, utcDateTime))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "M1TvFst-jrM",
|
||||
ExportName = "sceRtcConvertUtcToLocalTime",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcConvertUtcToLocalTime(CpuContext ctx)
|
||||
{
|
||||
var tickUtcAddress = ctx[CpuRegister.Rdi];
|
||||
var tickLocalAddress = ctx[CpuRegister.Rsi];
|
||||
if (tickUtcAddress == 0 || tickLocalAddress == 0)
|
||||
{
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
if (!ctx.TryReadUInt64(tickUtcAddress, out var tickUtc))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
if (!TryConvertTickToDateTime(tickUtc, out var utcDateTime))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
DateTime localDateTime;
|
||||
try
|
||||
{
|
||||
localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, TimeZoneInfo.Local);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (!TryWriteTickFromDateTime(ctx, tickLocalAddress, localDateTime))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "8SljQx6pDP8",
|
||||
ExportName = "sceRtcEnd",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcEnd(CpuContext ctx)
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "LN3Zcb72Q0c",
|
||||
ExportName = "sceRtcGetCurrentAdNetworkTick",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcGetCurrentAdNetworkTick(CpuContext ctx) => RtcGetCurrentTick(ctx);
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "8lfvnRMqwEM",
|
||||
ExportName = "sceRtcGetCurrentClock",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcGetCurrentClock(CpuContext ctx)
|
||||
{
|
||||
var timeAddress = ctx[CpuRegister.Rdi];
|
||||
if (timeAddress == 0)
|
||||
{
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
var timeZoneMinutes = unchecked((int)ctx[CpuRegister.Rsi]);
|
||||
DateTimeOffset currentTime;
|
||||
try
|
||||
{
|
||||
currentTime = DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromMinutes(timeZoneMinutes));
|
||||
}
|
||||
catch (ArgumentOutOfRangeException)
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (!TryWriteRtcDateTime(ctx, timeAddress, ToRtcDateTime(currentTime.DateTime)))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "ZPD1YOKI+Kw",
|
||||
@@ -20,30 +203,38 @@ public static class RtcExports
|
||||
var timeAddress = ctx[CpuRegister.Rdi];
|
||||
if (timeAddress == 0)
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
var now = DateTimeOffset.Now;
|
||||
Span<byte> rtcDateTime = stackalloc byte[16];
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(rtcDateTime[0..2], checked((ushort)now.Year));
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(rtcDateTime[2..4], checked((ushort)now.Month));
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(rtcDateTime[4..6], checked((ushort)now.Day));
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(rtcDateTime[6..8], checked((ushort)now.Hour));
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(rtcDateTime[8..10], checked((ushort)now.Minute));
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(rtcDateTime[10..12], checked((ushort)now.Second));
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(
|
||||
rtcDateTime[12..16],
|
||||
checked((uint)((now.Ticks % TimeSpan.TicksPerSecond) / 10)));
|
||||
|
||||
if (!ctx.Memory.TryWrite(timeAddress, rtcDateTime))
|
||||
if (!TryWriteRtcDateTime(ctx, timeAddress, ToRtcDateTime(DateTimeOffset.Now.DateTime)))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
ctx[CpuRegister.Rax] = 0;
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "Ot1DE3gif84",
|
||||
ExportName = "sceRtcGetCurrentDebugNetworkTick",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcGetCurrentDebugNetworkTick(CpuContext ctx) => RtcGetCurrentTick(ctx);
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "zO9UL3qIINQ",
|
||||
ExportName = "sceRtcGetCurrentNetworkTick",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcGetCurrentNetworkTick(CpuContext ctx) => RtcGetCurrentTick(ctx);
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "HWxHOdbM-Pg",
|
||||
ExportName = "sceRtcGetCurrentRawNetworkTick",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcGetCurrentRawNetworkTick(CpuContext ctx) => RtcGetCurrentTick(ctx);
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "18B2NS1y9UU",
|
||||
ExportName = "sceRtcGetCurrentTick",
|
||||
@@ -54,7 +245,7 @@ public static class RtcExports
|
||||
var tickAddress = ctx[CpuRegister.Rdi];
|
||||
if (tickAddress == 0)
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
var tickValue = unchecked((ulong)(DateTime.UtcNow.Ticks / DateTimeTicksPerMicrosecond));
|
||||
@@ -63,7 +254,87 @@ public static class RtcExports
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
ctx[CpuRegister.Rax] = 0;
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "CyIK-i4XdgQ",
|
||||
ExportName = "sceRtcGetDayOfWeek",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcGetDayOfWeek(CpuContext ctx)
|
||||
{
|
||||
var year = unchecked((int)ctx[CpuRegister.Rdi]);
|
||||
var month = unchecked((int)ctx[CpuRegister.Rsi]);
|
||||
var day = unchecked((int)ctx[CpuRegister.Rdx]);
|
||||
if (!IsValidCalendarDate(year, month, day))
|
||||
{
|
||||
return unchecked((int)0x80B50004);
|
||||
}
|
||||
|
||||
return (int)new DateTime(year, month, day).DayOfWeek;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "3O7Ln8AqJ1o",
|
||||
ExportName = "sceRtcGetDaysInMonth",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcGetDaysInMonth(CpuContext ctx)
|
||||
{
|
||||
var year = unchecked((int)ctx[CpuRegister.Rdi]);
|
||||
var month = unchecked((int)ctx[CpuRegister.Rsi]);
|
||||
if (year < 1 || year > 9999)
|
||||
{
|
||||
return unchecked((int)0x80B50008);
|
||||
}
|
||||
|
||||
if (month < 1 || month > 12)
|
||||
{
|
||||
return unchecked((int)0x80B50009);
|
||||
}
|
||||
|
||||
return DateTime.DaysInMonth(year, month);
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "E7AR4o7Ny7E",
|
||||
ExportName = "sceRtcGetDosTime",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcGetDosTime(CpuContext ctx)
|
||||
{
|
||||
var timeAddress = ctx[CpuRegister.Rdi];
|
||||
var dosTimeAddress = ctx[CpuRegister.Rsi];
|
||||
if (timeAddress == 0 || dosTimeAddress == 0)
|
||||
{
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
if (!TryReadRtcDateTime(ctx, timeAddress, out var time))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
var validationResult = ValidateRtcDateTime(time);
|
||||
if (validationResult != (int)OrbisGen2Result.ORBIS_GEN2_OK)
|
||||
{
|
||||
return validationResult;
|
||||
}
|
||||
|
||||
uint dosTime = 0;
|
||||
dosTime |= (uint)((time.Second / 2) & 0x1F);
|
||||
dosTime |= (uint)(time.Minute & 0x3F) << 5;
|
||||
dosTime |= (uint)(time.Hour & 0x1F) << 11;
|
||||
dosTime |= (uint)(time.Day & 0x1F) << 16;
|
||||
dosTime |= (uint)(time.Month & 0x0F) << 21;
|
||||
dosTime |= (uint)((time.Year - 1980) & 0x7F) << 25;
|
||||
|
||||
if (!ctx.TryWriteUInt32(dosTimeAddress, dosTime))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
@@ -74,45 +345,220 @@ public static class RtcExports
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcGetTick(CpuContext ctx)
|
||||
{
|
||||
var dateTimeAddress = ctx[CpuRegister.Rdi];
|
||||
var timeAddress = ctx[CpuRegister.Rdi];
|
||||
var tickAddress = ctx[CpuRegister.Rsi];
|
||||
if (dateTimeAddress == 0 || tickAddress == 0)
|
||||
if (timeAddress == 0 || tickAddress == 0)
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
if (!TryReadRtcDateTime(ctx, dateTimeAddress, out var rtcDateTime))
|
||||
if (!TryReadRtcDateTime(ctx, timeAddress, out var time))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
ulong tickValue;
|
||||
try
|
||||
var validationResult = ValidateRtcDateTime(time);
|
||||
if (validationResult != (int)OrbisGen2Result.ORBIS_GEN2_OK)
|
||||
{
|
||||
var baseDateTime = new DateTime(
|
||||
rtcDateTime.Year,
|
||||
rtcDateTime.Month,
|
||||
rtcDateTime.Day,
|
||||
rtcDateTime.Hour,
|
||||
rtcDateTime.Minute,
|
||||
rtcDateTime.Second,
|
||||
DateTimeKind.Utc);
|
||||
tickValue = checked((ulong)((baseDateTime.Ticks / DateTimeTicksPerMicrosecond) + rtcDateTime.Microsecond));
|
||||
return validationResult;
|
||||
}
|
||||
catch (Exception ex) when (ex is ArgumentOutOfRangeException or OverflowException)
|
||||
|
||||
if (!TryConvertRtcDateTimeToTick(time, out var tick))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (!ctx.TryWriteUInt64(tickAddress, tickValue))
|
||||
if (!ctx.TryWriteUInt64(tickAddress, tick))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
ctx[CpuRegister.Rax] = 0;
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "jMNwqYr4R-k",
|
||||
ExportName = "sceRtcGetTickResolution",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcGetTickResolution(CpuContext ctx)
|
||||
{
|
||||
return (int)MicrosecondsPerSecond;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "BtqmpTRXHgk",
|
||||
ExportName = "sceRtcGetTime_t",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcGetTimeT(CpuContext ctx)
|
||||
{
|
||||
var timeAddress = ctx[CpuRegister.Rdi];
|
||||
var timeTAddress = ctx[CpuRegister.Rsi];
|
||||
if (timeAddress == 0 || timeTAddress == 0)
|
||||
{
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
if (!TryReadRtcDateTime(ctx, timeAddress, out var time))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
var validationResult = ValidateRtcDateTime(time);
|
||||
if (validationResult != (int)OrbisGen2Result.ORBIS_GEN2_OK)
|
||||
{
|
||||
return validationResult;
|
||||
}
|
||||
|
||||
if (!TryConvertRtcDateTimeToTick(time, out var tick))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
var unixSeconds = tick < UnixEpochTicks ? 0UL : (tick - UnixEpochTicks) / MicrosecondsPerSecond;
|
||||
if (!ctx.TryWriteUInt64(timeTAddress, unixSeconds))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "jfRO0uTjtzA",
|
||||
ExportName = "sceRtcGetWin32FileTime",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcGetWin32FileTime(CpuContext ctx)
|
||||
{
|
||||
var timeAddress = ctx[CpuRegister.Rdi];
|
||||
var fileTimeAddress = ctx[CpuRegister.Rsi];
|
||||
if (timeAddress == 0 || fileTimeAddress == 0)
|
||||
{
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
if (!TryReadRtcDateTime(ctx, timeAddress, out var time))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
var validationResult = ValidateRtcDateTime(time);
|
||||
if (validationResult != (int)OrbisGen2Result.ORBIS_GEN2_OK)
|
||||
{
|
||||
return validationResult;
|
||||
}
|
||||
|
||||
if (!TryConvertRtcDateTimeToTick(time, out var tick))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
var win32Time = tick < Win32FileTimeEpochTicks ? 0UL : (tick - Win32FileTimeEpochTicks) * 10UL;
|
||||
if (!ctx.TryWriteUInt64(fileTimeAddress, win32Time))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "LlodCMDbk3o",
|
||||
ExportName = "sceRtcInit",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcInit(CpuContext ctx)
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "Ug8pCwQvh0c",
|
||||
ExportName = "sceRtcIsLeapYear",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcIsLeapYear(CpuContext ctx)
|
||||
{
|
||||
var year = unchecked((int)ctx[CpuRegister.Rdi]);
|
||||
if (year < 1 || year > 9999)
|
||||
{
|
||||
return unchecked((int)0x80B50008);
|
||||
}
|
||||
|
||||
return DateTime.IsLeapYear(year) ? 1 : 0;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "NR1J0N7L2xY",
|
||||
ExportName = "sceRtcTickAddDays",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcTickAddDays(CpuContext ctx) => AddTickDelta(ctx, MicrosecondsPerDay);
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "MDc5cd8HfCA",
|
||||
ExportName = "sceRtcTickAddHours",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcTickAddHours(CpuContext ctx) => AddTickDelta(ctx, MicrosecondsPerHour);
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "XPIiw58C+GM",
|
||||
ExportName = "sceRtcTickAddMicroseconds",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcTickAddMicroseconds(CpuContext ctx) => AddTickDelta(ctx, 1UL);
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "mn-tf4QiFzk",
|
||||
ExportName = "sceRtcTickAddMinutes",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcTickAddMinutes(CpuContext ctx) => AddTickDelta(ctx, MicrosecondsPerMinute);
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "CL6y9q-XbuQ",
|
||||
ExportName = "sceRtcTickAddMonths",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcTickAddMonths(CpuContext ctx)
|
||||
{
|
||||
return AddCalendarDelta(ctx, dateTime => dateTime.AddMonths(unchecked((int)ctx[CpuRegister.Rdx])));
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "07O525HgICs",
|
||||
ExportName = "sceRtcTickAddSeconds",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcTickAddSeconds(CpuContext ctx) => AddTickDelta(ctx, MicrosecondsPerSecond);
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "AqVMssr52Rc",
|
||||
ExportName = "sceRtcTickAddTicks",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcTickAddTicks(CpuContext ctx) => AddTickDelta(ctx, 1UL);
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "gI4t194c2W8",
|
||||
ExportName = "sceRtcTickAddWeeks",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcTickAddWeeks(CpuContext ctx) => AddTickDelta(ctx, MicrosecondsPerWeek);
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "-5y2uJ62qS8",
|
||||
ExportName = "sceRtcTickAddYears",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcTickAddYears(CpuContext ctx)
|
||||
{
|
||||
return AddCalendarDelta(ctx, dateTime => dateTime.AddYears(unchecked((int)ctx[CpuRegister.Rdx])));
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "ueega6v3GUw",
|
||||
ExportName = "sceRtcSetTick",
|
||||
@@ -120,11 +566,11 @@ public static class RtcExports
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcSetTick(CpuContext ctx)
|
||||
{
|
||||
var dateTimeAddress = ctx[CpuRegister.Rdi];
|
||||
var timeAddress = ctx[CpuRegister.Rdi];
|
||||
var tickAddress = ctx[CpuRegister.Rsi];
|
||||
if (dateTimeAddress == 0 || tickAddress == 0)
|
||||
if (timeAddress == 0 || tickAddress == 0)
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
if (!ctx.TryReadUInt64(tickAddress, out var tickValue))
|
||||
@@ -132,40 +578,249 @@ public static class RtcExports
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
if (tickValue > long.MaxValue / DateTimeTicksPerMicrosecond)
|
||||
if (!TryConvertTickToRtcDateTime(tickValue, out var time))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
return unchecked((int)0x80B50004);
|
||||
}
|
||||
|
||||
DateTime dateTime;
|
||||
try
|
||||
{
|
||||
dateTime = new DateTime(checked((long)tickValue * DateTimeTicksPerMicrosecond), DateTimeKind.Utc);
|
||||
}
|
||||
catch (ArgumentOutOfRangeException)
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (!TryWriteRtcDateTime(ctx, dateTimeAddress, dateTime))
|
||||
if (!TryWriteRtcDateTime(ctx, timeAddress, time))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
ctx[CpuRegister.Rax] = 0;
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
private static bool TryReadRtcDateTime(CpuContext ctx, ulong address, out RtcDateTime rtcDateTime)
|
||||
[SysAbiExport(
|
||||
Nid = "aYPCd1cChyg",
|
||||
ExportName = "sceRtcSetDosTime",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcSetDosTime(CpuContext ctx)
|
||||
{
|
||||
var timeAddress = ctx[CpuRegister.Rdi];
|
||||
if (timeAddress == 0)
|
||||
{
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
var dosTime = unchecked((uint)ctx[CpuRegister.Rsi]);
|
||||
var time = new RtcDateTime(
|
||||
(ushort)(1980 + ((dosTime >> 25) & 0x7F)),
|
||||
(ushort)((dosTime >> 21) & 0x0F),
|
||||
(ushort)((dosTime >> 16) & 0x1F),
|
||||
(ushort)((dosTime >> 11) & 0x1F),
|
||||
(ushort)((dosTime >> 5) & 0x3F),
|
||||
(ushort)((dosTime << 1) & 0x3E),
|
||||
0);
|
||||
|
||||
if (!TryWriteRtcDateTime(ctx, timeAddress, time))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "bDEVVP4bTjQ",
|
||||
ExportName = "sceRtcSetTime_t",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcSetTimeT(CpuContext ctx)
|
||||
{
|
||||
var timeAddress = ctx[CpuRegister.Rdi];
|
||||
if (timeAddress == 0)
|
||||
{
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
var timeSeconds = unchecked((long)ctx[CpuRegister.Rsi]);
|
||||
if (timeSeconds < 0)
|
||||
{
|
||||
return unchecked((int)0x80B50003);
|
||||
}
|
||||
|
||||
var tick = UnixEpochTicks + unchecked((ulong)timeSeconds) * MicrosecondsPerSecond;
|
||||
if (!TryConvertTickToRtcDateTime(tick, out var time))
|
||||
{
|
||||
return unchecked((int)0x80B50003);
|
||||
}
|
||||
|
||||
if (!TryWriteRtcDateTime(ctx, timeAddress, time))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
[SysAbiExport(
|
||||
Nid = "n5JiAJXsbcs",
|
||||
ExportName = "sceRtcSetWin32FileTime",
|
||||
Target = Generation.Gen4 | Generation.Gen5,
|
||||
LibraryName = "libSceRtc")]
|
||||
public static int RtcSetWin32FileTime(CpuContext ctx)
|
||||
{
|
||||
var timeAddress = ctx[CpuRegister.Rdi];
|
||||
if (timeAddress == 0)
|
||||
{
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
var fileTime = unchecked((long)ctx[CpuRegister.Rsi]);
|
||||
if (fileTime < 0)
|
||||
{
|
||||
return unchecked((int)0x80B50003);
|
||||
}
|
||||
|
||||
var tick = Win32FileTimeEpochTicks + unchecked((ulong)fileTime / 10UL);
|
||||
if (!TryConvertTickToRtcDateTime(tick, out var time))
|
||||
{
|
||||
return unchecked((int)0x80B50003);
|
||||
}
|
||||
|
||||
if (!TryWriteRtcDateTime(ctx, timeAddress, time))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
private static int AddTickDelta(CpuContext ctx, ulong microsecondsPerUnit)
|
||||
{
|
||||
var destinationAddress = ctx[CpuRegister.Rdi];
|
||||
var sourceAddress = ctx[CpuRegister.Rsi];
|
||||
var delta = unchecked((long)ctx[CpuRegister.Rdx]);
|
||||
if (destinationAddress == 0 || sourceAddress == 0)
|
||||
{
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
if (!ctx.TryReadUInt64(sourceAddress, out var sourceTick))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var resultTick = checked((long)sourceTick + checked(delta * (long)microsecondsPerUnit));
|
||||
if (resultTick < 0)
|
||||
{
|
||||
return unchecked((int)0x80B50003);
|
||||
}
|
||||
|
||||
if (!ctx.TryWriteUInt64(destinationAddress, unchecked((ulong)resultTick)))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
}
|
||||
catch (OverflowException)
|
||||
{
|
||||
return unchecked((int)0x80B50003);
|
||||
}
|
||||
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
private static int AddCalendarDelta(CpuContext ctx, Func<DateTime, DateTime> transform)
|
||||
{
|
||||
var destinationAddress = ctx[CpuRegister.Rdi];
|
||||
var sourceAddress = ctx[CpuRegister.Rsi];
|
||||
if (destinationAddress == 0 || sourceAddress == 0)
|
||||
{
|
||||
return unchecked((int)0x80B50002);
|
||||
}
|
||||
|
||||
if (!ctx.TryReadUInt64(sourceAddress, out var sourceTick))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
if (!TryConvertTickToDateTime(sourceTick, out var sourceDateTime))
|
||||
{
|
||||
return unchecked((int)0x80B50003);
|
||||
}
|
||||
|
||||
DateTime resultDateTime;
|
||||
try
|
||||
{
|
||||
resultDateTime = transform(sourceDateTime);
|
||||
}
|
||||
catch (ArgumentOutOfRangeException)
|
||||
{
|
||||
return unchecked((int)0x80B50003);
|
||||
}
|
||||
|
||||
if (!TryWriteTickFromDateTime(ctx, destinationAddress, resultDateTime))
|
||||
{
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
private static int ValidateRtcDateTime(RtcDateTime time)
|
||||
{
|
||||
if (time.Year < 1 || time.Year > 9999)
|
||||
{
|
||||
return unchecked((int)0x80B50008);
|
||||
}
|
||||
|
||||
if (time.Month < 1 || time.Month > 12)
|
||||
{
|
||||
return unchecked((int)0x80B50009);
|
||||
}
|
||||
|
||||
if (time.Day < 1 || time.Day > DateTime.DaysInMonth(time.Year, time.Month))
|
||||
{
|
||||
return unchecked((int)0x80B5000A);
|
||||
}
|
||||
|
||||
if (time.Hour > 23)
|
||||
{
|
||||
return unchecked((int)0x80B5000B);
|
||||
}
|
||||
|
||||
if (time.Minute > 59)
|
||||
{
|
||||
return unchecked((int)0x80B5000C);
|
||||
}
|
||||
|
||||
if (time.Second > 59)
|
||||
{
|
||||
return unchecked((int)0x80B5000D);
|
||||
}
|
||||
|
||||
if (time.Microsecond > 999_999)
|
||||
{
|
||||
return unchecked((int)0x80B5000E);
|
||||
}
|
||||
|
||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||
}
|
||||
|
||||
private static bool IsValidCalendarDate(int year, int month, int day)
|
||||
{
|
||||
if (year < 1 || year > 9999 || month < 1 || month > 12)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return day >= 1 && day <= DateTime.DaysInMonth(year, month);
|
||||
}
|
||||
|
||||
private static bool TryReadRtcDateTime(CpuContext ctx, ulong address, out RtcDateTime time)
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[16];
|
||||
if (!ctx.Memory.TryRead(address, buffer))
|
||||
{
|
||||
rtcDateTime = default;
|
||||
time = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
rtcDateTime = new RtcDateTime(
|
||||
time = new RtcDateTime(
|
||||
BinaryPrimitives.ReadUInt16LittleEndian(buffer[0..2]),
|
||||
BinaryPrimitives.ReadUInt16LittleEndian(buffer[2..4]),
|
||||
BinaryPrimitives.ReadUInt16LittleEndian(buffer[4..6]),
|
||||
@@ -176,19 +831,117 @@ public static class RtcExports
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool TryWriteRtcDateTime(CpuContext ctx, ulong address, DateTime dateTime)
|
||||
private static bool TryWriteRtcDateTime(CpuContext ctx, ulong address, RtcDateTime dateTime)
|
||||
{
|
||||
Span<byte> rtcDateTime = stackalloc byte[16];
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(rtcDateTime[0..2], checked((ushort)dateTime.Year));
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(rtcDateTime[2..4], checked((ushort)dateTime.Month));
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(rtcDateTime[4..6], checked((ushort)dateTime.Day));
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(rtcDateTime[6..8], checked((ushort)dateTime.Hour));
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(rtcDateTime[8..10], checked((ushort)dateTime.Minute));
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(rtcDateTime[10..12], checked((ushort)dateTime.Second));
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(
|
||||
rtcDateTime[12..16],
|
||||
checked((uint)((dateTime.Ticks % TimeSpan.TicksPerSecond) / DateTimeTicksPerMicrosecond)));
|
||||
return ctx.Memory.TryWrite(address, rtcDateTime);
|
||||
Span<byte> buffer = stackalloc byte[16];
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(buffer[0..2], dateTime.Year);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(buffer[2..4], dateTime.Month);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(buffer[4..6], dateTime.Day);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(buffer[6..8], dateTime.Hour);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(buffer[8..10], dateTime.Minute);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(buffer[10..12], dateTime.Second);
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(buffer[12..16], dateTime.Microsecond);
|
||||
return ctx.Memory.TryWrite(address, buffer);
|
||||
}
|
||||
|
||||
private static RtcDateTime ToRtcDateTime(DateTime dateTime)
|
||||
{
|
||||
return new RtcDateTime(
|
||||
(ushort)dateTime.Year,
|
||||
(ushort)dateTime.Month,
|
||||
(ushort)dateTime.Day,
|
||||
(ushort)dateTime.Hour,
|
||||
(ushort)dateTime.Minute,
|
||||
(ushort)dateTime.Second,
|
||||
(uint)(dateTime.Ticks % TimeSpan.TicksPerSecond / DateTimeTicksPerMicrosecond));
|
||||
}
|
||||
|
||||
private static RtcDateTime ToRtcDateTime(DateTimeOffset dateTimeOffset)
|
||||
{
|
||||
return ToRtcDateTime(dateTimeOffset.DateTime);
|
||||
}
|
||||
|
||||
private static bool TryWriteTickFromDateTime(CpuContext ctx, ulong address, DateTime dateTime)
|
||||
{
|
||||
return ctx.TryWriteUInt64(address, unchecked((ulong)(dateTime.Ticks / DateTimeTicksPerMicrosecond)));
|
||||
}
|
||||
|
||||
private static bool TryConvertTickToDateTime(ulong tick, out DateTime dateTime)
|
||||
{
|
||||
var maxSupportedTick = unchecked((ulong)(DateTime.MaxValue.Ticks / DateTimeTicksPerMicrosecond));
|
||||
if (tick > maxSupportedTick)
|
||||
{
|
||||
dateTime = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
dateTime = new DateTime(checked((long)(tick * (ulong)DateTimeTicksPerMicrosecond)), DateTimeKind.Utc);
|
||||
return true;
|
||||
}
|
||||
catch (ArgumentOutOfRangeException)
|
||||
{
|
||||
dateTime = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryConvertRtcDateTimeToTick(RtcDateTime time, out ulong tick)
|
||||
{
|
||||
try
|
||||
{
|
||||
var year = time.Year;
|
||||
var month = time.Month;
|
||||
var day = time.Day;
|
||||
if (month > 2)
|
||||
{
|
||||
month -= 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
month += 9;
|
||||
year -= 1;
|
||||
}
|
||||
|
||||
var century = year / 100;
|
||||
var yearOfCentury = year - (100 * century);
|
||||
|
||||
ulong days = ((146097UL * (ulong)century) >> 2)
|
||||
+ ((1461UL * (ulong)yearOfCentury) >> 2)
|
||||
+ ((153UL * (ulong)month + 2UL) / 5UL)
|
||||
+ day;
|
||||
days -= 307UL;
|
||||
days *= MicrosecondsPerDay;
|
||||
|
||||
var timeOfDay = (ulong)time.Hour * MicrosecondsPerHour
|
||||
+ (ulong)time.Minute * MicrosecondsPerMinute
|
||||
+ (ulong)time.Second * MicrosecondsPerSecond
|
||||
+ time.Microsecond;
|
||||
|
||||
tick = days + timeOfDay;
|
||||
return true;
|
||||
}
|
||||
catch (OverflowException)
|
||||
{
|
||||
tick = 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryConvertTickToRtcDateTime(ulong tick, out RtcDateTime time)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dateTime = new DateTime(checked((long)(tick * DateTimeTicksPerMicrosecond)), DateTimeKind.Utc);
|
||||
time = ToRtcDateTime(dateTime);
|
||||
return true;
|
||||
}
|
||||
catch (ArgumentOutOfRangeException)
|
||||
{
|
||||
time = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly record struct RtcDateTime(
|
||||
|
||||
@@ -178,6 +178,7 @@ internal static unsafe class VulkanVideoPresenter
|
||||
private static uint _windowHeight;
|
||||
private static bool _closed;
|
||||
private const string DebugUtilsExtensionName = "VK_EXT_debug_utils";
|
||||
private const uint NvidiaVendorId = 0x10DE;
|
||||
private static bool _splashHidden;
|
||||
private static long _enqueuedGuestWorkSequence;
|
||||
private static long _completedGuestWorkSequence;
|
||||
@@ -1502,6 +1503,13 @@ internal static unsafe class VulkanVideoPresenter
|
||||
Check(_vk.EnumeratePhysicalDevices(_instance, &deviceCount, devicePointer), "vkEnumeratePhysicalDevices");
|
||||
}
|
||||
|
||||
// Hybrid laptops enumerate the iGPU first, and AMD's integrated driver
|
||||
// segfaults compiling some translated shaders, so rank rather than take
|
||||
// the first hit. SHARPEMU_VK_DEVICE=<substring> pins an adapter by name.
|
||||
var deviceOverride = Environment.GetEnvironmentVariable("SHARPEMU_VK_DEVICE");
|
||||
var bestScore = int.MinValue;
|
||||
var found = false;
|
||||
|
||||
foreach (var device in devices)
|
||||
{
|
||||
uint queueCount = 0;
|
||||
@@ -1521,13 +1529,60 @@ internal static unsafe class VulkanVideoPresenter
|
||||
continue;
|
||||
}
|
||||
|
||||
_physicalDevice = device;
|
||||
_queueFamilyIndex = index;
|
||||
return;
|
||||
_vk.GetPhysicalDeviceProperties(device, out var properties);
|
||||
var name = SilkMarshal.PtrToString((nint)properties.DeviceName) ?? string.Empty;
|
||||
var score = ScorePhysicalDevice(properties, name, deviceOverride);
|
||||
Console.Error.WriteLine(
|
||||
$"[LOADER][INFO] Vulkan candidate: {name} ({properties.DeviceType}) score={score}");
|
||||
|
||||
if (score > bestScore)
|
||||
{
|
||||
bestScore = score;
|
||||
_physicalDevice = device;
|
||||
_queueFamilyIndex = index;
|
||||
found = true;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("No Vulkan graphics/present queue was found.");
|
||||
if (!found)
|
||||
{
|
||||
throw new InvalidOperationException("No Vulkan graphics/present queue was found.");
|
||||
}
|
||||
|
||||
_vk.GetPhysicalDeviceProperties(_physicalDevice, out var selected);
|
||||
Console.Error.WriteLine(
|
||||
$"[LOADER][INFO] Vulkan device: {SilkMarshal.PtrToString((nint)selected.DeviceName)} ({selected.DeviceType})");
|
||||
}
|
||||
|
||||
private static int ScorePhysicalDevice(
|
||||
PhysicalDeviceProperties properties,
|
||||
string name,
|
||||
string? deviceOverride)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(deviceOverride))
|
||||
{
|
||||
return name.Contains(deviceOverride, StringComparison.OrdinalIgnoreCase) ? 1000 : -1000;
|
||||
}
|
||||
|
||||
var score = properties.DeviceType switch
|
||||
{
|
||||
PhysicalDeviceType.DiscreteGpu => 300,
|
||||
PhysicalDeviceType.VirtualGpu => 100,
|
||||
PhysicalDeviceType.Cpu => 50,
|
||||
// Last resort: only picked when nothing else can present.
|
||||
PhysicalDeviceType.IntegratedGpu => -100,
|
||||
_ => 10,
|
||||
};
|
||||
|
||||
if (properties.VendorID == NvidiaVendorId)
|
||||
{
|
||||
score += 500;
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
private void CreateDevice()
|
||||
|
||||
Reference in New Issue
Block a user