32x, minor timer optimization
authorkub <derkub@gmail.com>
Tue, 4 Feb 2025 20:55:07 +0000 (21:55 +0100)
committerkub <derkub@gmail.com>
Tue, 4 Feb 2025 20:55:07 +0000 (21:55 +0100)
pico/32x/sh2soc.c

index 0eebe42..7d1e0d4 100644 (file)
@@ -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);