cpu: Replace do_interrupt() by CPUClass::do_interrupt method
[qemu/agraf.git] / target-m68k / op_helper.c
blob30f7d8b1ab303a3180aab86c9c2da798c259658f
1 /*
2 * M68K helper routines
4 * Copyright (c) 2007 CodeSourcery
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "cpu.h"
20 #include "helpers.h"
22 #if defined(CONFIG_USER_ONLY)
24 void m68k_cpu_do_interrupt(CPUState *cs)
26 M68kCPU *cpu = M68K_CPU(cs);
27 CPUM68KState *env = &cpu->env;
29 env->exception_index = -1;
32 void do_interrupt_m68k_hardirq(CPUM68KState *env)
36 #else
38 extern int semihosting_enabled;
40 #include "exec/softmmu_exec.h"
42 #define MMUSUFFIX _mmu
44 #define SHIFT 0
45 #include "exec/softmmu_template.h"
47 #define SHIFT 1
48 #include "exec/softmmu_template.h"
50 #define SHIFT 2
51 #include "exec/softmmu_template.h"
53 #define SHIFT 3
54 #include "exec/softmmu_template.h"
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) */
59 void tlb_fill(CPUM68KState *env, target_ulong addr, int is_write, int mmu_idx,
60 uintptr_t retaddr)
62 int ret;
64 ret = cpu_m68k_handle_mmu_fault(env, addr, is_write, mmu_idx);
65 if (unlikely(ret)) {
66 if (retaddr) {
67 /* now we have a real cpu fault */
68 cpu_restore_state(env, retaddr);
70 cpu_loop_exit(env);
74 static void do_rte(CPUM68KState *env)
76 uint32_t sp;
77 uint32_t fmt;
79 sp = env->aregs[7];
80 fmt = cpu_ldl_kernel(env, sp);
81 env->pc = cpu_ldl_kernel(env, sp + 4);
82 sp |= (fmt >> 28) & 3;
83 env->sr = fmt & 0xffff;
84 m68k_switch_sp(env);
85 env->aregs[7] = sp + 8;
88 static void do_interrupt_all(CPUM68KState *env, int is_hw)
90 CPUState *cs;
91 uint32_t sp;
92 uint32_t fmt;
93 uint32_t retaddr;
94 uint32_t vector;
96 fmt = 0;
97 retaddr = env->pc;
99 if (!is_hw) {
100 switch (env->exception_index) {
101 case EXCP_RTE:
102 /* Return from an exception. */
103 do_rte(env);
104 return;
105 case EXCP_HALT_INSN:
106 if (semihosting_enabled
107 && (env->sr & SR_S) != 0
108 && (env->pc & 3) == 0
109 && cpu_lduw_code(env, env->pc - 4) == 0x4e71
110 && cpu_ldl_code(env, env->pc) == 0x4e7bf000) {
111 env->pc += 4;
112 do_m68k_semihosting(env, env->dregs[0]);
113 return;
115 cs = CPU(m68k_env_get_cpu(env));
116 cs->halted = 1;
117 env->exception_index = EXCP_HLT;
118 cpu_loop_exit(env);
119 return;
121 if (env->exception_index >= EXCP_TRAP0
122 && env->exception_index <= EXCP_TRAP15) {
123 /* Move the PC after the trap instruction. */
124 retaddr += 2;
128 vector = env->exception_index << 2;
130 sp = env->aregs[7];
132 fmt |= 0x40000000;
133 fmt |= (sp & 3) << 28;
134 fmt |= vector << 16;
135 fmt |= env->sr;
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;
142 m68k_switch_sp(env);
144 /* ??? This could cause MMU faults. */
145 sp &= ~3;
146 sp -= 4;
147 cpu_stl_kernel(env, sp, retaddr);
148 sp -= 4;
149 cpu_stl_kernel(env, sp, fmt);
150 env->aregs[7] = sp;
151 /* Jump to vector. */
152 env->pc = cpu_ldl_kernel(env, env->vbr + vector);
155 void m68k_cpu_do_interrupt(CPUState *cs)
157 M68kCPU *cpu = M68K_CPU(cs);
158 CPUM68KState *env = &cpu->env;
160 do_interrupt_all(env, 0);
163 void do_interrupt_m68k_hardirq(CPUM68KState *env)
165 do_interrupt_all(env, 1);
167 #endif
169 static void raise_exception(CPUM68KState *env, int tt)
171 env->exception_index = tt;
172 cpu_loop_exit(env);
175 void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)
177 raise_exception(env, tt);
180 void HELPER(divu)(CPUM68KState *env, uint32_t word)
182 uint32_t num;
183 uint32_t den;
184 uint32_t quot;
185 uint32_t rem;
186 uint32_t flags;
188 num = env->div1;
189 den = env->div2;
190 /* ??? This needs to make sure the throwing location is accurate. */
191 if (den == 0) {
192 raise_exception(env, EXCP_DIV0);
194 quot = num / den;
195 rem = num % den;
196 flags = 0;
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;
208 void HELPER(divs)(CPUM68KState *env, uint32_t word)
210 int32_t num;
211 int32_t den;
212 int32_t quot;
213 int32_t rem;
214 int32_t flags;
216 num = env->div1;
217 den = env->div2;
218 if (den == 0) {
219 raise_exception(env, EXCP_DIV0);
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;