1 /* Disassemble AVR instructions.
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Denis Chertykov <denisc@overta.ru>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 typedef unsigned char u8
;
26 typedef unsigned short u16
;
27 typedef unsigned long u32
;
29 #define IFMASK(a,b) ((opcode & (a)) == (b))
31 static char* SREG_flags
= "CZNVSHTI";
32 static char* sect94
[] = {"COM","NEG","SWAP","INC",0,"ASR","LSR","ROR",
34 static char* sect98
[] = {"CBI","SBIC","SBI","SBIS"};
35 static char* branchs
[] = {
36 "BRCS","BREQ","BRMI","BRVS",
37 "BRLT","BRHS","BRTS","BRIE",
38 "BRCC","BRNE","BRPL","BRVC",
39 "BRGE","BRHC","BRTC","BRID"
42 static char* last4
[] = {"BLD","BST","SBRC","SBRS"};
45 static void dispLDD
PARAMS ((u16
, char *));
48 dispLDD (opcode
, dest
)
52 opcode
= (((opcode
& 0x2000) >> 8) | ((opcode
& 0x0c00) >> 7)
54 sprintf(dest
, "%d", opcode
);
58 static void regPP
PARAMS ((u16
, char *));
65 opcode
= ((opcode
& 0x0600) >> 5) | (opcode
& 0xf);
66 sprintf(dest
, "0x%02X", opcode
);
70 static void reg50
PARAMS ((u16
, char *));
77 opcode
= (opcode
& 0x01f0) >> 4;
78 sprintf(dest
, "R%d", opcode
);
82 static void reg104
PARAMS ((u16
, char *));
89 opcode
= (opcode
& 0xf) | ((opcode
& 0x0200) >> 5);
90 sprintf(dest
, "R%d", opcode
);
94 static void reg40
PARAMS ((u16
, char *));
101 opcode
= (opcode
& 0xf0) >> 4;
102 sprintf(dest
, "R%d", opcode
+ 16);
106 static void reg20w
PARAMS ((u16
, char *));
109 reg20w (opcode
, dest
)
113 opcode
= (opcode
& 0x30) >> 4;
114 sprintf(dest
, "R%d", 24 + opcode
* 2);
118 static void reg_fmul_d
PARAMS ((u16
, char *));
121 reg_fmul_d (opcode
, dest
)
125 sprintf(dest
, "R%d", 16 + ((opcode
>> 4) & 7));
129 static void reg_fmul_r
PARAMS ((u16
, char *));
132 reg_fmul_r (opcode
, dest
)
136 sprintf(dest
, "R%d", 16 + (opcode
& 7));
140 static void reg_muls_d
PARAMS ((u16
, char *));
143 reg_muls_d (opcode
, dest
)
147 sprintf(dest
, "R%d", 16 + ((opcode
>> 4) & 0xf));
151 static void reg_muls_r
PARAMS ((u16
, char *));
154 reg_muls_r (opcode
, dest
)
158 sprintf(dest
, "R%d", 16 + (opcode
& 0xf));
162 static void reg_movw_d
PARAMS ((u16
, char *));
165 reg_movw_d (opcode
, dest
)
169 sprintf(dest
, "R%d", 2 * ((opcode
>> 4) & 0xf));
173 static void reg_movw_r
PARAMS ((u16
, char *));
176 reg_movw_r (opcode
, dest
)
180 sprintf(dest
, "R%d", 2 * (opcode
& 0xf));
184 static void lit404
PARAMS ((u16
, char *));
187 lit404 (opcode
, dest
)
191 opcode
= ((opcode
& 0xf00) >> 4) | (opcode
& 0xf);
192 sprintf(dest
, "0x%02X", opcode
);
196 static void lit204
PARAMS ((u16
, char *));
199 lit204 (opcode
, dest
)
203 opcode
= ((opcode
& 0xc0) >> 2) | (opcode
& 0xf);
204 sprintf(dest
, "0x%02X", opcode
);
208 static void add0fff
PARAMS ((u16
, char *, int));
211 add0fff (op
, dest
, pc
)
216 int rel_addr
= (((op
& 0xfff) ^ 0x800) - 0x800) * 2;
217 sprintf(dest
, ".%+-8d ; 0x%06X", rel_addr
, pc
+ 2 + rel_addr
);
221 static void add03f8
PARAMS ((u16
, char *, int));
224 add03f8 (op
, dest
, pc
)
229 int rel_addr
= ((((op
>> 3) & 0x7f) ^ 0x40) - 0x40) * 2;
230 sprintf(dest
, ".%+-8d ; 0x%06X", rel_addr
, pc
+ 2 + rel_addr
);
234 static u16 avrdis_opcode
PARAMS ((bfd_vma
, disassemble_info
*));
237 avrdis_opcode (addr
, info
)
239 disassemble_info
*info
;
243 status
= info
->read_memory_func(addr
, buffer
, 2, info
);
246 info
->memory_error_func(status
, addr
, info
);
249 return bfd_getl16 (buffer
);
254 print_insn_avr(addr
, info
)
256 disassemble_info
*info
;
261 void *stream
= info
->stream
;
262 fprintf_ftype prin
= info
->fprintf_func
;
265 opcode
= avrdis_opcode (addr
, info
);
267 if (IFMASK(0xd000, 0x8000))
277 (*prin
) (stream
, " STD %c+%s,%s", letter
, rr
, rd
);
279 (*prin
) (stream
, " LDD %s,%c+%s", rd
, letter
, rr
);
283 switch (opcode
& 0xf000)
289 switch (opcode
& 0x0c00)
292 switch (opcode
& 0x0300)
295 (*prin
) (stream
, " NOP");
298 reg_movw_d(opcode
, rd
);
299 reg_movw_r(opcode
, rr
);
300 (*prin
) (stream
, " MOVW %s,%s", rd
, rr
);
303 reg_muls_d(opcode
, rd
);
304 reg_muls_r(opcode
, rr
);
305 (*prin
) (stream
, " MULS %s,%s", rd
, rr
);
308 reg_fmul_d(opcode
, rd
);
309 reg_fmul_r(opcode
, rr
);
311 (*prin
) (stream
, " MULSU %s,%s", rd
, rr
);
312 else if (IFMASK(0x88, 8))
313 (*prin
) (stream
, " FMUL %s,%s", rd
, rr
);
314 else if (IFMASK(0x88, 0x80))
315 (*prin
) (stream
, " FMULS %s,%s", rd
, rr
);
317 (*prin
) (stream
, " FMULSU %s,%s", rd
, rr
);
321 (*prin
) (stream
, " CPC %s,%s", rd
, rr
);
324 (*prin
) (stream
, " SBC %s,%s", rd
, rr
);
327 (*prin
) (stream
, " ADD %s,%s", rd
, rr
);
336 switch (opcode
& 0x0c00)
339 (*prin
) (stream
, " CPSE %s,%s", rd
, rr
);
342 (*prin
) (stream
, " CP %s,%s", rd
, rr
);
345 (*prin
) (stream
, " SUB %s,%s", rd
, rr
);
348 (*prin
) (stream
, " ADC %s,%s", rd
, rr
);
357 switch (opcode
& 0x0c00)
360 (*prin
) (stream
, " AND %s,%s", rd
, rr
);
363 (*prin
) (stream
, " EOR %s,%s", rd
, rr
);
366 (*prin
) (stream
, " OR %s,%s", rd
, rr
);
369 (*prin
) (stream
, " MOV %s,%s", rd
, rr
);
378 (*prin
) (stream
, " CPI %s,%s", rd
, rr
);
385 (*prin
) (stream
, " SBCI %s,%s", rd
, rr
);
392 (*prin
) (stream
, " SUBI %s,%s", rd
, rr
);
399 (*prin
) (stream
, " ORI %s,%s", rd
, rr
);
406 (*prin
) (stream
, " ANDI %s,%s", rd
, rr
);
411 switch (opcode
& 0x0e00)
416 switch (opcode
& 0xf)
420 (*prin
) (stream
, " LDS %s,0x%04X", rd
,
421 avrdis_opcode(addr
+ 2, info
));
426 (*prin
) (stream
, " LD %s,Z+", rd
);
429 (*prin
) (stream
, " LD %s,-Z", rd
);
432 (*prin
) (stream
, " LPM %s,Z", rd
);
435 (*prin
) (stream
, " LPM %s,Z+", rd
);
438 (*prin
) (stream
, " ELPM %s,Z", rd
);
441 (*prin
) (stream
, " ELPM %s,Z+", rd
);
444 (*prin
) (stream
, " LD %s,Y+", rd
);
447 (*prin
) (stream
, " LD %s,-Y", rd
);
450 (*prin
) (stream
, " LD %s,X", rd
);
453 (*prin
) (stream
, " LD %s,X+", rd
);
456 (*prin
) (stream
, " LD %s,-X", rd
);
459 (*prin
) (stream
, " POP %s", rd
);
462 (*prin
) (stream
, " ????");
470 switch (opcode
& 0xf)
474 (*prin
) (stream
, " STS 0x%04X,%s",
475 avrdis_opcode(addr
+ 2, info
), rd
);
480 (*prin
) (stream
, " ST Z+,%s", rd
);
483 (*prin
) (stream
, " ST -Z,%s", rd
);
486 (*prin
) (stream
, " ST Y+,%s", rd
);
489 (*prin
) (stream
, " ST -Y,%s", rd
);
492 (*prin
) (stream
, " ST X,%s", rd
);
495 (*prin
) (stream
, " ST X+,%s", rd
);
498 (*prin
) (stream
, " ST -X,%s", rd
);
501 (*prin
) (stream
, " PUSH %s", rd
);
504 (*prin
) (stream
, " ????");
511 if (IFMASK(0x020c, 0x000c))
513 u32 k
= ((opcode
& 0x01f0) >> 3) | (opcode
& 1);
514 k
= (k
<< 16) | avrdis_opcode(addr
+ 2, info
);
516 (*prin
) (stream
, " CALL 0x%06X", k
*2);
518 (*prin
) (stream
, " JMP 0x%06X", k
*2);
521 else if (IFMASK(0x010f, 0x0008))
523 int sf
= (opcode
& 0x70) >> 4;
525 (*prin
) (stream
, " CL%c", SREG_flags
[sf
]);
527 (*prin
) (stream
, " SE%c", SREG_flags
[sf
]);
529 else if (IFMASK(0x001f, 0x0009))
532 (*prin
) (stream
, " ICALL");
534 (*prin
) (stream
, " IJMP");
536 else if (IFMASK(0x001f, 0x0019))
539 (*prin
) (stream
, " EICALL");
541 (*prin
) (stream
, " EIJMP");
543 else if (IFMASK(0x010f, 0x0108))
545 if (IFMASK(0x0090, 0x0000))
546 (*prin
) (stream
, " RET");
547 else if (IFMASK(0x0090, 0x0010))
548 (*prin
) (stream
, " RETI");
549 else if (IFMASK(0x00e0, 0x0080))
550 (*prin
) (stream
, " SLEEP");
551 else if (IFMASK(0x00e0, 0x00a0))
552 (*prin
) (stream
, " WDR");
553 else if (IFMASK(0x00f0, 0x00c0))
554 (*prin
) (stream
, " LPM");
555 else if (IFMASK(0x00f0, 0x00d0))
556 (*prin
) (stream
, " ELPM");
557 else if (IFMASK(0x00f0, 0x00e0))
558 (*prin
) (stream
, " SPM");
559 else if (IFMASK(0x00f0, 0x00f0))
560 (*prin
) (stream
, " ESPM");
562 (*prin
) (stream
, " ????");
568 p
= sect94
[opcode
& 0xf];
571 (*prin
) (stream
, " %-8s%s", p
, rd
);
582 (*prin
) (stream
, " SBIW %s,%s", rr
, rd
);
584 (*prin
) (stream
, " ADIW %s,%s", rr
, rd
);
591 (*prin
) (stream
, " %-8s0x%02X,%d",
592 sect98
[(opcode
& 0x0300) >> 8],
593 (opcode
& 0xf8) >> 3,
601 (*prin
) (stream
, " MUL %s,%s", rd
, rr
);
611 (*prin
) (stream
, " OUT %s,%s", rr
, rd
);
613 (*prin
) (stream
, " IN %s,%s", rd
, rr
);
618 add0fff(opcode
, rd
, addr
);
619 (*prin
) (stream
, " RJMP %s", rd
);
624 add0fff(opcode
, rd
, addr
);
625 (*prin
) (stream
, " RCALL %s", rd
);
632 (*prin
) (stream
, " LDI %s,%s", rd
, rr
);
640 (*prin
) (stream
, " %-8s%s,%d",
641 last4
[(opcode
& 0x0600) >> 9],
647 add03f8(opcode
, rd
, addr
);
648 p
= branchs
[((opcode
& 0x0400) >> 7) | (opcode
& 7)];
649 (*prin
) (stream
, " %-8s%s", p
, rd
);