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