]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/avr-tdep.c
2003-06-11 Andrew Cagney <cagney@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / avr-tdep.c
1 /* Target-dependent code for Atmel AVR, for GDB.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* Contributed by Theodore A. Roth, troth@openavr.org */
23
24 /* Portions of this file were taken from the original gdb-4.18 patch developed
25 by Denis Chertykov, denisc@overta.ru */
26
27 #include "defs.h"
28 #include "gdbcmd.h"
29 #include "gdbcore.h"
30 #include "inferior.h"
31 #include "symfile.h"
32 #include "arch-utils.h"
33 #include "regcache.h"
34 #include "gdb_string.h"
35
36 /* AVR Background:
37
38 (AVR micros are pure Harvard Architecture processors.)
39
40 The AVR family of microcontrollers have three distinctly different memory
41 spaces: flash, sram and eeprom. The flash is 16 bits wide and is used for
42 the most part to store program instructions. The sram is 8 bits wide and is
43 used for the stack and the heap. Some devices lack sram and some can have
44 an additional external sram added on as a peripheral.
45
46 The eeprom is 8 bits wide and is used to store data when the device is
47 powered down. Eeprom is not directly accessible, it can only be accessed
48 via io-registers using a special algorithm. Accessing eeprom via gdb's
49 remote serial protocol ('m' or 'M' packets) looks difficult to do and is
50 not included at this time.
51
52 [The eeprom could be read manually via ``x/b <eaddr + AVR_EMEM_START>'' or
53 written using ``set {unsigned char}<eaddr + AVR_EMEM_START>''. For this to
54 work, the remote target must be able to handle eeprom accesses and perform
55 the address translation.]
56
57 All three memory spaces have physical addresses beginning at 0x0. In
58 addition, the flash is addressed by gcc/binutils/gdb with respect to 8 bit
59 bytes instead of the 16 bit wide words used by the real device for the
60 Program Counter.
61
62 In order for remote targets to work correctly, extra bits must be added to
63 addresses before they are send to the target or received from the target
64 via the remote serial protocol. The extra bits are the MSBs and are used to
65 decode which memory space the address is referring to. */
66
67 #undef XMALLOC
68 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
69
70 #undef EXTRACT_INSN
71 #define EXTRACT_INSN(addr) extract_unsigned_integer(addr,2)
72
73 /* Constants: prefixed with AVR_ to avoid name space clashes */
74
75 enum
76 {
77 AVR_REG_W = 24,
78 AVR_REG_X = 26,
79 AVR_REG_Y = 28,
80 AVR_FP_REGNUM = 28,
81 AVR_REG_Z = 30,
82
83 AVR_SREG_REGNUM = 32,
84 AVR_SP_REGNUM = 33,
85 AVR_PC_REGNUM = 34,
86
87 AVR_NUM_REGS = 32 + 1 /*SREG*/ + 1 /*SP*/ + 1 /*PC*/,
88 AVR_NUM_REG_BYTES = 32 + 1 /*SREG*/ + 2 /*SP*/ + 4 /*PC*/,
89
90 AVR_PC_REG_INDEX = 35, /* index into array of registers */
91
92 AVR_MAX_PROLOGUE_SIZE = 56, /* bytes */
93
94 /* Count of pushed registers. From r2 to r17 (inclusively), r28, r29 */
95 AVR_MAX_PUSHES = 18,
96
97 /* Number of the last pushed register. r17 for current avr-gcc */
98 AVR_LAST_PUSHED_REGNUM = 17,
99
100 /* FIXME: TRoth/2002-01-??: Can we shift all these memory masks left 8
101 bits? Do these have to match the bfd vma values?. It sure would make
102 things easier in the future if they didn't need to match.
103
104 Note: I chose these values so as to be consistent with bfd vma
105 addresses.
106
107 TRoth/2002-04-08: There is already a conflict with very large programs
108 in the mega128. The mega128 has 128K instruction bytes (64K words),
109 thus the Most Significant Bit is 0x10000 which gets masked off my
110 AVR_MEM_MASK.
111
112 The problem manifests itself when trying to set a breakpoint in a
113 function which resides in the upper half of the instruction space and
114 thus requires a 17-bit address.
115
116 For now, I've just removed the EEPROM mask and changed AVR_MEM_MASK
117 from 0x00ff0000 to 0x00f00000. Eeprom is not accessible from gdb yet,
118 but could be for some remote targets by just adding the correct offset
119 to the address and letting the remote target handle the low-level
120 details of actually accessing the eeprom. */
121
122 AVR_IMEM_START = 0x00000000, /* INSN memory */
123 AVR_SMEM_START = 0x00800000, /* SRAM memory */
124 #if 1
125 /* No eeprom mask defined */
126 AVR_MEM_MASK = 0x00f00000, /* mask to determine memory space */
127 #else
128 AVR_EMEM_START = 0x00810000, /* EEPROM memory */
129 AVR_MEM_MASK = 0x00ff0000, /* mask to determine memory space */
130 #endif
131 };
132
133 /* Any function with a frame looks like this
134 ....... <-SP POINTS HERE
135 LOCALS1 <-FP POINTS HERE
136 LOCALS0
137 SAVED FP
138 SAVED R3
139 SAVED R2
140 RET PC
141 FIRST ARG
142 SECOND ARG */
143
144 struct frame_extra_info
145 {
146 CORE_ADDR return_pc;
147 CORE_ADDR args_pointer;
148 int locals_size;
149 int framereg;
150 int framesize;
151 int is_main;
152 };
153
154 struct gdbarch_tdep
155 {
156 /* FIXME: TRoth: is there anything to put here? */
157 int foo;
158 };
159
160 /* Lookup the name of a register given it's number. */
161
162 static const char *
163 avr_register_name (int regnum)
164 {
165 static char *register_names[] = {
166 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
167 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
168 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
169 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
170 "SREG", "SP", "PC"
171 };
172 if (regnum < 0)
173 return NULL;
174 if (regnum >= (sizeof (register_names) / sizeof (*register_names)))
175 return NULL;
176 return register_names[regnum];
177 }
178
179 /* Index within `registers' of the first byte of the space for
180 register REGNUM. */
181
182 static int
183 avr_register_byte (int regnum)
184 {
185 if (regnum < AVR_PC_REGNUM)
186 return regnum;
187 else
188 return AVR_PC_REG_INDEX;
189 }
190
191 /* Number of bytes of storage in the actual machine representation for
192 register REGNUM. */
193
194 static int
195 avr_register_raw_size (int regnum)
196 {
197 switch (regnum)
198 {
199 case AVR_PC_REGNUM:
200 return 4;
201 case AVR_SP_REGNUM:
202 case AVR_FP_REGNUM:
203 return 2;
204 default:
205 return 1;
206 }
207 }
208
209 /* Number of bytes of storage in the program's representation
210 for register N. */
211
212 static int
213 avr_register_virtual_size (int regnum)
214 {
215 return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (regnum));
216 }
217
218 /* Return the GDB type object for the "standard" data type
219 of data in register N. */
220
221 static struct type *
222 avr_register_virtual_type (int regnum)
223 {
224 switch (regnum)
225 {
226 case AVR_PC_REGNUM:
227 return builtin_type_unsigned_long;
228 case AVR_SP_REGNUM:
229 return builtin_type_unsigned_short;
230 default:
231 return builtin_type_unsigned_char;
232 }
233 }
234
235 /* Instruction address checks and convertions. */
236
237 static CORE_ADDR
238 avr_make_iaddr (CORE_ADDR x)
239 {
240 return ((x) | AVR_IMEM_START);
241 }
242
243 static int
244 avr_iaddr_p (CORE_ADDR x)
245 {
246 return (((x) & AVR_MEM_MASK) == AVR_IMEM_START);
247 }
248
249 /* FIXME: TRoth: Really need to use a larger mask for instructions. Some
250 devices are already up to 128KBytes of flash space.
251
252 TRoth/2002-04-8: See comment above where AVR_IMEM_START is defined. */
253
254 static CORE_ADDR
255 avr_convert_iaddr_to_raw (CORE_ADDR x)
256 {
257 return ((x) & 0xffffffff);
258 }
259
260 /* SRAM address checks and convertions. */
261
262 static CORE_ADDR
263 avr_make_saddr (CORE_ADDR x)
264 {
265 return ((x) | AVR_SMEM_START);
266 }
267
268 static int
269 avr_saddr_p (CORE_ADDR x)
270 {
271 return (((x) & AVR_MEM_MASK) == AVR_SMEM_START);
272 }
273
274 static CORE_ADDR
275 avr_convert_saddr_to_raw (CORE_ADDR x)
276 {
277 return ((x) & 0xffffffff);
278 }
279
280 /* EEPROM address checks and convertions. I don't know if these will ever
281 actually be used, but I've added them just the same. TRoth */
282
283 /* TRoth/2002-04-08: Commented out for now to allow fix for problem with large
284 programs in the mega128. */
285
286 /* static CORE_ADDR */
287 /* avr_make_eaddr (CORE_ADDR x) */
288 /* { */
289 /* return ((x) | AVR_EMEM_START); */
290 /* } */
291
292 /* static int */
293 /* avr_eaddr_p (CORE_ADDR x) */
294 /* { */
295 /* return (((x) & AVR_MEM_MASK) == AVR_EMEM_START); */
296 /* } */
297
298 /* static CORE_ADDR */
299 /* avr_convert_eaddr_to_raw (CORE_ADDR x) */
300 /* { */
301 /* return ((x) & 0xffffffff); */
302 /* } */
303
304 /* Convert from address to pointer and vice-versa. */
305
306 static void
307 avr_address_to_pointer (struct type *type, void *buf, CORE_ADDR addr)
308 {
309 /* Is it a code address? */
310 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC
311 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD)
312 {
313 store_unsigned_integer (buf, TYPE_LENGTH (type),
314 avr_convert_iaddr_to_raw (addr));
315 }
316 else
317 {
318 /* Strip off any upper segment bits. */
319 store_unsigned_integer (buf, TYPE_LENGTH (type),
320 avr_convert_saddr_to_raw (addr));
321 }
322 }
323
324 static CORE_ADDR
325 avr_pointer_to_address (struct type *type, const void *buf)
326 {
327 CORE_ADDR addr = extract_unsigned_integer (buf, TYPE_LENGTH (type));
328
329 if (TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type)))
330 {
331 fprintf_unfiltered (gdb_stderr, "CODE_SPACE ---->> ptr->addr: 0x%lx\n",
332 addr);
333 fprintf_unfiltered (gdb_stderr,
334 "+++ If you see this, please send me an email <troth@openavr.org>\n");
335 }
336
337 /* Is it a code address? */
338 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC
339 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD
340 || TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type)))
341 return avr_make_iaddr (addr);
342 else
343 return avr_make_saddr (addr);
344 }
345
346 static CORE_ADDR
347 avr_read_pc (ptid_t ptid)
348 {
349 ptid_t save_ptid;
350 CORE_ADDR pc;
351 CORE_ADDR retval;
352
353 save_ptid = inferior_ptid;
354 inferior_ptid = ptid;
355 pc = (int) read_register (AVR_PC_REGNUM);
356 inferior_ptid = save_ptid;
357 retval = avr_make_iaddr (pc);
358 return retval;
359 }
360
361 static void
362 avr_write_pc (CORE_ADDR val, ptid_t ptid)
363 {
364 ptid_t save_ptid;
365
366 save_ptid = inferior_ptid;
367 inferior_ptid = ptid;
368 write_register (AVR_PC_REGNUM, avr_convert_iaddr_to_raw (val));
369 inferior_ptid = save_ptid;
370 }
371
372 static CORE_ADDR
373 avr_read_sp (void)
374 {
375 return (avr_make_saddr (read_register (AVR_SP_REGNUM)));
376 }
377
378 static void
379 avr_write_sp (CORE_ADDR val)
380 {
381 write_register (AVR_SP_REGNUM, avr_convert_saddr_to_raw (val));
382 }
383
384 static CORE_ADDR
385 avr_read_fp (void)
386 {
387 return (avr_make_saddr (read_register (AVR_FP_REGNUM)));
388 }
389
390 /* Translate a GDB virtual ADDR/LEN into a format the remote target
391 understands. Returns number of bytes that can be transfered
392 starting at TARG_ADDR. Return ZERO if no bytes can be transfered
393 (segmentation fault).
394
395 TRoth/2002-04-08: Could this be used to check for dereferencing an invalid
396 pointer? */
397
398 static void
399 avr_remote_translate_xfer_address (struct gdbarch *gdbarch,
400 struct regcache *regcache,
401 CORE_ADDR memaddr, int nr_bytes,
402 CORE_ADDR *targ_addr, int *targ_len)
403 {
404 long out_addr;
405 long out_len;
406
407 /* FIXME: TRoth: Do nothing for now. Will need to examine memaddr at this
408 point and see if the high bit are set with the masks that we want. */
409
410 *targ_addr = memaddr;
411 *targ_len = nr_bytes;
412 }
413
414 /* Function pointers obtained from the target are half of what gdb expects so
415 multiply by 2. */
416
417 static CORE_ADDR
418 avr_convert_from_func_ptr_addr (CORE_ADDR addr)
419 {
420 return addr * 2;
421 }
422
423 /* avr_scan_prologue is also used as the
424 deprecated_frame_init_saved_regs().
425
426 Put here the code to store, into fi->saved_regs, the addresses of
427 the saved registers of frame described by FRAME_INFO. This
428 includes special registers such as pc and fp saved in special ways
429 in the stack frame. sp is even more special: the address we return
430 for it IS the sp for the next frame. */
431
432 /* Function: avr_scan_prologue (helper function for avr_init_extra_frame_info)
433 This function decodes a AVR function prologue to determine:
434 1) the size of the stack frame
435 2) which registers are saved on it
436 3) the offsets of saved regs
437 This information is stored in the "extra_info" field of the frame_info.
438
439 A typical AVR function prologue might look like this:
440 push rXX
441 push r28
442 push r29
443 in r28,__SP_L__
444 in r29,__SP_H__
445 sbiw r28,<LOCALS_SIZE>
446 in __tmp_reg__,__SREG__
447 cli
448 out __SP_L__,r28
449 out __SREG__,__tmp_reg__
450 out __SP_H__,r29
451
452 A `-mcall-prologues' prologue look like this:
453 ldi r26,<LOCALS_SIZE>
454 ldi r27,<LOCALS_SIZE>/265
455 ldi r30,pm_lo8(.L_foo_body)
456 ldi r31,pm_hi8(.L_foo_body)
457 rjmp __prologue_saves__+RRR
458 .L_foo_body: */
459
460 static void
461 avr_scan_prologue (struct frame_info *fi)
462 {
463 CORE_ADDR prologue_start;
464 CORE_ADDR prologue_end;
465 int i;
466 unsigned short insn;
467 int regno;
468 int scan_stage = 0;
469 char *name;
470 struct minimal_symbol *msymbol;
471 int prologue_len;
472 unsigned char prologue[AVR_MAX_PROLOGUE_SIZE];
473 int vpc = 0;
474
475 get_frame_extra_info (fi)->framereg = AVR_SP_REGNUM;
476
477 if (find_pc_partial_function
478 (get_frame_pc (fi), &name, &prologue_start, &prologue_end))
479 {
480 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
481
482 if (sal.line == 0) /* no line info, use current PC */
483 prologue_end = get_frame_pc (fi);
484 else if (sal.end < prologue_end) /* next line begins after fn end */
485 prologue_end = sal.end; /* (probably means no prologue) */
486 }
487 else
488 /* We're in the boondocks: allow for */
489 /* 19 pushes, an add, and "mv fp,sp" */
490 prologue_end = prologue_start + AVR_MAX_PROLOGUE_SIZE;
491
492 prologue_end = min (prologue_end, get_frame_pc (fi));
493
494 /* Search the prologue looking for instructions that set up the
495 frame pointer, adjust the stack pointer, and save registers. */
496
497 get_frame_extra_info (fi)->framesize = 0;
498 prologue_len = prologue_end - prologue_start;
499 read_memory (prologue_start, prologue, prologue_len);
500
501 /* Scanning main()'s prologue
502 ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>)
503 ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>)
504 out __SP_H__,r29
505 out __SP_L__,r28 */
506
507 if (name && strcmp ("main", name) == 0 && prologue_len == 8)
508 {
509 CORE_ADDR locals;
510 unsigned char img[] = {
511 0xde, 0xbf, /* out __SP_H__,r29 */
512 0xcd, 0xbf /* out __SP_L__,r28 */
513 };
514
515 get_frame_extra_info (fi)->framereg = AVR_FP_REGNUM;
516 insn = EXTRACT_INSN (&prologue[vpc]);
517 /* ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>) */
518 if ((insn & 0xf0f0) == 0xe0c0)
519 {
520 locals = (insn & 0xf) | ((insn & 0x0f00) >> 4);
521 insn = EXTRACT_INSN (&prologue[vpc + 2]);
522 /* ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>) */
523 if ((insn & 0xf0f0) == 0xe0d0)
524 {
525 locals |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
526 if (memcmp (prologue + vpc + 4, img, sizeof (img)) == 0)
527 {
528 deprecated_update_frame_base_hack (fi, locals);
529
530 get_frame_extra_info (fi)->is_main = 1;
531 return;
532 }
533 }
534 }
535 }
536
537 /* Scanning `-mcall-prologues' prologue
538 FIXME: mega prologue have a 12 bytes long */
539
540 while (prologue_len <= 12) /* I'm use while to avoit many goto's */
541 {
542 int loc_size;
543 int body_addr;
544 unsigned num_pushes;
545
546 insn = EXTRACT_INSN (&prologue[vpc]);
547 /* ldi r26,<LOCALS_SIZE> */
548 if ((insn & 0xf0f0) != 0xe0a0)
549 break;
550 loc_size = (insn & 0xf) | ((insn & 0x0f00) >> 4);
551
552 insn = EXTRACT_INSN (&prologue[vpc + 2]);
553 /* ldi r27,<LOCALS_SIZE> / 256 */
554 if ((insn & 0xf0f0) != 0xe0b0)
555 break;
556 loc_size |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
557
558 insn = EXTRACT_INSN (&prologue[vpc + 4]);
559 /* ldi r30,pm_lo8(.L_foo_body) */
560 if ((insn & 0xf0f0) != 0xe0e0)
561 break;
562 body_addr = (insn & 0xf) | ((insn & 0x0f00) >> 4);
563
564 insn = EXTRACT_INSN (&prologue[vpc + 6]);
565 /* ldi r31,pm_hi8(.L_foo_body) */
566 if ((insn & 0xf0f0) != 0xe0f0)
567 break;
568 body_addr |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
569
570 if (body_addr != (prologue_start + 10) / 2)
571 break;
572
573 msymbol = lookup_minimal_symbol ("__prologue_saves__", NULL, NULL);
574 if (!msymbol)
575 break;
576
577 /* FIXME: prologue for mega have a JMP instead of RJMP */
578 insn = EXTRACT_INSN (&prologue[vpc + 8]);
579 /* rjmp __prologue_saves__+RRR */
580 if ((insn & 0xf000) != 0xc000)
581 break;
582
583 /* Extract PC relative offset from RJMP */
584 i = (insn & 0xfff) | (insn & 0x800 ? (-1 ^ 0xfff) : 0);
585 /* Convert offset to byte addressable mode */
586 i *= 2;
587 /* Destination address */
588 i += vpc + prologue_start + 10;
589 /* Resovle offset (in words) from __prologue_saves__ symbol.
590 Which is a pushes count in `-mcall-prologues' mode */
591 num_pushes = AVR_MAX_PUSHES - (i - SYMBOL_VALUE_ADDRESS (msymbol)) / 2;
592
593 if (num_pushes > AVR_MAX_PUSHES)
594 num_pushes = 0;
595
596 if (num_pushes)
597 {
598 int from;
599 get_frame_saved_regs (fi)[AVR_FP_REGNUM + 1] = num_pushes;
600 if (num_pushes >= 2)
601 get_frame_saved_regs (fi)[AVR_FP_REGNUM] = num_pushes - 1;
602 i = 0;
603 for (from = AVR_LAST_PUSHED_REGNUM + 1 - (num_pushes - 2);
604 from <= AVR_LAST_PUSHED_REGNUM; ++from)
605 get_frame_saved_regs (fi)[from] = ++i;
606 }
607 get_frame_extra_info (fi)->locals_size = loc_size;
608 get_frame_extra_info (fi)->framesize = loc_size + num_pushes;
609 get_frame_extra_info (fi)->framereg = AVR_FP_REGNUM;
610 return;
611 }
612
613 /* Scan interrupt or signal function */
614
615 if (prologue_len >= 12)
616 {
617 unsigned char img[] = {
618 0x78, 0x94, /* sei */
619 0x1f, 0x92, /* push r1 */
620 0x0f, 0x92, /* push r0 */
621 0x0f, 0xb6, /* in r0,0x3f SREG */
622 0x0f, 0x92, /* push r0 */
623 0x11, 0x24 /* clr r1 */
624 };
625 if (memcmp (prologue, img, sizeof (img)) == 0)
626 {
627 vpc += sizeof (img);
628 get_frame_saved_regs (fi)[0] = 2;
629 get_frame_saved_regs (fi)[1] = 1;
630 get_frame_extra_info (fi)->framesize += 3;
631 }
632 else if (memcmp (img + 1, prologue, sizeof (img) - 1) == 0)
633 {
634 vpc += sizeof (img) - 1;
635 get_frame_saved_regs (fi)[0] = 2;
636 get_frame_saved_regs (fi)[1] = 1;
637 get_frame_extra_info (fi)->framesize += 3;
638 }
639 }
640
641 /* First stage of the prologue scanning.
642 Scan pushes */
643
644 for (; vpc <= prologue_len; vpc += 2)
645 {
646 insn = EXTRACT_INSN (&prologue[vpc]);
647 if ((insn & 0xfe0f) == 0x920f) /* push rXX */
648 {
649 /* Bits 4-9 contain a mask for registers R0-R32. */
650 regno = (insn & 0x1f0) >> 4;
651 ++get_frame_extra_info (fi)->framesize;
652 get_frame_saved_regs (fi)[regno] = get_frame_extra_info (fi)->framesize;
653 scan_stage = 1;
654 }
655 else
656 break;
657 }
658
659 /* Second stage of the prologue scanning.
660 Scan:
661 in r28,__SP_L__
662 in r29,__SP_H__ */
663
664 if (scan_stage == 1 && vpc + 4 <= prologue_len)
665 {
666 unsigned char img[] = {
667 0xcd, 0xb7, /* in r28,__SP_L__ */
668 0xde, 0xb7 /* in r29,__SP_H__ */
669 };
670 unsigned short insn1;
671
672 if (memcmp (prologue + vpc, img, sizeof (img)) == 0)
673 {
674 vpc += 4;
675 get_frame_extra_info (fi)->framereg = AVR_FP_REGNUM;
676 scan_stage = 2;
677 }
678 }
679
680 /* Third stage of the prologue scanning. (Really two stages)
681 Scan for:
682 sbiw r28,XX or subi r28,lo8(XX)
683 sbci r29,hi8(XX)
684 in __tmp_reg__,__SREG__
685 cli
686 out __SP_L__,r28
687 out __SREG__,__tmp_reg__
688 out __SP_H__,r29 */
689
690 if (scan_stage == 2 && vpc + 12 <= prologue_len)
691 {
692 int locals_size = 0;
693 unsigned char img[] = {
694 0x0f, 0xb6, /* in r0,0x3f */
695 0xf8, 0x94, /* cli */
696 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
697 0x0f, 0xbe, /* out 0x3f,r0 ; SREG */
698 0xde, 0xbf /* out 0x3e,r29 ; SPH */
699 };
700 unsigned char img_sig[] = {
701 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
702 0xde, 0xbf /* out 0x3e,r29 ; SPH */
703 };
704 unsigned char img_int[] = {
705 0xf8, 0x94, /* cli */
706 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
707 0x78, 0x94, /* sei */
708 0xde, 0xbf /* out 0x3e,r29 ; SPH */
709 };
710
711 insn = EXTRACT_INSN (&prologue[vpc]);
712 vpc += 2;
713 if ((insn & 0xff30) == 0x9720) /* sbiw r28,XXX */
714 locals_size = (insn & 0xf) | ((insn & 0xc0) >> 2);
715 else if ((insn & 0xf0f0) == 0x50c0) /* subi r28,lo8(XX) */
716 {
717 locals_size = (insn & 0xf) | ((insn & 0xf00) >> 4);
718 insn = EXTRACT_INSN (&prologue[vpc]);
719 vpc += 2;
720 locals_size += ((insn & 0xf) | ((insn & 0xf00) >> 4) << 8);
721 }
722 else
723 return;
724 get_frame_extra_info (fi)->locals_size = locals_size;
725 get_frame_extra_info (fi)->framesize += locals_size;
726 }
727 }
728
729 /* This function actually figures out the frame address for a given pc and
730 sp. This is tricky because we sometimes don't use an explicit
731 frame pointer, and the previous stack pointer isn't necessarily recorded
732 on the stack. The only reliable way to get this info is to
733 examine the prologue. */
734
735 static void
736 avr_init_extra_frame_info (int fromleaf, struct frame_info *fi)
737 {
738 int reg;
739
740 if (get_next_frame (fi))
741 deprecated_update_frame_pc_hack (fi, DEPRECATED_FRAME_SAVED_PC (get_next_frame (fi)));
742
743 frame_extra_info_zalloc (fi, sizeof (struct frame_extra_info));
744 frame_saved_regs_zalloc (fi);
745
746 get_frame_extra_info (fi)->return_pc = 0;
747 get_frame_extra_info (fi)->args_pointer = 0;
748 get_frame_extra_info (fi)->locals_size = 0;
749 get_frame_extra_info (fi)->framereg = 0;
750 get_frame_extra_info (fi)->framesize = 0;
751 get_frame_extra_info (fi)->is_main = 0;
752
753 avr_scan_prologue (fi);
754
755 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), get_frame_base (fi),
756 get_frame_base (fi)))
757 {
758 /* We need to setup fi->frame here because call_function_by_hand
759 gets it wrong by assuming it's always FP. */
760 deprecated_update_frame_base_hack (fi, deprecated_read_register_dummy (get_frame_pc (fi), get_frame_base (fi),
761 AVR_PC_REGNUM));
762 }
763 else if (!get_next_frame (fi))
764 /* this is the innermost frame? */
765 deprecated_update_frame_base_hack (fi, read_register (get_frame_extra_info (fi)->framereg));
766 else if (get_frame_extra_info (fi)->is_main != 1)
767 /* not the innermost frame, not `main' */
768 /* If we have an next frame, the callee saved it. */
769 {
770 struct frame_info *next_fi = get_next_frame (fi);
771 if (get_frame_extra_info (fi)->framereg == AVR_SP_REGNUM)
772 deprecated_update_frame_base_hack (fi, (get_frame_base (next_fi)
773 + 2 /* ret addr */
774 + get_frame_extra_info (next_fi)->framesize));
775 /* FIXME: I don't analyse va_args functions */
776 else
777 {
778 CORE_ADDR fp = 0;
779 CORE_ADDR fp1 = 0;
780 unsigned int fp_low, fp_high;
781
782 /* Scan all frames */
783 for (; next_fi; next_fi = get_next_frame (next_fi))
784 {
785 /* look for saved AVR_FP_REGNUM */
786 if (get_frame_saved_regs (next_fi)[AVR_FP_REGNUM] && !fp)
787 fp = get_frame_saved_regs (next_fi)[AVR_FP_REGNUM];
788 /* look for saved AVR_FP_REGNUM + 1 */
789 if (get_frame_saved_regs (next_fi)[AVR_FP_REGNUM + 1] && !fp1)
790 fp1 = get_frame_saved_regs (next_fi)[AVR_FP_REGNUM + 1];
791 }
792 fp_low = (fp ? read_memory_unsigned_integer (avr_make_saddr (fp), 1)
793 : read_register (AVR_FP_REGNUM)) & 0xff;
794 fp_high =
795 (fp1 ? read_memory_unsigned_integer (avr_make_saddr (fp1), 1) :
796 read_register (AVR_FP_REGNUM + 1)) & 0xff;
797 deprecated_update_frame_base_hack (fi, fp_low | (fp_high << 8));
798 }
799 }
800
801 /* TRoth: Do we want to do this if we are in main? I don't think we should
802 since return_pc makes no sense when we are in main. */
803
804 if ((get_frame_pc (fi)) && (get_frame_extra_info (fi)->is_main == 0))
805 /* We are not in CALL_DUMMY */
806 {
807 CORE_ADDR addr;
808 int i;
809
810 addr = get_frame_base (fi) + get_frame_extra_info (fi)->framesize + 1;
811
812 /* Return address in stack in different endianness */
813
814 get_frame_extra_info (fi)->return_pc =
815 read_memory_unsigned_integer (avr_make_saddr (addr), 1) << 8;
816 get_frame_extra_info (fi)->return_pc |=
817 read_memory_unsigned_integer (avr_make_saddr (addr + 1), 1);
818
819 /* This return address in words,
820 must be converted to the bytes address */
821 get_frame_extra_info (fi)->return_pc *= 2;
822
823 /* Resolve a pushed registers addresses */
824 for (i = 0; i < NUM_REGS; i++)
825 {
826 if (get_frame_saved_regs (fi)[i])
827 get_frame_saved_regs (fi)[i] = addr - get_frame_saved_regs (fi)[i];
828 }
829 }
830 }
831
832 /* Restore the machine to the state it had before the current frame was
833 created. Usually used either by the "RETURN" command, or by
834 call_function_by_hand after the dummy_frame is finished. */
835
836 static void
837 avr_pop_frame (void)
838 {
839 unsigned regnum;
840 CORE_ADDR saddr;
841 struct frame_info *frame = get_current_frame ();
842
843 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
844 get_frame_base (frame),
845 get_frame_base (frame)))
846 {
847 generic_pop_dummy_frame ();
848 }
849 else
850 {
851 /* TRoth: Why only loop over 8 registers? */
852
853 for (regnum = 0; regnum < 8; regnum++)
854 {
855 /* Don't forget AVR_SP_REGNUM in a frame_saved_regs struct is the
856 actual value we want, not the address of the value we want. */
857 if (get_frame_saved_regs (frame)[regnum] && regnum != AVR_SP_REGNUM)
858 {
859 saddr = avr_make_saddr (get_frame_saved_regs (frame)[regnum]);
860 write_register (regnum,
861 read_memory_unsigned_integer (saddr, 1));
862 }
863 else if (get_frame_saved_regs (frame)[regnum] && regnum == AVR_SP_REGNUM)
864 write_register (regnum, get_frame_base (frame) + 2);
865 }
866
867 /* Don't forget the update the PC too! */
868 write_pc (get_frame_extra_info (frame)->return_pc);
869 }
870 flush_cached_frames ();
871 }
872
873 /* Return the saved PC from this frame. */
874
875 static CORE_ADDR
876 avr_frame_saved_pc (struct frame_info *frame)
877 {
878 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
879 get_frame_base (frame),
880 get_frame_base (frame)))
881 return deprecated_read_register_dummy (get_frame_pc (frame),
882 get_frame_base (frame),
883 AVR_PC_REGNUM);
884 else
885 return get_frame_extra_info (frame)->return_pc;
886 }
887
888 static CORE_ADDR
889 avr_saved_pc_after_call (struct frame_info *frame)
890 {
891 unsigned char m1, m2;
892 unsigned int sp = read_register (AVR_SP_REGNUM);
893 m1 = read_memory_unsigned_integer (avr_make_saddr (sp + 1), 1);
894 m2 = read_memory_unsigned_integer (avr_make_saddr (sp + 2), 1);
895 return (m2 | (m1 << 8)) * 2;
896 }
897
898 /* Returns the return address for a dummy. */
899
900 static CORE_ADDR
901 avr_call_dummy_address (void)
902 {
903 return entry_point_address ();
904 }
905
906 /* Setup the return address for a dummy frame, as called by
907 call_function_by_hand. Only necessary when you are using an empty
908 CALL_DUMMY. */
909
910 static CORE_ADDR
911 avr_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
912 {
913 unsigned char buf[2];
914 int wordsize = 2;
915 #if 0
916 struct minimal_symbol *msymbol;
917 CORE_ADDR mon_brk;
918 #endif
919
920 buf[0] = 0;
921 buf[1] = 0;
922 sp -= wordsize;
923 write_memory (sp + 1, buf, 2);
924
925 #if 0
926 /* FIXME: TRoth/2002-02-18: This should probably be removed since it's a
927 left-over from Denis' original patch which used avr-mon for the target
928 instead of the generic remote target. */
929 if ((strcmp (target_shortname, "avr-mon") == 0)
930 && (msymbol = lookup_minimal_symbol ("gdb_break", NULL, NULL)))
931 {
932 mon_brk = SYMBOL_VALUE_ADDRESS (msymbol);
933 store_unsigned_integer (buf, wordsize, mon_brk / 2);
934 sp -= wordsize;
935 write_memory (sp + 1, buf + 1, 1);
936 write_memory (sp + 2, buf, 1);
937 }
938 #endif
939 return sp;
940 }
941
942 static CORE_ADDR
943 avr_skip_prologue (CORE_ADDR pc)
944 {
945 CORE_ADDR func_addr, func_end;
946 struct symtab_and_line sal;
947
948 /* See what the symbol table says */
949
950 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
951 {
952 sal = find_pc_line (func_addr, 0);
953
954 /* troth/2002-08-05: For some very simple functions, gcc doesn't
955 generate a prologue and the sal.end ends up being the 2-byte ``ret''
956 instruction at the end of the function, but func_end ends up being
957 the address of the first instruction of the _next_ function. By
958 adjusting func_end by 2 bytes, we can catch these functions and not
959 return sal.end if it is the ``ret'' instruction. */
960
961 if (sal.line != 0 && sal.end < (func_end-2))
962 return sal.end;
963 }
964
965 /* Either we didn't find the start of this function (nothing we can do),
966 or there's no line info, or the line after the prologue is after
967 the end of the function (there probably isn't a prologue). */
968
969 return pc;
970 }
971
972 static CORE_ADDR
973 avr_frame_address (struct frame_info *fi)
974 {
975 return avr_make_saddr (get_frame_base (fi));
976 }
977
978 /* Given a GDB frame, determine the address of the calling function's
979 frame. This will be used to create a new GDB frame struct, and
980 then DEPRECATED_INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC
981 will be called for the new frame.
982
983 For us, the frame address is its stack pointer value, so we look up
984 the function prologue to determine the caller's sp value, and return it. */
985
986 static CORE_ADDR
987 avr_frame_chain (struct frame_info *frame)
988 {
989 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
990 get_frame_base (frame),
991 get_frame_base (frame)))
992 {
993 /* initialize the return_pc now */
994 get_frame_extra_info (frame)->return_pc
995 = deprecated_read_register_dummy (get_frame_pc (frame),
996 get_frame_base (frame),
997 AVR_PC_REGNUM);
998 return get_frame_base (frame);
999 }
1000 return (get_frame_extra_info (frame)->is_main ? 0
1001 : get_frame_base (frame) + get_frame_extra_info (frame)->framesize + 2 /* ret addr */ );
1002 }
1003
1004 /* Store the address of the place in which to copy the structure the
1005 subroutine will return. This is called from call_function.
1006
1007 We store structs through a pointer passed in the first Argument
1008 register. */
1009
1010 static void
1011 avr_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
1012 {
1013 write_register (0, addr);
1014 }
1015
1016 /* Setup the function arguments for calling a function in the inferior.
1017
1018 On the AVR architecture, there are 18 registers (R25 to R8) which are
1019 dedicated for passing function arguments. Up to the first 18 arguments
1020 (depending on size) may go into these registers. The rest go on the stack.
1021
1022 Arguments that are larger than WORDSIZE bytes will be split between two or
1023 more registers as available, but will NOT be split between a register and
1024 the stack.
1025
1026 An exceptional case exists for struct arguments (and possibly other
1027 aggregates such as arrays) -- if the size is larger than WORDSIZE bytes but
1028 not a multiple of WORDSIZE bytes. In this case the argument is never split
1029 between the registers and the stack, but instead is copied in its entirety
1030 onto the stack, AND also copied into as many registers as there is room
1031 for. In other words, space in registers permitting, two copies of the same
1032 argument are passed in. As far as I can tell, only the one on the stack is
1033 used, although that may be a function of the level of compiler
1034 optimization. I suspect this is a compiler bug. Arguments of these odd
1035 sizes are left-justified within the word (as opposed to arguments smaller
1036 than WORDSIZE bytes, which are right-justified).
1037
1038 If the function is to return an aggregate type such as a struct, the caller
1039 must allocate space into which the callee will copy the return value. In
1040 this case, a pointer to the return value location is passed into the callee
1041 in register R0, which displaces one of the other arguments passed in via
1042 registers R0 to R2. */
1043
1044 static CORE_ADDR
1045 avr_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1046 int struct_return, CORE_ADDR struct_addr)
1047 {
1048 int stack_alloc, stack_offset;
1049 int wordsize;
1050 int argreg;
1051 int argnum;
1052 struct type *type;
1053 CORE_ADDR regval;
1054 char *val;
1055 char valbuf[4];
1056 int len;
1057
1058 wordsize = 1;
1059 #if 0
1060 /* Now make sure there's space on the stack */
1061 for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
1062 stack_alloc += TYPE_LENGTH (VALUE_TYPE (args[argnum]));
1063 sp -= stack_alloc; /* make room on stack for args */
1064 /* we may over-allocate a little here, but that won't hurt anything */
1065 #endif
1066 argreg = 25;
1067 if (struct_return) /* "struct return" pointer takes up one argreg */
1068 {
1069 write_register (--argreg, struct_addr);
1070 }
1071
1072 /* Now load as many as possible of the first arguments into registers, and
1073 push the rest onto the stack. There are 3N bytes in three registers
1074 available. Loop thru args from first to last. */
1075
1076 for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
1077 {
1078 type = VALUE_TYPE (args[argnum]);
1079 len = TYPE_LENGTH (type);
1080 val = (char *) VALUE_CONTENTS (args[argnum]);
1081
1082 /* NOTE WELL!!!!! This is not an "else if" clause!!! That's because
1083 some *&^%$ things get passed on the stack AND in the registers! */
1084 while (len > 0)
1085 { /* there's room in registers */
1086 len -= wordsize;
1087 regval = extract_unsigned_integer (val + len, wordsize);
1088 write_register (argreg--, regval);
1089 }
1090 }
1091 return sp;
1092 }
1093
1094 /* Not all avr devices support the BREAK insn. Those that don't should treat
1095 it as a NOP. Thus, it should be ok. Since the avr is currently a remote
1096 only target, this shouldn't be a problem (I hope). TRoth/2003-05-14 */
1097
1098 static const unsigned char *
1099 avr_breakpoint_from_pc (CORE_ADDR * pcptr, int *lenptr)
1100 {
1101 static unsigned char avr_break_insn [] = { 0x98, 0x95 };
1102 *lenptr = sizeof (avr_break_insn);
1103 return avr_break_insn;
1104 }
1105
1106 /* Initialize the gdbarch structure for the AVR's. */
1107
1108 static struct gdbarch *
1109 avr_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1110 {
1111 /* FIXME: TRoth/2002-02-18: I have no idea if avr_call_dummy_words[] should
1112 be bigger or not. Initial testing seems to show that `call my_func()`
1113 works and backtrace from a breakpoint within the call looks correct.
1114 Admittedly, I haven't tested with more than a very simple program. */
1115 static LONGEST avr_call_dummy_words[] = { 0 };
1116
1117 struct gdbarch *gdbarch;
1118 struct gdbarch_tdep *tdep;
1119
1120 /* Find a candidate among the list of pre-declared architectures. */
1121 arches = gdbarch_list_lookup_by_info (arches, &info);
1122 if (arches != NULL)
1123 return arches->gdbarch;
1124
1125 /* None found, create a new architecture from the information provided. */
1126 tdep = XMALLOC (struct gdbarch_tdep);
1127 gdbarch = gdbarch_alloc (&info, tdep);
1128
1129 /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
1130 ready to unwind the PC first (see frame.c:get_prev_frame()). */
1131 set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_default);
1132
1133 /* If we ever need to differentiate the device types, do it here. */
1134 switch (info.bfd_arch_info->mach)
1135 {
1136 case bfd_mach_avr1:
1137 case bfd_mach_avr2:
1138 case bfd_mach_avr3:
1139 case bfd_mach_avr4:
1140 case bfd_mach_avr5:
1141 break;
1142 }
1143
1144 set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1145 set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1146 set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1147 set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1148 set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1149 set_gdbarch_addr_bit (gdbarch, 32);
1150 set_gdbarch_bfd_vma_bit (gdbarch, 32); /* FIXME: TRoth/2002-02-18: Is this needed? */
1151
1152 set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1153 set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1154 set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1155
1156 set_gdbarch_float_format (gdbarch, &floatformat_ieee_single_little);
1157 set_gdbarch_double_format (gdbarch, &floatformat_ieee_single_little);
1158 set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_single_little);
1159
1160 set_gdbarch_read_pc (gdbarch, avr_read_pc);
1161 set_gdbarch_write_pc (gdbarch, avr_write_pc);
1162 set_gdbarch_deprecated_target_read_fp (gdbarch, avr_read_fp);
1163 set_gdbarch_read_sp (gdbarch, avr_read_sp);
1164 set_gdbarch_deprecated_dummy_write_sp (gdbarch, avr_write_sp);
1165
1166 set_gdbarch_num_regs (gdbarch, AVR_NUM_REGS);
1167
1168 set_gdbarch_sp_regnum (gdbarch, AVR_SP_REGNUM);
1169 set_gdbarch_deprecated_fp_regnum (gdbarch, AVR_FP_REGNUM);
1170 set_gdbarch_pc_regnum (gdbarch, AVR_PC_REGNUM);
1171
1172 set_gdbarch_register_name (gdbarch, avr_register_name);
1173 set_gdbarch_deprecated_register_size (gdbarch, 1);
1174 set_gdbarch_deprecated_register_bytes (gdbarch, AVR_NUM_REG_BYTES);
1175 set_gdbarch_register_byte (gdbarch, avr_register_byte);
1176 set_gdbarch_register_raw_size (gdbarch, avr_register_raw_size);
1177 set_gdbarch_deprecated_max_register_raw_size (gdbarch, 4);
1178 set_gdbarch_register_virtual_size (gdbarch, avr_register_virtual_size);
1179 set_gdbarch_deprecated_max_register_virtual_size (gdbarch, 4);
1180 set_gdbarch_register_virtual_type (gdbarch, avr_register_virtual_type);
1181
1182 set_gdbarch_print_insn (gdbarch, print_insn_avr);
1183
1184 set_gdbarch_call_dummy_address (gdbarch, avr_call_dummy_address);
1185 set_gdbarch_deprecated_call_dummy_words (gdbarch, avr_call_dummy_words);
1186
1187 /* set_gdbarch_believe_pcc_promotion (gdbarch, 1); // TRoth: should this be set? */
1188
1189 set_gdbarch_address_to_pointer (gdbarch, avr_address_to_pointer);
1190 set_gdbarch_pointer_to_address (gdbarch, avr_pointer_to_address);
1191 set_gdbarch_deprecated_push_arguments (gdbarch, avr_push_arguments);
1192 set_gdbarch_deprecated_push_return_address (gdbarch, avr_push_return_address);
1193 set_gdbarch_deprecated_pop_frame (gdbarch, avr_pop_frame);
1194
1195 set_gdbarch_use_struct_convention (gdbarch, generic_use_struct_convention);
1196 set_gdbarch_deprecated_store_struct_return (gdbarch, avr_store_struct_return);
1197
1198 set_gdbarch_deprecated_frame_init_saved_regs (gdbarch, avr_scan_prologue);
1199 set_gdbarch_deprecated_init_extra_frame_info (gdbarch, avr_init_extra_frame_info);
1200 set_gdbarch_skip_prologue (gdbarch, avr_skip_prologue);
1201 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1202
1203 set_gdbarch_decr_pc_after_break (gdbarch, 0);
1204 set_gdbarch_breakpoint_from_pc (gdbarch, avr_breakpoint_from_pc);
1205
1206 set_gdbarch_function_start_offset (gdbarch, 0);
1207 set_gdbarch_remote_translate_xfer_address (gdbarch,
1208 avr_remote_translate_xfer_address);
1209 set_gdbarch_frame_args_skip (gdbarch, 0);
1210 set_gdbarch_frameless_function_invocation (gdbarch, frameless_look_for_prologue); /* ??? */
1211 set_gdbarch_deprecated_frame_chain (gdbarch, avr_frame_chain);
1212 set_gdbarch_deprecated_frame_saved_pc (gdbarch, avr_frame_saved_pc);
1213 set_gdbarch_frame_args_address (gdbarch, avr_frame_address);
1214 set_gdbarch_frame_locals_address (gdbarch, avr_frame_address);
1215 set_gdbarch_deprecated_saved_pc_after_call (gdbarch, avr_saved_pc_after_call);
1216
1217 set_gdbarch_convert_from_func_ptr_addr (gdbarch,
1218 avr_convert_from_func_ptr_addr);
1219
1220 return gdbarch;
1221 }
1222
1223 /* Send a query request to the avr remote target asking for values of the io
1224 registers. If args parameter is not NULL, then the user has requested info
1225 on a specific io register [This still needs implemented and is ignored for
1226 now]. The query string should be one of these forms:
1227
1228 "Ravr.io_reg" -> reply is "NN" number of io registers
1229
1230 "Ravr.io_reg:addr,len" where addr is first register and len is number of
1231 registers to be read. The reply should be "<NAME>,VV;" for each io register
1232 where, <NAME> is a string, and VV is the hex value of the register.
1233
1234 All io registers are 8-bit. */
1235
1236 static void
1237 avr_io_reg_read_command (char *args, int from_tty)
1238 {
1239 int bufsiz = 0;
1240 char buf[400];
1241 char query[400];
1242 char *p;
1243 unsigned int nreg = 0;
1244 unsigned int val;
1245 int i, j, k, step;
1246
1247 /* fprintf_unfiltered (gdb_stderr, "DEBUG: avr_io_reg_read_command (\"%s\", %d)\n", */
1248 /* args, from_tty); */
1249
1250 if (!current_target.to_query)
1251 {
1252 fprintf_unfiltered (gdb_stderr,
1253 "ERR: info io_registers NOT supported by current target\n");
1254 return;
1255 }
1256
1257 /* Just get the maximum buffer size. */
1258 target_query ((int) 'R', 0, 0, &bufsiz);
1259 if (bufsiz > sizeof (buf))
1260 bufsiz = sizeof (buf);
1261
1262 /* Find out how many io registers the target has. */
1263 strcpy (query, "avr.io_reg");
1264 target_query ((int) 'R', query, buf, &bufsiz);
1265
1266 if (strncmp (buf, "", bufsiz) == 0)
1267 {
1268 fprintf_unfiltered (gdb_stderr,
1269 "info io_registers NOT supported by target\n");
1270 return;
1271 }
1272
1273 if (sscanf (buf, "%x", &nreg) != 1)
1274 {
1275 fprintf_unfiltered (gdb_stderr,
1276 "Error fetching number of io registers\n");
1277 return;
1278 }
1279
1280 reinitialize_more_filter ();
1281
1282 printf_unfiltered ("Target has %u io registers:\n\n", nreg);
1283
1284 /* only fetch up to 8 registers at a time to keep the buffer small */
1285 step = 8;
1286
1287 for (i = 0; i < nreg; i += step)
1288 {
1289 /* how many registers this round? */
1290 j = step;
1291 if ((i+j) >= nreg)
1292 j = nreg - i; /* last block is less than 8 registers */
1293
1294 snprintf (query, sizeof (query) - 1, "avr.io_reg:%x,%x", i, j);
1295 target_query ((int) 'R', query, buf, &bufsiz);
1296
1297 p = buf;
1298 for (k = i; k < (i + j); k++)
1299 {
1300 if (sscanf (p, "%[^,],%x;", query, &val) == 2)
1301 {
1302 printf_filtered ("[%02x] %-15s : %02x\n", k, query, val);
1303 while ((*p != ';') && (*p != '\0'))
1304 p++;
1305 p++; /* skip over ';' */
1306 if (*p == '\0')
1307 break;
1308 }
1309 }
1310 }
1311 }
1312
1313 extern initialize_file_ftype _initialize_avr_tdep; /* -Wmissing-prototypes */
1314
1315 void
1316 _initialize_avr_tdep (void)
1317 {
1318 register_gdbarch_init (bfd_arch_avr, avr_gdbarch_init);
1319
1320 /* Add a new command to allow the user to query the avr remote target for
1321 the values of the io space registers in a saner way than just using
1322 `x/NNNb ADDR`. */
1323
1324 /* FIXME: TRoth/2002-02-18: This should probably be changed to 'info avr
1325 io_registers' to signify it is not available on other platforms. */
1326
1327 add_cmd ("io_registers", class_info, avr_io_reg_read_command,
1328 "query remote avr target for io space register values", &infolist);
1329 }