* defs.h, main.c (catch_errors): Add return_mask arg.
[platform/upstream/binutils.git] / gdb / remote-nindy.c
1 /* Memory-access and commands for remote NINDY process, for GDB.
2    Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3    Contributed by Intel Corporation.  Modified from remote.c by Chris Benenati.
4
5 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
6 WARRANTY.  No author or distributor accepts responsibility to anyone
7 for the consequences of using it or for whether it serves any
8 particular purpose or works at all, unless he says so in writing.
9 Refer to the GDB General Public License for full details.
10
11 Everyone is granted permission to copy, modify and redistribute GDB,
12 but only under the conditions described in the GDB General Public
13 License.  A copy of this license is supposed to have been given to you
14 along with GDB so you can know your rights and responsibilities.  It
15 should be in a file named COPYING.  Among other things, the copyright
16 notice and this notice must be preserved on all copies.
17
18 In other words, go ahead and share GDB, but don't try to stop
19 anyone else from sharing it farther.  Help stamp out software hoarding!
20 */
21
22 /*
23 Except for the data cache routines, this file bears little resemblence
24 to remote.c.  A new (although similar) protocol has been specified, and
25 portions of the code are entirely dependent on having an i80960 with a
26 NINDY ROM monitor at the other end of the line.
27 */
28
29 /*****************************************************************************
30  *
31  * REMOTE COMMUNICATION PROTOCOL BETWEEN GDB960 AND THE NINDY ROM MONITOR.
32  *
33  *
34  * MODES OF OPERATION
35  * ----- -- ---------
36  *      
37  * As far as NINDY is concerned, GDB is always in one of two modes: command
38  * mode or passthrough mode.
39  *
40  * In command mode (the default) pre-defined packets containing requests
41  * are sent by GDB to NINDY.  NINDY never talks except in reponse to a request.
42  *
43  * Once the the user program is started, GDB enters passthrough mode, to give
44  * the user program access to the terminal.  GDB remains in this mode until
45  * NINDY indicates that the program has stopped.
46  *
47  *
48  * PASSTHROUGH MODE
49  * ----------- ----
50  *
51  * GDB writes all input received from the keyboard directly to NINDY, and writes
52  * all characters received from NINDY directly to the monitor.
53  *
54  * Keyboard input is neither buffered nor echoed to the monitor.
55  *
56  * GDB remains in passthrough mode until NINDY sends a single ^P character,
57  * to indicate that the user process has stopped.
58  *
59  * Note:
60  *      GDB assumes NINDY performs a 'flushreg' when the user program stops.
61  *
62  *
63  * COMMAND MODE
64  * ------- ----
65  *
66  * All info (except for message ack and nak) is transferred between gdb
67  * and the remote processor in messages of the following format:
68  *
69  *              <info>#<checksum>
70  *
71  * where 
72  *      #       is a literal character
73  *
74  *      <info>  ASCII information;  all numeric information is in the
75  *              form of hex digits ('0'-'9' and lowercase 'a'-'f').
76  *
77  *      <checksum>
78  *              is a pair of ASCII hex digits representing an 8-bit
79  *              checksum formed by adding together each of the
80  *              characters in <info>.
81  *
82  * The receiver of a message always sends a single character to the sender
83  * to indicate that the checksum was good ('+') or bad ('-');  the sender
84  * re-transmits the entire message over until a '+' is received.
85  *
86  * In response to a command NINDY always sends back either data or
87  * a result code of the form "Xnn", where "nn" are hex digits and "X00"
88  * means no errors.  (Exceptions: the "s" and "c" commands don't respond.)
89  *
90  * SEE THE HEADER OF THE FILE "gdb.c" IN THE NINDY MONITOR SOURCE CODE FOR A
91  * FULL DESCRIPTION OF LEGAL COMMANDS.
92  *
93  * SEE THE FILE "stop.h" IN THE NINDY MONITOR SOURCE CODE FOR A LIST
94  * OF STOP CODES.
95  *
96  ******************************************************************************/
97
98 #include "defs.h"
99 #include <signal.h>
100 #include <sys/types.h>
101 #include <setjmp.h>
102
103 #include "frame.h"
104 #include "inferior.h"
105 #include "symfile.h"
106 #include "target.h"
107 #include "gdbcore.h"
108 #include "command.h"
109 #include "bfd.h"
110 #include "ieee-float.h"
111
112 #include "wait.h"
113 #include <sys/ioctl.h>
114 #include <sys/file.h>
115 #include <ctype.h>
116 #include "nindy-share/ttycntl.h"
117 #include "nindy-share/demux.h"
118 #include "nindy-share/env.h"
119 #include "nindy-share/stop.h"
120
121 extern int unlink();
122 extern char *getenv();
123 extern char *mktemp();
124
125 extern void generic_mourn_inferior ();
126
127 extern struct target_ops nindy_ops;
128 extern FILE *instream;
129 extern struct ext_format ext_format_i960;       /* i960-tdep.c */
130
131 extern char ninStopWhy ();
132
133 int nindy_initial_brk;  /* nonzero if want to send an initial BREAK to nindy */
134 int nindy_old_protocol; /* nonzero if want to use old protocol */
135 char *nindy_ttyname;    /* name of tty to talk to nindy on, or null */
136
137 #define DLE     '\020'  /* Character NINDY sends to indicate user program has
138                          * halted.  */
139 #define TRUE    1
140 #define FALSE   0
141
142 int nindy_fd = 0;       /* Descriptor for I/O to NINDY  */
143 static int have_regs = 0;       /* 1 iff regs read since i960 last halted */
144 static int regs_changed = 0;    /* 1 iff regs were modified since last read */
145
146 extern char *exists();
147
148 static void
149 dcache_flush (), dcache_poke (), dcache_init();
150
151 static int
152 dcache_fetch ();
153
154 static void
155 nindy_fetch_registers PARAMS ((int));
156
157 static void
158 nindy_store_registers PARAMS ((int));
159 \f
160 /* FIXME, we can probably use the normal terminal_inferior stuff here.
161    We have to do terminal_inferior and then set up the passthrough
162    settings initially.  Thereafter, terminal_ours and terminal_inferior
163    will automatically swap the settings around for us.  */
164
165 /* Restore TTY to normal operation */
166
167 static TTY_STRUCT orig_tty;     /* TTY attributes before entering passthrough */
168
169 static void
170 restore_tty()
171 {
172         ioctl( 0, TIOCSETN, &orig_tty );
173 }
174
175
176 /* Recover from ^Z or ^C while remote process is running */
177
178 static void (*old_ctrlc)();    /* Signal handlers before entering passthrough */
179
180 #ifdef SIGTSTP
181 static void (*old_ctrlz)();
182 #endif
183
184 static
185 #ifdef USG
186 void
187 #endif
188 cleanup()
189 {
190         restore_tty();
191         signal(SIGINT, old_ctrlc);
192 #ifdef SIGTSTP
193         signal(SIGTSTP, old_ctrlz);
194 #endif
195         error("\n\nYou may need to reset the 80960 and/or reload your program.\n");
196 }
197 \f
198 /* Clean up anything that needs cleaning when losing control.  */
199
200 static char *savename;
201
202 static void
203 nindy_close (quitting)
204      int quitting;
205 {
206   if (nindy_fd)
207     close (nindy_fd);
208   nindy_fd = 0;
209
210   if (savename)
211     free (savename);
212   savename = 0;
213 }
214
215 /* Open a connection to a remote debugger.   
216    FIXME, there should be a way to specify the various options that are
217    now specified with gdb command-line options.  (baud_rate, old_protocol,
218    and initial_brk)  */
219 void
220 nindy_open (name, from_tty)
221     char *name;         /* "/dev/ttyXX", "ttyXX", or "XX": tty to be opened */
222     int from_tty;
223 {
224
225   if (!name)
226     error_no_arg ("serial port device name");
227
228   target_preopen (from_tty);
229   
230   nindy_close (0);
231
232         have_regs = regs_changed = 0;
233         dcache_init();
234
235         /* Allow user to interrupt the following -- we could hang if
236          * there's no NINDY at the other end of the remote tty.
237          */
238         immediate_quit++;
239         nindy_fd = ninConnect( name, baud_rate? baud_rate: "9600",
240                         nindy_initial_brk, !from_tty, nindy_old_protocol );
241         immediate_quit--;
242
243         if ( nindy_fd < 0 ){
244                 nindy_fd = 0;
245                 error( "Can't open tty '%s'", name );
246         }
247
248         savename = savestring (name, strlen (name));
249         push_target (&nindy_ops);
250         target_fetch_registers(-1);
251 }
252
253 /* User-initiated quit of nindy operations.  */
254
255 static void
256 nindy_detach (name, from_tty)
257      char *name;
258      int from_tty;
259 {
260   if (name)
261     error ("Too many arguments");
262   pop_target ();
263 }
264
265 static void
266 nindy_files_info ()
267 {
268   printf("\tAttached to %s at %s bps%s%s.\n", savename,
269          baud_rate? baud_rate: "9600",
270          nindy_old_protocol? " in old protocol": "",
271          nindy_initial_brk? " with initial break": "");
272 }
273 \f
274 /* Return the number of characters in the buffer before
275    the first DLE character.  */
276
277 static
278 int
279 non_dle( buf, n )
280     char *buf;          /* Character buffer; NOT '\0'-terminated */
281     int n;              /* Number of characters in buffer */
282 {
283         int i;
284
285         for ( i = 0; i < n; i++ ){
286                 if ( buf[i] == DLE ){
287                         break;
288                 }
289         }
290         return i;
291 }
292 \f
293 /* Tell the remote machine to resume.  */
294
295 void
296 nindy_resume (step, siggnal)
297      int step, siggnal;
298 {
299         if (siggnal != 0 && siggnal != stop_signal)
300           error ("Can't send signals to remote NINDY targets.");
301
302         dcache_flush();
303         if ( regs_changed ){
304                 nindy_store_registers (-1);
305                 regs_changed = 0;
306         }
307         have_regs = 0;
308         ninGo( step );
309 }
310
311 /* Wait until the remote machine stops. While waiting, operate in passthrough
312  * mode; i.e., pass everything NINDY sends to stdout, and everything from
313  * stdin to NINDY.
314  *
315  * Return to caller, storing status in 'status' just as `wait' would.
316  */
317
318 static int
319 nindy_wait( status )
320     WAITTYPE *status;
321 {
322         DEMUX_DECL;     /* OS-dependent data needed by DEMUX... macros */
323         char buf[500];  /* FIXME, what is "500" here? */
324         int i, n;
325         unsigned char stop_exit;
326         unsigned char stop_code;
327         TTY_STRUCT tty;
328         long ip_value, fp_value, sp_value;      /* Reg values from stop */
329
330
331         WSETEXIT( (*status), 0 );
332
333         /* OPERATE IN PASSTHROUGH MODE UNTIL NINDY SENDS A DLE CHARACTER */
334
335         /* Save current tty attributes, set up signals to restore them.
336          */
337         ioctl( 0, TIOCGETP, &orig_tty );
338         old_ctrlc = signal( SIGINT, cleanup );
339 #ifdef SIGTSTP
340         old_ctrlz = signal( SIGTSTP, cleanup );
341 #endif
342
343         /* Pass input from keyboard to NINDY as it arrives.
344          * NINDY will interpret <CR> and perform echo.
345          */
346         tty = orig_tty;
347         TTY_NINDYTERM( tty );
348         ioctl( 0, TIOCSETN, &tty );
349
350         while ( 1 ){
351                 /* Go to sleep until there's something for us on either
352                  * the remote port or stdin.
353                  */
354
355                 DEMUX_WAIT( nindy_fd );
356
357                 /* Pass input through to correct place */
358
359                 n = DEMUX_READ( 0, buf, sizeof(buf) );
360                 if ( n ){                               /* Input on stdin */
361                         write( nindy_fd, buf, n );
362                 }
363
364                 n = DEMUX_READ( nindy_fd, buf, sizeof(buf) );
365                 if ( n ){                               /* Input on remote */
366                         /* Write out any characters in buffer preceding DLE */
367                         i = non_dle( buf, n );
368                         if ( i > 0 ){
369                                 write( 1, buf, i );
370                         }
371
372                         if ( i != n ){
373                                 /* There *was* a DLE in the buffer */
374                                 stop_exit = ninStopWhy( &stop_code,
375                                         &ip_value, &fp_value, &sp_value);
376                                 if ( !stop_exit && (stop_code==STOP_SRQ) ){
377                                         immediate_quit++;
378                                         ninSrq();
379                                         immediate_quit--;
380                                 } else {
381                                         /* Get out of loop */
382                                         supply_register (IP_REGNUM, 
383                                                          (char *)&ip_value);
384                                         supply_register (FP_REGNUM, 
385                                                          (char *)&fp_value);
386                                         supply_register (SP_REGNUM, 
387                                                          (char *)&sp_value);
388                                         break;
389                                 }
390                         }
391                 }
392         }
393
394         signal( SIGINT, old_ctrlc );
395 #ifdef SIGTSTP
396         signal( SIGTSTP, old_ctrlz );
397 #endif
398         restore_tty();
399
400         if ( stop_exit ){                       /* User program exited */
401                 WSETEXIT( (*status), stop_code );
402         } else {                                /* Fault or trace */
403                 switch (stop_code){
404                 case STOP_GDB_BPT:
405                 case TRACE_STEP:
406                         /* Make it look like a VAX trace trap */
407                         stop_code = SIGTRAP;
408                         break;
409                 default:
410                         /* The target is not running Unix, and its
411                            faults/traces do not map nicely into Unix signals.
412                            Make sure they do not get confused with Unix signals
413                            by numbering them with values higher than the highest
414                            legal Unix signal.  code in i960_print_fault(),
415                            called via PRINT_RANDOM_SIGNAL, will interpret the
416                            value.  */
417                         stop_code += NSIG;
418                         break;
419                 }
420                 WSETSTOP( (*status), stop_code );
421         }
422         return inferior_pid;
423 }
424
425 /* Read the remote registers into the block REGS.  */
426
427 /* This is the block that ninRegsGet and ninRegsPut handles.  */
428 struct nindy_regs {
429   char  local_regs[16 * 4];
430   char  global_regs[16 * 4];
431   char  pcw_acw[2 * 4];
432   char  ip[4];
433   char  tcw[4];
434   char  fp_as_double[4 * 8];
435 };
436
437 static void
438 nindy_fetch_registers(regno)
439      int regno;
440 {
441   struct nindy_regs nindy_regs;
442   int regnum, inv;
443   double dub;
444
445   immediate_quit++;
446   ninRegsGet( (char *) &nindy_regs );
447   immediate_quit--;
448
449   bcopy (nindy_regs.local_regs, &registers[REGISTER_BYTE (R0_REGNUM)], 16*4);
450   bcopy (nindy_regs.global_regs, &registers[REGISTER_BYTE (G0_REGNUM)], 16*4);
451   bcopy (nindy_regs.pcw_acw, &registers[REGISTER_BYTE (PCW_REGNUM)], 2*4);
452   bcopy (nindy_regs.ip, &registers[REGISTER_BYTE (IP_REGNUM)], 1*4);
453   bcopy (nindy_regs.tcw, &registers[REGISTER_BYTE (TCW_REGNUM)], 1*4);
454   for (regnum = FP0_REGNUM; regnum < FP0_REGNUM + 4; regnum++) {
455     dub = unpack_double (builtin_type_double,
456                          &nindy_regs.fp_as_double[8 * (regnum - FP0_REGNUM)],
457                          &inv);
458     /* dub now in host byte order */
459     double_to_ieee_extended (&ext_format_i960, &dub,
460                              &registers[REGISTER_BYTE (regnum)]);
461   }
462
463   registers_fetched ();
464 }
465
466 static void
467 nindy_prepare_to_store()
468 {
469   /* Fetch all regs if they aren't already here.  */
470   read_register_bytes (0, NULL, REGISTER_BYTES);
471 }
472
473 static void
474 nindy_store_registers(regno)
475      int regno;
476 {
477   struct nindy_regs nindy_regs;
478   int regnum, inv;
479   double dub;
480
481   bcopy (&registers[REGISTER_BYTE (R0_REGNUM)], nindy_regs.local_regs,  16*4);
482   bcopy (&registers[REGISTER_BYTE (G0_REGNUM)], nindy_regs.global_regs, 16*4);
483   bcopy (&registers[REGISTER_BYTE (PCW_REGNUM)], nindy_regs.pcw_acw,     2*4);
484   bcopy (&registers[REGISTER_BYTE (IP_REGNUM)], nindy_regs.ip,           1*4);
485   bcopy (&registers[REGISTER_BYTE (TCW_REGNUM)], nindy_regs.tcw,         1*4);
486   /* Float regs.  Only works on IEEE_FLOAT hosts.  FIXME!  */
487   for (regnum = FP0_REGNUM; regnum < FP0_REGNUM + 4; regnum++) {
488     ieee_extended_to_double (&ext_format_i960,
489                              &registers[REGISTER_BYTE (regnum)], &dub);
490     /* dub now in host byte order */
491     /* FIXME-someday, the arguments to unpack_double are backward.
492        It expects a target double and returns a host; we pass the opposite.
493        This mostly works but not quite.  */
494     dub = unpack_double (builtin_type_double, (char *)&dub, &inv);
495     /* dub now in target byte order */
496     bcopy ((char *)&dub, &nindy_regs.fp_as_double[8 * (regnum - FP0_REGNUM)],
497         8);
498   }
499
500   immediate_quit++;
501   ninRegsPut( (char *) &nindy_regs );
502   immediate_quit--;
503 }
504
505 /* Read a word from remote address ADDR and return it.
506  * This goes through the data cache.
507  */
508 int
509 nindy_fetch_word (addr)
510      CORE_ADDR addr;
511 {
512         return dcache_fetch (addr);
513 }
514
515 /* Write a word WORD into remote address ADDR.
516    This goes through the data cache.  */
517
518 void
519 nindy_store_word (addr, word)
520      CORE_ADDR addr;
521      int word;
522 {
523         dcache_poke (addr, word);
524 }
525
526 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
527    to debugger memory starting at MYADDR.   Copy to inferior if
528    WRITE is nonzero.  Returns the length copied.
529
530    This is stolen almost directly from infptrace.c's child_xfer_memory,
531    which also deals with a word-oriented memory interface.  Sometime,
532    FIXME, rewrite this to not use the word-oriented routines.  */
533
534 int
535 nindy_xfer_inferior_memory(memaddr, myaddr, len, write, target)
536      CORE_ADDR memaddr;
537      char *myaddr;
538      int len;
539      int write;
540      struct target_ops *target;                 /* ignored */
541 {
542   register int i;
543   /* Round starting address down to longword boundary.  */
544   register CORE_ADDR addr = memaddr & - sizeof (int);
545   /* Round ending address up; get number of longwords that makes.  */
546   register int count
547     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
548   /* Allocate buffer of that many longwords.  */
549   register int *buffer = (int *) alloca (count * sizeof (int));
550
551   if (write)
552     {
553       /* Fill start and end extra bytes of buffer with existing memory data.  */
554
555       if (addr != memaddr || len < (int)sizeof (int)) {
556         /* Need part of initial word -- fetch it.  */
557         buffer[0] = nindy_fetch_word (addr);
558       }
559
560       if (count > 1)            /* FIXME, avoid if even boundary */
561         {
562           buffer[count - 1]
563             = nindy_fetch_word (addr + (count - 1) * sizeof (int));
564         }
565
566       /* Copy data to be written over corresponding part of buffer */
567
568       bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
569
570       /* Write the entire buffer.  */
571
572       for (i = 0; i < count; i++, addr += sizeof (int))
573         {
574           errno = 0;
575           nindy_store_word (addr, buffer[i]);
576           if (errno)
577             return 0;
578         }
579     }
580   else
581     {
582       /* Read all the longwords */
583       for (i = 0; i < count; i++, addr += sizeof (int))
584         {
585           errno = 0;
586           buffer[i] = nindy_fetch_word (addr);
587           if (errno)
588             return 0;
589           QUIT;
590         }
591
592       /* Copy appropriate bytes out of the buffer.  */
593       bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
594     }
595   return len;
596 }
597 \f
598 /* The data cache records all the data read from the remote machine
599    since the last time it stopped.
600
601    Each cache block holds 16 bytes of data
602    starting at a multiple-of-16 address.  */
603
604 #define DCACHE_SIZE 64          /* Number of cache blocks */
605
606 struct dcache_block {
607         struct dcache_block *next, *last;
608         unsigned int addr;      /* Address for which data is recorded.  */
609         int data[4];
610 };
611
612 struct dcache_block dcache_free, dcache_valid;
613
614 /* Free all the data cache blocks, thus discarding all cached data.  */ 
615 static
616 void
617 dcache_flush ()
618 {
619   register struct dcache_block *db;
620
621   while ((db = dcache_valid.next) != &dcache_valid)
622     {
623       remque (db);
624       insque (db, &dcache_free);
625     }
626 }
627
628 /*
629  * If addr is present in the dcache, return the address of the block
630  * containing it.
631  */
632 static
633 struct dcache_block *
634 dcache_hit (addr)
635      unsigned int addr;
636 {
637   register struct dcache_block *db;
638
639   if (addr & 3)
640     abort ();
641
642   /* Search all cache blocks for one that is at this address.  */
643   db = dcache_valid.next;
644   while (db != &dcache_valid)
645     {
646       if ((addr & 0xfffffff0) == db->addr)
647         return db;
648       db = db->next;
649     }
650   return NULL;
651 }
652
653 /*  Return the int data at address ADDR in dcache block DC.  */
654 static
655 int
656 dcache_value (db, addr)
657      struct dcache_block *db;
658      unsigned int addr;
659 {
660   if (addr & 3)
661     abort ();
662   return (db->data[(addr>>2)&3]);
663 }
664
665 /* Get a free cache block, put or keep it on the valid list,
666    and return its address.  The caller should store into the block
667    the address and data that it describes, then remque it from the
668    free list and insert it into the valid list.  This procedure
669    prevents errors from creeping in if a ninMemGet is interrupted
670    (which used to put garbage blocks in the valid list...).  */
671 static
672 struct dcache_block *
673 dcache_alloc ()
674 {
675   register struct dcache_block *db;
676
677   if ((db = dcache_free.next) == &dcache_free)
678     {
679       /* If we can't get one from the free list, take last valid and put
680          it on the free list.  */
681       db = dcache_valid.last;
682       remque (db);
683       insque (db, &dcache_free);
684     }
685
686   remque (db);
687   insque (db, &dcache_valid);
688   return (db);
689 }
690
691 /* Return the contents of the word at address ADDR in the remote machine,
692    using the data cache.  */
693 static
694 int
695 dcache_fetch (addr)
696      CORE_ADDR addr;
697 {
698   register struct dcache_block *db;
699
700   db = dcache_hit (addr);
701   if (db == 0)
702     {
703       db = dcache_alloc ();
704       immediate_quit++;
705       ninMemGet(addr & ~0xf, (unsigned char *)db->data, 16);
706       immediate_quit--;
707       db->addr = addr & ~0xf;
708       remque (db);                      /* Off the free list */
709       insque (db, &dcache_valid);       /* On the valid list */
710     }
711   return (dcache_value (db, addr));
712 }
713
714 /* Write the word at ADDR both in the data cache and in the remote machine.  */
715 static void
716 dcache_poke (addr, data)
717      CORE_ADDR addr;
718      int data;
719 {
720   register struct dcache_block *db;
721
722   /* First make sure the word is IN the cache.  DB is its cache block.  */
723   db = dcache_hit (addr);
724   if (db == 0)
725     {
726       db = dcache_alloc ();
727       immediate_quit++;
728       ninMemGet(addr & ~0xf, (unsigned char *)db->data, 16);
729       immediate_quit--;
730       db->addr = addr & ~0xf;
731       remque (db);                      /* Off the free list */
732       insque (db, &dcache_valid);       /* On the valid list */
733     }
734
735   /* Modify the word in the cache.  */
736   db->data[(addr>>2)&3] = data;
737
738   /* Send the changed word.  */
739   immediate_quit++;
740   ninMemPut(addr, (unsigned char *)&data, 4);
741   immediate_quit--;
742 }
743
744 /* The cache itself. */
745 struct dcache_block the_cache[DCACHE_SIZE];
746
747 /* Initialize the data cache.  */
748 static void
749 dcache_init ()
750 {
751   register i;
752   register struct dcache_block *db;
753
754   db = the_cache;
755   dcache_free.next = dcache_free.last = &dcache_free;
756   dcache_valid.next = dcache_valid.last = &dcache_valid;
757   for (i=0;i<DCACHE_SIZE;i++,db++)
758     insque (db, &dcache_free);
759 }
760
761
762 static void
763 nindy_create_inferior (execfile, args, env)
764      char *execfile;
765      char *args;
766      char **env;
767 {
768   int entry_pt;
769   int pid;
770
771   if (args && *args)
772     error ("Can't pass arguments to remote NINDY process");
773
774   if (execfile == 0 || exec_bfd == 0)
775     error ("No exec file specified");
776
777   entry_pt = (int) bfd_get_start_address (exec_bfd);
778
779   pid = 42;
780
781 #ifdef CREATE_INFERIOR_HOOK
782   CREATE_INFERIOR_HOOK (pid);
783 #endif  
784
785 /* The "process" (board) is already stopped awaiting our commands, and
786    the program is already downloaded.  We just set its PC and go.  */
787
788   inferior_pid = pid;           /* Needed for wait_for_inferior below */
789
790   clear_proceed_status ();
791
792   /* Tell wait_for_inferior that we've started a new process.  */
793   init_wait_for_inferior ();
794
795   /* Set up the "saved terminal modes" of the inferior
796      based on what modes we are starting it with.  */
797   target_terminal_init ();
798
799   /* Install inferior's terminal modes.  */
800   target_terminal_inferior ();
801
802   /* insert_step_breakpoint ();  FIXME, do we need this?  */
803   proceed ((CORE_ADDR)entry_pt, -1, 0);         /* Let 'er rip... */
804 }
805
806 static void
807 reset_command(args, from_tty)
808      char *args;
809      int from_tty;
810 {
811         if ( !nindy_fd ){
812             error( "No target system to reset -- use 'target nindy' command.");
813         }
814         if ( query("Really reset the target system?",0,0) ){
815                 send_break( nindy_fd );
816                 tty_flush( nindy_fd );
817         }
818 }
819
820 void
821 nindy_kill (args, from_tty)
822      char *args;
823      int from_tty;
824 {
825   return;               /* Ignore attempts to kill target system */
826 }
827
828 /* Clean up when a program exits.
829
830    The program actually lives on in the remote processor's RAM, and may be
831    run again without a download.  Don't leave it full of breakpoint
832    instructions.  */
833
834 void
835 nindy_mourn_inferior ()
836 {
837   remove_breakpoints ();
838   unpush_target (&nindy_ops);
839   generic_mourn_inferior ();    /* Do all the proper things now */
840 }
841 \f
842 /* Pass the args the way catch_errors wants them.  */
843 static int
844 nindy_open_stub (arg)
845      char *arg;
846 {
847   nindy_open (arg, 1);
848   return 1;
849 }
850
851 static int
852 load_stub (arg)
853      char *arg;
854 {
855   target_load (arg, 1);
856   return 1;
857 }
858
859 /* This routine is run as a hook, just before the main command loop is
860    entered.  If gdb is configured for the i960, but has not had its
861    nindy target specified yet, this will loop prompting the user to do so.
862
863    Unlike the loop provided by Intel, we actually let the user get out
864    of this with a RETURN.  This is useful when e.g. simply examining
865    an i960 object file on the host system.  */
866
867 void
868 nindy_before_main_loop ()
869 {
870   char ttyname[100];
871   char *p, *p2;
872
873   while (current_target != &nindy_ops) { /* remote tty not specified yet */
874         if ( instream == stdin ){
875                 printf("\nAttach /dev/ttyNN -- specify NN, or \"quit\" to quit:  ");
876                 fflush( stdout );
877         }
878         fgets( ttyname, sizeof(ttyname)-1, stdin );
879
880         /* Strip leading and trailing whitespace */
881         for ( p = ttyname; isspace(*p); p++ ){
882                 ;
883         }
884         if ( *p == '\0' ){
885                 return;         /* User just hit spaces or return, wants out */
886         }
887         for ( p2= p; !isspace(*p2) && (*p2 != '\0'); p2++ ){
888                 ;
889         }
890         *p2= '\0';
891         if ( STREQ("quit",p) ){
892                 exit(1);
893         }
894
895         if (catch_errors (nindy_open_stub, p, "", RETURN_MASK_ALL))
896           {
897             /* Now that we have a tty open for talking to the remote machine,
898                download the executable file if one was specified.  */
899             if (exec_bfd)
900               {
901                 catch_errors (load_stub, bfd_get_filename (exec_bfd), "",
902                               RETURN_MASK_ALL);
903               }
904           }
905   }
906 }
907 \f
908 /* Define the target subroutine names */
909
910 struct target_ops nindy_ops = {
911         "nindy", "Remote serial target in i960 NINDY-specific protocol",
912         "Use a remote i960 system running NINDY connected by a serial line.\n\
913 Specify the name of the device the serial line is connected to.\n\
914 The speed (baud rate), whether to use the old NINDY protocol,\n\
915 and whether to send a break on startup, are controlled by options\n\
916 specified when you started GDB.",
917         nindy_open, nindy_close,
918         0,
919         nindy_detach,
920         nindy_resume,
921         nindy_wait,
922         nindy_fetch_registers, nindy_store_registers,
923         nindy_prepare_to_store,
924         nindy_xfer_inferior_memory, nindy_files_info,
925         0, 0, /* insert_breakpoint, remove_breakpoint, */
926         0, 0, 0, 0, 0,  /* Terminal crud */
927         nindy_kill,
928         generic_load,
929         0, /* lookup_symbol */
930         nindy_create_inferior,
931         nindy_mourn_inferior,
932         0,              /* can_run */
933         0, /* notice_signals */
934         process_stratum, 0, /* next */
935         1, 1, 1, 1, 1,  /* all mem, mem, stack, regs, exec */
936         0, 0,                   /* Section pointers */
937         OPS_MAGIC,              /* Always the last thing */
938 };
939
940 void
941 _initialize_nindy ()
942 {
943   add_target (&nindy_ops);
944   add_com ("reset", class_obscure, reset_command,
945            "Send a 'break' to the remote target system.\n\
946 Only useful if the target has been equipped with a circuit\n\
947 to perform a hard reset when a break is detected.");
948 }