0.0088 release
[picodrive.git] / cpu / Cyclone / OpBranch.cpp
index 320b898..d29ed03 100644 (file)
@@ -1,19 +1,15 @@
 \r
 #include "app.h"\r
 \r
-static void CheckPc(int reg)\r
+// in/out address in r0, trashes all temp regs\r
+static void CheckPc(void)\r
 {\r
 #if USE_CHECKPC_CALLBACK\r
-  ot(";@ Check Memory Base+pc (r%i)\n",reg);\r
-  if (reg != 0)\r
-    ot("  mov r0,r%i\n", reg);\r
+  ot(";@ Check Memory Base+pc\n");\r
   ot("  mov lr,pc\n");\r
   ot("  ldr pc,[r7,#0x64] ;@ Call checkpc()\n");\r
-  ot("  mov r4,r0\n");\r
-#else\r
-  ot("  bic r4,r%d,#1\n",reg); // we do not emulate address errors\r
-#endif\r
   ot("\n");\r
+#endif\r
 }\r
 \r
 // Push 32-bit value in r1 - trashes r0-r3,r12,lr\r
@@ -61,7 +57,12 @@ static void PopPc()
   MemHandler(0,2);\r
   ot("  add r0,r0,r10 ;@ Memory Base+PC\n");\r
   ot("\n");\r
-  CheckPc(0);\r
+  CheckPc();\r
+#if EMULATE_ADDRESS_ERRORS_JUMP\r
+  ot("  mov r4,r0\n");\r
+#else\r
+  ot("  bic r4,r0,#1\n");\r
+#endif\r
 }\r
 \r
 int OpTrap(int op)\r
@@ -73,8 +74,7 @@ int OpTrap(int 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("  orr r0,r0,#0x20 ;@ 32+n\n");\r
   ot("  bl Exception\n");\r
   ot("\n");\r
 \r
@@ -177,14 +177,30 @@ int Op4E70(int op)
     PopSr(1);\r
     ot("  ldr r10,[r7,#0x60] ;@ Get Memory base\n");\r
     PopPc();\r
-    SuperChange(op);\r
-    OpEnd(0x10,0,0,1);\r
+    ot("  ldr r1,[r7,#0x44] ;@ reload SR high\n");\r
+    SuperChange(op,1);\r
+#if EMULATE_ADDRESS_ERRORS_JUMP || EMULATE_ADDRESS_ERRORS_IO || EMULATE_HALT\r
+    ot("  ldr r1,[r7,#0x58]\n");\r
+    ot("  bic r1,r1,#0x0c ;@ clear 'not processing instruction' and 'doing addr error' bits\n");\r
+    ot("  str r1,[r7,#0x58]\n");\r
+#endif\r
+#if EMULATE_ADDRESS_ERRORS_JUMP\r
+    ot("  tst r4,#1 ;@ address error?\n");\r
+    ot("  bne ExceptionAddressError_r_prg_r4\n");\r
+#endif\r
+    opend_check_interrupt = 1;\r
+    opend_check_trace = 1;\r
+    OpEnd(0x10,0);\r
     return 0;\r
 \r
     case 5: // rts\r
     OpStart(op,0x10); Cycles=16;\r
     ot("  ldr r10,[r7,#0x60] ;@ Get Memory base\n");\r
     PopPc();\r
+#if EMULATE_ADDRESS_ERRORS_JUMP\r
+    ot("  tst r4,#1 ;@ address error?\n");\r
+    ot("  bne ExceptionAddressError_r_prg_r4\n");\r
+#endif\r
     OpEnd(0x10);\r
     return 0;\r
 \r
@@ -192,9 +208,10 @@ int Op4E70(int op)
     OpStart(op,0x10,0,1); Cycles=4;\r
     ot("  tst r9,#0x10000000\n");\r
     ot("  subne r5,r5,#%i\n",34);\r
-    ot("  movne r0,#0x1c ;@ TRAPV exception\n");\r
+    ot("  movne r0,#7 ;@ TRAPV exception\n");\r
     ot("  blne Exception\n");\r
-    OpEnd(0x10,0,1);\r
+    opend_op_changes_cycles = 1;\r
+    OpEnd(0x10,0);\r
     return 0;\r
 \r
     case 7: // rtr\r
@@ -202,6 +219,10 @@ int Op4E70(int op)
     PopSr(0);\r
     ot("  ldr r10,[r7,#0x60] ;@ Get Memory base\n");\r
     PopPc();\r
+#if EMULATE_ADDRESS_ERRORS_JUMP\r
+    ot("  tst r4,#1 ;@ address error?\n");\r
+    ot("  bne ExceptionAddressError_r_prg_r4\n");\r
+#endif\r
     OpEnd(0x10);\r
     return 0;\r
 \r
@@ -231,21 +252,32 @@ int OpJsr(int op)
   ot("\n");\r
   EaCalc(11,0x003f,sea,0);\r
 \r
+  ot(";@ Jump - Get new PC from r11\n");\r
+  ot("  add r0,r11,r10 ;@ Memory Base + New PC\n");\r
+  ot("\n");\r
+  CheckPc();\r
   if (!(op&0x40))\r
   {\r
-    ot(";@ Jsr - Push old PC first\n");\r
-    ot("  ldr r0,[r7,#0x3c]\n");\r
+    ot("  ldr r2,[r7,#0x3c]\n");\r
     ot("  sub r1,r4,r10 ;@ r1 = Old PC\n");\r
-    ot(";@ Push r1 onto stack\n");\r
-    ot("  sub r0,r0,#4 ;@ Predecrement A7\n");\r
+  }\r
+#if EMULATE_ADDRESS_ERRORS_JUMP\r
+  // jsr prefetches next instruction before pushing old PC,\r
+  // according to http://pasti.fxatari.com/68kdocs/68kPrefetch.html\r
+  ot("  mov r4,r0\n");\r
+  ot("  tst r4,#1 ;@ address error?\n");\r
+  ot("  bne ExceptionAddressError_r_prg_r4\n");\r
+#else\r
+  ot("  bic r4,r0,#1\n");\r
+#endif\r
+\r
+  if (!(op&0x40))\r
+  {\r
+    ot(";@ Push old PC onto stack\n");\r
+    ot("  sub r0,r2,#4 ;@ Predecrement A7\n");\r
     ot("  str r0,[r7,#0x3c] ;@ Save A7\n");\r
     MemHandler(1,2);\r
   }\r
-  ot(";@ Jump - Get new PC from r11\n");\r
-  ot("  add r0,r11,r10 ;@ Memory Base + New PC\n");\r
-  ot("\n");\r
-\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
@@ -310,14 +342,21 @@ int OpDbra(int op)
     ot(";@ Check if Dn.w is -1\n");\r
     ot("  cmn r0,#1\n");\r
 \r
-#if USE_CHECKPC_CALLBACK && USE_CHECKPC_DBRA\r
+#if (USE_CHECKPC_CALLBACK && USE_CHECKPC_DBRA) || EMULATE_ADDRESS_ERRORS_JUMP\r
     ot("  beq DbraMin1\n");\r
     ot("\n");\r
 \r
     ot(";@ Get Branch offset:\n");\r
     ot("  ldrsh r0,[r4]\n");\r
-    ot("  add r0,r4,r0 ;@ r4 = New PC\n");\r
-    CheckPc(0);\r
+    ot("  add r0,r4,r0 ;@ r0 = New PC\n");\r
+    CheckPc();\r
+#if EMULATE_ADDRESS_ERRORS_JUMP\r
+    ot("  mov r4,r0\n");\r
+    ot("  tst r4,#1 ;@ address error?\n");\r
+    ot("  bne ExceptionAddressError_r_prg_r4\n");\r
+#else\r
+    ot("  bic r4,r0,#1\n");\r
+#endif\r
 #else\r
     ot("\n");\r
     ot(";@ Get Branch offset:\n");\r
@@ -343,7 +382,7 @@ int OpDbra(int op)
     OpEnd();\r
   }\r
 \r
-#if USE_CHECKPC_CALLBACK && USE_CHECKPC_DBRA\r
+#if (USE_CHECKPC_CALLBACK && USE_CHECKPC_DBRA) || EMULATE_ADDRESS_ERRORS_JUMP\r
   if (op==0x51c8)\r
   {\r
     ot(";@ Dn.w is -1:\n");\r
@@ -451,15 +490,15 @@ int OpBranch(int op)
 #if USE_CHECKPC_CALLBACK\r
   if (offset==-1) checkpc=1;\r
 #endif\r
-  if (checkpc)\r
-  {\r
-    CheckPc(0);\r
-  }\r
-  else\r
-  {\r
-    ot("  bic r4,r0,#1\n"); // we do not emulate address errors\r
-    ot("\n");\r
-  }\r
+  if (checkpc) CheckPc();\r
+#if EMULATE_ADDRESS_ERRORS_JUMP\r
+  ot("  mov r4,r0\n");\r
+  ot("  tst r4,#1 ;@ address error?\n");\r
+  ot("  bne ExceptionAddressError_r_prg_r4\n");\r
+#else\r
+  ot("  bic r4,r0,#1\n");\r
+#endif\r
+  ot("\n");\r
 \r
   OpEnd(size?0x10:0);\r
 \r