Commit my changes, version set to 0.099
[cyclone68000.git] / Cyclone / OpArith.cpp
index c8144f4..d762049 100644 (file)
@@ -1,22 +1,25 @@
 \r
 // This file is part of the Cyclone 68000 Emulator\r
 \r
-// Copyright (c) 2011 FinalDave (emudave (at) gmail.com)\r
+// Copyright (c) 2004,2011 FinalDave (emudave (at) gmail.com)\r
+// Copyright (c) 2005-2011 GraÅžvydas "notaz" Ignotas (notasas (at) gmail.com)\r
 \r
 // This code is licensed under the GNU General Public License version 2.0 and the MAME License.\r
 // You can choose the license that has the most advantages for you.\r
 \r
 // SVN repository can be found at http://code.google.com/p/cyclone68000/\r
 \r
+\r
 #include "app.h"\r
 \r
 // --------------------- Opcodes 0x0000+ ---------------------\r
-// Emit an Ori/And/Sub/Add/Eor/Cmp Immediate opcode, 0000ttt0 00aaaaaa\r
+// Emit an Ori/And/Sub/Add/Eor/Cmp Immediate opcode, 0000ttt0 ssaaaaaa\r
 int OpArith(int op)\r
 {\r
   int type=0,size=0;\r
   int sea=0,tea=0;\r
   int use=0;\r
+  const char *shiftstr="";\r
 \r
   // Get source and target EA\r
   type=(op>>9)&7; if (type==4 || type>=7) return 1;\r
@@ -26,27 +29,28 @@ int OpArith(int op)
 \r
   // See if we can do this opcode:\r
   if (EaCanRead(tea,size)==0) return 1;\r
-  if (type!=6 && EaCanWrite(tea)==0) return 1;\r
+  if (EaCanWrite(tea)==0 || EaAn(tea)) return 1;\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,size);\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op); Cycles=4;\r
+  OpStart(op, sea, tea); Cycles=4;\r
 \r
-  EaCalc(10,0x0000, sea,size);\r
-  EaRead(10,    10, sea,size,1);\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
-  EaCalc(11,0x003f, tea,size);\r
-  EaRead(11,     0, tea,size,1);\r
+  if (size<2) shiftstr=(char *)(size?",asl #16":",asl #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\n");\r
-  if (type==1) ot("  and r1,r0,r10\n");\r
-  if (type==2) ot("  subs r1,r0,r10 ;@ Defines NZCV\n");\r
-  if (type==3) ot("  adds r1,r0,r10 ;@ Defines NZCV\n");\r
-  if (type==5) ot("  eor r1,r0,r10\n");\r
-  if (type==6) ot("  cmp r0,r10 ;@ Defines NZCV\n");\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
@@ -59,7 +63,7 @@ int OpArith(int op)
 \r
   if (type!=6)\r
   {\r
-    EaWrite(11, 1, tea,size,1);\r
+    EaWrite(11, 1, tea,size,0x003f,1);\r
   }\r
 \r
   // Correct cycles:\r
@@ -70,11 +74,11 @@ int OpArith(int op)
   else\r
   {\r
     if (size>=2) Cycles+=4;\r
-    if (tea>=0x10) Cycles+=4;\r
-    if (Amatch && type==1 && size>=2 && tea<0x10) Cycles-=2;\r
+    if (tea>=8)  Cycles+=4;\r
+    if (type==1 && size>=2 && tea<8) Cycles-=2;\r
   }\r
 \r
-  OpEnd();\r
+  OpEnd(sea,tea);\r
 \r
   return 0;\r
 }\r
@@ -95,20 +99,22 @@ int OpAddq(int op)
 \r
   // See if we can do this opcode:\r
   if (EaCanRead (ea,size)==0) return 1;\r
-  if (EaCanWrite(ea     )==0) return 1;\r
+  if (EaCanWrite(ea)     ==0) return 1;\r
+  if (size == 0 && EaAn(ea) ) return 1;\r
 \r
-  use=op; if (ea<0x38) use&=~7;\r
-  if ((ea&0x38)==0x08) { size=2; use&=~0xc0; } // Every addq #n,An is 32-bit\r
+  use=OpBase(op,size,1);\r
 \r
   if (num!=8) use|=0x0e00; // If num is not 8, use same handler\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op);\r
+  OpStart(op,ea);\r
   Cycles=ea<8?4:8;\r
-  if (size>=2 && ea!=8) Cycles+=4;\r
+  if(type==0&&size==1) Cycles=ea<0x10?4:8;\r
+  if(size>=2) Cycles=ea<0x10?8:12;\r
+\r
+  if (size>0 && (ea&0x38)==0x08) size=2; // addq.w #n,An is also 32-bit\r
 \r
-  EaCalc(10,0x003f, ea,size);\r
-  EaRead(10,     0, ea,size,1);\r
+  EaCalcReadNoSE(11,0,ea,size,0x003f);\r
 \r
   shift=32-(8<<size);\r
 \r
@@ -116,15 +122,19 @@ int OpAddq(int op)
   {\r
     int lsr=9-shift;\r
 \r
-    if (lsr>=0) ot("  mov r2,r8,lsr #%d ;@ Get quick value\n", lsr);\r
-    else        ot("  mov r2,r8,lsl #%d ;@ Get quick value\n",-lsr);\r
+    ot("  and r2,r8,#0x0e00 ;@ Get quick value\n");\r
+\r
+    if (lsr>=0) sprintf(count,"r2,lsr #%d",  lsr);\r
+    else        sprintf(count,"r2,lsl #%d", -lsr);\r
 \r
-    ot("  and r2,r2,#0x%.4x\n",7<<shift);\r
     ot("\n");\r
-    strcpy(count,"r2");\r
+  }\r
+  else\r
+  {\r
+    sprintf(count,"#0x%.4x",8<<shift);\r
   }\r
 \r
-  if (num==8) sprintf(count,"#0x%.4x",8<<shift);\r
+  if (size<2)  ot("  mov r0,r0,asl #%d\n\n",size?16:24);\r
 \r
   if (type==0) ot("  adds r1,r0,%s\n",count);\r
   if (type==1) ot("  subs r1,r0,%s\n",count);\r
@@ -132,9 +142,9 @@ int OpAddq(int op)
   if ((ea&0x38)!=0x08) OpGetFlags(type,1);\r
   ot("\n");\r
 \r
-  EaWrite(10,     1, ea,size,1);\r
+  EaWrite(11,     1, ea,size,0x003f,1);\r
 \r
-  OpEnd();\r
+  OpEnd(ea);\r
 \r
   return 0;\r
 }\r
@@ -145,39 +155,43 @@ int OpArithReg(int op)
 {\r
   int use=0;\r
   int type=0,size=0,dir=0,rea=0,ea=0;\r
+  const char *asl="";\r
+  const char *strop=0;\r
 \r
   type=(op>>12)&5;\r
   rea =(op>> 9)&7;\r
-  dir =(op>> 8)&1;\r
+  dir =(op>> 8)&1; // er,re\r
   size=(op>> 6)&3; if (size>=3) return 1;\r
   ea  = op&0x3f;\r
 \r
   if (dir && ea<0x10) return 1; // addx/subx opcode\r
 \r
   // See if we can do this opcode:\r
-  if (dir==0 && EaCanWrite(rea)==0) return 1;\r
-  if (dir    && EaCanWrite( ea)==0) return 1;\r
+  if (dir==0 && EaCanRead (ea,size)==0) return 1;\r
+  if (dir    && EaCanWrite(ea)==0)      return 1;\r
+  if ((size==0||!(type&1))&&EaAn(ea))   return 1;\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,size);\r
   use&=~0x0e00; // Use same opcode for Dn\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op); Cycles=4;\r
+  OpStart(op,ea); Cycles=4;\r
 \r
-  ot(";@ Get r10=EA r11=EA value\n");\r
-  EaCalc(10,0x003f, ea,size);\r
-  EaRead(10,    11, ea,size,1);\r
-  ot(";@ Get r0=Register r1=Register value\n");\r
-  EaCalc( 0,0x0e00,rea,size);\r
-  EaRead( 0,     1,rea,size,1);\r
+  EaCalcReadNoSE(dir?11:-1,0,ea,size,0x003f);\r
+\r
+  EaCalcReadNoSE(dir?-1:11,1,rea,size,0x0e00);\r
 \r
   ot(";@ Do arithmetic:\n");\r
-  if (type==0) ot("  orr  ");\r
-  if (type==1) ot("  subs ");\r
-  if (type==4) ot("  and  ");\r
-  if (type==5) ot("  adds ");\r
-  if (dir) ot("r1,r11,r1\n");\r
-  else     ot("r1,r1,r11\n");\r
+  if (type==0) strop = "orr";\r
+  if (type==1) strop = (char *) (dir ? "subs" : "rsbs");\r
+  if (type==4) strop = "and";\r
+  if (type==5) strop = "adds";\r
+\r
+  if (size==0) asl=",asl #24";\r
+  if (size==1) asl=",asl #16";\r
+\r
+  if (size<2) ot("  mov r0,r0%s\n",asl);\r
+  ot("  %s r1,r0,r1%s\n",strop,asl);\r
 \r
   if ((type&1)==0) ot("  adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
 \r
@@ -185,13 +199,23 @@ int OpArithReg(int op)
   ot("\n");\r
 \r
   ot(";@ Save result:\n");\r
-  if (dir) EaWrite(10, 1, ea,size,1);\r
-  else     EaWrite( 0, 1,rea,size,1);\r
-\r
-  if (size==1 && ea>=0x10) Cycles+=4;\r
-  if (size>=2) { if (ea<0x10) Cycles+=4; else Cycles+=2; }\r
+  if (size<2) ot("  mov r1,r1,asr #%d\n",size?16:24);\r
+  if (dir) EaWrite(11, 1, ea,size,0x003f,0,0);\r
+  else     EaWrite(11, 1,rea,size,0x0e00,0,0);\r
+\r
+  if(rea==ea) {\r
+    if(ea<8) Cycles=(size>=2)?8:4; else Cycles+=(size>=2)?26:14;\r
+  } else if(dir) {\r
+    Cycles+=4;\r
+    if(size>=2) Cycles+=4;\r
+  } else {\r
+    if(size>=2) {\r
+      Cycles+=2;\r
+      if(ea<0x10||ea==0x3c) Cycles+=2;\r
+    }\r
+  }\r
 \r
-  OpEnd();\r
+  OpEnd(ea);\r
 \r
   return 0;\r
 }\r
@@ -209,41 +233,55 @@ int OpMul(int op)
   ea  = op&0x3f;\r
 \r
   // See if we can do this opcode:\r
-  if (EaCanRead(ea,1)==0) return 1;\r
+  if (EaCanRead(ea,1)==0||EaAn(ea)) return 1;\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,1);\r
   use&=~0x0e00; // Use same for all registers\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op); Cycles=type?70:133;\r
+  OpStart(op,ea);\r
+  if(type) Cycles=54;\r
+  else     Cycles=sign?158:140;\r
 \r
-  EaCalc(10,0x003f, ea, 1);\r
-  EaRead(10,    10, ea, 1);\r
+  EaCalcReadNoSE(-1,0,ea,1,0x003f);\r
 \r
-  EaCalc (0,0x0e00,rea, 2);\r
-  EaRead (0,     2,rea, 2);\r
+  EaCalc(11,0x0e00,rea, 2);\r
+  EaRead(11,     2,rea, 2,0x0e00);\r
 \r
-  if (type==0)\r
+  ot("  movs r1,r0,asl #16\n");\r
+\r
+  if (type==0) // div\r
   {\r
-    ot("  cmp r10,#0\n");\r
-    ot("  moveq r10,#1 ;@ Divide by zero\n");\r
+    // the manual says C is always cleared, but neither Musashi nor FAME do that\r
+    //ot("  bic r10,r10,#0x20000000 ;@ always clear C\n");\r
+    ot("  beq divzero%.4x ;@ division by zero\n",op);\r
     ot("\n");\r
     \r
     if (sign)\r
     {\r
-      ot("  mov r11,#0 ;@ r11 = 1 if the result is negative\n");\r
-      ot("  eorlt r11,r11,#1\n");\r
-      ot("  rsblt r10,r10,#0 ;@ Make r10 positive\n");\r
+      ot("  mov r12,#0 ;@ r12 = 1 or 2 if the result is negative\n");\r
+      ot("  tst r2,r2\n");\r
+      ot("  orrmi r12,r12,#2\n");\r
+      ot("  rsbmi r2,r2,#0 ;@ Make r2 positive\n");\r
       ot("\n");\r
-      ot("  cmp r2,#0\n");\r
-      ot("  eorlt r11,r11,#1\n");\r
-      ot("  rsblt r2,r2,#0 ;@ Make r2 positive\n");\r
+      ot("  movs r0,r1,asr #16\n");\r
+      ot("  orrmi r12,r12,#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,r1,lsr #16 ;@ use only 16 bits of divisor\n");\r
     }\r
 \r
-    ot(";@ Divide r2 by r10\n");\r
+    ot("\n");\r
+    ot(";@ Divide r2 by r0\n");\r
     ot("  mov r3,#0\n");\r
-    ot("  mov r1,r10\n");\r
+    ot("  mov r1,r0\n");\r
     ot("\n");\r
     ot(";@ Shift up divisor till it's just less than numerator\n");\r
     ot("Shift%.4x%s\n",op,ms?"":":");\r
@@ -256,62 +294,85 @@ int OpMul(int op)
     ot("  cmp r2,r1\n");\r
     ot("  adc r3,r3,r3 ;@ Double r3 and add 1 if carry set\n");\r
     ot("  subcs r2,r2,r1\n");\r
-    ot("  teq r1,r10\n");\r
+    ot("  teq r1,r0\n");\r
     ot("  movne r1,r1,lsr #1\n");\r
     ot("  bne Divide%.4x\n",op);\r
     ot("\n");\r
+    ot(";@r3==quotient,r2==remainder\n");\r
 \r
     if (sign)\r
     {\r
-      ot("  tst r11,r11\n");\r
-      ot("  rsbne r3,r3,#0 ;@ Negate if result is negative\n");\r
-    }\r
+      // sign correction\r
+      ot("  and r1,r12,#1\n");\r
+      ot("  teq r1,r12,lsr #1\n");\r
+      ot("  rsbne r3,r3,#0 ;@ negate if quotient is negative\n");\r
+      ot("  tst r12,#2\n");\r
+      ot("  rsbne r2,r2,#0 ;@ negate the remainder if divident was negative\n");\r
+      ot("\n");\r
 \r
-    ot("  mov r11,r2 ;@ Remainder\n");\r
+      // signed overflow check\r
+      ot("  mov r1,r3,asl #16\n");\r
+      ot("  cmp r3,r1,asr #16 ;@ signed overflow?\n");\r
+      ot("  orrne r10,r10,#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
+      // overflow check\r
+      ot("  movs r1,r3,lsr #16 ;@ check for overflow condition\n");\r
+      ot("  orrne r10,r10,#0x10000000 ;@ set overflow flag\n");\r
+      ot("  bne endofop%.4x ;@ overflow!\n",op);\r
+      ot("\n");\r
+    }\r
 \r
-    ot("  adds r1,r3,#0 ;@ Defines NZ, clears CV\n");\r
+    ot("  mov r1,r3,lsl #16 ;@ Clip to 16-bits\n");\r
+    ot("  adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
     OpGetFlags(0,0);\r
 \r
-    ot("  mov r1,r1,lsl #16 ;@ Clip to 16-bits\n");\r
     ot("  mov r1,r1,lsr #16\n");\r
-    ot("  orr r1,r1,r11,lsl #16 ;@ Insert remainder\n");\r
+    ot("  orr r1,r1,r2,lsl #16 ;@ Insert remainder\n");\r
   }\r
 \r
   if (type==1)\r
   {\r
-    char *shift="asr";\r
-\r
     ot(";@ Get 16-bit signs right:\n");\r
-    if (sign==0) { ot("  mov r10,r10,lsl #16\n"); shift="lsr"; }\r
+    ot("  mov r0,r1,%s #16\n",sign?"asr":"lsr");\r
     ot("  mov r2,r2,lsl #16\n");\r
-\r
-    if (sign==0) ot("  mov r10,r10,lsr #16\n");\r
-    ot("  mov r2,r2,%s #16\n",shift);\r
+    ot("  mov r2,r2,%s #16\n",sign?"asr":"lsr");\r
     ot("\n");\r
 \r
-    ot("  mul r1,r2,r10\n");\r
+    ot("  mul r1,r2,r0\n");\r
     ot("  adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
     OpGetFlags(0,0);\r
-\r
-    if (Amatch && ea==0x3c) Cycles-=4;\r
   }\r
   ot("\n");\r
 \r
-  EaWrite(0,     1,rea, 2);\r
+  EaWrite(11,    1,rea, 2,0x0e00,1);\r
 \r
+  if (type==0) ot("endofop%.4x%s\n",op,ms?"":":");\r
+  OpEnd(ea);\r
 \r
-  OpEnd();\r
+  if (type==0) // div\r
+  {\r
+    ot("divzero%.4x%s\n",op,ms?"":":");\r
+    ot("  mov r0,#5 ;@ Divide by zero\n");\r
+    ot("  bl Exception\n");\r
+    Cycles+=38;\r
+    OpEnd(ea);\r
+    ot("\n");\r
+  }\r
 \r
   return 0;\r
 }\r
 \r
 // Get X Bit into carry - trashes r2\r
-static int GetXBit(int subtract)\r
+int GetXBit(int subtract)\r
 {\r
   ot(";@ Get X bit:\n");\r
-  ot("  ldrb r2,[r7,#0x45]\n");\r
-  if (subtract) ot("  mvn r2,r2,lsl #28 ;@ Invert it\n");\r
-  else          ot("  mov r2,r2,lsl #28\n");\r
+  ot("  ldr r2,[r7,#0x4c]\n");\r
+  if (subtract) ot("  mvn r2,r2 ;@ Invert it\n");\r
   ot("  msr cpsr_flg,r2 ;@ Get into Carry\n");\r
   ot("\n");\r
   return 0;\r
@@ -322,59 +383,158 @@ static int GetXBit(int subtract)
 int OpAbcd(int op)\r
 {\r
   int use=0;\r
-  int type=0,sea=0,addr=0,dea=0;\r
+  int type=0,sea=0,mem=0,dea=0;\r
   \r
-  type=(op>>14)&1;\r
+  type=(op>>14)&1; // sbcd/abcd\r
   dea =(op>> 9)&7;\r
-  addr=(op>> 3)&1;\r
+  mem =(op>> 3)&1;\r
   sea = op     &7;\r
 \r
-  if (addr) { sea|=0x20; dea|=0x20; }\r
+  if (mem) { sea|=0x20; dea|=0x20; }\r
 \r
-  use=op&~0x0e07; // Use same opcode for all registers\r
+  use=op&~0x0e07; // Use same opcode for all registers..\r
+  if (sea==0x27) use|=0x0007; // ___x.b -(a7)\r
+  if (dea==0x27) use|=0x0e00; // ___x.b -(a7)\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op); Cycles=6;\r
+  OpStart(op,sea,dea); Cycles=6;\r
 \r
-  EaCalc( 0,0x0007, sea,0);\r
-  EaRead( 0,    10, sea,0,1);\r
-  EaCalc(11,0x0e00, dea,0);\r
-  EaRead(11,     1, dea,0,1);\r
+  if (mem)\r
+  {\r
+    ot(";@ Get src/dest EA vals\n");\r
+    EaCalc (0,0x000f, sea,0,1);\r
+    EaRead (0,     6, sea,0,0x000f,1);\r
+    EaCalcReadNoSE(11,0,dea,0,0x0e00);\r
+  }\r
+  else\r
+  {\r
+    ot(";@ Get src/dest reg vals\n");\r
+    EaCalcReadNoSE(-1,6,sea,0,0x0007);\r
+    EaCalcReadNoSE(11,0,dea,0,0x0e00);\r
+    ot("  mov r6,r6,asl #24\n");\r
+  }\r
+  ot("  mov r1,r0,asl #24\n\n");\r
 \r
-  ot("  ldrb r2,[r7,#0x45] ;@ Get X bit\n");\r
-  ot("  tst r2,#2\n");\r
-  ot("  addne r10,r10,#0x01000000 ;@ Add carry bit\n");\r
+  ot("  bic r10,r10,#0xb1000000 ;@ clear all flags except old Z\n");\r
 \r
   if (type)\r
   {\r
-    ot(";@ Add units into r2:\n");\r
-    ot("  and r2,r1, #0x0f000000\n");\r
-    ot("  and r0,r10,#0x0f000000\n");\r
-    ot("  add r2,r2,r0\n");\r
-    ot("  cmp r2,#0x0a000000\n");\r
-    ot("  addpl r1,r1,#0x06000000 ;@ Decimal adjust units\n");\r
-    ot("  add r1,r1,r10 ;@ Add BCD\n");\r
-    ot("  mov r0,r1,lsr #24\n");\r
-    ot("  cmp r0,#0xa0\n");\r
-    ot("  addpl r1,r1,#0x60000000 ;@ Decimal adjust tens\n");\r
-    OpGetFlags(0,1);\r
+    ot("  ldr r0,[r7,#0x4c] ;@ Get X bit\n");\r
+    ot("  mov r3,#0x00f00000\n");\r
+    ot("  and r2,r3,r1,lsr #4\n");\r
+    ot("  tst r0,#0x20000000\n");\r
+    ot("  and r0,r3,r6,lsr #4\n");\r
+    ot("  add r0,r0,r2\n");\r
+    ot("  addne r0,r0,#0x00100000\n");\r
+//    ot("  tst r0,#0x00800000\n");\r
+//    ot("  orreq r10,r10,#0x01000000 ;@ Undefined V behavior\n");\r
+    ot("  cmp r0,#0x00900000\n");\r
+    ot("  addhi r0,r0,#0x00600000 ;@ Decimal adjust units\n");\r
+\r
+    ot("  mov r2,r1,lsr #28\n");\r
+    ot("  add r0,r0,r2,lsl #24\n");\r
+    ot("  mov r2,r6,lsr #28\n");\r
+    ot("  add r0,r0,r2,lsl #24\n");\r
+    ot("  cmp r0,#0x09900000\n");\r
+    ot("  orrhi r10,r10,#0x20000000 ;@ C\n");\r
+    ot("  subhi r0,r0,#0x0a000000\n");\r
+//    ot("  and r3,r10,r0,lsr #3 ;@ Undefined V behavior part II\n");\r
+//    ot("  orr r10,r10,r3,lsl #4 ;@ V\n");\r
+    ot("  movs r0,r0,lsl #4\n");\r
+    ot("  orrmi r10,r10,#0x90000000 ;@ Undefined N+V behavior\n"); // this is what Musashi really does\r
+    ot("  bicne r10,r10,#0x40000000 ;@ Z flag\n");\r
   }\r
   else\r
   {\r
-    ot(";@ Sub units into r2:\n");\r
-    ot("  and r2,r1, #0x0f000000\n");\r
-    ot("  and r0,r10,#0x0f000000\n");\r
-    ot("  subs r2,r2,r0\n");\r
-    ot("  submi r1,r1,#0x06000000 ;@ Decimal adjust units\n");\r
-    ot("  subs r1,r1,r10 ;@ Subtract BCD\n");\r
-    ot("  submis r1,r1,#0x60000000 ;@ Decimal adjust tens\n");\r
-    OpGetFlags(1,1);\r
+    ot("  ldr r0,[r7,#0x4c] ;@ Get X bit\n");\r
+    ot("  mov r3,#0x00f00000\n");\r
+    ot("  and r2,r3,r6,lsr #4\n");\r
+    ot("  tst r0,#0x20000000\n");\r
+    ot("  and r0,r3,r1,lsr #4\n");\r
+    ot("  sub r0,r0,r2\n");\r
+    ot("  subne r0,r0,#0x00100000\n");\r
+//    ot("  tst r0,#0x00800000\n");\r
+//    ot("  orreq r10,r10,#0x01000000 ;@ Undefined V behavior\n");\r
+    ot("  cmp r0,#0x00900000\n");\r
+    ot("  subhi r0,r0,#0x00600000 ;@ Decimal adjust units\n");\r
+\r
+    ot("  mov r2,r1,lsr #28\n");\r
+    ot("  add r0,r0,r2,lsl #24\n");\r
+    ot("  mov r2,r6,lsr #28\n");\r
+    ot("  sub r0,r0,r2,lsl #24\n");\r
+    ot("  cmp r0,#0x09900000\n");\r
+    ot("  orrhi r10,r10,#0xa0000000 ;@ N and C\n");\r
+    ot("  addhi r0,r0,#0x0a000000\n");\r
+//    ot("  and r3,r10,r0,lsr #3 ;@ Undefined V behavior part II\n");\r
+//    ot("  orr r10,r10,r3,lsl #4 ;@ V\n");\r
+    ot("  movs r0,r0,lsl #4\n");\r
+//    ot("  orrmi r10,r10,#0x80000000 ;@ Undefined N behavior\n");\r
+    ot("  bicne r10,r10,#0x40000000 ;@ Z flag\n");\r
   }\r
+\r
+  ot("  str r10,[r7,#0x4c] ;@ Save X bit\n");\r
+  ot("\n");\r
+\r
+  EaWrite(11,     0, dea,0,0x0e00,1);\r
+\r
+  ot("  ldr r6,[r7,#0x54]\n");\r
+  OpEnd(sea,dea);\r
+\r
+  return 0;\r
+}\r
+\r
+// 01001000 00eeeeee - nbcd <ea>\r
+int OpNbcd(int op)\r
+{\r
+  int use=0;\r
+  int ea=0;\r
+  \r
+  ea=op&0x3f;\r
+\r
+  if(EaCanWrite(ea)==0||EaAn(ea)) return 1;\r
+\r
+  use=OpBase(op,0);\r
+  if(op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
+\r
+  OpStart(op,ea); Cycles=6;\r
+  if(ea >= 8)  Cycles+=2;\r
+\r
+  EaCalcReadNoSE(6,0,ea,0,0x003f);\r
+\r
+  // this is rewrite of Musashi's code\r
+  ot("  ldr r2,[r7,#0x4c]\n");\r
+  ot("  bic r10,r10,#0xb0000000 ;@ clear all flags, except Z\n");\r
+  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("  rsb r11,r2,#0x9a000000 ;@ do arithmetic\n");\r
+\r
+  ot("  cmp r11,#0x9a000000\n");\r
+  ot("  beq finish%.4x\n",op);\r
   ot("\n");\r
 \r
-  EaWrite(11,     1, dea,0,1);\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 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 r10,r10,#0x40000000 ;@ Z\n");\r
+  ot("  orr r10,r10,r3,lsl #28 ;@ save V\n",op);\r
+  ot("  orr r10,r10,#0x20000000 ;@ C\n");\r
+  ot("\n");\r
+\r
+  EaWrite(6, 1, ea,0,0x3f,0,0);\r
+\r
+  ot("finish%.4x%s\n",op,ms?"":":");\r
+  ot("  tst r11,r11\n");\r
+  ot("  orrmi r10,r10,#0x80000000 ;@ N\n");\r
+  ot("  str r10,[r7,#0x4c] ;@ Save X\n");\r
+  ot("\n");\r
 \r
-  OpEnd();\r
+  ot("  ldr r6,[r7,#0x54]\n");\r
+  OpEnd(ea);\r
 \r
   return 0;\r
 }\r
@@ -385,6 +545,7 @@ int OpAritha(int op)
 {\r
   int use=0;\r
   int type=0,size=0,sea=0,dea=0;\r
+  const char *asr="";\r
 \r
   // Suba/Cmpa/Adda/(invalid):\r
   type=(op>>13)&3; if (type>=3) return 1;\r
@@ -396,83 +557,118 @@ int OpAritha(int op)
   // See if we can do this opcode:\r
   if (EaCanRead(sea,size)==0) return 1;\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,size);\r
   use&=~0x0e00; // Use same opcode for An\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op); Cycles=4;\r
-  EaCalc ( 0,0x003f, sea,size);\r
-  EaRead ( 0,    10, sea,size);\r
+  OpStart(op,sea); Cycles=(size==2)?6:8;\r
+  if(size==2&&(sea<0x10||sea==0x3c)) Cycles+=2;\r
+  if(type==1) Cycles=6;\r
+\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?11:-1,1,dea,2,0x0e00);\r
+  }\r
+#if 0\r
+  else\r
+  {\r
+    EaCalcReadNoSE(type!=1?11:-1,1,dea,2,0x0e00);\r
+    EaCalcReadNoSE(-1,0,sea,size,0x003f);\r
+  }\r
+#endif\r
 \r
-  EaCalc ( 0,0x0e00, dea,2);\r
-  EaRead ( 0,     1, dea,2);\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 r1,r1,r10\n");\r
-  if (type==1) ot("  cmp r1,r10 ;@ Defines NZCV\n");\r
+  if (type==0) ot("  sub r1,r1,r0%s\n",asr);\r
+  if (type==1) ot("  cmp r1,r0%s ;@ Defines NZCV\n",asr);\r
   if (type==1) OpGetFlags(1,0); // Get Cmp flags\r
-  if (type==2) ot("  add r1,r1,r10\n");\r
+  if (type==2) ot("  add r1,r1,r0%s\n",asr);\r
   ot("\n");\r
-  \r
-  EaWrite( 0,     1, dea,2);\r
 \r
-  if (Amatch && sea==0x3c) Cycles-=size<2?4:8; // Correct?\r
-  if (size>=2) { if (sea<0x10) Cycles+=4; else Cycles+=2; }\r
+  if (type!=1) EaWrite(11, 1, dea,2,0x0e00);\r
 \r
-  OpEnd();\r
+  OpEnd(sea);\r
 \r
   return 0;\r
 }\r
 \r
 // --------------------- Opcodes 0x9100+ ---------------------\r
-// Emit a Subx/Addx opcode, 1t01ddd1 zz000sss addx.z Ds,Dd\r
+// Emit a Subx/Addx opcode, 1t01ddd1 zz00rsss addx.z Ds,Dd\r
 int OpAddx(int op)\r
 {\r
   int use=0;\r
-  int type=0,size=0,dea=0,sea=0;\r
+  int type=0,size=0,dea=0,sea=0,mem=0;\r
+  const char *asl="";\r
 \r
-  type=(op>>12)&5;\r
+  type=(op>>14)&1;\r
   dea =(op>> 9)&7;\r
   size=(op>> 6)&3; if (size>=3) return 1;\r
-  sea = op&0x3f;\r
+  sea = op&7;\r
+  mem =(op>> 3)&1;\r
 \r
   // See if we can do this opcode:\r
   if (EaCanRead(sea,size)==0) return 1;\r
   if (EaCanWrite(dea)==0) return 1;\r
 \r
-  use=OpBase(op);\r
-  use&=~0x0e00; // Use same opcode for Dn\r
+  if (mem) { sea+=0x20; dea+=0x20; }\r
+\r
+  use=op&~0x0e07; // Use same opcode for Dn\r
+  if (size==0&&sea==0x27) use|=0x0007; // ___x.b -(a7)\r
+  if (size==0&&dea==0x27) use|=0x0e00; // ___x.b -(a7)\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op); Cycles=8;\r
+  OpStart(op,sea,dea); Cycles=4;\r
+  if(size>=2)   Cycles+=4;\r
+  if(sea>=0x10) Cycles+=2;\r
 \r
-  ot(";@ Get r10=EA r11=EA value\n");\r
-  EaCalc( 0,0x003f,sea,size);\r
-  EaRead( 0,    11,sea,size,1);\r
-  ot(";@ Get r0=Register r1=Register value\n");\r
-  EaCalc( 0,0x0e00,dea,size);\r
-  EaRead( 0,     1,dea,size,1);\r
+  if (mem)\r
+  {\r
+    ot(";@ Get src/dest EA vals\n");\r
+    EaCalc (0,0x000f, sea,size,1);\r
+    EaRead (0,     6, sea,size,0x000f,1);\r
+    EaCalcReadNoSE(11,0,dea,size,0x0e00);\r
+  }\r
+  else\r
+  {\r
+    ot(";@ Get src/dest reg vals\n");\r
+    EaCalcReadNoSE(-1,6,sea,size,0x0007);\r
+    EaCalcReadNoSE(11,0,dea,size,0x0e00);\r
+    if (size<2) ot("  mov r6,r6,asl #%d\n\n",size?16:24);\r
+  }\r
+\r
+  if (size<2) asl=(char *)(size?",asl #16":",asl #24");\r
 \r
   ot(";@ Do arithmetic:\n");\r
-  GetXBit(type==1);\r
+  GetXBit(type==0);\r
 \r
-  if (type==5 && size<2)\r
+  if (type==1 && size<2)\r
   {\r
     ot(";@ Make sure the carry bit will tip the balance:\n");\r
-    if (size==0) ot("  ldr r2,=0x00ffffff\n");\r
-    else         ot("  ldr r2,=0x0000ffff\n");\r
-    ot("  orr r11,r11,r2\n");\r
+    ot("  mvn r2,#0\n");\r
+    ot("  orr r6,r6,r2,lsr #%i\n",(size==0)?8:16);\r
     ot("\n");\r
   }\r
 \r
-  if (type==1) ot("  sbcs r1,r1,r11\n");\r
-  if (type==5) ot("  adcs r1,r1,r11\n");\r
-  OpGetFlags(type==1,1); // subtract\r
+  if (type==0) ot("  rscs r1,r6,r0%s\n",asl);\r
+  if (type==1) ot("  adcs r1,r6,r0%s\n",asl);\r
+  ot("  orr r3,r10,#0xb0000000 ;@ for old Z\n");\r
+  OpGetFlags(type==0,1,0); // subtract\r
+  if (size<2) {\r
+    ot("  movs r2,r1,lsr #%i\n", size?16:24);\r
+    ot("  orreq r10,r10,#0x40000000 ;@ add potentially missed Z\n");\r
+  }\r
+  ot("  andeq r10,r10,r3 ;@ fix Z\n");\r
   ot("\n");\r
 \r
   ot(";@ Save result:\n");\r
-  EaWrite( 0, 1, dea,size,1);\r
+  EaWrite(11, 1, dea,size,0x0e00,1);\r
 \r
-  OpEnd();\r
+  ot("  ldr r6,[r7,#0x54]\n");\r
+  OpEnd(sea,dea);\r
 \r
   return 0;\r
 }\r
@@ -483,6 +679,7 @@ int OpCmpEor(int op)
 {\r
   int rea=0,eor=0;\r
   int size=0,ea=0,use=0;\r
+  const char *asl="";\r
 \r
   // Get EA and register EA\r
   rea=(op>>9)&7;\r
@@ -490,41 +687,144 @@ int OpCmpEor(int op)
   size=(op>>6)&3; if (size>=3) return 1;\r
   ea=op&0x3f;\r
 \r
+  if (eor && (ea>>3) == 1) return 1; // not a valid mode for eor\r
+\r
   // See if we can do this opcode:\r
   if (EaCanRead(ea,size)==0) return 1;\r
   if (eor && EaCanWrite(ea)==0) return 1;\r
+  if (EaAn(ea)&&(eor||size==0)) return 1;\r
 \r
-  use=OpBase(op);\r
+  use=OpBase(op,size);\r
   use&=~0x0e00; // Use 1 handler for register d0-7\r
   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
 \r
-  OpStart(op); Cycles=eor?8:4;\r
+  OpStart(op,ea); Cycles=4;\r
+  if(eor) {\r
+    if(ea>8)     Cycles+=4;\r
+    if(size>=2)  Cycles+=4;\r
+  } else {\r
+    if(size>=2)  Cycles+=2;\r
+  }\r
 \r
-  ot(";@ Get EA into r10 and value into r0:\n");\r
-  EaCalc (10,0x003f,  ea,size);\r
-  EaRead (10,     0,  ea,size,1);\r
+  ot(";@ Get EA into r11 and value into r0:\n");\r
+  EaCalcReadNoSE(eor?11:-1,0,ea,size,0x003f);\r
 \r
   ot(";@ Get register operand into r1:\n");\r
-  EaCalc (1 ,0x0e00, rea,size);\r
-  EaRead (1,      1, rea,size,1);\r
+  EaCalcReadNoSE(-1,1,rea,size,0x0e00);\r
+\r
+  if (size<2) ot("  mov r0,r0,asl #%d\n\n",size?16:24);\r
+  if (size<2) asl=(char *)(size?",asl #16":",asl #24");\r
 \r
   ot(";@ Do arithmetic:\n");\r
-  if (eor==0) ot("  cmp r1,r0\n");\r
+  if (eor==0) ot("  rsbs r1,r0,r1%s\n",asl);\r
   if (eor)\r
   {\r
-    ot("  eor r1,r0,r1\n");\r
+    ot("  eor r1,r0,r1%s\n",asl);\r
     ot("  adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
   }\r
 \r
   OpGetFlags(eor==0,0); // Cmp like subtract\r
   ot("\n");\r
 \r
-  if (size>=2) Cycles+=4; // Correct?\r
-  if (ea==0x3c) Cycles-=4;\r
+  if (eor) EaWrite(11, 1,ea,size,0x003f,1);\r
+\r
+  OpEnd(ea);\r
+  return 0;\r
+}\r
+\r
+// Emit a Cmpm opcode, 1011ddd1 xx001sss (rrr=Adst, xx=size extension, sss=Asrc)\r
+int OpCmpm(int op)\r
+{\r
+  int size=0,sea=0,dea=0,use=0;\r
+  const char *asl="";\r
+\r
+  // get size, get EAs\r
+  size=(op>>6)&3; if (size>=3) return 1;\r
+  sea=(op&7)|0x18;\r
+  dea=(op>>9)&0x3f;\r
+\r
+  use=op&~0x0e07; // Use 1 handler for all registers..\r
+  if (size==0&&sea==0x1f) use|=0x0007; // ..except (a7)+\r
+  if (size==0&&dea==0x1f) use|=0x0e00;\r
+  if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
+\r
+  OpStart(op,sea); Cycles=4;\r
+\r
+  ot(";@ Get src operand into r11:\n");\r
+  EaCalc (0,0x0007, sea,size,1);\r
+  EaRead (0,    11, sea,size,0x0007,1);\r
+\r
+  ot(";@ Get dst operand into r0:\n");\r
+  EaCalcReadNoSE(-1,0,dea,size,0x0e00);\r
+\r
+  if (size<2) asl=(char *)(size?",asl #16":",asl #24");\r
+\r
+  ot("  rsbs r0,r11,r0%s\n",asl);\r
+  OpGetFlags(1,0); // Cmp like subtract\r
+  ot("\n");\r
+\r
+  OpEnd(sea);\r
+  return 0;\r
+}\r
+\r
+\r
+// Emit a Chk opcode, 0100ddd1 x0eeeeee (rrr=Dn, x=size extension, eeeeee=ea)\r
+int OpChk(int op)\r
+{\r
+  int rea=0;\r
+  int size=0,ea=0,use=0;\r
+\r
+  // Get EA and register EA\r
+  rea=(op>>9)&7;\r
+  if((op>>7)&1)\r
+       size=1; // word operation\r
+  else size=2; // long\r
+  ea=op&0x3f;\r
+\r
+  if (EaAn(ea)) return 1; // not a valid mode\r
+  if (size!=1)  return 1; // 000 variant only supports word\r
+\r
+  // See if we can do this opcode:\r
+  if (EaCanRead(ea,size)==0) return 1;\r
+\r
+  use=OpBase(op,size);\r
+  use&=~0x0e00; // Use 1 handler for register d0-7\r
+  if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
+\r
+  OpStart(op,ea); Cycles=10;\r
+\r
+  ot(";@ Get value into r0:\n");\r
+  EaCalcReadNoSE(-1,0,ea,size,0x003f);\r
+\r
+  ot(";@ Get register operand into r1:\n");\r
+  EaCalcReadNoSE(-1,1,rea,size,0x0e00);\r
+\r
+  if (size<2) ot("  mov r0,r0,asl #%d\n",size?16:24);\r
+  if (size<2) ot("  mov r1,r1,asl #%d\n\n",size?16:24);\r
+\r
+  ot(";@ get flags, including undocumented ones\n");\r
+  ot("  and r3,r10,#0x80000000\n");\r
+  ot("  adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
+  OpGetFlags(0,0);\r
+\r
+  ot(";@ is reg negative?\n");\r
+  ot("  bmi chktrap%.4x\n",op);\r
+\r
+  ot(";@ Do arithmetic:\n");\r
+  ot("  bic r10,r10,#0x80000000 ;@ N\n");\r
+  ot("  cmp r1,r0\n");\r
+  ot("  bgt chktrap%.4x\n",op);\r
+\r
+  ot(";@ old N remains\n");\r
+  ot("  orr r10,r10,r3\n");\r
+  OpEnd(ea);\r
 \r
-  if (eor) EaWrite(10, 1,ea,size,1);\r
+  ot("chktrap%.4x%s ;@ CHK exception:\n",op,ms?"":":");\r
+  ot("  mov r0,#6\n");\r
+  ot("  bl Exception\n");\r
+  Cycles+=40;\r
+  OpEnd(ea);\r
 \r
-  OpEnd();\r
   return 0;\r
 }\r
 \r