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