NOT setting upper bits on PUSH PC, minor adjustments
[picodrive.git] / cpu / Cyclone / Main.cpp
index 1d0318c..5647fb1 100644 (file)
@@ -3,12 +3,14 @@
 \r
 static FILE *AsmFile=NULL;\r
 \r
-static int CycloneVer=0x0086; // Version number of library\r
+static int CycloneVer=0x0087; // Version number of library\r
 int *CyJump=NULL; // Jump table\r
 int ms=USE_MS_SYNTAX; // If non-zero, output in Microsoft ARMASM format\r
 char *Narm[4]={ "b", "h","",""}; // Normal ARM Extensions for operand sizes 0,1,2\r
 char *Sarm[4]={"sb","sh","",""}; // Sign-extend ARM Extensions for operand sizes 0,1,2\r
 int Cycles; // Current cycles for opcode\r
+int pc_dirty; // something changed PC during processing\r
+static int arm_op_count;\r
 \r
 \r
 void ot(const char *format, ...)\r
@@ -21,6 +23,9 @@ void ot(const char *format, ...)
   for(i=0, len=strlen(format); i < len && format[i] != '\n'; i++);\r
   if(i < len-1 && format[len-1] != '\n') printf("\nWARNING: possible improper newline placement:\n%s\n", format);\r
 \r
+  if (format[0] == ' ' && format[1] == ' ' && format[2] != ' ' && format[2] != '.')\r
+    arm_op_count++;\r
+\r
   va_start(valist,format);\r
   if (AsmFile) vfprintf(AsmFile,format,valist);\r
   va_end(valist);\r
@@ -32,7 +37,7 @@ void ltorg()
   else    ot("  .ltorg\n");\r
 }\r
 \r
-#if CYCLONE_FOR_GENESIS\r
+#if (CYCLONE_FOR_GENESIS == 2)\r
 // r12=ptr to tas in table, trashes r0,r1\r
 static void ChangeTAS(int norm)\r
 {\r
@@ -97,9 +102,10 @@ static void PrintException(int ints)
   ot("no_sp_swap%i%s\n",ints,ms?"":":");\r
 \r
   ot("  ldr r10,[r7,#0x60] ;@ Get Memory base\n");\r
-  ot("  mov r1,r4,lsl #8\n");\r
-  ot("  sub r1,r1,r10,lsl #8 ;@ r1 = Old PC\n");\r
-  ot("  mov r1,r1,asr #8 ;@ push sign extended\n");\r
+//  ot("  mov r1,r4,lsl #8\n");\r
+//  ot("  sub r1,r1,r10,lsl #8 ;@ r1 = Old PC\n");\r
+//  ot("  mov r1,r1,asr #8 ;@ push sign extended\n");\r
+  ot("  sub r1,r4,r10 ;@ r1 = Old PC\n");\r
   OpPush32();\r
   OpPushSr(1);\r
   ot("  mov r0,r11\n");\r
@@ -142,6 +148,15 @@ void CheckInterrupt(int op)
   ot("\n");\r
 }\r
 \r
+void FlushPC(void)\r
+{\r
+#if MEMHANDLERS_NEED_PC\r
+  if (pc_dirty)\r
+    ot("  str r4,[r7,#0x40] ;@ Save PC\n");\r
+#endif\r
+  pc_dirty = 0;\r
+}\r
+\r
 static void PrintFramework()\r
 {\r
   ot(";@ --------------------------- Framework --------------------------\n");\r
@@ -186,7 +201,7 @@ static void PrintFramework()
   ot("CycloneEnd%s\n", ms?"":":");\r
   ot("  sub r4,r4,#2\n");\r
   ot("CycloneEndNoBack%s\n", ms?"":":");\r
-#ifdef CYCLONE_FOR_PICODRIVE\r
+#if (CYCLONE_FOR_GENESIS == 2)\r
   ot("  ldr r1,[r7,#0x54]\n");\r
   ot("  mov r9,r9,lsr #28\n");\r
   ot("  tst r1,r1\n");\r
@@ -263,6 +278,7 @@ static void PrintFramework()
     ot("  bx lr\n");\r
     ot("\n");\r
 #endif\r
+  // 68k: XNZVC, ARM: NZCV\r
   if (ms) ot("CycloneSetSr\n");\r
   else    ot("CycloneSetSr:\n");\r
   ot("  mov r2,r1,lsr #8\n");\r
@@ -277,8 +293,8 @@ static void PrintFramework()
   ot("  str r3,[r0,#0x3C]\n");\r
   ot("  str r2,[r0,#0x48]\n");\r
   ot("setsr_noswap%s\n",ms?"":":");\r
-  ot("  mov r2,r1,lsr #3\n");\r
-  ot("  strb r2,[r0,#0x45] ;@ the X flag\n");\r
+  ot("  mov r2,r1,lsl #25\n");\r
+  ot("  str r2,[r0,#0x4c] ;@ the X flag\n");\r
   ot("  bic r2,r1,#0xf3\n");\r
   ot("  tst r1,#1\n");\r
   ot("  orrne r2,r2,#2\n");\r
@@ -296,8 +312,8 @@ static void PrintFramework()
   ot("  orrne r2,r2,#2\n");\r
   ot("  tst r1,#2\n");\r
   ot("  orrne r2,r2,#1\n");\r
-  ot("  ldrb r1,[r0,#0x45] ;@ the X flag\n");\r
-  ot("  tst r1,#2\n");\r
+  ot("  ldr r1,[r0,#0x4c] ;@ the X flag\n");\r
+  ot("  tst r1,#0x20000000\n");\r
   ot("  orrne r2,r2,#0x10\n");\r
   ot("  ldrb r1,[r0,#0x44] ;@ the SR high\n");\r
   ot("  orr r0,r2,r1,lsl #8\n");\r
@@ -306,7 +322,7 @@ static void PrintFramework()
 \r
   if (ms) ot("CycloneSetRealTAS\n");\r
   else    ot("CycloneSetRealTAS:\n");\r
-#if CYCLONE_FOR_GENESIS\r
+#if (CYCLONE_FOR_GENESIS == 2)\r
   ot("  ldr r12,=CycloneJumpTab\n");\r
   ot("  tst r0,r0\n");\r
   ot("  add r12,r12,#0x4a00*4\n");\r
@@ -367,7 +383,6 @@ static void PrintFramework()
   ot("\n");\r
   \r
   ot("Exception%s\n", ms?"":":");\r
-  ot("\n");\r
   ot("  stmdb sp!,{lr} ;@ Preserve ARM return address\n");\r
   PrintException(0);\r
   ot("  ldmia sp!,{pc} ;@ Return\n");\r
@@ -377,7 +392,7 @@ static void PrintFramework()
 // ---------------------------------------------------------------------------\r
 // Call Read(r0), Write(r0,r1) or Fetch(r0)\r
 // Trashes r0-r3,r12,lr\r
-int MemHandler(int type,int size)\r
+int MemHandler(int type,int size,int addrreg)\r
 {\r
   int func=0;\r
   func=0x68+type*0xc+(size<<2); // Find correct offset\r
@@ -386,20 +401,31 @@ int MemHandler(int type,int size)
   ot("  mov r3,r9,lsr #28\n");\r
   ot("  strb r3,[r7,#0x46] ;@ Save Flags (NZCV)\n");\r
 #endif\r
+  FlushPC();\r
 \r
 #if (MEMHANDLERS_ADDR_MASK & 0xff000000)\r
-  ot("  bic r0,r0,#0x%08x\n", MEMHANDLERS_ADDR_MASK & 0xff000000);\r
+  ot("  bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0xff000000);\r
+  addrreg=0;\r
 #endif\r
 #if (MEMHANDLERS_ADDR_MASK & 0x00ff0000)\r
-  ot("  bic r0,r0,#0x%08x\n", MEMHANDLERS_ADDR_MASK & 0x00ff0000);\r
+  ot("  bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0x00ff0000);\r
+  addrreg=0;\r
 #endif\r
 #if (MEMHANDLERS_ADDR_MASK & 0x0000ff00)\r
-  ot("  bic r0,r0,#0x%08x\n", MEMHANDLERS_ADDR_MASK & 0x0000ff00);\r
+  ot("  bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0x0000ff00);\r
+  addrreg=0;\r
 #endif\r
 #if (MEMHANDLERS_ADDR_MASK & 0x000000ff)\r
-  ot("  bic r0,r0,#0x%08x\n", MEMHANDLERS_ADDR_MASK & 0x000000ff);\r
+  ot("  bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0x000000ff);\r
+  addrreg=0;\r
 #endif\r
-  ot("  mov lr,pc\n");\r
+  if (addrreg != 0)\r
+  {\r
+    ot("  add lr,pc,#4\n"); // helps to prevent interlocks\r
+    ot("  mov r0,r%i\n", addrreg);\r
+  }\r
+  else\r
+    ot("  mov lr,pc\n");\r
   ot("  ldr pc,[r7,#0x%x] ;@ Call ",func);\r
 \r
   // Document what we are calling:\r
@@ -411,9 +437,6 @@ int MemHandler(int type,int size)
   else         ot("%d(r0)",   8<<size);\r
   ot(" handler\n");\r
 \r
-#if MEMHANDLERS_CHANGE_CYCLES\r
-  ot("  ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r
-#endif\r
 #if MEMHANDLERS_CHANGE_FLAGS\r
   ot("  ldrb r9,[r7,#0x46] ;@ r9 = Load Flags (NZCV)\n");\r
   ot("  mov r9,r9,lsl #28\n");\r
@@ -428,7 +451,7 @@ int MemHandler(int type,int size)
 static void PrintOpcodes()\r
 {\r
   int op=0;\r
\r
+\r
   printf("Creating Opcodes: [");\r
 \r
   ot(";@ ---------------------------- Opcodes ---------------------------\n");\r
@@ -456,6 +479,7 @@ static void PrintOpcodes()
   ot("  mov r0,#0x10\n");\r
   ot("  bl Exception\n");\r
 #endif\r
+  ot("\n");\r
   Cycles=34;\r
   OpEnd();\r
 \r
@@ -482,6 +506,7 @@ static void PrintOpcodes()
   ot("  mov r0,#0x28\n");\r
   ot("  bl Exception\n");\r
 #endif\r
+  ot("\n");\r
   Cycles=4;\r
   OpEnd();\r
 \r
@@ -507,6 +532,7 @@ static void PrintOpcodes()
   ot("  mov r0,#0x2c\n");\r
   ot("  bl Exception\n");\r
 #endif\r
+  ot("\n");\r
   Cycles=4;\r
   OpEnd();\r
 \r
@@ -609,12 +635,11 @@ static void PrintJumpTable()
     }\r
     if(ip&1) ott("0x%.4x", 0, "\n",0,ip++,1);\r
     if(ip&7) fseek(AsmFile, -1, SEEK_CUR); // remove last comma\r
-    ot("\n");\r
     if(ip&7) {\r
       for(i = 8-(ip&7); i > 0; i--)\r
         ot(",0x0000");\r
-      ot("\n");\r
     }\r
+    ot("\n");\r
     if(ms) {\r
       for(i = (0x2000-ip/2)/8+1; i > 0; i--)\r
         ot("  dcd 0,0,0,0,0,0,0,0\n");\r
@@ -662,7 +687,7 @@ static int CycloneMake()
   ot("\n;@ Dave's Cyclone 68000 Emulator v%x.%.3x - Assembler Output\n\n",CycloneVer>>12,CycloneVer&0xfff);\r
 \r
   ot(";@ (c) Copyright 2003 Dave, All rights reserved.\n");\r
-  ot(";@ some code (c) Copyright 2005-2006 notaz, All rights reserved.\n");\r
+  ot(";@ some code (c) Copyright 2005-2007 notaz, All rights reserved.\n");\r
   ot(";@ Cyclone 68000 is free for non-commercial use.\n\n");\r
   ot(";@ For commercial use, separate licencing terms must be obtained.\n\n");\r
 \r
@@ -689,9 +714,9 @@ static int CycloneMake()
     ot("  .global CycloneRun\n");\r
     ot("  .global CycloneSetSr\n");\r
     ot("  .global CycloneGetSr\n");\r
-    ot("  .global CycloneSetRealTAS\n");\r
     ot("  .global CycloneVer\n");\r
-#ifdef CYCLONE_FOR_PICODRIVE\r
+#if (CYCLONE_FOR_GENESIS == 2)\r
+    ot("  .global CycloneSetRealTAS\n");\r
     ot("  .global CycloneDoInterrupt\n");\r
     ot("  .global CycloneJumpTab\n");\r
 #endif\r
@@ -700,7 +725,9 @@ static int CycloneMake()
   ot("\n");\r
 \r
   PrintFramework();\r
+  arm_op_count = 0;\r
   PrintOpcodes();\r
+  printf("~%i ARM instructions used for opcode handlers\n", arm_op_count);\r
   PrintJumpTable();\r
 \r
   if (ms) ot("  END\n");\r