]> Git Repo - qemu.git/blob - target-m68k/op_helper.c
kvm: fix traces to use %x instead of %d
[qemu.git] / target-m68k / op_helper.c
1 /*
2  *  M68K helper routines
3  *
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
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "cpu.h"
20 #include "helpers.h"
21
22 #if defined(CONFIG_USER_ONLY)
23
24 void m68k_cpu_do_interrupt(CPUState *cs)
25 {
26     M68kCPU *cpu = M68K_CPU(cs);
27     CPUM68KState *env = &cpu->env;
28
29     env->exception_index = -1;
30 }
31
32 void do_interrupt_m68k_hardirq(CPUM68KState *env)
33 {
34 }
35
36 #else
37
38 extern int semihosting_enabled;
39
40 #include "exec/softmmu_exec.h"
41
42 #define MMUSUFFIX _mmu
43
44 #define SHIFT 0
45 #include "exec/softmmu_template.h"
46
47 #define SHIFT 1
48 #include "exec/softmmu_template.h"
49
50 #define SHIFT 2
51 #include "exec/softmmu_template.h"
52
53 #define SHIFT 3
54 #include "exec/softmmu_template.h"
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) */
59 void tlb_fill(CPUM68KState *env, target_ulong addr, int is_write, int mmu_idx,
60               uintptr_t retaddr)
61 {
62     int ret;
63
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);
69         }
70         cpu_loop_exit(env);
71     }
72 }
73
74 static void do_rte(CPUM68KState *env)
75 {
76     uint32_t sp;
77     uint32_t fmt;
78
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;
86 }
87
88 static void do_interrupt_all(CPUM68KState *env, int is_hw)
89 {
90     CPUState *cs;
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.  */
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;
114             }
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;
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
128     vector = env->exception_index << 2;
129
130     sp = env->aregs[7];
131
132     fmt |= 0x40000000;
133     fmt |= (sp & 3) << 28;
134     fmt |= vector << 16;
135     fmt |= env->sr;
136
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
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);
153 }
154
155 void m68k_cpu_do_interrupt(CPUState *cs)
156 {
157     M68kCPU *cpu = M68K_CPU(cs);
158     CPUM68KState *env = &cpu->env;
159
160     do_interrupt_all(env, 0);
161 }
162
163 void do_interrupt_m68k_hardirq(CPUM68KState *env)
164 {
165     do_interrupt_all(env, 1);
166 }
167 #endif
168
169 static void raise_exception(CPUM68KState *env, int tt)
170 {
171     env->exception_index = tt;
172     cpu_loop_exit(env);
173 }
174
175 void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)
176 {
177     raise_exception(env, tt);
178 }
179
180 void HELPER(divu)(CPUM68KState *env, uint32_t word)
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.  */
191     if (den == 0) {
192         raise_exception(env, EXCP_DIV0);
193     }
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;
206 }
207
208 void HELPER(divs)(CPUM68KState *env, uint32_t word)
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;
218     if (den == 0) {
219         raise_exception(env, EXCP_DIV0);
220     }
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 }
This page took 0.039597 seconds and 4 git commands to generate.