--- /dev/null
+\r
+// This file is part of the PicoDrive Megadrive Emulator\r
+\r
+// Copyright (c) 2011 FinalDave (emudave (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
+// Disa 68000 Disassembler\r
+#ifndef __GNUC__\r
+#pragma warning(disable:4115)\r
+#endif\r
+\r
+#define _CRT_SECURE_NO_WARNINGS\r
+#include <stdio.h>\r
+#include <string.h>\r
+#include "Disa.h"\r
+\r
+unsigned int DisaPc=0;\r
+char *DisaText=NULL; // Text buffer to write in\r
+static char Tasm[]="bwl?";\r
+static char Comment[64]="";\r
+unsigned short (CPU_CALL *DisaWord)(unsigned int a)=NULL;\r
+\r
+static unsigned int DisaLong(unsigned int a)\r
+{\r
+ unsigned int d=0;\r
+ if (DisaWord==NULL) return d;\r
+\r
+ d= DisaWord(a)<<16;\r
+ d|=DisaWord(a+2)&0xffff;\r
+ return d;\r
+}\r
+\r
+// Get text version of the effective address\r
+int DisaGetEa(char *t,int ea,int size)\r
+{\r
+ ea&=0x3f; t[0]=0;\r
+ if ((ea&0x38)==0x00) { sprintf(t,"d%d",ea ); return 0; } // 000rrr\r
+ if ((ea&0x38)==0x08) { sprintf(t,"a%d",ea&7); return 0; } // 001rrr\r
+ if ((ea&0x38)==0x10) { sprintf(t,"(a%d)",ea&7); return 0; } // 010rrr\r
+ if ((ea&0x38)==0x18) { sprintf(t,"(a%d)+",ea&7); return 0; } // 011rrr\r
+ if ((ea&0x38)==0x20) { sprintf(t,"-(a%d)",ea&7); return 0; } // 100rrr\r
+ if ((ea&0x38)==0x28) { sprintf(t,"($%x,a%d)",DisaWord(DisaPc)&0xffff,ea&7); DisaPc+=2; return 0; } // 101rrr\r
+\r
+ if ((ea&0x38)==0x30)\r
+ {\r
+ // 110nnn - An + Disp + D/An\r
+ int areg=0,ext=0,off=0,da=0,reg=0,wol=0,scale=0;\r
+ ext=DisaWord(DisaPc)&0xffff;\r
+ \r
+ areg=ea&7;\r
+ off=ext&0xff; da =ext&0x8000?'a':'d';\r
+ reg=(ext>>12)&7; wol=ext&0x0800?'l':'w';\r
+ scale=1<<((ext>>9)&3);\r
+\r
+ if (scale<2) sprintf(t,"($%x,a%d,%c%d.%c)", off,areg,da,reg,wol);\r
+ else sprintf(t,"($%x,a%d,%c%d.%c*%d)",off,areg,da,reg,wol,scale); // 68020\r
+\r
+ DisaPc+=2;\r
+ return 0;\r
+ }\r
+\r
+ if (ea==0x38) { sprintf(t,"$%x.w",DisaWord(DisaPc)&0xffff); DisaPc+=2; return 0; } // 111000 - Absolute short\r
+ if (ea==0x39) { sprintf(t,"$%x.l",DisaLong(DisaPc)); DisaPc+=4; return 0; } // 111001 - Absolute long\r
+\r
+ if (ea==0x3a)\r
+ {\r
+ // 111010 - PC Relative\r
+ int ext=DisaWord(DisaPc)&0xffff;\r
+ sprintf(t,"($%x,pc)",ext);\r
+ sprintf(Comment,"; =%x",DisaPc+(short)ext); // Comment where pc+ext is\r
+ DisaPc+=2;\r
+ return 0;\r
+ }\r
+\r
+ if (ea==0x3b)\r
+ {\r
+ // 111011 - PC Relative + D/An\r
+ int ext=0,off=0,da=0,reg=0,wol=0,scale=0;\r
+ ext=DisaWord(DisaPc)&0xffff;\r
+ \r
+ off=ext&0xff; da =ext&0x8000?'a':'d';\r
+ reg=(ext>>12)&7; wol=ext&0x0800?'l':'w';\r
+ scale=1<<((ext>>9)&3);\r
+\r
+ if (scale<2) sprintf(t,"($%x,pc,%c%d.%c)", off,da,reg,wol);\r
+ else sprintf(t,"($%x,pc,%c%d.%c*%d)",off,da,reg,wol,scale); // 68020\r
+\r
+ sprintf(Comment,"; =%x",DisaPc+(char)off); // Comment where pc+ext is\r
+ DisaPc+=2;\r
+ return 0;\r
+ }\r
+\r
+ if (ea==0x3c)\r
+ {\r
+ // 111100 - Immediate\r
+ switch (size)\r
+ {\r
+ case 0: sprintf(t,"#$%x",DisaWord(DisaPc)&0x00ff); DisaPc+=2; return 0;\r
+ case 1: sprintf(t,"#$%x",DisaWord(DisaPc)&0xffff); DisaPc+=2; return 0;\r
+ case 2: sprintf(t,"#$%x",DisaLong(DisaPc) ); DisaPc+=4; return 0;\r
+ }\r
+ return 1;\r
+ }\r
+\r
+// Unknown effective address\r
+ sprintf(t,"ea=(%d%d%d %d%d%d)",\r
+ (ea>>5)&1,(ea>>4)&1,(ea>>3)&1,\r
+ (ea>>2)&1,(ea>>1)&1, ea &1);\r
+ return 1;\r
+}\r
+\r
+static void GetOffset(char *text)\r
+{\r
+ int off=(short)DisaWord(DisaPc); DisaPc+=2;\r
+\r
+ if (off<0) sprintf(text,"-$%x",-off);\r
+ else sprintf(text,"$%x", off);\r
+}\r
+\r
+// ================ Opcodes 0x0000+ ================\r
+static int DisaArithImm(int op)\r
+{\r
+ // Or/And/Sub/Add/Eor/Cmp Immediate 0000ttt0 xxDDDddd (tt=type, xx=size extension, DDDddd=Dest ea)\r
+ int dea=0;\r
+ char seat[64]="",deat[64]="";\r
+ int type=0,size=0;\r
+ char *arith[8]={"or","and","sub","add","?","eor","cmp","?"};\r
+\r
+ type=(op>>9)&7; if (type==4 || type>=7) return 1;\r
+ size=(op>>6)&3; if (size>=3) return 1;\r
+ dea=op&0x3f; if (dea==0x3c) return 1;\r
+\r
+ DisaGetEa(seat,0x3c,size);\r
+ DisaGetEa(deat,dea, size);\r
+\r
+ sprintf(DisaText,"%si.%c %s, %s",arith[type],Tasm[size],seat,deat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x0108+ ================\r
+static int DisaMovep(int op)\r
+{\r
+ // movep.x (Aa),Dn - 0000nnn1 dx001aaa nn\r
+ int dn=0,dir=0,size=0,an=0;\r
+ char offset[32]="";\r
+\r
+ dn =(op>>9)&7;\r
+ dir =(op>>7)&1;\r
+ size=(op>>6)&1; size++;\r
+ an = op &7;\r
+\r
+ GetOffset(offset);\r
+ if (dir) sprintf(DisaText,"movep.%c d%d, (%s,a%d)",Tasm[size],dn,offset,an);\r
+ else sprintf(DisaText,"movep.%c (%s,a%d), d%d",Tasm[size],offset,an,dn);\r
+\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x007c+ ================\r
+static int DisaArithSr(int op)\r
+{\r
+ // Ori/Andi/Eori $nnnn,sr 0000t0tx 0s111100\r
+ char *opcode[6]={"ori","andi","","","","eori"};\r
+ char seat[64]="";\r
+ int type=0,size=0;\r
+\r
+ type=(op>>9)&5;\r
+ size=(op>>6)&1;\r
+\r
+ DisaGetEa(seat,0x3c,size);\r
+ sprintf(DisaText,"%s.%c %s, %s", opcode[type], Tasm[size], seat, size?"sr":"ccr");\r
+\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x0100+ ================\r
+static int DisaBtstReg(int op)\r
+{\r
+ // Btst/Bchg/Bclr/Bset 0000nnn1 tteeeeee (nn=reg number, eeeeee=Dest ea)\r
+ int type=0;\r
+ int sea=0,dea=0;\r
+ char seat[64]="",deat[64]="";\r
+ char *opcode[4]={"btst","bchg","bclr","bset"};\r
+\r
+ sea =(op>>9)&7;\r
+ type=(op>>6)&3;\r
+ dea= op&0x3f;\r
+\r
+ if ((dea&0x38)==0x08) return 1; // movep\r
+ DisaGetEa(seat,sea,0);\r
+ DisaGetEa(deat,dea,0);\r
+\r
+ sprintf(DisaText,"%s %s, %s",opcode[type],seat,deat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x0800+ ================\r
+static int DisaBtstImm(int op)\r
+{\r
+ // Btst/Bchg/Bclr/Bset 00001000 tteeeeee 00 nn (eeeeee=ea, nn=bit number)\r
+ int type=0;\r
+ char seat[64]="",deat[64]="";\r
+ char *opcode[4]={"btst","bchg","bclr","bset"};\r
+\r
+ type=(op>>6)&3;\r
+ DisaGetEa(seat, 0x3c,0);\r
+ DisaGetEa(deat,op&0x3f,0);\r
+\r
+ sprintf(DisaText,"%s %s, %s",opcode[type],seat,deat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x1000+ ================\r
+static int DisaMove(int op)\r
+{\r
+ // Move 00xxdddD DDssssss (xx=size extension, ssssss=Source EA, DDDddd=Dest ea)\r
+ int sea=0,dea=0;\r
+ char inst[64]="",seat[64]="",deat[64]="";\r
+ char *movea="";\r
+ int size=0;\r
+\r
+ if ((op&0x01c0)==0x0040) movea="a"; // See if it's a movea opcode\r
+\r
+ // Find size extension\r
+ switch (op&0x3000)\r
+ {\r
+ case 0x1000: size=0; break;\r
+ case 0x3000: size=1; break;\r
+ case 0x2000: size=2; break;\r
+ default: return 1;\r
+ }\r
+\r
+ sea = op&0x003f;\r
+ DisaGetEa(seat,sea,size);\r
+ \r
+ dea =(op&0x01c0)>>3;\r
+ dea|=(op&0x0e00)>>9;\r
+ DisaGetEa(deat,dea,size);\r
+\r
+ sprintf(inst,"move%s.%c",movea,Tasm[size]);\r
+ sprintf(DisaText,"%s %s, %s",inst,seat,deat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x4000+ ================\r
+static int DisaNeg(int op)\r
+{\r
+ // 01000tt0 xxeeeeee (tt=negx/clr/neg/not, xx=size, eeeeee=EA)\r
+ char eat[64]="";\r
+ int type=0,size=0;\r
+ char *opcode[4]={"negx","clr","neg","not"};\r
+\r
+ type=(op>>9)&3;\r
+ size=(op>>6)&3; if (size>=3) return 1;\r
+ DisaGetEa(eat,op&0x3f,size);\r
+\r
+ sprintf(DisaText,"%s.%c %s",opcode[type],Tasm[size],eat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x40c0+ ================\r
+static int DisaMoveSr(int op)\r
+{\r
+ // 01000tt0 11eeeeee (tt=type, xx=size, eeeeee=EA)\r
+ int type=0,ea=0;\r
+ char eat[64]="";\r
+\r
+ type=(op>>9)&3;\r
+ ea=op&0x3f;\r
+ DisaGetEa(eat,ea,1);\r
+\r
+ switch (type)\r
+ {\r
+ default: sprintf(DisaText,"move sr, %s", eat); break;\r
+ case 1: sprintf(DisaText,"move ccr, %s",eat); break;\r
+ case 2: sprintf(DisaText,"move %s, ccr",eat); break;\r
+ case 3: sprintf(DisaText,"move %s, sr", eat); break;\r
+ }\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x41c0+ ================\r
+static int DisaLea(int op)\r
+{\r
+ // Lea 0100nnn1 11eeeeee (eeeeee=ea)\r
+ int sea=0,dea=0;\r
+ char seat[64]="",deat[64]="";\r
+\r
+ sea=op&0x003f;\r
+ DisaGetEa(seat,sea,0);\r
+\r
+ dea=(op>>9)&7; dea|=8;\r
+ DisaGetEa(deat,dea,2);\r
+\r
+ sprintf(DisaText,"lea %s, %s",seat,deat);\r
+ return 0;\r
+}\r
+\r
+static int MakeRegList(char *list,int mask,int ea)\r
+{\r
+ int reverse=0,i=0,low=0,len=0;\r
+\r
+ if ((ea&0x38)==0x20) reverse=1; // -(An), bitfield is reversed\r
+\r
+ mask&=0xffff; list[0]=0;\r
+\r
+ for (i=0;i<17;i++)\r
+ {\r
+ int bit=0;\r
+ \r
+ // Mask off bit i:\r
+ if (reverse) bit=0x8000>>i; else bit=1<<i;\r
+ bit&=mask;\r
+\r
+ if (bit==0 || i==8)\r
+ {\r
+ // low to i-1 are a continuous section, add it:\r
+ char add[16]="";\r
+ int ad=low&8?'a':'d';\r
+ if (low==i-1) sprintf(add,"%c%d/", ad,low&7);\r
+ if (low< i-1) sprintf(add,"%c%d-%c%d/",ad,low&7, ad,(i-1)&7);\r
+ strcat(list,add);\r
+\r
+ low=i; // Next section\r
+ }\r
+\r
+ if (bit==0) low=i+1;\r
+ }\r
+\r
+ // Knock off trailing '/'\r
+ len=strlen(list);\r
+ if (len>0) if (list[len-1]=='/') list[len-1]=0; \r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x4840+ ================\r
+static int DisaSwap(int op)\r
+{\r
+ // Swap, 01001000 01000nnn swap Dn\r
+ sprintf(DisaText,"swap d%d",op&7);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x4850+ ================\r
+static int DisaPea(int op)\r
+{\r
+ // Pea 01001000 01eeeeee (eeeeee=ea) pea \r
+ int ea=0;\r
+ char eat[64]="";\r
+\r
+ ea=op&0x003f; if (ea<0x10) return 1; // swap opcode\r
+ DisaGetEa(eat,ea,2);\r
+\r
+ sprintf(DisaText,"pea %s",eat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x4880+ ================\r
+static int DisaExt(int op)\r
+{\r
+ // Ext 01001000 1x000nnn (x=size, eeeeee=EA)\r
+ char eat[64]="";\r
+ int size=0;\r
+\r
+ size=(op>>6)&1; size++;\r
+ DisaGetEa(eat,op&0x3f,size);\r
+\r
+ sprintf(DisaText,"ext.%c %s",Tasm[size],eat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x4890+ ================\r
+static int DisaMovem(int op)\r
+{\r
+ // Movem 01001d00 1xeeeeee regmask d=direction, x=size, eeeeee=EA\r
+ int dir=0,size=0;\r
+ int ea=0,mask=0;\r
+ char list[64]="",eat[64]="";\r
+\r
+ dir=(op>>10)&1;\r
+ size=((op>>6)&1)+1;\r
+ ea=op&0x3f; if (ea<0x10) return 1; // ext opcode\r
+\r
+ mask=DisaWord(DisaPc)&0xffff; DisaPc+=2;\r
+\r
+ MakeRegList(list,mask,ea); // Turn register mask into text\r
+ DisaGetEa(eat,ea,size);\r
+\r
+ if (dir) sprintf(DisaText,"movem.%c %s, %s",Tasm[size],eat,list);\r
+ else sprintf(DisaText,"movem.%c %s, %s",Tasm[size],list,eat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x4e40+ ================\r
+static int DisaTrap(int op)\r
+{\r
+ sprintf(DisaText,"trap #%d",op&0xf);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x4e50+ ================\r
+static int DisaLink(int op)\r
+{\r
+ // Link opcode, 01001110 01010nnn dd link An,#offset\r
+ char eat[64]="";\r
+ char offset[32]="";\r
+\r
+ DisaGetEa(eat,(op&7)|8,0);\r
+ GetOffset(offset);\r
+\r
+ sprintf(DisaText,"link %s,#%s",eat,offset);\r
+\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x4e58+ ================\r
+static int DisaUnlk(int op)\r
+{\r
+ // Link opcode, 01001110 01011nnn dd unlk An\r
+ char eat[64]="";\r
+\r
+ DisaGetEa(eat,(op&7)|8,0);\r
+ sprintf(DisaText,"unlk %s",eat);\r
+\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x4e60+ ================\r
+static int DisaMoveUsp(int op)\r
+{\r
+ // Move USP opcode, 01001110 0110dnnn move An to/from USP (d=direction)\r
+ int ea=0,dir=0;\r
+ char eat[64]="";\r
+\r
+ dir=(op>>3)&1;\r
+ ea=(op&7)|8;\r
+ DisaGetEa(eat,ea,0);\r
+\r
+ if (dir) sprintf(DisaText,"move usp, %s",eat);\r
+ else sprintf(DisaText,"move %s, usp",eat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x4e70+ ================\r
+static int Disa4E70(int op)\r
+{\r
+ char *inst[8]={"reset","nop","stop","rte","rtd","rts","trapv","rtr"};\r
+ int n=0;\r
+\r
+ n=op&7;\r
+\r
+ sprintf(DisaText,"%s",inst[n]);\r
+\r
+ //todo - 'stop' with 16 bit data\r
+ \r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x4a00+ ================\r
+static int DisaTst(int op)\r
+{\r
+ // Tst 01001010 xxeeeeee (eeeeee=ea)\r
+ int ea=0;\r
+ char eat[64]="";\r
+ int size=0;\r
+\r
+ ea=op&0x003f;\r
+ DisaGetEa(eat,ea,0);\r
+ size=(op>>6)&3; if (size>=3) return 1;\r
+\r
+ sprintf(DisaText,"tst.%c %s",Tasm[size],eat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x4e80+ ================\r
+static int DisaJsr(int op)\r
+{\r
+ // Jsr/Jmp 0100 1110 1mEE Eeee (eeeeee=ea m=1=jmp)\r
+ int sea=0;\r
+ char seat[64]="";\r
+\r
+ sea=op&0x003f;\r
+ DisaGetEa(seat,sea,0);\r
+\r
+ sprintf(DisaText,"j%s %s", op&0x40?"mp":"sr", seat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x5000+ ================\r
+static int DisaAddq(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
+ char eat[64]="";\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
+ DisaGetEa(eat,ea,size);\r
+\r
+ sprintf(DisaText,"%s.%c #%d, %s",type?"subq":"addq",Tasm[size],num,eat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x50c0+ ================\r
+static int DisaSet(int op)\r
+{\r
+ // 0101cccc 11eeeeee (sxx ea)\r
+ static char *cond[16]=\r
+ {"t" ,"f", "hi","ls","cc","cs","ne","eq",\r
+ "vc","vs","pl","mi","ge","lt","gt","le"};\r
+ char *cc="";\r
+ int ea=0;\r
+ char eat[64]="";\r
+\r
+ cc=cond[(op>>8)&0xf]; // Get condition code\r
+ ea=op&0x3f;\r
+ if ((ea&0x38)==0x08) return 1; // dbra, not scc\r
+\r
+ DisaGetEa(eat,ea,0);\r
+ sprintf(DisaText,"s%s %s",cc,eat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x50c8+ ================\r
+static int DisaDbra(int op)\r
+{\r
+ // 0101cccc 11001nnn offset (dbra/dbxx Rn,offset)\r
+ int dea=0; char deat[64]="";\r
+ int pc=0,Offset=0;\r
+\r
+ static char *BraCode[16]=\r
+ {"bt" ,"bra","bhi","bls","bcc","bcs","bne","beq",\r
+ "bvc","bvs","bpl","bmi","bge","blt","bgt","ble"};\r
+ char *Bra="";\r
+\r
+ dea=op&7;\r
+ DisaGetEa(deat,dea,2);\r
+\r
+ // Get condition code\r
+ Bra=BraCode[(op>>8)&0xf];\r
+\r
+ // Get offset\r
+ pc=DisaPc;\r
+ Offset=(short)DisaWord(DisaPc); DisaPc+=2;\r
+\r
+ sprintf(DisaText,"d%s %s, %x",Bra,deat,pc+Offset);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x6000+ ================\r
+static int DisaBranch(int op)\r
+{\r
+ // Branch 0110cccc nn (cccc=condition)\r
+ int pc=0,Offset=0;\r
+\r
+ static char *BraCode[16]=\r
+ {"bra","bsr","bhi","bls","bcc","bcs","bne","beq",\r
+ "bvc","bvs","bpl","bmi","bge","blt","bgt","ble"};\r
+ char *Bra="";\r
+\r
+ // Get condition code\r
+ Bra=BraCode[(op>>8)&0x0f];\r
+\r
+ // Get offset\r
+ pc=DisaPc;\r
+ Offset=(char)(op&0xff);\r
+ if (Offset== 0) { Offset=(short)DisaWord(DisaPc); DisaPc+=2; }\r
+ else if (Offset==-1) { Offset= DisaLong(DisaPc); DisaPc+=4; }\r
+\r
+ sprintf(DisaText,"%s %x",Bra,pc+Offset);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x7000+ ================\r
+static int DisaMoveq(int op)\r
+{\r
+ // Moveq 0111rrr0 nn (rrr=Dest register, nn=data)\r
+\r
+ int dea=0; char deat[64]="";\r
+ char *inst="moveq";\r
+ int val=0;\r
+\r
+ dea=(op>>9)&7;\r
+ DisaGetEa(deat,dea,2);\r
+\r
+ val=(char)(op&0xff);\r
+ sprintf(DisaText,"%s #$%x, %s",inst,val,deat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x8000+ ================\r
+static int DisaArithReg(int op)\r
+{\r
+ // 1t0tnnnd xxeeeeee (tt=type:or/sub/and/add xx=size, eeeeee=EA)\r
+ int type=0,size=0,dir=0,rea=0,ea=0;\r
+ char reat[64]="",eat[64]="";\r
+ char *opcode[]={"or","sub","","","and","add"};\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 opcode\r
+\r
+ DisaGetEa(reat,rea,size);\r
+ DisaGetEa( eat, ea,size);\r
+\r
+ if (dir) sprintf(DisaText,"%s.%c %s, %s",opcode[type],Tasm[size],reat,eat);\r
+ else sprintf(DisaText,"%s.%c %s, %s",opcode[type],Tasm[size],eat,reat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x8100+ ================\r
+static int DisaAbcd(int op)\r
+{\r
+ // 1t00ddd1 0000asss - sbcd/abcd Ds,Dd or -(As),-(Ad)\r
+ int type=0;\r
+ int dn=0,addr=0,sn=0;\r
+ char *opcode[]={"sbcd","abcd"};\r
+\r
+ type=(op>>14)&1;\r
+ dn =(op>> 9)&7;\r
+ addr=(op>> 3)&1;\r
+ sn = op &7;\r
+\r
+ if (addr) sprintf(DisaText,"%s -(a%d), -(a%d)",opcode[type],sn,dn);\r
+ else sprintf(DisaText,"%s d%d, d%d", opcode[type],sn,dn);\r
+\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x80c0+ ================\r
+static int DisaMul(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,size=1;\r
+ char reat[64]="",eat[64]="";\r
+ char *opcode[2]={"div","mul"};\r
+\r
+ type=(op>>14)&1; // div/mul\r
+ rea =(op>> 9)&7;\r
+ sign=(op>> 8)&1;\r
+ ea = op&0x3f;\r
+\r
+ DisaGetEa(reat,rea,size);\r
+ DisaGetEa( eat, ea,size);\r
+\r
+ sprintf(DisaText,"%s%c.%c %s, %s",opcode[type],sign?'s':'u',Tasm[size],eat,reat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0x90c0+ ================\r
+static int DisaAritha(int op)\r
+{\r
+ // Suba/Cmpa/Adda 1tt1nnnx 11eeeeee (tt=type, x=size, eeeeee=Source EA)\r
+ int type=0,size=0,sea=0,dea=0;\r
+ char seat[64]="",deat[64]="";\r
+ char *aritha[4]={"suba","cmpa","adda",""};\r
+\r
+ type=(op>>13)&3; if (type>=3) return 1;\r
+ size=(op>>8)&1; size++;\r
+ dea =(op>>9)&7; dea|=8; // Dest=An\r
+ sea = op&0x003f; // Source\r
+\r
+ DisaGetEa(seat,sea,size);\r
+ DisaGetEa(deat,dea,size);\r
+\r
+ sprintf(DisaText,"%s.%c %s, %s",aritha[type],Tasm[size],seat,deat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0xb000+ ================\r
+static int DisaCmpEor(int op)\r
+{\r
+ // Cmp/Eor 1011rrrt xxeeeeee (rrr=Dn, t=cmp/eor, xx=size extension, eeeeee=ea)\r
+ char reat[64]="",eat[64]="";\r
+ int type=0,size=0;\r
+\r
+ type=(op>>8)&1;\r
+ size=(op>>6)&3; if (size>=3) return 1; // cmpa opcode\r
+ if ((op&0xf138)==0xb108) return 1; // cmpm opcode\r
+\r
+ DisaGetEa(reat,(op>>9)&7,size);\r
+ DisaGetEa(eat, op&0x3f, size);\r
+\r
+ if (type) sprintf(DisaText,"eor.%c %s, %s",Tasm[size],reat,eat);\r
+ else sprintf(DisaText,"cmp.%c %s, %s",Tasm[size],eat,reat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0xb108+ ================\r
+static int DisaCmpm(int op)\r
+{\r
+ // Cmpm 1011ddd1 xx001sss\r
+ int type=0,size=0,dea=0,sea=0;\r
+ char deat[64]="",seat[64]="";\r
+\r
+ type=(op>>12)&5;\r
+ dea =(op>> 9)&7; dea|=8;\r
+ size=(op>> 6)&3; if (size>=3) return 1;\r
+ sea = op&0x3f;\r
+\r
+ DisaGetEa(deat,dea,size);\r
+ DisaGetEa(seat,sea,size);\r
+\r
+ sprintf(DisaText,"cmpm.%c (%s)+, (%s)+",Tasm[size],seat,deat);\r
+\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0xc140+ ================\r
+// 1100ttt1 01000sss exg ds,dt\r
+// 1100ttt1 01001sss exg as,at\r
+// 1100ttt1 10001sss exg as,dt\r
+static int DisaExg(int op)\r
+{\r
+ int tr=0,type=0,sr=0;\r
+\r
+ tr =(op>>9)&7;\r
+ type= op&0xf8;\r
+ sr = op&7;\r
+\r
+ if (type==0x40) sprintf(DisaText,"exg d%d, d%d",sr,tr);\r
+ else if (type==0x48) sprintf(DisaText,"exg a%d, a%d",sr,tr);\r
+ else if (type==0x88) sprintf(DisaText,"exg a%d, d%d",sr,tr);\r
+ else return 1;\r
+\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0xd100+ ================\r
+static int DisaAddx(int op)\r
+{\r
+ // 1t01ddd1 xx000sss addx\r
+ int type=0,size=0,dea=0,sea=0;\r
+ char deat[64]="",seat[64]="";\r
+ char *opcode[6]={"","subx","","","","addx"};\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
+ DisaGetEa(deat,dea,size);\r
+ DisaGetEa(seat,sea,size);\r
+\r
+ sprintf(DisaText,"%s.%c %s, %s",opcode[type],Tasm[size],seat,deat);\r
+ return 0;\r
+}\r
+\r
+// ================ Opcodes 0xe000+ ================\r
+static char *AsrName[4]={"as","ls","rox","ro"};\r
+static int DisaAsr(int op)\r
+{\r
+ // Asr/l/Ror/l etc - 1110cccd xxuttnnn\r
+ // (ccc=count, d=direction xx=size extension, u=use reg for count, tt=type, nnn=register Dn)\r
+ int count=0,dir=0,size=0,usereg=0,type=0,num=0;\r
+\r
+ count =(op>>9)&7;\r
+ dir =(op>>8)&1;\r
+ size =(op>>6)&3; if (size>=3) return 1; // todo Asr EA\r
+ usereg=(op>>5)&1;\r
+ type =(op>>3)&3;\r
+ num = op &7; // Register number\r
+\r
+ if (usereg==0) count=((count-1)&7)+1; // because ccc=000 means 8\r
+\r
+ sprintf(DisaText,"%s%c.%c %c%d, d%d",\r
+ AsrName[type], dir?'l':'r', Tasm[size],\r
+ usereg?'d':'#', count, num);\r
+ return 0;\r
+}\r
+\r
+static int DisaAsrEa(int op)\r
+{\r
+ // Asr/l/Ror/l etc EA - 11100ttd 11eeeeee \r
+ int type=0,dir=0,size=1;\r
+ char eat[64]="";\r
+\r
+ type=(op>>9)&3;\r
+ dir =(op>>8)&1;\r
+ DisaGetEa(eat,op&0x3f,size);\r
+\r
+ sprintf(DisaText,"%s%c.w %s", AsrName[type], dir?'l':'r', eat);\r
+ return 0;\r
+}\r
+\r
+// =================================================================\r
+\r
+static int TryOp(int op)\r
+{\r
+ if ((op&0xf100)==0x0000) DisaArithImm(op); // Ori/And/Sub/Add/Eor/Cmp Immediate\r
+ if ((op&0xf5bf)==0x003c) DisaArithSr(op); // Ori/Andi/Eori $nnnn,sr\r
+ if ((op&0xf100)==0x0100) DisaBtstReg(op);\r
+ if ((op&0xf138)==0x0108) DisaMovep(op);\r
+ if ((op&0xff00)==0x0800) DisaBtstImm(op); // Btst/Bchg/Bclr/Bset\r
+ if ((op&0xc000)==0x0000) DisaMove(op);\r
+ if ((op&0xf900)==0x4000) DisaNeg(op); // Negx/Clr/Neg/Not\r
+ if ((op&0xf1c0)==0x41c0) DisaLea(op);\r
+ if ((op&0xf9c0)==0x40c0) DisaMoveSr(op);\r
+ if ((op&0xfff8)==0x4840) DisaSwap(op);\r
+ if ((op&0xffc0)==0x4840) DisaPea(op);\r
+ if ((op&0xffb8)==0x4880) DisaExt(op);\r
+ if ((op&0xfb80)==0x4880) DisaMovem(op);\r
+ if ((op&0xff00)==0x4a00) DisaTst(op);\r
+ if ((op&0xfff0)==0x4e40) DisaTrap(op);\r
+ if ((op&0xfff8)==0x4e50) DisaLink(op);\r
+ if ((op&0xfff8)==0x4e58) DisaUnlk(op);\r
+ if ((op&0xfff0)==0x4e60) DisaMoveUsp(op);\r
+ if ((op&0xfff8)==0x4e70) Disa4E70(op);\r
+ if ((op&0xff80)==0x4e80) DisaJsr(op);\r
+ if ((op&0xf000)==0x5000) DisaAddq(op);\r
+ if ((op&0xf0c0)==0x50c0) DisaSet(op);\r
+ if ((op&0xf0f8)==0x50c8) DisaDbra(op);\r
+ if ((op&0xf000)==0x6000) DisaBranch(op);\r
+ if ((op&0xa000)==0x8000) DisaArithReg(op); // Or/Sub/And/Add\r
+ if ((op&0xb1f0)==0x8100) DisaAbcd(op);\r
+ if ((op&0xb130)==0x9100) DisaAddx(op);\r
+ if ((op&0xb0c0)==0x80c0) DisaMul(op);\r
+ if ((op&0xf100)==0x7000) DisaMoveq(op);\r
+ if ((op&0x90c0)==0x90c0) DisaAritha(op);\r
+ if ((op&0xf000)==0xb000) DisaCmpEor(op);\r
+ if ((op&0xf138)==0xb108) DisaCmpm(op);\r
+ if ((op&0xf130)==0xc100) DisaExg(op);\r
+ if ((op&0xf000)==0xe000) DisaAsr(op);\r
+ if ((op&0xf8c0)==0xe0c0) DisaAsrEa(op);\r
+\r
+ // Unknown opcoode\r
+ return 0;\r
+}\r
+\r
+int DisaGet()\r
+{\r
+ int op=0;\r
+ if (DisaWord==NULL) return 1;\r
+\r
+ Comment[0]=0;\r
+ DisaText[0]=0; // Assume opcode unknown\r
+\r
+ op=DisaWord(DisaPc)&0xffff; DisaPc+=2;\r
+ TryOp(op);\r
+ strcat(DisaText,Comment);\r
+\r
+ // Unknown opcoode\r
+ return 0;\r
+}\r