Statistics
| Branch: | Revision:

root / target-cris / op_helper.c @ 94a943ef

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