NOT setting upper bits on PUSH PC, minor adjustments
[picodrive.git] / cpu / Cyclone / OpArith.cpp
index 8280a2b..93d34a2 100644 (file)
@@ -25,20 +25,21 @@ int OpArith(int op)
 \r
   OpStart(op, sea, tea); Cycles=4;\r
 \r
-  EaCalcReadNoSE((type!=6)?11:-1,0,tea,size,0x003f);\r
+  // imm must be read first\r
   EaCalcReadNoSE(-1,10,sea,size,0);\r
+  EaCalcReadNoSE((type!=6)?11:-1,0,tea,size,0x003f);\r
 \r
   if (size<2) shiftstr=(char *)(size?",asl #16":",asl #24");\r
-  if (size<2) ot("  mov r0,r0,asl %i\n",size?16:24);\r
+  if (size<2) ot("  mov r10,r10,asl #%i\n",size?16:24);\r
 \r
   ot(";@ Do arithmetic:\n");\r
 \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
+  if (type==0) ot("  orr r1,r10,r0%s\n",shiftstr);\r
+  if (type==1) ot("  and r1,r10,r0%s\n",shiftstr);\r
+  if (type==2||type==6)\r
+               ot("  rsbs r1,r10,r0%s ;@ Defines NZCV\n",shiftstr);\r
+  if (type==3) ot("  adds r1,r10,r0%s ;@ Defines NZCV\n",shiftstr);\r
+  if (type==5) ot("  eor r1,r10,r0%s\n",shiftstr);\r
 \r
   if (type<2 || type==5) ot("  adds r1,r1,#0 ;@ Defines NZ, clears CV\n"); // 0,1,5\r
 \r
@@ -236,7 +237,7 @@ int OpMul(int op)
   EaCalc(10,0x0e00,rea, 2);\r
   EaRead(10,     2,rea, 2,0x0e00);\r
 \r
-  ot("  movs r0,r0,asl #16\n");\r
+  ot("  movs r1,r0,asl #16\n");\r
 \r
   if (type==0) // div\r
   {\r
@@ -248,20 +249,25 @@ int OpMul(int op)
     if (sign)\r
     {\r
       ot("  mov r11,#0 ;@ r11 = 1 or 2 if the result is negative\n");\r
-      ot("  orrmi r11,r11,#1\n");\r
-      ot("  mov r0,r0,asr #16\n");\r
-      ot("  rsbmi r0,r0,#0 ;@ Make r0 positive\n");\r
-      ot("\n");\r
       ot("  tst r2,r2\n");\r
       ot("  orrmi r11,r11,#2\n");\r
       ot("  rsbmi r2,r2,#0 ;@ Make r2 positive\n");\r
       ot("\n");\r
+      ot("  movs r0,r1,asr #16\n");\r
+      ot("  orrmi r11,r11,#1\n");\r
+      ot("  rsbmi r0,r0,#0 ;@ Make r0 positive\n");\r
+      ot("\n");\r
+      ot(";@ detect the nasty 0x80000000 / -1 situation\n");\r
+      ot("  mov r3,r2,asr #31\n");\r
+      ot("  eors r3,r3,r1,asr #16\n");\r
+      ot("  beq wrendofop%.4x\n",op);\r
     }\r
     else\r
     {\r
-      ot("  mov r0,r0,lsr #16 ;@ use only 16 bits of divisor\n");\r
+      ot("  mov r0,r1,lsr #16 ;@ use only 16 bits of divisor\n");\r
     }\r
 \r
+    ot("\n");\r
     ot(";@ Divide r2 by r0\n");\r
     ot("  mov r3,#0\n");\r
     ot("  mov r1,r0\n");\r
@@ -298,6 +304,8 @@ int OpMul(int op)
       ot("  cmp r3,r1,asr #16 ;@ signed overflow?\n");\r
       ot("  orrne r9,r9,#0x10000000 ;@ set overflow flag\n");\r
       ot("  bne endofop%.4x ;@ overflow!\n",op);\r
+      ot("\n");\r
+      ot("wrendofop%.4x%s\n",op,ms?"":":");\r
     }\r
     else\r
     {\r
@@ -305,6 +313,7 @@ int OpMul(int op)
       ot("  movs r1,r3,lsr #16 ;@ check for overflow condition\n");\r
       ot("  orrne r9,r9,#0x10000000 ;@ set overflow flag\n");\r
       ot("  bne endofop%.4x ;@ overflow!\n",op);\r
+      ot("\n");\r
     }\r
 \r
     ot("  mov r1,r3,lsl #16 ;@ Clip to 16-bits\n");\r
@@ -318,7 +327,7 @@ int OpMul(int op)
   if (type==1)\r
   {\r
     ot(";@ Get 16-bit signs right:\n");\r
-    ot("  mov r0,r0,%s #16\n",sign?"asr":"lsr");\r
+    ot("  mov r0,r1,%s #16\n",sign?"asr":"lsr");\r
     ot("  mov r2,r2,lsl #16\n");\r
     ot("  mov r2,r2,%s #16\n",sign?"asr":"lsr");\r
     ot("\n");\r
@@ -384,7 +393,7 @@ int OpAbcd(int op)
     ot(";@ Get src/dest EA vals\n");\r
     EaCalc (0,0x000f, sea,0,1);\r
     EaRead (0,    10, sea,0,0x000f,1);\r
-    EaCalcReadNoSE(11,0,dea,0,0x1e00);\r
+    EaCalcReadNoSE(11,0,dea,0,0x0e00);\r
   }\r
   else\r
   {\r
@@ -485,29 +494,31 @@ int OpNbcd(int op)
   ot("  mov r0,r0,asl #24\n");\r
   ot("  and r2,r2,#0x20000000\n");\r
   ot("  add r2,r0,r2,lsr #5 ;@ add X\n");\r
-  ot("  rsbs r1,r2,#0x9a000000 ;@ do arithmetic\n");\r
+  ot("  rsb r11,r2,#0x9a000000 ;@ do arithmetic\n");\r
 \r
-  ot("  orrmi r9,r9,#0x80000000 ;@ N\n");\r
-  ot("  cmp r1,#0x9a000000\n");\r
+  ot("  cmp r11,#0x9a000000\n");\r
   ot("  beq finish%.4x\n",op);\r
   ot("\n");\r
 \r
-  ot("  mvn r3,r9,lsr #3 ;@ Undefined V behavior\n",op);\r
-  ot("  and r2,r1,#0x0f000000\n");\r
+  ot("  mvn r3,r11,lsr #31 ;@ Undefined V behavior\n",op);\r
+  ot("  and r2,r11,#0x0f000000\n");\r
   ot("  cmp r2,#0x0a000000\n");\r
-  ot("  andeq r1,r1,#0xf0000000\n");\r
-  ot("  addeq r1,r1,#0x10000000\n");\r
-  ot("  and r3,r3,r1,lsr #3 ;@ Undefined V behavior part II\n",op);\r
-  ot("  tst r1,r1\n");\r
-  ot("  orr r9,r9,r3 ;@ save V\n",op);\r
+  ot("  andeq r11,r11,#0xf0000000\n");\r
+  ot("  addeq r11,r11,#0x10000000\n");\r
+  ot("  and r3,r3,r11,lsr #31 ;@ Undefined V behavior part II\n",op);\r
+  ot("  movs r1,r11,asr #24\n");\r
   ot("  bicne r9,r9,#0x40000000 ;@ Z\n");\r
+  ot("  orr r9,r9,r3,lsl #28 ;@ save V\n",op);\r
   ot("  orr r9,r9,#0x20000000 ;@ C\n");\r
   ot("\n");\r
 \r
-  EaWrite(10,     1, ea,0,0x3f,1);\r
+  EaWrite(10,     1, ea,0,0x3f,0,0);\r
 \r
   ot("finish%.4x%s\n",op,ms?"":":");\r
+  ot("  tst r11,r11\n");\r
+  ot("  orrmi r9,r9,#0x80000000 ;@ N\n");\r
   ot("  str r9,[r7,#0x4c] ;@ Save X\n");\r
+  ot("\n");\r
 \r
   OpEnd(ea);\r
 \r
@@ -540,13 +551,20 @@ int OpAritha(int op)
   if(size==2&&(sea<0x10||sea==0x3c)) Cycles+=2;\r
   if(type==1) Cycles=6;\r
 \r
-  // must calculate reg EA first, because of situations like: suba.w (A0)+, A0\r
-  EaCalc (10,0x1e00, dea,2,1);\r
-  EaRead (10,    11, dea,2,0x1e00);\r
-\r
-  EaCalc ( 0,0x003f, sea,size,1);\r
-  EaRead ( 0,     0, sea,size,0x003f,1);\r
+  // EA calculation order defines how situations like  suba.w (A0)+, A0 get handled.\r
+  // different emus act differently in this situation, I couldn't fugure which is right behaviour.\r
+  if (/*type == */1)\r
+  {\r
+    EaCalcReadNoSE(-1,0,sea,size,0x003f);\r
+    EaCalcReadNoSE(type!=1?10:-1,11,dea,2,0x0e00);\r
+  }\r
+  else\r
+  {\r
+    EaCalcReadNoSE(type!=1?10:-1,11,dea,2,0x0e00);\r
+    EaCalcReadNoSE(-1,0,sea,size,0x003f);\r
+  }\r
 \r
+  if (size<2) ot("  mov r0,r0,asl #%d\n\n",size?16:24);\r
   if (size<2) asr=(char *)(size?",asr #16":",asr #24");\r
 \r
   if (type==0) ot("  sub r11,r11,r0%s\n",asr);\r
@@ -555,7 +573,7 @@ int OpAritha(int op)
   if (type==2) ot("  add r11,r11,r0%s\n",asr);\r
   ot("\n");\r
   \r
-  if (type!=1) EaWrite(10,    11, dea,2,0x0e00,1);\r
+  if (type!=1) EaWrite(10,    11, dea,2,0x0e00);\r
 \r
   OpEnd(sea);\r
 \r
@@ -596,7 +614,7 @@ int OpAddx(int op)
     ot(";@ Get src/dest EA vals\n");\r
     EaCalc (0,0x000f, sea,size,1);\r
     EaRead (0,    11, sea,size,0x000f,1);\r
-    EaCalcReadNoSE(10,0,dea,size,0x1e00);\r
+    EaCalcReadNoSE(10,0,dea,size,0x0e00);\r
   }\r
   else\r
   {\r
@@ -716,11 +734,11 @@ int OpCmpm(int op)
   OpStart(op,sea); Cycles=4;\r
 \r
   ot(";@ Get src operand into r10:\n");\r
-  EaCalc (0,0x1e00, sea,size,1);\r
-  EaRead (0,    10, sea,size,0x000f,1);\r
+  EaCalc (0,0x0007, sea,size,1);\r
+  EaRead (0,    10, sea,size,0x0007,1);\r
 \r
   ot(";@ Get dst operand into r0:\n");\r
-  EaCalcReadNoSE(-1,0,dea,size,0x1e00);\r
+  EaCalcReadNoSE(-1,0,dea,size,0x0e00);\r
 \r
   if (size<2) asl=(char *)(size?",asl #16":",asl #24");\r
 \r