* remote-hms.c, ser-go32.c, serial.h: Change from
[deliverable/binutils-gdb.git] / gdb / remote-hms.c
CommitLineData
1f46923f 1/* Remote debugging interface for Hitachi HMS Monitor Version 1.0
fa4b55a1 2 Copyright 1992 Free Software Foundation, Inc.
e17960fb
JG
3 Contributed by Cygnus Support. Written by Steve Chamberlain
4 (sac@cygnus.com).
fa4b55a1
SC
5
6This file is part of GDB.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
fa4b55a1
SC
22#include "defs.h"
23#include "inferior.h"
24#include "wait.h"
25#include "value.h"
e17960fb 26#include <string.h>
fa4b55a1
SC
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 */
37extern int stop_soon_quietly; /* for wait_for_inferior */
38
fa4b55a1
SC
39/* Forward data declarations */
40extern struct target_ops hms_ops; /* Forward declaration */
41
42/* Forward function declarations */
43static void hms_fetch_registers ();
44static int hms_store_registers ();
45static void hms_close ();
46static int hms_clear_breakpoints();
47
48extern struct target_ops hms_ops;
49
1f46923f
SC
50static int quiet = 1;
51
1f46923f
SC
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)
67struct 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
73struct dcache_block dcache_free, dcache_valid;
74
75/* Free all the data cache blocks, thus discarding all cached data. */
76static
77void
78dcache_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 */
93static
94struct dcache_block *
95dcache_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. */
115static
116int
117dcache_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...). */
132static
133struct dcache_block *
134dcache_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. */
154static
155int
156dcache_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. */
176static void
177dcache_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. */
206struct dcache_block the_cache[DCACHE_SIZE];
207
208/* Initialize the data cache. */
209static void
210dcache_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}
fa4b55a1 221
1f46923f
SC
222
223/***********************************************************************
224 * I/O stuff stolen from remote-eb.c
225 ***********************************************************************/
226
227static int timeout = 2;
fa4b55a1 228
b52373a2 229static const char *dev_name;
1f46923f 230
fa4b55a1
SC
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. */
fa4b55a1 235
1f46923f 236
ae0ea72e
SC
237int is_open = 0;
238int check_open()
fa4b55a1 239{
ae0ea72e
SC
240 if (!is_open)
241 {
242 error("remote device not open");
fa4b55a1 243 }
fa4b55a1
SC
244}
245
ae0ea72e
SC
246#define ON 1
247#define OFF 0
1f46923f
SC
248
249/* Read a character from the remote system, doing all the fancy
250 timeout stuff. */
251static int
252readchar ()
fa4b55a1 253{
ae0ea72e 254 int buf;
5a6242dd 255 buf = serial_readchar(timeout);
1f46923f 256
5a6242dd 257 if (buf < 0)
1f46923f
SC
258 error ("Timeout reading from remote system.");
259
260 if (!quiet)
261 printf("%c",buf);
262
263 return buf & 0x7f;
fa4b55a1
SC
264}
265
fa4b55a1 266static int
1f46923f 267readchar_nofail ()
fa4b55a1 268{
ae0ea72e 269 int buf;
5a6242dd
SG
270 buf = serial_readchar(timeout);
271 if (buf < 0) buf = 0;
ae0ea72e
SC
272 if (!quiet)
273 printf("%c",buf);
274
fa4b55a1 275 return buf & 0x7f;
ae0ea72e 276
fa4b55a1
SC
277}
278
279/* Keep discarding input from the remote system, until STRING is found.
280 Let the user break out immediately. */
281static void
282expect (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. */
319static void
320expect_prompt ()
321{
fa4b55a1
SC
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). */
327static int
328get_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. */
353static void
354get_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 */
365static long
366get_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. */
fa4b55a1 377
fa4b55a1 378
fa4b55a1
SC
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
384static int need_artificial_trap = 0;
385
386void
387hms_kill(arg,from_tty)
388char *arg;
389int from_tty;
390{
fa4b55a1 391
fa4b55a1 392}
1f46923f 393
ae0ea72e 394
1f46923f 395
fa4b55a1
SC
396/*
397 * Download a file specified in 'args', to the hms.
398 */
399static void
400hms_load(args,fromtty)
401char *args;
402int fromtty;
403{
404 bfd *abfd;
405 asection *s;
406 int n;
407 char buffer[1024];
ae0ea72e 408
1f46923f
SC
409 check_open();
410
411 dcache_flush();
412 inferior_pid = 0;
fa4b55a1
SC
413 abfd = bfd_openr(args,"coff-h8300");
414 if (!abfd)
415 {
1f46923f 416 printf_filtered("Unable to open file %s\n", args);
fa4b55a1
SC
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 {
96743d3c 431 int i;
fa4b55a1 432
96743d3c 433
ae0ea72e 434#define DELTA 1024
96743d3c 435 char *buffer = xmalloc(DELTA);
ae0ea72e 436 printf_filtered("%s\t: 0x%4x .. 0x%4x ",s->name, s->vma, s->vma + s->_raw_size);
96743d3c
SC
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 }
ae0ea72e 448 printf_filtered( "\n");
fa4b55a1
SC
449 free(buffer);
450 }
451 s = s->next;
452 }
ae0ea72e
SC
453 sprintf(buffer, "r PC=%x", abfd->start_address);
454 hms_write_cr(buffer);
455 expect_prompt();
fa4b55a1
SC
456}
457
458/* This is called not only when we first attach, but also when the
459 user types "run" after having attached. */
460void
461hms_create_inferior (execfile, args, env)
462 char *execfile;
463 char *args;
464 char **env;
465{
466 int entry_pt;
ae0ea72e 467 char buffer[100];
fa4b55a1
SC
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);
1f46923f 476 check_open();
1f46923f 477
fa4b55a1 478
ae0ea72e
SC
479 hms_kill(NULL,NULL);
480 hms_clear_breakpoints();
481 init_wait_for_inferior ();
482 hms_write_cr("");
483 expect_prompt();
fa4b55a1 484
ae0ea72e
SC
485 insert_breakpoints (); /* Needed to get correct instruction in cache */
486 proceed(entry_pt, -1, 0);
fa4b55a1
SC
487}
488
fa4b55a1
SC
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
495static char *
496find_end_of_word(s)
497char *s;
498{
499 while (*s && !isspace(*s))
500 s++;
501 return s;
502}
503
504static char *get_word(p)
505char **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
532static int baudrate = 9600;
1f46923f
SC
533
534static int
535is_baudrate_right()
536{
ae0ea72e 537 int ok;
1f46923f 538 /* Put this port into NORMAL mode, send the 'normal' character */
ae0ea72e 539
1f46923f
SC
540 hms_write("\001", 1); /* Control A */
541 hms_write("\r", 1); /* Cr */
1f46923f 542
ae0ea72e
SC
543 while (1)
544 {
5a6242dd
SG
545 ok = serial_readchar(timeout);
546 if (ok < 0) break;
ae0ea72e
SC
547 }
548
549 hms_write("r",1);
550
1f46923f
SC
551 if (readchar_nofail() == 'r')
552 return 1;
553
554 /* Not the right baudrate, or the board's not on */
555 return 0;
1f46923f
SC
556}
557static void
558set_rate()
559{
ae0ea72e
SC
560 if (!serial_setbaudrate(baudrate))
561 error("Can't set baudrate");
1f46923f
SC
562}
563
564static void
565get_baudrate_right()
566{
1f46923f
SC
567 while (!is_baudrate_right())
568 {
ae0ea72e
SC
569 baudrate = serial_nextbaudrate(baudrate);
570 if (baudrate == 0) {
571 printf_filtered("Board not yet in sync\n");
572 break;
1f46923f 573 }
1f46923f
SC
574 printf_filtered("Board not responding, trying %d baud\n",baudrate);
575 QUIT;
ae0ea72e 576 serial_setbaudrate(baudrate);
1f46923f
SC
577 }
578}
579
fa4b55a1
SC
580static void
581hms_open (name, from_tty)
582 char *name;
583 int from_tty;
584{
1f46923f 585
fa4b55a1
SC
586 unsigned int prl;
587 char *p;
588
fa4b55a1
SC
589 if(name == 0)
590 {
591 name = "";
fa4b55a1 592 }
ae0ea72e 593 if (is_open)
836e343b 594 hms_close (0);
ae0ea72e 595 if (name && strlen(name))
836e343b 596 dev_name = strdup(name);
ae0ea72e 597 if (!serial_open(dev_name))
836e343b 598 perror_with_name ((char *)dev_name);
ae0ea72e
SC
599 serial_raw();
600 is_open = 1;
fa4b55a1 601
1f46923f 602 dcache_init();
fa4b55a1 603
1f46923f 604 get_baudrate_right();
fa4b55a1
SC
605
606 /* Hello? Are you there? */
ae0ea72e 607 serial_write("\r",1);
fa4b55a1
SC
608 expect_prompt ();
609
610 /* Clear any break points */
611 hms_clear_breakpoints();
612
ae0ea72e 613 printf_filtered("Connected to remote H8/300 HMS system.\n");
fa4b55a1
SC
614}
615
616/* Close out all files and local state before this target loses control. */
617
618static void
619hms_close (quitting)
620 int quitting;
621{
622
fa4b55a1
SC
623
624 /* Clear any break points */
625 hms_clear_breakpoints();
626
627 /* Put this port back into REMOTE mode */
ae0ea72e
SC
628 sleep(1); /* Let any output make it all the way back */
629 serial_write("R\r", 2);
630 serial_close();
631 is_open = 0;
fa4b55a1
SC
632}
633
fa4b55a1
SC
634/* Terminate the open connection to the remote debugger.
635 Use this when you want to detach and do something else
636 with your gdb. */
637void
638hms_detach (args,from_tty)
639 char *args;
640 int from_tty;
641{
ae0ea72e
SC
642 if (is_open)
643 {
fa4b55a1 644 hms_clear_breakpoints();
fa4b55a1
SC
645 }
646
647 pop_target(); /* calls hms_close to do the real work */
648 if (from_tty)
649 printf_filtered ("Ending remote %s debugging\n", target_shortname);
fa4b55a1
SC
650}
651
652/* Tell the remote machine to resume. */
653
654void
655hms_resume (step, sig)
656 int step, sig;
657{
1f46923f
SC
658 dcache_flush();
659
fa4b55a1
SC
660 if (step)
661 {
662 hms_write_cr("s");
96743d3c
SC
663 expect("Step>");
664
fa4b55a1
SC
665 /* Force the next hms_wait to return a trap. Not doing anything
666 about I/O from the target means that the user has to type
667 "continue" to see any. FIXME, this should be fixed. */
668 need_artificial_trap = 1;
669 }
670 else
671 {
672 hms_write_cr("g");
96743d3c 673 expect("g");
fa4b55a1 674 }
fa4b55a1
SC
675}
676
677/* Wait until the remote machine stops, then return,
678 storing status in STATUS just as `wait' would. */
679
680int
681hms_wait (status)
682 WAITTYPE *status;
683{
684 /* Strings to look for. '?' means match any single character.
685 Note that with the algorithm we use, the initial character
686 of the string cannot recur in the string, or we will not
687 find some cases of the string in the input. */
688
96743d3c 689 static char bpt[] = "At breakpoint:";
fa4b55a1
SC
690 /* It would be tempting to look for "\n[__exit + 0x8]\n"
691 but that requires loading symbols with "yc i" and even if
692 we did do that we don't know that the file has symbols. */
693 static char exitmsg[] = "HMS>";
694 char *bp = bpt;
695 char *ep = exitmsg;
696
697 /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars. */
698 char swallowed[50];
699 /* Current position in swallowed. */
700 char *swallowed_p = swallowed;
701
702 int ch;
703 int ch_handled;
704 int old_timeout = timeout;
705 int old_immediate_quit = immediate_quit;
1f46923f
SC
706 int swallowed_cr = 0;
707
fa4b55a1
SC
708 WSETEXIT ((*status), 0);
709
710 if (need_artificial_trap != 0)
1f46923f
SC
711 {
712 WSETSTOP ((*status), SIGTRAP);
713 need_artificial_trap--;
714 return 0;
715 }
fa4b55a1 716
ae0ea72e 717 timeout = 99999; /* Don't time out -- user program is running. */
1f46923f 718 immediate_quit = 1; /* Helps ability to QUIT */
96743d3c
SC
719 while (1)
720 {
721 QUIT; /* Let user quit and leave process running */
722 ch_handled = 0;
723 ch = readchar ();
724 if (ch == *bp)
725 {
726 bp++;
727 if (*bp == '\0')
728 break;
729 ch_handled = 1;
730
731 *swallowed_p++ = ch;
732 }
733 else
734 {
735 bp = bpt;
736 }
737 if (ch == *ep || *ep == '?')
738 {
739 ep++;
740 if (*ep == '\0')
741 break;
742
743 if (!ch_handled)
744 *swallowed_p++ = ch;
745 ch_handled = 1;
746 }
747 else
748 {
749 ep = exitmsg;
750 }
751
752 if (!ch_handled) {
753 char *p;
754 /* Print out any characters which have been swallowed. */
755 for (p = swallowed; p < swallowed_p; ++p)
756 putc (*p, stdout);
757 swallowed_p = swallowed;
fa4b55a1
SC
758
759
96743d3c
SC
760 if ((ch != '\r' && ch != '\n') || swallowed_cr>10)
761 {
762 putc (ch, stdout);
763 swallowed_cr = 10;
fa4b55a1 764 }
96743d3c
SC
765 swallowed_cr ++;
766
767 }
768 }
fa4b55a1
SC
769 if (*bp== '\0')
770 {
771 WSETSTOP ((*status), SIGTRAP);
772 expect_prompt();
773 }
774 else
775 {
776 WSETEXIT ((*status), 0);
777 }
778
779 timeout = old_timeout;
780 immediate_quit = old_immediate_quit;
fa4b55a1
SC
781 return 0;
782}
783
784/* Return the name of register number REGNO
785 in the form input and output by hms.
786
787 Returns a pointer to a static buffer containing the answer. */
788static char *
789get_reg_name (regno)
790 int regno;
791{
ae0ea72e 792 static char *rn[NUM_REGS]= REGISTER_NAMES;
fa4b55a1 793 return rn[regno];
fa4b55a1
SC
794}
795
796/* Read the remote registers. */
797static int gethex(length, start, ok)
798unsigned int length;
799char *start;
800int *ok;
801{
802 int result = 0;
803 while (length--)
804 {
805 result <<= 4 ;
806 if (*start >='a' && *start <= 'f')
807 {
808 result += *start - 'a' + 10;
809 }
810 else if (*start >='A' && *start <= 'F')
811 {
812 result += *start - 'A' + 10;
813 }
814 else if (*start >='0' && *start <= '9')
815 {
816 result += *start - '0' ;
817 }
818 else *ok = 0;
819 start++;
820
821 }
822 return result;
823}
824static int
825timed_read(buf, n, timeout)
826char *buf;
827
828{
829 int i;
830 char c;
831 i = 0;
832 while (i < n)
833 {
834 c = readchar();
835
836 if (c == 0) return i;
837 buf[i] = c;
838 i++;
839
840 }
841 return i;
842
843}
844hms_write(a,l)
845char *a;
846{
847 int i;
ae0ea72e
SC
848 serial_write(a, l);
849
96743d3c
SC
850 if (!quiet)
851 for (i = 0; i < l ; i++)
852 {
853 printf("%c", a[i]);
854 }
fa4b55a1
SC
855}
856
1f46923f
SC
857hms_write_cr(s)
858char *s;
fa4b55a1 859{
ae0ea72e
SC
860 hms_write( s, strlen(s));
861 hms_write("\r",1);
1f46923f
SC
862}
863
864static void
ae0ea72e
SC
865hms_fetch_register (dummy)
866int dummy;
fa4b55a1
SC
867{
868#define REGREPLY_SIZE 79
869 char linebuf[REGREPLY_SIZE+1];
870 int i;
871 int s ;
872 int gottok;
873
1f46923f
SC
874 REGISTER_TYPE reg[NUM_REGS];
875 int foo[8];
fa4b55a1
SC
876 check_open();
877
878 do
879 {
880
881 hms_write_cr("r");
882 s = timed_read(linebuf, REGREPLY_SIZE, 1);
883
884
885 linebuf[REGREPLY_SIZE] = 0;
886 gottok = 0;
887 if (linebuf[0] == 'r' &&
fa4b55a1
SC
888 linebuf[3] == 'P' &&
889 linebuf[4] == 'C' &&
890 linebuf[5] == '=' &&
891 linebuf[75] == 'H' &&
892 linebuf[76] == 'M' &&
893 linebuf[77] == 'S')
894 {
895 /*
896 PC=XXXX CCR=XX:XXXXXXXX R0-R7= XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX
897 5436789012345678901234567890123456789012345678901234567890123456789012
898 0 1 2 3 4 5 6
899 */
900 gottok = 1;
901
902 reg[PC_REGNUM] = gethex(4,linebuf+6, &gottok);
903 reg[CCR_REGNUM] = gethex(2,linebuf+15, &gottok);
904 for (i = 0; i < 8; i++)
905 {
906 reg[i] = gethex(4, linebuf+34+5*i, &gottok);
907 }
908 }
909 }
910 while (!gottok);
911 for (i = 0; i < NUM_REGS; i++)
912 {
ae0ea72e
SC
913 char swapped[2];
914 swapped[1] = reg[i];
915 swapped[0] = (reg[i])>> 8;
fa4b55a1 916
ae0ea72e
SC
917 supply_register (i, swapped);
918 }
fa4b55a1
SC
919}
920
fa4b55a1
SC
921
922/* Store register REGNO, or all if REGNO == -1.
923 Return errno value. */
ae0ea72e 924static void
fa4b55a1
SC
925hms_store_register (regno)
926 int regno;
927{
928
1f46923f 929 /* printf("hms_store_register() called.\n"); fflush(stdout); /* */
ae0ea72e
SC
930 if (regno == -1)
931 {
932 for (regno = 0; regno < NUM_REGS; regno ++)
933 {
934 hms_store_register(regno);
935 }
936 }
fa4b55a1
SC
937 else
938 {
939 char *name = get_reg_name (regno);
940 char buffer[100];
941 sprintf(buffer,"r %s=%x", name, read_register(regno));
942 hms_write_cr(buffer);
943 expect_prompt();
944 }
fa4b55a1
SC
945}
946
ae0ea72e
SC
947
948
fa4b55a1
SC
949/* Get ready to modify the registers array. On machines which store
950 individual registers, this doesn't need to do anything. On machines
951 which store all the registers in one fell swoop, this makes sure
952 that registers contains all the registers from the program being
953 debugged. */
954
955void
956hms_prepare_to_store ()
957{
958 /* Do nothing, since we can store individual regs */
959}
960
961static CORE_ADDR
962translate_addr(addr)
963CORE_ADDR addr;
964{
965
966 return(addr);
967
968}
969
970/* Read a word from remote address ADDR and return it.
971 * This goes through the data cache.
972 */
973int
974hms_fetch_word (addr)
975 CORE_ADDR addr;
976{
977 return dcache_fetch (addr);
978}
979
980/* Write a word WORD into remote address ADDR.
981 This goes through the data cache. */
982
983void
984hms_store_word (addr, word)
985 CORE_ADDR addr;
986 int word;
987{
988 dcache_poke (addr, word);
989}
990
991int
992hms_xfer_inferior_memory(memaddr, myaddr, len, write, target)
993 CORE_ADDR memaddr;
994 char *myaddr;
995 int len;
996 int write;
997 struct target_ops *target; /* ignored */
998{
999 register int i;
1000 /* Round starting address down to longword boundary. */
1001 register CORE_ADDR addr;
1002 /* Round ending address up; get number of longwords that makes. */
1003 register int count;
1004 /* Allocate buffer of that many longwords. */
1005 register int *buffer ;
1006
1007 memaddr &= 0xffff;
1008 addr = memaddr & - sizeof (int);
1009 count = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
1010
1f46923f 1011
fa4b55a1
SC
1012 buffer = (int *)alloca (count * sizeof (int));
1013 if (write)
1014 {
1015 /* Fill start and end extra bytes of buffer with existing memory data. */
1016
1017 if (addr != memaddr || len < (int)sizeof (int)) {
1018 /* Need part of initial word -- fetch it. */
1019 buffer[0] = hms_fetch_word (addr);
1020 }
1021
1022 if (count > 1) /* FIXME, avoid if even boundary */
1023 {
1024 buffer[count - 1]
1025 = hms_fetch_word (addr + (count - 1) * sizeof (int));
1026 }
1027
1028 /* Copy data to be written over corresponding part of buffer */
1029
1030 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
1031
1032 /* Write the entire buffer. */
1033
1034 for (i = 0; i < count; i++, addr += sizeof (int))
1035 {
1036 errno = 0;
1037 hms_store_word (addr, buffer[i]);
1038 if (errno)
1039 {
1040
1041 return 0;
1042 }
1043
1044 }
1045 }
1046 else
1047 {
1048 /* Read all the longwords */
1049 for (i = 0; i < count; i++, addr += sizeof (int))
1050 {
1051 errno = 0;
1052 buffer[i] = hms_fetch_word (addr);
1053 if (errno)
1054 {
1055 return 0;
1056 }
1057 QUIT;
1058 }
1059
1060 /* Copy appropriate bytes out of the buffer. */
1061 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
1062 }
1063
1064
1065 return len;
1066}
1067
fa4b55a1
SC
1068int
1069hms_write_inferior_memory (memaddr, myaddr, len)
1070 CORE_ADDR memaddr;
ae0ea72e 1071 unsigned char *myaddr;
fa4b55a1
SC
1072 int len;
1073{
ae0ea72e
SC
1074 bfd_vma addr;
1075 int done;
1076 int todo ;
1077 done = 0;
1078 while(done < len)
1079 {
1080 char buffer[20];
1081 int thisgo;
1082 int idx;
1083 thisgo = len - done;
1084 if (thisgo > 20) thisgo = 20;
1085
1086 sprintf(buffer,"M.B %4x =", memaddr+done);
1087 hms_write(buffer,10);
1088 for (idx = 0; idx < thisgo; idx++)
1089 {
1090 char buf[20];
1091 sprintf(buf, "%2x ", myaddr[idx+done]);
1092 hms_write(buf, 3);
1093 }
1094 hms_write_cr("");
1095 expect_prompt();
1096 done += thisgo;
1097 }
1098
fa4b55a1 1099
fa4b55a1
SC
1100}
1101
1102void
1103hms_files_info ()
1104{
ae0ea72e
SC
1105char *file = "nothing";
1106if (exec_bfd)
1107 file = bfd_get_filename(exec_bfd);
1108
1109if (exec_bfd)
1110#ifdef __GO32__
1111 printf_filtered("\tAttached to DOS asynctsr and running program %s\n",file);
1112#else
1113 printf_filtered("\tAttached to %s at %d baud and running program %s\n",file);
1114#endif
fa4b55a1
SC
1115 printf_filtered("\ton an H8/300 processor.\n");
1116}
1117
1118/* Copy LEN bytes of data from debugger memory at MYADDR
1119 to inferior's memory at MEMADDR. Returns errno value.
1120 * sb/sh instructions don't work on unaligned addresses, when TU=1.
1121 */
1122
1123
1124/* Read LEN bytes from inferior memory at MEMADDR. Put the result
1125 at debugger address MYADDR. Returns errno value. */
1126int
1127hms_read_inferior_memory(memaddr, myaddr, len)
1128 CORE_ADDR memaddr;
1129 char *myaddr;
1130 int len;
1131{
1132 /* Align to nearest low 16 bits */
1133 int i;
1134
1135#if 0
1136 CORE_ADDR start = memaddr & ~0xf;
1137 CORE_ADDR end = ((memaddr + len +16) & ~0xf) -1;
1138#endif
1139 CORE_ADDR start = memaddr;
1140 CORE_ADDR end = memaddr + len -1;
1141
1142 int ok =1;
1143
1144 /*
1145 AAAA: XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX '................'
1146 012345678901234567890123456789012345678901234567890123456789012345
1147 0 1 2 3 4 5 6
1148 */
1149 char buffer[66];
1150 if (memaddr & 0xf) abort();
1151 if (len != 16) abort();
1152
96743d3c 1153 sprintf(buffer, "m %4x %4x", start & 0xffff, end & 0xffff);
fa4b55a1
SC
1154 hms_write_cr(buffer);
1155 /* drop the echo and newline*/
1156 for (i = 0; i < 13; i++)
1157 readchar();
1158
1159
1160
1161 /* Grab the lines as they come out and fill the area */
1162 /* Skip over cr */
1163 while(1)
1164 {
1165 int p;
1166 int i;
1167 int addr;
1168 size_t idx;
1169
1170 char byte[16];
1171 buffer[0] = readchar();
1172 if (buffer[0] == 'M')
1173 break;
1174 for (i = 1; i < 66; i++)
1175 buffer[i] = readchar();
1176
1177 /* Now parse the line */
1178
1179 addr = gethex(4, buffer, &ok);
1180 idx = 6;
1181 for (p = 0; p < 16; p+=2)
1182 {
1183 byte[p] = gethex(2, buffer + idx, &ok);
1184 byte[p+1] = gethex(2, buffer+ idx + 2, &ok);
1185 idx+=5;
1186
1187 }
1188
1189
1190 for (p = 0; p<16;p++)
1191 {
1192 if (addr + p >= memaddr &&
1193 addr + p < memaddr + len)
1194 {
1195 myaddr[ (addr + p)-memaddr] = byte[p];
1196
1197 }
1198
1199 }
1200 }
96743d3c 1201 hms_write_cr(" ");
fa4b55a1 1202 expect_prompt();
fa4b55a1 1203 return len;
fa4b55a1
SC
1204}
1205
1206/* This routine is run as a hook, just before the main command loop is
1207 entered. If gdb is configured for the H8, but has not had its
1208 target specified yet, this will loop prompting the user to do so.
1209*/
1210
1211hms_before_main_loop ()
1212{
1213 char ttyname[100];
1214 char *p, *p2;
1215 extern FILE *instream;
1216 extern jmp_buf to_top_level;
1217
1218 push_target (&hms_ops);
fa4b55a1
SC
1219}
1220
1221
1222#define MAX_BREAKS 16
1223static int num_brkpts=0;
1224static int
1225hms_insert_breakpoint(addr, save)
1226CORE_ADDR addr;
1227char *save; /* Throw away, let hms save instructions */
1228{
fa4b55a1
SC
1229 check_open();
1230
ae0ea72e
SC
1231 if (num_brkpts < MAX_BREAKS)
1232 {
1233 char buffer[100];
1234 num_brkpts++;
1235 sprintf(buffer,"b %x", addr & 0xffff);
1236 hms_write_cr(buffer);
1237 expect_prompt ();
ae0ea72e
SC
1238 return(0);
1239 }
1240 else
1241 {
1242 fprintf_filtered(stderr,
1243 "Too many break points, break point not installed\n");
ae0ea72e
SC
1244 return(1);
1245 }
fa4b55a1
SC
1246
1247
1248}
1249static int
1250hms_remove_breakpoint(addr, save)
1251CORE_ADDR addr;
1252char *save; /* Throw away, let hms save instructions */
1253{
ae0ea72e
SC
1254 if (num_brkpts > 0)
1255 {
1256 char buffer[100];
fa4b55a1 1257
ae0ea72e
SC
1258 num_brkpts--;
1259 sprintf(buffer,"b - %x", addr & 0xffff);
1260 hms_write_cr(buffer);
1261 expect_prompt();
fa4b55a1 1262
ae0ea72e 1263 }
fa4b55a1
SC
1264 return(0);
1265}
1266
1267/* Clear the hmss notion of what the break points are */
1268static int
1269hms_clear_breakpoints()
1270{
1271
ae0ea72e
SC
1272 if (is_open) {
1273 hms_write_cr("b -");
1274 expect_prompt ();
1275 }
fa4b55a1 1276 num_brkpts = 0;
fa4b55a1
SC
1277}
1278static void
1279hms_mourn()
1280{
fa4b55a1 1281 hms_clear_breakpoints();
fa4b55a1 1282 generic_mourn_inferior ();
fa4b55a1
SC
1283}
1284
fa4b55a1
SC
1285
1286
1287/* Put a command string, in args, out to the hms. The hms is assumed to
ae0ea72e 1288 be in raw mode, all writing/reading done through desc.
fa4b55a1
SC
1289 Ouput from the hms is placed on the users terminal until the
1290 prompt from the hms is seen.
1291 FIXME: Can't handle commands that take input. */
1292
1293void
1294hms_com (args, fromtty)
1295 char *args;
1296 int fromtty;
1297{
1298 check_open();
1299
1300 if (!args) return;
1301
1302 /* Clear all input so only command relative output is displayed */
1303
fa4b55a1
SC
1304 hms_write_cr(args);
1305 hms_write("\030",1);
1306 expect_prompt();
fa4b55a1
SC
1307}
1308
1309/* Define the target subroutine names */
1310
1311struct target_ops hms_ops = {
1312 "hms", "Remote HMS monitor",
1313"Use the H8 evaluation board running the HMS monitor connected\n\
1314by a serial line.",
1315
1316 hms_open, hms_close,
836e343b 1317 0, hms_detach, hms_resume, hms_wait, /* attach */
fa4b55a1 1318 hms_fetch_register, hms_store_register,
a03d4f8e 1319 hms_prepare_to_store,
fa4b55a1
SC
1320 hms_xfer_inferior_memory,
1321 hms_files_info,
1322 hms_insert_breakpoint, hms_remove_breakpoint, /* Breakpoints */
1323 0, 0, 0, 0, 0, /* Terminal handling */
1324 hms_kill, /* FIXME, kill */
1325 hms_load,
fa4b55a1
SC
1326 0, /* lookup_symbol */
1327 hms_create_inferior, /* create_inferior */
1328 hms_mourn, /* mourn_inferior FIXME */
5ee4e16c 1329 0, /* can_run */
3950a34e 1330 0, /* notice_signals */
fa4b55a1
SC
1331 process_stratum, 0, /* next */
1332 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
1333 0,0, /* Section pointers */
1334 OPS_MAGIC, /* Always the last thing */
1335};
1336
1337
1338hms_quiet()
1339{
ae0ea72e
SC
1340 quiet = ! quiet;
1341 if (quiet)
1342 printf_filtered("Snoop disabled\n");
1343 else
1344 printf_filtered("Snoop enabled\n");
1345
fa4b55a1
SC
1346}
1347
1348hms_device(s)
1349char *s;
1350{
ae0ea72e
SC
1351 if (s)
1352 {
1353 dev_name = get_word(&s);
1354 }
fa4b55a1
SC
1355}
1356
ae0ea72e 1357
96743d3c
SC
1358static
1359hms_speed(s)
fa4b55a1
SC
1360char *s;
1361{
ae0ea72e 1362 check_open();
96743d3c 1363
fa4b55a1
SC
1364 if (s)
1365 {
1366 char buffer[100];
1367 int newrate = atoi(s);
1368 int which = 0;
ae0ea72e 1369 if (!serial_setbaudrate(newrate))
fa4b55a1 1370 error("Can't use %d baud\n", newrate);
fa4b55a1 1371
fa4b55a1 1372 printf_filtered("Checking target is in sync\n");
fa4b55a1
SC
1373
1374 get_baudrate_right();
1375 baudrate = newrate;
1376 printf_filtered("Sending commands to set target to %d\n",
1377 baudrate);
1378
1379 sprintf(buffer, "tm %d. N 8 1", baudrate);
96743d3c 1380 hms_write_cr(buffer);
fa4b55a1
SC
1381 }
1382}
1383
1384/***********************************************************************/
1385
1386void
1387_initialize_remote_hms ()
1388{
1389 add_target (&hms_ops);
1390 add_com ("hms <command>", class_obscure, hms_com,
1391 "Send a command to the HMS monitor.");
1392 add_com ("snoop", class_obscure, hms_quiet,
1393 "Show what commands are going to the monitor");
ae0ea72e 1394
fa4b55a1
SC
1395 add_com ("device", class_obscure, hms_device,
1396 "Set the terminal line for HMS communications");
1397
1398 add_com ("speed", class_obscure, hms_speed,
1399 "Set the terminal line speed for HMS communications");
1400
ae0ea72e 1401 dev_name = serial_default_name();
fa4b55a1
SC
1402}
1403
1404
This page took 0.123295 seconds and 4 git commands to generate.