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