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