]> git.proxmox.com Git - qemu.git/blame - target-m68k/op_helper.c
Open 2.0 development tree
[qemu.git] / target-m68k / op_helper.c
CommitLineData
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 */
3e457172 19#include "cpu.h"
e5e84d22 20#include "helper.h"
0633879f
PB
21
22#if defined(CONFIG_USER_ONLY)
23
97a8ea5a 24void m68k_cpu_do_interrupt(CPUState *cs)
3c688828 25{
97a8ea5a
AF
26 M68kCPU *cpu = M68K_CPU(cs);
27 CPUM68KState *env = &cpu->env;
28
31871141 29 env->exception_index = -1;
3c688828
BS
30}
31
31871141 32void do_interrupt_m68k_hardirq(CPUM68KState *env)
0633879f 33{
0633879f
PB
34}
35
36#else
37
a87295e8
PB
38extern int semihosting_enabled;
39
022c62cb 40#include "exec/softmmu_exec.h"
3e457172 41
0633879f 42#define MMUSUFFIX _mmu
0633879f
PB
43
44#define SHIFT 0
022c62cb 45#include "exec/softmmu_template.h"
0633879f
PB
46
47#define SHIFT 1
022c62cb 48#include "exec/softmmu_template.h"
0633879f
PB
49
50#define SHIFT 2
022c62cb 51#include "exec/softmmu_template.h"
0633879f
PB
52
53#define SHIFT 3
022c62cb 54#include "exec/softmmu_template.h"
0633879f
PB
55
56/* Try to fill the TLB and return an exception if error. If retaddr is
57 NULL, it means that the function was called in C code (i.e. not
58 from generated code or from helper.c) */
31871141 59void tlb_fill(CPUM68KState *env, target_ulong addr, int is_write, int mmu_idx,
20503968 60 uintptr_t retaddr)
0633879f 61{
0633879f
PB
62 int ret;
63
97b348e7 64 ret = cpu_m68k_handle_mmu_fault(env, addr, is_write, mmu_idx);
551bd27f 65 if (unlikely(ret)) {
0633879f
PB
66 if (retaddr) {
67 /* now we have a real cpu fault */
a8a826a3 68 cpu_restore_state(env, retaddr);
0633879f 69 }
1162c041 70 cpu_loop_exit(env);
0633879f 71 }
0633879f
PB
72}
73
31871141 74static void do_rte(CPUM68KState *env)
0633879f
PB
75{
76 uint32_t sp;
77 uint32_t fmt;
78
79 sp = env->aregs[7];
31871141
BS
80 fmt = cpu_ldl_kernel(env, sp);
81 env->pc = cpu_ldl_kernel(env, sp + 4);
0633879f
PB
82 sp |= (fmt >> 28) & 3;
83 env->sr = fmt & 0xffff;
20dcee94 84 m68k_switch_sp(env);
0633879f
PB
85 env->aregs[7] = sp + 8;
86}
87
31871141 88static void do_interrupt_all(CPUM68KState *env, int is_hw)
0633879f 89{
259186a7 90 CPUState *cs;
0633879f
PB
91 uint32_t sp;
92 uint32_t fmt;
93 uint32_t retaddr;
94 uint32_t vector;
95
96 fmt = 0;
97 retaddr = env->pc;
98
99 if (!is_hw) {
100 switch (env->exception_index) {
101 case EXCP_RTE:
102 /* Return from an exception. */
31871141 103 do_rte(env);
0633879f 104 return;
a87295e8
PB
105 case EXCP_HALT_INSN:
106 if (semihosting_enabled
107 && (env->sr & SR_S) != 0
108 && (env->pc & 3) == 0
31871141
BS
109 && cpu_lduw_code(env, env->pc - 4) == 0x4e71
110 && cpu_ldl_code(env, env->pc) == 0x4e7bf000) {
a87295e8
PB
111 env->pc += 4;
112 do_m68k_semihosting(env, env->dregs[0]);
113 return;
114 }
259186a7
AF
115 cs = CPU(m68k_env_get_cpu(env));
116 cs->halted = 1;
a87295e8 117 env->exception_index = EXCP_HLT;
1162c041 118 cpu_loop_exit(env);
a87295e8 119 return;
0633879f
PB
120 }
121 if (env->exception_index >= EXCP_TRAP0
122 && env->exception_index <= EXCP_TRAP15) {
123 /* Move the PC after the trap instruction. */
124 retaddr += 2;
125 }
126 }
127
0633879f
PB
128 vector = env->exception_index << 2;
129
0cf5c677
PB
130 sp = env->aregs[7];
131
0633879f
PB
132 fmt |= 0x40000000;
133 fmt |= (sp & 3) << 28;
134 fmt |= vector << 16;
135 fmt |= env->sr;
136
20dcee94
PB
137 env->sr |= SR_S;
138 if (is_hw) {
139 env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT);
140 env->sr &= ~SR_M;
141 }
142 m68k_switch_sp(env);
143
0633879f
PB
144 /* ??? This could cause MMU faults. */
145 sp &= ~3;
146 sp -= 4;
31871141 147 cpu_stl_kernel(env, sp, retaddr);
0633879f 148 sp -= 4;
31871141 149 cpu_stl_kernel(env, sp, fmt);
0633879f 150 env->aregs[7] = sp;
0633879f 151 /* Jump to vector. */
31871141 152 env->pc = cpu_ldl_kernel(env, env->vbr + vector);
0633879f
PB
153}
154
97a8ea5a 155void m68k_cpu_do_interrupt(CPUState *cs)
3c688828 156{
97a8ea5a
AF
157 M68kCPU *cpu = M68K_CPU(cs);
158 CPUM68KState *env = &cpu->env;
159
31871141 160 do_interrupt_all(env, 0);
3c688828
BS
161}
162
31871141 163void do_interrupt_m68k_hardirq(CPUM68KState *env)
3c688828 164{
31871141 165 do_interrupt_all(env, 1);
3c688828 166}
0633879f 167#endif
e1f3808e 168
31871141 169static void raise_exception(CPUM68KState *env, int tt)
e1f3808e
PB
170{
171 env->exception_index = tt;
1162c041 172 cpu_loop_exit(env);
e1f3808e
PB
173}
174
31871141 175void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)
e1f3808e 176{
31871141 177 raise_exception(env, tt);
e1f3808e
PB
178}
179
2b3e3cfe 180void HELPER(divu)(CPUM68KState *env, uint32_t word)
e1f3808e
PB
181{
182 uint32_t num;
183 uint32_t den;
184 uint32_t quot;
185 uint32_t rem;
186 uint32_t flags;
187
188 num = env->div1;
189 den = env->div2;
190 /* ??? This needs to make sure the throwing location is accurate. */
31871141
BS
191 if (den == 0) {
192 raise_exception(env, EXCP_DIV0);
193 }
e1f3808e
PB
194 quot = num / den;
195 rem = num % den;
196 flags = 0;
e1f3808e
PB
197 if (word && quot > 0xffff)
198 flags |= CCF_V;
199 if (quot == 0)
200 flags |= CCF_Z;
201 else if ((int32_t)quot < 0)
202 flags |= CCF_N;
203 env->div1 = quot;
204 env->div2 = rem;
205 env->cc_dest = flags;
206}
207
2b3e3cfe 208void HELPER(divs)(CPUM68KState *env, uint32_t word)
e1f3808e
PB
209{
210 int32_t num;
211 int32_t den;
212 int32_t quot;
213 int32_t rem;
214 int32_t flags;
215
216 num = env->div1;
217 den = env->div2;
31871141
BS
218 if (den == 0) {
219 raise_exception(env, EXCP_DIV0);
220 }
e1f3808e
PB
221 quot = num / den;
222 rem = num % den;
223 flags = 0;
224 if (word && quot != (int16_t)quot)
225 flags |= CCF_V;
226 if (quot == 0)
227 flags |= CCF_Z;
228 else if (quot < 0)
229 flags |= CCF_N;
230 env->div1 = quot;
231 env->div2 = rem;
232 env->cc_dest = flags;
233}