cpu: Move exception_index field from CPU_COMMON to CPUState
[qemu.git] / target-m68k / op_helper.c
blob930d7c8d0462033d50a521d4145d798a4062691c
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 "helper.h"
22 #if defined(CONFIG_USER_ONLY)
24 void m68k_cpu_do_interrupt(CPUState *cs)
26 cs->exception_index = -1;
29 void do_interrupt_m68k_hardirq(CPUM68KState *env)
33 #else
35 extern int semihosting_enabled;
37 #include "exec/softmmu_exec.h"
39 #define MMUSUFFIX _mmu
41 #define SHIFT 0
42 #include "exec/softmmu_template.h"
44 #define SHIFT 1
45 #include "exec/softmmu_template.h"
47 #define SHIFT 2
48 #include "exec/softmmu_template.h"
50 #define SHIFT 3
51 #include "exec/softmmu_template.h"
53 /* Try to fill the TLB and return an exception if error. If retaddr is
54 NULL, it means that the function was called in C code (i.e. not
55 from generated code or from helper.c) */
56 void tlb_fill(CPUM68KState *env, target_ulong addr, int is_write, int mmu_idx,
57 uintptr_t retaddr)
59 M68kCPU *cpu = m68k_env_get_cpu(env);
60 int ret;
62 ret = m68k_cpu_handle_mmu_fault(CPU(cpu), addr, is_write, mmu_idx);
63 if (unlikely(ret)) {
64 if (retaddr) {
65 /* now we have a real cpu fault */
66 cpu_restore_state(env, retaddr);
68 cpu_loop_exit(env);
72 static void do_rte(CPUM68KState *env)
74 uint32_t sp;
75 uint32_t fmt;
77 sp = env->aregs[7];
78 fmt = cpu_ldl_kernel(env, sp);
79 env->pc = cpu_ldl_kernel(env, sp + 4);
80 sp |= (fmt >> 28) & 3;
81 env->sr = fmt & 0xffff;
82 m68k_switch_sp(env);
83 env->aregs[7] = sp + 8;
86 static void do_interrupt_all(CPUM68KState *env, int is_hw)
88 CPUState *cs = CPU(m68k_env_get_cpu(env));
89 uint32_t sp;
90 uint32_t fmt;
91 uint32_t retaddr;
92 uint32_t vector;
94 fmt = 0;
95 retaddr = env->pc;
97 if (!is_hw) {
98 switch (cs->exception_index) {
99 case EXCP_RTE:
100 /* Return from an exception. */
101 do_rte(env);
102 return;
103 case EXCP_HALT_INSN:
104 if (semihosting_enabled
105 && (env->sr & SR_S) != 0
106 && (env->pc & 3) == 0
107 && cpu_lduw_code(env, env->pc - 4) == 0x4e71
108 && cpu_ldl_code(env, env->pc) == 0x4e7bf000) {
109 env->pc += 4;
110 do_m68k_semihosting(env, env->dregs[0]);
111 return;
113 cs->halted = 1;
114 cs->exception_index = EXCP_HLT;
115 cpu_loop_exit(env);
116 return;
118 if (cs->exception_index >= EXCP_TRAP0
119 && cs->exception_index <= EXCP_TRAP15) {
120 /* Move the PC after the trap instruction. */
121 retaddr += 2;
125 vector = cs->exception_index << 2;
127 sp = env->aregs[7];
129 fmt |= 0x40000000;
130 fmt |= (sp & 3) << 28;
131 fmt |= vector << 16;
132 fmt |= env->sr;
134 env->sr |= SR_S;
135 if (is_hw) {
136 env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT);
137 env->sr &= ~SR_M;
139 m68k_switch_sp(env);
141 /* ??? This could cause MMU faults. */
142 sp &= ~3;
143 sp -= 4;
144 cpu_stl_kernel(env, sp, retaddr);
145 sp -= 4;
146 cpu_stl_kernel(env, sp, fmt);
147 env->aregs[7] = sp;
148 /* Jump to vector. */
149 env->pc = cpu_ldl_kernel(env, env->vbr + vector);
152 void m68k_cpu_do_interrupt(CPUState *cs)
154 M68kCPU *cpu = M68K_CPU(cs);
155 CPUM68KState *env = &cpu->env;
157 do_interrupt_all(env, 0);
160 void do_interrupt_m68k_hardirq(CPUM68KState *env)
162 do_interrupt_all(env, 1);
164 #endif
166 static void raise_exception(CPUM68KState *env, int tt)
168 CPUState *cs = CPU(m68k_env_get_cpu(env));
170 cs->exception_index = tt;
171 cpu_loop_exit(env);
174 void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)
176 raise_exception(env, tt);
179 void HELPER(divu)(CPUM68KState *env, uint32_t word)
181 uint32_t num;
182 uint32_t den;
183 uint32_t quot;
184 uint32_t rem;
185 uint32_t flags;
187 num = env->div1;
188 den = env->div2;
189 /* ??? This needs to make sure the throwing location is accurate. */
190 if (den == 0) {
191 raise_exception(env, EXCP_DIV0);
193 quot = num / den;
194 rem = num % den;
195 flags = 0;
196 if (word && quot > 0xffff)
197 flags |= CCF_V;
198 if (quot == 0)
199 flags |= CCF_Z;
200 else if ((int32_t)quot < 0)
201 flags |= CCF_N;
202 env->div1 = quot;
203 env->div2 = rem;
204 env->cc_dest = flags;
207 void HELPER(divs)(CPUM68KState *env, uint32_t word)
209 int32_t num;
210 int32_t den;
211 int32_t quot;
212 int32_t rem;
213 int32_t flags;
215 num = env->div1;
216 den = env->div2;
217 if (den == 0) {
218 raise_exception(env, EXCP_DIV0);
220 quot = num / den;
221 rem = num % den;
222 flags = 0;
223 if (word && quot != (int16_t)quot)
224 flags |= CCF_V;
225 if (quot == 0)
226 flags |= CCF_Z;
227 else if (quot < 0)
228 flags |= CCF_N;
229 env->div1 = quot;
230 env->div2 = rem;
231 env->cc_dest = flags;