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