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