platform ps2, handle audio similar to psp
[picodrive.git] / cpu / fame / famec_opcodes.h
index 7691899..7a7832c 100644 (file)
@@ -1,4 +1,10 @@
 
+#ifdef PICODRIVE_HACK
+#define NOT_POLLING ctx->not_polling = 1;
+#else
+#define NOT_POLLING
+#endif
+
 // ORI
 OPCODE(0x0000)
 {
@@ -638,7 +644,7 @@ OPCODE(0x007C)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
 #ifdef USE_CYCLONE_TIMING
                RET(0)
 #else
@@ -1068,11 +1074,7 @@ OPCODE(0x0280)
        flag_NotZ = res;
        flag_N = res >> 24;
        DREGu32((Opcode >> 0) & 7) = res;
-#ifdef USE_CYCLONE_TIMING
-RET(14)
-#else
 RET(16)
-#endif
 }
 
 // ANDI
@@ -1296,8 +1298,12 @@ OPCODE(0x027C)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(20)
 }
@@ -1946,8 +1952,12 @@ OPCODE(0x0A7C)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
                RET(0)
+#else
+               RET(4)
+#endif
        }
 RET(20)
 }
@@ -3898,7 +3908,12 @@ OPCODE(0x0840)
        flag_NotZ = res & src;
        res ^= src;
        DREGu32((Opcode >> 0) & 7) = res;
+#ifdef USE_CYCLONE_TIMING
+       if (src >> 16) ctx->io_cycle_counter -= 2;
+RET(10)
+#else
 RET(12)
+#endif
 }
 
 // BCHGn
@@ -4081,7 +4096,12 @@ OPCODE(0x0880)
        flag_NotZ = res & src;
        res &= ~src;
        DREGu32((Opcode >> 0) & 7) = res;
+#ifdef USE_CYCLONE_TIMING
+       if (src >> 16) ctx->io_cycle_counter -= 2;
+RET(12)
+#else
 RET(14)
+#endif
 }
 
 // BCLRn
@@ -4264,7 +4284,12 @@ OPCODE(0x08C0)
        flag_NotZ = res & src;
        res |= src;
        DREGu32((Opcode >> 0) & 7) = res;
+#ifdef USE_CYCLONE_TIMING
+       if (src >> 16) ctx->io_cycle_counter -= 2;
+RET(10)
+#else
 RET(12)
+#endif
 }
 
 // BSETn
@@ -4608,7 +4633,7 @@ OPCODE(0x013C)
        src = 1 << (src & 7);
        FETCH_BYTE(res);
        flag_NotZ = res & src;
-RET(8)
+RET(10)
 }
 
 // BTST
@@ -4657,7 +4682,12 @@ OPCODE(0x0140)
        flag_NotZ = res & src;
        res ^= src;
        DREGu32((Opcode >> 0) & 7) = res;
+#ifdef USE_CYCLONE_TIMING
+       if (src >> 16) ctx->io_cycle_counter -= 2;
+RET(6)
+#else
 RET(8)
+#endif
 }
 
 // BCHG
@@ -4840,7 +4870,12 @@ OPCODE(0x0180)
        flag_NotZ = res & src;
        res &= ~src;
        DREGu32((Opcode >> 0) & 7) = res;
+#ifdef USE_CYCLONE_TIMING
+       if (src >> 16) ctx->io_cycle_counter -= 2;
+RET(8)
+#else
 RET(10)
+#endif
 }
 
 // BCLR
@@ -5023,7 +5058,12 @@ OPCODE(0x01C0)
        flag_NotZ = res & src;
        res |= src;
        DREGu32((Opcode >> 0) & 7) = res;
+#ifdef USE_CYCLONE_TIMING
+       if (src >> 16) ctx->io_cycle_counter -= 2;
+RET(6)
+#else
 RET(8)
+#endif
 }
 
 // BSET
@@ -5478,8 +5518,8 @@ OPCODE(0x1008)
        u32 src, dst;
 
        // can't read byte from Ax registers !
-       m68kcontext.execinfo |= M68K_FAULTED;
-       m68kcontext.io_cycle_counter = 0;
+       ctx->execinfo |= M68K_FAULTED;
+       ctx->io_cycle_counter = 0;
 /*
        goto famec_Exec_End;
        flag_C = 0;
@@ -5498,8 +5538,8 @@ OPCODE(0x1088)
        u32 src, dst;
 
        // can't read byte from Ax registers !
-       m68kcontext.execinfo |= M68K_FAULTED;
-       m68kcontext.io_cycle_counter = 0;
+       ctx->execinfo |= M68K_FAULTED;
+       ctx->io_cycle_counter = 0;
 /*
        goto famec_Exec_End;
        flag_C = 0;
@@ -5521,8 +5561,8 @@ OPCODE(0x10C8)
        u32 src, dst;
 
        // can't read byte from Ax registers !
-       m68kcontext.execinfo |= M68K_FAULTED;
-       m68kcontext.io_cycle_counter = 0;
+       ctx->execinfo |= M68K_FAULTED;
+       ctx->io_cycle_counter = 0;
 /*
        goto famec_Exec_End;
        flag_C = 0;
@@ -5545,8 +5585,8 @@ OPCODE(0x1108)
        u32 src, dst;
 
        // can't read byte from Ax registers !
-       m68kcontext.execinfo |= M68K_FAULTED;
-       m68kcontext.io_cycle_counter = 0;
+       ctx->execinfo |= M68K_FAULTED;
+       ctx->io_cycle_counter = 0;
 /*
        goto famec_Exec_End;
        flag_C = 0;
@@ -5569,8 +5609,8 @@ OPCODE(0x1148)
        u32 src, dst;
 
        // can't read byte from Ax registers !
-       m68kcontext.execinfo |= M68K_FAULTED;
-       m68kcontext.io_cycle_counter = 0;
+       ctx->execinfo |= M68K_FAULTED;
+       ctx->io_cycle_counter = 0;
 /*
        goto famec_Exec_End;
        flag_C = 0;
@@ -5593,8 +5633,8 @@ OPCODE(0x1188)
        u32 src, dst;
 
        // can't read byte from Ax registers !
-       m68kcontext.execinfo |= M68K_FAULTED;
-       m68kcontext.io_cycle_counter = 0;
+       ctx->execinfo |= M68K_FAULTED;
+       ctx->io_cycle_counter = 0;
 /*
        goto famec_Exec_End;
        flag_C = 0;
@@ -5617,8 +5657,8 @@ OPCODE(0x11C8)
        u32 src, dst;
 
        // can't read byte from Ax registers !
-       m68kcontext.execinfo |= M68K_FAULTED;
-       m68kcontext.io_cycle_counter = 0;
+       ctx->execinfo |= M68K_FAULTED;
+       ctx->io_cycle_counter = 0;
 /*
        goto famec_Exec_End;
        flag_C = 0;
@@ -5640,8 +5680,8 @@ OPCODE(0x13C8)
        u32 src, dst;
 
        // can't read byte from Ax registers !
-       m68kcontext.execinfo |= M68K_FAULTED;
-       m68kcontext.io_cycle_counter = 0;
+       ctx->execinfo |= M68K_FAULTED;
+       ctx->io_cycle_counter = 0;
 /*
        goto famec_Exec_End;
        flag_C = 0;
@@ -5663,8 +5703,8 @@ OPCODE(0x1EC8)
        u32 src, dst;
 
        // can't read byte from Ax registers !
-       m68kcontext.execinfo |= M68K_FAULTED;
-       m68kcontext.io_cycle_counter = 0;
+       ctx->execinfo |= M68K_FAULTED;
+       ctx->io_cycle_counter = 0;
 /*
        goto famec_Exec_End;
        flag_C = 0;
@@ -5687,8 +5727,8 @@ OPCODE(0x1F08)
        u32 src, dst;
 
        // can't read byte from Ax registers !
-       m68kcontext.execinfo |= M68K_FAULTED;
-       m68kcontext.io_cycle_counter = 0;
+       ctx->execinfo |= M68K_FAULTED;
+       ctx->io_cycle_counter = 0;
 /*
        goto famec_Exec_End;
        flag_C = 0;
@@ -16564,8 +16604,12 @@ OPCODE(0x46C0)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(12)
 }
@@ -16593,8 +16637,12 @@ OPCODE(0x46D0)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(16)
 }
@@ -16623,8 +16671,12 @@ OPCODE(0x46D8)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(16)
 }
@@ -16653,8 +16705,12 @@ OPCODE(0x46E0)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(18)
 }
@@ -16683,8 +16739,12 @@ OPCODE(0x46E8)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(20)
 }
@@ -16713,8 +16773,12 @@ OPCODE(0x46F0)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(22)
 }
@@ -16743,8 +16807,12 @@ OPCODE(0x46F8)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(20)
 }
@@ -16772,8 +16840,12 @@ OPCODE(0x46F9)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(24)
 }
@@ -16802,8 +16874,12 @@ OPCODE(0x46FA)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(20)
 }
@@ -16832,8 +16908,12 @@ OPCODE(0x46FB)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(22)
 }
@@ -16858,8 +16938,12 @@ OPCODE(0x46FC)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(16)
 }
@@ -16888,8 +16972,12 @@ OPCODE(0x46DF)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(16)
 }
@@ -16918,8 +17006,12 @@ OPCODE(0x46E7)
        }
        else
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+               RET(0)
+#else
                RET(4)
+#endif
        }
 RET(18)
 }
@@ -16930,18 +17022,20 @@ OPCODE(0x4800)
        u32 adr, res;
        u32 src, dst;
 
-       res = DREGu8((Opcode >> 0) & 7);
-       res = 0x9a - res - ((flag_X >> M68K_SR_X_SFT) & 1);
+       dst = DREGu8((Opcode >> 0) & 7);
+       res = -dst - ((flag_X >> M68K_SR_X_SFT) & 1);
 
-       if (res != 0x9a)
+       if (res != 0)
        {
-               if ((res & 0x0f) == 0xa) res = (res & 0xf0) + 0x10;
-               res &= 0xFF;
-       DREGu8((Opcode >> 0) & 7) = res;
+               flag_V = res;
+               if (((res|dst) & 0x0f) == 0) res = (res & 0xf0) + 6;
+               res = (res + 0x9a) & 0xFF;
+               DREGu8((Opcode >> 0) & 7) = res;
+               flag_V &= ~res;
                flag_NotZ |= res;
                flag_X = flag_C = M68K_SR_C;
        }
-       else flag_X = flag_C = 0;
+       else flag_V = flag_X = flag_C = 0;
        flag_N = res;
 RET(6)
 }
@@ -16954,18 +17048,20 @@ OPCODE(0x4810)
 
        adr = AREG((Opcode >> 0) & 7);
        PRE_IO
-       READ_BYTE_F(adr, res)
-       res = 0x9a - res - ((flag_X >> M68K_SR_X_SFT) & 1);
+       READ_BYTE_F(adr, dst)
+       res = -dst - ((flag_X >> M68K_SR_X_SFT) & 1);
 
-       if (res != 0x9a)
+       if (res != 0)
        {
-               if ((res & 0x0f) == 0xa) res = (res & 0xf0) + 0x10;
-               res &= 0xFF;
-       WRITE_BYTE_F(adr, res)
+               flag_V = res;
+               if (((res|dst) & 0x0f) == 0) res = (res & 0xf0) + 6;
+               res = (res + 0x9a) & 0xFF;
+               WRITE_BYTE_F(adr, res)
+               flag_V &= ~res;
                flag_NotZ |= res;
                flag_X = flag_C = M68K_SR_C;
        }
-       else flag_X = flag_C = 0;
+       else flag_V = flag_X = flag_C = 0;
        flag_N = res;
        POST_IO
 RET(12)
@@ -16980,18 +17076,20 @@ OPCODE(0x4818)
        adr = AREG((Opcode >> 0) & 7);
        AREG((Opcode >> 0) & 7) += 1;
        PRE_IO
-       READ_BYTE_F(adr, res)
-       res = 0x9a - res - ((flag_X >> M68K_SR_X_SFT) & 1);
+       READ_BYTE_F(adr, dst)
+       res = -dst - ((flag_X >> M68K_SR_X_SFT) & 1);
 
-       if (res != 0x9a)
+       if (res != 0)
        {
-               if ((res & 0x0f) == 0xa) res = (res & 0xf0) + 0x10;
-               res &= 0xFF;
-       WRITE_BYTE_F(adr, res)
+               flag_V = res;
+               if (((res|dst) & 0x0f) == 0) res = (res & 0xf0) + 6;
+               res = (res + 0x9a) & 0xFF;
+               WRITE_BYTE_F(adr, res)
+               flag_V &= ~res;
                flag_NotZ |= res;
                flag_X = flag_C = M68K_SR_C;
        }
-       else flag_X = flag_C = 0;
+       else flag_V = flag_X = flag_C = 0;
        flag_N = res;
        POST_IO
 RET(12)
@@ -17006,18 +17104,20 @@ OPCODE(0x4820)
        adr = AREG((Opcode >> 0) & 7) - 1;
        AREG((Opcode >> 0) & 7) = adr;
        PRE_IO
-       READ_BYTE_F(adr, res)
-       res = 0x9a - res - ((flag_X >> M68K_SR_X_SFT) & 1);
+       READ_BYTE_F(adr, dst)
+       res = -dst - ((flag_X >> M68K_SR_X_SFT) & 1);
 
-       if (res != 0x9a)
+       if (res != 0)
        {
-               if ((res & 0x0f) == 0xa) res = (res & 0xf0) + 0x10;
-               res &= 0xFF;
-       WRITE_BYTE_F(adr, res)
+               flag_V = res;
+               if (((res|dst) & 0x0f) == 0) res = (res & 0xf0) + 6;
+               res = (res + 0x9a) & 0xFF;
+               WRITE_BYTE_F(adr, res)
+               flag_V &= ~res;
                flag_NotZ |= res;
                flag_X = flag_C = M68K_SR_C;
        }
-       else flag_X = flag_C = 0;
+       else flag_V = flag_X = flag_C = 0;
        flag_N = res;
        POST_IO
 RET(14)
@@ -17032,18 +17132,20 @@ OPCODE(0x4828)
        FETCH_SWORD(adr);
        adr += AREG((Opcode >> 0) & 7);
        PRE_IO
-       READ_BYTE_F(adr, res)
-       res = 0x9a - res - ((flag_X >> M68K_SR_X_SFT) & 1);
+       READ_BYTE_F(adr, dst)
+       res = -dst - ((flag_X >> M68K_SR_X_SFT) & 1);
 
-       if (res != 0x9a)
+       if (res != 0)
        {
-               if ((res & 0x0f) == 0xa) res = (res & 0xf0) + 0x10;
-               res &= 0xFF;
-       WRITE_BYTE_F(adr, res)
+               flag_V = res;
+               if (((res|dst) & 0x0f) == 0) res = (res & 0xf0) + 6;
+               res = (res + 0x9a) & 0xFF;
+               WRITE_BYTE_F(adr, res)
+               flag_V &= ~res;
                flag_NotZ |= res;
                flag_X = flag_C = M68K_SR_C;
        }
-       else flag_X = flag_C = 0;
+       else flag_V = flag_X = flag_C = 0;
        flag_N = res;
        POST_IO
 RET(16)
@@ -17058,18 +17160,20 @@ OPCODE(0x4830)
        adr = AREG((Opcode >> 0) & 7);
        DECODE_EXT_WORD
        PRE_IO
-       READ_BYTE_F(adr, res)
-       res = 0x9a - res - ((flag_X >> M68K_SR_X_SFT) & 1);
+       READ_BYTE_F(adr, dst)
+       res = -dst - ((flag_X >> M68K_SR_X_SFT) & 1);
 
-       if (res != 0x9a)
+       if (res != 0)
        {
-               if ((res & 0x0f) == 0xa) res = (res & 0xf0) + 0x10;
-               res &= 0xFF;
-       WRITE_BYTE_F(adr, res)
+               flag_V = res;
+               if (((res|dst) & 0x0f) == 0) res = (res & 0xf0) + 6;
+               res = (res + 0x9a) & 0xFF;
+               WRITE_BYTE_F(adr, res)
+               flag_V &= ~res;
                flag_NotZ |= res;
                flag_X = flag_C = M68K_SR_C;
        }
-       else flag_X = flag_C = 0;
+       else flag_V = flag_X = flag_C = 0;
        flag_N = res;
        POST_IO
 RET(18)
@@ -17083,18 +17187,20 @@ OPCODE(0x4838)
 
        FETCH_SWORD(adr);
        PRE_IO
-       READ_BYTE_F(adr, res)
-       res = 0x9a - res - ((flag_X >> M68K_SR_X_SFT) & 1);
+       READ_BYTE_F(adr, dst)
+       res = -dst - ((flag_X >> M68K_SR_X_SFT) & 1);
 
-       if (res != 0x9a)
+       if (res != 0)
        {
-               if ((res & 0x0f) == 0xa) res = (res & 0xf0) + 0x10;
-               res &= 0xFF;
-       WRITE_BYTE_F(adr, res)
+               flag_V = res;
+               if (((res|dst) & 0x0f) == 0) res = (res & 0xf0) + 6;
+               res = (res + 0x9a) & 0xFF;
+               WRITE_BYTE_F(adr, res)
+               flag_V &= ~res;
                flag_NotZ |= res;
                flag_X = flag_C = M68K_SR_C;
        }
-       else flag_X = flag_C = 0;
+       else flag_V = flag_X = flag_C = 0;
        flag_N = res;
        POST_IO
 RET(16)
@@ -17108,18 +17214,20 @@ OPCODE(0x4839)
 
        FETCH_LONG(adr);
        PRE_IO
-       READ_BYTE_F(adr, res)
-       res = 0x9a - res - ((flag_X >> M68K_SR_X_SFT) & 1);
+       READ_BYTE_F(adr, dst)
+       res = -dst - ((flag_X >> M68K_SR_X_SFT) & 1);
 
-       if (res != 0x9a)
+       if (res != 0)
        {
-               if ((res & 0x0f) == 0xa) res = (res & 0xf0) + 0x10;
-               res &= 0xFF;
-       WRITE_BYTE_F(adr, res)
+               flag_V = res;
+               if (((res|dst) & 0x0f) == 0) res = (res & 0xf0) + 6;
+               res = (res + 0x9a) & 0xFF;
+               WRITE_BYTE_F(adr, res)
+               flag_V &= ~res;
                flag_NotZ |= res;
                flag_X = flag_C = M68K_SR_C;
        }
-       else flag_X = flag_C = 0;
+       else flag_V = flag_X = flag_C = 0;
        flag_N = res;
        POST_IO
 RET(20)
@@ -17134,18 +17242,20 @@ OPCODE(0x481F)
        adr = AREG(7);
        AREG(7) += 2;
        PRE_IO
-       READ_BYTE_F(adr, res)
-       res = 0x9a - res - ((flag_X >> M68K_SR_X_SFT) & 1);
+       READ_BYTE_F(adr, dst)
+       res = -dst - ((flag_X >> M68K_SR_X_SFT) & 1);
 
-       if (res != 0x9a)
+       if (res != 0)
        {
-               if ((res & 0x0f) == 0xa) res = (res & 0xf0) + 0x10;
-               res &= 0xFF;
-       WRITE_BYTE_F(adr, res)
+               flag_V = res;
+               if (((res|dst) & 0x0f) == 0) res = (res & 0xf0) + 6;
+               res = (res + 0x9a) & 0xFF;
+               WRITE_BYTE_F(adr, res)
+               flag_V &= ~res;
                flag_NotZ |= res;
                flag_X = flag_C = M68K_SR_C;
        }
-       else flag_X = flag_C = 0;
+       else flag_V = flag_X = flag_C = 0;
        flag_N = res;
        POST_IO
 RET(12)
@@ -17160,18 +17270,20 @@ OPCODE(0x4827)
        adr = AREG(7) - 2;
        AREG(7) = adr;
        PRE_IO
-       READ_BYTE_F(adr, res)
-       res = 0x9a - res - ((flag_X >> M68K_SR_X_SFT) & 1);
+       READ_BYTE_F(adr, dst)
+       res = -dst - ((flag_X >> M68K_SR_X_SFT) & 1);
 
-       if (res != 0x9a)
+       if (res != 0)
        {
-               if ((res & 0x0f) == 0xa) res = (res & 0xf0) + 0x10;
-               res &= 0xFF;
-       WRITE_BYTE_F(adr, res)
+               flag_V = res;
+               if (((res|dst) & 0x0f) == 0) res = (res & 0xf0) + 6;
+               res = (res + 0x9a) & 0xFF;
+               WRITE_BYTE_F(adr, res)
+               flag_V &= ~res;
                flag_NotZ |= res;
                flag_X = flag_C = M68K_SR_C;
        }
-       else flag_X = flag_C = 0;
+       else flag_V = flag_X = flag_C = 0;
        flag_N = res;
        POST_IO
 RET(14)
@@ -17311,7 +17423,7 @@ OPCODE(0x4890)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(8)
 #else
@@ -17343,7 +17455,7 @@ OPCODE(0x48A0)
        } while (res >>= 1);
        AREG((Opcode >> 0) & 7) = adr;
        POST_IO
-       m68kcontext.io_cycle_counter -= (dst - adr) * 2;
+       ctx->io_cycle_counter -= (dst - adr) * 2;
 RET(8)
 }
 
@@ -17371,7 +17483,7 @@ OPCODE(0x48A8)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(12)
 #else
@@ -17403,7 +17515,7 @@ OPCODE(0x48B0)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(14)
 #else
@@ -17434,7 +17546,7 @@ OPCODE(0x48B8)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(12)
 #else
@@ -17465,7 +17577,7 @@ OPCODE(0x48B9)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(16)
 #else
@@ -17497,7 +17609,7 @@ OPCODE(0x48A7)
        } while (res >>= 1);
        AREG(7) = adr;
        POST_IO
-       m68kcontext.io_cycle_counter -= (dst - adr) * 2;
+       ctx->io_cycle_counter -= (dst - adr) * 2;
 RET(8)
 }
 
@@ -17524,7 +17636,7 @@ OPCODE(0x48D0)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(8)
 #else
@@ -17556,7 +17668,7 @@ OPCODE(0x48E0)
        } while (res >>= 1);
        AREG((Opcode >> 0) & 7) = adr;
        POST_IO
-       m68kcontext.io_cycle_counter -= (dst - adr) * 2;
+       ctx->io_cycle_counter -= (dst - adr) * 2;
 RET(8)
 }
 
@@ -17584,7 +17696,7 @@ OPCODE(0x48E8)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(12)
 #else
@@ -17616,7 +17728,7 @@ OPCODE(0x48F0)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(14)
 #else
@@ -17647,7 +17759,7 @@ OPCODE(0x48F8)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(12)
 #else
@@ -17678,7 +17790,7 @@ OPCODE(0x48F9)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(16)
 #else
@@ -17710,7 +17822,7 @@ OPCODE(0x48E7)
        } while (res >>= 1);
        AREG(7) = adr;
        POST_IO
-       m68kcontext.io_cycle_counter -= (dst - adr) * 2;
+       ctx->io_cycle_counter -= (dst - adr) * 2;
 RET(8)
 }
 
@@ -18293,7 +18405,7 @@ OPCODE(0x4AD0)
        flag_NotZ = res;
        flag_N = res;
 #ifdef PICODRIVE_HACK
-       if (g_m68kcontext == &PicoCpuFS68k) {
+       if (ctx == &PicoCpuFS68k) {
                res |= 0x80;
                WRITE_BYTE_F(adr, res);
        }
@@ -18301,7 +18413,7 @@ OPCODE(0x4AD0)
 
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(18)
+RET(14)
 #else
 RET(8)
 #endif
@@ -18323,7 +18435,7 @@ OPCODE(0x4AD8)
        flag_N = res;
 
 #ifdef PICODRIVE_HACK
-       if (g_m68kcontext == &PicoCpuFS68k) {
+       if (ctx == &PicoCpuFS68k) {
                res |= 0x80;
                WRITE_BYTE_F(adr, res);
        }
@@ -18331,7 +18443,7 @@ OPCODE(0x4AD8)
 
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(18)
+RET(14)
 #else
 RET(8)
 #endif
@@ -18353,7 +18465,7 @@ OPCODE(0x4AE0)
        flag_N = res;
 
 #ifdef PICODRIVE_HACK
-       if (g_m68kcontext == &PicoCpuFS68k) {
+       if (ctx == &PicoCpuFS68k) {
                res |= 0x80;
                WRITE_BYTE_F(adr, res);
        }
@@ -18361,7 +18473,7 @@ OPCODE(0x4AE0)
 
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(20)
+RET(16)
 #else
 RET(10)
 #endif
@@ -18383,7 +18495,7 @@ OPCODE(0x4AE8)
        flag_N = res;
 
 #ifdef PICODRIVE_HACK
-       if (g_m68kcontext == &PicoCpuFS68k) {
+       if (ctx == &PicoCpuFS68k) {
                res |= 0x80;
                WRITE_BYTE_F(adr, res);
        }
@@ -18391,7 +18503,7 @@ OPCODE(0x4AE8)
 
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(22)
+RET(18)
 #else
 RET(12)
 #endif
@@ -18413,7 +18525,7 @@ OPCODE(0x4AF0)
        flag_N = res;
 
 #ifdef PICODRIVE_HACK
-       if (g_m68kcontext == &PicoCpuFS68k) {
+       if (ctx == &PicoCpuFS68k) {
                res |= 0x80;
                WRITE_BYTE_F(adr, res);
        }
@@ -18421,7 +18533,7 @@ OPCODE(0x4AF0)
 
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(24)
+RET(20)
 #else
 RET(14)
 #endif
@@ -18442,7 +18554,7 @@ OPCODE(0x4AF8)
        flag_N = res;
 
 #ifdef PICODRIVE_HACK
-       if (g_m68kcontext == &PicoCpuFS68k) {
+       if (ctx == &PicoCpuFS68k) {
                res |= 0x80;
                WRITE_BYTE_F(adr, res);
        }
@@ -18450,7 +18562,7 @@ OPCODE(0x4AF8)
 
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(22)
+RET(18)
 #else
 RET(12)
 #endif
@@ -18471,7 +18583,7 @@ OPCODE(0x4AF9)
        flag_N = res;
 
 #ifdef PICODRIVE_HACK
-       if (g_m68kcontext == &PicoCpuFS68k) {
+       if (ctx == &PicoCpuFS68k) {
                res |= 0x80;
                WRITE_BYTE_F(adr, res);
        }
@@ -18479,7 +18591,7 @@ OPCODE(0x4AF9)
 
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(26)
+RET(22)
 #else
 RET(16)
 #endif
@@ -18501,7 +18613,7 @@ OPCODE(0x4ADF)
        flag_N = res;
 
 #ifdef PICODRIVE_HACK
-       if (g_m68kcontext == &PicoCpuFS68k) {
+       if (ctx == &PicoCpuFS68k) {
                res |= 0x80;
                WRITE_BYTE_F(adr, res);
        }
@@ -18509,7 +18621,7 @@ OPCODE(0x4ADF)
 
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(18)
+RET(14)
 #else
 RET(8)
 #endif
@@ -18531,7 +18643,7 @@ OPCODE(0x4AE7)
        flag_N = res;
 
 #ifdef PICODRIVE_HACK
-       if (g_m68kcontext == &PicoCpuFS68k) {
+       if (ctx == &PicoCpuFS68k) {
                res |= 0x80;
                WRITE_BYTE_F(adr, res);
        }
@@ -18548,22 +18660,26 @@ RET(8)
 // ILLEGAL
 OPCODE(0x4AFC)
 {
-       SET_PC(execute_exception(M68K_ILLEGAL_INSTRUCTION_EX, GET_PC-2, GET_SR));
+#ifdef PICODRIVE_HACK
+       extern void SekFinishIdleDet(void);
+       SekFinishIdleDet();
+#endif
+       SET_PC(execute_exception(ctx, M68K_ILLEGAL_INSTRUCTION_EX, GET_PC-2, GET_SR));
 RET(0)
 }
 
 // ILLEGAL A000-AFFF
 OPCODE(0xA000)
 {
-       SET_PC(execute_exception(M68K_1010_EX, GET_PC-2, GET_SR));
+       SET_PC(execute_exception(ctx, M68K_1010_EX, GET_PC-2, GET_SR));
 RET(0)
 }
 
 // ILLEGAL F000-FFFF
 OPCODE(0xF000)
 {
-       SET_PC(execute_exception(M68K_1111_EX, GET_PC-2, GET_SR));
-RET(0) // 4 already taken by exc. handler
+       SET_PC(execute_exception(ctx, M68K_1111_EX, GET_PC-2, GET_SR));
+RET(0) 
 }
 
 // MOVEMaR
@@ -18589,7 +18705,7 @@ OPCODE(0x4C90)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(12)
 #else
@@ -18621,7 +18737,7 @@ OPCODE(0x4C98)
        } while (res >>= 1);
        AREG((Opcode >> 0) & 7) = adr;
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 RET(12)
 }
 
@@ -18649,7 +18765,7 @@ OPCODE(0x4CA8)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(16)
 #else
@@ -18681,7 +18797,7 @@ OPCODE(0x4CB0)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(18)
 #else
@@ -18712,7 +18828,7 @@ OPCODE(0x4CB8)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(16)
 #else
@@ -18743,7 +18859,7 @@ OPCODE(0x4CB9)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(20)
 #else
@@ -18775,7 +18891,7 @@ OPCODE(0x4CBA)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(16)
 #else
@@ -18807,7 +18923,7 @@ OPCODE(0x4CBB)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(18)
 #else
@@ -18839,7 +18955,7 @@ OPCODE(0x4C9F)
        } while (res >>= 1);
        AREG(7) = adr;
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 RET(12)
 }
 
@@ -18866,7 +18982,7 @@ OPCODE(0x4CD0)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(12)
 #else
@@ -18898,7 +19014,7 @@ OPCODE(0x4CD8)
        } while (res >>= 1);
        AREG((Opcode >> 0) & 7) = adr;
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 RET(12)
 }
 
@@ -18926,7 +19042,7 @@ OPCODE(0x4CE8)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(16)
 #else
@@ -18958,7 +19074,7 @@ OPCODE(0x4CF0)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(18)
 #else
@@ -18989,7 +19105,7 @@ OPCODE(0x4CF8)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(16)
 #else
@@ -19020,7 +19136,7 @@ OPCODE(0x4CF9)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(20)
 #else
@@ -19052,7 +19168,7 @@ OPCODE(0x4CFA)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(16)
 #else
@@ -19084,7 +19200,7 @@ OPCODE(0x4CFB)
                psrc++;
        } while (res >>= 1);
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 #ifdef USE_CYCLONE_TIMING
 RET(18)
 #else
@@ -19116,15 +19232,19 @@ OPCODE(0x4CDF)
        } while (res >>= 1);
        AREG(7) = adr;
        POST_IO
-       m68kcontext.io_cycle_counter -= (adr - dst) * 2;
+       ctx->io_cycle_counter -= (adr - dst) * 2;
 RET(12)
 }
 
 // TRAP
 OPCODE(0x4E40)
 {
-       SET_PC(execute_exception(M68K_TRAP_BASE_EX + (Opcode & 0xF), GET_PC, GET_SR));
+       SET_PC(execute_exception(ctx, M68K_TRAP_BASE_EX + (Opcode & 0xF), GET_PC, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+RET(0)
+#else
 RET(4)
+#endif
 }
 
 // LINK
@@ -19194,8 +19314,12 @@ OPCODE(0x4E60)
 
        if (!flag_S)
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+RET(0)
+#else
                RET(4)
+#endif
        }
        res = AREGu32((Opcode >> 0) & 7);
        ASP = res;
@@ -19210,8 +19334,12 @@ OPCODE(0x4E68)
 
        if (!flag_S)
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+RET(0)
+#else
                RET(4)
+#endif
        }
        res = ASP;
        AREG((Opcode >> 0) & 7) = res;
@@ -19226,11 +19354,15 @@ OPCODE(0x4E70)
 
        if (!flag_S)
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+RET(0)
+#else
                RET(4)
+#endif
        }
        PRE_IO
-       if (m68kcontext.reset_handler) m68kcontext.reset_handler();
+       if (ctx->reset_handler) ctx->reset_handler();
 //     CPU->Reset_CallBack();
        POST_IO
 RET(132)
@@ -19250,8 +19382,12 @@ OPCODE(0x4E72)
 
        if (!flag_S)
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+RET(0)
+#else
                RET(4)
+#endif
        }
        FETCH_WORD(res);
        res &= M68K_SR_MASK;
@@ -19262,7 +19398,7 @@ OPCODE(0x4E72)
                AREG(7) = ASP;
                ASP = res;
        }
-       m68kcontext.execinfo |= FM68K_HALTED;
+       ctx->execinfo |= FM68K_HALTED;
 RET0()
 }
 
@@ -19274,8 +19410,12 @@ OPCODE(0x4E73)
 
        if (!flag_S)
        {
-               SET_PC(execute_exception(M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_PRIVILEGE_VIOLATION_EX, GET_PC-2, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+RET(0)
+#else
                RET(4)
+#endif
        }
        PRE_IO
        POP_16_F(res)
@@ -19288,8 +19428,9 @@ OPCODE(0x4E73)
                AREG(7) = ASP;
                ASP = res;
        }
+       CHECK_BRANCH_EXCEPTION(res)
        POST_IO
-       m68kcontext.execinfo &= ~(FM68K_EMULATE_GROUP_0|FM68K_EMULATE_TRACE|FM68K_DO_TRACE);
+       ctx->execinfo &= ~(FM68K_EMULATE_GROUP_0|FM68K_EMULATE_TRACE|FM68K_DO_TRACE);
        CHECK_INT_TO_JUMP(20)
 RET(20)
 }
@@ -19311,8 +19452,12 @@ RET(16)
 // TRAPV
 OPCODE(0x4E76)
 {
-       if (flag_V & 0x80)
-               SET_PC(execute_exception(M68K_TRAPV_EX, GET_PC, GET_SR));
+       if (flag_V & 0x80) {
+               SET_PC(execute_exception(ctx, M68K_TRAPV_EX, GET_PC, GET_SR));
+#ifdef USE_CYCLONE_TIMING
+RET(0)
+#endif
+       }
 RET(4)
 }
 
@@ -19567,15 +19712,15 @@ RET(14)
 // CHK
 OPCODE(0x4180)
 {
-       u32 adr, res;
-       u32 src, dst;
+       s32 src, res;
 
-       src = DREGu16((Opcode >> 0) & 7);
-       res = DREGu16((Opcode >> 9) & 7);
-       if (((s32)res < 0) || (res > src))
+       src = DREGs16((Opcode >> 0) & 7);
+       res = DREGs16((Opcode >> 9) & 7);
+       if ((res < 0) || (res > src))
        {
                flag_N = res >> 8;
-               SET_PC(execute_exception(M68K_CHK_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_CHK_EX, GET_PC, GET_SR));
+RET(4)
        }
 RET(10)
 }
@@ -19583,17 +19728,18 @@ RET(10)
 // CHK
 OPCODE(0x4190)
 {
-       u32 adr, res;
-       u32 src, dst;
+       s32 src, res;
+       u32 adr;
 
        adr = AREG((Opcode >> 0) & 7);
        PRE_IO
-       READ_WORD_F(adr, src)
-       res = DREGu16((Opcode >> 9) & 7);
-       if (((s32)res < 0) || (res > src))
+       READSX_WORD_F(adr, src)
+       res = DREGs16((Opcode >> 9) & 7);
+       if ((res < 0) || (res > src))
        {
                flag_N = res >> 8;
-               SET_PC(execute_exception(M68K_CHK_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_CHK_EX, GET_PC, GET_SR));
+RET(8)
        }
        POST_IO
 RET(14)
@@ -19602,18 +19748,19 @@ RET(14)
 // CHK
 OPCODE(0x4198)
 {
-       u32 adr, res;
-       u32 src, dst;
+       s32 src, res;
+       u32 adr;
 
        adr = AREG((Opcode >> 0) & 7);
        AREG((Opcode >> 0) & 7) += 2;
        PRE_IO
-       READ_WORD_F(adr, src)
-       res = DREGu16((Opcode >> 9) & 7);
-       if (((s32)res < 0) || (res > src))
+       READSX_WORD_F(adr, src)
+       res = DREGs16((Opcode >> 9) & 7);
+       if ((res < 0) || (res > src))
        {
                flag_N = res >> 8;
-               SET_PC(execute_exception(M68K_CHK_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_CHK_EX, GET_PC, GET_SR));
+RET(8)
        }
        POST_IO
 RET(14)
@@ -19622,18 +19769,19 @@ RET(14)
 // CHK
 OPCODE(0x41A0)
 {
-       u32 adr, res;
-       u32 src, dst;
+       s32 src, res;
+       u32 adr;
 
        adr = AREG((Opcode >> 0) & 7) - 2;
        AREG((Opcode >> 0) & 7) = adr;
        PRE_IO
-       READ_WORD_F(adr, src)
-       res = DREGu16((Opcode >> 9) & 7);
-       if (((s32)res < 0) || (res > src))
+       READSX_WORD_F(adr, src)
+       res = DREGs16((Opcode >> 9) & 7);
+       if ((res < 0) || (res > src))
        {
                flag_N = res >> 8;
-               SET_PC(execute_exception(M68K_CHK_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_CHK_EX, GET_PC, GET_SR));
+RET(10)
        }
        POST_IO
 RET(16)
@@ -19642,18 +19790,19 @@ RET(16)
 // CHK
 OPCODE(0x41A8)
 {
-       u32 adr, res;
-       u32 src, dst;
+       s32 src, res;
+       u32 adr;
 
        FETCH_SWORD(adr);
        adr += AREG((Opcode >> 0) & 7);
        PRE_IO
-       READ_WORD_F(adr, src)
-       res = DREGu16((Opcode >> 9) & 7);
-       if (((s32)res < 0) || (res > src))
+       READSX_WORD_F(adr, src)
+       res = DREGs16((Opcode >> 9) & 7);
+       if ((res < 0) || (res > src))
        {
                flag_N = res >> 8;
-               SET_PC(execute_exception(M68K_CHK_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_CHK_EX, GET_PC, GET_SR));
+RET(12)
        }
        POST_IO
 RET(18)
@@ -19662,18 +19811,19 @@ RET(18)
 // CHK
 OPCODE(0x41B0)
 {
-       u32 adr, res;
-       u32 src, dst;
+       s32 src, res;
+       u32 adr;
 
        adr = AREG((Opcode >> 0) & 7);
        DECODE_EXT_WORD
        PRE_IO
-       READ_WORD_F(adr, src)
-       res = DREGu16((Opcode >> 9) & 7);
-       if (((s32)res < 0) || (res > src))
+       READSX_WORD_F(adr, src)
+       res = DREGs16((Opcode >> 9) & 7);
+       if ((res < 0) || (res > src))
        {
                flag_N = res >> 8;
-               SET_PC(execute_exception(M68K_CHK_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_CHK_EX, GET_PC, GET_SR));
+RET(14)
        }
        POST_IO
 RET(20)
@@ -19682,17 +19832,18 @@ RET(20)
 // CHK
 OPCODE(0x41B8)
 {
-       u32 adr, res;
-       u32 src, dst;
+       s32 src, res;
+       u32 adr;
 
        FETCH_SWORD(adr);
        PRE_IO
-       READ_WORD_F(adr, src)
-       res = DREGu16((Opcode >> 9) & 7);
-       if (((s32)res < 0) || (res > src))
+       READSX_WORD_F(adr, src)
+       res = DREGs16((Opcode >> 9) & 7);
+       if ((res < 0) || (res > src))
        {
                flag_N = res >> 8;
-               SET_PC(execute_exception(M68K_CHK_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_CHK_EX, GET_PC, GET_SR));
+RET(12)
        }
        POST_IO
 RET(18)
@@ -19701,17 +19852,18 @@ RET(18)
 // CHK
 OPCODE(0x41B9)
 {
-       u32 adr, res;
-       u32 src, dst;
+       s32 src, res;
+       u32 adr;
 
        FETCH_LONG(adr);
        PRE_IO
-       READ_WORD_F(adr, src)
-       res = DREGu16((Opcode >> 9) & 7);
-       if (((s32)res < 0) || (res > src))
+       READSX_WORD_F(adr, src)
+       res = DREGs16((Opcode >> 9) & 7);
+       if ((res < 0) || (res > src))
        {
                flag_N = res >> 8;
-               SET_PC(execute_exception(M68K_CHK_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_CHK_EX, GET_PC, GET_SR));
+RET(16)
        }
        POST_IO
 RET(22)
@@ -19720,18 +19872,19 @@ RET(22)
 // CHK
 OPCODE(0x41BA)
 {
-       u32 adr, res;
-       u32 src, dst;
+       s32 src, res;
+       u32 adr;
 
        adr = GET_SWORD + GET_PC;
        PC++;
        PRE_IO
-       READ_WORD_F(adr, src)
-       res = DREGu16((Opcode >> 9) & 7);
-       if (((s32)res < 0) || (res > src))
+       READSX_WORD_F(adr, src)
+       res = DREGs16((Opcode >> 9) & 7);
+       if ((res < 0) || (res > src))
        {
                flag_N = res >> 8;
-               SET_PC(execute_exception(M68K_CHK_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_CHK_EX, GET_PC, GET_SR));
+RET(12)
        }
        POST_IO
 RET(18)
@@ -19740,18 +19893,19 @@ RET(18)
 // CHK
 OPCODE(0x41BB)
 {
-       u32 adr, res;
-       u32 src, dst;
+       s32 src, res;
+       u32 adr;
 
        adr = GET_PC;
        DECODE_EXT_WORD
        PRE_IO
-       READ_WORD_F(adr, src)
-       res = DREGu16((Opcode >> 9) & 7);
-       if (((s32)res < 0) || (res > src))
+       READSX_WORD_F(adr, src)
+       res = DREGs16((Opcode >> 9) & 7);
+       if ((res < 0) || (res > src))
        {
                flag_N = res >> 8;
-               SET_PC(execute_exception(M68K_CHK_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_CHK_EX, GET_PC, GET_SR));
+RET(14)
        }
        POST_IO
 RET(20)
@@ -19760,15 +19914,16 @@ RET(20)
 // CHK
 OPCODE(0x41BC)
 {
-       u32 adr, res;
-       u32 src, dst;
+       s32 src, res;
+       u32 adr;
 
-       FETCH_WORD(src);
-       res = DREGu16((Opcode >> 9) & 7);
-       if (((s32)res < 0) || (res > src))
+       FETCH_SWORD(src);
+       res = DREGs16((Opcode >> 9) & 7);
+       if ((res < 0) || (res > src))
        {
                flag_N = res >> 8;
-               SET_PC(execute_exception(M68K_CHK_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_CHK_EX, GET_PC, GET_SR));
+RET(8)
        }
        POST_IO
 RET(14)
@@ -19777,18 +19932,19 @@ RET(14)
 // CHK
 OPCODE(0x419F)
 {
-       u32 adr, res;
-       u32 src, dst;
+       s32 src, res;
+       u32 adr;
 
        adr = AREG(7);
        AREG(7) += 2;
        PRE_IO
-       READ_WORD_F(adr, src)
-       res = DREGu16((Opcode >> 9) & 7);
-       if (((s32)res < 0) || (res > src))
+       READSX_WORD_F(adr, src)
+       res = DREGs16((Opcode >> 9) & 7);
+       if ((res < 0) || (res > src))
        {
                flag_N = res >> 8;
-               SET_PC(execute_exception(M68K_CHK_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_CHK_EX, GET_PC, GET_SR));
+RET(8)
        }
        POST_IO
 RET(14)
@@ -19797,18 +19953,19 @@ RET(14)
 // CHK
 OPCODE(0x41A7)
 {
-       u32 adr, res;
-       u32 src, dst;
+       s32 src, res;
+       u32 adr;
 
        adr = AREG(7) - 2;
        AREG(7) = adr;
        PRE_IO
-       READ_WORD_F(adr, src)
-       res = DREGu16((Opcode >> 9) & 7);
-       if (((s32)res < 0) || (res > src))
+       READSX_WORD_F(adr, src)
+       res = DREGs16((Opcode >> 9) & 7);
+       if ((res < 0) || (res > src))
        {
                flag_N = res >> 8;
-               SET_PC(execute_exception(M68K_CHK_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_CHK_EX, GET_PC, GET_SR));
+RET(10)
        }
        POST_IO
 RET(16)
@@ -23298,6 +23455,8 @@ OPCODE(0x51C8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        res = DREGu16((Opcode >> 0) & 7);
        res--;
        DREGu16((Opcode >> 0) & 7) = res;
@@ -23321,6 +23480,8 @@ OPCODE(0x52C8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if ((!flag_NotZ) || (flag_C & 0x100))
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23352,6 +23513,8 @@ OPCODE(0x53C8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if (flag_NotZ && (!(flag_C & 0x100)))
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23383,6 +23546,8 @@ OPCODE(0x54C8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if (flag_C & 0x100)
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23414,6 +23579,8 @@ OPCODE(0x55C8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if (!(flag_C & 0x100))
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23445,6 +23612,8 @@ OPCODE(0x56C8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if (!flag_NotZ)
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23476,6 +23645,8 @@ OPCODE(0x57C8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if (flag_NotZ)
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23507,6 +23678,8 @@ OPCODE(0x58C8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if (flag_V & 0x80)
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23538,6 +23711,8 @@ OPCODE(0x59C8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if (!(flag_V & 0x80))
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23569,6 +23744,8 @@ OPCODE(0x5AC8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if (flag_N & 0x80)
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23600,6 +23777,8 @@ OPCODE(0x5BC8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if (!(flag_N & 0x80))
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23631,6 +23810,8 @@ OPCODE(0x5CC8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if ((flag_N ^ flag_V) & 0x80)
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23662,6 +23843,8 @@ OPCODE(0x5DC8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if (!((flag_N ^ flag_V) & 0x80))
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23693,6 +23876,8 @@ OPCODE(0x5EC8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if ((!flag_NotZ) || ((flag_N ^ flag_V) & 0x80))
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23724,6 +23909,8 @@ OPCODE(0x5FC8)
        u32 adr, res;
        u32 src, dst;
 
+       NOT_POLLING
+
        if (flag_NotZ && (!((flag_N ^ flag_V) & 0x80)))
        {
        res = DREGu16((Opcode >> 0) & 7);
@@ -23968,11 +24155,7 @@ OPCODE(0x5048)
        dst = AREGu32((Opcode >> 0) & 7);
        res = dst + src;
        AREG((Opcode >> 0) & 7) = res;
-#ifdef USE_CYCLONE_TIMING
-RET(4)
-#else
 RET(8)
-#endif
 }
 
 // ADDQ
@@ -24992,7 +25175,7 @@ OPCODE(0x6201)
        if (flag_NotZ && (!(flag_C & 0x100)))
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -25006,7 +25189,7 @@ OPCODE(0x6301)
        if ((!flag_NotZ) || (flag_C & 0x100))
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -25020,7 +25203,7 @@ OPCODE(0x6401)
        if (!(flag_C & 0x100))
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -25034,7 +25217,7 @@ OPCODE(0x6501)
        if (flag_C & 0x100)
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -25048,7 +25231,7 @@ OPCODE(0x6601)
        if (flag_NotZ)
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -25062,7 +25245,7 @@ OPCODE(0x6701)
        if (!flag_NotZ)
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -25076,7 +25259,7 @@ OPCODE(0x6801)
        if (!(flag_V & 0x80))
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -25090,7 +25273,7 @@ OPCODE(0x6901)
        if (flag_V & 0x80)
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -25104,7 +25287,7 @@ OPCODE(0x6A01)
        if (!(flag_N & 0x80))
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -25118,7 +25301,7 @@ OPCODE(0x6B01)
        if (flag_N & 0x80)
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -25132,7 +25315,7 @@ OPCODE(0x6C01)
        if (!((flag_N ^ flag_V) & 0x80))
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -25146,7 +25329,7 @@ OPCODE(0x6D01)
        if ((flag_N ^ flag_V) & 0x80)
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -25160,7 +25343,7 @@ OPCODE(0x6E01)
        if (flag_NotZ && (!((flag_N ^ flag_V) & 0x80)))
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -25174,7 +25357,7 @@ OPCODE(0x6F01)
        if ((!flag_NotZ) || ((flag_N ^ flag_V) & 0x80))
        {
                PC += ((s8)(Opcode & 0xFE)) >> 1;
-       m68kcontext.io_cycle_counter -= 2;
+       ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }
@@ -26898,19 +27081,19 @@ OPCODE(0x8100)
 {
        u32 adr, res;
        u32 src, dst;
+       u32 corf = 0;
 
        src = DREGu8((Opcode >> 0) & 7);
        dst = DREGu8((Opcode >> 9) & 7);
        res = (dst & 0xF) - (src & 0xF) - ((flag_X >> M68K_SR_X_SFT) & 1);
-       if (res > 9) res -= 6;
+       if (res > 0xF) corf = 6;
        res += (dst & 0xF0) - (src & 0xF0);
-       if (res > 0x99)
-       {
-               res += 0xA0;
-               flag_X = flag_C = M68K_SR_C;
-       }
-       else flag_X = flag_C = 0;
-       flag_NotZ |= res & 0xFF;
+       flag_V = res;
+       flag_X = flag_C = (s32)res < (s32)corf ? M68K_SR_C : 0;
+       if (res > 0xff) res += 0xA0;
+       res = (res - corf) & 0xFF;
+       flag_V &= ~res;
+       flag_NotZ |= res;
        flag_N = res;
        DREGu8((Opcode >> 9) & 7) = res;
 RET(6)
@@ -26921,6 +27104,7 @@ OPCODE(0x8108)
 {
        u32 adr, res;
        u32 src, dst;
+       u32 corf = 0;
 
        adr = AREG((Opcode >> 0) & 7) - 1;
        AREG((Opcode >> 0) & 7) = adr;
@@ -26930,15 +27114,14 @@ OPCODE(0x8108)
        AREG((Opcode >> 9) & 7) = adr;
        READ_BYTE_F(adr, dst)
        res = (dst & 0xF) - (src & 0xF) - ((flag_X >> M68K_SR_X_SFT) & 1);
-       if (res > 9) res -= 6;
+       if (res > 0xF) corf = 6;
        res += (dst & 0xF0) - (src & 0xF0);
-       if (res > 0x99)
-       {
-               res += 0xA0;
-               flag_X = flag_C = M68K_SR_C;
-       }
-       else flag_X = flag_C = 0;
-       flag_NotZ |= res & 0xFF;
+       flag_V = res;
+       flag_X = flag_C = (s32)res < (s32)corf ? M68K_SR_C : 0;
+       if (res > 0xff) res += 0xA0;
+       res = (res - corf) & 0xFF;
+       flag_V &= ~res;
+       flag_NotZ |= res;
        flag_N = res;
        WRITE_BYTE_F(adr, res)
        POST_IO
@@ -26950,6 +27133,7 @@ OPCODE(0x810F)
 {
        u32 adr, res;
        u32 src, dst;
+       u32 corf = 0;
 
        adr = AREG(7) - 2;
        AREG(7) = adr;
@@ -26959,15 +27143,14 @@ OPCODE(0x810F)
        AREG((Opcode >> 9) & 7) = adr;
        READ_BYTE_F(adr, dst)
        res = (dst & 0xF) - (src & 0xF) - ((flag_X >> M68K_SR_X_SFT) & 1);
-       if (res > 9) res -= 6;
+       if (res > 0xF) corf = 6;
        res += (dst & 0xF0) - (src & 0xF0);
-       if (res > 0x99)
-       {
-               res += 0xA0;
-               flag_X = flag_C = M68K_SR_C;
-       }
-       else flag_X = flag_C = 0;
-       flag_NotZ |= res & 0xFF;
+       flag_V = res;
+       flag_X = flag_C = (s32)res < (s32)corf ? M68K_SR_C : 0;
+       if (res > 0xff) res += 0xA0;
+       res = (res - corf) & 0xFF;
+       flag_V &= ~res;
+       flag_NotZ |= res;
        flag_N = res;
        WRITE_BYTE_F(adr, res)
        POST_IO
@@ -26979,6 +27162,7 @@ OPCODE(0x8F08)
 {
        u32 adr, res;
        u32 src, dst;
+       u32 corf = 0;
 
        adr = AREG((Opcode >> 0) & 7) - 1;
        AREG((Opcode >> 0) & 7) = adr;
@@ -26988,15 +27172,14 @@ OPCODE(0x8F08)
        AREG(7) = adr;
        READ_BYTE_F(adr, dst)
        res = (dst & 0xF) - (src & 0xF) - ((flag_X >> M68K_SR_X_SFT) & 1);
-       if (res > 9) res -= 6;
+       if (res > 0xF) corf = 6;
        res += (dst & 0xF0) - (src & 0xF0);
-       if (res > 0x99)
-       {
-               res += 0xA0;
-               flag_X = flag_C = M68K_SR_C;
-       }
-       else flag_X = flag_C = 0;
-       flag_NotZ |= res & 0xFF;
+       flag_V = res;
+       flag_X = flag_C = (s32)res < (s32)corf ? M68K_SR_C : 0;
+       if (res > 0xff) res += 0xA0;
+       res = (res - corf) & 0xFF;
+       flag_V &= ~res;
+       flag_NotZ |= res;
        flag_N = res;
        WRITE_BYTE_F(adr, res)
        POST_IO
@@ -27008,6 +27191,7 @@ OPCODE(0x8F0F)
 {
        u32 adr, res;
        u32 src, dst;
+       u32 corf = 0;
 
        adr = AREG(7) - 2;
        AREG(7) = adr;
@@ -27017,15 +27201,14 @@ OPCODE(0x8F0F)
        AREG(7) = adr;
        READ_BYTE_F(adr, dst)
        res = (dst & 0xF) - (src & 0xF) - ((flag_X >> M68K_SR_X_SFT) & 1);
-       if (res > 9) res -= 6;
+       if (res > 0xF) corf = 6;
        res += (dst & 0xF0) - (src & 0xF0);
-       if (res > 0x99)
-       {
-               res += 0xA0;
-               flag_X = flag_C = M68K_SR_C;
-       }
-       else flag_X = flag_C = 0;
-       flag_NotZ |= res & 0xFF;
+       flag_V = res;
+       flag_X = flag_C = (s32)res < (s32)corf ? M68K_SR_C : 0;
+       if (res > 0xff) res += 0xA0;
+       res = (res - corf) & 0xFF;
+       flag_V &= ~res;
+       flag_NotZ |= res;
        flag_N = res;
        WRITE_BYTE_F(adr, res)
        POST_IO
@@ -27041,11 +27224,11 @@ OPCODE(0x80C0)
        src = DREGu16((Opcode >> 0) & 7);
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(140)
+RET(4)
 #else
-RET(10)
+RET(14)
 #endif
        }
        dst = DREGu32((Opcode >> 9) & 7);
@@ -27059,7 +27242,7 @@ RET(10)
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(140)
+RET(10)
 #else
 RET(70)
 #endif
@@ -27070,12 +27253,12 @@ RET(70)
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(140)
+RET(138-BITCOUNT(res,q)*2)
 #else
 RET(90)
 #endif
+       }
 }
 
 // DIVU
@@ -27089,11 +27272,11 @@ OPCODE(0x80D0)
        READ_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(144)
+RET(8)
 #else
-RET(14)
+RET(18)
 #endif
        }
        dst = DREGu32((Opcode >> 9) & 7);
@@ -27107,7 +27290,7 @@ RET(14)
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(144)
+RET(14)
 #else
        RET(74)
 #endif
@@ -27118,12 +27301,12 @@ RET(144)
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(144)
+RET(142-BITCOUNT(res,q)*2)
 #else
 RET(94)
 #endif
+       }
 }
 
 // DIVU
@@ -27138,11 +27321,11 @@ OPCODE(0x80D8)
        READ_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(144)
+RET(8)
 #else
-RET(14)
+RET(18)
 #endif
        }
        dst = DREGu32((Opcode >> 9) & 7);
@@ -27156,7 +27339,7 @@ RET(14)
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(144)
+RET(14)
 #else
        RET(74)
 #endif
@@ -27167,12 +27350,12 @@ RET(144)
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(144)
+RET(142-BITCOUNT(res,q)*2)
 #else
 RET(94)
 #endif
+       }
 }
 
 // DIVU
@@ -27187,11 +27370,11 @@ OPCODE(0x80E0)
        READ_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(146)
+RET(10)
 #else
-RET(16)
+RET(20)
 #endif
        }
        dst = DREGu32((Opcode >> 9) & 7);
@@ -27205,7 +27388,7 @@ RET(16)
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(146)
+RET(16)
 #else
        RET(76)
 #endif
@@ -27216,12 +27399,12 @@ RET(146)
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(146)
+RET(144-BITCOUNT(res,q)*2)
 #else
 RET(96)
 #endif
+       }
 }
 
 // DIVU
@@ -27236,11 +27419,11 @@ OPCODE(0x80E8)
        READ_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(148)
+RET(12)
 #else
-RET(18)
+RET(22)
 #endif
        }
        dst = DREGu32((Opcode >> 9) & 7);
@@ -27254,7 +27437,7 @@ RET(18)
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(148)
+RET(18)
 #else
        RET(78)
 #endif
@@ -27265,12 +27448,12 @@ RET(148)
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(148)
+RET(146-BITCOUNT(res,q)*2)
 #else
 RET(98)
 #endif
+       }
 }
 
 // DIVU
@@ -27285,11 +27468,11 @@ OPCODE(0x80F0)
        READ_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(150)
+RET(14)
 #else
-RET(20)
+RET(24)
 #endif
        }
        dst = DREGu32((Opcode >> 9) & 7);
@@ -27303,7 +27486,7 @@ RET(20)
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(150)
+RET(20)
 #else
        RET(80)
 #endif
@@ -27314,12 +27497,12 @@ RET(150)
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(150)
+RET(148-BITCOUNT(res,q)*2)
 #else
 RET(100)
 #endif
+       }
 }
 
 // DIVU
@@ -27333,11 +27516,11 @@ OPCODE(0x80F8)
        READ_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(148)
+RET(12)
 #else
-RET(18)
+RET(22)
 #endif
        }
        dst = DREGu32((Opcode >> 9) & 7);
@@ -27351,7 +27534,7 @@ RET(18)
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(148)
+RET(18)
 #else
        RET(78)
 #endif
@@ -27362,12 +27545,12 @@ RET(148)
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(148)
+RET(146-BITCOUNT(res,q)*2)
 #else
 RET(98)
 #endif
+       }
 }
 
 // DIVU
@@ -27381,11 +27564,11 @@ OPCODE(0x80F9)
        READ_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(162)
+RET(16)
 #else
-RET(22)
+RET(26)
 #endif
        }
        dst = DREGu32((Opcode >> 9) & 7);
@@ -27399,7 +27582,7 @@ RET(22)
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(162)
+RET(22)
 #else
        RET(82)
 #endif
@@ -27410,12 +27593,12 @@ RET(162)
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(162)
+RET(150-BITCOUNT(res,q)*2)
 #else
 RET(102)
 #endif
+       }
 }
 
 // DIVU
@@ -27430,11 +27613,11 @@ OPCODE(0x80FA)
        READ_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(148)
+RET(12)
 #else
-RET(18)
+RET(22)
 #endif
        }
        dst = DREGu32((Opcode >> 9) & 7);
@@ -27448,7 +27631,7 @@ RET(18)
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(148)
+RET(18)
 #else
        RET(78)
 #endif
@@ -27459,12 +27642,12 @@ RET(148)
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(148)
+RET(146-BITCOUNT(res,q)*2)
 #else
 RET(98)
 #endif
+       }
 }
 
 // DIVU
@@ -27479,11 +27662,11 @@ OPCODE(0x80FB)
        READ_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(160)
+RET(14)
 #else
-RET(20)
+RET(24)
 #endif
        }
        dst = DREGu32((Opcode >> 9) & 7);
@@ -27497,7 +27680,7 @@ RET(20)
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(160)
+RET(20)
 #else
        RET(80)
 #endif
@@ -27508,12 +27691,12 @@ RET(160)
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(160)
+RET(148-BITCOUNT(res,q)*2)
 #else
 RET(100)
 #endif
+       }
 }
 
 // DIVU
@@ -27525,11 +27708,11 @@ OPCODE(0x80FC)
        FETCH_WORD(src);
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(144)
+RET(8)
 #else
-RET(14)
+RET(18)
 #endif
        }
        dst = DREGu32((Opcode >> 9) & 7);
@@ -27543,7 +27726,7 @@ RET(14)
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(144)
+RET(14)
 #else
        RET(74)
 #endif
@@ -27554,12 +27737,12 @@ RET(144)
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(144)
+RET(142-BITCOUNT(res,q)*2)
 #else
 RET(94)
 #endif
+       }
 }
 
 // DIVU
@@ -27574,11 +27757,11 @@ OPCODE(0x80DF)
        READ_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(144)
+RET(8)
 #else
-RET(14)
+RET(18)
 #endif
        }
        dst = DREGu32((Opcode >> 9) & 7);
@@ -27592,7 +27775,7 @@ RET(14)
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(144)
+RET(14)
 #else
        RET(74)
 #endif
@@ -27603,12 +27786,12 @@ RET(144)
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(144)
+RET(142-BITCOUNT(res,q)*2)
 #else
 RET(94)
 #endif
+       }
 }
 
 // DIVU
@@ -27623,11 +27806,11 @@ OPCODE(0x80E7)
        READ_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(146)
+RET(10)
 #else
-RET(16)
+RET(20)
 #endif
        }
        dst = DREGu32((Opcode >> 9) & 7);
@@ -27641,7 +27824,7 @@ RET(16)
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(146)
+RET(16)
 #else
        RET(76)
 #endif
@@ -27652,12 +27835,12 @@ RET(146)
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-RET(146)
+RET(144-BITCOUNT(res,q)*2)
 #else
 RET(96)
 #endif
+       }
 }
 
 // DIVS
@@ -27669,11 +27852,12 @@ OPCODE(0x81C0)
        src = (s32)DREGs16((Opcode >> 0) & 7);
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81C0;
+RET(4)
+#else
+               RET(14)
 #endif
-               RET(10)
        }
        dst = DREGu32((Opcode >> 9) & 7);
        if ((dst == 0x80000000) && (src == (u32)-1))
@@ -27683,9 +27867,10 @@ goto end81C0;
                res = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81C0;
-#endif
+RET(16+(dst>>31)*2)
+#else
        RET(50)
+#endif
        }
        {
                s32 q, r;
@@ -27697,9 +27882,12 @@ goto end81C0;
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81C0;
-#endif
+if (q > 0xFFFF || q < -0x10000) {
+       RET(16+(dst>>31)*2)
+} else RET(152+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
        RET(80)
+#endif
                }
                q &= 0x0000FFFF;
                flag_NotZ = q;
@@ -27707,11 +27895,12 @@ goto end81C0;
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-end81C0: m68kcontext.io_cycle_counter -= 50;
-#endif
+RET(152+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
 RET(108)
+#endif
+       }
 }
 
 // DIVS
@@ -27725,11 +27914,12 @@ OPCODE(0x81D0)
        READSX_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81D0;
+RET(8)
+#else
+               RET(18)
 #endif
-               RET(14)
        }
        dst = DREGu32((Opcode >> 9) & 7);
        if ((dst == 0x80000000) && (src == (u32)-1))
@@ -27739,9 +27929,10 @@ goto end81D0;
                res = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81D0;
-#endif
+RET(20+(dst>>31)*2)
+#else
        RET(54)
+#endif
        }
        {
                s32 q, r;
@@ -27753,9 +27944,12 @@ goto end81D0;
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81D0;
-#endif
+if (q > 0xFFFF || q < -0x10000) {
+       RET(20+(dst>>31)*2)
+} else RET(156+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
        RET(84)
+#endif
                }
                q &= 0x0000FFFF;
                flag_NotZ = q;
@@ -27763,11 +27957,12 @@ goto end81D0;
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-end81D0: m68kcontext.io_cycle_counter -= 50;
-#endif
+RET(156+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
 RET(112)
+#endif
+       }
 }
 
 // DIVS
@@ -27782,11 +27977,12 @@ OPCODE(0x81D8)
        READSX_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81D8;
+RET(8)
+#else
+               RET(18)
 #endif
-               RET(14)
        }
        dst = DREGu32((Opcode >> 9) & 7);
        if ((dst == 0x80000000) && (src == (u32)-1))
@@ -27796,9 +27992,10 @@ goto end81D8;
                res = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81D8;
-#endif
+RET(20+(dst>>31)*2)
+#else
        RET(54)
+#endif
        }
        {
                s32 q, r;
@@ -27810,9 +28007,12 @@ goto end81D8;
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81D8;
-#endif
+if (q > 0xFFFF || q < -0x10000) {
+       RET(20+(dst>>31)*2)
+} else RET(156+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
        RET(84)
+#endif
                }
                q &= 0x0000FFFF;
                flag_NotZ = q;
@@ -27820,11 +28020,12 @@ goto end81D8;
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-end81D8: m68kcontext.io_cycle_counter -= 50;
-#endif
+RET(156+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
 RET(112)
+#endif
+       }
 }
 
 // DIVS
@@ -27839,11 +28040,12 @@ OPCODE(0x81E0)
        READSX_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81E0;
+RET(10)
+#else
+               RET(20)
 #endif
-               RET(16)
        }
        dst = DREGu32((Opcode >> 9) & 7);
        if ((dst == 0x80000000) && (src == (u32)-1))
@@ -27853,9 +28055,10 @@ goto end81E0;
                res = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81E0;
-#endif
+RET(22+(dst>>31)*2)
+#else
        RET(56)
+#endif
        }
        {
                s32 q, r;
@@ -27867,9 +28070,12 @@ goto end81E0;
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81E0;
-#endif
+if (q > 0xFFFF || q < -0x10000) {
+       RET(22+(dst>>31)*2)
+} else RET(158+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
        RET(86)
+#endif
                }
                q &= 0x0000FFFF;
                flag_NotZ = q;
@@ -27877,11 +28083,12 @@ goto end81E0;
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-end81E0: m68kcontext.io_cycle_counter -= 50;
-#endif
+RET(158+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
 RET(114)
+#endif
+       }
 }
 
 // DIVS
@@ -27896,11 +28103,12 @@ OPCODE(0x81E8)
        READSX_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81E8;
+RET(12)
+#else
+               RET(22)
 #endif
-               RET(18)
        }
        dst = DREGu32((Opcode >> 9) & 7);
        if ((dst == 0x80000000) && (src == (u32)-1))
@@ -27910,9 +28118,10 @@ goto end81E8;
                res = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81E8;
-#endif
+RET(24+(dst>>31)*2)
+#else
        RET(58)
+#endif
        }
        {
                s32 q, r;
@@ -27924,9 +28133,12 @@ goto end81E8;
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81E8;
-#endif
+if (q > 0xFFFF || q < -0x10000) {
+       RET(24+(dst>>31)*2)
+} else RET(160+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
        RET(88)
+#endif
                }
                q &= 0x0000FFFF;
                flag_NotZ = q;
@@ -27934,11 +28146,12 @@ goto end81E8;
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-end81E8: m68kcontext.io_cycle_counter -= 50;
-#endif
+RET(160+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
 RET(116)
+#endif
+       }
 }
 
 // DIVS
@@ -27953,11 +28166,12 @@ OPCODE(0x81F0)
        READSX_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81F0;
+RET(14)
+#else
+               RET(24)
 #endif
-               RET(20)
        }
        dst = DREGu32((Opcode >> 9) & 7);
        if ((dst == 0x80000000) && (src == (u32)-1))
@@ -27967,9 +28181,10 @@ goto end81F0;
                res = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81F0;
-#endif
+RET(26+(dst>>31)*2)
+#else
        RET(60)
+#endif
        }
        {
                s32 q, r;
@@ -27981,9 +28196,12 @@ goto end81F0;
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81F0;
-#endif
+if (q > 0xFFFF || q < -0x10000) {
+       RET(26+(dst>>31)*2)
+} else RET(162+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
        RET(90)
+#endif
                }
                q &= 0x0000FFFF;
                flag_NotZ = q;
@@ -27991,11 +28209,12 @@ goto end81F0;
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-end81F0: m68kcontext.io_cycle_counter -= 50;
-#endif
+RET(162+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
 RET(118)
+#endif
+       }
 }
 
 // DIVS
@@ -28009,11 +28228,12 @@ OPCODE(0x81F8)
        READSX_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81F8;
+RET(12)
+#else
+               RET(22)
 #endif
-               RET(18)
        }
        dst = DREGu32((Opcode >> 9) & 7);
        if ((dst == 0x80000000) && (src == (u32)-1))
@@ -28023,9 +28243,10 @@ goto end81F8;
                res = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81F8;
-#endif
+RET(24+(dst>>31)*2)
+#else
        RET(58)
+#endif
        }
        {
                s32 q, r;
@@ -28037,9 +28258,12 @@ goto end81F8;
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81F8;
-#endif
+if (q > 0xFFFF || q < -0x10000) {
+       RET(24+(dst>>31)*2)
+} else RET(160+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
        RET(88)
+#endif
                }
                q &= 0x0000FFFF;
                flag_NotZ = q;
@@ -28047,11 +28271,12 @@ goto end81F8;
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-end81F8: m68kcontext.io_cycle_counter -= 50;
-#endif
+RET(160+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
 RET(116)
+#endif
+       }
 }
 
 // DIVS
@@ -28065,11 +28290,12 @@ OPCODE(0x81F9)
        READSX_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81F9;
+RET(16)
+#else
+               RET(26)
 #endif
-               RET(22)
        }
        dst = DREGu32((Opcode >> 9) & 7);
        if ((dst == 0x80000000) && (src == (u32)-1))
@@ -28079,9 +28305,10 @@ goto end81F9;
                res = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81F9;
-#endif
+RET(28+(dst>>31)*2)
+#else
        RET(62)
+#endif
        }
        {
                s32 q, r;
@@ -28093,9 +28320,12 @@ goto end81F9;
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81F9;
-#endif
+if (q > 0xFFFF || q < -0x10000) {
+       RET(28+(dst>>31)*2)
+} else RET(164+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
        RET(92)
+#endif
                }
                q &= 0x0000FFFF;
                flag_NotZ = q;
@@ -28103,11 +28333,12 @@ goto end81F9;
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-end81F9: m68kcontext.io_cycle_counter -= 50;
-#endif
+RET(164+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
 RET(120)
+#endif
+       }
 }
 
 // DIVS
@@ -28122,11 +28353,12 @@ OPCODE(0x81FA)
        READSX_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81FA;
+RET(12)
+#else
+               RET(22)
 #endif
-               RET(18)
        }
        dst = DREGu32((Opcode >> 9) & 7);
        if ((dst == 0x80000000) && (src == (u32)-1))
@@ -28136,9 +28368,10 @@ goto end81FA;
                res = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81FA;
-#endif
+RET(24+(dst>>31)*2)
+#else
        RET(58)
+#endif
        }
        {
                s32 q, r;
@@ -28150,9 +28383,12 @@ goto end81FA;
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81FA;
-#endif
+if (q > 0xFFFF || q < -0x10000) {
+       RET(24+(dst>>31)*2)
+} else RET(160+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
        RET(88)
+#endif
                }
                q &= 0x0000FFFF;
                flag_NotZ = q;
@@ -28160,11 +28396,12 @@ goto end81FA;
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-end81FA: m68kcontext.io_cycle_counter -= 50;
-#endif
+RET(160+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
 RET(116)
+#endif
+       }
 }
 
 // DIVS
@@ -28179,11 +28416,12 @@ OPCODE(0x81FB)
        READSX_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81FB;
+RET(14)
+#else
+               RET(24)
 #endif
-               RET(20)
        }
        dst = DREGu32((Opcode >> 9) & 7);
        if ((dst == 0x80000000) && (src == (u32)-1))
@@ -28193,9 +28431,10 @@ goto end81FB;
                res = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81FB;
-#endif
+RET(26+(dst>>31)*2)
+#else
        RET(60)
+#endif
        }
        {
                s32 q, r;
@@ -28207,9 +28446,12 @@ goto end81FB;
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81FB;
-#endif
+if (q > 0xFFFF || q < -0x10000) {
+       RET(26+(dst>>31)*2)
+} else RET(162+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
        RET(90)
+#endif
                }
                q &= 0x0000FFFF;
                flag_NotZ = q;
@@ -28217,11 +28459,12 @@ goto end81FB;
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-end81FB: m68kcontext.io_cycle_counter -= 50;
-#endif
+RET(162+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
 RET(118)
+#endif
+       }
 }
 
 // DIVS
@@ -28233,11 +28476,12 @@ OPCODE(0x81FC)
        FETCH_SWORD(src);
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81FC;
+RET(8)
+#else
+               RET(18)
 #endif
-               RET(14)
        }
        dst = DREGu32((Opcode >> 9) & 7);
        if ((dst == 0x80000000) && (src == (u32)-1))
@@ -28247,9 +28491,10 @@ goto end81FC;
                res = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81FC;
-#endif
+RET(20+(dst>>31)*2)
+#else
        RET(54)
+#endif
        }
        {
                s32 q, r;
@@ -28261,9 +28506,12 @@ goto end81FC;
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81FC;
-#endif
+if (q > 0xFFFF || q < -0x10000) {
+       RET(20+(dst>>31)*2)
+} else RET(156+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
        RET(84)
+#endif
                }
                q &= 0x0000FFFF;
                flag_NotZ = q;
@@ -28271,11 +28519,12 @@ goto end81FC;
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-end81FC: m68kcontext.io_cycle_counter -= 50;
-#endif
+RET(156+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
 RET(112)
+#endif
+       }
 }
 
 // DIVS
@@ -28290,11 +28539,12 @@ OPCODE(0x81DF)
        READSX_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81DF;
+RET(8)
+#else
+               RET(18)
 #endif
-               RET(14)
        }
        dst = DREGu32((Opcode >> 9) & 7);
        if ((dst == 0x80000000) && (src == (u32)-1))
@@ -28304,9 +28554,10 @@ goto end81DF;
                res = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81DF;
-#endif
+RET(20+(dst>>31)*2)
+#else
        RET(54)
+#endif
        }
        {
                s32 q, r;
@@ -28318,9 +28569,12 @@ goto end81DF;
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81DF;
-#endif
+if (q > 0xFFFF || q < -0x10000) {
+       RET(20+(dst>>31)*2)
+} else RET(156+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
        RET(84)
+#endif
                }
                q &= 0x0000FFFF;
                flag_NotZ = q;
@@ -28328,11 +28582,12 @@ goto end81DF;
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-end81DF: m68kcontext.io_cycle_counter -= 50;
-#endif
+RET(156+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
 RET(112)
+#endif
+       }
 }
 
 // DIVS
@@ -28347,11 +28602,12 @@ OPCODE(0x81E7)
        READSX_WORD_F(adr, src)
        if (src == 0)
        {
-               SET_PC(execute_exception(M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
+               SET_PC(execute_exception(ctx, M68K_ZERO_DIVIDE_EX, GET_PC, GET_SR));
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81E7;
+RET(10)
+#else
+               RET(20)
 #endif
-               RET(16)
        }
        dst = DREGu32((Opcode >> 9) & 7);
        if ((dst == 0x80000000) && (src == (u32)-1))
@@ -28361,9 +28617,10 @@ goto end81E7;
                res = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81E7;
-#endif
+RET(22+(dst>>31)*2)
+#else
        RET(56)
+#endif
        }
        {
                s32 q, r;
@@ -28375,9 +28632,12 @@ goto end81E7;
                {
                        flag_V = M68K_SR_V;
 #ifdef USE_CYCLONE_TIMING_DIV
-goto end81E7;
-#endif
+if (q > 0xFFFF || q < -0x10000) {
+       RET(22+(dst>>31)*2)
+} else RET(158+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
        RET(86)
+#endif
                }
                q &= 0x0000FFFF;
                flag_NotZ = q;
@@ -28385,11 +28645,12 @@ goto end81E7;
                flag_V = flag_C = 0;
                res = q | (r << 16);
        DREGu32((Opcode >> 9) & 7) = res;
-       }
 #ifdef USE_CYCLONE_TIMING_DIV
-end81E7: m68kcontext.io_cycle_counter -= 50;
-#endif
+RET(158+(dst>>31)*4-(q>>31)*2-BITCOUNT(res,abs(q))*2)
+#else
 RET(114)
+#endif
+       }
 }
 
 // SUBaD
@@ -28416,8 +28677,8 @@ OPCODE(0x9008)
        u32 src, dst;
 
        // can't read byte from Ax registers !
-       m68kcontext.execinfo |= M68K_FAULTED;
-       m68kcontext.io_cycle_counter = 0;
+       ctx->execinfo |= M68K_FAULTED;
+       ctx->io_cycle_counter = 0;
 /*
        goto famec_Exec_End;
        dst = DREGu8((Opcode >> 9) & 7);
@@ -30590,8 +30851,8 @@ OPCODE(0xB008)
        u32 src, dst;
 
        // can't read byte from Ax registers !
-       m68kcontext.execinfo |= M68K_FAULTED;
-       m68kcontext.io_cycle_counter = 0;
+       ctx->execinfo |= M68K_FAULTED;
+       ctx->io_cycle_counter = 0;
 /*
        goto famec_Exec_End;
        dst = DREGu8((Opcode >> 9) & 7);
@@ -34085,18 +34346,22 @@ OPCODE(0xC100)
 {
        u32 adr, res;
        u32 src, dst;
+       u32 corf = 0;
 
        src = DREGu8((Opcode >> 0) & 7);
        dst = DREGu8((Opcode >> 9) & 7);
        res = (dst & 0xF) + (src & 0xF) + ((flag_X >> M68K_SR_X_SFT) & 1);
-       if (res > 9) res += 6;
+       if (res > 9) corf = 6;
        res += (dst & 0xF0) + (src & 0xF0);
-       if (res > 0x99)
+       flag_V = ~res;
+       res += corf;
+       if (res > 0x9F)
        {
                res -= 0xA0;
                flag_X = flag_C = M68K_SR_C;
        }
        else flag_X = flag_C = 0;
+       flag_V &= res;
        flag_NotZ |= res & 0xFF;
        flag_N = res;
        DREGu8((Opcode >> 9) & 7) = res;
@@ -34108,6 +34373,7 @@ OPCODE(0xC108)
 {
        u32 adr, res;
        u32 src, dst;
+       u32 corf = 0;
 
        adr = AREG((Opcode >> 0) & 7) - 1;
        AREG((Opcode >> 0) & 7) = adr;
@@ -34117,14 +34383,17 @@ OPCODE(0xC108)
        AREG((Opcode >> 9) & 7) = adr;
        READ_BYTE_F(adr, dst)
        res = (dst & 0xF) + (src & 0xF) + ((flag_X >> M68K_SR_X_SFT) & 1);
-       if (res > 9) res += 6;
+       if (res > 9) corf = 6;
        res += (dst & 0xF0) + (src & 0xF0);
-       if (res > 0x99)
+       flag_V = ~res;
+       res += corf;
+       if (res > 0x9F)
        {
                res -= 0xA0;
                flag_X = flag_C = M68K_SR_C;
        }
        else flag_X = flag_C = 0;
+       flag_V &= res;
        flag_NotZ |= res & 0xFF;
        flag_N = res;
        WRITE_BYTE_F(adr, res)
@@ -34137,6 +34406,7 @@ OPCODE(0xC10F)
 {
        u32 adr, res;
        u32 src, dst;
+       u32 corf = 0;
 
        adr = AREG(7) - 2;
        AREG(7) = adr;
@@ -34146,14 +34416,17 @@ OPCODE(0xC10F)
        AREG((Opcode >> 9) & 7) = adr;
        READ_BYTE_F(adr, dst)
        res = (dst & 0xF) + (src & 0xF) + ((flag_X >> M68K_SR_X_SFT) & 1);
-       if (res > 9) res += 6;
+       if (res > 9) corf = 6;
        res += (dst & 0xF0) + (src & 0xF0);
-       if (res > 0x99)
+       flag_V = ~res;
+       res += corf;
+       if (res > 0x9F)
        {
                res -= 0xA0;
                flag_X = flag_C = M68K_SR_C;
        }
        else flag_X = flag_C = 0;
+       flag_V &= res;
        flag_NotZ |= res & 0xFF;
        flag_N = res;
        WRITE_BYTE_F(adr, res)
@@ -34166,6 +34439,7 @@ OPCODE(0xCF08)
 {
        u32 adr, res;
        u32 src, dst;
+       u32 corf = 0;
 
        adr = AREG((Opcode >> 0) & 7) - 1;
        AREG((Opcode >> 0) & 7) = adr;
@@ -34175,14 +34449,17 @@ OPCODE(0xCF08)
        AREG(7) = adr;
        READ_BYTE_F(adr, dst)
        res = (dst & 0xF) + (src & 0xF) + ((flag_X >> M68K_SR_X_SFT) & 1);
-       if (res > 9) res += 6;
+       if (res > 9) corf = 6;
        res += (dst & 0xF0) + (src & 0xF0);
-       if (res > 0x99)
+       flag_V = ~res;
+       res += corf;
+       if (res > 0x9F)
        {
                res -= 0xA0;
                flag_X = flag_C = M68K_SR_C;
        }
        else flag_X = flag_C = 0;
+       flag_V &= res;
        flag_NotZ |= res & 0xFF;
        flag_N = res;
        WRITE_BYTE_F(adr, res)
@@ -34195,6 +34472,7 @@ OPCODE(0xCF0F)
 {
        u32 adr, res;
        u32 src, dst;
+       u32 corf = 0;
 
        adr = AREG(7) - 2;
        AREG(7) = adr;
@@ -34204,14 +34482,17 @@ OPCODE(0xCF0F)
        AREG(7) = adr;
        READ_BYTE_F(adr, dst)
        res = (dst & 0xF) + (src & 0xF) + ((flag_X >> M68K_SR_X_SFT) & 1);
-       if (res > 9) res += 6;
+       if (res > 9) corf = 6;
        res += (dst & 0xF0) + (src & 0xF0);
-       if (res > 0x99)
+       flag_V = ~res;
+       res += corf;
+       if (res > 0x9F)
        {
                res -= 0xA0;
                flag_X = flag_C = M68K_SR_C;
        }
        else flag_X = flag_C = 0;
+       flag_V &= res;
        flag_NotZ |= res & 0xFF;
        flag_N = res;
        WRITE_BYTE_F(adr, res)
@@ -34233,7 +34514,7 @@ OPCODE(0xC0C0)
        flag_V = flag_C = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING
-RET(54)
+RET(38+BITCOUNT(res,src)*2)
 #else
 RET(50)
 #endif
@@ -34256,7 +34537,7 @@ OPCODE(0xC0D0)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(58)
+RET(42+BITCOUNT(res,src)*2)
 #else
 RET(54)
 #endif
@@ -34280,7 +34561,7 @@ OPCODE(0xC0D8)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(58)
+RET(42+BITCOUNT(res,src)*2)
 #else
 RET(54)
 #endif
@@ -34304,7 +34585,7 @@ OPCODE(0xC0E0)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(60)
+RET(44+BITCOUNT(res,src)*2)
 #else
 RET(56)
 #endif
@@ -34328,7 +34609,7 @@ OPCODE(0xC0E8)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(62)
+RET(46+BITCOUNT(res,src)*2)
 #else
 RET(58)
 #endif
@@ -34352,7 +34633,7 @@ OPCODE(0xC0F0)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(64)
+RET(48+BITCOUNT(res,src)*2)
 #else
 RET(60)
 #endif
@@ -34375,7 +34656,7 @@ OPCODE(0xC0F8)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(62)
+RET(46+BITCOUNT(res,src)*2)
 #else
 RET(58)
 #endif
@@ -34398,7 +34679,7 @@ OPCODE(0xC0F9)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(66)
+RET(50+BITCOUNT(res,src)*2)
 #else
 RET(62)
 #endif
@@ -34422,7 +34703,7 @@ OPCODE(0xC0FA)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(62)
+RET(46+BITCOUNT(res,src)*2)
 #else
 RET(58)
 #endif
@@ -34446,7 +34727,7 @@ OPCODE(0xC0FB)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(64)
+RET(48+BITCOUNT(res,src)*2)
 #else
 RET(60)
 #endif
@@ -34466,7 +34747,7 @@ OPCODE(0xC0FC)
        flag_V = flag_C = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING
-RET(58)
+RET(42+BITCOUNT(res,src)*2)
 #else
 RET(54)
 #endif
@@ -34490,7 +34771,7 @@ OPCODE(0xC0DF)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(58)
+RET(42+BITCOUNT(res,src)*2)
 #else
 RET(54)
 #endif
@@ -34514,7 +34795,7 @@ OPCODE(0xC0E7)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(60)
+RET(44+BITCOUNT(res,src)*2)
 #else
 RET(56)
 #endif
@@ -34534,7 +34815,7 @@ OPCODE(0xC1C0)
        flag_V = flag_C = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING
-RET(54)
+RET(38+BITCOUNT(res,src^(src<<1)))
 #else
 RET(50)
 #endif
@@ -34557,7 +34838,7 @@ OPCODE(0xC1D0)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(58)
+RET(42+BITCOUNT(res,src^(src<<1))*2)
 #else
 RET(54)
 #endif
@@ -34581,7 +34862,7 @@ OPCODE(0xC1D8)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(58)
+RET(42+BITCOUNT(res,src^(src<<1))*2)
 #else
 RET(54)
 #endif
@@ -34605,7 +34886,7 @@ OPCODE(0xC1E0)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(60)
+RET(44+BITCOUNT(res,src^(src<<1))*2)
 #else
 RET(56)
 #endif
@@ -34629,7 +34910,7 @@ OPCODE(0xC1E8)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(62)
+RET(46+BITCOUNT(res,src^(src<<1))*2)
 #else
 RET(58)
 #endif
@@ -34653,7 +34934,7 @@ OPCODE(0xC1F0)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(64)
+RET(48+BITCOUNT(res,src^(src<<1))*2)
 #else
 RET(60)
 #endif
@@ -34676,7 +34957,7 @@ OPCODE(0xC1F8)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(62)
+RET(46+BITCOUNT(res,src^(src<<1))*2)
 #else
 RET(58)
 #endif
@@ -34699,7 +34980,7 @@ OPCODE(0xC1F9)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(66)
+RET(50+BITCOUNT(res,src^(src<<1))*2)
 #else
 RET(62)
 #endif
@@ -34723,7 +35004,7 @@ OPCODE(0xC1FA)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(62)
+RET(46+BITCOUNT(res,src^(src<<1))*2)
 #else
 RET(58)
 #endif
@@ -34747,7 +35028,7 @@ OPCODE(0xC1FB)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(64)
+RET(48+BITCOUNT(res,src^(src<<1))*2)
 #else
 RET(60)
 #endif
@@ -34767,7 +35048,7 @@ OPCODE(0xC1FC)
        flag_V = flag_C = 0;
        DREGu32((Opcode >> 9) & 7) = res;
 #ifdef USE_CYCLONE_TIMING
-RET(58)
+RET(42+BITCOUNT(res,src^(src<<1))*2)
 #else
 RET(54)
 #endif
@@ -34791,7 +35072,7 @@ OPCODE(0xC1DF)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(58)
+RET(42+BITCOUNT(res,src^(src<<1))*2)
 #else
 RET(54)
 #endif
@@ -34815,7 +35096,7 @@ OPCODE(0xC1E7)
        DREGu32((Opcode >> 9) & 7) = res;
        POST_IO
 #ifdef USE_CYCLONE_TIMING
-RET(60)
+RET(44+BITCOUNT(res,src^(src<<1))*2)
 #else
 RET(56)
 #endif
@@ -34887,8 +35168,8 @@ OPCODE(0xD008)
        u32 src, dst;
 
        // can't read byte from Ax registers !
-       m68kcontext.execinfo |= M68K_FAULTED;
-       m68kcontext.io_cycle_counter = 0;
+       ctx->execinfo |= M68K_FAULTED;
+       ctx->io_cycle_counter = 0;
 /*
        goto famec_Exec_End;
        dst = DREGu8((Opcode >> 9) & 7);
@@ -37047,7 +37328,7 @@ OPCODE(0xE000)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = (s32)DREGs8((Opcode >> 0) & 7);
        flag_V = 0;
        flag_X = flag_C = src << ((M68K_SR_C_SFT + 1) - sft);
@@ -37067,7 +37348,7 @@ OPCODE(0xE040)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = (s32)DREGs16((Opcode >> 0) & 7);
        flag_V = 0;
        flag_X = flag_C = src << ((M68K_SR_C_SFT + 1) - sft);
@@ -37087,7 +37368,7 @@ OPCODE(0xE080)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = (s32)DREGs32((Opcode >> 0) & 7);
        flag_V = 0;
        flag_X = flag_C = src << ((M68K_SR_C_SFT + 1) - sft);
@@ -37107,7 +37388,7 @@ OPCODE(0xE008)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu8((Opcode >> 0) & 7);
        flag_N = flag_V = 0;
        flag_X = flag_C = src << ((M68K_SR_C_SFT + 1) - sft);
@@ -37126,7 +37407,7 @@ OPCODE(0xE048)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu16((Opcode >> 0) & 7);
        flag_N = flag_V = 0;
        flag_X = flag_C = src << ((M68K_SR_C_SFT + 1) - sft);
@@ -37145,7 +37426,7 @@ OPCODE(0xE088)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu32((Opcode >> 0) & 7);
        flag_N = flag_V = 0;
        flag_X = flag_C = src << ((M68K_SR_C_SFT + 1) - sft);
@@ -37164,7 +37445,7 @@ OPCODE(0xE010)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu8((Opcode >> 0) & 7);
        src |= (flag_X & M68K_SR_X) << 0;
        res = (src >> sft) | (src << (9 - sft));
@@ -37185,7 +37466,7 @@ OPCODE(0xE050)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu16((Opcode >> 0) & 7);
        src |= (flag_X & M68K_SR_X) << 8;
        res = (src >> sft) | (src << (17 - sft));
@@ -37206,7 +37487,7 @@ OPCODE(0xE090)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu32((Opcode >> 0) & 7);
        flag_C = src << ((M68K_SR_C_SFT + 1) - sft);
        if (sft == 1) res = (src >> 1) | ((flag_X & M68K_SR_X) << (32 - (M68K_SR_X_SFT + 1)));
@@ -37228,7 +37509,7 @@ OPCODE(0xE018)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu8((Opcode >> 0) & 7);
        flag_V = 0;
        flag_C = src << ((M68K_SR_C_SFT + 1) - sft);
@@ -37248,7 +37529,7 @@ OPCODE(0xE058)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu16((Opcode >> 0) & 7);
        flag_V = 0;
        flag_C = src << ((M68K_SR_C_SFT + 1) - sft);
@@ -37268,7 +37549,7 @@ OPCODE(0xE098)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu32((Opcode >> 0) & 7);
        flag_V = 0;
        flag_C = src << ((M68K_SR_C_SFT + 1) - sft);
@@ -37288,7 +37569,7 @@ OPCODE(0xE100)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu8((Opcode >> 0) & 7);
        if (sft < 8)
        {
@@ -37327,7 +37608,7 @@ OPCODE(0xE140)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu16((Opcode >> 0) & 7);
                flag_X = flag_C = src >> (8 - sft);
                res = src << sft;
@@ -37352,7 +37633,7 @@ OPCODE(0xE180)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu32((Opcode >> 0) & 7);
                flag_X = flag_C = src >> (24 - sft);
                res = src << sft;
@@ -37377,7 +37658,7 @@ OPCODE(0xE108)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu8((Opcode >> 0) & 7);
        flag_V = 0;
        flag_X = flag_C = src << (0 + sft);
@@ -37397,7 +37678,7 @@ OPCODE(0xE148)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu16((Opcode >> 0) & 7);
        flag_V = 0;
        flag_X = flag_C = src >> (8 - sft);
@@ -37417,7 +37698,7 @@ OPCODE(0xE188)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu32((Opcode >> 0) & 7);
        flag_V = 0;
        flag_X = flag_C = src >> (24 - sft);
@@ -37437,7 +37718,7 @@ OPCODE(0xE110)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu8((Opcode >> 0) & 7);
        src |= (flag_X & M68K_SR_X) << 0;
        res = (src << sft) | (src >> (9 - sft));
@@ -37458,7 +37739,7 @@ OPCODE(0xE150)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu16((Opcode >> 0) & 7);
        src |= (flag_X & M68K_SR_X) << 8;
        res = (src << sft) | (src >> (17 - sft));
@@ -37479,7 +37760,7 @@ OPCODE(0xE190)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu32((Opcode >> 0) & 7);
        flag_C = src >> ((32 - M68K_SR_C_SFT) - sft);
        if (sft == 1) res = (src << 1) | ((flag_X & M68K_SR_X) >> ((M68K_SR_X_SFT + 1) - 1));
@@ -37501,7 +37782,7 @@ OPCODE(0xE118)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu8((Opcode >> 0) & 7);
        flag_V = 0;
        flag_C = src << (0 + sft);
@@ -37521,7 +37802,7 @@ OPCODE(0xE158)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu16((Opcode >> 0) & 7);
        flag_V = 0;
        flag_C = src >> (8 - sft);
@@ -37541,7 +37822,7 @@ OPCODE(0xE198)
        u32 sft;
 
        sft = (((Opcode >> 9) - 1) & 7) + 1;
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
        src = DREGu32((Opcode >> 0) & 7);
        flag_V = 0;
        flag_C = src >> (24 - sft);
@@ -37564,7 +37845,7 @@ OPCODE(0xE020)
        src = (s32)DREGs8((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft < 8)
                {
                        flag_V = 0;
@@ -37617,7 +37898,7 @@ OPCODE(0xE060)
        src = (s32)DREGs16((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft < 16)
                {
                        flag_V = 0;
@@ -37675,7 +37956,7 @@ OPCODE(0xE0A0)
        src = (s32)DREGs32((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft < 32)
                {
                        flag_V = 0;
@@ -37729,7 +38010,7 @@ OPCODE(0xE028)
        src = DREGu8((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft <= 8)
                {
                        flag_N = flag_V = 0;
@@ -37768,7 +38049,7 @@ OPCODE(0xE068)
        src = DREGu16((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft <= 16)
                {
                        flag_N = flag_V = 0;
@@ -37812,7 +38093,7 @@ OPCODE(0xE0A8)
        src = DREGu32((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft < 32)
                {
                        flag_N = flag_V = 0;
@@ -37854,7 +38135,7 @@ OPCODE(0xE030)
        src = DREGu8((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                sft %= 9;
 
                src |= (flag_X & M68K_SR_X) << 0;
@@ -37886,7 +38167,7 @@ OPCODE(0xE070)
        src = DREGu16((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                sft %= 17;
 
                src |= (flag_X & M68K_SR_X) << 8;
@@ -37923,14 +38204,14 @@ OPCODE(0xE0B0)
        src = DREGu32((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                sft %= 33;
 
                if (sft != 0)
                {
                        if (sft == 1) res = (src >> 1) | ((flag_X & M68K_SR_X) << (32 - (M68K_SR_X_SFT + 1)));
                        else res = (src >> sft) | (src << (33 - sft)) | (((flag_X & M68K_SR_X) << (32 - (M68K_SR_X_SFT + 1))) >> (sft - 1));
-                       flag_X = (src >> (32 - sft)) << M68K_SR_X_SFT;
+                       flag_X = (src >> (sft - 1)) << M68K_SR_X_SFT;
                }
                else res = src;
                flag_C = flag_X;
@@ -37961,7 +38242,7 @@ OPCODE(0xE038)
        src = DREGu8((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                sft &= 0x07;
 
                flag_C = src << (M68K_SR_C_SFT - ((sft - 1) & 7));
@@ -37992,7 +38273,7 @@ OPCODE(0xE078)
        src = DREGu16((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                sft &= 0x0F;
 
                flag_C = (src >> ((sft - 1) & 15)) << M68K_SR_C_SFT;
@@ -38028,7 +38309,7 @@ OPCODE(0xE0B8)
        src = DREGu32((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                sft &= 0x1F;
 
                flag_C = (src >> ((sft - 1) & 31)) << M68K_SR_C_SFT;
@@ -38060,7 +38341,7 @@ OPCODE(0xE120)
        src = DREGu8((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft < 8)
                {
                        flag_X = flag_C = (src << sft) >> 0;
@@ -38108,7 +38389,7 @@ OPCODE(0xE160)
        src = DREGu16((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft < 16)
                {
                        flag_X = flag_C = (src << sft) >> 8;
@@ -38161,7 +38442,7 @@ OPCODE(0xE1A0)
        src = DREGu32((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft < 32)
                {
                        flag_X = flag_C = (src >> (32 - sft)) << M68K_SR_C_SFT;
@@ -38210,7 +38491,7 @@ OPCODE(0xE128)
        src = DREGu8((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft <= 8)
                {
                        flag_X = flag_C = (src << sft) >> 0;
@@ -38250,7 +38531,7 @@ OPCODE(0xE168)
        src = DREGu16((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft <= 16)
                {
                        flag_X = flag_C = (src << sft) >> 8;
@@ -38295,7 +38576,7 @@ OPCODE(0xE1A8)
        src = DREGu32((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft < 32)
                {
                        flag_X = flag_C = (src >> (32 - sft)) << M68K_SR_C_SFT;
@@ -38338,7 +38619,7 @@ OPCODE(0xE130)
        src = DREGu8((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                sft %= 9;
 
                src |= (flag_X & M68K_SR_X) << 0;
@@ -38370,7 +38651,7 @@ OPCODE(0xE170)
        src = DREGu16((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                sft %= 17;
 
                src |= (flag_X & M68K_SR_X) << 8;
@@ -38407,7 +38688,7 @@ OPCODE(0xE1B0)
        src = DREGu32((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                sft %= 33;
 
                if (sft != 0)
@@ -38445,7 +38726,7 @@ OPCODE(0xE138)
        src = DREGu8((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft &= 0x07)
                {
                        flag_C = (src << sft) >> 0;
@@ -38483,7 +38764,7 @@ OPCODE(0xE178)
        src = DREGu16((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft &= 0x0F)
                {
                        flag_C = (src << sft) >> 8;
@@ -38526,7 +38807,7 @@ OPCODE(0xE1B8)
        src = DREGu32((Opcode >> 0) & 7);
        if (sft)
        {
-       m68kcontext.io_cycle_counter -= sft * 2;
+       ctx->io_cycle_counter -= sft * 2;
                if (sft &= 0x1F)
                {
                        flag_C = (src >> (32 - sft)) << M68K_SR_C_SFT;
@@ -40012,20 +40293,18 @@ RET(8)
 }
 
 
+extern int SekIsIdleReady(void);
 extern int SekIsIdleCode(unsigned short *dst, int bytes);
 extern int SekRegisterIdlePatch(unsigned int pc, int oldop, int newop, void *ctx);
 
 OPCODE(idle_detector_bcc8)
 {
-       extern int idledet_start_frame;
-       extern char Pico[];
        int frame_count, cond_true, bytes, ret, newop;
        u16 *dest_pc;
 
        dest_pc = PC + (((s8)(Opcode & 0xFE)) >> 1);
 
-       frame_count = *(int *)(Pico+0x22208+0x1c); // Pico.m.frame_count
-       if (frame_count < idledet_start_frame)
+       if (!SekIsIdleReady())
                goto end;
 
        bytes = 0 - (s8)(Opcode & 0xFE) - 2;
@@ -40035,7 +40314,7 @@ OPCODE(idle_detector_bcc8)
        if (  Opcode & 0x0100)  newop |= 0x400; // beq
        if (!(Opcode & 0x0f00)) newop |= 0xc00; // bra
 
-       ret = SekRegisterIdlePatch(GET_PC - 2, Opcode, newop, &m68kcontext);
+       ret = SekRegisterIdlePatch(GET_PC - 2, Opcode, newop, ctx);
        switch (ret)
        {
                case 0: PC[-1] = newop; break;
@@ -40051,7 +40330,7 @@ end:
        if (cond_true)
        {
                PC = dest_pc;
-               m68kcontext.io_cycle_counter -= 2;
+               ctx->io_cycle_counter -= 2;
        }
 RET(8)
 }