1 /* Memory-access and commands for remote es1800 processes, for GDB.
2 Copyright (C) 1988, 1992 Free Software Foundation, Inc.
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
10 This file is part of GDB.
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)
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.
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. */
27 /* Emulator communication protocol.
28 All values are encoded in ascii hex digits.
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
40 Each byte of register data is described by two hex digits.
62 Each byte of register data is described by two hex digits.
68 AA..AA is address, XXXXXXX is the contents
73 AA..AA is address, XXXXXXXX is data
79 AA..AA is address to resume. If AA..AA is omitted, resume at same address.
85 AA..AA is address to resume. If AA..AA is omitted, resume at same address.
94 /* Comment out entire file until it can be fixed (need to clean up
99 #include <sys/ioctl.h>
100 #include <sys/file.h>
108 #include "inferior.h"
111 #include "terminal.h"
113 #include "remote-utils.h"
116 #include <sys/types.h>
122 /* External variables referenced. */
124 extern bfd *exec_bfd;
126 /* Prototypes for local functions */
129 es1800_child_detach PARAMS ((char *, int));
132 es1800_child_open PARAMS ((char *, int));
135 es1800_transparent PARAMS ((char *, int));
138 es1800_create_inferior PARAMS ((char *, char *, char **));
141 es1800_load PARAMS ((char *, int));
144 es1800_kill PARAMS ((void));
147 verify_break PARAMS ((int));
150 es1800_remove_breakpoint PARAMS ((CORE_ADDR, char *));
153 es1800_insert_breakpoint PARAMS ((CORE_ADDR, char *));
156 es1800_files_info PARAMS ((struct target_ops *));
159 es1800_xfer_inferior_memory PARAMS ((CORE_ADDR, char *, int, int,
160 struct target_ops *));
163 es1800_prepare_to_store PARAMS ((void));
166 es1800_wait PARAMS ((WAITTYPE *));
168 static void es1800_resume PARAMS ((int, int, enum target_signal));
171 es1800_detach PARAMS ((char *, int));
174 es1800_attach PARAMS ((char *, int));
177 damn_b PARAMS ((char *));
180 es1800_open PARAMS ((char *, int));
183 es1800_timer PARAMS ((void));
186 es1800_reset PARAMS ((char *));
189 es1800_request_quit PARAMS ((void));
192 readchar PARAMS ((void));
195 expect PARAMS ((char *, int));
198 expect_prompt PARAMS ((void));
201 download PARAMS ((FILE *, int, int));
205 bfd_copy PARAMS ((bfd *, bfd *));
209 get_break_addr PARAMS ((int, CORE_ADDR *));
212 fromhex PARAMS ((int));
215 tohex PARAMS ((int));
218 es1800_close PARAMS ((int));
221 es1800_fetch_registers PARAMS ((void));
224 es1800_fetch_register PARAMS ((int));
227 es1800_store_register PARAMS ((int));
230 es1800_read_bytes PARAMS ((CORE_ADDR, char *, int));
233 es1800_write_bytes PARAMS ((CORE_ADDR, char *, int));
236 send_with_reply PARAMS ((char *, char *, int));
239 send_command PARAMS ((char *));
242 send PARAMS ((char *));
245 getmessage PARAMS ((char *, int));
248 es1800_mourn_inferior PARAMS ((void));
251 es1800_create_break_insn PARAMS ((char *, int));
254 es1800_init_break PARAMS ((char *, int));
256 /* Local variables */
258 #define LOG_FILE "es1800.log"
259 #if defined (LOG_FILE)
260 static FILE *log_file;
263 extern struct target_ops es1800_ops; /* Forward decl */
264 extern struct target_ops es1800_child_ops; /* Forward decl */
267 static int timeout = 100;
268 static char *savename; /* Name of i/o device used */
269 static TERMINAL es1800_sg_save; /* Save stty state */
270 static int es1800_fc_save; /* Save fcntl state */
272 /* indicates that the emulator uses 32-bit data-adress (68020-mode)
273 instead of 24-bit (68000 -mode) */
277 #define MODE (m68020 ? "M68020" : "M68000" )
278 #define ES1800_BREAK_VEC (0xf)
280 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
281 es1800_open knows that we don't have a file open when the program
284 static int es1800_desc = -1;
287 #define HDRLEN sizeof("@.BAAAAAAAA=$VV\r")
289 /* Maximum number of bytes to read/write at once. The value here
290 is chosen to fill up a packet. */
292 #define MAXBUFBYTES ((PBUFSIZ-150)*16/75 )
294 static int es1800_break_vec = 0;
295 static char es1800_break_insn[2];
296 static long es1800_break_address;
297 static void (*old_sigint)(); /* Old signal-handler for sigint */
298 static jmp_buf interrupt;
300 /* Local signalhandler to allow breaking tranfers or program run.
301 Rely on global variables: old_sigint(), interrupt */
304 es1800_request_quit ()
306 /* restore original signalhandler */
307 signal (SIGINT, old_sigint);
308 longjmp (interrupt, 1);
313 Sending reset character(octal 32) to emulator.
314 quit - return to '(esgdb)' prompt or continue */
324 printf ("\nResetting emulator... ");
326 strcpy (buf, "\032");
336 /* Called when SIGALRM signal sent due to alarm() timeout.
337 Rely on global variables: timeout */
346 printf ("es1800_timer called\n");
351 #endif /* HAVE_TERMIO */
354 /* Open a connection to a remote debugger and push the new target
355 onto the stack. Check if the emulator is responding and find out
356 what kind of processor the emulator is connected to.
357 Initiate the breakpoint handling in the emulator.
359 name - the filename used for communication (ex. '/dev/tta')
360 from_tty - says whether to be verbose or not */
363 es1800_open (name, from_tty)
375 if (!name) /* no device name given in target command */
377 error_no_arg ("serial port device name");
379 sprintf(baudrate, "%d", sr_get_baud_rate());
381 target_preopen (from_tty);
384 /* open the device and configure it for communication */
388 es1800_desc = open (name, O_RDWR);
391 perror_with_name (name);
393 savename = savestring (name, strlen (name));
395 if (ioctl (es1800_desc, TIOCGETP, &sg) == -1)
397 perror_with_name (name);
401 if ((fcflag = fcntl (es1800_desc, F_GETFL, 0)) == -1)
403 perror_with_name ("fcntl serial");
405 es1800_fc_save = fcflag;
407 fcflag = (fcflag & (FREAD | FWRITE)); /* mask out any funny stuff */
408 if (fcntl (es1800_desc, F_SETFL, fcflag) == -1)
410 perror_with_name ("fcntl serial");
414 sg.c_cc[VMIN] = 0; /* read with timeout. */
415 sg.c_cc[VTIME] = timeout * 10;
416 sg.c_lflag &= ~(ICANON | ECHO);
417 sg.c_cflag = (sg.c_cflag & ~CBAUD) | damn_b (baudrate);
419 sg.sg_ispeed = damn_b (baudrate);
420 sg.sg_ospeed = damn_b (baudrate);
421 sg.sg_flags = CBREAK+TANDEM;
424 if ((ioctl (es1800_desc, TIOCSETP, &sg)) == -1)
426 perror ("es1800_open: error in ioctl");
429 #endif /* DEBUG_STDIN */
431 push_target (&es1800_ops); /* Switch to using remote target now */
434 printf ("Remote ES1800 debugging using %s\n", name);
439 #ifndef NO_SIGINTERRUPT
441 /* Cause SIGALRM's to make reads fail with EINTR instead of resuming
444 if (siginterrupt (SIGALRM, 1) != 0)
446 perror ("es1800_open: error in siginterrupt");
449 #endif /* NO_SIGINTERRUPT */
451 /* Set up read timeout timer. */
453 if ((void(*)()) signal (SIGALRM, es1800_timer) == (void(*)()) -1)
455 perror ("es1800_open: error in signal");
458 #endif /* HAVE_TERMIO */
461 #if defined (LOG_FILE)
463 log_file = fopen (LOG_FILE, "w");
464 if (log_file == NULL)
466 perror_with_name (LOG_FILE);
469 #endif /* LOG_FILE */
471 /* Hello? Are you there?, also check mode */
473 /* send_with_reply( "DB 0 TO 1", buf, sizeof(buf)); */
474 /* for (p = buf, i = 0; *p++ =='0';) */ /* count the number of zeros */
478 getmessage (buf, sizeof (buf)); /* send reset character */
482 printf ("Checking mode.... ");
484 /* m68020 = (i==8); */ /* if eight zeros then we are in m68020 mode */
486 /* What kind of processor am i talking to ?*/
488 while (*p++ != '\n') {;}
489 while (*p++ != '\n') {;}
490 while (*p++ != '\n') {;}
491 for (i = 0; i < 20; i++, p++) {;}
492 m68020 = !strncmp (p, "68020", 5);
495 printf ("You are in %s(%c%c%c%c%c)-mode\n", MODE, p[0], p[1], p[2],
499 /* if no init_break statement is present in .gdb file we have to check
500 whether to download a breakpoint routine or not */
503 if ((es1800_break_vec == 0) || (verify_break (es1800_break_vec) != 0)
504 && query ("No breakpoint routine in ES 1800 emulator!\nDownload a breakpoint routine to the emulator? "))
506 CORE_ADDR memaddress;
507 printf ("Give the start address of the breakpoint routine: ");
508 scanf ("%li", &memaddress);
509 es1800_init_break ((es1800_break_vec ? es1800_break_vec :
510 ES1800_BREAK_VEC), memaddress);
516 /* Close out all files and local state before this target loses control.
517 quitting - are we quitting gdb now? */
520 es1800_close (quitting)
523 if (es1800_desc >= 0)
525 printf ("\nClosing connection to emulator...\n");
526 ioctl (es1800_desc, TIOCSETP, &es1800_sg_save);
527 fcntl (es1800_desc,F_SETFL, es1800_fc_save);
531 if (savename != NULL)
537 #if defined (LOG_FILE)
539 if (log_file != NULL)
541 if (ferror (log_file))
543 printf ("Error writing log file.\n");
545 if (fclose (log_file) != 0)
547 printf ("Error closing log file.\n");
552 #endif /* LOG_FILE */
558 Translate baud rates from integers to damn B_codes. Unix should
559 have outgrown this crap years ago, but even POSIX wouldn't buck it.
560 rate - the baudrate given as a string
561 return value: the baudrate as a B_code */
600 for (i = 0; baudtab[i].rate != 0; i++)
602 if (STREQ (rate, baudtab[i].rate))
604 return (baudtab[i].damn_b);
607 error ("Illegal baudrate");
611 /* Attaches to a process on the target side
612 proc_id - the id of the process to be attached.
613 from_tty - says whether to be verbose or not */
616 es1800_attach (args, from_tty)
620 error ("Cannot attach to pid %s, this feature is not implemented yet.",
625 /* Takes a program previously attached to and detaches it.
626 We better not have left any breakpoints
627 in the program or it'll die when it hits one.
628 Close the open connection to the remote debugger.
629 Use this when you want to detach and do something else
632 args - arguments given to the 'detach' command
633 from_tty - says whether to be verbose or not */
636 es1800_detach (args, from_tty)
642 error ("Argument given to \"detach\" when remotely debugging.");
647 printf ("Ending es1800 remote debugging.\n");
652 /* Tell the remote machine to resume.
653 step - single-step or run free
654 siggnal - the signal value to be given to the target (0 = no signal) */
657 es1800_resume (pid, step, siggnal)
660 enum target_signal siggnal;
666 error ("Can't send signals to a remote system.");
670 strcpy (buf,"STP\r");
675 send_command ("RBK");
679 /* Wait until the remote machine stops, then return,
680 storing status in STATUS just as `wait' would.
685 struct target_waitstatus *status;
687 unsigned char buf[PBUFSIZ];
688 int old_timeout = timeout;
690 status->kind = TARGET_WAITKIND_EXITED;
691 status->value.integer = 0;
693 timeout = 0; /* Don't time out -- user program is running. */
694 if (!setjmp (interrupt))
696 old_sigint = signal (SIGINT, es1800_request_quit);
699 getmessage (buf, sizeof(buf));
700 if (strncmp ( buf, "\r\n* BREAK *", 11) == 0)
702 status->kind = TARGET_WAITKIND_STOPPED;
703 status->value.sig = TARGET_SIGNAL_TRAP;
704 send_command ("STP"); /* Restore stack and PC and such */
707 send_command ("STP");
711 if (strncmp (buf, "STP\r\n ", 6) == 0)
713 status->kind = TARGET_WAITKIND_STOPPED;
714 status->value.sig = TARGET_SIGNAL_TRAP;
717 if (buf[strlen (buf) - 2] == 'R')
719 printf ("Unexpected emulator reply: \n%s\n", buf);
723 printf ("Unexpected stop: \n%s\n", buf);
724 status->kind = TARGET_WAITKIND_STOPPED;
725 status->value.sig = TARGET_SIGNAL_QUIT;
733 printf ("\nStopping emulator...");
734 if (!setjmp (interrupt))
736 old_sigint = signal (SIGINT, es1800_request_quit);
737 send_command ("STP");
738 printf (" emulator stopped\n");
739 status->kind = TARGET_WAITKIND_STOPPED;
740 status->value.sig = TARGET_SIGNAL_INT;
745 es1800_reset ((char*) 1);
748 signal (SIGINT, old_sigint);
749 timeout = old_timeout;
754 /* Fetch register values from remote machine.
755 regno - the register to be fetched (fetch all registers if -1) */
758 es1800_fetch_register (regno)
765 static char regtab[18][4] =
767 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
768 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
772 if ((regno < 15) || (regno == 16) || (regno == 17))
775 send_with_reply (regtab[regno], buf, sizeof (buf));
777 for (k = 0; k < 4; k++)
779 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
781 error ("Emulator reply is too short: %s", buf);
783 registers[r++] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
788 es1800_fetch_registers ();
792 /* Read the remote registers into REGISTERS.
793 Always fetches all registers. */
796 es1800_fetch_registers ()
799 char SR_buf[PBUFSIZ];
805 send_with_reply ("DR", buf, sizeof (buf));
807 /* Reply is edited to a string that describes registers byte by byte,
808 each byte encoded as two hex characters. */
813 /* parsing row one - D0-D7-registers */
815 while (*p++ != '\n') {;}
816 for (i = 4; i < 70; i += (i == 39 ? 3 : 1))
818 for (k = 0; k < 4; k++)
820 if (p[i+0] == 0 || p[i+1] == 0)
822 error ("Emulator reply is too short: %s", buf);
824 registers[r++] = (fromhex (p[i+0]) * 16) + fromhex (p[i+1]);
830 /* parsing row two - A0-A6-registers */
832 while (*p++ != '\n') {;}
833 for (i = 4; i < 61; i += (i == 39 ? 3 : 1))
835 for (k = 0; k < 4; k++)
837 if (p[i+0] == 0 || p[i+1] == 0)
839 error ("Emulator reply is too short: %s", buf);
841 registers[r++] = (fromhex (p[i+0])) * 16 + fromhex (p[i+1]);
847 while (*p++ != '\n') {;}
849 /* fetch SSP-, SR- and PC-registers */
851 /* first - check STATUS-word and decide which stackpointer to use */
853 send_with_reply ("SR", SR_buf, sizeof (SR_buf));
859 if (*p == '3') /* use masterstackpointer MSP */
861 send_with_reply ("MSP", buf, sizeof (buf));
863 else if (*p == '2') /* use interruptstackpointer ISP */
865 send_with_reply ("ISP", buf, sizeof (buf));
867 else /* use userstackpointer USP */
869 send_with_reply ("USP", buf, sizeof (buf));
872 for (k = 0; k<4; k++)
874 if (p[k*2+1] == 0 || p[k*2+2] == 0)
876 error ("Emulator reply is too short: %s", buf);
878 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
882 for (k = 0; k < 4; k++)
884 if (p[k*2+1] == 0 || p[k*2+2] == 0)
886 error ("Emulator reply is too short: %s", buf);
889 fromhex (SR_buf[k*2+1]) * 16 + fromhex (SR_buf[k*2+2]);
891 send_with_reply ("PC", buf, sizeof (buf));
893 for (k = 0; k<4; k++)
895 if (p[k*2+1] == 0 || p[k*2+2] == 0)
897 error ("Emulator reply is too short: %s", buf);
899 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
902 else /* 68000-mode */
904 if (*p == '2') /* use supervisorstackpointer SSP */
906 send_with_reply ("SSP", buf, sizeof (buf));
908 else /* use userstackpointer USP */
910 send_with_reply ("USP", buf, sizeof (buf));
913 /* fetch STACKPOINTER */
916 for (k = 0; k < 4; k++)
918 if (p[k*2 + 1] == 0 || p[k*2 + 2] == 0)
920 error ("Emulator reply is too short: %s", buf);
922 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
928 for (k = 0; k < 4; k++)
930 if (p[k*2+1] == 0 || p[k*2+2] == 0)
932 error ("Emulator reply is too short: %s", buf);
935 fromhex (SR_buf[k*2+1]) * 16 + fromhex (SR_buf[k*2+2]);
940 send_with_reply ("PC", buf, sizeof (buf));
942 for (k = 0; k < 4; k++)
944 if (p[k*2+1] == 0 || p[k*2+2] == 0)
946 error ("Emulator reply is too short: %s", buf);
948 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
953 /* Store register value, located in REGISTER, on the target processor.
954 regno - the register-number of the register to store
955 (-1 means store them all)
956 FIXME: Return errno value. */
959 es1800_store_register(regno)
963 static char regtab[18][4] =
965 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
966 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
971 char SR_buf[PBUFSIZ];
972 char stack_pointer[4];
979 r = (unsigned char *) registers;
981 if (regno == -1) /* write all registers */
986 else /* write one register */
993 if ((regno == -1) || (regno == 15))
995 /* fetch current status */
996 send_with_reply ("SR", SR_buf, sizeof (SR_buf));
1001 if (*p == '3') /* use masterstackpointer MSP */
1003 strcpy (stack_pointer,"MSP");
1007 if (*p == '2') /* use interruptstackpointer ISP */
1009 strcpy (stack_pointer,"ISP");
1013 strcpy (stack_pointer,"USP"); /* use userstackpointer USP */
1017 else /* 68000-mode */
1019 if (*p == '2') /* use supervisorstackpointer SSP */
1021 strcpy (stack_pointer,"SSP");
1025 strcpy (stack_pointer,"USP");/* use userstackpointer USP */
1028 strcpy (regtab[15],stack_pointer);
1031 for (i = j; i<k; i++)
1033 buf[0] = regtab[i][0];
1034 buf[1] = regtab[i][1];
1035 buf[2] = regtab[i][2];
1038 buf[5] = tohex ((*r >> 4) & 0x0f);
1039 buf[6] = tohex (*r++ & 0x0f);
1040 buf[7] = tohex ((*r >> 4) & 0x0f);
1041 buf[8] = tohex (*r++ & 0x0f);
1042 buf[9] = tohex ((*r >> 4) & 0x0f);
1043 buf[10] = tohex (*r++ & 0x0f);
1044 buf[11] = tohex ((*r >> 4) & 0x0f);
1045 buf[12] = tohex (*r++ & 0x0f);
1048 send_with_reply (buf, buf, sizeof (buf)); /* FIXME, reply not used? */
1053 /* Prepare to store registers. */
1056 es1800_prepare_to_store ()
1058 /* Do nothing, since we can store individual regs */
1061 /* Convert hex digit A to a number. */
1067 if (a >= '0' && a <= '9')
1071 else if (a >= 'a' && a <= 'f')
1073 return a - 'a' + 10;
1075 else if (a >= 'A' && a <= 'F')
1077 return a - 'A' + 10;
1081 error ("Reply contains invalid hex digit");
1087 /* Convert number NIB to a hex digit. */
1099 return ('A' + nib - 10);
1103 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1104 to or from debugger address MYADDR. Write to inferior if WRITE is
1105 nonzero. Returns length of data written or read; 0 for error.
1107 memaddr - the target's address
1108 myaddr - gdb's address
1109 len - number of bytes
1110 write - write if != 0 otherwise read */
1113 es1800_xfer_inferior_memory (memaddr, myaddr, len, write, tops)
1118 struct target_ops *tops; /* Unused */
1125 xfersize = len > MAXBUFBYTES ? MAXBUFBYTES : len;
1128 es1800_write_bytes (memaddr, myaddr, xfersize);
1132 es1800_read_bytes (memaddr, myaddr, xfersize);
1134 memaddr += xfersize;
1138 return (origlen); /* no error possible */
1142 /* Write memory data directly to the emulator.
1143 This does not inform the data cache; the data cache uses this.
1144 MEMADDR is the address in the remote memory space.
1145 MYADDR is the address of the buffer in our space.
1146 LEN is the number of bytes.
1148 memaddr - the target's address
1149 myaddr - gdb's address
1150 len - number of bytes */
1153 es1800_write_bytes (memaddr, myaddr, len)
1163 for (i = 0; i < len; i++)
1165 sprintf (buf, "@.B$%x=$%x", memaddr+i, (*p++) & 0xff);
1166 send_with_reply (buf, buf, sizeof (buf)); /* FIXME send_command? */
1171 /* Read memory data directly from the emulator.
1172 This does not use the data cache; the data cache uses this.
1174 memaddr - the target's address
1175 myaddr - gdb's address
1176 len - number of bytes */
1179 es1800_read_bytes (memaddr, myaddr, len)
1184 static int DB_tab[16] = {8,11,14,17,20,23,26,29,34,37,40,43,46,49,52,55};
1191 if (len > PBUFSIZ / 2 - 1)
1196 if (len == 1) /* The emulator does not like expressions like: */
1198 len = 2; /* DB.B $20018 TO $20018 */
1201 /* Reply describes registers byte by byte, each byte encoded as two hex
1204 sprintf (buf, "DB.B $%x TO $%x", memaddr, memaddr+len-1);
1205 send_with_reply (buf, buf, sizeof (buf));
1207 low_addr = memaddr&0x0f;
1208 for (i = low_addr; i < low_addr + len; i++)
1210 if ((!(i % 16)) && i)
1211 { /* if (i = 16,32,48) */
1212 while (*p++!='\n') {;}
1215 p = b + DB_tab[i%16] + (m68020 ? 2 : 0);
1216 if (p[0] == 32 || p[1] == 32)
1218 error ("Emulator reply is too short: %s", buf);
1220 myaddr[i-low_addr] = fromhex (p[0]) * 16 + fromhex (p[1]);
1224 /* Information about the current target */
1227 es1800_files_info (tops)
1228 struct target_ops *tops; /* Unused */
1230 printf ("ES1800 Attached to %s at %d baud in %s mode\n", savename, 19200,
1235 /* We read the contents of the target location and stash it,
1236 then overwrite it with a breakpoint instruction.
1238 addr - is the target location in the target machine.
1239 contents_cache - is a pointer to memory allocated for saving the target contents.
1240 It is guaranteed by the caller to be long enough to save sizeof
1243 FIXME: This size is target_arch dependent and should be available in
1244 the target_arch transfer vector, if we ever have one... */
1247 es1800_insert_breakpoint (addr, contents_cache)
1249 char *contents_cache;
1253 val = target_read_memory (addr, contents_cache, sizeof (es1800_break_insn));
1257 val = target_write_memory (addr, es1800_break_insn,
1258 sizeof (es1800_break_insn));
1265 /* Write back the stashed instruction
1267 addr - is the target location in the target machine.
1268 contents_cache - is a pointer to memory allocated for saving the target contents.
1269 It is guaranteed by the caller to be long enough to save sizeof
1270 BREAKPOINT bytes. */
1273 es1800_remove_breakpoint (addr, contents_cache)
1275 char *contents_cache;
1278 return (target_write_memory (addr, contents_cache,
1279 sizeof (es1800_break_insn)));
1282 /* create_break_insn ()
1283 Primitive datastructures containing the es1800 breakpoint instruction */
1286 es1800_create_break_insn (ins, vec)
1299 Seach for breakpoint routine in emulator memory.
1300 returns non-zero on failure
1301 vec - trap vector used for breakpoints */
1307 CORE_ADDR memaddress;
1309 char *instr = "NqNqNqNs"; /* breakpoint routine */
1312 get_break_addr (vec, &memaddress);
1316 status = target_read_memory (memaddress, buf, 8);
1319 memory_error (status, memaddress);
1321 return (STRCMP (instr, buf));
1327 /* get_break_addr ()
1328 find address of breakpint routine
1329 vec - trap vector used for breakpoints
1330 addrp - store the address here */
1333 get_break_addr (vec, addrp)
1337 CORE_ADDR memaddress = 0;
1346 send_with_reply ("VBR ", buf, sizeof (buf));
1348 for (k = 0; k < 4; k++)
1350 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
1352 error ("Emulator reply is too short: %s", buf);
1354 base_addr[k] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
1356 /* base addr of exception vector table */
1357 memaddress = *((CORE_ADDR *) base_addr);
1360 memaddress += (vec + 32) * 4; /* address of trap vector */
1361 status = target_read_memory (memaddress, (char *) addrp, 4);
1364 memory_error (status, memaddress);
1369 /* Kill an inferior process */
1374 if (inferior_pid != 0)
1377 es1800_mourn_inferior ();
1382 /* Load a file to the ES1800 emulator.
1383 Converts the file from a.out format into Extended Tekhex format
1384 before the file is loaded.
1385 Also loads the trap routine, and sets the ES1800 breakpoint on it
1386 filename - the a.out to be loaded
1387 from_tty - says whether to be verbose or not
1388 FIXME Uses emulator overlay memory for trap routine */
1391 es1800_load (filename, from_tty)
1399 struct cleanup *old_chain;
1400 int es1800_load_format = 5;
1402 if (es1800_desc < 0)
1404 printf ("No emulator attached, type emulator-command first\n");
1408 filename = tilde_expand (filename);
1409 make_cleanup (free, filename);
1411 switch (es1800_load_format)
1413 case 2: /* Extended Tekhex */
1416 printf ("Converting \"%s\" to Extended Tekhex Format\n", filename);
1418 sprintf (buf, "tekhex %s", filename);
1420 sprintf (loadname, "out.hex");
1423 case 5: /* Motorola S-rec */
1426 printf ("Converting \"%s\" to Motorola S-record format\n",
1429 /* in the future the source code in copy (part of binutils-1.93) will
1430 be included in this file */
1432 "copy -s \"a.out-sunos-big\" -d \"srec\" %s /tmp/out.hex",
1435 sprintf (loadname, "/tmp/out.hex");
1439 error ("Downloading format not defined\n");
1442 breakpoint_init_inferior ();
1446 printf ("Downloading \"%s\" to the ES 1800\n",filename);
1448 if ((instream = fopen (loadname, "r")) == NULL)
1450 perror_with_name ("fopen:");
1453 old_chain = make_cleanup (fclose, instream);
1458 download (instream, from_tty, es1800_load_format);
1460 /* if breakpoint routine is not present anymore we have to check
1461 whether to download a new breakpoint routine or not */
1463 if ((verify_break (es1800_break_vec) != 0)
1464 && query ("No breakpoint routine in ES 1800 emulator!\nDownload a breakpoint routine to the emulator? "))
1467 printf ("Using break vector 0x%x\n", es1800_break_vec);
1468 sprintf (buf, "0x%x ", es1800_break_vec);
1469 printf ("Give the start address of the breakpoint routine: ");
1470 fgets (buf + strlen (buf), sizeof (buf) - strlen (buf), stdin);
1471 es1800_init_break (buf, 0);
1474 do_cleanups (old_chain);
1476 readchar (); /* FIXME I am getting a ^G = 7 after the prompt */
1479 if (fclose (instream) == EOF)
1484 if (es1800_load_format != 2)
1486 sprintf (buf, "/usr/bin/rm %s", loadname);
1490 symbol_file_command (filename, from_tty); /* reading symbol table */
1496 #define NUMCPYBYTES 20
1499 bfd_copy (from_bfd, to_bfd)
1505 char buf[NUMCPYBYTES];
1507 for (p = from_bfd->sections; p != NULL; p = p->next)
1509 printf (" Copying section %s. Size = %x.\n", p->name, p->_cooked_size);
1510 printf (" vma = %x, offset = %x, output_sec = %x\n",
1511 p->vma, p->output_offset, p->output_section);
1512 new = bfd_make_section (to_bfd, p->name);
1513 if (p->_cooked_size &&
1514 !bfd_set_section_size (to_bfd, new, p->_cooked_size))
1516 error ("Wrong BFD size!\n");
1518 if (!bfd_set_section_flags (to_bfd, new, p->flags))
1520 error ("bfd_set_section_flags");
1524 for (i = 0; (i + NUMCPYBYTES) < p->_cooked_size ; i += NUMCPYBYTES)
1526 if (!bfd_get_section_contents (from_bfd, p, (PTR) buf, (file_ptr) i,
1527 (bfd_size_type) NUMCPYBYTES))
1529 error ("bfd_get_section_contents\n");
1531 if (!bfd_set_section_contents (to_bfd, new, (PTR) buf, (file_ptr) i,
1532 (bfd_size_type) NUMCPYBYTES))
1534 error ("bfd_set_section_contents\n");
1537 bfd_get_section_contents (from_bfd, p, (PTR) buf, (file_ptr) i,
1538 (bfd_size_type) (p->_cooked_size - i));
1539 bfd_set_section_contents (to_bfd, new, (PTR) buf,(file_ptr) i,
1540 (bfd_size_type) (p->_cooked_size - i));
1546 /* Start an process on the es1800 and set inferior_pid to the new
1548 execfile - the file to run
1549 args - arguments passed to the program
1550 env - the environment vector to pass */
1553 es1800_create_inferior (execfile, args, env)
1561 struct expression *expr;
1562 register struct cleanup *old_chain = 0;
1568 error ("Can't pass arguments to remote ES1800 process");
1572 if (query ("Use 'start' as entry point? "))
1574 expr = parse_c_expression ("start");
1575 old_chain = make_cleanup (free_current_contents, &expr);
1576 val = evaluate_expression (expr);
1577 entry_pt = (val->location).address;
1581 printf ("Enter the program's entry point (in hexadecimal): ");
1582 scanf ("%x", &entry_pt);
1586 if (execfile == 0 || exec_bfd == 0)
1588 error ("No exec file specified");
1591 entry_pt = (int) bfd_get_start_address (exec_bfd);
1595 /* Now that we have a child process, make it our target. */
1597 push_target (&es1800_child_ops);
1599 /* The "process" (board) is already stopped awaiting our commands, and
1600 the program is already downloaded. We just set its PC and go. */
1602 inferior_pid = pid; /* Needed for wait_for_inferior below */
1604 clear_proceed_status ();
1606 /* Tell wait_for_inferior that we've started a new process. */
1608 init_wait_for_inferior ();
1610 /* Set up the "saved terminal modes" of the inferior
1611 based on what modes we are starting it with. */
1613 target_terminal_init ();
1615 /* Install inferior's terminal modes. */
1617 target_terminal_inferior ();
1619 /* remote_start (args); */
1620 /* trap_expected = 0; */
1621 /* insert_step_breakpoint (); FIXME, do we need this? */
1623 /* Let 'er rip... */
1624 proceed ((CORE_ADDR) entry_pt, TARGET_SIGNAL_DEFAULT, 0);
1629 /* The process has died, clean up. */
1632 es1800_mourn_inferior ()
1634 remove_breakpoints ();
1635 unpush_target (&es1800_child_ops);
1636 generic_mourn_inferior (); /* Do all the proper things now */
1639 /* ES1800-protocol specific routines */
1641 /* Keep discarding input from the remote system, until STRING is found.
1642 Let the user break out immediately.
1643 string - the string to expect
1644 nowait - break out if string not the emulator's first respond otherwise
1645 read until string is found (== 0) */
1648 expect (string, nowait)
1663 if (c == toupper (*p))
1678 printf ("\'%s\' expected\n" , string);
1679 printf ("char %d is %d", p - string, c);
1685 /* Keep discarding input until we see the prompt. */
1694 /* Read one character */
1698 /* read from stdin */
1706 printf ("readchar, give one character\n");
1709 #if defined (LOG_FILE)
1710 putc (buf[0] & 0x7f, log_file);
1713 return (buf[0] & 0x7f);
1716 #else /* !DEBUG_STDIN */
1718 /* Read a character from the remote system, doing all the fancy
1730 /* termio does the timeout for us. */
1731 read (es1800_desc, buf, 1);
1736 while (read (es1800_desc, buf, 1) != 1)
1740 error ("Timeout reading from remote system.");
1742 else if (errno != EWOULDBLOCK)
1744 perror_with_name ("remote read");
1750 #if defined (LOG_FILE)
1751 putc (buf[0] & 0x7f, log_file);
1755 return (buf[0] & 0x7f);
1758 #endif /* DEBUG_STDIN */
1761 /* Send a command to the emulator and save the reply.
1762 Report an error if we get an error reply.
1763 string - the es1800 command
1764 buf - containing the emulator reply on return
1765 len - size of buf */
1768 send_with_reply (string, buf, len)
1773 write (es1800_desc, "\r", 1);
1780 getmessage (buf, len);
1784 /* Send the command in STR to the emulator adding \r. check
1785 the echo for consistency.
1786 string - the es1800 command */
1789 send_command (string)
1793 write (es1800_desc, "\r", 1);
1803 string - the es1800 command */
1811 fprintf (stderr, "Sending: %s\n", string);
1813 write (es1800_desc, string, strlen (string));
1817 /* Read a message from the emulator and store it in BUF.
1818 buf - containing the emulator reply on return
1819 len - size of buf */
1822 getmessage (buf, len)
1828 int prompt_found = 0;
1831 #if defined (LOG_FILE)
1832 /* This is a convenient place to do this. The idea is to do it often
1833 enough that we never lose much data if we terminate abnormally. */
1843 if (len-- < 2) /* char and terminaling NULL */
1845 error ("input buffer overrun\n");
1850 if ((c == '>') && (*(bp - 1) == ' '))
1855 while (!prompt_found);
1860 fprintf (stderr,"message received :%s\n", buf);
1865 download (instream, from_tty, format)
1874 send_command ("SET #2,$1A"); /* reset char = ^Z */
1875 send_command ("SET #3,$11,$13"); /* XON XOFF */
1878 send_command ("SET #26,#2");
1882 send_command ("SET #26,#5"); /* Format=Extended Tekhex */
1884 send_command ("DFB = $10");
1885 send_command ("PUR");
1886 send_command ("CES");
1891 printf (" 0 records loaded...\r");
1893 while (fgets (buf, 160, instream))
1898 printf ("%5d\b\b\b\b\b",++i);
1901 if ((c = readchar ()) != 006)
1903 error ("expected ACK");
1912 /* Additional commands */
1914 /* Talk directly to the emulator
1915 FIXME, uses busy wait, and is SUNOS (or at least BSD) specific */
1919 es1800_transparent (args, from_tty)
1924 struct sgttyb modebl;
1927 struct sgttyb console_mode_save;
1928 int console_fc_save;
1933 char consolebuf[80];
1935 char es1800_buf[80];
1939 if (es1800_desc < 0)
1941 printf ("No emulator attached, type emulator-command first\n");
1946 printf ("You are now communicating directly with the ES 1800 emulator.\n");
1947 printf ("To leave this mode (transparent mode), press ^E.\n");
1952 if ((console = open ("/dev/tty", O_RDWR)) == -1)
1954 perror_with_name ("/dev/tty:");
1957 if ((fcflag = fcntl (console, F_GETFL, 0)) == -1)
1959 perror_with_name ("fcntl console");
1962 console_fc_save = fcflag;
1963 fcflag = fcflag | FNDELAY;
1965 if (fcntl (console, F_SETFL, fcflag) == -1)
1967 perror_with_name ("fcntl console");
1970 if (ioctl (console, TIOCGETP, &modebl))
1972 perror_with_name ("ioctl console");
1975 console_mode_save = modebl;
1976 modebl.sg_flags = RAW;
1978 if (ioctl (console, TIOCSETP, &modebl))
1980 perror_with_name ("ioctl console");
1983 if ((fcflag = fcntl (es1800_desc, F_GETFL, 0)) == -1)
1985 perror_with_name ("fcntl serial");
1988 es1800_fc_save = fcflag;
1989 fcflag = fcflag | FNDELAY;
1991 if (fcntl (es1800_desc, F_SETFL, fcflag) == -1)
1993 perror_with_name ("fcntl serial");
1998 cc = read (console, inputbuf, inputcnt);
2001 if ((*inputbuf & 0x7f) == 0x05)
2005 for (i = 0; i < cc; )
2007 es1800_buf[es1800_cnt++] = inputbuf[i++];
2009 if ((cc = write (es1800_desc, es1800_buf, es1800_cnt)) == -1)
2011 perror_with_name ("FEL! read:");
2014 if (es1800_cnt && cc)
2016 for (i = 0; i < es1800_cnt; i++)
2018 es1800_buf[i] = es1800_buf[cc+i];
2022 else if (errno != EWOULDBLOCK)
2024 perror_with_name ("FEL! read:");
2027 cc = read (es1800_desc,inputbuf,inputcnt);
2030 for (i = 0; i < cc; )
2032 consolebuf[consolecnt++] = inputbuf[i++];
2034 if ((cc = write (console,consolebuf,consolecnt)) == -1)
2036 perror_with_name ("FEL! write:");
2039 if (consolecnt && cc)
2041 for (i = 0; i < consolecnt; i++)
2043 consolebuf[i] = consolebuf[cc+i];
2047 else if (errno != EWOULDBLOCK)
2049 perror_with_name ("FEL! read:");
2053 console_fc_save = console_fc_save & !FNDELAY;
2054 if (fcntl (console, F_SETFL, console_fc_save) == -1)
2056 perror_with_name ("FEL! fcntl");
2059 if (ioctl (console, TIOCSETP, &console_mode_save))
2061 perror_with_name ("FEL! ioctl");
2066 if (fcntl (es1800_desc, F_SETFL, es1800_fc_save) == -1)
2068 perror_with_name ("FEL! fcntl");
2076 es1800_init_break (args, from_tty)
2080 CORE_ADDR memaddress = 0;
2089 error_no_arg ("a trap vector");
2092 if (!(space_index = strchr (args, ' ')))
2094 error ("Two arguments needed (trap vector and address of break routine).\n");
2097 *space_index = '\0';
2099 es1800_break_vec = strtol (args, (char **) NULL, 0);
2100 es1800_break_address = parse_and_eval_address (space_index + 1);
2102 es1800_create_break_insn (es1800_break_insn, es1800_break_vec);
2106 send_with_reply ("VBR ", buf, sizeof (buf));
2108 for (k = 0; k < 4; k++)
2110 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
2112 error ("Emulator reply is too short: %s", buf);
2114 base_addr[k] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
2116 /* base addr of exception vector table */
2117 memaddress = *((CORE_ADDR *) base_addr);
2120 memaddress += (es1800_break_vec + 32) * 4; /* address of trap vector */
2122 sprintf (buf, "@.L%lx=$%lx", memaddress, es1800_break_address);
2123 send_command (buf); /* set the address of the break routine in the */
2126 sprintf (buf, "@.L%lx=$4E714E71", es1800_break_address); /* NOP; NOP */
2128 sprintf (buf, "@.L%lx=$4E714E73", es1800_break_address + 4); /* NOP; RTE */
2131 sprintf (buf, "AC2=$%lx", es1800_break_address + 4);
2132 /* breakpoint at es1800-break_address */
2134 send_command ("WHEN AC2 THEN BRK"); /* ie in exception routine */
2138 printf ("Breakpoint (trap $%x) routine at address: %lx\n",
2139 es1800_break_vec, es1800_break_address);
2144 es1800_child_open (arg, from_tty)
2148 error ("Use the \"run\" command to start a child process.");
2152 es1800_child_detach (args, from_tty)
2158 error ("Argument given to \"detach\" when remotely debugging.");
2164 printf ("Ending debugging the process %d.\n", inferior_pid);
2169 /* Define the target subroutine names */
2171 static struct target_ops es1800_ops =
2173 "es1800", /* to_shortname */
2175 "Remote serial target in ES1800-emulator protocol",
2177 "Remote debugging on the es1800 emulator via a serial line.\n\
2178 Specify the serial device it is connected to (e.g. /dev/ttya).",
2179 es1800_open, /* to_open */
2180 es1800_close, /* to_close */
2181 es1800_attach, /* to_attach */
2182 es1800_detach, /* to_detach */
2183 es1800_resume, /* to_resume */
2185 NULL, /* to_fetch_registers */
2186 NULL, /* to_store_registers */
2187 es1800_prepare_to_store, /* to_prepare_to_store */
2188 es1800_xfer_inferior_memory, /* to_xfer_memory */
2189 es1800_files_info, /* to_files_info */
2190 es1800_insert_breakpoint, /* to_insert_breakpoint */
2191 es1800_remove_breakpoint, /* to_remove_breakpoint */
2192 NULL, /* to_terminal_init */
2193 NULL, /* to_terminal_inferior */
2194 NULL, /* to_terminal_ours_for_output */
2195 NULL, /* to_terminal_ours */
2196 NULL, /* to_terminal_info */
2198 es1800_load, /* to_load */
2199 NULL, /* to_lookup_symbol */
2200 es1800_create_inferior, /* to_create_inferior */
2201 NULL, /* to_mourn_inferior */
2203 0, /* notice_signals */
2204 core_stratum, /* to_stratum */
2206 0, /* to_has_all_memory */
2207 1, /* to_has_memory */
2208 0, /* to_has_stack */
2209 0, /* to_has_registers */
2210 0, /* to_has_execution */
2211 NULL, /* to_sections */
2212 NULL, /* to_sections_end */
2213 OPS_MAGIC /* to_magic (always last) */
2216 /* Define the target subroutine names */
2218 static struct target_ops es1800_child_ops =
2220 "es1800_process", /* to_shortname */
2222 "Remote serial target in ES1800-emulator protocol",
2224 "Remote debugging on the es1800 emulator via a serial line.\n\
2225 Specify the serial device it is connected to (e.g. /dev/ttya).",
2226 es1800_child_open, /* to_open */
2227 NULL, /* to_close */
2228 es1800_attach, /* to_attach */
2229 es1800_child_detach, /* to_detach */
2230 es1800_resume, /* to_resume */
2231 es1800_wait, /* to_wait */
2232 es1800_fetch_register, /* to_fetch_registers */
2233 es1800_store_register, /* to_store_registers */
2234 es1800_prepare_to_store, /* to_prepare_to_store */
2235 es1800_xfer_inferior_memory, /* to_xfer_memory */
2236 es1800_files_info, /* to_files_info */
2237 es1800_insert_breakpoint, /* to_insert_breakpoint */
2238 es1800_remove_breakpoint, /* to_remove_breakpoint */
2239 NULL, /* to_terminal_init */
2240 NULL, /* to_terminal_inferior */
2241 NULL, /* to_terminal_ours_for_output */
2242 NULL, /* to_terminal_ours */
2243 NULL, /* to_terminal_info */
2244 es1800_kill, /* to_kill */
2245 es1800_load, /* to_load */
2246 NULL, /* to_lookup_symbol */
2247 es1800_create_inferior, /* to_create_inferior */
2248 es1800_mourn_inferior, /* to_mourn_inferior */
2250 0, /* notice_signals */
2251 process_stratum, /* to_stratum */
2253 1, /* to_has_all_memory */
2254 1, /* to_has_memory */
2255 1, /* to_has_stack */
2256 1, /* to_has_registers */
2257 1, /* to_has_execution */
2258 NULL, /* to_sections */
2259 NULL, /* to_sections_end */
2260 OPS_MAGIC /* to_magic (always last) */
2265 _initialize_es1800 ()
2268 add_target (&es1800_ops);
2269 add_target (&es1800_child_ops);
2270 add_com ("transparent", class_support, es1800_transparent,
2271 "Start transparent communication with the ES 1800 emulator.");
2272 add_com ("init_break", class_support, es1800_init_break,
2273 "Download break routine and initialize break facility on ES 1800");