From 06a6e26588629e014c5842ad372870796023bf46 Mon Sep 17 00:00:00 2001 From: Flyinghead Date: Thu, 11 Apr 2024 15:25:25 +0200 Subject: [PATCH] get rid of os_GetSeconds() replace it with std::chrono-based getTimeMs() --- core/hw/modem/modem.cpp | 11 ++-- core/hw/pvr/spg.cpp | 43 +++++++------- core/hw/sh4/modules/fastmmu.cpp | 7 ++- core/input/gamepad_device.cpp | 12 ++-- core/input/gamepad_device.h | 2 +- core/linux-dist/evdev_gamepad.h | 8 +-- core/linux/common.cpp | 56 ++++++------------- core/log/LogManager.cpp | 10 ++-- core/network/picoppp.cpp | 5 +- core/oslib/oslib.h | 1 - core/rend/boxart/gamesdb.cpp | 11 ++-- core/rend/boxart/gamesdb.h | 2 +- core/rend/gui.cpp | 24 ++++---- core/sdl/sdl.cpp | 6 +- core/sdl/sdl_gamepad.h | 8 +-- core/sdl/sdl_keyboard_mac.h | 7 ++- core/stdclass.cpp | 10 ++++ core/stdclass.h | 2 + core/types.h | 3 - core/windows/fault_handler.cpp | 15 ----- .../src/main/jni/src/android_keyboard.h | 6 +- .../emulator/FlycastViewController.mm | 6 +- shell/libretro/libretro.cpp | 11 +--- shell/switch/switch_gamepad.h | 8 +-- tests/src/test_stubs.cpp | 17 ------ 25 files changed, 119 insertions(+), 172 deletions(-) diff --git a/core/hw/modem/modem.cpp b/core/hw/modem/modem.cpp index 93fda73d8..f3d6e904d 100644 --- a/core/hw/modem/modem.cpp +++ b/core/hw/modem/modem.cpp @@ -28,10 +28,7 @@ #include "network/picoppp.h" #include "serialize.h" #include "cfg/option.h" - -#ifndef NDEBUG -#include "oslib/oslib.h" -#endif +#include "stdclass.h" #include #define MODEM_COUNTRY_RES 0 @@ -127,7 +124,7 @@ static u64 last_dial_time; static bool data_sent; #ifndef NDEBUG -static double last_comm_stats; +static u64 last_comm_stats; static int sent_bytes; static int recvd_bytes; static FILE *recv_fp; @@ -137,7 +134,7 @@ static FILE *sent_fp; static int modem_sched_func(int tag, int cycles, int jitter, void *arg) { #ifndef NDEBUG - if (os_GetSeconds() - last_comm_stats >= 2) + if (getTimeMs() - last_comm_stats >= 2000) { if (last_comm_stats != 0) { @@ -147,7 +144,7 @@ static int modem_sched_func(int tag, int cycles, int jitter, void *arg) sent_bytes = 0; recvd_bytes = 0; } - last_comm_stats = os_GetSeconds(); + last_comm_stats = getTimeMs(); } #endif int callback_cycles = 0; diff --git a/core/hw/pvr/spg.cpp b/core/hw/pvr/spg.cpp index ff3b6f320..6cdc5bec0 100755 --- a/core/hw/pvr/spg.cpp +++ b/core/hw/pvr/spg.cpp @@ -1,13 +1,13 @@ -#include #include "spg.h" #include "hw/holly/holly_intc.h" #include "hw/holly/sb.h" #include "hw/sh4/sh4_sched.h" -#include "oslib/oslib.h" #include "hw/maple/maple_if.h" #include "serialize.h" #include "network/ggpo.h" #include "hw/pvr/Renderer_if.h" +#include "stdclass.h" +#include #ifdef TEST_AUTOMATION #include "input/gamepad_device.h" @@ -21,7 +21,7 @@ static u32 prv_cur_scanline = -1; #if !defined(NDEBUG) || defined(DEBUGFAST) static u32 vblk_cnt; -static float last_fps; +static u64 last_fps; #endif // 27 mhz pixel clock @@ -156,17 +156,18 @@ static int spg_line_sched(int tag, int cycles, int jitter, void *arg) rend_vblank(); - double now = os_GetSeconds() * 1000000.0; + u64 now = getTimeMs(); cpu_time_idx = (cpu_time_idx + 1) % cpu_cycles.size(); if (cpu_cycles[cpu_time_idx] != 0) { u32 cycle_span = (u32)(sh4_sched_now64() - cpu_cycles[cpu_time_idx]); - double time_span = now - real_times[cpu_time_idx]; - double cpu_speed = ((double)cycle_span / time_span) / (SH4_MAIN_CLOCK / 100000000); - SH4FastEnough = cpu_speed >= 85.0; + u64 time_span = now - real_times[cpu_time_idx]; + float cpu_speed = ((float)cycle_span / time_span) / (SH4_MAIN_CLOCK / 100000); + SH4FastEnough = cpu_speed >= 85.f; } - else + else { SH4FastEnough = false; + } cpu_cycles[cpu_time_idx] = sh4_sched_now64(); real_times[cpu_time_idx] = now; @@ -176,15 +177,15 @@ static int spg_line_sched(int tag, int cycles, int jitter, void *arg) #if !defined(NDEBUG) || defined(DEBUGFAST) vblk_cnt++; - if ((os_GetSeconds()-last_fps)>2) + if (getTimeMs() - last_fps >= 2000) { static int Last_FC; - double ts=os_GetSeconds()-last_fps; - double spd_fps=(FrameCount-Last_FC)/ts; - double spd_vbs=vblk_cnt/ts; - double spd_cpu=spd_vbs*Frame_Cycles; - spd_cpu/=1000000; //mrhz kthx - double fullvbs=(spd_vbs/spd_cpu)*200; + double ts = ((double)getTimeMs() - last_fps) / 1000.0; + double spd_fps = (FrameCount - Last_FC) / ts; + double spd_vbs = vblk_cnt / ts; + double spd_cpu = spd_vbs * Frame_Cycles; + spd_cpu /= 1000000.0; //mrhz kthx + double fullvbs = (spd_vbs / spd_cpu) * 200.0; Last_FC=FrameCount; @@ -210,13 +211,13 @@ static int spg_line_sched(int tag, int cycles, int jitter, void *arg) double full_rps = spd_fps + fskip / ts; - INFO_LOG(COMMON, "%s/%c - %4.2f - %4.2f - V: %4.2f (%.2f, %s%s%4.2f) R: %4.2f+%4.2f", - VER_SHORTNAME,'n',mspdf,spd_cpu*100/200,spd_vbs, - spd_vbs/full_rps,mode,res,fullvbs, - spd_fps,fskip/ts); + INFO_LOG(COMMON, "SPG - %4.2f - %4.2f - V: %4.2f (%.2f, %s%s%4.2f) R: %4.2f+%4.2f", + mspdf, spd_cpu * 100 / 200, spd_vbs, + spd_vbs / full_rps, mode, res, fullvbs, + spd_fps, fskip / ts); - fskip=0; - last_fps=os_GetSeconds(); + fskip = 0; + last_fps = getTimeMs(); } #endif } diff --git a/core/hw/sh4/modules/fastmmu.cpp b/core/hw/sh4/modules/fastmmu.cpp index e599d03b5..bc9c08487 100644 --- a/core/hw/sh4/modules/fastmmu.cpp +++ b/core/hw/sh4/modules/fastmmu.cpp @@ -20,6 +20,7 @@ #include "hw/sh4/sh4_if.h" #include "hw/sh4/sh4_core.h" #include "types.h" +#include "stdclass.h" #ifdef FAST_MMU @@ -156,7 +157,7 @@ int main(int argc, char *argv[]) addrs.push_back(random()); asids.push_back(666); } - double start = os_GetSeconds(); + u64 start = getTimeMs(); int success = 0; const int loops = 100000; for (int i = 0; i < loops; i++) @@ -170,8 +171,8 @@ int main(int argc, char *argv[]) success++; } } - double end = os_GetSeconds(); - printf("Lookup time: %f ms. Success rate %f max_len %d\n", (end - start) * 1000.0 / addrs.size(), (double)success / addrs.size() / loops, 0/*max_length*/); + u64 end = getTimeMs(); + printf("Lookup time: %f ms. Success rate %f max_len %d\n", ((double)end - start) / addrs.size(), (double)success / addrs.size() / loops, 0/*max_length*/); } #endif diff --git a/core/input/gamepad_device.cpp b/core/input/gamepad_device.cpp index 8fdbc1b7e..702fcbc65 100644 --- a/core/input/gamepad_device.cpp +++ b/core/input/gamepad_device.cpp @@ -19,7 +19,7 @@ #include "gamepad_device.h" #include "cfg/cfg.h" -#include "oslib/oslib.h" +#include "stdclass.h" #include "rend/gui.h" #include "emulator.h" #include "hw/maple/maple_devs.h" @@ -152,7 +152,7 @@ bool GamepadDevice::handleButtonInput(int port, DreamcastKey key, bool pressed) bool GamepadDevice::gamepad_btn_input(u32 code, bool pressed) { if (_input_detected != nullptr && _detecting_button - && os_GetSeconds() >= _detection_start_time && pressed) + && getTimeMs() >= _detection_start_time && pressed) { _input_detected(code, false, false); _input_detected = nullptr; @@ -207,7 +207,7 @@ bool GamepadDevice::gamepad_axis_input(u32 code, int value) { bool positive = value >= 0; if (_input_detected != NULL && _detecting_axis - && os_GetSeconds() >= _detection_start_time && std::abs(value) >= 16384) + && getTimeMs() >= _detection_start_time && std::abs(value) >= 16384) { _input_detected(code, true, positive); _input_detected = nullptr; @@ -505,7 +505,7 @@ void GamepadDevice::detect_btn_input(input_detected_cb button_pressed) _input_detected = button_pressed; _detecting_button = true; _detecting_axis = false; - _detection_start_time = os_GetSeconds() + 0.2; + _detection_start_time = getTimeMs() + 200; } void GamepadDevice::detect_axis_input(input_detected_cb axis_moved) @@ -513,7 +513,7 @@ void GamepadDevice::detect_axis_input(input_detected_cb axis_moved) _input_detected = axis_moved; _detecting_button = false; _detecting_axis = true; - _detection_start_time = os_GetSeconds() + 0.2; + _detection_start_time = getTimeMs() + 200; } void GamepadDevice::detectButtonOrAxisInput(input_detected_cb input_changed) @@ -521,7 +521,7 @@ void GamepadDevice::detectButtonOrAxisInput(input_detected_cb input_changed) _input_detected = input_changed; _detecting_button = true; _detecting_axis = true; - _detection_start_time = os_GetSeconds() + 0.2; + _detection_start_time = getTimeMs() + 200; } #ifdef TEST_AUTOMATION diff --git a/core/input/gamepad_device.h b/core/input/gamepad_device.h index 4941f1c7f..aef633870 100644 --- a/core/input/gamepad_device.h +++ b/core/input/gamepad_device.h @@ -177,7 +177,7 @@ private: int _maple_port; bool _detecting_button = false; bool _detecting_axis = false; - double _detection_start_time = 0.0; + u64 _detection_start_time = 0; input_detected_cb _input_detected; bool _remappable; u32 digitalToAnalogState[4]; diff --git a/core/linux-dist/evdev_gamepad.h b/core/linux-dist/evdev_gamepad.h index 263868453..aae93e552 100644 --- a/core/linux-dist/evdev_gamepad.h +++ b/core/linux-dist/evdev_gamepad.h @@ -1,7 +1,7 @@ #pragma once #include "evdev.h" #include "input/gamepad_device.h" -#include "oslib/oslib.h" +#include "stdclass.h" #include #include @@ -81,7 +81,7 @@ public: void rumble(float power, float inclination, u32 duration_ms) override { vib_inclination = inclination * power; - vib_stop_time = os_GetSeconds() + duration_ms / 1000.0; + vib_stop_time = getTimeMs() + duration_ms; do_rumble(power, duration_ms); } @@ -89,7 +89,7 @@ public: { if (vib_inclination > 0) { - int rem_time = (vib_stop_time - os_GetSeconds()) * 1000; + int rem_time = vib_stop_time - getTimeMs(); if (rem_time <= 0) vib_inclination = 0; else @@ -308,7 +308,7 @@ private: std::string _devnode; int _rumble_effect_id = -1; float vib_inclination = 0; - double vib_stop_time = 0; + u64 vib_stop_time = 0; std::map axis_min_values; std::map axis_ranges; static std::map> evdev_gamepads; diff --git a/core/linux/common.cpp b/core/linux/common.cpp index 37c5151ca..00453b762 100644 --- a/core/linux/common.cpp +++ b/core/linux/common.cpp @@ -11,9 +11,6 @@ #if defined(__linux__) && !defined(__ANDROID__) #include #endif -#if !defined(TARGET_BSD) && !defined(__ANDROID__) && defined(TARGET_VIDEOCORE) - #include -#endif #include #ifdef __linux__ #include @@ -123,14 +120,6 @@ void os_UninstallFaultHandler() } #endif // !defined(TARGET_NO_EXCEPTIONS) -double os_GetSeconds() -{ - timeval a; - gettimeofday (&a,0); - static u64 tvs_base=a.tv_sec; - return a.tv_sec-tvs_base+a.tv_usec/1000000.0; -} - #if !defined(__unix__) && !defined(LIBRETRO) && !defined(__SWITCH__) [[noreturn]] void os_DebugBreak() { @@ -138,11 +127,15 @@ double os_GetSeconds() } #endif -void enable_runfast() +// RunFast mode is the combination of the following conditions: +// * the VFP11 coprocessor is in flush-to-zero mode +// * the VFP11 coprocessor is in default NaN mode +// * all exception enable bits are cleared. +static void enable_runfast() { - #if HOST_CPU==CPU_ARM && !defined(ARMCC) - static const unsigned int x = 0x04086060; - static const unsigned int y = 0x03000000; +#if HOST_CPU == CPU_ARM && !defined(ARMCC) + static const unsigned int x = 0x04086060; // reset and disable FP exceptions, flush-to-zero, default NaN mode + static const unsigned int y = 0x03000000; // round to zero int r; asm volatile ( "fmrx %0, fpscr \n\t" //r0 = FPSCR @@ -154,32 +147,18 @@ void enable_runfast() ); DEBUG_LOG(BOOT, "ARM VFP-Run Fast (NFP) enabled !"); - #endif -} - -void linux_fix_personality() { -#if defined(__linux__) && !defined(__ANDROID__) - DEBUG_LOG(BOOT, "Personality: %08X", personality(0xFFFFFFFF)); - personality(~READ_IMPLIES_EXEC & personality(0xFFFFFFFF)); - DEBUG_LOG(BOOT, "Updated personality: %08X", personality(0xFFFFFFFF)); #endif } -void linux_rpi2_init() { -#if !defined(TARGET_BSD) && !defined(__ANDROID__) && defined(TARGET_VIDEOCORE) - void* handle; - void (*rpi_bcm_init)(void); - - handle = dlopen("libbcm_host.so", RTLD_LAZY); - - if (handle) { - DEBUG_LOG(BOOT, "found libbcm_host"); - *(void**) (&rpi_bcm_init) = dlsym(handle, "bcm_host_init"); - if (rpi_bcm_init) { - DEBUG_LOG(BOOT, "rpi2: bcm_init"); - rpi_bcm_init(); - } - } +// Some old CPUs lack the NX (no exec) flag so READ_IMPLIES_EXEC is set by default on these platforms. +// However resetting the flag isn't going to magically change the way the CPU works. So I wonder how useful this is. +// It's not needed on modern 64-bit architectures anyway. +static void linux_fix_personality() +{ +#if defined(__linux__) && !defined(__ANDROID__) && (HOST_CPU == CPU_X86 || HOST_CPU == CPU_ARM) + DEBUG_LOG(BOOT, "Personality: %08X", personality(0xFFFFFFFF)); + personality(~READ_IMPLIES_EXEC & personality(0xFFFFFFFF)); + DEBUG_LOG(BOOT, "Updated personality: %08X", personality(0xFFFFFFFF)); #endif } @@ -193,7 +172,6 @@ static void sigintHandler(int) void common_linux_setup() { linux_fix_personality(); - linux_rpi2_init(); enable_runfast(); os_InstallFaultHandler(); diff --git a/core/log/LogManager.cpp b/core/log/LogManager.cpp index d86102ac4..dacaac706 100644 --- a/core/log/LogManager.cpp +++ b/core/log/LogManager.cpp @@ -175,10 +175,12 @@ LogManager::~LogManager() // in the form 00:00:000. static std::string GetTimeFormatted() { - double now = os_GetSeconds(); - u32 minutes = (u32)now / 60; - u32 seconds = (u32)now % 60; - u32 ms = (now - (u32)now) * 1000; + u64 now = getTimeMs(); + u32 ms = (u32)(now % 1000); + now /= 1000; + u32 seconds = (u32)(now % 60); + now /= 60; + u32 minutes = (u32)now; return StringFromFormat("%02d:%02d:%03d", minutes, seconds, ms); } diff --git a/core/network/picoppp.cpp b/core/network/picoppp.cpp index bbfb34bba..7ba626a94 100644 --- a/core/network/picoppp.cpp +++ b/core/network/picoppp.cpp @@ -26,9 +26,6 @@ #include "stdclass.h" //#define BBA_PCAPNG_DUMP -#ifdef BBA_PCAPNG_DUMP -#include "oslib/oslib.h" -#endif #ifdef __MINGW32__ #define _POSIX_SOURCE @@ -883,7 +880,7 @@ static void dumpFrame(const u8 *frame, u32 size) fwrite(&roundedSize, sizeof(roundedSize), 1, pcapngDump); u32 ifId = 0; fwrite(&ifId, sizeof(ifId), 1, pcapngDump); - u64 now = (u64)(os_GetSeconds() * 1000000.0); + u64 now = getTimeMs() * 1000; fwrite((u32 *)&now + 1, 4, 1, pcapngDump); fwrite(&now, 4, 1, pcapngDump); fwrite(&size, sizeof(size), 1, pcapngDump); diff --git a/core/oslib/oslib.h b/core/oslib/oslib.h index 4c7f97eb7..91aff2106 100644 --- a/core/oslib/oslib.h +++ b/core/oslib/oslib.h @@ -5,7 +5,6 @@ #endif void os_SetWindowText(const char* text); -double os_GetSeconds(); void os_DoEvents(); void os_CreateWindow(); diff --git a/core/rend/boxart/gamesdb.cpp b/core/rend/boxart/gamesdb.cpp index 063c3587e..f03fc03c6 100644 --- a/core/rend/boxart/gamesdb.cpp +++ b/core/rend/boxart/gamesdb.cpp @@ -19,7 +19,6 @@ #include "gamesdb.h" #include "http_client.h" #include "stdclass.h" -#include "oslib/oslib.h" #include "emulator.h" #define APIKEY "3fcc5e726a129924972be97abfd577ac5311f8f12398a9d9bcb5a377d4656fa8" @@ -74,9 +73,9 @@ void TheGamesDb::copyFile(const std::string& from, const std::string& to) json TheGamesDb::httpGet(const std::string& url) { - if (os_GetSeconds() < blackoutPeriod) + if (getTimeMs() < blackoutPeriod) throw std::runtime_error(""); - blackoutPeriod = 0.0; + blackoutPeriod = 0; DEBUG_LOG(COMMON, "TheGameDb: GET %s", url.c_str()); std::vector receivedData; @@ -84,9 +83,9 @@ json TheGamesDb::httpGet(const std::string& url) bool success = http::success(status); if (status == 403) // hit rate-limit cap - blackoutPeriod = os_GetSeconds() + 60.0; + blackoutPeriod = getTimeMs() + 60 * 1000; else if (!success) - blackoutPeriod = os_GetSeconds() + 1.0; + blackoutPeriod = getTimeMs() + 1000; if (!success || receivedData.empty()) throw std::runtime_error("http error"); @@ -382,7 +381,7 @@ void TheGamesDb::fetchByUids(std::vector& items) void TheGamesDb::scrape(std::vector& items) { - if (os_GetSeconds() < blackoutPeriod) + if (getTimeMs() < blackoutPeriod) throw std::runtime_error(""); blackoutPeriod = 0.0; diff --git a/core/rend/boxart/gamesdb.h b/core/rend/boxart/gamesdb.h index 90e179074..4d8445971 100644 --- a/core/rend/boxart/gamesdb.h +++ b/core/rend/boxart/gamesdb.h @@ -47,7 +47,7 @@ private: int dreamcastPlatformId = 0; int arcadePlatformId = 0; - double blackoutPeriod = 0.0; + u64 blackoutPeriod = 0; std::map boxartCache; // key: url, value: local file path }; diff --git a/core/rend/gui.cpp b/core/rend/gui.cpp index ea07d6cf2..db6398486 100644 --- a/core/rend/gui.cpp +++ b/core/rend/gui.cpp @@ -78,7 +78,7 @@ static float mouseWheel; static std::string error_msg; static bool error_msg_shown; static std::string osd_message; -static double osd_message_end; +static u64 osd_message_end; static std::mutex osd_message_mutex; static void (*showOnScreenKeyboard)(bool show); static bool keysUpNextFrame[512]; @@ -920,7 +920,7 @@ static std::shared_ptr mapped_device; static u32 mapped_code; static bool analogAxis; static bool positiveDirection; -static double map_start_time; +static u64 map_start_time; static bool arcade_button_mode; static u32 gamepad_port; @@ -986,8 +986,8 @@ static void detect_input_popup(const Mapping *mapping) if (ImGui::BeginPopupModal("Map Control", NULL, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove)) { ImGui::Text("Waiting for control '%s'...", mapping->name); - double now = os_GetSeconds(); - ImGui::Text("Time out in %d s", (int)(5 - (now - map_start_time))); + u64 now = getTimeMs(); + ImGui::Text("Time out in %d s", (int)(5 - (now - map_start_time) / 1000)); if (mapped_code != (u32)-1) { std::shared_ptr input_mapping = mapped_device->get_input_mapping(); @@ -1012,7 +1012,7 @@ static void detect_input_popup(const Mapping *mapping) mapped_device = NULL; ImGui::CloseCurrentPopup(); } - else if (now - map_start_time >= 5) + else if (now - map_start_time >= 5000) { mapped_device = NULL; ImGui::CloseCurrentPopup(); @@ -1239,7 +1239,7 @@ static void controller_mapping_popup(const std::shared_ptr& gamep ImGui::NextColumn(); if (ImGui::Button("Map")) { - map_start_time = os_GetSeconds(); + map_start_time = getTimeMs(); ImGui::OpenPopup("Map Control"); mapped_device = gamepad; mapped_code = -1; @@ -2859,13 +2859,13 @@ void gui_display_notification(const char *msg, int duration) { std::lock_guard lock(osd_message_mutex); osd_message = msg; - osd_message_end = os_GetSeconds() + (double)duration / 1000.0; + osd_message_end = getTimeMs() + duration; } static std::string get_notification() { std::lock_guard lock(osd_message_mutex); - if (!osd_message.empty() && os_GetSeconds() >= osd_message_end) + if (!osd_message.empty() && getTimeMs() >= osd_message_end) osd_message.clear(); return osd_message; } @@ -3348,7 +3348,7 @@ void gui_display_ui() emu.start(); } -static float LastFPSTime; +static u64 LastFPSTime; static int lastFrameCount = 0; static float fps = -1; @@ -3356,9 +3356,9 @@ static std::string getFPSNotification() { if (config::ShowFPS) { - double now = os_GetSeconds(); - if (now - LastFPSTime >= 1.0) { - fps = (MainFrameCount - lastFrameCount) / (now - LastFPSTime); + u64 now = getTimeMs(); + if (now - LastFPSTime >= 1000) { + fps = ((float)MainFrameCount - lastFrameCount) * 1000.f / (now - LastFPSTime); LastFPSTime = now; lastFrameCount = MainFrameCount; } diff --git a/core/sdl/sdl.cpp b/core/sdl/sdl.cpp index b3846b048..fb9c7d51c 100644 --- a/core/sdl/sdl.cpp +++ b/core/sdl/sdl.cpp @@ -47,7 +47,7 @@ static bool gameRunning; static bool mouseCaptured; static std::string clipboardText; static std::string barcode; -static double lastBarcodeTime; +static u64 lastBarcodeTime; static KeyboardLayout detectKeyboardLayout(); static bool handleBarcodeScanner(const SDL_Event& event); @@ -1179,8 +1179,8 @@ static bool handleBarcodeScanner(const SDL_Event& event) return false; } } - double now = os_GetSeconds(); - if (!barcode.empty() && now - lastBarcodeTime >= 0.5) + u64 now = getTimeMs(); + if (!barcode.empty() && now - lastBarcodeTime >= 500) { INFO_LOG(INPUT, "Barcode timeout"); barcode.clear(); diff --git a/core/sdl/sdl_gamepad.h b/core/sdl/sdl_gamepad.h index 3c1a4cd20..60782f738 100644 --- a/core/sdl/sdl_gamepad.h +++ b/core/sdl/sdl_gamepad.h @@ -1,7 +1,7 @@ #pragma once #include "input/gamepad_device.h" #include "input/mouse.h" -#include "oslib/oslib.h" +#include "stdclass.h" #include "sdl.h" template @@ -233,7 +233,7 @@ public: if (rumbleEnabled) { vib_inclination = inclination * power; - vib_stop_time = os_GetSeconds() + duration_ms / 1000.0; + vib_stop_time = getTimeMs() + duration_ms; u16 intensity = getRumbleIntensity(power); SDL_JoystickRumble(sdl_joystick, intensity, intensity, duration_ms); @@ -245,7 +245,7 @@ public: return; if (vib_inclination > 0) { - int rem_time = (vib_stop_time - os_GetSeconds()) * 1000; + int rem_time = vib_stop_time - getTimeMs(); if (rem_time <= 0) vib_inclination = 0; else @@ -418,7 +418,7 @@ public: } protected: - double vib_stop_time = 0; + u64 vib_stop_time = 0; SDL_JoystickID sdl_joystick_instance; private: diff --git a/core/sdl/sdl_keyboard_mac.h b/core/sdl/sdl_keyboard_mac.h index abb53ac97..732f8e8b0 100644 --- a/core/sdl/sdl_keyboard_mac.h +++ b/core/sdl/sdl_keyboard_mac.h @@ -19,6 +19,7 @@ #pragma once #ifdef __APPLE__ #include "sdl_keyboard.h" +#include "stdclass.h" #include #include #include @@ -55,7 +56,7 @@ public: if (!rumbleEnabled) return; - vib_stop_time = os_GetSeconds() + duration_ms / 1000.0; + vib_stop_time = getTimeMs() + duration_ms; __block int pattern; if (power >= 0.75) @@ -82,7 +83,7 @@ public: dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 10 * NSEC_PER_MSEC * rumblePower / 100.f, 0); dispatch_source_set_event_handler(_timer, ^{ - if ( vib_stop_time - os_GetSeconds() < 0 ) + if (vib_stop_time < getTimeMs()) { dispatch_source_cancel(_timer); return; @@ -105,7 +106,7 @@ public: private: std::stack vib_timer_stack; CFTypeRef vib_device = NULL; - double vib_stop_time = 0; + u64 vib_stop_time = 0; }; #endif // _APPLE_ diff --git a/core/stdclass.cpp b/core/stdclass.cpp index 8751941b3..32b26bd3d 100644 --- a/core/stdclass.cpp +++ b/core/stdclass.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #ifdef _WIN32 #include @@ -211,3 +212,12 @@ void RamRegion::free() freeAligned(data); data = nullptr; } + +u64 getTimeMs() +{ + using the_clock = std::chrono::steady_clock; + std::chrono::time_point now = the_clock::now(); + static std::chrono::time_point start = now; + + return std::chrono::duration_cast(now - start).count(); +} diff --git a/core/stdclass.h b/core/stdclass.h index 8b08f0577..d5efa9e82 100644 --- a/core/stdclass.h +++ b/core/stdclass.h @@ -197,3 +197,5 @@ public: return v; } }; + +u64 getTimeMs(); diff --git a/core/types.h b/core/types.h index 21b9ab3d5..239b0c7ea 100644 --- a/core/types.h +++ b/core/types.h @@ -80,9 +80,6 @@ inline static void JITWriteProtect(bool enabled) { #include "log/Log.h" -#define VER_EMUNAME "Flycast" -#define VER_SHORTNAME VER_EMUNAME - #ifndef _MSC_VER #define stricmp strcasecmp #endif diff --git a/core/windows/fault_handler.cpp b/core/windows/fault_handler.cpp index e30715499..ac7ce0903 100644 --- a/core/windows/fault_handler.cpp +++ b/core/windows/fault_handler.cpp @@ -169,18 +169,3 @@ void os_UninstallFaultHandler() #endif SetUnhandledExceptionFilter(prevExceptionHandler); } - -double os_GetSeconds() -{ - static double qpfd = []() { - LARGE_INTEGER qpf; - QueryPerformanceFrequency(&qpf); - return 1.0 / qpf.QuadPart; }(); - - LARGE_INTEGER time_now; - - QueryPerformanceCounter(&time_now); - static LARGE_INTEGER time_now_base = time_now; - - return (time_now.QuadPart - time_now_base.QuadPart) * qpfd; -} diff --git a/shell/android-studio/flycast/src/main/jni/src/android_keyboard.h b/shell/android-studio/flycast/src/main/jni/src/android_keyboard.h index 087af3a5f..f8bc27b83 100644 --- a/shell/android-studio/flycast/src/main/jni/src/android_keyboard.h +++ b/shell/android-studio/flycast/src/main/jni/src/android_keyboard.h @@ -688,8 +688,8 @@ private: return false; } - double now = os_GetSeconds(); - if (!barcode.empty() && now - lastBarcodeTime >= 0.5) + u64 now = getTimeMs(); + if (!barcode.empty() && now - lastBarcodeTime >= 500) { INFO_LOG(INPUT, "Barcode timeout"); barcode.clear(); @@ -721,5 +721,5 @@ private: int modifiers = 0; std::string barcode; - double lastBarcodeTime = 0.0; + u64 lastBarcodeTime = 0; }; diff --git a/shell/apple/emulator-ios/emulator/FlycastViewController.mm b/shell/apple/emulator-ios/emulator/FlycastViewController.mm index 07cd93a42..4d4c66cde 100644 --- a/shell/apple/emulator-ios/emulator/FlycastViewController.mm +++ b/shell/apple/emulator-ios/emulator/FlycastViewController.mm @@ -738,11 +738,11 @@ void pickIosFile() const char *getIosJitStatus() { - static double lastCheckTime; - if (!iosJitAuthorized && os_GetSeconds() - lastCheckTime > 10.0) + static u64 lastCheckTime; + if (!iosJitAuthorized && getTimeMs() - lastCheckTime > 10000) { [flycastViewController altKitStart]; - lastCheckTime = os_GetSeconds(); + lastCheckTime = getTimeMs(); } return iosJitStatus.c_str(); } diff --git a/shell/libretro/libretro.cpp b/shell/libretro/libretro.cpp index d28ba5c7a..78a5a7bbc 100644 --- a/shell/libretro/libretro.cpp +++ b/shell/libretro/libretro.cpp @@ -2502,11 +2502,6 @@ void retro_rend_present() is_dupe = false; } -static uint32_t get_time_ms() -{ - return (uint32_t)(os_GetSeconds() * 1000.0); -} - static void get_analog_stick( retro_input_state_t input_state_cb, int player_index, int stick, @@ -2963,14 +2958,14 @@ static void UpdateInputState(u32 port) } if (rumble.set_rumble_state != NULL && vib_stop_time[port] > 0) { - if (get_time_ms() >= vib_stop_time[port]) + if (getTimeMs() >= vib_stop_time[port]) { vib_stop_time[port] = 0; rumble.set_rumble_state(port, RETRO_RUMBLE_STRONG, 0); } else if (vib_delta[port] > 0.0) { - u32 rem_time = vib_stop_time[port] - get_time_ms(); + u32 rem_time = vib_stop_time[port] - getTimeMs(); rumble.set_rumble_state(port, RETRO_RUMBLE_STRONG, 65535 * vib_strength[port] * rem_time * vib_delta[port]); } } @@ -3378,7 +3373,7 @@ static void updateVibration(u32 port, float power, float inclination, u32 durati vib_strength[port] = power; rumble.set_rumble_state(port, RETRO_RUMBLE_STRONG, (u16)(65535 * power)); - vib_stop_time[port] = get_time_ms() + durationMs; + vib_stop_time[port] = getTimeMs() + durationMs; vib_delta[port] = inclination; } diff --git a/shell/switch/switch_gamepad.h b/shell/switch/switch_gamepad.h index b21c7b96e..e6956eff1 100644 --- a/shell/switch/switch_gamepad.h +++ b/shell/switch/switch_gamepad.h @@ -59,23 +59,23 @@ public: memcpy(&vibValues[1], &vibValues[0], sizeof(HidVibrationValue)); hidSendVibrationValues(getDeviceHandle(), vibValues, 2); if (power != 0.f) - vib_stop_time = os_GetSeconds() + duration_ms / 1000.0; + vib_stop_time = getTimeMs() + duration_ms; else - vib_stop_time = 0.0; + vib_stop_time = 0; } void update_rumble() override { if (!rumbleEnabled || vib_stop_time == 0.0) return; - int rem_time = (vib_stop_time - os_GetSeconds()) * 1000; + int rem_time = vib_stop_time - getTimeMs(); if (rem_time <= 0) { HidVibrationValue vibValues[2]{}; vibValues[0].freq_low = vibValues[1].freq_low = 160.f; vibValues[0].freq_high = vibValues[1].freq_high = 320.f; hidSendVibrationValues(getDeviceHandle(), vibValues, 2); - vib_stop_time = 0.0; + vib_stop_time = 0; } } diff --git a/tests/src/test_stubs.cpp b/tests/src/test_stubs.cpp index 0b6b16001..e87bc1459 100644 --- a/tests/src/test_stubs.cpp +++ b/tests/src/test_stubs.cpp @@ -38,20 +38,3 @@ void os_RunInstance(int argc, const char *argv[]) { } #endif - -#ifdef _WIN32 -#include - -static LARGE_INTEGER qpf; -static double qpfd; -//Helper functions -double os_GetSeconds() -{ - static bool initme = (QueryPerformanceFrequency(&qpf), qpfd=1/(double)qpf.QuadPart); - LARGE_INTEGER time_now; - - QueryPerformanceCounter(&time_now); - static LARGE_INTEGER time_now_base = time_now; - return (time_now.QuadPart - time_now_base.QuadPart)*qpfd; -} -#endif