Statistics
| Branch: | Revision:

root / target-sh4 / translate.c @ 73e5716c

History | View | Annotate | Download (61.9 kB)

1 fdf9b3e8 bellard
/*
2 fdf9b3e8 bellard
 *  SH4 translation
3 5fafdf24 ths
 *
4 fdf9b3e8 bellard
 *  Copyright (c) 2005 Samuel Tardieu
5 fdf9b3e8 bellard
 *
6 fdf9b3e8 bellard
 * This library is free software; you can redistribute it and/or
7 fdf9b3e8 bellard
 * modify it under the terms of the GNU Lesser General Public
8 fdf9b3e8 bellard
 * License as published by the Free Software Foundation; either
9 fdf9b3e8 bellard
 * version 2 of the License, or (at your option) any later version.
10 fdf9b3e8 bellard
 *
11 fdf9b3e8 bellard
 * This library is distributed in the hope that it will be useful,
12 fdf9b3e8 bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 fdf9b3e8 bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 fdf9b3e8 bellard
 * Lesser General Public License for more details.
15 fdf9b3e8 bellard
 *
16 fdf9b3e8 bellard
 * You should have received a copy of the GNU Lesser General Public
17 8167ee88 Blue Swirl
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 fdf9b3e8 bellard
 */
19 fdf9b3e8 bellard
20 fdf9b3e8 bellard
#define DEBUG_DISAS
21 fdf9b3e8 bellard
#define SH4_DEBUG_DISAS
22 fdf9b3e8 bellard
//#define SH4_SINGLE_STEP
23 fdf9b3e8 bellard
24 fdf9b3e8 bellard
#include "cpu.h"
25 fdf9b3e8 bellard
#include "disas.h"
26 57fec1fe bellard
#include "tcg-op.h"
27 fdf9b3e8 bellard
28 a7812ae4 pbrook
#include "helper.h"
29 a7812ae4 pbrook
#define GEN_HELPER 1
30 a7812ae4 pbrook
#include "helper.h"
31 a7812ae4 pbrook
32 fdf9b3e8 bellard
typedef struct DisasContext {
33 fdf9b3e8 bellard
    struct TranslationBlock *tb;
34 fdf9b3e8 bellard
    target_ulong pc;
35 fdf9b3e8 bellard
    uint32_t sr;
36 eda9b09b bellard
    uint32_t fpscr;
37 fdf9b3e8 bellard
    uint16_t opcode;
38 fdf9b3e8 bellard
    uint32_t flags;
39 823029f9 ths
    int bstate;
40 fdf9b3e8 bellard
    int memidx;
41 fdf9b3e8 bellard
    uint32_t delayed_pc;
42 fdf9b3e8 bellard
    int singlestep_enabled;
43 71968fa6 aurel32
    uint32_t features;
44 852d481f edgar_igl
    int has_movcal;
45 fdf9b3e8 bellard
} DisasContext;
46 fdf9b3e8 bellard
47 fe25591e aurel32
#if defined(CONFIG_USER_ONLY)
48 fe25591e aurel32
#define IS_USER(ctx) 1
49 fe25591e aurel32
#else
50 fe25591e aurel32
#define IS_USER(ctx) (!(ctx->sr & SR_MD))
51 fe25591e aurel32
#endif
52 fe25591e aurel32
53 823029f9 ths
enum {
54 823029f9 ths
    BS_NONE     = 0, /* We go out of the TB without reaching a branch or an
55 823029f9 ths
                      * exception condition
56 823029f9 ths
                      */
57 823029f9 ths
    BS_STOP     = 1, /* We want to stop translation for any reason */
58 823029f9 ths
    BS_BRANCH   = 2, /* We reached a branch condition     */
59 823029f9 ths
    BS_EXCP     = 3, /* We reached an exception condition */
60 823029f9 ths
};
61 823029f9 ths
62 1e8864f7 aurel32
/* global register indexes */
63 a7812ae4 pbrook
static TCGv_ptr cpu_env;
64 1e8864f7 aurel32
static TCGv cpu_gregs[24];
65 3a8a44c4 aurel32
static TCGv cpu_pc, cpu_sr, cpu_ssr, cpu_spc, cpu_gbr;
66 3a8a44c4 aurel32
static TCGv cpu_vbr, cpu_sgr, cpu_dbr, cpu_mach, cpu_macl;
67 66c7c806 aurel32
static TCGv cpu_pr, cpu_fpscr, cpu_fpul, cpu_ldst;
68 66ba317c aurel32
static TCGv cpu_fregs[32];
69 1000822b aurel32
70 1000822b aurel32
/* internal register indexes */
71 1000822b aurel32
static TCGv cpu_flags, cpu_delayed_pc;
72 1e8864f7 aurel32
73 1a7ff922 Paolo Bonzini
static uint32_t gen_opc_hflags[OPC_BUF_SIZE];
74 1a7ff922 Paolo Bonzini
75 2e70f6ef pbrook
#include "gen-icount.h"
76 2e70f6ef pbrook
77 a5f1b965 blueswir1
static void sh4_translate_init(void)
78 2e70f6ef pbrook
{
79 1e8864f7 aurel32
    int i;
80 2e70f6ef pbrook
    static int done_init = 0;
81 559dd74d aurel32
    static const char * const gregnames[24] = {
82 1e8864f7 aurel32
        "R0_BANK0", "R1_BANK0", "R2_BANK0", "R3_BANK0",
83 1e8864f7 aurel32
        "R4_BANK0", "R5_BANK0", "R6_BANK0", "R7_BANK0",
84 1e8864f7 aurel32
        "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15",
85 1e8864f7 aurel32
        "R0_BANK1", "R1_BANK1", "R2_BANK1", "R3_BANK1",
86 1e8864f7 aurel32
        "R4_BANK1", "R5_BANK1", "R6_BANK1", "R7_BANK1"
87 1e8864f7 aurel32
    };
88 66ba317c aurel32
    static const char * const fregnames[32] = {
89 66ba317c aurel32
         "FPR0_BANK0",  "FPR1_BANK0",  "FPR2_BANK0",  "FPR3_BANK0",
90 66ba317c aurel32
         "FPR4_BANK0",  "FPR5_BANK0",  "FPR6_BANK0",  "FPR7_BANK0",
91 66ba317c aurel32
         "FPR8_BANK0",  "FPR9_BANK0", "FPR10_BANK0", "FPR11_BANK0",
92 66ba317c aurel32
        "FPR12_BANK0", "FPR13_BANK0", "FPR14_BANK0", "FPR15_BANK0",
93 66ba317c aurel32
         "FPR0_BANK1",  "FPR1_BANK1",  "FPR2_BANK1",  "FPR3_BANK1",
94 66ba317c aurel32
         "FPR4_BANK1",  "FPR5_BANK1",  "FPR6_BANK1",  "FPR7_BANK1",
95 66ba317c aurel32
         "FPR8_BANK1",  "FPR9_BANK1", "FPR10_BANK1", "FPR11_BANK1",
96 66ba317c aurel32
        "FPR12_BANK1", "FPR13_BANK1", "FPR14_BANK1", "FPR15_BANK1",
97 66ba317c aurel32
    };
98 1e8864f7 aurel32
99 2e70f6ef pbrook
    if (done_init)
100 2e70f6ef pbrook
        return;
101 1e8864f7 aurel32
102 a7812ae4 pbrook
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
103 1e8864f7 aurel32
104 1e8864f7 aurel32
    for (i = 0; i < 24; i++)
105 a7812ae4 pbrook
        cpu_gregs[i] = tcg_global_mem_new_i32(TCG_AREG0,
106 73e5716c Andreas Färber
                                              offsetof(CPUSH4State, gregs[i]),
107 66ba317c aurel32
                                              gregnames[i]);
108 988d7eaa aurel32
109 a7812ae4 pbrook
    cpu_pc = tcg_global_mem_new_i32(TCG_AREG0,
110 73e5716c Andreas Färber
                                    offsetof(CPUSH4State, pc), "PC");
111 a7812ae4 pbrook
    cpu_sr = tcg_global_mem_new_i32(TCG_AREG0,
112 73e5716c Andreas Färber
                                    offsetof(CPUSH4State, sr), "SR");
113 a7812ae4 pbrook
    cpu_ssr = tcg_global_mem_new_i32(TCG_AREG0,
114 73e5716c Andreas Färber
                                     offsetof(CPUSH4State, ssr), "SSR");
115 a7812ae4 pbrook
    cpu_spc = tcg_global_mem_new_i32(TCG_AREG0,
116 73e5716c Andreas Färber
                                     offsetof(CPUSH4State, spc), "SPC");
117 a7812ae4 pbrook
    cpu_gbr = tcg_global_mem_new_i32(TCG_AREG0,
118 73e5716c Andreas Färber
                                     offsetof(CPUSH4State, gbr), "GBR");
119 a7812ae4 pbrook
    cpu_vbr = tcg_global_mem_new_i32(TCG_AREG0,
120 73e5716c Andreas Färber
                                     offsetof(CPUSH4State, vbr), "VBR");
121 a7812ae4 pbrook
    cpu_sgr = tcg_global_mem_new_i32(TCG_AREG0,
122 73e5716c Andreas Färber
                                     offsetof(CPUSH4State, sgr), "SGR");
123 a7812ae4 pbrook
    cpu_dbr = tcg_global_mem_new_i32(TCG_AREG0,
124 73e5716c Andreas Färber
                                     offsetof(CPUSH4State, dbr), "DBR");
125 a7812ae4 pbrook
    cpu_mach = tcg_global_mem_new_i32(TCG_AREG0,
126 73e5716c Andreas Färber
                                      offsetof(CPUSH4State, mach), "MACH");
127 a7812ae4 pbrook
    cpu_macl = tcg_global_mem_new_i32(TCG_AREG0,
128 73e5716c Andreas Färber
                                      offsetof(CPUSH4State, macl), "MACL");
129 a7812ae4 pbrook
    cpu_pr = tcg_global_mem_new_i32(TCG_AREG0,
130 73e5716c Andreas Färber
                                    offsetof(CPUSH4State, pr), "PR");
131 a7812ae4 pbrook
    cpu_fpscr = tcg_global_mem_new_i32(TCG_AREG0,
132 73e5716c Andreas Färber
                                       offsetof(CPUSH4State, fpscr), "FPSCR");
133 a7812ae4 pbrook
    cpu_fpul = tcg_global_mem_new_i32(TCG_AREG0,
134 73e5716c Andreas Färber
                                      offsetof(CPUSH4State, fpul), "FPUL");
135 a7812ae4 pbrook
136 a7812ae4 pbrook
    cpu_flags = tcg_global_mem_new_i32(TCG_AREG0,
137 73e5716c Andreas Färber
                                       offsetof(CPUSH4State, flags), "_flags_");
138 a7812ae4 pbrook
    cpu_delayed_pc = tcg_global_mem_new_i32(TCG_AREG0,
139 73e5716c Andreas Färber
                                            offsetof(CPUSH4State, delayed_pc),
140 a7812ae4 pbrook
                                            "_delayed_pc_");
141 66c7c806 aurel32
    cpu_ldst = tcg_global_mem_new_i32(TCG_AREG0,
142 73e5716c Andreas Färber
                                      offsetof(CPUSH4State, ldst), "_ldst_");
143 1000822b aurel32
144 66ba317c aurel32
    for (i = 0; i < 32; i++)
145 66ba317c aurel32
        cpu_fregs[i] = tcg_global_mem_new_i32(TCG_AREG0,
146 73e5716c Andreas Färber
                                              offsetof(CPUSH4State, fregs[i]),
147 66ba317c aurel32
                                              fregnames[i]);
148 66ba317c aurel32
149 988d7eaa aurel32
    /* register helpers */
150 a7812ae4 pbrook
#define GEN_HELPER 2
151 988d7eaa aurel32
#include "helper.h"
152 988d7eaa aurel32
153 2e70f6ef pbrook
    done_init = 1;
154 2e70f6ef pbrook
}
155 2e70f6ef pbrook
156 73e5716c Andreas Färber
void cpu_dump_state(CPUSH4State * env, FILE * f,
157 fdf9b3e8 bellard
                    int (*cpu_fprintf) (FILE * f, const char *fmt, ...),
158 fdf9b3e8 bellard
                    int flags)
159 fdf9b3e8 bellard
{
160 fdf9b3e8 bellard
    int i;
161 eda9b09b bellard
    cpu_fprintf(f, "pc=0x%08x sr=0x%08x pr=0x%08x fpscr=0x%08x\n",
162 eda9b09b bellard
                env->pc, env->sr, env->pr, env->fpscr);
163 274a9e70 aurel32
    cpu_fprintf(f, "spc=0x%08x ssr=0x%08x gbr=0x%08x vbr=0x%08x\n",
164 274a9e70 aurel32
                env->spc, env->ssr, env->gbr, env->vbr);
165 274a9e70 aurel32
    cpu_fprintf(f, "sgr=0x%08x dbr=0x%08x delayed_pc=0x%08x fpul=0x%08x\n",
166 274a9e70 aurel32
                env->sgr, env->dbr, env->delayed_pc, env->fpul);
167 fdf9b3e8 bellard
    for (i = 0; i < 24; i += 4) {
168 fdf9b3e8 bellard
        cpu_fprintf(f, "r%d=0x%08x r%d=0x%08x r%d=0x%08x r%d=0x%08x\n",
169 fdf9b3e8 bellard
                    i, env->gregs[i], i + 1, env->gregs[i + 1],
170 fdf9b3e8 bellard
                    i + 2, env->gregs[i + 2], i + 3, env->gregs[i + 3]);
171 fdf9b3e8 bellard
    }
172 fdf9b3e8 bellard
    if (env->flags & DELAY_SLOT) {
173 fdf9b3e8 bellard
        cpu_fprintf(f, "in delay slot (delayed_pc=0x%08x)\n",
174 fdf9b3e8 bellard
                    env->delayed_pc);
175 fdf9b3e8 bellard
    } else if (env->flags & DELAY_SLOT_CONDITIONAL) {
176 fdf9b3e8 bellard
        cpu_fprintf(f, "in conditional delay slot (delayed_pc=0x%08x)\n",
177 fdf9b3e8 bellard
                    env->delayed_pc);
178 fdf9b3e8 bellard
    }
179 fdf9b3e8 bellard
}
180 fdf9b3e8 bellard
181 1bba0dc9 Andreas Färber
void cpu_state_reset(CPUSH4State *env)
182 fdf9b3e8 bellard
{
183 eca1bdf4 aliguori
    if (qemu_loglevel_mask(CPU_LOG_RESET)) {
184 eca1bdf4 aliguori
        qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
185 eca1bdf4 aliguori
        log_cpu_state(env, 0);
186 eca1bdf4 aliguori
    }
187 eca1bdf4 aliguori
188 4f6493ff Aurelien Jarno
    memset(env, 0, offsetof(CPUSH4State, breakpoints));
189 4f6493ff Aurelien Jarno
    tlb_flush(env, 1);
190 4f6493ff Aurelien Jarno
191 fdf9b3e8 bellard
    env->pc = 0xA0000000;
192 ea6cf6be ths
#if defined(CONFIG_USER_ONLY)
193 ea6cf6be ths
    env->fpscr = FPSCR_PR; /* value for userspace according to the kernel */
194 b0b3de89 bellard
    set_float_rounding_mode(float_round_nearest_even, &env->fp_status); /* ?! */
195 ea6cf6be ths
#else
196 4f6493ff Aurelien Jarno
    env->sr = SR_MD | SR_RB | SR_BL | SR_I3 | SR_I2 | SR_I1 | SR_I0;
197 26ac1ea5 Aurelien Jarno
    env->fpscr = FPSCR_DN | FPSCR_RM_ZERO; /* CPU reset value according to SH4 manual */
198 b0b3de89 bellard
    set_float_rounding_mode(float_round_to_zero, &env->fp_status);
199 a0d4ac33 Aurelien Jarno
    set_flush_to_zero(1, &env->fp_status);
200 ea6cf6be ths
#endif
201 26ac1ea5 Aurelien Jarno
    set_default_nan_mode(1, &env->fp_status);
202 fdf9b3e8 bellard
}
203 fdf9b3e8 bellard
204 0fd3ca30 aurel32
typedef struct {
205 b55266b5 blueswir1
    const char *name;
206 0fd3ca30 aurel32
    int id;
207 0fd3ca30 aurel32
    uint32_t pvr;
208 0fd3ca30 aurel32
    uint32_t prr;
209 0fd3ca30 aurel32
    uint32_t cvr;
210 71968fa6 aurel32
    uint32_t features;
211 0fd3ca30 aurel32
} sh4_def_t;
212 0fd3ca30 aurel32
213 0fd3ca30 aurel32
static sh4_def_t sh4_defs[] = {
214 0fd3ca30 aurel32
    {
215 0fd3ca30 aurel32
        .name = "SH7750R",
216 0fd3ca30 aurel32
        .id = SH_CPU_SH7750R,
217 0fd3ca30 aurel32
        .pvr = 0x00050000,
218 0fd3ca30 aurel32
        .prr = 0x00000100,
219 0fd3ca30 aurel32
        .cvr = 0x00110000,
220 c2432a42 aurel32
        .features = SH_FEATURE_BCR3_AND_BCR4,
221 0fd3ca30 aurel32
    }, {
222 0fd3ca30 aurel32
        .name = "SH7751R",
223 0fd3ca30 aurel32
        .id = SH_CPU_SH7751R,
224 0fd3ca30 aurel32
        .pvr = 0x04050005,
225 0fd3ca30 aurel32
        .prr = 0x00000113,
226 0fd3ca30 aurel32
        .cvr = 0x00110000,        /* Neutered caches, should be 0x20480000 */
227 c2432a42 aurel32
        .features = SH_FEATURE_BCR3_AND_BCR4,
228 a9c43f8e aurel32
    }, {
229 a9c43f8e aurel32
        .name = "SH7785",
230 a9c43f8e aurel32
        .id = SH_CPU_SH7785,
231 a9c43f8e aurel32
        .pvr = 0x10300700,
232 a9c43f8e aurel32
        .prr = 0x00000200,
233 a9c43f8e aurel32
        .cvr = 0x71440211,
234 71968fa6 aurel32
        .features = SH_FEATURE_SH4A,
235 a9c43f8e aurel32
     },
236 0fd3ca30 aurel32
};
237 0fd3ca30 aurel32
238 b55266b5 blueswir1
static const sh4_def_t *cpu_sh4_find_by_name(const char *name)
239 0fd3ca30 aurel32
{
240 0fd3ca30 aurel32
    int i;
241 0fd3ca30 aurel32
242 0fd3ca30 aurel32
    if (strcasecmp(name, "any") == 0)
243 0fd3ca30 aurel32
        return &sh4_defs[0];
244 0fd3ca30 aurel32
245 b1503cda malc
    for (i = 0; i < ARRAY_SIZE(sh4_defs); i++)
246 0fd3ca30 aurel32
        if (strcasecmp(name, sh4_defs[i].name) == 0)
247 0fd3ca30 aurel32
            return &sh4_defs[i];
248 0fd3ca30 aurel32
249 0fd3ca30 aurel32
    return NULL;
250 0fd3ca30 aurel32
}
251 0fd3ca30 aurel32
252 9a78eead Stefan Weil
void sh4_cpu_list(FILE *f, fprintf_function cpu_fprintf)
253 0fd3ca30 aurel32
{
254 0fd3ca30 aurel32
    int i;
255 0fd3ca30 aurel32
256 b1503cda malc
    for (i = 0; i < ARRAY_SIZE(sh4_defs); i++)
257 0fd3ca30 aurel32
        (*cpu_fprintf)(f, "%s\n", sh4_defs[i].name);
258 0fd3ca30 aurel32
}
259 0fd3ca30 aurel32
260 4f6493ff Aurelien Jarno
static void cpu_register(CPUSH4State *env, const sh4_def_t *def)
261 0fd3ca30 aurel32
{
262 0fd3ca30 aurel32
    env->pvr = def->pvr;
263 0fd3ca30 aurel32
    env->prr = def->prr;
264 0fd3ca30 aurel32
    env->cvr = def->cvr;
265 0fd3ca30 aurel32
    env->id = def->id;
266 0fd3ca30 aurel32
}
267 0fd3ca30 aurel32
268 aaed909a bellard
CPUSH4State *cpu_sh4_init(const char *cpu_model)
269 fdf9b3e8 bellard
{
270 fdf9b3e8 bellard
    CPUSH4State *env;
271 0fd3ca30 aurel32
    const sh4_def_t *def;
272 fdf9b3e8 bellard
273 0fd3ca30 aurel32
    def = cpu_sh4_find_by_name(cpu_model);
274 0fd3ca30 aurel32
    if (!def)
275 0fd3ca30 aurel32
        return NULL;
276 7267c094 Anthony Liguori
    env = g_malloc0(sizeof(CPUSH4State));
277 71968fa6 aurel32
    env->features = def->features;
278 fdf9b3e8 bellard
    cpu_exec_init(env);
279 852d481f edgar_igl
    env->movcal_backup_tail = &(env->movcal_backup);
280 2e70f6ef pbrook
    sh4_translate_init();
281 7478757e aurel32
    env->cpu_model_str = cpu_model;
282 1bba0dc9 Andreas Färber
    cpu_state_reset(env);
283 4f6493ff Aurelien Jarno
    cpu_register(env, def);
284 0bf46a40 aliguori
    qemu_init_vcpu(env);
285 fdf9b3e8 bellard
    return env;
286 fdf9b3e8 bellard
}
287 fdf9b3e8 bellard
288 fdf9b3e8 bellard
static void gen_goto_tb(DisasContext * ctx, int n, target_ulong dest)
289 fdf9b3e8 bellard
{
290 fdf9b3e8 bellard
    TranslationBlock *tb;
291 fdf9b3e8 bellard
    tb = ctx->tb;
292 fdf9b3e8 bellard
293 fdf9b3e8 bellard
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
294 fdf9b3e8 bellard
        !ctx->singlestep_enabled) {
295 fdf9b3e8 bellard
        /* Use a direct jump if in same page and singlestep not enabled */
296 57fec1fe bellard
        tcg_gen_goto_tb(n);
297 3a8a44c4 aurel32
        tcg_gen_movi_i32(cpu_pc, dest);
298 4b4a72e5 Stefan Weil
        tcg_gen_exit_tb((tcg_target_long)tb + n);
299 fdf9b3e8 bellard
    } else {
300 3a8a44c4 aurel32
        tcg_gen_movi_i32(cpu_pc, dest);
301 57fec1fe bellard
        if (ctx->singlestep_enabled)
302 a7812ae4 pbrook
            gen_helper_debug();
303 57fec1fe bellard
        tcg_gen_exit_tb(0);
304 fdf9b3e8 bellard
    }
305 fdf9b3e8 bellard
}
306 fdf9b3e8 bellard
307 fdf9b3e8 bellard
static void gen_jump(DisasContext * ctx)
308 fdf9b3e8 bellard
{
309 fdf9b3e8 bellard
    if (ctx->delayed_pc == (uint32_t) - 1) {
310 fdf9b3e8 bellard
        /* Target is not statically known, it comes necessarily from a
311 fdf9b3e8 bellard
           delayed jump as immediate jump are conditinal jumps */
312 1000822b aurel32
        tcg_gen_mov_i32(cpu_pc, cpu_delayed_pc);
313 fdf9b3e8 bellard
        if (ctx->singlestep_enabled)
314 a7812ae4 pbrook
            gen_helper_debug();
315 57fec1fe bellard
        tcg_gen_exit_tb(0);
316 fdf9b3e8 bellard
    } else {
317 fdf9b3e8 bellard
        gen_goto_tb(ctx, 0, ctx->delayed_pc);
318 fdf9b3e8 bellard
    }
319 fdf9b3e8 bellard
}
320 fdf9b3e8 bellard
321 1000822b aurel32
static inline void gen_branch_slot(uint32_t delayed_pc, int t)
322 1000822b aurel32
{
323 c55497ec aurel32
    TCGv sr;
324 1000822b aurel32
    int label = gen_new_label();
325 1000822b aurel32
    tcg_gen_movi_i32(cpu_delayed_pc, delayed_pc);
326 a7812ae4 pbrook
    sr = tcg_temp_new();
327 c55497ec aurel32
    tcg_gen_andi_i32(sr, cpu_sr, SR_T);
328 6f396c8f Aurelien Jarno
    tcg_gen_brcondi_i32(t ? TCG_COND_EQ:TCG_COND_NE, sr, 0, label);
329 1000822b aurel32
    tcg_gen_ori_i32(cpu_flags, cpu_flags, DELAY_SLOT_TRUE);
330 1000822b aurel32
    gen_set_label(label);
331 1000822b aurel32
}
332 1000822b aurel32
333 fdf9b3e8 bellard
/* Immediate conditional jump (bt or bf) */
334 fdf9b3e8 bellard
static void gen_conditional_jump(DisasContext * ctx,
335 fdf9b3e8 bellard
                                 target_ulong ift, target_ulong ifnott)
336 fdf9b3e8 bellard
{
337 fdf9b3e8 bellard
    int l1;
338 c55497ec aurel32
    TCGv sr;
339 fdf9b3e8 bellard
340 fdf9b3e8 bellard
    l1 = gen_new_label();
341 a7812ae4 pbrook
    sr = tcg_temp_new();
342 c55497ec aurel32
    tcg_gen_andi_i32(sr, cpu_sr, SR_T);
343 6f396c8f Aurelien Jarno
    tcg_gen_brcondi_i32(TCG_COND_NE, sr, 0, l1);
344 fdf9b3e8 bellard
    gen_goto_tb(ctx, 0, ifnott);
345 fdf9b3e8 bellard
    gen_set_label(l1);
346 fdf9b3e8 bellard
    gen_goto_tb(ctx, 1, ift);
347 fdf9b3e8 bellard
}
348 fdf9b3e8 bellard
349 fdf9b3e8 bellard
/* Delayed conditional jump (bt or bf) */
350 fdf9b3e8 bellard
static void gen_delayed_conditional_jump(DisasContext * ctx)
351 fdf9b3e8 bellard
{
352 fdf9b3e8 bellard
    int l1;
353 c55497ec aurel32
    TCGv ds;
354 fdf9b3e8 bellard
355 fdf9b3e8 bellard
    l1 = gen_new_label();
356 a7812ae4 pbrook
    ds = tcg_temp_new();
357 c55497ec aurel32
    tcg_gen_andi_i32(ds, cpu_flags, DELAY_SLOT_TRUE);
358 6f396c8f Aurelien Jarno
    tcg_gen_brcondi_i32(TCG_COND_NE, ds, 0, l1);
359 823029f9 ths
    gen_goto_tb(ctx, 1, ctx->pc + 2);
360 fdf9b3e8 bellard
    gen_set_label(l1);
361 1000822b aurel32
    tcg_gen_andi_i32(cpu_flags, cpu_flags, ~DELAY_SLOT_TRUE);
362 9c2a9ea1 pbrook
    gen_jump(ctx);
363 fdf9b3e8 bellard
}
364 fdf9b3e8 bellard
365 a4625612 aurel32
static inline void gen_set_t(void)
366 a4625612 aurel32
{
367 a4625612 aurel32
    tcg_gen_ori_i32(cpu_sr, cpu_sr, SR_T);
368 a4625612 aurel32
}
369 a4625612 aurel32
370 a4625612 aurel32
static inline void gen_clr_t(void)
371 a4625612 aurel32
{
372 a4625612 aurel32
    tcg_gen_andi_i32(cpu_sr, cpu_sr, ~SR_T);
373 a4625612 aurel32
}
374 a4625612 aurel32
375 a4625612 aurel32
static inline void gen_cmp(int cond, TCGv t0, TCGv t1)
376 a4625612 aurel32
{
377 c5c19137 Aurelien Jarno
    TCGv t;
378 c5c19137 Aurelien Jarno
379 c5c19137 Aurelien Jarno
    t = tcg_temp_new();
380 c5c19137 Aurelien Jarno
    tcg_gen_setcond_i32(cond, t, t1, t0);
381 c5c19137 Aurelien Jarno
    tcg_gen_andi_i32(cpu_sr, cpu_sr, ~SR_T);
382 c5c19137 Aurelien Jarno
    tcg_gen_or_i32(cpu_sr, cpu_sr, t);
383 c5c19137 Aurelien Jarno
384 c5c19137 Aurelien Jarno
    tcg_temp_free(t);
385 a4625612 aurel32
}
386 a4625612 aurel32
387 a4625612 aurel32
static inline void gen_cmp_imm(int cond, TCGv t0, int32_t imm)
388 a4625612 aurel32
{
389 c5c19137 Aurelien Jarno
    TCGv t;
390 c5c19137 Aurelien Jarno
391 c5c19137 Aurelien Jarno
    t = tcg_temp_new();
392 c5c19137 Aurelien Jarno
    tcg_gen_setcondi_i32(cond, t, t0, imm);
393 c5c19137 Aurelien Jarno
    tcg_gen_andi_i32(cpu_sr, cpu_sr, ~SR_T);
394 c5c19137 Aurelien Jarno
    tcg_gen_or_i32(cpu_sr, cpu_sr, t);
395 c5c19137 Aurelien Jarno
396 c5c19137 Aurelien Jarno
    tcg_temp_free(t);
397 a4625612 aurel32
}
398 a4625612 aurel32
399 1000822b aurel32
static inline void gen_store_flags(uint32_t flags)
400 1000822b aurel32
{
401 1000822b aurel32
    tcg_gen_andi_i32(cpu_flags, cpu_flags, DELAY_SLOT_TRUE);
402 1000822b aurel32
    tcg_gen_ori_i32(cpu_flags, cpu_flags, flags);
403 1000822b aurel32
}
404 1000822b aurel32
405 69d6275b aurel32
static inline void gen_copy_bit_i32(TCGv t0, int p0, TCGv t1, int p1)
406 69d6275b aurel32
{
407 a7812ae4 pbrook
    TCGv tmp = tcg_temp_new();
408 69d6275b aurel32
409 69d6275b aurel32
    p0 &= 0x1f;
410 69d6275b aurel32
    p1 &= 0x1f;
411 69d6275b aurel32
412 69d6275b aurel32
    tcg_gen_andi_i32(tmp, t1, (1 << p1));
413 69d6275b aurel32
    tcg_gen_andi_i32(t0, t0, ~(1 << p0));
414 69d6275b aurel32
    if (p0 < p1)
415 69d6275b aurel32
        tcg_gen_shri_i32(tmp, tmp, p1 - p0);
416 69d6275b aurel32
    else if (p0 > p1)
417 69d6275b aurel32
        tcg_gen_shli_i32(tmp, tmp, p0 - p1);
418 69d6275b aurel32
    tcg_gen_or_i32(t0, t0, tmp);
419 69d6275b aurel32
420 69d6275b aurel32
    tcg_temp_free(tmp);
421 69d6275b aurel32
}
422 69d6275b aurel32
423 a7812ae4 pbrook
static inline void gen_load_fpr64(TCGv_i64 t, int reg)
424 cc4ba6a9 aurel32
{
425 66ba317c aurel32
    tcg_gen_concat_i32_i64(t, cpu_fregs[reg + 1], cpu_fregs[reg]);
426 cc4ba6a9 aurel32
}
427 cc4ba6a9 aurel32
428 a7812ae4 pbrook
static inline void gen_store_fpr64 (TCGv_i64 t, int reg)
429 cc4ba6a9 aurel32
{
430 a7812ae4 pbrook
    TCGv_i32 tmp = tcg_temp_new_i32();
431 cc4ba6a9 aurel32
    tcg_gen_trunc_i64_i32(tmp, t);
432 66ba317c aurel32
    tcg_gen_mov_i32(cpu_fregs[reg + 1], tmp);
433 cc4ba6a9 aurel32
    tcg_gen_shri_i64(t, t, 32);
434 cc4ba6a9 aurel32
    tcg_gen_trunc_i64_i32(tmp, t);
435 66ba317c aurel32
    tcg_gen_mov_i32(cpu_fregs[reg], tmp);
436 a7812ae4 pbrook
    tcg_temp_free_i32(tmp);
437 cc4ba6a9 aurel32
}
438 cc4ba6a9 aurel32
439 fdf9b3e8 bellard
#define B3_0 (ctx->opcode & 0xf)
440 fdf9b3e8 bellard
#define B6_4 ((ctx->opcode >> 4) & 0x7)
441 fdf9b3e8 bellard
#define B7_4 ((ctx->opcode >> 4) & 0xf)
442 fdf9b3e8 bellard
#define B7_0 (ctx->opcode & 0xff)
443 fdf9b3e8 bellard
#define B7_0s ((int32_t) (int8_t) (ctx->opcode & 0xff))
444 fdf9b3e8 bellard
#define B11_0s (ctx->opcode & 0x800 ? 0xfffff000 | (ctx->opcode & 0xfff) : \
445 fdf9b3e8 bellard
  (ctx->opcode & 0xfff))
446 fdf9b3e8 bellard
#define B11_8 ((ctx->opcode >> 8) & 0xf)
447 fdf9b3e8 bellard
#define B15_12 ((ctx->opcode >> 12) & 0xf)
448 fdf9b3e8 bellard
449 fdf9b3e8 bellard
#define REG(x) ((x) < 8 && (ctx->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB) ? \
450 7efbe241 aurel32
                (cpu_gregs[x + 16]) : (cpu_gregs[x]))
451 fdf9b3e8 bellard
452 fdf9b3e8 bellard
#define ALTREG(x) ((x) < 8 && (ctx->sr & (SR_MD | SR_RB)) != (SR_MD | SR_RB) \
453 7efbe241 aurel32
                ? (cpu_gregs[x + 16]) : (cpu_gregs[x]))
454 fdf9b3e8 bellard
455 eda9b09b bellard
#define FREG(x) (ctx->fpscr & FPSCR_FR ? (x) ^ 0x10 : (x))
456 f09111e0 ths
#define XHACK(x) ((((x) & 1 ) << 4) | ((x) & 0xe))
457 eda9b09b bellard
#define XREG(x) (ctx->fpscr & FPSCR_FR ? XHACK(x) ^ 0x10 : XHACK(x))
458 ea6cf6be ths
#define DREG(x) FREG(x) /* Assumes lsb of (x) is always 0 */
459 eda9b09b bellard
460 fdf9b3e8 bellard
#define CHECK_NOT_DELAY_SLOT \
461 d8299bcc aurel32
  if (ctx->flags & (DELAY_SLOT | DELAY_SLOT_CONDITIONAL))     \
462 d8299bcc aurel32
  {                                                           \
463 d8299bcc aurel32
      gen_helper_raise_slot_illegal_instruction();            \
464 d8299bcc aurel32
      ctx->bstate = BS_EXCP;                                  \
465 d8299bcc aurel32
      return;                                                 \
466 d8299bcc aurel32
  }
467 fdf9b3e8 bellard
468 86865c5f Aurelien Jarno
#define CHECK_PRIVILEGED                                        \
469 86865c5f Aurelien Jarno
  if (IS_USER(ctx)) {                                           \
470 86865c5f Aurelien Jarno
      if (ctx->flags & (DELAY_SLOT | DELAY_SLOT_CONDITIONAL)) { \
471 86865c5f Aurelien Jarno
         gen_helper_raise_slot_illegal_instruction();           \
472 86865c5f Aurelien Jarno
      } else {                                                  \
473 86865c5f Aurelien Jarno
         gen_helper_raise_illegal_instruction();                \
474 86865c5f Aurelien Jarno
      }                                                         \
475 86865c5f Aurelien Jarno
      ctx->bstate = BS_EXCP;                                    \
476 86865c5f Aurelien Jarno
      return;                                                   \
477 fe25591e aurel32
  }
478 fe25591e aurel32
479 d8299bcc aurel32
#define CHECK_FPU_ENABLED                                       \
480 d8299bcc aurel32
  if (ctx->flags & SR_FD) {                                     \
481 d8299bcc aurel32
      if (ctx->flags & (DELAY_SLOT | DELAY_SLOT_CONDITIONAL)) { \
482 d8299bcc aurel32
          gen_helper_raise_slot_fpu_disable();                  \
483 d8299bcc aurel32
      } else {                                                  \
484 d8299bcc aurel32
          gen_helper_raise_fpu_disable();                       \
485 d8299bcc aurel32
      }                                                         \
486 d8299bcc aurel32
      ctx->bstate = BS_EXCP;                                    \
487 d8299bcc aurel32
      return;                                                   \
488 d8299bcc aurel32
  }
489 d8299bcc aurel32
490 b1d8e52e blueswir1
static void _decode_opc(DisasContext * ctx)
491 fdf9b3e8 bellard
{
492 852d481f edgar_igl
    /* This code tries to make movcal emulation sufficiently
493 852d481f edgar_igl
       accurate for Linux purposes.  This instruction writes
494 852d481f edgar_igl
       memory, and prior to that, always allocates a cache line.
495 852d481f edgar_igl
       It is used in two contexts:
496 852d481f edgar_igl
       - in memcpy, where data is copied in blocks, the first write
497 852d481f edgar_igl
       of to a block uses movca.l for performance.
498 852d481f edgar_igl
       - in arch/sh/mm/cache-sh4.c, movcal.l + ocbi combination is used
499 852d481f edgar_igl
       to flush the cache. Here, the data written by movcal.l is never
500 852d481f edgar_igl
       written to memory, and the data written is just bogus.
501 852d481f edgar_igl

502 852d481f edgar_igl
       To simulate this, we simulate movcal.l, we store the value to memory,
503 852d481f edgar_igl
       but we also remember the previous content. If we see ocbi, we check
504 852d481f edgar_igl
       if movcal.l for that address was done previously. If so, the write should
505 852d481f edgar_igl
       not have hit the memory, so we restore the previous content.
506 852d481f edgar_igl
       When we see an instruction that is neither movca.l
507 852d481f edgar_igl
       nor ocbi, the previous content is discarded.
508 852d481f edgar_igl

509 852d481f edgar_igl
       To optimize, we only try to flush stores when we're at the start of
510 852d481f edgar_igl
       TB, or if we already saw movca.l in this TB and did not flush stores
511 852d481f edgar_igl
       yet.  */
512 852d481f edgar_igl
    if (ctx->has_movcal)
513 852d481f edgar_igl
        {
514 852d481f edgar_igl
          int opcode = ctx->opcode & 0xf0ff;
515 852d481f edgar_igl
          if (opcode != 0x0093 /* ocbi */
516 852d481f edgar_igl
              && opcode != 0x00c3 /* movca.l */)
517 852d481f edgar_igl
              {
518 852d481f edgar_igl
                  gen_helper_discard_movcal_backup ();
519 852d481f edgar_igl
                  ctx->has_movcal = 0;
520 852d481f edgar_igl
              }
521 852d481f edgar_igl
        }
522 852d481f edgar_igl
523 fdf9b3e8 bellard
#if 0
524 fdf9b3e8 bellard
    fprintf(stderr, "Translating opcode 0x%04x\n", ctx->opcode);
525 fdf9b3e8 bellard
#endif
526 f6198371 aurel32
527 fdf9b3e8 bellard
    switch (ctx->opcode) {
528 fdf9b3e8 bellard
    case 0x0019:                /* div0u */
529 3a8a44c4 aurel32
        tcg_gen_andi_i32(cpu_sr, cpu_sr, ~(SR_M | SR_Q | SR_T));
530 fdf9b3e8 bellard
        return;
531 fdf9b3e8 bellard
    case 0x000b:                /* rts */
532 1000822b aurel32
        CHECK_NOT_DELAY_SLOT
533 1000822b aurel32
        tcg_gen_mov_i32(cpu_delayed_pc, cpu_pr);
534 fdf9b3e8 bellard
        ctx->flags |= DELAY_SLOT;
535 fdf9b3e8 bellard
        ctx->delayed_pc = (uint32_t) - 1;
536 fdf9b3e8 bellard
        return;
537 fdf9b3e8 bellard
    case 0x0028:                /* clrmac */
538 3a8a44c4 aurel32
        tcg_gen_movi_i32(cpu_mach, 0);
539 3a8a44c4 aurel32
        tcg_gen_movi_i32(cpu_macl, 0);
540 fdf9b3e8 bellard
        return;
541 fdf9b3e8 bellard
    case 0x0048:                /* clrs */
542 3a8a44c4 aurel32
        tcg_gen_andi_i32(cpu_sr, cpu_sr, ~SR_S);
543 fdf9b3e8 bellard
        return;
544 fdf9b3e8 bellard
    case 0x0008:                /* clrt */
545 a4625612 aurel32
        gen_clr_t();
546 fdf9b3e8 bellard
        return;
547 fdf9b3e8 bellard
    case 0x0038:                /* ldtlb */
548 fe25591e aurel32
        CHECK_PRIVILEGED
549 a7812ae4 pbrook
        gen_helper_ldtlb();
550 fdf9b3e8 bellard
        return;
551 c5e814b2 ths
    case 0x002b:                /* rte */
552 fe25591e aurel32
        CHECK_PRIVILEGED
553 1000822b aurel32
        CHECK_NOT_DELAY_SLOT
554 1000822b aurel32
        tcg_gen_mov_i32(cpu_sr, cpu_ssr);
555 1000822b aurel32
        tcg_gen_mov_i32(cpu_delayed_pc, cpu_spc);
556 fdf9b3e8 bellard
        ctx->flags |= DELAY_SLOT;
557 fdf9b3e8 bellard
        ctx->delayed_pc = (uint32_t) - 1;
558 fdf9b3e8 bellard
        return;
559 fdf9b3e8 bellard
    case 0x0058:                /* sets */
560 3a8a44c4 aurel32
        tcg_gen_ori_i32(cpu_sr, cpu_sr, SR_S);
561 fdf9b3e8 bellard
        return;
562 fdf9b3e8 bellard
    case 0x0018:                /* sett */
563 a4625612 aurel32
        gen_set_t();
564 fdf9b3e8 bellard
        return;
565 24988dc2 aurel32
    case 0xfbfd:                /* frchg */
566 6f06939b aurel32
        tcg_gen_xori_i32(cpu_fpscr, cpu_fpscr, FPSCR_FR);
567 823029f9 ths
        ctx->bstate = BS_STOP;
568 fdf9b3e8 bellard
        return;
569 24988dc2 aurel32
    case 0xf3fd:                /* fschg */
570 6f06939b aurel32
        tcg_gen_xori_i32(cpu_fpscr, cpu_fpscr, FPSCR_SZ);
571 823029f9 ths
        ctx->bstate = BS_STOP;
572 fdf9b3e8 bellard
        return;
573 fdf9b3e8 bellard
    case 0x0009:                /* nop */
574 fdf9b3e8 bellard
        return;
575 fdf9b3e8 bellard
    case 0x001b:                /* sleep */
576 fe25591e aurel32
        CHECK_PRIVILEGED
577 a7812ae4 pbrook
        gen_helper_sleep(tcg_const_i32(ctx->pc + 2));
578 fdf9b3e8 bellard
        return;
579 fdf9b3e8 bellard
    }
580 fdf9b3e8 bellard
581 fdf9b3e8 bellard
    switch (ctx->opcode & 0xf000) {
582 fdf9b3e8 bellard
    case 0x1000:                /* mov.l Rm,@(disp,Rn) */
583 c55497ec aurel32
        {
584 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
585 c55497ec aurel32
            tcg_gen_addi_i32(addr, REG(B11_8), B3_0 * 4);
586 c55497ec aurel32
            tcg_gen_qemu_st32(REG(B7_4), addr, ctx->memidx);
587 c55497ec aurel32
            tcg_temp_free(addr);
588 c55497ec aurel32
        }
589 fdf9b3e8 bellard
        return;
590 fdf9b3e8 bellard
    case 0x5000:                /* mov.l @(disp,Rm),Rn */
591 c55497ec aurel32
        {
592 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
593 c55497ec aurel32
            tcg_gen_addi_i32(addr, REG(B7_4), B3_0 * 4);
594 c55497ec aurel32
            tcg_gen_qemu_ld32s(REG(B11_8), addr, ctx->memidx);
595 c55497ec aurel32
            tcg_temp_free(addr);
596 c55497ec aurel32
        }
597 fdf9b3e8 bellard
        return;
598 24988dc2 aurel32
    case 0xe000:                /* mov #imm,Rn */
599 7efbe241 aurel32
        tcg_gen_movi_i32(REG(B11_8), B7_0s);
600 fdf9b3e8 bellard
        return;
601 fdf9b3e8 bellard
    case 0x9000:                /* mov.w @(disp,PC),Rn */
602 c55497ec aurel32
        {
603 c55497ec aurel32
            TCGv addr = tcg_const_i32(ctx->pc + 4 + B7_0 * 2);
604 c55497ec aurel32
            tcg_gen_qemu_ld16s(REG(B11_8), addr, ctx->memidx);
605 c55497ec aurel32
            tcg_temp_free(addr);
606 c55497ec aurel32
        }
607 fdf9b3e8 bellard
        return;
608 fdf9b3e8 bellard
    case 0xd000:                /* mov.l @(disp,PC),Rn */
609 c55497ec aurel32
        {
610 c55497ec aurel32
            TCGv addr = tcg_const_i32((ctx->pc + 4 + B7_0 * 4) & ~3);
611 c55497ec aurel32
            tcg_gen_qemu_ld32s(REG(B11_8), addr, ctx->memidx);
612 c55497ec aurel32
            tcg_temp_free(addr);
613 c55497ec aurel32
        }
614 fdf9b3e8 bellard
        return;
615 24988dc2 aurel32
    case 0x7000:                /* add #imm,Rn */
616 7efbe241 aurel32
        tcg_gen_addi_i32(REG(B11_8), REG(B11_8), B7_0s);
617 fdf9b3e8 bellard
        return;
618 fdf9b3e8 bellard
    case 0xa000:                /* bra disp */
619 fdf9b3e8 bellard
        CHECK_NOT_DELAY_SLOT
620 1000822b aurel32
        ctx->delayed_pc = ctx->pc + 4 + B11_0s * 2;
621 1000822b aurel32
        tcg_gen_movi_i32(cpu_delayed_pc, ctx->delayed_pc);
622 fdf9b3e8 bellard
        ctx->flags |= DELAY_SLOT;
623 fdf9b3e8 bellard
        return;
624 fdf9b3e8 bellard
    case 0xb000:                /* bsr disp */
625 fdf9b3e8 bellard
        CHECK_NOT_DELAY_SLOT
626 1000822b aurel32
        tcg_gen_movi_i32(cpu_pr, ctx->pc + 4);
627 1000822b aurel32
        ctx->delayed_pc = ctx->pc + 4 + B11_0s * 2;
628 1000822b aurel32
        tcg_gen_movi_i32(cpu_delayed_pc, ctx->delayed_pc);
629 fdf9b3e8 bellard
        ctx->flags |= DELAY_SLOT;
630 fdf9b3e8 bellard
        return;
631 fdf9b3e8 bellard
    }
632 fdf9b3e8 bellard
633 fdf9b3e8 bellard
    switch (ctx->opcode & 0xf00f) {
634 fdf9b3e8 bellard
    case 0x6003:                /* mov Rm,Rn */
635 7efbe241 aurel32
        tcg_gen_mov_i32(REG(B11_8), REG(B7_4));
636 fdf9b3e8 bellard
        return;
637 fdf9b3e8 bellard
    case 0x2000:                /* mov.b Rm,@Rn */
638 7efbe241 aurel32
        tcg_gen_qemu_st8(REG(B7_4), REG(B11_8), ctx->memidx);
639 fdf9b3e8 bellard
        return;
640 fdf9b3e8 bellard
    case 0x2001:                /* mov.w Rm,@Rn */
641 7efbe241 aurel32
        tcg_gen_qemu_st16(REG(B7_4), REG(B11_8), ctx->memidx);
642 fdf9b3e8 bellard
        return;
643 fdf9b3e8 bellard
    case 0x2002:                /* mov.l Rm,@Rn */
644 7efbe241 aurel32
        tcg_gen_qemu_st32(REG(B7_4), REG(B11_8), ctx->memidx);
645 fdf9b3e8 bellard
        return;
646 fdf9b3e8 bellard
    case 0x6000:                /* mov.b @Rm,Rn */
647 7efbe241 aurel32
        tcg_gen_qemu_ld8s(REG(B11_8), REG(B7_4), ctx->memidx);
648 fdf9b3e8 bellard
        return;
649 fdf9b3e8 bellard
    case 0x6001:                /* mov.w @Rm,Rn */
650 7efbe241 aurel32
        tcg_gen_qemu_ld16s(REG(B11_8), REG(B7_4), ctx->memidx);
651 fdf9b3e8 bellard
        return;
652 fdf9b3e8 bellard
    case 0x6002:                /* mov.l @Rm,Rn */
653 7efbe241 aurel32
        tcg_gen_qemu_ld32s(REG(B11_8), REG(B7_4), ctx->memidx);
654 fdf9b3e8 bellard
        return;
655 fdf9b3e8 bellard
    case 0x2004:                /* mov.b Rm,@-Rn */
656 c55497ec aurel32
        {
657 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
658 c55497ec aurel32
            tcg_gen_subi_i32(addr, REG(B11_8), 1);
659 c55497ec aurel32
            tcg_gen_qemu_st8(REG(B7_4), addr, ctx->memidx);        /* might cause re-execution */
660 3101e99c Aurelien Jarno
            tcg_gen_mov_i32(REG(B11_8), addr);                        /* modify register status */
661 c55497ec aurel32
            tcg_temp_free(addr);
662 c55497ec aurel32
        }
663 fdf9b3e8 bellard
        return;
664 fdf9b3e8 bellard
    case 0x2005:                /* mov.w Rm,@-Rn */
665 c55497ec aurel32
        {
666 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
667 c55497ec aurel32
            tcg_gen_subi_i32(addr, REG(B11_8), 2);
668 c55497ec aurel32
            tcg_gen_qemu_st16(REG(B7_4), addr, ctx->memidx);
669 3101e99c Aurelien Jarno
            tcg_gen_mov_i32(REG(B11_8), addr);
670 c55497ec aurel32
            tcg_temp_free(addr);
671 c55497ec aurel32
        }
672 fdf9b3e8 bellard
        return;
673 fdf9b3e8 bellard
    case 0x2006:                /* mov.l Rm,@-Rn */
674 c55497ec aurel32
        {
675 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
676 c55497ec aurel32
            tcg_gen_subi_i32(addr, REG(B11_8), 4);
677 c55497ec aurel32
            tcg_gen_qemu_st32(REG(B7_4), addr, ctx->memidx);
678 3101e99c Aurelien Jarno
            tcg_gen_mov_i32(REG(B11_8), addr);
679 c55497ec aurel32
        }
680 fdf9b3e8 bellard
        return;
681 eda9b09b bellard
    case 0x6004:                /* mov.b @Rm+,Rn */
682 7efbe241 aurel32
        tcg_gen_qemu_ld8s(REG(B11_8), REG(B7_4), ctx->memidx);
683 24988dc2 aurel32
        if ( B11_8 != B7_4 )
684 7efbe241 aurel32
                tcg_gen_addi_i32(REG(B7_4), REG(B7_4), 1);
685 fdf9b3e8 bellard
        return;
686 fdf9b3e8 bellard
    case 0x6005:                /* mov.w @Rm+,Rn */
687 7efbe241 aurel32
        tcg_gen_qemu_ld16s(REG(B11_8), REG(B7_4), ctx->memidx);
688 24988dc2 aurel32
        if ( B11_8 != B7_4 )
689 7efbe241 aurel32
                tcg_gen_addi_i32(REG(B7_4), REG(B7_4), 2);
690 fdf9b3e8 bellard
        return;
691 fdf9b3e8 bellard
    case 0x6006:                /* mov.l @Rm+,Rn */
692 7efbe241 aurel32
        tcg_gen_qemu_ld32s(REG(B11_8), REG(B7_4), ctx->memidx);
693 24988dc2 aurel32
        if ( B11_8 != B7_4 )
694 7efbe241 aurel32
                tcg_gen_addi_i32(REG(B7_4), REG(B7_4), 4);
695 fdf9b3e8 bellard
        return;
696 fdf9b3e8 bellard
    case 0x0004:                /* mov.b Rm,@(R0,Rn) */
697 c55497ec aurel32
        {
698 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
699 c55497ec aurel32
            tcg_gen_add_i32(addr, REG(B11_8), REG(0));
700 c55497ec aurel32
            tcg_gen_qemu_st8(REG(B7_4), addr, ctx->memidx);
701 c55497ec aurel32
            tcg_temp_free(addr);
702 c55497ec aurel32
        }
703 fdf9b3e8 bellard
        return;
704 fdf9b3e8 bellard
    case 0x0005:                /* mov.w Rm,@(R0,Rn) */
705 c55497ec aurel32
        {
706 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
707 c55497ec aurel32
            tcg_gen_add_i32(addr, REG(B11_8), REG(0));
708 c55497ec aurel32
            tcg_gen_qemu_st16(REG(B7_4), addr, ctx->memidx);
709 c55497ec aurel32
            tcg_temp_free(addr);
710 c55497ec aurel32
        }
711 fdf9b3e8 bellard
        return;
712 fdf9b3e8 bellard
    case 0x0006:                /* mov.l Rm,@(R0,Rn) */
713 c55497ec aurel32
        {
714 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
715 c55497ec aurel32
            tcg_gen_add_i32(addr, REG(B11_8), REG(0));
716 c55497ec aurel32
            tcg_gen_qemu_st32(REG(B7_4), addr, ctx->memidx);
717 c55497ec aurel32
            tcg_temp_free(addr);
718 c55497ec aurel32
        }
719 fdf9b3e8 bellard
        return;
720 fdf9b3e8 bellard
    case 0x000c:                /* mov.b @(R0,Rm),Rn */
721 c55497ec aurel32
        {
722 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
723 c55497ec aurel32
            tcg_gen_add_i32(addr, REG(B7_4), REG(0));
724 c55497ec aurel32
            tcg_gen_qemu_ld8s(REG(B11_8), addr, ctx->memidx);
725 c55497ec aurel32
            tcg_temp_free(addr);
726 c55497ec aurel32
        }
727 fdf9b3e8 bellard
        return;
728 fdf9b3e8 bellard
    case 0x000d:                /* mov.w @(R0,Rm),Rn */
729 c55497ec aurel32
        {
730 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
731 c55497ec aurel32
            tcg_gen_add_i32(addr, REG(B7_4), REG(0));
732 c55497ec aurel32
            tcg_gen_qemu_ld16s(REG(B11_8), addr, ctx->memidx);
733 c55497ec aurel32
            tcg_temp_free(addr);
734 c55497ec aurel32
        }
735 fdf9b3e8 bellard
        return;
736 fdf9b3e8 bellard
    case 0x000e:                /* mov.l @(R0,Rm),Rn */
737 c55497ec aurel32
        {
738 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
739 c55497ec aurel32
            tcg_gen_add_i32(addr, REG(B7_4), REG(0));
740 c55497ec aurel32
            tcg_gen_qemu_ld32s(REG(B11_8), addr, ctx->memidx);
741 c55497ec aurel32
            tcg_temp_free(addr);
742 c55497ec aurel32
        }
743 fdf9b3e8 bellard
        return;
744 fdf9b3e8 bellard
    case 0x6008:                /* swap.b Rm,Rn */
745 c55497ec aurel32
        {
746 3101e99c Aurelien Jarno
            TCGv high, low;
747 a7812ae4 pbrook
            high = tcg_temp_new();
748 3101e99c Aurelien Jarno
            tcg_gen_andi_i32(high, REG(B7_4), 0xffff0000);
749 a7812ae4 pbrook
            low = tcg_temp_new();
750 3101e99c Aurelien Jarno
            tcg_gen_ext16u_i32(low, REG(B7_4));
751 3101e99c Aurelien Jarno
            tcg_gen_bswap16_i32(low, low);
752 c55497ec aurel32
            tcg_gen_or_i32(REG(B11_8), high, low);
753 c55497ec aurel32
            tcg_temp_free(low);
754 c55497ec aurel32
            tcg_temp_free(high);
755 c55497ec aurel32
        }
756 fdf9b3e8 bellard
        return;
757 fdf9b3e8 bellard
    case 0x6009:                /* swap.w Rm,Rn */
758 c55497ec aurel32
        {
759 c55497ec aurel32
            TCGv high, low;
760 a7812ae4 pbrook
            high = tcg_temp_new();
761 3101e99c Aurelien Jarno
            tcg_gen_shli_i32(high, REG(B7_4), 16);
762 a7812ae4 pbrook
            low = tcg_temp_new();
763 c55497ec aurel32
            tcg_gen_shri_i32(low, REG(B7_4), 16);
764 c55497ec aurel32
            tcg_gen_ext16u_i32(low, low);
765 c55497ec aurel32
            tcg_gen_or_i32(REG(B11_8), high, low);
766 c55497ec aurel32
            tcg_temp_free(low);
767 c55497ec aurel32
            tcg_temp_free(high);
768 c55497ec aurel32
        }
769 fdf9b3e8 bellard
        return;
770 fdf9b3e8 bellard
    case 0x200d:                /* xtrct Rm,Rn */
771 c55497ec aurel32
        {
772 c55497ec aurel32
            TCGv high, low;
773 a7812ae4 pbrook
            high = tcg_temp_new();
774 3101e99c Aurelien Jarno
            tcg_gen_shli_i32(high, REG(B7_4), 16);
775 a7812ae4 pbrook
            low = tcg_temp_new();
776 c55497ec aurel32
            tcg_gen_shri_i32(low, REG(B11_8), 16);
777 c55497ec aurel32
            tcg_gen_ext16u_i32(low, low);
778 c55497ec aurel32
            tcg_gen_or_i32(REG(B11_8), high, low);
779 c55497ec aurel32
            tcg_temp_free(low);
780 c55497ec aurel32
            tcg_temp_free(high);
781 c55497ec aurel32
        }
782 fdf9b3e8 bellard
        return;
783 fdf9b3e8 bellard
    case 0x300c:                /* add Rm,Rn */
784 7efbe241 aurel32
        tcg_gen_add_i32(REG(B11_8), REG(B11_8), REG(B7_4));
785 fdf9b3e8 bellard
        return;
786 fdf9b3e8 bellard
    case 0x300e:                /* addc Rm,Rn */
787 a7812ae4 pbrook
        gen_helper_addc(REG(B11_8), REG(B7_4), REG(B11_8));
788 fdf9b3e8 bellard
        return;
789 fdf9b3e8 bellard
    case 0x300f:                /* addv Rm,Rn */
790 a7812ae4 pbrook
        gen_helper_addv(REG(B11_8), REG(B7_4), REG(B11_8));
791 fdf9b3e8 bellard
        return;
792 fdf9b3e8 bellard
    case 0x2009:                /* and Rm,Rn */
793 7efbe241 aurel32
        tcg_gen_and_i32(REG(B11_8), REG(B11_8), REG(B7_4));
794 fdf9b3e8 bellard
        return;
795 fdf9b3e8 bellard
    case 0x3000:                /* cmp/eq Rm,Rn */
796 7efbe241 aurel32
        gen_cmp(TCG_COND_EQ, REG(B7_4), REG(B11_8));
797 fdf9b3e8 bellard
        return;
798 fdf9b3e8 bellard
    case 0x3003:                /* cmp/ge Rm,Rn */
799 7efbe241 aurel32
        gen_cmp(TCG_COND_GE, REG(B7_4), REG(B11_8));
800 fdf9b3e8 bellard
        return;
801 fdf9b3e8 bellard
    case 0x3007:                /* cmp/gt Rm,Rn */
802 7efbe241 aurel32
        gen_cmp(TCG_COND_GT, REG(B7_4), REG(B11_8));
803 fdf9b3e8 bellard
        return;
804 fdf9b3e8 bellard
    case 0x3006:                /* cmp/hi Rm,Rn */
805 7efbe241 aurel32
        gen_cmp(TCG_COND_GTU, REG(B7_4), REG(B11_8));
806 fdf9b3e8 bellard
        return;
807 fdf9b3e8 bellard
    case 0x3002:                /* cmp/hs Rm,Rn */
808 7efbe241 aurel32
        gen_cmp(TCG_COND_GEU, REG(B7_4), REG(B11_8));
809 fdf9b3e8 bellard
        return;
810 fdf9b3e8 bellard
    case 0x200c:                /* cmp/str Rm,Rn */
811 69d6275b aurel32
        {
812 c5c19137 Aurelien Jarno
            TCGv cmp1 = tcg_temp_new();
813 c5c19137 Aurelien Jarno
            TCGv cmp2 = tcg_temp_new();
814 c5c19137 Aurelien Jarno
            tcg_gen_andi_i32(cpu_sr, cpu_sr, ~SR_T);
815 c55497ec aurel32
            tcg_gen_xor_i32(cmp1, REG(B7_4), REG(B11_8));
816 c55497ec aurel32
            tcg_gen_andi_i32(cmp2, cmp1, 0xff000000);
817 c5c19137 Aurelien Jarno
            tcg_gen_setcondi_i32(TCG_COND_EQ, cmp2, cmp2, 0);
818 c5c19137 Aurelien Jarno
            tcg_gen_or_i32(cpu_sr, cpu_sr, cmp2);
819 c55497ec aurel32
            tcg_gen_andi_i32(cmp2, cmp1, 0x00ff0000);
820 c5c19137 Aurelien Jarno
            tcg_gen_setcondi_i32(TCG_COND_EQ, cmp2, cmp2, 0);
821 c5c19137 Aurelien Jarno
            tcg_gen_or_i32(cpu_sr, cpu_sr, cmp2);
822 c55497ec aurel32
            tcg_gen_andi_i32(cmp2, cmp1, 0x0000ff00);
823 c5c19137 Aurelien Jarno
            tcg_gen_setcondi_i32(TCG_COND_EQ, cmp2, cmp2, 0);
824 c5c19137 Aurelien Jarno
            tcg_gen_or_i32(cpu_sr, cpu_sr, cmp2);
825 c55497ec aurel32
            tcg_gen_andi_i32(cmp2, cmp1, 0x000000ff);
826 c5c19137 Aurelien Jarno
            tcg_gen_setcondi_i32(TCG_COND_EQ, cmp2, cmp2, 0);
827 c5c19137 Aurelien Jarno
            tcg_gen_or_i32(cpu_sr, cpu_sr, cmp2);
828 c55497ec aurel32
            tcg_temp_free(cmp2);
829 c55497ec aurel32
            tcg_temp_free(cmp1);
830 69d6275b aurel32
        }
831 fdf9b3e8 bellard
        return;
832 fdf9b3e8 bellard
    case 0x2007:                /* div0s Rm,Rn */
833 c55497ec aurel32
        {
834 c55497ec aurel32
            gen_copy_bit_i32(cpu_sr, 8, REG(B11_8), 31);        /* SR_Q */
835 c55497ec aurel32
            gen_copy_bit_i32(cpu_sr, 9, REG(B7_4), 31);                /* SR_M */
836 a7812ae4 pbrook
            TCGv val = tcg_temp_new();
837 c55497ec aurel32
            tcg_gen_xor_i32(val, REG(B7_4), REG(B11_8));
838 c55497ec aurel32
            gen_copy_bit_i32(cpu_sr, 0, val, 31);                /* SR_T */
839 c55497ec aurel32
            tcg_temp_free(val);
840 c55497ec aurel32
        }
841 fdf9b3e8 bellard
        return;
842 fdf9b3e8 bellard
    case 0x3004:                /* div1 Rm,Rn */
843 a7812ae4 pbrook
        gen_helper_div1(REG(B11_8), REG(B7_4), REG(B11_8));
844 fdf9b3e8 bellard
        return;
845 fdf9b3e8 bellard
    case 0x300d:                /* dmuls.l Rm,Rn */
846 6f06939b aurel32
        {
847 a7812ae4 pbrook
            TCGv_i64 tmp1 = tcg_temp_new_i64();
848 a7812ae4 pbrook
            TCGv_i64 tmp2 = tcg_temp_new_i64();
849 6f06939b aurel32
850 7efbe241 aurel32
            tcg_gen_ext_i32_i64(tmp1, REG(B7_4));
851 7efbe241 aurel32
            tcg_gen_ext_i32_i64(tmp2, REG(B11_8));
852 6f06939b aurel32
            tcg_gen_mul_i64(tmp1, tmp1, tmp2);
853 6f06939b aurel32
            tcg_gen_trunc_i64_i32(cpu_macl, tmp1);
854 6f06939b aurel32
            tcg_gen_shri_i64(tmp1, tmp1, 32);
855 6f06939b aurel32
            tcg_gen_trunc_i64_i32(cpu_mach, tmp1);
856 6f06939b aurel32
857 a7812ae4 pbrook
            tcg_temp_free_i64(tmp2);
858 a7812ae4 pbrook
            tcg_temp_free_i64(tmp1);
859 6f06939b aurel32
        }
860 fdf9b3e8 bellard
        return;
861 fdf9b3e8 bellard
    case 0x3005:                /* dmulu.l Rm,Rn */
862 6f06939b aurel32
        {
863 a7812ae4 pbrook
            TCGv_i64 tmp1 = tcg_temp_new_i64();
864 a7812ae4 pbrook
            TCGv_i64 tmp2 = tcg_temp_new_i64();
865 6f06939b aurel32
866 7efbe241 aurel32
            tcg_gen_extu_i32_i64(tmp1, REG(B7_4));
867 7efbe241 aurel32
            tcg_gen_extu_i32_i64(tmp2, REG(B11_8));
868 6f06939b aurel32
            tcg_gen_mul_i64(tmp1, tmp1, tmp2);
869 6f06939b aurel32
            tcg_gen_trunc_i64_i32(cpu_macl, tmp1);
870 6f06939b aurel32
            tcg_gen_shri_i64(tmp1, tmp1, 32);
871 6f06939b aurel32
            tcg_gen_trunc_i64_i32(cpu_mach, tmp1);
872 6f06939b aurel32
873 a7812ae4 pbrook
            tcg_temp_free_i64(tmp2);
874 a7812ae4 pbrook
            tcg_temp_free_i64(tmp1);
875 6f06939b aurel32
        }
876 fdf9b3e8 bellard
        return;
877 fdf9b3e8 bellard
    case 0x600e:                /* exts.b Rm,Rn */
878 7efbe241 aurel32
        tcg_gen_ext8s_i32(REG(B11_8), REG(B7_4));
879 fdf9b3e8 bellard
        return;
880 fdf9b3e8 bellard
    case 0x600f:                /* exts.w Rm,Rn */
881 7efbe241 aurel32
        tcg_gen_ext16s_i32(REG(B11_8), REG(B7_4));
882 fdf9b3e8 bellard
        return;
883 fdf9b3e8 bellard
    case 0x600c:                /* extu.b Rm,Rn */
884 7efbe241 aurel32
        tcg_gen_ext8u_i32(REG(B11_8), REG(B7_4));
885 fdf9b3e8 bellard
        return;
886 fdf9b3e8 bellard
    case 0x600d:                /* extu.w Rm,Rn */
887 7efbe241 aurel32
        tcg_gen_ext16u_i32(REG(B11_8), REG(B7_4));
888 fdf9b3e8 bellard
        return;
889 24988dc2 aurel32
    case 0x000f:                /* mac.l @Rm+,@Rn+ */
890 c55497ec aurel32
        {
891 c55497ec aurel32
            TCGv arg0, arg1;
892 a7812ae4 pbrook
            arg0 = tcg_temp_new();
893 c55497ec aurel32
            tcg_gen_qemu_ld32s(arg0, REG(B7_4), ctx->memidx);
894 a7812ae4 pbrook
            arg1 = tcg_temp_new();
895 c55497ec aurel32
            tcg_gen_qemu_ld32s(arg1, REG(B11_8), ctx->memidx);
896 a7812ae4 pbrook
            gen_helper_macl(arg0, arg1);
897 c55497ec aurel32
            tcg_temp_free(arg1);
898 c55497ec aurel32
            tcg_temp_free(arg0);
899 c55497ec aurel32
            tcg_gen_addi_i32(REG(B7_4), REG(B7_4), 4);
900 c55497ec aurel32
            tcg_gen_addi_i32(REG(B11_8), REG(B11_8), 4);
901 c55497ec aurel32
        }
902 fdf9b3e8 bellard
        return;
903 fdf9b3e8 bellard
    case 0x400f:                /* mac.w @Rm+,@Rn+ */
904 c55497ec aurel32
        {
905 c55497ec aurel32
            TCGv arg0, arg1;
906 a7812ae4 pbrook
            arg0 = tcg_temp_new();
907 c55497ec aurel32
            tcg_gen_qemu_ld32s(arg0, REG(B7_4), ctx->memidx);
908 a7812ae4 pbrook
            arg1 = tcg_temp_new();
909 c55497ec aurel32
            tcg_gen_qemu_ld32s(arg1, REG(B11_8), ctx->memidx);
910 a7812ae4 pbrook
            gen_helper_macw(arg0, arg1);
911 c55497ec aurel32
            tcg_temp_free(arg1);
912 c55497ec aurel32
            tcg_temp_free(arg0);
913 c55497ec aurel32
            tcg_gen_addi_i32(REG(B11_8), REG(B11_8), 2);
914 c55497ec aurel32
            tcg_gen_addi_i32(REG(B7_4), REG(B7_4), 2);
915 c55497ec aurel32
        }
916 fdf9b3e8 bellard
        return;
917 fdf9b3e8 bellard
    case 0x0007:                /* mul.l Rm,Rn */
918 7efbe241 aurel32
        tcg_gen_mul_i32(cpu_macl, REG(B7_4), REG(B11_8));
919 fdf9b3e8 bellard
        return;
920 fdf9b3e8 bellard
    case 0x200f:                /* muls.w Rm,Rn */
921 c55497ec aurel32
        {
922 c55497ec aurel32
            TCGv arg0, arg1;
923 a7812ae4 pbrook
            arg0 = tcg_temp_new();
924 c55497ec aurel32
            tcg_gen_ext16s_i32(arg0, REG(B7_4));
925 a7812ae4 pbrook
            arg1 = tcg_temp_new();
926 c55497ec aurel32
            tcg_gen_ext16s_i32(arg1, REG(B11_8));
927 c55497ec aurel32
            tcg_gen_mul_i32(cpu_macl, arg0, arg1);
928 c55497ec aurel32
            tcg_temp_free(arg1);
929 c55497ec aurel32
            tcg_temp_free(arg0);
930 c55497ec aurel32
        }
931 fdf9b3e8 bellard
        return;
932 fdf9b3e8 bellard
    case 0x200e:                /* mulu.w Rm,Rn */
933 c55497ec aurel32
        {
934 c55497ec aurel32
            TCGv arg0, arg1;
935 a7812ae4 pbrook
            arg0 = tcg_temp_new();
936 c55497ec aurel32
            tcg_gen_ext16u_i32(arg0, REG(B7_4));
937 a7812ae4 pbrook
            arg1 = tcg_temp_new();
938 c55497ec aurel32
            tcg_gen_ext16u_i32(arg1, REG(B11_8));
939 c55497ec aurel32
            tcg_gen_mul_i32(cpu_macl, arg0, arg1);
940 c55497ec aurel32
            tcg_temp_free(arg1);
941 c55497ec aurel32
            tcg_temp_free(arg0);
942 c55497ec aurel32
        }
943 fdf9b3e8 bellard
        return;
944 fdf9b3e8 bellard
    case 0x600b:                /* neg Rm,Rn */
945 7efbe241 aurel32
        tcg_gen_neg_i32(REG(B11_8), REG(B7_4));
946 fdf9b3e8 bellard
        return;
947 fdf9b3e8 bellard
    case 0x600a:                /* negc Rm,Rn */
948 b2d9eda5 Aurelien Jarno
        {
949 b2d9eda5 Aurelien Jarno
            TCGv t0, t1;
950 b2d9eda5 Aurelien Jarno
            t0 = tcg_temp_new();
951 b2d9eda5 Aurelien Jarno
            tcg_gen_neg_i32(t0, REG(B7_4));
952 b2d9eda5 Aurelien Jarno
            t1 = tcg_temp_new();
953 b2d9eda5 Aurelien Jarno
            tcg_gen_andi_i32(t1, cpu_sr, SR_T);
954 b2d9eda5 Aurelien Jarno
            tcg_gen_sub_i32(REG(B11_8), t0, t1);
955 b2d9eda5 Aurelien Jarno
            tcg_gen_andi_i32(cpu_sr, cpu_sr, ~SR_T);
956 7026259f Aurelien Jarno
            tcg_gen_setcondi_i32(TCG_COND_GTU, t1, t0, 0);
957 b2d9eda5 Aurelien Jarno
            tcg_gen_or_i32(cpu_sr, cpu_sr, t1);
958 7026259f Aurelien Jarno
            tcg_gen_setcond_i32(TCG_COND_GTU, t1, REG(B11_8), t0);
959 b2d9eda5 Aurelien Jarno
            tcg_gen_or_i32(cpu_sr, cpu_sr, t1);
960 b2d9eda5 Aurelien Jarno
            tcg_temp_free(t0);
961 b2d9eda5 Aurelien Jarno
            tcg_temp_free(t1);
962 b2d9eda5 Aurelien Jarno
        }
963 fdf9b3e8 bellard
        return;
964 fdf9b3e8 bellard
    case 0x6007:                /* not Rm,Rn */
965 7efbe241 aurel32
        tcg_gen_not_i32(REG(B11_8), REG(B7_4));
966 fdf9b3e8 bellard
        return;
967 fdf9b3e8 bellard
    case 0x200b:                /* or Rm,Rn */
968 7efbe241 aurel32
        tcg_gen_or_i32(REG(B11_8), REG(B11_8), REG(B7_4));
969 fdf9b3e8 bellard
        return;
970 fdf9b3e8 bellard
    case 0x400c:                /* shad Rm,Rn */
971 69d6275b aurel32
        {
972 69d6275b aurel32
            int label1 = gen_new_label();
973 69d6275b aurel32
            int label2 = gen_new_label();
974 69d6275b aurel32
            int label3 = gen_new_label();
975 69d6275b aurel32
            int label4 = gen_new_label();
976 3101e99c Aurelien Jarno
            TCGv shift;
977 7efbe241 aurel32
            tcg_gen_brcondi_i32(TCG_COND_LT, REG(B7_4), 0, label1);
978 69d6275b aurel32
            /* Rm positive, shift to the left */
979 3101e99c Aurelien Jarno
            shift = tcg_temp_new();
980 c55497ec aurel32
            tcg_gen_andi_i32(shift, REG(B7_4), 0x1f);
981 c55497ec aurel32
            tcg_gen_shl_i32(REG(B11_8), REG(B11_8), shift);
982 3101e99c Aurelien Jarno
            tcg_temp_free(shift);
983 69d6275b aurel32
            tcg_gen_br(label4);
984 69d6275b aurel32
            /* Rm negative, shift to the right */
985 69d6275b aurel32
            gen_set_label(label1);
986 3101e99c Aurelien Jarno
            shift = tcg_temp_new();
987 c55497ec aurel32
            tcg_gen_andi_i32(shift, REG(B7_4), 0x1f);
988 c55497ec aurel32
            tcg_gen_brcondi_i32(TCG_COND_EQ, shift, 0, label2);
989 c55497ec aurel32
            tcg_gen_not_i32(shift, REG(B7_4));
990 c55497ec aurel32
            tcg_gen_andi_i32(shift, shift, 0x1f);
991 c55497ec aurel32
            tcg_gen_addi_i32(shift, shift, 1);
992 c55497ec aurel32
            tcg_gen_sar_i32(REG(B11_8), REG(B11_8), shift);
993 3101e99c Aurelien Jarno
            tcg_temp_free(shift);
994 69d6275b aurel32
            tcg_gen_br(label4);
995 69d6275b aurel32
            /* Rm = -32 */
996 69d6275b aurel32
            gen_set_label(label2);
997 7efbe241 aurel32
            tcg_gen_brcondi_i32(TCG_COND_LT, REG(B11_8), 0, label3);
998 7efbe241 aurel32
            tcg_gen_movi_i32(REG(B11_8), 0);
999 69d6275b aurel32
            tcg_gen_br(label4);
1000 69d6275b aurel32
            gen_set_label(label3);
1001 7efbe241 aurel32
            tcg_gen_movi_i32(REG(B11_8), 0xffffffff);
1002 69d6275b aurel32
            gen_set_label(label4);
1003 69d6275b aurel32
        }
1004 fdf9b3e8 bellard
        return;
1005 fdf9b3e8 bellard
    case 0x400d:                /* shld Rm,Rn */
1006 69d6275b aurel32
        {
1007 69d6275b aurel32
            int label1 = gen_new_label();
1008 69d6275b aurel32
            int label2 = gen_new_label();
1009 69d6275b aurel32
            int label3 = gen_new_label();
1010 3101e99c Aurelien Jarno
            TCGv shift;
1011 7efbe241 aurel32
            tcg_gen_brcondi_i32(TCG_COND_LT, REG(B7_4), 0, label1);
1012 69d6275b aurel32
            /* Rm positive, shift to the left */
1013 3101e99c Aurelien Jarno
            shift = tcg_temp_new();
1014 c55497ec aurel32
            tcg_gen_andi_i32(shift, REG(B7_4), 0x1f);
1015 c55497ec aurel32
            tcg_gen_shl_i32(REG(B11_8), REG(B11_8), shift);
1016 3101e99c Aurelien Jarno
            tcg_temp_free(shift);
1017 69d6275b aurel32
            tcg_gen_br(label3);
1018 69d6275b aurel32
            /* Rm negative, shift to the right */
1019 69d6275b aurel32
            gen_set_label(label1);
1020 3101e99c Aurelien Jarno
            shift = tcg_temp_new();
1021 c55497ec aurel32
            tcg_gen_andi_i32(shift, REG(B7_4), 0x1f);
1022 c55497ec aurel32
            tcg_gen_brcondi_i32(TCG_COND_EQ, shift, 0, label2);
1023 c55497ec aurel32
            tcg_gen_not_i32(shift, REG(B7_4));
1024 c55497ec aurel32
            tcg_gen_andi_i32(shift, shift, 0x1f);
1025 c55497ec aurel32
            tcg_gen_addi_i32(shift, shift, 1);
1026 c55497ec aurel32
            tcg_gen_shr_i32(REG(B11_8), REG(B11_8), shift);
1027 3101e99c Aurelien Jarno
            tcg_temp_free(shift);
1028 69d6275b aurel32
            tcg_gen_br(label3);
1029 69d6275b aurel32
            /* Rm = -32 */
1030 69d6275b aurel32
            gen_set_label(label2);
1031 7efbe241 aurel32
            tcg_gen_movi_i32(REG(B11_8), 0);
1032 69d6275b aurel32
            gen_set_label(label3);
1033 69d6275b aurel32
        }
1034 fdf9b3e8 bellard
        return;
1035 fdf9b3e8 bellard
    case 0x3008:                /* sub Rm,Rn */
1036 7efbe241 aurel32
        tcg_gen_sub_i32(REG(B11_8), REG(B11_8), REG(B7_4));
1037 fdf9b3e8 bellard
        return;
1038 fdf9b3e8 bellard
    case 0x300a:                /* subc Rm,Rn */
1039 a7812ae4 pbrook
        gen_helper_subc(REG(B11_8), REG(B7_4), REG(B11_8));
1040 fdf9b3e8 bellard
        return;
1041 fdf9b3e8 bellard
    case 0x300b:                /* subv Rm,Rn */
1042 a7812ae4 pbrook
        gen_helper_subv(REG(B11_8), REG(B7_4), REG(B11_8));
1043 fdf9b3e8 bellard
        return;
1044 fdf9b3e8 bellard
    case 0x2008:                /* tst Rm,Rn */
1045 c55497ec aurel32
        {
1046 a7812ae4 pbrook
            TCGv val = tcg_temp_new();
1047 c55497ec aurel32
            tcg_gen_and_i32(val, REG(B7_4), REG(B11_8));
1048 c55497ec aurel32
            gen_cmp_imm(TCG_COND_EQ, val, 0);
1049 c55497ec aurel32
            tcg_temp_free(val);
1050 c55497ec aurel32
        }
1051 fdf9b3e8 bellard
        return;
1052 fdf9b3e8 bellard
    case 0x200a:                /* xor Rm,Rn */
1053 7efbe241 aurel32
        tcg_gen_xor_i32(REG(B11_8), REG(B11_8), REG(B7_4));
1054 fdf9b3e8 bellard
        return;
1055 e67888a7 ths
    case 0xf00c: /* fmov {F,D,X}Rm,{F,D,X}Rn - FPSCR: Nothing */
1056 f6198371 aurel32
        CHECK_FPU_ENABLED
1057 022a22c7 ths
        if (ctx->fpscr & FPSCR_SZ) {
1058 a7812ae4 pbrook
            TCGv_i64 fp = tcg_temp_new_i64();
1059 cc4ba6a9 aurel32
            gen_load_fpr64(fp, XREG(B7_4));
1060 cc4ba6a9 aurel32
            gen_store_fpr64(fp, XREG(B11_8));
1061 a7812ae4 pbrook
            tcg_temp_free_i64(fp);
1062 eda9b09b bellard
        } else {
1063 66ba317c aurel32
            tcg_gen_mov_i32(cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B7_4)]);
1064 eda9b09b bellard
        }
1065 eda9b09b bellard
        return;
1066 e67888a7 ths
    case 0xf00a: /* fmov {F,D,X}Rm,@Rn - FPSCR: Nothing */
1067 f6198371 aurel32
        CHECK_FPU_ENABLED
1068 022a22c7 ths
        if (ctx->fpscr & FPSCR_SZ) {
1069 11bb09f1 aurel32
            TCGv addr_hi = tcg_temp_new();
1070 11bb09f1 aurel32
            int fr = XREG(B7_4);
1071 11bb09f1 aurel32
            tcg_gen_addi_i32(addr_hi, REG(B11_8), 4);
1072 11bb09f1 aurel32
            tcg_gen_qemu_st32(cpu_fregs[fr  ], REG(B11_8), ctx->memidx);
1073 11bb09f1 aurel32
            tcg_gen_qemu_st32(cpu_fregs[fr+1], addr_hi,           ctx->memidx);
1074 11bb09f1 aurel32
            tcg_temp_free(addr_hi);
1075 eda9b09b bellard
        } else {
1076 66ba317c aurel32
            tcg_gen_qemu_st32(cpu_fregs[FREG(B7_4)], REG(B11_8), ctx->memidx);
1077 eda9b09b bellard
        }
1078 eda9b09b bellard
        return;
1079 e67888a7 ths
    case 0xf008: /* fmov @Rm,{F,D,X}Rn - FPSCR: Nothing */
1080 f6198371 aurel32
        CHECK_FPU_ENABLED
1081 022a22c7 ths
        if (ctx->fpscr & FPSCR_SZ) {
1082 11bb09f1 aurel32
            TCGv addr_hi = tcg_temp_new();
1083 11bb09f1 aurel32
            int fr = XREG(B11_8);
1084 11bb09f1 aurel32
            tcg_gen_addi_i32(addr_hi, REG(B7_4), 4);
1085 11bb09f1 aurel32
            tcg_gen_qemu_ld32u(cpu_fregs[fr  ], REG(B7_4), ctx->memidx);
1086 11bb09f1 aurel32
            tcg_gen_qemu_ld32u(cpu_fregs[fr+1], addr_hi,   ctx->memidx);
1087 11bb09f1 aurel32
            tcg_temp_free(addr_hi);
1088 eda9b09b bellard
        } else {
1089 66ba317c aurel32
            tcg_gen_qemu_ld32u(cpu_fregs[FREG(B11_8)], REG(B7_4), ctx->memidx);
1090 eda9b09b bellard
        }
1091 eda9b09b bellard
        return;
1092 e67888a7 ths
    case 0xf009: /* fmov @Rm+,{F,D,X}Rn - FPSCR: Nothing */
1093 f6198371 aurel32
        CHECK_FPU_ENABLED
1094 022a22c7 ths
        if (ctx->fpscr & FPSCR_SZ) {
1095 11bb09f1 aurel32
            TCGv addr_hi = tcg_temp_new();
1096 11bb09f1 aurel32
            int fr = XREG(B11_8);
1097 11bb09f1 aurel32
            tcg_gen_addi_i32(addr_hi, REG(B7_4), 4);
1098 11bb09f1 aurel32
            tcg_gen_qemu_ld32u(cpu_fregs[fr  ], REG(B7_4), ctx->memidx);
1099 11bb09f1 aurel32
            tcg_gen_qemu_ld32u(cpu_fregs[fr+1], addr_hi,   ctx->memidx);
1100 11bb09f1 aurel32
            tcg_gen_addi_i32(REG(B7_4), REG(B7_4), 8);
1101 11bb09f1 aurel32
            tcg_temp_free(addr_hi);
1102 eda9b09b bellard
        } else {
1103 66ba317c aurel32
            tcg_gen_qemu_ld32u(cpu_fregs[FREG(B11_8)], REG(B7_4), ctx->memidx);
1104 cc4ba6a9 aurel32
            tcg_gen_addi_i32(REG(B7_4), REG(B7_4), 4);
1105 eda9b09b bellard
        }
1106 eda9b09b bellard
        return;
1107 e67888a7 ths
    case 0xf00b: /* fmov {F,D,X}Rm,@-Rn - FPSCR: Nothing */
1108 f6198371 aurel32
        CHECK_FPU_ENABLED
1109 022a22c7 ths
        if (ctx->fpscr & FPSCR_SZ) {
1110 11bb09f1 aurel32
            TCGv addr = tcg_temp_new_i32();
1111 11bb09f1 aurel32
            int fr = XREG(B7_4);
1112 11bb09f1 aurel32
            tcg_gen_subi_i32(addr, REG(B11_8), 4);
1113 11bb09f1 aurel32
            tcg_gen_qemu_st32(cpu_fregs[fr+1], addr, ctx->memidx);
1114 3101e99c Aurelien Jarno
            tcg_gen_subi_i32(addr, addr, 4);
1115 11bb09f1 aurel32
            tcg_gen_qemu_st32(cpu_fregs[fr  ], addr, ctx->memidx);
1116 11bb09f1 aurel32
            tcg_gen_mov_i32(REG(B11_8), addr);
1117 cc4ba6a9 aurel32
            tcg_temp_free(addr);
1118 eda9b09b bellard
        } else {
1119 a7812ae4 pbrook
            TCGv addr;
1120 a7812ae4 pbrook
            addr = tcg_temp_new_i32();
1121 cc4ba6a9 aurel32
            tcg_gen_subi_i32(addr, REG(B11_8), 4);
1122 66ba317c aurel32
            tcg_gen_qemu_st32(cpu_fregs[FREG(B7_4)], addr, ctx->memidx);
1123 3101e99c Aurelien Jarno
            tcg_gen_mov_i32(REG(B11_8), addr);
1124 cc4ba6a9 aurel32
            tcg_temp_free(addr);
1125 eda9b09b bellard
        }
1126 eda9b09b bellard
        return;
1127 e67888a7 ths
    case 0xf006: /* fmov @(R0,Rm),{F,D,X}Rm - FPSCR: Nothing */
1128 f6198371 aurel32
        CHECK_FPU_ENABLED
1129 cc4ba6a9 aurel32
        {
1130 a7812ae4 pbrook
            TCGv addr = tcg_temp_new_i32();
1131 cc4ba6a9 aurel32
            tcg_gen_add_i32(addr, REG(B7_4), REG(0));
1132 cc4ba6a9 aurel32
            if (ctx->fpscr & FPSCR_SZ) {
1133 11bb09f1 aurel32
                int fr = XREG(B11_8);
1134 11bb09f1 aurel32
                tcg_gen_qemu_ld32u(cpu_fregs[fr         ], addr, ctx->memidx);
1135 11bb09f1 aurel32
                tcg_gen_addi_i32(addr, addr, 4);
1136 11bb09f1 aurel32
                tcg_gen_qemu_ld32u(cpu_fregs[fr+1], addr, ctx->memidx);
1137 cc4ba6a9 aurel32
            } else {
1138 66ba317c aurel32
                tcg_gen_qemu_ld32u(cpu_fregs[FREG(B11_8)], addr, ctx->memidx);
1139 cc4ba6a9 aurel32
            }
1140 cc4ba6a9 aurel32
            tcg_temp_free(addr);
1141 eda9b09b bellard
        }
1142 eda9b09b bellard
        return;
1143 e67888a7 ths
    case 0xf007: /* fmov {F,D,X}Rn,@(R0,Rn) - FPSCR: Nothing */
1144 f6198371 aurel32
        CHECK_FPU_ENABLED
1145 cc4ba6a9 aurel32
        {
1146 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1147 cc4ba6a9 aurel32
            tcg_gen_add_i32(addr, REG(B11_8), REG(0));
1148 cc4ba6a9 aurel32
            if (ctx->fpscr & FPSCR_SZ) {
1149 11bb09f1 aurel32
                int fr = XREG(B7_4);
1150 11bb09f1 aurel32
                tcg_gen_qemu_ld32u(cpu_fregs[fr         ], addr, ctx->memidx);
1151 11bb09f1 aurel32
                tcg_gen_addi_i32(addr, addr, 4);
1152 11bb09f1 aurel32
                tcg_gen_qemu_ld32u(cpu_fregs[fr+1], addr, ctx->memidx);
1153 cc4ba6a9 aurel32
            } else {
1154 66ba317c aurel32
                tcg_gen_qemu_st32(cpu_fregs[FREG(B7_4)], addr, ctx->memidx);
1155 cc4ba6a9 aurel32
            }
1156 cc4ba6a9 aurel32
            tcg_temp_free(addr);
1157 eda9b09b bellard
        }
1158 eda9b09b bellard
        return;
1159 e67888a7 ths
    case 0xf000: /* fadd Rm,Rn - FPSCR: R[PR,Enable.O/U/I]/W[Cause,Flag] */
1160 e67888a7 ths
    case 0xf001: /* fsub Rm,Rn - FPSCR: R[PR,Enable.O/U/I]/W[Cause,Flag] */
1161 e67888a7 ths
    case 0xf002: /* fmul Rm,Rn - FPSCR: R[PR,Enable.O/U/I]/W[Cause,Flag] */
1162 e67888a7 ths
    case 0xf003: /* fdiv Rm,Rn - FPSCR: R[PR,Enable.O/U/I]/W[Cause,Flag] */
1163 e67888a7 ths
    case 0xf004: /* fcmp/eq Rm,Rn - FPSCR: R[PR,Enable.V]/W[Cause,Flag] */
1164 e67888a7 ths
    case 0xf005: /* fcmp/gt Rm,Rn - FPSCR: R[PR,Enable.V]/W[Cause,Flag] */
1165 cc4ba6a9 aurel32
        {
1166 f6198371 aurel32
            CHECK_FPU_ENABLED
1167 cc4ba6a9 aurel32
            if (ctx->fpscr & FPSCR_PR) {
1168 a7812ae4 pbrook
                TCGv_i64 fp0, fp1;
1169 a7812ae4 pbrook
1170 cc4ba6a9 aurel32
                if (ctx->opcode & 0x0110)
1171 cc4ba6a9 aurel32
                    break; /* illegal instruction */
1172 a7812ae4 pbrook
                fp0 = tcg_temp_new_i64();
1173 a7812ae4 pbrook
                fp1 = tcg_temp_new_i64();
1174 cc4ba6a9 aurel32
                gen_load_fpr64(fp0, DREG(B11_8));
1175 cc4ba6a9 aurel32
                gen_load_fpr64(fp1, DREG(B7_4));
1176 a7812ae4 pbrook
                switch (ctx->opcode & 0xf00f) {
1177 a7812ae4 pbrook
                case 0xf000:                /* fadd Rm,Rn */
1178 a7812ae4 pbrook
                    gen_helper_fadd_DT(fp0, fp0, fp1);
1179 a7812ae4 pbrook
                    break;
1180 a7812ae4 pbrook
                case 0xf001:                /* fsub Rm,Rn */
1181 a7812ae4 pbrook
                    gen_helper_fsub_DT(fp0, fp0, fp1);
1182 a7812ae4 pbrook
                    break;
1183 a7812ae4 pbrook
                case 0xf002:                /* fmul Rm,Rn */
1184 a7812ae4 pbrook
                    gen_helper_fmul_DT(fp0, fp0, fp1);
1185 a7812ae4 pbrook
                    break;
1186 a7812ae4 pbrook
                case 0xf003:                /* fdiv Rm,Rn */
1187 a7812ae4 pbrook
                    gen_helper_fdiv_DT(fp0, fp0, fp1);
1188 a7812ae4 pbrook
                    break;
1189 a7812ae4 pbrook
                case 0xf004:                /* fcmp/eq Rm,Rn */
1190 a7812ae4 pbrook
                    gen_helper_fcmp_eq_DT(fp0, fp1);
1191 a7812ae4 pbrook
                    return;
1192 a7812ae4 pbrook
                case 0xf005:                /* fcmp/gt Rm,Rn */
1193 a7812ae4 pbrook
                    gen_helper_fcmp_gt_DT(fp0, fp1);
1194 a7812ae4 pbrook
                    return;
1195 a7812ae4 pbrook
                }
1196 a7812ae4 pbrook
                gen_store_fpr64(fp0, DREG(B11_8));
1197 a7812ae4 pbrook
                tcg_temp_free_i64(fp0);
1198 a7812ae4 pbrook
                tcg_temp_free_i64(fp1);
1199 a7812ae4 pbrook
            } else {
1200 a7812ae4 pbrook
                switch (ctx->opcode & 0xf00f) {
1201 a7812ae4 pbrook
                case 0xf000:                /* fadd Rm,Rn */
1202 66ba317c aurel32
                    gen_helper_fadd_FT(cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B7_4)]);
1203 a7812ae4 pbrook
                    break;
1204 a7812ae4 pbrook
                case 0xf001:                /* fsub Rm,Rn */
1205 66ba317c aurel32
                    gen_helper_fsub_FT(cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B7_4)]);
1206 a7812ae4 pbrook
                    break;
1207 a7812ae4 pbrook
                case 0xf002:                /* fmul Rm,Rn */
1208 66ba317c aurel32
                    gen_helper_fmul_FT(cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B7_4)]);
1209 a7812ae4 pbrook
                    break;
1210 a7812ae4 pbrook
                case 0xf003:                /* fdiv Rm,Rn */
1211 66ba317c aurel32
                    gen_helper_fdiv_FT(cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B7_4)]);
1212 a7812ae4 pbrook
                    break;
1213 a7812ae4 pbrook
                case 0xf004:                /* fcmp/eq Rm,Rn */
1214 66ba317c aurel32
                    gen_helper_fcmp_eq_FT(cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B7_4)]);
1215 a7812ae4 pbrook
                    return;
1216 a7812ae4 pbrook
                case 0xf005:                /* fcmp/gt Rm,Rn */
1217 66ba317c aurel32
                    gen_helper_fcmp_gt_FT(cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B7_4)]);
1218 a7812ae4 pbrook
                    return;
1219 a7812ae4 pbrook
                }
1220 cc4ba6a9 aurel32
            }
1221 ea6cf6be ths
        }
1222 ea6cf6be ths
        return;
1223 5b7141a1 aurel32
    case 0xf00e: /* fmac FR0,RM,Rn */
1224 5b7141a1 aurel32
        {
1225 5b7141a1 aurel32
            CHECK_FPU_ENABLED
1226 5b7141a1 aurel32
            if (ctx->fpscr & FPSCR_PR) {
1227 5b7141a1 aurel32
                break; /* illegal instruction */
1228 5b7141a1 aurel32
            } else {
1229 5b7141a1 aurel32
                gen_helper_fmac_FT(cpu_fregs[FREG(B11_8)],
1230 5b7141a1 aurel32
                                   cpu_fregs[FREG(0)], cpu_fregs[FREG(B7_4)], cpu_fregs[FREG(B11_8)]);
1231 5b7141a1 aurel32
                return;
1232 5b7141a1 aurel32
            }
1233 5b7141a1 aurel32
        }
1234 fdf9b3e8 bellard
    }
1235 fdf9b3e8 bellard
1236 fdf9b3e8 bellard
    switch (ctx->opcode & 0xff00) {
1237 fdf9b3e8 bellard
    case 0xc900:                /* and #imm,R0 */
1238 7efbe241 aurel32
        tcg_gen_andi_i32(REG(0), REG(0), B7_0);
1239 fdf9b3e8 bellard
        return;
1240 24988dc2 aurel32
    case 0xcd00:                /* and.b #imm,@(R0,GBR) */
1241 c55497ec aurel32
        {
1242 c55497ec aurel32
            TCGv addr, val;
1243 a7812ae4 pbrook
            addr = tcg_temp_new();
1244 c55497ec aurel32
            tcg_gen_add_i32(addr, REG(0), cpu_gbr);
1245 a7812ae4 pbrook
            val = tcg_temp_new();
1246 c55497ec aurel32
            tcg_gen_qemu_ld8u(val, addr, ctx->memidx);
1247 c55497ec aurel32
            tcg_gen_andi_i32(val, val, B7_0);
1248 c55497ec aurel32
            tcg_gen_qemu_st8(val, addr, ctx->memidx);
1249 c55497ec aurel32
            tcg_temp_free(val);
1250 c55497ec aurel32
            tcg_temp_free(addr);
1251 c55497ec aurel32
        }
1252 fdf9b3e8 bellard
        return;
1253 fdf9b3e8 bellard
    case 0x8b00:                /* bf label */
1254 fdf9b3e8 bellard
        CHECK_NOT_DELAY_SLOT
1255 fdf9b3e8 bellard
            gen_conditional_jump(ctx, ctx->pc + 2,
1256 fdf9b3e8 bellard
                                 ctx->pc + 4 + B7_0s * 2);
1257 823029f9 ths
        ctx->bstate = BS_BRANCH;
1258 fdf9b3e8 bellard
        return;
1259 fdf9b3e8 bellard
    case 0x8f00:                /* bf/s label */
1260 fdf9b3e8 bellard
        CHECK_NOT_DELAY_SLOT
1261 1000822b aurel32
        gen_branch_slot(ctx->delayed_pc = ctx->pc + 4 + B7_0s * 2, 0);
1262 fdf9b3e8 bellard
        ctx->flags |= DELAY_SLOT_CONDITIONAL;
1263 fdf9b3e8 bellard
        return;
1264 fdf9b3e8 bellard
    case 0x8900:                /* bt label */
1265 fdf9b3e8 bellard
        CHECK_NOT_DELAY_SLOT
1266 fdf9b3e8 bellard
            gen_conditional_jump(ctx, ctx->pc + 4 + B7_0s * 2,
1267 fdf9b3e8 bellard
                                 ctx->pc + 2);
1268 823029f9 ths
        ctx->bstate = BS_BRANCH;
1269 fdf9b3e8 bellard
        return;
1270 fdf9b3e8 bellard
    case 0x8d00:                /* bt/s label */
1271 fdf9b3e8 bellard
        CHECK_NOT_DELAY_SLOT
1272 1000822b aurel32
        gen_branch_slot(ctx->delayed_pc = ctx->pc + 4 + B7_0s * 2, 1);
1273 fdf9b3e8 bellard
        ctx->flags |= DELAY_SLOT_CONDITIONAL;
1274 fdf9b3e8 bellard
        return;
1275 fdf9b3e8 bellard
    case 0x8800:                /* cmp/eq #imm,R0 */
1276 7efbe241 aurel32
        gen_cmp_imm(TCG_COND_EQ, REG(0), B7_0s);
1277 fdf9b3e8 bellard
        return;
1278 fdf9b3e8 bellard
    case 0xc400:                /* mov.b @(disp,GBR),R0 */
1279 c55497ec aurel32
        {
1280 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1281 c55497ec aurel32
            tcg_gen_addi_i32(addr, cpu_gbr, B7_0);
1282 c55497ec aurel32
            tcg_gen_qemu_ld8s(REG(0), addr, ctx->memidx);
1283 c55497ec aurel32
            tcg_temp_free(addr);
1284 c55497ec aurel32
        }
1285 fdf9b3e8 bellard
        return;
1286 fdf9b3e8 bellard
    case 0xc500:                /* mov.w @(disp,GBR),R0 */
1287 c55497ec aurel32
        {
1288 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1289 c55497ec aurel32
            tcg_gen_addi_i32(addr, cpu_gbr, B7_0 * 2);
1290 c55497ec aurel32
            tcg_gen_qemu_ld16s(REG(0), addr, ctx->memidx);
1291 c55497ec aurel32
            tcg_temp_free(addr);
1292 c55497ec aurel32
        }
1293 fdf9b3e8 bellard
        return;
1294 fdf9b3e8 bellard
    case 0xc600:                /* mov.l @(disp,GBR),R0 */
1295 c55497ec aurel32
        {
1296 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1297 c55497ec aurel32
            tcg_gen_addi_i32(addr, cpu_gbr, B7_0 * 4);
1298 c55497ec aurel32
            tcg_gen_qemu_ld32s(REG(0), addr, ctx->memidx);
1299 c55497ec aurel32
            tcg_temp_free(addr);
1300 c55497ec aurel32
        }
1301 fdf9b3e8 bellard
        return;
1302 fdf9b3e8 bellard
    case 0xc000:                /* mov.b R0,@(disp,GBR) */
1303 c55497ec aurel32
        {
1304 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1305 c55497ec aurel32
            tcg_gen_addi_i32(addr, cpu_gbr, B7_0);
1306 c55497ec aurel32
            tcg_gen_qemu_st8(REG(0), addr, ctx->memidx);
1307 c55497ec aurel32
            tcg_temp_free(addr);
1308 c55497ec aurel32
        }
1309 fdf9b3e8 bellard
        return;
1310 fdf9b3e8 bellard
    case 0xc100:                /* mov.w R0,@(disp,GBR) */
1311 c55497ec aurel32
        {
1312 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1313 c55497ec aurel32
            tcg_gen_addi_i32(addr, cpu_gbr, B7_0 * 2);
1314 c55497ec aurel32
            tcg_gen_qemu_st16(REG(0), addr, ctx->memidx);
1315 c55497ec aurel32
            tcg_temp_free(addr);
1316 c55497ec aurel32
        }
1317 fdf9b3e8 bellard
        return;
1318 fdf9b3e8 bellard
    case 0xc200:                /* mov.l R0,@(disp,GBR) */
1319 c55497ec aurel32
        {
1320 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1321 c55497ec aurel32
            tcg_gen_addi_i32(addr, cpu_gbr, B7_0 * 4);
1322 c55497ec aurel32
            tcg_gen_qemu_st32(REG(0), addr, ctx->memidx);
1323 c55497ec aurel32
            tcg_temp_free(addr);
1324 c55497ec aurel32
        }
1325 fdf9b3e8 bellard
        return;
1326 fdf9b3e8 bellard
    case 0x8000:                /* mov.b R0,@(disp,Rn) */
1327 c55497ec aurel32
        {
1328 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1329 c55497ec aurel32
            tcg_gen_addi_i32(addr, REG(B7_4), B3_0);
1330 c55497ec aurel32
            tcg_gen_qemu_st8(REG(0), addr, ctx->memidx);
1331 c55497ec aurel32
            tcg_temp_free(addr);
1332 c55497ec aurel32
        }
1333 fdf9b3e8 bellard
        return;
1334 fdf9b3e8 bellard
    case 0x8100:                /* mov.w R0,@(disp,Rn) */
1335 c55497ec aurel32
        {
1336 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1337 c55497ec aurel32
            tcg_gen_addi_i32(addr, REG(B7_4), B3_0 * 2);
1338 c55497ec aurel32
            tcg_gen_qemu_st16(REG(0), addr, ctx->memidx);
1339 c55497ec aurel32
            tcg_temp_free(addr);
1340 c55497ec aurel32
        }
1341 fdf9b3e8 bellard
        return;
1342 fdf9b3e8 bellard
    case 0x8400:                /* mov.b @(disp,Rn),R0 */
1343 c55497ec aurel32
        {
1344 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1345 c55497ec aurel32
            tcg_gen_addi_i32(addr, REG(B7_4), B3_0);
1346 c55497ec aurel32
            tcg_gen_qemu_ld8s(REG(0), addr, ctx->memidx);
1347 c55497ec aurel32
            tcg_temp_free(addr);
1348 c55497ec aurel32
        }
1349 fdf9b3e8 bellard
        return;
1350 fdf9b3e8 bellard
    case 0x8500:                /* mov.w @(disp,Rn),R0 */
1351 c55497ec aurel32
        {
1352 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1353 c55497ec aurel32
            tcg_gen_addi_i32(addr, REG(B7_4), B3_0 * 2);
1354 c55497ec aurel32
            tcg_gen_qemu_ld16s(REG(0), addr, ctx->memidx);
1355 c55497ec aurel32
            tcg_temp_free(addr);
1356 c55497ec aurel32
        }
1357 fdf9b3e8 bellard
        return;
1358 fdf9b3e8 bellard
    case 0xc700:                /* mova @(disp,PC),R0 */
1359 7efbe241 aurel32
        tcg_gen_movi_i32(REG(0), ((ctx->pc & 0xfffffffc) + 4 + B7_0 * 4) & ~3);
1360 fdf9b3e8 bellard
        return;
1361 fdf9b3e8 bellard
    case 0xcb00:                /* or #imm,R0 */
1362 7efbe241 aurel32
        tcg_gen_ori_i32(REG(0), REG(0), B7_0);
1363 fdf9b3e8 bellard
        return;
1364 24988dc2 aurel32
    case 0xcf00:                /* or.b #imm,@(R0,GBR) */
1365 c55497ec aurel32
        {
1366 c55497ec aurel32
            TCGv addr, val;
1367 a7812ae4 pbrook
            addr = tcg_temp_new();
1368 c55497ec aurel32
            tcg_gen_add_i32(addr, REG(0), cpu_gbr);
1369 a7812ae4 pbrook
            val = tcg_temp_new();
1370 c55497ec aurel32
            tcg_gen_qemu_ld8u(val, addr, ctx->memidx);
1371 c55497ec aurel32
            tcg_gen_ori_i32(val, val, B7_0);
1372 c55497ec aurel32
            tcg_gen_qemu_st8(val, addr, ctx->memidx);
1373 c55497ec aurel32
            tcg_temp_free(val);
1374 c55497ec aurel32
            tcg_temp_free(addr);
1375 c55497ec aurel32
        }
1376 fdf9b3e8 bellard
        return;
1377 fdf9b3e8 bellard
    case 0xc300:                /* trapa #imm */
1378 c55497ec aurel32
        {
1379 c55497ec aurel32
            TCGv imm;
1380 c55497ec aurel32
            CHECK_NOT_DELAY_SLOT
1381 c55497ec aurel32
            imm = tcg_const_i32(B7_0);
1382 a7812ae4 pbrook
            gen_helper_trapa(imm);
1383 c55497ec aurel32
            tcg_temp_free(imm);
1384 c55497ec aurel32
            ctx->bstate = BS_BRANCH;
1385 c55497ec aurel32
        }
1386 fdf9b3e8 bellard
        return;
1387 fdf9b3e8 bellard
    case 0xc800:                /* tst #imm,R0 */
1388 c55497ec aurel32
        {
1389 a7812ae4 pbrook
            TCGv val = tcg_temp_new();
1390 c55497ec aurel32
            tcg_gen_andi_i32(val, REG(0), B7_0);
1391 c55497ec aurel32
            gen_cmp_imm(TCG_COND_EQ, val, 0);
1392 c55497ec aurel32
            tcg_temp_free(val);
1393 c55497ec aurel32
        }
1394 fdf9b3e8 bellard
        return;
1395 24988dc2 aurel32
    case 0xcc00:                /* tst.b #imm,@(R0,GBR) */
1396 c55497ec aurel32
        {
1397 a7812ae4 pbrook
            TCGv val = tcg_temp_new();
1398 c55497ec aurel32
            tcg_gen_add_i32(val, REG(0), cpu_gbr);
1399 c55497ec aurel32
            tcg_gen_qemu_ld8u(val, val, ctx->memidx);
1400 c55497ec aurel32
            tcg_gen_andi_i32(val, val, B7_0);
1401 c55497ec aurel32
            gen_cmp_imm(TCG_COND_EQ, val, 0);
1402 c55497ec aurel32
            tcg_temp_free(val);
1403 c55497ec aurel32
        }
1404 fdf9b3e8 bellard
        return;
1405 fdf9b3e8 bellard
    case 0xca00:                /* xor #imm,R0 */
1406 7efbe241 aurel32
        tcg_gen_xori_i32(REG(0), REG(0), B7_0);
1407 fdf9b3e8 bellard
        return;
1408 24988dc2 aurel32
    case 0xce00:                /* xor.b #imm,@(R0,GBR) */
1409 c55497ec aurel32
        {
1410 c55497ec aurel32
            TCGv addr, val;
1411 a7812ae4 pbrook
            addr = tcg_temp_new();
1412 c55497ec aurel32
            tcg_gen_add_i32(addr, REG(0), cpu_gbr);
1413 a7812ae4 pbrook
            val = tcg_temp_new();
1414 c55497ec aurel32
            tcg_gen_qemu_ld8u(val, addr, ctx->memidx);
1415 c55497ec aurel32
            tcg_gen_xori_i32(val, val, B7_0);
1416 c55497ec aurel32
            tcg_gen_qemu_st8(val, addr, ctx->memidx);
1417 c55497ec aurel32
            tcg_temp_free(val);
1418 c55497ec aurel32
            tcg_temp_free(addr);
1419 c55497ec aurel32
        }
1420 fdf9b3e8 bellard
        return;
1421 fdf9b3e8 bellard
    }
1422 fdf9b3e8 bellard
1423 fdf9b3e8 bellard
    switch (ctx->opcode & 0xf08f) {
1424 fdf9b3e8 bellard
    case 0x408e:                /* ldc Rm,Rn_BANK */
1425 fe25591e aurel32
        CHECK_PRIVILEGED
1426 7efbe241 aurel32
        tcg_gen_mov_i32(ALTREG(B6_4), REG(B11_8));
1427 fdf9b3e8 bellard
        return;
1428 fdf9b3e8 bellard
    case 0x4087:                /* ldc.l @Rm+,Rn_BANK */
1429 fe25591e aurel32
        CHECK_PRIVILEGED
1430 7efbe241 aurel32
        tcg_gen_qemu_ld32s(ALTREG(B6_4), REG(B11_8), ctx->memidx);
1431 7efbe241 aurel32
        tcg_gen_addi_i32(REG(B11_8), REG(B11_8), 4);
1432 fdf9b3e8 bellard
        return;
1433 fdf9b3e8 bellard
    case 0x0082:                /* stc Rm_BANK,Rn */
1434 fe25591e aurel32
        CHECK_PRIVILEGED
1435 7efbe241 aurel32
        tcg_gen_mov_i32(REG(B11_8), ALTREG(B6_4));
1436 fdf9b3e8 bellard
        return;
1437 fdf9b3e8 bellard
    case 0x4083:                /* stc.l Rm_BANK,@-Rn */
1438 fe25591e aurel32
        CHECK_PRIVILEGED
1439 c55497ec aurel32
        {
1440 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1441 c55497ec aurel32
            tcg_gen_subi_i32(addr, REG(B11_8), 4);
1442 c55497ec aurel32
            tcg_gen_qemu_st32(ALTREG(B6_4), addr, ctx->memidx);
1443 3101e99c Aurelien Jarno
            tcg_gen_mov_i32(REG(B11_8), addr);
1444 c55497ec aurel32
            tcg_temp_free(addr);
1445 c55497ec aurel32
        }
1446 fdf9b3e8 bellard
        return;
1447 fdf9b3e8 bellard
    }
1448 fdf9b3e8 bellard
1449 fdf9b3e8 bellard
    switch (ctx->opcode & 0xf0ff) {
1450 fdf9b3e8 bellard
    case 0x0023:                /* braf Rn */
1451 7efbe241 aurel32
        CHECK_NOT_DELAY_SLOT
1452 7efbe241 aurel32
        tcg_gen_addi_i32(cpu_delayed_pc, REG(B11_8), ctx->pc + 4);
1453 fdf9b3e8 bellard
        ctx->flags |= DELAY_SLOT;
1454 fdf9b3e8 bellard
        ctx->delayed_pc = (uint32_t) - 1;
1455 fdf9b3e8 bellard
        return;
1456 fdf9b3e8 bellard
    case 0x0003:                /* bsrf Rn */
1457 7efbe241 aurel32
        CHECK_NOT_DELAY_SLOT
1458 1000822b aurel32
        tcg_gen_movi_i32(cpu_pr, ctx->pc + 4);
1459 7efbe241 aurel32
        tcg_gen_add_i32(cpu_delayed_pc, REG(B11_8), cpu_pr);
1460 fdf9b3e8 bellard
        ctx->flags |= DELAY_SLOT;
1461 fdf9b3e8 bellard
        ctx->delayed_pc = (uint32_t) - 1;
1462 fdf9b3e8 bellard
        return;
1463 fdf9b3e8 bellard
    case 0x4015:                /* cmp/pl Rn */
1464 7efbe241 aurel32
        gen_cmp_imm(TCG_COND_GT, REG(B11_8), 0);
1465 fdf9b3e8 bellard
        return;
1466 fdf9b3e8 bellard
    case 0x4011:                /* cmp/pz Rn */
1467 7efbe241 aurel32
        gen_cmp_imm(TCG_COND_GE, REG(B11_8), 0);
1468 fdf9b3e8 bellard
        return;
1469 fdf9b3e8 bellard
    case 0x4010:                /* dt Rn */
1470 7efbe241 aurel32
        tcg_gen_subi_i32(REG(B11_8), REG(B11_8), 1);
1471 7efbe241 aurel32
        gen_cmp_imm(TCG_COND_EQ, REG(B11_8), 0);
1472 fdf9b3e8 bellard
        return;
1473 fdf9b3e8 bellard
    case 0x402b:                /* jmp @Rn */
1474 7efbe241 aurel32
        CHECK_NOT_DELAY_SLOT
1475 7efbe241 aurel32
        tcg_gen_mov_i32(cpu_delayed_pc, REG(B11_8));
1476 fdf9b3e8 bellard
        ctx->flags |= DELAY_SLOT;
1477 fdf9b3e8 bellard
        ctx->delayed_pc = (uint32_t) - 1;
1478 fdf9b3e8 bellard
        return;
1479 fdf9b3e8 bellard
    case 0x400b:                /* jsr @Rn */
1480 7efbe241 aurel32
        CHECK_NOT_DELAY_SLOT
1481 1000822b aurel32
        tcg_gen_movi_i32(cpu_pr, ctx->pc + 4);
1482 7efbe241 aurel32
        tcg_gen_mov_i32(cpu_delayed_pc, REG(B11_8));
1483 fdf9b3e8 bellard
        ctx->flags |= DELAY_SLOT;
1484 fdf9b3e8 bellard
        ctx->delayed_pc = (uint32_t) - 1;
1485 fdf9b3e8 bellard
        return;
1486 fe25591e aurel32
    case 0x400e:                /* ldc Rm,SR */
1487 fe25591e aurel32
        CHECK_PRIVILEGED
1488 7efbe241 aurel32
        tcg_gen_andi_i32(cpu_sr, REG(B11_8), 0x700083f3);
1489 390af821 aurel32
        ctx->bstate = BS_STOP;
1490 390af821 aurel32
        return;
1491 fe25591e aurel32
    case 0x4007:                /* ldc.l @Rm+,SR */
1492 fe25591e aurel32
        CHECK_PRIVILEGED
1493 c55497ec aurel32
        {
1494 a7812ae4 pbrook
            TCGv val = tcg_temp_new();
1495 c55497ec aurel32
            tcg_gen_qemu_ld32s(val, REG(B11_8), ctx->memidx);
1496 c55497ec aurel32
            tcg_gen_andi_i32(cpu_sr, val, 0x700083f3);
1497 c55497ec aurel32
            tcg_temp_free(val);
1498 c55497ec aurel32
            tcg_gen_addi_i32(REG(B11_8), REG(B11_8), 4);
1499 c55497ec aurel32
            ctx->bstate = BS_STOP;
1500 c55497ec aurel32
        }
1501 390af821 aurel32
        return;
1502 fe25591e aurel32
    case 0x0002:                /* stc SR,Rn */
1503 fe25591e aurel32
        CHECK_PRIVILEGED
1504 7efbe241 aurel32
        tcg_gen_mov_i32(REG(B11_8), cpu_sr);
1505 390af821 aurel32
        return;
1506 fe25591e aurel32
    case 0x4003:                /* stc SR,@-Rn */
1507 fe25591e aurel32
        CHECK_PRIVILEGED
1508 c55497ec aurel32
        {
1509 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1510 c55497ec aurel32
            tcg_gen_subi_i32(addr, REG(B11_8), 4);
1511 c55497ec aurel32
            tcg_gen_qemu_st32(cpu_sr, addr, ctx->memidx);
1512 3101e99c Aurelien Jarno
            tcg_gen_mov_i32(REG(B11_8), addr);
1513 c55497ec aurel32
            tcg_temp_free(addr);
1514 c55497ec aurel32
        }
1515 390af821 aurel32
        return;
1516 8e9b0678 Alexandre Courbot
#define LD(reg,ldnum,ldpnum,prechk)                \
1517 fdf9b3e8 bellard
  case ldnum:                                                        \
1518 fe25591e aurel32
    prechk                                                            \
1519 7efbe241 aurel32
    tcg_gen_mov_i32 (cpu_##reg, REG(B11_8));                        \
1520 fdf9b3e8 bellard
    return;                                                        \
1521 fdf9b3e8 bellard
  case ldpnum:                                                        \
1522 fe25591e aurel32
    prechk                                                            \
1523 7efbe241 aurel32
    tcg_gen_qemu_ld32s (cpu_##reg, REG(B11_8), ctx->memidx);        \
1524 7efbe241 aurel32
    tcg_gen_addi_i32(REG(B11_8), REG(B11_8), 4);                \
1525 8e9b0678 Alexandre Courbot
    return;
1526 8e9b0678 Alexandre Courbot
#define ST(reg,stnum,stpnum,prechk)                \
1527 fdf9b3e8 bellard
  case stnum:                                                        \
1528 fe25591e aurel32
    prechk                                                            \
1529 7efbe241 aurel32
    tcg_gen_mov_i32 (REG(B11_8), cpu_##reg);                        \
1530 fdf9b3e8 bellard
    return;                                                        \
1531 fdf9b3e8 bellard
  case stpnum:                                                        \
1532 fe25591e aurel32
    prechk                                                            \
1533 c55497ec aurel32
    {                                                                \
1534 3101e99c Aurelien Jarno
        TCGv addr = tcg_temp_new();                                \
1535 c55497ec aurel32
        tcg_gen_subi_i32(addr, REG(B11_8), 4);                        \
1536 c55497ec aurel32
        tcg_gen_qemu_st32 (cpu_##reg, addr, ctx->memidx);        \
1537 3101e99c Aurelien Jarno
        tcg_gen_mov_i32(REG(B11_8), addr);                        \
1538 c55497ec aurel32
        tcg_temp_free(addr);                                        \
1539 86e0abc7 aurel32
    }                                                                \
1540 fdf9b3e8 bellard
    return;
1541 8e9b0678 Alexandre Courbot
#define LDST(reg,ldnum,ldpnum,stnum,stpnum,prechk)                \
1542 8e9b0678 Alexandre Courbot
        LD(reg,ldnum,ldpnum,prechk)                                \
1543 8e9b0678 Alexandre Courbot
        ST(reg,stnum,stpnum,prechk)
1544 fe25591e aurel32
        LDST(gbr,  0x401e, 0x4017, 0x0012, 0x4013, {})
1545 fe25591e aurel32
        LDST(vbr,  0x402e, 0x4027, 0x0022, 0x4023, CHECK_PRIVILEGED)
1546 fe25591e aurel32
        LDST(ssr,  0x403e, 0x4037, 0x0032, 0x4033, CHECK_PRIVILEGED)
1547 fe25591e aurel32
        LDST(spc,  0x404e, 0x4047, 0x0042, 0x4043, CHECK_PRIVILEGED)
1548 935fc175 Alexandre Courbot
        ST(sgr,  0x003a, 0x4032, CHECK_PRIVILEGED)
1549 935fc175 Alexandre Courbot
        LD(sgr,  0x403a, 0x4036, CHECK_PRIVILEGED if (!(ctx->features & SH_FEATURE_SH4A)) break;)
1550 fe25591e aurel32
        LDST(dbr,  0x40fa, 0x40f6, 0x00fa, 0x40f2, CHECK_PRIVILEGED)
1551 fe25591e aurel32
        LDST(mach, 0x400a, 0x4006, 0x000a, 0x4002, {})
1552 fe25591e aurel32
        LDST(macl, 0x401a, 0x4016, 0x001a, 0x4012, {})
1553 fe25591e aurel32
        LDST(pr,   0x402a, 0x4026, 0x002a, 0x4022, {})
1554 d8299bcc aurel32
        LDST(fpul, 0x405a, 0x4056, 0x005a, 0x4052, {CHECK_FPU_ENABLED})
1555 390af821 aurel32
    case 0x406a:                /* lds Rm,FPSCR */
1556 d8299bcc aurel32
        CHECK_FPU_ENABLED
1557 a7812ae4 pbrook
        gen_helper_ld_fpscr(REG(B11_8));
1558 390af821 aurel32
        ctx->bstate = BS_STOP;
1559 390af821 aurel32
        return;
1560 390af821 aurel32
    case 0x4066:                /* lds.l @Rm+,FPSCR */
1561 d8299bcc aurel32
        CHECK_FPU_ENABLED
1562 c55497ec aurel32
        {
1563 a7812ae4 pbrook
            TCGv addr = tcg_temp_new();
1564 c55497ec aurel32
            tcg_gen_qemu_ld32s(addr, REG(B11_8), ctx->memidx);
1565 c55497ec aurel32
            tcg_gen_addi_i32(REG(B11_8), REG(B11_8), 4);
1566 a7812ae4 pbrook
            gen_helper_ld_fpscr(addr);
1567 c55497ec aurel32
            tcg_temp_free(addr);
1568 c55497ec aurel32
            ctx->bstate = BS_STOP;
1569 c55497ec aurel32
        }
1570 390af821 aurel32
        return;
1571 390af821 aurel32
    case 0x006a:                /* sts FPSCR,Rn */
1572 d8299bcc aurel32
        CHECK_FPU_ENABLED
1573 c55497ec aurel32
        tcg_gen_andi_i32(REG(B11_8), cpu_fpscr, 0x003fffff);
1574 390af821 aurel32
        return;
1575 390af821 aurel32
    case 0x4062:                /* sts FPSCR,@-Rn */
1576 d8299bcc aurel32
        CHECK_FPU_ENABLED
1577 c55497ec aurel32
        {
1578 c55497ec aurel32
            TCGv addr, val;
1579 a7812ae4 pbrook
            val = tcg_temp_new();
1580 c55497ec aurel32
            tcg_gen_andi_i32(val, cpu_fpscr, 0x003fffff);
1581 a7812ae4 pbrook
            addr = tcg_temp_new();
1582 c55497ec aurel32
            tcg_gen_subi_i32(addr, REG(B11_8), 4);
1583 c55497ec aurel32
            tcg_gen_qemu_st32(val, addr, ctx->memidx);
1584 3101e99c Aurelien Jarno
            tcg_gen_mov_i32(REG(B11_8), addr);
1585 c55497ec aurel32
            tcg_temp_free(addr);
1586 c55497ec aurel32
            tcg_temp_free(val);
1587 c55497ec aurel32
        }
1588 390af821 aurel32
        return;
1589 fdf9b3e8 bellard
    case 0x00c3:                /* movca.l R0,@Rm */
1590 852d481f edgar_igl
        {
1591 852d481f edgar_igl
            TCGv val = tcg_temp_new();
1592 852d481f edgar_igl
            tcg_gen_qemu_ld32u(val, REG(B11_8), ctx->memidx);
1593 852d481f edgar_igl
            gen_helper_movcal (REG(B11_8), val);            
1594 852d481f edgar_igl
            tcg_gen_qemu_st32(REG(0), REG(B11_8), ctx->memidx);
1595 852d481f edgar_igl
        }
1596 852d481f edgar_igl
        ctx->has_movcal = 1;
1597 fdf9b3e8 bellard
        return;
1598 7526aa2d aurel32
    case 0x40a9:
1599 7526aa2d aurel32
        /* MOVUA.L @Rm,R0 (Rm) -> R0
1600 7526aa2d aurel32
           Load non-boundary-aligned data */
1601 7526aa2d aurel32
        tcg_gen_qemu_ld32u(REG(0), REG(B11_8), ctx->memidx);
1602 7526aa2d aurel32
        return;
1603 7526aa2d aurel32
    case 0x40e9:
1604 7526aa2d aurel32
        /* MOVUA.L @Rm+,R0   (Rm) -> R0, Rm + 4 -> Rm
1605 7526aa2d aurel32
           Load non-boundary-aligned data */
1606 7526aa2d aurel32
        tcg_gen_qemu_ld32u(REG(0), REG(B11_8), ctx->memidx);
1607 7526aa2d aurel32
        tcg_gen_addi_i32(REG(B11_8), REG(B11_8), 4);
1608 7526aa2d aurel32
        return;
1609 fdf9b3e8 bellard
    case 0x0029:                /* movt Rn */
1610 7efbe241 aurel32
        tcg_gen_andi_i32(REG(B11_8), cpu_sr, SR_T);
1611 fdf9b3e8 bellard
        return;
1612 66c7c806 aurel32
    case 0x0073:
1613 66c7c806 aurel32
        /* MOVCO.L
1614 66c7c806 aurel32
               LDST -> T
1615 66c7c806 aurel32
               If (T == 1) R0 -> (Rn)
1616 66c7c806 aurel32
               0 -> LDST
1617 66c7c806 aurel32
        */
1618 66c7c806 aurel32
        if (ctx->features & SH_FEATURE_SH4A) {
1619 66c7c806 aurel32
            int label = gen_new_label();
1620 66c7c806 aurel32
            gen_clr_t();
1621 66c7c806 aurel32
            tcg_gen_or_i32(cpu_sr, cpu_sr, cpu_ldst);
1622 66c7c806 aurel32
            tcg_gen_brcondi_i32(TCG_COND_EQ, cpu_ldst, 0, label);
1623 66c7c806 aurel32
            tcg_gen_qemu_st32(REG(0), REG(B11_8), ctx->memidx);
1624 66c7c806 aurel32
            gen_set_label(label);
1625 66c7c806 aurel32
            tcg_gen_movi_i32(cpu_ldst, 0);
1626 66c7c806 aurel32
            return;
1627 66c7c806 aurel32
        } else
1628 66c7c806 aurel32
            break;
1629 66c7c806 aurel32
    case 0x0063:
1630 66c7c806 aurel32
        /* MOVLI.L @Rm,R0
1631 66c7c806 aurel32
               1 -> LDST
1632 66c7c806 aurel32
               (Rm) -> R0
1633 66c7c806 aurel32
               When interrupt/exception
1634 66c7c806 aurel32
               occurred 0 -> LDST
1635 66c7c806 aurel32
        */
1636 66c7c806 aurel32
        if (ctx->features & SH_FEATURE_SH4A) {
1637 66c7c806 aurel32
            tcg_gen_movi_i32(cpu_ldst, 0);
1638 66c7c806 aurel32
            tcg_gen_qemu_ld32s(REG(0), REG(B11_8), ctx->memidx);
1639 66c7c806 aurel32
            tcg_gen_movi_i32(cpu_ldst, 1);
1640 66c7c806 aurel32
            return;
1641 66c7c806 aurel32
        } else
1642 66c7c806 aurel32
            break;
1643 fdf9b3e8 bellard
    case 0x0093:                /* ocbi @Rn */
1644 c55497ec aurel32
        {
1645 852d481f edgar_igl
            gen_helper_ocbi (REG(B11_8));
1646 c55497ec aurel32
        }
1647 fdf9b3e8 bellard
        return;
1648 24988dc2 aurel32
    case 0x00a3:                /* ocbp @Rn */
1649 fdf9b3e8 bellard
    case 0x00b3:                /* ocbwb @Rn */
1650 0cdb9554 Aurelien Jarno
        /* These instructions are supposed to do nothing in case of
1651 0cdb9554 Aurelien Jarno
           a cache miss. Given that we only partially emulate caches
1652 0cdb9554 Aurelien Jarno
           it is safe to simply ignore them. */
1653 fdf9b3e8 bellard
        return;
1654 fdf9b3e8 bellard
    case 0x0083:                /* pref @Rn */
1655 fdf9b3e8 bellard
        return;
1656 71968fa6 aurel32
    case 0x00d3:                /* prefi @Rn */
1657 71968fa6 aurel32
        if (ctx->features & SH_FEATURE_SH4A)
1658 71968fa6 aurel32
            return;
1659 71968fa6 aurel32
        else
1660 71968fa6 aurel32
            break;
1661 71968fa6 aurel32
    case 0x00e3:                /* icbi @Rn */
1662 71968fa6 aurel32
        if (ctx->features & SH_FEATURE_SH4A)
1663 71968fa6 aurel32
            return;
1664 71968fa6 aurel32
        else
1665 71968fa6 aurel32
            break;
1666 71968fa6 aurel32
    case 0x00ab:                /* synco */
1667 71968fa6 aurel32
        if (ctx->features & SH_FEATURE_SH4A)
1668 71968fa6 aurel32
            return;
1669 71968fa6 aurel32
        else
1670 71968fa6 aurel32
            break;
1671 fdf9b3e8 bellard
    case 0x4024:                /* rotcl Rn */
1672 c55497ec aurel32
        {
1673 a7812ae4 pbrook
            TCGv tmp = tcg_temp_new();
1674 c55497ec aurel32
            tcg_gen_mov_i32(tmp, cpu_sr);
1675 c55497ec aurel32
            gen_copy_bit_i32(cpu_sr, 0, REG(B11_8), 31);
1676 c55497ec aurel32
            tcg_gen_shli_i32(REG(B11_8), REG(B11_8), 1);
1677 c55497ec aurel32
            gen_copy_bit_i32(REG(B11_8), 0, tmp, 0);
1678 c55497ec aurel32
            tcg_temp_free(tmp);
1679 c55497ec aurel32
        }
1680 fdf9b3e8 bellard
        return;
1681 fdf9b3e8 bellard
    case 0x4025:                /* rotcr Rn */
1682 c55497ec aurel32
        {
1683 a7812ae4 pbrook
            TCGv tmp = tcg_temp_new();
1684 c55497ec aurel32
            tcg_gen_mov_i32(tmp, cpu_sr);
1685 c55497ec aurel32
            gen_copy_bit_i32(cpu_sr, 0, REG(B11_8), 0);
1686 c55497ec aurel32
            tcg_gen_shri_i32(REG(B11_8), REG(B11_8), 1);
1687 c55497ec aurel32
            gen_copy_bit_i32(REG(B11_8), 31, tmp, 0);
1688 c55497ec aurel32
            tcg_temp_free(tmp);
1689 c55497ec aurel32
        }
1690 fdf9b3e8 bellard
        return;
1691 fdf9b3e8 bellard
    case 0x4004:                /* rotl Rn */
1692 2411fde9 Aurelien Jarno
        tcg_gen_rotli_i32(REG(B11_8), REG(B11_8), 1);
1693 2411fde9 Aurelien Jarno
        gen_copy_bit_i32(cpu_sr, 0, REG(B11_8), 0);
1694 fdf9b3e8 bellard
        return;
1695 fdf9b3e8 bellard
    case 0x4005:                /* rotr Rn */
1696 7efbe241 aurel32
        gen_copy_bit_i32(cpu_sr, 0, REG(B11_8), 0);
1697 2411fde9 Aurelien Jarno
        tcg_gen_rotri_i32(REG(B11_8), REG(B11_8), 1);
1698 fdf9b3e8 bellard
        return;
1699 fdf9b3e8 bellard
    case 0x4000:                /* shll Rn */
1700 fdf9b3e8 bellard
    case 0x4020:                /* shal Rn */
1701 7efbe241 aurel32
        gen_copy_bit_i32(cpu_sr, 0, REG(B11_8), 31);
1702 7efbe241 aurel32
        tcg_gen_shli_i32(REG(B11_8), REG(B11_8), 1);
1703 fdf9b3e8 bellard
        return;
1704 fdf9b3e8 bellard
    case 0x4021:                /* shar Rn */
1705 7efbe241 aurel32
        gen_copy_bit_i32(cpu_sr, 0, REG(B11_8), 0);
1706 7efbe241 aurel32
        tcg_gen_sari_i32(REG(B11_8), REG(B11_8), 1);
1707 fdf9b3e8 bellard
        return;
1708 fdf9b3e8 bellard
    case 0x4001:                /* shlr Rn */
1709 7efbe241 aurel32
        gen_copy_bit_i32(cpu_sr, 0, REG(B11_8), 0);
1710 7efbe241 aurel32
        tcg_gen_shri_i32(REG(B11_8), REG(B11_8), 1);
1711 fdf9b3e8 bellard
        return;
1712 fdf9b3e8 bellard
    case 0x4008:                /* shll2 Rn */
1713 7efbe241 aurel32
        tcg_gen_shli_i32(REG(B11_8), REG(B11_8), 2);
1714 fdf9b3e8 bellard
        return;
1715 fdf9b3e8 bellard
    case 0x4018:                /* shll8 Rn */
1716 7efbe241 aurel32
        tcg_gen_shli_i32(REG(B11_8), REG(B11_8), 8);
1717 fdf9b3e8 bellard
        return;
1718 fdf9b3e8 bellard
    case 0x4028:                /* shll16 Rn */
1719 7efbe241 aurel32
        tcg_gen_shli_i32(REG(B11_8), REG(B11_8), 16);
1720 fdf9b3e8 bellard
        return;
1721 fdf9b3e8 bellard
    case 0x4009:                /* shlr2 Rn */
1722 7efbe241 aurel32
        tcg_gen_shri_i32(REG(B11_8), REG(B11_8), 2);
1723 fdf9b3e8 bellard
        return;
1724 fdf9b3e8 bellard
    case 0x4019:                /* shlr8 Rn */
1725 7efbe241 aurel32
        tcg_gen_shri_i32(REG(B11_8), REG(B11_8), 8);
1726 fdf9b3e8 bellard
        return;
1727 fdf9b3e8 bellard
    case 0x4029:                /* shlr16 Rn */
1728 7efbe241 aurel32
        tcg_gen_shri_i32(REG(B11_8), REG(B11_8), 16);
1729 fdf9b3e8 bellard
        return;
1730 fdf9b3e8 bellard
    case 0x401b:                /* tas.b @Rn */
1731 c55497ec aurel32
        {
1732 c55497ec aurel32
            TCGv addr, val;
1733 df9247b2 aurel32
            addr = tcg_temp_local_new();
1734 c55497ec aurel32
            tcg_gen_mov_i32(addr, REG(B11_8));
1735 df9247b2 aurel32
            val = tcg_temp_local_new();
1736 c55497ec aurel32
            tcg_gen_qemu_ld8u(val, addr, ctx->memidx);
1737 c55497ec aurel32
            gen_cmp_imm(TCG_COND_EQ, val, 0);
1738 c55497ec aurel32
            tcg_gen_ori_i32(val, val, 0x80);
1739 c55497ec aurel32
            tcg_gen_qemu_st8(val, addr, ctx->memidx);
1740 c55497ec aurel32
            tcg_temp_free(val);
1741 c55497ec aurel32
            tcg_temp_free(addr);
1742 c55497ec aurel32
        }
1743 fdf9b3e8 bellard
        return;
1744 e67888a7 ths
    case 0xf00d: /* fsts FPUL,FRn - FPSCR: Nothing */
1745 f6198371 aurel32
        CHECK_FPU_ENABLED
1746 f6198371 aurel32
        tcg_gen_mov_i32(cpu_fregs[FREG(B11_8)], cpu_fpul);
1747 eda9b09b bellard
        return;
1748 e67888a7 ths
    case 0xf01d: /* flds FRm,FPUL - FPSCR: Nothing */
1749 f6198371 aurel32
        CHECK_FPU_ENABLED
1750 f6198371 aurel32
        tcg_gen_mov_i32(cpu_fpul, cpu_fregs[FREG(B11_8)]);
1751 eda9b09b bellard
        return;
1752 e67888a7 ths
    case 0xf02d: /* float FPUL,FRn/DRn - FPSCR: R[PR,Enable.I]/W[Cause,Flag] */
1753 f6198371 aurel32
        CHECK_FPU_ENABLED
1754 ea6cf6be ths
        if (ctx->fpscr & FPSCR_PR) {
1755 a7812ae4 pbrook
            TCGv_i64 fp;
1756 ea6cf6be ths
            if (ctx->opcode & 0x0100)
1757 ea6cf6be ths
                break; /* illegal instruction */
1758 a7812ae4 pbrook
            fp = tcg_temp_new_i64();
1759 a7812ae4 pbrook
            gen_helper_float_DT(fp, cpu_fpul);
1760 cc4ba6a9 aurel32
            gen_store_fpr64(fp, DREG(B11_8));
1761 a7812ae4 pbrook
            tcg_temp_free_i64(fp);
1762 ea6cf6be ths
        }
1763 ea6cf6be ths
        else {
1764 66ba317c aurel32
            gen_helper_float_FT(cpu_fregs[FREG(B11_8)], cpu_fpul);
1765 ea6cf6be ths
        }
1766 ea6cf6be ths
        return;
1767 e67888a7 ths
    case 0xf03d: /* ftrc FRm/DRm,FPUL - FPSCR: R[PR,Enable.V]/W[Cause,Flag] */
1768 f6198371 aurel32
        CHECK_FPU_ENABLED
1769 ea6cf6be ths
        if (ctx->fpscr & FPSCR_PR) {
1770 a7812ae4 pbrook
            TCGv_i64 fp;
1771 ea6cf6be ths
            if (ctx->opcode & 0x0100)
1772 ea6cf6be ths
                break; /* illegal instruction */
1773 a7812ae4 pbrook
            fp = tcg_temp_new_i64();
1774 cc4ba6a9 aurel32
            gen_load_fpr64(fp, DREG(B11_8));
1775 a7812ae4 pbrook
            gen_helper_ftrc_DT(cpu_fpul, fp);
1776 a7812ae4 pbrook
            tcg_temp_free_i64(fp);
1777 ea6cf6be ths
        }
1778 ea6cf6be ths
        else {
1779 66ba317c aurel32
            gen_helper_ftrc_FT(cpu_fpul, cpu_fregs[FREG(B11_8)]);
1780 ea6cf6be ths
        }
1781 ea6cf6be ths
        return;
1782 24988dc2 aurel32
    case 0xf04d: /* fneg FRn/DRn - FPSCR: Nothing */
1783 f6198371 aurel32
        CHECK_FPU_ENABLED
1784 7fdf924f aurel32
        {
1785 66ba317c aurel32
            gen_helper_fneg_T(cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B11_8)]);
1786 7fdf924f aurel32
        }
1787 24988dc2 aurel32
        return;
1788 24988dc2 aurel32
    case 0xf05d: /* fabs FRn/DRn */
1789 f6198371 aurel32
        CHECK_FPU_ENABLED
1790 24988dc2 aurel32
        if (ctx->fpscr & FPSCR_PR) {
1791 24988dc2 aurel32
            if (ctx->opcode & 0x0100)
1792 24988dc2 aurel32
                break; /* illegal instruction */
1793 a7812ae4 pbrook
            TCGv_i64 fp = tcg_temp_new_i64();
1794 cc4ba6a9 aurel32
            gen_load_fpr64(fp, DREG(B11_8));
1795 a7812ae4 pbrook
            gen_helper_fabs_DT(fp, fp);
1796 cc4ba6a9 aurel32
            gen_store_fpr64(fp, DREG(B11_8));
1797 a7812ae4 pbrook
            tcg_temp_free_i64(fp);
1798 24988dc2 aurel32
        } else {
1799 66ba317c aurel32
            gen_helper_fabs_FT(cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B11_8)]);
1800 24988dc2 aurel32
        }
1801 24988dc2 aurel32
        return;
1802 24988dc2 aurel32
    case 0xf06d: /* fsqrt FRn */
1803 f6198371 aurel32
        CHECK_FPU_ENABLED
1804 24988dc2 aurel32
        if (ctx->fpscr & FPSCR_PR) {
1805 24988dc2 aurel32
            if (ctx->opcode & 0x0100)
1806 24988dc2 aurel32
                break; /* illegal instruction */
1807 a7812ae4 pbrook
            TCGv_i64 fp = tcg_temp_new_i64();
1808 cc4ba6a9 aurel32
            gen_load_fpr64(fp, DREG(B11_8));
1809 a7812ae4 pbrook
            gen_helper_fsqrt_DT(fp, fp);
1810 cc4ba6a9 aurel32
            gen_store_fpr64(fp, DREG(B11_8));
1811 a7812ae4 pbrook
            tcg_temp_free_i64(fp);
1812 24988dc2 aurel32
        } else {
1813 66ba317c aurel32
            gen_helper_fsqrt_FT(cpu_fregs[FREG(B11_8)], cpu_fregs[FREG(B11_8)]);
1814 24988dc2 aurel32
        }
1815 24988dc2 aurel32
        return;
1816 24988dc2 aurel32
    case 0xf07d: /* fsrra FRn */
1817 f6198371 aurel32
        CHECK_FPU_ENABLED
1818 24988dc2 aurel32
        break;
1819 e67888a7 ths
    case 0xf08d: /* fldi0 FRn - FPSCR: R[PR] */
1820 f6198371 aurel32
        CHECK_FPU_ENABLED
1821 ea6cf6be ths
        if (!(ctx->fpscr & FPSCR_PR)) {
1822 66ba317c aurel32
            tcg_gen_movi_i32(cpu_fregs[FREG(B11_8)], 0);
1823 ea6cf6be ths
        }
1824 12d96138 aurel32
        return;
1825 e67888a7 ths
    case 0xf09d: /* fldi1 FRn - FPSCR: R[PR] */
1826 f6198371 aurel32
        CHECK_FPU_ENABLED
1827 ea6cf6be ths
        if (!(ctx->fpscr & FPSCR_PR)) {
1828 66ba317c aurel32
            tcg_gen_movi_i32(cpu_fregs[FREG(B11_8)], 0x3f800000);
1829 ea6cf6be ths
        }
1830 12d96138 aurel32
        return;
1831 24988dc2 aurel32
    case 0xf0ad: /* fcnvsd FPUL,DRn */
1832 f6198371 aurel32
        CHECK_FPU_ENABLED
1833 cc4ba6a9 aurel32
        {
1834 a7812ae4 pbrook
            TCGv_i64 fp = tcg_temp_new_i64();
1835 a7812ae4 pbrook
            gen_helper_fcnvsd_FT_DT(fp, cpu_fpul);
1836 cc4ba6a9 aurel32
            gen_store_fpr64(fp, DREG(B11_8));
1837 a7812ae4 pbrook
            tcg_temp_free_i64(fp);
1838 cc4ba6a9 aurel32
        }
1839 24988dc2 aurel32
        return;
1840 24988dc2 aurel32
    case 0xf0bd: /* fcnvds DRn,FPUL */
1841 f6198371 aurel32
        CHECK_FPU_ENABLED
1842 cc4ba6a9 aurel32
        {
1843 a7812ae4 pbrook
            TCGv_i64 fp = tcg_temp_new_i64();
1844 cc4ba6a9 aurel32
            gen_load_fpr64(fp, DREG(B11_8));
1845 a7812ae4 pbrook
            gen_helper_fcnvds_DT_FT(cpu_fpul, fp);
1846 a7812ae4 pbrook
            tcg_temp_free_i64(fp);
1847 cc4ba6a9 aurel32
        }
1848 24988dc2 aurel32
        return;
1849 af8c2bde Aurelien Jarno
    case 0xf0ed: /* fipr FVm,FVn */
1850 af8c2bde Aurelien Jarno
        CHECK_FPU_ENABLED
1851 af8c2bde Aurelien Jarno
        if ((ctx->fpscr & FPSCR_PR) == 0) {
1852 af8c2bde Aurelien Jarno
            TCGv m, n;
1853 f840fa99 Stefan Weil
            m = tcg_const_i32((ctx->opcode >> 8) & 3);
1854 f840fa99 Stefan Weil
            n = tcg_const_i32((ctx->opcode >> 10) & 3);
1855 af8c2bde Aurelien Jarno
            gen_helper_fipr(m, n);
1856 af8c2bde Aurelien Jarno
            tcg_temp_free(m);
1857 af8c2bde Aurelien Jarno
            tcg_temp_free(n);
1858 af8c2bde Aurelien Jarno
            return;
1859 af8c2bde Aurelien Jarno
        }
1860 af8c2bde Aurelien Jarno
        break;
1861 17075f10 Aurelien Jarno
    case 0xf0fd: /* ftrv XMTRX,FVn */
1862 17075f10 Aurelien Jarno
        CHECK_FPU_ENABLED
1863 17075f10 Aurelien Jarno
        if ((ctx->opcode & 0x0300) == 0x0100 &&
1864 17075f10 Aurelien Jarno
            (ctx->fpscr & FPSCR_PR) == 0) {
1865 17075f10 Aurelien Jarno
            TCGv n;
1866 f840fa99 Stefan Weil
            n = tcg_const_i32((ctx->opcode >> 10) & 3);
1867 17075f10 Aurelien Jarno
            gen_helper_ftrv(n);
1868 17075f10 Aurelien Jarno
            tcg_temp_free(n);
1869 17075f10 Aurelien Jarno
            return;
1870 17075f10 Aurelien Jarno
        }
1871 17075f10 Aurelien Jarno
        break;
1872 fdf9b3e8 bellard
    }
1873 bacc637a aurel32
#if 0
1874 fdf9b3e8 bellard
    fprintf(stderr, "unknown instruction 0x%04x at pc 0x%08x\n",
1875 fdf9b3e8 bellard
            ctx->opcode, ctx->pc);
1876 bacc637a aurel32
    fflush(stderr);
1877 bacc637a aurel32
#endif
1878 86865c5f Aurelien Jarno
    if (ctx->flags & (DELAY_SLOT | DELAY_SLOT_CONDITIONAL)) {
1879 86865c5f Aurelien Jarno
       gen_helper_raise_slot_illegal_instruction();
1880 86865c5f Aurelien Jarno
    } else {
1881 86865c5f Aurelien Jarno
       gen_helper_raise_illegal_instruction();
1882 86865c5f Aurelien Jarno
    }
1883 823029f9 ths
    ctx->bstate = BS_EXCP;
1884 823029f9 ths
}
1885 823029f9 ths
1886 b1d8e52e blueswir1
static void decode_opc(DisasContext * ctx)
1887 823029f9 ths
{
1888 823029f9 ths
    uint32_t old_flags = ctx->flags;
1889 823029f9 ths
1890 be15c50d Aurelien Jarno
    if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
1891 be15c50d Aurelien Jarno
        tcg_gen_debug_insn_start(ctx->pc);
1892 be15c50d Aurelien Jarno
    }
1893 be15c50d Aurelien Jarno
1894 823029f9 ths
    _decode_opc(ctx);
1895 823029f9 ths
1896 823029f9 ths
    if (old_flags & (DELAY_SLOT | DELAY_SLOT_CONDITIONAL)) {
1897 823029f9 ths
        if (ctx->flags & DELAY_SLOT_CLEARME) {
1898 1000822b aurel32
            gen_store_flags(0);
1899 274a9e70 aurel32
        } else {
1900 274a9e70 aurel32
            /* go out of the delay slot */
1901 274a9e70 aurel32
            uint32_t new_flags = ctx->flags;
1902 274a9e70 aurel32
            new_flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
1903 1000822b aurel32
            gen_store_flags(new_flags);
1904 823029f9 ths
        }
1905 823029f9 ths
        ctx->flags = 0;
1906 823029f9 ths
        ctx->bstate = BS_BRANCH;
1907 823029f9 ths
        if (old_flags & DELAY_SLOT_CONDITIONAL) {
1908 823029f9 ths
            gen_delayed_conditional_jump(ctx);
1909 823029f9 ths
        } else if (old_flags & DELAY_SLOT) {
1910 823029f9 ths
            gen_jump(ctx);
1911 823029f9 ths
        }
1912 823029f9 ths
1913 823029f9 ths
    }
1914 274a9e70 aurel32
1915 274a9e70 aurel32
    /* go into a delay slot */
1916 274a9e70 aurel32
    if (ctx->flags & (DELAY_SLOT | DELAY_SLOT_CONDITIONAL))
1917 1000822b aurel32
        gen_store_flags(ctx->flags);
1918 fdf9b3e8 bellard
}
1919 fdf9b3e8 bellard
1920 2cfc5f17 ths
static inline void
1921 73e5716c Andreas Färber
gen_intermediate_code_internal(CPUSH4State * env, TranslationBlock * tb,
1922 820e00f2 ths
                               int search_pc)
1923 fdf9b3e8 bellard
{
1924 fdf9b3e8 bellard
    DisasContext ctx;
1925 fdf9b3e8 bellard
    target_ulong pc_start;
1926 fdf9b3e8 bellard
    static uint16_t *gen_opc_end;
1927 a1d1bb31 aliguori
    CPUBreakpoint *bp;
1928 355fb23d pbrook
    int i, ii;
1929 2e70f6ef pbrook
    int num_insns;
1930 2e70f6ef pbrook
    int max_insns;
1931 fdf9b3e8 bellard
1932 fdf9b3e8 bellard
    pc_start = tb->pc;
1933 fdf9b3e8 bellard
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
1934 fdf9b3e8 bellard
    ctx.pc = pc_start;
1935 823029f9 ths
    ctx.flags = (uint32_t)tb->flags;
1936 823029f9 ths
    ctx.bstate = BS_NONE;
1937 fdf9b3e8 bellard
    ctx.sr = env->sr;
1938 eda9b09b bellard
    ctx.fpscr = env->fpscr;
1939 1f486815 Aurelien Jarno
    ctx.memidx = (env->sr & SR_MD) == 0 ? 1 : 0;
1940 9854bc46 pbrook
    /* We don't know if the delayed pc came from a dynamic or static branch,
1941 9854bc46 pbrook
       so assume it is a dynamic branch.  */
1942 823029f9 ths
    ctx.delayed_pc = -1; /* use delayed pc from env pointer */
1943 fdf9b3e8 bellard
    ctx.tb = tb;
1944 fdf9b3e8 bellard
    ctx.singlestep_enabled = env->singlestep_enabled;
1945 71968fa6 aurel32
    ctx.features = env->features;
1946 852d481f edgar_igl
    ctx.has_movcal = (tb->flags & TB_FLAG_PENDING_MOVCA);
1947 fdf9b3e8 bellard
1948 355fb23d pbrook
    ii = -1;
1949 2e70f6ef pbrook
    num_insns = 0;
1950 2e70f6ef pbrook
    max_insns = tb->cflags & CF_COUNT_MASK;
1951 2e70f6ef pbrook
    if (max_insns == 0)
1952 2e70f6ef pbrook
        max_insns = CF_COUNT_MASK;
1953 2e70f6ef pbrook
    gen_icount_start();
1954 823029f9 ths
    while (ctx.bstate == BS_NONE && gen_opc_ptr < gen_opc_end) {
1955 72cf2d4f Blue Swirl
        if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
1956 72cf2d4f Blue Swirl
            QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
1957 a1d1bb31 aliguori
                if (ctx.pc == bp->pc) {
1958 fdf9b3e8 bellard
                    /* We have hit a breakpoint - make sure PC is up-to-date */
1959 3a8a44c4 aurel32
                    tcg_gen_movi_i32(cpu_pc, ctx.pc);
1960 a7812ae4 pbrook
                    gen_helper_debug();
1961 823029f9 ths
                    ctx.bstate = BS_EXCP;
1962 fdf9b3e8 bellard
                    break;
1963 fdf9b3e8 bellard
                }
1964 fdf9b3e8 bellard
            }
1965 fdf9b3e8 bellard
        }
1966 355fb23d pbrook
        if (search_pc) {
1967 355fb23d pbrook
            i = gen_opc_ptr - gen_opc_buf;
1968 355fb23d pbrook
            if (ii < i) {
1969 355fb23d pbrook
                ii++;
1970 355fb23d pbrook
                while (ii < i)
1971 355fb23d pbrook
                    gen_opc_instr_start[ii++] = 0;
1972 355fb23d pbrook
            }
1973 355fb23d pbrook
            gen_opc_pc[ii] = ctx.pc;
1974 823029f9 ths
            gen_opc_hflags[ii] = ctx.flags;
1975 355fb23d pbrook
            gen_opc_instr_start[ii] = 1;
1976 2e70f6ef pbrook
            gen_opc_icount[ii] = num_insns;
1977 355fb23d pbrook
        }
1978 2e70f6ef pbrook
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
1979 2e70f6ef pbrook
            gen_io_start();
1980 fdf9b3e8 bellard
#if 0
1981 fdf9b3e8 bellard
        fprintf(stderr, "Loading opcode at address 0x%08x\n", ctx.pc);
1982 fdf9b3e8 bellard
        fflush(stderr);
1983 fdf9b3e8 bellard
#endif
1984 fdf9b3e8 bellard
        ctx.opcode = lduw_code(ctx.pc);
1985 fdf9b3e8 bellard
        decode_opc(&ctx);
1986 2e70f6ef pbrook
        num_insns++;
1987 fdf9b3e8 bellard
        ctx.pc += 2;
1988 fdf9b3e8 bellard
        if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0)
1989 fdf9b3e8 bellard
            break;
1990 fdf9b3e8 bellard
        if (env->singlestep_enabled)
1991 fdf9b3e8 bellard
            break;
1992 2e70f6ef pbrook
        if (num_insns >= max_insns)
1993 2e70f6ef pbrook
            break;
1994 1b530a6d aurel32
        if (singlestep)
1995 1b530a6d aurel32
            break;
1996 fdf9b3e8 bellard
    }
1997 2e70f6ef pbrook
    if (tb->cflags & CF_LAST_IO)
1998 2e70f6ef pbrook
        gen_io_end();
1999 fdf9b3e8 bellard
    if (env->singlestep_enabled) {
2000 bdbf22e6 aurel32
        tcg_gen_movi_i32(cpu_pc, ctx.pc);
2001 a7812ae4 pbrook
        gen_helper_debug();
2002 823029f9 ths
    } else {
2003 823029f9 ths
        switch (ctx.bstate) {
2004 823029f9 ths
        case BS_STOP:
2005 823029f9 ths
            /* gen_op_interrupt_restart(); */
2006 823029f9 ths
            /* fall through */
2007 823029f9 ths
        case BS_NONE:
2008 823029f9 ths
            if (ctx.flags) {
2009 1000822b aurel32
                gen_store_flags(ctx.flags | DELAY_SLOT_CLEARME);
2010 823029f9 ths
            }
2011 823029f9 ths
            gen_goto_tb(&ctx, 0, ctx.pc);
2012 823029f9 ths
            break;
2013 823029f9 ths
        case BS_EXCP:
2014 823029f9 ths
            /* gen_op_interrupt_restart(); */
2015 57fec1fe bellard
            tcg_gen_exit_tb(0);
2016 823029f9 ths
            break;
2017 823029f9 ths
        case BS_BRANCH:
2018 823029f9 ths
        default:
2019 823029f9 ths
            break;
2020 823029f9 ths
        }
2021 fdf9b3e8 bellard
    }
2022 823029f9 ths
2023 2e70f6ef pbrook
    gen_icount_end(tb, num_insns);
2024 fdf9b3e8 bellard
    *gen_opc_ptr = INDEX_op_end;
2025 355fb23d pbrook
    if (search_pc) {
2026 355fb23d pbrook
        i = gen_opc_ptr - gen_opc_buf;
2027 355fb23d pbrook
        ii++;
2028 355fb23d pbrook
        while (ii <= i)
2029 355fb23d pbrook
            gen_opc_instr_start[ii++] = 0;
2030 355fb23d pbrook
    } else {
2031 355fb23d pbrook
        tb->size = ctx.pc - pc_start;
2032 2e70f6ef pbrook
        tb->icount = num_insns;
2033 355fb23d pbrook
    }
2034 fdf9b3e8 bellard
2035 fdf9b3e8 bellard
#ifdef DEBUG_DISAS
2036 fdf9b3e8 bellard
#ifdef SH4_DEBUG_DISAS
2037 93fcfe39 aliguori
    qemu_log_mask(CPU_LOG_TB_IN_ASM, "\n");
2038 fdf9b3e8 bellard
#endif
2039 8fec2b8c aliguori
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
2040 93fcfe39 aliguori
        qemu_log("IN:\n");        /* , lookup_symbol(pc_start)); */
2041 93fcfe39 aliguori
        log_target_disas(pc_start, ctx.pc - pc_start, 0);
2042 93fcfe39 aliguori
        qemu_log("\n");
2043 fdf9b3e8 bellard
    }
2044 fdf9b3e8 bellard
#endif
2045 fdf9b3e8 bellard
}
2046 fdf9b3e8 bellard
2047 73e5716c Andreas Färber
void gen_intermediate_code(CPUSH4State * env, struct TranslationBlock *tb)
2048 fdf9b3e8 bellard
{
2049 2cfc5f17 ths
    gen_intermediate_code_internal(env, tb, 0);
2050 fdf9b3e8 bellard
}
2051 fdf9b3e8 bellard
2052 73e5716c Andreas Färber
void gen_intermediate_code_pc(CPUSH4State * env, struct TranslationBlock *tb)
2053 fdf9b3e8 bellard
{
2054 2cfc5f17 ths
    gen_intermediate_code_internal(env, tb, 1);
2055 fdf9b3e8 bellard
}
2056 d2856f1a aurel32
2057 73e5716c Andreas Färber
void restore_state_to_opc(CPUSH4State *env, TranslationBlock *tb, int pc_pos)
2058 d2856f1a aurel32
{
2059 d2856f1a aurel32
    env->pc = gen_opc_pc[pc_pos];
2060 d2856f1a aurel32
    env->flags = gen_opc_hflags[pc_pos];
2061 d2856f1a aurel32
}