* Rename remote-es1800.c to remote-es.c
[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));
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 (step, siggnal)
654 int step;
655 int siggnal;
656 {
657 char buf[PBUFSIZ];
658
659 if (siggnal)
660 {
661 error ("Can't send signals to a remote system.");
662 }
663 if (step)
664 {
665 strcpy (buf,"STP\r");
666 send (buf);
667 }
668 else
669 {
670 send_command ("RBK");
671 }
672 }
673
674 /* Wait until the remote machine stops, then return,
675 storing status in STATUS just as `wait' would.
676 status - */
677
678 static int
679 es1800_wait (status)
680 WAITTYPE *status;
681 {
682 unsigned char buf[PBUFSIZ];
683 int old_timeout = timeout;
684
685 WSETEXIT ((*status), 0);
686 timeout = 0; /* Don't time out -- user program is running. */
687 if (!setjmp (interrupt))
688 {
689 old_sigint = signal (SIGINT, es1800_request_quit);
690 while (1)
691 {
692 getmessage (buf, sizeof(buf));
693 if (strncmp ( buf, "\r\n* BREAK *", 11) == 0)
694 {
695 WSETSTOP ((*status), SIGTRAP);
696 send_command ("STP"); /* Restore stack and PC and such */
697 if (m68020)
698 {
699 send_command ("STP");
700 }
701 break;
702 }
703 if (strncmp (buf, "STP\r\n ", 6) == 0)
704 {
705 WSETSTOP ((*status), SIGTRAP);
706 break;
707 }
708 if (buf[strlen (buf) - 2] == 'R')
709 {
710 printf ("Unexpected emulator reply: \n%s\n", buf);
711 }
712 else
713 {
714 printf ("Unexpected stop: \n%s\n", buf);
715 WSETSTOP ((*status), SIGQUIT);
716 break;
717 }
718 }
719 }
720 else
721 {
722 fflush (stdin);
723 printf ("\nStopping emulator...");
724 if (!setjmp (interrupt))
725 {
726 old_sigint = signal (SIGINT, es1800_request_quit);
727 send_command ("STP");
728 printf (" emulator stopped\n");
729 WSETSTOP ((*status), SIGINT);
730 }
731 else
732 {
733 fflush (stdin);
734 es1800_reset ((char*) 1);
735 }
736 }
737 signal (SIGINT, old_sigint);
738 timeout = old_timeout;
739 return (0);
740 }
741
742
743 /* Fetch register values from remote machine.
744 regno - the register to be fetched (fetch all registers if -1) */
745
746 static void
747 es1800_fetch_register (regno)
748 int regno;
749 {
750 char buf[PBUFSIZ];
751 int k;
752 int r;
753 char *p;
754 static char regtab[18][4] =
755 {
756 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
757 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
758 "SR ", "PC "
759 };
760
761 if ((regno < 15) || (regno == 16) || (regno == 17))
762 {
763 r = regno * 4;
764 send_with_reply (regtab[regno], buf, sizeof (buf));
765 p = buf;
766 for (k = 0; k < 4; k++)
767 {
768 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
769 {
770 error ("Emulator reply is too short: %s", buf);
771 }
772 registers[r++] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
773 }
774 }
775 else
776 {
777 es1800_fetch_registers ();
778 }
779 }
780
781 /* Read the remote registers into REGISTERS.
782 Always fetches all registers. */
783
784 static void
785 es1800_fetch_registers ()
786 {
787 char buf[PBUFSIZ];
788 char SR_buf[PBUFSIZ];
789 int i;
790 int k;
791 int r;
792 char *p;
793
794 send_with_reply ("DR", buf, sizeof (buf));
795
796 /* Reply is edited to a string that describes registers byte by byte,
797 each byte encoded as two hex characters. */
798
799 p = buf;
800 r = 0;
801
802 /* parsing row one - D0-D7-registers */
803
804 while (*p++ != '\n') {;}
805 for (i = 4; i < 70; i += (i == 39 ? 3 : 1))
806 {
807 for (k = 0; k < 4; k++)
808 {
809 if (p[i+0] == 0 || p[i+1] == 0)
810 {
811 error ("Emulator reply is too short: %s", buf);
812 }
813 registers[r++] = (fromhex (p[i+0]) * 16) + fromhex (p[i+1]);
814 i += 2;
815 }
816 }
817 p += i;
818
819 /* parsing row two - A0-A6-registers */
820
821 while (*p++ != '\n') {;}
822 for (i = 4; i < 61; i += (i == 39 ? 3 : 1))
823 {
824 for (k = 0; k < 4; k++)
825 {
826 if (p[i+0] == 0 || p[i+1] == 0)
827 {
828 error ("Emulator reply is too short: %s", buf);
829 }
830 registers[r++] = (fromhex (p[i+0])) * 16 + fromhex (p[i+1]);
831 i += 2;
832 }
833 }
834 p += i;
835
836 while (*p++ != '\n') {;}
837
838 /* fetch SSP-, SR- and PC-registers */
839
840 /* first - check STATUS-word and decide which stackpointer to use */
841
842 send_with_reply ("SR", SR_buf, sizeof (SR_buf));
843 p = SR_buf;
844 p += 5;
845
846 if (m68020)
847 {
848 if (*p == '3') /* use masterstackpointer MSP */
849 {
850 send_with_reply ("MSP", buf, sizeof (buf));
851 }
852 else if (*p == '2') /* use interruptstackpointer ISP */
853 {
854 send_with_reply ("ISP", buf, sizeof (buf));
855 }
856 else /* use userstackpointer USP */
857 {
858 send_with_reply ("USP", buf, sizeof (buf));
859 }
860 p = buf;
861 for (k = 0; k<4; k++)
862 {
863 if (p[k*2+1] == 0 || p[k*2+2] == 0)
864 {
865 error ("Emulator reply is too short: %s", buf);
866 }
867 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
868 }
869
870 p = SR_buf;
871 for (k = 0; k < 4; k++)
872 {
873 if (p[k*2+1] == 0 || p[k*2+2] == 0)
874 {
875 error ("Emulator reply is too short: %s", buf);
876 }
877 registers[r++] =
878 fromhex (SR_buf[k*2+1]) * 16 + fromhex (SR_buf[k*2+2]);
879 }
880 send_with_reply ("PC", buf, sizeof (buf));
881 p = buf;
882 for (k = 0; k<4; k++)
883 {
884 if (p[k*2+1] == 0 || p[k*2+2] == 0)
885 {
886 error ("Emulator reply is too short: %s", buf);
887 }
888 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
889 }
890 }
891 else /* 68000-mode */
892 {
893 if (*p == '2') /* use supervisorstackpointer SSP */
894 {
895 send_with_reply ("SSP", buf, sizeof (buf));
896 }
897 else /* use userstackpointer USP */
898 {
899 send_with_reply ("USP", buf, sizeof (buf));
900 }
901
902 /* fetch STACKPOINTER */
903
904 p = buf;
905 for (k = 0; k < 4; k++)
906 {
907 if (p[k*2 + 1] == 0 || p[k*2 + 2] == 0)
908 {
909 error ("Emulator reply is too short: %s", buf);
910 }
911 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
912 }
913
914 /* fetch STATUS */
915
916 p = SR_buf;
917 for (k = 0; k < 4; k++)
918 {
919 if (p[k*2+1] == 0 || p[k*2+2] == 0)
920 {
921 error ("Emulator reply is too short: %s", buf);
922 }
923 registers[r++] =
924 fromhex (SR_buf[k*2+1]) * 16 + fromhex (SR_buf[k*2+2]);
925 }
926
927 /* fetch PC */
928
929 send_with_reply ("PC", buf, sizeof (buf));
930 p = buf;
931 for (k = 0; k < 4; k++)
932 {
933 if (p[k*2+1] == 0 || p[k*2+2] == 0)
934 {
935 error ("Emulator reply is too short: %s", buf);
936 }
937 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
938 }
939 }
940 }
941
942 /* Store register value, located in REGISTER, on the target processor.
943 regno - the register-number of the register to store
944 (-1 means store them all)
945 FIXME: Return errno value. */
946
947 static void
948 es1800_store_register(regno)
949 int regno;
950 {
951
952 static char regtab[18][4] =
953 {
954 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
955 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
956 "SR ", "PC "
957 };
958
959 char buf[PBUFSIZ];
960 char SR_buf[PBUFSIZ];
961 char stack_pointer[4];
962 char *p;
963 int i;
964 int j;
965 int k;
966 unsigned char *r;
967
968 r = (unsigned char *) registers;
969
970 if (regno == -1) /* write all registers */
971 {
972 j = 0;
973 k = 18;
974 }
975 else /* write one register */
976 {
977 j = regno;
978 k = regno+1;
979 r += regno * 4;
980 }
981
982 if ((regno == -1) || (regno == 15))
983 {
984 /* fetch current status */
985 send_with_reply ("SR", SR_buf, sizeof (SR_buf));
986 p = SR_buf;
987 p += 5;
988 if (m68020)
989 {
990 if (*p == '3') /* use masterstackpointer MSP */
991 {
992 strcpy (stack_pointer,"MSP");
993 }
994 else
995 {
996 if (*p == '2') /* use interruptstackpointer ISP */
997 {
998 strcpy (stack_pointer,"ISP");
999 }
1000 else
1001 {
1002 strcpy (stack_pointer,"USP"); /* use userstackpointer USP */
1003 }
1004 }
1005 }
1006 else /* 68000-mode */
1007 {
1008 if (*p == '2') /* use supervisorstackpointer SSP */
1009 {
1010 strcpy (stack_pointer,"SSP");
1011 }
1012 else
1013 {
1014 strcpy (stack_pointer,"USP");/* use userstackpointer USP */
1015 }
1016 }
1017 strcpy (regtab[15],stack_pointer);
1018 }
1019
1020 for (i = j; i<k; i++)
1021 {
1022 buf[0] = regtab[i][0];
1023 buf[1] = regtab[i][1];
1024 buf[2] = regtab[i][2];
1025 buf[3] = '=';
1026 buf[4] = '$';
1027 buf[5] = tohex ((*r >> 4) & 0x0f);
1028 buf[6] = tohex (*r++ & 0x0f);
1029 buf[7] = tohex ((*r >> 4) & 0x0f);
1030 buf[8] = tohex (*r++ & 0x0f);
1031 buf[9] = tohex ((*r >> 4) & 0x0f);
1032 buf[10] = tohex (*r++ & 0x0f);
1033 buf[11] = tohex ((*r >> 4) & 0x0f);
1034 buf[12] = tohex (*r++ & 0x0f);
1035 buf[13] = 0;
1036
1037 send_with_reply (buf, buf, sizeof (buf)); /* FIXME, reply not used? */
1038 }
1039 }
1040
1041
1042 /* Prepare to store registers. */
1043
1044 static void
1045 es1800_prepare_to_store ()
1046 {
1047 /* Do nothing, since we can store individual regs */
1048 }
1049
1050 /* Convert hex digit A to a number. */
1051
1052 static int
1053 fromhex (a)
1054 int a;
1055 {
1056 if (a >= '0' && a <= '9')
1057 {
1058 return a - '0';
1059 }
1060 else if (a >= 'a' && a <= 'f')
1061 {
1062 return a - 'a' + 10;
1063 }
1064 else if (a >= 'A' && a <= 'F')
1065 {
1066 return a - 'A' + 10;
1067 }
1068 else
1069 {
1070 error ("Reply contains invalid hex digit");
1071 }
1072 return (-1);
1073 }
1074
1075
1076 /* Convert number NIB to a hex digit. */
1077
1078 static int
1079 tohex (nib)
1080 int nib;
1081 {
1082 if (nib < 10)
1083 {
1084 return ('0' + nib);
1085 }
1086 else
1087 {
1088 return ('A' + nib - 10);
1089 }
1090 }
1091
1092 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1093 to or from debugger address MYADDR. Write to inferior if WRITE is
1094 nonzero. Returns length of data written or read; 0 for error.
1095
1096 memaddr - the target's address
1097 myaddr - gdb's address
1098 len - number of bytes
1099 write - write if != 0 otherwise read */
1100
1101 static int
1102 es1800_xfer_inferior_memory (memaddr, myaddr, len, write, tops)
1103 CORE_ADDR memaddr;
1104 char *myaddr;
1105 int len;
1106 int write;
1107 struct target_ops *tops; /* Unused */
1108 {
1109 int origlen = len;
1110 int xfersize;
1111
1112 while (len > 0)
1113 {
1114 xfersize = len > MAXBUFBYTES ? MAXBUFBYTES : len;
1115 if (write)
1116 {
1117 es1800_write_bytes (memaddr, myaddr, xfersize);
1118 }
1119 else
1120 {
1121 es1800_read_bytes (memaddr, myaddr, xfersize);
1122 }
1123 memaddr += xfersize;
1124 myaddr += xfersize;
1125 len -= xfersize;
1126 }
1127 return (origlen); /* no error possible */
1128 }
1129
1130
1131 /* Write memory data directly to the emulator.
1132 This does not inform the data cache; the data cache uses this.
1133 MEMADDR is the address in the remote memory space.
1134 MYADDR is the address of the buffer in our space.
1135 LEN is the number of bytes.
1136
1137 memaddr - the target's address
1138 myaddr - gdb's address
1139 len - number of bytes */
1140
1141 static void
1142 es1800_write_bytes (memaddr, myaddr, len)
1143 CORE_ADDR memaddr;
1144 char *myaddr;
1145 int len;
1146 {
1147 char buf[PBUFSIZ];
1148 int i;
1149 char *p;
1150
1151 p = myaddr;
1152 for (i = 0; i < len; i++)
1153 {
1154 sprintf (buf, "@.B$%x=$%x", memaddr+i, (*p++) & 0xff);
1155 send_with_reply (buf, buf, sizeof (buf)); /* FIXME send_command? */
1156 }
1157 }
1158
1159
1160 /* Read memory data directly from the emulator.
1161 This does not use the data cache; the data cache uses this.
1162
1163 memaddr - the target's address
1164 myaddr - gdb's address
1165 len - number of bytes */
1166
1167 static void
1168 es1800_read_bytes (memaddr, myaddr, len)
1169 CORE_ADDR memaddr;
1170 char *myaddr;
1171 int len;
1172 {
1173 static int DB_tab[16] = {8,11,14,17,20,23,26,29,34,37,40,43,46,49,52,55};
1174 char buf[PBUFSIZ];
1175 int i;
1176 int low_addr;
1177 char *p;
1178 char *b;
1179
1180 if (len > PBUFSIZ / 2 - 1)
1181 {
1182 abort ();
1183 }
1184
1185 if (len == 1) /* The emulator does not like expressions like: */
1186 {
1187 len = 2; /* DB.B $20018 TO $20018 */
1188 }
1189
1190 /* Reply describes registers byte by byte, each byte encoded as two hex
1191 characters. */
1192
1193 sprintf (buf, "DB.B $%x TO $%x", memaddr, memaddr+len-1);
1194 send_with_reply (buf, buf, sizeof (buf));
1195 b = buf;
1196 low_addr = memaddr&0x0f;
1197 for (i = low_addr; i < low_addr + len; i++)
1198 {
1199 if ((!(i % 16)) && i)
1200 { /* if (i = 16,32,48) */
1201 while (*p++!='\n') {;}
1202 b = p;
1203 }
1204 p = b + DB_tab[i%16] + (m68020 ? 2 : 0);
1205 if (p[0] == 32 || p[1] == 32)
1206 {
1207 error ("Emulator reply is too short: %s", buf);
1208 }
1209 myaddr[i-low_addr] = fromhex (p[0]) * 16 + fromhex (p[1]);
1210 }
1211 }
1212
1213 /* Information about the current target */
1214
1215 static void
1216 es1800_files_info (tops)
1217 struct target_ops *tops; /* Unused */
1218 {
1219 printf ("ES1800 Attached to %s at %d baud in %s mode\n", savename, 19200,
1220 MODE);
1221 }
1222
1223
1224 /* We read the contents of the target location and stash it,
1225 then overwrite it with a breakpoint instruction.
1226
1227 addr - is the target location in the target machine.
1228 contents_cache - is a pointer to memory allocated for saving the target contents.
1229 It is guaranteed by the caller to be long enough to save sizeof
1230 BREAKPOINT bytes.
1231
1232 FIXME: This size is target_arch dependent and should be available in
1233 the target_arch transfer vector, if we ever have one... */
1234
1235 static int
1236 es1800_insert_breakpoint (addr, contents_cache)
1237 CORE_ADDR addr;
1238 char *contents_cache;
1239 {
1240 int val;
1241
1242 val = target_read_memory (addr, contents_cache, sizeof (es1800_break_insn));
1243
1244 if (val == 0)
1245 {
1246 val = target_write_memory (addr, es1800_break_insn,
1247 sizeof (es1800_break_insn));
1248 }
1249
1250 return (val);
1251 }
1252
1253
1254 /* Write back the stashed instruction
1255
1256 addr - is the target location in the target machine.
1257 contents_cache - is a pointer to memory allocated for saving the target contents.
1258 It is guaranteed by the caller to be long enough to save sizeof
1259 BREAKPOINT bytes. */
1260
1261 static int
1262 es1800_remove_breakpoint (addr, contents_cache)
1263 CORE_ADDR addr;
1264 char *contents_cache;
1265 {
1266
1267 return (target_write_memory (addr, contents_cache,
1268 sizeof (es1800_break_insn)));
1269 }
1270
1271 /* create_break_insn ()
1272 Primitive datastructures containing the es1800 breakpoint instruction */
1273
1274 static void
1275 es1800_create_break_insn (ins, vec)
1276 char *ins;
1277 int vec;
1278 {
1279 if (vec == 15)
1280 {
1281 ins[0] = 0x4e;
1282 ins[1] = 0x4f;
1283 }
1284 }
1285
1286
1287 /* verify_break ()
1288 Seach for breakpoint routine in emulator memory.
1289 returns non-zero on failure
1290 vec - trap vector used for breakpoints */
1291
1292 static int
1293 verify_break (vec)
1294 int vec;
1295 {
1296 CORE_ADDR memaddress;
1297 char buf[8];
1298 char *instr = "NqNqNqNs"; /* breakpoint routine */
1299 int status;
1300
1301 get_break_addr (vec, &memaddress);
1302
1303 if (memaddress)
1304 {
1305 status = target_read_memory (memaddress, buf, 8);
1306 if (status != 0)
1307 {
1308 memory_error (status, memaddress);
1309 }
1310 return (STRCMP (instr, buf));
1311 }
1312 return (-1);
1313 }
1314
1315
1316 /* get_break_addr ()
1317 find address of breakpint routine
1318 vec - trap vector used for breakpoints
1319 addrp - store the address here */
1320
1321 static void
1322 get_break_addr (vec, addrp)
1323 int vec;
1324 CORE_ADDR *addrp;
1325 {
1326 CORE_ADDR memaddress = 0;
1327 int status;
1328 int k;
1329 char buf[PBUFSIZ];
1330 char base_addr[4];
1331 char *p;
1332
1333 if (m68020)
1334 {
1335 send_with_reply ("VBR ", buf, sizeof (buf));
1336 p = buf;
1337 for (k = 0; k < 4; k++)
1338 {
1339 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
1340 {
1341 error ("Emulator reply is too short: %s", buf);
1342 }
1343 base_addr[k] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
1344 }
1345 /* base addr of exception vector table */
1346 memaddress = *((CORE_ADDR *) base_addr);
1347 }
1348
1349 memaddress += (vec + 32) * 4; /* address of trap vector */
1350 status = target_read_memory (memaddress, (char *) addrp, 4);
1351 if (status != 0)
1352 {
1353 memory_error (status, memaddress);
1354 }
1355 }
1356
1357
1358 /* Kill an inferior process */
1359
1360 static void
1361 es1800_kill ()
1362 {
1363 if (inferior_pid != 0)
1364 {
1365 inferior_pid = 0;
1366 es1800_mourn_inferior ();
1367 }
1368 }
1369
1370
1371 /* Load a file to the ES1800 emulator.
1372 Converts the file from a.out format into Extended Tekhex format
1373 before the file is loaded.
1374 Also loads the trap routine, and sets the ES1800 breakpoint on it
1375 filename - the a.out to be loaded
1376 from_tty - says whether to be verbose or not
1377 FIXME Uses emulator overlay memory for trap routine */
1378
1379 static void
1380 es1800_load (filename, from_tty)
1381 char *filename;
1382 int from_tty;
1383 {
1384
1385 FILE *instream;
1386 char loadname[15];
1387 char buf[160];
1388 struct cleanup *old_chain;
1389 int es1800_load_format = 5;
1390
1391 if (es1800_desc < 0)
1392 {
1393 printf ("No emulator attached, type emulator-command first\n");
1394 return;
1395 }
1396
1397 filename = tilde_expand (filename);
1398 make_cleanup (free, filename);
1399
1400 switch (es1800_load_format)
1401 {
1402 case 2: /* Extended Tekhex */
1403 if (from_tty)
1404 {
1405 printf ("Converting \"%s\" to Extended Tekhex Format\n", filename);
1406 }
1407 sprintf (buf, "tekhex %s", filename);
1408 system (buf);
1409 sprintf (loadname, "out.hex");
1410 break;
1411
1412 case 5: /* Motorola S-rec */
1413 if (from_tty)
1414 {
1415 printf ("Converting \"%s\" to Motorola S-record format\n",
1416 filename);
1417 }
1418 /* in the future the source code in copy (part of binutils-1.93) will
1419 be included in this file */
1420 sprintf (buf,
1421 "copy -s \"a.out-sunos-big\" -d \"srec\" %s /tmp/out.hex",
1422 filename);
1423 system (buf);
1424 sprintf (loadname, "/tmp/out.hex");
1425 break;
1426
1427 default:
1428 error ("Downloading format not defined\n");
1429 }
1430
1431 mark_breakpoints_out ();
1432 inferior_pid = 0;
1433 if (from_tty)
1434 {
1435 printf ("Downloading \"%s\" to the ES 1800\n",filename);
1436 }
1437 if ((instream = fopen (loadname, "r")) == NULL)
1438 {
1439 perror_with_name ("fopen:");
1440 }
1441
1442 old_chain = make_cleanup (fclose, instream);
1443 immediate_quit++;
1444
1445 es1800_reset (0);
1446
1447 download (instream, from_tty, es1800_load_format);
1448
1449 /* if breakpoint routine is not present anymore we have to check
1450 whether to download a new breakpoint routine or not */
1451
1452 if ((verify_break (es1800_break_vec) != 0)
1453 && query ("No breakpoint routine in ES 1800 emulator!\nDownload a breakpoint routine to the emulator? "))
1454 {
1455 char buf[128];
1456 printf ("Using break vector 0x%x\n", es1800_break_vec);
1457 sprintf (buf, "0x%x ", es1800_break_vec);
1458 printf ("Give the start address of the breakpoint routine: ");
1459 fgets (buf + strlen (buf), sizeof (buf) - strlen (buf), stdin);
1460 es1800_init_break (buf, 0);
1461 }
1462
1463 do_cleanups (old_chain);
1464 expect_prompt ();
1465 readchar (); /* FIXME I am getting a ^G = 7 after the prompt */
1466 printf ("\n");
1467
1468 if (fclose (instream) == EOF)
1469 {
1470 ;
1471 }
1472
1473 if (es1800_load_format != 2)
1474 {
1475 sprintf (buf, "/usr/bin/rm %s", loadname);
1476 system (buf);
1477 }
1478
1479 symbol_file_command (filename, from_tty); /* reading symbol table */
1480 immediate_quit--;
1481 }
1482
1483 #if 0
1484
1485 #define NUMCPYBYTES 20
1486
1487 static void
1488 bfd_copy (from_bfd, to_bfd)
1489 bfd *from_bfd;
1490 bfd *to_bfd;
1491 {
1492 asection *p, *new;
1493 int i;
1494 char buf[NUMCPYBYTES];
1495
1496 for (p = from_bfd->sections; p != NULL; p = p->next)
1497 {
1498 printf (" Copying section %s. Size = %x.\n", p->name, p->_cooked_size);
1499 printf (" vma = %x, offset = %x, output_sec = %x\n",
1500 p->vma, p->output_offset, p->output_section);
1501 new = bfd_make_section (to_bfd, p->name);
1502 if (p->_cooked_size &&
1503 !bfd_set_section_size (to_bfd, new, p->_cooked_size))
1504 {
1505 error ("Wrong BFD size!\n");
1506 }
1507 if (!bfd_set_section_flags (to_bfd, new, p->flags))
1508 {
1509 error ("bfd_set_section_flags");
1510 }
1511 new->vma = p->vma;
1512
1513 for (i = 0; (i + NUMCPYBYTES) < p->_cooked_size ; i += NUMCPYBYTES)
1514 {
1515 if (!bfd_get_section_contents (from_bfd, p, (PTR) buf, (file_ptr) i,
1516 (bfd_size_type) NUMCPYBYTES))
1517 {
1518 error ("bfd_get_section_contents\n");
1519 }
1520 if (!bfd_set_section_contents (to_bfd, new, (PTR) buf, (file_ptr) i,
1521 (bfd_size_type) NUMCPYBYTES))
1522 {
1523 error ("bfd_set_section_contents\n");
1524 }
1525 }
1526 bfd_get_section_contents (from_bfd, p, (PTR) buf, (file_ptr) i,
1527 (bfd_size_type) (p->_cooked_size - i));
1528 bfd_set_section_contents (to_bfd, new, (PTR) buf,(file_ptr) i,
1529 (bfd_size_type) (p->_cooked_size - i));
1530 }
1531 }
1532
1533 #endif
1534
1535 /* Start an process on the es1800 and set inferior_pid to the new
1536 process' pid.
1537 execfile - the file to run
1538 args - arguments passed to the program
1539 env - the environment vector to pass */
1540
1541 static void
1542 es1800_create_inferior (execfile, args, env)
1543 char *execfile;
1544 char *args;
1545 char **env;
1546 {
1547 int entry_pt;
1548 int pid;
1549 #if 0
1550 struct expression *expr;
1551 register struct cleanup *old_chain = 0;
1552 register value val;
1553 #endif
1554
1555 if (args && *args)
1556 {
1557 error ("Can't pass arguments to remote ES1800 process");
1558 }
1559
1560 #if 0
1561 if (query ("Use 'start' as entry point? "))
1562 {
1563 expr = parse_c_expression ("start");
1564 old_chain = make_cleanup (free_current_contents, &expr);
1565 val = evaluate_expression (expr);
1566 entry_pt = (val->location).address;
1567 }
1568 else
1569 {
1570 printf ("Enter the program's entry point (in hexadecimal): ");
1571 scanf ("%x", &entry_pt);
1572 }
1573 #endif
1574
1575 if (execfile == 0 || exec_bfd == 0)
1576 {
1577 error ("No exec file specified");
1578 }
1579
1580 entry_pt = (int) bfd_get_start_address (exec_bfd);
1581
1582 pid = 42;
1583
1584 /* Now that we have a child process, make it our target. */
1585
1586 push_target (&es1800_child_ops);
1587
1588 /* The "process" (board) is already stopped awaiting our commands, and
1589 the program is already downloaded. We just set its PC and go. */
1590
1591 inferior_pid = pid; /* Needed for wait_for_inferior below */
1592
1593 clear_proceed_status ();
1594
1595 /* Tell wait_for_inferior that we've started a new process. */
1596
1597 init_wait_for_inferior ();
1598
1599 /* Set up the "saved terminal modes" of the inferior
1600 based on what modes we are starting it with. */
1601
1602 target_terminal_init ();
1603
1604 /* Install inferior's terminal modes. */
1605
1606 target_terminal_inferior ();
1607
1608 /* remote_start (args); */
1609 /* trap_expected = 0; */
1610 /* insert_step_breakpoint (); FIXME, do we need this? */
1611
1612 proceed ((CORE_ADDR) entry_pt, -1, 0); /* Let 'er rip... */
1613
1614 }
1615
1616
1617 /* The process has died, clean up. */
1618
1619 static void
1620 es1800_mourn_inferior ()
1621 {
1622 remove_breakpoints ();
1623 unpush_target (&es1800_child_ops);
1624 generic_mourn_inferior (); /* Do all the proper things now */
1625 }
1626
1627 /* ES1800-protocol specific routines */
1628
1629 /* Keep discarding input from the remote system, until STRING is found.
1630 Let the user break out immediately.
1631 string - the string to expect
1632 nowait - break out if string not the emulator's first respond otherwise
1633 read until string is found (== 0) */
1634
1635 static void
1636 expect (string, nowait)
1637 char *string;
1638 int nowait;
1639 {
1640 char c;
1641 char *p = string;
1642
1643 immediate_quit++;
1644 while (1)
1645 {
1646 c = readchar ();
1647 if (isalpha (c))
1648 {
1649 c = toupper (c);
1650 }
1651 if (c == toupper (*p))
1652 {
1653 p++;
1654 if (*p == '\0')
1655 {
1656 immediate_quit--;
1657 return;
1658 }
1659 }
1660 else if (!nowait)
1661 {
1662 p = string;
1663 }
1664 else
1665 {
1666 printf ("\'%s\' expected\n" , string);
1667 printf ("char %d is %d", p - string, c);
1668 error ("\n" );
1669 }
1670 }
1671 }
1672
1673 /* Keep discarding input until we see the prompt. */
1674
1675 static void
1676 expect_prompt ()
1677 {
1678 expect (">", 0);
1679 }
1680
1681
1682 /* Read one character */
1683
1684 #ifdef DEBUG_STDIN
1685
1686 /* read from stdin */
1687
1688 static int
1689 readchar ()
1690 {
1691 char buf[1];
1692
1693 buf[0] = '\0';
1694 printf ("readchar, give one character\n");
1695 read (0, buf, 1);
1696
1697 #if defined (LOG_FILE)
1698 putc (buf[0] & 0x7f, log_file);
1699 #endif
1700
1701 return (buf[0] & 0x7f);
1702 }
1703
1704 #else /* !DEBUG_STDIN */
1705
1706 /* Read a character from the remote system, doing all the fancy
1707 timeout stuff. */
1708
1709 static int
1710 readchar ()
1711 {
1712 char buf[1];
1713
1714 buf[0] = '\0';
1715
1716 #ifdef HAVE_TERMIO
1717
1718 /* termio does the timeout for us. */
1719 read (es1800_desc, buf, 1);
1720
1721 #else
1722
1723 alarm (timeout);
1724 while (read (es1800_desc, buf, 1) != 1)
1725 {
1726 if (errno == EINTR)
1727 {
1728 error ("Timeout reading from remote system.");
1729 }
1730 else if (errno != EWOULDBLOCK)
1731 {
1732 perror_with_name ("remote read");
1733 }
1734 }
1735 alarm (0);
1736 #endif
1737
1738 #if defined (LOG_FILE)
1739 putc (buf[0] & 0x7f, log_file);
1740 fflush (log_file);
1741 #endif
1742
1743 return (buf[0] & 0x7f);
1744 }
1745
1746 #endif /* DEBUG_STDIN */
1747
1748
1749 /* Send a command to the emulator and save the reply.
1750 Report an error if we get an error reply.
1751 string - the es1800 command
1752 buf - containing the emulator reply on return
1753 len - size of buf */
1754
1755 static void
1756 send_with_reply (string, buf, len)
1757 char *string, *buf;
1758 int len;
1759 {
1760 send (string);
1761 write (es1800_desc, "\r", 1);
1762
1763 #ifndef DEBUG_STDIN
1764 expect (string, 1);
1765 expect ("\r\n", 0);
1766 #endif
1767
1768 getmessage (buf, len);
1769 }
1770
1771
1772 /* Send the command in STR to the emulator adding \r. check
1773 the echo for consistency.
1774 string - the es1800 command */
1775
1776 static void
1777 send_command (string)
1778 char *string;
1779 {
1780 send (string);
1781 write (es1800_desc, "\r", 1);
1782
1783 #ifndef DEBUG_STDIN
1784 expect (string, 0);
1785 expect_prompt ();
1786 #endif
1787
1788 }
1789
1790 /* Send a string
1791 string - the es1800 command */
1792
1793 static void
1794 send (string)
1795 char *string;
1796 {
1797 if (kiodebug)
1798 {
1799 fprintf (stderr, "Sending: %s\n", string);
1800 }
1801 write (es1800_desc, string, strlen (string));
1802 }
1803
1804
1805 /* Read a message from the emulator and store it in BUF.
1806 buf - containing the emulator reply on return
1807 len - size of buf */
1808
1809 static void
1810 getmessage (buf, len)
1811 char *buf;
1812 int len;
1813 {
1814 char *bp;
1815 int c;
1816 int prompt_found = 0;
1817 extern kiodebug;
1818
1819 #if defined (LOG_FILE)
1820 /* This is a convenient place to do this. The idea is to do it often
1821 enough that we never lose much data if we terminate abnormally. */
1822 fflush (log_file);
1823 #endif
1824
1825 bp = buf;
1826 c = readchar ();
1827 do
1828 {
1829 if (c)
1830 {
1831 if (len-- < 2) /* char and terminaling NULL */
1832 {
1833 error ("input buffer overrun\n");
1834 }
1835 *bp++ = c;
1836 }
1837 c = readchar ();
1838 if ((c == '>') && (*(bp - 1) == ' '))
1839 {
1840 prompt_found = 1;
1841 }
1842 }
1843 while (!prompt_found);
1844 *bp = 0;
1845
1846 if (kiodebug)
1847 {
1848 fprintf (stderr,"message received :%s\n", buf);
1849 }
1850 }
1851
1852 static void
1853 download (instream, from_tty, format)
1854 FILE *instream;
1855 int from_tty;
1856 int format;
1857 {
1858 char c;
1859 char buf[160];
1860 int i = 0;
1861
1862 send_command ("SET #2,$1A"); /* reset char = ^Z */
1863 send_command ("SET #3,$11,$13"); /* XON XOFF */
1864 if (format == 2)
1865 {
1866 send_command ("SET #26,#2");
1867 }
1868 else
1869 {
1870 send_command ("SET #26,#5"); /* Format=Extended Tekhex */
1871 }
1872 send_command ("DFB = $10");
1873 send_command ("PUR");
1874 send_command ("CES");
1875 send ("DNL\r");
1876 expect ("DNL", 1);
1877 if (from_tty)
1878 {
1879 printf (" 0 records loaded...\r");
1880 }
1881 while (fgets (buf, 160, instream))
1882 {
1883 send (buf);
1884 if (from_tty)
1885 {
1886 printf ("%5d\b\b\b\b\b",++i);
1887 fflush (stdout);
1888 }
1889 if ((c = readchar ()) != 006)
1890 {
1891 error ("expected ACK");
1892 }
1893 }
1894 if (from_tty)
1895 {
1896 printf ("- All");
1897 }
1898 }
1899
1900 /* Additional commands */
1901
1902 /* Talk directly to the emulator
1903 FIXME, uses busy wait, and is SUNOS (or at least BSD) specific */
1904
1905 /*ARGSUSED*/
1906 static void
1907 es1800_transparent (args, from_tty)
1908 char *args;
1909 int from_tty;
1910 {
1911 int console;
1912 struct sgttyb modebl;
1913 int fcflag;
1914 int cc;
1915 struct sgttyb console_mode_save;
1916 int console_fc_save;
1917 int es1800_fc_save;
1918 int inputcnt = 80;
1919 char inputbuf[80];
1920 int consolecnt = 0;
1921 char consolebuf[80];
1922 int es1800_cnt = 0;
1923 char es1800_buf[80];
1924 int i;
1925
1926 dont_repeat ();
1927 if (es1800_desc < 0)
1928 {
1929 printf ("No emulator attached, type emulator-command first\n");
1930 return;
1931 }
1932
1933 printf ("\n");
1934 printf ("You are now communicating directly with the ES 1800 emulator.\n");
1935 printf ("To leave this mode (transparent mode), press ^E.\n");
1936 printf ("\n");
1937 printf (" >");
1938 fflush (stdout);
1939
1940 if ((console = open ("/dev/tty", O_RDWR)) == -1)
1941 {
1942 perror_with_name ("/dev/tty:");
1943 }
1944
1945 if ((fcflag = fcntl (console, F_GETFL, 0)) == -1)
1946 {
1947 perror_with_name ("fcntl console");
1948 }
1949
1950 console_fc_save = fcflag;
1951 fcflag = fcflag | FNDELAY;
1952
1953 if (fcntl (console, F_SETFL, fcflag) == -1)
1954 {
1955 perror_with_name ("fcntl console");
1956 }
1957
1958 if (ioctl (console, TIOCGETP, &modebl))
1959 {
1960 perror_with_name ("ioctl console");
1961 }
1962
1963 console_mode_save = modebl;
1964 modebl.sg_flags = RAW;
1965
1966 if (ioctl (console, TIOCSETP, &modebl))
1967 {
1968 perror_with_name ("ioctl console");
1969 }
1970
1971 if ((fcflag = fcntl (es1800_desc, F_GETFL, 0)) == -1)
1972 {
1973 perror_with_name ("fcntl serial");
1974 }
1975
1976 es1800_fc_save = fcflag;
1977 fcflag = fcflag | FNDELAY;
1978
1979 if (fcntl (es1800_desc, F_SETFL, fcflag) == -1)
1980 {
1981 perror_with_name ("fcntl serial");
1982 }
1983
1984 while (1)
1985 {
1986 cc = read (console, inputbuf, inputcnt);
1987 if (cc != -1)
1988 {
1989 if ((*inputbuf & 0x7f) == 0x05)
1990 {
1991 break;
1992 }
1993 for (i = 0; i < cc; )
1994 {
1995 es1800_buf[es1800_cnt++] = inputbuf[i++];
1996 }
1997 if ((cc = write (es1800_desc, es1800_buf, es1800_cnt)) == -1)
1998 {
1999 perror_with_name ("FEL! read:");
2000 }
2001 es1800_cnt -= cc;
2002 if (es1800_cnt && cc)
2003 {
2004 for (i = 0; i < es1800_cnt; i++)
2005 {
2006 es1800_buf[i] = es1800_buf[cc+i];
2007 }
2008 }
2009 }
2010 else if (errno != EWOULDBLOCK)
2011 {
2012 perror_with_name ("FEL! read:");
2013 }
2014
2015 cc = read (es1800_desc,inputbuf,inputcnt);
2016 if (cc != -1)
2017 {
2018 for (i = 0; i < cc; )
2019 {
2020 consolebuf[consolecnt++] = inputbuf[i++];
2021 }
2022 if ((cc = write (console,consolebuf,consolecnt)) == -1)
2023 {
2024 perror_with_name ("FEL! write:");
2025 }
2026 consolecnt -= cc;
2027 if (consolecnt && cc)
2028 {
2029 for (i = 0; i < consolecnt; i++)
2030 {
2031 consolebuf[i] = consolebuf[cc+i];
2032 }
2033 }
2034 }
2035 else if (errno != EWOULDBLOCK)
2036 {
2037 perror_with_name ("FEL! read:");
2038 }
2039 }
2040
2041 console_fc_save = console_fc_save & !FNDELAY;
2042 if (fcntl (console, F_SETFL, console_fc_save) == -1)
2043 {
2044 perror_with_name ("FEL! fcntl");
2045 }
2046
2047 if (ioctl (console, TIOCSETP, &console_mode_save))
2048 {
2049 perror_with_name ("FEL! ioctl");
2050 }
2051
2052 close (console);
2053
2054 if (fcntl (es1800_desc, F_SETFL, es1800_fc_save) == -1)
2055 {
2056 perror_with_name ("FEL! fcntl");
2057 }
2058
2059 printf ("\n");
2060
2061 }
2062
2063 static void
2064 es1800_init_break (args, from_tty)
2065 char *args;
2066 int from_tty;
2067 {
2068 CORE_ADDR memaddress = 0;
2069 char buf[PBUFSIZ];
2070 char base_addr[4];
2071 char *space_index;
2072 char *p;
2073 int k;
2074
2075 if (args == NULL)
2076 {
2077 error_no_arg ("a trap vector");
2078 }
2079
2080 if (!(space_index = strchr (args, ' ')))
2081 {
2082 error ("Two arguments needed (trap vector and address of break routine).\n");
2083 }
2084
2085 *space_index = '\0';
2086
2087 es1800_break_vec = strtol (args, (char **) NULL, 0);
2088 es1800_break_address = parse_and_eval_address (space_index + 1);
2089
2090 es1800_create_break_insn (es1800_break_insn, es1800_break_vec);
2091
2092 if (m68020)
2093 {
2094 send_with_reply ("VBR ", buf, sizeof (buf));
2095 p = buf;
2096 for (k = 0; k < 4; k++)
2097 {
2098 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
2099 {
2100 error ("Emulator reply is too short: %s", buf);
2101 }
2102 base_addr[k] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
2103 }
2104 /* base addr of exception vector table */
2105 memaddress = *((CORE_ADDR *) base_addr);
2106 }
2107
2108 memaddress += (es1800_break_vec + 32) * 4; /* address of trap vector */
2109
2110 sprintf (buf, "@.L%lx=$%lx", memaddress, es1800_break_address);
2111 send_command (buf); /* set the address of the break routine in the */
2112 /* trap vector */
2113
2114 sprintf (buf, "@.L%lx=$4E714E71", es1800_break_address); /* NOP; NOP */
2115 send_command (buf);
2116 sprintf (buf, "@.L%lx=$4E714E73", es1800_break_address + 4); /* NOP; RTE */
2117 send_command (buf);
2118
2119 sprintf (buf, "AC2=$%lx", es1800_break_address + 4);
2120 /* breakpoint at es1800-break_address */
2121 send_command (buf);
2122 send_command ("WHEN AC2 THEN BRK"); /* ie in exception routine */
2123
2124 if (from_tty)
2125 {
2126 printf ("Breakpoint (trap $%x) routine at address: %lx\n",
2127 es1800_break_vec, es1800_break_address);
2128 }
2129 }
2130
2131 static void
2132 es1800_child_open (arg, from_tty)
2133 char *arg;
2134 int from_tty;
2135 {
2136 error ("Use the \"run\" command to start a child process.");
2137 }
2138
2139 static void
2140 es1800_child_detach (args, from_tty)
2141 char *args;
2142 int from_tty;
2143 {
2144 if (args)
2145 {
2146 error ("Argument given to \"detach\" when remotely debugging.");
2147 }
2148
2149 pop_target ();
2150 if (from_tty)
2151 {
2152 printf ("Ending debugging the process %d.\n", inferior_pid);
2153 }
2154 }
2155
2156
2157 /* Define the target subroutine names */
2158
2159 static struct target_ops es1800_ops =
2160 {
2161 "es1800", /* to_shortname */
2162 /* to_longname */
2163 "Remote serial target in ES1800-emulator protocol",
2164 /* to_doc */
2165 "Remote debugging on the es1800 emulator via a serial line.\n\
2166 Specify the serial device it is connected to (e.g. /dev/ttya).",
2167 es1800_open, /* to_open */
2168 es1800_close, /* to_close */
2169 es1800_attach, /* to_attach */
2170 es1800_detach, /* to_detach */
2171 es1800_resume, /* to_resume */
2172 NULL, /* to_wait */
2173 NULL, /* to_fetch_registers */
2174 NULL, /* to_store_registers */
2175 es1800_prepare_to_store, /* to_prepare_to_store */
2176 es1800_xfer_inferior_memory, /* to_xfer_memory */
2177 es1800_files_info, /* to_files_info */
2178 es1800_insert_breakpoint, /* to_insert_breakpoint */
2179 es1800_remove_breakpoint, /* to_remove_breakpoint */
2180 NULL, /* to_terminal_init */
2181 NULL, /* to_terminal_inferior */
2182 NULL, /* to_terminal_ours_for_output */
2183 NULL, /* to_terminal_ours */
2184 NULL, /* to_terminal_info */
2185 NULL, /* to_kill */
2186 es1800_load, /* to_load */
2187 NULL, /* to_lookup_symbol */
2188 es1800_create_inferior, /* to_create_inferior */
2189 NULL, /* to_mourn_inferior */
2190 0, /* to_can_run */
2191 0, /* notice_signals */
2192 core_stratum, /* to_stratum */
2193 0, /* to_next */
2194 0, /* to_has_all_memory */
2195 1, /* to_has_memory */
2196 0, /* to_has_stack */
2197 0, /* to_has_registers */
2198 0, /* to_has_execution */
2199 NULL, /* to_sections */
2200 NULL, /* to_sections_end */
2201 OPS_MAGIC /* to_magic (always last) */
2202 };
2203
2204 /* Define the target subroutine names */
2205
2206 static struct target_ops es1800_child_ops =
2207 {
2208 "es1800_process", /* to_shortname */
2209 /* to_longname */
2210 "Remote serial target in ES1800-emulator protocol",
2211 /* to_doc */
2212 "Remote debugging on the es1800 emulator via a serial line.\n\
2213 Specify the serial device it is connected to (e.g. /dev/ttya).",
2214 es1800_child_open, /* to_open */
2215 NULL, /* to_close */
2216 es1800_attach, /* to_attach */
2217 es1800_child_detach, /* to_detach */
2218 es1800_resume, /* to_resume */
2219 es1800_wait, /* to_wait */
2220 es1800_fetch_register, /* to_fetch_registers */
2221 es1800_store_register, /* to_store_registers */
2222 es1800_prepare_to_store, /* to_prepare_to_store */
2223 es1800_xfer_inferior_memory, /* to_xfer_memory */
2224 es1800_files_info, /* to_files_info */
2225 es1800_insert_breakpoint, /* to_insert_breakpoint */
2226 es1800_remove_breakpoint, /* to_remove_breakpoint */
2227 NULL, /* to_terminal_init */
2228 NULL, /* to_terminal_inferior */
2229 NULL, /* to_terminal_ours_for_output */
2230 NULL, /* to_terminal_ours */
2231 NULL, /* to_terminal_info */
2232 es1800_kill, /* to_kill */
2233 es1800_load, /* to_load */
2234 NULL, /* to_lookup_symbol */
2235 es1800_create_inferior, /* to_create_inferior */
2236 es1800_mourn_inferior, /* to_mourn_inferior */
2237 0, /* to_can_run */
2238 0, /* notice_signals */
2239 process_stratum, /* to_stratum */
2240 0, /* to_next */
2241 1, /* to_has_all_memory */
2242 1, /* to_has_memory */
2243 1, /* to_has_stack */
2244 1, /* to_has_registers */
2245 1, /* to_has_execution */
2246 NULL, /* to_sections */
2247 NULL, /* to_sections_end */
2248 OPS_MAGIC /* to_magic (always last) */
2249 };
2250
2251 /* _initialize_es1800 () */
2252
2253 void
2254 _initialize_es1800 ()
2255 {
2256 add_target (&es1800_ops);
2257 add_target (&es1800_child_ops);
2258 add_com ("transparent", class_support, es1800_transparent,
2259 "Start transparent communication with the ES 1800 emulator.");
2260 add_com ("init_break", class_support, es1800_init_break,
2261 "Download break routine and initialize break facility on ES 1800");
2262 }
This page took 0.076412 seconds and 5 git commands to generate.