* remote-nindy.c: Removed declaration of coffstrip.
[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 "target.h"
106 #include "gdbcore.h"
107 #include "command.h"
108 #include "bfd.h"
109 #include "ieee-float.h"
110
111 #include "wait.h"
112 #include <sys/ioctl.h>
113 #include <sys/file.h>
114 #include <ctype.h>
115 #include "nindy-share/ttycntl.h"
116 #include "nindy-share/demux.h"
117 #include "nindy-share/env.h"
118 #include "nindy-share/stop.h"
119
120 extern int unlink();
121 extern char *getenv();
122 extern char *mktemp();
123
124 extern void generic_mourn_inferior ();
125
126 extern struct target_ops nindy_ops;
127 extern jmp_buf to_top_level;
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 /******************************************************************************
275  * remote_load:
276  *      Download an object file to the remote system by invoking the "comm960"
277  *      utility.  We look for "comm960" in $G960BIN, $G960BASE/bin, and
278  *      DEFAULT_BASE/bin/HOST/bin where
279  *              DEFAULT_BASE is defined in env.h, and
280  *              HOST must be defined on the compiler invocation line.
281  ******************************************************************************/
282
283 static void
284 nindy_load( filename, from_tty )
285     char *filename;
286     int from_tty;
287 {
288   asection *s;
289   /* Can't do unix style forking on a VMS system, so we'll use bfd to do
290      all the work for us 
291      */
292
293   bfd *file = bfd_openr(filename,0);
294   if (!file) 
295   {
296     perror_with_name(filename);
297     return;
298   }
299   
300   if (!bfd_check_format(file, bfd_object)) 
301   {
302     error("can't prove it's an object file\n");
303     return;
304   }
305   
306   for ( s = file->sections; s; s=s->next) 
307   {
308     if (s->flags & SEC_LOAD) 
309     {
310       char *buffer = xmalloc(s->_raw_size);
311       bfd_get_section_contents(file, s, buffer, 0, s->_raw_size);
312       printf("Loading section %s, size %x vma %x\n",
313              s->name, 
314              s->_raw_size,
315              s->vma);
316       ninMemPut(s->vma, buffer, s->_raw_size);
317       free(buffer);
318     }
319   }
320   bfd_close(file);
321 }
322
323 /* Return the number of characters in the buffer before the first DLE character.
324  */
325
326 static
327 int
328 non_dle( buf, n )
329     char *buf;          /* Character buffer; NOT '\0'-terminated */
330     int n;              /* Number of characters in buffer */
331 {
332         int i;
333
334         for ( i = 0; i < n; i++ ){
335                 if ( buf[i] == DLE ){
336                         break;
337                 }
338         }
339         return i;
340 }
341 \f
342 /* Tell the remote machine to resume.  */
343
344 void
345 nindy_resume (step, siggnal)
346      int step, siggnal;
347 {
348         if (siggnal != 0 && siggnal != stop_signal)
349           error ("Can't send signals to remote NINDY targets.");
350
351         dcache_flush();
352         if ( regs_changed ){
353                 nindy_store_registers (-1);
354                 regs_changed = 0;
355         }
356         have_regs = 0;
357         ninGo( step );
358 }
359
360 /* Wait until the remote machine stops. While waiting, operate in passthrough
361  * mode; i.e., pass everything NINDY sends to stdout, and everything from
362  * stdin to NINDY.
363  *
364  * Return to caller, storing status in 'status' just as `wait' would.
365  */
366
367 static int
368 nindy_wait( status )
369     WAITTYPE *status;
370 {
371         DEMUX_DECL;     /* OS-dependent data needed by DEMUX... macros */
372         char buf[500];  /* FIXME, what is "500" here? */
373         int i, n;
374         unsigned char stop_exit;
375         unsigned char stop_code;
376         TTY_STRUCT tty;
377         long ip_value, fp_value, sp_value;      /* Reg values from stop */
378
379
380         WSETEXIT( (*status), 0 );
381
382         /* OPERATE IN PASSTHROUGH MODE UNTIL NINDY SENDS A DLE CHARACTER */
383
384         /* Save current tty attributes, set up signals to restore them.
385          */
386         ioctl( 0, TIOCGETP, &orig_tty );
387         old_ctrlc = signal( SIGINT, cleanup );
388 #ifdef SIGTSTP
389         old_ctrlz = signal( SIGTSTP, cleanup );
390 #endif
391
392         /* Pass input from keyboard to NINDY as it arrives.
393          * NINDY will interpret <CR> and perform echo.
394          */
395         tty = orig_tty;
396         TTY_NINDYTERM( tty );
397         ioctl( 0, TIOCSETN, &tty );
398
399         while ( 1 ){
400                 /* Go to sleep until there's something for us on either
401                  * the remote port or stdin.
402                  */
403
404                 DEMUX_WAIT( nindy_fd );
405
406                 /* Pass input through to correct place */
407
408                 n = DEMUX_READ( 0, buf, sizeof(buf) );
409                 if ( n ){                               /* Input on stdin */
410                         write( nindy_fd, buf, n );
411                 }
412
413                 n = DEMUX_READ( nindy_fd, buf, sizeof(buf) );
414                 if ( n ){                               /* Input on remote */
415                         /* Write out any characters in buffer preceding DLE */
416                         i = non_dle( buf, n );
417                         if ( i > 0 ){
418                                 write( 1, buf, i );
419                         }
420
421                         if ( i != n ){
422                                 /* There *was* a DLE in the buffer */
423                                 stop_exit = ninStopWhy( &stop_code,
424                                         &ip_value, &fp_value, &sp_value);
425                                 if ( !stop_exit && (stop_code==STOP_SRQ) ){
426                                         immediate_quit++;
427                                         ninSrq();
428                                         immediate_quit--;
429                                 } else {
430                                         /* Get out of loop */
431                                         supply_register (IP_REGNUM, 
432                                                          (char *)&ip_value);
433                                         supply_register (FP_REGNUM, 
434                                                          (char *)&fp_value);
435                                         supply_register (SP_REGNUM, 
436                                                          (char *)&sp_value);
437                                         break;
438                                 }
439                         }
440                 }
441         }
442
443         signal( SIGINT, old_ctrlc );
444 #ifdef SIGTSTP
445         signal( SIGTSTP, old_ctrlz );
446 #endif
447         restore_tty();
448
449         if ( stop_exit ){                       /* User program exited */
450                 WSETEXIT( (*status), stop_code );
451         } else {                                /* Fault or trace */
452                 switch (stop_code){
453                 case STOP_GDB_BPT:
454                 case TRACE_STEP:
455                         /* Make it look like a VAX trace trap */
456                         stop_code = SIGTRAP;
457                         break;
458                 default:
459                         /* The target is not running Unix, and its
460                            faults/traces do not map nicely into Unix signals.
461                            Make sure they do not get confused with Unix signals
462                            by numbering them with values higher than the highest
463                            legal Unix signal.  code in i960_print_fault(),
464                            called via PRINT_RANDOM_SIGNAL, will interpret the
465                            value.  */
466                         stop_code += NSIG;
467                         break;
468                 }
469                 WSETSTOP( (*status), stop_code );
470         }
471         return inferior_pid;
472 }
473
474 /* Read the remote registers into the block REGS.  */
475
476 /* This is the block that ninRegsGet and ninRegsPut handles.  */
477 struct nindy_regs {
478   char  local_regs[16 * 4];
479   char  global_regs[16 * 4];
480   char  pcw_acw[2 * 4];
481   char  ip[4];
482   char  tcw[4];
483   char  fp_as_double[4 * 8];
484 };
485
486 static void
487 nindy_fetch_registers(regno)
488      int regno;
489 {
490   struct nindy_regs nindy_regs;
491   int regnum, inv;
492   double dub;
493
494   immediate_quit++;
495   ninRegsGet( (char *) &nindy_regs );
496   immediate_quit--;
497
498   bcopy (nindy_regs.local_regs, &registers[REGISTER_BYTE (R0_REGNUM)], 16*4);
499   bcopy (nindy_regs.global_regs, &registers[REGISTER_BYTE (G0_REGNUM)], 16*4);
500   bcopy (nindy_regs.pcw_acw, &registers[REGISTER_BYTE (PCW_REGNUM)], 2*4);
501   bcopy (nindy_regs.ip, &registers[REGISTER_BYTE (IP_REGNUM)], 1*4);
502   bcopy (nindy_regs.tcw, &registers[REGISTER_BYTE (TCW_REGNUM)], 1*4);
503   for (regnum = FP0_REGNUM; regnum < FP0_REGNUM + 4; regnum++) {
504     dub = unpack_double (builtin_type_double,
505                          &nindy_regs.fp_as_double[8 * (regnum - FP0_REGNUM)],
506                          &inv);
507     /* dub now in host byte order */
508     double_to_ieee_extended (&ext_format_i960, &dub,
509                              &registers[REGISTER_BYTE (regnum)]);
510   }
511
512   registers_fetched ();
513 }
514
515 static void
516 nindy_prepare_to_store()
517 {
518   /* Fetch all regs if they aren't already here.  */
519   read_register_bytes (0, NULL, REGISTER_BYTES);
520 }
521
522 static void
523 nindy_store_registers(regno)
524      int regno;
525 {
526   struct nindy_regs nindy_regs;
527   int regnum, inv;
528   double dub;
529
530   bcopy (&registers[REGISTER_BYTE (R0_REGNUM)], nindy_regs.local_regs,  16*4);
531   bcopy (&registers[REGISTER_BYTE (G0_REGNUM)], nindy_regs.global_regs, 16*4);
532   bcopy (&registers[REGISTER_BYTE (PCW_REGNUM)], nindy_regs.pcw_acw,     2*4);
533   bcopy (&registers[REGISTER_BYTE (IP_REGNUM)], nindy_regs.ip,           1*4);
534   bcopy (&registers[REGISTER_BYTE (TCW_REGNUM)], nindy_regs.tcw,         1*4);
535   /* Float regs.  Only works on IEEE_FLOAT hosts.  FIXME!  */
536   for (regnum = FP0_REGNUM; regnum < FP0_REGNUM + 4; regnum++) {
537     ieee_extended_to_double (&ext_format_i960,
538                              &registers[REGISTER_BYTE (regnum)], &dub);
539     /* dub now in host byte order */
540     /* FIXME-someday, the arguments to unpack_double are backward.
541        It expects a target double and returns a host; we pass the opposite.
542        This mostly works but not quite.  */
543     dub = unpack_double (builtin_type_double, (char *)&dub, &inv);
544     /* dub now in target byte order */
545     bcopy ((char *)&dub, &nindy_regs.fp_as_double[8 * (regnum - FP0_REGNUM)],
546         8);
547   }
548
549   immediate_quit++;
550   ninRegsPut( (char *) &nindy_regs );
551   immediate_quit--;
552 }
553
554 /* Read a word from remote address ADDR and return it.
555  * This goes through the data cache.
556  */
557 int
558 nindy_fetch_word (addr)
559      CORE_ADDR addr;
560 {
561         return dcache_fetch (addr);
562 }
563
564 /* Write a word WORD into remote address ADDR.
565    This goes through the data cache.  */
566
567 void
568 nindy_store_word (addr, word)
569      CORE_ADDR addr;
570      int word;
571 {
572         dcache_poke (addr, word);
573 }
574
575 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
576    to debugger memory starting at MYADDR.   Copy to inferior if
577    WRITE is nonzero.  Returns the length copied.
578
579    This is stolen almost directly from infptrace.c's child_xfer_memory,
580    which also deals with a word-oriented memory interface.  Sometime,
581    FIXME, rewrite this to not use the word-oriented routines.  */
582
583 int
584 nindy_xfer_inferior_memory(memaddr, myaddr, len, write, target)
585      CORE_ADDR memaddr;
586      char *myaddr;
587      int len;
588      int write;
589      struct target_ops *target;                 /* ignored */
590 {
591   register int i;
592   /* Round starting address down to longword boundary.  */
593   register CORE_ADDR addr = memaddr & - sizeof (int);
594   /* Round ending address up; get number of longwords that makes.  */
595   register int count
596     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
597   /* Allocate buffer of that many longwords.  */
598   register int *buffer = (int *) alloca (count * sizeof (int));
599
600   if (write)
601     {
602       /* Fill start and end extra bytes of buffer with existing memory data.  */
603
604       if (addr != memaddr || len < (int)sizeof (int)) {
605         /* Need part of initial word -- fetch it.  */
606         buffer[0] = nindy_fetch_word (addr);
607       }
608
609       if (count > 1)            /* FIXME, avoid if even boundary */
610         {
611           buffer[count - 1]
612             = nindy_fetch_word (addr + (count - 1) * sizeof (int));
613         }
614
615       /* Copy data to be written over corresponding part of buffer */
616
617       bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
618
619       /* Write the entire buffer.  */
620
621       for (i = 0; i < count; i++, addr += sizeof (int))
622         {
623           errno = 0;
624           nindy_store_word (addr, buffer[i]);
625           if (errno)
626             return 0;
627         }
628     }
629   else
630     {
631       /* Read all the longwords */
632       for (i = 0; i < count; i++, addr += sizeof (int))
633         {
634           errno = 0;
635           buffer[i] = nindy_fetch_word (addr);
636           if (errno)
637             return 0;
638           QUIT;
639         }
640
641       /* Copy appropriate bytes out of the buffer.  */
642       bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
643     }
644   return len;
645 }
646 \f
647 /* The data cache records all the data read from the remote machine
648    since the last time it stopped.
649
650    Each cache block holds 16 bytes of data
651    starting at a multiple-of-16 address.  */
652
653 #define DCACHE_SIZE 64          /* Number of cache blocks */
654
655 struct dcache_block {
656         struct dcache_block *next, *last;
657         unsigned int addr;      /* Address for which data is recorded.  */
658         int data[4];
659 };
660
661 struct dcache_block dcache_free, dcache_valid;
662
663 /* Free all the data cache blocks, thus discarding all cached data.  */ 
664 static
665 void
666 dcache_flush ()
667 {
668   register struct dcache_block *db;
669
670   while ((db = dcache_valid.next) != &dcache_valid)
671     {
672       remque (db);
673       insque (db, &dcache_free);
674     }
675 }
676
677 /*
678  * If addr is present in the dcache, return the address of the block
679  * containing it.
680  */
681 static
682 struct dcache_block *
683 dcache_hit (addr)
684      unsigned int addr;
685 {
686   register struct dcache_block *db;
687
688   if (addr & 3)
689     abort ();
690
691   /* Search all cache blocks for one that is at this address.  */
692   db = dcache_valid.next;
693   while (db != &dcache_valid)
694     {
695       if ((addr & 0xfffffff0) == db->addr)
696         return db;
697       db = db->next;
698     }
699   return NULL;
700 }
701
702 /*  Return the int data at address ADDR in dcache block DC.  */
703 static
704 int
705 dcache_value (db, addr)
706      struct dcache_block *db;
707      unsigned int addr;
708 {
709   if (addr & 3)
710     abort ();
711   return (db->data[(addr>>2)&3]);
712 }
713
714 /* Get a free cache block, put or keep it on the valid list,
715    and return its address.  The caller should store into the block
716    the address and data that it describes, then remque it from the
717    free list and insert it into the valid list.  This procedure
718    prevents errors from creeping in if a ninMemGet is interrupted
719    (which used to put garbage blocks in the valid list...).  */
720 static
721 struct dcache_block *
722 dcache_alloc ()
723 {
724   register struct dcache_block *db;
725
726   if ((db = dcache_free.next) == &dcache_free)
727     {
728       /* If we can't get one from the free list, take last valid and put
729          it on the free list.  */
730       db = dcache_valid.last;
731       remque (db);
732       insque (db, &dcache_free);
733     }
734
735   remque (db);
736   insque (db, &dcache_valid);
737   return (db);
738 }
739
740 /* Return the contents of the word at address ADDR in the remote machine,
741    using the data cache.  */
742 static
743 int
744 dcache_fetch (addr)
745      CORE_ADDR addr;
746 {
747   register struct dcache_block *db;
748
749   db = dcache_hit (addr);
750   if (db == 0)
751     {
752       db = dcache_alloc ();
753       immediate_quit++;
754       ninMemGet(addr & ~0xf, (unsigned char *)db->data, 16);
755       immediate_quit--;
756       db->addr = addr & ~0xf;
757       remque (db);                      /* Off the free list */
758       insque (db, &dcache_valid);       /* On the valid list */
759     }
760   return (dcache_value (db, addr));
761 }
762
763 /* Write the word at ADDR both in the data cache and in the remote machine.  */
764 static void
765 dcache_poke (addr, data)
766      CORE_ADDR addr;
767      int data;
768 {
769   register struct dcache_block *db;
770
771   /* First make sure the word is IN the cache.  DB is its cache block.  */
772   db = dcache_hit (addr);
773   if (db == 0)
774     {
775       db = dcache_alloc ();
776       immediate_quit++;
777       ninMemGet(addr & ~0xf, (unsigned char *)db->data, 16);
778       immediate_quit--;
779       db->addr = addr & ~0xf;
780       remque (db);                      /* Off the free list */
781       insque (db, &dcache_valid);       /* On the valid list */
782     }
783
784   /* Modify the word in the cache.  */
785   db->data[(addr>>2)&3] = data;
786
787   /* Send the changed word.  */
788   immediate_quit++;
789   ninMemPut(addr, (unsigned char *)&data, 4);
790   immediate_quit--;
791 }
792
793 /* The cache itself. */
794 struct dcache_block the_cache[DCACHE_SIZE];
795
796 /* Initialize the data cache.  */
797 static void
798 dcache_init ()
799 {
800   register i;
801   register struct dcache_block *db;
802
803   db = the_cache;
804   dcache_free.next = dcache_free.last = &dcache_free;
805   dcache_valid.next = dcache_valid.last = &dcache_valid;
806   for (i=0;i<DCACHE_SIZE;i++,db++)
807     insque (db, &dcache_free);
808 }
809
810
811 static void
812 nindy_create_inferior (execfile, args, env)
813      char *execfile;
814      char *args;
815      char **env;
816 {
817   int entry_pt;
818   int pid;
819
820   if (args && *args)
821     error ("Can't pass arguments to remote NINDY process");
822
823   if (execfile == 0 || exec_bfd == 0)
824     error ("No exec file specified");
825
826   entry_pt = (int) bfd_get_start_address (exec_bfd);
827
828   pid = 42;
829
830 #ifdef CREATE_INFERIOR_HOOK
831   CREATE_INFERIOR_HOOK (pid);
832 #endif  
833
834 /* The "process" (board) is already stopped awaiting our commands, and
835    the program is already downloaded.  We just set its PC and go.  */
836
837   inferior_pid = pid;           /* Needed for wait_for_inferior below */
838
839   clear_proceed_status ();
840
841   /* Tell wait_for_inferior that we've started a new process.  */
842   init_wait_for_inferior ();
843
844   /* Set up the "saved terminal modes" of the inferior
845      based on what modes we are starting it with.  */
846   target_terminal_init ();
847
848   /* Install inferior's terminal modes.  */
849   target_terminal_inferior ();
850
851   /* insert_step_breakpoint ();  FIXME, do we need this?  */
852   proceed ((CORE_ADDR)entry_pt, -1, 0);         /* Let 'er rip... */
853 }
854
855 static void
856 reset_command(args, from_tty)
857      char *args;
858      int from_tty;
859 {
860         if ( !nindy_fd ){
861             error( "No target system to reset -- use 'target nindy' command.");
862         }
863         if ( query("Really reset the target system?",0,0) ){
864                 send_break( nindy_fd );
865                 tty_flush( nindy_fd );
866         }
867 }
868
869 void
870 nindy_kill (args, from_tty)
871      char *args;
872      int from_tty;
873 {
874   return;               /* Ignore attempts to kill target system */
875 }
876
877 /* Clean up when a program exits.
878
879    The program actually lives on in the remote processor's RAM, and may be
880    run again without a download.  Don't leave it full of breakpoint
881    instructions.  */
882
883 void
884 nindy_mourn_inferior ()
885 {
886   remove_breakpoints ();
887   unpush_target (&nindy_ops);
888   generic_mourn_inferior ();    /* Do all the proper things now */
889 }
890 \f
891 /* This routine is run as a hook, just before the main command loop is
892    entered.  If gdb is configured for the i960, but has not had its
893    nindy target specified yet, this will loop prompting the user to do so.
894
895    Unlike the loop provided by Intel, we actually let the user get out
896    of this with a RETURN.  This is useful when e.g. simply examining
897    an i960 object file on the host system.  */
898
899 void
900 nindy_before_main_loop ()
901 {
902   char ttyname[100];
903   char *p, *p2;
904
905   setjmp(to_top_level);
906   while (current_target != &nindy_ops) { /* remote tty not specified yet */
907         if ( instream == stdin ){
908                 printf("\nAttach /dev/ttyNN -- specify NN, or \"quit\" to quit:  ");
909                 fflush( stdout );
910         }
911         fgets( ttyname, sizeof(ttyname)-1, stdin );
912
913         /* Strip leading and trailing whitespace */
914         for ( p = ttyname; isspace(*p); p++ ){
915                 ;
916         }
917         if ( *p == '\0' ){
918                 return;         /* User just hit spaces or return, wants out */
919         }
920         for ( p2= p; !isspace(*p2) && (*p2 != '\0'); p2++ ){
921                 ;
922         }
923         *p2= '\0';
924         if ( STREQ("quit",p) ){
925                 exit(1);
926         }
927
928         nindy_open( p, 1 );
929
930         /* Now that we have a tty open for talking to the remote machine,
931            download the executable file if one was specified.  */
932         if ( !setjmp(to_top_level) && exec_bfd ) {
933               target_load (bfd_get_filename (exec_bfd), 1);
934         }
935   }
936 }
937 \f
938 /* Define the target subroutine names */
939
940 struct target_ops nindy_ops = {
941         "nindy", "Remote serial target in i960 NINDY-specific protocol",
942         "Use a remote i960 system running NINDY connected by a serial line.\n\
943 Specify the name of the device the serial line is connected to.\n\
944 The speed (baud rate), whether to use the old NINDY protocol,\n\
945 and whether to send a break on startup, are controlled by options\n\
946 specified when you started GDB.",
947         nindy_open, nindy_close,
948         0,
949         nindy_detach,
950         nindy_resume,
951         nindy_wait,
952         nindy_fetch_registers, nindy_store_registers,
953         nindy_prepare_to_store,
954         nindy_xfer_inferior_memory, nindy_files_info,
955         0, 0, /* insert_breakpoint, remove_breakpoint, */
956         0, 0, 0, 0, 0,  /* Terminal crud */
957         nindy_kill,
958         nindy_load,
959         0, /* lookup_symbol */
960         nindy_create_inferior,
961         nindy_mourn_inferior,
962         0,              /* can_run */
963         0, /* notice_signals */
964         process_stratum, 0, /* next */
965         1, 1, 1, 1, 1,  /* all mem, mem, stack, regs, exec */
966         0, 0,                   /* Section pointers */
967         OPS_MAGIC,              /* Always the last thing */
968 };
969
970 void
971 _initialize_nindy ()
972 {
973   add_target (&nindy_ops);
974   add_com ("reset", class_obscure, reset_command,
975            "Send a 'break' to the remote target system.\n\
976 Only useful if the target has been equipped with a circuit\n\
977 to perform a hard reset when a break is detected.");
978 }