* monitor.c (monitor_write, monitor_readchar): New functions.
[deliverable/binutils-gdb.git] / gdb / dve3900-rom.c
1 /* Remote debugging interface for Densan DVE-R3900 ROM monitor for
2 GDB, the GNU debugger.
3 Copyright 1997 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, Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include "gdbcore.h"
23 #include "target.h"
24 #include "monitor.h"
25 #include "serial.h"
26 #include "inferior.h"
27 #include "command.h"
28 #include "gdb_string.h"
29 #include <time.h>
30
31 /* Packet escape character used by Densan monitor. */
32
33 #define PESC 0xdc
34
35 /* Maximum packet size. This is actually smaller than necessary
36 just to be safe. */
37
38 #define MAXPSIZE 1024
39
40 /* External functions. */
41
42 extern void report_transfer_performance PARAMS ((unsigned long,
43 time_t, time_t));
44
45 /* Certain registers are "bitmapped", in that the monitor can only display
46 them or let the user modify them as a series of named bitfields.
47 This structure describes a field in a bitmapped register. */
48
49 struct bit_field
50 {
51 char *prefix; /* string appearing before the value */
52 char *suffix; /* string appearing after the value */
53 char *user_name; /* name used by human when entering field value */
54 int length; /* number of bits in the field */
55 int start; /* starting (least significant) bit number of field */
56 };
57
58 /* Local functions for register manipulation. */
59
60 static void r3900_supply_register PARAMS ((char *regname, int regnamelen,
61 char *val, int vallen));
62 static void fetch_bad_vaddr PARAMS ((void));
63 static unsigned long fetch_fields PARAMS ((struct bit_field *bf));
64 static void fetch_bitmapped_register PARAMS ((int regno,
65 struct bit_field *bf));
66 static void r3900_fetch_registers PARAMS ((int regno));
67 static void store_bitmapped_register PARAMS ((int regno,
68 struct bit_field *bf));
69 static void r3900_store_registers PARAMS ((int regno));
70
71 /* Local functions for fast binary loading. */
72
73 static void write_long PARAMS ((char *buf, long n));
74 static void write_long_le PARAMS ((char *buf, long n));
75 static int debug_readchar PARAMS ((int hex));
76 static void debug_write PARAMS ((unsigned char *buf, int buflen));
77 static void ignore_packet PARAMS ((void));
78 static void send_packet PARAMS ((char type, unsigned char *buf, int buflen,
79 int seq));
80 static void process_read_request PARAMS ((unsigned char *buf, int buflen));
81 static void count_section PARAMS ((bfd *abfd, asection *s,
82 unsigned int *section_count));
83 static void load_section PARAMS ((bfd *abfd, asection *s,
84 unsigned int *data_count));
85 static void r3900_load PARAMS ((char *filename, int from_tty));
86
87 /* Miscellaneous local functions. */
88
89 static void r3900_open PARAMS ((char *args, int from_tty));
90
91
92 /* Pointers to static functions in monitor.c for fetching and storing
93 registers. We can't use these function in certain cases where the Densan
94 monitor acts perversely: for registers that it displays in bit-map
95 format, and those that can't be modified at all. In those cases
96 we have to use our own functions to fetch and store their values. */
97
98 static void (*orig_monitor_fetch_registers) PARAMS ((int regno));
99 static void (*orig_monitor_store_registers) PARAMS ((int regno));
100
101 /* Pointer to static function in monitor. for loading programs.
102 We use this function for loading S-records via the serial link. */
103
104 static void (*orig_monitor_load) PARAMS ((char *file, int from_tty));
105
106 /* This flag is set if a fast ethernet download should be used. */
107
108 static int ethernet = 0;
109
110 /* This array of registers needs to match the indexes used by GDB. The
111 whole reason this exists is because the various ROM monitors use
112 different names than GDB does, and don't support all the registers
113 either. */
114
115 static char *r3900_regnames[NUM_REGS] =
116 {
117 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
118 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
119 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
120 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
121
122 "S", /* PS_REGNUM */
123 "l", /* LO_REGNUM */
124 "h", /* HI_REGNUM */
125 "B", /* BADVADDR_REGNUM */
126 "Pcause", /* CAUSE_REGNUM */
127 "p" /* PC_REGNUM */
128 };
129
130
131 /* Table of register names produced by monitor's register dump command. */
132
133 static struct reg_entry
134 {
135 char *name;
136 int regno;
137 } reg_table[] =
138 {
139 { "r0_zero", 0 }, { "r1_at", 1 }, { "r2_v0", 2 }, { "r3_v1", 3 },
140 { "r4_a0", 4 }, { "r5_a1", 5 }, { "r6_a2", 6 }, { "r7_a3", 7 },
141 { "r8_t0", 8 }, { "r9_t1", 9 }, { "r10_t2", 10 }, { "r11_t3", 11 },
142 { "r12_t4", 12 }, { "r13_t5", 13 }, { "r14_t6", 14 }, { "r15_t7", 15 },
143 { "r16_s0", 16 }, { "r17_s1", 17 }, { "r18_s2", 18 }, { "r19_s3", 19 },
144 { "r20_s4", 20 }, { "r21_s5", 21 }, { "r22_s6", 22 }, { "r23_s7", 23 },
145 { "r24_t8", 24 }, { "r25_t9", 25 }, { "r26_k0", 26 }, { "r27_k1", 27 },
146 { "r28_gp", 28 }, { "r29_sp", 29 }, { "r30_fp", 30 }, { "r31_ra", 31 },
147 { "HI", HI_REGNUM },
148 { "LO", LO_REGNUM },
149 { "PC", PC_REGNUM },
150 { "BadV", BADVADDR_REGNUM },
151 { NULL, 0 }
152 };
153
154
155 /* The monitor displays the cache register along with the status register,
156 as if they were a single register. So when we want to fetch the
157 status register, parse but otherwise ignore the fields of the
158 cache register that the monitor displays. Register fields that should
159 be ignored have a length of zero in the tables below. */
160
161 static struct bit_field status_fields [] =
162 {
163 /* Status register portion */
164 { "SR[<CU=", " ", "cu", 4, 28 },
165 { "RE=", " ", "re", 1, 25 },
166 { "BEV=", " ", "bev", 1, 22 },
167 { "TS=", " ", "ts", 1, 21 },
168 { "Nmi=", " ", "nmi", 1, 20 },
169 { "INT=", " ", "int", 6, 10 },
170 { "SW=", ">]", "sw", 2, 8 },
171 { "[<KUO=", " ", "kuo", 1, 5 },
172 { "IEO=", " ", "ieo", 1, 4 },
173 { "KUP=", " ", "kup", 1, 3 },
174 { "IEP=", " ", "iep", 1, 2 },
175 { "KUC=", " ", "kuc", 1, 1 },
176 { "IEC=", ">]", "iec", 1, 0 },
177
178 /* Cache register portion (dummy for parsing only) */
179 { "CR[<IalO="," ", "ialo", 0, 13 },
180 { "DalO=", " ", "dalo", 0, 12 },
181 { "IalP=", " ", "ialp", 0, 11 },
182 { "DalP=", " ", "dalp", 0, 10 },
183 { "IalC=", " ", "ialc", 0, 9 },
184 { "DalC=", ">] ", "dalc", 0, 8 },
185
186 { NULL, NULL, 0, 0 } /* end of table marker */
187 };
188
189
190 static struct bit_field cache_fields [] =
191 {
192 /* Status register portion (dummy for parsing only) */
193 { "SR[<CU=", " ", "cu", 0, 28 },
194 { "RE=", " ", "re", 0, 25 },
195 { "BEV=", " ", "bev", 0, 22 },
196 { "TS=", " ", "ts", 0, 21 },
197 { "Nmi=", " ", "nmi", 0, 20 },
198 { "INT=", " ", "int", 0, 10 },
199 { "SW=", ">]", "sw", 0, 8 },
200 { "[<KUO=", " ", "kuo", 0, 5 },
201 { "IEO=", " ", "ieo", 0, 4 },
202 { "KUP=", " ", "kup", 0, 3 },
203 { "IEP=", " ", "iep", 0, 2 },
204 { "KUC=", " ", "kuc", 0, 1 },
205 { "IEC=", ">]", "iec", 0, 0 },
206
207 /* Cache register portion */
208 { "CR[<IalO="," ", "ialo", 1, 13 },
209 { "DalO=", " ", "dalo", 1, 12 },
210 { "IalP=", " ", "ialp", 1, 11 },
211 { "DalP=", " ", "dalp", 1, 10 },
212 { "IalC=", " ", "ialc", 1, 9 },
213 { "DalC=", ">] ", "dalc", 1, 8 },
214
215 { NULL, NULL, NULL, 0, 0 } /* end of table marker */
216 };
217
218 static struct bit_field cause_fields[] =
219 {
220 { "<BD=", " ", "bd", 1, 31 },
221 { "CE=", " ", "ce", 2, 28 },
222 { "IP=", " ", "ip", 6, 10 },
223 { "SW=", " ", "sw", 2, 8 },
224 { "EC=", ">]" , "ec", 5, 2 },
225
226 { NULL, NULL, NULL, 0, 0 } /* end of table marker */
227 };
228
229
230 /* The monitor prints register values in the form
231
232 regname = xxxx xxxx
233
234 We look up the register name in a table, and remove the embedded space in
235 the hex value before passing it to monitor_supply_register. */
236
237 static void
238 r3900_supply_register (regname, regnamelen, val, vallen)
239 char *regname;
240 int regnamelen;
241 char *val;
242 int vallen;
243 {
244 int regno = -1;
245 int i;
246 char valbuf[10];
247 char *p;
248
249 /* Perform some sanity checks on the register name and value. */
250 if (regnamelen < 2 || regnamelen > 7 || vallen != 9)
251 return;
252
253 /* Look up the register name. */
254 for (i = 0; reg_table[i].name != NULL; i++)
255 {
256 int rlen = strlen (reg_table[i].name);
257 if (rlen == regnamelen && strncmp (regname, reg_table[i].name, rlen) == 0)
258 {
259 regno = reg_table[i].regno;
260 break;
261 }
262 }
263 if (regno == -1)
264 return;
265
266 /* Copy the hex value to a buffer and eliminate the embedded space. */
267 for (i = 0, p = valbuf; i < vallen; i++)
268 if (val[i] != ' ')
269 *p++ = val[i];
270 *p = '\0';
271
272 monitor_supply_register (regno, valbuf);
273 }
274
275 /* Fetch the BadVaddr register. Unlike the other registers, this
276 one can't be modified, and the monitor won't even prompt to let
277 you modify it. */
278
279 static void
280 fetch_bad_vaddr()
281 {
282 char buf[20];
283
284 monitor_printf ("xB\r");
285 monitor_expect ("BadV=", NULL, 0);
286 monitor_expect_prompt (buf, sizeof(buf));
287 monitor_supply_register (BADVADDR_REGNUM, buf);
288 }
289
290
291 /* Read a series of bit fields from the monitor, and return their
292 combined binary value. */
293
294 static unsigned long
295 fetch_fields (bf)
296 struct bit_field *bf;
297 {
298 char buf[20];
299 unsigned long val = 0;
300 unsigned long bits;
301
302 for ( ; bf->prefix != NULL; bf++)
303 {
304 monitor_expect (bf->prefix, NULL, 0); /* get prefix */
305 monitor_expect (bf->suffix, buf, sizeof (buf)); /* hex value, suffix */
306 if (bf->length != 0)
307 {
308 bits = strtoul (buf, NULL, 16); /* get field value */
309 bits &= ((1 << bf->length) - 1); /* mask out useless bits */
310 val |= bits << bf->start; /* insert into register */
311 }
312
313 }
314
315 return val;
316 }
317
318 static void
319 fetch_bitmapped_register (regno, bf)
320 int regno;
321 struct bit_field *bf;
322 {
323 unsigned long val;
324 unsigned char regbuf[MAX_REGISTER_RAW_SIZE];
325
326 monitor_printf ("x%s\r", r3900_regnames[regno]);
327 val = fetch_fields (bf);
328 monitor_printf (".\r");
329 monitor_expect_prompt (NULL, 0);
330
331 /* supply register stores in target byte order, so swap here */
332
333 store_unsigned_integer (regbuf, REGISTER_RAW_SIZE (regno), val);
334 supply_register (regno, regbuf);
335
336 }
337
338 /* Fetch all registers (if regno is -1), or one register from the
339 monitor. For most registers, we can use the generic monitor_
340 monitor_fetch_registers function. But others are displayed in
341 very unusual fashion and must be handled specially. */
342
343 static void
344 r3900_fetch_registers (regno)
345 int regno;
346 {
347 switch (regno)
348 {
349 case BADVADDR_REGNUM:
350 fetch_bad_vaddr ();
351 return;
352 case PS_REGNUM:
353 fetch_bitmapped_register (PS_REGNUM, status_fields);
354 return;
355 case CAUSE_REGNUM:
356 fetch_bitmapped_register (CAUSE_REGNUM, cause_fields);
357 return;
358 default:
359 orig_monitor_fetch_registers (regno);
360 }
361 }
362
363
364 /* Write the new value of the bitmapped register to the monitor. */
365
366 static void
367 store_bitmapped_register (regno, bf)
368 int regno;
369 struct bit_field *bf;
370 {
371 unsigned long oldval, newval;
372
373 /* Fetch the current value of the register. */
374 monitor_printf ("x%s\r", r3900_regnames[regno]);
375 oldval = fetch_fields (bf);
376 newval = read_register (regno);
377
378 /* To save time, write just the fields that have changed. */
379 for ( ; bf->prefix != NULL; bf++)
380 {
381 if (bf->length != 0)
382 {
383 unsigned long oldbits, newbits, mask;
384
385 mask = (1 << bf->length) - 1;
386 oldbits = (oldval >> bf->start) & mask;
387 newbits = (newval >> bf->start) & mask;
388 if (oldbits != newbits)
389 monitor_printf ("%s %x ", bf->user_name, newbits);
390 }
391 }
392
393 monitor_printf (".\r");
394 monitor_expect_prompt (NULL, 0);
395 }
396
397 static void
398 r3900_store_registers (regno)
399 int regno;
400 {
401 switch (regno)
402 {
403 case PS_REGNUM:
404 store_bitmapped_register (PS_REGNUM, status_fields);
405 return;
406 case CAUSE_REGNUM:
407 store_bitmapped_register (CAUSE_REGNUM, cause_fields);
408 return;
409 default:
410 orig_monitor_store_registers (regno);
411 }
412 }
413
414
415 /* Write a 4-byte integer to the buffer in big-endian order. */
416
417 static void
418 write_long (buf, n)
419 char *buf;
420 long n;
421 {
422 buf[0] = (n >> 24) & 0xff;
423 buf[1] = (n >> 16) & 0xff;
424 buf[2] = (n >> 8) & 0xff;
425 buf[3] = n & 0xff;
426 }
427
428
429 /* Write a 4-byte integer to the buffer in little-endian order. */
430
431 static void
432 write_long_le (buf, n)
433 char *buf;
434 long n;
435 {
436 buf[0] = n & 0xff;
437 buf[1] = (n >> 8) & 0xff;
438 buf[2] = (n >> 16) & 0xff;
439 buf[3] = (n >> 24) & 0xff;
440 }
441
442
443 /* Read a character from the monitor. If remote debugging is on,
444 print the received character. If HEX is non-zero, print the
445 character in hexadecimal; otherwise, print it in ascii. */
446
447 static int
448 debug_readchar (hex)
449 int hex;
450 {
451 char buf [10];
452 int c = monitor_readchar ();
453
454 if (remote_debug > 0)
455 {
456 if (hex)
457 sprintf (buf, "[%02x]", c & 0xff);
458 else if (c == '\0')
459 strcpy (buf, "\\0");
460 else
461 {
462 buf[0] = c;
463 buf[1] = '\0';
464 }
465 puts_debug ("Read -->", buf, "<--");
466 }
467 return c;
468 }
469
470
471 /* Send a buffer of characters to the monitor. If remote debugging is on,
472 print the sent buffer in hex. */
473
474 static void
475 debug_write (buf, buflen)
476 unsigned char *buf;
477 int buflen;
478 {
479 char s[10];
480
481 monitor_write (buf, buflen);
482
483 if (remote_debug > 0)
484 {
485 while (buflen-- > 0)
486 {
487 sprintf (s, "[%02x]", *buf & 0xff);
488 puts_debug ("Sent -->", s, "<--");
489 buf++;
490 }
491 }
492 }
493
494
495 /* Ignore a packet sent to us by the monitor. It send packets
496 when its console is in "communications interface" mode. A packet
497 is of this form:
498
499 start of packet flag (one byte: 0xdc)
500 packet type (one byte)
501 length (low byte)
502 length (high byte)
503 data (length bytes)
504 */
505
506 static void
507 ignore_packet ()
508 {
509 int c;
510 int len;
511
512 /* Ignore lots of trash (messages about section addresses, for example)
513 until we see the start of a packet. */
514 for (len = 0; len < 256; len++)
515 {
516 c = debug_readchar (0);
517 if (c == PESC)
518 break;
519 }
520 if (len == 8)
521 error ("Packet header byte not found; %02x seen instead.", c);
522
523 /* Read the packet type and length. */
524 c = debug_readchar (1); /* type */
525
526 c = debug_readchar (1); /* low byte of length */
527 len = c & 0xff;
528
529 c = debug_readchar (1); /* high byte of length */
530 len += (c & 0xff) << 8;
531
532 /* Ignore the rest of the packet. */
533 while (len-- > 0)
534 c = debug_readchar (1);
535 }
536
537
538 /* Send a packet to the monitor. */
539
540 static void
541 send_packet (type, buf, buflen, seq)
542 char type;
543 unsigned char *buf;
544 int buflen, seq;
545 {
546 unsigned char hdr[4];
547 int len = buflen;
548 int sum, i;
549
550 /* If this is a 'p' packet, add one byte for a sequence number. */
551 if (type == 'p')
552 len++;
553
554 /* If the buffer has a non-zero length, add two bytes for a checksum. */
555 if (len > 0)
556 len += 2;
557
558 /* Write the packet header. */
559 hdr[0] = PESC;
560 hdr[1] = type;
561 hdr[2] = len & 0xff;
562 hdr[3] = (len >> 8) & 0xff;
563 debug_write (hdr, sizeof (hdr));
564
565 if (len)
566 {
567 /* Write the packet data. */
568 debug_write (buf, buflen);
569
570 /* Write the sequence number if this is a 'p' packet. */
571 if (type == 'p')
572 {
573 hdr[0] = seq;
574 debug_write (hdr, 1);
575 }
576
577 /* Write the checksum. */
578 sum = 0;
579 for (i = 0; i < buflen; i++)
580 {
581 int tmp = (buf[i] & 0xff);
582 if (i & 1)
583 sum += tmp;
584 else
585 sum += tmp << 8;
586 }
587 if (type == 'p')
588 {
589 if (buflen & 1)
590 sum += (seq & 0xff);
591 else
592 sum += (seq & 0xff) << 8;
593 }
594 sum = (sum & 0xffff) + ((sum >> 16) & 0xffff);
595 sum += (sum >> 16) & 1;
596 sum = ~sum;
597
598 hdr[0] = (sum >> 8) & 0xff;
599 hdr[1] = sum & 0xff;
600 debug_write (hdr, 2);
601 }
602 }
603
604
605 /* Respond to an expected read request from the monitor by sending
606 data in chunks. Handle all acknowledgements and handshaking packets. */
607
608 static void
609 process_read_request (buf, buflen)
610 unsigned char *buf;
611 int buflen;
612 {
613 unsigned char len[4];
614 int i, chunk;
615 unsigned char seq;
616
617 /* Discard the read request. We have to hope it's for
618 the exact number of bytes we want to send. */
619 ignore_packet ();
620
621 for (i = chunk = 0, seq = 0; i < buflen; i += chunk, seq++)
622 {
623 /* Don't send more than 256 bytes at a time. */
624 chunk = buflen - i;
625 if (chunk > MAXPSIZE)
626 chunk = MAXPSIZE;
627
628 /* Write a packet containing the number of bytes we are sending. */
629 write_long_le (len, chunk);
630 send_packet ('p', len, sizeof (len), seq);
631
632 /* Write the data in raw form following the packet. */
633 debug_write (&buf[i], chunk);
634
635 /* Discard the ACK packet. */
636 ignore_packet ();
637 }
638
639 /* Send an "end of data" packet. */
640 send_packet ('e', "", 0, 0);
641 }
642
643
644 /* Count loadable sections (helper function for r3900_load). */
645
646 static void
647 count_section (abfd, s, section_count)
648 bfd *abfd;
649 asection *s;
650 unsigned int *section_count;
651 {
652 if (s->flags & SEC_LOAD && bfd_section_size (abfd, s) != 0)
653 (*section_count)++;
654 }
655
656
657 /* Load a single BFD section (helper function for r3900_load).
658
659 WARNING: this code is filled with assumptions about how
660 the Densan monitor loads programs. The monitor issues
661 packets containing read requests, but rather than respond
662 to them in an general way, we expect them to following
663 a certain pattern.
664
665 For example, we know that the monitor will start loading by
666 issuing an 8-byte read request for the binary file header.
667 We know this is coming and ignore the actual contents
668 of the read request packet.
669 */
670
671 static void
672 load_section (abfd, s, data_count)
673 bfd *abfd;
674 asection *s;
675 unsigned int *data_count;
676 {
677 if (s->flags & SEC_LOAD)
678 {
679 bfd_size_type section_size = bfd_section_size (abfd, s);
680 bfd_vma section_base = bfd_section_lma (abfd, s);
681 unsigned char *buffer;
682 unsigned char header[8];
683
684 /* Don't output zero-length sections. */
685 if (section_size == 0)
686 return;
687 if (data_count)
688 *data_count += section_size;
689
690 /* Print some fluff about the section being loaded. */
691 printf_filtered ("Loading section %s, size 0x%lx lma ",
692 bfd_section_name (abfd, s), (long)section_size);
693 print_address_numeric (section_base, 1, gdb_stdout);
694 printf_filtered ("\n");
695 gdb_flush (gdb_stdout);
696
697 /* Write the section header (location and size). */
698 write_long (&header[0], (long)section_base);
699 write_long (&header[4], (long)section_size);
700 process_read_request (header, sizeof (header));
701
702 /* Read the section contents into a buffer, write it out,
703 then free the buffer. */
704 buffer = (unsigned char *) xmalloc (section_size);
705 bfd_get_section_contents (abfd, s, buffer, 0, section_size);
706 process_read_request (buffer, section_size);
707 free (buffer);
708 }
709 }
710
711
712 /* When the ethernet is used as the console port on the Densan board,
713 we can use the "Rm" command to do a fast binary load. The format
714 of the download data is:
715
716 number of sections (4 bytes)
717 starting address (4 bytes)
718 repeat for each section:
719 location address (4 bytes)
720 section size (4 bytes)
721 binary data
722
723 The 4-byte fields are all in big-endian order.
724
725 Using this command is tricky because we have to put the monitor
726 into a special funky "communications interface" mode, in which
727 it sends and receives packets of data along with the normal prompt.
728 */
729
730 static void
731 r3900_load (filename, from_tty)
732 char *filename;
733 int from_tty;
734 {
735 bfd *abfd;
736 unsigned int data_count = 0;
737 time_t start_time, end_time; /* for timing of download */
738 int section_count = 0;
739 unsigned char buffer[8];
740
741 /* If we are not using the ethernet, use the normal monitor load,
742 which sends S-records over the serial link. */
743 if (!ethernet)
744 {
745 orig_monitor_load (filename, from_tty);
746 return;
747 }
748
749 /* Open the file. */
750 if (filename == NULL || filename[0] == 0)
751 filename = get_exec_file (1);
752 abfd = bfd_openr (filename, 0);
753 if (!abfd)
754 error ("Unable to open file %s\n", filename);
755 if (bfd_check_format (abfd, bfd_object) == 0)
756 error ("File is not an object file\n");
757
758 /* Output the "vconsi" command to get the monitor in the communication
759 state where it will accept a load command. This will cause
760 the monitor to emit a packet before each prompt, so ignore the packet. */
761 monitor_printf ("vconsi\r");
762 ignore_packet ();
763 monitor_expect_prompt (NULL, 0);
764
765 /* Output the "Rm" (load) command and respond to the subsequent "open"
766 packet by sending an ACK packet. */
767 monitor_printf ("Rm\r");
768 ignore_packet ();
769 send_packet ('a', "", 0, 0);
770
771 /* Output the fast load header (number of sections and starting address). */
772 bfd_map_over_sections ((bfd *) abfd, count_section, &section_count);
773 write_long (&buffer[0], (long)section_count);
774 if (exec_bfd)
775 write_long (&buffer[4], (long)bfd_get_start_address (exec_bfd));
776 else
777 write_long (&buffer[4], 0);
778 process_read_request (buffer, sizeof (buffer));
779
780 /* Output the section data. */
781 start_time = time (NULL);
782 bfd_map_over_sections (abfd, load_section, &data_count);
783 end_time = time (NULL);
784
785 /* Acknowledge the close packet and put the monitor back into
786 "normal" mode so it won't send packets any more. */
787 ignore_packet ();
788 send_packet ('a', "", 0, 0);
789 monitor_expect_prompt (NULL, 0);
790 monitor_printf ("vconsx\r");
791 monitor_expect_prompt (NULL, 0);
792
793 /* Print download performance information. */
794 printf_filtered ("Start address 0x%lx\n", (long)bfd_get_start_address (abfd));
795 report_transfer_performance (data_count, start_time, end_time);
796
797 /* Finally, make the PC point at the start address */
798 if (exec_bfd)
799 write_pc (bfd_get_start_address (exec_bfd));
800
801 inferior_pid = 0; /* No process now */
802
803 /* This is necessary because many things were based on the PC at the
804 time that we attached to the monitor, which is no longer valid
805 now that we have loaded new code (and just changed the PC).
806 Another way to do this might be to call normal_stop, except that
807 the stack may not be valid, and things would get horribly
808 confused... */
809
810 clear_symtab_users ();
811 }
812
813
814 static struct target_ops r3900_ops;
815
816 /* Commands to send to the monitor when first connecting:
817 * The bare carriage return forces a prompt from the monitor
818 (monitor doesn't prompt after a reset).
819 * The "vconsx" switches the monitor back to interactive mode
820 in case an aborted download had left it in packet mode.
821 * The "Xtr" command causes subsequent "t" (trace) commands to display
822 the general registers only.
823 * The "Xxr" command does the same thing for the "x" (examine
824 registers) command.
825 * The "bx" command clears all breakpoints.
826 */
827
828 static char *r3900_inits[] = {"\r", "vconsx\r", "Xtr\r", "Xxr\r", "bx\r", NULL};
829 static char *dummy_inits[] = { NULL };
830
831 static struct monitor_ops r3900_cmds;
832
833 static void
834 r3900_open (args, from_tty)
835 char *args;
836 int from_tty;
837 {
838 char buf[64];
839 int i;
840
841 monitor_open (args, &r3900_cmds, from_tty);
842
843 /* We have to handle sending the init strings ourselves, because
844 the first two strings we send (carriage returns) may not be echoed
845 by the monitor, but the rest will be. */
846 monitor_printf_noecho ("\r\r");
847 for (i = 0; r3900_inits[i] != NULL; i++)
848 {
849 monitor_printf (r3900_inits[i]);
850 monitor_expect_prompt (NULL, 0);
851 }
852
853 /* Attempt to determine whether the console device is ethernet or serial.
854 This will tell us which kind of load to use (S-records over a serial
855 link, or the Densan fast binary multi-section format over the net). */
856
857 ethernet = 0;
858 monitor_printf ("v\r");
859 if (monitor_expect ("console device :", NULL, 0) != -1)
860 if (monitor_expect ("\n", buf, sizeof (buf)) != -1)
861 if (strstr (buf, "ethernet") != NULL)
862 ethernet = 1;
863 monitor_expect_prompt (NULL, 0);
864 }
865
866 void
867 _initialize_r3900_rom ()
868 {
869 r3900_cmds.flags = MO_NO_ECHO_ON_OPEN |
870 MO_ADDR_BITS_REMOVE |
871 MO_CLR_BREAK_USES_ADDR |
872 MO_PRINT_PROGRAM_OUTPUT;
873
874 r3900_cmds.init = dummy_inits;
875 r3900_cmds.cont = "g\r";
876 r3900_cmds.step = "t\r";
877 r3900_cmds.set_break = "b %Lx\r"; /* COREADDR */
878 r3900_cmds.clr_break = "b %Lx,0\r"; /* COREADDR */
879 r3900_cmds.fill = "fx %Lx s %x %x\r"; /* COREADDR, len, val */
880
881 r3900_cmds.setmem.cmdb = "sx %Lx %x\r"; /* COREADDR, val */
882 r3900_cmds.setmem.cmdw = "sh %Lx %x\r"; /* COREADDR, val */
883 r3900_cmds.setmem.cmdl = "sw %Lx %x\r"; /* COREADDR, val */
884
885 r3900_cmds.getmem.cmdb = "dx %Lx s %x\r"; /* COREADDR, len */
886 r3900_cmds.getmem.resp_delim = " : ";
887
888 r3900_cmds.setreg.cmd = "x%s %x\r"; /* regname, val */
889
890 r3900_cmds.getreg.cmd = "x%s\r"; /* regname */
891 r3900_cmds.getreg.resp_delim = "=";
892 r3900_cmds.getreg.term = " ";
893 r3900_cmds.getreg.term_cmd = ".\r";
894
895 r3900_cmds.dump_registers = "x\r";
896 r3900_cmds.register_pattern =
897 "\\([a-zA-Z0-9_]+\\) *=\\([0-9a-f]+ [0-9a-f]+\\b\\)";
898 r3900_cmds.supply_register = r3900_supply_register;
899 /* S-record download, via "keyboard port". */
900 r3900_cmds.load = "r0\r";
901 r3900_cmds.prompt = "#";
902 r3900_cmds.line_term = "\r";
903 r3900_cmds.target = &r3900_ops;
904 r3900_cmds.stopbits = SERIAL_1_STOPBITS;
905 r3900_cmds.regnames = r3900_regnames;
906 r3900_cmds.magic = MONITOR_OPS_MAGIC;
907
908 init_monitor_ops (&r3900_ops);
909
910 r3900_ops.to_shortname = "r3900";
911 r3900_ops.to_longname = "R3900 monitor";
912 r3900_ops.to_doc = "Debug using the DVE R3900 monitor.\n\
913 Specify the serial device it is connected to (e.g. /dev/ttya).";
914 r3900_ops.to_open = r3900_open;
915
916 /* Override the functions to fetch and store registers. But save the
917 addresses of the default functions, because we will use those functions
918 for "normal" registers. */
919
920 orig_monitor_fetch_registers = r3900_ops.to_fetch_registers;
921 orig_monitor_store_registers = r3900_ops.to_store_registers;
922 r3900_ops.to_fetch_registers = r3900_fetch_registers;
923 r3900_ops.to_store_registers = r3900_store_registers;
924
925 /* Override the load function, but save the address of the default
926 function to use when loading S-records over a serial link. */
927 orig_monitor_load = r3900_ops.to_load;
928 r3900_ops.to_load = r3900_load;
929
930 add_target (&r3900_ops);
931 }
This page took 0.049002 seconds and 5 git commands to generate.