Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. // This file is part of the PicoDrive Megadrive Emulator
  3.  
  4. // Copyright (c) 2011 FinalDave (emudave (at) gmail.com)
  5.  
  6. // This code is licensed under the GNU General Public License version 2.0 and the MAME License.
  7. // You can choose the license that has the most advantages for you.
  8.  
  9. // SVN repository can be found at http://code.google.com/p/cyclone68000/
  10.  
  11. // Disa 68000 Disassembler
  12. #ifndef __GNUC__
  13. #pragma warning(disable:4115)
  14. #endif
  15.  
  16. #define _CRT_SECURE_NO_WARNINGS
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "Disa.h"
  20.  
  21. unsigned int DisaPc=0;
  22. char *DisaText=NULL; // Text buffer to write in
  23. static char Tasm[]="bwl?";
  24. static char Comment[64]="";
  25. unsigned short (CPU_CALL *DisaWord)(unsigned int a)=NULL;
  26.  
  27. static unsigned int DisaLong(unsigned int a)
  28. {
  29.   unsigned int d=0;
  30.   if (DisaWord==NULL) return d;
  31.  
  32.   d= DisaWord(a)<<16;
  33.   d|=DisaWord(a+2)&0xffff;
  34.   return d;
  35. }
  36.  
  37. // Get text version of the effective address
  38. int DisaGetEa(char *t,int ea,int size)
  39. {
  40.   ea&=0x3f; t[0]=0;
  41.   if ((ea&0x38)==0x00) { sprintf(t,"d%d",ea  ); return 0; }    // 000rrr
  42.   if ((ea&0x38)==0x08) { sprintf(t,"a%d",ea&7); return 0; }    // 001rrr
  43.   if ((ea&0x38)==0x10) { sprintf(t,"(a%d)",ea&7); return 0; }  // 010rrr
  44.   if ((ea&0x38)==0x18) { sprintf(t,"(a%d)+",ea&7); return 0; } // 011rrr
  45.   if ((ea&0x38)==0x20) { sprintf(t,"-(a%d)",ea&7); return 0; } // 100rrr
  46.   if ((ea&0x38)==0x28) { sprintf(t,"($%x,a%d)",DisaWord(DisaPc)&0xffff,ea&7); DisaPc+=2; return 0; } // 101rrr
  47.  
  48.   if ((ea&0x38)==0x30)
  49.   {
  50.     // 110nnn - An + Disp + D/An
  51.     int areg=0,ext=0,off=0,da=0,reg=0,wol=0,scale=0;
  52.     ext=DisaWord(DisaPc)&0xffff;
  53.    
  54.     areg=ea&7;
  55.     off=ext&0xff;    da =ext&0x8000?'a':'d';
  56.     reg=(ext>>12)&7; wol=ext&0x0800?'l':'w';
  57.     scale=1<<((ext>>9)&3);
  58.  
  59.     if (scale<2) sprintf(t,"($%x,a%d,%c%d.%c)",   off,areg,da,reg,wol);
  60.     else         sprintf(t,"($%x,a%d,%c%d.%c*%d)",off,areg,da,reg,wol,scale); // 68020
  61.  
  62.     DisaPc+=2;
  63.     return 0;
  64.   }
  65.  
  66.   if (ea==0x38) { sprintf(t,"$%x.w",DisaWord(DisaPc)&0xffff); DisaPc+=2; return 0; } // 111000 - Absolute short
  67.   if (ea==0x39) { sprintf(t,"$%x.l",DisaLong(DisaPc));        DisaPc+=4; return 0; } // 111001 - Absolute long
  68.  
  69.   if (ea==0x3a)
  70.   {
  71.     // 111010 - PC Relative
  72.     int ext=DisaWord(DisaPc)&0xffff;
  73.     sprintf(t,"($%x,pc)",ext);
  74.     sprintf(Comment,"; =%x",DisaPc+(short)ext); // Comment where pc+ext is
  75.     DisaPc+=2;
  76.     return 0;
  77.   }
  78.  
  79.   if (ea==0x3b)
  80.   {
  81.     // 111011 - PC Relative + D/An
  82.     int ext=0,off=0,da=0,reg=0,wol=0,scale=0;
  83.     ext=DisaWord(DisaPc)&0xffff;
  84.    
  85.     off=ext&0xff;    da =ext&0x8000?'a':'d';
  86.     reg=(ext>>12)&7; wol=ext&0x0800?'l':'w';
  87.     scale=1<<((ext>>9)&3);
  88.  
  89.     if (scale<2) sprintf(t,"($%x,pc,%c%d.%c)",   off,da,reg,wol);
  90.     else         sprintf(t,"($%x,pc,%c%d.%c*%d)",off,da,reg,wol,scale); // 68020
  91.  
  92.     sprintf(Comment,"; =%x",DisaPc+(char)off); // Comment where pc+ext is
  93.     DisaPc+=2;
  94.     return 0;
  95.   }
  96.  
  97.   if (ea==0x3c)
  98.   {
  99.     // 111100 - Immediate
  100.     switch (size)
  101.     {
  102.       case 0: sprintf(t,"#$%x",DisaWord(DisaPc)&0x00ff); DisaPc+=2; return 0;
  103.       case 1: sprintf(t,"#$%x",DisaWord(DisaPc)&0xffff); DisaPc+=2; return 0;
  104.       case 2: sprintf(t,"#$%x",DisaLong(DisaPc)       ); DisaPc+=4; return 0;
  105.     }
  106.     return 1;
  107.   }
  108.  
  109. // Unknown effective address
  110.   sprintf(t,"ea=(%d%d%d %d%d%d)",
  111.     (ea>>5)&1,(ea>>4)&1,(ea>>3)&1,
  112.     (ea>>2)&1,(ea>>1)&1, ea    &1);
  113.   return 1;
  114. }
  115.  
  116. static void GetOffset(char *text)
  117. {
  118.   int off=(short)DisaWord(DisaPc); DisaPc+=2;
  119.  
  120.   if (off<0) sprintf(text,"-$%x",-off);
  121.   else       sprintf(text,"$%x",  off);
  122. }
  123.  
  124. // ================ Opcodes 0x0000+ ================
  125. static int DisaArithImm(int op)
  126. {
  127.   // Or/And/Sub/Add/Eor/Cmp Immediate 0000ttt0 xxDDDddd (tt=type, xx=size extension, DDDddd=Dest ea)
  128.   int dea=0;
  129.   char seat[64]="",deat[64]="";
  130.   int type=0,size=0;
  131.   char *arith[8]={"or","and","sub","add","?","eor","cmp","?"};
  132.  
  133.   type=(op>>9)&7; if (type==4 || type>=7) return 1;
  134.   size=(op>>6)&3; if (size>=3) return 1;
  135.   dea=op&0x3f; if (dea==0x3c) return 1;
  136.  
  137.   DisaGetEa(seat,0x3c,size);
  138.   DisaGetEa(deat,dea, size);
  139.  
  140.   sprintf(DisaText,"%si.%c %s, %s",arith[type],Tasm[size],seat,deat);
  141.   return 0;
  142. }
  143.  
  144. // ================ Opcodes 0x0108+ ================
  145. static int DisaMovep(int op)
  146. {
  147.   // movep.x (Aa),Dn - 0000nnn1 dx001aaa  nn
  148.   int dn=0,dir=0,size=0,an=0;
  149.   char offset[32]="";
  150.  
  151.   dn  =(op>>9)&7;
  152.   dir =(op>>7)&1;
  153.   size=(op>>6)&1; size++;
  154.   an  = op    &7;
  155.  
  156.   GetOffset(offset);
  157.   if (dir) sprintf(DisaText,"movep.%c d%d, (%s,a%d)",Tasm[size],dn,offset,an);
  158.   else     sprintf(DisaText,"movep.%c (%s,a%d), d%d",Tasm[size],offset,an,dn);
  159.  
  160.   return 0;
  161. }
  162.  
  163. // ================ Opcodes 0x007c+ ================
  164. static int DisaArithSr(int op)
  165. {
  166.   // Ori/Andi/Eori $nnnn,sr 0000t0tx 0s111100
  167.   char *opcode[6]={"ori","andi","","","","eori"};
  168.   char seat[64]="";
  169.   int type=0,size=0;
  170.  
  171.   type=(op>>9)&5;
  172.   size=(op>>6)&1;
  173.  
  174.   DisaGetEa(seat,0x3c,size);
  175.   sprintf(DisaText,"%s.%c %s, %s", opcode[type], Tasm[size], seat, size?"sr":"ccr");
  176.  
  177.   return 0;
  178. }
  179.  
  180. // ================ Opcodes 0x0100+ ================
  181. static int DisaBtstReg(int op)
  182. {
  183.   // Btst/Bchg/Bclr/Bset 0000nnn1 tteeeeee (nn=reg number, eeeeee=Dest ea)
  184.   int type=0;
  185.   int sea=0,dea=0;
  186.   char seat[64]="",deat[64]="";
  187.   char *opcode[4]={"btst","bchg","bclr","bset"};
  188.  
  189.   sea =(op>>9)&7;
  190.   type=(op>>6)&3;
  191.   dea=  op&0x3f;
  192.  
  193.   if ((dea&0x38)==0x08) return 1; // movep
  194.   DisaGetEa(seat,sea,0);
  195.   DisaGetEa(deat,dea,0);
  196.  
  197.   sprintf(DisaText,"%s %s, %s",opcode[type],seat,deat);
  198.   return 0;
  199. }
  200.  
  201. // ================ Opcodes 0x0800+ ================
  202. static int DisaBtstImm(int op)
  203. {
  204.   // Btst/Bchg/Bclr/Bset 00001000 tteeeeee 00 nn (eeeeee=ea, nn=bit number)
  205.   int type=0;
  206.   char seat[64]="",deat[64]="";
  207.   char *opcode[4]={"btst","bchg","bclr","bset"};
  208.  
  209.   type=(op>>6)&3;
  210.   DisaGetEa(seat,   0x3c,0);
  211.   DisaGetEa(deat,op&0x3f,0);
  212.  
  213.   sprintf(DisaText,"%s %s, %s",opcode[type],seat,deat);
  214.   return 0;
  215. }
  216.  
  217. // ================ Opcodes 0x1000+ ================
  218. static int DisaMove(int op)
  219. {
  220.   // Move 00xxdddD DDssssss (xx=size extension, ssssss=Source EA, DDDddd=Dest ea)
  221.   int sea=0,dea=0;
  222.   char inst[64]="",seat[64]="",deat[64]="";
  223.   char *movea="";
  224.   int size=0;
  225.  
  226.   if ((op&0x01c0)==0x0040) movea="a"; // See if it's a movea opcode
  227.  
  228.   // Find size extension
  229.   switch (op&0x3000)
  230.   {
  231.     case 0x1000: size=0; break;
  232.     case 0x3000: size=1; break;
  233.     case 0x2000: size=2; break;
  234.     default: return 1;
  235.   }
  236.  
  237.   sea = op&0x003f;
  238.   DisaGetEa(seat,sea,size);
  239.  
  240.   dea =(op&0x01c0)>>3;
  241.   dea|=(op&0x0e00)>>9;
  242.   DisaGetEa(deat,dea,size);
  243.  
  244.   sprintf(inst,"move%s.%c",movea,Tasm[size]);
  245.   sprintf(DisaText,"%s %s, %s",inst,seat,deat);
  246.   return 0;
  247. }
  248.  
  249. // ================ Opcodes 0x4000+ ================
  250. static int DisaNeg(int op)
  251. {
  252.   // 01000tt0 xxeeeeee (tt=negx/clr/neg/not, xx=size, eeeeee=EA)
  253.   char eat[64]="";
  254.   int type=0,size=0;
  255.   char *opcode[4]={"negx","clr","neg","not"};
  256.  
  257.   type=(op>>9)&3;
  258.   size=(op>>6)&3; if (size>=3) return 1;
  259.   DisaGetEa(eat,op&0x3f,size);
  260.  
  261.   sprintf(DisaText,"%s.%c %s",opcode[type],Tasm[size],eat);
  262.   return 0;
  263. }
  264.  
  265. // ================ Opcodes 0x40c0+ ================
  266. static int DisaMoveSr(int op)
  267. {
  268.   // 01000tt0 11eeeeee (tt=type, xx=size, eeeeee=EA)
  269.   int type=0,ea=0;
  270.   char eat[64]="";
  271.  
  272.   type=(op>>9)&3;
  273.   ea=op&0x3f;
  274.   DisaGetEa(eat,ea,1);
  275.  
  276.   switch (type)
  277.   {
  278.     default: sprintf(DisaText,"move sr, %s", eat); break;
  279.     case 1:  sprintf(DisaText,"move ccr, %s",eat); break;
  280.     case 2:  sprintf(DisaText,"move %s, ccr",eat); break;
  281.     case 3:  sprintf(DisaText,"move %s, sr", eat); break;
  282.   }
  283.   return 0;
  284. }
  285.  
  286. // ================ Opcodes 0x41c0+ ================
  287. static int DisaLea(int op)
  288. {
  289.   // Lea 0100nnn1 11eeeeee (eeeeee=ea)
  290.   int sea=0,dea=0;
  291.   char seat[64]="",deat[64]="";
  292.  
  293.   sea=op&0x003f;
  294.   DisaGetEa(seat,sea,0);
  295.  
  296.   dea=(op>>9)&7; dea|=8;
  297.   DisaGetEa(deat,dea,2);
  298.  
  299.   sprintf(DisaText,"lea %s, %s",seat,deat);
  300.   return 0;
  301. }
  302.  
  303. static int MakeRegList(char *list,int mask,int ea)
  304. {
  305.   int reverse=0,i=0,low=0,len=0;
  306.  
  307.   if ((ea&0x38)==0x20) reverse=1; // -(An), bitfield is reversed
  308.  
  309.   mask&=0xffff; list[0]=0;
  310.  
  311.   for (i=0;i<17;i++)
  312.   {
  313.     int bit=0;
  314.    
  315.     // Mask off bit i:
  316.     if (reverse) bit=0x8000>>i; else bit=1<<i;
  317.     bit&=mask;
  318.  
  319.     if (bit==0 || i==8)
  320.     {
  321.       // low to i-1 are a continuous section, add it:
  322.       char add[16]="";
  323.       int ad=low&8?'a':'d';
  324.       if (low==i-1) sprintf(add,"%c%d/",     ad,low&7);
  325.       if (low< i-1) sprintf(add,"%c%d-%c%d/",ad,low&7, ad,(i-1)&7);
  326.       strcat(list,add);
  327.  
  328.       low=i; // Next section
  329.     }
  330.  
  331.     if (bit==0) low=i+1;
  332.   }
  333.  
  334.   // Knock off trailing '/'
  335.   len=strlen(list);
  336.   if (len>0) if (list[len-1]=='/') list[len-1]=0;
  337.   return 0;
  338. }
  339.  
  340. // ================ Opcodes 0x4840+ ================
  341. static int DisaSwap(int op)
  342. {
  343.   // Swap, 01001000 01000nnn swap Dn
  344.   sprintf(DisaText,"swap d%d",op&7);
  345.   return 0;
  346. }
  347.  
  348. // ================ Opcodes 0x4850+ ================
  349. static int DisaPea(int op)
  350. {
  351.   // Pea 01001000 01eeeeee  (eeeeee=ea)  pea
  352.   int ea=0;
  353.   char eat[64]="";
  354.  
  355.   ea=op&0x003f; if (ea<0x10) return 1; // swap opcode
  356.   DisaGetEa(eat,ea,2);
  357.  
  358.   sprintf(DisaText,"pea %s",eat);
  359.   return 0;
  360. }
  361.  
  362. // ================ Opcodes 0x4880+ ================
  363. static int DisaExt(int op)
  364. {
  365.   // Ext 01001000 1x000nnn (x=size, eeeeee=EA)
  366.   char eat[64]="";
  367.   int size=0;
  368.  
  369.   size=(op>>6)&1; size++;
  370.   DisaGetEa(eat,op&0x3f,size);
  371.  
  372.   sprintf(DisaText,"ext.%c %s",Tasm[size],eat);
  373.   return 0;
  374. }
  375.  
  376. // ================ Opcodes 0x4890+ ================
  377. static int DisaMovem(int op)
  378. {
  379.   // Movem 01001d00 1xeeeeee regmask  d=direction, x=size, eeeeee=EA
  380.   int dir=0,size=0;
  381.   int ea=0,mask=0;
  382.   char list[64]="",eat[64]="";
  383.  
  384.   dir=(op>>10)&1;
  385.   size=((op>>6)&1)+1;
  386.   ea=op&0x3f; if (ea<0x10) return 1; // ext opcode
  387.  
  388.   mask=DisaWord(DisaPc)&0xffff; DisaPc+=2;
  389.  
  390.   MakeRegList(list,mask,ea); // Turn register mask into text
  391.   DisaGetEa(eat,ea,size);
  392.  
  393.   if (dir) sprintf(DisaText,"movem.%c %s, %s",Tasm[size],eat,list);
  394.   else     sprintf(DisaText,"movem.%c %s, %s",Tasm[size],list,eat);
  395.   return 0;
  396. }
  397.  
  398. // ================ Opcodes 0x4e40+ ================
  399. static int DisaTrap(int op)
  400. {
  401.   sprintf(DisaText,"trap #%d",op&0xf);
  402.   return 0;
  403. }
  404.  
  405. // ================ Opcodes 0x4e50+ ================
  406. static int DisaLink(int op)
  407. {
  408.   // Link opcode, 01001110 01010nnn dd   link An,#offset
  409.   char eat[64]="";
  410.   char offset[32]="";
  411.  
  412.   DisaGetEa(eat,(op&7)|8,0);
  413.   GetOffset(offset);
  414.  
  415.   sprintf(DisaText,"link %s,#%s",eat,offset);
  416.  
  417.   return 0;
  418. }
  419.  
  420. // ================ Opcodes 0x4e58+ ================
  421. static int DisaUnlk(int op)
  422. {
  423.   // Link opcode, 01001110 01011nnn dd   unlk An
  424.   char eat[64]="";
  425.  
  426.   DisaGetEa(eat,(op&7)|8,0);
  427.   sprintf(DisaText,"unlk %s",eat);
  428.  
  429.   return 0;
  430. }
  431.  
  432. // ================ Opcodes 0x4e60+ ================
  433. static int DisaMoveUsp(int op)
  434. {
  435.   // Move USP opcode, 01001110 0110dnnn move An to/from USP (d=direction)
  436.   int ea=0,dir=0;
  437.   char eat[64]="";
  438.  
  439.   dir=(op>>3)&1;
  440.   ea=(op&7)|8;
  441.   DisaGetEa(eat,ea,0);
  442.  
  443.   if (dir) sprintf(DisaText,"move usp, %s",eat);
  444.   else     sprintf(DisaText,"move %s, usp",eat);
  445.   return 0;
  446. }
  447.  
  448. // ================ Opcodes 0x4e70+ ================
  449. static int Disa4E70(int op)
  450. {
  451.   char *inst[8]={"reset","nop","stop","rte","rtd","rts","trapv","rtr"};
  452.   int n=0;
  453.  
  454.   n=op&7;
  455.  
  456.   sprintf(DisaText,"%s",inst[n]);
  457.  
  458.   //todo - 'stop' with 16 bit data
  459.  
  460.   return 0;
  461. }
  462.  
  463. // ================ Opcodes 0x4a00+ ================
  464. static int DisaTst(int op)
  465. {
  466.   // Tst 01001010 xxeeeeee  (eeeeee=ea)
  467.   int ea=0;
  468.   char eat[64]="";
  469.   int size=0;
  470.  
  471.   ea=op&0x003f;
  472.   DisaGetEa(eat,ea,0);
  473.   size=(op>>6)&3; if (size>=3) return 1;
  474.  
  475.   sprintf(DisaText,"tst.%c %s",Tasm[size],eat);
  476.   return 0;
  477. }
  478.  
  479. // ================ Opcodes 0x4e80+ ================
  480. static int DisaJsr(int op)
  481. {
  482.   // Jsr/Jmp 0100 1110 1mEE Eeee (eeeeee=ea m=1=jmp)
  483.   int sea=0;
  484.   char seat[64]="";
  485.  
  486.   sea=op&0x003f;
  487.   DisaGetEa(seat,sea,0);
  488.  
  489.   sprintf(DisaText,"j%s %s", op&0x40?"mp":"sr", seat);
  490.   return 0;
  491. }
  492.  
  493. // ================ Opcodes 0x5000+ ================
  494. static int DisaAddq(int op)
  495. {
  496.   // 0101nnnt xxeeeeee (nnn=#8,1-7 t=addq/subq xx=size, eeeeee=EA)
  497.   int num=0,type=0,size=0,ea=0;
  498.   char eat[64]="";
  499.  
  500.   num =(op>>9)&7; if (num==0) num=8;
  501.   type=(op>>8)&1;
  502.   size=(op>>6)&3; if (size>=3) return 1;
  503.   ea  = op&0x3f;
  504.  
  505.   DisaGetEa(eat,ea,size);
  506.  
  507.   sprintf(DisaText,"%s.%c #%d, %s",type?"subq":"addq",Tasm[size],num,eat);
  508.   return 0;
  509. }
  510.  
  511. // ================ Opcodes 0x50c0+ ================
  512. static int DisaSet(int op)
  513. {
  514.   // 0101cccc 11eeeeee (sxx ea)
  515.   static char *cond[16]=
  516.   {"t" ,"f", "hi","ls","cc","cs","ne","eq",
  517.    "vc","vs","pl","mi","ge","lt","gt","le"};
  518.   char *cc="";
  519.   int ea=0;
  520.   char eat[64]="";
  521.  
  522.   cc=cond[(op>>8)&0xf]; // Get condition code
  523.   ea=op&0x3f;
  524.   if ((ea&0x38)==0x08) return 1; // dbra, not scc
  525.  
  526.   DisaGetEa(eat,ea,0);
  527.   sprintf(DisaText,"s%s %s",cc,eat);
  528.   return 0;
  529. }
  530.  
  531. // ================ Opcodes 0x50c8+ ================
  532. static int DisaDbra(int op)
  533. {
  534.   // 0101cccc 11001nnn offset  (dbra/dbxx Rn,offset)
  535.   int dea=0; char deat[64]="";
  536.   int pc=0,Offset=0;
  537.  
  538.   static char *BraCode[16]=
  539.   {"bt" ,"bra","bhi","bls","bcc","bcs","bne","beq",
  540.    "bvc","bvs","bpl","bmi","bge","blt","bgt","ble"};
  541.   char *Bra="";
  542.  
  543.   dea=op&7;
  544.   DisaGetEa(deat,dea,2);
  545.  
  546.   // Get condition code
  547.   Bra=BraCode[(op>>8)&0xf];
  548.  
  549.   // Get offset
  550.   pc=DisaPc;
  551.   Offset=(short)DisaWord(DisaPc); DisaPc+=2;
  552.  
  553.   sprintf(DisaText,"d%s %s, %x",Bra,deat,pc+Offset);
  554.   return 0;
  555. }
  556.  
  557. // ================ Opcodes 0x6000+ ================
  558. static int DisaBranch(int op)
  559. {
  560.   // Branch 0110cccc nn  (cccc=condition)
  561.   int pc=0,Offset=0;
  562.  
  563.   static char *BraCode[16]=
  564.   {"bra","bsr","bhi","bls","bcc","bcs","bne","beq",
  565.    "bvc","bvs","bpl","bmi","bge","blt","bgt","ble"};
  566.   char *Bra="";
  567.  
  568.   // Get condition code
  569.   Bra=BraCode[(op>>8)&0x0f];
  570.  
  571.   // Get offset
  572.   pc=DisaPc;
  573.   Offset=(char)(op&0xff);
  574.        if (Offset== 0) { Offset=(short)DisaWord(DisaPc); DisaPc+=2; }
  575.   else if (Offset==-1) { Offset=       DisaLong(DisaPc); DisaPc+=4; }
  576.  
  577.   sprintf(DisaText,"%s %x",Bra,pc+Offset);
  578.   return 0;
  579. }
  580.  
  581. // ================ Opcodes 0x7000+ ================
  582. static int DisaMoveq(int op)
  583. {
  584.   // Moveq 0111rrr0 nn (rrr=Dest register, nn=data)
  585.  
  586.   int dea=0; char deat[64]="";
  587.   char *inst="moveq";
  588.   int val=0;
  589.  
  590.   dea=(op>>9)&7;
  591.   DisaGetEa(deat,dea,2);
  592.  
  593.   val=(char)(op&0xff);
  594.   sprintf(DisaText,"%s #$%x, %s",inst,val,deat);
  595.   return 0;
  596. }
  597.  
  598. // ================ Opcodes 0x8000+ ================
  599. static int DisaArithReg(int op)
  600. {
  601.   // 1t0tnnnd xxeeeeee (tt=type:or/sub/and/add xx=size, eeeeee=EA)
  602.   int type=0,size=0,dir=0,rea=0,ea=0;
  603.   char reat[64]="",eat[64]="";
  604.   char *opcode[]={"or","sub","","","and","add"};
  605.  
  606.   type=(op>>12)&5;
  607.   rea =(op>> 9)&7;
  608.   dir =(op>> 8)&1;
  609.   size=(op>> 6)&3; if (size>=3) return 1;
  610.   ea  = op&0x3f;
  611.  
  612.   if (dir && ea<0x10) return 1; // addx opcode
  613.  
  614.   DisaGetEa(reat,rea,size);
  615.   DisaGetEa( eat, ea,size);
  616.  
  617.   if (dir) sprintf(DisaText,"%s.%c %s, %s",opcode[type],Tasm[size],reat,eat);
  618.   else     sprintf(DisaText,"%s.%c %s, %s",opcode[type],Tasm[size],eat,reat);
  619.   return 0;
  620. }
  621.  
  622. // ================ Opcodes 0x8100+ ================
  623. static int DisaAbcd(int op)
  624. {
  625.   // 1t00ddd1 0000asss - sbcd/abcd Ds,Dd or -(As),-(Ad)
  626.   int type=0;
  627.   int dn=0,addr=0,sn=0;
  628.   char *opcode[]={"sbcd","abcd"};
  629.  
  630.   type=(op>>14)&1;
  631.   dn  =(op>> 9)&7;
  632.   addr=(op>> 3)&1;
  633.   sn  = op     &7;
  634.  
  635.   if (addr) sprintf(DisaText,"%s -(a%d), -(a%d)",opcode[type],sn,dn);
  636.   else      sprintf(DisaText,"%s d%d, d%d",       opcode[type],sn,dn);
  637.  
  638.   return 0;
  639. }
  640.  
  641. // ================ Opcodes 0x80c0+ ================
  642. static int DisaMul(int op)
  643. {
  644.   // Div/Mul: 1m00nnns 11eeeeee (m=Mul, nnn=Register Dn, s=signed, eeeeee=EA)
  645.   int type=0,rea=0,sign=0,ea=0,size=1;
  646.   char reat[64]="",eat[64]="";
  647.   char *opcode[2]={"div","mul"};
  648.  
  649.   type=(op>>14)&1; // div/mul
  650.   rea =(op>> 9)&7;
  651.   sign=(op>> 8)&1;
  652.   ea  = op&0x3f;
  653.  
  654.   DisaGetEa(reat,rea,size);
  655.   DisaGetEa( eat, ea,size);
  656.  
  657.   sprintf(DisaText,"%s%c.%c %s, %s",opcode[type],sign?'s':'u',Tasm[size],eat,reat);
  658.   return 0;
  659. }
  660.  
  661. // ================ Opcodes 0x90c0+ ================
  662. static int DisaAritha(int op)
  663. {
  664.   // Suba/Cmpa/Adda 1tt1nnnx 11eeeeee (tt=type, x=size, eeeeee=Source EA)
  665.   int type=0,size=0,sea=0,dea=0;
  666.   char seat[64]="",deat[64]="";
  667.   char *aritha[4]={"suba","cmpa","adda",""};
  668.  
  669.   type=(op>>13)&3; if (type>=3) return 1;
  670.   size=(op>>8)&1; size++;
  671.   dea =(op>>9)&7; dea|=8; // Dest=An
  672.   sea = op&0x003f; // Source
  673.  
  674.   DisaGetEa(seat,sea,size);
  675.   DisaGetEa(deat,dea,size);
  676.  
  677.   sprintf(DisaText,"%s.%c %s, %s",aritha[type],Tasm[size],seat,deat);
  678.   return 0;
  679. }
  680.  
  681. // ================ Opcodes 0xb000+ ================
  682. static int DisaCmpEor(int op)
  683. {
  684.   // Cmp/Eor 1011rrrt xxeeeeee (rrr=Dn, t=cmp/eor, xx=size extension, eeeeee=ea)
  685.   char reat[64]="",eat[64]="";
  686.   int type=0,size=0;
  687.  
  688.   type=(op>>8)&1;
  689.   size=(op>>6)&3; if (size>=3) return 1; // cmpa opcode
  690.   if ((op&0xf138)==0xb108) return 1; // cmpm opcode
  691.  
  692.   DisaGetEa(reat,(op>>9)&7,size);
  693.   DisaGetEa(eat,  op&0x3f, size);
  694.  
  695.   if (type) sprintf(DisaText,"eor.%c %s, %s",Tasm[size],reat,eat);
  696.   else      sprintf(DisaText,"cmp.%c %s, %s",Tasm[size],eat,reat);
  697.   return 0;
  698. }
  699.  
  700. // ================ Opcodes 0xb108+ ================
  701. static int DisaCmpm(int op)
  702. {
  703.   // Cmpm  1011ddd1 xx001sss
  704.   int type=0,size=0,dea=0,sea=0;
  705.   char deat[64]="",seat[64]="";
  706.  
  707.   type=(op>>12)&5;
  708.   (void)type;
  709.   dea =(op>> 9)&7; dea|=8;
  710.   size=(op>> 6)&3; if (size>=3) return 1;
  711.   sea  = op&0x3f;
  712.  
  713.   DisaGetEa(deat,dea,size);
  714.   DisaGetEa(seat,sea,size);
  715.  
  716.   sprintf(DisaText,"cmpm.%c (%s)+, (%s)+",Tasm[size],seat,deat);
  717.  
  718.   return 0;
  719. }
  720.  
  721. // ================ Opcodes 0xc140+ ================
  722. // 1100ttt1 01000sss  exg ds,dt
  723. // 1100ttt1 01001sss  exg as,at
  724. // 1100ttt1 10001sss  exg as,dt
  725. static int DisaExg(int op)
  726. {
  727.   int tr=0,type=0,sr=0;
  728.  
  729.   tr  =(op>>9)&7;
  730.   type= op&0xf8;
  731.   sr  = op&7;
  732.  
  733.        if (type==0x40) sprintf(DisaText,"exg d%d, d%d",sr,tr);
  734.   else if (type==0x48) sprintf(DisaText,"exg a%d, a%d",sr,tr);
  735.   else if (type==0x88) sprintf(DisaText,"exg a%d, d%d",sr,tr);
  736.   else return 1;
  737.  
  738.   return 0;
  739. }
  740.  
  741. // ================ Opcodes 0xd100+ ================
  742. static int DisaAddx(int op)
  743. {
  744.   // 1t01ddd1 xx000sss addx
  745.   int type=0,size=0,dea=0,sea=0;
  746.   char deat[64]="",seat[64]="";
  747.   char *opcode[6]={"","subx","","","","addx"};
  748.  
  749.   type=(op>>12)&5;
  750.   dea =(op>> 9)&7;
  751.   size=(op>> 6)&3; if (size>=3) return 1;
  752.   sea  = op&0x3f;
  753.  
  754.   DisaGetEa(deat,dea,size);
  755.   DisaGetEa(seat,sea,size);
  756.  
  757.   sprintf(DisaText,"%s.%c %s, %s",opcode[type],Tasm[size],seat,deat);
  758.   return 0;
  759. }
  760.  
  761. // ================ Opcodes 0xe000+ ================
  762. static char *AsrName[4]={"as","ls","rox","ro"};
  763. static int DisaAsr(int op)
  764. {
  765.   // Asr/l/Ror/l etc - 1110cccd xxuttnnn
  766.   // (ccc=count, d=direction xx=size extension, u=use reg for count, tt=type, nnn=register Dn)
  767.   int count=0,dir=0,size=0,usereg=0,type=0,num=0;
  768.  
  769.   count =(op>>9)&7;
  770.   dir   =(op>>8)&1;
  771.   size  =(op>>6)&3; if (size>=3) return 1; // todo Asr EA
  772.   usereg=(op>>5)&1;
  773.   type  =(op>>3)&3;
  774.   num   = op    &7; // Register number
  775.  
  776.   if (usereg==0) count=((count-1)&7)+1; // because ccc=000 means 8
  777.  
  778.   sprintf(DisaText,"%s%c.%c %c%d, d%d",
  779.     AsrName[type], dir?'l':'r', Tasm[size],
  780.     usereg?'d':'#', count, num);
  781.   return 0;
  782. }
  783.  
  784. static int DisaAsrEa(int op)
  785. {
  786.   // Asr/l/Ror/l etc EA - 11100ttd 11eeeeee
  787.   int type=0,dir=0,size=1;
  788.   char eat[64]="";
  789.  
  790.   type=(op>>9)&3;
  791.   dir =(op>>8)&1;
  792.   DisaGetEa(eat,op&0x3f,size);
  793.  
  794.   sprintf(DisaText,"%s%c.w %s", AsrName[type], dir?'l':'r', eat);
  795.   return 0;
  796. }
  797.  
  798. // =================================================================
  799.  
  800. static int TryOp(int op)
  801. {
  802.   if ((op&0xf100)==0x0000) DisaArithImm(op); // Ori/And/Sub/Add/Eor/Cmp Immediate
  803.   if ((op&0xf5bf)==0x003c) DisaArithSr(op); // Ori/Andi/Eori $nnnn,sr
  804.   if ((op&0xf100)==0x0100) DisaBtstReg(op);
  805.   if ((op&0xf138)==0x0108) DisaMovep(op);
  806.   if ((op&0xff00)==0x0800) DisaBtstImm(op); // Btst/Bchg/Bclr/Bset
  807.   if ((op&0xc000)==0x0000) DisaMove(op);
  808.   if ((op&0xf900)==0x4000) DisaNeg(op); // Negx/Clr/Neg/Not
  809.   if ((op&0xf1c0)==0x41c0) DisaLea(op);
  810.   if ((op&0xf9c0)==0x40c0) DisaMoveSr(op);
  811.   if ((op&0xfff8)==0x4840) DisaSwap(op);
  812.   if ((op&0xffc0)==0x4840) DisaPea(op);
  813.   if ((op&0xffb8)==0x4880) DisaExt(op);
  814.   if ((op&0xfb80)==0x4880) DisaMovem(op);
  815.   if ((op&0xff00)==0x4a00) DisaTst(op);
  816.   if ((op&0xfff0)==0x4e40) DisaTrap(op);
  817.   if ((op&0xfff8)==0x4e50) DisaLink(op);
  818.   if ((op&0xfff8)==0x4e58) DisaUnlk(op);
  819.   if ((op&0xfff0)==0x4e60) DisaMoveUsp(op);
  820.   if ((op&0xfff8)==0x4e70) Disa4E70(op);
  821.   if ((op&0xff80)==0x4e80) DisaJsr(op);
  822.   if ((op&0xf000)==0x5000) DisaAddq(op);
  823.   if ((op&0xf0c0)==0x50c0) DisaSet(op);
  824.   if ((op&0xf0f8)==0x50c8) DisaDbra(op);
  825.   if ((op&0xf000)==0x6000) DisaBranch(op);
  826.   if ((op&0xa000)==0x8000) DisaArithReg(op); // Or/Sub/And/Add
  827.   if ((op&0xb1f0)==0x8100) DisaAbcd(op);
  828.   if ((op&0xb130)==0x9100) DisaAddx(op);
  829.   if ((op&0xb0c0)==0x80c0) DisaMul(op);
  830.   if ((op&0xf100)==0x7000) DisaMoveq(op);
  831.   if ((op&0x90c0)==0x90c0) DisaAritha(op);
  832.   if ((op&0xf000)==0xb000) DisaCmpEor(op);
  833.   if ((op&0xf138)==0xb108) DisaCmpm(op);
  834.   if ((op&0xf130)==0xc100) DisaExg(op);
  835.   if ((op&0xf000)==0xe000) DisaAsr(op);
  836.   if ((op&0xf8c0)==0xe0c0) DisaAsrEa(op);
  837.  
  838.   // Unknown opcoode
  839.   return 0;
  840. }
  841.  
  842. int DisaGet()
  843. {
  844.   int op=0;
  845.   if (DisaWord==NULL) return 1;
  846.  
  847.   Comment[0]=0;
  848.   DisaText[0]=0; // Assume opcode unknown
  849.  
  850.   op=DisaWord(DisaPc)&0xffff; DisaPc+=2;
  851.   TryOp(op);
  852.   strcat(DisaText,Comment);
  853.  
  854.   // Unknown opcoode
  855.   return 0;
  856. }
  857.