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. */
24 typedef unsigned char u8;
25 typedef unsigned short u16;
26 typedef unsigned long u32;
28 #define IFMASK(a,b) ((opcode & (a)) == (b))
29 #define CODE_MAX 65537
31 static char* SREG_flags = "CZNVSHTI";
32 static char* sect94[] = {"COM","NEG","SWAP","INC","NULL","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"
41 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 lit404 PARAMS ((u16, char *));
121 lit404 (opcode, dest)
125 opcode = ((opcode & 0xf00) >> 4) | (opcode & 0xf);
126 sprintf(dest, "0x%02X", opcode);
130 static void lit204 PARAMS ((u16, char *));
133 lit204 (opcode, dest)
137 opcode = ((opcode & 0xc0) >> 2) | (opcode & 0xf);
138 sprintf(dest, "0x%02X", opcode);
142 static void add0fff PARAMS ((u16, char *, int));
145 add0fff (op, dest, pc)
150 int opcode = op & 0x0fff;
151 sprintf(dest, ".%+-8d ; 0x%06X", opcode * 2, pc + 2 + opcode * 2);
155 static void add03f8 PARAMS ((u16, char *, int));
158 add03f8 (op, dest, pc)
163 int opcode = (op >> 3) & 0x7f;
164 sprintf(dest, ".%+-8d ; 0x%06X", opcode * 2, pc + 2 + opcode * 2);
168 static u16 avrdis_opcode PARAMS ((bfd_vma, disassemble_info *));
171 avrdis_opcode (addr, info)
173 disassemble_info *info;
177 status = info->read_memory_func(addr, buffer, 2, info);
180 info->memory_error_func(status, addr, info);
183 return bfd_getl16 (buffer);
188 print_insn_avr(addr, info)
190 disassemble_info *info;
195 void *stream = info->stream;
196 fprintf_ftype prin = info->fprintf_func;
199 opcode = avrdis_opcode (addr, info);
201 if (IFMASK(0xd000, 0x8000))
211 (*prin) (stream, " STD %c+%s,%s", letter, rr, rd);
213 (*prin) (stream, " LDD %s,%c+%s", rd, letter, rr);
217 switch (opcode & 0xf000)
223 switch (opcode & 0x0c00)
226 (*prin) (stream, " NOP");
229 (*prin) (stream, " CPC %s,%s", rd, rr);
232 (*prin) (stream, " SBC %s,%s", rd, rr);
235 (*prin) (stream, " ADD %s,%s", rd, rr);
244 switch (opcode & 0x0c00)
247 (*prin) (stream, " CPSE %s,%s", rd, rr);
250 (*prin) (stream, " CP %s,%s", rd, rr);
253 (*prin) (stream, " SUB %s,%s", rd, rr);
256 (*prin) (stream, " ADC %s,%s", rd, rr);
265 switch (opcode & 0x0c00)
268 (*prin) (stream, " AND %s,%s", rd, rr);
271 (*prin) (stream, " EOR %s,%s", rd, rr);
274 (*prin) (stream, " OR %s,%s", rd, rr);
277 (*prin) (stream, " MOV %s,%s", rd, rr);
286 (*prin) (stream, " CPI %s,%s", rd, rr);
293 (*prin) (stream, " SBCI %s,%s", rd, rr);
300 (*prin) (stream, " SUBI %s,%s", rd, rr);
307 (*prin) (stream, " ORI %s,%s", rd, rr);
314 (*prin) (stream, " ANDI %s,%s", rd, rr);
319 switch (opcode & 0x0e00)
324 switch (opcode & 0xf)
328 (*prin) (stream, " LDS %s,0x%04X", rd,
329 avrdis_opcode(addr + 2, info));
335 (*prin) (stream, " LD %s,Z+", rd);
340 (*prin) (stream, " LD %s,-Z", rd);
345 (*prin) (stream, " LD %s,Y+", rd);
350 (*prin) (stream, " LD %s,-Y", rd);
355 (*prin) (stream, " LD %s,X", rd);
360 (*prin) (stream, " LD %s,X+", rd);
365 (*prin) (stream, " LD %s,-X", rd);
370 (*prin) (stream, " POP %s", rd);
375 (*prin) (stream, " ????");
384 switch (opcode & 0xf)
388 (*prin) (stream, " STS 0x%04X,%s",
389 avrdis_opcode(addr + 2, info), rd);
395 (*prin) (stream, " ST Z+,%s", rd);
400 (*prin) (stream, " ST -Z,%s", rd);
405 (*prin) (stream, " ST Y+,%s", rd);
410 (*prin) (stream, " ST -Y,%s", rd);
415 (*prin) (stream, " ST X,%s", rd);
420 (*prin) (stream, " ST X+,%s", rd);
425 (*prin) (stream, " ST -X,%s", rd);
430 (*prin) (stream, " PUSH %s", rd);
435 (*prin) (stream, " ????");
443 if (IFMASK(0x020c, 0x000c))
445 u32 k = ((opcode & 0x01f0) >> 3) | (opcode & 1);
446 k = (k << 16) | avrdis_opcode(addr + 2, info);
448 (*prin) (stream, " CALL 0x%06X", k*2);
450 (*prin) (stream, " JMP 0x%06X", k*2);
453 else if (IFMASK(0x010f, 0x0008))
455 int sf = (opcode & 0x70) >> 4;
457 (*prin) (stream, " CL%c", SREG_flags[sf]);
459 (*prin) (stream, " SE%c", SREG_flags[sf]);
461 else if (IFMASK(0x000f, 0x0009))
464 (*prin) (stream, " ICALL");
466 (*prin) (stream, " IJMP");
468 else if (IFMASK(0x010f, 0x0108))
470 if (IFMASK(0x0090, 0x0000))
471 (*prin) (stream, " RET");
472 else if (IFMASK(0x0090, 0x0010))
473 (*prin) (stream, " RETI");
474 else if (IFMASK(0x00e0, 0x0080))
475 (*prin) (stream, " SLEEP");
476 else if (IFMASK(0x00e0, 0x00a0))
477 (*prin) (stream, " WDR");
478 else if (IFMASK(0x00f0, 0x00c0))
479 (*prin) (stream, " LPM");
480 else if (IFMASK(0x00f0, 0x00d0))
481 (*prin) (stream, " ELPM");
483 (*prin) (stream, " ????");
489 p = sect94[opcode & 0xf];
492 (*prin) (stream, " %-8s%s", p, rd);
503 (*prin) (stream, " SBIW %s,%s", rr, rd);
505 (*prin) (stream, " ADIW %s,%s", rr, rd);
512 (*prin) (stream, " %-8s0x%02X,%d",
513 sect98[(opcode & 0x0300) >> 8],
514 (opcode & 0xf8) >> 3,
522 (*prin) (stream, " MUL %s,%s", rd, rr);
532 (*prin) (stream, " OUT %s,%s", rr, rd);
534 (*prin) (stream, " IN %s,%s", rd, rr);
539 add0fff(opcode, rd, addr);
540 (*prin) (stream, " RJMP %s", rd);
545 add0fff(opcode, rd, addr);
546 (*prin) (stream, " RCALL %s", rd);
553 (*prin) (stream, " LDI %s,%s", rd, rr);
561 (*prin) (stream, " %-8s%s,%d",
562 last4[(opcode & 0x0600) >> 9],
568 add03f8(opcode, rd, addr);
569 p = branchs[((opcode & 0x0400) >> 7) | (opcode & 7)];
570 (*prin) (stream, " %-8s%s", p, rd);