Statistics
| Branch: | Revision:

root / target-cris / op_helper.c @ 97aff481

History | View | Annotate | Download (13.5 kB)

1 81fdc5f8 ths
/*
2 81fdc5f8 ths
 *  CRIS helper routines
3 81fdc5f8 ths
 *
4 81fdc5f8 ths
 *  Copyright (c) 2007 AXIS Communications
5 81fdc5f8 ths
 *  Written by Edgar E. Iglesias
6 81fdc5f8 ths
 *
7 81fdc5f8 ths
 * This library is free software; you can redistribute it and/or
8 81fdc5f8 ths
 * modify it under the terms of the GNU Lesser General Public
9 81fdc5f8 ths
 * License as published by the Free Software Foundation; either
10 81fdc5f8 ths
 * version 2 of the License, or (at your option) any later version.
11 81fdc5f8 ths
 *
12 81fdc5f8 ths
 * This library is distributed in the hope that it will be useful,
13 81fdc5f8 ths
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 81fdc5f8 ths
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 81fdc5f8 ths
 * Lesser General Public License for more details.
16 81fdc5f8 ths
 *
17 81fdc5f8 ths
 * You should have received a copy of the GNU Lesser General Public
18 81fdc5f8 ths
 * License along with this library; if not, write to the Free Software
19 fad6cb1a aurel32
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
20 81fdc5f8 ths
 */
21 81fdc5f8 ths
22 81fdc5f8 ths
#include "exec.h"
23 786c02f1 edgar_igl
#include "mmu.h"
24 30abcfc7 edgar_igl
#include "helper.h"
25 c38ac98d edgar_igl
#include "host-utils.h"
26 81fdc5f8 ths
27 d12d51d5 aliguori
//#define CRIS_OP_HELPER_DEBUG
28 d12d51d5 aliguori
29 d12d51d5 aliguori
30 d12d51d5 aliguori
#ifdef CRIS_OP_HELPER_DEBUG
31 d12d51d5 aliguori
#define D(x) x
32 93fcfe39 aliguori
#define D_LOG(...) qemu_log(__VA__ARGS__)
33 d12d51d5 aliguori
#else
34 e2eef170 pbrook
#define D(x)
35 d12d51d5 aliguori
#define D_LOG(...) do { } while (0)
36 d12d51d5 aliguori
#endif
37 e2eef170 pbrook
38 e2eef170 pbrook
#if !defined(CONFIG_USER_ONLY)
39 e2eef170 pbrook
40 81fdc5f8 ths
#define MMUSUFFIX _mmu
41 81fdc5f8 ths
42 81fdc5f8 ths
#define SHIFT 0
43 81fdc5f8 ths
#include "softmmu_template.h"
44 81fdc5f8 ths
45 81fdc5f8 ths
#define SHIFT 1
46 81fdc5f8 ths
#include "softmmu_template.h"
47 81fdc5f8 ths
48 81fdc5f8 ths
#define SHIFT 2
49 81fdc5f8 ths
#include "softmmu_template.h"
50 81fdc5f8 ths
51 81fdc5f8 ths
#define SHIFT 3
52 81fdc5f8 ths
#include "softmmu_template.h"
53 81fdc5f8 ths
54 81fdc5f8 ths
/* Try to fill the TLB and return an exception if error. If retaddr is
55 81fdc5f8 ths
   NULL, it means that the function was called in C code (i.e. not
56 81fdc5f8 ths
   from generated code or from helper.c) */
57 81fdc5f8 ths
/* XXX: fix it to restore all registers */
58 6ebbf390 j_mayer
void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
59 81fdc5f8 ths
{
60 81fdc5f8 ths
    TranslationBlock *tb;
61 81fdc5f8 ths
    CPUState *saved_env;
62 44f8625d bellard
    unsigned long pc;
63 81fdc5f8 ths
    int ret;
64 81fdc5f8 ths
65 81fdc5f8 ths
    /* XXX: hack to restore env in all cases, even if not called from
66 81fdc5f8 ths
       generated code */
67 81fdc5f8 ths
    saved_env = env;
68 81fdc5f8 ths
    env = cpu_single_env;
69 b41f7df0 edgar_igl
70 d12d51d5 aliguori
    D_LOG("%s pc=%x tpc=%x ra=%x\n", __func__, 
71 d12d51d5 aliguori
             env->pc, env->debug1, retaddr);
72 6ebbf390 j_mayer
    ret = cpu_cris_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
73 551bd27f ths
    if (unlikely(ret)) {
74 81fdc5f8 ths
        if (retaddr) {
75 81fdc5f8 ths
            /* now we have a real cpu fault */
76 44f8625d bellard
            pc = (unsigned long)retaddr;
77 81fdc5f8 ths
            tb = tb_find_pc(pc);
78 81fdc5f8 ths
            if (tb) {
79 81fdc5f8 ths
                /* the PC is inside the translated code. It means that we have
80 81fdc5f8 ths
                   a virtual CPU fault */
81 81fdc5f8 ths
                cpu_restore_state(tb, env, pc, NULL);
82 30abcfc7 edgar_igl
83 30abcfc7 edgar_igl
                /* Evaluate flags after retranslation.  */
84 30abcfc7 edgar_igl
                helper_top_evaluate_flags();
85 81fdc5f8 ths
            }
86 81fdc5f8 ths
        }
87 81fdc5f8 ths
        cpu_loop_exit();
88 81fdc5f8 ths
    }
89 81fdc5f8 ths
    env = saved_env;
90 81fdc5f8 ths
}
91 81fdc5f8 ths
92 e2eef170 pbrook
#endif
93 e2eef170 pbrook
94 dceaf394 edgar_igl
void helper_raise_exception(uint32_t index)
95 786c02f1 edgar_igl
{
96 dceaf394 edgar_igl
        env->exception_index = index;
97 dceaf394 edgar_igl
        cpu_loop_exit();
98 786c02f1 edgar_igl
}
99 786c02f1 edgar_igl
100 cf1d97f0 edgar_igl
void helper_tlb_flush_pid(uint32_t pid)
101 cf1d97f0 edgar_igl
{
102 cf1d97f0 edgar_igl
#if !defined(CONFIG_USER_ONLY)
103 28de16da edgar_igl
        pid &= 0xff;
104 28de16da edgar_igl
        if (pid != (env->pregs[PR_PID] & 0xff))
105 28de16da edgar_igl
                cris_mmu_flush_pid(env, env->pregs[PR_PID]);
106 cf1d97f0 edgar_igl
#endif
107 cf1d97f0 edgar_igl
}
108 cf1d97f0 edgar_igl
109 a1aebcb8 edgar_igl
void helper_spc_write(uint32_t new_spc)
110 a1aebcb8 edgar_igl
{
111 a1aebcb8 edgar_igl
#if !defined(CONFIG_USER_ONLY)
112 a1aebcb8 edgar_igl
        tlb_flush_page(env, env->pregs[PR_SPC]);
113 a1aebcb8 edgar_igl
        tlb_flush_page(env, new_spc);
114 a1aebcb8 edgar_igl
#endif
115 a1aebcb8 edgar_igl
}
116 a1aebcb8 edgar_igl
117 30abcfc7 edgar_igl
void helper_dump(uint32_t a0, uint32_t a1, uint32_t a2)
118 b41f7df0 edgar_igl
{
119 93fcfe39 aliguori
        qemu_log("%s: a0=%x a1=%x\n", __func__, a0, a1);
120 b41f7df0 edgar_igl
}
121 b41f7df0 edgar_igl
122 cf1d97f0 edgar_igl
/* Used by the tlb decoder.  */
123 cf1d97f0 edgar_igl
#define EXTRACT_FIELD(src, start, end) \
124 cf1d97f0 edgar_igl
            (((src) >> start) & ((1 << (end - start + 1)) - 1))
125 cf1d97f0 edgar_igl
126 dceaf394 edgar_igl
void helper_movl_sreg_reg (uint32_t sreg, uint32_t reg)
127 dceaf394 edgar_igl
{
128 dceaf394 edgar_igl
        uint32_t srs;
129 dceaf394 edgar_igl
        srs = env->pregs[PR_SRS];
130 dceaf394 edgar_igl
        srs &= 3;
131 dceaf394 edgar_igl
        env->sregs[srs][sreg] = env->regs[reg];
132 dceaf394 edgar_igl
133 dceaf394 edgar_igl
#if !defined(CONFIG_USER_ONLY)
134 dceaf394 edgar_igl
        if (srs == 1 || srs == 2) {
135 dceaf394 edgar_igl
                if (sreg == 6) {
136 dceaf394 edgar_igl
                        /* Writes to tlb-hi write to mm_cause as a side 
137 dceaf394 edgar_igl
                           effect.  */
138 6913ba56 edgar_igl
                        env->sregs[SFR_RW_MM_TLB_HI] = env->regs[reg];
139 6913ba56 edgar_igl
                        env->sregs[SFR_R_MM_CAUSE] = env->regs[reg];
140 dceaf394 edgar_igl
                }
141 dceaf394 edgar_igl
                else if (sreg == 5) {
142 dceaf394 edgar_igl
                        uint32_t set;
143 dceaf394 edgar_igl
                        uint32_t idx;
144 dceaf394 edgar_igl
                        uint32_t lo, hi;
145 dceaf394 edgar_igl
                        uint32_t vaddr;
146 cf1d97f0 edgar_igl
                        int tlb_v;
147 dceaf394 edgar_igl
148 dceaf394 edgar_igl
                        idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
149 dceaf394 edgar_igl
                        set >>= 4;
150 dceaf394 edgar_igl
                        set &= 3;
151 dceaf394 edgar_igl
152 dceaf394 edgar_igl
                        idx &= 15;
153 dceaf394 edgar_igl
                        /* We've just made a write to tlb_lo.  */
154 dceaf394 edgar_igl
                        lo = env->sregs[SFR_RW_MM_TLB_LO];
155 dceaf394 edgar_igl
                        /* Writes are done via r_mm_cause.  */
156 dceaf394 edgar_igl
                        hi = env->sregs[SFR_R_MM_CAUSE];
157 cf1d97f0 edgar_igl
158 cf1d97f0 edgar_igl
                        vaddr = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].hi,
159 cf1d97f0 edgar_igl
                                              13, 31);
160 cf1d97f0 edgar_igl
                        vaddr <<= TARGET_PAGE_BITS;
161 cf1d97f0 edgar_igl
                        tlb_v = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].lo,
162 cf1d97f0 edgar_igl
                                            3, 3);
163 dceaf394 edgar_igl
                        env->tlbsets[srs - 1][set][idx].lo = lo;
164 dceaf394 edgar_igl
                        env->tlbsets[srs - 1][set][idx].hi = hi;
165 cf1d97f0 edgar_igl
166 d12d51d5 aliguori
                        D_LOG("tlb flush vaddr=%x v=%d pc=%x\n", 
167 d12d51d5 aliguori
                                  vaddr, tlb_v, env->pc);
168 cf1d97f0 edgar_igl
                        tlb_flush_page(env, vaddr);
169 dceaf394 edgar_igl
                }
170 dceaf394 edgar_igl
        }
171 dceaf394 edgar_igl
#endif
172 dceaf394 edgar_igl
}
173 dceaf394 edgar_igl
174 dceaf394 edgar_igl
void helper_movl_reg_sreg (uint32_t reg, uint32_t sreg)
175 dceaf394 edgar_igl
{
176 dceaf394 edgar_igl
        uint32_t srs;
177 dceaf394 edgar_igl
        env->pregs[PR_SRS] &= 3;
178 dceaf394 edgar_igl
        srs = env->pregs[PR_SRS];
179 dceaf394 edgar_igl
        
180 dceaf394 edgar_igl
#if !defined(CONFIG_USER_ONLY)
181 dceaf394 edgar_igl
        if (srs == 1 || srs == 2)
182 dceaf394 edgar_igl
        {
183 dceaf394 edgar_igl
                uint32_t set;
184 dceaf394 edgar_igl
                uint32_t idx;
185 dceaf394 edgar_igl
                uint32_t lo, hi;
186 dceaf394 edgar_igl
187 dceaf394 edgar_igl
                idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
188 dceaf394 edgar_igl
                set >>= 4;
189 dceaf394 edgar_igl
                set &= 3;
190 dceaf394 edgar_igl
                idx &= 15;
191 dceaf394 edgar_igl
192 dceaf394 edgar_igl
                /* Update the mirror regs.  */
193 dceaf394 edgar_igl
                hi = env->tlbsets[srs - 1][set][idx].hi;
194 dceaf394 edgar_igl
                lo = env->tlbsets[srs - 1][set][idx].lo;
195 dceaf394 edgar_igl
                env->sregs[SFR_RW_MM_TLB_HI] = hi;
196 dceaf394 edgar_igl
                env->sregs[SFR_RW_MM_TLB_LO] = lo;
197 dceaf394 edgar_igl
        }
198 dceaf394 edgar_igl
#endif
199 dceaf394 edgar_igl
        env->regs[reg] = env->sregs[srs][sreg];
200 dceaf394 edgar_igl
}
201 dceaf394 edgar_igl
202 dceaf394 edgar_igl
static void cris_ccs_rshift(CPUState *env)
203 dceaf394 edgar_igl
{
204 dceaf394 edgar_igl
        uint32_t ccs;
205 dceaf394 edgar_igl
206 dceaf394 edgar_igl
        /* Apply the ccs shift.  */
207 dceaf394 edgar_igl
        ccs = env->pregs[PR_CCS];
208 dceaf394 edgar_igl
        ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
209 dceaf394 edgar_igl
        if (ccs & U_FLAG)
210 dceaf394 edgar_igl
        {
211 dceaf394 edgar_igl
                /* Enter user mode.  */
212 dceaf394 edgar_igl
                env->ksp = env->regs[R_SP];
213 dceaf394 edgar_igl
                env->regs[R_SP] = env->pregs[PR_USP];
214 dceaf394 edgar_igl
        }
215 dceaf394 edgar_igl
216 dceaf394 edgar_igl
        env->pregs[PR_CCS] = ccs;
217 dceaf394 edgar_igl
}
218 dceaf394 edgar_igl
219 b41f7df0 edgar_igl
void helper_rfe(void)
220 b41f7df0 edgar_igl
{
221 bf443337 edgar_igl
        int rflag = env->pregs[PR_CCS] & R_FLAG;
222 bf443337 edgar_igl
223 d12d51d5 aliguori
        D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n", 
224 b41f7df0 edgar_igl
                 env->pregs[PR_ERP], env->pregs[PR_PID],
225 b41f7df0 edgar_igl
                 env->pregs[PR_CCS],
226 d12d51d5 aliguori
                 env->btarget);
227 dceaf394 edgar_igl
228 dceaf394 edgar_igl
        cris_ccs_rshift(env);
229 dceaf394 edgar_igl
230 dceaf394 edgar_igl
        /* RFE sets the P_FLAG only if the R_FLAG is not set.  */
231 bf443337 edgar_igl
        if (!rflag)
232 dceaf394 edgar_igl
                env->pregs[PR_CCS] |= P_FLAG;
233 b41f7df0 edgar_igl
}
234 b41f7df0 edgar_igl
235 5bf8f1ab edgar_igl
void helper_rfn(void)
236 5bf8f1ab edgar_igl
{
237 5bf8f1ab edgar_igl
        int rflag = env->pregs[PR_CCS] & R_FLAG;
238 5bf8f1ab edgar_igl
239 d12d51d5 aliguori
        D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n", 
240 5bf8f1ab edgar_igl
                 env->pregs[PR_ERP], env->pregs[PR_PID],
241 5bf8f1ab edgar_igl
                 env->pregs[PR_CCS],
242 d12d51d5 aliguori
                 env->btarget);
243 5bf8f1ab edgar_igl
244 5bf8f1ab edgar_igl
        cris_ccs_rshift(env);
245 5bf8f1ab edgar_igl
246 5bf8f1ab edgar_igl
        /* Set the P_FLAG only if the R_FLAG is not set.  */
247 5bf8f1ab edgar_igl
        if (!rflag)
248 5bf8f1ab edgar_igl
                env->pregs[PR_CCS] |= P_FLAG;
249 5bf8f1ab edgar_igl
250 5bf8f1ab edgar_igl
    /* Always set the M flag.  */
251 5bf8f1ab edgar_igl
    env->pregs[PR_CCS] |= M_FLAG;
252 5bf8f1ab edgar_igl
}
253 5bf8f1ab edgar_igl
254 c38ac98d edgar_igl
uint32_t helper_lz(uint32_t t0)
255 c38ac98d edgar_igl
{
256 c38ac98d edgar_igl
        return clz32(t0);
257 c38ac98d edgar_igl
}
258 c38ac98d edgar_igl
259 abd5c94e edgar_igl
uint32_t helper_btst(uint32_t t0, uint32_t t1, uint32_t ccs)
260 abd5c94e edgar_igl
{
261 abd5c94e edgar_igl
        /* FIXME: clean this up.  */
262 abd5c94e edgar_igl
263 abd5c94e edgar_igl
        /* des ref:
264 abd5c94e edgar_igl
           The N flag is set according to the selected bit in the dest reg.
265 abd5c94e edgar_igl
           The Z flag is set if the selected bit and all bits to the right are
266 abd5c94e edgar_igl
           zero.
267 abd5c94e edgar_igl
           The X flag is cleared.
268 abd5c94e edgar_igl
           Other flags are left untouched.
269 abd5c94e edgar_igl
           The destination reg is not affected.*/
270 abd5c94e edgar_igl
        unsigned int fz, sbit, bset, mask, masked_t0;
271 abd5c94e edgar_igl
272 abd5c94e edgar_igl
        sbit = t1 & 31;
273 abd5c94e edgar_igl
        bset = !!(t0 & (1 << sbit));
274 abd5c94e edgar_igl
        mask = sbit == 31 ? -1 : (1 << (sbit + 1)) - 1;
275 abd5c94e edgar_igl
        masked_t0 = t0 & mask;
276 abd5c94e edgar_igl
        fz = !(masked_t0 | bset);
277 abd5c94e edgar_igl
278 abd5c94e edgar_igl
        /* Clear the X, N and Z flags.  */
279 abd5c94e edgar_igl
        ccs = ccs & ~(X_FLAG | N_FLAG | Z_FLAG);
280 abd5c94e edgar_igl
        /* Set the N and Z flags accordingly.  */
281 abd5c94e edgar_igl
        ccs |= (bset << 3) | (fz << 2);
282 abd5c94e edgar_igl
        return ccs;
283 abd5c94e edgar_igl
}
284 abd5c94e edgar_igl
285 6231868b edgar_igl
static inline uint32_t evaluate_flags_writeback(uint32_t flags, uint32_t ccs)
286 b41f7df0 edgar_igl
{
287 a8cf66bb edgar_igl
        unsigned int x, z, mask;
288 b41f7df0 edgar_igl
289 b41f7df0 edgar_igl
        /* Extended arithmetics, leave the z flag alone.  */
290 30abcfc7 edgar_igl
        x = env->cc_x;
291 a8cf66bb edgar_igl
        mask = env->cc_mask | X_FLAG;
292 a8cf66bb edgar_igl
        if (x) {
293 a8cf66bb edgar_igl
                z = flags & Z_FLAG;
294 a8cf66bb edgar_igl
                mask = mask & ~z;
295 a8cf66bb edgar_igl
        }
296 a8cf66bb edgar_igl
        flags &= mask;
297 b41f7df0 edgar_igl
298 b41f7df0 edgar_igl
        /* all insn clear the x-flag except setf or clrf.  */
299 6231868b edgar_igl
        ccs &= ~mask;
300 6231868b edgar_igl
        ccs |= flags;
301 6231868b edgar_igl
        return ccs;
302 b41f7df0 edgar_igl
}
303 b41f7df0 edgar_igl
304 6231868b edgar_igl
uint32_t helper_evaluate_flags_muls(uint32_t ccs, uint32_t res, uint32_t mof)
305 b41f7df0 edgar_igl
{
306 b41f7df0 edgar_igl
        uint32_t flags = 0;
307 dceaf394 edgar_igl
        int64_t tmp;
308 b41f7df0 edgar_igl
        int dneg;
309 b41f7df0 edgar_igl
310 b41f7df0 edgar_igl
        dneg = ((int32_t)res) < 0;
311 b41f7df0 edgar_igl
312 dceaf394 edgar_igl
        tmp = mof;
313 dceaf394 edgar_igl
        tmp <<= 32;
314 dceaf394 edgar_igl
        tmp |= res;
315 b41f7df0 edgar_igl
        if (tmp == 0)
316 b41f7df0 edgar_igl
                flags |= Z_FLAG;
317 b41f7df0 edgar_igl
        else if (tmp < 0)
318 b41f7df0 edgar_igl
                flags |= N_FLAG;
319 b41f7df0 edgar_igl
        if ((dneg && mof != -1)
320 b41f7df0 edgar_igl
            || (!dneg && mof != 0))
321 b41f7df0 edgar_igl
                flags |= V_FLAG;
322 6231868b edgar_igl
        return evaluate_flags_writeback(flags, ccs);
323 b41f7df0 edgar_igl
}
324 b41f7df0 edgar_igl
325 6231868b edgar_igl
uint32_t helper_evaluate_flags_mulu(uint32_t ccs, uint32_t res, uint32_t mof)
326 b41f7df0 edgar_igl
{
327 b41f7df0 edgar_igl
        uint32_t flags = 0;
328 dceaf394 edgar_igl
        uint64_t tmp;
329 b41f7df0 edgar_igl
330 dceaf394 edgar_igl
        tmp = mof;
331 dceaf394 edgar_igl
        tmp <<= 32;
332 dceaf394 edgar_igl
        tmp |= res;
333 b41f7df0 edgar_igl
        if (tmp == 0)
334 b41f7df0 edgar_igl
                flags |= Z_FLAG;
335 b41f7df0 edgar_igl
        else if (tmp >> 63)
336 b41f7df0 edgar_igl
                flags |= N_FLAG;
337 b41f7df0 edgar_igl
        if (mof)
338 b41f7df0 edgar_igl
                flags |= V_FLAG;
339 b41f7df0 edgar_igl
340 6231868b edgar_igl
        return evaluate_flags_writeback(flags, ccs);
341 b41f7df0 edgar_igl
}
342 b41f7df0 edgar_igl
343 6231868b edgar_igl
uint32_t helper_evaluate_flags_mcp(uint32_t ccs,
344 6231868b edgar_igl
                                   uint32_t src, uint32_t dst, uint32_t res)
345 b41f7df0 edgar_igl
{
346 b41f7df0 edgar_igl
        uint32_t flags = 0;
347 b41f7df0 edgar_igl
348 6231868b edgar_igl
        src = src & 0x80000000;
349 6231868b edgar_igl
        dst = dst & 0x80000000;
350 b41f7df0 edgar_igl
351 b41f7df0 edgar_igl
        if ((res & 0x80000000L) != 0L)
352 b41f7df0 edgar_igl
        {
353 b41f7df0 edgar_igl
                flags |= N_FLAG;
354 a8cf66bb edgar_igl
                if (!src && !dst)
355 b41f7df0 edgar_igl
                        flags |= V_FLAG;
356 a8cf66bb edgar_igl
                else if (src & dst)
357 b41f7df0 edgar_igl
                        flags |= R_FLAG;
358 b41f7df0 edgar_igl
        }
359 b41f7df0 edgar_igl
        else
360 b41f7df0 edgar_igl
        {
361 b41f7df0 edgar_igl
                if (res == 0L)
362 b41f7df0 edgar_igl
                        flags |= Z_FLAG;
363 a8cf66bb edgar_igl
                if (src & dst) 
364 b41f7df0 edgar_igl
                        flags |= V_FLAG;
365 a8cf66bb edgar_igl
                if (dst | src) 
366 b41f7df0 edgar_igl
                        flags |= R_FLAG;
367 b41f7df0 edgar_igl
        }
368 b41f7df0 edgar_igl
369 6231868b edgar_igl
        return evaluate_flags_writeback(flags, ccs);
370 b41f7df0 edgar_igl
}
371 b41f7df0 edgar_igl
372 6231868b edgar_igl
uint32_t helper_evaluate_flags_alu_4(uint32_t ccs,
373 6231868b edgar_igl
                                     uint32_t src, uint32_t dst, uint32_t res)
374 b41f7df0 edgar_igl
{
375 b41f7df0 edgar_igl
        uint32_t flags = 0;
376 b41f7df0 edgar_igl
377 6231868b edgar_igl
        src = src & 0x80000000;
378 6231868b edgar_igl
        dst = dst & 0x80000000;
379 30abcfc7 edgar_igl
380 a8cf66bb edgar_igl
        if ((res & 0x80000000L) != 0L)
381 30abcfc7 edgar_igl
        {
382 a8cf66bb edgar_igl
                flags |= N_FLAG;
383 a8cf66bb edgar_igl
                if (!src && !dst)
384 a8cf66bb edgar_igl
                        flags |= V_FLAG;
385 a8cf66bb edgar_igl
                else if (src & dst)
386 a8cf66bb edgar_igl
                        flags |= C_FLAG;
387 a8cf66bb edgar_igl
        }
388 a8cf66bb edgar_igl
        else
389 a8cf66bb edgar_igl
        {
390 a8cf66bb edgar_igl
                if (res == 0L)
391 a8cf66bb edgar_igl
                        flags |= Z_FLAG;
392 a8cf66bb edgar_igl
                if (src & dst) 
393 a8cf66bb edgar_igl
                        flags |= V_FLAG;
394 a8cf66bb edgar_igl
                if (dst | src) 
395 a8cf66bb edgar_igl
                        flags |= C_FLAG;
396 30abcfc7 edgar_igl
        }
397 30abcfc7 edgar_igl
398 6231868b edgar_igl
        return evaluate_flags_writeback(flags, ccs);
399 a8cf66bb edgar_igl
}
400 a8cf66bb edgar_igl
401 6231868b edgar_igl
uint32_t helper_evaluate_flags_sub_4(uint32_t ccs,
402 6231868b edgar_igl
                                     uint32_t src, uint32_t dst, uint32_t res)
403 a8cf66bb edgar_igl
{
404 a8cf66bb edgar_igl
        uint32_t flags = 0;
405 a8cf66bb edgar_igl
406 6231868b edgar_igl
        src = (~src) & 0x80000000;
407 6231868b edgar_igl
        dst = dst & 0x80000000;
408 b41f7df0 edgar_igl
409 b41f7df0 edgar_igl
        if ((res & 0x80000000L) != 0L)
410 b41f7df0 edgar_igl
        {
411 b41f7df0 edgar_igl
                flags |= N_FLAG;
412 a8cf66bb edgar_igl
                if (!src && !dst)
413 b41f7df0 edgar_igl
                        flags |= V_FLAG;
414 a8cf66bb edgar_igl
                else if (src & dst)
415 b41f7df0 edgar_igl
                        flags |= C_FLAG;
416 b41f7df0 edgar_igl
        }
417 b41f7df0 edgar_igl
        else
418 b41f7df0 edgar_igl
        {
419 b41f7df0 edgar_igl
                if (res == 0L)
420 b41f7df0 edgar_igl
                        flags |= Z_FLAG;
421 a8cf66bb edgar_igl
                if (src & dst) 
422 b41f7df0 edgar_igl
                        flags |= V_FLAG;
423 a8cf66bb edgar_igl
                if (dst | src) 
424 b41f7df0 edgar_igl
                        flags |= C_FLAG;
425 b41f7df0 edgar_igl
        }
426 b41f7df0 edgar_igl
427 a8cf66bb edgar_igl
        flags ^= C_FLAG;
428 6231868b edgar_igl
        return evaluate_flags_writeback(flags, ccs);
429 b41f7df0 edgar_igl
}
430 b41f7df0 edgar_igl
431 6231868b edgar_igl
uint32_t helper_evaluate_flags_move_4(uint32_t ccs, uint32_t res)
432 b41f7df0 edgar_igl
{
433 b41f7df0 edgar_igl
        uint32_t flags = 0;
434 b41f7df0 edgar_igl
435 b41f7df0 edgar_igl
        if ((int32_t)res < 0)
436 b41f7df0 edgar_igl
                flags |= N_FLAG;
437 b41f7df0 edgar_igl
        else if (res == 0L)
438 b41f7df0 edgar_igl
                flags |= Z_FLAG;
439 b41f7df0 edgar_igl
440 6231868b edgar_igl
        return evaluate_flags_writeback(flags, ccs);
441 b41f7df0 edgar_igl
}
442 6231868b edgar_igl
uint32_t helper_evaluate_flags_move_2(uint32_t ccs, uint32_t res)
443 b41f7df0 edgar_igl
{
444 b41f7df0 edgar_igl
        uint32_t flags = 0;
445 b41f7df0 edgar_igl
446 b41f7df0 edgar_igl
        if ((int16_t)res < 0L)
447 b41f7df0 edgar_igl
                flags |= N_FLAG;
448 b41f7df0 edgar_igl
        else if (res == 0)
449 b41f7df0 edgar_igl
                flags |= Z_FLAG;
450 b41f7df0 edgar_igl
451 6231868b edgar_igl
        return evaluate_flags_writeback(flags, ccs);
452 b41f7df0 edgar_igl
}
453 b41f7df0 edgar_igl
454 b41f7df0 edgar_igl
/* TODO: This is expensive. We could split things up and only evaluate part of
455 b41f7df0 edgar_igl
   CCR on a need to know basis. For now, we simply re-evaluate everything.  */
456 6231868b edgar_igl
void  helper_evaluate_flags(void)
457 b41f7df0 edgar_igl
{
458 6231868b edgar_igl
        uint32_t src, dst, res;
459 b41f7df0 edgar_igl
        uint32_t flags = 0;
460 b41f7df0 edgar_igl
461 b41f7df0 edgar_igl
        src = env->cc_src;
462 b41f7df0 edgar_igl
        dst = env->cc_dest;
463 b41f7df0 edgar_igl
        res = env->cc_result;
464 b41f7df0 edgar_igl
465 30abcfc7 edgar_igl
        if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
466 30abcfc7 edgar_igl
                src = ~src;
467 b41f7df0 edgar_igl
468 b41f7df0 edgar_igl
        /* Now, evaluate the flags. This stuff is based on
469 b41f7df0 edgar_igl
           Per Zander's CRISv10 simulator.  */
470 b41f7df0 edgar_igl
        switch (env->cc_size)
471 b41f7df0 edgar_igl
        {
472 b41f7df0 edgar_igl
                case 1:
473 b41f7df0 edgar_igl
                        if ((res & 0x80L) != 0L)
474 b41f7df0 edgar_igl
                        {
475 b41f7df0 edgar_igl
                                flags |= N_FLAG;
476 b41f7df0 edgar_igl
                                if (((src & 0x80L) == 0L)
477 b41f7df0 edgar_igl
                                    && ((dst & 0x80L) == 0L))
478 b41f7df0 edgar_igl
                                {
479 b41f7df0 edgar_igl
                                        flags |= V_FLAG;
480 b41f7df0 edgar_igl
                                }
481 b41f7df0 edgar_igl
                                else if (((src & 0x80L) != 0L)
482 b41f7df0 edgar_igl
                                         && ((dst & 0x80L) != 0L))
483 b41f7df0 edgar_igl
                                {
484 b41f7df0 edgar_igl
                                        flags |= C_FLAG;
485 b41f7df0 edgar_igl
                                }
486 b41f7df0 edgar_igl
                        }
487 b41f7df0 edgar_igl
                        else
488 b41f7df0 edgar_igl
                        {
489 b41f7df0 edgar_igl
                                if ((res & 0xFFL) == 0L)
490 b41f7df0 edgar_igl
                                {
491 b41f7df0 edgar_igl
                                        flags |= Z_FLAG;
492 b41f7df0 edgar_igl
                                }
493 b41f7df0 edgar_igl
                                if (((src & 0x80L) != 0L)
494 b41f7df0 edgar_igl
                                    && ((dst & 0x80L) != 0L))
495 b41f7df0 edgar_igl
                                {
496 b41f7df0 edgar_igl
                                        flags |= V_FLAG;
497 b41f7df0 edgar_igl
                                }
498 b41f7df0 edgar_igl
                                if ((dst & 0x80L) != 0L
499 b41f7df0 edgar_igl
                                    || (src & 0x80L) != 0L)
500 b41f7df0 edgar_igl
                                {
501 b41f7df0 edgar_igl
                                        flags |= C_FLAG;
502 b41f7df0 edgar_igl
                                }
503 b41f7df0 edgar_igl
                        }
504 b41f7df0 edgar_igl
                        break;
505 b41f7df0 edgar_igl
                case 2:
506 b41f7df0 edgar_igl
                        if ((res & 0x8000L) != 0L)
507 b41f7df0 edgar_igl
                        {
508 b41f7df0 edgar_igl
                                flags |= N_FLAG;
509 b41f7df0 edgar_igl
                                if (((src & 0x8000L) == 0L)
510 b41f7df0 edgar_igl
                                    && ((dst & 0x8000L) == 0L))
511 b41f7df0 edgar_igl
                                {
512 b41f7df0 edgar_igl
                                        flags |= V_FLAG;
513 b41f7df0 edgar_igl
                                }
514 b41f7df0 edgar_igl
                                else if (((src & 0x8000L) != 0L)
515 b41f7df0 edgar_igl
                                         && ((dst & 0x8000L) != 0L))
516 b41f7df0 edgar_igl
                                {
517 b41f7df0 edgar_igl
                                        flags |= C_FLAG;
518 b41f7df0 edgar_igl
                                }
519 b41f7df0 edgar_igl
                        }
520 b41f7df0 edgar_igl
                        else
521 b41f7df0 edgar_igl
                        {
522 b41f7df0 edgar_igl
                                if ((res & 0xFFFFL) == 0L)
523 b41f7df0 edgar_igl
                                {
524 b41f7df0 edgar_igl
                                        flags |= Z_FLAG;
525 b41f7df0 edgar_igl
                                }
526 b41f7df0 edgar_igl
                                if (((src & 0x8000L) != 0L)
527 b41f7df0 edgar_igl
                                    && ((dst & 0x8000L) != 0L))
528 b41f7df0 edgar_igl
                                {
529 b41f7df0 edgar_igl
                                        flags |= V_FLAG;
530 b41f7df0 edgar_igl
                                }
531 b41f7df0 edgar_igl
                                if ((dst & 0x8000L) != 0L
532 b41f7df0 edgar_igl
                                    || (src & 0x8000L) != 0L)
533 b41f7df0 edgar_igl
                                {
534 b41f7df0 edgar_igl
                                        flags |= C_FLAG;
535 b41f7df0 edgar_igl
                                }
536 b41f7df0 edgar_igl
                        }
537 b41f7df0 edgar_igl
                        break;
538 b41f7df0 edgar_igl
                case 4:
539 b41f7df0 edgar_igl
                        if ((res & 0x80000000L) != 0L)
540 b41f7df0 edgar_igl
                        {
541 b41f7df0 edgar_igl
                                flags |= N_FLAG;
542 b41f7df0 edgar_igl
                                if (((src & 0x80000000L) == 0L)
543 b41f7df0 edgar_igl
                                    && ((dst & 0x80000000L) == 0L))
544 b41f7df0 edgar_igl
                                {
545 b41f7df0 edgar_igl
                                        flags |= V_FLAG;
546 b41f7df0 edgar_igl
                                }
547 b41f7df0 edgar_igl
                                else if (((src & 0x80000000L) != 0L) &&
548 b41f7df0 edgar_igl
                                         ((dst & 0x80000000L) != 0L))
549 b41f7df0 edgar_igl
                                {
550 b41f7df0 edgar_igl
                                        flags |= C_FLAG;
551 b41f7df0 edgar_igl
                                }
552 b41f7df0 edgar_igl
                        }
553 b41f7df0 edgar_igl
                        else
554 b41f7df0 edgar_igl
                        {
555 b41f7df0 edgar_igl
                                if (res == 0L)
556 b41f7df0 edgar_igl
                                        flags |= Z_FLAG;
557 b41f7df0 edgar_igl
                                if (((src & 0x80000000L) != 0L)
558 b41f7df0 edgar_igl
                                    && ((dst & 0x80000000L) != 0L))
559 b41f7df0 edgar_igl
                                        flags |= V_FLAG;
560 b41f7df0 edgar_igl
                                if ((dst & 0x80000000L) != 0L
561 b41f7df0 edgar_igl
                                    || (src & 0x80000000L) != 0L)
562 b41f7df0 edgar_igl
                                        flags |= C_FLAG;
563 b41f7df0 edgar_igl
                        }
564 b41f7df0 edgar_igl
                        break;
565 b41f7df0 edgar_igl
                default:
566 b41f7df0 edgar_igl
                        break;
567 b41f7df0 edgar_igl
        }
568 b41f7df0 edgar_igl
569 6231868b edgar_igl
        if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
570 b41f7df0 edgar_igl
                flags ^= C_FLAG;
571 6231868b edgar_igl
572 6231868b edgar_igl
        env->pregs[PR_CCS] = evaluate_flags_writeback(flags, env->pregs[PR_CCS]);
573 b41f7df0 edgar_igl
}
574 30abcfc7 edgar_igl
575 30abcfc7 edgar_igl
void helper_top_evaluate_flags(void)
576 30abcfc7 edgar_igl
{
577 30abcfc7 edgar_igl
        switch (env->cc_op)
578 30abcfc7 edgar_igl
        {
579 30abcfc7 edgar_igl
                case CC_OP_MCP:
580 6231868b edgar_igl
                        env->pregs[PR_CCS] = helper_evaluate_flags_mcp(
581 6231868b edgar_igl
                                        env->pregs[PR_CCS], env->cc_src,
582 6231868b edgar_igl
                                        env->cc_dest, env->cc_result);
583 30abcfc7 edgar_igl
                        break;
584 30abcfc7 edgar_igl
                case CC_OP_MULS:
585 6231868b edgar_igl
                        env->pregs[PR_CCS] = helper_evaluate_flags_muls(
586 6231868b edgar_igl
                                        env->pregs[PR_CCS], env->cc_result,
587 6231868b edgar_igl
                                        env->pregs[PR_MOF]);
588 30abcfc7 edgar_igl
                        break;
589 30abcfc7 edgar_igl
                case CC_OP_MULU:
590 6231868b edgar_igl
                        env->pregs[PR_CCS] = helper_evaluate_flags_mulu(
591 6231868b edgar_igl
                                        env->pregs[PR_CCS], env->cc_result,
592 6231868b edgar_igl
                                        env->pregs[PR_MOF]);
593 30abcfc7 edgar_igl
                        break;
594 30abcfc7 edgar_igl
                case CC_OP_MOVE:
595 30abcfc7 edgar_igl
                case CC_OP_AND:
596 30abcfc7 edgar_igl
                case CC_OP_OR:
597 30abcfc7 edgar_igl
                case CC_OP_XOR:
598 30abcfc7 edgar_igl
                case CC_OP_ASR:
599 30abcfc7 edgar_igl
                case CC_OP_LSR:
600 30abcfc7 edgar_igl
                case CC_OP_LSL:
601 6231868b edgar_igl
                switch (env->cc_size)
602 6231868b edgar_igl
                {
603 6231868b edgar_igl
                        case 4:
604 6231868b edgar_igl
                                env->pregs[PR_CCS] =
605 6231868b edgar_igl
                                        helper_evaluate_flags_move_4(
606 6231868b edgar_igl
                                                        env->pregs[PR_CCS],
607 6231868b edgar_igl
                                                        env->cc_result);
608 6231868b edgar_igl
                                break;
609 6231868b edgar_igl
                        case 2:
610 6231868b edgar_igl
                                env->pregs[PR_CCS] =
611 6231868b edgar_igl
                                        helper_evaluate_flags_move_2(
612 6231868b edgar_igl
                                                        env->pregs[PR_CCS],
613 6231868b edgar_igl
                                                        env->cc_result);
614 6231868b edgar_igl
                                break;
615 6231868b edgar_igl
                        default:
616 6231868b edgar_igl
                                helper_evaluate_flags();
617 6231868b edgar_igl
                                break;
618 6231868b edgar_igl
                }
619 6231868b edgar_igl
                break;
620 30abcfc7 edgar_igl
                case CC_OP_FLAGS:
621 30abcfc7 edgar_igl
                        /* live.  */
622 30abcfc7 edgar_igl
                        break;
623 a8cf66bb edgar_igl
                case CC_OP_SUB:
624 a8cf66bb edgar_igl
                case CC_OP_CMP:
625 a8cf66bb edgar_igl
                        if (env->cc_size == 4)
626 6231868b edgar_igl
                                env->pregs[PR_CCS] =
627 6231868b edgar_igl
                                        helper_evaluate_flags_sub_4(
628 6231868b edgar_igl
                                                env->pregs[PR_CCS],
629 6231868b edgar_igl
                                                env->cc_src, env->cc_dest,
630 6231868b edgar_igl
                                                env->cc_result);
631 a8cf66bb edgar_igl
                        else
632 a8cf66bb edgar_igl
                                helper_evaluate_flags();
633 a8cf66bb edgar_igl
                        break;
634 30abcfc7 edgar_igl
                default:
635 30abcfc7 edgar_igl
                {
636 30abcfc7 edgar_igl
                        switch (env->cc_size)
637 30abcfc7 edgar_igl
                        {
638 6231868b edgar_igl
                        case 4:
639 6231868b edgar_igl
                                env->pregs[PR_CCS] =
640 6231868b edgar_igl
                                        helper_evaluate_flags_alu_4(
641 6231868b edgar_igl
                                                env->pregs[PR_CCS],
642 6231868b edgar_igl
                                                env->cc_src, env->cc_dest,
643 6231868b edgar_igl
                                                env->cc_result);
644 6231868b edgar_igl
                                break;
645 6231868b edgar_igl
                        default:
646 6231868b edgar_igl
                                helper_evaluate_flags();
647 6231868b edgar_igl
                                break;
648 30abcfc7 edgar_igl
                        }
649 30abcfc7 edgar_igl
                }
650 30abcfc7 edgar_igl
                break;
651 30abcfc7 edgar_igl
        }
652 30abcfc7 edgar_igl
}