| 1 | /* |
| 2 | * avarice - The "avarice" program. |
| 3 | * Copyright (C) 2001 Scott Finneran |
| 4 | * Copyright (C) 2002 Intel Corporation |
| 5 | * Copyright (C) 2005, 2007 Joerg Wunsch |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License Version 2 |
| 9 | * as published by the Free Software Foundation. |
| 10 | * |
| 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. |
| 15 | * |
| 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, USA. |
| 19 | * |
| 20 | * This file contains functions for interfacing with the JTAG box. |
| 21 | * |
| 22 | * $Id: jtagrun.cc 290 2011-12-12 13:24:07Z joerg_wunsch $ |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include <stdarg.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <unistd.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/time.h> |
| 32 | #include <termios.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | #include "avarice.h" |
| 37 | #include "jtag.h" |
| 38 | #include "jtag1.h" |
| 39 | #include "remote.h" |
| 40 | |
| 41 | unsigned long jtag1::getProgramCounter(void) |
| 42 | { |
| 43 | uchar *response = NULL; |
| 44 | uchar command[] = {'2', JTAG_EOM }; |
| 45 | unsigned long result = 0; |
| 46 | |
| 47 | response = doJtagCommand(command, sizeof(command), 4); |
| 48 | |
| 49 | if (response[3] != JTAG_R_OK) |
| 50 | result = PC_INVALID; |
| 51 | else |
| 52 | { |
| 53 | result = decodeAddress(response); |
| 54 | |
| 55 | result--; // returned value is PC + 1 as far as GDB is concerned |
| 56 | |
| 57 | // The JTAG box sees program memory as 16-bit wide locations. GDB |
| 58 | // sees bytes. As such, double the PC value. |
| 59 | result *= 2; |
| 60 | } |
| 61 | |
| 62 | delete [] response; |
| 63 | return result; |
| 64 | } |
| 65 | |
| 66 | void jtag1::setProgramCounter(unsigned long pc) |
| 67 | { |
| 68 | uchar *response = NULL; |
| 69 | uchar command[] = {'3', 0, 0, 0, JTAG_EOM }; |
| 70 | |
| 71 | // See decoding in getProgramCounter |
| 72 | encodeAddress(&command[1], pc / 2 + 1); |
| 73 | |
| 74 | response = doJtagCommand(command, sizeof(command), 1); |
| 75 | |
| 76 | if (response[0] != JTAG_R_OK) |
| 77 | throw jtag_exception(); |
| 78 | |
| 79 | delete [] response; |
| 80 | } |
| 81 | |
| 82 | void jtag1::resetProgram(bool possible_nSRST) |
| 83 | { |
| 84 | if (possible_nSRST && apply_nSRST) { |
| 85 | setJtagParameter(JTAG_P_EXTERNAL_RESET, 0x01); |
| 86 | } |
| 87 | doSimpleJtagCommand('x', 1); |
| 88 | if (possible_nSRST && apply_nSRST) { |
| 89 | setJtagParameter(JTAG_P_EXTERNAL_RESET, 0x00); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void jtag1::interruptProgram(void) |
| 94 | { |
| 95 | // Just ignore the returned PC. It appears to be wrong if the most |
| 96 | // recent instruction was a branch. |
| 97 | doSimpleJtagCommand('F', 4); |
| 98 | } |
| 99 | |
| 100 | void jtag1::resumeProgram(void) |
| 101 | { |
| 102 | doSimpleJtagCommand('G', 0); |
| 103 | } |
| 104 | |
| 105 | void jtag1::jtagSingleStep(void) |
| 106 | { |
| 107 | doSimpleJtagCommand('1', 1); |
| 108 | } |
| 109 | |
| 110 | void jtag1::parseEvents(const char *) |
| 111 | { |
| 112 | // current no event name parsing in mkI |
| 113 | } |
| 114 | |
| 115 | bool jtag1::jtagContinue(void) |
| 116 | { |
| 117 | updateBreakpoints(); // download new bp configuration |
| 118 | |
| 119 | if (!doSimpleJtagCommand('G', 0)) |
| 120 | { |
| 121 | gdbOut("Failed to continue\n"); |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | for (;;) |
| 126 | { |
| 127 | int maxfd; |
| 128 | fd_set readfds; |
| 129 | bool breakpoint = false, gdbInterrupt = false; |
| 130 | |
| 131 | // Now that we are "going", wait for either a response from the JTAG |
| 132 | // box or a nudge from GDB. |
| 133 | debugOut("Waiting for input.\n"); |
| 134 | |
| 135 | // Check for input from JTAG ICE (breakpoint, sleep, info, power) |
| 136 | // or gdb (user break) |
| 137 | FD_ZERO (&readfds); |
| 138 | FD_SET (gdbFileDescriptor, &readfds); |
| 139 | FD_SET (jtagBox, &readfds); |
| 140 | maxfd = jtagBox > gdbFileDescriptor ? jtagBox : gdbFileDescriptor; |
| 141 | |
| 142 | int numfds = select(maxfd + 1, &readfds, 0, 0, 0); |
| 143 | if (numfds < 0) |
| 144 | { |
| 145 | fprintf(stderr, "GDB/JTAG ICE communications failure"); |
| 146 | throw jtag_exception(); |
| 147 | } |
| 148 | |
| 149 | if (FD_ISSET(gdbFileDescriptor, &readfds)) |
| 150 | { |
| 151 | int c = getDebugChar(); |
| 152 | if (c == 3) // interrupt |
| 153 | { |
| 154 | debugOut("interrupted by GDB\n"); |
| 155 | gdbInterrupt = true; |
| 156 | } |
| 157 | else |
| 158 | debugOut("Unexpected GDB input `%02x'\n", c); |
| 159 | } |
| 160 | |
| 161 | // Read all extant responses (there's a small chance there could |
| 162 | // be more than one) |
| 163 | |
| 164 | // Note: In case of a gdb interrupt, it's possible that we will |
| 165 | // receive one of these responses before interruptProgram is |
| 166 | // called. This should not cause a problem as the normal retry |
| 167 | // mechanism should eat up this response. |
| 168 | // It might be cleaner to handle JTAG_R_xxx in sendJtagCommand |
| 169 | |
| 170 | // Check the JTAG ICE's message. It can indicate a breakpoint |
| 171 | // which causes us to return, a sleep status change (ignored), |
| 172 | // power "event" -- whatever that is (ignored), or a byte of |
| 173 | // info sent by the program (currently ignored, could be used |
| 174 | // for something...) |
| 175 | uchar response; |
| 176 | |
| 177 | // This read shouldn't need to be a timeout_read(), but some cygwin |
| 178 | // systems don't seem to honor the O_NONBLOCK flag on file |
| 179 | // descriptors. |
| 180 | while (timeout_read(&response, 1, 1) == 1) |
| 181 | { |
| 182 | uchar buf[2]; |
| 183 | int count; |
| 184 | |
| 185 | debugOut("JTAG box sent %c", response); |
| 186 | switch (response) |
| 187 | { |
| 188 | case JTAG_R_BREAK: |
| 189 | count = timeout_read(buf, 2, JTAG_RESPONSE_TIMEOUT); |
| 190 | if (count < 2) |
| 191 | throw jtag_exception(); |
| 192 | breakpoint = true; |
| 193 | debugOut(": Break Status Register = 0x%02x%02x\n", |
| 194 | buf[0], buf[1]); |
| 195 | break; |
| 196 | case JTAG_R_INFO: case JTAG_R_SLEEP: |
| 197 | // we could do something here, esp. for info |
| 198 | count = timeout_read(buf, 2, JTAG_RESPONSE_TIMEOUT); |
| 199 | if (count < 2) |
| 200 | throw jtag_exception(); |
| 201 | debugOut(": 0x%02, 0x%02\n", buf[0], buf[1]); |
| 202 | break; |
| 203 | case JTAG_R_POWER: |
| 204 | // apparently no args? |
| 205 | debugOut("\n"); |
| 206 | break; |
| 207 | default: |
| 208 | debugOut(": Unknown response\n"); |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // We give priority to user interrupts |
| 214 | if (gdbInterrupt) |
| 215 | return false; |
| 216 | if (breakpoint) |
| 217 | return true; |
| 218 | } |
| 219 | } |
| 220 |