s68k-side Silpheed hack (needed for EU version)
[picodrive.git] / cpu / Cyclone / OpLogic.cpp
index 207ff45..d6b8933 100644 (file)
@@ -23,7 +23,7 @@ int OpBtstReg(int op)
     if (EaCanWrite(tea)==0) return 1;\r
   }\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,size);\r
   use&=~0x0e00; // Use same handler for all registers\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
@@ -36,32 +36,28 @@ int OpBtstReg(int op)
     if(size>=2) Cycles+=2;\r
   }\r
 \r
-  EaCalc (10,0x0e00,sea,0,0,0);\r
-  EaRead (10,    10,sea,0,0x0e00,0,0);\r
+  EaCalcReadNoSE(-1,11,sea,0,0x0e00);\r
 \r
-  EaCalc ( 0,0x003f,tea,size,0,0);\r
-  if (type>0)\r
-    ot("  mov r11,r0\n");\r
-  EaRead ( 0,     0,tea,size,0x003f,0,0);\r
+  EaCalcReadNoSE((type>0)?8:-1,0,tea,size,0x003f);\r
 \r
   if (tea>=0x10)\r
-       ot("  and r10,r10,#7  ;@ mem - do mod 8\n");  // size always 0\r
-  else ot("  and r10,r10,#31 ;@ reg - do mod 32\n"); // size always 2\r
+       ot("  and r11,r11,#7  ;@ mem - do mod 8\n");  // size always 0\r
+  else ot("  and r11,r11,#31 ;@ reg - do mod 32\n"); // size always 2\r
   ot("\n");\r
 \r
   ot("  mov r1,#1\n");\r
-  ot("  tst r0,r1,lsl r10 ;@ Do arithmetic\n");\r
-  ot("  bicne r9,r9,#0x40000000\n");\r
-  ot("  orreq r9,r9,#0x40000000 ;@ Get Z flag\n");\r
+  ot("  tst r0,r1,lsl r11 ;@ Do arithmetic\n");\r
+  ot("  bicne r10,r10,#0x40000000\n");\r
+  ot("  orreq r10,r10,#0x40000000 ;@ Get Z flag\n");\r
   ot("\n");\r
 \r
   if (type>0)\r
   {\r
-    if (type==1) ot("  eor r1,r0,r1,lsl r10 ;@ Toggle bit\n");\r
-    if (type==2) ot("  bic r1,r0,r1,lsl r10 ;@ Clear bit\n");\r
-    if (type==3) ot("  orr r1,r0,r1,lsl r10 ;@ Set bit\n");\r
+    if (type==1) ot("  eor r1,r0,r1,lsl r11 ;@ Toggle bit\n");\r
+    if (type==2) ot("  bic r1,r0,r1,lsl r11 ;@ Clear bit\n");\r
+    if (type==3) ot("  orr r1,r0,r1,lsl r11 ;@ Set bit\n");\r
     ot("\n");\r
-    EaWrite(11,   1,tea,size,0x003f,0,0);\r
+    EaWrite(8,1,tea,size,0x003f,0,0);\r
   }\r
   OpEnd(tea);\r
 \r
@@ -89,20 +85,19 @@ int OpBtstImm(int op)
     if (EaCanWrite(tea)==0) return 1;\r
   }\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,size);\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
   OpStart(op,sea,tea);\r
 \r
   ot("\n");\r
-  EaCalc ( 0,0x0000,sea,0,0,0);\r
-  EaRead ( 0,     0,sea,0,0,0,0);\r
-  ot("  mov r10,#1\n");\r
-  ot("  bic r9,r9,#0x40000000 ;@ Blank Z flag\n");\r
+  EaCalcReadNoSE(-1,0,sea,0,0);\r
+  ot("  mov r11,#1\n");\r
+  ot("  bic r10,r10,#0x40000000 ;@ Blank Z flag\n");\r
   if (tea>=0x10)\r
        ot("  and r0,r0,#7    ;@ mem - do mod 8\n");  // size always 0\r
   else ot("  and r0,r0,#0x1F ;@ reg - do mod 32\n"); // size always 2\r
-  ot("  mov r10,r10,lsl r0 ;@ Make bit mask\n");\r
+  ot("  mov r11,r11,lsl r0 ;@ Make bit mask\n");\r
   ot("\n");\r
 \r
   if(type==1||type==3) {\r
@@ -112,19 +107,23 @@ int OpBtstImm(int op)
     if(size>=2) Cycles+=2;\r
   }\r
 \r
-  EaCalc (11,0x003f,tea,size,0,0);\r
-  EaRead (11,     0,tea,size,0x003f,0,0);\r
-  ot("  tst r0,r10 ;@ Do arithmetic\n");\r
-  ot("  orreq r9,r9,#0x40000000 ;@ Get Z flag\n");\r
+  EaCalcReadNoSE((type>0)?8:-1,0,tea,size,0x003f);\r
+  ot("  tst r0,r11 ;@ Do arithmetic\n");\r
+  ot("  orreq r10,r10,#0x40000000 ;@ Get Z flag\n");\r
   ot("\n");\r
 \r
   if (type>0)\r
   {\r
-    if (type==1) ot("  eor r1,r0,r10 ;@ Toggle bit\n");\r
-    if (type==2) ot("  bic r1,r0,r10 ;@ Clear bit\n");\r
-    if (type==3) ot("  orr r1,r0,r10 ;@ Set bit\n");\r
+    if (type==1) ot("  eor r1,r0,r11 ;@ Toggle bit\n");\r
+    if (type==2) ot("  bic r1,r0,r11 ;@ Clear bit\n");\r
+    if (type==3) ot("  orr r1,r0,r11 ;@ Set bit\n");\r
     ot("\n");\r
-    EaWrite(11,   1,tea,size,0x003f,0,0);\r
+    EaWrite(8,   1,tea,size,0x003f,0,0);\r
+#if CYCLONE_FOR_GENESIS && !MEMHANDLERS_CHANGE_CYCLES\r
+    // this is a bit hacky (device handlers might modify cycles)\r
+    if (tea==0x38||tea==0x39)\r
+      ot("  ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r
+#endif\r
   }\r
 \r
   OpEnd(sea,tea);\r
@@ -146,23 +145,15 @@ int OpNeg(int op)
   if (EaCanRead (ea,size)==0||EaAn(ea)) return 1;\r
   if (EaCanWrite(ea     )==0) return 1;\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,size);\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op,ea);   Cycles=size<2?4:6;\r
-  if(ea >= 0x10) {\r
-    Cycles*=2;\r
-#if CYCLONE_FOR_GENESIS\r
-    // This is same as in Starscream core, CLR uses only 6 cycles for memory EAs.\r
-    // May be this is similar case as with TAS opcode, but this time the dummy\r
-    // read is ignored somehow? Without this hack Fatal Rewind hangs even in Gens.\r
-    if(type==1&&size<2) Cycles-=2;\r
-#endif\r
-  }\r
+  OpStart(op,ea); Cycles=size<2?4:6;\r
+  if(ea >= 0x10)  Cycles*=2;\r
 \r
-  EaCalc (10,0x003f,ea,size,0,0);\r
+  EaCalc (11,0x003f,ea,size,0,0);\r
 \r
-  if (type!=1) EaRead (10,0,ea,size,0x003f,0,0); // Don't need to read for 'clr' (or do we, for dummy read?)\r
+  if (type!=1) EaRead (11,0,ea,size,0x003f,0,0); // Don't need to read for 'clr' (or do we, for a dummy read?)\r
   if (type==1) ot("\n");\r
 \r
   if (type==0)\r
@@ -171,13 +162,13 @@ int OpNeg(int op)
     GetXBit(1);\r
     if(size!=2) ot("  mov r0,r0,asl #%i\n",size?16:24);\r
     ot("  rscs r1,r0,#0 ;@ do arithmetic\n");\r
-    ot("  orr r3,r9,#0xb0000000 ;@ for old Z\n");\r
+    ot("  orr r3,r10,#0xb0000000 ;@ for old Z\n");\r
     OpGetFlags(1,1,0);\r
     if(size!=2) {\r
       ot("  movs r1,r1,asr #%i\n",size?16:24);\r
-      ot("  orreq r9,r9,#0x40000000 ;@ possily missed Z\n");\r
+      ot("  orreq r10,r10,#0x40000000 ;@ possily missed Z\n");\r
     }\r
-    ot("  andeq r9,r9,r3 ;@ fix Z\n");\r
+    ot("  andeq r10,r10,r3 ;@ fix Z\n");\r
     ot("\n");\r
   }\r
 \r
@@ -185,7 +176,7 @@ int OpNeg(int op)
   {\r
     ot(";@ Clear:\n");\r
     ot("  mov r1,#0\n");\r
-    ot("  mov r9,#0x40000000 ;@ NZCV=0100\n");\r
+    ot("  mov r10,#0x40000000 ;@ NZCV=0100\n");\r
     ot("\n");\r
   }\r
 \r
@@ -213,7 +204,8 @@ int OpNeg(int op)
     ot("\n");\r
   }\r
 \r
-  EaWrite(10,     1,ea,size,0x003f,0,0);\r
+  if (type==1) eawrite_check_addrerr=1;\r
+  EaWrite(11,     1,ea,size,0x003f,0,0);\r
 \r
   OpEnd(ea);\r
 \r
@@ -233,14 +225,14 @@ int OpSwap(int op)
 \r
   OpStart(op); Cycles=4;\r
 \r
-  EaCalc (10,0x0007,ea,2,1);\r
-  EaRead (10,     0,ea,2,0x0007,1);\r
+  EaCalc (11,0x0007,ea,2,1);\r
+  EaRead (11,     0,ea,2,0x0007,1);\r
 \r
   ot("  mov r1,r0,ror #16\n");\r
   ot("  adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
   OpGetFlags(0,0);\r
 \r
-  EaWrite(10,     1,8,2,0x0007,1);\r
+  EaWrite(11,     1,8,2,0x0007,1);\r
 \r
   OpEnd();\r
 \r
@@ -260,7 +252,7 @@ int OpTst(int op)
   // See if we can do this opcode:\r
   if (EaCanWrite(sea)==0||EaAn(sea)) return 1;\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,size);\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
   OpStart(op,sea); Cycles=4;\r
@@ -269,7 +261,7 @@ int OpTst(int op)
   EaRead ( 0,     0,sea,size,0x003f,1);\r
 \r
   ot("  adds r0,r0,#0 ;@ Defines NZ, clears CV\n");\r
-  ot("  mrs r9,cpsr ;@ r9=flags\n");\r
+  ot("  mrs r10,cpsr ;@ r10=flags\n");\r
   ot("\n");\r
 \r
   OpEnd(sea);\r
@@ -288,21 +280,21 @@ int OpExt(int op)
   size=(op>>6)&1;\r
   shift=32-(8<<size);\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,size);\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
   OpStart(op); Cycles=4;\r
 \r
-  EaCalc (10,0x0007,ea,size+1,0,0);\r
-  EaRead (10,     0,ea,size+1,0x0007,0,0);\r
+  EaCalc (11,0x0007,ea,size+1,0,0);\r
+  EaRead (11,     0,ea,size+1,0x0007,0,0);\r
 \r
   ot("  mov r0,r0,asl #%d\n",shift);\r
   ot("  adds r0,r0,#0 ;@ Defines NZ, clears CV\n");\r
-  ot("  mrs r9,cpsr ;@ r9=flags\n");\r
+  ot("  mrs r10,cpsr ;@ r10=flags\n");\r
   ot("  mov r1,r0,asr #%d\n",shift);\r
   ot("\n");\r
 \r
-  EaWrite(10,     1,ea,size+1,0x0007,0,0);\r
+  EaWrite(11,     1,ea,size+1,0x0007,0,0);\r
 \r
   OpEnd();\r
   return 0;\r
@@ -313,8 +305,8 @@ int OpExt(int op)
 int OpSet(int op)\r
 {\r
   int cc=0,ea=0;\r
-  int size=0,use=0;\r
-  char *cond[16]=\r
+  int size=0,use=0,changed_cycles=0;\r
+  static const char * const cond[16]=\r
   {\r
     "al","", "hi","ls","cc","cs","ne","eq",\r
     "vc","vs","pl","mi","ge","lt","gt","le"\r
@@ -328,10 +320,11 @@ int OpSet(int op)
   // See if we can do this opcode:\r
   if (EaCanWrite(ea)==0) return 1;\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,size);\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op,ea); Cycles=8;\r
+  changed_cycles=ea<8 && cc>=2;\r
+  OpStart(op,ea,0,changed_cycles); Cycles=8;\r
   if (ea<8) Cycles=4;\r
 \r
   if (cc)\r
@@ -346,18 +339,18 @@ int OpSet(int op)
     case 1: // F\r
       break;\r
     case 2: // hi\r
-      ot("  ands r0,r9,#0x60000000 ;@ hi: !C && !Z\n");\r
+      ot("  tst r10,#0x60000000 ;@ hi: !C && !Z\n");\r
       ot("  mvneq r1,r1\n");\r
       if (ea<8) ot("  subeq r5,r5,#2 ;@ Extra cycles\n");\r
       break;\r
     case 3: // ls\r
-      ot("  tst r9,#0x60000000 ;@ ls: C || Z\n");\r
+      ot("  tst r10,#0x60000000 ;@ ls: C || Z\n");\r
       ot("  mvnne r1,r1\n");\r
       if (ea<8) ot("  subne r5,r5,#2 ;@ Extra cycles\n");\r
       break;\r
     default:\r
       ot(";@ Is the condition true?\n");\r
-      ot("  msr cpsr_flg,r9 ;@ ARM flags = 68000 flags\n");\r
+      ot("  msr cpsr_flg,r10 ;@ ARM flags = 68000 flags\n");\r
       ot("  mvn%s r1,r1\n",cond[cc]);\r
       if (ea<8) ot("  sub%s r5,r5,#2 ;@ Extra cycles\n",cond[cc]);\r
       break;\r
@@ -365,10 +358,12 @@ int OpSet(int op)
 \r
   ot("\n");\r
 \r
+  eawrite_check_addrerr=1;\r
   EaCalc (0,0x003f, ea,size,0,0);\r
   EaWrite(0,     1, ea,size,0x003f,0,0);\r
 \r
-  OpEnd(ea);\r
+  opend_op_changes_cycles=changed_cycles;\r
+  OpEnd(ea,0);\r
   return 0;\r
 }\r
 \r
@@ -383,7 +378,7 @@ static int EmitAsr(int op,int type,int dir,int count,int size,int usereg)
   if (usereg)\r
   {\r
     ot(";@ Use Dn for count:\n");\r
-    ot("  and r2,r8,#7<<9\n");\r
+    ot("  and r2,r8,#0x0e00\n");\r
     ot("  ldr r2,[r7,r2,lsr #7]\n");\r
     ot("  and r2,r2,#63\n");\r
     ot("\n");\r
@@ -409,29 +404,30 @@ static int EmitAsr(int op,int type,int dir,int count,int size,int usereg)
       ot("\n");\r
     }\r
 \r
-    if (type==0 && dir) ot("  mov r3,r0 ;@ save old value for V flag calculation\n");\r
+    if (type==0 && dir) ot("  adds r3,r0,#0 ;@ save old value for V flag calculation, also clear V\n");\r
 \r
     ot(";@ Shift register:\n");\r
     if (type==0) ot("  movs r0,r0,%s %s\n",dir?"asl":"asr",pct);\r
     if (type==1) ot("  movs r0,r0,%s %s\n",dir?"lsl":"lsr",pct);\r
 \r
-    if (dir==0 && size<2)\r
-    {\r
-      ot(";@ restore after right shift:\n");\r
-      ot("  mov r0,r0,lsl #%d\n",32-(8<<size));\r
-      ot("\n");\r
-    }\r
-\r
     OpGetFlags(0,0);\r
     if (usereg) { // store X only if count is not 0\r
       ot("  cmp %s,#0 ;@ shifting by 0?\n",pct);\r
-      ot("  biceq r9,r9,#0x20000000 ;@ if so, clear carry\n");\r
-      ot("  movne r1,r9,lsr #28\n");\r
-      ot("  strneb r1,[r7,#0x45] ;@ else Save X bit\n");\r
+      ot("  biceq r10,r10,#0x20000000 ;@ if so, clear carry\n");\r
+      ot("  strne r10,[r7,#0x4c] ;@ else Save X bit\n");\r
     } else {\r
       // count will never be 0 if we use immediate\r
-      ot("  mov r1,r9,lsr #28\n");\r
-      ot("  strb r1,[r7,#0x45] ;@ Save X bit\n");\r
+      ot("  str r10,[r7,#0x4c] ;@ Save X bit\n");\r
+    }\r
+    ot("\n");\r
+\r
+    if (dir==0 && size<2)\r
+    {\r
+      ot(";@ restore after right shift:\n");\r
+      ot("  movs r0,r0,lsl #%d\n",32-(8<<size));\r
+      if (type)\r
+        ot("  orrmi r10,r10,#0x80000000 ;@ Potentially missed N flag\n");\r
+      ot("\n");\r
     }\r
 \r
     if (type==0 && dir) {\r
@@ -439,11 +435,11 @@ static int EmitAsr(int op,int type,int dir,int count,int size,int usereg)
       ot("  mov r1,#0x80000000\n");\r
       ot("  ands r3,r3,r1,asr %s\n", pct);\r
       ot("  cmpne r3,r1,asr %s\n", pct);\r
-      ot("  biceq r9,r9,#0x10000000\n");\r
-      ot("  orrne r9,r9,#0x10000000\n");\r
+      ot("  eoreq r1,r0,r3\n"); // above check doesn't catch (-1)<<(32+), so we need this\r
+      ot("  tsteq r1,#0x80000000\n");\r
+      ot("  orrne r10,r10,#0x10000000\n");\r
+      ot("\n");\r
     }\r
-\r
-    ot("\n");\r
   }\r
 \r
   // --------------------------------------\r
@@ -452,7 +448,8 @@ static int EmitAsr(int op,int type,int dir,int count,int size,int usereg)
     int wide=8<<size;\r
 \r
     // Roxr\r
-    if(count == 1) {\r
+    if(count == 1)\r
+    {\r
       if(dir==0) {\r
         if(size!=2) {\r
           ot("  orr r0,r0,r0,lsr #%i\n", size?16:24);\r
@@ -462,25 +459,33 @@ static int EmitAsr(int op,int type,int dir,int count,int size,int usereg)
         ot("  movs r0,r0,rrx\n");\r
         OpGetFlags(0,1);\r
       } else {\r
-        ot("  ldrb r3,[r7,#0x45]\n");\r
+        ot("  ldr r3,[r7,#0x4c]\n");\r
         ot("  movs r0,r0,lsl #1\n");\r
         OpGetFlags(0,1);\r
-        ot("  tst r3,#2\n");\r
+        ot("  tst r3,#0x20000000\n");\r
         ot("  orrne r0,r0,#0x%x\n", 1<<(32-wide));\r
-        ot("  bicne r9,r9,#0x40000000 ;@ clear Z in case it got there\n");\r
+        ot("  bicne r10,r10,#0x40000000 ;@ clear Z in case it got there\n");\r
       }\r
-      ot("  bic r9,r9,#0x10000000 ;@ make suve V is clear\n");\r
+      ot("  bic r10,r10,#0x10000000 ;@ make suve V is clear\n");\r
       return 0;\r
     }\r
 \r
     if (usereg)\r
     {\r
-      ot(";@ Reduce r2 until <0:\n");\r
-      ot("Reduce_%.4x%s\n",op,ms?"":":");\r
-      ot("  subs r2,r2,#%d\n",wide+1);\r
-      ot("  bpl Reduce_%.4x\n",op);\r
-      ot("  adds r2,r2,#%d ;@ Now r2=0-%d\n",wide+1,wide);\r
-      ot("  beq norotx%.4x\n",op);\r
+      if (size==2)\r
+      {\r
+        ot("  subs r2,r2,#33\n");\r
+        ot("  addmis r2,r2,#33 ;@ Now r2=0-%d\n",wide);\r
+      }\r
+      else\r
+      {\r
+        ot(";@ Reduce r2 until <0:\n");\r
+        ot("Reduce_%.4x%s\n",op,ms?"":":");\r
+        ot("  subs r2,r2,#%d\n",wide+1);\r
+        ot("  bpl Reduce_%.4x\n",op);\r
+        ot("  adds r2,r2,#%d ;@ Now r2=0-%d\n",wide+1,wide);\r
+      }\r
+      ot("  beq norotx_%.4x\n",op);\r
       ot("\n");\r
     }\r
 \r
@@ -496,36 +501,31 @@ static int EmitAsr(int op,int type,int dir,int count,int size,int usereg)
 \r
     if (shift) ot("  mov r0,r0,lsr #%d ;@ Shift down\n",shift);\r
 \r
+    ot("\n");\r
+    ot(";@ First get X bit (middle):\n");\r
+    ot("  ldr r3,[r7,#0x4c]\n");\r
+    ot("  rsb r1,r2,#%d\n",wide);\r
+    ot("  and r3,r3,#0x20000000\n");\r
+    ot("  mov r3,r3,lsr #29\n");\r
+    ot("  mov r3,r3,lsl r1\n");\r
+\r
     ot(";@ Rotate bits:\n");\r
-    ot("  mov r3,r0,lsr r2 ;@ Get right part\n");\r
+    ot("  orr r3,r3,r0,lsr r2 ;@ Orr right part\n");\r
     ot("  rsbs r2,r2,#%d ;@ should also clear ARM V\n",wide+1);\r
-    ot("  movs r0,r0,lsl r2 ;@ Get left part\n");\r
-    ot("  orr r0,r3,r0 ;@ r0=Rotated value\n");\r
-\r
-    ot(";@ Insert X bit into r2-1:\n");\r
-    ot("  ldrb r3,[r7,#0x45]\n");\r
-    ot("  sub r2,r2,#1\n");\r
-    ot("  and r3,r3,#2\n");\r
-    ot("  mov r3,r3,lsr #1\n");\r
-    ot("  orr r0,r0,r3,lsl r2\n");\r
+    ot("  orrs r0,r3,r0,lsl r2 ;@ Orr left part, set flags\n");\r
     ot("\n");\r
 \r
     if (shift) ot("  movs r0,r0,lsl #%d ;@ Shift up and get correct NC flags\n",shift);\r
     OpGetFlags(0,!usereg);\r
-    if (!shift) {\r
-      ot("  tst r0,r0\n");\r
-      ot("  bicne r9,r9,#0x40000000 ;@ make sure we didn't mess Z\n");\r
-    }\r
     if (usereg) { // store X only if count is not 0\r
-      ot("  mov r2,r9,lsr #28\n");\r
-      ot("  strb r2,[r7,#0x45] ;@ if not 0, Save X bit\n");\r
+      ot("  str r10,[r7,#0x4c] ;@ if not 0, Save X bit\n");\r
       ot("  b nozerox%.4x\n",op);\r
-      ot("norotx%.4x%s\n",op,ms?"":":");\r
-      ot("  ldrb r2,[r7,#0x45]\n");\r
+      ot("norotx_%.4x%s\n",op,ms?"":":");\r
+      ot("  ldr r2,[r7,#0x4c]\n");\r
       ot("  adds r0,r0,#0 ;@ Defines NZ, clears CV\n");\r
       OpGetFlags(0,0);\r
-      ot("  and r2,r2,#2\n");\r
-      ot("  orr r9,r9,r2,lsl #28 ;@ C = old_X\n");\r
+      ot("  and r2,r2,#0x20000000\n");\r
+      ot("  orr r10,r10,r2 ;@ C = old_X\n");\r
       ot("nozerox%.4x%s\n",op,ms?"":":");\r
     }\r
 \r
@@ -545,9 +545,10 @@ static int EmitAsr(int op,int type,int dir,int count,int size,int usereg)
     }\r
 \r
     ot(";@ Rotate register:\n");\r
+    if (!dir) ot("  adds r0,r0,#0 ;@ first clear V and C\n"); // ARM does not clear C if rot count is 0\r
     if (count<0)\r
     {\r
-      if (dir) ot("  rsbs %s,%s,#32\n",pct,pct);\r
+      if (dir) ot("  rsb %s,%s,#32\n",pct,pct);\r
       ot("  movs r0,r0,ror %s\n",pct);\r
     }\r
     else\r
@@ -558,9 +559,9 @@ static int EmitAsr(int op,int type,int dir,int count,int size,int usereg)
     }\r
 \r
     OpGetFlags(0,0);\r
-    if (!dir) ot("  bic r9,r9,#0x10000000 ;@ make suve V is clear\n");\r
     if (dir)\r
     {\r
+      ot("  bic r10,r10,#0x30000000 ;@ clear CV\n");\r
       ot(";@ Get carry bit from bit 0:\n");\r
       if (usereg)\r
       {\r
@@ -569,15 +570,7 @@ static int EmitAsr(int op,int type,int dir,int count,int size,int usereg)
       }\r
       else\r
         ot("  tst r0,#1\n");\r
-      ot("  orrne r9,r9,#0x20000000\n");\r
-      ot("  biceq r9,r9,#0x20000000\n");\r
-    }\r
-    else if (usereg)\r
-    {\r
-      // if we rotate something by 0, ARM doesn't clear C\r
-      // so we need to detect that\r
-      ot("  cmp %s,#0\n",pct);\r
-      ot("  biceq r9,r9,#0x20000000\n");\r
+      ot("  orrne r10,r10,#0x20000000\n");\r
     }\r
     ot("\n");\r
 \r
@@ -595,7 +588,6 @@ int OpAsr(int op)
   int count=0,dir=0;\r
   int size=0,usereg=0,type=0;\r
 \r
-  ea=0;\r
   count =(op>>9)&7;\r
   dir   =(op>>8)&1;\r
   size  =(op>>6)&3;\r
@@ -608,22 +600,23 @@ int OpAsr(int op)
   // Use the same opcode for target registers:\r
   use=op&~0x0007;\r
 \r
-  // As long as count is not 8, use the same opcode for all shift counts::\r
+  // As long as count is not 8, use the same opcode for all shift counts:\r
   if (usereg==0 && count!=8 && !(count==1&&type==2)) { use|=0x0e00; count=-1; }\r
   if (usereg) { use&=~0x0e00; count=-1; } // Use same opcode for all Dn\r
 \r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op); Cycles=size<2?6:8;\r
+  OpStart(op,ea,0,count<0); Cycles=size<2?6:8;\r
 \r
-  EaCalc(10,0x0007, ea,size,1);\r
-  EaRead(10,     0, ea,size,0x0007,1);\r
+  EaCalc(11,0x0007, ea,size,1);\r
+  EaRead(11,     0, ea,size,0x0007,1);\r
 \r
   EmitAsr(op,type,dir,count, size,usereg);\r
 \r
-  EaWrite(10,    0, ea,size,0x0007,1);\r
+  EaWrite(11,    0, ea,size,0x0007,1);\r
 \r
-  OpEnd();\r
+  opend_op_changes_cycles = (count<0);\r
+  OpEnd(ea,0);\r
 \r
   return 0;\r
 }\r
@@ -642,17 +635,17 @@ int OpAsrEa(int op)
   if (EaCanRead(ea,0)==0) return 1;\r
   if (EaCanWrite(ea)==0) return 1;\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,size);\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
   OpStart(op,ea); Cycles=6; // EmitAsr() will add 2\r
 \r
-  EaCalc (10,0x003f,ea,size,1);\r
-  EaRead (10,     0,ea,size,0x003f,1);\r
+  EaCalc (11,0x003f,ea,size,1);\r
+  EaRead (11,     0,ea,size,0x003f,1);\r
 \r
   EmitAsr(op,type,dir,1,size,0);\r
 \r
-  EaWrite(10,     0,ea,size,0x003f,1);\r
+  EaWrite(11,     0,ea,size,0x003f,1);\r
 \r
   OpEnd(ea);\r
   return 0;\r
@@ -668,7 +661,7 @@ int OpTas(int op, int gen_special)
   // See if we can do this opcode:\r
   if (EaCanWrite(ea)==0 || EaAn(ea)) return 1;\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,0);\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
   if (!gen_special) OpStart(op,ea);\r
@@ -678,8 +671,8 @@ int OpTas(int op, int gen_special)
   Cycles=4;\r
   if(ea>=8) Cycles+=10;\r
 \r
-  EaCalc (10,0x003f,ea,0,1);\r
-  EaRead (10,     1,ea,0,0x003f,1);\r
+  EaCalc (11,0x003f,ea,0,1);\r
+  EaRead (11,     1,ea,0,0x003f,1);\r
 \r
   ot("  adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
   OpGetFlags(0,0);\r
@@ -691,7 +684,7 @@ int OpTas(int op, int gen_special)
 #endif\r
     ot("  orr r1,r1,#0x80000000 ;@ set bit7\n");\r
 \r
-    EaWrite(10,     1,ea,0,0x003f,1);\r
+    EaWrite(11,     1,ea,0,0x003f,1);\r
 #if CYCLONE_FOR_GENESIS\r
   }\r
 #endif\r