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