From: kub <derkub@gmail.com>
Date: Tue, 4 Feb 2025 20:55:07 +0000 (+0100)
Subject: 32x, minor timer optimization
X-Git-Tag: v2.04~62
X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd3a0826e0e3c2b9d910f926e2a2fdcbd582a277;p=picodrive.git

32x, minor timer optimization
---

diff --git a/pico/32x/sh2soc.c b/pico/32x/sh2soc.c
index 0eebe427..7d1e0d45 100644
--- a/pico/32x/sh2soc.c
+++ b/pico/32x/sh2soc.c
@@ -190,7 +190,7 @@ static void dmac_trigger(SH2 *sh2, struct dma_chan *chan)
 }
 
 // timer state - FIXME
-static u32 timer_cycles[2];
+static u32 timer_cycles[2]; // TODO save this in state!
 static u32 timer_tick_cycles[2];
 static u32 timer_tick_factor[2];
 
@@ -207,13 +207,12 @@ void p32x_timers_recalc(void)
       sh2s[i].state |= SH2_TIMER_RUN;
     tmp = PREG8(sh2s[i].peri_regs, 0x80) & 7;
     // Sclk cycles per timer tick
-    cycles = 2;
-    if (tmp)
-      cycles <<= tmp + 4;
-    if (tmp >= 6)
-      cycles <<= 1;
+    if (tmp >= 6) tmp++;
+    if (tmp) tmp += 5;
+    else tmp = 1;
+    cycles = 1 << tmp;
     timer_tick_cycles[i] = cycles;
-    timer_tick_factor[i] = (1ULL << 32) / cycles;
+    timer_tick_factor[i] = tmp;
     timer_cycles[i] = 0;
     elprintf(EL_32XP, "WDT cycles[%d] = %d", i, cycles);
   }
@@ -228,8 +227,7 @@ NOINLINE void p32x_timer_do(SH2 *sh2, unsigned int m68k_slice)
   // WDT timer
   timer_cycles[i] += cycles;
   if (timer_cycles[i] > timer_tick_cycles[i]) {
-    // cnt = timer_cycles[i] / timer_tick_cycles[i];
-    cnt = (1ULL * timer_cycles[i] * timer_tick_factor[i]) >> 32;
+    cnt = (timer_cycles[i] >> timer_tick_factor[i]);
     timer_cycles[i] -= timer_tick_cycles[i] * cnt;
 
     cnt += PREG8(pregs, 0x81);