1 /* Remote debugging interface to m32r ROM monitor for GDB, the GNU debugger.
2 Copyright 1996 Free Software Foundation, Inc.
4 Adapted by Michael Snyder of Cygnus Support.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This module defines communication with the Mitsubishi m32r monitor */
32 #include "symfile.h" /* for generic load */
33 #include <time.h> /* for time_t */
34 #include "gdb_string.h"
37 * All this stuff just to get my host computer's IP address!
39 #include <netdb.h> /* for hostent */
40 #include <netinet/in.h> /* for struct in_addr */
42 #include <arpa/inet.h> /* for inet_ntoa */
45 static char *board_addr; /* user-settable IP address for M32R-EVA */
46 static char *server_addr; /* user-settable IP address for gdb host */
47 static char *download_path; /* user-settable path for SREC files */
51 * Function: m32r_load_1 (helper function)
55 m32r_load_section (abfd, s, data_count)
58 unsigned int *data_count;
60 if (s->flags & SEC_LOAD)
62 bfd_size_type section_size = bfd_section_size (abfd, s);
63 bfd_vma section_base = bfd_section_vma (abfd, s);
64 unsigned int buffer, i;
66 *data_count += section_size;
68 printf_filtered ("Loading section %s, size 0x%lx vma ",
69 bfd_section_name (abfd, s), section_size);
70 print_address_numeric (section_base, 1, gdb_stdout);
71 printf_filtered ("\n");
72 gdb_flush (gdb_stdout);
73 monitor_printf ("%x mw\r" , section_base);
74 for (i = 0; i < section_size; i += 4)
77 monitor_expect (" -> ", NULL, 0);
78 bfd_get_section_contents (abfd, s, (char *) &buffer, i, 4);
79 monitor_printf ("%x\n", buffer);
81 monitor_expect (" -> ", NULL, 0);
82 monitor_printf ("q\n");
83 monitor_expect_prompt (NULL, 0);
93 bfd_map_over_sections ((bfd *) dummy, m32r_load_section, &data_count);
98 * Function: m32r_load (an alternate way to load)
102 m32r_load (filename, from_tty)
106 extern int inferior_pid;
109 unsigned int i, data_count = 0;
110 time_t start_time, end_time; /* for timing of download */
112 if (filename == NULL || filename[0] == 0)
113 filename = get_exec_file (1);
115 abfd = bfd_openr (filename, 0);
117 error ("Unable to open file %s\n", filename);
118 if (bfd_check_format (abfd, bfd_object) == 0)
119 error ("File is not an object file\n");
120 start_time = time (NULL);
122 for (s = abfd->sections; s; s = s->next)
123 if (s->flags & SEC_LOAD)
125 bfd_size_type section_size = bfd_section_size (abfd, s);
126 bfd_vma section_base = bfd_section_vma (abfd, s);
129 data_count += section_size;
131 printf_filtered ("Loading section %s, size 0x%lx vma ",
132 bfd_section_name (abfd, s), section_size);
133 print_address_numeric (section_base, 1, gdb_stdout);
134 printf_filtered ("\n");
135 gdb_flush (gdb_stdout);
136 monitor_printf ("%x mw\r" , section_base);
137 for (i = 0; i < section_size; i += 4)
139 monitor_expect (" -> ", NULL, 0);
140 bfd_get_section_contents (abfd, s, (char *) &buffer, i, 4);
141 monitor_printf ("%x\n", buffer);
143 monitor_expect (" -> ", NULL, 0);
144 monitor_printf ("q\n");
145 monitor_expect_prompt (NULL, 0);
148 if (!(catch_errors (m32r_load_1, abfd, "Load aborted!\n", RETURN_MASK_ALL)))
150 monitor_printf ("q\n");
154 end_time = time (NULL);
155 printf_filtered ("Start address 0x%lx\n", bfd_get_start_address (abfd));
156 report_transfer_performance (data_count, start_time, end_time);
158 /* Finally, make the PC point at the start address */
160 write_pc (bfd_get_start_address (exec_bfd));
162 inferior_pid = 0; /* No process now */
164 /* This is necessary because many things were based on the PC at the
165 time that we attached to the monitor, which is no longer valid
166 now that we have loaded new code (and just changed the PC).
167 Another way to do this might be to call normal_stop, except that
168 the stack may not be valid, and things would get horribly
171 clear_symtab_users ();
175 m32r_load_gen (filename, from_tty)
179 generic_load (filename, from_tty);
182 static void m32r_open PARAMS ((char *args, int from_tty));
184 /* This array of registers needs to match the indexes used by GDB. The
185 whole reason this exists is because the various ROM monitors use
186 different names than GDB does, and don't support all the registers
187 either. So, typing "info reg sp" becomes an "A7". */
189 static char *m32r_regnames[] =
190 { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
191 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
192 "psw", "cbr", "spi", "spu", "bpc", "pc", "accl", "acch",
196 m32r_supply_register (regname, regnamelen, val, vallen)
203 int num_regs = sizeof(m32r_regnames) / sizeof(m32r_regnames[0]);
205 for (regno = 0; regno < num_regs; regno++)
206 if (strncmp(regname, m32r_regnames[regno], regnamelen) == 0)
209 if (regno >= num_regs)
210 return; /* no match */
212 if (regno == ACCL_REGNUM)
213 { /* special handling for 64-bit acc reg */
214 monitor_supply_register (ACCH_REGNUM, val);
215 if (val = (char *) strchr(val, ':')) /* skip past ':' to get 2nd word */
216 monitor_supply_register (ACCL_REGNUM, val + 1);
220 monitor_supply_register (regno, val);
221 if (regno == PSW_REGNUM)
223 unsigned long psw = strtoul (val, NULL, 16);
224 char *zero = "00000000", *one = "00000001";
228 monitor_supply_register (SM_REGNUM, (psw & 0x80) ? one : zero);
231 /* Backup stack mode bit */
232 monitor_supply_register (BSM_REGNUM, (psw & 0x8000) ? one : zero);
235 /* Interrupt enable bit */
236 monitor_supply_register (IE_REGNUM, (psw & 0x40) ? one : zero);
239 /* Backup interrupt enable bit */
240 monitor_supply_register (BIE_REGNUM, (psw & 0x4000) ? one : zero);
243 /* Condition bit (carry etc.) */
244 monitor_supply_register (COND_REGNUM, (psw & 0x1) ? one : zero);
247 monitor_supply_register (CBR_REGNUM, (psw & 0x1) ? one : zero);
250 monitor_supply_register (BPC_REGNUM, zero); /* KLUDGE: (???????) */
253 monitor_supply_register (BCARRY_REGNUM, zero); /* KLUDGE: (??????) */
257 if (regno == SPI_REGNUM || regno == SPU_REGNUM)
258 { /* special handling for stack pointer (spu or spi) */
259 unsigned long stackmode = read_register (PSW_REGNUM) & 0x80;
261 if (regno == SPI_REGNUM && !stackmode) /* SP == SPI */
262 monitor_supply_register (SP_REGNUM, val);
263 else if (regno == SPU_REGNUM && stackmode) /* SP == SPU */
264 monitor_supply_register (SP_REGNUM, val);
269 static struct target_ops m32r_ops;
271 static char *m32r_inits[] = {"\r", NULL};
273 static struct monitor_ops m32r_cmds =
275 MO_CLR_BREAK_USES_ADDR | MO_REGISTER_VALUE_FIRST,
276 m32r_inits, /* Init strings */
277 "go\r", /* continue command */
278 "step\r", /* single step */
279 NULL, /* interrupt command */
280 "%x +bp\r", /* set a breakpoint */
281 "%x -bp\r", /* clear a breakpoint */
282 "bpoff\r", /* clear all breakpoints */
283 "%x %x %x fill\r", /* fill (start length val) */
285 "%x 1 %x fill\r", /* setmem.cmdb (addr, value) */
286 "%x 1 %x fillh\r", /* setmem.cmdw (addr, value) */
287 "%x 1 %x fillw\r", /* setmem.cmdl (addr, value) */
288 NULL, /* setmem.cmdll (addr, value) */
289 NULL, /* setmem.resp_delim */
290 NULL, /* setmem.term */
291 NULL /* setmem.term_cmd */
294 "%x %x dump\r", /* getmem.cmdb (addr, len) */
295 NULL, /* getmem.cmdw (addr, len) */
296 NULL, /* getmem.cmdl (addr, len) */
297 NULL, /* getmem.cmdll (addr, len) */
298 ": ", /* getmem.resp_delim */
299 NULL, /* getmem.term */
300 NULL /* getmem.term_cmd */
303 "%x to %%%s\r", /* setreg.cmd (name, value) */
304 NULL, /* setreg.resp_delim */
305 NULL, /* setreg.term */
306 NULL /* setreg.term_cmd */
309 NULL, /* getreg.cmd (name) */
310 NULL, /* getreg.resp_delim */
311 NULL, /* getreg.term */
312 NULL /* getreg.term_cmd */
314 ".reg\r", /* dump_registers */
315 "\\(\\w+\\) += \\([0-9a-fA-F]+\\b\\)", /* register_pattern */
316 m32r_supply_register, /* supply_register */
317 NULL, /* load_routine (defaults to SRECs) */
318 NULL, /* download command */
319 NULL, /* load response */
320 "ok ", /* monitor command prompt */
321 "\r", /* end-of-line terminator */
322 NULL, /* optional command terminator */
323 &m32r_ops, /* target operations */
324 SERIAL_1_STOPBITS, /* number of stop bits */
325 m32r_regnames, /* registers names */
326 MONITOR_OPS_MAGIC /* magic */
330 m32r_open(args, from_tty)
334 monitor_open (args, &m32r_cmds, from_tty);
337 /* Function: set_board_address
338 Tell the BootOne monitor what it's ethernet IP address is. */
341 m32r_set_board_address (args, from_tty)
350 monitor_printf ("ulip %s\n", args);
351 resp_len = monitor_expect_prompt (buf, sizeof(buf));
352 /* now parse the result for success */
355 error ("Requires argument (IP address for M32R-EVA board)");
358 /* Function: set_server_address
359 Tell the BootOne monitor what gdb's ethernet IP address is. */
362 m32r_set_server_address (args, from_tty)
371 monitor_printf ("uhip %s\n", args);
372 resp_len = monitor_expect_prompt (buf, sizeof(buf));
373 /* now parse the result for success */
376 error ("Requires argument (IP address of GDB's host computer)");
379 /* Function: set_download_path
380 Tell the BootOne monitor the default path for downloadable SREC files. */
383 m32r_set_download_path (args, from_tty)
392 monitor_printf ("up %s\n", args);
393 resp_len = monitor_expect_prompt (buf, sizeof(buf));
394 /* now parse the result for success */
397 error ("Requires argument (default path for downloadable SREC files)");
401 m32r_upload_command (args, from_tty)
407 time_t start_time, end_time; /* for timing of download */
408 extern int inferior_pid;
409 int resp_len, data_count = 0;
411 struct hostent *hostent;
412 struct in_addr inet_addr;
414 /* first check to see if there's an ethernet port! */
415 monitor_printf ("ust\r");
416 resp_len = monitor_expect_prompt (buf, sizeof(buf));
417 if (!strchr (buf, ':'))
418 error ("No ethernet connection!");
422 /* scan second colon in the output from the "ust" command */
423 char * myIPaddress = strchr (strchr (buf, ':') + 1, ':') + 1;
425 while (isspace(*myIPaddress))
428 if (!strncmp (myIPaddress, "0.0.", 4)) /* empty */
429 error ("Please use 'set board-address' to set the M32R-EVA board's IP address.");
430 if (strchr (myIPaddress, '('))
431 *(strchr (myIPaddress, '(')) = '\0'; /* delete trailing junk */
432 board_addr = strsave (myIPaddress);
434 if (server_addr == 0)
437 gethostname (buf, sizeof(buf));
439 hostent = gethostbyname (buf);
443 memcpy (&inet_addr.s_addr, hostent->h_addr,
444 sizeof(inet_addr.s_addr));
445 server_addr = (char *) inet_ntoa (inet_addr);
447 server_addr = (char *) inet_ntoa (hostent->h_addr);
450 if (server_addr == 0) /* failed? */
451 error ("Need to know gdb host computer's IP address (use 'set server-address')");
454 if (args == 0 || args[0] == 0) /* no args: upload the current file */
455 args = get_exec_file (1);
457 if (args[0] != '/' && download_path == 0)
458 if (current_directory)
459 download_path = strsave (current_directory);
461 error ("Need to know default download path (use 'set download-path')");
463 start_time = time (NULL);
464 monitor_printf ("uhip %s\r", server_addr);
465 resp_len = monitor_expect_prompt (buf, sizeof(buf)); /* parse result? */
466 monitor_printf ("ulip %s\r", board_addr);
467 resp_len = monitor_expect_prompt (buf, sizeof(buf)); /* parse result? */
469 monitor_printf ("up %s\r", download_path); /* use default path */
471 monitor_printf ("up\r"); /* rooted filename/path */
472 resp_len = monitor_expect_prompt (buf, sizeof(buf)); /* parse result? */
474 if (strrchr (args, '.') && !strcmp (strrchr (args, '.'), ".srec"))
475 monitor_printf ("ul %s\r", args);
476 else /* add ".srec" suffix */
477 monitor_printf ("ul %s.srec\r", args);
478 resp_len = monitor_expect_prompt (buf, sizeof(buf)); /* parse result? */
480 if (buf[0] == 0 || strstr(buf, "complete") == 0)
481 error("Upload file not found: %s.srec\nCheck IP addresses and download path.", args);
483 printf_filtered (" -- Ethernet load complete.\n");
485 end_time = time (NULL);
486 if (abfd = bfd_openr (args, 0))
487 { /* Download is done -- print section statistics */
488 if (bfd_check_format (abfd, bfd_object) == 0)
490 printf_filtered ("File is not an object file\n");
492 for (s = abfd->sections; s; s = s->next)
493 if (s->flags & SEC_LOAD)
495 bfd_size_type section_size = bfd_section_size (abfd, s);
496 bfd_vma section_base = bfd_section_vma (abfd, s);
499 data_count += section_size;
501 printf_filtered ("Loading section %s, size 0x%lx vma ",
502 bfd_section_name (abfd, s), section_size);
503 print_address_numeric (section_base, 1, gdb_stdout);
504 printf_filtered ("\n");
505 gdb_flush (gdb_stdout);
507 /* Finally, make the PC point at the start address */
508 write_pc (bfd_get_start_address (abfd));
509 report_transfer_performance (data_count, start_time, end_time);
510 printf_filtered ("Start address 0x%lx\n", bfd_get_start_address (abfd));
512 inferior_pid = 0; /* No process now */
514 /* This is necessary because many things were based on the PC at the
515 time that we attached to the monitor, which is no longer valid
516 now that we have loaded new code (and just changed the PC).
517 Another way to do this might be to call normal_stop, except that
518 the stack may not be valid, and things would get horribly
521 clear_symtab_users ();
525 _initialize_m32r_rom ()
527 init_monitor_ops (&m32r_ops);
529 m32r_ops.to_shortname = "m32r";
530 m32r_ops.to_longname = "m32r monitor";
531 m32r_ops.to_load = m32r_load_gen; /* monitor lacks a download command */
532 m32r_ops.to_doc = "Debug via the m32r monitor.\n\
533 Specify the serial device it is connected to (e.g. /dev/ttya).";
534 m32r_ops.to_open = m32r_open;
537 (add_set_cmd ("download-path", class_obscure, var_string,
538 (char *) &download_path,
539 "Set the default path for downloadable SREC files.",
544 (add_set_cmd ("board-address", class_obscure, var_string,
545 (char *) &board_addr,
546 "Set IP address for M32R-EVA target board.",
551 (add_set_cmd ("server-address", class_obscure, var_string,
552 (char *) &server_addr,
553 "Set IP address for download server (GDB's host computer).",
557 add_com ("upload", class_obscure, m32r_upload_command,
558 "Upload the srec file via the monitor's Ethernet upload capability.");
560 add_com ("tload", class_obscure, m32r_load, "test upload command.");
562 add_target (&m32r_ops);