1 /* Target communications support for Macraigor Systems' On-Chip Debugging
2 Copyright 1996, 1997, 2001 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "gdb_string.h"
32 #include "gdb-stabs.h"
33 #include <sys/types.h>
38 /* Prototypes for local functions */
40 static int ocd_read_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
);
42 static int ocd_start_remote (PTR dummy
);
44 static int readchar (int timeout
);
46 static void reset_packet (void);
48 static void output_packet (void);
50 static int get_quoted_char (int timeout
);
52 static void put_quoted_char (int c
);
54 static void ocd_interrupt (int signo
);
56 static void ocd_interrupt_twice (int signo
);
58 static void interrupt_query (void);
60 static unsigned char *ocd_do_command (int cmd
, int *statusp
, int *lenp
);
62 static void ocd_put_packet (unsigned char *packet
, int pktlen
);
64 static unsigned char *ocd_get_packet (int cmd
, int *pktlen
, int timeout
);
66 static struct target_ops
*current_ops
= NULL
;
68 static int last_run_status
;
70 /* This was 5 seconds, which is a long time to sit and wait.
71 Unless this is going though some terminal server or multiplexer or
72 other form of hairy serial connection, I would think 2 seconds would
76 /* FIXME: Change to allow option to set timeout value on a per target
78 static int remote_timeout
= 2;
81 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
82 ocd_open knows that we don't have a file open when the program
84 static serial_t ocd_desc
= NULL
;
87 ocd_error (char *s
, int error_code
)
91 fputs_filtered (s
, gdb_stderr
);
92 fputs_filtered (" ", gdb_stderr
);
103 s
= "Cable disconnected";
106 s
= "Couldn't enter OCD mode";
109 s
= "Target stuck in reset";
112 s
= "OCD hasn't been initialized";
115 s
= "Write verify failed";
118 s
= "Reg buff error (during MPC5xx fp reg read/write)";
121 s
= "Invalid CPU register access attempt failed";
127 s
= "Checksum error";
130 s
= "Illegal command";
133 s
= "Parameter error";
136 s
= "Internal error";
139 s
= "Flash erase error";
142 sprintf (buf
, "Unknown error code %d", error_code
);
149 /* Return nonzero if the thread TH is still alive on the remote system. */
152 ocd_thread_alive (int th
)
157 /* Clean up connection to a remote debugger. */
161 ocd_close (int quitting
)
164 SERIAL_CLOSE (ocd_desc
);
168 /* Stub for catch_errors. */
171 ocd_start_remote (PTR dummy
)
173 unsigned char buf
[10], *p
;
178 enum ocd_target_type target_type
;
180 target_type
= *(enum ocd_target_type
*) dummy
;
182 immediate_quit
++; /* Allow user to interrupt it */
184 SERIAL_SEND_BREAK (ocd_desc
); /* Wake up the wiggler */
186 speed
= 80; /* Divide clock by 4000 */
190 buf
[2] = speed
& 0xff;
191 buf
[3] = target_type
;
192 ocd_put_packet (buf
, 4); /* Init OCD params */
193 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
196 error ("Truncated response packet from OCD device");
202 ocd_error ("OCD_INIT:", error_code
);
204 ocd_do_command (OCD_AYT
, &status
, &pktlen
);
206 p
= ocd_do_command (OCD_GET_VERSION
, &status
, &pktlen
);
208 printf_unfiltered ("[Wiggler version %x.%x, capability 0x%x]\n",
209 p
[0], p
[1], (p
[2] << 16) | p
[3]);
212 /* Reset the target */
214 ocd_do_command (OCD_RESET_RUN
, &status
, &pktlen
);
215 /* ocd_do_command (OCD_RESET, &status, &pktlen); */
218 /* If processor is still running, stop it. */
220 if (!(status
& OCD_FLAG_BDM
))
224 /* When using a target box, we want to asynchronously return status when
225 target stops. The OCD_SET_CTL_FLAGS command is ignored by Wigglers.dll
226 when using a parallel Wiggler */
227 buf
[0] = OCD_SET_CTL_FLAGS
;
230 ocd_put_packet (buf
, 3);
232 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
235 error ("Truncated response packet from OCD device");
241 ocd_error ("OCD_SET_CTL_FLAGS:", error_code
);
246 /* This is really the job of start_remote however, that makes an assumption
247 that the target is about to print out a status message of some sort. That
248 doesn't happen here (in fact, it may not be possible to get the monitor to
249 send the appropriate packet). */
251 flush_cached_frames ();
252 registers_changed ();
253 stop_pc
= read_pc ();
254 set_current_frame (create_new_frame (read_fp (), stop_pc
));
255 select_frame (get_current_frame (), 0);
256 print_stack_frame (selected_frame
, -1, 1);
258 buf
[0] = OCD_LOG_FILE
;
259 buf
[1] = 3; /* close existing WIGGLERS.LOG */
260 ocd_put_packet (buf
, 2);
261 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
263 buf
[0] = OCD_LOG_FILE
;
264 buf
[1] = 2; /* append to existing WIGGLERS.LOG */
265 ocd_put_packet (buf
, 2);
266 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
271 /* Open a connection to a remote debugger.
272 NAME is the filename used for communication. */
275 ocd_open (char *name
, int from_tty
, enum ocd_target_type target_type
,
276 struct target_ops
*ops
)
278 unsigned char buf
[10], *p
;
282 error ("To open an OCD connection, you need to specify the\n\
283 device the OCD device is attached to (e.g. /dev/ttya).");
285 target_preopen (from_tty
);
289 unpush_target (current_ops
);
291 if (strncmp (name
, "wiggler", 7) == 0)
293 ocd_desc
= SERIAL_OPEN ("ocd");
295 perror_with_name (name
);
297 buf
[0] = OCD_LOG_FILE
;
298 buf
[1] = 1; /* open new or overwrite existing WIGGLERS.LOG */
299 ocd_put_packet (buf
, 2);
300 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
302 buf
[0] = OCD_SET_CONNECTION
;
303 buf
[1] = 0x01; /* atoi (name[11]); */
304 ocd_put_packet (buf
, 2);
305 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
308 /* not using Wigglers.dll */
310 ocd_desc
= SERIAL_OPEN (name
);
312 perror_with_name (name
);
317 if (SERIAL_SETBAUDRATE (ocd_desc
, baud_rate
))
319 SERIAL_CLOSE (ocd_desc
);
320 perror_with_name (name
);
324 SERIAL_RAW (ocd_desc
);
326 /* If there is something sitting in the buffer we might take it as a
327 response to a command, which would be bad. */
328 SERIAL_FLUSH_INPUT (ocd_desc
);
332 puts_filtered ("Remote target wiggler connected to ");
333 puts_filtered (name
);
334 puts_filtered ("\n");
336 push_target (current_ops
); /* Switch to using remote target now */
338 /* Without this, some commands which require an active target (such as kill)
339 won't work. This variable serves (at least) double duty as both the pid
340 of the target process (if it has such), and as a flag indicating that a
341 target is active. These functions should be split out into seperate
342 variables, especially since GDB will someday have a notion of debugging
343 several processes. */
345 inferior_pid
= 42000;
346 /* Start the remote connection; if error (0), discard this target.
347 In particular, if the user quits, be sure to discard it
348 (we'd be in an inconsistent state otherwise). */
349 if (!catch_errors (ocd_start_remote
, &target_type
,
350 "Couldn't establish connection to remote target\n",
354 error ("Failed to connect to OCD.");
358 /* This takes a program previously attached to and detaches it. After
359 this is done, GDB can be used to debug some other program. We
360 better not have left any breakpoints in the target program or it'll
361 die when it hits one. */
364 ocd_detach (char *args
, int from_tty
)
367 error ("Argument given to \"detach\" when remotely debugging.");
371 puts_filtered ("Ending remote debugging.\n");
374 /* Tell the remote machine to resume. */
377 ocd_resume (int pid
, int step
, enum target_signal siggnal
)
382 ocd_do_command (OCD_STEP
, &last_run_status
, &pktlen
);
384 ocd_do_command (OCD_RUN
, &last_run_status
, &pktlen
);
393 ocd_do_command (OCD_STOP
, &status
, &pktlen
);
395 if (!(status
& OCD_FLAG_BDM
))
396 error ("Can't stop target via BDM");
399 static volatile int ocd_interrupt_flag
;
401 /* Send ^C to target to halt it. Target will respond, and send us a
405 ocd_interrupt (int signo
)
407 /* If this doesn't work, try more severe steps. */
408 signal (signo
, ocd_interrupt_twice
);
411 printf_unfiltered ("ocd_interrupt called\n");
418 ocd_put_packet (buf
, 1);
419 ocd_interrupt_flag
= 1;
423 static void (*ofunc
) ();
425 /* The user typed ^C twice. */
427 ocd_interrupt_twice (int signo
)
429 signal (signo
, ofunc
);
433 signal (signo
, ocd_interrupt
);
436 /* Ask the user what to do when an interrupt is received. */
439 interrupt_query (void)
441 target_terminal_ours ();
443 if (query ("Interrupted while waiting for the program.\n\
444 Give up (and stop debugging it)? "))
446 target_mourn_inferior ();
447 return_to_top_level (RETURN_QUIT
);
450 target_terminal_inferior ();
453 /* If nonzero, ignore the next kill. */
454 static int kill_kludge
;
456 /* Wait until the remote machine stops, then return,
457 storing status in STATUS just as `wait' would.
458 Returns "pid" (though it's not clear what, if anything, that
459 means in the case of this target). */
469 ocd_interrupt_flag
= 0;
471 /* Target might already be stopped by the time we get here. */
472 /* If we aren't already stopped, we need to loop until we've dropped
473 back into BDM mode */
475 while (!(last_run_status
& OCD_FLAG_BDM
))
478 ocd_put_packet (buf
, 1);
479 p
= ocd_get_packet (OCD_AYT
, &pktlen
, -1);
481 ofunc
= (void (*)()) signal (SIGINT
, ocd_interrupt
);
482 signal (SIGINT
, ofunc
);
485 error ("Truncated response packet from OCD device");
487 last_run_status
= p
[1];
491 ocd_error ("target_wait:", error_code
);
493 if (last_run_status
& OCD_FLAG_PWF
)
494 error ("OCD device lost VCC at BDM interface.");
495 else if (last_run_status
& OCD_FLAG_CABLE_DISC
)
496 error ("OCD device cable appears to have been disconnected.");
499 if (ocd_interrupt_flag
)
505 /* Read registers from the OCD device. Specify the starting and ending
506 register number. Return the number of regs actually read in *NUMREGS.
507 Returns a pointer to a static array containing the register contents. */
510 ocd_read_bdm_registers (int first_bdm_regno
, int last_bdm_regno
, int *reglen
)
512 unsigned char buf
[10];
516 int error_code
, status
;
519 buf
[0] = OCD_READ_REGS
;
520 buf
[1] = first_bdm_regno
>> 8;
521 buf
[2] = first_bdm_regno
& 0xff;
522 buf
[3] = last_bdm_regno
>> 8;
523 buf
[4] = last_bdm_regno
& 0xff;
525 ocd_put_packet (buf
, 5);
526 p
= ocd_get_packet (OCD_READ_REGS
, &pktlen
, remote_timeout
);
532 ocd_error ("read_bdm_registers:", error_code
);
540 error ("Register block size bad: %d", i
);
549 /* Read register BDM_REGNO and returns its value ala read_register() */
552 ocd_read_bdm_register (int bdm_regno
)
558 p
= ocd_read_bdm_registers (bdm_regno
, bdm_regno
, ®len
);
559 regval
= extract_unsigned_integer (p
, reglen
);
565 ocd_write_bdm_registers (int first_bdm_regno
, unsigned char *regptr
, int reglen
)
569 int error_code
, status
;
572 buf
= alloca (4 + reglen
);
574 buf
[0] = OCD_WRITE_REGS
;
575 buf
[1] = first_bdm_regno
>> 8;
576 buf
[2] = first_bdm_regno
& 0xff;
578 memcpy (buf
+ 4, regptr
, reglen
);
580 ocd_put_packet (buf
, 4 + reglen
);
581 p
= ocd_get_packet (OCD_WRITE_REGS
, &pktlen
, remote_timeout
);
584 error ("Truncated response packet from OCD device");
590 ocd_error ("ocd_write_bdm_registers:", error_code
);
594 ocd_write_bdm_register (int bdm_regno
, CORE_ADDR reg
)
596 unsigned char buf
[4];
598 store_unsigned_integer (buf
, 4, reg
);
600 ocd_write_bdm_registers (bdm_regno
, buf
, 4);
604 ocd_prepare_to_store (void)
608 /* Write memory data directly to the remote machine.
609 This does not inform the data cache; the data cache uses this.
610 MEMADDR is the address in the remote memory space.
611 MYADDR is the address of the buffer in our space.
612 LEN is the number of bytes.
614 Returns number of bytes transferred, or 0 for error. */
616 static int write_mem_command
= OCD_WRITE_MEM
;
619 ocd_write_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
)
627 buf
[0] = write_mem_command
;
628 buf
[5] = 1; /* Write as bytes */
629 buf
[6] = 0; /* Don't verify */
635 int status
, error_code
;
637 numbytes
= min (len
, 256 - 8);
639 buf
[1] = memaddr
>> 24;
640 buf
[2] = memaddr
>> 16;
641 buf
[3] = memaddr
>> 8;
646 memcpy (&buf
[8], myaddr
, numbytes
);
647 ocd_put_packet (buf
, 8 + numbytes
);
648 p
= ocd_get_packet (OCD_WRITE_MEM
, &pktlen
, remote_timeout
);
650 error ("Truncated response packet from OCD device");
655 if (error_code
== 0x11) /* Got a bus error? */
657 CORE_ADDR error_address
;
659 error_address
= p
[3] << 24;
660 error_address
|= p
[4] << 16;
661 error_address
|= p
[5] << 8;
662 error_address
|= p
[6];
663 numbytes
= error_address
- memaddr
;
671 else if (error_code
!= 0)
672 ocd_error ("ocd_write_bytes:", error_code
);
679 return origlen
- len
;
682 /* Read memory data directly from the remote machine.
683 This does not use the data cache; the data cache uses this.
684 MEMADDR is the address in the remote memory space.
685 MYADDR is the address of the buffer in our space.
686 LEN is the number of bytes.
688 Returns number of bytes transferred, or 0 for error. */
691 ocd_read_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
)
699 buf
[0] = OCD_READ_MEM
;
700 buf
[5] = 1; /* Read as bytes */
706 int status
, error_code
;
708 numbytes
= min (len
, 256 - 7);
710 buf
[1] = memaddr
>> 24;
711 buf
[2] = memaddr
>> 16;
712 buf
[3] = memaddr
>> 8;
717 ocd_put_packet (buf
, 7);
718 p
= ocd_get_packet (OCD_READ_MEM
, &pktlen
, remote_timeout
);
720 error ("Truncated response packet from OCD device");
725 if (error_code
== 0x11) /* Got a bus error? */
727 CORE_ADDR error_address
;
729 error_address
= p
[3] << 24;
730 error_address
|= p
[4] << 16;
731 error_address
|= p
[5] << 8;
732 error_address
|= p
[6];
733 numbytes
= error_address
- memaddr
;
741 else if (error_code
!= 0)
742 ocd_error ("ocd_read_bytes:", error_code
);
744 memcpy (myaddr
, &p
[4], numbytes
);
751 return origlen
- len
;
754 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
755 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
756 nonzero. Returns length of data written or read; 0 for error. TARGET
761 ocd_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int should_write
,
762 struct target_ops
*target
)
767 res
= ocd_write_bytes (memaddr
, myaddr
, len
);
769 res
= ocd_read_bytes (memaddr
, myaddr
, len
);
775 ocd_files_info (struct target_ops
*ignore
)
777 puts_filtered ("Debugging a target over a serial line.\n");
780 /* Stuff for dealing with the packets which are part of this protocol.
781 See comment at top of file for details. */
783 /* Read a single character from the remote side, handling wierd errors. */
786 readchar (int timeout
)
790 ch
= SERIAL_READCHAR (ocd_desc
, timeout
);
795 error ("Remote connection closed");
797 perror_with_name ("Remote communication error");
805 /* Read a character from the data stream, dequoting as necessary. SYN is
806 treated special. Any SYNs appearing in the data stream are returned as the
807 distinct value RAW_SYN (which has a value > 8 bits and therefore cannot be
808 mistaken for real data). */
811 get_quoted_char (int timeout
)
815 ch
= readchar (timeout
);
820 error ("Timeout in mid-packet, aborting");
824 ch
= readchar (timeout
);
833 static unsigned char pkt
[256 * 2 + 10], *pktp
; /* Worst case */
844 if (SERIAL_WRITE (ocd_desc
, pkt
, pktp
- pkt
))
845 perror_with_name ("output_packet: write failed");
850 /* Output a quoted character. SYNs and DLEs are quoted. Everything else goes
851 through untouched. */
854 put_quoted_char (int c
)
867 /* Send a packet to the OCD device. The packet framed by a SYN character,
868 a byte count and a checksum. The byte count only counts the number of
869 bytes between the count and the checksum. A count of zero actually
870 means 256. Any SYNs within the packet (including the checksum and
871 count) must be quoted. The quote character must be quoted as well.
872 Quoting is done by replacing the character with the two-character sequence
873 DLE, {char} | 0100. Note that the quoting mechanism has no effect on the
877 stu_put_packet (unsigned char *buf
, int len
)
879 unsigned char checksum
;
882 if (len
== 0 || len
> 256)
883 internal_error (__FILE__
, __LINE__
, "failed internal consistency check"); /* Can't represent 0 length packet */
889 put_quoted_char (RAW_SYN
);
903 put_quoted_char (-checksum
& 0xff);
910 /* Send a packet to the OCD device. The packet framed by a SYN character,
911 a byte count and a checksum. The byte count only counts the number of
912 bytes between the count and the checksum. A count of zero actually
913 means 256. Any SYNs within the packet (including the checksum and
914 count) must be quoted. The quote character must be quoted as well.
915 Quoting is done by replacing the character with the two-character sequence
916 DLE, {char} | 0100. Note that the quoting mechanism has no effect on the
920 ocd_put_packet (unsigned char *buf
, int len
)
922 unsigned char checksum
;
924 unsigned char *packet
, *packet_ptr
;
926 packet
= alloca (len
+ 1 + 1); /* packet + SYN + checksum */
931 *packet_ptr
++ = 0x55;
941 *packet_ptr
++ = -checksum
;
942 if (SERIAL_WRITE (ocd_desc
, packet
, packet_ptr
- packet
))
943 perror_with_name ("output_packet: write failed");
948 /* Get a packet from the OCD device. Timeout is only enforced for the
949 first byte of the packet. Subsequent bytes are expected to arrive in
950 time <= remote_timeout. Returns a pointer to a static buffer containing
951 the payload of the packet. *LENP contains the length of the packet.
954 static unsigned char *
955 stu_get_packet (unsigned char cmd
, int *lenp
, int timeout
)
959 static unsigned char buf
[256 + 10], *p
;
960 unsigned char checksum
;
964 ch
= get_quoted_char (timeout
);
967 error ("get_packet (readchar): %d", ch
);
972 found_syn
: /* Found the start of a packet */
977 len
= get_quoted_char (remote_timeout
);
987 len
++; /* Include checksum */
991 ch
= get_quoted_char (remote_timeout
);
1003 error ("Response phase error. Got 0x%x, expected 0x%x", buf
[0], cmd
);
1005 *lenp
= p
- buf
- 1;
1011 /* Get a packet from the OCD device. Timeout is only enforced for the
1012 first byte of the packet. Subsequent bytes are expected to arrive in
1013 time <= remote_timeout. Returns a pointer to a static buffer containing
1014 the payload of the packet. *LENP contains the length of the packet.
1017 static unsigned char *
1018 ocd_get_packet (int cmd
, int *lenp
, int timeout
)
1022 static unsigned char packet
[512];
1023 unsigned char *packet_ptr
;
1024 unsigned char checksum
;
1026 ch
= readchar (timeout
);
1029 error ("ocd_get_packet (readchar): %d", ch
);
1032 error ("ocd_get_packet (readchar): %d", ch
);
1034 /* Found the start of a packet */
1036 packet_ptr
= packet
;
1039 /* Read command char. That sort of tells us how long the packet is. */
1041 ch
= readchar (timeout
);
1044 error ("ocd_get_packet (readchar): %d", ch
);
1051 ch
= readchar (timeout
);
1054 error ("ocd_get_packet (readchar): %d", ch
);
1058 /* Get error code. */
1060 ch
= readchar (timeout
);
1063 error ("ocd_get_packet (readchar): %d", ch
);
1067 switch (ch
) /* Figure out length of packet */
1069 case 0x7: /* Write verify error? */
1070 len
= 8; /* write address, value read back */
1072 case 0x11: /* Bus error? */
1073 /* write address, read flag */
1074 case 0x15: /* Internal error */
1075 len
= 5; /* error code, vector */
1077 default: /* Error w/no params */
1080 case 0x0: /* Normal result */
1083 case OCD_AYT
: /* Are You There? */
1084 case OCD_SET_BAUD_RATE
: /* Set Baud Rate */
1085 case OCD_INIT
: /* Initialize OCD device */
1086 case OCD_SET_SPEED
: /* Set Speed */
1087 case OCD_SET_FUNC_CODE
: /* Set Function Code */
1088 case OCD_SET_CTL_FLAGS
: /* Set Control Flags */
1089 case OCD_SET_BUF_ADDR
: /* Set Register Buffer Address */
1090 case OCD_RUN
: /* Run Target from PC */
1091 case OCD_RUN_ADDR
: /* Run Target from Specified Address */
1092 case OCD_STOP
: /* Stop Target */
1093 case OCD_RESET_RUN
: /* Reset Target and Run */
1094 case OCD_RESET
: /* Reset Target and Halt */
1095 case OCD_STEP
: /* Single Step */
1096 case OCD_WRITE_REGS
: /* Write Register */
1097 case OCD_WRITE_MEM
: /* Write Memory */
1098 case OCD_FILL_MEM
: /* Fill Memory */
1099 case OCD_MOVE_MEM
: /* Move Memory */
1100 case OCD_WRITE_INT_MEM
: /* Write Internal Memory */
1101 case OCD_JUMP
: /* Jump to Subroutine */
1102 case OCD_ERASE_FLASH
: /* Erase flash memory */
1103 case OCD_PROGRAM_FLASH
: /* Write flash memory */
1104 case OCD_EXIT_MON
: /* Exit the flash programming monitor */
1105 case OCD_ENTER_MON
: /* Enter the flash programming monitor */
1106 case OCD_LOG_FILE
: /* Make Wigglers.dll save Wigglers.log */
1107 case OCD_SET_CONNECTION
: /* Set type of connection in Wigglers.dll */
1110 case OCD_GET_VERSION
: /* Get Version */
1113 case OCD_GET_STATUS_MASK
: /* Get Status Mask */
1116 case OCD_GET_CTRS
: /* Get Error Counters */
1117 case OCD_READ_REGS
: /* Read Register */
1118 case OCD_READ_MEM
: /* Read Memory */
1119 case OCD_READ_INT_MEM
: /* Read Internal Memory */
1123 error ("ocd_get_packet: unknown packet type 0x%x\n", ch
);
1127 if (len
== 257) /* Byte stream? */
1128 { /* Yes, byte streams contain the length */
1129 ch
= readchar (timeout
);
1132 error ("ocd_get_packet (readchar): %d", ch
);
1140 while (len
-- >= 0) /* Do rest of packet and checksum */
1142 ch
= readchar (timeout
);
1145 error ("ocd_get_packet (readchar): %d", ch
);
1151 error ("ocd_get_packet: bad packet checksum");
1153 if (cmd
!= -1 && cmd
!= packet
[0])
1154 error ("Response phase error. Got 0x%x, expected 0x%x", packet
[0], cmd
);
1156 *lenp
= packet_ptr
- packet
- 1; /* Subtract checksum byte */
1161 /* Execute a simple (one-byte) command. Returns a pointer to the data
1162 following the error code. */
1164 static unsigned char *
1165 ocd_do_command (int cmd
, int *statusp
, int *lenp
)
1167 unsigned char buf
[100], *p
;
1168 int status
, error_code
;
1171 unsigned char logbuf
[100];
1175 ocd_put_packet (buf
, 1); /* Send command */
1176 p
= ocd_get_packet (*buf
, lenp
, remote_timeout
);
1179 error ("Truncated response packet from OCD device");
1184 if (error_code
!= 0)
1186 sprintf (errbuf
, "ocd_do_command (0x%x):", cmd
);
1187 ocd_error (errbuf
, error_code
);
1190 if (status
& OCD_FLAG_PWF
)
1191 error ("OCD device can't detect VCC at BDM interface.");
1192 else if (status
& OCD_FLAG_CABLE_DISC
)
1193 error ("BDM cable appears to be disconnected.");
1197 logbuf
[0] = OCD_LOG_FILE
;
1198 logbuf
[1] = 3; /* close existing WIGGLERS.LOG */
1199 ocd_put_packet (logbuf
, 2);
1200 ocd_get_packet (logbuf
[0], &logpktlen
, remote_timeout
);
1202 logbuf
[0] = OCD_LOG_FILE
;
1203 logbuf
[1] = 2; /* append to existing WIGGLERS.LOG */
1204 ocd_put_packet (logbuf
, 2);
1205 ocd_get_packet (logbuf
[0], &logpktlen
, remote_timeout
);
1213 /* For some mysterious reason, wait_for_inferior calls kill instead of
1214 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1218 target_mourn_inferior ();
1222 /* Don't wait for it to die. I'm not really sure it matters whether
1224 target_mourn_inferior ();
1230 unpush_target (current_ops
);
1231 generic_mourn_inferior ();
1234 /* All we actually do is set the PC to the start address of exec_bfd, and start
1235 the program at that point. */
1238 ocd_create_inferior (char *exec_file
, char *args
, char **env
)
1240 if (args
&& (*args
!= '\000'))
1241 error ("Args are not supported by BDM.");
1243 clear_proceed_status ();
1244 proceed (bfd_get_start_address (exec_bfd
), TARGET_SIGNAL_0
, 0);
1248 ocd_load (char *args
, int from_tty
)
1250 generic_load (args
, from_tty
);
1254 /* This is necessary because many things were based on the PC at the time that
1255 we attached to the monitor, which is no longer valid now that we have loaded
1256 new code (and just changed the PC). Another way to do this might be to call
1257 normal_stop, except that the stack may not be valid, and things would get
1258 horribly confused... */
1260 clear_symtab_users ();
1263 /* This should be defined for each target */
1264 /* But we want to be able to compile this file for some configurations
1265 not yet supported fully */
1267 #define BDM_BREAKPOINT {0x0,0x0,0x0,0x0} /* For ppc 8xx */
1269 #define BDM_BREAKPOINT {0x4a,0xfa} /* BGND insn used for CPU32 */
1272 /* BDM (at least on CPU32) uses a different breakpoint */
1275 ocd_insert_breakpoint (CORE_ADDR addr
, char *contents_cache
)
1277 static char break_insn
[] = BDM_BREAKPOINT
;
1280 val
= target_read_memory (addr
, contents_cache
, sizeof (break_insn
));
1283 val
= target_write_memory (addr
, break_insn
, sizeof (break_insn
));
1289 ocd_remove_breakpoint (CORE_ADDR addr
, char *contents_cache
)
1291 static char break_insn
[] = BDM_BREAKPOINT
;
1294 val
= target_write_memory (addr
, contents_cache
, sizeof (break_insn
));
1300 bdm_command (char *args
, int from_tty
)
1302 error ("bdm command must be followed by `reset'");
1306 bdm_reset_command (char *args
, int from_tty
)
1311 error ("Not connected to OCD device.");
1313 ocd_do_command (OCD_RESET
, &status
, &pktlen
);
1314 dcache_invalidate (target_dcache
);
1315 registers_changed ();
1319 bdm_restart_command (char *args
, int from_tty
)
1324 error ("Not connected to OCD device.");
1326 ocd_do_command (OCD_RESET_RUN
, &status
, &pktlen
);
1327 last_run_status
= status
;
1328 clear_proceed_status ();
1329 wait_for_inferior ();
1333 /* Temporary replacement for target_store_registers(). This prevents
1334 generic_load from trying to set the PC. */
1337 noop_store_registers (int regno
)
1342 bdm_update_flash_command (char *args
, int from_tty
)
1345 struct cleanup
*old_chain
;
1346 void (*store_registers_tmp
) (int);
1349 error ("Not connected to OCD device.");
1352 error ("Must specify file containing new OCD code.");
1354 /* old_chain = make_cleanup (flash_cleanup, 0); */
1356 ocd_do_command (OCD_ENTER_MON
, &status
, &pktlen
);
1358 ocd_do_command (OCD_ERASE_FLASH
, &status
, &pktlen
);
1360 write_mem_command
= OCD_PROGRAM_FLASH
;
1361 store_registers_tmp
= current_target
.to_store_registers
;
1362 current_target
.to_store_registers
= noop_store_registers
;
1364 generic_load (args
, from_tty
);
1366 current_target
.to_store_registers
= store_registers_tmp
;
1367 write_mem_command
= OCD_WRITE_MEM
;
1369 ocd_do_command (OCD_EXIT_MON
, &status
, &pktlen
);
1371 /* discard_cleanups (old_chain); */
1375 bdm_read_register_command (char *args
, int from_tty
)
1377 /* XXX repeat should go on to the next register */
1380 error ("Not connected to OCD device.");
1383 error ("Must specify BDM register number.");
1388 _initialize_remote_ocd (void)
1390 extern struct cmd_list_element
*cmdlist
;
1391 static struct cmd_list_element
*ocd_cmd_list
= NULL
;
1393 add_show_from_set (add_set_cmd ("remotetimeout", no_class
,
1394 var_integer
, (char *) &remote_timeout
,
1395 "Set timeout value for remote read.\n", &setlist
),
1398 add_prefix_cmd ("ocd", class_obscure
, bdm_command
, "", &ocd_cmd_list
, "ocd ",
1401 add_cmd ("reset", class_obscure
, bdm_reset_command
, "", &ocd_cmd_list
);
1402 add_cmd ("restart", class_obscure
, bdm_restart_command
, "", &ocd_cmd_list
);
1403 add_cmd ("update-flash", class_obscure
, bdm_update_flash_command
, "", &ocd_cmd_list
);
1404 /* add_cmd ("read-register", class_obscure, bdm_read_register_command, "", &ocd_cmd_list); */