some more optimizations
[picodrive.git] / cpu / Cyclone / OpArith.cpp
index 29b6cdf..74673b8 100644 (file)
@@ -8,6 +8,7 @@ int OpArith(int op)
   int type=0,size=0;\r
   int sea=0,tea=0;\r
   int use=0;\r
+  char *shiftstr="";\r
 \r
   // Get source and target EA\r
   type=(op>>9)&7; if (type==4 || type>=7) return 1;\r
@@ -22,22 +23,24 @@ int OpArith(int op)
   use=OpBase(op);\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op, sea|tea); Cycles=4;\r
+  OpStart(op, seatea); Cycles=4;\r
 \r
   EaCalc(10,0x0000, sea,size,1);\r
-  EaRead(10,    10, sea,size,0,1);\r
-\r
   EaCalc(11,0x003f, tea,size,1);\r
+  EaRead(10,    10, sea,size,0,0,0);\r
   EaRead(11,     0, tea,size,0x003f,1);\r
 \r
+  if (size==0) shiftstr=",asl #24";\r
+  else if (size==1) shiftstr=",asl #16";\r
+\r
   ot(";@ Do arithmetic:\n");\r
 \r
-  if (type==0) ot("  orr r1,r0,r10\n");\r
-  if (type==1) ot("  and r1,r0,r10\n");\r
-  if (type==2) ot("  subs r1,r0,r10 ;@ Defines NZCV\n");\r
-  if (type==3) ot("  adds r1,r0,r10 ;@ Defines NZCV\n");\r
-  if (type==5) ot("  eor r1,r0,r10\n");\r
-  if (type==6) ot("  cmp r0,r10 ;@ Defines NZCV\n");\r
+  if (type==0) ot("  orr r1,r0,r10%s\n",shiftstr);\r
+  if (type==1) ot("  and r1,r0,r10%s\n",shiftstr);\r
+  if (type==2) ot("  subs r1,r0,r10%s ;@ Defines NZCV\n",shiftstr);\r
+  if (type==3) ot("  adds r1,r0,r10%s ;@ Defines NZCV\n",shiftstr);\r
+  if (type==5) ot("  eor r1,r0,r10%s\n",shiftstr);\r
+  if (type==6) ot("  cmp r0,r10%s ;@ Defines NZCV\n",shiftstr);\r
 \r
   if (type<2 || type==5) ot("  adds r1,r1,#0 ;@ Defines NZ, clears CV\n"); // 0,1,5\r
 \r
@@ -65,7 +68,7 @@ int OpArith(int op)
     if (type==1 && size>=2 && tea<8) Cycles-=2;\r
   }\r
 \r
-  OpEnd();\r
+  OpEnd(sea,tea);\r
 \r
   return 0;\r
 }\r
@@ -110,15 +113,17 @@ int OpAddq(int op)
   {\r
     int lsr=9-shift;\r
 \r
-    if (lsr>=0) ot("  mov r2,r8,lsr #%d ;@ Get quick value\n", lsr);\r
-    else        ot("  mov r2,r8,lsl #%d ;@ Get quick value\n",-lsr);\r
+    ot("  and r2,r8,#0x0e00 ;@ Get quick value\n");\r
+\r
+    if (lsr>=0) sprintf(count,"r2,lsr #%d",  lsr);\r
+    else        sprintf(count,"r2,lsl #%d", -lsr);\r
 \r
-    ot("  and r2,r2,#0x%.4x\n",7<<shift);\r
     ot("\n");\r
-    strcpy(count,"r2");\r
   }\r
-\r
-  if (num==8) sprintf(count,"#0x%.4x",8<<shift);\r
+  else\r
+  {\r
+    sprintf(count,"#0x%.4x",8<<shift);\r
+  }\r
 \r
   if (type==0) ot("  adds r1,r0,%s\n",count);\r
   if (type==1) ot("  subs r1,r0,%s\n",count);\r
@@ -128,7 +133,7 @@ int OpAddq(int op)
 \r
   EaWrite(10,     1, ea,size,0x003f,1);\r
 \r
-  OpEnd();\r
+  OpEnd(ea);\r
 \r
   return 0;\r
 }\r
@@ -195,7 +200,7 @@ int OpArithReg(int op)
     }\r
   }\r
 \r
-  OpEnd();\r
+  OpEnd(ea);\r
 \r
   return 0;\r
 }\r
@@ -328,13 +333,13 @@ int OpMul(int op)
   EaWrite(0,     1,rea, 2,0x0e00,1);\r
 \r
   ot("endofop%.4x%s\n",op,ms?"":":");\r
-  OpEnd();\r
+  OpEnd(ea);\r
 \r
   ot("divzero%.4x%s\n",op,ms?"":":");\r
   ot("  mov r0,#0x14 ;@ Divide by zero\n");\r
   ot("  bl Exception\n");\r
   Cycles+=38;\r
-  OpEnd();\r
+  OpEnd(ea);\r
   ot("\n");\r
 \r
   return 0;\r
@@ -370,7 +375,7 @@ int OpAbcd(int op)
   if (sea==0x27||dea==0x27) use=op; // ..except -(a7)\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op,sea|dea); Cycles=6;\r
+  OpStart(op,sea,dea); Cycles=6;\r
 \r
   EaCalc( 0,0x0007, sea,0,1);\r
   EaRead( 0,    10, sea,0,0x0007,1);\r
@@ -438,7 +443,7 @@ int OpAbcd(int op)
   ot("  strb r2,[r7,#0x45] ;@ Save X bit\n");\r
 \r
   EaWrite(11,     0, dea,0,0x0e00,1);\r
-  OpEnd();\r
+  OpEnd(sea,dea);\r
 \r
   return 0;\r
 }\r
@@ -493,7 +498,7 @@ int OpNbcd(int op)
   ot("  mov r2,r9,lsr #28\n");\r
   ot("  strb r2, [r7,#0x45]\n");\r
 \r
-  OpEnd();\r
+  OpEnd(ea);\r
 \r
   return 0;\r
 }\r
@@ -524,21 +529,22 @@ int OpAritha(int op)
   if(type==1) Cycles=6;\r
 \r
   \r
-  EaCalc ( 0,0x003f, sea,size);\r
-  EaRead ( 0,    10, sea,size,0x003f);\r
+  // must calculate reg EA first, because of situations like: suba.w (A0)+, A0\r
+  EaCalc (10,0x0e00, dea,2,1);\r
+  EaRead (10,    11, dea,2,0x0e00);\r
 \r
-  EaCalc ( 0,0x0e00, dea,2,1);\r
-  EaRead ( 0,     1, dea,2,0x0e00);\r
+  EaCalc ( 0,0x003f, sea,size);\r
+  EaRead ( 0,     0, sea,size,0x003f);\r
 \r
-  if (type==0) ot("  sub r1,r1,r10\n");\r
-  if (type==1) ot("  cmp r1,r10 ;@ Defines NZCV\n");\r
+  if (type==0) ot("  sub r11,r11,r0\n");\r
+  if (type==1) ot("  cmp r11,r0 ;@ Defines NZCV\n");\r
   if (type==1) OpGetFlags(1,0); // Get Cmp flags\r
-  if (type==2) ot("  add r1,r1,r10\n");\r
+  if (type==2) ot("  add r11,r11,r0\n");\r
   ot("\n");\r
   \r
-  if (type!=1) EaWrite( 0,     1, dea,2,0x0e00,1);\r
+  if (type!=1) EaWrite(10,    11, dea,2,0x0e00,1);\r
 \r
-  OpEnd();\r
+  OpEnd(sea);\r
 \r
   return 0;\r
 }\r
@@ -566,7 +572,7 @@ int OpAddx(int op)
   if (size==0&&(sea==0x27||dea==0x27)) use=op; // ___x.b -(a7)\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op,sea|dea); Cycles=4;\r
+  OpStart(op,sea,dea); Cycles=4;\r
   if(size>=2)   Cycles+=4;\r
   if(sea>=0x10) Cycles+=2;\r
 \r
@@ -602,7 +608,7 @@ int OpAddx(int op)
   ot(";@ Save result:\n");\r
   EaWrite( 0, 1, dea,size,0x0e00,1);\r
 \r
-  OpEnd();\r
+  OpEnd(sea,dea);\r
 \r
   return 0;\r
 }\r
@@ -660,7 +666,7 @@ int OpCmpEor(int op)
 \r
   if (eor) EaWrite(10, 1,ea,size,0x003f,1);\r
 \r
-  OpEnd();\r
+  OpEnd(ea);\r
   return 0;\r
 }\r
 \r
@@ -691,7 +697,7 @@ int OpCmpm(int op)
   ot("  cmp r0,r10\n");\r
   OpGetFlags(1,0); // Cmp like subtract\r
 \r
-  OpEnd();\r
+  OpEnd(sea);\r
   return 0;\r
 }\r
 \r
@@ -745,13 +751,13 @@ int OpChk(int op)
   ot(";@ old N remains\n");\r
   ot("  bic r9,r9,#0x80000000 ;@ N\n");\r
   ot("  orr r9,r9,r3\n");\r
-  OpEnd();\r
+  OpEnd(ea);\r
 \r
   ot("chktrap%.4x%s ;@ CHK exception:\n",op,ms?"":":");\r
   ot("  mov r0,#0x18\n");\r
   ot("  bl Exception\n");\r
   Cycles+=40;\r
-  OpEnd();\r
+  OpEnd(ea);\r
 \r
   return 0;\r
 }\r