cortex_m3: use armv7m's async algorithm implementation
[openocd.git] / src / target / dsp563xx.c
blobb7f23c70f96649602dd8b8aee965dbf37ac608ed
1 /***************************************************************************
2 * Copyright (C) 2009-2011 by Mathias Kuester *
3 * mkdorg@users.sourceforge.net *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <jim.h>
26 #include "target.h"
27 #include "target_type.h"
28 #include "algorithm.h"
29 #include "register.h"
30 #include "dsp563xx.h"
31 #include "dsp563xx_once.h"
33 #define ASM_REG_W_R0 0x60F400
34 #define ASM_REG_W_R1 0x61F400
35 #define ASM_REG_W_R2 0x62F400
36 #define ASM_REG_W_R3 0x63F400
37 #define ASM_REG_W_R4 0x64F400
38 #define ASM_REG_W_R5 0x65F400
39 #define ASM_REG_W_R6 0x66F400
40 #define ASM_REG_W_R7 0x67F400
42 #define ASM_REG_W_N0 0x70F400
43 #define ASM_REG_W_N1 0x71F400
44 #define ASM_REG_W_N2 0x72F400
45 #define ASM_REG_W_N3 0x73F400
46 #define ASM_REG_W_N4 0x74F400
47 #define ASM_REG_W_N5 0x75F400
48 #define ASM_REG_W_N6 0x76F400
49 #define ASM_REG_W_N7 0x77F400
51 #define ASM_REG_W_M0 0x05F420
52 #define ASM_REG_W_M1 0x05F421
53 #define ASM_REG_W_M2 0x05F422
54 #define ASM_REG_W_M3 0x05F423
55 #define ASM_REG_W_M4 0x05F424
56 #define ASM_REG_W_M5 0x05F425
57 #define ASM_REG_W_M6 0x05F426
58 #define ASM_REG_W_M7 0x05F427
60 #define ASM_REG_W_X0 0x44F400
61 #define ASM_REG_W_X1 0x45F400
63 #define ASM_REG_W_Y0 0x46F400
64 #define ASM_REG_W_Y1 0x47F400
66 #define ASM_REG_W_A0 0x50F400
67 #define ASM_REG_W_A1 0x54F400
68 #define ASM_REG_W_A2 0x52F400
70 #define ASM_REG_W_B0 0x51F400
71 #define ASM_REG_W_B1 0x55F400
72 #define ASM_REG_W_B2 0x53F400
74 #define ASM_REG_W_VBA 0x05F430
75 #define ASM_REG_W_OMR 0x05F43A
76 #define ASM_REG_W_EP 0x05F42A
77 #define ASM_REG_W_SC 0x05F431
78 #define ASM_REG_W_SZ 0x05F438
79 #define ASM_REG_W_SR 0x05F439
80 #define ASM_REG_W_SP 0x05F43B
81 #define ASM_REG_W_SSH 0x05F43C
82 #define ASM_REG_W_SSL 0x05F43D
83 #define ASM_REG_W_LA 0x05F43E
84 #define ASM_REG_W_LC 0x05F43F
85 #define ASM_REG_W_PC 0x000000
86 #define ASM_REG_W_IPRC 0xFFFFFF
87 #define ASM_REG_W_IPRP 0xFFFFFE
89 #define ASM_REG_W_BCR 0xFFFFFB
90 #define ASM_REG_W_DCR 0xFFFFFA
91 #define ASM_REG_W_AAR0 0xFFFFF9
92 #define ASM_REG_W_AAR1 0xFFFFF8
93 #define ASM_REG_W_AAR2 0xFFFFF7
94 #define ASM_REG_W_AAR3 0xFFFFF6
96 enum once_reg_idx {
97 ONCE_REG_IDX_OSCR=0,
98 ONCE_REG_IDX_OMBC=1,
99 ONCE_REG_IDX_OBCR=2,
100 ONCE_REG_IDX_OMLR0=3,
101 ONCE_REG_IDX_OMLR1=4,
102 ONCE_REG_IDX_OGDBR=5,
103 ONCE_REG_IDX_OPDBR=6,
104 ONCE_REG_IDX_OPILR=7,
105 ONCE_REG_IDX_PDB=8,
106 ONCE_REG_IDX_OTC=9,
107 ONCE_REG_IDX_OPABFR=10,
108 ONCE_REG_IDX_OPABDR=11,
109 ONCE_REG_IDX_OPABEX=12,
110 ONCE_REG_IDX_OPABF0=13,
111 ONCE_REG_IDX_OPABF1=14,
112 ONCE_REG_IDX_OPABF2=15,
113 ONCE_REG_IDX_OPABF3=16,
114 ONCE_REG_IDX_OPABF4=17,
115 ONCE_REG_IDX_OPABF5=18,
116 ONCE_REG_IDX_OPABF6=19,
117 ONCE_REG_IDX_OPABF7=20,
118 ONCE_REG_IDX_OPABF8=21,
119 ONCE_REG_IDX_OPABF9=22,
120 ONCE_REG_IDX_OPABF10=23,
121 ONCE_REG_IDX_OPABF11=24,
124 static struct once_reg once_regs[] = {
125 {ONCE_REG_IDX_OSCR, DSP563XX_ONCE_OSCR, 24, "OSCR", 0},
126 {ONCE_REG_IDX_OMBC, DSP563XX_ONCE_OMBC, 24, "OMBC", 0},
127 {ONCE_REG_IDX_OBCR, DSP563XX_ONCE_OBCR, 24, "OBCR", 0},
128 {ONCE_REG_IDX_OMLR0, DSP563XX_ONCE_OMLR0, 24, "OMLR0", 0},
129 {ONCE_REG_IDX_OMLR1, DSP563XX_ONCE_OMLR1, 24, "OMLR1", 0},
130 {ONCE_REG_IDX_OGDBR, DSP563XX_ONCE_OGDBR, 24, "OGDBR", 0},
131 {ONCE_REG_IDX_OPDBR, DSP563XX_ONCE_OPDBR, 24, "OPDBR", 0},
132 {ONCE_REG_IDX_OPILR, DSP563XX_ONCE_OPILR, 24, "OPILR", 0},
133 {ONCE_REG_IDX_PDB, DSP563XX_ONCE_PDBGOTO, 24, "PDB", 0},
134 {ONCE_REG_IDX_OTC, DSP563XX_ONCE_OTC, 24, "OTC", 0},
135 {ONCE_REG_IDX_OPABFR, DSP563XX_ONCE_OPABFR, 24, "OPABFR", 0},
136 {ONCE_REG_IDX_OPABDR, DSP563XX_ONCE_OPABDR, 24, "OPABDR", 0},
137 {ONCE_REG_IDX_OPABEX, DSP563XX_ONCE_OPABEX, 24, "OPABEX", 0},
138 {ONCE_REG_IDX_OPABF0, DSP563XX_ONCE_OPABF11, 25, "OPABF0", 0},
139 {ONCE_REG_IDX_OPABF1, DSP563XX_ONCE_OPABF11, 25, "OPABF1", 0},
140 {ONCE_REG_IDX_OPABF2, DSP563XX_ONCE_OPABF11, 25, "OPABF2", 0},
141 {ONCE_REG_IDX_OPABF3, DSP563XX_ONCE_OPABF11, 25, "OPABF3", 0},
142 {ONCE_REG_IDX_OPABF4, DSP563XX_ONCE_OPABF11, 25, "OPABF4", 0},
143 {ONCE_REG_IDX_OPABF5, DSP563XX_ONCE_OPABF11, 25, "OPABF5", 0},
144 {ONCE_REG_IDX_OPABF6, DSP563XX_ONCE_OPABF11, 25, "OPABF6", 0},
145 {ONCE_REG_IDX_OPABF7, DSP563XX_ONCE_OPABF11, 25, "OPABF7", 0},
146 {ONCE_REG_IDX_OPABF8, DSP563XX_ONCE_OPABF11, 25, "OPABF8", 0},
147 {ONCE_REG_IDX_OPABF9, DSP563XX_ONCE_OPABF11, 25, "OPABF9", 0},
148 {ONCE_REG_IDX_OPABF10, DSP563XX_ONCE_OPABF11, 25, "OPABF10", 0},
149 {ONCE_REG_IDX_OPABF11, DSP563XX_ONCE_OPABF11, 25, "OPABF11", 0},
150 // {25,0x1f,24,"NRSEL",0},
153 enum dsp563xx_reg_idx {
154 DSP563XX_REG_IDX_R0=0,
155 DSP563XX_REG_IDX_R1=1,
156 DSP563XX_REG_IDX_R2=2,
157 DSP563XX_REG_IDX_R3=3,
158 DSP563XX_REG_IDX_R4=4,
159 DSP563XX_REG_IDX_R5=5,
160 DSP563XX_REG_IDX_R6=6,
161 DSP563XX_REG_IDX_R7=7,
162 DSP563XX_REG_IDX_N0=8,
163 DSP563XX_REG_IDX_N1=9,
164 DSP563XX_REG_IDX_N2=10,
165 DSP563XX_REG_IDX_N3=11,
166 DSP563XX_REG_IDX_N4=12,
167 DSP563XX_REG_IDX_N5=13,
168 DSP563XX_REG_IDX_N6=14,
169 DSP563XX_REG_IDX_N7=15,
170 DSP563XX_REG_IDX_M0=16,
171 DSP563XX_REG_IDX_M1=17,
172 DSP563XX_REG_IDX_M2=18,
173 DSP563XX_REG_IDX_M3=19,
174 DSP563XX_REG_IDX_M4=20,
175 DSP563XX_REG_IDX_M5=21,
176 DSP563XX_REG_IDX_M6=22,
177 DSP563XX_REG_IDX_M7=23,
178 DSP563XX_REG_IDX_X0=24,
179 DSP563XX_REG_IDX_X1=25,
180 DSP563XX_REG_IDX_Y0=26,
181 DSP563XX_REG_IDX_Y1=27,
182 DSP563XX_REG_IDX_A0=28,
183 DSP563XX_REG_IDX_A1=29,
184 DSP563XX_REG_IDX_A2=30,
185 DSP563XX_REG_IDX_B0=31,
186 DSP563XX_REG_IDX_B1=32,
187 DSP563XX_REG_IDX_B2=33,
188 DSP563XX_REG_IDX_SSH=34,
189 DSP563XX_REG_IDX_SSL=35,
190 DSP563XX_REG_IDX_SP=36,
191 DSP563XX_REG_IDX_EP=37,
192 DSP563XX_REG_IDX_SZ=38,
193 DSP563XX_REG_IDX_SC=39,
194 DSP563XX_REG_IDX_PC=40,
195 DSP563XX_REG_IDX_SR=41,
196 DSP563XX_REG_IDX_OMR=42,
197 DSP563XX_REG_IDX_LA=43,
198 DSP563XX_REG_IDX_LC=44,
199 DSP563XX_REG_IDX_VBA=45,
200 DSP563XX_REG_IDX_IPRC=46,
201 DSP563XX_REG_IDX_IPRP=47,
202 DSP563XX_REG_IDX_BCR=48,
203 DSP563XX_REG_IDX_DCR=49,
204 DSP563XX_REG_IDX_AAR0=50,
205 DSP563XX_REG_IDX_AAR1=51,
206 DSP563XX_REG_IDX_AAR2=52,
207 DSP563XX_REG_IDX_AAR3=53,
210 static const struct
212 unsigned id;
213 const char *name;
214 unsigned bits;
215 /* effective addressing mode encoding */
216 uint8_t eame;
217 uint32_t instr_mask;
218 } dsp563xx_regs[] =
220 /* *INDENT-OFF* */
221 /* address registers */
222 {DSP563XX_REG_IDX_R0, "r0", 24, 0x10, ASM_REG_W_R0},
223 {DSP563XX_REG_IDX_R1, "r1", 24, 0x11, ASM_REG_W_R1},
224 {DSP563XX_REG_IDX_R2, "r2", 24, 0x12, ASM_REG_W_R2},
225 {DSP563XX_REG_IDX_R3, "r3", 24, 0x13, ASM_REG_W_R3},
226 {DSP563XX_REG_IDX_R4, "r4", 24, 0x14, ASM_REG_W_R4},
227 {DSP563XX_REG_IDX_R5, "r5", 24, 0x15, ASM_REG_W_R5},
228 {DSP563XX_REG_IDX_R6, "r6", 24, 0x16, ASM_REG_W_R6},
229 {DSP563XX_REG_IDX_R7, "r7", 24, 0x17, ASM_REG_W_R7},
230 /* offset registers */
231 {DSP563XX_REG_IDX_N0, "n0", 24, 0x18, ASM_REG_W_N0},
232 {DSP563XX_REG_IDX_N1, "n1", 24, 0x19, ASM_REG_W_N1},
233 {DSP563XX_REG_IDX_N2, "n2", 24, 0x1a, ASM_REG_W_N2},
234 {DSP563XX_REG_IDX_N3, "n3", 24, 0x1b, ASM_REG_W_N3},
235 {DSP563XX_REG_IDX_N4, "n4", 24, 0x1c, ASM_REG_W_N4},
236 {DSP563XX_REG_IDX_N5, "n5", 24, 0x1d, ASM_REG_W_N5},
237 {DSP563XX_REG_IDX_N6, "n6", 24, 0x1e, ASM_REG_W_N6},
238 {DSP563XX_REG_IDX_N7, "n7", 24, 0x1f, ASM_REG_W_N7},
239 /* modifier registers */
240 {DSP563XX_REG_IDX_M0, "m0", 24, 0x20, ASM_REG_W_M0},
241 {DSP563XX_REG_IDX_M1, "m1", 24, 0x21, ASM_REG_W_M1},
242 {DSP563XX_REG_IDX_M2, "m2", 24, 0x22, ASM_REG_W_M2},
243 {DSP563XX_REG_IDX_M3, "m3", 24, 0x23, ASM_REG_W_M3},
244 {DSP563XX_REG_IDX_M4, "m4", 24, 0x24, ASM_REG_W_M4},
245 {DSP563XX_REG_IDX_M5, "m5", 24, 0x25, ASM_REG_W_M5},
246 {DSP563XX_REG_IDX_M6, "m6", 24, 0x26, ASM_REG_W_M6},
247 {DSP563XX_REG_IDX_M7, "m7", 24, 0x27, ASM_REG_W_M7},
248 /* data alu input register */
249 {DSP563XX_REG_IDX_X0, "x0", 24, 0x04, ASM_REG_W_X0},
250 {DSP563XX_REG_IDX_X1, "x1", 24, 0x05, ASM_REG_W_X1},
251 {DSP563XX_REG_IDX_Y0, "y0", 24, 0x06, ASM_REG_W_Y0},
252 {DSP563XX_REG_IDX_Y1, "y1", 24, 0x07, ASM_REG_W_Y1},
253 /* data alu accumulator register */
254 {DSP563XX_REG_IDX_A0, "a0", 24, 0x08, ASM_REG_W_A0},
255 {DSP563XX_REG_IDX_A1, "a1", 24, 0x0c, ASM_REG_W_A1},
256 {DSP563XX_REG_IDX_A2, "a2", 8, 0x0a, ASM_REG_W_A2},
257 {DSP563XX_REG_IDX_B0, "b0", 24, 0x09, ASM_REG_W_B0},
258 {DSP563XX_REG_IDX_B1, "b1", 24, 0x0d, ASM_REG_W_B1},
259 {DSP563XX_REG_IDX_B2, "b2", 8, 0x0b, ASM_REG_W_B2},
260 /* stack */
261 {DSP563XX_REG_IDX_SSH, "ssh",24, 0x3c, ASM_REG_W_SSH},
262 {DSP563XX_REG_IDX_SSL, "ssl",24, 0x3d, ASM_REG_W_SSL},
263 {DSP563XX_REG_IDX_SP, "sp", 24, 0x3b, ASM_REG_W_SP},
264 {DSP563XX_REG_IDX_EP, "ep", 24, 0x2a, ASM_REG_W_EP},
265 {DSP563XX_REG_IDX_SZ, "sz", 24, 0x38, ASM_REG_W_SZ},
266 {DSP563XX_REG_IDX_SC, "sc", 24, 0x31, ASM_REG_W_SC},
267 /* system */
268 {DSP563XX_REG_IDX_PC, "pc", 24, 0x00, ASM_REG_W_PC},
269 {DSP563XX_REG_IDX_SR, "sr", 24, 0x39, ASM_REG_W_SR},
270 {DSP563XX_REG_IDX_OMR, "omr",24, 0x3a, ASM_REG_W_OMR},
271 {DSP563XX_REG_IDX_LA, "la", 24, 0x3e, ASM_REG_W_LA},
272 {DSP563XX_REG_IDX_LC, "lc", 24, 0x3f, ASM_REG_W_LC},
273 /* interrupt */
274 {DSP563XX_REG_IDX_VBA, "vba", 24, 0x30, ASM_REG_W_VBA},
275 {DSP563XX_REG_IDX_IPRC, "iprc",24, 0x00, ASM_REG_W_IPRC},
276 {DSP563XX_REG_IDX_IPRP, "iprp",24, 0x00, ASM_REG_W_IPRP},
277 /* port a */
278 {DSP563XX_REG_IDX_BCR, "bcr", 24, 0x00, ASM_REG_W_BCR},
279 {DSP563XX_REG_IDX_DCR, "dcr", 24, 0x00, ASM_REG_W_DCR},
280 {DSP563XX_REG_IDX_AAR0, "aar0",24, 0x00, ASM_REG_W_AAR0},
281 {DSP563XX_REG_IDX_AAR1, "aar1",24, 0x00, ASM_REG_W_AAR1},
282 {DSP563XX_REG_IDX_AAR2, "aar2",24, 0x00, ASM_REG_W_AAR2},
283 {DSP563XX_REG_IDX_AAR3, "aar3",24, 0x00, ASM_REG_W_AAR3},
284 /* *INDENT-ON* */
287 enum memory_type
289 MEM_X = 0,
290 MEM_Y = 1,
291 MEM_P = 2,
292 MEM_L = 3,
295 #define INSTR_JUMP 0x0AF080
296 /* Effective Addressing Mode Encoding */
297 #define EAME_R0 0x10
298 /* instrcution encoder */
299 /* movep
300 * s - peripheral space X/Y (X=0,Y=1)
301 * w - write/read
302 * d - source/destination register
303 * p - IO short address
305 #define INSTR_MOVEP_REG_HIO(s,w,d,p) (0x084000 | ((s & 1)<<16) | ((w&1)<<15) | ((d & 0x3f)<<8) | (p & 0x3f))
307 /* the gdb register list is send in this order */
308 uint8_t gdb_reg_list_idx[] = {
309 DSP563XX_REG_IDX_X1, DSP563XX_REG_IDX_X0, DSP563XX_REG_IDX_Y1, DSP563XX_REG_IDX_Y0,
310 DSP563XX_REG_IDX_A2, DSP563XX_REG_IDX_A1, DSP563XX_REG_IDX_A0, DSP563XX_REG_IDX_B2,
311 DSP563XX_REG_IDX_B1, DSP563XX_REG_IDX_B0, DSP563XX_REG_IDX_PC, DSP563XX_REG_IDX_SR,
312 DSP563XX_REG_IDX_OMR,DSP563XX_REG_IDX_LA, DSP563XX_REG_IDX_LC, DSP563XX_REG_IDX_SSH,
313 DSP563XX_REG_IDX_SSL,DSP563XX_REG_IDX_SP, DSP563XX_REG_IDX_EP, DSP563XX_REG_IDX_SZ,
314 DSP563XX_REG_IDX_SC, DSP563XX_REG_IDX_VBA,DSP563XX_REG_IDX_IPRC, DSP563XX_REG_IDX_IPRP,
315 DSP563XX_REG_IDX_BCR,DSP563XX_REG_IDX_DCR,DSP563XX_REG_IDX_AAR0,DSP563XX_REG_IDX_AAR1,
316 DSP563XX_REG_IDX_AAR2,DSP563XX_REG_IDX_AAR3,DSP563XX_REG_IDX_R0,DSP563XX_REG_IDX_R1,
317 DSP563XX_REG_IDX_R2, DSP563XX_REG_IDX_R3, DSP563XX_REG_IDX_R4, DSP563XX_REG_IDX_R5,
318 DSP563XX_REG_IDX_R6, DSP563XX_REG_IDX_R7, DSP563XX_REG_IDX_N0, DSP563XX_REG_IDX_N1,
319 DSP563XX_REG_IDX_N2, DSP563XX_REG_IDX_N3, DSP563XX_REG_IDX_N4, DSP563XX_REG_IDX_N5,
320 DSP563XX_REG_IDX_N6, DSP563XX_REG_IDX_N7, DSP563XX_REG_IDX_M0, DSP563XX_REG_IDX_M1,
321 DSP563XX_REG_IDX_M2, DSP563XX_REG_IDX_M3, DSP563XX_REG_IDX_M4, DSP563XX_REG_IDX_M5,
322 DSP563XX_REG_IDX_M6, DSP563XX_REG_IDX_M7,
325 static int dsp563xx_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size)
327 int i;
328 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
330 if (target->state != TARGET_HALTED)
332 return ERROR_TARGET_NOT_HALTED;
335 *reg_list_size = DSP563XX_NUMCOREREGS;
336 *reg_list = malloc(sizeof(struct reg *) * (*reg_list_size));
338 if (!*reg_list)
339 return ERROR_INVALID_ARGUMENTS;
341 for (i = 0; i < DSP563XX_NUMCOREREGS; i++)
343 (*reg_list)[i] = &dsp563xx->core_cache->reg_list[gdb_reg_list_idx[i]];
346 return ERROR_OK;
350 static int dsp563xx_read_core_reg(struct target *target, int num)
352 uint32_t reg_value;
353 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
355 if ((num < 0) || (num >= DSP563XX_NUMCOREREGS))
356 return ERROR_INVALID_ARGUMENTS;
358 reg_value = dsp563xx->core_regs[num];
359 buf_set_u32(dsp563xx->core_cache->reg_list[num].value, 0, 32, reg_value);
360 dsp563xx->core_cache->reg_list[num].valid = 1;
361 dsp563xx->core_cache->reg_list[num].dirty = 0;
363 return ERROR_OK;
366 static int dsp563xx_write_core_reg(struct target *target, int num)
368 uint32_t reg_value;
369 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
371 if ((num < 0) || (num >= DSP563XX_NUMCOREREGS))
372 return ERROR_INVALID_ARGUMENTS;
374 reg_value = buf_get_u32(dsp563xx->core_cache->reg_list[num].value, 0, 32);
375 dsp563xx->core_regs[num] = reg_value;
376 dsp563xx->core_cache->reg_list[num].valid = 1;
377 dsp563xx->core_cache->reg_list[num].dirty = 0;
379 return ERROR_OK;
382 static int dsp563xx_get_core_reg(struct reg *reg)
384 struct dsp563xx_core_reg *dsp563xx_reg = reg->arch_info;
385 struct target *target = dsp563xx_reg->target;
386 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
388 LOG_DEBUG("%s", __FUNCTION__);
390 if (target->state != TARGET_HALTED)
392 return ERROR_TARGET_NOT_HALTED;
395 return dsp563xx->read_core_reg(target, dsp563xx_reg->num);
398 static int dsp563xx_set_core_reg(struct reg *reg, uint8_t * buf)
400 LOG_DEBUG("%s", __FUNCTION__);
402 struct dsp563xx_core_reg *dsp563xx_reg = reg->arch_info;
403 struct target *target = dsp563xx_reg->target;
404 uint32_t value = buf_get_u32(buf, 0, 32);
406 if (target->state != TARGET_HALTED)
408 return ERROR_TARGET_NOT_HALTED;
411 buf_set_u32(reg->value, 0, reg->size, value);
412 reg->dirty = 1;
413 reg->valid = 1;
415 return ERROR_OK;
418 static const struct reg_arch_type dsp563xx_reg_type = {
419 .get = dsp563xx_get_core_reg,
420 .set = dsp563xx_set_core_reg,
423 static void dsp563xx_build_reg_cache(struct target *target)
425 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
427 struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
428 struct reg_cache *cache = malloc(sizeof(struct reg_cache));
429 struct reg *reg_list = malloc(sizeof(struct reg) * DSP563XX_NUMCOREREGS);
430 struct dsp563xx_core_reg *arch_info = malloc(sizeof(struct dsp563xx_core_reg) * DSP563XX_NUMCOREREGS);
431 int i;
433 /* Build the process context cache */
434 cache->name = "dsp563xx registers";
435 cache->next = NULL;
436 cache->reg_list = reg_list;
437 cache->num_regs = DSP563XX_NUMCOREREGS;
438 (*cache_p) = cache;
439 dsp563xx->core_cache = cache;
441 for (i = 0; i < DSP563XX_NUMCOREREGS; i++)
443 arch_info[i].num = dsp563xx_regs[i].id;
444 arch_info[i].name = dsp563xx_regs[i].name;
445 arch_info[i].size = dsp563xx_regs[i].bits;
446 arch_info[i].eame = dsp563xx_regs[i].eame;
447 arch_info[i].instr_mask = dsp563xx_regs[i].instr_mask;
448 arch_info[i].target = target;
449 arch_info[i].dsp563xx_common = dsp563xx;
450 reg_list[i].name = dsp563xx_regs[i].name;
451 reg_list[i].size = 32; //dsp563xx_regs[i].bits;
452 reg_list[i].value = calloc(1, 4);
453 reg_list[i].dirty = 0;
454 reg_list[i].valid = 0;
455 reg_list[i].type = &dsp563xx_reg_type;
456 reg_list[i].arch_info = &arch_info[i];
460 static int dsp563xx_read_register(struct target *target, int num, int force);
461 static int dsp563xx_write_register(struct target *target, int num, int force);
463 static int dsp563xx_reg_read_high_io(struct target *target, uint32_t instr_mask, uint32_t * data)
465 int err;
466 uint32_t instr;
467 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
469 /* we use r0 to store temporary data */
470 if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].valid)
471 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R0);
473 /* move source memory to r0 */
474 instr = INSTR_MOVEP_REG_HIO(MEM_X, 0, EAME_R0, instr_mask);
475 if ((err = dsp563xx_once_execute_sw_ir(target->tap, 0, instr)) != ERROR_OK)
476 return err;
477 /* move r0 to debug register */
478 instr = INSTR_MOVEP_REG_HIO(MEM_X, 1, EAME_R0, 0xfffffc);
479 if ((err = dsp563xx_once_execute_sw_ir(target->tap, 1, instr)) != ERROR_OK)
480 return err;
481 /* read debug register */
482 if ((err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OGDBR, data)) != ERROR_OK)
483 return err;
484 /* r0 is no longer valid on target */
485 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].dirty = 1;
487 return ERROR_OK;
490 static int dsp563xx_reg_write_high_io(struct target *target, uint32_t instr_mask, uint32_t data)
492 int err;
493 uint32_t instr;
494 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
496 /* we use r0 to store temporary data */
497 if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].valid)
498 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R0);
500 /* move data to r0 */
501 if ((err = dsp563xx_once_execute_dw_ir(target->tap, 0, 0x60F400, data)) != ERROR_OK)
502 return err;
503 /* move r0 to destination memory */
504 instr = INSTR_MOVEP_REG_HIO(MEM_X, 1, EAME_R0, instr_mask);
505 if ((err = dsp563xx_once_execute_sw_ir(target->tap, 1, instr)) != ERROR_OK)
506 return err;
508 /* r0 is no longer valid on target */
509 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].dirty = 1;
511 return ERROR_OK;
514 static int dsp563xx_reg_read(struct target *target, uint32_t eame, uint32_t * data)
516 int err;
517 uint32_t instr;
519 instr = INSTR_MOVEP_REG_HIO(MEM_X, 1, eame, 0xfffffc);
520 if ((err = dsp563xx_once_execute_sw_ir(target->tap, 0, instr)) != ERROR_OK)
521 return err;
522 /* nop */
523 if ((err = dsp563xx_once_execute_sw_ir(target->tap, 1, 0x000000)) != ERROR_OK)
524 return err;
525 /* read debug register */
526 return dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OGDBR, data);
529 static int dsp563xx_reg_write(struct target *target, uint32_t instr_mask, uint32_t data)
531 int err;
533 if ((err = dsp563xx_once_execute_dw_ir(target->tap, 0, instr_mask, data)) != ERROR_OK)
534 return err;
535 /* nop */
536 return dsp563xx_once_execute_sw_ir(target->tap, 1, 0x000000);
539 static int dsp563xx_reg_pc_read(struct target *target)
541 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
543 /* pc was changed, nothing todo */
544 if (dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_PC].dirty)
545 return ERROR_OK;
547 /* conditional branch check */
548 if ( once_regs[ONCE_REG_IDX_OPABDR].reg == once_regs[ONCE_REG_IDX_OPABEX].reg )
550 if ( (once_regs[ONCE_REG_IDX_OPABF11].reg & 1) == 0 )
552 LOG_DEBUG("%s conditional branch not supported yet (0x%x 0x%x 0x%x)", __FUNCTION__,
553 (once_regs[ONCE_REG_IDX_OPABF11].reg >> 1),
554 once_regs[ONCE_REG_IDX_OPABDR].reg,
555 once_regs[ONCE_REG_IDX_OPABEX].reg);
557 /* TODO: use disassembly to set correct pc offset
558 * read 2 words from OPABF11 and disasm the instruction
560 dsp563xx->core_regs[DSP563XX_REG_IDX_PC] = (once_regs[ONCE_REG_IDX_OPABF11].reg >> 1) & 0x00FFFFFF;
562 else
564 if ( once_regs[ONCE_REG_IDX_OPABEX].reg == once_regs[ONCE_REG_IDX_OPABFR].reg )
566 dsp563xx->core_regs[DSP563XX_REG_IDX_PC] = once_regs[ONCE_REG_IDX_OPABEX].reg;
568 else
570 dsp563xx->core_regs[DSP563XX_REG_IDX_PC] = once_regs[ONCE_REG_IDX_OPABEX].reg - 1;
574 else
576 dsp563xx->core_regs[DSP563XX_REG_IDX_PC] = once_regs[ONCE_REG_IDX_OPABEX].reg;
579 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_PC);
581 return ERROR_OK;
584 static int dsp563xx_reg_ssh_read(struct target *target)
586 int err;
587 uint32_t sp;
588 struct dsp563xx_core_reg *arch_info;
589 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
591 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSH].arch_info;
593 /* get a valid stack pointer */
594 if ((err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SP, 0)) != ERROR_OK)
595 return err;
596 sp = dsp563xx->core_regs[DSP563XX_REG_IDX_SP];
597 if ((err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SP, 0)) != ERROR_OK)
598 return err;
600 /* get a valid stack count */
601 if ((err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SC, 0)) != ERROR_OK)
602 return err;
604 if ((err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SC, 0)) != ERROR_OK)
605 return err;
607 /* get a valid extended pointer */
608 if ((err = dsp563xx_read_register(target, DSP563XX_REG_IDX_EP, 0)) != ERROR_OK)
609 return err;
611 if ((err = dsp563xx_write_register(target, DSP563XX_REG_IDX_EP, 0)) != ERROR_OK)
612 return err;
614 if (!sp)
616 sp = 0x00FFFFFF;
618 else
620 if ((err = dsp563xx_reg_read(target, arch_info->eame, &sp)) != ERROR_OK)
621 return err;
623 if ((err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SC, 1)) != ERROR_OK)
624 return err;
625 if ((err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SP, 1)) != ERROR_OK)
626 return err;
627 if ((err = dsp563xx_write_register(target, DSP563XX_REG_IDX_EP, 1)) != ERROR_OK)
628 return err;
631 dsp563xx->core_regs[DSP563XX_REG_IDX_SSH] = sp;
632 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_SSH);
634 return ERROR_OK;
637 static int dsp563xx_reg_ssh_write(struct target *target)
639 int err;
640 uint32_t sp;
641 struct dsp563xx_core_reg *arch_info;
642 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
644 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSH].arch_info;
646 /* get a valid stack pointer */
647 if ((err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SP, 0)) != ERROR_OK)
648 return err;
649 sp = dsp563xx->core_regs[DSP563XX_REG_IDX_SP];
651 if (sp)
653 sp--;
654 /* write new stackpointer */
655 dsp563xx->core_regs[DSP563XX_REG_IDX_SP] = sp;
656 if ((err = dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_SP)) != ERROR_OK)
657 return err;
658 if ((err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SP, 1)) != ERROR_OK)
659 return err;
661 if ((err = dsp563xx_reg_write(target, arch_info->instr_mask, dsp563xx->core_regs[DSP563XX_REG_IDX_SSH])) != ERROR_OK)
662 return err;
664 if ((err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SP, 1)) != ERROR_OK)
665 return err;
666 if ((err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SSH, 1)) != ERROR_OK)
667 return err;
670 return ERROR_OK;
673 static int dsp563xx_reg_ssl_read(struct target *target)
675 int err;
676 uint32_t sp;
677 struct dsp563xx_core_reg *arch_info;
678 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
680 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSL].arch_info;
682 /* get a valid stack pointer */
683 if ((err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SP, 0)) != ERROR_OK)
684 return err;
685 sp = dsp563xx->core_regs[DSP563XX_REG_IDX_SP];
687 if (!sp)
689 sp = 0x00FFFFFF;
691 else
693 if ((err = dsp563xx_reg_read(target, arch_info->eame, &sp)) != ERROR_OK)
694 return err;
697 dsp563xx->core_regs[DSP563XX_REG_IDX_SSL] = sp;
698 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_SSL);
700 return ERROR_OK;
703 static int dsp563xx_read_register(struct target *target, int num, int force)
705 int err = ERROR_OK;
706 uint32_t data = 0;
707 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
708 struct dsp563xx_core_reg *arch_info;
710 if (force)
711 dsp563xx->core_cache->reg_list[num].valid = 0;
713 if (!dsp563xx->core_cache->reg_list[num].valid)
715 arch_info = dsp563xx->core_cache->reg_list[num].arch_info;
717 switch (arch_info->num)
719 case DSP563XX_REG_IDX_SSH:
720 err = dsp563xx_reg_ssh_read(target);
721 break;
722 case DSP563XX_REG_IDX_SSL:
723 err = dsp563xx_reg_ssl_read(target);
724 break;
725 case DSP563XX_REG_IDX_PC:
726 err = dsp563xx_reg_pc_read(target);
727 break;
728 case DSP563XX_REG_IDX_IPRC:
729 case DSP563XX_REG_IDX_IPRP:
730 case DSP563XX_REG_IDX_BCR:
731 case DSP563XX_REG_IDX_DCR:
732 case DSP563XX_REG_IDX_AAR0:
733 case DSP563XX_REG_IDX_AAR1:
734 case DSP563XX_REG_IDX_AAR2:
735 case DSP563XX_REG_IDX_AAR3:
736 err = dsp563xx_reg_read_high_io(target, arch_info->instr_mask, &data);
737 if (err == ERROR_OK)
739 dsp563xx->core_regs[num] = data;
740 dsp563xx->read_core_reg(target, num);
742 break;
743 default:
744 err = dsp563xx_reg_read(target, arch_info->eame, &data);
745 if (err == ERROR_OK)
747 dsp563xx->core_regs[num] = data;
748 dsp563xx->read_core_reg(target, num);
750 break;
755 return err;
758 static int dsp563xx_write_register(struct target *target, int num, int force)
760 int err = ERROR_OK;
761 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
762 struct dsp563xx_core_reg *arch_info;
764 if (force)
765 dsp563xx->core_cache->reg_list[num].dirty = 1;
767 if (dsp563xx->core_cache->reg_list[num].dirty)
769 arch_info = dsp563xx->core_cache->reg_list[num].arch_info;
771 dsp563xx->write_core_reg(target, num);
773 switch (arch_info->num)
775 case DSP563XX_REG_IDX_SSH:
776 err = dsp563xx_reg_ssh_write(target);
777 break;
778 case DSP563XX_REG_IDX_PC:
779 /* pc is updated on resume, no need to write it here */
780 break;
781 case DSP563XX_REG_IDX_IPRC:
782 case DSP563XX_REG_IDX_IPRP:
783 case DSP563XX_REG_IDX_BCR:
784 case DSP563XX_REG_IDX_DCR:
785 case DSP563XX_REG_IDX_AAR0:
786 case DSP563XX_REG_IDX_AAR1:
787 case DSP563XX_REG_IDX_AAR2:
788 case DSP563XX_REG_IDX_AAR3:
789 err = dsp563xx_reg_write_high_io(target, arch_info->instr_mask, dsp563xx->core_regs[num]);
790 break;
791 default:
792 err = dsp563xx_reg_write(target, arch_info->instr_mask, dsp563xx->core_regs[num]);
794 if ((err == ERROR_OK) && (arch_info->num == DSP563XX_REG_IDX_SP))
796 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSH].valid = 0;
797 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSL].valid = 0;
800 break;
804 return err;
807 static int dsp563xx_save_context(struct target *target)
809 int i, err = ERROR_OK;
811 for (i = 0; i < DSP563XX_NUMCOREREGS; i++)
813 if ((err = dsp563xx_read_register(target, i, 0)) != ERROR_OK)
814 break;
817 return err;
820 static int dsp563xx_restore_context(struct target *target)
822 int i, err = ERROR_OK;
824 for (i = 0; i < DSP563XX_NUMCOREREGS; i++)
826 if ((err = dsp563xx_write_register(target, i, 0)) != ERROR_OK)
827 break;
830 return err;
833 static void dsp563xx_invalidate_x_context(struct target *target, uint32_t addr_start, uint32_t addr_end )
835 int i;
836 struct dsp563xx_core_reg *arch_info;
837 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
839 if ( addr_start > ASM_REG_W_IPRC )
840 return;
841 if ( addr_start < ASM_REG_W_AAR3 )
842 return;
844 for (i = DSP563XX_REG_IDX_IPRC; i < DSP563XX_NUMCOREREGS; i++)
846 arch_info = dsp563xx->core_cache->reg_list[i].arch_info;
848 if ( (arch_info->instr_mask >= addr_start) &&
849 (arch_info->instr_mask <= addr_end))
851 dsp563xx->core_cache->reg_list[i].valid = 0;
852 dsp563xx->core_cache->reg_list[i].dirty = 0;
857 static int dsp563xx_target_create(struct target *target, Jim_Interp * interp)
859 struct dsp563xx_common *dsp563xx = calloc(1, sizeof(struct dsp563xx_common));
861 if (!dsp563xx)
862 return ERROR_INVALID_ARGUMENTS;
864 dsp563xx->jtag_info.tap = target->tap;
865 target->arch_info = dsp563xx;
866 dsp563xx->read_core_reg = dsp563xx_read_core_reg;
867 dsp563xx->write_core_reg = dsp563xx_write_core_reg;
869 return ERROR_OK;
872 static int dsp563xx_init_target(struct command_context *cmd_ctx, struct target *target)
874 LOG_DEBUG("%s", __FUNCTION__);
876 dsp563xx_build_reg_cache(target);
878 return ERROR_OK;
881 static int dsp563xx_examine(struct target *target)
883 uint32_t chip;
885 if (target->tap->hasidcode == false)
887 LOG_ERROR("no IDCODE present on device");
889 return ERROR_INVALID_ARGUMENTS;
892 if (!target_was_examined(target))
894 target_set_examined(target);
896 /* examine core and chip derivate number */
897 chip = (target->tap->idcode>>12)&0x3ff;
898 /* core number 0 means DSP563XX */
899 if ( ((chip>>5)&0x1f) == 0 )
900 chip += 300;
902 LOG_INFO("DSP56%03d device found",chip);
905 return ERROR_OK;
908 static int dsp563xx_arch_state(struct target *target)
910 LOG_DEBUG("%s", __FUNCTION__);
911 return ERROR_OK;
914 #define DSP563XX_SR_SA (1<<17)
915 #define DSP563XX_SR_SC (1<<13)
917 static int dsp563xx_debug_once_init(struct target *target)
919 return dsp563xx_once_read_register(target->tap, 1, once_regs, DSP563XX_NUMONCEREGS);
922 static int dsp563xx_debug_init(struct target *target)
924 int err;
925 uint32_t sr;
926 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
927 struct dsp563xx_core_reg *arch_info;
929 if ((err = dsp563xx_debug_once_init(target)) != ERROR_OK)
930 return err;
932 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SR].arch_info;
934 /* check 24bit mode */
935 if ((err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SR, 0)) != ERROR_OK)
936 return err;
938 sr = dsp563xx->core_regs[DSP563XX_REG_IDX_SR];
940 if (sr & (DSP563XX_SR_SA | DSP563XX_SR_SC))
942 sr &= ~(DSP563XX_SR_SA | DSP563XX_SR_SC);
944 if ((err = dsp563xx_once_execute_dw_ir(target->tap, 1, arch_info->instr_mask, sr)) != ERROR_OK)
945 return err;
946 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SR].dirty = 1;
949 if ((err = dsp563xx_read_register(target, DSP563XX_REG_IDX_N0, 0)) != ERROR_OK)
950 return err;
951 if ((err = dsp563xx_read_register(target, DSP563XX_REG_IDX_N1, 0)) != ERROR_OK)
952 return err;
953 if ((err = dsp563xx_read_register(target, DSP563XX_REG_IDX_M0, 0)) != ERROR_OK)
954 return err;
955 if ((err = dsp563xx_read_register(target, DSP563XX_REG_IDX_M1, 0)) != ERROR_OK)
956 return err;
958 if (dsp563xx->core_regs[DSP563XX_REG_IDX_N0] != 0x000000)
960 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_N0].arch_info;
961 if ((err = dsp563xx_reg_write(target, arch_info->instr_mask, 0x000000)) != ERROR_OK)
962 return err;
964 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_N0].dirty = 1;
966 if (dsp563xx->core_regs[DSP563XX_REG_IDX_N1] != 0x000000)
968 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_N1].arch_info;
969 if ((err = dsp563xx_reg_write(target, arch_info->instr_mask, 0x000000)) != ERROR_OK)
970 return err;
972 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_N1].dirty = 1;
974 if (dsp563xx->core_regs[DSP563XX_REG_IDX_M0] != 0xffffff)
976 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_M0].arch_info;
977 if ((err = dsp563xx_reg_write(target, arch_info->instr_mask, 0xffffff)) != ERROR_OK)
978 return err;
980 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_M0].dirty = 1;
982 if (dsp563xx->core_regs[DSP563XX_REG_IDX_M1] != 0xffffff)
984 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_M1].arch_info;
985 if ((err = dsp563xx_reg_write(target, arch_info->instr_mask, 0xffffff)) != ERROR_OK)
986 return err;
988 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_M1].dirty = 1;
990 if ((err = dsp563xx_save_context(target)) != ERROR_OK)
991 return err;
993 return ERROR_OK;
996 static int dsp563xx_jtag_debug_request(struct target *target)
998 return dsp563xx_once_request_debug(target->tap, target->state == TARGET_RESET);
1001 static int dsp563xx_poll(struct target *target)
1003 int err;
1004 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1005 uint32_t once_status=0;
1006 int state;
1008 state = dsp563xx_once_target_status(target->tap);
1010 if (state == TARGET_UNKNOWN)
1012 target->state = state;
1013 LOG_ERROR("jtag status contains invalid mode value - communication failure");
1014 return ERROR_TARGET_FAILURE;
1017 if ((err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OSCR, &once_status)) != ERROR_OK)
1018 return err;
1020 if ((once_status & DSP563XX_ONCE_OSCR_DEBUG_M) == DSP563XX_ONCE_OSCR_DEBUG_M)
1022 if (target->state != TARGET_HALTED)
1024 target->state = TARGET_HALTED;
1026 if ((err = dsp563xx_debug_init(target)) != ERROR_OK)
1027 return err;
1029 if ( once_status & (DSP563XX_ONCE_OSCR_MBO|DSP563XX_ONCE_OSCR_SWO) )
1031 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
1033 else
1035 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
1038 LOG_DEBUG("target->state: %s (%x)", target_state_name(target),once_status);
1040 LOG_INFO("halted: PC: 0x%x", dsp563xx->core_regs[DSP563XX_REG_IDX_PC] );
1044 return ERROR_OK;
1047 static int dsp563xx_halt(struct target *target)
1049 int err;
1051 LOG_DEBUG("%s", __FUNCTION__);
1053 if (target->state == TARGET_HALTED)
1055 LOG_DEBUG("target was already halted");
1056 return ERROR_OK;
1059 if (target->state == TARGET_UNKNOWN)
1061 LOG_WARNING("target was in unknown state when halt was requested");
1064 if ((err = dsp563xx_jtag_debug_request(target)) != ERROR_OK)
1065 return err;
1067 target->debug_reason = DBG_REASON_DBGRQ;
1069 return ERROR_OK;
1072 static int dsp563xx_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
1074 int err;
1075 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1077 /* check if pc was changed and resume want to execute the next address
1078 * if pc was changed from gdb or other interface we will
1079 * jump to this address and don't execute the next address
1080 * this will not affect the resume command with an address argument
1081 * because current is set to zero then
1083 if ( current && dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_PC].dirty )
1085 dsp563xx_write_core_reg(target,DSP563XX_REG_IDX_PC);
1086 address = dsp563xx->core_regs[DSP563XX_REG_IDX_PC];
1087 current = 0;
1090 LOG_DEBUG("%s %08X %08X", __FUNCTION__, current, (unsigned) address);
1092 if ((err = dsp563xx_restore_context(target)) != ERROR_OK)
1093 return err;
1094 register_cache_invalidate(dsp563xx->core_cache);
1096 if (current)
1098 /* restore pipeline registers and go */
1099 if ((err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR, once_regs[ONCE_REG_IDX_OPILR].reg)) != ERROR_OK)
1100 return err;
1101 if ((err =
1102 dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR | DSP563XX_ONCE_OCR_EX | DSP563XX_ONCE_OCR_GO,
1103 once_regs[ONCE_REG_IDX_OPDBR].reg)) != ERROR_OK)
1104 return err;
1106 else
1108 /* set to go register and jump */
1109 if ((err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR, INSTR_JUMP)) != ERROR_OK)
1110 return err;
1111 if ((err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_PDBGOTO | DSP563XX_ONCE_OCR_EX | DSP563XX_ONCE_OCR_GO, address)) != ERROR_OK)
1112 return err;
1115 target->state = TARGET_RUNNING;
1117 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
1119 return ERROR_OK;
1122 static int dsp563xx_step_ex(struct target *target, int current, uint32_t address, int handle_breakpoints, int steps)
1124 int err;
1125 uint32_t once_status;
1126 uint32_t dr_in, cnt;
1127 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1129 if (target->state != TARGET_HALTED)
1131 LOG_DEBUG("target was not halted");
1132 return ERROR_OK;
1135 /* check if pc was changed and step want to execute the next address
1136 * if pc was changed from gdb or other interface we will
1137 * jump to this address and don't execute the next address
1138 * this will not affect the step command with an address argument
1139 * because current is set to zero then
1141 if ( current && dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_PC].dirty )
1143 dsp563xx_write_core_reg(target,DSP563XX_REG_IDX_PC);
1144 address = dsp563xx->core_regs[DSP563XX_REG_IDX_PC];
1145 current = 0;
1148 LOG_DEBUG("%s %08X %08X", __FUNCTION__, current, (unsigned) address);
1150 if ((err = dsp563xx_jtag_debug_request(target)) != ERROR_OK)
1151 return err;
1152 if ((err = dsp563xx_restore_context(target)) != ERROR_OK)
1153 return err;
1155 /* reset trace mode */
1156 if ((err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OSCR, 0x000000)) != ERROR_OK)
1157 return err;
1158 /* enable trace mode */
1159 if ((err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OSCR, DSP563XX_ONCE_OSCR_TME)) != ERROR_OK)
1160 return err;
1162 cnt = steps;
1164 /* on JUMP we need one extra cycle */
1165 if (!current)
1166 cnt++;
1168 /* load step counter with N-1 */
1169 if ((err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OTC, cnt)) != ERROR_OK)
1170 return err;
1172 if (current)
1174 /* restore pipeline registers and go */
1175 if ((err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR, once_regs[ONCE_REG_IDX_OPILR].reg)) != ERROR_OK)
1176 return err;
1177 if ((err =
1178 dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR | DSP563XX_ONCE_OCR_EX | DSP563XX_ONCE_OCR_GO,
1179 once_regs[ONCE_REG_IDX_OPDBR].reg)) != ERROR_OK)
1180 return err;
1182 else
1184 /* set to go register and jump */
1185 if ((err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR, INSTR_JUMP)) != ERROR_OK)
1186 return err;
1187 if ((err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_PDBGOTO | DSP563XX_ONCE_OCR_EX | DSP563XX_ONCE_OCR_GO, address)) != ERROR_OK)
1188 return err;
1191 while (1)
1193 if ((err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OSCR, &once_status)) != ERROR_OK)
1194 return err;
1196 if (once_status & DSP563XX_ONCE_OSCR_TO)
1198 if ((err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OPABFR, &dr_in)) != ERROR_OK)
1199 return err;
1200 LOG_DEBUG("fetch: %08X", (unsigned) dr_in&0x00ffffff);
1201 if ((err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OPABDR, &dr_in)) != ERROR_OK)
1202 return err;
1203 LOG_DEBUG("decode: %08X", (unsigned) dr_in&0x00ffffff);
1204 if ((err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OPABEX, &dr_in)) != ERROR_OK)
1205 return err;
1206 LOG_DEBUG("execute: %08X", (unsigned) dr_in&0x00ffffff);
1208 /* reset trace mode */
1209 if ((err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OSCR, 0x000000)) != ERROR_OK)
1210 return err;
1212 register_cache_invalidate(dsp563xx->core_cache);
1213 if ((err = dsp563xx_debug_init(target)) != ERROR_OK)
1214 return err;
1216 break;
1220 return ERROR_OK;
1223 static int dsp563xx_step(struct target *target, int current, uint32_t address, int handle_breakpoints)
1225 int err;
1226 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1228 if (target->state != TARGET_HALTED)
1230 LOG_WARNING("target not halted");
1231 return ERROR_TARGET_NOT_HALTED;
1234 if ( (err=dsp563xx_step_ex(target, current, address, handle_breakpoints, 0)) != ERROR_OK )
1236 return err;
1239 target->debug_reason = DBG_REASON_SINGLESTEP;
1240 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
1242 LOG_INFO("halted: PC: 0x%x", dsp563xx->core_regs[DSP563XX_REG_IDX_PC] );
1244 return err;
1247 static int dsp563xx_assert_reset(struct target *target)
1249 int retval = 0;
1250 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1251 enum reset_types jtag_reset_config = jtag_get_reset_config();
1253 if (jtag_reset_config & RESET_HAS_SRST)
1255 /* default to asserting srst */
1256 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
1258 jtag_add_reset(1, 1);
1260 else
1262 jtag_add_reset(0, 1);
1266 target->state = TARGET_RESET;
1267 jtag_add_sleep(5000);
1269 /* registers are now invalid */
1270 register_cache_invalidate(dsp563xx->core_cache);
1272 if (target->reset_halt)
1274 if ((retval = target_halt(target)) != ERROR_OK)
1275 return retval;
1278 LOG_DEBUG("%s", __FUNCTION__);
1279 return ERROR_OK;
1282 static int dsp563xx_deassert_reset(struct target *target)
1284 int err;
1286 /* deassert reset lines */
1287 jtag_add_reset(0, 0);
1289 if ((err = dsp563xx_poll(target)) != ERROR_OK)
1290 return err;
1292 if (target->reset_halt)
1294 if (target->state == TARGET_HALTED)
1296 /* after a reset the cpu jmp to the
1297 * reset vector and need 2 cycles to fill
1298 * the cache (fetch,decode,excecute)
1300 if ((err = dsp563xx_step_ex(target, 1, 0, 1, 1)) != ERROR_OK)
1301 return err;
1304 else
1306 target->state = TARGET_RUNNING;
1309 LOG_DEBUG("%s", __FUNCTION__);
1310 return ERROR_OK;
1313 static int dsp563xx_soft_reset_halt(struct target *target)
1315 LOG_DEBUG("%s", __FUNCTION__);
1316 return ERROR_OK;
1319 static int dsp563xx_run_algorithm(struct target *target,
1320 int num_mem_params, struct mem_param *mem_params,
1321 int num_reg_params, struct reg_param *reg_params,
1322 uint32_t entry_point, uint32_t exit_point,
1323 int timeout_ms, void *arch_info)
1325 int i;
1326 int retvaltemp,retval = 0;
1327 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1329 if (target->state != TARGET_HALTED)
1331 LOG_WARNING("target not halted");
1332 return ERROR_TARGET_NOT_HALTED;
1335 for (i = 0; i < num_mem_params; i++)
1337 if ((retval = target_write_buffer(target, mem_params[i].address, mem_params[i].size, mem_params[i].value)) != ERROR_OK)
1339 return retval;
1343 for (i = 0; i < num_reg_params; i++)
1345 struct reg *reg = register_get_by_name(dsp563xx->core_cache, reg_params[i].reg_name, 0);
1347 if (!reg)
1349 LOG_ERROR("BUG: register '%s' not found", reg_params[i].reg_name);
1350 continue;
1353 if (reg->size != reg_params[i].size)
1355 LOG_ERROR("BUG: register '%s' size doesn't match reg_params[i].size", reg_params[i].reg_name);
1356 continue;
1359 if ((retval = dsp563xx_set_core_reg(reg, reg_params[i].value)) != ERROR_OK)
1361 return retval;
1365 /* exec */
1366 if ((retval = target_resume(target, 0, entry_point, 1, 1)) != ERROR_OK)
1368 return retval;
1371 if ((retval = target_wait_state(target, TARGET_HALTED, timeout_ms)) != ERROR_OK)
1373 return retval;
1376 for (i = 0; i < num_mem_params; i++)
1378 if (mem_params[i].direction != PARAM_OUT)
1379 if ((retvaltemp = target_read_buffer(target, mem_params[i].address, mem_params[i].size, mem_params[i].value)) != ERROR_OK)
1381 retval = retvaltemp;
1385 for (i = 0; i < num_reg_params; i++)
1387 if (reg_params[i].direction != PARAM_OUT)
1390 struct reg *reg = register_get_by_name(dsp563xx->core_cache, reg_params[i].reg_name, 0);
1391 if (!reg)
1393 LOG_ERROR("BUG: register '%s' not found", reg_params[i].reg_name);
1394 continue;
1397 if (reg->size != reg_params[i].size)
1399 LOG_ERROR("BUG: register '%s' size doesn't match reg_params[i].size", reg_params[i].reg_name);
1400 continue;
1403 buf_set_u32(reg_params[i].value, 0, 32, buf_get_u32(reg->value, 0, 32));
1407 return ERROR_OK;
1410 /* global command context from openocd.c */
1411 extern struct command_context *global_cmd_ctx;
1413 static int dsp563xx_get_default_memory(void)
1415 Jim_Interp *interp;
1416 Jim_Obj * memspace;
1417 char * c;
1419 if ( !global_cmd_ctx )
1420 return MEM_P;
1422 interp = global_cmd_ctx->interp;
1424 if ( !interp )
1425 return MEM_P;
1427 memspace = Jim_GetGlobalVariableStr(interp,"memspace", JIM_NONE);
1429 if ( !memspace )
1430 return MEM_P;
1432 c = (char*)Jim_GetString(memspace,NULL);
1434 if ( !c )
1435 return MEM_P;
1437 switch(c[0])
1439 case '1':
1440 return MEM_X;
1441 case '2':
1442 return MEM_Y;
1443 case '3':
1444 return MEM_L;
1445 default:
1446 break;
1449 return MEM_P;
1452 static int dsp563xx_read_memory_core(struct target *target, int mem_type, uint32_t address, uint32_t size, uint32_t count, uint8_t * buffer)
1454 int err;
1455 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1456 uint32_t i, x;
1457 uint32_t data, move_cmd = 0;
1458 uint8_t *b;
1460 LOG_DEBUG("memtype: %d address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", mem_type,address, size, count);
1462 if (target->state != TARGET_HALTED)
1464 LOG_WARNING("target not halted");
1465 return ERROR_TARGET_NOT_HALTED;
1468 switch (mem_type)
1470 case MEM_X:
1471 /* TODO: mark effected queued registers */
1472 move_cmd = 0x61d800;
1473 break;
1474 case MEM_Y:
1475 move_cmd = 0x69d800;
1476 break;
1477 case MEM_P:
1478 move_cmd = 0x07d891;
1479 break;
1480 default:
1481 return ERROR_INVALID_ARGUMENTS;
1484 /* we use r0 to store temporary data */
1485 if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].valid)
1486 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R0);
1487 /* we use r1 to store temporary data */
1488 if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R1].valid)
1489 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R1);
1491 /* r0 is no longer valid on target */
1492 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].dirty = 1;
1493 /* r1 is no longer valid on target */
1494 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R1].dirty = 1;
1496 x = count;
1497 b = buffer;
1499 if ((err = dsp563xx_once_execute_dw_ir(target->tap, 1, 0x60F400, address)) != ERROR_OK)
1500 return err;
1502 for (i = 0; i < x; i++)
1504 if ((err = dsp563xx_once_execute_sw_ir(target->tap, 0, move_cmd)) != ERROR_OK)
1505 return err;
1506 if ((err = dsp563xx_once_execute_sw_ir(target->tap, 0, 0x08D13C)) != ERROR_OK)
1507 return err;
1508 if ((err = dsp563xx_once_reg_read(target->tap, 0, DSP563XX_ONCE_OGDBR, (uint32_t*)(void *)b)) != ERROR_OK)
1509 return err;
1510 b += 4;
1513 /* flush the jtag queue */
1514 if ((err = jtag_execute_queue()) != ERROR_OK)
1516 return err;
1519 /* walk over the buffer and fix target endianness */
1520 b = buffer;
1522 for (i = 0; i < x; i++)
1524 data = buf_get_u32(b, 0, 32) & 0x00FFFFFF;
1525 // LOG_DEBUG("R: %08X", *((uint32_t*)b));
1526 target_buffer_set_u32(target, b, data);
1527 b += 4;
1530 return ERROR_OK;
1533 static int dsp563xx_read_memory(struct target *target, int mem_type, uint32_t address, uint32_t size, uint32_t count, uint8_t * buffer)
1535 int err;
1536 uint32_t i,i1;
1537 uint8_t *buffer_y,*buffer_x;
1539 /* if size equals zero we are called from target read memory
1540 * and have to handle the parameter here */
1541 if ( (size == 0) && (count != 0) )
1543 size = count % 4;
1545 if ( size )
1547 LOG_DEBUG("size is not aligned to 4 byte");
1550 count = (count - size) / 4;
1551 size = 4;
1554 /* we only support 4 byte aligned data */
1555 if ( (size != 4) || (!count) )
1557 return ERROR_INVALID_ARGUMENTS;
1560 if ( mem_type != MEM_L )
1562 return dsp563xx_read_memory_core(target,mem_type,address,size,count,buffer);
1565 if ( !(buffer_y = malloc(size*count)) )
1567 return ERROR_INVALID_ARGUMENTS;
1570 if ( !(buffer_x = malloc(size*count)) )
1572 free(buffer_y);
1573 return ERROR_INVALID_ARGUMENTS;
1576 err = dsp563xx_read_memory_core(target,MEM_Y,address,size,count/2,buffer_y);
1578 if ( err != ERROR_OK )
1580 free(buffer_y);
1581 free(buffer_x);
1582 return err;
1585 err = dsp563xx_read_memory_core(target,MEM_X,address,size,count/2,buffer_x);
1587 if ( err != ERROR_OK )
1589 free(buffer_y);
1590 free(buffer_x);
1591 return err;
1594 for(i=0,i1=0;i<count;i+=2,i1++)
1596 buf_set_u32(buffer + i*sizeof(uint32_t), 0, 32, buf_get_u32(buffer_y+i1*sizeof(uint32_t), 0, 32));
1597 buf_set_u32(buffer + (i + 1) *sizeof(uint32_t), 0, 32, buf_get_u32(buffer_x+i1*sizeof(uint32_t), 0, 32));
1600 free(buffer_y);
1601 free(buffer_x);
1603 return ERROR_OK;
1606 static int dsp563xx_read_memory_default(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t * buffer)
1609 return dsp563xx_read_memory(target, dsp563xx_get_default_memory(), address, size, count, buffer);
1612 static int dsp563xx_read_buffer_default(struct target *target, uint32_t address, uint32_t size, uint8_t * buffer)
1615 return dsp563xx_read_memory(target, dsp563xx_get_default_memory(), address, size, 0, buffer);
1618 static int dsp563xx_write_memory_core(struct target *target, int mem_type, uint32_t address, uint32_t size, uint32_t count, const uint8_t * buffer)
1620 int err;
1621 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1622 uint32_t i, x;
1623 uint32_t data, move_cmd = 0;
1624 const uint8_t *b;
1626 LOG_DEBUG("memtype: %d address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", mem_type,address, size, count);
1628 if (target->state != TARGET_HALTED)
1630 LOG_WARNING("target not halted");
1631 return ERROR_TARGET_NOT_HALTED;
1634 switch (mem_type)
1636 case MEM_X:
1637 /* invalidate affected x registers */
1638 dsp563xx_invalidate_x_context(target,address,address+count-1);
1639 move_cmd = 0x615800;
1640 break;
1641 case MEM_Y:
1642 move_cmd = 0x695800;
1643 break;
1644 case MEM_P:
1645 move_cmd = 0x075891;
1646 break;
1647 default:
1648 return ERROR_INVALID_ARGUMENTS;
1651 /* we use r0 to store temporary data */
1652 if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].valid)
1653 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R0);
1654 /* we use r1 to store temporary data */
1655 if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R1].valid)
1656 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R1);
1658 /* r0 is no longer valid on target */
1659 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].dirty = 1;
1660 /* r1 is no longer valid on target */
1661 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R1].dirty = 1;
1663 x = count;
1664 b = buffer;
1666 if ((err = dsp563xx_once_execute_dw_ir(target->tap, 1, 0x60F400, address)) != ERROR_OK)
1667 return err;
1669 for (i = 0; i < x; i++)
1671 data = target_buffer_get_u32(target, b);
1673 // LOG_DEBUG("W: %08X", data);
1675 data &= 0x00ffffff;
1677 if ((err = dsp563xx_once_execute_dw_ir(target->tap, 0, 0x61F400, data)) != ERROR_OK)
1678 return err;
1679 if ((err = dsp563xx_once_execute_sw_ir(target->tap, 0, move_cmd)) != ERROR_OK)
1680 return err;
1681 b += 4;
1684 /* flush the jtag queue */
1685 if ((err = jtag_execute_queue()) != ERROR_OK)
1687 return err;
1690 return ERROR_OK;
1693 static int dsp563xx_write_memory(struct target *target, int mem_type, uint32_t address, uint32_t size, uint32_t count, const uint8_t * buffer)
1695 int err;
1696 uint32_t i,i1;
1697 uint8_t *buffer_y,*buffer_x;
1699 /* if size equals zero we are called from target write memory
1700 * and have to handle the parameter here */
1701 if ( (size == 0) && (count != 0) )
1703 size = count % 4;
1705 if ( size )
1707 LOG_DEBUG("size is not aligned to 4 byte");
1710 count = (count - size) / 4;
1711 size = 4;
1714 /* we only support 4 byte aligned data */
1715 if ( (size != 4) || (!count) )
1717 return ERROR_INVALID_ARGUMENTS;
1720 if ( mem_type != MEM_L )
1722 return dsp563xx_write_memory_core(target,mem_type,address,size,count,buffer);
1725 if ( !(buffer_y = malloc(size*count)) )
1727 return ERROR_INVALID_ARGUMENTS;
1730 if ( !(buffer_x = malloc(size*count)) )
1732 free(buffer_y);
1733 return ERROR_INVALID_ARGUMENTS;
1736 for(i=0,i1=0;i<count;i+=2,i1++)
1738 buf_set_u32(buffer_y + i1*sizeof(uint32_t), 0, 32, buf_get_u32(buffer+i*sizeof(uint32_t), 0, 32));
1739 buf_set_u32(buffer_x + i1*sizeof(uint32_t), 0, 32, buf_get_u32(buffer+(i+1)*sizeof(uint32_t), 0, 32));
1742 err = dsp563xx_write_memory_core(target,MEM_Y,address,size,count/2,buffer_y);
1744 if ( err != ERROR_OK )
1746 free(buffer_y);
1747 free(buffer_x);
1748 return err;
1751 err = dsp563xx_write_memory_core(target,MEM_X,address,size,count/2,buffer_x);
1753 if ( err != ERROR_OK )
1755 free(buffer_y);
1756 free(buffer_x);
1757 return err;
1760 free(buffer_y);
1761 free(buffer_x);
1763 return ERROR_OK;
1766 static int dsp563xx_write_memory_default(struct target *target, uint32_t address, uint32_t size, uint32_t count, const uint8_t * buffer)
1768 return dsp563xx_write_memory(target, dsp563xx_get_default_memory(), address, size, count, buffer);
1771 static int dsp563xx_write_buffer_default(struct target *target, uint32_t address, uint32_t size, const uint8_t * buffer)
1773 return dsp563xx_write_memory(target, dsp563xx_get_default_memory(), address, size, 0, buffer);
1776 static int dsp563xx_bulk_write_memory_default(struct target *target, uint32_t address, uint32_t count, const uint8_t *buffer)
1778 return dsp563xx_write_memory(target, dsp563xx_get_default_memory(), address, 4, count, buffer);
1781 static int dsp563xx_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
1783 return ERROR_OK;
1786 static int dsp563xx_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
1788 return ERROR_OK;
1791 static int dsp563xx_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
1793 return ERROR_OK;
1796 static int dsp563xx_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
1798 return ERROR_OK;
1801 static void handle_md_output(struct command_context *cmd_ctx, struct target *target, uint32_t address, unsigned size, unsigned count, const uint8_t * buffer)
1803 const unsigned line_bytecnt = 32;
1804 unsigned line_modulo = line_bytecnt / size;
1806 char output[line_bytecnt * 4 + 1];
1807 unsigned output_len = 0;
1809 const char *value_fmt;
1810 switch (size)
1812 case 4:
1813 value_fmt = "%8.8x ";
1814 break;
1815 case 2:
1816 value_fmt = "%4.4x ";
1817 break;
1818 case 1:
1819 value_fmt = "%2.2x ";
1820 break;
1821 default:
1822 /* "can't happen", caller checked */
1823 LOG_ERROR("invalid memory read size: %u", size);
1824 return;
1827 for (unsigned i = 0; i < count; i++)
1829 if (i % line_modulo == 0)
1831 output_len += snprintf(output + output_len, sizeof(output) - output_len, "0x%8.8x: ", (unsigned) (address + i));
1834 uint32_t value = 0;
1835 const uint8_t *value_ptr = buffer + i * size;
1836 switch (size)
1838 case 4:
1839 value = target_buffer_get_u32(target, value_ptr);
1840 break;
1841 case 2:
1842 value = target_buffer_get_u16(target, value_ptr);
1843 break;
1844 case 1:
1845 value = *value_ptr;
1847 output_len += snprintf(output + output_len, sizeof(output) - output_len, value_fmt, value);
1849 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
1851 command_print(cmd_ctx, "%s", output);
1852 output_len = 0;
1857 COMMAND_HANDLER(dsp563xx_mem_command)
1859 struct target *target = get_current_target(CMD_CTX);
1860 int err = ERROR_OK;
1861 int read_mem;
1862 uint32_t address = 0;
1863 uint32_t count = 1, i;
1864 uint32_t pattern = 0;
1865 uint32_t mem_type;
1866 uint8_t *buffer, *b;
1868 switch (CMD_NAME[1])
1870 case 'w':
1871 read_mem = 0;
1872 break;
1873 case 'd':
1874 read_mem = 1;
1875 break;
1876 default:
1877 return ERROR_COMMAND_SYNTAX_ERROR;
1880 switch (CMD_NAME[3])
1882 case 'x':
1883 mem_type = MEM_X;
1884 break;
1885 case 'y':
1886 mem_type = MEM_Y;
1887 break;
1888 case 'p':
1889 mem_type = MEM_P;
1890 break;
1891 default:
1892 return ERROR_COMMAND_SYNTAX_ERROR;
1895 if (CMD_ARGC > 0)
1897 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
1900 if (read_mem == 0)
1902 if (CMD_ARGC < 2)
1904 return ERROR_COMMAND_SYNTAX_ERROR;
1906 if (CMD_ARGC > 1)
1908 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern);
1910 if (CMD_ARGC > 2)
1912 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
1916 if (read_mem == 1)
1918 if (CMD_ARGC < 1)
1920 return ERROR_COMMAND_SYNTAX_ERROR;
1922 if (CMD_ARGC > 1)
1924 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], count);
1928 buffer = calloc(count, sizeof(uint32_t));
1930 if (read_mem == 1)
1932 if ((err = dsp563xx_read_memory(target, mem_type, address, sizeof(uint32_t), count, buffer)) == ERROR_OK)
1933 handle_md_output(CMD_CTX, target, address, sizeof(uint32_t), count, buffer);
1935 else
1937 b = buffer;
1939 for (i = 0; i < count; i++)
1941 target_buffer_set_u32(target, b, pattern);
1942 b += 4;
1945 err = dsp563xx_write_memory(target, mem_type, address, sizeof(uint32_t), count, buffer);
1948 free(buffer);
1950 return err;
1953 static const struct command_registration dsp563xx_command_handlers[] = {
1955 .name = "mwwx",
1956 .handler = dsp563xx_mem_command,
1957 .mode = COMMAND_EXEC,
1958 .help = "write x memory words",
1959 .usage = "mwwx address value [count]",
1962 .name = "mwwy",
1963 .handler = dsp563xx_mem_command,
1964 .mode = COMMAND_EXEC,
1965 .help = "write y memory words",
1966 .usage = "mwwy address value [count]",
1969 .name = "mwwp",
1970 .handler = dsp563xx_mem_command,
1971 .mode = COMMAND_EXEC,
1972 .help = "write p memory words",
1973 .usage = "mwwp address value [count]",
1976 .name = "mdwx",
1977 .handler = dsp563xx_mem_command,
1978 .mode = COMMAND_EXEC,
1979 .help = "display x memory words",
1980 .usage = "mdwx address [count]",
1983 .name = "mdwy",
1984 .handler = dsp563xx_mem_command,
1985 .mode = COMMAND_EXEC,
1986 .help = "display y memory words",
1987 .usage = "mdwy address [count]",
1990 .name = "mdwp",
1991 .handler = dsp563xx_mem_command,
1992 .mode = COMMAND_EXEC,
1993 .help = "display p memory words",
1994 .usage = "mdwp address [count]",
1996 COMMAND_REGISTRATION_DONE
1999 /** Holds methods for DSP563XX targets. */
2000 struct target_type dsp563xx_target = {
2001 .name = "dsp563xx",
2003 .poll = dsp563xx_poll,
2004 .arch_state = dsp563xx_arch_state,
2006 .target_request_data = NULL,
2008 .get_gdb_reg_list = dsp563xx_get_gdb_reg_list,
2010 .halt = dsp563xx_halt,
2011 .resume = dsp563xx_resume,
2012 .step = dsp563xx_step,
2014 .assert_reset = dsp563xx_assert_reset,
2015 .deassert_reset = dsp563xx_deassert_reset,
2016 .soft_reset_halt = dsp563xx_soft_reset_halt,
2018 .read_memory = dsp563xx_read_memory_default,
2019 .write_memory = dsp563xx_write_memory_default,
2020 .bulk_write_memory = dsp563xx_bulk_write_memory_default,
2022 .read_buffer = dsp563xx_read_buffer_default,
2023 .write_buffer = dsp563xx_write_buffer_default,
2025 .run_algorithm = dsp563xx_run_algorithm,
2027 .add_breakpoint = dsp563xx_add_breakpoint,
2028 .remove_breakpoint = dsp563xx_remove_breakpoint,
2029 .add_watchpoint = dsp563xx_add_watchpoint,
2030 .remove_watchpoint = dsp563xx_remove_watchpoint,
2032 .commands = dsp563xx_command_handlers,
2033 .target_create = dsp563xx_target_create,
2034 .init_target = dsp563xx_init_target,
2035 .examine = dsp563xx_examine,