root / hw / mips_timer.c @ fcb4a419
History | View | Annotate | Download (2.2 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 | fcb4a419 | ths | idx = (seed >> 16) % (env->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 | e16fe40c | ths | return env->CP0_Count +
|
21 | e16fe40c | ths | (uint32_t)muldiv64(qemu_get_clock(vm_clock), |
22 | e16fe40c | ths | 100 * 1000 * 1000, ticks_per_sec); |
23 | e16fe40c | ths | } |
24 | e16fe40c | ths | |
25 | 3529b538 | ths | void cpu_mips_store_count (CPUState *env, uint32_t count)
|
26 | e16fe40c | ths | { |
27 | e16fe40c | ths | uint64_t now, next; |
28 | e16fe40c | ths | uint32_t tmp; |
29 | 3529b538 | ths | uint32_t compare = env->CP0_Compare; |
30 | 39d51eb8 | ths | |
31 | e16fe40c | ths | tmp = count; |
32 | e16fe40c | ths | if (count == compare)
|
33 | e16fe40c | ths | tmp++; |
34 | e16fe40c | ths | now = qemu_get_clock(vm_clock); |
35 | e16fe40c | ths | next = now + muldiv64(compare - tmp, ticks_per_sec, 100 * 1000 * 1000); |
36 | e16fe40c | ths | if (next == now)
|
37 | e16fe40c | ths | next++; |
38 | e16fe40c | ths | #if 0
|
39 | e16fe40c | ths | if (logfile) {
|
40 | e16fe40c | ths | fprintf(logfile, "%s: 0x%08" PRIx64 " %08x %08x => 0x%08" PRIx64 "\n",
|
41 | e16fe40c | ths | __func__, now, count, compare, next - now);
|
42 | e16fe40c | ths | }
|
43 | e16fe40c | ths | #endif
|
44 | e16fe40c | ths | /* Store new count and compare registers */
|
45 | e16fe40c | ths | env->CP0_Compare = compare; |
46 | e16fe40c | ths | env->CP0_Count = |
47 | e16fe40c | ths | count - (uint32_t)muldiv64(now, 100 * 1000 * 1000, ticks_per_sec); |
48 | e16fe40c | ths | /* Adjust timer */
|
49 | e16fe40c | ths | qemu_mod_timer(env->timer, next); |
50 | e16fe40c | ths | } |
51 | e16fe40c | ths | |
52 | 3529b538 | ths | static void cpu_mips_update_count (CPUState *env, uint32_t count) |
53 | e16fe40c | ths | { |
54 | 3529b538 | ths | if (env->CP0_Cause & (1 << CP0Ca_DC)) |
55 | 3529b538 | ths | return;
|
56 | 3529b538 | ths | |
57 | 3529b538 | ths | cpu_mips_store_count(env, count); |
58 | e16fe40c | ths | } |
59 | e16fe40c | ths | |
60 | e16fe40c | ths | void cpu_mips_store_compare (CPUState *env, uint32_t value)
|
61 | e16fe40c | ths | { |
62 | 3529b538 | ths | env->CP0_Compare = value; |
63 | 3529b538 | ths | cpu_mips_update_count(env, cpu_mips_get_count(env)); |
64 | 39d51eb8 | ths | if ((env->CP0_Config0 & (0x7 << CP0C0_AR)) == (1 << CP0C0_AR)) |
65 | 39d51eb8 | ths | env->CP0_Cause &= ~(1 << CP0Ca_TI);
|
66 | d537cf6c | pbrook | qemu_irq_lower(env->irq[7]);
|
67 | e16fe40c | ths | } |
68 | e16fe40c | ths | |
69 | e16fe40c | ths | static void mips_timer_cb (void *opaque) |
70 | e16fe40c | ths | { |
71 | e16fe40c | ths | CPUState *env; |
72 | e16fe40c | ths | |
73 | e16fe40c | ths | env = opaque; |
74 | e16fe40c | ths | #if 0
|
75 | e16fe40c | ths | if (logfile) {
|
76 | e16fe40c | ths | fprintf(logfile, "%s\n", __func__);
|
77 | e16fe40c | ths | }
|
78 | e16fe40c | ths | #endif
|
79 | 3529b538 | ths | cpu_mips_update_count(env, cpu_mips_get_count(env)); |
80 | 39d51eb8 | ths | if ((env->CP0_Config0 & (0x7 << CP0C0_AR)) == (1 << CP0C0_AR)) |
81 | 39d51eb8 | ths | env->CP0_Cause |= 1 << CP0Ca_TI;
|
82 | d537cf6c | pbrook | qemu_irq_raise(env->irq[7]);
|
83 | e16fe40c | ths | } |
84 | e16fe40c | ths | |
85 | e16fe40c | ths | void cpu_mips_clock_init (CPUState *env)
|
86 | e16fe40c | ths | { |
87 | e16fe40c | ths | env->timer = qemu_new_timer(vm_clock, &mips_timer_cb, env); |
88 | e16fe40c | ths | env->CP0_Compare = 0;
|
89 | 3529b538 | ths | cpu_mips_update_count(env, 1);
|
90 | e16fe40c | ths | } |