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