* configure.in: Add m68k-ericsson-*.
[deliverable/binutils-gdb.git] / gdb / remote-es1800.c
1 /* Memory-access and commands for remote es1800 processes, for GDB.
2 Copyright (C) 1988, 1992 Free Software Foundation, Inc.
3
4 This file is added to GDB to make it possible to do debugging via an
5 ES-1800 emulator. The code was originally written by Johan Holmberg
6 TT/SJ Ericsson Telecom AB and later modified byJohan Henriksson
7 TT/SJ. It was modified for gdb 4.0 by TX/DK Jan Nordenand by TX/DKG
8 Harald Johansen.
9
10 This file is part of GDB.
11
12 GDB is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 1, or (at your option)
15 any later version.
16
17 GDB is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GDB; see the file COPYING. If not, write to
24 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
25
26
27 /* Emulator communication protocol.
28 All values are encoded in ascii hex digits.
29
30 Request
31 Command
32 Reply
33 read registers:
34 DR<cr>
35 - 0 - - 1 - - 2 - - 3 - - 4 - - 5 - -- 6 - - 7 -
36 D = XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
37 A = XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
38 PC = XXXXXX SSP = XXXXXX USP = XXXXXX SR = XXXXXXXX
39 >
40 Each byte of register data is described by two hex digits.
41
42 write regs
43 D0=XXXXXXXX<cr>
44 >D1=XXXXXXXX<cr>
45 >D2=XXXXXXXX<cr>
46 >D3=XXXXXXXX<cr>
47 >D4=XXXXXXXX<cr>
48 >D5=XXXXXXXX<cr>
49 >D6=XXXXXXXX<cr>
50 >D7=XXXXXXXX<cr>
51 >A0=XXXXXXXX<cr>
52 >A1=XXXXXXXX<cr>
53 >A2=XXXXXXXX<cr>
54 >A3=XXXXXXXX<cr>
55 >A4=XXXXXXXX<cr>
56 >A5=XXXXXXXX<cr>
57 >A6=XXXXXXXX<cr>
58 >A7=XXXXXXXX<cr>
59 >SR=XXXXXXXX<cr>
60 >PC=XXXXXX<cr>
61 >
62 Each byte of register data is described by two hex digits.
63
64 read mem
65 @.BAA..AA
66 $FFFFFFXX
67 >
68 AA..AA is address, XXXXXXX is the contents
69
70 write mem
71 @.BAA..AA=$XXXXXXXX
72 >
73 AA..AA is address, XXXXXXXX is data
74
75 cont
76 PC=$AA..AA
77 >RBK
78 R>
79 AA..AA is address to resume. If AA..AA is omitted, resume at same address.
80
81 step
82 PC=$AA..AA
83 >STP
84 R>
85 AA..AA is address to resume. If AA..AA is omitted, resume at same address.
86
87 kill req
88 STP
89 >
90 */
91
92
93 #include <stdio.h>
94 #include <signal.h>
95 #include <sys/ioctl.h>
96 #include <sys/file.h>
97 #include <errno.h>
98 #include <ctype.h>
99 #include <string.h>
100 #include <setjmp.h>
101 #include <fcntl.h>
102 #include "defs.h"
103 #include "frame.h"
104 #include "inferior.h"
105 #include "target.h"
106 #include "wait.h"
107 #include "terminal.h"
108 #include "command.h"
109
110 #ifdef USG
111 #include <sys/types.h>
112 #endif
113
114 #include <signal.h>
115
116 /* External variables referenced. */
117
118 extern bfd *exec_bfd;
119
120 /* Prototypes for local functions */
121
122 static void
123 es1800_child_detach PARAMS ((char *, int));
124
125 static void
126 es1800_child_open PARAMS ((char *, int));
127
128 static void
129 es1800_transparent PARAMS ((char *, int));
130
131 static void
132 es1800_create_inferior PARAMS ((char *, char *, char **));
133
134 static void
135 es1800_load PARAMS ((char *, int));
136
137 static void
138 es1800_kill PARAMS ((void));
139
140 static int
141 verify_break PARAMS ((int));
142
143 static int
144 es1800_remove_breakpoint PARAMS ((CORE_ADDR, char *));
145
146 static int
147 es1800_insert_breakpoint PARAMS ((CORE_ADDR, char *));
148
149 static void
150 es1800_files_info PARAMS ((struct target_ops *));
151
152 static int
153 es1800_xfer_inferior_memory PARAMS ((CORE_ADDR, char *, int, int,
154 struct target_ops *));
155
156 static void
157 es1800_prepare_to_store PARAMS ((void));
158
159 static int
160 es1800_wait PARAMS ((WAITTYPE *));
161
162 static void
163 es1800_resume PARAMS ((int, int));
164
165 static void
166 es1800_detach PARAMS ((char *, int));
167
168 static void
169 es1800_attach PARAMS ((char *, int));
170
171 static int
172 damn_b PARAMS ((char *));
173
174 static void
175 es1800_open PARAMS ((char *, int));
176
177 static void
178 es1800_timer PARAMS ((void));
179
180 static void
181 es1800_reset PARAMS ((char *));
182
183 static void
184 es1800_request_quit PARAMS ((void));
185
186 static int
187 readchar PARAMS ((void));
188
189 static void
190 expect PARAMS ((char *, int));
191
192 static void
193 expect_prompt PARAMS ((void));
194
195 static void
196 download PARAMS ((FILE *, int, int));
197
198 #if 0
199 static void
200 bfd_copy PARAMS ((bfd *, bfd *));
201 #endif
202
203 static void
204 get_break_addr PARAMS ((int, CORE_ADDR *));
205
206 static int
207 fromhex PARAMS ((int));
208
209 static int
210 tohex PARAMS ((int));
211
212 static void
213 es1800_close PARAMS ((int));
214
215 static void
216 es1800_fetch_registers PARAMS ((void));
217
218 static void
219 es1800_fetch_register PARAMS ((int));
220
221 static void
222 es1800_store_register PARAMS ((int));
223
224 static void
225 es1800_read_bytes PARAMS ((CORE_ADDR, char *, int));
226
227 static void
228 es1800_write_bytes PARAMS ((CORE_ADDR, char *, int));
229
230 static void
231 send_with_reply PARAMS ((char *, char *, int));
232
233 static void
234 send_command PARAMS ((char *));
235
236 static void
237 send PARAMS ((char *));
238
239 static void
240 getmessage PARAMS ((char *, int));
241
242 static void
243 es1800_mourn_inferior PARAMS ((void));
244
245 static void
246 es1800_create_break_insn PARAMS ((char *, int));
247
248 static void
249 es1800_init_break PARAMS ((char *, int));
250
251 /* Local variables */
252
253 #define LOG_FILE "es1800.log"
254 #if defined (LOG_FILE)
255 static FILE *log_file;
256 #endif
257
258 static struct target_ops es1800_ops; /* Forward decl */
259 static struct target_ops es1800_child_ops; /* Forward decl */
260
261 static int kiodebug;
262 static int timeout = 100;
263 static char *savename; /* Name of i/o device used */
264 static TERMINAL es1800_sg_save; /* Save stty state */
265 static int es1800_fc_save; /* Save fcntl state */
266
267 /* indicates that the emulator uses 32-bit data-adress (68020-mode)
268 instead of 24-bit (68000 -mode) */
269
270 static int m68020;
271
272 #define MODE (m68020 ? "M68020" : "M68000" )
273 #define ES1800_BREAK_VEC (0xf)
274
275 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
276 es1800_open knows that we don't have a file open when the program
277 starts. */
278
279 static int es1800_desc = -1;
280
281 #define PBUFSIZ 1000
282 #define HDRLEN sizeof("@.BAAAAAAAA=$VV\r")
283
284 /* Maximum number of bytes to read/write at once. The value here
285 is chosen to fill up a packet. */
286
287 #define MAXBUFBYTES ((PBUFSIZ-150)*16/75 )
288
289 static int es1800_break_vec = 0;
290 static char es1800_break_insn[2];
291 static long es1800_break_address;
292 static void (*old_sigint)(); /* Old signal-handler for sigint */
293 static jmp_buf interrupt;
294
295 /* Local signalhandler to allow breaking tranfers or program run.
296 Rely on global variables: old_sigint(), interrupt */
297
298 static void
299 es1800_request_quit ()
300 {
301 /* restore original signalhandler */
302 signal (SIGINT, old_sigint);
303 longjmp (interrupt, 1);
304 }
305
306
307 /* Reset emulator.
308 Sending reset character(octal 32) to emulator.
309 quit - return to '(esgdb)' prompt or continue */
310
311 static void
312 es1800_reset (quit)
313 char *quit;
314 {
315 char buf[80];
316
317 if (quit)
318 {
319 printf ("\nResetting emulator... ");
320 }
321 strcpy (buf, "\032");
322 send (buf);
323 expect_prompt ();
324 if (quit)
325 {
326 error ("done\n");
327 }
328 }
329
330
331 /* Called when SIGALRM signal sent due to alarm() timeout.
332 Rely on global variables: timeout */
333
334 #ifndef HAVE_TERMIO
335
336 static void
337 es1800_timer ()
338 {
339 if (kiodebug)
340 {
341 printf ("es1800_timer called\n");
342 }
343 alarm (timeout);
344 }
345
346 #endif /* HAVE_TERMIO */
347
348
349 /* Open a connection to a remote debugger and push the new target
350 onto the stack. Check if the emulator is responding and find out
351 what kind of processor the emulator is connected to.
352 Initiate the breakpoint handling in the emulator.
353
354 name - the filename used for communication (ex. '/dev/tta')
355 from_tty - says whether to be verbose or not */
356
357 static void
358 es1800_open (name, from_tty)
359 char *name;
360 int from_tty;
361 {
362 TERMINAL sg;
363 char buf[PBUFSIZ];
364 char *p;
365 int i, fcflag;
366 char *baudrate;
367
368 m68020 = 0;
369
370 if (!name) /* no device name given in target command */
371 {
372 error_no_arg ("serial port device name");
373 }
374 baudrate = baud_rate ? baud_rate : "19200"; /* default baudrate = 19200 */
375
376 target_preopen (from_tty);
377 es1800_close (0);
378
379 /* open the device and configure it for communication */
380
381 #ifndef DEBUG_STDIN
382
383 es1800_desc = open (name, O_RDWR);
384 if (es1800_desc < 0)
385 {
386 perror_with_name (name);
387 }
388 savename = savestring (name, strlen (name));
389
390 if (ioctl (es1800_desc, TIOCGETP, &sg) == -1)
391 {
392 perror_with_name (name);
393 }
394 es1800_sg_save = sg;
395
396 if ((fcflag = fcntl (es1800_desc, F_GETFL, 0)) == -1)
397 {
398 perror_with_name ("fcntl serial");
399 }
400 es1800_fc_save = fcflag;
401
402 fcflag = (fcflag & (_FREAD | _FWRITE)); /* mask out any funny stuff */
403 if (fcntl (es1800_desc, F_SETFL, fcflag) == -1)
404 {
405 perror_with_name ("fcntl serial");
406 }
407
408 #ifdef HAVE_TERMIO
409 sg.c_cc[VMIN] = 0; /* read with timeout. */
410 sg.c_cc[VTIME] = timeout * 10;
411 sg.c_lflag &= ~(ICANON | ECHO);
412 sg.c_cflag = (sg.c_cflag & ~CBAUD) | damn_b (baudrate);
413 #else
414 sg.sg_ispeed = damn_b (baudrate);
415 sg.sg_ospeed = damn_b (baudrate);
416 sg.sg_flags = CBREAK+TANDEM;
417 #endif
418
419 if ((ioctl (es1800_desc, TIOCSETP, &sg)) == -1)
420 {
421 perror ("es1800_open: error in ioctl");
422 }
423
424 #endif /* DEBUG_STDIN */
425
426 push_target (&es1800_ops); /* Switch to using remote target now */
427 if (from_tty)
428 {
429 printf ("Remote ES1800 debugging using %s\n", name);
430 }
431
432 #ifndef HAVE_TERMIO
433
434 #ifndef NO_SIGINTERRUPT
435
436 /* Cause SIGALRM's to make reads fail with EINTR instead of resuming
437 the read. */
438
439 if (siginterrupt (SIGALRM, 1) != 0)
440 {
441 perror ("es1800_open: error in siginterrupt");
442 }
443
444 #endif /* NO_SIGINTERRUPT */
445
446 /* Set up read timeout timer. */
447
448 if ((void(*)()) signal (SIGALRM, es1800_timer) == (void(*)()) -1)
449 {
450 perror ("es1800_open: error in signal");
451 }
452
453 #endif /* HAVE_TERMIO */
454
455
456 #if defined (LOG_FILE)
457
458 log_file = fopen (LOG_FILE, "w");
459 if (log_file == NULL)
460 {
461 perror_with_name (LOG_FILE);
462 }
463
464 #endif /* LOG_FILE */
465
466 /* Hello? Are you there?, also check mode */
467
468 /* send_with_reply( "DB 0 TO 1", buf, sizeof(buf)); */
469 /* for (p = buf, i = 0; *p++ =='0';) */ /* count the number of zeros */
470 /* i++; */
471
472 send ("\032");
473 getmessage (buf, sizeof (buf)); /* send reset character */
474
475 if (from_tty)
476 {
477 printf ("Checking mode.... ");
478 }
479 /* m68020 = (i==8); */ /* if eight zeros then we are in m68020 mode */
480
481 /* What kind of processor am i talking to ?*/
482 p = buf;
483 while (*p++ != '\n') {;}
484 while (*p++ != '\n') {;}
485 while (*p++ != '\n') {;}
486 for (i = 0; i < 20; i++, p++) {;}
487 m68020 = !strncmp (p, "68020", 5);
488 if (from_tty)
489 {
490 printf ("You are in %s(%c%c%c%c%c)-mode\n", MODE, p[0], p[1], p[2],
491 p[3], p[4]);
492 }
493
494 /* if no init_break statement is present in .gdb file we have to check
495 whether to download a breakpoint routine or not */
496
497 #if 0
498 if ((es1800_break_vec == 0) || (verify_break (es1800_break_vec) != 0)
499 && query ("No breakpoint routine in ES 1800 emulator!\nDownload a breakpoint routine to the emulator? "))
500 {
501 CORE_ADDR memaddress;
502 printf ("Give the start address of the breakpoint routine: ");
503 scanf ("%li", &memaddress);
504 es1800_init_break ((es1800_break_vec ? es1800_break_vec :
505 ES1800_BREAK_VEC), memaddress);
506 }
507 #endif
508
509 }
510
511 /* Close out all files and local state before this target loses control.
512 quitting - are we quitting gdb now? */
513
514 static void
515 es1800_close (quitting)
516 int quitting;
517 {
518 if (es1800_desc >= 0)
519 {
520 printf ("\nClosing connection to emulator...\n");
521 ioctl (es1800_desc, TIOCSETP, &es1800_sg_save);
522 fcntl (es1800_desc,F_SETFL, es1800_fc_save);
523 close (es1800_desc);
524 es1800_desc = -1;
525 }
526 if (savename != NULL)
527 {
528 free (savename);
529 }
530 savename = NULL;
531
532 #if defined (LOG_FILE)
533
534 if (log_file != NULL)
535 {
536 if (ferror (log_file))
537 {
538 printf ("Error writing log file.\n");
539 }
540 if (fclose (log_file) != 0)
541 {
542 printf ("Error closing log file.\n");
543 }
544 log_file = NULL;
545 }
546
547 #endif /* LOG_FILE */
548
549 }
550
551 /* damn_b()
552
553 Translate baud rates from integers to damn B_codes. Unix should
554 have outgrown this crap years ago, but even POSIX wouldn't buck it.
555 rate - the baudrate given as a string
556 return value: the baudrate as a B_code */
557
558 #ifndef B19200
559 # define B19200 EXTA
560 #endif
561 #ifndef B38400
562 # define B38400 EXTB
563 #endif
564
565 struct
566 {
567 char *rate,
568 damn_b;
569 } baudtab[] = {
570 {"0", B0},
571 {"50", B50},
572 {"75", B75},
573 {"110", B110},
574 {"134", B134},
575 {"150", B150},
576 {"200", B200},
577 {"300", B300},
578 {"600", B600},
579 {"1200", B1200},
580 {"1800", B1800},
581 {"2400", B2400},
582 {"4800", B4800},
583 {"9600", B9600},
584 {"19200", B19200},
585 {"38400", B38400},
586 {0, -1},
587 };
588
589 static int
590 damn_b (rate)
591 char *rate;
592 {
593 int i;
594
595 for (i = 0; baudtab[i].rate != 0; i++)
596 {
597 if (!strcmp (rate, baudtab[i].rate))
598 {
599 return (baudtab[i].damn_b);
600 }
601 }
602 error ("Illegal baudrate");
603 }
604
605
606 /* Attaches to a process on the target side
607 proc_id - the id of the process to be attached.
608 from_tty - says whether to be verbose or not */
609
610 static void
611 es1800_attach (args, from_tty)
612 char *args;
613 int from_tty;
614 {
615 error ("Cannot attach to pid %s, this feature is not implemented yet.",
616 args);
617 }
618
619
620 /* Takes a program previously attached to and detaches it.
621 We better not have left any breakpoints
622 in the program or it'll die when it hits one.
623 Close the open connection to the remote debugger.
624 Use this when you want to detach and do something else
625 with your gdb.
626
627 args - arguments given to the 'detach' command
628 from_tty - says whether to be verbose or not */
629
630 static void
631 es1800_detach (args, from_tty)
632 char *args;
633 int from_tty;
634 {
635 if (args)
636 {
637 error ("Argument given to \"detach\" when remotely debugging.");
638 }
639 pop_target ();
640 if (from_tty)
641 {
642 printf ("Ending es1800 remote debugging.\n");
643 }
644 }
645
646
647 /* Tell the remote machine to resume.
648 step - single-step or run free
649 siggnal - the signal value to be given to the target (0 = no signal) */
650
651 static void
652 es1800_resume (step, siggnal)
653 int step;
654 int siggnal;
655 {
656 char buf[PBUFSIZ];
657
658 if (siggnal)
659 {
660 error ("Can't send signals to a remote system.");
661 }
662 if (step)
663 {
664 strcpy (buf,"STP\r");
665 send (buf);
666 }
667 else
668 {
669 send_command ("RBK");
670 }
671 }
672
673 /* Wait until the remote machine stops, then return,
674 storing status in STATUS just as `wait' would.
675 status - */
676
677 static int
678 es1800_wait (status)
679 WAITTYPE *status;
680 {
681 unsigned char buf[PBUFSIZ];
682 int old_timeout = timeout;
683
684 WSETEXIT ((*status), 0);
685 timeout = 0; /* Don't time out -- user program is running. */
686 if (!setjmp (interrupt))
687 {
688 old_sigint = signal (SIGINT, es1800_request_quit);
689 while (1)
690 {
691 getmessage (buf, sizeof(buf));
692 if (strncmp ( buf, "\r\n* BREAK *", 11) == 0)
693 {
694 WSETSTOP ((*status), SIGTRAP);
695 send_command ("STP"); /* Restore stack and PC and such */
696 if (m68020)
697 {
698 send_command ("STP");
699 }
700 break;
701 }
702 if (strncmp (buf, "STP\r\n ", 6) == 0)
703 {
704 WSETSTOP ((*status), SIGTRAP);
705 break;
706 }
707 if (buf[strlen (buf) - 2] == 'R')
708 {
709 printf ("Unexpected emulator reply: \n%s\n", buf);
710 }
711 else
712 {
713 printf ("Unexpected stop: \n%s\n", buf);
714 WSETSTOP ((*status), SIGQUIT);
715 break;
716 }
717 }
718 }
719 else
720 {
721 fflush (stdin);
722 printf ("\nStopping emulator...");
723 if (!setjmp (interrupt))
724 {
725 old_sigint = signal (SIGINT, es1800_request_quit);
726 send_command ("STP");
727 printf (" emulator stopped\n");
728 WSETSTOP ((*status), SIGINT);
729 }
730 else
731 {
732 fflush (stdin);
733 es1800_reset ((char*) 1);
734 }
735 }
736 signal (SIGINT, old_sigint);
737 timeout = old_timeout;
738 return (0);
739 }
740
741
742 /* Fetch register values from remote machine.
743 regno - the register to be fetched (fetch all registers if -1) */
744
745 static void
746 es1800_fetch_register (regno)
747 int regno;
748 {
749 char buf[PBUFSIZ];
750 int k;
751 int r;
752 char *p;
753 static char regtab[18][4] =
754 {
755 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
756 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
757 "SR ", "PC "
758 };
759
760 if ((regno < 15) || (regno == 16) || (regno == 17))
761 {
762 r = regno * 4;
763 send_with_reply (regtab[regno], buf, sizeof (buf));
764 p = buf;
765 for (k = 0; k < 4; k++)
766 {
767 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
768 {
769 error ("Emulator reply is too short: %s", buf);
770 }
771 registers[r++] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
772 }
773 }
774 else
775 {
776 es1800_fetch_registers ();
777 }
778 }
779
780 /* Read the remote registers into REGISTERS.
781 Always fetches all registers. */
782
783 static void
784 es1800_fetch_registers ()
785 {
786 char buf[PBUFSIZ];
787 char SR_buf[PBUFSIZ];
788 int i;
789 int k;
790 int r;
791 char *p;
792
793 send_with_reply ("DR", buf, sizeof (buf));
794
795 /* Reply is edited to a string that describes registers byte by byte,
796 each byte encoded as two hex characters. */
797
798 p = buf;
799 r = 0;
800
801 /* parsing row one - D0-D7-registers */
802
803 while (*p++ != '\n') {;}
804 for (i = 4; i < 70; i += (i == 39 ? 3 : 1))
805 {
806 for (k = 0; k < 4; k++)
807 {
808 if (p[i+0] == 0 || p[i+1] == 0)
809 {
810 error ("Emulator reply is too short: %s", buf);
811 }
812 registers[r++] = (fromhex (p[i+0]) * 16) + fromhex (p[i+1]);
813 i += 2;
814 }
815 }
816 p += i;
817
818 /* parsing row two - A0-A6-registers */
819
820 while (*p++ != '\n') {;}
821 for (i = 4; i < 61; i += (i == 39 ? 3 : 1))
822 {
823 for (k = 0; k < 4; k++)
824 {
825 if (p[i+0] == 0 || p[i+1] == 0)
826 {
827 error ("Emulator reply is too short: %s", buf);
828 }
829 registers[r++] = (fromhex (p[i+0])) * 16 + fromhex (p[i+1]);
830 i += 2;
831 }
832 }
833 p += i;
834
835 while (*p++ != '\n') {;}
836
837 /* fetch SSP-, SR- and PC-registers */
838
839 /* first - check STATUS-word and decide which stackpointer to use */
840
841 send_with_reply ("SR", SR_buf, sizeof (SR_buf));
842 p = SR_buf;
843 p += 5;
844
845 if (m68020)
846 {
847 if (*p == '3') /* use masterstackpointer MSP */
848 {
849 send_with_reply ("MSP", buf, sizeof (buf));
850 }
851 else if (*p == '2') /* use interruptstackpointer ISP */
852 {
853 send_with_reply ("ISP", buf, sizeof (buf));
854 }
855 else /* use userstackpointer USP */
856 {
857 send_with_reply ("USP", buf, sizeof (buf));
858 }
859 p = buf;
860 for (k = 0; k<4; k++)
861 {
862 if (p[k*2+1] == 0 || p[k*2+2] == 0)
863 {
864 error ("Emulator reply is too short: %s", buf);
865 }
866 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
867 }
868
869 p = SR_buf;
870 for (k = 0; k < 4; k++)
871 {
872 if (p[k*2+1] == 0 || p[k*2+2] == 0)
873 {
874 error ("Emulator reply is too short: %s", buf);
875 }
876 registers[r++] =
877 fromhex (SR_buf[k*2+1]) * 16 + fromhex (SR_buf[k*2+2]);
878 }
879 send_with_reply ("PC", buf, sizeof (buf));
880 p = buf;
881 for (k = 0; k<4; k++)
882 {
883 if (p[k*2+1] == 0 || p[k*2+2] == 0)
884 {
885 error ("Emulator reply is too short: %s", buf);
886 }
887 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
888 }
889 }
890 else /* 68000-mode */
891 {
892 if (*p == '2') /* use supervisorstackpointer SSP */
893 {
894 send_with_reply ("SSP", buf, sizeof (buf));
895 }
896 else /* use userstackpointer USP */
897 {
898 send_with_reply ("USP", buf, sizeof (buf));
899 }
900
901 /* fetch STACKPOINTER */
902
903 p = buf;
904 for (k = 0; k < 4; k++)
905 {
906 if (p[k*2 + 1] == 0 || p[k*2 + 2] == 0)
907 {
908 error ("Emulator reply is too short: %s", buf);
909 }
910 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
911 }
912
913 /* fetch STATUS */
914
915 p = SR_buf;
916 for (k = 0; k < 4; k++)
917 {
918 if (p[k*2+1] == 0 || p[k*2+2] == 0)
919 {
920 error ("Emulator reply is too short: %s", buf);
921 }
922 registers[r++] =
923 fromhex (SR_buf[k*2+1]) * 16 + fromhex (SR_buf[k*2+2]);
924 }
925
926 /* fetch PC */
927
928 send_with_reply ("PC", buf, sizeof (buf));
929 p = buf;
930 for (k = 0; k < 4; k++)
931 {
932 if (p[k*2+1] == 0 || p[k*2+2] == 0)
933 {
934 error ("Emulator reply is too short: %s", buf);
935 }
936 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
937 }
938 }
939 }
940
941 /* Store register value, located in REGISTER, on the target processor.
942 regno - the register-number of the register to store
943 (-1 means store them all)
944 FIXME: Return errno value. */
945
946 static void
947 es1800_store_register(regno)
948 int regno;
949 {
950
951 static char regtab[18][4] =
952 {
953 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
954 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
955 "SR ", "PC "
956 };
957
958 char buf[PBUFSIZ];
959 char SR_buf[PBUFSIZ];
960 char stack_pointer[4];
961 char *p;
962 int i;
963 int j;
964 int k;
965 unsigned char *r;
966
967 r = (unsigned char *) registers;
968
969 if (regno == -1) /* write all registers */
970 {
971 j = 0;
972 k = 18;
973 }
974 else /* write one register */
975 {
976 j = regno;
977 k = regno+1;
978 r += regno * 4;
979 }
980
981 if ((regno == -1) || (regno == 15))
982 {
983 /* fetch current status */
984 send_with_reply ("SR", SR_buf, sizeof (SR_buf));
985 p = SR_buf;
986 p += 5;
987 if (m68020)
988 {
989 if (*p == '3') /* use masterstackpointer MSP */
990 {
991 strcpy (stack_pointer,"MSP");
992 }
993 else
994 {
995 if (*p == '2') /* use interruptstackpointer ISP */
996 {
997 strcpy (stack_pointer,"ISP");
998 }
999 else
1000 {
1001 strcpy (stack_pointer,"USP"); /* use userstackpointer USP */
1002 }
1003 }
1004 }
1005 else /* 68000-mode */
1006 {
1007 if (*p == '2') /* use supervisorstackpointer SSP */
1008 {
1009 strcpy (stack_pointer,"SSP");
1010 }
1011 else
1012 {
1013 strcpy (stack_pointer,"USP");/* use userstackpointer USP */
1014 }
1015 }
1016 strcpy (regtab[15],stack_pointer);
1017 }
1018
1019 for (i = j; i<k; i++)
1020 {
1021 buf[0] = regtab[i][0];
1022 buf[1] = regtab[i][1];
1023 buf[2] = regtab[i][2];
1024 buf[3] = '=';
1025 buf[4] = '$';
1026 buf[5] = tohex ((*r >> 4) & 0x0f);
1027 buf[6] = tohex (*r++ & 0x0f);
1028 buf[7] = tohex ((*r >> 4) & 0x0f);
1029 buf[8] = tohex (*r++ & 0x0f);
1030 buf[9] = tohex ((*r >> 4) & 0x0f);
1031 buf[10] = tohex (*r++ & 0x0f);
1032 buf[11] = tohex ((*r >> 4) & 0x0f);
1033 buf[12] = tohex (*r++ & 0x0f);
1034 buf[13] = 0;
1035
1036 send_with_reply (buf, buf, sizeof (buf)); /* FIXME, reply not used? */
1037 }
1038 }
1039
1040
1041 /* Prepare to store registers. */
1042
1043 static void
1044 es1800_prepare_to_store ()
1045 {
1046 /* Do nothing, since we can store individual regs */
1047 }
1048
1049 /* Convert hex digit A to a number. */
1050
1051 static int
1052 fromhex (a)
1053 int a;
1054 {
1055 if (a >= '0' && a <= '9')
1056 {
1057 return a - '0';
1058 }
1059 else if (a >= 'a' && a <= 'f')
1060 {
1061 return a - 'a' + 10;
1062 }
1063 else if (a >= 'A' && a <= 'F')
1064 {
1065 return a - 'A' + 10;
1066 }
1067 else
1068 {
1069 error ("Reply contains invalid hex digit");
1070 }
1071 return (-1);
1072 }
1073
1074
1075 /* Convert number NIB to a hex digit. */
1076
1077 static int
1078 tohex (nib)
1079 int nib;
1080 {
1081 if (nib < 10)
1082 {
1083 return ('0' + nib);
1084 }
1085 else
1086 {
1087 return ('A' + nib - 10);
1088 }
1089 }
1090
1091 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1092 to or from debugger address MYADDR. Write to inferior if WRITE is
1093 nonzero. Returns length of data written or read; 0 for error.
1094
1095 memaddr - the target's address
1096 myaddr - gdb's address
1097 len - number of bytes
1098 write - write if != 0 otherwise read */
1099
1100 static int
1101 es1800_xfer_inferior_memory (memaddr, myaddr, len, write, tops)
1102 CORE_ADDR memaddr;
1103 char *myaddr;
1104 int len;
1105 int write;
1106 struct target_ops *tops; /* Unused */
1107 {
1108 int origlen = len;
1109 int xfersize;
1110
1111 while (len > 0)
1112 {
1113 xfersize = len > MAXBUFBYTES ? MAXBUFBYTES : len;
1114 if (write)
1115 {
1116 es1800_write_bytes (memaddr, myaddr, xfersize);
1117 }
1118 else
1119 {
1120 es1800_read_bytes (memaddr, myaddr, xfersize);
1121 }
1122 memaddr += xfersize;
1123 myaddr += xfersize;
1124 len -= xfersize;
1125 }
1126 return (origlen); /* no error possible */
1127 }
1128
1129
1130 /* Write memory data directly to the emulator.
1131 This does not inform the data cache; the data cache uses this.
1132 MEMADDR is the address in the remote memory space.
1133 MYADDR is the address of the buffer in our space.
1134 LEN is the number of bytes.
1135
1136 memaddr - the target's address
1137 myaddr - gdb's address
1138 len - number of bytes */
1139
1140 static void
1141 es1800_write_bytes (memaddr, myaddr, len)
1142 CORE_ADDR memaddr;
1143 char *myaddr;
1144 int len;
1145 {
1146 char buf[PBUFSIZ];
1147 int i;
1148 char *p;
1149
1150 p = myaddr;
1151 for (i = 0; i < len; i++)
1152 {
1153 sprintf (buf, "@.B$%x=$%x", memaddr+i, (*p++) & 0xff);
1154 send_with_reply (buf, buf, sizeof (buf)); /* FIXME send_command? */
1155 }
1156 }
1157
1158
1159 /* Read memory data directly from the emulator.
1160 This does not use the data cache; the data cache uses this.
1161
1162 memaddr - the target's address
1163 myaddr - gdb's address
1164 len - number of bytes */
1165
1166 static void
1167 es1800_read_bytes (memaddr, myaddr, len)
1168 CORE_ADDR memaddr;
1169 char *myaddr;
1170 int len;
1171 {
1172 static int DB_tab[16] = {8,11,14,17,20,23,26,29,34,37,40,43,46,49,52,55};
1173 char buf[PBUFSIZ];
1174 int i;
1175 int low_addr;
1176 char *p;
1177 char *b;
1178
1179 if (len > PBUFSIZ / 2 - 1)
1180 {
1181 abort ();
1182 }
1183
1184 if (len == 1) /* The emulator does not like expressions like: */
1185 {
1186 len = 2; /* DB.B $20018 TO $20018 */
1187 }
1188
1189 /* Reply describes registers byte by byte, each byte encoded as two hex
1190 characters. */
1191
1192 sprintf (buf, "DB.B $%x TO $%x", memaddr, memaddr+len-1);
1193 send_with_reply (buf, buf, sizeof (buf));
1194 b = buf;
1195 low_addr = memaddr&0x0f;
1196 for (i = low_addr; i < low_addr + len; i++)
1197 {
1198 if ((!(i % 16)) && i)
1199 { /* if (i = 16,32,48) */
1200 while (*p++!='\n') {;}
1201 b = p;
1202 }
1203 p = b + DB_tab[i%16] + (m68020 ? 2 : 0);
1204 if (p[0] == 32 || p[1] == 32)
1205 {
1206 error ("Emulator reply is too short: %s", buf);
1207 }
1208 myaddr[i-low_addr] = fromhex (p[0]) * 16 + fromhex (p[1]);
1209 }
1210 }
1211
1212 /* Information about the current target */
1213
1214 static void
1215 es1800_files_info (tops)
1216 struct target_ops *tops; /* Unused */
1217 {
1218 printf ("ES1800 Attached to %s at %d baud in %s mode\n", savename, 19200,
1219 MODE);
1220 }
1221
1222
1223 /* We read the contents of the target location and stash it,
1224 then overwrite it with a breakpoint instruction.
1225
1226 addr - is the target location in the target machine.
1227 contents_cache - is a pointer to memory allocated for saving the target contents.
1228 It is guaranteed by the caller to be long enough to save sizeof
1229 BREAKPOINT bytes.
1230
1231 FIXME: This size is target_arch dependent and should be available in
1232 the target_arch transfer vector, if we ever have one... */
1233
1234 static int
1235 es1800_insert_breakpoint (addr, contents_cache)
1236 CORE_ADDR addr;
1237 char *contents_cache;
1238 {
1239 int val;
1240
1241 val = target_read_memory (addr, contents_cache, sizeof (es1800_break_insn));
1242
1243 if (val == 0)
1244 {
1245 val = target_write_memory (addr, es1800_break_insn,
1246 sizeof (es1800_break_insn));
1247 }
1248
1249 return (val);
1250 }
1251
1252
1253 /* Write back the stashed instruction
1254
1255 addr - is the target location in the target machine.
1256 contents_cache - is a pointer to memory allocated for saving the target contents.
1257 It is guaranteed by the caller to be long enough to save sizeof
1258 BREAKPOINT bytes. */
1259
1260 static int
1261 es1800_remove_breakpoint (addr, contents_cache)
1262 CORE_ADDR addr;
1263 char *contents_cache;
1264 {
1265
1266 return (target_write_memory (addr, contents_cache,
1267 sizeof (es1800_break_insn)));
1268 }
1269
1270 /* create_break_insn ()
1271 Primitive datastructures containing the es1800 breakpoint instruction */
1272
1273 static void
1274 es1800_create_break_insn (ins, vec)
1275 char *ins;
1276 int vec;
1277 {
1278 if (vec == 15)
1279 {
1280 ins[0] = 0x4e;
1281 ins[1] = 0x4f;
1282 }
1283 }
1284
1285
1286 /* verify_break ()
1287 Seach for breakpoint routine in emulator memory.
1288 returns non-zero on failure
1289 vec - trap vector used for breakpoints */
1290
1291 static int
1292 verify_break (vec)
1293 int vec;
1294 {
1295 CORE_ADDR memaddress;
1296 char buf[8];
1297 char *instr = "NqNqNqNs"; /* breakpoint routine */
1298 int status;
1299
1300 get_break_addr (vec, &memaddress);
1301
1302 if (memaddress)
1303 {
1304 status = target_read_memory (memaddress, buf, 8);
1305 if (status != 0)
1306 {
1307 memory_error (status, memaddress);
1308 }
1309 return (strcmp (instr, buf));
1310 }
1311 return (-1);
1312 }
1313
1314
1315 /* get_break_addr ()
1316 find address of breakpint routine
1317 vec - trap vector used for breakpoints
1318 addrp - store the address here */
1319
1320 static void
1321 get_break_addr (vec, addrp)
1322 int vec;
1323 CORE_ADDR *addrp;
1324 {
1325 CORE_ADDR memaddress = 0;
1326 int status;
1327 int k;
1328 char buf[PBUFSIZ];
1329 char base_addr[4];
1330 char *p;
1331
1332 if (m68020)
1333 {
1334 send_with_reply ("VBR ", buf, sizeof (buf));
1335 p = buf;
1336 for (k = 0; k < 4; k++)
1337 {
1338 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
1339 {
1340 error ("Emulator reply is too short: %s", buf);
1341 }
1342 base_addr[k] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
1343 }
1344 /* base addr of exception vector table */
1345 memaddress = *((CORE_ADDR *) base_addr);
1346 }
1347
1348 memaddress += (vec + 32) * 4; /* address of trap vector */
1349 status = target_read_memory (memaddress, (char *) addrp, 4);
1350 if (status != 0)
1351 {
1352 memory_error (status, memaddress);
1353 }
1354 }
1355
1356
1357 /* Kill an inferior process */
1358
1359 static void
1360 es1800_kill ()
1361 {
1362 if (inferior_pid != 0)
1363 {
1364 inferior_pid = 0;
1365 es1800_mourn_inferior ();
1366 }
1367 }
1368
1369
1370 /* Load a file to the ES1800 emulator.
1371 Converts the file from a.out format into Extended Tekhex format
1372 before the file is loaded.
1373 Also loads the trap routine, and sets the ES1800 breakpoint on it
1374 filename - the a.out to be loaded
1375 from_tty - says whether to be verbose or not
1376 FIXME Uses emulator overlay memory for trap routine */
1377
1378 static void
1379 es1800_load (filename, from_tty)
1380 char *filename;
1381 int from_tty;
1382 {
1383
1384 FILE *instream;
1385 char loadname[15];
1386 char buf[160];
1387 struct cleanup *old_chain;
1388 int es1800_load_format = 5;
1389
1390 if (es1800_desc < 0)
1391 {
1392 printf ("No emulator attached, type emulator-command first\n");
1393 return;
1394 }
1395
1396 filename = tilde_expand (filename);
1397 make_cleanup (free, filename);
1398
1399 switch (es1800_load_format)
1400 {
1401 case 2: /* Extended Tekhex */
1402 if (from_tty)
1403 {
1404 printf ("Converting \"%s\" to Extended Tekhex Format\n", filename);
1405 }
1406 sprintf (buf, "tekhex %s", filename);
1407 system (buf);
1408 sprintf (loadname, "out.hex");
1409 break;
1410
1411 case 5: /* Motorola S-rec */
1412 if (from_tty)
1413 {
1414 printf ("Converting \"%s\" to Motorola S-record format\n",
1415 filename);
1416 }
1417 /* in the future the source code in copy (part of binutils-1.93) will
1418 be included in this file */
1419 sprintf (buf,
1420 "copy -s \"a.out-sunos-big\" -d \"srec\" %s /tmp/out.hex",
1421 filename);
1422 system (buf);
1423 sprintf (loadname, "/tmp/out.hex");
1424 break;
1425
1426 default:
1427 error ("Downloading format not defined\n");
1428 }
1429
1430 mark_breakpoints_out ();
1431 inferior_pid = 0;
1432 if (from_tty)
1433 {
1434 printf ("Downloading \"%s\" to the ES 1800\n",filename);
1435 }
1436 if ((instream = fopen (loadname, "r")) == NULL)
1437 {
1438 perror_with_name ("fopen:");
1439 }
1440
1441 old_chain = make_cleanup (fclose, instream);
1442 immediate_quit++;
1443
1444 es1800_reset (0);
1445
1446 download (instream, from_tty, es1800_load_format);
1447
1448 /* if breakpoint routine is not present anymore we have to check
1449 whether to download a new breakpoint routine or not */
1450
1451 if ((verify_break (es1800_break_vec) != 0)
1452 && query ("No breakpoint routine in ES 1800 emulator!\nDownload a breakpoint routine to the emulator? "))
1453 {
1454 char buf[128];
1455 printf ("Using break vector 0x%x\n", es1800_break_vec);
1456 sprintf (buf, "0x%x ", es1800_break_vec);
1457 printf ("Give the start address of the breakpoint routine: ");
1458 fgets (buf + strlen (buf), sizeof (buf) - strlen (buf), stdin);
1459 es1800_init_break (buf, 0);
1460 }
1461
1462 do_cleanups (old_chain);
1463 expect_prompt ();
1464 readchar (); /* FIXME I am getting a ^G = 7 after the prompt */
1465 printf ("\n");
1466
1467 if (fclose (instream) == EOF)
1468 {
1469 ;
1470 }
1471
1472 if (es1800_load_format != 2)
1473 {
1474 sprintf (buf, "/usr/bin/rm %s", loadname);
1475 system (buf);
1476 }
1477
1478 symbol_file_command (filename, from_tty); /* reading symbol table */
1479 immediate_quit--;
1480 }
1481
1482 #if 0
1483
1484 #define NUMCPYBYTES 20
1485
1486 static void
1487 bfd_copy (from_bfd, to_bfd)
1488 bfd *from_bfd;
1489 bfd *to_bfd;
1490 {
1491 asection *p, *new;
1492 int i;
1493 char buf[NUMCPYBYTES];
1494
1495 for (p = from_bfd->sections; p != NULL; p = p->next)
1496 {
1497 printf (" Copying section %s. Size = %x.\n", p->name, p->_cooked_size);
1498 printf (" vma = %x, offset = %x, output_sec = %x\n",
1499 p->vma, p->output_offset, p->output_section);
1500 new = bfd_make_section (to_bfd, p->name);
1501 if (p->_cooked_size &&
1502 !bfd_set_section_size (to_bfd, new, p->_cooked_size))
1503 {
1504 error ("Wrong BFD size!\n");
1505 }
1506 if (!bfd_set_section_flags (to_bfd, new, p->flags))
1507 {
1508 error ("bfd_set_section_flags");
1509 }
1510 new->vma = p->vma;
1511
1512 for (i = 0; (i + NUMCPYBYTES) < p->_cooked_size ; i += NUMCPYBYTES)
1513 {
1514 if (!bfd_get_section_contents (from_bfd, p, (PTR) buf, (file_ptr) i,
1515 (bfd_size_type) NUMCPYBYTES))
1516 {
1517 error ("bfd_get_section_contents\n");
1518 }
1519 if (!bfd_set_section_contents (to_bfd, new, (PTR) buf, (file_ptr) i,
1520 (bfd_size_type) NUMCPYBYTES))
1521 {
1522 error ("bfd_set_section_contents\n");
1523 }
1524 }
1525 bfd_get_section_contents (from_bfd, p, (PTR) buf, (file_ptr) i,
1526 (bfd_size_type) (p->_cooked_size - i));
1527 bfd_set_section_contents (to_bfd, new, (PTR) buf,(file_ptr) i,
1528 (bfd_size_type) (p->_cooked_size - i));
1529 }
1530 }
1531
1532 #endif
1533
1534 /* Start an process on the es1800 and set inferior_pid to the new
1535 process' pid.
1536 execfile - the file to run
1537 args - arguments passed to the program
1538 env - the environment vector to pass */
1539
1540 static void
1541 es1800_create_inferior (execfile, args, env)
1542 char *execfile;
1543 char *args;
1544 char **env;
1545 {
1546 int entry_pt;
1547 int pid;
1548 #if 0
1549 struct expression *expr;
1550 register struct cleanup *old_chain = 0;
1551 register value val;
1552 #endif
1553
1554 if (args && *args)
1555 {
1556 error ("Can't pass arguments to remote ES1800 process");
1557 }
1558
1559 #if 0
1560 if (query ("Use 'start' as entry point? "))
1561 {
1562 expr = parse_c_expression ("start");
1563 old_chain = make_cleanup (free_current_contents, &expr);
1564 val = evaluate_expression (expr);
1565 entry_pt = (val->location).address;
1566 }
1567 else
1568 {
1569 printf ("Enter the program's entry point (in hexadecimal): ");
1570 scanf ("%x", &entry_pt);
1571 }
1572 #endif
1573
1574 if (execfile == 0 || exec_bfd == 0)
1575 {
1576 error ("No exec file specified");
1577 }
1578
1579 entry_pt = (int) bfd_get_start_address (exec_bfd);
1580
1581 pid = 42;
1582
1583 /* Now that we have a child process, make it our target. */
1584
1585 push_target (&es1800_child_ops);
1586
1587 /* The "process" (board) is already stopped awaiting our commands, and
1588 the program is already downloaded. We just set its PC and go. */
1589
1590 inferior_pid = pid; /* Needed for wait_for_inferior below */
1591
1592 clear_proceed_status ();
1593
1594 /* Tell wait_for_inferior that we've started a new process. */
1595
1596 init_wait_for_inferior ();
1597
1598 /* Set up the "saved terminal modes" of the inferior
1599 based on what modes we are starting it with. */
1600
1601 target_terminal_init ();
1602
1603 /* Install inferior's terminal modes. */
1604
1605 target_terminal_inferior ();
1606
1607 /* remote_start (args); */
1608 /* trap_expected = 0; */
1609 /* insert_step_breakpoint (); FIXME, do we need this? */
1610
1611 proceed ((CORE_ADDR) entry_pt, -1, 0); /* Let 'er rip... */
1612
1613 }
1614
1615
1616 /* The process has died, clean up. */
1617
1618 static void
1619 es1800_mourn_inferior ()
1620 {
1621 remove_breakpoints ();
1622 unpush_target (&es1800_child_ops);
1623 generic_mourn_inferior (); /* Do all the proper things now */
1624 }
1625
1626 /* ES1800-protocol specific routines */
1627
1628 /* Keep discarding input from the remote system, until STRING is found.
1629 Let the user break out immediately.
1630 string - the string to expect
1631 nowait - break out if string not the emulator's first respond otherwise
1632 read until string is found (== 0) */
1633
1634 static void
1635 expect (string, nowait)
1636 char *string;
1637 int nowait;
1638 {
1639 char c;
1640 char *p = string;
1641
1642 immediate_quit++;
1643 while (1)
1644 {
1645 c = readchar ();
1646 if (isalpha (c))
1647 {
1648 c = toupper (c);
1649 }
1650 if (c == toupper (*p))
1651 {
1652 p++;
1653 if (*p == '\0')
1654 {
1655 immediate_quit--;
1656 return;
1657 }
1658 }
1659 else if (!nowait)
1660 {
1661 p = string;
1662 }
1663 else
1664 {
1665 printf ("\'%s\' expected\n" , string);
1666 printf ("char %d is %d", p - string, c);
1667 error ("\n" );
1668 }
1669 }
1670 }
1671
1672 /* Keep discarding input until we see the prompt. */
1673
1674 static void
1675 expect_prompt ()
1676 {
1677 expect (">", 0);
1678 }
1679
1680
1681 /* Read one character */
1682
1683 #ifdef DEBUG_STDIN
1684
1685 /* read from stdin */
1686
1687 static int
1688 readchar ()
1689 {
1690 char buf[1];
1691
1692 buf[0] = '\0';
1693 printf ("readchar, give one character\n");
1694 read (0, buf, 1);
1695
1696 #if defined (LOG_FILE)
1697 putc (buf[0] & 0x7f, log_file);
1698 #endif
1699
1700 return (buf[0] & 0x7f);
1701 }
1702
1703 #else /* !DEBUG_STDIN */
1704
1705 /* Read a character from the remote system, doing all the fancy
1706 timeout stuff. */
1707
1708 static int
1709 readchar ()
1710 {
1711 char buf[1];
1712
1713 buf[0] = '\0';
1714
1715 #ifdef HAVE_TERMIO
1716
1717 /* termio does the timeout for us. */
1718 read (es1800_desc, buf, 1);
1719
1720 #else
1721
1722 alarm (timeout);
1723 while (read (es1800_desc, buf, 1) != 1)
1724 {
1725 if (errno == EINTR)
1726 {
1727 error ("Timeout reading from remote system.");
1728 }
1729 else if (errno != EWOULDBLOCK)
1730 {
1731 perror_with_name ("remote read");
1732 }
1733 }
1734 alarm (0);
1735 #endif
1736
1737 #if defined (LOG_FILE)
1738 putc (buf[0] & 0x7f, log_file);
1739 fflush (log_file);
1740 #endif
1741
1742 return (buf[0] & 0x7f);
1743 }
1744
1745 #endif /* DEBUG_STDIN */
1746
1747
1748 /* Send a command to the emulator and save the reply.
1749 Report an error if we get an error reply.
1750 string - the es1800 command
1751 buf - containing the emulator reply on return
1752 len - size of buf */
1753
1754 static void
1755 send_with_reply (string, buf, len)
1756 char *string, *buf;
1757 int len;
1758 {
1759 send (string);
1760 write (es1800_desc, "\r", 1);
1761
1762 #ifndef DEBUG_STDIN
1763 expect (string, 1);
1764 expect ("\r\n", 0);
1765 #endif
1766
1767 getmessage (buf, len);
1768 }
1769
1770
1771 /* Send the command in STR to the emulator adding \r. check
1772 the echo for consistency.
1773 string - the es1800 command */
1774
1775 static void
1776 send_command (string)
1777 char *string;
1778 {
1779 send (string);
1780 write (es1800_desc, "\r", 1);
1781
1782 #ifndef DEBUG_STDIN
1783 expect (string, 0);
1784 expect_prompt ();
1785 #endif
1786
1787 }
1788
1789 /* Send a string
1790 string - the es1800 command */
1791
1792 static void
1793 send (string)
1794 char *string;
1795 {
1796 if (kiodebug)
1797 {
1798 fprintf (stderr, "Sending: %s\n", string);
1799 }
1800 write (es1800_desc, string, strlen (string));
1801 }
1802
1803
1804 /* Read a message from the emulator and store it in BUF.
1805 buf - containing the emulator reply on return
1806 len - size of buf */
1807
1808 static void
1809 getmessage (buf, len)
1810 char *buf;
1811 int len;
1812 {
1813 char *bp;
1814 int c;
1815 int prompt_found = 0;
1816 extern kiodebug;
1817
1818 #if defined (LOG_FILE)
1819 /* This is a convenient place to do this. The idea is to do it often
1820 enough that we never lose much data if we terminate abnormally. */
1821 fflush (log_file);
1822 #endif
1823
1824 bp = buf;
1825 c = readchar ();
1826 do
1827 {
1828 if (c)
1829 {
1830 if (len-- < 2) /* char and terminaling NULL */
1831 {
1832 error ("input buffer overrun\n");
1833 }
1834 *bp++ = c;
1835 }
1836 c = readchar ();
1837 if ((c == '>') && (*(bp - 1) == ' '))
1838 {
1839 prompt_found = 1;
1840 }
1841 }
1842 while (!prompt_found);
1843 *bp = 0;
1844
1845 if (kiodebug)
1846 {
1847 fprintf (stderr,"message received :%s\n", buf);
1848 }
1849 }
1850
1851 static void
1852 download (instream, from_tty, format)
1853 FILE *instream;
1854 int from_tty;
1855 int format;
1856 {
1857 char c;
1858 char buf[160];
1859 int i = 0;
1860
1861 send_command ("SET #2,$1A"); /* reset char = ^Z */
1862 send_command ("SET #3,$11,$13"); /* XON XOFF */
1863 if (format == 2)
1864 {
1865 send_command ("SET #26,#2");
1866 }
1867 else
1868 {
1869 send_command ("SET #26,#5"); /* Format=Extended Tekhex */
1870 }
1871 send_command ("DFB = $10");
1872 send_command ("PUR");
1873 send_command ("CES");
1874 send ("DNL\r");
1875 expect ("DNL", 1);
1876 if (from_tty)
1877 {
1878 printf (" 0 records loaded...\r");
1879 }
1880 while (fgets (buf, 160, instream))
1881 {
1882 send (buf);
1883 if (from_tty)
1884 {
1885 printf ("%5d\b\b\b\b\b",++i);
1886 fflush (stdout);
1887 }
1888 if ((c = readchar ()) != 006)
1889 {
1890 error ("expected ACK");
1891 }
1892 }
1893 if (from_tty)
1894 {
1895 printf ("- All");
1896 }
1897 }
1898
1899 /* Additional commands */
1900
1901 /* Talk directly to the emulator
1902 FIXME, uses busy wait, and is SUNOS (or at least BSD) specific */
1903
1904 /*ARGSUSED*/
1905 static void
1906 es1800_transparent (args, from_tty)
1907 char *args;
1908 int from_tty;
1909 {
1910 int console;
1911 struct sgttyb modebl;
1912 int fcflag;
1913 int cc;
1914 struct sgttyb console_mode_save;
1915 int console_fc_save;
1916 int es1800_fc_save;
1917 int inputcnt = 80;
1918 char inputbuf[80];
1919 int consolecnt = 0;
1920 char consolebuf[80];
1921 int es1800_cnt = 0;
1922 char es1800_buf[80];
1923 int i;
1924
1925 dont_repeat ();
1926 if (es1800_desc < 0)
1927 {
1928 printf ("No emulator attached, type emulator-command first\n");
1929 return;
1930 }
1931
1932 printf ("\n");
1933 printf ("You are now communicating directly with the ES 1800 emulator.\n");
1934 printf ("To leave this mode (transparent mode), press ^E.\n");
1935 printf ("\n");
1936 printf (" >");
1937 fflush (stdout);
1938
1939 if ((console = open ("/dev/tty", O_RDWR)) == -1)
1940 {
1941 perror_with_name ("/dev/tty:");
1942 }
1943
1944 if ((fcflag = fcntl (console, F_GETFL, 0)) == -1)
1945 {
1946 perror_with_name ("fcntl console");
1947 }
1948
1949 console_fc_save = fcflag;
1950 fcflag = fcflag | FNDELAY;
1951
1952 if (fcntl (console, F_SETFL, fcflag) == -1)
1953 {
1954 perror_with_name ("fcntl console");
1955 }
1956
1957 if (ioctl (console, TIOCGETP, &modebl))
1958 {
1959 perror_with_name ("ioctl console");
1960 }
1961
1962 console_mode_save = modebl;
1963 modebl.sg_flags = RAW;
1964
1965 if (ioctl (console, TIOCSETP, &modebl))
1966 {
1967 perror_with_name ("ioctl console");
1968 }
1969
1970 if ((fcflag = fcntl (es1800_desc, F_GETFL, 0)) == -1)
1971 {
1972 perror_with_name ("fcntl serial");
1973 }
1974
1975 es1800_fc_save = fcflag;
1976 fcflag = fcflag | FNDELAY;
1977
1978 if (fcntl (es1800_desc, F_SETFL, fcflag) == -1)
1979 {
1980 perror_with_name ("fcntl serial");
1981 }
1982
1983 while (1)
1984 {
1985 cc = read (console, inputbuf, inputcnt);
1986 if (cc != -1)
1987 {
1988 if ((*inputbuf & 0x7f) == 0x05)
1989 {
1990 break;
1991 }
1992 for (i = 0; i < cc; )
1993 {
1994 es1800_buf[es1800_cnt++] = inputbuf[i++];
1995 }
1996 if ((cc = write (es1800_desc, es1800_buf, es1800_cnt)) == -1)
1997 {
1998 perror_with_name ("FEL! read:");
1999 }
2000 es1800_cnt -= cc;
2001 if (es1800_cnt && cc)
2002 {
2003 for (i = 0; i < es1800_cnt; i++)
2004 {
2005 es1800_buf[i] = es1800_buf[cc+i];
2006 }
2007 }
2008 }
2009 else if (errno != EWOULDBLOCK)
2010 {
2011 perror_with_name ("FEL! read:");
2012 }
2013
2014 cc = read (es1800_desc,inputbuf,inputcnt);
2015 if (cc != -1)
2016 {
2017 for (i = 0; i < cc; )
2018 {
2019 consolebuf[consolecnt++] = inputbuf[i++];
2020 }
2021 if ((cc = write (console,consolebuf,consolecnt)) == -1)
2022 {
2023 perror_with_name ("FEL! write:");
2024 }
2025 consolecnt -= cc;
2026 if (consolecnt && cc)
2027 {
2028 for (i = 0; i < consolecnt; i++)
2029 {
2030 consolebuf[i] = consolebuf[cc+i];
2031 }
2032 }
2033 }
2034 else if (errno != EWOULDBLOCK)
2035 {
2036 perror_with_name ("FEL! read:");
2037 }
2038 }
2039
2040 console_fc_save = console_fc_save & !FNDELAY;
2041 if (fcntl (console, F_SETFL, console_fc_save) == -1)
2042 {
2043 perror_with_name ("FEL! fcntl");
2044 }
2045
2046 if (ioctl (console, TIOCSETP, &console_mode_save))
2047 {
2048 perror_with_name ("FEL! ioctl");
2049 }
2050
2051 close (console);
2052
2053 if (fcntl (es1800_desc, F_SETFL, es1800_fc_save) == -1)
2054 {
2055 perror_with_name ("FEL! fcntl");
2056 }
2057
2058 printf ("\n");
2059
2060 }
2061
2062 static void
2063 es1800_init_break (args, from_tty)
2064 char *args;
2065 int from_tty;
2066 {
2067 CORE_ADDR memaddress = 0;
2068 char buf[PBUFSIZ];
2069 char base_addr[4];
2070 char *space_index;
2071 char *p;
2072 int k;
2073
2074 if (args == NULL)
2075 {
2076 error_no_arg ("a trap vector");
2077 }
2078
2079 if (!(space_index = strchr (args, ' ')))
2080 {
2081 error ("Two arguments needed (trap vector and address of break routine).\n");
2082 }
2083
2084 *space_index = '\0';
2085
2086 es1800_break_vec = strtol (args, (char **) NULL, 0);
2087 es1800_break_address = parse_and_eval_address (space_index + 1);
2088
2089 es1800_create_break_insn (es1800_break_insn, es1800_break_vec);
2090
2091 if (m68020)
2092 {
2093 send_with_reply ("VBR ", buf, sizeof (buf));
2094 p = buf;
2095 for (k = 0; k < 4; k++)
2096 {
2097 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
2098 {
2099 error ("Emulator reply is too short: %s", buf);
2100 }
2101 base_addr[k] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
2102 }
2103 /* base addr of exception vector table */
2104 memaddress = *((CORE_ADDR *) base_addr);
2105 }
2106
2107 memaddress += (es1800_break_vec + 32) * 4; /* address of trap vector */
2108
2109 sprintf (buf, "@.L%lx=$%lx", memaddress, es1800_break_address);
2110 send_command (buf); /* set the address of the break routine in the */
2111 /* trap vector */
2112
2113 sprintf (buf, "@.L%lx=$4E714E71", es1800_break_address); /* NOP; NOP */
2114 send_command (buf);
2115 sprintf (buf, "@.L%lx=$4E714E73", es1800_break_address + 4); /* NOP; RTE */
2116 send_command (buf);
2117
2118 sprintf (buf, "AC2=$%lx", es1800_break_address + 4);
2119 /* breakpoint at es1800-break_address */
2120 send_command (buf);
2121 send_command ("WHEN AC2 THEN BRK"); /* ie in exception routine */
2122
2123 if (from_tty)
2124 {
2125 printf ("Breakpoint (trap $%x) routine at address: %lx\n",
2126 es1800_break_vec, es1800_break_address);
2127 }
2128 }
2129
2130 static void
2131 es1800_child_open (arg, from_tty)
2132 char *arg;
2133 int from_tty;
2134 {
2135 error ("Use the \"run\" command to start a child process.");
2136 }
2137
2138 static void
2139 es1800_child_detach (args, from_tty)
2140 char *args;
2141 int from_tty;
2142 {
2143 if (args)
2144 {
2145 error ("Argument given to \"detach\" when remotely debugging.");
2146 }
2147
2148 pop_target ();
2149 if (from_tty)
2150 {
2151 printf ("Ending debugging the process %d.\n", inferior_pid);
2152 }
2153 }
2154
2155
2156 /* Define the target subroutine names */
2157
2158 static struct target_ops es1800_ops =
2159 {
2160 "es1800", /* to_shortname */
2161 /* to_longname */
2162 "Remote serial target in ES1800-emulator protocol",
2163 /* to_doc */
2164 "Remote debugging on the es1800 emulator via a serial line.\n\
2165 Specify the serial device it is connected to (e.g. /dev/ttya).",
2166 es1800_open, /* to_open */
2167 es1800_close, /* to_close */
2168 es1800_attach, /* to_attach */
2169 es1800_detach, /* to_detach */
2170 es1800_resume, /* to_resume */
2171 NULL, /* to_wait */
2172 NULL, /* to_fetch_registers */
2173 NULL, /* to_store_registers */
2174 es1800_prepare_to_store, /* to_prepare_to_store */
2175 NULL, /* to_convert_to_virtual */
2176 NULL, /* to_convert_from_virtual */
2177 es1800_xfer_inferior_memory, /* to_xfer_memory */
2178 es1800_files_info, /* to_files_info */
2179 es1800_insert_breakpoint, /* to_insert_breakpoint */
2180 es1800_remove_breakpoint, /* to_remove_breakpoint */
2181 NULL, /* to_terminal_init */
2182 NULL, /* to_terminal_inferior */
2183 NULL, /* to_terminal_ours_for_output */
2184 NULL, /* to_terminal_ours */
2185 NULL, /* to_terminal_info */
2186 NULL, /* to_kill */
2187 es1800_load, /* to_load */
2188 NULL, /* to_lookup_symbol */
2189 es1800_create_inferior, /* to_create_inferior */
2190 NULL, /* to_mourn_inferior */
2191 core_stratum, /* to_stratum */
2192 0, /* to_next */
2193 0, /* to_has_all_memory */
2194 1, /* to_has_memory */
2195 0, /* to_has_stack */
2196 0, /* to_has_registers */
2197 0, /* to_has_execution */
2198 NULL, /* to_sections */
2199 NULL, /* to_sections_end */
2200 OPS_MAGIC /* to_magic (always last) */
2201 };
2202
2203 /* Define the target subroutine names */
2204
2205 static struct target_ops es1800_child_ops =
2206 {
2207 "es1800_process", /* to_shortname */
2208 /* to_longname */
2209 "Remote serial target in ES1800-emulator protocol",
2210 /* to_doc */
2211 "Remote debugging on the es1800 emulator via a serial line.\n\
2212 Specify the serial device it is connected to (e.g. /dev/ttya).",
2213 es1800_child_open, /* to_open */
2214 NULL, /* to_close */
2215 es1800_attach, /* to_attach */
2216 es1800_child_detach, /* to_detach */
2217 es1800_resume, /* to_resume */
2218 es1800_wait, /* to_wait */
2219 es1800_fetch_register, /* to_fetch_registers */
2220 es1800_store_register, /* to_store_registers */
2221 es1800_prepare_to_store, /* to_prepare_to_store */
2222 NULL, /* to_convert_to_virtual */
2223 NULL, /* to_convert_from_virtual */
2224 es1800_xfer_inferior_memory, /* to_xfer_memory */
2225 es1800_files_info, /* to_files_info */
2226 es1800_insert_breakpoint, /* to_insert_breakpoint */
2227 es1800_remove_breakpoint, /* to_remove_breakpoint */
2228 NULL, /* to_terminal_init */
2229 NULL, /* to_terminal_inferior */
2230 NULL, /* to_terminal_ours_for_output */
2231 NULL, /* to_terminal_ours */
2232 NULL, /* to_terminal_info */
2233 es1800_kill, /* to_kill */
2234 es1800_load, /* to_load */
2235 NULL, /* to_lookup_symbol */
2236 es1800_create_inferior, /* to_create_inferior */
2237 es1800_mourn_inferior, /* to_mourn_inferior */
2238 process_stratum, /* to_stratum */
2239 0, /* to_next */
2240 1, /* to_has_all_memory */
2241 1, /* to_has_memory */
2242 1, /* to_has_stack */
2243 1, /* to_has_registers */
2244 1, /* to_has_execution */
2245 NULL, /* to_sections */
2246 NULL, /* to_sections_end */
2247 OPS_MAGIC /* to_magic (always last) */
2248 };
2249
2250 /* _initialize_es1800 () */
2251
2252 void
2253 _initialize_es1800 ()
2254 {
2255 add_target (&es1800_ops);
2256 add_target (&es1800_child_ops);
2257 add_com ("transparent", class_support, es1800_transparent,
2258 "Start transparent communication with the ES 1800 emulator.");
2259 add_com ("init_break", class_support, es1800_init_break,
2260 "Download break routine and initialize break facility on ES 1800");
2261 }
This page took 0.103276 seconds and 5 git commands to generate.