helper.c 9.9 KB
Newer Older
1
/*
2
 * Copyright (C) 2010-2012 Guan Xuetao
3 4 5 6
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
7 8 9
 *
 * Contributions from 2012-04-01 on are considered under GPL version 2,
 * or (at your option) any later version.
10 11 12 13 14 15 16
 */

#include "cpu.h"
#include "gdbstub.h"
#include "helper.h"
#include "host-utils.h"

17
CPUUniCore32State *uc32_cpu_init(const char *cpu_model)
18
{
A
Andreas Färber 已提交
19
    UniCore32CPU *cpu;
20
    CPUUniCore32State *env;
21 22
    static int inited = 1;

A
Andreas Färber 已提交
23 24 25 26 27
    if (object_class_by_name(cpu_model) == NULL) {
        return NULL;
    }
    cpu = UNICORE32_CPU(object_new(cpu_model));
    env = &cpu->env;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

    if (inited) {
        inited = 0;
        uc32_translate_init();
    }

    qemu_init_vcpu(env);
    return env;
}

uint32_t HELPER(clo)(uint32_t x)
{
    return clo32(x);
}

uint32_t HELPER(clz)(uint32_t x)
{
    return clz32(x);
}

48 49 50 51 52 53 54 55
#ifdef CONFIG_USER_ONLY
void switch_mode(CPUUniCore32State *env, int mode)
{
    if (mode != ASR_MODE_USER) {
        cpu_abort(env, "Tried to switch out of user mode\n");
    }
}

56
void do_interrupt(CPUUniCore32State *env)
57
{
58
    cpu_abort(env, "NO interrupt in user mode\n");
59 60
}

61 62
int uc32_cpu_handle_mmu_fault(CPUUniCore32State *env, target_ulong address,
                              int access_type, int mmu_idx)
63
{
64
    cpu_abort(env, "NO mmu fault in user mode\n");
65 66
    return 1;
}
67
#endif
68 69

/* These should probably raise undefined insn exceptions.  */
70
void HELPER(set_cp)(CPUUniCore32State *env, uint32_t insn, uint32_t val)
71 72 73 74 75 76
{
    int op1 = (insn >> 8) & 0xf;
    cpu_abort(env, "cp%i insn %08x\n", op1, insn);
    return;
}

77
uint32_t HELPER(get_cp)(CPUUniCore32State *env, uint32_t insn)
78 79 80 81 82 83
{
    int op1 = (insn >> 8) & 0xf;
    cpu_abort(env, "cp%i insn %08x\n", op1, insn);
    return 0;
}

84
void HELPER(set_cp0)(CPUUniCore32State *env, uint32_t insn, uint32_t val)
85 86 87 88
{
    cpu_abort(env, "cp0 insn %08x\n", insn);
}

89
uint32_t HELPER(get_cp0)(CPUUniCore32State *env, uint32_t insn)
90 91 92 93 94
{
    cpu_abort(env, "cp0 insn %08x\n", insn);
    return 0;
}

95
void HELPER(set_r29_banked)(CPUUniCore32State *env, uint32_t mode, uint32_t val)
96 97 98 99
{
    cpu_abort(env, "banked r29 write\n");
}

100
uint32_t HELPER(get_r29_banked)(CPUUniCore32State *env, uint32_t mode)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
{
    cpu_abort(env, "banked r29 read\n");
    return 0;
}

/* UniCore-F64 support.  We follow the convention used for F64 instrunctions:
   Single precition routines have a "s" suffix, double precision a
   "d" suffix.  */

/* Convert host exception flags to f64 form.  */
static inline int ucf64_exceptbits_from_host(int host_bits)
{
    int target_bits = 0;

    if (host_bits & float_flag_invalid) {
        target_bits |= UCF64_FPSCR_FLAG_INVALID;
    }
    if (host_bits & float_flag_divbyzero) {
        target_bits |= UCF64_FPSCR_FLAG_DIVZERO;
    }
    if (host_bits & float_flag_overflow) {
        target_bits |= UCF64_FPSCR_FLAG_OVERFLOW;
    }
    if (host_bits & float_flag_underflow) {
        target_bits |= UCF64_FPSCR_FLAG_UNDERFLOW;
    }
    if (host_bits & float_flag_inexact) {
        target_bits |= UCF64_FPSCR_FLAG_INEXACT;
    }
    return target_bits;
}

133
uint32_t HELPER(ucf64_get_fpscr)(CPUUniCore32State *env)
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
{
    int i;
    uint32_t fpscr;

    fpscr = (env->ucf64.xregs[UC32_UCF64_FPSCR] & UCF64_FPSCR_MASK);
    i = get_float_exception_flags(&env->ucf64.fp_status);
    fpscr |= ucf64_exceptbits_from_host(i);
    return fpscr;
}

/* Convert ucf64 exception flags to target form.  */
static inline int ucf64_exceptbits_to_host(int target_bits)
{
    int host_bits = 0;

    if (target_bits & UCF64_FPSCR_FLAG_INVALID) {
        host_bits |= float_flag_invalid;
    }
    if (target_bits & UCF64_FPSCR_FLAG_DIVZERO) {
        host_bits |= float_flag_divbyzero;
    }
    if (target_bits & UCF64_FPSCR_FLAG_OVERFLOW) {
        host_bits |= float_flag_overflow;
    }
    if (target_bits & UCF64_FPSCR_FLAG_UNDERFLOW) {
        host_bits |= float_flag_underflow;
    }
    if (target_bits & UCF64_FPSCR_FLAG_INEXACT) {
        host_bits |= float_flag_inexact;
    }
    return host_bits;
}

167
void HELPER(ucf64_set_fpscr)(CPUUniCore32State *env, uint32_t val)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
{
    int i;
    uint32_t changed;

    changed = env->ucf64.xregs[UC32_UCF64_FPSCR];
    env->ucf64.xregs[UC32_UCF64_FPSCR] = (val & UCF64_FPSCR_MASK);

    changed ^= val;
    if (changed & (UCF64_FPSCR_RND_MASK)) {
        i = UCF64_FPSCR_RND(val);
        switch (i) {
        case 0:
            i = float_round_nearest_even;
            break;
        case 1:
            i = float_round_to_zero;
            break;
        case 2:
            i = float_round_up;
            break;
        case 3:
            i = float_round_down;
            break;
        default: /* 100 and 101 not implement */
            cpu_abort(env, "Unsupported UniCore-F64 round mode");
        }
        set_float_rounding_mode(i, &env->ucf64.fp_status);
    }

    i = ucf64_exceptbits_to_host(UCF64_FPSCR_TRAPEN(val));
    set_float_exception_flags(i, &env->ucf64.fp_status);
}

201
float32 HELPER(ucf64_adds)(float32 a, float32 b, CPUUniCore32State *env)
202 203 204 205
{
    return float32_add(a, b, &env->ucf64.fp_status);
}

206
float64 HELPER(ucf64_addd)(float64 a, float64 b, CPUUniCore32State *env)
207 208 209 210
{
    return float64_add(a, b, &env->ucf64.fp_status);
}

211
float32 HELPER(ucf64_subs)(float32 a, float32 b, CPUUniCore32State *env)
212 213 214 215
{
    return float32_sub(a, b, &env->ucf64.fp_status);
}

216
float64 HELPER(ucf64_subd)(float64 a, float64 b, CPUUniCore32State *env)
217 218 219 220
{
    return float64_sub(a, b, &env->ucf64.fp_status);
}

221
float32 HELPER(ucf64_muls)(float32 a, float32 b, CPUUniCore32State *env)
222 223 224 225
{
    return float32_mul(a, b, &env->ucf64.fp_status);
}

226
float64 HELPER(ucf64_muld)(float64 a, float64 b, CPUUniCore32State *env)
227 228 229 230
{
    return float64_mul(a, b, &env->ucf64.fp_status);
}

231
float32 HELPER(ucf64_divs)(float32 a, float32 b, CPUUniCore32State *env)
232 233 234 235
{
    return float32_div(a, b, &env->ucf64.fp_status);
}

236
float64 HELPER(ucf64_divd)(float64 a, float64 b, CPUUniCore32State *env)
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
{
    return float64_div(a, b, &env->ucf64.fp_status);
}

float32 HELPER(ucf64_negs)(float32 a)
{
    return float32_chs(a);
}

float64 HELPER(ucf64_negd)(float64 a)
{
    return float64_chs(a);
}

float32 HELPER(ucf64_abss)(float32 a)
{
    return float32_abs(a);
}

float64 HELPER(ucf64_absd)(float64 a)
{
    return float64_abs(a);
}

/* XXX: check quiet/signaling case */
262
void HELPER(ucf64_cmps)(float32 a, float32 b, uint32_t c, CPUUniCore32State *env)
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
{
    int flag;
    flag = float32_compare_quiet(a, b, &env->ucf64.fp_status);
    env->CF = 0;
    switch (c & 0x7) {
    case 0: /* F */
        break;
    case 1: /* UN */
        if (flag == 2) {
            env->CF = 1;
        }
        break;
    case 2: /* EQ */
        if (flag == 0) {
            env->CF = 1;
        }
        break;
    case 3: /* UEQ */
        if ((flag == 0) || (flag == 2)) {
            env->CF = 1;
        }
        break;
    case 4: /* OLT */
        if (flag == -1) {
            env->CF = 1;
        }
        break;
    case 5: /* ULT */
        if ((flag == -1) || (flag == 2)) {
            env->CF = 1;
        }
        break;
    case 6: /* OLE */
        if ((flag == -1) || (flag == 0)) {
            env->CF = 1;
        }
        break;
    case 7: /* ULE */
        if (flag != 1) {
            env->CF = 1;
        }
        break;
    }
    env->ucf64.xregs[UC32_UCF64_FPSCR] = (env->CF << 29)
                    | (env->ucf64.xregs[UC32_UCF64_FPSCR] & 0x0fffffff);
}

310
void HELPER(ucf64_cmpd)(float64 a, float64 b, uint32_t c, CPUUniCore32State *env)
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
{
    int flag;
    flag = float64_compare_quiet(a, b, &env->ucf64.fp_status);
    env->CF = 0;
    switch (c & 0x7) {
    case 0: /* F */
        break;
    case 1: /* UN */
        if (flag == 2) {
            env->CF = 1;
        }
        break;
    case 2: /* EQ */
        if (flag == 0) {
            env->CF = 1;
        }
        break;
    case 3: /* UEQ */
        if ((flag == 0) || (flag == 2)) {
            env->CF = 1;
        }
        break;
    case 4: /* OLT */
        if (flag == -1) {
            env->CF = 1;
        }
        break;
    case 5: /* ULT */
        if ((flag == -1) || (flag == 2)) {
            env->CF = 1;
        }
        break;
    case 6: /* OLE */
        if ((flag == -1) || (flag == 0)) {
            env->CF = 1;
        }
        break;
    case 7: /* ULE */
        if (flag != 1) {
            env->CF = 1;
        }
        break;
    }
    env->ucf64.xregs[UC32_UCF64_FPSCR] = (env->CF << 29)
                    | (env->ucf64.xregs[UC32_UCF64_FPSCR] & 0x0fffffff);
}

/* Helper routines to perform bitwise copies between float and int.  */
static inline float32 ucf64_itos(uint32_t i)
{
    union {
        uint32_t i;
        float32 s;
    } v;

    v.i = i;
    return v.s;
}

static inline uint32_t ucf64_stoi(float32 s)
{
    union {
        uint32_t i;
        float32 s;
    } v;

    v.s = s;
    return v.i;
}

static inline float64 ucf64_itod(uint64_t i)
{
    union {
        uint64_t i;
        float64 d;
    } v;

    v.i = i;
    return v.d;
}

static inline uint64_t ucf64_dtoi(float64 d)
{
    union {
        uint64_t i;
        float64 d;
    } v;

    v.d = d;
    return v.i;
}

/* Integer to float conversion.  */
404
float32 HELPER(ucf64_si2sf)(float32 x, CPUUniCore32State *env)
405 406 407 408
{
    return int32_to_float32(ucf64_stoi(x), &env->ucf64.fp_status);
}

409
float64 HELPER(ucf64_si2df)(float32 x, CPUUniCore32State *env)
410 411 412 413 414
{
    return int32_to_float64(ucf64_stoi(x), &env->ucf64.fp_status);
}

/* Float to integer conversion.  */
415
float32 HELPER(ucf64_sf2si)(float32 x, CPUUniCore32State *env)
416 417 418 419
{
    return ucf64_itos(float32_to_int32(x, &env->ucf64.fp_status));
}

420
float32 HELPER(ucf64_df2si)(float64 x, CPUUniCore32State *env)
421 422 423 424 425
{
    return ucf64_itos(float64_to_int32(x, &env->ucf64.fp_status));
}

/* floating point conversion */
426
float64 HELPER(ucf64_sf2df)(float32 x, CPUUniCore32State *env)
427 428 429 430
{
    return float32_to_float64(x, &env->ucf64.fp_status);
}

431
float32 HELPER(ucf64_df2sf)(float64 x, CPUUniCore32State *env)
432 433 434
{
    return float64_to_float32(x, &env->ucf64.fp_status);
}