| 1 | /* |
| 2 | * avarice - The "avarice" program. |
| 3 | * Copyright (C) 2001 Scott Finneran |
| 4 | * Copyright (C) 2002 Intel Corporation |
| 5 | * Copyright (C) 2005 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: jtagbp.cc 322 2013-01-07 09:47:52Z 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 <termios.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include "avarice.h" |
| 36 | #include "jtag.h" |
| 37 | #include "jtag1.h" |
| 38 | |
| 39 | bool jtag1::codeBreakpointAt(unsigned int address) |
| 40 | { |
| 41 | address /= 2; |
| 42 | for (int i = 0; i < numBreakpointsCode; i++) |
| 43 | if (bpCode[i].address == address) |
| 44 | return true; |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | void jtag1::deleteAllBreakpoints(void) |
| 49 | { |
| 50 | numBreakpointsData = numBreakpointsCode = 0; |
| 51 | } |
| 52 | |
| 53 | PRAGMA_DIAG_PUSH |
| 54 | PRAGMA_DIAG_IGNORED("-Wunused-parameter") |
| 55 | |
| 56 | bool jtag1::addBreakpoint(unsigned int address, bpType type, unsigned int length) |
| 57 | { |
| 58 | breakpoint *bp; |
| 59 | |
| 60 | debugOut("BP ADD type: %d addr: 0x%x ", type, address); |
| 61 | |
| 62 | // Respect overall breakpoint limit |
| 63 | if (numBreakpointsCode + numBreakpointsData == MAX_BREAKPOINTS) |
| 64 | return false; |
| 65 | |
| 66 | // There's a spare breakpoint, is there one of the appropriate type |
| 67 | // available? |
| 68 | if (type == CODE) |
| 69 | { |
| 70 | if (numBreakpointsCode == MAX_BREAKPOINTS_CODE) |
| 71 | { |
| 72 | debugOut("FAILED\n"); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | bp = &bpCode[numBreakpointsCode++]; |
| 77 | |
| 78 | // The JTAG box sees program memory as 16-bit wide locations. GDB |
| 79 | // sees bytes. As such, halve the breakpoint address. |
| 80 | address /= 2; |
| 81 | } |
| 82 | else // data breakpoint |
| 83 | { |
| 84 | if (numBreakpointsData == MAX_BREAKPOINTS_DATA) |
| 85 | { |
| 86 | debugOut("FAILED\n"); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | bp = &bpData[numBreakpointsData++]; |
| 91 | } |
| 92 | |
| 93 | bp->address = address; |
| 94 | bp->type = type; |
| 95 | |
| 96 | debugOut(" ADDED\n"); |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | bool jtag1::deleteBreakpoint(unsigned int address, bpType type, unsigned int length) |
| 102 | { |
| 103 | breakpoint *bp; |
| 104 | int *numBp; |
| 105 | |
| 106 | debugOut("BP DEL type: %d addr: 0x%x ", type, address); |
| 107 | |
| 108 | if (type == CODE) |
| 109 | { |
| 110 | bp = bpCode; |
| 111 | numBp = &numBreakpointsCode; |
| 112 | // The JTAG box sees program memory as 16-bit wide locations. GDB |
| 113 | // sees bytes. As such, halve the breakpoint address. |
| 114 | address /= 2; |
| 115 | } |
| 116 | else // data |
| 117 | { |
| 118 | bp = bpData; |
| 119 | numBp = &numBreakpointsData; |
| 120 | } |
| 121 | |
| 122 | // Find and squash the removed breakpoint |
| 123 | for (int i = 0; i < *numBp; i++) |
| 124 | { |
| 125 | if (bp[i].type == type && bp[i].address == address) |
| 126 | { |
| 127 | debugOut("REMOVED %d\n", i); |
| 128 | (*numBp)--; |
| 129 | memmove(&bp[i], &bp[i + 1], (*numBp - i) * sizeof(breakpoint)); |
| 130 | return true; |
| 131 | } |
| 132 | } |
| 133 | debugOut("FAILED\n"); |
| 134 | return false; |
| 135 | } |
| 136 | PRAGMA_DIAG_POP |
| 137 | |
| 138 | void jtag1::updateBreakpoints(void) |
| 139 | { |
| 140 | unsigned char bpMode = 0x00; |
| 141 | int bpC = 0, bpD = 0; |
| 142 | breakpoint *bp; |
| 143 | |
| 144 | debugOut("updateBreakpoints\n"); |
| 145 | |
| 146 | // BP 0 (aka breakpoint Z0). |
| 147 | // Send breakpoint array down to the target. |
| 148 | // BP 1 is activated by writing a 1 to BP address space. |
| 149 | // note: BP1 only supports code space breakpoints. |
| 150 | if (bpC < numBreakpointsCode) |
| 151 | { |
| 152 | uchar zero = 0; |
| 153 | jtagWrite(BREAKPOINT_SPACE_ADDR_OFFSET + bpCode[bpC++].address, 1, &zero); |
| 154 | } |
| 155 | |
| 156 | // BP 1 (aka breakpoint Z1). |
| 157 | // Send breakpoint array down to the target. |
| 158 | // BP 1 is activated by writing a 1 to BP address space. |
| 159 | // note: BP1 only supports code space breakpoints. |
| 160 | if (bpC < numBreakpointsCode) |
| 161 | { |
| 162 | uchar one = 1; |
| 163 | jtagWrite(BREAKPOINT_SPACE_ADDR_OFFSET + bpCode[bpC++].address, 1, &one); |
| 164 | } |
| 165 | |
| 166 | // Set X & Y breakpoints. Code is cut&pasty, but it's only two copies and |
| 167 | // has tons of parameters. |
| 168 | |
| 169 | // Find next breakpoint |
| 170 | bp = NULL; |
| 171 | if (bpC < numBreakpointsCode) |
| 172 | bp = &bpCode[bpC++]; |
| 173 | else if (bpD < numBreakpointsData) |
| 174 | bp = &bpData[bpD++]; |
| 175 | |
| 176 | // BP 2 (aka breakpoint X). |
| 177 | if (bp) |
| 178 | { |
| 179 | setJtagParameter(JTAG_P_BP_X_HIGH, bp->address >> 8); |
| 180 | setJtagParameter(JTAG_P_BP_X_LOW, bp->address & 0xff); |
| 181 | |
| 182 | bpMode |= 0x20; // turn on this breakpoint |
| 183 | switch (bp->type) |
| 184 | { |
| 185 | case READ_DATA: |
| 186 | bpMode |= 0x00; |
| 187 | break; |
| 188 | case WRITE_DATA: |
| 189 | bpMode |= 0x04; |
| 190 | break; |
| 191 | case ACCESS_DATA: |
| 192 | bpMode |= 0x08; |
| 193 | break; |
| 194 | case CODE: |
| 195 | bpMode |= 0x0c; |
| 196 | break; |
| 197 | case NONE: |
| 198 | default: |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | |
| 203 | // Find next breakpoint |
| 204 | bp = NULL; |
| 205 | if (bpC < numBreakpointsCode) |
| 206 | bp = &bpCode[bpC++]; |
| 207 | else if (bpD < numBreakpointsData) |
| 208 | bp = &bpData[bpD++]; |
| 209 | |
| 210 | // BP 3 (aka breakpoint Y). |
| 211 | if (bp) |
| 212 | { |
| 213 | setJtagParameter(JTAG_P_BP_Y_HIGH, bp->address >> 8); |
| 214 | setJtagParameter(JTAG_P_BP_Y_LOW, bp->address & 0xff); |
| 215 | |
| 216 | // Only used when BP2 is on, so this is correcy |
| 217 | bpMode |= 0x10; // turn on this breakpoint |
| 218 | switch (bp->type) |
| 219 | { |
| 220 | case READ_DATA: |
| 221 | bpMode |= 0x00; |
| 222 | break; |
| 223 | case WRITE_DATA: |
| 224 | bpMode |= 0x01; |
| 225 | break; |
| 226 | case ACCESS_DATA: |
| 227 | bpMode |= 0x02; |
| 228 | break; |
| 229 | case CODE: |
| 230 | bpMode |= 0x03; |
| 231 | break; |
| 232 | case NONE: |
| 233 | default: |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | setJtagParameter(JTAG_P_BP_MODE, bpMode); |
| 239 | } |
| 240 | } |