OSDN Git Service

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