more MAME fixes, some optimization
[picodrive.git] / cpu / Cyclone / OpBranch.cpp
index 71f9785..31e82fc 100644 (file)
@@ -1,17 +1,21 @@
 \r
 #include "app.h"\r
 \r
-#if USE_CHECKPC_CALLBACK\r
-static void CheckPc()\r
+static void CheckPc(int reg)\r
 {\r
+#if USE_CHECKPC_CALLBACK\r
   ot(";@ Check Memory Base+pc (r4)\n");\r
-  ot("  add lr,pc,#4\n");\r
-  ot("  mov r0,r4\n");\r
+  if (reg != 0)\r
+    ot("  mov r0,r%i\n", reg);\r
+  ot("  mov lr,pc\n");\r
   ot("  ldr pc,[r7,#0x64] ;@ Call checkpc()\n");\r
   ot("  mov r4,r0\n");\r
+#else\r
+  if (reg != 4)\r
+    ot("  mov r4,r%i\n", reg);\r
+#endif\r
   ot("\n");\r
 }\r
-#endif\r
 \r
 // Push 32-bit value in r1 - trashes r0-r3,r12,lr\r
 void OpPush32()\r
@@ -56,9 +60,9 @@ static void PopPc()
   ot("  add r1,r0,#4 ;@ Postincrement A7\n");\r
   ot("  str r1,[r7,#0x3c] ;@ Save A7\n");\r
   MemHandler(0,2);\r
-  ot("  add r4,r0,r10 ;@ r4=Memory Base+PC\n");\r
+  ot("  add r0,r0,r10 ;@ Memory Base+PC\n");\r
   ot("\n");\r
-  CheckPc();\r
+  CheckPc(0);\r
 }\r
 \r
 int OpTrap(int op)\r
@@ -68,14 +72,14 @@ int OpTrap(int op)
   use=op&~0xf;\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op);\r
+  OpStart(op,0x10);\r
   ot("  and r0,r8,#0xf ;@ Get trap number\n");\r
   ot("  orr r0,r0,#0x20\n");\r
   ot("  mov r0,r0,asl #2\n");\r
   ot("  bl Exception\n");\r
   ot("\n");\r
 \r
-  Cycles=38; OpEnd();\r
+  Cycles=38; OpEnd(0x10);\r
 \r
   return 0;\r
 }\r
@@ -90,7 +94,7 @@ int OpLink(int op)
   if (reg==7) use=op;\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op);\r
+  OpStart(op,0x10);\r
 \r
   if(reg!=7) {\r
     ot(";@ Get An\n");\r
@@ -120,7 +124,7 @@ int OpLink(int op)
   ot("\n");\r
 \r
   Cycles=16;\r
-  OpEnd();\r
+  OpEnd(0x10);\r
   return 0;\r
 }\r
 \r
@@ -132,7 +136,7 @@ int OpUnlk(int op)
   use=op&~7;\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op);\r
+  OpStart(op,0x10);\r
 \r
   ot(";@ Get An\n");\r
   EaCalc(10, 7, 8, 2, 1);\r
@@ -149,7 +153,7 @@ int OpUnlk(int op)
   EaWrite(10, 0, 8, 2, 7, 1);\r
   \r
   Cycles=12;\r
-  OpEnd();\r
+  OpEnd(0x10);\r
   return 0;\r
 }\r
 \r
@@ -168,40 +172,40 @@ int Op4E70(int op)
     OpEnd();\r
     return 0;\r
 \r
-       case 3: // rte\r
-    OpStart(op); Cycles=20;\r
-       SuperCheck(op);\r
+    case 3: // rte\r
+    OpStart(op,0x10); Cycles=20;\r
+    SuperCheck(op);\r
     PopSr(1);\r
     ot("  ldr r10,[r7,#0x60] ;@ Get Memory base\n");\r
     PopPc();\r
-       SuperChange(op);\r
+    SuperChange(op);\r
     CheckInterrupt(op);\r
-    OpEnd();\r
-       SuperEnd(op);\r
+    OpEnd(0x10);\r
+    SuperEnd(op);\r
     return 0;\r
 \r
     case 5: // rts\r
-    OpStart(op); Cycles=16;\r
+    OpStart(op,0x10); Cycles=16;\r
     ot("  ldr r10,[r7,#0x60] ;@ Get Memory base\n");\r
     PopPc();\r
-    OpEnd();\r
+    OpEnd(0x10);\r
     return 0;\r
 \r
     case 6: // trapv\r
-    OpStart(op); Cycles=4;\r
+    OpStart(op,0x10); Cycles=4;\r
     ot("  tst r9,#0x10000000\n");\r
     ot("  subne r5,r5,#%i\n",30);\r
     ot("  movne r0,#0x1c ;@ TRAPV exception\n");\r
     ot("  blne Exception\n");\r
-    OpEnd();\r
+    OpEnd(0x10);\r
     return 0;\r
 \r
     case 7: // rtr\r
-    OpStart(op); Cycles=20;\r
+    OpStart(op,0x10); Cycles=20;\r
     PopSr(0);\r
     ot("  ldr r10,[r7,#0x60] ;@ Get Memory base\n");\r
     PopPc();\r
-    OpEnd();\r
+    OpEnd(0x10);\r
     return 0;\r
 \r
     default:\r
@@ -224,24 +228,23 @@ int OpJsr(int op)
   use=OpBase(op);\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op);\r
+  OpStart(op,(op&0x40)?0:0x10);\r
 \r
   ot("  ldr r10,[r7,#0x60] ;@ Get Memory base\n");\r
   ot("\n");\r
-  EaCalc(0,0x003f,sea,0);\r
+  EaCalc(11,0x003f,sea,0);\r
 \r
   ot(";@ Jump - Get new PC from r0\n");\r
   if (op&0x40)\r
   {\r
     // Jmp - Get new PC from r0\r
-    ot("  add r4,r0,r10 ;@ r4 = Memory Base + New PC\n");\r
+    ot("  add r0,r11,r10 ;@ Memory Base + New PC\n");\r
     ot("\n");\r
   }\r
   else\r
   {\r
     ot(";@ Jsr - Push old PC first\n");\r
     ot("  sub r1,r4,r10 ;@ r1 = Old PC\n");\r
-    ot("  add r4,r0,r10 ;@ r4 = Memory Base + New PC\n");\r
     ot("  mov r1,r1,lsl #8\n");\r
     ot("  ldr r0,[r7,#0x3c]\n");\r
     ot("  mov r1,r1,asr #8\n");\r
@@ -249,17 +252,16 @@ int OpJsr(int op)
     ot("  sub r0,r0,#4 ;@ Predecrement A7\n");\r
     ot("  str r0,[r7,#0x3c] ;@ Save A7\n");\r
     MemHandler(1,2);\r
+    ot("  add r0,r11,r10 ;@ Memory Base + New PC\n");\r
     ot("\n");\r
   }\r
 \r
-#if USE_CHECKPC_CALLBACK\r
-  CheckPc();\r
-#endif\r
+  CheckPc(0);\r
 \r
   Cycles=(op&0x40) ? 4 : 12;\r
   Cycles+=Ea_add_ns((op&0x40) ? g_jmp_cycle_table : g_jsr_cycle_table, sea);\r
 \r
-  OpEnd();\r
+  OpEnd((op&0x40)?0:0x10);\r
 \r
   return 0;\r
 }\r
@@ -352,7 +354,7 @@ int OpBranch(int op)
   else use=(op&0xff00)+1; // Use same opcode for all 8-bit branches\r
 \r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
-  OpStart(op);\r
+  OpStart(op,size?0x10:0);\r
 \r
   ot(";@ Get Branch offset:\n");\r
   if (size) \r
@@ -360,12 +362,12 @@ int OpBranch(int op)
     EaCalc(0,0,0x3c,size);\r
     EaRead(0,0,0x3c,size,0);\r
   }\r
+  else\r
+    ot("  mov r0,r8,asl #24 ;@ Shift 8-bit signed offset up...\n\n");\r
 \r
   // above code messes cycles\r
   Cycles=10; // Assume branch taken\r
 \r
-  if (size==0) ot("  mov r0,r8,asl #24 ;@ Shift 8-bit signed offset up...\n\n");\r
-\r
   if (cc==1) ot("  ldr r10,[r7,#0x60] ;@ Get Memory base\n");\r
 \r
   if (cc>=2)\r
@@ -375,16 +377,11 @@ int OpBranch(int op)
     ot("  msr cpsr_flg,r9 ;@ ARM flags = 68000 flags\n");\r
     if ((cc&~1)==2) ot("  eor r9,r9,#0x20000000\n");\r
 \r
-    if (size==0) ot("  mov r0,r0,asr #24 ;@ ...shift down\n\n");\r
-\r
     ot("  b%s DontBranch%.4x\n",Cond[cc^1],op);\r
-\r
     ot("\n");\r
   }\r
-  else\r
-  {\r
-    if (size==0) ot("  mov r0,r0,asr #24 ;@ ...shift down\n\n");\r
-  }\r
+\r
+  if (size==0) ot("  mov r0,r0,asr #24 ;@ ...shift down\n\n");\r
 \r
   ot(";@ Branch taken - Add on r0 to PC\n");\r
 \r
@@ -404,29 +401,36 @@ int OpBranch(int op)
     MemHandler(1,2);\r
     ot("\n");\r
     Cycles=18; // always 18\r
+    if (offset==0 || offset==-1)\r
+    {\r
+      ot(";@ Branch is quite far, so may be a good idea to check Memory Base+pc\n");\r
+      CheckPc(4);\r
+    }\r
   }\r
   else\r
   {\r
     if (size) ot("  sub r4,r4,#%d ;@ (Branch is relative to Opcode+2)\n",1<<size);\r
-    ot("  add r4,r4,r0 ;@ r4 = New PC\n");\r
-    ot("\n");\r
+    if (offset==0 || offset==-1)\r
+    {\r
+      ot("  add r0,r4,r0 ;@ r4 = New PC\n");\r
+      ot(";@ Branch is quite far, so may be a good idea to check Memory Base+pc\n");\r
+      CheckPc(0);\r
+    }\r
+    else\r
+    {\r
+      ot("  add r4,r4,r0 ;@ r4 = New PC\n");\r
+      ot("\n");\r
+    }\r
   }\r
 \r
-#if USE_CHECKPC_CALLBACK\r
-  if (offset==0 || offset==-1)\r
-  {\r
-    ot(";@ Branch is quite far, so may be a good idea to check Memory Base+pc\n");\r
-    CheckPc();\r
-  }\r
-#endif\r
 \r
-  OpEnd();\r
+  OpEnd(size?0x10:0);\r
 \r
   if (cc>=2)\r
   {\r
     ot("DontBranch%.4x%s\n", op, ms?"":":");\r
     Cycles+=(size==1)?  2 : -2; // Branch not taken\r
-    OpEnd();\r
+    OpEnd(size?0x10:0);\r
   }\r
 \r
   return 0;\r