aboutsummaryrefslogtreecommitdiff
path: root/target-m68k/helper.c
diff options
context:
space:
mode:
authorRichard Henderson <rth@twiddle.net>2015-08-14 07:59:20 -0700
committerLaurent Vivier <laurent@vivier.eu>2016-10-25 20:54:47 +0200
commit620c6cf66584bfbee90db84a7e87a6eabf230ca9 (patch)
tree681936c50972ff25b385e52507b374dfd0240646 /target-m68k/helper.c
parent18dd87f26bed46f22bb1b9536329c02de500f407 (diff)
target-m68k: Reorg flags handling
Separate all ccr bits. Continue to batch updates via cc_op. Signed-off-by: Richard 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: Laurent Vivier <laurent@vivier.eu> target-m68k: sr/ccr cleanup Signed-off-by: Laurent Vivier <laurent@vivier.eu>
Diffstat (limited to 'target-m68k/helper.c')
-rw-r--r--target-m68k/helper.c359
1 files changed, 135 insertions, 224 deletions
diff --git a/target-m68k/helper.c b/target-m68k/helper.c
index a9974b1b73..094a7e59a9 100644
--- a/target-m68k/helper.c
+++ b/target-m68k/helper.c
@@ -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;
+ new_x = (res < 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;
}
@@ -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)
{
- return cpu_m68k_flush_flags(env, op);
+ 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)
+{
+ 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)