about summary refs log tree commit diff stats
path: root/src/emu
diff options
context:
space:
mode:
authorYang Liu <liuyang22@iscas.ac.cn>2024-12-26 21:40:04 +0800
committerGitHub <noreply@github.com>2024-12-26 14:40:04 +0100
commitcf24eaf9d2bcb739aaec5b4c75d6bb09b4b902ae (patch)
treea9ac91cf3005c7e867ef87ec7cc0a2bdd172e6e8 /src/emu
parentcd1ac51b7932dbd2eb158e67f7dc6bee71163b96 (diff)
downloadbox64-cf24eaf9d2bcb739aaec5b4c75d6bb09b4b902ae.tar.gz
box64-cf24eaf9d2bcb739aaec5b4c75d6bb09b4b902ae.zip
[LA64_DYNAREC] Added hardware wall-clock support (#2216)
* [LA64_DYNAREC] Added hardware wall-clock support

* Refined multi-language support
Diffstat (limited to 'src/emu')
-rw-r--r--src/emu/x64emu.c35
1 files changed, 33 insertions, 2 deletions
diff --git a/src/emu/x64emu.c b/src/emu/x64emu.c
index e63f46dd..e35f2608 100644
--- a/src/emu/x64emu.c
+++ b/src/emu/x64emu.c
@@ -702,6 +702,37 @@ static inline uint64_t readFreq()
                  : "=r"(val));
     return val;
 }
+#elif defined(LA64)
+static inline uint64_t readCycleCounter()
+{
+    uint64_t val;
+    asm volatile("rdtime.d %0, %1"
+                 : "=r"(val) : "r"(0));
+    return val;
+}
+
+static inline uint64_t readFreq()
+{
+    static size_t val = -1;
+
+    FILE* f = popen("cat /proc/cpuinfo | grep -i \"CPU MHz\" | head -n 1 | sed -r 's/CPU MHz.+:\\s{1,}//g'", "r");
+    if(f) {
+        char tmp[200] = "";
+        ssize_t s = fread(tmp, 1, 200, f);
+        pclose(f);
+        if (s > 0) return (uint64_t)atof(tmp) * 1e6;
+    }
+    
+    // fallback to rdtime + sleep
+    struct timespec ts;
+    ts.tv_sec = 0;
+    ts.tv_nsec = 50000000; // 50 milliseconds
+    uint64_t cycles = readCycleCounter();
+    nanosleep(&ts, NULL);
+    // round to MHz
+    val = (size_t)round(((double)(readCycleCounter() - cycles) * 20) / 1e6) * 1e6;
+    return (uint64_t)val;
+}
 #endif
 
 uint64_t ReadTSC(x64emu_t* emu)
@@ -709,7 +740,7 @@ uint64_t ReadTSC(x64emu_t* emu)
     (void)emu;
     
     // Hardware counter, per architecture
-#if defined(ARM64) || defined(RV64)
+#if defined(ARM64) || defined(RV64) || defined(LA64)
     if (!box64_rdtsc) return readCycleCounter();
 #endif
     // fall back to gettime...
@@ -728,7 +759,7 @@ uint64_t ReadTSCFrequency(x64emu_t* emu)
 {
     (void)emu;
     // Hardware counter, per architecture
-#if defined(ARM64) || defined(RV64)
+#if defined(ARM64) || defined(RV64) || defined(LA64)
     if (!box64_rdtsc) return readFreq();
 #endif
     // fall back to get time