1 /* Target communications support for Macraigor Systems' On-Chip Debugging
2 Copyright 1996, 1997 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, Boston, MA 02111-1307, USA. */
22 #include "gdb_string.h"
32 #include "gdb-stabs.h"
34 #include <sys/types.h>
39 /* Prototypes for local functions */
41 static int ocd_read_bytes
PARAMS ((CORE_ADDR memaddr
,
42 char *myaddr
, int len
));
44 static int ocd_start_remote
PARAMS ((char *dummy
));
46 static int readchar
PARAMS ((int timeout
));
48 static void reset_packet
PARAMS ((void));
50 static void output_packet
PARAMS ((void));
52 static int get_quoted_char
PARAMS ((int timeout
));
54 static void put_quoted_char
PARAMS ((int c
));
56 static void ocd_interrupt
PARAMS ((int signo
));
58 static void ocd_interrupt_twice
PARAMS ((int signo
));
60 static void interrupt_query
PARAMS ((void));
62 static unsigned char * ocd_do_command
PARAMS ((int cmd
, int *statusp
, int *lenp
));
64 static void ocd_put_packet
PARAMS ((unsigned char *packet
, int pktlen
));
66 static unsigned char * ocd_get_packet
PARAMS ((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
77 /* FIXME: Change to allow option to set timeout value on a per target
80 static int remote_timeout = 2;
82 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
83 ocd_open knows that we don't have a file open when the program
85 static serial_t ocd_desc
= NULL
;
88 ocd_error (s
, error_code
)
94 fputs_filtered (s
, gdb_stderr
);
95 fputs_filtered (" ", gdb_stderr
);
99 case 0x1: s
= "Unknown fault"; break;
100 case 0x2: s
= "Power failed"; break;
101 case 0x3: s
= "Cable disconnected"; break;
102 case 0x4: s
= "Couldn't enter OCD mode"; break;
103 case 0x5: s
= "Target stuck in reset"; break;
104 case 0x6: s
= "OCD hasn't been initialized"; break;
105 case 0x7: s
= "Write verify failed"; break;
106 case 0x8: s
= "Reg buff error (during MPC5xx fp reg read/write)"; break;
107 case 0x9: s
= "Invalid CPU register access attempt failed"; break;
108 case 0x11: s
= "Bus error"; break;
109 case 0x12: s
= "Checksum error"; break;
110 case 0x13: s
= "Illegal command"; break;
111 case 0x14: s
= "Parameter error"; break;
112 case 0x15: s
= "Internal error"; break;
113 case 0x80: s
= "Flash erase error"; break;
115 sprintf (buf
, "Unknown error code %d", error_code
);
122 /* Return nonzero if the thread TH is still alive on the remote system. */
125 ocd_thread_alive (th
)
131 /* Clean up connection to a remote debugger. */
139 SERIAL_CLOSE (ocd_desc
);
143 /* Stub for catch_errors. */
146 ocd_start_remote (dummy
)
149 unsigned char buf
[10], *p
;
154 enum ocd_target_type target_type
;
156 target_type
= (enum ocd_target_type
)dummy
;
158 immediate_quit
= 1; /* Allow user to interrupt it */
160 SERIAL_SEND_BREAK (ocd_desc
); /* Wake up the wiggler */
162 speed
= 80; /* Divide clock by 4000 */
166 buf
[2] = speed
& 0xff;
167 buf
[3] = target_type
;
168 ocd_put_packet (buf
, 4); /* Init OCD params */
169 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
172 error ("Truncated response packet from OCD device");
178 ocd_error ("OCD_INIT:", error_code
);
180 ocd_do_command (OCD_AYT
, &status
, &pktlen
);
182 p
= ocd_do_command (OCD_GET_VERSION
, &status
, &pktlen
);
184 printf_unfiltered ("[Wiggler version %x.%x, capability 0x%x]\n",
185 p
[0], p
[1], (p
[2] << 16) | p
[3]);
188 /* Reset the target */
190 ocd_do_command (OCD_RESET_RUN
, &status
, &pktlen
);
191 /* ocd_do_command (OCD_RESET, &status, &pktlen);*/
194 /* If processor is still running, stop it. */
196 if (!(status
& OCD_FLAG_BDM
))
200 /* When using a target box, we want to asynchronously return status when
201 target stops. The OCD_SET_CTL_FLAGS command is ignored by Wigglers.dll
202 when using a parallel Wiggler */
203 buf
[0] = OCD_SET_CTL_FLAGS
;
206 ocd_put_packet (buf
, 3);
208 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
211 error ("Truncated response packet from OCD device");
217 ocd_error ("OCD_SET_CTL_FLAGS:", error_code
);
222 /* This is really the job of start_remote however, that makes an assumption
223 that the target is about to print out a status message of some sort. That
224 doesn't happen here (in fact, it may not be possible to get the monitor to
225 send the appropriate packet). */
227 flush_cached_frames ();
228 registers_changed ();
229 stop_pc
= read_pc ();
230 set_current_frame (create_new_frame (read_fp (), stop_pc
));
231 select_frame (get_current_frame (), 0);
232 print_stack_frame (selected_frame
, -1, 1);
234 buf
[0] = OCD_LOG_FILE
;
235 buf
[1] = 3; /* close existing WIGGLERS.LOG */
236 ocd_put_packet (buf
, 2);
237 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
239 buf
[0] = OCD_LOG_FILE
;
240 buf
[1] = 2; /* append to existing WIGGLERS.LOG */
241 ocd_put_packet (buf
, 2);
242 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
247 /* Open a connection to a remote debugger.
248 NAME is the filename used for communication. */
250 static DCACHE
*ocd_dcache
;
253 ocd_open (name
, from_tty
, target_type
, ops
)
256 enum ocd_target_type target_type
;
257 struct target_ops
*ops
;
259 unsigned char buf
[10], *p
;
264 error ("To open an OCD connection, you need to specify the\n\
265 device the OCD device is attached to (e.g. /dev/ttya).");
267 target_preopen (from_tty
);
271 unpush_target (current_ops
);
273 ocd_dcache
= dcache_init (ocd_read_bytes
, ocd_write_bytes
);
275 if (strncmp(name
,"wiggler",7) == 0)
277 ocd_desc
= SERIAL_OPEN ("ocd");
279 perror_with_name (name
);
281 buf
[0] = OCD_LOG_FILE
;
282 buf
[1] = 1; /* open new or overwrite existing WIGGLERS.LOG */
283 ocd_put_packet (buf
, 2);
284 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
286 buf
[0] = OCD_SET_CONNECTION
;
287 buf
[1] = 0x01; /* atoi (name[11]); */
288 ocd_put_packet (buf
, 2);
289 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
291 else /* not using Wigglers.dll */
293 ocd_desc
= SERIAL_OPEN (name
);
295 perror_with_name (name
);
300 if (SERIAL_SETBAUDRATE (ocd_desc
, baud_rate
))
302 SERIAL_CLOSE (ocd_desc
);
303 perror_with_name (name
);
307 SERIAL_RAW (ocd_desc
);
309 /* If there is something sitting in the buffer we might take it as a
310 response to a command, which would be bad. */
311 SERIAL_FLUSH_INPUT (ocd_desc
);
315 puts_filtered ("Remote target wiggler connected to ");
316 puts_filtered (name
);
317 puts_filtered ("\n");
319 push_target (current_ops
); /* Switch to using remote target now */
321 /* Without this, some commands which require an active target (such as kill)
322 won't work. This variable serves (at least) double duty as both the pid
323 of the target process (if it has such), and as a flag indicating that a
324 target is active. These functions should be split out into seperate
325 variables, especially since GDB will someday have a notion of debugging
326 several processes. */
328 inferior_pid
= 42000;
329 /* Start the remote connection; if error (0), discard this target.
330 In particular, if the user quits, be sure to discard it
331 (we'd be in an inconsistent state otherwise). */
332 if (!catch_errors (ocd_start_remote
, (char *)target_type
,
333 "Couldn't establish connection to remote target\n",
338 /* This takes a program previously attached to and detaches it. After
339 this is done, GDB can be used to debug some other program. We
340 better not have left any breakpoints in the target program or it'll
341 die when it hits one. */
344 ocd_detach (args
, from_tty
)
349 error ("Argument given to \"detach\" when remotely debugging.");
353 puts_filtered ("Ending remote debugging.\n");
356 /* Tell the remote machine to resume. */
359 ocd_resume (pid
, step
, siggnal
)
361 enum target_signal siggnal
;
365 dcache_flush (ocd_dcache
);
368 ocd_do_command (OCD_STEP
, &last_run_status
, &pktlen
);
370 ocd_do_command (OCD_RUN
, &last_run_status
, &pktlen
);
379 ocd_do_command (OCD_STOP
, &status
, &pktlen
);
381 if (!(status
& OCD_FLAG_BDM
))
382 error ("Can't stop target via BDM");
385 static volatile int ocd_interrupt_flag
;
387 /* Send ^C to target to halt it. Target will respond, and send us a
391 ocd_interrupt (signo
)
394 /* If this doesn't work, try more severe steps. */
395 signal (signo
, ocd_interrupt_twice
);
398 printf_unfiltered ("ocd_interrupt called\n");
405 ocd_put_packet (buf
, 1);
406 ocd_interrupt_flag
= 1;
410 static void (*ofunc
)();
412 /* The user typed ^C twice. */
414 ocd_interrupt_twice (signo
)
417 signal (signo
, ofunc
);
421 signal (signo
, ocd_interrupt
);
424 /* Ask the user what to do when an interrupt is received. */
429 target_terminal_ours ();
431 if (query ("Interrupted while waiting for the program.\n\
432 Give up (and stop debugging it)? "))
434 target_mourn_inferior ();
435 return_to_top_level (RETURN_QUIT
);
438 target_terminal_inferior ();
441 /* If nonzero, ignore the next kill. */
442 static int kill_kludge
;
444 /* Wait until the remote machine stops, then return,
445 storing status in STATUS just as `wait' would.
446 Returns "pid" (though it's not clear what, if anything, that
447 means in the case of this target). */
457 ocd_interrupt_flag
= 0;
459 /* Target might already be stopped by the time we get here. */
460 /* If we aren't already stopped, we need to loop until we've dropped
461 back into BDM mode */
463 while (!(last_run_status
& OCD_FLAG_BDM
))
466 ocd_put_packet (buf
, 1);
467 p
= ocd_get_packet (OCD_AYT
, &pktlen
, -1);
469 ofunc
= (void (*)()) signal (SIGINT
, ocd_interrupt
);
470 signal (SIGINT
, ofunc
);
473 error ("Truncated response packet from OCD device");
475 last_run_status
= p
[1];
479 ocd_error ("target_wait:", error_code
);
481 if (last_run_status
& OCD_FLAG_PWF
)
482 error ("OCD device lost VCC at BDM interface.");
483 else if (last_run_status
& OCD_FLAG_CABLE_DISC
)
484 error ("OCD device cable appears to have been disconnected.");
487 if (ocd_interrupt_flag
)
493 /* Read registers from the OCD device. Specify the starting and ending
494 register number. Return the number of regs actually read in *NUMREGS.
495 Returns a pointer to a static array containing the register contents. */
498 ocd_read_bdm_registers (first_bdm_regno
, last_bdm_regno
, reglen
)
503 unsigned char buf
[10];
507 int error_code
, status
;
510 buf
[0] = OCD_READ_REGS
;
511 buf
[1] = first_bdm_regno
>> 8;
512 buf
[2] = first_bdm_regno
& 0xff;
513 buf
[3] = last_bdm_regno
>> 8;
514 buf
[4] = last_bdm_regno
& 0xff;
516 ocd_put_packet (buf
, 5);
517 p
= ocd_get_packet (OCD_READ_REGS
, &pktlen
, remote_timeout
);
523 ocd_error ("read_bdm_registers:", error_code
);
531 error ("Register block size bad: %d", i
);
540 /* Read register BDM_REGNO and returns its value ala read_register() */
543 ocd_read_bdm_register (bdm_regno
)
550 p
= ocd_read_bdm_registers (bdm_regno
, bdm_regno
, ®len
);
551 regval
= extract_unsigned_integer (p
, reglen
);
557 ocd_write_bdm_registers (first_bdm_regno
, regptr
, reglen
)
559 unsigned char *regptr
;
564 int error_code
, status
;
567 buf
= alloca (4 + reglen
);
569 buf
[0] = OCD_WRITE_REGS
;
570 buf
[1] = first_bdm_regno
>> 8;
571 buf
[2] = first_bdm_regno
& 0xff;
573 memcpy (buf
+ 4, regptr
, reglen
);
575 ocd_put_packet (buf
, 4 + reglen
);
576 p
= ocd_get_packet (OCD_WRITE_REGS
, &pktlen
, remote_timeout
);
579 error ("Truncated response packet from OCD device");
585 ocd_error ("ocd_write_bdm_registers:", error_code
);
589 ocd_write_bdm_register (bdm_regno
, reg
)
593 unsigned char buf
[4];
595 store_unsigned_integer (buf
, 4, reg
);
597 ocd_write_bdm_registers (bdm_regno
, buf
, 4);
601 ocd_prepare_to_store ()
605 /* Write memory data directly to the remote machine.
606 This does not inform the data cache; the data cache uses this.
607 MEMADDR is the address in the remote memory space.
608 MYADDR is the address of the buffer in our space.
609 LEN is the number of bytes.
611 Returns number of bytes transferred, or 0 for error. */
613 static int write_mem_command
= OCD_WRITE_MEM
;
616 ocd_write_bytes (memaddr
, myaddr
, 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 (memaddr
, myaddr
, len
)
702 buf
[0] = OCD_READ_MEM
;
703 buf
[5] = 1; /* Read as bytes */
709 int status
, error_code
;
711 numbytes
= min (len
, 256 - 7);
713 buf
[1] = memaddr
>> 24;
714 buf
[2] = memaddr
>> 16;
715 buf
[3] = memaddr
>> 8;
720 ocd_put_packet (buf
, 7);
721 p
= ocd_get_packet (OCD_READ_MEM
, &pktlen
, remote_timeout
);
723 error ("Truncated response packet from OCD device");
728 if (error_code
== 0x11) /* Got a bus error? */
730 CORE_ADDR error_address
;
732 error_address
= p
[3] << 24;
733 error_address
|= p
[4] << 16;
734 error_address
|= p
[5] << 8;
735 error_address
|= p
[6];
736 numbytes
= error_address
- memaddr
;
744 else if (error_code
!= 0)
745 ocd_error ("ocd_read_bytes:", error_code
);
747 memcpy (myaddr
, &p
[4], numbytes
);
754 return origlen
- len
;
757 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
758 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
759 nonzero. Returns length of data written or read; 0 for error. */
763 ocd_xfer_memory (memaddr
, myaddr
, len
, should_write
, target
)
768 struct target_ops
*target
; /* ignored */
770 return dcache_xfer_memory (ocd_dcache
, memaddr
, myaddr
, len
, should_write
);
774 ocd_files_info (ignore
)
775 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. */
791 ch
= SERIAL_READCHAR (ocd_desc
, timeout
);
796 error ("Remote connection closed");
798 perror_with_name ("Remote communication error");
806 /* Read a character from the data stream, dequoting as necessary. SYN is
807 treated special. Any SYNs appearing in the data stream are returned as the
808 distinct value RAW_SYN (which has a value > 8 bits and therefore cannot be
809 mistaken for real data). */
812 get_quoted_char (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. */
870 /* Send a packet to the OCD device. The packet framed by a SYN character,
871 a byte count and a checksum. The byte count only counts the number of
872 bytes between the count and the checksum. A count of zero actually
873 means 256. Any SYNs within the packet (including the checksum and
874 count) must be quoted. The quote character must be quoted as well.
875 Quoting is done by replacing the character with the two-character sequence
876 DLE, {char} | 0100. Note that the quoting mechanism has no effect on the
880 stu_put_packet (buf
, len
)
884 unsigned char checksum
;
887 if (len
== 0 || len
> 256)
888 abort (); /* Can't represent 0 length packet */
894 put_quoted_char (RAW_SYN
);
908 put_quoted_char (-checksum
& 0xff);
915 /* Send a packet to the OCD device. The packet framed by a SYN character,
916 a byte count and a checksum. The byte count only counts the number of
917 bytes between the count and the checksum. A count of zero actually
918 means 256. Any SYNs within the packet (including the checksum and
919 count) must be quoted. The quote character must be quoted as well.
920 Quoting is done by replacing the character with the two-character sequence
921 DLE, {char} | 0100. Note that the quoting mechanism has no effect on the
925 ocd_put_packet (buf
, len
)
929 unsigned char checksum
;
931 unsigned char *packet
, *packet_ptr
;
933 packet
= alloca (len
+ 1 + 1); /* packet + SYN + checksum */
938 *packet_ptr
++ = 0x55;
948 *packet_ptr
++ = -checksum
;
949 if (SERIAL_WRITE (ocd_desc
, packet
, packet_ptr
- packet
))
950 perror_with_name ("output_packet: write failed");
955 /* Get a packet from the OCD device. Timeout is only enforced for the
956 first byte of the packet. Subsequent bytes are expected to arrive in
957 time <= remote_timeout. Returns a pointer to a static buffer containing
958 the payload of the packet. *LENP contains the length of the packet.
961 static unsigned char *
962 stu_get_packet (cmd
, lenp
, timeout
)
968 static unsigned char buf
[256 + 10], *p
;
969 unsigned char checksum
;
973 ch
= get_quoted_char (timeout
);
976 error ("get_packet (readchar): %d", ch
);
981 found_syn
: /* Found the start of a packet */
986 len
= get_quoted_char (remote_timeout
);
996 len
++; /* Include checksum */
1000 ch
= get_quoted_char (remote_timeout
);
1012 error ("Response phase error. Got 0x%x, expected 0x%x", buf
[0], cmd
);
1014 *lenp
= p
- buf
- 1;
1020 /* Get a packet from the OCD device. Timeout is only enforced for the
1021 first byte of the packet. Subsequent bytes are expected to arrive in
1022 time <= remote_timeout. Returns a pointer to a static buffer containing
1023 the payload of the packet. *LENP contains the length of the packet.
1026 static unsigned char *
1027 ocd_get_packet (cmd
, lenp
, timeout
)
1034 static unsigned char packet
[512];
1035 unsigned char *packet_ptr
;
1036 unsigned char checksum
;
1038 ch
= readchar (timeout
);
1041 error ("ocd_get_packet (readchar): %d", ch
);
1044 error ("ocd_get_packet (readchar): %d", ch
);
1046 /* Found the start of a packet */
1048 packet_ptr
= packet
;
1051 /* Read command char. That sort of tells us how long the packet is. */
1053 ch
= readchar (timeout
);
1056 error ("ocd_get_packet (readchar): %d", ch
);
1063 ch
= readchar (timeout
);
1066 error ("ocd_get_packet (readchar): %d", ch
);
1070 /* Get error code. */
1072 ch
= readchar (timeout
);
1075 error ("ocd_get_packet (readchar): %d", ch
);
1079 switch (ch
) /* Figure out length of packet */
1081 case 0x7: /* Write verify error? */
1082 len
= 8; /* write address, value read back */
1084 case 0x11: /* Bus error? */
1085 /* write address, read flag */
1086 case 0x15: /* Internal error */
1087 len
= 5; /* error code, vector */
1089 default: /* Error w/no params */
1092 case 0x0: /* Normal result */
1095 case OCD_AYT
: /* Are You There? */
1096 case OCD_SET_BAUD_RATE
: /* Set Baud Rate */
1097 case OCD_INIT
: /* Initialize OCD device */
1098 case OCD_SET_SPEED
: /* Set Speed */
1099 case OCD_SET_FUNC_CODE
: /* Set Function Code */
1100 case OCD_SET_CTL_FLAGS
: /* Set Control Flags */
1101 case OCD_SET_BUF_ADDR
: /* Set Register Buffer Address */
1102 case OCD_RUN
: /* Run Target from PC */
1103 case OCD_RUN_ADDR
: /* Run Target from Specified Address */
1104 case OCD_STOP
: /* Stop Target */
1105 case OCD_RESET_RUN
: /* Reset Target and Run */
1106 case OCD_RESET
: /* Reset Target and Halt */
1107 case OCD_STEP
: /* Single Step */
1108 case OCD_WRITE_REGS
: /* Write Register */
1109 case OCD_WRITE_MEM
: /* Write Memory */
1110 case OCD_FILL_MEM
: /* Fill Memory */
1111 case OCD_MOVE_MEM
: /* Move Memory */
1112 case OCD_WRITE_INT_MEM
: /* Write Internal Memory */
1113 case OCD_JUMP
: /* Jump to Subroutine */
1114 case OCD_ERASE_FLASH
: /* Erase flash memory */
1115 case OCD_PROGRAM_FLASH
: /* Write flash memory */
1116 case OCD_EXIT_MON
: /* Exit the flash programming monitor */
1117 case OCD_ENTER_MON
: /* Enter the flash programming monitor */
1118 case OCD_LOG_FILE
: /* Make Wigglers.dll save Wigglers.log */
1119 case OCD_SET_CONNECTION
: /* Set type of connection in Wigglers.dll */
1122 case OCD_GET_VERSION
: /* Get Version */
1125 case OCD_GET_STATUS_MASK
: /* Get Status Mask */
1128 case OCD_GET_CTRS
: /* Get Error Counters */
1129 case OCD_READ_REGS
: /* Read Register */
1130 case OCD_READ_MEM
: /* Read Memory */
1131 case OCD_READ_INT_MEM
: /* Read Internal Memory */
1135 error ("ocd_get_packet: unknown packet type 0x%x\n", ch
);
1139 if (len
== 257) /* Byte stream? */
1140 { /* Yes, byte streams contain the length */
1141 ch
= readchar (timeout
);
1144 error ("ocd_get_packet (readchar): %d", ch
);
1152 while (len
-- >= 0) /* Do rest of packet and checksum */
1154 ch
= readchar (timeout
);
1157 error ("ocd_get_packet (readchar): %d", ch
);
1163 error ("ocd_get_packet: bad packet checksum");
1165 if (cmd
!= -1 && cmd
!= packet
[0])
1166 error ("Response phase error. Got 0x%x, expected 0x%x", packet
[0], cmd
);
1168 *lenp
= packet_ptr
- packet
- 1; /* Subtract checksum byte */
1173 /* Execute a simple (one-byte) command. Returns a pointer to the data
1174 following the error code. */
1176 static unsigned char *
1177 ocd_do_command (cmd
, statusp
, lenp
)
1182 unsigned char buf
[100], *p
;
1183 int status
, error_code
;
1186 unsigned char logbuf
[100];
1190 ocd_put_packet (buf
, 1); /* Send command */
1191 p
= ocd_get_packet (*buf
, lenp
, remote_timeout
);
1194 error ("Truncated response packet from OCD device");
1199 if (error_code
!= 0)
1201 sprintf (errbuf
, "ocd_do_command (0x%x):", cmd
);
1202 ocd_error (errbuf
, error_code
);
1205 if (status
& OCD_FLAG_PWF
)
1206 error ("OCD device can't detect VCC at BDM interface.");
1207 else if (status
& OCD_FLAG_CABLE_DISC
)
1208 error ("BDM cable appears to be disconnected.");
1212 logbuf
[0] = OCD_LOG_FILE
;
1213 logbuf
[1] = 3; /* close existing WIGGLERS.LOG */
1214 ocd_put_packet (logbuf
, 2);
1215 ocd_get_packet (logbuf
[0], &logpktlen
, remote_timeout
);
1217 logbuf
[0] = OCD_LOG_FILE
;
1218 logbuf
[1] = 2; /* append to existing WIGGLERS.LOG */
1219 ocd_put_packet (logbuf
, 2);
1220 ocd_get_packet (logbuf
[0], &logpktlen
, remote_timeout
);
1228 /* For some mysterious reason, wait_for_inferior calls kill instead of
1229 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1233 target_mourn_inferior ();
1237 /* Don't wait for it to die. I'm not really sure it matters whether
1239 target_mourn_inferior ();
1245 unpush_target (current_ops
);
1246 generic_mourn_inferior ();
1249 /* All we actually do is set the PC to the start address of exec_bfd, and start
1250 the program at that point. */
1253 ocd_create_inferior (exec_file
, args
, env
)
1258 if (args
&& (*args
!= '\000'))
1259 error ("Args are not supported by BDM.");
1261 clear_proceed_status ();
1262 proceed (bfd_get_start_address (exec_bfd
), TARGET_SIGNAL_0
, 0);
1266 ocd_load (args
, from_tty
)
1270 generic_load (args
, from_tty
);
1274 /* This is necessary because many things were based on the PC at the time that
1275 we attached to the monitor, which is no longer valid now that we have loaded
1276 new code (and just changed the PC). Another way to do this might be to call
1277 normal_stop, except that the stack may not be valid, and things would get
1278 horribly confused... */
1280 clear_symtab_users ();
1283 /* This should be defined for each target */
1284 /* But we want to be able to compile this file for some configurations
1285 not yet supported fully */
1287 #define BDM_BREAKPOINT {0x0,0x0,0x0,0x0} /* For ppc 8xx */
1288 /* #define BDM_BREAKPOINT {0x4a,0xfa} /* BGND insn used for CPU32 */
1290 /* BDM (at least on CPU32) uses a different breakpoint */
1293 ocd_insert_breakpoint (addr
, contents_cache
)
1295 char *contents_cache
;
1297 static char break_insn
[] = BDM_BREAKPOINT
;
1300 val
= target_read_memory (addr
, contents_cache
, sizeof (break_insn
));
1303 val
= target_write_memory (addr
, break_insn
, sizeof (break_insn
));
1309 ocd_remove_breakpoint (addr
, contents_cache
)
1311 char *contents_cache
;
1313 static char break_insn
[] = BDM_BREAKPOINT
;
1316 val
= target_write_memory (addr
, contents_cache
, sizeof (break_insn
));
1322 bdm_command (args
, from_tty
)
1326 error ("bdm command must be followed by `reset'");
1330 bdm_reset_command (args
, from_tty
)
1337 error ("Not connected to OCD device.");
1339 ocd_do_command (OCD_RESET
, &status
, &pktlen
);
1340 dcache_flush (ocd_dcache
);
1341 registers_changed ();
1345 bdm_restart_command (args
, from_tty
)
1352 error ("Not connected to OCD device.");
1354 ocd_do_command (OCD_RESET_RUN
, &status
, &pktlen
);
1355 last_run_status
= status
;
1356 clear_proceed_status ();
1357 wait_for_inferior ();
1361 /* Temporary replacement for target_store_registers(). This prevents
1362 generic_load from trying to set the PC. */
1365 noop_store_registers (regno
)
1371 bdm_update_flash_command (args
, from_tty
)
1376 struct cleanup
*old_chain
;
1377 void (*store_registers_tmp
) PARAMS ((int));
1380 error ("Not connected to OCD device.");
1383 error ("Must specify file containing new OCD code.");
1385 /* old_chain = make_cleanup (flash_cleanup, 0);*/
1387 ocd_do_command (OCD_ENTER_MON
, &status
, &pktlen
);
1389 ocd_do_command (OCD_ERASE_FLASH
, &status
, &pktlen
);
1391 write_mem_command
= OCD_PROGRAM_FLASH
;
1392 store_registers_tmp
= current_target
.to_store_registers
;
1393 current_target
.to_store_registers
= noop_store_registers
;
1395 generic_load (args
, from_tty
);
1397 current_target
.to_store_registers
= store_registers_tmp
;
1398 write_mem_command
= OCD_WRITE_MEM
;
1400 ocd_do_command (OCD_EXIT_MON
, &status
, &pktlen
);
1402 /* discard_cleanups (old_chain);*/
1406 bdm_read_register_command (args
, from_tty
)
1410 /* XXX repeat should go on to the next register */
1413 error ("Not connected to OCD device.");
1416 error ("Must specify BDM register number.");
1421 _initialize_remote_ocd ()
1423 extern struct cmd_list_element
*cmdlist
;
1424 static struct cmd_list_element
*ocd_cmd_list
= NULL
;
1426 add_show_from_set (add_set_cmd ("remotetimeout", no_class
,
1427 var_integer
, (char *)&remote_timeout
,
1428 "Set timeout value for remote read.\n", &setlist
),
1431 add_prefix_cmd ("ocd", class_obscure
, bdm_command
, "", &ocd_cmd_list
, "ocd ",
1434 add_cmd ("reset", class_obscure
, bdm_reset_command
, "", &ocd_cmd_list
);
1435 add_cmd ("restart", class_obscure
, bdm_restart_command
, "", &ocd_cmd_list
);
1436 add_cmd ("update-flash", class_obscure
, bdm_update_flash_command
, "", &ocd_cmd_list
);
1437 /* add_cmd ("read-register", class_obscure, bdm_read_register_command, "", &ocd_cmd_list);*/