2009-02-10 Pierre Muller <muller@ics.u-strasbg.fr>
[external/binutils.git] / gdb / remote-m32r-sdi.c
1 /* Remote debugging interface for M32R/SDI.
2
3    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5
6    Contributed by Renesas Technology Co.
7    Written by Kei Sakamoto <sakamoto.kei@renesas.com>.
8
9    This file is part of GDB.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23
24 #include "defs.h"
25 #include "gdbcmd.h"
26 #include "gdbcore.h"
27 #include "inferior.h"
28 #include "target.h"
29 #include "regcache.h"
30 #include "gdb_string.h"
31 #include "gdbthread.h"
32 #include <ctype.h>
33 #include <signal.h>
34 #ifdef __MINGW32__
35 #include <winsock.h>
36 #else
37 #include <netinet/in.h>
38 #endif
39 #include <sys/types.h>
40 #include <sys/time.h>
41 #include <signal.h>
42 #include <time.h>
43
44
45 #include "serial.h"
46
47 /* Descriptor for I/O to remote machine.  */
48
49 static struct serial *sdi_desc = NULL;
50
51 #define SDI_TIMEOUT 30
52
53
54 #define SDIPORT 3232
55
56 static char chip_name[64];
57
58 static int step_mode;
59 static unsigned long last_pc_addr = 0xffffffff;
60 static unsigned char last_pc_addr_data[2];
61
62 static int mmu_on = 0;
63
64 static int use_ib_breakpoints = 1;
65
66 #define MAX_BREAKPOINTS 1024
67 static int max_ib_breakpoints;
68 static unsigned long bp_address[MAX_BREAKPOINTS];
69 static unsigned char bp_data[MAX_BREAKPOINTS][4];
70
71 /* dbt -> nop */
72 static const unsigned char dbt_bp_entry[] = {
73   0x10, 0xe0, 0x70, 0x00
74 };
75
76 #define MAX_ACCESS_BREAKS 4
77 static int max_access_breaks;
78 static unsigned long ab_address[MAX_ACCESS_BREAKS];
79 static unsigned int ab_type[MAX_ACCESS_BREAKS];
80 static unsigned int ab_size[MAX_ACCESS_BREAKS];
81 static CORE_ADDR hit_watchpoint_addr = 0;
82
83 static int interrupted = 0;
84
85 /* Forward data declarations */
86 extern struct target_ops m32r_ops;
87
88 /* This is the ptid we use while we're connected to the remote.  Its
89    value is arbitrary, as the target doesn't have a notion of
90    processes or threads, but we need something non-null to place in
91    inferior_ptid.  */
92 static ptid_t remote_m32r_ptid;
93
94 /* Commands */
95 #define SDI_OPEN                 1
96 #define SDI_CLOSE                2
97 #define SDI_RELEASE              3
98 #define SDI_READ_CPU_REG         4
99 #define SDI_WRITE_CPU_REG        5
100 #define SDI_READ_MEMORY          6
101 #define SDI_WRITE_MEMORY         7
102 #define SDI_EXEC_CPU             8
103 #define SDI_STOP_CPU             9
104 #define SDI_WAIT_FOR_READY      10
105 #define SDI_GET_ATTR            11
106 #define SDI_SET_ATTR            12
107 #define SDI_STATUS              13
108
109 /* Attributes */
110 #define SDI_ATTR_NAME            1
111 #define SDI_ATTR_BRK             2
112 #define SDI_ATTR_ABRK            3
113 #define SDI_ATTR_CACHE           4
114 #define SDI_CACHE_TYPE_M32102    0
115 #define SDI_CACHE_TYPE_CHAOS     1
116 #define SDI_ATTR_MEM_ACCESS      5
117 #define SDI_MEM_ACCESS_DEBUG_DMA 0
118 #define SDI_MEM_ACCESS_MON_CODE  1
119
120 /* Registers */
121 #define SDI_REG_R0               0
122 #define SDI_REG_R1               1
123 #define SDI_REG_R2               2
124 #define SDI_REG_R3               3
125 #define SDI_REG_R4               4
126 #define SDI_REG_R5               5
127 #define SDI_REG_R6               6
128 #define SDI_REG_R7               7
129 #define SDI_REG_R8               8
130 #define SDI_REG_R9               9
131 #define SDI_REG_R10             10
132 #define SDI_REG_R11             11
133 #define SDI_REG_R12             12
134 #define SDI_REG_FP              13
135 #define SDI_REG_LR              14
136 #define SDI_REG_SP              15
137 #define SDI_REG_PSW             16
138 #define SDI_REG_CBR             17
139 #define SDI_REG_SPI             18
140 #define SDI_REG_SPU             19
141 #define SDI_REG_CR4             20
142 #define SDI_REG_EVB             21
143 #define SDI_REG_BPC             22
144 #define SDI_REG_CR7             23
145 #define SDI_REG_BBPSW           24
146 #define SDI_REG_CR9             25
147 #define SDI_REG_CR10            26
148 #define SDI_REG_CR11            27
149 #define SDI_REG_CR12            28
150 #define SDI_REG_WR              29
151 #define SDI_REG_BBPC            30
152 #define SDI_REG_PBP             31
153 #define SDI_REG_ACCH            32
154 #define SDI_REG_ACCL            33
155 #define SDI_REG_ACC1H           34
156 #define SDI_REG_ACC1L           35
157
158
159 /* Low level communication functions */
160
161 /* Check an ack packet from the target */
162 static int
163 get_ack (void)
164 {
165   int c;
166
167   if (!sdi_desc)
168     return -1;
169
170   c = serial_readchar (sdi_desc, SDI_TIMEOUT);
171
172   if (c < 0)
173     return -1;
174
175   if (c != '+')                 /* error */
176     return -1;
177
178   return 0;
179 }
180
181 /* Send data to the target and check an ack packet */
182 static int
183 send_data (void *buf, int len)
184 {
185   int ret;
186
187   if (!sdi_desc)
188     return -1;
189
190   if (serial_write (sdi_desc, buf, len) != 0)
191     return -1;
192
193   if (get_ack () == -1)
194     return -1;
195
196   return len;
197 }
198
199 /* Receive data from the target */
200 static int
201 recv_data (void *buf, int len)
202 {
203   int total = 0;
204   int c;
205
206   if (!sdi_desc)
207     return -1;
208
209   while (total < len)
210     {
211       c = serial_readchar (sdi_desc, SDI_TIMEOUT);
212
213       if (c < 0)
214         return -1;
215
216       ((unsigned char *) buf)[total++] = c;
217     }
218
219   return len;
220 }
221
222 /* Store unsigned long parameter on packet */
223 static void
224 store_long_parameter (void *buf, long val)
225 {
226   val = htonl (val);
227   memcpy (buf, &val, 4);
228 }
229
230 static int
231 send_cmd (unsigned char cmd)
232 {
233   unsigned char buf[1];
234   buf[0] = cmd;
235   return send_data (buf, 1);
236 }
237
238 static int
239 send_one_arg_cmd (unsigned char cmd, unsigned char arg1)
240 {
241   unsigned char buf[2];
242   buf[0] = cmd;
243   buf[1] = arg1;
244   return send_data (buf, 2);
245 }
246
247 static int
248 send_two_arg_cmd (unsigned char cmd, unsigned char arg1, unsigned long arg2)
249 {
250   unsigned char buf[6];
251   buf[0] = cmd;
252   buf[1] = arg1;
253   store_long_parameter (buf + 2, arg2);
254   return send_data (buf, 6);
255 }
256
257 static int
258 send_three_arg_cmd (unsigned char cmd, unsigned long arg1, unsigned long arg2,
259                     unsigned long arg3)
260 {
261   unsigned char buf[13];
262   buf[0] = cmd;
263   store_long_parameter (buf + 1, arg1);
264   store_long_parameter (buf + 5, arg2);
265   store_long_parameter (buf + 9, arg3);
266   return send_data (buf, 13);
267 }
268
269 static unsigned char
270 recv_char_data (void)
271 {
272   unsigned char val;
273   recv_data (&val, 1);
274   return val;
275 }
276
277 static unsigned long
278 recv_long_data (void)
279 {
280   unsigned long val;
281   recv_data (&val, 4);
282   return ntohl (val);
283 }
284
285
286 /* Check if MMU is on */
287 static void
288 check_mmu_status (void)
289 {
290   unsigned long val;
291
292   /* Read PC address */
293   if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC) == -1)
294     return;
295   val = recv_long_data ();
296   if ((val & 0xc0000000) == 0x80000000)
297     {
298       mmu_on = 1;
299       return;
300     }
301
302   /* Read EVB address */
303   if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_EVB) == -1)
304     return;
305   val = recv_long_data ();
306   if ((val & 0xc0000000) == 0x80000000)
307     {
308       mmu_on = 1;
309       return;
310     }
311
312   mmu_on = 0;
313 }
314
315
316 /* This is called not only when we first attach, but also when the
317    user types "run" after having attached.  */
318 static void
319 m32r_create_inferior (struct target_ops *ops, char *execfile,
320                       char *args, char **env, int from_tty)
321 {
322   CORE_ADDR entry_pt;
323
324   if (args && *args)
325     error (_("Cannot pass arguments to remote STDEBUG process"));
326
327   if (execfile == 0 || exec_bfd == 0)
328     error (_("No executable file specified"));
329
330   if (remote_debug)
331     fprintf_unfiltered (gdb_stdlog, "m32r_create_inferior(%s,%s)\n", execfile,
332                         args);
333
334   entry_pt = bfd_get_start_address (exec_bfd);
335
336   /* The "process" (board) is already stopped awaiting our commands, and
337      the program is already downloaded.  We just set its PC and go.  */
338
339   clear_proceed_status ();
340
341   /* Tell wait_for_inferior that we've started a new process.  */
342   init_wait_for_inferior ();
343
344   /* Set up the "saved terminal modes" of the inferior
345      based on what modes we are starting it with.  */
346   target_terminal_init ();
347
348   /* Install inferior's terminal modes.  */
349   target_terminal_inferior ();
350
351   write_pc (entry_pt);
352 }
353
354 /* Open a connection to a remote debugger.
355    NAME is the filename used for communication.  */
356
357 static void
358 m32r_open (char *args, int from_tty)
359 {
360   struct hostent *host_ent;
361   struct sockaddr_in server_addr;
362   char *port_str, hostname[256];
363   int port;
364   int i, n;
365   int yes = 1;
366
367   if (remote_debug)
368     fprintf_unfiltered (gdb_stdlog, "m32r_open(%d)\n", from_tty);
369
370   target_preopen (from_tty);
371
372   push_target (&m32r_ops);
373
374   if (args == NULL)
375     sprintf (hostname, "localhost:%d", SDIPORT);
376   else
377     {
378       port_str = strchr (args, ':');
379       if (port_str == NULL)
380         sprintf (hostname, "%s:%d", args, SDIPORT);
381       else
382         strcpy (hostname, args);
383     }
384
385   sdi_desc = serial_open (hostname);
386   if (!sdi_desc)
387     error (_("Connection refused."));
388
389   if (get_ack () == -1)
390     error (_("Cannot connect to SDI target."));
391
392   if (send_cmd (SDI_OPEN) == -1)
393     error (_("Cannot connect to SDI target."));
394
395   /* Get maximum number of ib breakpoints */
396   send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_BRK);
397   max_ib_breakpoints = recv_char_data ();
398   if (remote_debug)
399     printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints);
400
401   /* Initialize breakpoints. */
402   for (i = 0; i < MAX_BREAKPOINTS; i++)
403     bp_address[i] = 0xffffffff;
404
405   /* Get maximum number of access breaks. */
406   send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_ABRK);
407   max_access_breaks = recv_char_data ();
408   if (remote_debug)
409     printf_filtered ("Max Access Breaks = %d\n", max_access_breaks);
410
411   /* Initialize access breask. */
412   for (i = 0; i < MAX_ACCESS_BREAKS; i++)
413     ab_address[i] = 0x00000000;
414
415   check_mmu_status ();
416
417   /* Get the name of chip on target board. */
418   send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_NAME);
419   recv_data (chip_name, 64);
420
421   if (from_tty)
422     printf_filtered ("Remote %s connected to %s\n", target_shortname,
423                      chip_name);
424 }
425
426 /* Close out all files and local state before this target loses control. */
427
428 static void
429 m32r_close (int quitting)
430 {
431   if (remote_debug)
432     fprintf_unfiltered (gdb_stdlog, "m32r_close(%d)\n", quitting);
433
434   if (sdi_desc)
435     {
436       send_cmd (SDI_CLOSE);
437       serial_close (sdi_desc);
438       sdi_desc = NULL;
439     }
440
441   inferior_ptid = null_ptid;
442   delete_thread_silent (remote_m32r_ptid);
443   return;
444 }
445
446 /* Tell the remote machine to resume.  */
447
448 static void
449 m32r_resume (ptid_t ptid, int step, enum target_signal sig)
450 {
451   unsigned long pc_addr, bp_addr, ab_addr;
452   int ib_breakpoints;
453   unsigned char buf[13];
454   int i;
455
456   if (remote_debug)
457     {
458       if (step)
459         fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(step)\n");
460       else
461         fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(cont)\n");
462     }
463
464   check_mmu_status ();
465
466   pc_addr = read_pc ();
467   if (remote_debug)
468     fprintf_unfiltered (gdb_stdlog, "pc <= 0x%lx\n", pc_addr);
469
470   /* At pc address there is a parallel instruction with +2 offset,
471      so we have to make it a serial instruction or avoid it. */
472   if (pc_addr == last_pc_addr)
473     {
474       /* Avoid a parallel nop. */
475       if (last_pc_addr_data[0] == 0xf0 && last_pc_addr_data[1] == 0x00)
476         {
477           pc_addr += 2;
478           /* Now we can forget this instruction. */
479           last_pc_addr = 0xffffffff;
480         }
481       /* Clear a parallel bit. */
482       else
483         {
484           buf[0] = SDI_WRITE_MEMORY;
485           if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
486             store_long_parameter (buf + 1, pc_addr);
487           else
488             store_long_parameter (buf + 1, pc_addr - 1);
489           store_long_parameter (buf + 5, 1);
490           buf[9] = last_pc_addr_data[0] & 0x7f;
491           send_data (buf, 10);
492         }
493     }
494
495   /* Set PC. */
496   send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);
497
498   /* step mode. */
499   step_mode = step;
500   if (step)
501     {
502       /* Set PBP. */
503       send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, pc_addr | 1);
504     }
505   else
506     {
507       /* Unset PBP. */
508       send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, 0x00000000);
509     }
510
511   if (use_ib_breakpoints)
512     ib_breakpoints = max_ib_breakpoints;
513   else
514     ib_breakpoints = 0;
515
516   /* Set ib breakpoints. */
517   for (i = 0; i < ib_breakpoints; i++)
518     {
519       bp_addr = bp_address[i];
520
521       if (bp_addr == 0xffffffff)
522         continue;
523
524       /* Set PBP. */
525       if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
526         send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
527                             0x00000006);
528       else
529         send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
530                             0x06000000);
531
532       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8080 + 4 * i, 4, bp_addr);
533     }
534
535   /* Set dbt breakpoints. */
536   for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
537     {
538       bp_addr = bp_address[i];
539
540       if (bp_addr == 0xffffffff)
541         continue;
542
543       if (!mmu_on)
544         bp_addr &= 0x7fffffff;
545
546       /* Write DBT instruction. */
547       buf[0] = SDI_WRITE_MEMORY;
548       store_long_parameter (buf + 1, (bp_addr & 0xfffffffc));
549       store_long_parameter (buf + 5, 4);
550       if ((bp_addr & 2) == 0 && bp_addr != (pc_addr & 0xfffffffc))
551         {
552           if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
553             {
554               buf[9] = dbt_bp_entry[0];
555               buf[10] = dbt_bp_entry[1];
556               buf[11] = dbt_bp_entry[2];
557               buf[12] = dbt_bp_entry[3];
558             }
559           else
560             {
561               buf[9] = dbt_bp_entry[3];
562               buf[10] = dbt_bp_entry[2];
563               buf[11] = dbt_bp_entry[1];
564               buf[12] = dbt_bp_entry[0];
565             }
566         }
567       else
568         {
569           if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
570             {
571               if ((bp_addr & 2) == 0)
572                 {
573                   buf[9] = dbt_bp_entry[0];
574                   buf[10] = dbt_bp_entry[1];
575                   buf[11] = bp_data[i][2] & 0x7f;
576                   buf[12] = bp_data[i][3];
577                 }
578               else
579                 {
580                   buf[9] = bp_data[i][0];
581                   buf[10] = bp_data[i][1];
582                   buf[11] = dbt_bp_entry[0];
583                   buf[12] = dbt_bp_entry[1];
584                 }
585             }
586           else
587             {
588               if ((bp_addr & 2) == 0)
589                 {
590                   buf[9] = bp_data[i][0];
591                   buf[10] = bp_data[i][1] & 0x7f;
592                   buf[11] = dbt_bp_entry[1];
593                   buf[12] = dbt_bp_entry[0];
594                 }
595               else
596                 {
597                   buf[9] = dbt_bp_entry[1];
598                   buf[10] = dbt_bp_entry[0];
599                   buf[11] = bp_data[i][2];
600                   buf[12] = bp_data[i][3];
601                 }
602             }
603         }
604       send_data (buf, 13);
605     }
606
607   /* Set access breaks. */
608   for (i = 0; i < max_access_breaks; i++)
609     {
610       ab_addr = ab_address[i];
611
612       if (ab_addr == 0x00000000)
613         continue;
614
615       /* DBC register */
616       if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
617         {
618           switch (ab_type[i])
619             {
620             case 0:             /* write watch */
621               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
622                                   0x00000086);
623               break;
624             case 1:             /* read watch */
625               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
626                                   0x00000046);
627               break;
628             case 2:             /* access watch */
629               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
630                                   0x00000006);
631               break;
632             }
633         }
634       else
635         {
636           switch (ab_type[i])
637             {
638             case 0:             /* write watch */
639               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
640                                   0x86000000);
641               break;
642             case 1:             /* read watch */
643               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
644                                   0x46000000);
645               break;
646             case 2:             /* access watch */
647               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
648                                   0x06000000);
649               break;
650             }
651         }
652
653       /* DBAH register */
654       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8180 + 4 * i, 4, ab_addr);
655
656       /* DBAL register */
657       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8200 + 4 * i, 4,
658                           0xffffffff);
659
660       /* DBD register */
661       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8280 + 4 * i, 4,
662                           0x00000000);
663
664       /* DBDM register */
665       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8300 + 4 * i, 4,
666                           0x00000000);
667     }
668
669   /* Resume program. */
670   send_cmd (SDI_EXEC_CPU);
671
672   /* Without this, some commands which require an active target (such as kill)
673      won't work.  This variable serves (at least) double duty as both the pid
674      of the target process (if it has such), and as a flag indicating that a
675      target is active.  These functions should be split out into seperate
676      variables, especially since GDB will someday have a notion of debugging
677      several processes.  */
678   inferior_ptid = remote_m32r_ptid;
679   add_thread_silent (remote_m32r_ptid);
680
681   return;
682 }
683
684 /* Wait until the remote machine stops, then return,
685    storing status in STATUS just as `wait' would.  */
686
687 static void
688 gdb_cntrl_c (int signo)
689 {
690   if (remote_debug)
691     fprintf_unfiltered (gdb_stdlog, "interrupt\n");
692   interrupted = 1;
693 }
694
695 static ptid_t
696 m32r_wait (struct target_ops *ops,
697            ptid_t ptid, struct target_waitstatus *status)
698 {
699   static RETSIGTYPE (*prev_sigint) ();
700   unsigned long bp_addr, pc_addr;
701   int ib_breakpoints;
702   long i;
703   unsigned char buf[13];
704   unsigned long val;
705   int ret, c;
706
707   if (remote_debug)
708     fprintf_unfiltered (gdb_stdlog, "m32r_wait()\n");
709
710   status->kind = TARGET_WAITKIND_EXITED;
711   status->value.sig = 0;
712
713   interrupted = 0;
714   prev_sigint = signal (SIGINT, gdb_cntrl_c);
715
716   /* Wait for ready */
717   buf[0] = SDI_WAIT_FOR_READY;
718   if (serial_write (sdi_desc, buf, 1) != 0)
719     error (_("Remote connection closed"));
720
721   while (1)
722     {
723       c = serial_readchar (sdi_desc, SDI_TIMEOUT);
724       if (c < 0)
725         error (_("Remote connection closed"));
726
727       if (c == '-')             /* error */
728         {
729           status->kind = TARGET_WAITKIND_STOPPED;
730           status->value.sig = TARGET_SIGNAL_HUP;
731           return inferior_ptid;
732         }
733       else if (c == '+')        /* stopped */
734         break;
735
736       if (interrupted)
737         ret = serial_write (sdi_desc, "!", 1);  /* packet to interrupt */
738       else
739         ret = serial_write (sdi_desc, ".", 1);  /* packet to wait */
740       if (ret != 0)
741         error (_("Remote connection closed"));
742     }
743
744   status->kind = TARGET_WAITKIND_STOPPED;
745   if (interrupted)
746     status->value.sig = TARGET_SIGNAL_INT;
747   else
748     status->value.sig = TARGET_SIGNAL_TRAP;
749
750   interrupted = 0;
751   signal (SIGINT, prev_sigint);
752
753   check_mmu_status ();
754
755   /* Recover parallel bit. */
756   if (last_pc_addr != 0xffffffff)
757     {
758       buf[0] = SDI_WRITE_MEMORY;
759       if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
760         store_long_parameter (buf + 1, last_pc_addr);
761       else
762         store_long_parameter (buf + 1, last_pc_addr - 1);
763       store_long_parameter (buf + 5, 1);
764       buf[9] = last_pc_addr_data[0];
765       send_data (buf, 10);
766       last_pc_addr = 0xffffffff;
767     }
768
769   if (use_ib_breakpoints)
770     ib_breakpoints = max_ib_breakpoints;
771   else
772     ib_breakpoints = 0;
773
774   /* Set back pc by 2 if m32r is stopped with dbt. */
775   last_pc_addr = 0xffffffff;
776   send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC);
777   pc_addr = recv_long_data () - 2;
778   for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
779     {
780       if (pc_addr == bp_address[i])
781         {
782           send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);
783
784           /* If there is a parallel instruction with +2 offset at pc
785              address, we have to take care of it later. */
786           if ((pc_addr & 0x2) != 0)
787             {
788               if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
789                 {
790                   if ((bp_data[i][2] & 0x80) != 0)
791                     {
792                       last_pc_addr = pc_addr;
793                       last_pc_addr_data[0] = bp_data[i][2];
794                       last_pc_addr_data[1] = bp_data[i][3];
795                     }
796                 }
797               else
798                 {
799                   if ((bp_data[i][1] & 0x80) != 0)
800                     {
801                       last_pc_addr = pc_addr;
802                       last_pc_addr_data[0] = bp_data[i][1];
803                       last_pc_addr_data[1] = bp_data[i][0];
804                     }
805                 }
806             }
807           break;
808         }
809     }
810
811   /* Remove ib breakpoints. */
812   for (i = 0; i < ib_breakpoints; i++)
813     {
814       if (bp_address[i] != 0xffffffff)
815         send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
816                             0x00000000);
817     }
818   /* Remove dbt breakpoints. */
819   for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
820     {
821       bp_addr = bp_address[i];
822       if (bp_addr != 0xffffffff)
823         {
824           if (!mmu_on)
825             bp_addr &= 0x7fffffff;
826           buf[0] = SDI_WRITE_MEMORY;
827           store_long_parameter (buf + 1, bp_addr & 0xfffffffc);
828           store_long_parameter (buf + 5, 4);
829           buf[9] = bp_data[i][0];
830           buf[10] = bp_data[i][1];
831           buf[11] = bp_data[i][2];
832           buf[12] = bp_data[i][3];
833           send_data (buf, 13);
834         }
835     }
836
837   /* Remove access breaks. */
838   hit_watchpoint_addr = 0;
839   for (i = 0; i < max_access_breaks; i++)
840     {
841       if (ab_address[i] != 0x00000000)
842         {
843           buf[0] = SDI_READ_MEMORY;
844           store_long_parameter (buf + 1, 0xffff8100 + 4 * i);
845           store_long_parameter (buf + 5, 4);
846           serial_write (sdi_desc, buf, 9);
847           c = serial_readchar (sdi_desc, SDI_TIMEOUT);
848           if (c != '-' && recv_data (buf, 4) != -1)
849             {
850               if (gdbarch_byte_order (target_gdbarch) == BFD_ENDIAN_BIG)
851                 {
852                   if ((buf[3] & 0x1) == 0x1)
853                     hit_watchpoint_addr = ab_address[i];
854                 }
855               else
856                 {
857                   if ((buf[0] & 0x1) == 0x1)
858                     hit_watchpoint_addr = ab_address[i];
859                 }
860             }
861
862           send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
863                               0x00000000);
864         }
865     }
866
867   if (remote_debug)
868     fprintf_unfiltered (gdb_stdlog, "pc => 0x%lx\n", pc_addr);
869
870   return inferior_ptid;
871 }
872
873 /* Terminate the open connection to the remote debugger.
874    Use this when you want to detach and do something else
875    with your gdb.  */
876 static void
877 m32r_detach (struct target_ops *ops, char *args, int from_tty)
878 {
879   if (remote_debug)
880     fprintf_unfiltered (gdb_stdlog, "m32r_detach(%d)\n", from_tty);
881
882   m32r_resume (inferior_ptid, 0, 0);
883
884   /* calls m32r_close to do the real work */
885   pop_target ();
886   if (from_tty)
887     fprintf_unfiltered (gdb_stdlog, "Ending remote %s debugging\n",
888                         target_shortname);
889 }
890
891 /* Return the id of register number REGNO. */
892
893 static int
894 get_reg_id (int regno)
895 {
896   switch (regno)
897     {
898     case 20:
899       return SDI_REG_BBPC;
900     case 21:
901       return SDI_REG_BPC;
902     case 22:
903       return SDI_REG_ACCL;
904     case 23:
905       return SDI_REG_ACCH;
906     case 24:
907       return SDI_REG_EVB;
908     }
909
910   return regno;
911 }
912
913 /* Read the remote registers into the block REGS.  */
914
915 static void m32r_fetch_register (struct regcache *, int);
916
917 static void
918 m32r_fetch_registers (struct regcache *regcache)
919 {
920   int regno;
921
922   for (regno = 0;
923        regno < gdbarch_num_regs (get_regcache_arch (regcache));
924        regno++)
925     m32r_fetch_register (regcache, regno);
926 }
927
928 /* Fetch register REGNO, or all registers if REGNO is -1.
929    Returns errno value.  */
930 static void
931 m32r_fetch_register (struct regcache *regcache, int regno)
932 {
933   unsigned long val, val2, regid;
934
935   if (regno == -1)
936     m32r_fetch_registers (regcache);
937   else
938     {
939       char buffer[MAX_REGISTER_SIZE];
940
941       regid = get_reg_id (regno);
942       send_one_arg_cmd (SDI_READ_CPU_REG, regid);
943       val = recv_long_data ();
944
945       if (regid == SDI_REG_PSW)
946         {
947           send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
948           val2 = recv_long_data ();
949           val = ((0x00cf & val2) << 8) | ((0xcf00 & val) >> 8);
950         }
951
952       if (remote_debug)
953         fprintf_unfiltered (gdb_stdlog, "m32r_fetch_register(%d,0x%08lx)\n",
954                             regno, val);
955
956       /* We got the number the register holds, but gdb expects to see a
957          value in the target byte ordering.  */
958       store_unsigned_integer (buffer, 4, val);
959       regcache_raw_supply (regcache, regno, buffer);
960     }
961   return;
962 }
963
964 /* Store the remote registers from the contents of the block REGS.  */
965
966 static void m32r_store_register (struct regcache *, int);
967
968 static void
969 m32r_store_registers (struct regcache *regcache)
970 {
971   int regno;
972
973   for (regno = 0;
974        regno < gdbarch_num_regs (get_regcache_arch (regcache));
975        regno++)
976     m32r_store_register (regcache, regno);
977
978   registers_changed ();
979 }
980
981 /* Store register REGNO, or all if REGNO == 0.
982    Return errno value.  */
983 static void
984 m32r_store_register (struct regcache *regcache, int regno)
985 {
986   int regid;
987   ULONGEST regval, tmp;
988
989   if (regno == -1)
990     m32r_store_registers (regcache);
991   else
992     {
993       regcache_cooked_read_unsigned (regcache, regno, &regval);
994       regid = get_reg_id (regno);
995
996       if (regid == SDI_REG_PSW)
997         {
998           unsigned long psw, bbpsw;
999
1000           send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_PSW);
1001           psw = recv_long_data ();
1002
1003           send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
1004           bbpsw = recv_long_data ();
1005
1006           tmp = (0x00cf & psw) | ((0x00cf & regval) << 8);
1007           send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PSW, tmp);
1008
1009           tmp = (0x0030 & bbpsw) | ((0xcf00 & regval) >> 8);
1010           send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BBPSW, tmp);
1011         }
1012       else
1013         {
1014           send_two_arg_cmd (SDI_WRITE_CPU_REG, regid, regval);
1015         }
1016
1017       if (remote_debug)
1018         fprintf_unfiltered (gdb_stdlog, "m32r_store_register(%d,0x%08lu)\n",
1019                             regno, (unsigned long) regval);
1020     }
1021 }
1022
1023 /* Get ready to modify the registers array.  On machines which store
1024    individual registers, this doesn't need to do anything.  On machines
1025    which store all the registers in one fell swoop, this makes sure
1026    that registers contains all the registers from the program being
1027    debugged.  */
1028
1029 static void
1030 m32r_prepare_to_store (struct regcache *regcache)
1031 {
1032   /* Do nothing, since we can store individual regs */
1033   if (remote_debug)
1034     fprintf_unfiltered (gdb_stdlog, "m32r_prepare_to_store()\n");
1035 }
1036
1037 static void
1038 m32r_files_info (struct target_ops *target)
1039 {
1040   char *file = "nothing";
1041
1042   if (exec_bfd)
1043     {
1044       file = bfd_get_filename (exec_bfd);
1045       printf_filtered ("\tAttached to %s running program %s\n",
1046                        chip_name, file);
1047     }
1048 }
1049
1050 /* Read/Write memory.  */
1051 static int
1052 m32r_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len,
1053                   int write,
1054                   struct mem_attrib *attrib, struct target_ops *target)
1055 {
1056   unsigned long taddr;
1057   unsigned char buf[0x2000];
1058   int ret, c;
1059
1060   taddr = memaddr;
1061
1062   if (!mmu_on)
1063     {
1064       if ((taddr & 0xa0000000) == 0x80000000)
1065         taddr &= 0x7fffffff;
1066     }
1067
1068   if (remote_debug)
1069     {
1070       if (write)
1071         fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%s,%d,write)\n",
1072                             paddr (memaddr), len);
1073       else
1074         fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%s,%d,read)\n",
1075                             paddr (memaddr), len);
1076     }
1077
1078   if (write)
1079     {
1080       buf[0] = SDI_WRITE_MEMORY;
1081       store_long_parameter (buf + 1, taddr);
1082       store_long_parameter (buf + 5, len);
1083       if (len < 0x1000)
1084         {
1085           memcpy (buf + 9, myaddr, len);
1086           ret = send_data (buf, len + 9) - 9;
1087         }
1088       else
1089         {
1090           if (serial_write (sdi_desc, buf, 9) != 0)
1091             {
1092               if (remote_debug)
1093                 fprintf_unfiltered (gdb_stdlog,
1094                                     "m32r_xfer_memory() failed\n");
1095               return 0;
1096             }
1097           ret = send_data (myaddr, len);
1098         }
1099     }
1100   else
1101     {
1102       buf[0] = SDI_READ_MEMORY;
1103       store_long_parameter (buf + 1, taddr);
1104       store_long_parameter (buf + 5, len);
1105       if (serial_write (sdi_desc, buf, 9) != 0)
1106         {
1107           if (remote_debug)
1108             fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1109           return 0;
1110         }
1111
1112       c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1113       if (c < 0 || c == '-')
1114         {
1115           if (remote_debug)
1116             fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1117           return 0;
1118         }
1119
1120       ret = recv_data (myaddr, len);
1121     }
1122
1123   if (ret <= 0)
1124     {
1125       if (remote_debug)
1126         fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() fails\n");
1127       return 0;
1128     }
1129
1130   return ret;
1131 }
1132
1133 static void
1134 m32r_kill (void)
1135 {
1136   if (remote_debug)
1137     fprintf_unfiltered (gdb_stdlog, "m32r_kill()\n");
1138
1139   inferior_ptid = null_ptid;
1140   delete_thread_silent (remote_m32r_ptid);
1141
1142   return;
1143 }
1144
1145 /* Clean up when a program exits.
1146
1147    The program actually lives on in the remote processor's RAM, and may be
1148    run again without a download.  Don't leave it full of breakpoint
1149    instructions.  */
1150
1151 static void
1152 m32r_mourn_inferior (struct target_ops *ops)
1153 {
1154   if (remote_debug)
1155     fprintf_unfiltered (gdb_stdlog, "m32r_mourn_inferior()\n");
1156
1157   remove_breakpoints ();
1158   generic_mourn_inferior ();
1159 }
1160
1161 static int
1162 m32r_insert_breakpoint (struct bp_target_info *bp_tgt)
1163 {
1164   CORE_ADDR addr = bp_tgt->placed_address;
1165   int ib_breakpoints;
1166   unsigned char buf[13];
1167   int i, c;
1168
1169   if (remote_debug)
1170     fprintf_unfiltered (gdb_stdlog, "m32r_insert_breakpoint(%s,...)\n",
1171                         paddr (addr));
1172
1173   if (use_ib_breakpoints)
1174     ib_breakpoints = max_ib_breakpoints;
1175   else
1176     ib_breakpoints = 0;
1177
1178   for (i = 0; i < MAX_BREAKPOINTS; i++)
1179     {
1180       if (bp_address[i] == 0xffffffff)
1181         {
1182           bp_address[i] = addr;
1183           if (i >= ib_breakpoints)
1184             {
1185               buf[0] = SDI_READ_MEMORY;
1186               if (mmu_on)
1187                 store_long_parameter (buf + 1, addr & 0xfffffffc);
1188               else
1189                 store_long_parameter (buf + 1, addr & 0x7ffffffc);
1190               store_long_parameter (buf + 5, 4);
1191               serial_write (sdi_desc, buf, 9);
1192               c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1193               if (c != '-')
1194                 recv_data (bp_data[i], 4);
1195             }
1196           return 0;
1197         }
1198     }
1199
1200   error (_("Too many breakpoints"));
1201   return 1;
1202 }
1203
1204 static int
1205 m32r_remove_breakpoint (struct bp_target_info *bp_tgt)
1206 {
1207   CORE_ADDR addr = bp_tgt->placed_address;
1208   int i;
1209
1210   if (remote_debug)
1211     fprintf_unfiltered (gdb_stdlog, "m32r_remove_breakpoint(%s)\n",
1212                         paddr (addr));
1213
1214   for (i = 0; i < MAX_BREAKPOINTS; i++)
1215     {
1216       if (bp_address[i] == addr)
1217         {
1218           bp_address[i] = 0xffffffff;
1219           break;
1220         }
1221     }
1222
1223   return 0;
1224 }
1225
1226 static void
1227 m32r_load (char *args, int from_tty)
1228 {
1229   struct cleanup *old_chain;
1230   asection *section;
1231   bfd *pbfd;
1232   bfd_vma entry;
1233   char *filename;
1234   int quiet;
1235   int nostart;
1236   struct timeval start_time, end_time;
1237   unsigned long data_count;     /* Number of bytes transferred to memory */
1238   int ret;
1239   static RETSIGTYPE (*prev_sigint) ();
1240
1241   /* for direct tcp connections, we can do a fast binary download */
1242   quiet = 0;
1243   nostart = 0;
1244   filename = NULL;
1245
1246   while (*args != '\000')
1247     {
1248       char *arg;
1249
1250       while (isspace (*args))
1251         args++;
1252
1253       arg = args;
1254
1255       while ((*args != '\000') && !isspace (*args))
1256         args++;
1257
1258       if (*args != '\000')
1259         *args++ = '\000';
1260
1261       if (*arg != '-')
1262         filename = arg;
1263       else if (strncmp (arg, "-quiet", strlen (arg)) == 0)
1264         quiet = 1;
1265       else if (strncmp (arg, "-nostart", strlen (arg)) == 0)
1266         nostart = 1;
1267       else
1268         error (_("Unknown option `%s'"), arg);
1269     }
1270
1271   if (!filename)
1272     filename = get_exec_file (1);
1273
1274   pbfd = bfd_openr (filename, gnutarget);
1275   if (pbfd == NULL)
1276     {
1277       perror_with_name (filename);
1278       return;
1279     }
1280   old_chain = make_cleanup_bfd_close (pbfd);
1281
1282   if (!bfd_check_format (pbfd, bfd_object))
1283     error (_("\"%s\" is not an object file: %s"), filename,
1284            bfd_errmsg (bfd_get_error ()));
1285
1286   gettimeofday (&start_time, NULL);
1287   data_count = 0;
1288
1289   interrupted = 0;
1290   prev_sigint = signal (SIGINT, gdb_cntrl_c);
1291
1292   for (section = pbfd->sections; section; section = section->next)
1293     {
1294       if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
1295         {
1296           bfd_vma section_address;
1297           bfd_size_type section_size;
1298           file_ptr fptr;
1299           int n;
1300
1301           section_address = bfd_section_lma (pbfd, section);
1302           section_size = bfd_get_section_size (section);
1303
1304           if (!mmu_on)
1305             {
1306               if ((section_address & 0xa0000000) == 0x80000000)
1307                 section_address &= 0x7fffffff;
1308             }
1309
1310           if (!quiet)
1311             printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1312                              bfd_get_section_name (pbfd, section),
1313                              (unsigned long) section_address,
1314                              (int) section_size);
1315
1316           fptr = 0;
1317
1318           data_count += section_size;
1319
1320           n = 0;
1321           while (section_size > 0)
1322             {
1323               char unsigned buf[0x1000 + 9];
1324               int count;
1325
1326               count = min (section_size, 0x1000);
1327
1328               buf[0] = SDI_WRITE_MEMORY;
1329               store_long_parameter (buf + 1, section_address);
1330               store_long_parameter (buf + 5, count);
1331
1332               bfd_get_section_contents (pbfd, section, buf + 9, fptr, count);
1333               if (send_data (buf, count + 9) <= 0)
1334                 error (_("Error while downloading %s section."),
1335                        bfd_get_section_name (pbfd, section));
1336
1337               if (!quiet)
1338                 {
1339                   printf_unfiltered (".");
1340                   if (n++ > 60)
1341                     {
1342                       printf_unfiltered ("\n");
1343                       n = 0;
1344                     }
1345                   gdb_flush (gdb_stdout);
1346                 }
1347
1348               section_address += count;
1349               fptr += count;
1350               section_size -= count;
1351
1352               if (interrupted)
1353                 break;
1354             }
1355
1356           if (!quiet && !interrupted)
1357             {
1358               printf_unfiltered ("done.\n");
1359               gdb_flush (gdb_stdout);
1360             }
1361         }
1362
1363       if (interrupted)
1364         {
1365           printf_unfiltered ("Interrupted.\n");
1366           break;
1367         }
1368     }
1369
1370   interrupted = 0;
1371   signal (SIGINT, prev_sigint);
1372
1373   gettimeofday (&end_time, NULL);
1374
1375   /* Make the PC point at the start address */
1376   if (exec_bfd)
1377     write_pc (bfd_get_start_address (exec_bfd));
1378
1379   inferior_ptid = null_ptid;    /* No process now */
1380   delete_thread_silent (remote_m32r_ptid);
1381
1382   /* This is necessary because many things were based on the PC at the time
1383      that we attached to the monitor, which is no longer valid now that we
1384      have loaded new code (and just changed the PC).  Another way to do this
1385      might be to call normal_stop, except that the stack may not be valid,
1386      and things would get horribly confused... */
1387
1388   clear_symtab_users ();
1389
1390   if (!nostart)
1391     {
1392       entry = bfd_get_start_address (pbfd);
1393
1394       if (!quiet)
1395         printf_unfiltered ("[Starting %s at 0x%lx]\n", filename,
1396                            (unsigned long) entry);
1397     }
1398
1399   print_transfer_performance (gdb_stdout, data_count, 0, &start_time,
1400                               &end_time);
1401
1402   do_cleanups (old_chain);
1403 }
1404
1405 static void
1406 m32r_stop (ptid_t ptid)
1407 {
1408   if (remote_debug)
1409     fprintf_unfiltered (gdb_stdlog, "m32r_stop()\n");
1410
1411   send_cmd (SDI_STOP_CPU);
1412
1413   return;
1414 }
1415
1416
1417 /* Tell whether this target can support a hardware breakpoint.  CNT
1418    is the number of hardware breakpoints already installed.  This
1419    implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro.  */
1420
1421 int
1422 m32r_can_use_hw_watchpoint (int type, int cnt, int othertype)
1423 {
1424   return sdi_desc != NULL && cnt < max_access_breaks;
1425 }
1426
1427 /* Set a data watchpoint.  ADDR and LEN should be obvious.  TYPE is 0
1428    for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1429    watchpoint. */
1430
1431 int
1432 m32r_insert_watchpoint (CORE_ADDR addr, int len, int type)
1433 {
1434   int i;
1435
1436   if (remote_debug)
1437     fprintf_unfiltered (gdb_stdlog, "m32r_insert_watchpoint(%s,%d,%d)\n",
1438                         paddr (addr), len, type);
1439
1440   for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1441     {
1442       if (ab_address[i] == 0x00000000)
1443         {
1444           ab_address[i] = addr;
1445           ab_size[i] = len;
1446           ab_type[i] = type;
1447           return 0;
1448         }
1449     }
1450
1451   error (_("Too many watchpoints"));
1452   return 1;
1453 }
1454
1455 int
1456 m32r_remove_watchpoint (CORE_ADDR addr, int len, int type)
1457 {
1458   int i;
1459
1460   if (remote_debug)
1461     fprintf_unfiltered (gdb_stdlog, "m32r_remove_watchpoint(%s,%d,%d)\n",
1462                         paddr (addr), len, type);
1463
1464   for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1465     {
1466       if (ab_address[i] == addr)
1467         {
1468           ab_address[i] = 0x00000000;
1469           break;
1470         }
1471     }
1472
1473   return 0;
1474 }
1475
1476 int
1477 m32r_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1478 {
1479   int rc = 0;
1480   if (hit_watchpoint_addr != 0x00000000)
1481     {
1482       *addr_p = hit_watchpoint_addr;
1483       rc = 1;
1484     }
1485   return rc;
1486 }
1487
1488 int
1489 m32r_stopped_by_watchpoint (void)
1490 {
1491   CORE_ADDR addr;
1492   return m32r_stopped_data_address (&current_target, &addr);
1493 }
1494
1495 /* Check to see if a thread is still alive.  */
1496
1497 static int
1498 m32r_thread_alive (ptid_t ptid)
1499 {
1500   if (ptid_equal (ptid, remote_m32r_ptid))
1501     /* The main task is always alive.  */
1502     return 1;
1503
1504   return 0;
1505 }
1506
1507 /* Convert a thread ID to a string.  Returns the string in a static
1508    buffer.  */
1509
1510 static char *
1511 m32r_pid_to_str (struct target_ops *ops, ptid_t ptid)
1512 {
1513   static char buf[64];
1514
1515   if (ptid_equal (remote_m32r_ptid, ptid))
1516     {
1517       xsnprintf (buf, sizeof buf, "Thread <main>");
1518       return buf;
1519     }
1520
1521   return normal_pid_to_str (ptid);
1522 }
1523
1524 static void
1525 sdireset_command (char *args, int from_tty)
1526 {
1527   if (remote_debug)
1528     fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1529
1530   send_cmd (SDI_OPEN);
1531
1532   inferior_ptid = null_ptid;
1533   delete_thread_silent (remote_m32r_ptid);
1534 }
1535
1536
1537 static void
1538 sdistatus_command (char *args, int from_tty)
1539 {
1540   unsigned char buf[4096];
1541   int i, c;
1542
1543   if (remote_debug)
1544     fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1545
1546   if (!sdi_desc)
1547     return;
1548
1549   send_cmd (SDI_STATUS);
1550   for (i = 0; i < 4096; i++)
1551     {
1552       c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1553       if (c < 0)
1554         return;
1555       buf[i] = c;
1556       if (c == 0)
1557         break;
1558     }
1559
1560   printf_filtered ("%s", buf);
1561 }
1562
1563
1564 static void
1565 debug_chaos_command (char *args, int from_tty)
1566 {
1567   unsigned char buf[3];
1568
1569   buf[0] = SDI_SET_ATTR;
1570   buf[1] = SDI_ATTR_CACHE;
1571   buf[2] = SDI_CACHE_TYPE_CHAOS;
1572   send_data (buf, 3);
1573 }
1574
1575
1576 static void
1577 use_debug_dma_command (char *args, int from_tty)
1578 {
1579   unsigned char buf[3];
1580
1581   buf[0] = SDI_SET_ATTR;
1582   buf[1] = SDI_ATTR_MEM_ACCESS;
1583   buf[2] = SDI_MEM_ACCESS_DEBUG_DMA;
1584   send_data (buf, 3);
1585 }
1586
1587 static void
1588 use_mon_code_command (char *args, int from_tty)
1589 {
1590   unsigned char buf[3];
1591
1592   buf[0] = SDI_SET_ATTR;
1593   buf[1] = SDI_ATTR_MEM_ACCESS;
1594   buf[2] = SDI_MEM_ACCESS_MON_CODE;
1595   send_data (buf, 3);
1596 }
1597
1598
1599 static void
1600 use_ib_breakpoints_command (char *args, int from_tty)
1601 {
1602   use_ib_breakpoints = 1;
1603 }
1604
1605 static void
1606 use_dbt_breakpoints_command (char *args, int from_tty)
1607 {
1608   use_ib_breakpoints = 0;
1609 }
1610
1611
1612 /* Define the target subroutine names */
1613
1614 struct target_ops m32r_ops;
1615
1616 static void
1617 init_m32r_ops (void)
1618 {
1619   m32r_ops.to_shortname = "m32rsdi";
1620   m32r_ops.to_longname = "Remote M32R debugging over SDI interface";
1621   m32r_ops.to_doc = "Use an M32R board using SDI debugging protocol.";
1622   m32r_ops.to_open = m32r_open;
1623   m32r_ops.to_close = m32r_close;
1624   m32r_ops.to_detach = m32r_detach;
1625   m32r_ops.to_resume = m32r_resume;
1626   m32r_ops.to_wait = m32r_wait;
1627   m32r_ops.to_fetch_registers = m32r_fetch_register;
1628   m32r_ops.to_store_registers = m32r_store_register;
1629   m32r_ops.to_prepare_to_store = m32r_prepare_to_store;
1630   m32r_ops.deprecated_xfer_memory = m32r_xfer_memory;
1631   m32r_ops.to_files_info = m32r_files_info;
1632   m32r_ops.to_insert_breakpoint = m32r_insert_breakpoint;
1633   m32r_ops.to_remove_breakpoint = m32r_remove_breakpoint;
1634   m32r_ops.to_can_use_hw_breakpoint = m32r_can_use_hw_watchpoint;
1635   m32r_ops.to_insert_watchpoint = m32r_insert_watchpoint;
1636   m32r_ops.to_remove_watchpoint = m32r_remove_watchpoint;
1637   m32r_ops.to_stopped_by_watchpoint = m32r_stopped_by_watchpoint;
1638   m32r_ops.to_stopped_data_address = m32r_stopped_data_address;
1639   m32r_ops.to_kill = m32r_kill;
1640   m32r_ops.to_load = m32r_load;
1641   m32r_ops.to_create_inferior = m32r_create_inferior;
1642   m32r_ops.to_mourn_inferior = m32r_mourn_inferior;
1643   m32r_ops.to_stop = m32r_stop;
1644   m32r_ops.to_log_command = serial_log_command;
1645   m32r_ops.to_thread_alive = m32r_thread_alive;
1646   m32r_ops.to_pid_to_str = m32r_pid_to_str;
1647   m32r_ops.to_stratum = process_stratum;
1648   m32r_ops.to_has_all_memory = 1;
1649   m32r_ops.to_has_memory = 1;
1650   m32r_ops.to_has_stack = 1;
1651   m32r_ops.to_has_registers = 1;
1652   m32r_ops.to_has_execution = 1;
1653   m32r_ops.to_magic = OPS_MAGIC;
1654 };
1655
1656
1657 extern initialize_file_ftype _initialize_remote_m32r;
1658
1659 void
1660 _initialize_remote_m32r (void)
1661 {
1662   int i;
1663
1664   init_m32r_ops ();
1665
1666   /* Initialize breakpoints. */
1667   for (i = 0; i < MAX_BREAKPOINTS; i++)
1668     bp_address[i] = 0xffffffff;
1669
1670   /* Initialize access breaks. */
1671   for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1672     ab_address[i] = 0x00000000;
1673
1674   add_target (&m32r_ops);
1675
1676   add_com ("sdireset", class_obscure, sdireset_command,
1677            _("Reset SDI connection."));
1678
1679   add_com ("sdistatus", class_obscure, sdistatus_command,
1680            _("Show status of SDI connection."));
1681
1682   add_com ("debug_chaos", class_obscure, debug_chaos_command,
1683            _("Debug M32R/Chaos."));
1684
1685   add_com ("use_debug_dma", class_obscure, use_debug_dma_command,
1686            _("Use debug DMA mem access."));
1687   add_com ("use_mon_code", class_obscure, use_mon_code_command,
1688            _("Use mon code mem access."));
1689
1690   add_com ("use_ib_break", class_obscure, use_ib_breakpoints_command,
1691            _("Set breakpoints by IB break."));
1692   add_com ("use_dbt_break", class_obscure, use_dbt_breakpoints_command,
1693            _("Set breakpoints by dbt."));
1694
1695   /* Yes, 42000 is arbitrary.  The only sense out of it, is that it
1696      isn't 0.  */
1697   remote_m32r_ptid = ptid_build (42000, 0, 42000);
1698 }