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","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"
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 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 rel_addr
= (((op
& 0xfff) ^ 0x800) - 0x800) * 2;
151 sprintf(dest
, ".%+-8d ; 0x%06X", rel_addr
, pc
+ 2 + rel_addr
);
155 static void add03f8
PARAMS ((u16
, char *, int));
158 add03f8 (op
, dest
, pc
)
163 int rel_addr
= ((((op
>> 3) & 0x7f) ^ 0x40) - 0x40) * 2;
164 sprintf(dest
, ".%+-8d ; 0x%06X", rel_addr
, pc
+ 2 + rel_addr
);
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
));
334 (*prin
) (stream
, " LD %s,Z+", rd
);
337 (*prin
) (stream
, " LD %s,-Z", rd
);
340 (*prin
) (stream
, " LD %s,Y+", rd
);
343 (*prin
) (stream
, " LD %s,-Y", rd
);
346 (*prin
) (stream
, " LD %s,X", rd
);
349 (*prin
) (stream
, " LD %s,X+", rd
);
352 (*prin
) (stream
, " LD %s,-X", rd
);
355 (*prin
) (stream
, " POP %s", rd
);
358 (*prin
) (stream
, " ????");
366 switch (opcode
& 0xf)
370 (*prin
) (stream
, " STS 0x%04X,%s",
371 avrdis_opcode(addr
+ 2, info
), rd
);
376 (*prin
) (stream
, " ST Z+,%s", rd
);
379 (*prin
) (stream
, " ST -Z,%s", rd
);
382 (*prin
) (stream
, " ST Y+,%s", rd
);
385 (*prin
) (stream
, " ST -Y,%s", rd
);
388 (*prin
) (stream
, " ST X,%s", rd
);
391 (*prin
) (stream
, " ST X+,%s", rd
);
394 (*prin
) (stream
, " ST -X,%s", rd
);
397 (*prin
) (stream
, " PUSH %s", rd
);
400 (*prin
) (stream
, " ????");
407 if (IFMASK(0x020c, 0x000c))
409 u32 k
= ((opcode
& 0x01f0) >> 3) | (opcode
& 1);
410 k
= (k
<< 16) | avrdis_opcode(addr
+ 2, info
);
412 (*prin
) (stream
, " CALL 0x%06X", k
*2);
414 (*prin
) (stream
, " JMP 0x%06X", k
*2);
417 else if (IFMASK(0x010f, 0x0008))
419 int sf
= (opcode
& 0x70) >> 4;
421 (*prin
) (stream
, " CL%c", SREG_flags
[sf
]);
423 (*prin
) (stream
, " SE%c", SREG_flags
[sf
]);
425 else if (IFMASK(0x000f, 0x0009))
428 (*prin
) (stream
, " ICALL");
430 (*prin
) (stream
, " IJMP");
432 else if (IFMASK(0x010f, 0x0108))
434 if (IFMASK(0x0090, 0x0000))
435 (*prin
) (stream
, " RET");
436 else if (IFMASK(0x0090, 0x0010))
437 (*prin
) (stream
, " RETI");
438 else if (IFMASK(0x00e0, 0x0080))
439 (*prin
) (stream
, " SLEEP");
440 else if (IFMASK(0x00e0, 0x00a0))
441 (*prin
) (stream
, " WDR");
442 else if (IFMASK(0x00f0, 0x00c0))
443 (*prin
) (stream
, " LPM");
444 else if (IFMASK(0x00f0, 0x00d0))
445 (*prin
) (stream
, " ELPM");
447 (*prin
) (stream
, " ????");
453 p
= sect94
[opcode
& 0xf];
456 (*prin
) (stream
, " %-8s%s", p
, rd
);
467 (*prin
) (stream
, " SBIW %s,%s", rr
, rd
);
469 (*prin
) (stream
, " ADIW %s,%s", rr
, rd
);
476 (*prin
) (stream
, " %-8s0x%02X,%d",
477 sect98
[(opcode
& 0x0300) >> 8],
478 (opcode
& 0xf8) >> 3,
486 (*prin
) (stream
, " MUL %s,%s", rd
, rr
);
496 (*prin
) (stream
, " OUT %s,%s", rr
, rd
);
498 (*prin
) (stream
, " IN %s,%s", rd
, rr
);
503 add0fff(opcode
, rd
, addr
);
504 (*prin
) (stream
, " RJMP %s", rd
);
509 add0fff(opcode
, rd
, addr
);
510 (*prin
) (stream
, " RCALL %s", rd
);
517 (*prin
) (stream
, " LDI %s,%s", rd
, rr
);
525 (*prin
) (stream
, " %-8s%s,%d",
526 last4
[(opcode
& 0x0600) >> 9],
532 add03f8(opcode
, rd
, addr
);
533 p
= branchs
[((opcode
& 0x0400) >> 7) | (opcode
& 7)];
534 (*prin
) (stream
, " %-8s%s", p
, rd
);
This page took 0.044086 seconds and 5 git commands to generate.