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