diff options
| author | 2016-07-20 20:20:01 +0800 | |
|---|---|---|
| committer | 2016-07-23 14:30:23 +0300 | |
| commit | 00c34e4df73add0f3eda53a71ea23125ac343971 (patch) | |
| tree | 089824bd6bb5e12dea07d6fef94b4a4b4b8b05f4 /src/core/hle/shared_page.cpp | |
| parent | Merge pull request #1964 from Lectem/sdl2_dll_copy_fix (diff) | |
| download | yuzu-00c34e4df73add0f3eda53a71ea23125ac343971.tar.gz yuzu-00c34e4df73add0f3eda53a71ea23125ac343971.tar.xz yuzu-00c34e4df73add0f3eda53a71ea23125ac343971.zip | |
HLE: implement system time
Diffstat (limited to 'src/core/hle/shared_page.cpp')
| -rw-r--r-- | src/core/hle/shared_page.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/core/hle/shared_page.cpp b/src/core/hle/shared_page.cpp index 2a1caeaac..4d9272923 100644 --- a/src/core/hle/shared_page.cpp +++ b/src/core/hle/shared_page.cpp | |||
| @@ -2,8 +2,11 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <chrono> | ||
| 5 | #include <cstring> | 6 | #include <cstring> |
| 7 | #include <ctime> | ||
| 6 | 8 | ||
| 9 | #include "core/core_timing.h" | ||
| 7 | #include "core/hle/shared_page.h" | 10 | #include "core/hle/shared_page.h" |
| 8 | 11 | ||
| 9 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 12 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| @@ -12,6 +15,57 @@ namespace SharedPage { | |||
| 12 | 15 | ||
| 13 | SharedPageDef shared_page; | 16 | SharedPageDef shared_page; |
| 14 | 17 | ||
| 18 | static int update_time_event; | ||
| 19 | |||
| 20 | /// Gets system time in 3DS format. The epoch is Jan 1900, and the unit is millisecond. | ||
| 21 | static u64 GetSystemTime() { | ||
| 22 | auto now = std::chrono::system_clock::now(); | ||
| 23 | |||
| 24 | // 3DS system does't allow user to set a time before Jan 1 2000, | ||
| 25 | // so we use it as an auxiliary epoch to calculate the console time. | ||
| 26 | std::tm epoch_tm; | ||
| 27 | epoch_tm.tm_sec = 0; | ||
| 28 | epoch_tm.tm_min = 0; | ||
| 29 | epoch_tm.tm_hour = 0; | ||
| 30 | epoch_tm.tm_mday = 1; | ||
| 31 | epoch_tm.tm_mon = 0; | ||
| 32 | epoch_tm.tm_year = 100; | ||
| 33 | epoch_tm.tm_isdst = 0; | ||
| 34 | auto epoch = std::chrono::system_clock::from_time_t(std::mktime(&epoch_tm)); | ||
| 35 | |||
| 36 | // 3DS console time uses Jan 1 1900 as internal epoch, | ||
| 37 | // so we use the milliseconds between 1900 and 2000 as base console time | ||
| 38 | u64 console_time = 3155673600000ULL; | ||
| 39 | |||
| 40 | // Only when system time is after 2000, we set it as 3DS system time | ||
| 41 | if (now > epoch) { | ||
| 42 | console_time += std::chrono::duration_cast<std::chrono::milliseconds>(now - epoch).count(); | ||
| 43 | } | ||
| 44 | |||
| 45 | // If the system time is in daylight saving, we give an additional hour to console time | ||
| 46 | std::time_t now_time_t = std::chrono::system_clock::to_time_t(now); | ||
| 47 | std::tm* now_tm = std::localtime(&now_time_t); | ||
| 48 | if (now_tm && now_tm->tm_isdst > 0) | ||
| 49 | console_time += 60 * 60 * 1000; | ||
| 50 | |||
| 51 | return console_time; | ||
| 52 | } | ||
| 53 | |||
| 54 | static void UpdateTimeCallback(u64 userdata, int cycles_late) { | ||
| 55 | DateTime& date_time = shared_page.date_time_counter % 2 ? | ||
| 56 | shared_page.date_time_0 : shared_page.date_time_1; | ||
| 57 | |||
| 58 | date_time.date_time = GetSystemTime(); | ||
| 59 | date_time.update_tick = CoreTiming::GetTicks(); | ||
| 60 | date_time.tick_to_second_coefficient = g_clock_rate_arm11; | ||
| 61 | date_time.tick_offset = 0; | ||
| 62 | |||
| 63 | ++shared_page.date_time_counter; | ||
| 64 | |||
| 65 | // system time is updated hourly | ||
| 66 | CoreTiming::ScheduleEvent(msToCycles(60 * 60 * 1000) - cycles_late, update_time_event); | ||
| 67 | } | ||
| 68 | |||
| 15 | void Init() { | 69 | void Init() { |
| 16 | std::memset(&shared_page, 0, sizeof(shared_page)); | 70 | std::memset(&shared_page, 0, sizeof(shared_page)); |
| 17 | 71 | ||
| @@ -19,6 +73,9 @@ void Init() { | |||
| 19 | 73 | ||
| 20 | // Some games wait until this value becomes 0x1, before asking running_hw | 74 | // Some games wait until this value becomes 0x1, before asking running_hw |
| 21 | shared_page.unknown_value = 0x1; | 75 | shared_page.unknown_value = 0x1; |
| 76 | |||
| 77 | update_time_event = CoreTiming::RegisterEvent("SharedPage::UpdateTimeCallback", UpdateTimeCallback); | ||
| 78 | CoreTiming::ScheduleEvent(0, update_time_event); | ||
| 22 | } | 79 | } |
| 23 | 80 | ||
| 24 | } // namespace | 81 | } // namespace |