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.
95 #include <sys/ioctl.h>
104 #include "inferior.h"
107 #include "terminal.h"
109 #include "remote-utils.h"
112 #include <sys/types.h>
118 /* External variables referenced. */
120 extern bfd
*exec_bfd
;
122 /* Prototypes for local functions */
125 es1800_child_detach
PARAMS ((char *, int));
128 es1800_child_open
PARAMS ((char *, int));
131 es1800_transparent
PARAMS ((char *, int));
134 es1800_create_inferior
PARAMS ((char *, char *, char **));
137 es1800_load
PARAMS ((char *, int));
140 es1800_kill
PARAMS ((void));
143 verify_break
PARAMS ((int));
146 es1800_remove_breakpoint
PARAMS ((CORE_ADDR
, char *));
149 es1800_insert_breakpoint
PARAMS ((CORE_ADDR
, char *));
152 es1800_files_info
PARAMS ((struct target_ops
*));
155 es1800_xfer_inferior_memory
PARAMS ((CORE_ADDR
, char *, int, int,
156 struct target_ops
*));
159 es1800_prepare_to_store
PARAMS ((void));
162 es1800_wait
PARAMS ((WAITTYPE
*));
165 es1800_resume
PARAMS ((int, int, int));
168 es1800_detach
PARAMS ((char *, int));
171 es1800_attach
PARAMS ((char *, int));
174 damn_b
PARAMS ((char *));
177 es1800_open
PARAMS ((char *, int));
180 es1800_timer
PARAMS ((void));
183 es1800_reset
PARAMS ((char *));
186 es1800_request_quit
PARAMS ((void));
189 readchar
PARAMS ((void));
192 expect
PARAMS ((char *, int));
195 expect_prompt
PARAMS ((void));
198 download
PARAMS ((FILE *, int, int));
202 bfd_copy
PARAMS ((bfd
*, bfd
*));
206 get_break_addr
PARAMS ((int, CORE_ADDR
*));
209 fromhex
PARAMS ((int));
212 tohex
PARAMS ((int));
215 es1800_close
PARAMS ((int));
218 es1800_fetch_registers
PARAMS ((void));
221 es1800_fetch_register
PARAMS ((int));
224 es1800_store_register
PARAMS ((int));
227 es1800_read_bytes
PARAMS ((CORE_ADDR
, char *, int));
230 es1800_write_bytes
PARAMS ((CORE_ADDR
, char *, int));
233 send_with_reply
PARAMS ((char *, char *, int));
236 send_command
PARAMS ((char *));
239 send
PARAMS ((char *));
242 getmessage
PARAMS ((char *, int));
245 es1800_mourn_inferior
PARAMS ((void));
248 es1800_create_break_insn
PARAMS ((char *, int));
251 es1800_init_break
PARAMS ((char *, int));
253 /* Local variables */
255 #define LOG_FILE "es1800.log"
256 #if defined (LOG_FILE)
257 static FILE *log_file
;
260 extern struct target_ops es1800_ops
; /* Forward decl */
261 extern struct target_ops es1800_child_ops
; /* Forward decl */
264 static int timeout
= 100;
265 static char *savename
; /* Name of i/o device used */
266 static TERMINAL es1800_sg_save
; /* Save stty state */
267 static int es1800_fc_save
; /* Save fcntl state */
269 /* indicates that the emulator uses 32-bit data-adress (68020-mode)
270 instead of 24-bit (68000 -mode) */
274 #define MODE (m68020 ? "M68020" : "M68000" )
275 #define ES1800_BREAK_VEC (0xf)
277 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
278 es1800_open knows that we don't have a file open when the program
281 static int es1800_desc
= -1;
284 #define HDRLEN sizeof("@.BAAAAAAAA=$VV\r")
286 /* Maximum number of bytes to read/write at once. The value here
287 is chosen to fill up a packet. */
289 #define MAXBUFBYTES ((PBUFSIZ-150)*16/75 )
291 static int es1800_break_vec
= 0;
292 static char es1800_break_insn
[2];
293 static long es1800_break_address
;
294 static void (*old_sigint
)(); /* Old signal-handler for sigint */
295 static jmp_buf interrupt
;
297 /* Local signalhandler to allow breaking tranfers or program run.
298 Rely on global variables: old_sigint(), interrupt */
301 es1800_request_quit ()
303 /* restore original signalhandler */
304 signal (SIGINT
, old_sigint
);
305 longjmp (interrupt
, 1);
310 Sending reset character(octal 32) to emulator.
311 quit - return to '(esgdb)' prompt or continue */
321 printf ("\nResetting emulator... ");
323 strcpy (buf
, "\032");
333 /* Called when SIGALRM signal sent due to alarm() timeout.
334 Rely on global variables: timeout */
343 printf ("es1800_timer called\n");
348 #endif /* HAVE_TERMIO */
351 /* Open a connection to a remote debugger and push the new target
352 onto the stack. Check if the emulator is responding and find out
353 what kind of processor the emulator is connected to.
354 Initiate the breakpoint handling in the emulator.
356 name - the filename used for communication (ex. '/dev/tta')
357 from_tty - says whether to be verbose or not */
360 es1800_open (name
, from_tty
)
372 if (!name
) /* no device name given in target command */
374 error_no_arg ("serial port device name");
376 sprintf(baudrate
, "%d", sr_get_baud_rate());
378 target_preopen (from_tty
);
381 /* open the device and configure it for communication */
385 es1800_desc
= open (name
, O_RDWR
);
388 perror_with_name (name
);
390 savename
= savestring (name
, strlen (name
));
392 if (ioctl (es1800_desc
, TIOCGETP
, &sg
) == -1)
394 perror_with_name (name
);
398 if ((fcflag
= fcntl (es1800_desc
, F_GETFL
, 0)) == -1)
400 perror_with_name ("fcntl serial");
402 es1800_fc_save
= fcflag
;
404 fcflag
= (fcflag
& (FREAD
| FWRITE
)); /* mask out any funny stuff */
405 if (fcntl (es1800_desc
, F_SETFL
, fcflag
) == -1)
407 perror_with_name ("fcntl serial");
411 sg
.c_cc
[VMIN
] = 0; /* read with timeout. */
412 sg
.c_cc
[VTIME
] = timeout
* 10;
413 sg
.c_lflag
&= ~(ICANON
| ECHO
);
414 sg
.c_cflag
= (sg
.c_cflag
& ~CBAUD
) | damn_b (baudrate
);
416 sg
.sg_ispeed
= damn_b (baudrate
);
417 sg
.sg_ospeed
= damn_b (baudrate
);
418 sg
.sg_flags
= CBREAK
+TANDEM
;
421 if ((ioctl (es1800_desc
, TIOCSETP
, &sg
)) == -1)
423 perror ("es1800_open: error in ioctl");
426 #endif /* DEBUG_STDIN */
428 push_target (&es1800_ops
); /* Switch to using remote target now */
431 printf ("Remote ES1800 debugging using %s\n", name
);
436 #ifndef NO_SIGINTERRUPT
438 /* Cause SIGALRM's to make reads fail with EINTR instead of resuming
441 if (siginterrupt (SIGALRM
, 1) != 0)
443 perror ("es1800_open: error in siginterrupt");
446 #endif /* NO_SIGINTERRUPT */
448 /* Set up read timeout timer. */
450 if ((void(*)()) signal (SIGALRM
, es1800_timer
) == (void(*)()) -1)
452 perror ("es1800_open: error in signal");
455 #endif /* HAVE_TERMIO */
458 #if defined (LOG_FILE)
460 log_file
= fopen (LOG_FILE
, "w");
461 if (log_file
== NULL
)
463 perror_with_name (LOG_FILE
);
466 #endif /* LOG_FILE */
468 /* Hello? Are you there?, also check mode */
470 /* send_with_reply( "DB 0 TO 1", buf, sizeof(buf)); */
471 /* for (p = buf, i = 0; *p++ =='0';) */ /* count the number of zeros */
475 getmessage (buf
, sizeof (buf
)); /* send reset character */
479 printf ("Checking mode.... ");
481 /* m68020 = (i==8); */ /* if eight zeros then we are in m68020 mode */
483 /* What kind of processor am i talking to ?*/
485 while (*p
++ != '\n') {;}
486 while (*p
++ != '\n') {;}
487 while (*p
++ != '\n') {;}
488 for (i
= 0; i
< 20; i
++, p
++) {;}
489 m68020
= !strncmp (p
, "68020", 5);
492 printf ("You are in %s(%c%c%c%c%c)-mode\n", MODE
, p
[0], p
[1], p
[2],
496 /* if no init_break statement is present in .gdb file we have to check
497 whether to download a breakpoint routine or not */
500 if ((es1800_break_vec
== 0) || (verify_break (es1800_break_vec
) != 0)
501 && query ("No breakpoint routine in ES 1800 emulator!\nDownload a breakpoint routine to the emulator? "))
503 CORE_ADDR memaddress
;
504 printf ("Give the start address of the breakpoint routine: ");
505 scanf ("%li", &memaddress
);
506 es1800_init_break ((es1800_break_vec
? es1800_break_vec
:
507 ES1800_BREAK_VEC
), memaddress
);
513 /* Close out all files and local state before this target loses control.
514 quitting - are we quitting gdb now? */
517 es1800_close (quitting
)
520 if (es1800_desc
>= 0)
522 printf ("\nClosing connection to emulator...\n");
523 ioctl (es1800_desc
, TIOCSETP
, &es1800_sg_save
);
524 fcntl (es1800_desc
,F_SETFL
, es1800_fc_save
);
528 if (savename
!= NULL
)
534 #if defined (LOG_FILE)
536 if (log_file
!= NULL
)
538 if (ferror (log_file
))
540 printf ("Error writing log file.\n");
542 if (fclose (log_file
) != 0)
544 printf ("Error closing log file.\n");
549 #endif /* LOG_FILE */
555 Translate baud rates from integers to damn B_codes. Unix should
556 have outgrown this crap years ago, but even POSIX wouldn't buck it.
557 rate - the baudrate given as a string
558 return value: the baudrate as a B_code */
597 for (i
= 0; baudtab
[i
].rate
!= 0; i
++)
599 if (STREQ (rate
, baudtab
[i
].rate
))
601 return (baudtab
[i
].damn_b
);
604 error ("Illegal baudrate");
608 /* Attaches to a process on the target side
609 proc_id - the id of the process to be attached.
610 from_tty - says whether to be verbose or not */
613 es1800_attach (args
, from_tty
)
617 error ("Cannot attach to pid %s, this feature is not implemented yet.",
622 /* Takes a program previously attached to and detaches it.
623 We better not have left any breakpoints
624 in the program or it'll die when it hits one.
625 Close the open connection to the remote debugger.
626 Use this when you want to detach and do something else
629 args - arguments given to the 'detach' command
630 from_tty - says whether to be verbose or not */
633 es1800_detach (args
, from_tty
)
639 error ("Argument given to \"detach\" when remotely debugging.");
644 printf ("Ending es1800 remote debugging.\n");
649 /* Tell the remote machine to resume.
650 step - single-step or run free
651 siggnal - the signal value to be given to the target (0 = no signal) */
654 es1800_resume (pid
, step
, siggnal
)
663 error ("Can't send signals to a remote system.");
667 strcpy (buf
,"STP\r");
672 send_command ("RBK");
676 /* Wait until the remote machine stops, then return,
677 storing status in STATUS just as `wait' would.
684 unsigned char buf
[PBUFSIZ
];
685 int old_timeout
= timeout
;
687 WSETEXIT ((*status
), 0);
688 timeout
= 0; /* Don't time out -- user program is running. */
689 if (!setjmp (interrupt
))
691 old_sigint
= signal (SIGINT
, es1800_request_quit
);
694 getmessage (buf
, sizeof(buf
));
695 if (strncmp ( buf
, "\r\n* BREAK *", 11) == 0)
697 WSETSTOP ((*status
), SIGTRAP
);
698 send_command ("STP"); /* Restore stack and PC and such */
701 send_command ("STP");
705 if (strncmp (buf
, "STP\r\n ", 6) == 0)
707 WSETSTOP ((*status
), SIGTRAP
);
710 if (buf
[strlen (buf
) - 2] == 'R')
712 printf ("Unexpected emulator reply: \n%s\n", buf
);
716 printf ("Unexpected stop: \n%s\n", buf
);
717 WSETSTOP ((*status
), SIGQUIT
);
725 printf ("\nStopping emulator...");
726 if (!setjmp (interrupt
))
728 old_sigint
= signal (SIGINT
, es1800_request_quit
);
729 send_command ("STP");
730 printf (" emulator stopped\n");
731 WSETSTOP ((*status
), SIGINT
);
736 es1800_reset ((char*) 1);
739 signal (SIGINT
, old_sigint
);
740 timeout
= old_timeout
;
745 /* Fetch register values from remote machine.
746 regno - the register to be fetched (fetch all registers if -1) */
749 es1800_fetch_register (regno
)
756 static char regtab
[18][4] =
758 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
759 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
763 if ((regno
< 15) || (regno
== 16) || (regno
== 17))
766 send_with_reply (regtab
[regno
], buf
, sizeof (buf
));
768 for (k
= 0; k
< 4; k
++)
770 if ((p
[k
*2 + 1] == 0) || (p
[k
*2 + 2] == 0))
772 error ("Emulator reply is too short: %s", buf
);
774 registers
[r
++] = (fromhex (p
[k
*2 + 1]) * 16) + fromhex (p
[k
*2 + 2]);
779 es1800_fetch_registers ();
783 /* Read the remote registers into REGISTERS.
784 Always fetches all registers. */
787 es1800_fetch_registers ()
790 char SR_buf
[PBUFSIZ
];
796 send_with_reply ("DR", buf
, sizeof (buf
));
798 /* Reply is edited to a string that describes registers byte by byte,
799 each byte encoded as two hex characters. */
804 /* parsing row one - D0-D7-registers */
806 while (*p
++ != '\n') {;}
807 for (i
= 4; i
< 70; i
+= (i
== 39 ? 3 : 1))
809 for (k
= 0; k
< 4; k
++)
811 if (p
[i
+0] == 0 || p
[i
+1] == 0)
813 error ("Emulator reply is too short: %s", buf
);
815 registers
[r
++] = (fromhex (p
[i
+0]) * 16) + fromhex (p
[i
+1]);
821 /* parsing row two - A0-A6-registers */
823 while (*p
++ != '\n') {;}
824 for (i
= 4; i
< 61; i
+= (i
== 39 ? 3 : 1))
826 for (k
= 0; k
< 4; k
++)
828 if (p
[i
+0] == 0 || p
[i
+1] == 0)
830 error ("Emulator reply is too short: %s", buf
);
832 registers
[r
++] = (fromhex (p
[i
+0])) * 16 + fromhex (p
[i
+1]);
838 while (*p
++ != '\n') {;}
840 /* fetch SSP-, SR- and PC-registers */
842 /* first - check STATUS-word and decide which stackpointer to use */
844 send_with_reply ("SR", SR_buf
, sizeof (SR_buf
));
850 if (*p
== '3') /* use masterstackpointer MSP */
852 send_with_reply ("MSP", buf
, sizeof (buf
));
854 else if (*p
== '2') /* use interruptstackpointer ISP */
856 send_with_reply ("ISP", buf
, sizeof (buf
));
858 else /* use userstackpointer USP */
860 send_with_reply ("USP", buf
, sizeof (buf
));
863 for (k
= 0; k
<4; k
++)
865 if (p
[k
*2+1] == 0 || p
[k
*2+2] == 0)
867 error ("Emulator reply is too short: %s", buf
);
869 registers
[r
++] = fromhex (buf
[k
*2+1]) * 16 + fromhex (buf
[k
*2+2]);
873 for (k
= 0; k
< 4; k
++)
875 if (p
[k
*2+1] == 0 || p
[k
*2+2] == 0)
877 error ("Emulator reply is too short: %s", buf
);
880 fromhex (SR_buf
[k
*2+1]) * 16 + fromhex (SR_buf
[k
*2+2]);
882 send_with_reply ("PC", buf
, sizeof (buf
));
884 for (k
= 0; k
<4; k
++)
886 if (p
[k
*2+1] == 0 || p
[k
*2+2] == 0)
888 error ("Emulator reply is too short: %s", buf
);
890 registers
[r
++] = fromhex (buf
[k
*2+1]) * 16 + fromhex (buf
[k
*2+2]);
893 else /* 68000-mode */
895 if (*p
== '2') /* use supervisorstackpointer SSP */
897 send_with_reply ("SSP", buf
, sizeof (buf
));
899 else /* use userstackpointer USP */
901 send_with_reply ("USP", buf
, sizeof (buf
));
904 /* fetch STACKPOINTER */
907 for (k
= 0; k
< 4; k
++)
909 if (p
[k
*2 + 1] == 0 || p
[k
*2 + 2] == 0)
911 error ("Emulator reply is too short: %s", buf
);
913 registers
[r
++] = fromhex (buf
[k
*2+1]) * 16 + fromhex (buf
[k
*2+2]);
919 for (k
= 0; k
< 4; k
++)
921 if (p
[k
*2+1] == 0 || p
[k
*2+2] == 0)
923 error ("Emulator reply is too short: %s", buf
);
926 fromhex (SR_buf
[k
*2+1]) * 16 + fromhex (SR_buf
[k
*2+2]);
931 send_with_reply ("PC", buf
, sizeof (buf
));
933 for (k
= 0; k
< 4; k
++)
935 if (p
[k
*2+1] == 0 || p
[k
*2+2] == 0)
937 error ("Emulator reply is too short: %s", buf
);
939 registers
[r
++] = fromhex (buf
[k
*2+1]) * 16 + fromhex (buf
[k
*2+2]);
944 /* Store register value, located in REGISTER, on the target processor.
945 regno - the register-number of the register to store
946 (-1 means store them all)
947 FIXME: Return errno value. */
950 es1800_store_register(regno
)
954 static char regtab
[18][4] =
956 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
957 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
962 char SR_buf
[PBUFSIZ
];
963 char stack_pointer
[4];
970 r
= (unsigned char *) registers
;
972 if (regno
== -1) /* write all registers */
977 else /* write one register */
984 if ((regno
== -1) || (regno
== 15))
986 /* fetch current status */
987 send_with_reply ("SR", SR_buf
, sizeof (SR_buf
));
992 if (*p
== '3') /* use masterstackpointer MSP */
994 strcpy (stack_pointer
,"MSP");
998 if (*p
== '2') /* use interruptstackpointer ISP */
1000 strcpy (stack_pointer
,"ISP");
1004 strcpy (stack_pointer
,"USP"); /* use userstackpointer USP */
1008 else /* 68000-mode */
1010 if (*p
== '2') /* use supervisorstackpointer SSP */
1012 strcpy (stack_pointer
,"SSP");
1016 strcpy (stack_pointer
,"USP");/* use userstackpointer USP */
1019 strcpy (regtab
[15],stack_pointer
);
1022 for (i
= j
; i
<k
; i
++)
1024 buf
[0] = regtab
[i
][0];
1025 buf
[1] = regtab
[i
][1];
1026 buf
[2] = regtab
[i
][2];
1029 buf
[5] = tohex ((*r
>> 4) & 0x0f);
1030 buf
[6] = tohex (*r
++ & 0x0f);
1031 buf
[7] = tohex ((*r
>> 4) & 0x0f);
1032 buf
[8] = tohex (*r
++ & 0x0f);
1033 buf
[9] = tohex ((*r
>> 4) & 0x0f);
1034 buf
[10] = tohex (*r
++ & 0x0f);
1035 buf
[11] = tohex ((*r
>> 4) & 0x0f);
1036 buf
[12] = tohex (*r
++ & 0x0f);
1039 send_with_reply (buf
, buf
, sizeof (buf
)); /* FIXME, reply not used? */
1044 /* Prepare to store registers. */
1047 es1800_prepare_to_store ()
1049 /* Do nothing, since we can store individual regs */
1052 /* Convert hex digit A to a number. */
1058 if (a
>= '0' && a
<= '9')
1062 else if (a
>= 'a' && a
<= 'f')
1064 return a
- 'a' + 10;
1066 else if (a
>= 'A' && a
<= 'F')
1068 return a
- 'A' + 10;
1072 error ("Reply contains invalid hex digit");
1078 /* Convert number NIB to a hex digit. */
1090 return ('A' + nib
- 10);
1094 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1095 to or from debugger address MYADDR. Write to inferior if WRITE is
1096 nonzero. Returns length of data written or read; 0 for error.
1098 memaddr - the target's address
1099 myaddr - gdb's address
1100 len - number of bytes
1101 write - write if != 0 otherwise read */
1104 es1800_xfer_inferior_memory (memaddr
, myaddr
, len
, write
, tops
)
1109 struct target_ops
*tops
; /* Unused */
1116 xfersize
= len
> MAXBUFBYTES
? MAXBUFBYTES
: len
;
1119 es1800_write_bytes (memaddr
, myaddr
, xfersize
);
1123 es1800_read_bytes (memaddr
, myaddr
, xfersize
);
1125 memaddr
+= xfersize
;
1129 return (origlen
); /* no error possible */
1133 /* Write memory data directly to the emulator.
1134 This does not inform the data cache; the data cache uses this.
1135 MEMADDR is the address in the remote memory space.
1136 MYADDR is the address of the buffer in our space.
1137 LEN is the number of bytes.
1139 memaddr - the target's address
1140 myaddr - gdb's address
1141 len - number of bytes */
1144 es1800_write_bytes (memaddr
, myaddr
, len
)
1154 for (i
= 0; i
< len
; i
++)
1156 sprintf (buf
, "@.B$%x=$%x", memaddr
+i
, (*p
++) & 0xff);
1157 send_with_reply (buf
, buf
, sizeof (buf
)); /* FIXME send_command? */
1162 /* Read memory data directly from the emulator.
1163 This does not use the data cache; the data cache uses this.
1165 memaddr - the target's address
1166 myaddr - gdb's address
1167 len - number of bytes */
1170 es1800_read_bytes (memaddr
, myaddr
, len
)
1175 static int DB_tab
[16] = {8,11,14,17,20,23,26,29,34,37,40,43,46,49,52,55};
1182 if (len
> PBUFSIZ
/ 2 - 1)
1187 if (len
== 1) /* The emulator does not like expressions like: */
1189 len
= 2; /* DB.B $20018 TO $20018 */
1192 /* Reply describes registers byte by byte, each byte encoded as two hex
1195 sprintf (buf
, "DB.B $%x TO $%x", memaddr
, memaddr
+len
-1);
1196 send_with_reply (buf
, buf
, sizeof (buf
));
1198 low_addr
= memaddr
&0x0f;
1199 for (i
= low_addr
; i
< low_addr
+ len
; i
++)
1201 if ((!(i
% 16)) && i
)
1202 { /* if (i = 16,32,48) */
1203 while (*p
++!='\n') {;}
1206 p
= b
+ DB_tab
[i
%16] + (m68020
? 2 : 0);
1207 if (p
[0] == 32 || p
[1] == 32)
1209 error ("Emulator reply is too short: %s", buf
);
1211 myaddr
[i
-low_addr
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
1215 /* Information about the current target */
1218 es1800_files_info (tops
)
1219 struct target_ops
*tops
; /* Unused */
1221 printf ("ES1800 Attached to %s at %d baud in %s mode\n", savename
, 19200,
1226 /* We read the contents of the target location and stash it,
1227 then overwrite it with a breakpoint instruction.
1229 addr - is the target location in the target machine.
1230 contents_cache - is a pointer to memory allocated for saving the target contents.
1231 It is guaranteed by the caller to be long enough to save sizeof
1234 FIXME: This size is target_arch dependent and should be available in
1235 the target_arch transfer vector, if we ever have one... */
1238 es1800_insert_breakpoint (addr
, contents_cache
)
1240 char *contents_cache
;
1244 val
= target_read_memory (addr
, contents_cache
, sizeof (es1800_break_insn
));
1248 val
= target_write_memory (addr
, es1800_break_insn
,
1249 sizeof (es1800_break_insn
));
1256 /* Write back the stashed instruction
1258 addr - is the target location in the target machine.
1259 contents_cache - is a pointer to memory allocated for saving the target contents.
1260 It is guaranteed by the caller to be long enough to save sizeof
1261 BREAKPOINT bytes. */
1264 es1800_remove_breakpoint (addr
, contents_cache
)
1266 char *contents_cache
;
1269 return (target_write_memory (addr
, contents_cache
,
1270 sizeof (es1800_break_insn
)));
1273 /* create_break_insn ()
1274 Primitive datastructures containing the es1800 breakpoint instruction */
1277 es1800_create_break_insn (ins
, vec
)
1290 Seach for breakpoint routine in emulator memory.
1291 returns non-zero on failure
1292 vec - trap vector used for breakpoints */
1298 CORE_ADDR memaddress
;
1300 char *instr
= "NqNqNqNs"; /* breakpoint routine */
1303 get_break_addr (vec
, &memaddress
);
1307 status
= target_read_memory (memaddress
, buf
, 8);
1310 memory_error (status
, memaddress
);
1312 return (STRCMP (instr
, buf
));
1318 /* get_break_addr ()
1319 find address of breakpint routine
1320 vec - trap vector used for breakpoints
1321 addrp - store the address here */
1324 get_break_addr (vec
, addrp
)
1328 CORE_ADDR memaddress
= 0;
1337 send_with_reply ("VBR ", buf
, sizeof (buf
));
1339 for (k
= 0; k
< 4; k
++)
1341 if ((p
[k
*2 + 1] == 0) || (p
[k
*2 + 2] == 0))
1343 error ("Emulator reply is too short: %s", buf
);
1345 base_addr
[k
] = (fromhex (p
[k
*2 + 1]) * 16) + fromhex (p
[k
*2 + 2]);
1347 /* base addr of exception vector table */
1348 memaddress
= *((CORE_ADDR
*) base_addr
);
1351 memaddress
+= (vec
+ 32) * 4; /* address of trap vector */
1352 status
= target_read_memory (memaddress
, (char *) addrp
, 4);
1355 memory_error (status
, memaddress
);
1360 /* Kill an inferior process */
1365 if (inferior_pid
!= 0)
1368 es1800_mourn_inferior ();
1373 /* Load a file to the ES1800 emulator.
1374 Converts the file from a.out format into Extended Tekhex format
1375 before the file is loaded.
1376 Also loads the trap routine, and sets the ES1800 breakpoint on it
1377 filename - the a.out to be loaded
1378 from_tty - says whether to be verbose or not
1379 FIXME Uses emulator overlay memory for trap routine */
1382 es1800_load (filename
, from_tty
)
1390 struct cleanup
*old_chain
;
1391 int es1800_load_format
= 5;
1393 if (es1800_desc
< 0)
1395 printf ("No emulator attached, type emulator-command first\n");
1399 filename
= tilde_expand (filename
);
1400 make_cleanup (free
, filename
);
1402 switch (es1800_load_format
)
1404 case 2: /* Extended Tekhex */
1407 printf ("Converting \"%s\" to Extended Tekhex Format\n", filename
);
1409 sprintf (buf
, "tekhex %s", filename
);
1411 sprintf (loadname
, "out.hex");
1414 case 5: /* Motorola S-rec */
1417 printf ("Converting \"%s\" to Motorola S-record format\n",
1420 /* in the future the source code in copy (part of binutils-1.93) will
1421 be included in this file */
1423 "copy -s \"a.out-sunos-big\" -d \"srec\" %s /tmp/out.hex",
1426 sprintf (loadname
, "/tmp/out.hex");
1430 error ("Downloading format not defined\n");
1433 breakpoint_init_inferior ();
1437 printf ("Downloading \"%s\" to the ES 1800\n",filename
);
1439 if ((instream
= fopen (loadname
, "r")) == NULL
)
1441 perror_with_name ("fopen:");
1444 old_chain
= make_cleanup (fclose
, instream
);
1449 download (instream
, from_tty
, es1800_load_format
);
1451 /* if breakpoint routine is not present anymore we have to check
1452 whether to download a new breakpoint routine or not */
1454 if ((verify_break (es1800_break_vec
) != 0)
1455 && query ("No breakpoint routine in ES 1800 emulator!\nDownload a breakpoint routine to the emulator? "))
1458 printf ("Using break vector 0x%x\n", es1800_break_vec
);
1459 sprintf (buf
, "0x%x ", es1800_break_vec
);
1460 printf ("Give the start address of the breakpoint routine: ");
1461 fgets (buf
+ strlen (buf
), sizeof (buf
) - strlen (buf
), stdin
);
1462 es1800_init_break (buf
, 0);
1465 do_cleanups (old_chain
);
1467 readchar (); /* FIXME I am getting a ^G = 7 after the prompt */
1470 if (fclose (instream
) == EOF
)
1475 if (es1800_load_format
!= 2)
1477 sprintf (buf
, "/usr/bin/rm %s", loadname
);
1481 symbol_file_command (filename
, from_tty
); /* reading symbol table */
1487 #define NUMCPYBYTES 20
1490 bfd_copy (from_bfd
, to_bfd
)
1496 char buf
[NUMCPYBYTES
];
1498 for (p
= from_bfd
->sections
; p
!= NULL
; p
= p
->next
)
1500 printf (" Copying section %s. Size = %x.\n", p
->name
, p
->_cooked_size
);
1501 printf (" vma = %x, offset = %x, output_sec = %x\n",
1502 p
->vma
, p
->output_offset
, p
->output_section
);
1503 new = bfd_make_section (to_bfd
, p
->name
);
1504 if (p
->_cooked_size
&&
1505 !bfd_set_section_size (to_bfd
, new, p
->_cooked_size
))
1507 error ("Wrong BFD size!\n");
1509 if (!bfd_set_section_flags (to_bfd
, new, p
->flags
))
1511 error ("bfd_set_section_flags");
1515 for (i
= 0; (i
+ NUMCPYBYTES
) < p
->_cooked_size
; i
+= NUMCPYBYTES
)
1517 if (!bfd_get_section_contents (from_bfd
, p
, (PTR
) buf
, (file_ptr
) i
,
1518 (bfd_size_type
) NUMCPYBYTES
))
1520 error ("bfd_get_section_contents\n");
1522 if (!bfd_set_section_contents (to_bfd
, new, (PTR
) buf
, (file_ptr
) i
,
1523 (bfd_size_type
) NUMCPYBYTES
))
1525 error ("bfd_set_section_contents\n");
1528 bfd_get_section_contents (from_bfd
, p
, (PTR
) buf
, (file_ptr
) i
,
1529 (bfd_size_type
) (p
->_cooked_size
- i
));
1530 bfd_set_section_contents (to_bfd
, new, (PTR
) buf
,(file_ptr
) i
,
1531 (bfd_size_type
) (p
->_cooked_size
- i
));
1537 /* Start an process on the es1800 and set inferior_pid to the new
1539 execfile - the file to run
1540 args - arguments passed to the program
1541 env - the environment vector to pass */
1544 es1800_create_inferior (execfile
, args
, env
)
1552 struct expression
*expr
;
1553 register struct cleanup
*old_chain
= 0;
1559 error ("Can't pass arguments to remote ES1800 process");
1563 if (query ("Use 'start' as entry point? "))
1565 expr
= parse_c_expression ("start");
1566 old_chain
= make_cleanup (free_current_contents
, &expr
);
1567 val
= evaluate_expression (expr
);
1568 entry_pt
= (val
->location
).address
;
1572 printf ("Enter the program's entry point (in hexadecimal): ");
1573 scanf ("%x", &entry_pt
);
1577 if (execfile
== 0 || exec_bfd
== 0)
1579 error ("No exec file specified");
1582 entry_pt
= (int) bfd_get_start_address (exec_bfd
);
1586 /* Now that we have a child process, make it our target. */
1588 push_target (&es1800_child_ops
);
1590 /* The "process" (board) is already stopped awaiting our commands, and
1591 the program is already downloaded. We just set its PC and go. */
1593 inferior_pid
= pid
; /* Needed for wait_for_inferior below */
1595 clear_proceed_status ();
1597 /* Tell wait_for_inferior that we've started a new process. */
1599 init_wait_for_inferior ();
1601 /* Set up the "saved terminal modes" of the inferior
1602 based on what modes we are starting it with. */
1604 target_terminal_init ();
1606 /* Install inferior's terminal modes. */
1608 target_terminal_inferior ();
1610 /* remote_start (args); */
1611 /* trap_expected = 0; */
1612 /* insert_step_breakpoint (); FIXME, do we need this? */
1614 proceed ((CORE_ADDR
) entry_pt
, -1, 0); /* Let 'er rip... */
1619 /* The process has died, clean up. */
1622 es1800_mourn_inferior ()
1624 remove_breakpoints ();
1625 unpush_target (&es1800_child_ops
);
1626 generic_mourn_inferior (); /* Do all the proper things now */
1629 /* ES1800-protocol specific routines */
1631 /* Keep discarding input from the remote system, until STRING is found.
1632 Let the user break out immediately.
1633 string - the string to expect
1634 nowait - break out if string not the emulator's first respond otherwise
1635 read until string is found (== 0) */
1638 expect (string
, nowait
)
1653 if (c
== toupper (*p
))
1668 printf ("\'%s\' expected\n" , string
);
1669 printf ("char %d is %d", p
- string
, c
);
1675 /* Keep discarding input until we see the prompt. */
1684 /* Read one character */
1688 /* read from stdin */
1696 printf ("readchar, give one character\n");
1699 #if defined (LOG_FILE)
1700 putc (buf
[0] & 0x7f, log_file
);
1703 return (buf
[0] & 0x7f);
1706 #else /* !DEBUG_STDIN */
1708 /* Read a character from the remote system, doing all the fancy
1720 /* termio does the timeout for us. */
1721 read (es1800_desc
, buf
, 1);
1726 while (read (es1800_desc
, buf
, 1) != 1)
1730 error ("Timeout reading from remote system.");
1732 else if (errno
!= EWOULDBLOCK
)
1734 perror_with_name ("remote read");
1740 #if defined (LOG_FILE)
1741 putc (buf
[0] & 0x7f, log_file
);
1745 return (buf
[0] & 0x7f);
1748 #endif /* DEBUG_STDIN */
1751 /* Send a command to the emulator and save the reply.
1752 Report an error if we get an error reply.
1753 string - the es1800 command
1754 buf - containing the emulator reply on return
1755 len - size of buf */
1758 send_with_reply (string
, buf
, len
)
1763 write (es1800_desc
, "\r", 1);
1770 getmessage (buf
, len
);
1774 /* Send the command in STR to the emulator adding \r. check
1775 the echo for consistency.
1776 string - the es1800 command */
1779 send_command (string
)
1783 write (es1800_desc
, "\r", 1);
1793 string - the es1800 command */
1801 fprintf (stderr
, "Sending: %s\n", string
);
1803 write (es1800_desc
, string
, strlen (string
));
1807 /* Read a message from the emulator and store it in BUF.
1808 buf - containing the emulator reply on return
1809 len - size of buf */
1812 getmessage (buf
, len
)
1818 int prompt_found
= 0;
1821 #if defined (LOG_FILE)
1822 /* This is a convenient place to do this. The idea is to do it often
1823 enough that we never lose much data if we terminate abnormally. */
1833 if (len
-- < 2) /* char and terminaling NULL */
1835 error ("input buffer overrun\n");
1840 if ((c
== '>') && (*(bp
- 1) == ' '))
1845 while (!prompt_found
);
1850 fprintf (stderr
,"message received :%s\n", buf
);
1855 download (instream
, from_tty
, format
)
1864 send_command ("SET #2,$1A"); /* reset char = ^Z */
1865 send_command ("SET #3,$11,$13"); /* XON XOFF */
1868 send_command ("SET #26,#2");
1872 send_command ("SET #26,#5"); /* Format=Extended Tekhex */
1874 send_command ("DFB = $10");
1875 send_command ("PUR");
1876 send_command ("CES");
1881 printf (" 0 records loaded...\r");
1883 while (fgets (buf
, 160, instream
))
1888 printf ("%5d\b\b\b\b\b",++i
);
1891 if ((c
= readchar ()) != 006)
1893 error ("expected ACK");
1902 /* Additional commands */
1904 /* Talk directly to the emulator
1905 FIXME, uses busy wait, and is SUNOS (or at least BSD) specific */
1909 es1800_transparent (args
, from_tty
)
1914 struct sgttyb modebl
;
1917 struct sgttyb console_mode_save
;
1918 int console_fc_save
;
1923 char consolebuf
[80];
1925 char es1800_buf
[80];
1929 if (es1800_desc
< 0)
1931 printf ("No emulator attached, type emulator-command first\n");
1936 printf ("You are now communicating directly with the ES 1800 emulator.\n");
1937 printf ("To leave this mode (transparent mode), press ^E.\n");
1942 if ((console
= open ("/dev/tty", O_RDWR
)) == -1)
1944 perror_with_name ("/dev/tty:");
1947 if ((fcflag
= fcntl (console
, F_GETFL
, 0)) == -1)
1949 perror_with_name ("fcntl console");
1952 console_fc_save
= fcflag
;
1953 fcflag
= fcflag
| FNDELAY
;
1955 if (fcntl (console
, F_SETFL
, fcflag
) == -1)
1957 perror_with_name ("fcntl console");
1960 if (ioctl (console
, TIOCGETP
, &modebl
))
1962 perror_with_name ("ioctl console");
1965 console_mode_save
= modebl
;
1966 modebl
.sg_flags
= RAW
;
1968 if (ioctl (console
, TIOCSETP
, &modebl
))
1970 perror_with_name ("ioctl console");
1973 if ((fcflag
= fcntl (es1800_desc
, F_GETFL
, 0)) == -1)
1975 perror_with_name ("fcntl serial");
1978 es1800_fc_save
= fcflag
;
1979 fcflag
= fcflag
| FNDELAY
;
1981 if (fcntl (es1800_desc
, F_SETFL
, fcflag
) == -1)
1983 perror_with_name ("fcntl serial");
1988 cc
= read (console
, inputbuf
, inputcnt
);
1991 if ((*inputbuf
& 0x7f) == 0x05)
1995 for (i
= 0; i
< cc
; )
1997 es1800_buf
[es1800_cnt
++] = inputbuf
[i
++];
1999 if ((cc
= write (es1800_desc
, es1800_buf
, es1800_cnt
)) == -1)
2001 perror_with_name ("FEL! read:");
2004 if (es1800_cnt
&& cc
)
2006 for (i
= 0; i
< es1800_cnt
; i
++)
2008 es1800_buf
[i
] = es1800_buf
[cc
+i
];
2012 else if (errno
!= EWOULDBLOCK
)
2014 perror_with_name ("FEL! read:");
2017 cc
= read (es1800_desc
,inputbuf
,inputcnt
);
2020 for (i
= 0; i
< cc
; )
2022 consolebuf
[consolecnt
++] = inputbuf
[i
++];
2024 if ((cc
= write (console
,consolebuf
,consolecnt
)) == -1)
2026 perror_with_name ("FEL! write:");
2029 if (consolecnt
&& cc
)
2031 for (i
= 0; i
< consolecnt
; i
++)
2033 consolebuf
[i
] = consolebuf
[cc
+i
];
2037 else if (errno
!= EWOULDBLOCK
)
2039 perror_with_name ("FEL! read:");
2043 console_fc_save
= console_fc_save
& !FNDELAY
;
2044 if (fcntl (console
, F_SETFL
, console_fc_save
) == -1)
2046 perror_with_name ("FEL! fcntl");
2049 if (ioctl (console
, TIOCSETP
, &console_mode_save
))
2051 perror_with_name ("FEL! ioctl");
2056 if (fcntl (es1800_desc
, F_SETFL
, es1800_fc_save
) == -1)
2058 perror_with_name ("FEL! fcntl");
2066 es1800_init_break (args
, from_tty
)
2070 CORE_ADDR memaddress
= 0;
2079 error_no_arg ("a trap vector");
2082 if (!(space_index
= strchr (args
, ' ')))
2084 error ("Two arguments needed (trap vector and address of break routine).\n");
2087 *space_index
= '\0';
2089 es1800_break_vec
= strtol (args
, (char **) NULL
, 0);
2090 es1800_break_address
= parse_and_eval_address (space_index
+ 1);
2092 es1800_create_break_insn (es1800_break_insn
, es1800_break_vec
);
2096 send_with_reply ("VBR ", buf
, sizeof (buf
));
2098 for (k
= 0; k
< 4; k
++)
2100 if ((p
[k
*2 + 1] == 0) || (p
[k
*2 + 2] == 0))
2102 error ("Emulator reply is too short: %s", buf
);
2104 base_addr
[k
] = (fromhex (p
[k
*2 + 1]) * 16) + fromhex (p
[k
*2 + 2]);
2106 /* base addr of exception vector table */
2107 memaddress
= *((CORE_ADDR
*) base_addr
);
2110 memaddress
+= (es1800_break_vec
+ 32) * 4; /* address of trap vector */
2112 sprintf (buf
, "@.L%lx=$%lx", memaddress
, es1800_break_address
);
2113 send_command (buf
); /* set the address of the break routine in the */
2116 sprintf (buf
, "@.L%lx=$4E714E71", es1800_break_address
); /* NOP; NOP */
2118 sprintf (buf
, "@.L%lx=$4E714E73", es1800_break_address
+ 4); /* NOP; RTE */
2121 sprintf (buf
, "AC2=$%lx", es1800_break_address
+ 4);
2122 /* breakpoint at es1800-break_address */
2124 send_command ("WHEN AC2 THEN BRK"); /* ie in exception routine */
2128 printf ("Breakpoint (trap $%x) routine at address: %lx\n",
2129 es1800_break_vec
, es1800_break_address
);
2134 es1800_child_open (arg
, from_tty
)
2138 error ("Use the \"run\" command to start a child process.");
2142 es1800_child_detach (args
, from_tty
)
2148 error ("Argument given to \"detach\" when remotely debugging.");
2154 printf ("Ending debugging the process %d.\n", inferior_pid
);
2159 /* Define the target subroutine names */
2161 static struct target_ops es1800_ops
=
2163 "es1800", /* to_shortname */
2165 "Remote serial target in ES1800-emulator protocol",
2167 "Remote debugging on the es1800 emulator via a serial line.\n\
2168 Specify the serial device it is connected to (e.g. /dev/ttya).",
2169 es1800_open
, /* to_open */
2170 es1800_close
, /* to_close */
2171 es1800_attach
, /* to_attach */
2172 es1800_detach
, /* to_detach */
2173 es1800_resume
, /* to_resume */
2175 NULL
, /* to_fetch_registers */
2176 NULL
, /* to_store_registers */
2177 es1800_prepare_to_store
, /* to_prepare_to_store */
2178 es1800_xfer_inferior_memory
, /* to_xfer_memory */
2179 es1800_files_info
, /* to_files_info */
2180 es1800_insert_breakpoint
, /* to_insert_breakpoint */
2181 es1800_remove_breakpoint
, /* to_remove_breakpoint */
2182 NULL
, /* to_terminal_init */
2183 NULL
, /* to_terminal_inferior */
2184 NULL
, /* to_terminal_ours_for_output */
2185 NULL
, /* to_terminal_ours */
2186 NULL
, /* to_terminal_info */
2188 es1800_load
, /* to_load */
2189 NULL
, /* to_lookup_symbol */
2190 es1800_create_inferior
, /* to_create_inferior */
2191 NULL
, /* to_mourn_inferior */
2193 0, /* notice_signals */
2194 core_stratum
, /* to_stratum */
2196 0, /* to_has_all_memory */
2197 1, /* to_has_memory */
2198 0, /* to_has_stack */
2199 0, /* to_has_registers */
2200 0, /* to_has_execution */
2201 NULL
, /* to_sections */
2202 NULL
, /* to_sections_end */
2203 OPS_MAGIC
/* to_magic (always last) */
2206 /* Define the target subroutine names */
2208 static struct target_ops es1800_child_ops
=
2210 "es1800_process", /* to_shortname */
2212 "Remote serial target in ES1800-emulator protocol",
2214 "Remote debugging on the es1800 emulator via a serial line.\n\
2215 Specify the serial device it is connected to (e.g. /dev/ttya).",
2216 es1800_child_open
, /* to_open */
2217 NULL
, /* to_close */
2218 es1800_attach
, /* to_attach */
2219 es1800_child_detach
, /* to_detach */
2220 es1800_resume
, /* to_resume */
2221 es1800_wait
, /* to_wait */
2222 es1800_fetch_register
, /* to_fetch_registers */
2223 es1800_store_register
, /* to_store_registers */
2224 es1800_prepare_to_store
, /* to_prepare_to_store */
2225 es1800_xfer_inferior_memory
, /* to_xfer_memory */
2226 es1800_files_info
, /* to_files_info */
2227 es1800_insert_breakpoint
, /* to_insert_breakpoint */
2228 es1800_remove_breakpoint
, /* to_remove_breakpoint */
2229 NULL
, /* to_terminal_init */
2230 NULL
, /* to_terminal_inferior */
2231 NULL
, /* to_terminal_ours_for_output */
2232 NULL
, /* to_terminal_ours */
2233 NULL
, /* to_terminal_info */
2234 es1800_kill
, /* to_kill */
2235 es1800_load
, /* to_load */
2236 NULL
, /* to_lookup_symbol */
2237 es1800_create_inferior
, /* to_create_inferior */
2238 es1800_mourn_inferior
, /* to_mourn_inferior */
2240 0, /* notice_signals */
2241 process_stratum
, /* to_stratum */
2243 1, /* to_has_all_memory */
2244 1, /* to_has_memory */
2245 1, /* to_has_stack */
2246 1, /* to_has_registers */
2247 1, /* to_has_execution */
2248 NULL
, /* to_sections */
2249 NULL
, /* to_sections_end */
2250 OPS_MAGIC
/* to_magic (always last) */
2253 /* _initialize_es1800 () */
2256 _initialize_es1800 ()
2258 add_target (&es1800_ops
);
2259 add_target (&es1800_child_ops
);
2260 add_com ("transparent", class_support
, es1800_transparent
,
2261 "Start transparent communication with the ES 1800 emulator.");
2262 add_com ("init_break", class_support
, es1800_init_break
,
2263 "Download break routine and initialize break facility on ES 1800");