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