Windows support bits from Steve Chamberlain <sac@slash.cygnus.com>.
[deliverable/binutils-gdb.git] / gdb / remote-hms.c
1 /* Remote debugging interface for Hitachi HMS Monitor Version 1.0
2 Copyright 1995 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Steve Chamberlain
4 (sac@cygnus.com).
5
6 This file is part of GDB.
7
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.
12
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.
17
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 #include "defs.h"
23 #include "gdbcore.h"
24 #include "target.h"
25 #include "monitor.h"
26 #include "serial.h"
27
28 static void hms_open PARAMS ((char *args, int from_tty));
29
30 static void
31 hms_supply_register (regname, regnamelen, val, vallen)
32 char *regname;
33 int regnamelen;
34 char *val;
35 int vallen;
36 {
37 int regno;
38
39 if (regnamelen != 2)
40 return;
41 if (regname[0] != 'P')
42 return;
43 /* We scan off all the registers in one go */
44
45 val = monitor_supply_register (PC_REGNUM, val);
46 /* Skip the ccr string */
47 while (*val != '=' && *val)
48 val++;
49
50 val = monitor_supply_register (CCR_REGNUM, val + 1);
51
52 /* Skip up to rest of regs */
53 while (*val != '=' && *val)
54 val++;
55
56 for (regno = 0; regno < 7; regno++)
57 {
58 val = monitor_supply_register (regno, val + 1);
59 }
60 }
61
62 /*
63 * This array of registers needs to match the indexes used by GDB. The
64 * whole reason this exists is because the various ROM monitors use
65 * different names than GDB does, and don't support all the
66 * registers either. So, typing "info reg sp" becomes a "r30".
67 */
68
69 static char *hms_regnames[NUM_REGS] =
70 {
71 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "CCR", "PC"
72 };
73
74 /*
75 * Define the monitor command strings. Since these are passed directly
76 * through to a printf style function, we need can include formatting
77 * strings. We also need a CR or LF on the end.
78 */
79
80 static struct target_ops hms_ops;
81
82 static char *hms_inits[] =
83 {"\003", /* Resets the prompt, and clears repeated cmds */
84 NULL};
85
86 static struct monitor_ops hms_cmds =
87 {
88 MO_CLR_BREAK_USES_ADDR | MO_FILL_USES_ADDR | MO_GETMEM_NEEDS_RANGE,
89 hms_inits, /* Init strings */
90 "g\r", /* continue command */
91 "s\r", /* single step */
92 "\003", /* ^C interrupts the program */
93 "b %x\r", /* set a breakpoint */
94 "b - %x\r", /* clear a breakpoint */
95 "b -\r", /* clear all breakpoints */
96 "f %x %x %x\r", /* fill (start end val) */
97 {
98 "m.b %x=%x\r", /* setmem.cmdb (addr, value) */
99 "m.w %x=%x\r", /* setmem.cmdw (addr, value) */
100 NULL, /* setmem.cmdl (addr, value) */
101 NULL, /* setmem.cmdll (addr, value) */
102 NULL, /* setreg.resp_delim */
103 NULL, /* setreg.term */
104 NULL, /* setreg.term_cmd */
105 },
106 {
107 "m.b %x %x\r", /* getmem.cmdb (addr, addr) */
108 "m.w %x %x\r", /* getmem.cmdw (addr, addr) */
109 NULL, /* getmem.cmdl (addr, addr) */
110 NULL, /* getmem.cmdll (addr, addr) */
111 ": ", /* getmem.resp_delim */
112 ">", /* getmem.term */
113 "\003", /* getmem.term_cmd */
114 },
115 {
116 "\003r %s=%x\r", /* setreg.cmd (name, value) */
117 NULL, /* setreg.resp_delim */
118 NULL, /* setreg.term */
119 NULL /* setreg.term_cmd */
120 },
121 {
122 "r %s\r", /* getreg.cmd (name) */
123 " (", /* getreg.resp_delim */
124 "):", /* getreg.term */
125 "\003", /* getreg.term_cmd */
126 },
127 "r\r", /* dump_registers */
128 "\\(\\w+\\)=\\([0-9a-fA-F]+\\)", /* register_pattern */
129 hms_supply_register, /* supply_register */
130 NULL, /* load_routine (defaults to SRECs) */
131 "tl\r", /* download command */
132 NULL, /* load response */
133 ">", /* monitor command prompt */
134 NULL, /* end-of-command delimitor */
135 NULL, /* optional command terminator */
136 &hms_ops, /* target operations */
137 SERIAL_1_STOPBITS, /* number of stop bits */
138 hms_regnames, /* registers names */
139 MONITOR_OPS_MAGIC /* magic */
140 };
141
142 void
143 hms_open (args, from_tty)
144 char *args;
145 int from_tty;
146 {
147 monitor_open (args, &hms_cmds, from_tty);
148 }
149
150
151 int write_dos_tick_delay;
152 void
153 _initialize_remote_hms ()
154 {
155 init_monitor_ops (&hms_ops);
156
157 hms_ops.to_shortname = "hms";
158 hms_ops.to_longname = "Hitachi Microsystems H8/300 debug monitor";
159 hms_ops.to_doc = "Debug via the HMS monitor.\n\
160 Specify the serial device it is connected to (e.g. /dev/ttya).";
161 hms_ops.to_open = hms_open;
162 /* By trial and error I've found that this delay doesn't break things */
163 write_dos_tick_delay = 1;
164 add_target (&hms_ops);
165 }
166
167
168
169 #if 0
170 /* This is kept here because we used to support the H8/500 in this module,
171 and I haven't done the H8/500 yet */
172 #include "defs.h"
173 #include "inferior.h"
174 #include "wait.h"
175 #include "value.h"
176 #include <string.h>
177 #include <ctype.h>
178 #include <fcntl.h>
179 #include <signal.h>
180 #include <setjmp.h>
181 #include <errno.h>
182 #include "terminal.h"
183 #include "target.h"
184 #include "gdbcore.h"
185 #include "serial.h"
186 #include "remote-utils.h"
187 /* External data declarations */
188 extern int stop_soon_quietly; /* for wait_for_inferior */
189
190 /* Forward data declarations */
191 extern struct target_ops hms_ops; /* Forward declaration */
192
193 /* Forward function declarations */
194 static void hms_fetch_registers ();
195 static int hms_store_registers ();
196 static void hms_close ();
197 static int hms_clear_breakpoints ();
198
199 extern struct target_ops hms_ops;
200 static void hms_drain ();
201 static void add_commands ();
202 static void remove_commands ();
203
204 static int quiet = 1; /* FIXME - can be removed after Dec '94 */
205
206 DCACHE *dcache_ptr;
207 int remote_dcache;
208 serial_t desc;
209
210
211 /***********************************************************************
212 * I/O stuff stolen from remote-eb.c
213 ***********************************************************************/
214
215 static int timeout = 2;
216
217 static const char *dev_name;
218
219 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
220 hms_open knows that we don't have a file open when the program
221 starts. */
222
223 static int before = 0xdead;
224 static int is_open = 0;
225 static int after = 0xdead;
226 int
227 check_open ()
228 {
229 if (before != 0xdead
230 || after != 0xdead)
231 printf ("OUTCH! \n");
232 if (!is_open)
233 {
234 error ("remote device not open");
235 }
236 }
237
238 #define ON 1
239 #define OFF 0
240
241 /* Read a character from the remote system, doing all the fancy
242 timeout stuff. */
243 static int
244 readchar ()
245 {
246 int buf;
247
248 buf = SERIAL_READCHAR (desc, timeout);
249
250 if (buf == SERIAL_TIMEOUT)
251 {
252 hms_write (".\r\n", 3);
253 error ("Timeout reading from remote system.");
254 }
255 if (buf == SERIAL_ERROR)
256 {
257 error ("Serial port error!");
258 }
259
260 if (!quiet || remote_debug)
261 printf_unfiltered ("%c", buf);
262
263 return buf & 0x7f;
264 }
265
266 static void
267 flush ()
268 {
269 while (1)
270 {
271 int b = SERIAL_READCHAR (desc, 0);
272 if (b == SERIAL_TIMEOUT)
273 return;
274 }
275 }
276
277 static int
278 readchar_nofail ()
279 {
280 int buf;
281
282 buf = SERIAL_READCHAR (desc, timeout);
283 if (buf == SERIAL_TIMEOUT)
284 buf = 0;
285 if (!quiet || remote_debug)
286 printf_unfiltered ("%c", buf);
287
288 return buf & 0x7f;
289
290 }
291
292 /* Keep discarding input from the remote system, until STRING is found.
293 Let the user break out immediately. */
294 static void
295 expect (string)
296 char *string;
297 {
298 char *p = string;
299 char c;
300 immediate_quit = 1;
301 while (1)
302 {
303 c = readchar ();
304 if (c == *p)
305 {
306 p++;
307 if (*p == '\0')
308 {
309 immediate_quit = 0;
310 return;
311 }
312 }
313 else
314 {
315 p = string;
316 if (c == *p)
317 p++;
318 }
319 }
320 }
321
322 /* Keep discarding input until we see the hms prompt.
323
324 The convention for dealing with the prompt is that you
325 o give your command
326 o *then* wait for the prompt.
327
328 Thus the last thing that a procedure does with the serial line
329 will be an expect_prompt(). Exception: hms_resume does not
330 wait for the prompt, because the terminal is being handed over
331 to the inferior. However, the next thing which happens after that
332 is a hms_wait which does wait for the prompt.
333 Note that this includes abnormal exit, e.g. error(). This is
334 necessary to prevent getting into states from which we can't
335 recover. */
336 static void
337 expect_prompt ()
338 {
339 expect ("HMS>");
340 }
341
342 /* Get a hex digit from the remote system & return its value.
343 If ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
344 static int
345 get_hex_digit (ignore_space)
346 int ignore_space;
347 {
348 int ch;
349
350 while (1)
351 {
352 ch = readchar ();
353 if (ch >= '0' && ch <= '9')
354 return ch - '0';
355 else if (ch >= 'A' && ch <= 'F')
356 return ch - 'A' + 10;
357 else if (ch >= 'a' && ch <= 'f')
358 return ch - 'a' + 10;
359 else if (ch == ' ' && ignore_space)
360 ;
361 else
362 {
363 expect_prompt ();
364 error ("Invalid hex digit from remote system.");
365 }
366 }
367 }
368
369 /* Get a byte from hms_desc and put it in *BYT. Accept any number
370 leading spaces. */
371 static void
372 get_hex_byte (byt)
373 char *byt;
374 {
375 int val;
376
377 val = get_hex_digit (1) << 4;
378 val |= get_hex_digit (0);
379 *byt = val;
380 }
381
382 /* Read a 32-bit hex word from the hms, preceded by a space */
383 static long
384 get_hex_word ()
385 {
386 long val;
387 int j;
388
389 val = 0;
390 for (j = 0; j < 8; j++)
391 val = (val << 4) + get_hex_digit (j == 0);
392 return val;
393 }
394
395 /* Called when SIGALRM signal sent due to alarm() timeout. */
396
397 /* Number of SIGTRAPs we need to simulate. That is, the next
398 NEED_ARTIFICIAL_TRAP calls to hms_wait should just return
399 SIGTRAP without actually waiting for anything. */
400
401 static int need_artificial_trap = 0;
402
403 void
404 hms_kill (arg, from_tty)
405 char *arg;
406 int from_tty;
407 {
408
409 }
410
411 /* This is called not only when we first attach, but also when the
412 user types "run" after having attached. */
413 void
414 hms_create_inferior (execfile, args, env)
415 char *execfile;
416 char *args;
417 char **env;
418 {
419 int entry_pt;
420 char buffer[100];
421
422 if (args && *args)
423 error ("Can't pass arguments to remote hms process.");
424
425 if (execfile == 0 || exec_bfd == 0)
426 error ("No exec file specified");
427
428 entry_pt = (int) bfd_get_start_address (exec_bfd);
429 check_open ();
430
431 hms_kill (NULL, NULL);
432 hms_clear_breakpoints ();
433 init_wait_for_inferior ();
434 hms_write_cr ("");
435 expect_prompt ();
436
437 insert_breakpoints (); /* Needed to get correct instruction in cache */
438 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
439 }
440
441 /* Open a connection to a remote debugger.
442 NAME is the filename used for communication, then a space,
443 then the baud rate.
444 */
445
446 static char *
447 find_end_of_word (s)
448 char *s;
449 {
450 while (*s && !isspace (*s))
451 s++;
452 return s;
453 }
454
455 static char *
456 get_word (p)
457 char **p;
458 {
459 char *s = *p;
460 char *word;
461 char *copy;
462 size_t len;
463
464 while (isspace (*s))
465 s++;
466
467 word = s;
468
469 len = 0;
470
471 while (*s && !isspace (*s))
472 {
473 s++;
474 len++;
475
476 }
477 copy = xmalloc (len + 1);
478 memcpy (copy, word, len);
479 copy[len] = 0;
480 *p = s;
481 return copy;
482 }
483
484 static int baudrate = 9600;
485
486 static int
487 is_baudrate_right ()
488 {
489 int ok;
490
491 /* Put this port into NORMAL mode, send the 'normal' character */
492
493 hms_write ("\001", 1); /* Control A */
494 hms_write ("\r\n", 2); /* Cr */
495
496 while (1)
497 {
498 ok = SERIAL_READCHAR (desc, timeout);
499 if (ok < 0)
500 break;
501 }
502
503 hms_write ("r", 1);
504
505 if (readchar_nofail () == 'r')
506 return 1;
507
508 /* Not the right baudrate, or the board's not on */
509 return 0;
510 }
511 static void
512 set_rate ()
513 {
514 if (!SERIAL_SETBAUDRATE (desc, baudrate))
515 error ("Can't set baudrate");
516 }
517
518
519
520 /* Close out all files and local state before this target loses control. */
521
522 static void
523 hms_close (quitting)
524 int quitting;
525 {
526 /* Clear any break points */
527 remove_commands ();
528 hms_clear_breakpoints ();
529 sleep (1); /* Let any output make it all the way back */
530 if (is_open)
531 {
532 SERIAL_WRITE (desc, "R\r\n", 3);
533 SERIAL_CLOSE (desc);
534 }
535 is_open = 0;
536 remote_dcache = 0;
537 }
538
539 /* Terminate the open connection to the remote debugger. Use this
540 when you want to detach and do something else with your gdb. */ void
541 hms_detach (args, from_tty)
542 char *args;
543 int from_tty;
544 {
545 if (is_open)
546 {
547 hms_clear_breakpoints ();
548 }
549
550 pop_target (); /* calls hms_close to do the real work
551 */
552 if (from_tty)
553 printf_filtered ("Ending remote %s debugging\n",
554 target_shortname);
555 }
556
557 /* Tell the remote machine to resume. */
558
559 void
560 hms_resume (pid, step, sig)
561 int pid, step;
562 enum target_signal
563 sig;
564 {
565 dcache_flush (dcache_ptr);
566
567 if (step)
568 {
569 hms_write_cr ("s");
570 expect ("Step>");
571
572 /* Force the next hms_wait to return a trap. Not doing anything
573 about I/O from the target means that the user has to type "continue"
574 to see any. FIXME, this should be fixed. */
575 need_artificial_trap = 1;
576 }
577 else
578 {
579 hms_write_cr ("g");
580 expect ("g");
581 }
582 }
583
584 /* Wait until the remote machine stops, then return, storing status in
585 STATUS just as `wait' would. */
586
587 int
588 hms_wait (pid, status)
589 int pid;
590 struct target_waitstatus *status;
591 {
592 /* Strings to look for. '?' means match any single character. Note
593 that with the algorithm we use, the initial character of the string
594 cannot recur in the string, or we will not find some cases of the
595 string in the input. */
596
597 static char bpt[] = "At breakpoint:";
598
599 /* It would be tempting to look for "\n[__exit + 0x8]\n" but that
600 requires loading symbols with "yc i" and even if we did do that we
601 don't know that the file has symbols. */
602 static char exitmsg[] = "HMS>";
603 char *bp = bpt;
604 char *ep = exitmsg;
605
606 /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars.
607 */
608 char swallowed[50];
609
610 /* Current position in swallowed. */
611 char *swallowed_p = swallowed;
612
613 int ch;
614 int ch_handled;
615 int old_timeout = timeout;
616 int
617 old_immediate_quit = immediate_quit;
618 int swallowed_cr = 0;
619
620 status->kind = TARGET_WAITKIND_EXITED;
621 status->value.integer = 0;
622
623 if (need_artificial_trap != 0)
624 {
625 status->kind =
626 TARGET_WAITKIND_STOPPED;
627 status->value.sig = TARGET_SIGNAL_TRAP;
628 need_artificial_trap--;
629 return 0;
630 }
631
632 timeout = 5; /* Don't time out for a while - user program is running.
633 */
634 immediate_quit = 1; /* Helps ability to QUIT */
635 while (1)
636 {
637 QUIT; /* Let user quit and leave process running */
638 ch_handled = 0;
639 ch = readchar ();
640 if (ch == *bp)
641 {
642 bp++;
643 if (*bp == '\0')
644 break;
645 ch_handled = 1;
646
647 *swallowed_p++ = ch;
648 }
649 else
650 {
651 bp = bpt;
652 }
653 if
654 (ch == *ep || *ep == '?')
655 {
656 ep++;
657 if (*ep == '\0')
658 break;
659
660 if (!ch_handled)
661 *swallowed_p++ = ch;
662 ch_handled =
663 1;
664 }
665 else
666 {
667 ep = exitmsg;
668 }
669
670 if (!ch_handled)
671 {
672 char *p;
673
674 /* Print out any characters which have been swallowed. */
675 for (p = swallowed; p < swallowed_p; ++p)
676 putchar_unfiltered (*p);
677 swallowed_p = swallowed;
678
679 if ((ch != '\r' && ch != '\n') || swallowed_cr > 10)
680 {
681 putchar_unfiltered (ch);
682 swallowed_cr = 10;
683 }
684 swallowed_cr++;
685
686 }
687 }
688 if (*bp == '\0')
689 {
690 status->kind = TARGET_WAITKIND_STOPPED;
691 status->value.sig = TARGET_SIGNAL_TRAP;
692 expect_prompt ();
693 }
694 else
695 {
696 status->kind = TARGET_WAITKIND_EXITED;
697 status->value.integer =
698 TARGET_SIGNAL_STOP;
699 }
700
701 timeout = old_timeout;
702 immediate_quit = old_immediate_quit;
703 return
704 0;
705 }
706
707 /* Return the name of register number REGNO in the form input and
708 output by hms.
709
710 Returns a pointer to a static buffer containing the answer. */
711 static char *
712 get_reg_name (regno)
713 int regno;
714 {
715 static char *rn[] =
716 REGISTER_NAMES;
717
718 return rn[regno];
719 }
720
721 /* Read the remote registers. */
722
723 static int
724 gethex (length, start, ok)
725 unsigned int length;
726 char *start;
727 int *ok;
728 {
729 int result = 0;
730
731 while (length--)
732 {
733 result <<= 4;
734 if (*start >= 'a' && *start <= 'f')
735 {
736 result += *start - 'a' + 10;
737 }
738 else if (*start >= 'A' &&
739 *start <= 'F')
740 {
741 result += *start - 'A' + 10;
742 }
743 else if
744 (*start >= '0' && *start <= '9')
745 {
746 result += *start - '0';
747 }
748 else
749 *ok = 0;
750 start++;
751
752 }
753 return result;
754 }
755 static int
756 timed_read (buf, n, timeout)
757 char
758 *buf;
759
760 {
761 int i;
762 char c;
763
764 i = 0;
765 while (i < n)
766 {
767 c = readchar ();
768
769 if (c == 0)
770 return i;
771 buf[i] = c;
772 i++;
773
774 }
775 return i;
776 }
777
778 hms_write (a, l)
779 char *a;
780 {
781 int i;
782
783 SERIAL_WRITE (desc, a, l);
784
785 if (!quiet || remote_debug)
786 {
787 printf_unfiltered ("<");
788 for (i = 0; i < l; i++)
789 {
790 printf_unfiltered ("%c", a[i]);
791 }
792 printf_unfiltered (">");
793 }
794 }
795
796 hms_write_cr (s)
797 char *s;
798 {
799 hms_write (s, strlen (s));
800 hms_write ("\r\n", 2);
801 }
802
803 #ifdef GDB_TARGET_IS_H8500
804
805 /* H8/500 monitor reg dump looks like:
806
807 HMS>r
808 PC:8000 SR:070C .7NZ.. CP:00 DP:00 EP:00 TP:00 BR:00
809 R0-R7: FF5A 0001 F4FE F500 0000 F528 F528 F4EE
810 HMS>
811
812
813 */
814
815 supply_val (n, size, ptr, segptr)
816 int n;
817 int size;
818 char *ptr;
819 char *segptr;
820 {
821 int ok;
822 char raw[4];
823 switch (size)
824 {
825 case 2:
826 raw[0] = gethex (2, ptr, &ok);
827 raw[1] = gethex (2, ptr + 2, &ok);
828 supply_register (n, raw);
829 break;
830 case 1:
831 raw[0] = gethex (2, ptr, &ok);
832 supply_register (n, raw);
833 break;
834 case 4:
835 {
836 int v = gethex (4, ptr, &ok);
837 v |= gethex (2, segptr, &ok) << 16;
838 raw[0] = 0;
839 raw[1] = (v >> 16) & 0xff;
840 raw[2] = (v >> 8) & 0xff;
841 raw[3] = (v >> 0) & 0xff;
842 supply_register (n, raw);
843 }
844 }
845
846 }
847 static void
848 hms_fetch_register (dummy)
849 int dummy;
850 {
851 #define REGREPLY_SIZE 108
852 char linebuf[REGREPLY_SIZE + 1];
853 int i;
854 int s;
855 int gottok;
856
857 LONGEST reg[NUM_REGS];
858 check_open ();
859
860 do
861 {
862
863 hms_write_cr ("r");
864 expect ("r");
865 s = timed_read (linebuf + 1, REGREPLY_SIZE, 1);
866
867 linebuf[REGREPLY_SIZE] = 0;
868 gottok = 0;
869 if (linebuf[3] == 'P' &&
870 linebuf[4] == 'C' &&
871 linebuf[5] == ':' &&
872 linebuf[105] == 'H' &&
873 linebuf[106] == 'M' &&
874 linebuf[107] == 'S')
875 {
876
877 /*
878 012
879 r**
880 -------1---------2---------3---------4---------5-----
881 345678901234567890123456789012345678901234567890123456
882 PC:8000 SR:070C .7NZ.. CP:00 DP:00 EP:00 TP:00 BR:00**
883 ---6---------7---------8---------9--------10----
884 789012345678901234567890123456789012345678901234
885 R0-R7: FF5A 0001 F4FE F500 0000 F528 F528 F4EE**
886
887 56789
888 HMS>
889 */
890 gottok = 1;
891
892
893 supply_val (PC_REGNUM, 4, linebuf + 6, linebuf + 29);
894
895 supply_val (CCR_REGNUM, 2, linebuf + 14);
896 supply_val (SEG_C_REGNUM, 1, linebuf + 29);
897 supply_val (SEG_D_REGNUM, 1, linebuf + 35);
898 supply_val (SEG_E_REGNUM, 1, linebuf + 41);
899 supply_val (SEG_T_REGNUM, 1, linebuf + 47);
900 for (i = 0; i < 8; i++)
901 {
902 static int sr[8] =
903 {35, 35, 35, 35,
904 41, 41, 47, 47};
905
906 char raw[4];
907 char *src = linebuf + 64 + 5 * i;
908 char *segsrc = linebuf + sr[i];
909 supply_val (R0_REGNUM + i, 2, src);
910 supply_val (PR0_REGNUM + i, 4, src, segsrc);
911 }
912 }
913 if (!gottok)
914 {
915 hms_write_cr ("");
916 expect ("HMS>");
917 }
918 }
919 while (!gottok);
920 }
921 #endif
922
923 #ifdef GDB_TARGET_IS_H8300
924 static void
925 hms_fetch_register (dummy)
926 int dummy;
927 {
928 #define REGREPLY_SIZE 79
929 char linebuf[REGREPLY_SIZE + 1];
930 int i;
931 int s;
932 int gottok;
933
934 unsigned LONGEST reg[NUM_REGS];
935
936 check_open ();
937
938 do
939 {
940 hms_write_cr ("r");
941
942 s = timed_read (linebuf, 1, 1);
943
944 while (linebuf[0] != 'r')
945 s = timed_read (linebuf, 1, 1);
946
947 s = timed_read (linebuf + 1, REGREPLY_SIZE - 1, 1);
948
949 linebuf[REGREPLY_SIZE] = 0;
950 gottok = 0;
951 if (linebuf[0] == 'r' &&
952 linebuf[3] == 'P' &&
953 linebuf[4] == 'C' &&
954 linebuf[5] == '=' &&
955 linebuf[75] == 'H' &&
956 linebuf[76] == 'M' &&
957 linebuf[77] == 'S')
958 {
959 /*
960 PC=XXXX CCR=XX:XXXXXXXX R0-R7= XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX
961 5436789012345678901234567890123456789012345678901234567890123456789012
962 0 1 2 3 4 5 6
963 */
964 gottok = 1;
965
966 reg[PC_REGNUM] = gethex (4, linebuf + 6, &gottok);
967 reg[CCR_REGNUM] = gethex (2, linebuf + 15, &gottok);
968 for (i = 0; i < 8; i++)
969 {
970 reg[i] = gethex (4, linebuf + 34 + 5 * i, &gottok);
971 }
972 }
973 }
974 while (!gottok);
975 for (i = 0; i < NUM_REGS; i++)
976 {
977 char swapped[2];
978
979 swapped[1] = reg[i];
980 swapped[0] = (reg[i]) >> 8;
981
982 supply_register (i, swapped);
983 }
984 }
985 #endif
986 /* Store register REGNO, or all if REGNO == -1.
987 Return errno value. */
988 static void
989 hms_store_register (regno)
990 int regno;
991 {
992 if (regno == -1)
993 {
994 for (regno = 0; regno < NUM_REGS; regno++)
995 {
996 hms_store_register (regno);
997 }
998 }
999 else
1000 {
1001 char *name = get_reg_name (regno);
1002 char buffer[100];
1003 /* Some regs dont really exist */
1004 if (!(name[0] == 'p' && name[1] == 'r')
1005 && !(name[0] == 'c' && name[1] == 'y')
1006 && !(name[0] == 't' && name[1] == 'i')
1007 && !(name[0] == 'i' && name[1] == 'n'))
1008 {
1009 sprintf (buffer, "r %s=%x", name, read_register (regno));
1010 hms_write_cr (buffer);
1011 expect_prompt ();
1012 }
1013 }
1014 }
1015
1016
1017 /* Get ready to modify the registers array. On machines which store
1018 individual registers, this doesn't need to do anything. On machines
1019 which store all the registers in one fell swoop, this makes sure
1020 that registers contains all the registers from the program being
1021 debugged. */
1022
1023 void
1024 hms_prepare_to_store ()
1025 {
1026 /* Do nothing, since we can store individual regs */
1027 }
1028
1029 static CORE_ADDR
1030 translate_addr (addr)
1031 CORE_ADDR addr;
1032 {
1033
1034 return (addr);
1035
1036 }
1037
1038 /* Read a word from remote address ADDR and return it.
1039 * This goes through the data cache.
1040 */
1041 int
1042 hms_fetch_word (addr)
1043 CORE_ADDR addr;
1044 {
1045 return dcache_fetch (dcache_ptr, addr);
1046 }
1047
1048 /* Write a word WORD into remote address ADDR.
1049 This goes through the data cache. */
1050
1051 void
1052 hms_store_word (addr, word)
1053 CORE_ADDR addr;
1054 int word;
1055 {
1056 dcache_poke (dcache_ptr, addr, word);
1057 }
1058
1059 int
1060 hms_xfer_inferior_memory (memaddr, myaddr, len, write, target)
1061 CORE_ADDR memaddr;
1062 char *myaddr;
1063 int len;
1064 int write;
1065 struct target_ops *target; /* ignored */
1066 {
1067 register int i;
1068
1069 /* Round starting address down to longword boundary. */
1070 register CORE_ADDR addr;
1071
1072 /* Round ending address up; get number of longwords that makes. */
1073 register int count;
1074
1075 /* Allocate buffer of that many longwords. */
1076 register int *buffer;
1077
1078 memaddr &= 0xffff;
1079 addr = memaddr & -sizeof (int);
1080 count = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
1081
1082 buffer = (int *) alloca (count * sizeof (int));
1083
1084 if (write)
1085 {
1086 /* Fill start and end extra bytes of buffer with existing memory data. */
1087
1088 if (addr != memaddr || len < (int) sizeof (int))
1089 {
1090 /* Need part of initial word -- fetch it. */
1091 buffer[0] = hms_fetch_word (addr);
1092 }
1093
1094 if (count > 1) /* FIXME, avoid if even boundary */
1095 {
1096 buffer[count - 1]
1097 = hms_fetch_word (addr + (count - 1) * sizeof (int));
1098 }
1099
1100 /* Copy data to be written over corresponding part of buffer */
1101
1102 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
1103
1104 /* Write the entire buffer. */
1105
1106 for (i = 0; i < count; i++, addr += sizeof (int))
1107 {
1108 errno = 0;
1109 hms_store_word (addr, buffer[i]);
1110 if (errno)
1111 {
1112 return 0;
1113 }
1114
1115 }
1116 }
1117 else
1118 {
1119 /* Read all the longwords */
1120 for (i = 0; i < count; i++, addr += sizeof (int))
1121 {
1122 errno = 0;
1123 buffer[i] = hms_fetch_word (addr);
1124 if (errno)
1125 {
1126 return 0;
1127 }
1128 QUIT;
1129 }
1130
1131 /* Copy appropriate bytes out of the buffer. */
1132 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
1133 }
1134
1135 return len;
1136 }
1137
1138 int
1139 hms_write_inferior_memory (memaddr, myaddr, len)
1140 CORE_ADDR memaddr;
1141 unsigned char *myaddr;
1142 int len;
1143 {
1144 bfd_vma addr;
1145 int done;
1146 int todo;
1147 char buffer[100];
1148 done = 0;
1149 hms_write_cr (".");
1150 expect_prompt ();
1151 while (done < len)
1152 {
1153 char *ptr = buffer;
1154 int thisgo;
1155 int idx;
1156
1157 thisgo = len - done;
1158 if (thisgo > 20)
1159 thisgo = 20;
1160
1161 sprintf (ptr, "M.B %4x =", memaddr + done);
1162 ptr += 10;
1163 for (idx = 0; idx < thisgo; idx++)
1164 {
1165 sprintf (ptr, "%2x ", myaddr[idx + done]);
1166 ptr += 3;
1167 }
1168 hms_write_cr (buffer);
1169 expect_prompt ();
1170 done += thisgo;
1171 }
1172 }
1173
1174 void
1175 hms_files_info ()
1176 {
1177 char *file = "nothing";
1178
1179 if (exec_bfd)
1180 file = bfd_get_filename (exec_bfd);
1181
1182 if (exec_bfd)
1183 #ifdef __GO32__
1184 printf_filtered ("\tAttached to DOS asynctsr and running program %s\n", file);
1185 #else
1186 printf_filtered ("\tAttached to %s at %d baud and running program %s\n", dev_name, baudrate, file);
1187 #endif
1188 printf_filtered ("\ton an H8/300 processor.\n");
1189 }
1190
1191 /* Copy LEN bytes of data from debugger memory at MYADDR
1192 to inferior's memory at MEMADDR. Returns errno value.
1193 * sb/sh instructions don't work on unaligned addresses, when TU=1.
1194 */
1195
1196 /* Read LEN bytes from inferior memory at MEMADDR. Put the result
1197 at debugger address MYADDR. Returns errno value. */
1198 int
1199 hms_read_inferior_memory (memaddr, myaddr, len)
1200 CORE_ADDR memaddr;
1201 char *myaddr;
1202 int len;
1203 {
1204 /* Align to nearest low 16 bits */
1205 int i;
1206
1207 CORE_ADDR start = memaddr;
1208 CORE_ADDR end = memaddr + len - 1;
1209
1210 int ok = 1;
1211
1212 /*
1213 AAAA: XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX '................'
1214 012345678901234567890123456789012345678901234567890123456789012345
1215 0 1 2 3 4 5 6
1216 */
1217 char buffer[66];
1218
1219 if (memaddr & 0xf)
1220 abort ();
1221 if (len != 16)
1222 abort ();
1223
1224 sprintf (buffer, "m %4x %4x", start & 0xffff, end & 0xffff);
1225
1226 flush ();
1227 hms_write_cr (buffer);
1228 /* drop the echo and newline */
1229 for (i = 0; i < 13; i++)
1230 readchar ();
1231
1232 /* Grab the lines as they come out and fill the area */
1233 /* Skip over cr */
1234 while (1)
1235 {
1236 int p;
1237 int i;
1238 int addr;
1239 size_t idx;
1240
1241 char byte[16];
1242
1243 buffer[0] = readchar ();
1244 while (buffer[0] == '\r'
1245 || buffer[0] == '\n')
1246 buffer[0] = readchar ();
1247
1248 if (buffer[0] == 'M')
1249 break;
1250
1251 for (i = 1; i < 50; i++)
1252 {
1253 buffer[i] = readchar ();
1254 }
1255 /* sometimes we loose characters in the ascii representation of the
1256 data. I don't know where. So just scan for the end of line */
1257 i = readchar ();
1258 while (i != '\n' && i != '\r')
1259 i = readchar ();
1260
1261 /* Now parse the line */
1262
1263 addr = gethex (4, buffer, &ok);
1264 idx = 6;
1265 for (p = 0; p < 16; p += 2)
1266 {
1267 byte[p] = gethex (2, buffer + idx, &ok);
1268 byte[p + 1] = gethex (2, buffer + idx + 2, &ok);
1269 idx += 5;
1270 }
1271
1272 for (p = 0; p < 16; p++)
1273 {
1274 if (addr + p >= memaddr &&
1275 addr + p < memaddr + len)
1276 {
1277 myaddr[(addr + p) - memaddr] = byte[p];
1278
1279 }
1280
1281 }
1282 }
1283 #ifdef GDB_TARGET_IS_H8500
1284 expect ("ore>");
1285 #endif
1286 #ifdef GDB_TARGET_IS_H8300
1287 expect ("emory>");
1288 #endif
1289 hms_write_cr (".");
1290
1291 expect_prompt ();
1292 return len;
1293 }
1294
1295
1296
1297 #define MAX_BREAKS 16
1298 static int num_brkpts = 0;
1299 static int
1300 hms_insert_breakpoint (addr, save)
1301 CORE_ADDR addr;
1302 char *save; /* Throw away, let hms save instructions */
1303 {
1304 check_open ();
1305
1306 if (num_brkpts < MAX_BREAKS)
1307 {
1308 char buffer[100];
1309
1310 num_brkpts++;
1311 sprintf (buffer, "b %x", addr & 0xffff);
1312 hms_write_cr (buffer);
1313 expect_prompt ();
1314 return (0);
1315 }
1316 else
1317 {
1318 fprintf_filtered (gdb_stderr,
1319 "Too many break points, break point not installed\n");
1320 return (1);
1321 }
1322
1323 }
1324 static int
1325 hms_remove_breakpoint (addr, save)
1326 CORE_ADDR addr;
1327 char *save; /* Throw away, let hms save instructions */
1328 {
1329 if (num_brkpts > 0)
1330 {
1331 char buffer[100];
1332
1333 num_brkpts--;
1334 sprintf (buffer, "b - %x", addr & 0xffff);
1335 hms_write_cr (buffer);
1336 expect_prompt ();
1337
1338 }
1339 return (0);
1340 }
1341
1342 /* Clear the hmss notion of what the break points are */
1343 static int
1344 hms_clear_breakpoints ()
1345 {
1346
1347 if (is_open)
1348 {
1349 hms_write_cr ("b -");
1350 expect_prompt ();
1351 }
1352 num_brkpts = 0;
1353 }
1354 static void
1355 hms_mourn ()
1356 {
1357 hms_clear_breakpoints ();
1358 unpush_target (&hms_ops);
1359 generic_mourn_inferior ();
1360 }
1361
1362 /* Put a command string, in args, out to the hms. The hms is assumed to
1363 be in raw mode, all writing/reading done through desc.
1364 Ouput from the hms is placed on the users terminal until the
1365 prompt from the hms is seen.
1366 FIXME: Can't handle commands that take input. */
1367
1368 void
1369 hms_com (args, fromtty)
1370 char *args;
1371 int fromtty;
1372 {
1373 check_open ();
1374
1375 if (!args)
1376 return;
1377
1378 /* Clear all input so only command relative output is displayed */
1379
1380 hms_write_cr (args);
1381 /* hms_write ("\030", 1); */
1382 expect_prompt ();
1383 }
1384
1385 static void
1386 hms_open (name, from_tty)
1387 char *name;
1388 int from_tty;
1389 {
1390 unsigned int prl;
1391 char *p;
1392
1393 if (name == 0)
1394 {
1395 name = "";
1396 }
1397 if (is_open)
1398 hms_close (0);
1399 dev_name = strdup (name);
1400
1401 if (!(desc = SERIAL_OPEN (dev_name)))
1402 perror_with_name ((char *) dev_name);
1403
1404 SERIAL_RAW (desc);
1405 is_open = 1;
1406 push_target (&hms_ops);
1407 dcache_ptr = dcache_init (hms_read_inferior_memory,
1408 hms_write_inferior_memory);
1409 remote_dcache = 1;
1410 /* Hello? Are you there? */
1411 SERIAL_WRITE (desc, "\r\n", 2);
1412 expect_prompt ();
1413
1414 /* Clear any break points */
1415 hms_clear_breakpoints ();
1416
1417 printf_filtered ("Connected to remote board running HMS monitor.\n");
1418 add_commands ();
1419 /* hms_drain (); */
1420 }
1421
1422 /* Define the target subroutine names */
1423
1424 struct target_ops hms_ops =
1425 {
1426 "hms", "Remote HMS monitor",
1427 "Use the H8 evaluation board running the HMS monitor connected\n\
1428 by a serial line.",
1429
1430 hms_open, hms_close,
1431 0, hms_detach, hms_resume, hms_wait, /* attach */
1432 hms_fetch_register, hms_store_register,
1433 hms_prepare_to_store,
1434 hms_xfer_inferior_memory,
1435 hms_files_info,
1436 hms_insert_breakpoint, hms_remove_breakpoint, /* Breakpoints */
1437 0, 0, 0, 0, 0, /* Terminal handling */
1438 hms_kill, /* FIXME, kill */
1439 gr_load_image,
1440 0, /* lookup_symbol */
1441 hms_create_inferior, /* create_inferior */
1442 hms_mourn, /* mourn_inferior FIXME */
1443 0, /* can_run */
1444 0, /* notice_signals */
1445 0, /* to_stop */
1446 process_stratum, 0, /* next */
1447 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
1448 0, 0, /* Section pointers */
1449 OPS_MAGIC, /* Always the last thing */
1450 };
1451
1452 hms_quiet () /* FIXME - this routine can be removed after Dec '94 */
1453 {
1454 quiet = !quiet;
1455 if (quiet)
1456 printf_filtered ("Snoop disabled\n");
1457 else
1458 printf_filtered ("Snoop enabled\n");
1459
1460 printf_filtered ("`snoop' is obsolete, please use `set remotedebug'.\n");
1461 }
1462
1463 hms_device (s)
1464 char *s;
1465 {
1466 if (s)
1467 {
1468 dev_name = get_word (&s);
1469 }
1470 }
1471
1472 static
1473 hms_speed (s)
1474 char *s;
1475 {
1476 check_open ();
1477
1478 if (s)
1479 {
1480 char buffer[100];
1481 int newrate = atoi (s);
1482 int which = 0;
1483
1484 if (SERIAL_SETBAUDRATE (desc, newrate))
1485 error ("Can't use %d baud\n", newrate);
1486
1487 printf_filtered ("Checking target is in sync\n");
1488
1489 printf_filtered ("Sending commands to set target to %d\n",
1490 baudrate);
1491
1492 sprintf (buffer, "tm %d. N 8 1", baudrate);
1493 hms_write_cr (buffer);
1494 }
1495 }
1496
1497 /***********************************************************************/
1498
1499 static void
1500 hms_drain (args, fromtty)
1501 char *args;
1502 int fromtty;
1503 {
1504 int c;
1505 while (1)
1506 {
1507 c = SERIAL_READCHAR (desc, 1);
1508 if (c == SERIAL_TIMEOUT)
1509 break;
1510 if (c == SERIAL_ERROR)
1511 break;
1512 if (c > ' ' && c < 127)
1513 printf ("%c", c & 0xff);
1514 else
1515 printf ("<%x>", c & 0xff);
1516 }
1517 }
1518
1519 static void
1520 add_commands ()
1521 {
1522
1523 add_com ("hms_drain", class_obscure, hms_drain,
1524 "Drain pending hms text buffers.");
1525 }
1526
1527 static void
1528 remove_commands ()
1529 {
1530 extern struct cmd_list_element *cmdlist;
1531 delete_cmd ("hms-drain", &cmdlist);
1532 }
1533
1534
1535 void
1536 _initialize_remote_hms ()
1537 {
1538 add_target (&hms_ops);
1539
1540 add_com ("hms <command>", class_obscure, hms_com,
1541 "Send a command to the HMS monitor.");
1542
1543 /* FIXME - hms_quiet and `snoop' can be removed after Dec '94 */
1544 add_com ("snoop", class_obscure, hms_quiet,
1545 "Show what commands are going to the monitor (OBSOLETE - see 'set remotedebug')");
1546
1547 add_com ("device", class_obscure, hms_device,
1548 "Set the terminal line for HMS communications");
1549
1550 add_com ("speed", class_obscure, hms_speed,
1551 "Set the terminal line speed for HMS communications");
1552
1553 dev_name = NULL;
1554 }
1555 #endif
1556
This page took 0.076337 seconds and 4 git commands to generate.