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