Disa.c 22 KB

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