From b79c3d9b472db03baef1d76635d7481ad88559e3 Mon Sep 17 00:00:00 2001 From: Stacey D Son Date: Mon, 15 May 2017 21:50:53 +0200 Subject: [PATCH] More accurately emulate MIPS Count register. The MIPS Count register is incremented every 2 cycles, not every cycle (according to sys/mips/include/clock.h). Also, instead of using muldiv64() to convert from ticks to nanoseconds: y = muldiv64(x, get_ticks_per_sec(), TIMER_FREQ) where y = number of system ticks and x = device ticks we can just do: y = x * 10ns (for 100Mhz clock) --- hw/mips/cputimer.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/hw/mips/cputimer.c b/hw/mips/cputimer.c index 577c9aeab87..08877ef82de 100644 --- a/hw/mips/cputimer.c +++ b/hw/mips/cputimer.c @@ -25,7 +25,9 @@ #include "qemu/timer.h" #include "sysemu/kvm.h" -#define TIMER_FREQ 100 * 1000 * 1000 +#define CLOCK_PERIOD 10 /* 10 ns period for 100 Mhz frequency */ +#define CYCLES_PER_CNT 2 +#define TIMER_PERIOD (CYCLES_PER_CNT * CLOCK_PERIOD) /* XXX: do not use a global */ uint32_t cpu_mips_get_random (CPUMIPSState *env) @@ -49,9 +51,8 @@ static void cpu_mips_timer_update(CPUMIPSState *env) uint32_t wait; now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); - wait = env->CP0_Compare - env->CP0_Count - - (uint32_t)muldiv64(now, TIMER_FREQ, get_ticks_per_sec()); - next = now + muldiv64(wait, get_ticks_per_sec(), TIMER_FREQ); + wait = env->CP0_Compare - env->CP0_Count - (uint32_t)(now / TIMER_PERIOD); + next = now + (uint64_t)wait * CLOCK_PERIOD; timer_mod(env->timer, next); } @@ -79,8 +80,7 @@ uint32_t cpu_mips_get_count (CPUMIPSState *env) cpu_mips_timer_expire(env); } - return env->CP0_Count + - (uint32_t)muldiv64(now, TIMER_FREQ, get_ticks_per_sec()); + return env->CP0_Count + (uint32_t)(now / TIMER_PERIOD); } } @@ -95,9 +95,8 @@ void cpu_mips_store_count (CPUMIPSState *env, uint32_t count) env->CP0_Count = count; else { /* Store new count register */ - env->CP0_Count = - count - (uint32_t)muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), - TIMER_FREQ, get_ticks_per_sec()); + env->CP0_Count = count - + (uint32_t)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / TIMER_PERIOD); /* Update timer timer */ cpu_mips_timer_update(env); } @@ -121,8 +120,8 @@ void cpu_mips_start_count(CPUMIPSState *env) void cpu_mips_stop_count(CPUMIPSState *env) { /* Store the current value */ - env->CP0_Count += (uint32_t)muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), - TIMER_FREQ, get_ticks_per_sec()); + env->CP0_Count += (uint32_t)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / + TIMER_PERIOD); } static void mips_timer_cb (void *opaque)