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 byJohan 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"
111 #include <sys/types.h>
116 /* External variables referenced. */
118 extern bfd
*exec_bfd
;
120 /* Prototypes for local functions */
123 es1800_child_detach
PARAMS ((char *, int));
126 es1800_child_open
PARAMS ((char *, int));
129 es1800_transparent
PARAMS ((char *, int));
132 es1800_create_inferior
PARAMS ((char *, char *, char **));
135 es1800_load
PARAMS ((char *, int));
138 es1800_kill
PARAMS ((void));
141 verify_break
PARAMS ((int));
144 es1800_remove_breakpoint
PARAMS ((CORE_ADDR
, char *));
147 es1800_insert_breakpoint
PARAMS ((CORE_ADDR
, char *));
150 es1800_files_info
PARAMS ((struct target_ops
*));
153 es1800_xfer_inferior_memory
PARAMS ((CORE_ADDR
, char *, int, int,
154 struct target_ops
*));
157 es1800_prepare_to_store
PARAMS ((void));
160 es1800_wait
PARAMS ((WAITTYPE
*));
163 es1800_resume
PARAMS ((int, int));
166 es1800_detach
PARAMS ((char *, int));
169 es1800_attach
PARAMS ((char *, int));
172 damn_b
PARAMS ((char *));
175 es1800_open
PARAMS ((char *, int));
178 es1800_timer
PARAMS ((void));
181 es1800_reset
PARAMS ((char *));
184 es1800_request_quit
PARAMS ((void));
187 readchar
PARAMS ((void));
190 expect
PARAMS ((char *, int));
193 expect_prompt
PARAMS ((void));
196 download
PARAMS ((FILE *, int, int));
200 bfd_copy
PARAMS ((bfd
*, bfd
*));
204 get_break_addr
PARAMS ((int, CORE_ADDR
*));
207 fromhex
PARAMS ((int));
210 tohex
PARAMS ((int));
213 es1800_close
PARAMS ((int));
216 es1800_fetch_registers
PARAMS ((void));
219 es1800_fetch_register
PARAMS ((int));
222 es1800_store_register
PARAMS ((int));
225 es1800_read_bytes
PARAMS ((CORE_ADDR
, char *, int));
228 es1800_write_bytes
PARAMS ((CORE_ADDR
, char *, int));
231 send_with_reply
PARAMS ((char *, char *, int));
234 send_command
PARAMS ((char *));
237 send
PARAMS ((char *));
240 getmessage
PARAMS ((char *, int));
243 es1800_mourn_inferior
PARAMS ((void));
246 es1800_create_break_insn
PARAMS ((char *, int));
249 es1800_init_break
PARAMS ((char *, int));
251 /* Local variables */
253 #define LOG_FILE "es1800.log"
254 #if defined (LOG_FILE)
255 static FILE *log_file
;
258 static struct target_ops es1800_ops
; /* Forward decl */
259 static struct target_ops es1800_child_ops
; /* Forward decl */
262 static int timeout
= 100;
263 static char *savename
; /* Name of i/o device used */
264 static TERMINAL es1800_sg_save
; /* Save stty state */
265 static int es1800_fc_save
; /* Save fcntl state */
267 /* indicates that the emulator uses 32-bit data-adress (68020-mode)
268 instead of 24-bit (68000 -mode) */
272 #define MODE (m68020 ? "M68020" : "M68000" )
273 #define ES1800_BREAK_VEC (0xf)
275 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
276 es1800_open knows that we don't have a file open when the program
279 static int es1800_desc
= -1;
282 #define HDRLEN sizeof("@.BAAAAAAAA=$VV\r")
284 /* Maximum number of bytes to read/write at once. The value here
285 is chosen to fill up a packet. */
287 #define MAXBUFBYTES ((PBUFSIZ-150)*16/75 )
289 static int es1800_break_vec
= 0;
290 static char es1800_break_insn
[2];
291 static long es1800_break_address
;
292 static void (*old_sigint
)(); /* Old signal-handler for sigint */
293 static jmp_buf interrupt
;
295 /* Local signalhandler to allow breaking tranfers or program run.
296 Rely on global variables: old_sigint(), interrupt */
299 es1800_request_quit ()
301 /* restore original signalhandler */
302 signal (SIGINT
, old_sigint
);
303 longjmp (interrupt
, 1);
308 Sending reset character(octal 32) to emulator.
309 quit - return to '(esgdb)' prompt or continue */
319 printf ("\nResetting emulator... ");
321 strcpy (buf
, "\032");
331 /* Called when SIGALRM signal sent due to alarm() timeout.
332 Rely on global variables: timeout */
341 printf ("es1800_timer called\n");
346 #endif /* HAVE_TERMIO */
349 /* Open a connection to a remote debugger and push the new target
350 onto the stack. Check if the emulator is responding and find out
351 what kind of processor the emulator is connected to.
352 Initiate the breakpoint handling in the emulator.
354 name - the filename used for communication (ex. '/dev/tta')
355 from_tty - says whether to be verbose or not */
358 es1800_open (name
, from_tty
)
370 if (!name
) /* no device name given in target command */
372 error_no_arg ("serial port device name");
374 baudrate
= baud_rate
? baud_rate
: "19200"; /* default baudrate = 19200 */
376 target_preopen (from_tty
);
379 /* open the device and configure it for communication */
383 es1800_desc
= open (name
, O_RDWR
);
386 perror_with_name (name
);
388 savename
= savestring (name
, strlen (name
));
390 if (ioctl (es1800_desc
, TIOCGETP
, &sg
) == -1)
392 perror_with_name (name
);
396 if ((fcflag
= fcntl (es1800_desc
, F_GETFL
, 0)) == -1)
398 perror_with_name ("fcntl serial");
400 es1800_fc_save
= fcflag
;
402 fcflag
= (fcflag
& (_FREAD
| _FWRITE
)); /* mask out any funny stuff */
403 if (fcntl (es1800_desc
, F_SETFL
, fcflag
) == -1)
405 perror_with_name ("fcntl serial");
409 sg
.c_cc
[VMIN
] = 0; /* read with timeout. */
410 sg
.c_cc
[VTIME
] = timeout
* 10;
411 sg
.c_lflag
&= ~(ICANON
| ECHO
);
412 sg
.c_cflag
= (sg
.c_cflag
& ~CBAUD
) | damn_b (baudrate
);
414 sg
.sg_ispeed
= damn_b (baudrate
);
415 sg
.sg_ospeed
= damn_b (baudrate
);
416 sg
.sg_flags
= CBREAK
+TANDEM
;
419 if ((ioctl (es1800_desc
, TIOCSETP
, &sg
)) == -1)
421 perror ("es1800_open: error in ioctl");
424 #endif /* DEBUG_STDIN */
426 push_target (&es1800_ops
); /* Switch to using remote target now */
429 printf ("Remote ES1800 debugging using %s\n", name
);
434 #ifndef NO_SIGINTERRUPT
436 /* Cause SIGALRM's to make reads fail with EINTR instead of resuming
439 if (siginterrupt (SIGALRM
, 1) != 0)
441 perror ("es1800_open: error in siginterrupt");
444 #endif /* NO_SIGINTERRUPT */
446 /* Set up read timeout timer. */
448 if ((void(*)()) signal (SIGALRM
, es1800_timer
) == (void(*)()) -1)
450 perror ("es1800_open: error in signal");
453 #endif /* HAVE_TERMIO */
456 #if defined (LOG_FILE)
458 log_file
= fopen (LOG_FILE
, "w");
459 if (log_file
== NULL
)
461 perror_with_name (LOG_FILE
);
464 #endif /* LOG_FILE */
466 /* Hello? Are you there?, also check mode */
468 /* send_with_reply( "DB 0 TO 1", buf, sizeof(buf)); */
469 /* for (p = buf, i = 0; *p++ =='0';) */ /* count the number of zeros */
473 getmessage (buf
, sizeof (buf
)); /* send reset character */
477 printf ("Checking mode.... ");
479 /* m68020 = (i==8); */ /* if eight zeros then we are in m68020 mode */
481 /* What kind of processor am i talking to ?*/
483 while (*p
++ != '\n') {;}
484 while (*p
++ != '\n') {;}
485 while (*p
++ != '\n') {;}
486 for (i
= 0; i
< 20; i
++, p
++) {;}
487 m68020
= !strncmp (p
, "68020", 5);
490 printf ("You are in %s(%c%c%c%c%c)-mode\n", MODE
, p
[0], p
[1], p
[2],
494 /* if no init_break statement is present in .gdb file we have to check
495 whether to download a breakpoint routine or not */
498 if ((es1800_break_vec
== 0) || (verify_break (es1800_break_vec
) != 0)
499 && query ("No breakpoint routine in ES 1800 emulator!\nDownload a breakpoint routine to the emulator? "))
501 CORE_ADDR memaddress
;
502 printf ("Give the start address of the breakpoint routine: ");
503 scanf ("%li", &memaddress
);
504 es1800_init_break ((es1800_break_vec
? es1800_break_vec
:
505 ES1800_BREAK_VEC
), memaddress
);
511 /* Close out all files and local state before this target loses control.
512 quitting - are we quitting gdb now? */
515 es1800_close (quitting
)
518 if (es1800_desc
>= 0)
520 printf ("\nClosing connection to emulator...\n");
521 ioctl (es1800_desc
, TIOCSETP
, &es1800_sg_save
);
522 fcntl (es1800_desc
,F_SETFL
, es1800_fc_save
);
526 if (savename
!= NULL
)
532 #if defined (LOG_FILE)
534 if (log_file
!= NULL
)
536 if (ferror (log_file
))
538 printf ("Error writing log file.\n");
540 if (fclose (log_file
) != 0)
542 printf ("Error closing log file.\n");
547 #endif /* LOG_FILE */
553 Translate baud rates from integers to damn B_codes. Unix should
554 have outgrown this crap years ago, but even POSIX wouldn't buck it.
555 rate - the baudrate given as a string
556 return value: the baudrate as a B_code */
595 for (i
= 0; baudtab
[i
].rate
!= 0; i
++)
597 if (!strcmp (rate
, baudtab
[i
].rate
))
599 return (baudtab
[i
].damn_b
);
602 error ("Illegal baudrate");
606 /* Attaches to a process on the target side
607 proc_id - the id of the process to be attached.
608 from_tty - says whether to be verbose or not */
611 es1800_attach (args
, from_tty
)
615 error ("Cannot attach to pid %s, this feature is not implemented yet.",
620 /* Takes a program previously attached to and detaches it.
621 We better not have left any breakpoints
622 in the program or it'll die when it hits one.
623 Close the open connection to the remote debugger.
624 Use this when you want to detach and do something else
627 args - arguments given to the 'detach' command
628 from_tty - says whether to be verbose or not */
631 es1800_detach (args
, from_tty
)
637 error ("Argument given to \"detach\" when remotely debugging.");
642 printf ("Ending es1800 remote debugging.\n");
647 /* Tell the remote machine to resume.
648 step - single-step or run free
649 siggnal - the signal value to be given to the target (0 = no signal) */
652 es1800_resume (step
, siggnal
)
660 error ("Can't send signals to a remote system.");
664 strcpy (buf
,"STP\r");
669 send_command ("RBK");
673 /* Wait until the remote machine stops, then return,
674 storing status in STATUS just as `wait' would.
681 unsigned char buf
[PBUFSIZ
];
682 int old_timeout
= timeout
;
684 WSETEXIT ((*status
), 0);
685 timeout
= 0; /* Don't time out -- user program is running. */
686 if (!setjmp (interrupt
))
688 old_sigint
= signal (SIGINT
, es1800_request_quit
);
691 getmessage (buf
, sizeof(buf
));
692 if (strncmp ( buf
, "\r\n* BREAK *", 11) == 0)
694 WSETSTOP ((*status
), SIGTRAP
);
695 send_command ("STP"); /* Restore stack and PC and such */
698 send_command ("STP");
702 if (strncmp (buf
, "STP\r\n ", 6) == 0)
704 WSETSTOP ((*status
), SIGTRAP
);
707 if (buf
[strlen (buf
) - 2] == 'R')
709 printf ("Unexpected emulator reply: \n%s\n", buf
);
713 printf ("Unexpected stop: \n%s\n", buf
);
714 WSETSTOP ((*status
), SIGQUIT
);
722 printf ("\nStopping emulator...");
723 if (!setjmp (interrupt
))
725 old_sigint
= signal (SIGINT
, es1800_request_quit
);
726 send_command ("STP");
727 printf (" emulator stopped\n");
728 WSETSTOP ((*status
), SIGINT
);
733 es1800_reset ((char*) 1);
736 signal (SIGINT
, old_sigint
);
737 timeout
= old_timeout
;
742 /* Fetch register values from remote machine.
743 regno - the register to be fetched (fetch all registers if -1) */
746 es1800_fetch_register (regno
)
753 static char regtab
[18][4] =
755 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
756 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
760 if ((regno
< 15) || (regno
== 16) || (regno
== 17))
763 send_with_reply (regtab
[regno
], buf
, sizeof (buf
));
765 for (k
= 0; k
< 4; k
++)
767 if ((p
[k
*2 + 1] == 0) || (p
[k
*2 + 2] == 0))
769 error ("Emulator reply is too short: %s", buf
);
771 registers
[r
++] = (fromhex (p
[k
*2 + 1]) * 16) + fromhex (p
[k
*2 + 2]);
776 es1800_fetch_registers ();
780 /* Read the remote registers into REGISTERS.
781 Always fetches all registers. */
784 es1800_fetch_registers ()
787 char SR_buf
[PBUFSIZ
];
793 send_with_reply ("DR", buf
, sizeof (buf
));
795 /* Reply is edited to a string that describes registers byte by byte,
796 each byte encoded as two hex characters. */
801 /* parsing row one - D0-D7-registers */
803 while (*p
++ != '\n') {;}
804 for (i
= 4; i
< 70; i
+= (i
== 39 ? 3 : 1))
806 for (k
= 0; k
< 4; k
++)
808 if (p
[i
+0] == 0 || p
[i
+1] == 0)
810 error ("Emulator reply is too short: %s", buf
);
812 registers
[r
++] = (fromhex (p
[i
+0]) * 16) + fromhex (p
[i
+1]);
818 /* parsing row two - A0-A6-registers */
820 while (*p
++ != '\n') {;}
821 for (i
= 4; i
< 61; i
+= (i
== 39 ? 3 : 1))
823 for (k
= 0; k
< 4; k
++)
825 if (p
[i
+0] == 0 || p
[i
+1] == 0)
827 error ("Emulator reply is too short: %s", buf
);
829 registers
[r
++] = (fromhex (p
[i
+0])) * 16 + fromhex (p
[i
+1]);
835 while (*p
++ != '\n') {;}
837 /* fetch SSP-, SR- and PC-registers */
839 /* first - check STATUS-word and decide which stackpointer to use */
841 send_with_reply ("SR", SR_buf
, sizeof (SR_buf
));
847 if (*p
== '3') /* use masterstackpointer MSP */
849 send_with_reply ("MSP", buf
, sizeof (buf
));
851 else if (*p
== '2') /* use interruptstackpointer ISP */
853 send_with_reply ("ISP", buf
, sizeof (buf
));
855 else /* use userstackpointer USP */
857 send_with_reply ("USP", buf
, sizeof (buf
));
860 for (k
= 0; k
<4; k
++)
862 if (p
[k
*2+1] == 0 || p
[k
*2+2] == 0)
864 error ("Emulator reply is too short: %s", buf
);
866 registers
[r
++] = fromhex (buf
[k
*2+1]) * 16 + fromhex (buf
[k
*2+2]);
870 for (k
= 0; k
< 4; k
++)
872 if (p
[k
*2+1] == 0 || p
[k
*2+2] == 0)
874 error ("Emulator reply is too short: %s", buf
);
877 fromhex (SR_buf
[k
*2+1]) * 16 + fromhex (SR_buf
[k
*2+2]);
879 send_with_reply ("PC", buf
, sizeof (buf
));
881 for (k
= 0; k
<4; k
++)
883 if (p
[k
*2+1] == 0 || p
[k
*2+2] == 0)
885 error ("Emulator reply is too short: %s", buf
);
887 registers
[r
++] = fromhex (buf
[k
*2+1]) * 16 + fromhex (buf
[k
*2+2]);
890 else /* 68000-mode */
892 if (*p
== '2') /* use supervisorstackpointer SSP */
894 send_with_reply ("SSP", buf
, sizeof (buf
));
896 else /* use userstackpointer USP */
898 send_with_reply ("USP", buf
, sizeof (buf
));
901 /* fetch STACKPOINTER */
904 for (k
= 0; k
< 4; k
++)
906 if (p
[k
*2 + 1] == 0 || p
[k
*2 + 2] == 0)
908 error ("Emulator reply is too short: %s", buf
);
910 registers
[r
++] = fromhex (buf
[k
*2+1]) * 16 + fromhex (buf
[k
*2+2]);
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
);
923 fromhex (SR_buf
[k
*2+1]) * 16 + fromhex (SR_buf
[k
*2+2]);
928 send_with_reply ("PC", buf
, sizeof (buf
));
930 for (k
= 0; k
< 4; k
++)
932 if (p
[k
*2+1] == 0 || p
[k
*2+2] == 0)
934 error ("Emulator reply is too short: %s", buf
);
936 registers
[r
++] = fromhex (buf
[k
*2+1]) * 16 + fromhex (buf
[k
*2+2]);
941 /* Store register value, located in REGISTER, on the target processor.
942 regno - the register-number of the register to store
943 (-1 means store them all)
944 FIXME: Return errno value. */
947 es1800_store_register(regno
)
951 static char regtab
[18][4] =
953 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
954 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
959 char SR_buf
[PBUFSIZ
];
960 char stack_pointer
[4];
967 r
= (unsigned char *) registers
;
969 if (regno
== -1) /* write all registers */
974 else /* write one register */
981 if ((regno
== -1) || (regno
== 15))
983 /* fetch current status */
984 send_with_reply ("SR", SR_buf
, sizeof (SR_buf
));
989 if (*p
== '3') /* use masterstackpointer MSP */
991 strcpy (stack_pointer
,"MSP");
995 if (*p
== '2') /* use interruptstackpointer ISP */
997 strcpy (stack_pointer
,"ISP");
1001 strcpy (stack_pointer
,"USP"); /* use userstackpointer USP */
1005 else /* 68000-mode */
1007 if (*p
== '2') /* use supervisorstackpointer SSP */
1009 strcpy (stack_pointer
,"SSP");
1013 strcpy (stack_pointer
,"USP");/* use userstackpointer USP */
1016 strcpy (regtab
[15],stack_pointer
);
1019 for (i
= j
; i
<k
; i
++)
1021 buf
[0] = regtab
[i
][0];
1022 buf
[1] = regtab
[i
][1];
1023 buf
[2] = regtab
[i
][2];
1026 buf
[5] = tohex ((*r
>> 4) & 0x0f);
1027 buf
[6] = tohex (*r
++ & 0x0f);
1028 buf
[7] = tohex ((*r
>> 4) & 0x0f);
1029 buf
[8] = tohex (*r
++ & 0x0f);
1030 buf
[9] = tohex ((*r
>> 4) & 0x0f);
1031 buf
[10] = tohex (*r
++ & 0x0f);
1032 buf
[11] = tohex ((*r
>> 4) & 0x0f);
1033 buf
[12] = tohex (*r
++ & 0x0f);
1036 send_with_reply (buf
, buf
, sizeof (buf
)); /* FIXME, reply not used? */
1041 /* Prepare to store registers. */
1044 es1800_prepare_to_store ()
1046 /* Do nothing, since we can store individual regs */
1049 /* Convert hex digit A to a number. */
1055 if (a
>= '0' && a
<= '9')
1059 else if (a
>= 'a' && a
<= 'f')
1061 return a
- 'a' + 10;
1063 else if (a
>= 'A' && a
<= 'F')
1065 return a
- 'A' + 10;
1069 error ("Reply contains invalid hex digit");
1075 /* Convert number NIB to a hex digit. */
1087 return ('A' + nib
- 10);
1091 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1092 to or from debugger address MYADDR. Write to inferior if WRITE is
1093 nonzero. Returns length of data written or read; 0 for error.
1095 memaddr - the target's address
1096 myaddr - gdb's address
1097 len - number of bytes
1098 write - write if != 0 otherwise read */
1101 es1800_xfer_inferior_memory (memaddr
, myaddr
, len
, write
, tops
)
1106 struct target_ops
*tops
; /* Unused */
1113 xfersize
= len
> MAXBUFBYTES
? MAXBUFBYTES
: len
;
1116 es1800_write_bytes (memaddr
, myaddr
, xfersize
);
1120 es1800_read_bytes (memaddr
, myaddr
, xfersize
);
1122 memaddr
+= xfersize
;
1126 return (origlen
); /* no error possible */
1130 /* Write memory data directly to the emulator.
1131 This does not inform the data cache; the data cache uses this.
1132 MEMADDR is the address in the remote memory space.
1133 MYADDR is the address of the buffer in our space.
1134 LEN is the number of bytes.
1136 memaddr - the target's address
1137 myaddr - gdb's address
1138 len - number of bytes */
1141 es1800_write_bytes (memaddr
, myaddr
, len
)
1151 for (i
= 0; i
< len
; i
++)
1153 sprintf (buf
, "@.B$%x=$%x", memaddr
+i
, (*p
++) & 0xff);
1154 send_with_reply (buf
, buf
, sizeof (buf
)); /* FIXME send_command? */
1159 /* Read memory data directly from the emulator.
1160 This does not use the data cache; the data cache uses this.
1162 memaddr - the target's address
1163 myaddr - gdb's address
1164 len - number of bytes */
1167 es1800_read_bytes (memaddr
, myaddr
, len
)
1172 static int DB_tab
[16] = {8,11,14,17,20,23,26,29,34,37,40,43,46,49,52,55};
1179 if (len
> PBUFSIZ
/ 2 - 1)
1184 if (len
== 1) /* The emulator does not like expressions like: */
1186 len
= 2; /* DB.B $20018 TO $20018 */
1189 /* Reply describes registers byte by byte, each byte encoded as two hex
1192 sprintf (buf
, "DB.B $%x TO $%x", memaddr
, memaddr
+len
-1);
1193 send_with_reply (buf
, buf
, sizeof (buf
));
1195 low_addr
= memaddr
&0x0f;
1196 for (i
= low_addr
; i
< low_addr
+ len
; i
++)
1198 if ((!(i
% 16)) && i
)
1199 { /* if (i = 16,32,48) */
1200 while (*p
++!='\n') {;}
1203 p
= b
+ DB_tab
[i
%16] + (m68020
? 2 : 0);
1204 if (p
[0] == 32 || p
[1] == 32)
1206 error ("Emulator reply is too short: %s", buf
);
1208 myaddr
[i
-low_addr
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
1212 /* Information about the current target */
1215 es1800_files_info (tops
)
1216 struct target_ops
*tops
; /* Unused */
1218 printf ("ES1800 Attached to %s at %d baud in %s mode\n", savename
, 19200,
1223 /* We read the contents of the target location and stash it,
1224 then overwrite it with a breakpoint instruction.
1226 addr - is the target location in the target machine.
1227 contents_cache - is a pointer to memory allocated for saving the target contents.
1228 It is guaranteed by the caller to be long enough to save sizeof
1231 FIXME: This size is target_arch dependent and should be available in
1232 the target_arch transfer vector, if we ever have one... */
1235 es1800_insert_breakpoint (addr
, contents_cache
)
1237 char *contents_cache
;
1241 val
= target_read_memory (addr
, contents_cache
, sizeof (es1800_break_insn
));
1245 val
= target_write_memory (addr
, es1800_break_insn
,
1246 sizeof (es1800_break_insn
));
1253 /* Write back the stashed instruction
1255 addr - is the target location in the target machine.
1256 contents_cache - is a pointer to memory allocated for saving the target contents.
1257 It is guaranteed by the caller to be long enough to save sizeof
1258 BREAKPOINT bytes. */
1261 es1800_remove_breakpoint (addr
, contents_cache
)
1263 char *contents_cache
;
1266 return (target_write_memory (addr
, contents_cache
,
1267 sizeof (es1800_break_insn
)));
1270 /* create_break_insn ()
1271 Primitive datastructures containing the es1800 breakpoint instruction */
1274 es1800_create_break_insn (ins
, vec
)
1287 Seach for breakpoint routine in emulator memory.
1288 returns non-zero on failure
1289 vec - trap vector used for breakpoints */
1295 CORE_ADDR memaddress
;
1297 char *instr
= "NqNqNqNs"; /* breakpoint routine */
1300 get_break_addr (vec
, &memaddress
);
1304 status
= target_read_memory (memaddress
, buf
, 8);
1307 memory_error (status
, memaddress
);
1309 return (strcmp (instr
, buf
));
1315 /* get_break_addr ()
1316 find address of breakpint routine
1317 vec - trap vector used for breakpoints
1318 addrp - store the address here */
1321 get_break_addr (vec
, addrp
)
1325 CORE_ADDR memaddress
= 0;
1334 send_with_reply ("VBR ", buf
, sizeof (buf
));
1336 for (k
= 0; k
< 4; k
++)
1338 if ((p
[k
*2 + 1] == 0) || (p
[k
*2 + 2] == 0))
1340 error ("Emulator reply is too short: %s", buf
);
1342 base_addr
[k
] = (fromhex (p
[k
*2 + 1]) * 16) + fromhex (p
[k
*2 + 2]);
1344 /* base addr of exception vector table */
1345 memaddress
= *((CORE_ADDR
*) base_addr
);
1348 memaddress
+= (vec
+ 32) * 4; /* address of trap vector */
1349 status
= target_read_memory (memaddress
, (char *) addrp
, 4);
1352 memory_error (status
, memaddress
);
1357 /* Kill an inferior process */
1362 if (inferior_pid
!= 0)
1365 es1800_mourn_inferior ();
1370 /* Load a file to the ES1800 emulator.
1371 Converts the file from a.out format into Extended Tekhex format
1372 before the file is loaded.
1373 Also loads the trap routine, and sets the ES1800 breakpoint on it
1374 filename - the a.out to be loaded
1375 from_tty - says whether to be verbose or not
1376 FIXME Uses emulator overlay memory for trap routine */
1379 es1800_load (filename
, from_tty
)
1387 struct cleanup
*old_chain
;
1388 int es1800_load_format
= 5;
1390 if (es1800_desc
< 0)
1392 printf ("No emulator attached, type emulator-command first\n");
1396 filename
= tilde_expand (filename
);
1397 make_cleanup (free
, filename
);
1399 switch (es1800_load_format
)
1401 case 2: /* Extended Tekhex */
1404 printf ("Converting \"%s\" to Extended Tekhex Format\n", filename
);
1406 sprintf (buf
, "tekhex %s", filename
);
1408 sprintf (loadname
, "out.hex");
1411 case 5: /* Motorola S-rec */
1414 printf ("Converting \"%s\" to Motorola S-record format\n",
1417 /* in the future the source code in copy (part of binutils-1.93) will
1418 be included in this file */
1420 "copy -s \"a.out-sunos-big\" -d \"srec\" %s /tmp/out.hex",
1423 sprintf (loadname
, "/tmp/out.hex");
1427 error ("Downloading format not defined\n");
1430 mark_breakpoints_out ();
1434 printf ("Downloading \"%s\" to the ES 1800\n",filename
);
1436 if ((instream
= fopen (loadname
, "r")) == NULL
)
1438 perror_with_name ("fopen:");
1441 old_chain
= make_cleanup (fclose
, instream
);
1446 download (instream
, from_tty
, es1800_load_format
);
1448 /* if breakpoint routine is not present anymore we have to check
1449 whether to download a new breakpoint routine or not */
1451 if ((verify_break (es1800_break_vec
) != 0)
1452 && query ("No breakpoint routine in ES 1800 emulator!\nDownload a breakpoint routine to the emulator? "))
1455 printf ("Using break vector 0x%x\n", es1800_break_vec
);
1456 sprintf (buf
, "0x%x ", es1800_break_vec
);
1457 printf ("Give the start address of the breakpoint routine: ");
1458 fgets (buf
+ strlen (buf
), sizeof (buf
) - strlen (buf
), stdin
);
1459 es1800_init_break (buf
, 0);
1462 do_cleanups (old_chain
);
1464 readchar (); /* FIXME I am getting a ^G = 7 after the prompt */
1467 if (fclose (instream
) == EOF
)
1472 if (es1800_load_format
!= 2)
1474 sprintf (buf
, "/usr/bin/rm %s", loadname
);
1478 symbol_file_command (filename
, from_tty
); /* reading symbol table */
1484 #define NUMCPYBYTES 20
1487 bfd_copy (from_bfd
, to_bfd
)
1493 char buf
[NUMCPYBYTES
];
1495 for (p
= from_bfd
->sections
; p
!= NULL
; p
= p
->next
)
1497 printf (" Copying section %s. Size = %x.\n", p
->name
, p
->_cooked_size
);
1498 printf (" vma = %x, offset = %x, output_sec = %x\n",
1499 p
->vma
, p
->output_offset
, p
->output_section
);
1500 new = bfd_make_section (to_bfd
, p
->name
);
1501 if (p
->_cooked_size
&&
1502 !bfd_set_section_size (to_bfd
, new, p
->_cooked_size
))
1504 error ("Wrong BFD size!\n");
1506 if (!bfd_set_section_flags (to_bfd
, new, p
->flags
))
1508 error ("bfd_set_section_flags");
1512 for (i
= 0; (i
+ NUMCPYBYTES
) < p
->_cooked_size
; i
+= NUMCPYBYTES
)
1514 if (!bfd_get_section_contents (from_bfd
, p
, (PTR
) buf
, (file_ptr
) i
,
1515 (bfd_size_type
) NUMCPYBYTES
))
1517 error ("bfd_get_section_contents\n");
1519 if (!bfd_set_section_contents (to_bfd
, new, (PTR
) buf
, (file_ptr
) i
,
1520 (bfd_size_type
) NUMCPYBYTES
))
1522 error ("bfd_set_section_contents\n");
1525 bfd_get_section_contents (from_bfd
, p
, (PTR
) buf
, (file_ptr
) i
,
1526 (bfd_size_type
) (p
->_cooked_size
- i
));
1527 bfd_set_section_contents (to_bfd
, new, (PTR
) buf
,(file_ptr
) i
,
1528 (bfd_size_type
) (p
->_cooked_size
- i
));
1534 /* Start an process on the es1800 and set inferior_pid to the new
1536 execfile - the file to run
1537 args - arguments passed to the program
1538 env - the environment vector to pass */
1541 es1800_create_inferior (execfile
, args
, env
)
1549 struct expression
*expr
;
1550 register struct cleanup
*old_chain
= 0;
1556 error ("Can't pass arguments to remote ES1800 process");
1560 if (query ("Use 'start' as entry point? "))
1562 expr
= parse_c_expression ("start");
1563 old_chain
= make_cleanup (free_current_contents
, &expr
);
1564 val
= evaluate_expression (expr
);
1565 entry_pt
= (val
->location
).address
;
1569 printf ("Enter the program's entry point (in hexadecimal): ");
1570 scanf ("%x", &entry_pt
);
1574 if (execfile
== 0 || exec_bfd
== 0)
1576 error ("No exec file specified");
1579 entry_pt
= (int) bfd_get_start_address (exec_bfd
);
1583 /* Now that we have a child process, make it our target. */
1585 push_target (&es1800_child_ops
);
1587 /* The "process" (board) is already stopped awaiting our commands, and
1588 the program is already downloaded. We just set its PC and go. */
1590 inferior_pid
= pid
; /* Needed for wait_for_inferior below */
1592 clear_proceed_status ();
1594 /* Tell wait_for_inferior that we've started a new process. */
1596 init_wait_for_inferior ();
1598 /* Set up the "saved terminal modes" of the inferior
1599 based on what modes we are starting it with. */
1601 target_terminal_init ();
1603 /* Install inferior's terminal modes. */
1605 target_terminal_inferior ();
1607 /* remote_start (args); */
1608 /* trap_expected = 0; */
1609 /* insert_step_breakpoint (); FIXME, do we need this? */
1611 proceed ((CORE_ADDR
) entry_pt
, -1, 0); /* Let 'er rip... */
1616 /* The process has died, clean up. */
1619 es1800_mourn_inferior ()
1621 remove_breakpoints ();
1622 unpush_target (&es1800_child_ops
);
1623 generic_mourn_inferior (); /* Do all the proper things now */
1626 /* ES1800-protocol specific routines */
1628 /* Keep discarding input from the remote system, until STRING is found.
1629 Let the user break out immediately.
1630 string - the string to expect
1631 nowait - break out if string not the emulator's first respond otherwise
1632 read until string is found (== 0) */
1635 expect (string
, nowait
)
1650 if (c
== toupper (*p
))
1665 printf ("\'%s\' expected\n" , string
);
1666 printf ("char %d is %d", p
- string
, c
);
1672 /* Keep discarding input until we see the prompt. */
1681 /* Read one character */
1685 /* read from stdin */
1693 printf ("readchar, give one character\n");
1696 #if defined (LOG_FILE)
1697 putc (buf
[0] & 0x7f, log_file
);
1700 return (buf
[0] & 0x7f);
1703 #else /* !DEBUG_STDIN */
1705 /* Read a character from the remote system, doing all the fancy
1717 /* termio does the timeout for us. */
1718 read (es1800_desc
, buf
, 1);
1723 while (read (es1800_desc
, buf
, 1) != 1)
1727 error ("Timeout reading from remote system.");
1729 else if (errno
!= EWOULDBLOCK
)
1731 perror_with_name ("remote read");
1737 #if defined (LOG_FILE)
1738 putc (buf
[0] & 0x7f, log_file
);
1742 return (buf
[0] & 0x7f);
1745 #endif /* DEBUG_STDIN */
1748 /* Send a command to the emulator and save the reply.
1749 Report an error if we get an error reply.
1750 string - the es1800 command
1751 buf - containing the emulator reply on return
1752 len - size of buf */
1755 send_with_reply (string
, buf
, len
)
1760 write (es1800_desc
, "\r", 1);
1767 getmessage (buf
, len
);
1771 /* Send the command in STR to the emulator adding \r. check
1772 the echo for consistency.
1773 string - the es1800 command */
1776 send_command (string
)
1780 write (es1800_desc
, "\r", 1);
1790 string - the es1800 command */
1798 fprintf (stderr
, "Sending: %s\n", string
);
1800 write (es1800_desc
, string
, strlen (string
));
1804 /* Read a message from the emulator and store it in BUF.
1805 buf - containing the emulator reply on return
1806 len - size of buf */
1809 getmessage (buf
, len
)
1815 int prompt_found
= 0;
1818 #if defined (LOG_FILE)
1819 /* This is a convenient place to do this. The idea is to do it often
1820 enough that we never lose much data if we terminate abnormally. */
1830 if (len
-- < 2) /* char and terminaling NULL */
1832 error ("input buffer overrun\n");
1837 if ((c
== '>') && (*(bp
- 1) == ' '))
1842 while (!prompt_found
);
1847 fprintf (stderr
,"message received :%s\n", buf
);
1852 download (instream
, from_tty
, format
)
1861 send_command ("SET #2,$1A"); /* reset char = ^Z */
1862 send_command ("SET #3,$11,$13"); /* XON XOFF */
1865 send_command ("SET #26,#2");
1869 send_command ("SET #26,#5"); /* Format=Extended Tekhex */
1871 send_command ("DFB = $10");
1872 send_command ("PUR");
1873 send_command ("CES");
1878 printf (" 0 records loaded...\r");
1880 while (fgets (buf
, 160, instream
))
1885 printf ("%5d\b\b\b\b\b",++i
);
1888 if ((c
= readchar ()) != 006)
1890 error ("expected ACK");
1899 /* Additional commands */
1901 /* Talk directly to the emulator
1902 FIXME, uses busy wait, and is SUNOS (or at least BSD) specific */
1906 es1800_transparent (args
, from_tty
)
1911 struct sgttyb modebl
;
1914 struct sgttyb console_mode_save
;
1915 int console_fc_save
;
1920 char consolebuf
[80];
1922 char es1800_buf
[80];
1926 if (es1800_desc
< 0)
1928 printf ("No emulator attached, type emulator-command first\n");
1933 printf ("You are now communicating directly with the ES 1800 emulator.\n");
1934 printf ("To leave this mode (transparent mode), press ^E.\n");
1939 if ((console
= open ("/dev/tty", O_RDWR
)) == -1)
1941 perror_with_name ("/dev/tty:");
1944 if ((fcflag
= fcntl (console
, F_GETFL
, 0)) == -1)
1946 perror_with_name ("fcntl console");
1949 console_fc_save
= fcflag
;
1950 fcflag
= fcflag
| FNDELAY
;
1952 if (fcntl (console
, F_SETFL
, fcflag
) == -1)
1954 perror_with_name ("fcntl console");
1957 if (ioctl (console
, TIOCGETP
, &modebl
))
1959 perror_with_name ("ioctl console");
1962 console_mode_save
= modebl
;
1963 modebl
.sg_flags
= RAW
;
1965 if (ioctl (console
, TIOCSETP
, &modebl
))
1967 perror_with_name ("ioctl console");
1970 if ((fcflag
= fcntl (es1800_desc
, F_GETFL
, 0)) == -1)
1972 perror_with_name ("fcntl serial");
1975 es1800_fc_save
= fcflag
;
1976 fcflag
= fcflag
| FNDELAY
;
1978 if (fcntl (es1800_desc
, F_SETFL
, fcflag
) == -1)
1980 perror_with_name ("fcntl serial");
1985 cc
= read (console
, inputbuf
, inputcnt
);
1988 if ((*inputbuf
& 0x7f) == 0x05)
1992 for (i
= 0; i
< cc
; )
1994 es1800_buf
[es1800_cnt
++] = inputbuf
[i
++];
1996 if ((cc
= write (es1800_desc
, es1800_buf
, es1800_cnt
)) == -1)
1998 perror_with_name ("FEL! read:");
2001 if (es1800_cnt
&& cc
)
2003 for (i
= 0; i
< es1800_cnt
; i
++)
2005 es1800_buf
[i
] = es1800_buf
[cc
+i
];
2009 else if (errno
!= EWOULDBLOCK
)
2011 perror_with_name ("FEL! read:");
2014 cc
= read (es1800_desc
,inputbuf
,inputcnt
);
2017 for (i
= 0; i
< cc
; )
2019 consolebuf
[consolecnt
++] = inputbuf
[i
++];
2021 if ((cc
= write (console
,consolebuf
,consolecnt
)) == -1)
2023 perror_with_name ("FEL! write:");
2026 if (consolecnt
&& cc
)
2028 for (i
= 0; i
< consolecnt
; i
++)
2030 consolebuf
[i
] = consolebuf
[cc
+i
];
2034 else if (errno
!= EWOULDBLOCK
)
2036 perror_with_name ("FEL! read:");
2040 console_fc_save
= console_fc_save
& !FNDELAY
;
2041 if (fcntl (console
, F_SETFL
, console_fc_save
) == -1)
2043 perror_with_name ("FEL! fcntl");
2046 if (ioctl (console
, TIOCSETP
, &console_mode_save
))
2048 perror_with_name ("FEL! ioctl");
2053 if (fcntl (es1800_desc
, F_SETFL
, es1800_fc_save
) == -1)
2055 perror_with_name ("FEL! fcntl");
2063 es1800_init_break (args
, from_tty
)
2067 CORE_ADDR memaddress
= 0;
2076 error_no_arg ("a trap vector");
2079 if (!(space_index
= strchr (args
, ' ')))
2081 error ("Two arguments needed (trap vector and address of break routine).\n");
2084 *space_index
= '\0';
2086 es1800_break_vec
= strtol (args
, (char **) NULL
, 0);
2087 es1800_break_address
= parse_and_eval_address (space_index
+ 1);
2089 es1800_create_break_insn (es1800_break_insn
, es1800_break_vec
);
2093 send_with_reply ("VBR ", buf
, sizeof (buf
));
2095 for (k
= 0; k
< 4; k
++)
2097 if ((p
[k
*2 + 1] == 0) || (p
[k
*2 + 2] == 0))
2099 error ("Emulator reply is too short: %s", buf
);
2101 base_addr
[k
] = (fromhex (p
[k
*2 + 1]) * 16) + fromhex (p
[k
*2 + 2]);
2103 /* base addr of exception vector table */
2104 memaddress
= *((CORE_ADDR
*) base_addr
);
2107 memaddress
+= (es1800_break_vec
+ 32) * 4; /* address of trap vector */
2109 sprintf (buf
, "@.L%lx=$%lx", memaddress
, es1800_break_address
);
2110 send_command (buf
); /* set the address of the break routine in the */
2113 sprintf (buf
, "@.L%lx=$4E714E71", es1800_break_address
); /* NOP; NOP */
2115 sprintf (buf
, "@.L%lx=$4E714E73", es1800_break_address
+ 4); /* NOP; RTE */
2118 sprintf (buf
, "AC2=$%lx", es1800_break_address
+ 4);
2119 /* breakpoint at es1800-break_address */
2121 send_command ("WHEN AC2 THEN BRK"); /* ie in exception routine */
2125 printf ("Breakpoint (trap $%x) routine at address: %lx\n",
2126 es1800_break_vec
, es1800_break_address
);
2131 es1800_child_open (arg
, from_tty
)
2135 error ("Use the \"run\" command to start a child process.");
2139 es1800_child_detach (args
, from_tty
)
2145 error ("Argument given to \"detach\" when remotely debugging.");
2151 printf ("Ending debugging the process %d.\n", inferior_pid
);
2156 /* Define the target subroutine names */
2158 static struct target_ops es1800_ops
=
2160 "es1800", /* to_shortname */
2162 "Remote serial target in ES1800-emulator protocol",
2164 "Remote debugging on the es1800 emulator via a serial line.\n\
2165 Specify the serial device it is connected to (e.g. /dev/ttya).",
2166 es1800_open
, /* to_open */
2167 es1800_close
, /* to_close */
2168 es1800_attach
, /* to_attach */
2169 es1800_detach
, /* to_detach */
2170 es1800_resume
, /* to_resume */
2172 NULL
, /* to_fetch_registers */
2173 NULL
, /* to_store_registers */
2174 es1800_prepare_to_store
, /* to_prepare_to_store */
2175 NULL
, /* to_convert_to_virtual */
2176 NULL
, /* to_convert_from_virtual */
2177 es1800_xfer_inferior_memory
, /* to_xfer_memory */
2178 es1800_files_info
, /* to_files_info */
2179 es1800_insert_breakpoint
, /* to_insert_breakpoint */
2180 es1800_remove_breakpoint
, /* to_remove_breakpoint */
2181 NULL
, /* to_terminal_init */
2182 NULL
, /* to_terminal_inferior */
2183 NULL
, /* to_terminal_ours_for_output */
2184 NULL
, /* to_terminal_ours */
2185 NULL
, /* to_terminal_info */
2187 es1800_load
, /* to_load */
2188 NULL
, /* to_lookup_symbol */
2189 es1800_create_inferior
, /* to_create_inferior */
2190 NULL
, /* to_mourn_inferior */
2191 core_stratum
, /* to_stratum */
2193 0, /* to_has_all_memory */
2194 1, /* to_has_memory */
2195 0, /* to_has_stack */
2196 0, /* to_has_registers */
2197 0, /* to_has_execution */
2198 NULL
, /* to_sections */
2199 NULL
, /* to_sections_end */
2200 OPS_MAGIC
/* to_magic (always last) */
2203 /* Define the target subroutine names */
2205 static struct target_ops es1800_child_ops
=
2207 "es1800_process", /* to_shortname */
2209 "Remote serial target in ES1800-emulator protocol",
2211 "Remote debugging on the es1800 emulator via a serial line.\n\
2212 Specify the serial device it is connected to (e.g. /dev/ttya).",
2213 es1800_child_open
, /* to_open */
2214 NULL
, /* to_close */
2215 es1800_attach
, /* to_attach */
2216 es1800_child_detach
, /* to_detach */
2217 es1800_resume
, /* to_resume */
2218 es1800_wait
, /* to_wait */
2219 es1800_fetch_register
, /* to_fetch_registers */
2220 es1800_store_register
, /* to_store_registers */
2221 es1800_prepare_to_store
, /* to_prepare_to_store */
2222 NULL
, /* to_convert_to_virtual */
2223 NULL
, /* to_convert_from_virtual */
2224 es1800_xfer_inferior_memory
, /* to_xfer_memory */
2225 es1800_files_info
, /* to_files_info */
2226 es1800_insert_breakpoint
, /* to_insert_breakpoint */
2227 es1800_remove_breakpoint
, /* to_remove_breakpoint */
2228 NULL
, /* to_terminal_init */
2229 NULL
, /* to_terminal_inferior */
2230 NULL
, /* to_terminal_ours_for_output */
2231 NULL
, /* to_terminal_ours */
2232 NULL
, /* to_terminal_info */
2233 es1800_kill
, /* to_kill */
2234 es1800_load
, /* to_load */
2235 NULL
, /* to_lookup_symbol */
2236 es1800_create_inferior
, /* to_create_inferior */
2237 es1800_mourn_inferior
, /* to_mourn_inferior */
2238 process_stratum
, /* to_stratum */
2240 1, /* to_has_all_memory */
2241 1, /* to_has_memory */
2242 1, /* to_has_stack */
2243 1, /* to_has_registers */
2244 1, /* to_has_execution */
2245 NULL
, /* to_sections */
2246 NULL
, /* to_sections_end */
2247 OPS_MAGIC
/* to_magic (always last) */
2250 /* _initialize_es1800 () */
2253 _initialize_es1800 ()
2255 add_target (&es1800_ops
);
2256 add_target (&es1800_child_ops
);
2257 add_com ("transparent", class_support
, es1800_transparent
,
2258 "Start transparent communication with the ES 1800 emulator.");
2259 add_com ("init_break", class_support
, es1800_init_break
,
2260 "Download break routine and initialize break facility on ES 1800");