1 /* Target communications support for Macraigor Systems' On-Chip Debugging
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
33 #include "gdb-stabs.h"
34 #include <sys/types.h>
40 /* Prototypes for local functions */
42 static int ocd_read_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
);
44 static int ocd_start_remote (PTR dummy
);
46 static int readchar (int timeout
);
48 static void reset_packet (void);
50 static void output_packet (void);
52 static int get_quoted_char (int timeout
);
54 static void put_quoted_char (int c
);
56 static void ocd_interrupt (int signo
);
58 static void ocd_interrupt_twice (int signo
);
60 static void interrupt_query (void);
62 static unsigned char *ocd_do_command (int cmd
, int *statusp
, int *lenp
);
64 static void ocd_put_packet (unsigned char *packet
, int pktlen
);
66 static unsigned char *ocd_get_packet (int cmd
, int *pktlen
, int timeout
);
68 static struct target_ops
*current_ops
= NULL
;
70 static int last_run_status
;
72 /* This was 5 seconds, which is a long time to sit and wait.
73 Unless this is going though some terminal server or multiplexer or
74 other form of hairy serial connection, I would think 2 seconds would
78 /* FIXME: Change to allow option to set timeout value on a per target
80 static int remote_timeout
= 2;
83 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
84 ocd_open knows that we don't have a file open when the program
86 static serial_t ocd_desc
= NULL
;
89 ocd_error (char *s
, int error_code
)
93 fputs_filtered (s
, gdb_stderr
);
94 fputs_filtered (" ", gdb_stderr
);
105 s
= "Cable disconnected";
108 s
= "Couldn't enter OCD mode";
111 s
= "Target stuck in reset";
114 s
= "OCD hasn't been initialized";
117 s
= "Write verify failed";
120 s
= "Reg buff error (during MPC5xx fp reg read/write)";
123 s
= "Invalid CPU register access attempt failed";
129 s
= "Checksum error";
132 s
= "Illegal command";
135 s
= "Parameter error";
138 s
= "Internal error";
141 s
= "Flash erase error";
144 sprintf (buf
, "Unknown error code %d", error_code
);
151 /* Return nonzero if the thread TH is still alive on the remote system. */
154 ocd_thread_alive (ptid_t th
)
159 /* Clean up connection to a remote debugger. */
163 ocd_close (int quitting
)
166 SERIAL_CLOSE (ocd_desc
);
170 /* Stub for catch_errors. */
173 ocd_start_remote (PTR dummy
)
175 unsigned char buf
[10], *p
;
180 enum ocd_target_type target_type
;
182 target_type
= *(enum ocd_target_type
*) dummy
;
184 immediate_quit
++; /* Allow user to interrupt it */
186 SERIAL_SEND_BREAK (ocd_desc
); /* Wake up the wiggler */
188 speed
= 80; /* Divide clock by 4000 */
192 buf
[2] = speed
& 0xff;
193 buf
[3] = target_type
;
194 ocd_put_packet (buf
, 4); /* Init OCD params */
195 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
198 error ("Truncated response packet from OCD device");
204 ocd_error ("OCD_INIT:", error_code
);
206 ocd_do_command (OCD_AYT
, &status
, &pktlen
);
208 p
= ocd_do_command (OCD_GET_VERSION
, &status
, &pktlen
);
210 printf_unfiltered ("[Wiggler version %x.%x, capability 0x%x]\n",
211 p
[0], p
[1], (p
[2] << 16) | p
[3]);
214 /* Reset the target */
216 ocd_do_command (OCD_RESET_RUN
, &status
, &pktlen
);
217 /* ocd_do_command (OCD_RESET, &status, &pktlen); */
220 /* If processor is still running, stop it. */
222 if (!(status
& OCD_FLAG_BDM
))
226 /* When using a target box, we want to asynchronously return status when
227 target stops. The OCD_SET_CTL_FLAGS command is ignored by Wigglers.dll
228 when using a parallel Wiggler */
229 buf
[0] = OCD_SET_CTL_FLAGS
;
232 ocd_put_packet (buf
, 3);
234 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
237 error ("Truncated response packet from OCD device");
243 ocd_error ("OCD_SET_CTL_FLAGS:", error_code
);
248 /* This is really the job of start_remote however, that makes an assumption
249 that the target is about to print out a status message of some sort. That
250 doesn't happen here (in fact, it may not be possible to get the monitor to
251 send the appropriate packet). */
253 flush_cached_frames ();
254 registers_changed ();
255 stop_pc
= read_pc ();
256 set_current_frame (create_new_frame (read_fp (), stop_pc
));
257 select_frame (get_current_frame (), 0);
258 print_stack_frame (selected_frame
, -1, 1);
260 buf
[0] = OCD_LOG_FILE
;
261 buf
[1] = 3; /* close existing WIGGLERS.LOG */
262 ocd_put_packet (buf
, 2);
263 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
265 buf
[0] = OCD_LOG_FILE
;
266 buf
[1] = 2; /* append to existing WIGGLERS.LOG */
267 ocd_put_packet (buf
, 2);
268 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
273 /* Open a connection to a remote debugger.
274 NAME is the filename used for communication. */
277 ocd_open (char *name
, int from_tty
, enum ocd_target_type target_type
,
278 struct target_ops
*ops
)
280 unsigned char buf
[10], *p
;
284 error ("To open an OCD connection, you need to specify the\n\
285 device the OCD device is attached to (e.g. /dev/ttya).");
287 target_preopen (from_tty
);
291 unpush_target (current_ops
);
293 if (strncmp (name
, "wiggler", 7) == 0)
295 ocd_desc
= SERIAL_OPEN ("ocd");
297 perror_with_name (name
);
299 buf
[0] = OCD_LOG_FILE
;
300 buf
[1] = 1; /* open new or overwrite existing WIGGLERS.LOG */
301 ocd_put_packet (buf
, 2);
302 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
304 buf
[0] = OCD_SET_CONNECTION
;
305 buf
[1] = 0x01; /* atoi (name[11]); */
306 ocd_put_packet (buf
, 2);
307 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
310 /* not using Wigglers.dll */
312 ocd_desc
= SERIAL_OPEN (name
);
314 perror_with_name (name
);
319 if (SERIAL_SETBAUDRATE (ocd_desc
, baud_rate
))
321 SERIAL_CLOSE (ocd_desc
);
322 perror_with_name (name
);
326 SERIAL_RAW (ocd_desc
);
328 /* If there is something sitting in the buffer we might take it as a
329 response to a command, which would be bad. */
330 SERIAL_FLUSH_INPUT (ocd_desc
);
334 puts_filtered ("Remote target wiggler connected to ");
335 puts_filtered (name
);
336 puts_filtered ("\n");
338 push_target (current_ops
); /* Switch to using remote target now */
340 /* Without this, some commands which require an active target (such as kill)
341 won't work. This variable serves (at least) double duty as both the pid
342 of the target process (if it has such), and as a flag indicating that a
343 target is active. These functions should be split out into seperate
344 variables, especially since GDB will someday have a notion of debugging
345 several processes. */
347 inferior_ptid
= pid_to_ptid (42000);
348 /* Start the remote connection; if error (0), discard this target.
349 In particular, if the user quits, be sure to discard it
350 (we'd be in an inconsistent state otherwise). */
351 if (!catch_errors (ocd_start_remote
, &target_type
,
352 "Couldn't establish connection to remote target\n",
356 error ("Failed to connect to OCD.");
360 /* This takes a program previously attached to and detaches it. After
361 this is done, GDB can be used to debug some other program. We
362 better not have left any breakpoints in the target program or it'll
363 die when it hits one. */
366 ocd_detach (char *args
, int from_tty
)
369 error ("Argument given to \"detach\" when remotely debugging.");
373 puts_filtered ("Ending remote debugging.\n");
376 /* Tell the remote machine to resume. */
379 ocd_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
384 ocd_do_command (OCD_STEP
, &last_run_status
, &pktlen
);
386 ocd_do_command (OCD_RUN
, &last_run_status
, &pktlen
);
395 ocd_do_command (OCD_STOP
, &status
, &pktlen
);
397 if (!(status
& OCD_FLAG_BDM
))
398 error ("Can't stop target via BDM");
401 static volatile int ocd_interrupt_flag
;
403 /* Send ^C to target to halt it. Target will respond, and send us a
407 ocd_interrupt (int signo
)
409 /* If this doesn't work, try more severe steps. */
410 signal (signo
, ocd_interrupt_twice
);
413 printf_unfiltered ("ocd_interrupt called\n");
420 ocd_put_packet (buf
, 1);
421 ocd_interrupt_flag
= 1;
425 static void (*ofunc
) ();
427 /* The user typed ^C twice. */
429 ocd_interrupt_twice (int signo
)
431 signal (signo
, ofunc
);
435 signal (signo
, ocd_interrupt
);
438 /* Ask the user what to do when an interrupt is received. */
441 interrupt_query (void)
443 target_terminal_ours ();
445 if (query ("Interrupted while waiting for the program.\n\
446 Give up (and stop debugging it)? "))
448 target_mourn_inferior ();
449 return_to_top_level (RETURN_QUIT
);
452 target_terminal_inferior ();
455 /* If nonzero, ignore the next kill. */
456 static int kill_kludge
;
458 /* Wait until the remote machine stops, then return,
459 storing status in STATUS just as `wait' would.
460 Returns "pid" (though it's not clear what, if anything, that
461 means in the case of this target). */
471 ocd_interrupt_flag
= 0;
473 /* Target might already be stopped by the time we get here. */
474 /* If we aren't already stopped, we need to loop until we've dropped
475 back into BDM mode */
477 while (!(last_run_status
& OCD_FLAG_BDM
))
480 ocd_put_packet (buf
, 1);
481 p
= ocd_get_packet (OCD_AYT
, &pktlen
, -1);
483 ofunc
= (void (*)()) signal (SIGINT
, ocd_interrupt
);
484 signal (SIGINT
, ofunc
);
487 error ("Truncated response packet from OCD device");
489 last_run_status
= p
[1];
493 ocd_error ("target_wait:", error_code
);
495 if (last_run_status
& OCD_FLAG_PWF
)
496 error ("OCD device lost VCC at BDM interface.");
497 else if (last_run_status
& OCD_FLAG_CABLE_DISC
)
498 error ("OCD device cable appears to have been disconnected.");
501 if (ocd_interrupt_flag
)
507 /* Read registers from the OCD device. Specify the starting and ending
508 register number. Return the number of regs actually read in *NUMREGS.
509 Returns a pointer to a static array containing the register contents. */
512 ocd_read_bdm_registers (int first_bdm_regno
, int last_bdm_regno
, int *reglen
)
514 unsigned char buf
[10];
518 int error_code
, status
;
521 buf
[0] = OCD_READ_REGS
;
522 buf
[1] = first_bdm_regno
>> 8;
523 buf
[2] = first_bdm_regno
& 0xff;
524 buf
[3] = last_bdm_regno
>> 8;
525 buf
[4] = last_bdm_regno
& 0xff;
527 ocd_put_packet (buf
, 5);
528 p
= ocd_get_packet (OCD_READ_REGS
, &pktlen
, remote_timeout
);
534 ocd_error ("read_bdm_registers:", error_code
);
542 error ("Register block size bad: %d", i
);
551 /* Read register BDM_REGNO and returns its value ala read_register() */
554 ocd_read_bdm_register (int bdm_regno
)
560 p
= ocd_read_bdm_registers (bdm_regno
, bdm_regno
, ®len
);
561 regval
= extract_unsigned_integer (p
, reglen
);
567 ocd_write_bdm_registers (int first_bdm_regno
, unsigned char *regptr
, int reglen
)
571 int error_code
, status
;
574 buf
= alloca (4 + reglen
);
576 buf
[0] = OCD_WRITE_REGS
;
577 buf
[1] = first_bdm_regno
>> 8;
578 buf
[2] = first_bdm_regno
& 0xff;
580 memcpy (buf
+ 4, regptr
, reglen
);
582 ocd_put_packet (buf
, 4 + reglen
);
583 p
= ocd_get_packet (OCD_WRITE_REGS
, &pktlen
, remote_timeout
);
586 error ("Truncated response packet from OCD device");
592 ocd_error ("ocd_write_bdm_registers:", error_code
);
596 ocd_write_bdm_register (int bdm_regno
, CORE_ADDR reg
)
598 unsigned char buf
[4];
600 store_unsigned_integer (buf
, 4, reg
);
602 ocd_write_bdm_registers (bdm_regno
, buf
, 4);
606 ocd_prepare_to_store (void)
610 /* Write memory data directly to the remote machine.
611 This does not inform the data cache; the data cache uses this.
612 MEMADDR is the address in the remote memory space.
613 MYADDR is the address of the buffer in our space.
614 LEN is the number of bytes.
616 Returns number of bytes transferred, or 0 for error. */
618 static int write_mem_command
= OCD_WRITE_MEM
;
621 ocd_write_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
)
629 buf
[0] = write_mem_command
;
630 buf
[5] = 1; /* Write as bytes */
631 buf
[6] = 0; /* Don't verify */
637 int status
, error_code
;
639 numbytes
= min (len
, 256 - 8);
641 buf
[1] = memaddr
>> 24;
642 buf
[2] = memaddr
>> 16;
643 buf
[3] = memaddr
>> 8;
648 memcpy (&buf
[8], myaddr
, numbytes
);
649 ocd_put_packet (buf
, 8 + numbytes
);
650 p
= ocd_get_packet (OCD_WRITE_MEM
, &pktlen
, remote_timeout
);
652 error ("Truncated response packet from OCD device");
657 if (error_code
== 0x11) /* Got a bus error? */
659 CORE_ADDR error_address
;
661 error_address
= p
[3] << 24;
662 error_address
|= p
[4] << 16;
663 error_address
|= p
[5] << 8;
664 error_address
|= p
[6];
665 numbytes
= error_address
- memaddr
;
673 else if (error_code
!= 0)
674 ocd_error ("ocd_write_bytes:", error_code
);
681 return origlen
- len
;
684 /* Read memory data directly from the remote machine.
685 This does not use the data cache; the data cache uses this.
686 MEMADDR is the address in the remote memory space.
687 MYADDR is the address of the buffer in our space.
688 LEN is the number of bytes.
690 Returns number of bytes transferred, or 0 for error. */
693 ocd_read_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
)
701 buf
[0] = OCD_READ_MEM
;
702 buf
[5] = 1; /* Read as bytes */
708 int status
, error_code
;
710 numbytes
= min (len
, 256 - 7);
712 buf
[1] = memaddr
>> 24;
713 buf
[2] = memaddr
>> 16;
714 buf
[3] = memaddr
>> 8;
719 ocd_put_packet (buf
, 7);
720 p
= ocd_get_packet (OCD_READ_MEM
, &pktlen
, remote_timeout
);
722 error ("Truncated response packet from OCD device");
727 if (error_code
== 0x11) /* Got a bus error? */
729 CORE_ADDR error_address
;
731 error_address
= p
[3] << 24;
732 error_address
|= p
[4] << 16;
733 error_address
|= p
[5] << 8;
734 error_address
|= p
[6];
735 numbytes
= error_address
- memaddr
;
743 else if (error_code
!= 0)
744 ocd_error ("ocd_read_bytes:", error_code
);
746 memcpy (myaddr
, &p
[4], numbytes
);
753 return origlen
- len
;
756 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
757 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
758 nonzero. Returns length of data written or read; 0 for error. TARGET
763 ocd_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int should_write
,
764 struct mem_attrib
*attrib
, struct target_ops
*target
)
769 res
= ocd_write_bytes (memaddr
, myaddr
, len
);
771 res
= ocd_read_bytes (memaddr
, myaddr
, len
);
777 ocd_files_info (struct target_ops
*ignore
)
779 puts_filtered ("Debugging a target over a serial line.\n");
782 /* Stuff for dealing with the packets which are part of this protocol.
783 See comment at top of file for details. */
785 /* Read a single character from the remote side, handling wierd errors. */
788 readchar (int timeout
)
792 ch
= SERIAL_READCHAR (ocd_desc
, timeout
);
797 error ("Remote connection closed");
799 perror_with_name ("Remote communication error");
807 /* Read a character from the data stream, dequoting as necessary. SYN is
808 treated special. Any SYNs appearing in the data stream are returned as the
809 distinct value RAW_SYN (which has a value > 8 bits and therefore cannot be
810 mistaken for real data). */
813 get_quoted_char (int timeout
)
817 ch
= readchar (timeout
);
822 error ("Timeout in mid-packet, aborting");
826 ch
= readchar (timeout
);
835 static unsigned char pkt
[256 * 2 + 10], *pktp
; /* Worst case */
846 if (SERIAL_WRITE (ocd_desc
, pkt
, pktp
- pkt
))
847 perror_with_name ("output_packet: write failed");
852 /* Output a quoted character. SYNs and DLEs are quoted. Everything else goes
853 through untouched. */
856 put_quoted_char (int c
)
869 /* Send a packet to the OCD device. The packet framed by a SYN character,
870 a byte count and a checksum. The byte count only counts the number of
871 bytes between the count and the checksum. A count of zero actually
872 means 256. Any SYNs within the packet (including the checksum and
873 count) must be quoted. The quote character must be quoted as well.
874 Quoting is done by replacing the character with the two-character sequence
875 DLE, {char} | 0100. Note that the quoting mechanism has no effect on the
879 stu_put_packet (unsigned char *buf
, int len
)
881 unsigned char checksum
;
884 if (len
== 0 || len
> 256)
885 internal_error (__FILE__
, __LINE__
, "failed internal consistency check"); /* Can't represent 0 length packet */
891 put_quoted_char (RAW_SYN
);
905 put_quoted_char (-checksum
& 0xff);
912 /* Send a packet to the OCD device. The packet framed by a SYN character,
913 a byte count and a checksum. The byte count only counts the number of
914 bytes between the count and the checksum. A count of zero actually
915 means 256. Any SYNs within the packet (including the checksum and
916 count) must be quoted. The quote character must be quoted as well.
917 Quoting is done by replacing the character with the two-character sequence
918 DLE, {char} | 0100. Note that the quoting mechanism has no effect on the
922 ocd_put_packet (unsigned char *buf
, int len
)
924 unsigned char checksum
;
926 unsigned char *packet
, *packet_ptr
;
928 packet
= alloca (len
+ 1 + 1); /* packet + SYN + checksum */
933 *packet_ptr
++ = 0x55;
943 *packet_ptr
++ = -checksum
;
944 if (SERIAL_WRITE (ocd_desc
, packet
, packet_ptr
- packet
))
945 perror_with_name ("output_packet: write failed");
950 /* Get a packet from the OCD device. Timeout is only enforced for the
951 first byte of the packet. Subsequent bytes are expected to arrive in
952 time <= remote_timeout. Returns a pointer to a static buffer containing
953 the payload of the packet. *LENP contains the length of the packet.
956 static unsigned char *
957 stu_get_packet (unsigned char cmd
, int *lenp
, int timeout
)
961 static unsigned char buf
[256 + 10], *p
;
962 unsigned char checksum
;
966 ch
= get_quoted_char (timeout
);
969 error ("get_packet (readchar): %d", ch
);
974 found_syn
: /* Found the start of a packet */
979 len
= get_quoted_char (remote_timeout
);
989 len
++; /* Include checksum */
993 ch
= get_quoted_char (remote_timeout
);
1005 error ("Response phase error. Got 0x%x, expected 0x%x", buf
[0], cmd
);
1007 *lenp
= p
- buf
- 1;
1013 /* Get a packet from the OCD device. Timeout is only enforced for the
1014 first byte of the packet. Subsequent bytes are expected to arrive in
1015 time <= remote_timeout. Returns a pointer to a static buffer containing
1016 the payload of the packet. *LENP contains the length of the packet.
1019 static unsigned char *
1020 ocd_get_packet (int cmd
, int *lenp
, int timeout
)
1024 static unsigned char packet
[512];
1025 unsigned char *packet_ptr
;
1026 unsigned char checksum
;
1028 ch
= readchar (timeout
);
1031 error ("ocd_get_packet (readchar): %d", ch
);
1034 error ("ocd_get_packet (readchar): %d", ch
);
1036 /* Found the start of a packet */
1038 packet_ptr
= packet
;
1041 /* Read command char. That sort of tells us how long the packet is. */
1043 ch
= readchar (timeout
);
1046 error ("ocd_get_packet (readchar): %d", ch
);
1053 ch
= readchar (timeout
);
1056 error ("ocd_get_packet (readchar): %d", ch
);
1060 /* Get error code. */
1062 ch
= readchar (timeout
);
1065 error ("ocd_get_packet (readchar): %d", ch
);
1069 switch (ch
) /* Figure out length of packet */
1071 case 0x7: /* Write verify error? */
1072 len
= 8; /* write address, value read back */
1074 case 0x11: /* Bus error? */
1075 /* write address, read flag */
1076 case 0x15: /* Internal error */
1077 len
= 5; /* error code, vector */
1079 default: /* Error w/no params */
1082 case 0x0: /* Normal result */
1085 case OCD_AYT
: /* Are You There? */
1086 case OCD_SET_BAUD_RATE
: /* Set Baud Rate */
1087 case OCD_INIT
: /* Initialize OCD device */
1088 case OCD_SET_SPEED
: /* Set Speed */
1089 case OCD_SET_FUNC_CODE
: /* Set Function Code */
1090 case OCD_SET_CTL_FLAGS
: /* Set Control Flags */
1091 case OCD_SET_BUF_ADDR
: /* Set Register Buffer Address */
1092 case OCD_RUN
: /* Run Target from PC */
1093 case OCD_RUN_ADDR
: /* Run Target from Specified Address */
1094 case OCD_STOP
: /* Stop Target */
1095 case OCD_RESET_RUN
: /* Reset Target and Run */
1096 case OCD_RESET
: /* Reset Target and Halt */
1097 case OCD_STEP
: /* Single Step */
1098 case OCD_WRITE_REGS
: /* Write Register */
1099 case OCD_WRITE_MEM
: /* Write Memory */
1100 case OCD_FILL_MEM
: /* Fill Memory */
1101 case OCD_MOVE_MEM
: /* Move Memory */
1102 case OCD_WRITE_INT_MEM
: /* Write Internal Memory */
1103 case OCD_JUMP
: /* Jump to Subroutine */
1104 case OCD_ERASE_FLASH
: /* Erase flash memory */
1105 case OCD_PROGRAM_FLASH
: /* Write flash memory */
1106 case OCD_EXIT_MON
: /* Exit the flash programming monitor */
1107 case OCD_ENTER_MON
: /* Enter the flash programming monitor */
1108 case OCD_LOG_FILE
: /* Make Wigglers.dll save Wigglers.log */
1109 case OCD_SET_CONNECTION
: /* Set type of connection in Wigglers.dll */
1112 case OCD_GET_VERSION
: /* Get Version */
1115 case OCD_GET_STATUS_MASK
: /* Get Status Mask */
1118 case OCD_GET_CTRS
: /* Get Error Counters */
1119 case OCD_READ_REGS
: /* Read Register */
1120 case OCD_READ_MEM
: /* Read Memory */
1121 case OCD_READ_INT_MEM
: /* Read Internal Memory */
1125 error ("ocd_get_packet: unknown packet type 0x%x\n", ch
);
1129 if (len
== 257) /* Byte stream? */
1130 { /* Yes, byte streams contain the length */
1131 ch
= readchar (timeout
);
1134 error ("ocd_get_packet (readchar): %d", ch
);
1142 while (len
-- >= 0) /* Do rest of packet and checksum */
1144 ch
= readchar (timeout
);
1147 error ("ocd_get_packet (readchar): %d", ch
);
1153 error ("ocd_get_packet: bad packet checksum");
1155 if (cmd
!= -1 && cmd
!= packet
[0])
1156 error ("Response phase error. Got 0x%x, expected 0x%x", packet
[0], cmd
);
1158 *lenp
= packet_ptr
- packet
- 1; /* Subtract checksum byte */
1163 /* Execute a simple (one-byte) command. Returns a pointer to the data
1164 following the error code. */
1166 static unsigned char *
1167 ocd_do_command (int cmd
, int *statusp
, int *lenp
)
1169 unsigned char buf
[100], *p
;
1170 int status
, error_code
;
1173 unsigned char logbuf
[100];
1177 ocd_put_packet (buf
, 1); /* Send command */
1178 p
= ocd_get_packet (*buf
, lenp
, remote_timeout
);
1181 error ("Truncated response packet from OCD device");
1186 if (error_code
!= 0)
1188 sprintf (errbuf
, "ocd_do_command (0x%x):", cmd
);
1189 ocd_error (errbuf
, error_code
);
1192 if (status
& OCD_FLAG_PWF
)
1193 error ("OCD device can't detect VCC at BDM interface.");
1194 else if (status
& OCD_FLAG_CABLE_DISC
)
1195 error ("BDM cable appears to be disconnected.");
1199 logbuf
[0] = OCD_LOG_FILE
;
1200 logbuf
[1] = 3; /* close existing WIGGLERS.LOG */
1201 ocd_put_packet (logbuf
, 2);
1202 ocd_get_packet (logbuf
[0], &logpktlen
, remote_timeout
);
1204 logbuf
[0] = OCD_LOG_FILE
;
1205 logbuf
[1] = 2; /* append to existing WIGGLERS.LOG */
1206 ocd_put_packet (logbuf
, 2);
1207 ocd_get_packet (logbuf
[0], &logpktlen
, remote_timeout
);
1215 /* For some mysterious reason, wait_for_inferior calls kill instead of
1216 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1220 target_mourn_inferior ();
1224 /* Don't wait for it to die. I'm not really sure it matters whether
1226 target_mourn_inferior ();
1232 unpush_target (current_ops
);
1233 generic_mourn_inferior ();
1236 /* All we actually do is set the PC to the start address of exec_bfd, and start
1237 the program at that point. */
1240 ocd_create_inferior (char *exec_file
, char *args
, char **env
)
1242 if (args
&& (*args
!= '\000'))
1243 error ("Args are not supported by BDM.");
1245 clear_proceed_status ();
1246 proceed (bfd_get_start_address (exec_bfd
), TARGET_SIGNAL_0
, 0);
1250 ocd_load (char *args
, int from_tty
)
1252 generic_load (args
, from_tty
);
1254 inferior_ptid
= null_ptid
;
1256 /* This is necessary because many things were based on the PC at the time that
1257 we attached to the monitor, which is no longer valid now that we have loaded
1258 new code (and just changed the PC). Another way to do this might be to call
1259 normal_stop, except that the stack may not be valid, and things would get
1260 horribly confused... */
1262 clear_symtab_users ();
1265 /* This should be defined for each target */
1266 /* But we want to be able to compile this file for some configurations
1267 not yet supported fully */
1269 #define BDM_BREAKPOINT {0x0,0x0,0x0,0x0} /* For ppc 8xx */
1271 #define BDM_BREAKPOINT {0x4a,0xfa} /* BGND insn used for CPU32 */
1274 /* BDM (at least on CPU32) uses a different breakpoint */
1277 ocd_insert_breakpoint (CORE_ADDR addr
, char *contents_cache
)
1279 static char break_insn
[] = BDM_BREAKPOINT
;
1282 val
= target_read_memory (addr
, contents_cache
, sizeof (break_insn
));
1285 val
= target_write_memory (addr
, break_insn
, sizeof (break_insn
));
1291 ocd_remove_breakpoint (CORE_ADDR addr
, char *contents_cache
)
1293 static char break_insn
[] = BDM_BREAKPOINT
;
1296 val
= target_write_memory (addr
, contents_cache
, sizeof (break_insn
));
1302 bdm_command (char *args
, int from_tty
)
1304 error ("bdm command must be followed by `reset'");
1308 bdm_reset_command (char *args
, int from_tty
)
1313 error ("Not connected to OCD device.");
1315 ocd_do_command (OCD_RESET
, &status
, &pktlen
);
1316 dcache_invalidate (target_dcache
);
1317 registers_changed ();
1321 bdm_restart_command (char *args
, int from_tty
)
1326 error ("Not connected to OCD device.");
1328 ocd_do_command (OCD_RESET_RUN
, &status
, &pktlen
);
1329 last_run_status
= status
;
1330 clear_proceed_status ();
1331 wait_for_inferior ();
1335 /* Temporary replacement for target_store_registers(). This prevents
1336 generic_load from trying to set the PC. */
1339 noop_store_registers (int regno
)
1344 bdm_update_flash_command (char *args
, int from_tty
)
1347 struct cleanup
*old_chain
;
1348 void (*store_registers_tmp
) (int);
1351 error ("Not connected to OCD device.");
1354 error ("Must specify file containing new OCD code.");
1356 /* old_chain = make_cleanup (flash_cleanup, 0); */
1358 ocd_do_command (OCD_ENTER_MON
, &status
, &pktlen
);
1360 ocd_do_command (OCD_ERASE_FLASH
, &status
, &pktlen
);
1362 write_mem_command
= OCD_PROGRAM_FLASH
;
1363 store_registers_tmp
= current_target
.to_store_registers
;
1364 current_target
.to_store_registers
= noop_store_registers
;
1366 generic_load (args
, from_tty
);
1368 current_target
.to_store_registers
= store_registers_tmp
;
1369 write_mem_command
= OCD_WRITE_MEM
;
1371 ocd_do_command (OCD_EXIT_MON
, &status
, &pktlen
);
1373 /* discard_cleanups (old_chain); */
1377 bdm_read_register_command (char *args
, int from_tty
)
1379 /* XXX repeat should go on to the next register */
1382 error ("Not connected to OCD device.");
1385 error ("Must specify BDM register number.");
1390 _initialize_remote_ocd (void)
1392 extern struct cmd_list_element
*cmdlist
;
1393 static struct cmd_list_element
*ocd_cmd_list
= NULL
;
1395 add_show_from_set (add_set_cmd ("remotetimeout", no_class
,
1396 var_integer
, (char *) &remote_timeout
,
1397 "Set timeout value for remote read.\n", &setlist
),
1400 add_prefix_cmd ("ocd", class_obscure
, bdm_command
, "", &ocd_cmd_list
, "ocd ",
1403 add_cmd ("reset", class_obscure
, bdm_reset_command
, "", &ocd_cmd_list
);
1404 add_cmd ("restart", class_obscure
, bdm_restart_command
, "", &ocd_cmd_list
);
1405 add_cmd ("update-flash", class_obscure
, bdm_update_flash_command
, "", &ocd_cmd_list
);
1406 /* add_cmd ("read-register", class_obscure, bdm_read_register_command, "", &ocd_cmd_list); */