From c4cb20f846fedb0ef654fb42215cf04acd3854e7 Mon Sep 17 00:00:00 2001 From: wheremyfoodat Date: Sat, 24 Sep 2022 04:40:43 +0300 Subject: [PATCH] [Kernel/Memory] Implement datetime a bit more nicely --- include/cpu_dynarmic.hpp | 5 +++++ include/emulator.hpp | 2 +- include/memory.hpp | 5 +++-- src/core/memory.cpp | 10 ++++++++-- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/include/cpu_dynarmic.hpp b/include/cpu_dynarmic.hpp index 2329c4a8..96f692c7 100644 --- a/include/cpu_dynarmic.hpp +++ b/include/cpu_dynarmic.hpp @@ -155,6 +155,11 @@ public: return env.totalTicks; } + // Get reference to tick count. Memory needs access to this + u64& getTicksRef() { + return env.totalTicks; + } + void runFrame() { env.ticksLeft = 268111856 / 60; const auto exitReason = jit->Run(); diff --git a/include/emulator.hpp b/include/emulator.hpp index ec6c30c9..9fda043b 100644 --- a/include/emulator.hpp +++ b/include/emulator.hpp @@ -30,7 +30,7 @@ class Emulator { public: Emulator() : window(sf::VideoMode(width, height), "Alber", sf::Style::Default, sf::ContextSettings(0, 0, 0, 4, 3)), - kernel(cpu, memory, gpu), cpu(memory, kernel), gpu(memory) { + kernel(cpu, memory, gpu), cpu(memory, kernel), gpu(memory), memory(cpu.getTicksRef()) { reset(); window.setActive(true); } diff --git a/include/memory.hpp b/include/memory.hpp index 906255fb..55b52d59 100644 --- a/include/memory.hpp +++ b/include/memory.hpp @@ -72,6 +72,7 @@ namespace KernelMemoryTypes { class Memory { u8* fcram; + u64& cpuTicks; // Reference to the CPU tick counter // Our dynarmic core uses page tables for reads and writes with 4096 byte pages std::vector readTable, writeTable; @@ -93,13 +94,14 @@ class Memory { std::bitset usedFCRAMPages; std::optional findPaddr(u32 size); + u64 timeSince3DSEpoch(); public: u16 kernelVersion = 0; u32 usedUserMemory = 0; std::optional gspMemIndex; // Index of GSP shared mem in lockedMemoryInfo or nullopt if it's already reserved - Memory(); + Memory(u64& cpuTicks); void reset(); void* getReadPointer(u32 address); void* getWritePointer(u32 address); @@ -118,7 +120,6 @@ public: u32 getLinearHeapVaddr(); u8* getFCRAM() { return fcram; } - u64 timeSince3DSEpoch(); // Returns whether "addr" is aligned to a page (4096 byte) boundary static constexpr bool isAligned(u32 addr) { diff --git a/src/core/memory.cpp b/src/core/memory.cpp index be966c7e..a694d24a 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -6,7 +6,7 @@ using namespace KernelMemoryTypes; -Memory::Memory() { +Memory::Memory(u64& cpuTicks) : cpuTicks(cpuTicks) { fcram = new uint8_t[FCRAM_SIZE](); readTable.resize(totalPageCount, 0); writeTable.resize(totalPageCount, 0); @@ -88,6 +88,12 @@ u32 Memory::read32(u32 vaddr) { switch (vaddr) { case ConfigMem::Datetime0: return u32(timeSince3DSEpoch()); // ms elapsed since Jan 1 1900, bottom 32 bits case ConfigMem::Datetime0 + 4: return u32(timeSince3DSEpoch() >> 32); // top 32 bits + // Ticks since time was last updated. For now we return the current tick count + case ConfigMem::Datetime0 + 8: return u32(cpuTicks); + case ConfigMem::Datetime0 + 12: return u32(cpuTicks >> 32); + case ConfigMem::Datetime0 + 16: return 0xFFB0FF0; // Unknown, set by PTM + case ConfigMem::Datetime0 + 20: case ConfigMem::Datetime0 + 24: case ConfigMem::Datetime0 + 28: + return 0; // Set to 0 by PTM case ConfigMem::AppMemAlloc: return appResourceLimits.maxCommit; case 0x1FF81000: return 0; // TODO: Figure out what this config mem address does @@ -360,7 +366,7 @@ void Memory::mapGSPSharedMemory(u32 vaddr, u32 myPerms, u32 otherPerms) { // Get the number of ms since Jan 1 1900 u64 Memory::timeSince3DSEpoch() { using namespace std::chrono; - + // ms since Jan 1 1970 milliseconds ms = duration_cast(system_clock::now().time_since_epoch()); // ms between Jan 1 1900 and Jan 1 1970 (2208988800 seconds elapsed between the two)