--- /dev/null
+\r
+#include "app.h"\r
+\r
+// --------------------- Opcodes 0x0000+ ---------------------\r
+// Emit an Ori/And/Sub/Add/Eor/Cmp Immediate opcode, 0000ttt0 00aaaaaa\r
+int OpArith(int op)\r
+{\r
+ int type=0,size=0;\r
+ int sea=0,tea=0;\r
+ int use=0;\r
+\r
+ // Get source and target EA\r
+ type=(op>>9)&7; if (type==4 || type>=7) return 1;\r
+ size=(op>>6)&3; if (size>=3) return 1;\r
+ sea= 0x003c;\r
+ tea=op&0x003f;\r
+\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
+\r
+ use=OpBase(op);\r
+ if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
+\r
+ OpStart(op); Cycles=4;\r
+\r
+ EaCalc(10,0x0000, sea,size);\r
+ EaRead(10, 10, sea,size,1);\r
+\r
+ EaCalc(11,0x003f, tea,size);\r
+ EaRead(11, 0, tea,size,1);\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
+\r
+ if (type<2 || type==5) ot(" adds r1,r1,#0 ;@ Defines NZ, clears CV\n"); // 0,1,5\r
+\r
+ if (type< 2) OpGetFlags(0,0); // Ori/And\r
+ if (type==2) OpGetFlags(1,1); // Sub: Subtract/X-bit\r
+ if (type==3) OpGetFlags(0,1); // Add: X-bit\r
+ if (type==5) OpGetFlags(0,0); // Eor\r
+ if (type==6) OpGetFlags(1,0); // Cmp: Subtract\r
+ ot("\n");\r
+\r
+ if (type!=6)\r
+ {\r
+ EaWrite(11, 1, tea,size,1);\r
+ }\r
+\r
+ // Correct cycles:\r
+ if (type==6)\r
+ {\r
+ if (size>=2 && tea<0x10) Cycles+=2;\r
+ }\r
+ 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
+ }\r
+\r
+ OpEnd();\r
+\r
+ return 0;\r
+}\r
+\r
+// --------------------- Opcodes 0x5000+ ---------------------\r
+int OpAddq(int op)\r
+{\r
+ // 0101nnnt xxeeeeee (nnn=#8,1-7 t=addq/subq xx=size, eeeeee=EA)\r
+ int num=0,type=0,size=0,ea=0;\r
+ int use=0;\r
+ char count[16]="";\r
+ int shift=0;\r
+\r
+ num =(op>>9)&7; if (num==0) num=8;\r
+ type=(op>>8)&1;\r
+ size=(op>>6)&3; if (size>=3) return 1;\r
+ ea = op&0x3f;\r
+\r
+ // See if we can do this opcode:\r
+ if (EaCanRead (ea,size)==0) return 1;\r
+ if (EaCanWrite(ea )==0) 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
+\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
+ Cycles=ea<8?4:8;\r
+ if (size>=2 && ea!=8) Cycles+=4;\r
+\r
+ EaCalc(10,0x003f, ea,size);\r
+ EaRead(10, 0, ea,size,1);\r
+\r
+ shift=32-(8<<size);\r
+\r
+ if (num!=8)\r
+ {\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
+\r
+ ot(" and r2,r2,#0x%.4x\n",7<<shift);\r
+ ot("\n");\r
+ strcpy(count,"r2");\r
+ }\r
+\r
+ if (num==8) sprintf(count,"#0x%.4x",8<<shift);\r
+\r
+ if (type==0) ot(" adds r1,r0,%s\n",count);\r
+ if (type==1) ot(" subs r1,r0,%s\n",count);\r
+\r
+ if ((ea&0x38)!=0x08) OpGetFlags(type,1);\r
+ ot("\n");\r
+\r
+ EaWrite(10, 1, ea,size,1);\r
+\r
+ OpEnd();\r
+\r
+ return 0;\r
+}\r
+\r
+// --------------------- Opcodes 0x8000+ ---------------------\r
+// 1t0tnnnd xxeeeeee (tt=type:or/sub/and/add xx=size, eeeeee=EA)\r
+int OpArithReg(int op)\r
+{\r
+ int use=0;\r
+ int type=0,size=0,dir=0,rea=0,ea=0;\r
+\r
+ type=(op>>12)&5;\r
+ rea =(op>> 9)&7;\r
+ dir =(op>> 8)&1;\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
+\r
+ use=OpBase(op);\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
+\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
+\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
+\r
+ if ((type&1)==0) ot(" adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
+\r
+ OpGetFlags(type==1,type&1); // 1==subtract\r
+ 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
+\r
+ OpEnd();\r
+\r
+ return 0;\r
+}\r
+\r
+// --------------------- Opcodes 0x80c0+ ---------------------\r
+int OpMul(int op)\r
+{\r
+ // Div/Mul: 1m00nnns 11eeeeee (m=Mul, nnn=Register Dn, s=signed, eeeeee=EA)\r
+ int type=0,rea=0,sign=0,ea=0;\r
+ int use=0;\r
+\r
+ type=(op>>14)&1; // div/mul\r
+ rea =(op>> 9)&7;\r
+ sign=(op>> 8)&1;\r
+ ea = op&0x3f;\r
+\r
+ // See if we can do this opcode:\r
+ if (EaCanRead(ea,1)==0) return 1;\r
+\r
+ use=OpBase(op);\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
+\r
+ EaCalc(10,0x003f, ea, 1);\r
+ EaRead(10, 10, ea, 1);\r
+\r
+ EaCalc (0,0x0e00,rea, 2);\r
+ EaRead (0, 2,rea, 2);\r
+\r
+ if (type==0)\r
+ {\r
+ ot(" cmp r10,#0\n");\r
+ ot(" moveq r10,#1 ;@ Divide by zero\n");\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("\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("\n");\r
+ }\r
+\r
+ ot(";@ Divide r2 by r10\n");\r
+ ot(" mov r3,#0\n");\r
+ ot(" mov r1,r10\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
+ ot(" cmp r1,r2,lsr #1\n");\r
+ ot(" movls r1,r1,lsl #1\n");\r
+ ot(" bcc Shift%.4x\n",op);\r
+ ot("\n");\r
+\r
+ ot("Divide%.4x%s\n",op,ms?"":":");\r
+ 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(" movne r1,r1,lsr #1\n");\r
+ ot(" bne Divide%.4x\n",op);\r
+ ot("\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
+\r
+ ot(" mov r11,r2 ;@ Remainder\n");\r
+\r
+ ot(" adds r1,r3,#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
+ }\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 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("\n");\r
+\r
+ ot(" mul r1,r2,r10\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
+\r
+\r
+ OpEnd();\r
+\r
+ return 0;\r
+}\r
+\r
+// Get X Bit into carry - trashes r2\r
+static 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(" msr cpsr_flg,r2 ;@ Get into Carry\n");\r
+ ot("\n");\r
+ return 0;\r
+}\r
+\r
+// --------------------- Opcodes 0x8100+ ---------------------\r
+// 1t00ddd1 0000asss - sbcd/abcd Ds,Dd or -(As),-(Ad)\r
+int OpAbcd(int op)\r
+{\r
+ int use=0;\r
+ int type=0,sea=0,addr=0,dea=0;\r
+ \r
+ type=(op>>14)&1;\r
+ dea =(op>> 9)&7;\r
+ addr=(op>> 3)&1;\r
+ sea = op &7;\r
+\r
+ if (addr) { sea|=0x20; dea|=0x20; }\r
+\r
+ use=op&~0x0e07; // Use same opcode for all registers\r
+ if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
+\r
+ OpStart(op); 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
+\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
+\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
+ }\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
+ }\r
+ ot("\n");\r
+\r
+ EaWrite(11, 1, dea,0,1);\r
+\r
+ OpEnd();\r
+\r
+ return 0;\r
+}\r
+\r
+// --------------------- Opcodes 0x90c0+ ---------------------\r
+// Suba/Cmpa/Adda 1tt1nnnx 11eeeeee (tt=type, x=size, eeeeee=Source EA)\r
+int OpAritha(int op)\r
+{\r
+ int use=0;\r
+ int type=0,size=0,sea=0,dea=0;\r
+\r
+ // Suba/Cmpa/Adda/(invalid):\r
+ type=(op>>13)&3; if (type>=3) return 1;\r
+\r
+ size=(op>>8)&1; size++;\r
+ dea=(op>>9)&7; dea|=8; // Dest=An\r
+ sea=op&0x003f; // Source\r
+\r
+ // See if we can do this opcode:\r
+ if (EaCanRead(sea,size)==0) return 1;\r
+\r
+ use=OpBase(op);\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
+\r
+ EaCalc ( 0,0x0e00, dea,2);\r
+ EaRead ( 0, 1, dea,2);\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==1) OpGetFlags(1,0); // Get Cmp flags\r
+ if (type==2) ot(" add r1,r1,r10\n");\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
+\r
+ OpEnd();\r
+\r
+ return 0;\r
+}\r
+\r
+// --------------------- Opcodes 0x9100+ ---------------------\r
+// Emit a Subx/Addx opcode, 1t01ddd1 zz000sss 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
+\r
+ type=(op>>12)&5;\r
+ dea =(op>> 9)&7;\r
+ size=(op>> 6)&3; if (size>=3) return 1;\r
+ sea = op&0x3f;\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 (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
+\r
+ OpStart(op); Cycles=8;\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
+\r
+ ot(";@ Do arithmetic:\n");\r
+ GetXBit(type==1);\r
+\r
+ if (type==5 && 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("\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
+ ot("\n");\r
+\r
+ ot(";@ Save result:\n");\r
+ EaWrite( 0, 1, dea,size,1);\r
+\r
+ OpEnd();\r
+\r
+ return 0;\r
+}\r
+\r
+// --------------------- Opcodes 0xb000+ ---------------------\r
+// Emit a Cmp/Eor opcode, 1011rrrt xxeeeeee (rrr=Dn, t=cmp/eor, xx=size extension, eeeeee=ea)\r
+int OpCmpEor(int op)\r
+{\r
+ int rea=0,eor=0;\r
+ int size=0,ea=0,use=0;\r
+\r
+ // Get EA and register EA\r
+ rea=(op>>9)&7;\r
+ eor=(op>>8)&1;\r
+ size=(op>>6)&3; if (size>=3) return 1;\r
+ ea=op&0x3f;\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
+\r
+ use=OpBase(op);\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
+\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
+\r
+ ot(";@ Get register operand into r1:\n");\r
+ EaCalc (1 ,0x0e00, rea,size);\r
+ EaRead (1, 1, rea,size,1);\r
+\r
+ ot(";@ Do arithmetic:\n");\r
+ if (eor==0) ot(" cmp r1,r0\n");\r
+ if (eor)\r
+ {\r
+ ot(" eor r1,r0,r1\n");\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
+\r
+ if (eor) EaWrite(10, 1,ea,size,1);\r
+\r
+ OpEnd();\r
+ return 0;\r
+}\r
+\r