Commit 620c6cf6 authored by Richard Henderson's avatar Richard Henderson Committed by Laurent Vivier
Browse files

target-m68k: Reorg flags handling



Separate all ccr bits.  Continue to batch updates via cc_op.

Signed-off-by: default avatarRichard Henderson <rth@twiddle.net>

Fix gen_logic_cc() to really extend the size of the result.
Fix gen_get_ccr(): update cc_op as it is used by the helper.
Factorize flags computing and src/ccr cleanup

Signed-off-by: default avatarLaurent Vivier <laurent@vivier.eu>

target-m68k: sr/ccr cleanup

Signed-off-by: default avatarLaurent Vivier <laurent@vivier.eu>
parent 18dd87f2
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ static void m68k_cpu_reset(CPUState *s)
#endif
    m68k_switch_sp(env);
    /* ??? FP regs should be initialized to NaN.  */
    env->cc_op = CC_OP_FLAGS;
    cpu_m68k_set_ccr(env, 0);
    /* TODO: We should set PC from the interrupt vector.  */
    env->pc = 0;
    tlb_flush(s, 1);
+22 −24
Original line number Diff line number Diff line
@@ -75,9 +75,11 @@ typedef struct CPUM68KState {

    /* Condition flags.  */
    uint32_t cc_op;
    uint32_t cc_dest;
    uint32_t cc_src;
    uint32_t cc_x;
    uint32_t cc_x; /* always 0/1 */
    uint32_t cc_n; /* in bit 31 (i.e. negative) */
    uint32_t cc_v; /* in bit 31, unused, or computed from cc_n and cc_v */
    uint32_t cc_c; /* either 0/1, unused, or computed from cc_n and cc_v */
    uint32_t cc_z; /* == 0 or unused */

    float64 fregs[8];
    float64 fp_result;
@@ -170,27 +172,23 @@ void cpu_m68k_set_ccr(CPUM68KState *env, uint32_t);
 * are only needed for conditional branches.
 */
typedef enum {
    CC_OP_DYNAMIC, /* Use env->cc_op  */
    CC_OP_FLAGS, /* CC_DEST = CVZN, CC_SRC = unused */
    CC_OP_LOGICB, /* CC_DEST = result, CC_SRC = unused */
    CC_OP_LOGICW, /* CC_DEST = result, CC_SRC = unused */
    CC_OP_LOGIC, /* CC_DEST = result, CC_SRC = unused */
    CC_OP_ADDB,   /* CC_DEST = result, CC_SRC = source */
    CC_OP_ADDW,   /* CC_DEST = result, CC_SRC = source */
    CC_OP_ADD,   /* CC_DEST = result, CC_SRC = source */
    CC_OP_SUBB,   /* CC_DEST = result, CC_SRC = source */
    CC_OP_SUBW,   /* CC_DEST = result, CC_SRC = source */
    CC_OP_SUB,   /* CC_DEST = result, CC_SRC = source */
    CC_OP_ADDXB,  /* CC_DEST = result, CC_SRC = source */
    CC_OP_ADDXW,  /* CC_DEST = result, CC_SRC = source */
    CC_OP_ADDX,  /* CC_DEST = result, CC_SRC = source */
    CC_OP_SUBXB,  /* CC_DEST = result, CC_SRC = source */
    CC_OP_SUBXW,  /* CC_DEST = result, CC_SRC = source */
    CC_OP_SUBX,  /* CC_DEST = result, CC_SRC = source */
    CC_OP_SHIFTB, /* CC_DEST = result, CC_SRC = carry */
    CC_OP_SHIFTW, /* CC_DEST = result, CC_SRC = carry */
    CC_OP_SHIFT, /* CC_DEST = result, CC_SRC = carry */
    CC_OP_NB,
    /* Translator only -- use env->cc_op.  */
    CC_OP_DYNAMIC = -1,

    /* Each flag bit computed into cc_[xcnvz].  */
    CC_OP_FLAGS,

    /* X in cc_x, C = X, N in cc_n, Z in cc_n, V via cc_n/cc_v.  */
    CC_OP_ADD,
    CC_OP_SUB,

    /* X in cc_x, {N,Z,C,V} via cc_n/cc_v.  */
    CC_OP_CMP,

    /* X in cc_x, C = 0, V = 0, N in cc_n, Z in cc_n.  */
    CC_OP_LOGIC,

    CC_OP_NB
} CCOp;

#define CCF_C 0x01
+135 −224
Original line number Diff line number Diff line
@@ -132,151 +132,6 @@ void m68k_cpu_init_gdb(M68kCPU *cpu)
    /* TODO: Add [E]MAC registers.  */
}

static uint32_t cpu_m68k_flush_flags(CPUM68KState *env, int op)
{
    int flags;
    uint32_t src;
    uint32_t dest;
    uint32_t tmp;

#define HIGHBIT(type) (1u << (sizeof(type) * 8 - 1))

#define SET_NZ(x, type) do { \
        if ((type)(x) == 0) { \
            flags |= CCF_Z; \
        } else if ((type)(x) < 0) { \
            flags |= CCF_N; \
        } \
    } while (0)

#define SET_FLAGS_SUB(type, utype) do { \
        SET_NZ(dest, type); \
        tmp = dest + src; \
        if ((utype) tmp < (utype) src) { \
            flags |= CCF_C; \
        } \
        if (HIGHBIT(type) & (tmp ^ dest) & (tmp ^ src)) { \
            flags |= CCF_V; \
        } \
    } while (0)

#define SET_FLAGS_ADD(type, utype) do { \
        SET_NZ(dest, type); \
        if ((utype) dest < (utype) src) { \
            flags |= CCF_C; \
        } \
        tmp = dest - src; \
        if (HIGHBIT(type) & (src ^ dest) & ~(tmp ^ src)) { \
            flags |= CCF_V; \
        } \
    } while (0)

#define SET_FLAGS_ADDX(type, utype) do { \
        SET_NZ(dest, type); \
        if ((utype) dest <= (utype) src) { \
            flags |= CCF_C; \
        } \
        tmp = dest - src - 1; \
        if (HIGHBIT(type) & (src ^ dest) & ~(tmp ^ src)) { \
            flags |= CCF_V; \
        } \
    } while (0)

#define SET_FLAGS_SUBX(type, utype) do { \
        SET_NZ(dest, type); \
        tmp = dest + src + 1; \
        if ((utype) tmp <= (utype) src) { \
            flags |= CCF_C; \
        } \
        if (HIGHBIT(type) & (tmp ^ dest) & (tmp ^ src)) { \
            flags |= CCF_V; \
        } \
    } while (0)

#define SET_FLAGS_SHIFT(type) do { \
    SET_NZ(dest, type); \
    flags |= src; \
    } while (0)

    flags = 0;
    src = env->cc_src;
    dest = env->cc_dest;
    switch (op) {
    case CC_OP_FLAGS:
        flags = dest;
        break;
    case CC_OP_LOGICB:
        SET_NZ(dest, int8_t);
        break;
    case CC_OP_LOGICW:
        SET_NZ(dest, int16_t);
        break;
    case CC_OP_LOGIC:
        SET_NZ(dest, int32_t);
        break;
    case CC_OP_ADDB:
        SET_FLAGS_ADD(int8_t, uint8_t);
        break;
    case CC_OP_ADDW:
        SET_FLAGS_ADD(int16_t, uint16_t);
        break;
    case CC_OP_ADD:
        SET_FLAGS_ADD(int32_t, uint32_t);
        break;
    case CC_OP_SUBB:
        SET_FLAGS_SUB(int8_t, uint8_t);
        break;
    case CC_OP_SUBW:
        SET_FLAGS_SUB(int16_t, uint16_t);
        break;
    case CC_OP_SUB:
        SET_FLAGS_SUB(int32_t, uint32_t);
        break;
    case CC_OP_ADDXB:
        SET_FLAGS_ADDX(int8_t, uint8_t);
        break;
    case CC_OP_ADDXW:
        SET_FLAGS_ADDX(int16_t, uint16_t);
        break;
    case CC_OP_ADDX:
        SET_FLAGS_ADDX(int32_t, uint32_t);
        break;
    case CC_OP_SUBXB:
        SET_FLAGS_SUBX(int8_t, uint8_t);
        break;
    case CC_OP_SUBXW:
        SET_FLAGS_SUBX(int16_t, uint16_t);
        break;
    case CC_OP_SUBX:
        SET_FLAGS_SUBX(int32_t, uint32_t);
        break;
    case CC_OP_SHIFTB:
        SET_FLAGS_SHIFT(int8_t);
        break;
    case CC_OP_SHIFTW:
        SET_FLAGS_SHIFT(int16_t);
        break;
    case CC_OP_SHIFT:
        SET_FLAGS_SHIFT(int32_t);
        break;
    default:
        g_assert_not_reached();
    }
    return flags;
}

uint32_t cpu_m68k_get_ccr(CPUM68KState *env)
{
    return cpu_m68k_flush_flags(env, env->cc_op) | env->cc_x * CCF_X;
}

void cpu_m68k_set_ccr(CPUM68KState *env, uint32_t val)
{
    env->cc_op = CC_OP_FLAGS;
    env->cc_dest = val & 0xf;
    env->cc_x = (val & CCF_X ? 1 : 0);
}

void HELPER(movec)(CPUM68KState *env, uint32_t reg, uint32_t val)
{
    M68kCPU *cpu = m68k_env_get_cpu(env);
@@ -413,59 +268,52 @@ uint32_t HELPER(ff1)(uint32_t x)
    return n;
}

uint32_t HELPER(sats)(uint32_t val, uint32_t ccr)
uint32_t HELPER(sats)(uint32_t val, uint32_t v)
{
    /* The result has the opposite sign to the original value.  */
    if (ccr & CCF_V)
    if ((int32_t)v < 0) {
        val = (((int32_t)val) >> 31) ^ SIGNBIT;
    }
    return val;
}

uint32_t HELPER(subx_cc)(CPUM68KState *env, uint32_t op1, uint32_t op2)
{
    uint32_t res;
    uint32_t old_flags;
    int op;
    uint32_t res, new_x;

    old_flags = env->cc_dest;
    if (env->cc_x) {
        env->cc_x = (op1 <= op2);
        op = CC_OP_SUBX;
        new_x = (op1 <= op2);
        res = op1 - (op2 + 1);
    } else {
        env->cc_x = (op1 < op2);
        op = CC_OP_SUB;
        new_x = (op1 < op2);
        res = op1 - op2;
    }
    env->cc_dest = res;
    env->cc_src = op2;
    env->cc_dest = cpu_m68k_flush_flags(env, op);
    /* !Z is sticky.  */
    env->cc_dest &= (old_flags | ~CCF_Z);
    env->cc_x = new_x;
    env->cc_c = new_x;
    env->cc_n = res;
    env->cc_z |= res; /* !Z is sticky */
    env->cc_v = (res ^ op1) & (op1 ^ op2);

    return res;
}

uint32_t HELPER(addx_cc)(CPUM68KState *env, uint32_t op1, uint32_t op2)
{
    uint32_t res;
    uint32_t old_flags;
    int op;
    uint32_t res, new_x;

    old_flags = env->cc_dest;
    if (env->cc_x) {
        res = op1 + op2 + 1;
        env->cc_x = (res <= op2);
        op = CC_OP_ADDX;
        new_x = (res <= op2);
    } else {
        res = op1 + op2;
        env->cc_x = (res < op2);
        op = CC_OP_ADD;
    }
    env->cc_dest = res;
    env->cc_src = op2;
    env->cc_dest = cpu_m68k_flush_flags(env, op);
    /* !Z is sticky.  */
    env->cc_dest &= (old_flags | ~CCF_Z);
        new_x = (res < op2);
    }
    env->cc_x = new_x;
    env->cc_c = new_x;
    env->cc_n = res;
    env->cc_z |= res; /* !Z is sticky.  */
    env->cc_v = (res ^ op1) & ~(op1 ^ op2);

    return res;
}

@@ -478,73 +326,53 @@ void HELPER(set_sr)(CPUM68KState *env, uint32_t val)

uint32_t HELPER(shl_cc)(CPUM68KState *env, uint32_t val, uint32_t shift)
{
    uint32_t result;
    uint32_t cf;
    uint64_t result;

    shift &= 63;
    if (shift == 0) {
        result = val;
        cf = env->cc_src & CCF_C;
    } else if (shift < 32) {
        result = val << shift;
        cf = (val >> (32 - shift)) & 1;
    } else if (shift == 32) {
        result = 0;
        cf = val & 1;
    } else /* shift > 32 */ {
        result = 0;
        cf = 0;
    }
    env->cc_src = cf;
    env->cc_x = (cf != 0);
    env->cc_dest = result;
    result = (uint64_t)val << shift;

    env->cc_c = (result >> 32) & 1;
    env->cc_n = result;
    env->cc_z = result;
    env->cc_v = 0;
    env->cc_x = shift ? env->cc_c : env->cc_x;

    return result;
}

uint32_t HELPER(shr_cc)(CPUM68KState *env, uint32_t val, uint32_t shift)
{
    uint64_t temp;
    uint32_t result;
    uint32_t cf;

    shift &= 63;
    if (shift == 0) {
        result = val;
        cf = env->cc_src & CCF_C;
    } else if (shift < 32) {
        result = val >> shift;
        cf = (val >> (shift - 1)) & 1;
    } else if (shift == 32) {
        result = 0;
        cf = val >> 31;
    } else /* shift > 32 */ {
        result = 0;
        cf = 0;
    }
    env->cc_src = cf;
    env->cc_x = (cf != 0);
    env->cc_dest = result;
    temp = (uint64_t)val << 32 >> shift;
    result = temp >> 32;

    env->cc_c = (temp >> 31) & 1;
    env->cc_n = result;
    env->cc_z = result;
    env->cc_v = 0;
    env->cc_x = shift ? env->cc_c : env->cc_x;

    return result;
}

uint32_t HELPER(sar_cc)(CPUM68KState *env, uint32_t val, uint32_t shift)
{
    uint64_t temp;
    uint32_t result;
    uint32_t cf;

    shift &= 63;
    if (shift == 0) {
        result = val;
        cf = (env->cc_src & CCF_C) != 0;
    } else if (shift < 32) {
        result = (int32_t)val >> shift;
        cf = (val >> (shift - 1)) & 1;
    } else /* shift >= 32 */ {
        result = (int32_t)val >> 31;
        cf = val >> 31;
    }
    env->cc_src = cf;
    env->cc_x = cf;
    env->cc_dest = result;
    temp = (int64_t)val << 32 >> shift;
    result = temp >> 32;

    env->cc_c = (temp >> 31) & 1;
    env->cc_n = result;
    env->cc_z = result;
    env->cc_v = result ^ val;
    env->cc_x = shift ? env->cc_c : env->cc_x;

    return result;
}

@@ -796,9 +624,92 @@ void HELPER(mac_set_flags)(CPUM68KState *env, uint32_t acc)
    }
}

uint32_t HELPER(flush_flags)(CPUM68KState *env, uint32_t op)

#define COMPUTE_CCR(op, x, n, z, v, c) {                                   \
    switch (op) {                                                          \
    case CC_OP_FLAGS:                                                      \
        /* Everything in place.  */                                        \
        break;                                                             \
    case CC_OP_ADD:                                                        \
        res = n;                                                           \
        src2 = v;                                                          \
        src1 = res - src2;                                                 \
        c = x;                                                             \
        z = n;                                                             \
        v = (res ^ src1) & ~(src1 ^ src2);                                 \
        break;                                                             \
    case CC_OP_SUB:                                                        \
        res = n;                                                           \
        src2 = v;                                                          \
        src1 = res + src2;                                                 \
        c = x;                                                             \
        z = n;                                                             \
        v = (res ^ src1) & (src1 ^ src2);                                  \
        break;                                                             \
    case CC_OP_CMP:                                                        \
        src1 = n;                                                          \
        src2 = v;                                                          \
        res = src1 - src2;                                                 \
        n = res;                                                           \
        z = res;                                                           \
        c = src1 < src2;                                                   \
        v = (res ^ src1) & (src1 ^ src2);                                  \
        break;                                                             \
    case CC_OP_LOGIC:                                                      \
        c = v = 0;                                                         \
        z = n;                                                             \
        break;                                                             \
    default:                                                               \
        cpu_abort(CPU(m68k_env_get_cpu(env)), "Bad CC_OP %d", op);         \
    }                                                                      \
} while (0)

uint32_t cpu_m68k_get_ccr(CPUM68KState *env)
{
    uint32_t x, c, n, z, v;
    uint32_t res, src1, src2;

    x = env->cc_x;
    c = env->cc_c;
    n = env->cc_n;
    z = env->cc_z;
    v = env->cc_v;

    COMPUTE_CCR(env->cc_op, x, n, z, v, c);

    n = n >> 31;
    v = v >> 31;
    z = (z == 0);

    return x * CCF_X + n * CCF_N + z * CCF_Z + v * CCF_V + c * CCF_C;
}

uint32_t HELPER(get_ccr)(CPUM68KState *env)
{
    return cpu_m68k_get_ccr(env);
}

void cpu_m68k_set_ccr(CPUM68KState *env, uint32_t ccr)
{
    env->cc_x = (ccr & CCF_X ? 1 : 0);
    env->cc_n = (ccr & CCF_N ? -1 : 0);
    env->cc_z = (ccr & CCF_Z ? 0 : 1);
    env->cc_v = (ccr & CCF_V ? -1 : 0);
    env->cc_c = (ccr & CCF_C ? 1 : 0);
    env->cc_op = CC_OP_FLAGS;
}

void HELPER(set_ccr)(CPUM68KState *env, uint32_t ccr)
{
    return cpu_m68k_flush_flags(env, op);
    cpu_m68k_set_ccr(env, ccr);
}

void HELPER(flush_flags)(CPUM68KState *env, uint32_t cc_op)
{
    uint32_t res, src1, src2;

    COMPUTE_CCR(cc_op, env->cc_x, env->cc_n, env->cc_z, env->cc_v, env->cc_c);
    env->cc_op = CC_OP_FLAGS;
}

uint32_t HELPER(get_macf)(CPUM68KState *env, uint64_t val)
+4 −2
Original line number Diff line number Diff line
DEF_HELPER_1(bitrev, i32, i32)
DEF_HELPER_1(ff1, i32, i32)
DEF_HELPER_2(sats, i32, i32, i32)
DEF_HELPER_FLAGS_2(sats, TCG_CALL_NO_RWG_SE, i32, i32, i32)
DEF_HELPER_2(divu, void, env, i32)
DEF_HELPER_2(divs, void, env, i32)
DEF_HELPER_3(addx_cc, i32, env, i32, i32)
@@ -45,5 +45,7 @@ DEF_HELPER_3(set_mac_extf, void, env, i32, i32)
DEF_HELPER_3(set_mac_exts, void, env, i32, i32)
DEF_HELPER_3(set_mac_extu, void, env, i32, i32)

DEF_HELPER_2(flush_flags, i32, env, i32)
DEF_HELPER_2(flush_flags, void, env, i32)
DEF_HELPER_2(set_ccr, void, env, i32)
DEF_HELPER_FLAGS_1(get_ccr, TCG_CALL_NO_WG_SE, i32, env)
DEF_HELPER_2(raise_exception, void, env, i32)
+12 −18
Original line number Diff line number Diff line
@@ -185,7 +185,6 @@ void HELPER(divu)(CPUM68KState *env, uint32_t word)
    uint32_t den;
    uint32_t quot;
    uint32_t rem;
    uint32_t flags;

    num = env->div1;
    den = env->div2;
@@ -195,16 +194,14 @@ void HELPER(divu)(CPUM68KState *env, uint32_t word)
    }
    quot = num / den;
    rem = num % den;
    flags = 0;
    if (word && quot > 0xffff)
        flags |= CCF_V;
    if (quot == 0)
        flags |= CCF_Z;
    else if ((int32_t)quot < 0)
        flags |= CCF_N;

    env->cc_v = (word && quot > 0xffff ? -1 : 0);
    env->cc_z = quot;
    env->cc_n = quot;
    env->cc_c = 0;

    env->div1 = quot;
    env->div2 = rem;
    env->cc_dest = flags;
}

void HELPER(divs)(CPUM68KState *env, uint32_t word)
@@ -213,7 +210,6 @@ void HELPER(divs)(CPUM68KState *env, uint32_t word)
    int32_t den;
    int32_t quot;
    int32_t rem;
    int32_t flags;

    num = env->div1;
    den = env->div2;
@@ -222,14 +218,12 @@ void HELPER(divs)(CPUM68KState *env, uint32_t word)
    }
    quot = num / den;
    rem = num % den;
    flags = 0;
    if (word && quot != (int16_t)quot)
        flags |= CCF_V;
    if (quot == 0)
        flags |= CCF_Z;
    else if (quot < 0)
        flags |= CCF_N;

    env->cc_v = (word && quot != (int16_t)quot ? -1 : 0);
    env->cc_z = quot;
    env->cc_n = quot;
    env->cc_c = 0;

    env->div1 = quot;
    env->div2 = rem;
    env->cc_dest = flags;
}
Loading