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