code review and optimizations
[picodrive.git] / cpu / Cyclone / OpMove.cpp
index 7d317b0..c0e436c 100644 (file)
@@ -6,15 +6,15 @@
 // trashes r0,r2\r
 void OpFlagsToReg(int high)\r
 {\r
-  ot("  ldrb r0,[r7,#0x45]  ;@ X bit\n");\r
+  ot("  ldr r0,[r7,#0x4c]   ;@ X bit\n");\r
   ot("  mov r1,r9,lsr #28   ;@ ____NZCV\n");\r
   ot("  eor r2,r1,r1,ror #1 ;@ Bit 0=C^V\n");\r
   ot("  tst r2,#1           ;@ 1 if C!=V\n");\r
   ot("  eorne r1,r1,#3      ;@ ____NZVC\n");\r
   ot("\n");\r
   if (high) ot("  ldrb r2,[r7,#0x44]  ;@ Include SR high\n");\r
-  ot("  and r0,r0,#0x02\n");\r
-  ot("  orr r1,r1,r0,lsl #3 ;@ ___XNZVC\n");\r
+  ot("  and r0,r0,#0x20000000\n");\r
+  ot("  orr r1,r1,r0,lsr #25 ;@ ___XNZVC\n");\r
   if (high) ot("  orr r1,r1,r2,lsl #8\n");\r
   ot("\n");\r
 }\r
@@ -24,10 +24,10 @@ void OpFlagsToReg(int high)
 void OpRegToFlags(int high)\r
 {\r
   ot("  eor r1,r0,r0,ror #1 ;@ Bit 0=C^V\n");\r
-  ot("  mov r2,r0,lsr #3    ;@ r2=___XN\n");\r
+  ot("  mov r2,r0,lsl #25\n");\r
   ot("  tst r1,#1           ;@ 1 if C!=V\n");\r
   ot("  eorne r0,r0,#3      ;@ ___XNZCV\n");\r
-  ot("  strb r2,[r7,#0x45]  ;@ Store X bit\n");\r
+  ot("  str r2,[r7,#0x4c]   ;@ Store X bit\n");\r
   ot("  mov r9,r0,lsl #28   ;@ r9=NZCV...\n");\r
 \r
   if (high)\r
@@ -45,14 +45,16 @@ void SuperCheck(int op)
 {\r
   ot("  ldr r11,[r7,#0x44] ;@ Get SR high\n");\r
   ot("  tst r11,#0x20 ;@ Check we are in supervisor mode\n");\r
-  ot("  beq WrongMode%.4x ;@ No\n",op);\r
+  ot("  beq WrongPrivilegeMode ;@ No\n");\r
   ot("\n");\r
 }\r
 \r
-void SuperEnd(int op)\r
+void SuperEnd(void)\r
 {\r
-  ot("WrongMode%.4x%s\n",op,ms?"":":");\r
-  ot("  sub r4,r4,#2 ;@ this opcode wasn't executed - go back\n");\r
+  ot(";@ ----------\n");\r
+  ot(";@ tried execute privileged instruction in user mode\n");\r
+  ot("WrongPrivilegeMode%s\n",ms?"":":");\r
+  ot("  sub r4,r4,#2 ;@ last opcode wasn't executed - go back\n");\r
   ot("  mov r0,#0x20 ;@ privilege violation\n");\r
   ot("  bl Exception\n");\r
   Cycles=34;\r
@@ -61,20 +63,20 @@ void SuperEnd(int op)
 \r
 // does OSP and A7 swapping if needed\r
 // new or old SR (not the one already in [r7,#0x44]) should be passed in r11\r
-// trashes r1,r11\r
-void SuperChange(int op)\r
+// trashes r0,r11\r
+void SuperChange(int op,int load_srh)\r
 {\r
   ot(";@ A7 <-> OSP?\n");\r
-  ot("  ldr r1,[r7,#0x44] ;@ Get other SR high\n");\r
-  ot("  and r11,r11,#0x20\n");\r
-  ot("  and r1,r1,#0x20\n");\r
-  ot("  teq r11,r1 ;@ r11 xor r1\n");\r
+  if (load_srh)\r
+    ot("  ldr r0,[r7,#0x44] ;@ Get other SR high\n");\r
+  ot("  eor r0,r0,r11\n");\r
+  ot("  tst r0,#0x20\n");\r
   ot("  beq no_sp_swap%.4x\n",op);\r
   ot(" ;@ swap OSP and A7:\n");\r
   ot("  ldr r11,[r7,#0x3C] ;@ Get A7\n");\r
-  ot("  ldr r1, [r7,#0x48] ;@ Get OSP\n");\r
+  ot("  ldr r0, [r7,#0x48] ;@ Get OSP\n");\r
   ot("  str r11,[r7,#0x48]\n");\r
-  ot("  str r1, [r7,#0x3C]\n");\r
+  ot("  str r0, [r7,#0x3C]\n");\r
   ot("no_sp_swap%.4x%s\n", op, ms?"":":");\r
 }\r
 \r
@@ -110,19 +112,19 @@ int OpMove(int op)
   if (EaCanRead (sea,size)==0) return 1;\r
   if (EaCanWrite(tea     )==0) return 1;\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,size);\r
   if (tea<0x38) use&=~0x0e00; // Use same handler for register ?0-7\r
   \r
-  if (tea>=0x18 && tea<0x28 && (tea&7)==7) use|=0x0e00; // Specific handler for (a7)+ and -(a7)\r
+  if (tea==0x1f || tea==0x27) use|=0x0e00; // Specific handler for (a7)+ and -(a7)\r
 \r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
   OpStart(op,sea,tea); Cycles=4;\r
 \r
-  EaCalc(0,0x003f,sea,size);\r
-  EaRead(0,     1,sea,size,0x003f);\r
+  EaCalcRead(-1,1,sea,size,0x003f);\r
 \r
-  if (movea==0) {\r
+  if (movea==0)\r
+  {\r
     ot("  adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
     ot("  mrs r9,cpsr ;@ r9=NZCV flags\n");\r
     ot("\n");\r
@@ -130,19 +132,19 @@ int OpMove(int op)
 \r
   if (movea) size=2; // movea always expands to 32-bits\r
 \r
-  EaCalc (0,0x0e00,tea,size,0,0);\r
 #if SPLIT_MOVEL_PD\r
+  EaCalc (10,0x1e00,tea,size,0,0);\r
   if ((tea&0x38)==0x20 && size==2) { // -(An)\r
-    ot("  mov r10,r0\n");\r
     ot("  mov r11,r1\n");\r
-    ot("  add r0,r0,#2\n");\r
-    EaWrite(0,     1,tea,1,0x0e00,0,0);\r
-    EaWrite(10,   11,tea,1,0x0e00,1);\r
+    ot("  add r0,r10,#2\n");\r
+    EaWrite(0,     1,tea,1,0x1e00,0,0);\r
+    EaWrite(10,   11,tea,1,0x1e00,1);\r
   } else {\r
-    EaWrite(0,     1,tea,size,0x0e00,0,0);\r
+    EaWrite(0,     1,tea,size,0x1e00,0,0);\r
   }\r
 #else\r
-  EaWrite(0,     1,tea,size,0x0e00,0,0);\r
+  EaCalc (0,0x1e00,tea,size,0,0);\r
+  EaWrite(0,     1,tea,size,0x1e00,0,0);\r
 #endif\r
 \r
 #if CYCLONE_FOR_GENESIS && !MEMHANDLERS_CHANGE_CYCLES\r
@@ -169,15 +171,15 @@ int OpLea(int op)
 \r
   if (EaCanRead(sea,-1)==0) return 1; // See if we can do this opcode\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,0);\r
   use&=~0x0e00; // Also use 1 handler for target ?0-7\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
   OpStart(op,sea,tea);\r
 \r
   EaCalc (1,0x003f,sea,0); // Lea\r
-  EaCalc (0,0x0e00,tea,2,1);\r
-  EaWrite(0,     1,tea,2,0x0e00,1);\r
+  EaCalc (0,0x1e00,tea,2);\r
+  EaWrite(0,     1,tea,2,0x1e00);\r
 \r
   Cycles=Ea_add_ns(g_lea_cycle_table,sea);\r
 \r
@@ -212,7 +214,7 @@ int OpMoveSr(int op)
       break;\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,ea);\r
@@ -230,19 +232,16 @@ int OpMoveSr(int op)
 \r
   if (type==2 || type==3)\r
   {\r
-    EaCalc(0,0x003f,ea,size,0,0);\r
-    EaRead(0,     0,ea,size,0x003f,0,0);\r
+    EaCalcReadNoSE(-1,0,ea,size,0x003f);\r
     OpRegToFlags(type==3);\r
     if (type==3) {\r
-      SuperChange(op);\r
+      SuperChange(op,0);\r
       CheckInterrupt(op);\r
     }\r
   }\r
 \r
   OpEnd(ea);\r
 \r
-  if (type==3) SuperEnd(op);\r
-\r
   return 0;\r
 }\r
 \r
@@ -257,7 +256,7 @@ int OpArithSr(int op)
   size=(op>>6)&1; // ccr or sr?\r
   ea=0x3c;\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=16;\r
@@ -273,12 +272,11 @@ int OpArithSr(int op)
   if (type==5) ot("  eor r0,r1,r10\n");\r
   OpRegToFlags(size);\r
   if (size) {\r
-    SuperChange(op);\r
+    SuperChange(op,0);\r
     CheckInterrupt(op);\r
   }\r
 \r
   OpEnd(ea);\r
-  if (size) SuperEnd(op);\r
 \r
   return 0;\r
 }\r
@@ -293,7 +291,7 @@ int OpPea(int op)
   ea=op&0x003f; if (ea<0x10) return 1; // Swap opcode\r
   if (EaCanRead(ea,-1)==0) return 1; // See if we can do this opcode:\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,0);\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
   OpStart(op,ea);\r
@@ -336,7 +334,7 @@ int OpMovem(int op)
 \r
   cea=ea; if (change) cea=0x10;\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);\r
@@ -430,20 +428,18 @@ int OpMoveUsp(int op)
   if (dir)\r
   {\r
     ot("  ldr r1,[r7,#0x48] ;@ Get from USP\n\n");\r
-    EaCalc (0,0x0007,8,2,1);\r
-    EaWrite(0,     1,8,2,0x0007,1);\r
+    EaCalc (0,0x000f,8,2,1);\r
+    EaWrite(0,     1,8,2,0x000f,1);\r
   }\r
   else\r
   {\r
-    EaCalc (0,0x0007,8,2,1);\r
-    EaRead (0,     0,8,2,0x0007,1);\r
+    EaCalc (0,0x000f,8,2,1);\r
+    EaRead (0,     0,8,2,0x000f,1);\r
     ot("  str r0,[r7,#0x48] ;@ Put in USP\n\n");\r
   }\r
     \r
   OpEnd();\r
 \r
-  SuperEnd(op);\r
-\r
   return 0;\r
 }\r
 \r
@@ -509,14 +505,15 @@ int OpExg(int op)
 // 0000sss1 1z001ddd (to mem)\r
 int OpMovep(int op)\r
 {\r
-  int ea=0;\r
-  int size=1,use=0,dir;\r
+  int ea=0,rea=0;\r
+  int size=1,use=0,dir,aadd=0;\r
 \r
   use=op&0xf1f8;\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler (for all dests, srcs)\r
 \r
   // Get EA\r
   ea = (op&0x0007)|0x28;\r
+  rea=  op&0x0e00;\r
   dir = (op>>7)&1;\r
 \r
   // Find size extension\r
@@ -525,41 +522,43 @@ int OpMovep(int op)
   OpStart(op,ea);\r
   \r
   if(dir) { // reg to mem\r
-    EaCalc(11,0x0e00,0,size);      // reg number -> r11\r
-    EaRead(11,11,0,size,0x0e00);   // regval -> r11\r
-    EaCalc(10,0x0007,ea,size);\r
+    EaCalcReadNoSE(-1,11,rea,size,0x1e00);\r
+\r
+    EaCalc(10,0x000f,ea,size);\r
     if(size==2) { // if operand is long\r
       ot("  mov r1,r11,lsr #24 ;@ first byte\n");\r
-      EaWrite(10,1,ea,0,0x0007); // store first byte\r
-      ot("  add r10,r10,#2\n");\r
+      EaWrite(10,1,ea,0,0x000f); // store first byte\r
+      ot("  add r0,r10,#%i\n",(aadd+=2));\r
       ot("  mov r1,r11,lsr #16 ;@ second byte\n");\r
-      EaWrite(10,1,ea,0,0x0007); // store second byte\r
-      ot("  add r10,r10,#2\n");\r
+      EaWrite(0,1,ea,0,0x000f); // store second byte\r
+      ot("  add r0,r10,#%i\n",(aadd+=2));\r
+    } else {\r
+      ot("  mov r0,r10\n");\r
     }\r
     ot("  mov r1,r11,lsr #8 ;@ first or third byte\n");\r
-    EaWrite(10,1,ea,0,0x0007);\r
-    ot("  add r10,r10,#2\n");\r
+    EaWrite(0,1,ea,0,0x000f);\r
+    ot("  add r0,r10,#%i\n",(aadd+=2));\r
     ot("  and r1,r11,#0xff\n");\r
-    EaWrite(10,1,ea,0,0x0007);\r
+    EaWrite(0,1,ea,0,0x000f);\r
   } else { // mem to reg\r
-    EaCalc(10,0x0007,ea,size,1);\r
-    EaRead(10,11,ea,0,0x0007,1); // read first byte\r
-    ot("  add r10,r10,#2\n");\r
-    EaRead(10,1,ea,0,0x0007,1); // read second byte\r
+    EaCalc(10,0x000f,ea,size,1);\r
+    EaRead(10,11,ea,0,0x000f,1); // read first byte\r
+    ot("  add r0,r10,#2\n");\r
+    EaRead(0,1,ea,0,0x000f,1); // read second byte\r
     if(size==2) { // if operand is long\r
       ot("  orr r11,r11,r1,lsr #8 ;@ second byte\n");\r
-      ot("  add r10,r10,#2\n");\r
-      EaRead(10,1,ea,0,0x0007,1);\r
+      ot("  add r0,r10,#4\n");\r
+      EaRead(0,1,ea,0,0x000f,1);\r
       ot("  orr r11,r11,r1,lsr #16 ;@ third byte\n");\r
-      ot("  add r10,r10,#2\n");\r
-      EaRead(10,1,ea,0,0x0007,1);\r
-      ot("  orr r0,r11,r1,lsr #24 ;@ fourth byte\n");\r
+      ot("  add r0,r10,#6\n");\r
+      EaRead(0,1,ea,0,0x000f,1);\r
+      ot("  orr r1,r11,r1,lsr #24 ;@ fourth byte\n");\r
     } else {\r
-      ot("  orr r0,r11,r1,lsr #8 ;@ second byte\n");\r
+      ot("  orr r1,r11,r1,lsr #8 ;@ second byte\n");\r
     }\r
     // store the result\r
-    EaCalc(11,0x0e00,0,size,1);      // reg number -> r11\r
-    EaWrite(11,0,0,size,0x0e00,1);\r
+    EaCalc(11,0x0e00,rea,size,1);      // reg number -> r11\r
+    EaWrite(11,1,rea,size,0x0e00,1);\r
   }\r
 \r
   Cycles=(size==2)?24:16;\r
@@ -571,7 +570,7 @@ int OpMovep(int op)
 // Emit a Stop/Reset opcodes, 01001110 011100t0 imm\r
 int OpStopReset(int op)\r
 {\r
-  int type=(op>>1)&1; // reset/stop\r
+  int type=(op>>1)&1; // stop/reset\r
 \r
   OpStart(op);\r
 \r
@@ -580,8 +579,8 @@ int OpStopReset(int op)
   if(type) {\r
     // copy immediate to SR, stop the CPU and eat all remaining cycles.\r
     ot("  ldrh r0,[r4],#2 ;@ Fetch the immediate\n");\r
-    SuperChange(op);\r
     OpRegToFlags(1);\r
+    SuperChange(op,0);\r
 \r
     ot("\n");\r
 \r
@@ -604,16 +603,16 @@ int OpStopReset(int op)
     ot("  ldr r11,[r7,#0x90] ;@ ResetCallback\n");\r
     ot("  tst r11,r11\n");\r
     ot("  movne lr,pc\n");\r
-    ot("  movne pc,r11 ;@ call ResetCallback if it is defined\n");\r
+    ot("  bxne r11 ;@ call ResetCallback if it is defined\n");\r
     ot("  ldrb r9,[r7,#0x46] ;@ r9 = Load Flags (NZCV)\n");\r
     ot("  ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r
     ot("  ldr r4,[r7,#0x40] ;@ Load PC\n");\r
     ot("  mov r9,r9,lsl #28\n");\r
+    ot("\n");\r
 #endif\r
   }\r
 \r
   OpEnd();\r
-  SuperEnd(op);\r
 \r
   return 0;\r
 }\r