Statistics
| Branch: | Revision:

root / hw / mips_timer.c @ 115646b6

History | View | Annotate | Download (2.8 kB)

1 e16fe40c ths
#include "vl.h"
2 e16fe40c ths
3 e16fe40c ths
void cpu_mips_irqctrl_init (void)
4 e16fe40c ths
{
5 e16fe40c ths
}
6 e16fe40c ths
7 e16fe40c ths
/* XXX: do not use a global */
8 e16fe40c ths
uint32_t cpu_mips_get_random (CPUState *env)
9 e16fe40c ths
{
10 e16fe40c ths
    static uint32_t seed = 0;
11 e16fe40c ths
    uint32_t idx;
12 e16fe40c ths
    seed = seed * 314159 + 1;
13 ead9360e ths
    idx = (seed >> 16) % (env->tlb->nb_tlb - env->CP0_Wired) + env->CP0_Wired;
14 e16fe40c ths
    return idx;
15 e16fe40c ths
}
16 e16fe40c ths
17 e16fe40c ths
/* MIPS R4K timer */
18 e16fe40c ths
uint32_t cpu_mips_get_count (CPUState *env)
19 e16fe40c ths
{
20 42532189 ths
    if (env->CP0_Cause & (1 << CP0Ca_DC))
21 42532189 ths
        return env->CP0_Count;
22 42532189 ths
    else
23 42532189 ths
        return env->CP0_Count +
24 42532189 ths
            (uint32_t)muldiv64(qemu_get_clock(vm_clock),
25 42532189 ths
                               100 * 1000 * 1000, ticks_per_sec);
26 e16fe40c ths
}
27 e16fe40c ths
28 3529b538 ths
void cpu_mips_store_count (CPUState *env, uint32_t count)
29 e16fe40c ths
{
30 e16fe40c ths
    uint64_t now, next;
31 e16fe40c ths
    uint32_t tmp;
32 3529b538 ths
    uint32_t compare = env->CP0_Compare;
33 39d51eb8 ths
34 e16fe40c ths
    tmp = count;
35 e16fe40c ths
    if (count == compare)
36 e16fe40c ths
        tmp++;
37 e16fe40c ths
    now = qemu_get_clock(vm_clock);
38 e16fe40c ths
    next = now + muldiv64(compare - tmp, ticks_per_sec, 100 * 1000 * 1000);
39 e16fe40c ths
    if (next == now)
40 e16fe40c ths
        next++;
41 e16fe40c ths
#if 0
42 e16fe40c ths
    if (logfile) {
43 e16fe40c ths
        fprintf(logfile, "%s: 0x%08" PRIx64 " %08x %08x => 0x%08" PRIx64 "\n",
44 e16fe40c ths
                __func__, now, count, compare, next - now);
45 e16fe40c ths
    }
46 e16fe40c ths
#endif
47 e16fe40c ths
    /* Store new count and compare registers */
48 e16fe40c ths
    env->CP0_Compare = compare;
49 e16fe40c ths
    env->CP0_Count =
50 e16fe40c ths
        count - (uint32_t)muldiv64(now, 100 * 1000 * 1000, ticks_per_sec);
51 e16fe40c ths
    /* Adjust timer */
52 e16fe40c ths
    qemu_mod_timer(env->timer, next);
53 e16fe40c ths
}
54 e16fe40c ths
55 3529b538 ths
static void cpu_mips_update_count (CPUState *env, uint32_t count)
56 e16fe40c ths
{
57 3529b538 ths
    if (env->CP0_Cause & (1 << CP0Ca_DC))
58 3529b538 ths
        return;
59 3529b538 ths
60 3529b538 ths
    cpu_mips_store_count(env, count);
61 e16fe40c ths
}
62 e16fe40c ths
63 e16fe40c ths
void cpu_mips_store_compare (CPUState *env, uint32_t value)
64 e16fe40c ths
{
65 3529b538 ths
    env->CP0_Compare = value;
66 3529b538 ths
    cpu_mips_update_count(env, cpu_mips_get_count(env));
67 39d51eb8 ths
    if ((env->CP0_Config0 & (0x7 << CP0C0_AR)) == (1 << CP0C0_AR))
68 39d51eb8 ths
        env->CP0_Cause &= ~(1 << CP0Ca_TI);
69 42532189 ths
    qemu_irq_lower(env->irq[(env->CP0_IntCtl >> CP0IntCtl_IPTI) & 0x7]);
70 42532189 ths
}
71 42532189 ths
72 42532189 ths
void cpu_mips_start_count(CPUState *env)
73 42532189 ths
{
74 42532189 ths
    cpu_mips_store_count(env, env->CP0_Count);
75 42532189 ths
}
76 42532189 ths
77 42532189 ths
void cpu_mips_stop_count(CPUState *env)
78 42532189 ths
{
79 42532189 ths
    /* Store the current value */
80 42532189 ths
    env->CP0_Count += (uint32_t)muldiv64(qemu_get_clock(vm_clock),
81 42532189 ths
                                         100 * 1000 * 1000, ticks_per_sec);
82 e16fe40c ths
}
83 e16fe40c ths
84 e16fe40c ths
static void mips_timer_cb (void *opaque)
85 e16fe40c ths
{
86 e16fe40c ths
    CPUState *env;
87 e16fe40c ths
88 e16fe40c ths
    env = opaque;
89 e16fe40c ths
#if 0
90 e16fe40c ths
    if (logfile) {
91 e16fe40c ths
        fprintf(logfile, "%s\n", __func__);
92 e16fe40c ths
    }
93 e16fe40c ths
#endif
94 42532189 ths
95 42532189 ths
    if (env->CP0_Cause & (1 << CP0Ca_DC))
96 42532189 ths
        return;
97 42532189 ths
98 3529b538 ths
    cpu_mips_update_count(env, cpu_mips_get_count(env));
99 39d51eb8 ths
    if ((env->CP0_Config0 & (0x7 << CP0C0_AR)) == (1 << CP0C0_AR))
100 39d51eb8 ths
        env->CP0_Cause |= 1 << CP0Ca_TI;
101 42532189 ths
    qemu_irq_raise(env->irq[(env->CP0_IntCtl >> CP0IntCtl_IPTI) & 0x7]);
102 e16fe40c ths
}
103 e16fe40c ths
104 e16fe40c ths
void cpu_mips_clock_init (CPUState *env)
105 e16fe40c ths
{
106 e16fe40c ths
    env->timer = qemu_new_timer(vm_clock, &mips_timer_cb, env);
107 e16fe40c ths
    env->CP0_Compare = 0;
108 3529b538 ths
    cpu_mips_update_count(env, 1);
109 e16fe40c ths
}