]>
Commit | Line | Data |
---|---|---|
0633879f PB |
1 | /* |
2 | * M68K helper routines | |
5fafdf24 | 3 | * |
0633879f PB |
4 | * Copyright (c) 2007 CodeSourcery |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
0633879f | 18 | */ |
d8416665 | 19 | #include "qemu/osdep.h" |
3e457172 | 20 | #include "cpu.h" |
2ef6175a | 21 | #include "exec/helper-proto.h" |
63c91552 | 22 | #include "exec/exec-all.h" |
f08b6170 | 23 | #include "exec/cpu_ldst.h" |
cfe67cef | 24 | #include "exec/semihost.h" |
0633879f PB |
25 | |
26 | #if defined(CONFIG_USER_ONLY) | |
27 | ||
97a8ea5a | 28 | void m68k_cpu_do_interrupt(CPUState *cs) |
3c688828 | 29 | { |
27103424 | 30 | cs->exception_index = -1; |
3c688828 BS |
31 | } |
32 | ||
ab409bb3 | 33 | static inline void do_interrupt_m68k_hardirq(CPUM68KState *env) |
0633879f | 34 | { |
0633879f PB |
35 | } |
36 | ||
37 | #else | |
38 | ||
0633879f PB |
39 | /* Try to fill the TLB and return an exception if error. If retaddr is |
40 | NULL, it means that the function was called in C code (i.e. not | |
41 | from generated code or from helper.c) */ | |
b35399bb SS |
42 | void tlb_fill(CPUState *cs, target_ulong addr, MMUAccessType access_type, |
43 | int mmu_idx, uintptr_t retaddr) | |
0633879f | 44 | { |
0633879f PB |
45 | int ret; |
46 | ||
b35399bb | 47 | ret = m68k_cpu_handle_mmu_fault(cs, addr, access_type, mmu_idx); |
551bd27f | 48 | if (unlikely(ret)) { |
0633879f PB |
49 | if (retaddr) { |
50 | /* now we have a real cpu fault */ | |
3f38f309 | 51 | cpu_restore_state(cs, retaddr); |
0633879f | 52 | } |
5638d180 | 53 | cpu_loop_exit(cs); |
0633879f | 54 | } |
0633879f PB |
55 | } |
56 | ||
31871141 | 57 | static void do_rte(CPUM68KState *env) |
0633879f PB |
58 | { |
59 | uint32_t sp; | |
60 | uint32_t fmt; | |
61 | ||
62 | sp = env->aregs[7]; | |
31871141 BS |
63 | fmt = cpu_ldl_kernel(env, sp); |
64 | env->pc = cpu_ldl_kernel(env, sp + 4); | |
0633879f PB |
65 | sp |= (fmt >> 28) & 3; |
66 | env->sr = fmt & 0xffff; | |
67 | env->aregs[7] = sp + 8; | |
0c8ff723 | 68 | m68k_switch_sp(env); |
0633879f PB |
69 | } |
70 | ||
31871141 | 71 | static void do_interrupt_all(CPUM68KState *env, int is_hw) |
0633879f | 72 | { |
27103424 | 73 | CPUState *cs = CPU(m68k_env_get_cpu(env)); |
0633879f PB |
74 | uint32_t sp; |
75 | uint32_t fmt; | |
76 | uint32_t retaddr; | |
77 | uint32_t vector; | |
78 | ||
79 | fmt = 0; | |
80 | retaddr = env->pc; | |
81 | ||
82 | if (!is_hw) { | |
27103424 | 83 | switch (cs->exception_index) { |
0633879f PB |
84 | case EXCP_RTE: |
85 | /* Return from an exception. */ | |
31871141 | 86 | do_rte(env); |
0633879f | 87 | return; |
a87295e8 | 88 | case EXCP_HALT_INSN: |
cfe67cef | 89 | if (semihosting_enabled() |
a87295e8 PB |
90 | && (env->sr & SR_S) != 0 |
91 | && (env->pc & 3) == 0 | |
31871141 BS |
92 | && cpu_lduw_code(env, env->pc - 4) == 0x4e71 |
93 | && cpu_ldl_code(env, env->pc) == 0x4e7bf000) { | |
a87295e8 PB |
94 | env->pc += 4; |
95 | do_m68k_semihosting(env, env->dregs[0]); | |
96 | return; | |
97 | } | |
259186a7 | 98 | cs->halted = 1; |
27103424 | 99 | cs->exception_index = EXCP_HLT; |
5638d180 | 100 | cpu_loop_exit(cs); |
a87295e8 | 101 | return; |
0633879f | 102 | } |
27103424 AF |
103 | if (cs->exception_index >= EXCP_TRAP0 |
104 | && cs->exception_index <= EXCP_TRAP15) { | |
0633879f PB |
105 | /* Move the PC after the trap instruction. */ |
106 | retaddr += 2; | |
107 | } | |
108 | } | |
109 | ||
27103424 | 110 | vector = cs->exception_index << 2; |
0633879f PB |
111 | |
112 | fmt |= 0x40000000; | |
0633879f PB |
113 | fmt |= vector << 16; |
114 | fmt |= env->sr; | |
115 | ||
20dcee94 PB |
116 | env->sr |= SR_S; |
117 | if (is_hw) { | |
118 | env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT); | |
119 | env->sr &= ~SR_M; | |
120 | } | |
121 | m68k_switch_sp(env); | |
0c8ff723 GU |
122 | sp = env->aregs[7]; |
123 | fmt |= (sp & 3) << 28; | |
20dcee94 | 124 | |
0633879f PB |
125 | /* ??? This could cause MMU faults. */ |
126 | sp &= ~3; | |
127 | sp -= 4; | |
31871141 | 128 | cpu_stl_kernel(env, sp, retaddr); |
0633879f | 129 | sp -= 4; |
31871141 | 130 | cpu_stl_kernel(env, sp, fmt); |
0633879f | 131 | env->aregs[7] = sp; |
0633879f | 132 | /* Jump to vector. */ |
31871141 | 133 | env->pc = cpu_ldl_kernel(env, env->vbr + vector); |
0633879f PB |
134 | } |
135 | ||
97a8ea5a | 136 | void m68k_cpu_do_interrupt(CPUState *cs) |
3c688828 | 137 | { |
97a8ea5a AF |
138 | M68kCPU *cpu = M68K_CPU(cs); |
139 | CPUM68KState *env = &cpu->env; | |
140 | ||
31871141 | 141 | do_interrupt_all(env, 0); |
3c688828 BS |
142 | } |
143 | ||
ab409bb3 | 144 | static inline void do_interrupt_m68k_hardirq(CPUM68KState *env) |
3c688828 | 145 | { |
31871141 | 146 | do_interrupt_all(env, 1); |
3c688828 | 147 | } |
0633879f | 148 | #endif |
e1f3808e | 149 | |
ab409bb3 RH |
150 | bool m68k_cpu_exec_interrupt(CPUState *cs, int interrupt_request) |
151 | { | |
152 | M68kCPU *cpu = M68K_CPU(cs); | |
153 | CPUM68KState *env = &cpu->env; | |
154 | ||
155 | if (interrupt_request & CPU_INTERRUPT_HARD | |
156 | && ((env->sr & SR_I) >> SR_I_SHIFT) < env->pending_level) { | |
157 | /* Real hardware gets the interrupt vector via an IACK cycle | |
158 | at this point. Current emulated hardware doesn't rely on | |
159 | this, so we provide/save the vector when the interrupt is | |
160 | first signalled. */ | |
161 | cs->exception_index = env->pending_vector; | |
162 | do_interrupt_m68k_hardirq(env); | |
163 | return true; | |
164 | } | |
165 | return false; | |
166 | } | |
167 | ||
31871141 | 168 | static void raise_exception(CPUM68KState *env, int tt) |
e1f3808e | 169 | { |
27103424 AF |
170 | CPUState *cs = CPU(m68k_env_get_cpu(env)); |
171 | ||
172 | cs->exception_index = tt; | |
5638d180 | 173 | cpu_loop_exit(cs); |
e1f3808e PB |
174 | } |
175 | ||
31871141 | 176 | void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt) |
e1f3808e | 177 | { |
31871141 | 178 | raise_exception(env, tt); |
e1f3808e PB |
179 | } |
180 | ||
2b3e3cfe | 181 | void HELPER(divu)(CPUM68KState *env, uint32_t word) |
e1f3808e PB |
182 | { |
183 | uint32_t num; | |
184 | uint32_t den; | |
185 | uint32_t quot; | |
186 | uint32_t rem; | |
187 | uint32_t flags; | |
188 | ||
189 | num = env->div1; | |
190 | den = env->div2; | |
191 | /* ??? This needs to make sure the throwing location is accurate. */ | |
31871141 BS |
192 | if (den == 0) { |
193 | raise_exception(env, EXCP_DIV0); | |
194 | } | |
e1f3808e PB |
195 | quot = num / den; |
196 | rem = num % den; | |
197 | flags = 0; | |
e1f3808e PB |
198 | if (word && quot > 0xffff) |
199 | flags |= CCF_V; | |
200 | if (quot == 0) | |
201 | flags |= CCF_Z; | |
202 | else if ((int32_t)quot < 0) | |
203 | flags |= CCF_N; | |
204 | env->div1 = quot; | |
205 | env->div2 = rem; | |
206 | env->cc_dest = flags; | |
207 | } | |
208 | ||
2b3e3cfe | 209 | void HELPER(divs)(CPUM68KState *env, uint32_t word) |
e1f3808e PB |
210 | { |
211 | int32_t num; | |
212 | int32_t den; | |
213 | int32_t quot; | |
214 | int32_t rem; | |
215 | int32_t flags; | |
216 | ||
217 | num = env->div1; | |
218 | den = env->div2; | |
31871141 BS |
219 | if (den == 0) { |
220 | raise_exception(env, EXCP_DIV0); | |
221 | } | |
e1f3808e PB |
222 | quot = num / den; |
223 | rem = num % den; | |
224 | flags = 0; | |
225 | if (word && quot != (int16_t)quot) | |
226 | flags |= CCF_V; | |
227 | if (quot == 0) | |
228 | flags |= CCF_Z; | |
229 | else if (quot < 0) | |
230 | flags |= CCF_N; | |
231 | env->div1 = quot; | |
232 | env->div2 = rem; | |
233 | env->cc_dest = flags; | |
234 | } |