code review and optimizations
[picodrive.git] / cpu / Cyclone / OpBranch.cpp
index f8eb584..c24c769 100644 (file)
@@ -139,8 +139,8 @@ int OpUnlk(int op)
   OpStart(op,0x10);\r
 \r
   ot(";@ Get An\n");\r
-  EaCalc(10, 7, 8, 2, 1);\r
-  EaRead(10, 0, 8, 2, 7, 1);\r
+  EaCalc(10, 0xf, 8, 2,   1);\r
+  EaRead(10,   0, 8, 2, 0xf, 1);\r
 \r
   ot("  add r11,r0,#4 ;@ A7+=4\n");\r
   ot("\n");\r
@@ -151,7 +151,7 @@ int OpUnlk(int op)
   ot("\n");\r
   ot(";@ An = value from stack:\n");\r
   EaWrite(10, 0, 8, 2, 7, 1);\r
-  \r
+\r
   Cycles=12;\r
   OpEnd(0x10);\r
   return 0;\r
@@ -181,7 +181,6 @@ int Op4E70(int op)
     SuperChange(op);\r
     CheckInterrupt(op);\r
     OpEnd(0x10);\r
-    SuperEnd(op);\r
     return 0;\r
 \r
     case 5: // rts\r
@@ -225,7 +224,7 @@ int OpJsr(int op)
   // See if we can do this opcode:\r
   if (EaCanRead(sea,-1)==0) 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
   OpStart(op,(op&0x40)?0:0x10);\r
@@ -294,17 +293,17 @@ int OpDbra(int op)
       break;\r
     case 2: // hi\r
       ot("  tst r9,#0x60000000 ;@ hi: !C && !Z\n");\r
-      ot("  beq DbraTrue%.4x\n\n",op);\r
+      ot("  beq DbraTrue\n\n");\r
       break;\r
     case 3: // ls\r
       ot("  tst r9,#0x60000000 ;@ ls: C || Z\n");\r
-      ot("  bne DbraTrue%.4x\n\n",op);\r
+      ot("  bne DbraTrue\n\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(";@ If so, don't dbra\n");\r
-      ot("  b%s DbraTrue%.4x\n\n",Cond[cc],op);\r
+      ot("  b%s DbraTrue\n\n",Cond[cc]);\r
       break;\r
   }\r
 \r
@@ -332,10 +331,11 @@ int OpDbra(int op)
     OpEnd();\r
   }\r
   \r
-  if (cc==0||cc>=2)\r
+  //if (cc==0||cc>=2)\r
+  if (op==0x50c8)\r
   {\r
     ot(";@ condition true:\n");\r
-    ot("DbraTrue%.4x%s\n", op, ms?"":":");\r
+    ot("DbraTrue%s\n", ms?"":":");\r
     ot("  add r4,r4,#2 ;@ Skip branch offset\n");\r
     ot("\n");\r
     Cycles=12;\r
@@ -352,6 +352,7 @@ int OpBranch(int op)
   int size=0,use=0;\r
   int offset=0;\r
   int cc=0;\r
+  char *asr_r11="";\r
 \r
   offset=(char)(op&0xff);\r
   cc=(op>>8)&15;\r
@@ -365,33 +366,48 @@ int OpBranch(int op)
 \r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
   OpStart(op,size?0x10:0);\r
-\r
-  ot(";@ Get Branch offset:\n");\r
-  if (size) \r
-  {\r
-    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 (cc==1) ot("  ldr r10,[r7,#0x60] ;@ Get Memory base\n");\r
 \r
-  if (cc>=2)\r
+  switch (cc)\r
   {\r
-    ot(";@ Is the condition true?\n");\r
-    if ((cc&~1)==2) ot("  eor r9,r9,#0x20000000 ;@ Invert carry for hi/ls\n");\r
-    ot("  msr cpsr_flg,r9 ;@ ARM flags = 68000 flags\n");\r
-    if ((cc&~1)==2) ot("  eor r9,r9,#0x20000000\n");\r
-\r
-    ot("  b%s DontBranch%.4x\n",Cond[cc^1],op);\r
-    ot("\n");\r
+    case 0: // T\r
+    case 1: // F\r
+      break;\r
+    case 2: // hi\r
+      ot("  tst r9,#0x60000000 ;@ hi: !C && !Z\n");\r
+      ot("  bne BccDontBranch%i\n\n",8<<size);\r
+      break;\r
+    case 3: // ls\r
+      ot("  tst r9,#0x60000000 ;@ ls: C || Z\n");\r
+      ot("  beq BccDontBranch%i\n\n",8<<size);\r
+      break;\r
+    default:\r
+      ot(";@ Is the condition true?\n");\r
+      ot("  msr cpsr_flg,r9 ;@ ARM flags = 68000 flags\n");\r
+      ot("  b%s BccDontBranch%i\n\n",Cond[cc^1],8<<size);\r
+      break;\r
   }\r
 \r
-  if (size==0) ot("  mov r0,r0,asr #24 ;@ ...shift down\n\n");\r
+  if (size) \r
+  {\r
+    if (size<2)\r
+    {\r
+      ot("  ldrsh r11,[r4] ;@ Fetch Branch offset\n");\r
+    }\r
+    else\r
+    {\r
+      ot("  ldrh r2,[r4] ;@ Fetch Branch offset\n");\r
+      ot("  ldrh r11,[r4,#2]\n");\r
+      ot("  orr r11,r11,r2,lsl #16\n");\r
+    }\r
+  }\r
+  else\r
+  {\r
+    ot("  mov r11,r8,asl #24 ;@ Shift 8-bit signed offset up...\n\n");\r
+    asr_r11=",asr #24";\r
+  }\r
 \r
   ot(";@ Branch taken - Add on r0 to PC\n");\r
 \r
@@ -399,12 +415,11 @@ int OpBranch(int op)
   {\r
     ot(";@ Bsr - remember old PC\n");\r
     ot("  sub r1,r4,r10 ;@ r1 = Old PC\n");\r
+    if (size) ot("  add r1,r1,#%d\n",1<<size);\r
     ot("  mov r1,r1, lsl #8\n");\r
+    ot("  ldr r2,[r7,#0x3c]\n");\r
     ot("  mov r1,r1, asr #8\n");\r
     ot("\n");\r
-    if (size) ot("  sub r4,r4,#%d ;@ (Branch is relative to Opcode+2)\n",1<<size);\r
-    ot("  ldr r2,[r7,#0x3c]\n");\r
-    ot("  add r4,r4,r0 ;@ r4 = New PC\n");\r
     ot(";@ Push r1 onto stack\n");\r
     ot("  sub r0,r2,#4 ;@ Predecrement A7\n");\r
     ot("  str r0,[r7,#0x3c] ;@ Save A7\n");\r
@@ -414,33 +429,36 @@ int OpBranch(int op)
     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
+      ot("  add r0,r4,r11%s ;@ r4 = New PC\n",asr_r11);\r
+      CheckPc(0);\r
     }\r
+    else\r
+      ot("  add r4,r4,r11%s ;@ r4 = New PC\n",asr_r11);\r
   }\r
   else\r
   {\r
-    if (size) ot("  sub r4,r4,#%d ;@ (Branch is relative to Opcode+2)\n",1<<size);\r
     if (offset==0 || offset==-1)\r
     {\r
-      ot("  add r0,r4,r0 ;@ r4 = New PC\n");\r
+      ot("  add r0,r4,r11%s ;@ r4 = New PC\n",asr_r11);\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("  add r4,r4,r11%s ;@ r4 = New PC\n",asr_r11);\r
       ot("\n");\r
     }\r
   }\r
 \r
-\r
   OpEnd(size?0x10:0);\r
 \r
-  if (cc>=2)\r
+  // since all "DontBranch" code is same for every size, output only once\r
+  if (cc>=2&&(op&0xff00)==0x6200)\r
   {\r
-    ot("DontBranch%.4x%s\n", op, ms?"":":");\r
-    Cycles+=(size==1)?  2 : -2; // Branch not taken\r
-    OpEnd(size?0x10:0);\r
+    ot("BccDontBranch%i%s\n", 8<<size, ms?"":":");\r
+    if (size) ot("  add r4,r4,#%d\n",8<<size);\r
+    Cycles+=(size==1) ? 2 : -2; // Branch not taken\r
+    OpEnd(0);\r
   }\r
 \r
   return 0;\r