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