- Fixes '=' whitespace
[fw/openocd] / src / jtag / commands.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   Copyright (C) 2009 SoftPLC Corporation                                *
9  *       http://softplc.com                                                *
10  *   dick@softplc.com                                                      *
11  *                                                                         *
12  *   Copyright (C) 2009 Zachary T Welch                                    *
13  *   zw@superlucidity.net                                                  *
14  *                                                                         *
15  *   This program is free software; you can redistribute it and/or modify  *
16  *   it under the terms of the GNU General Public License as published by  *
17  *   the Free Software Foundation; either version 2 of the License, or     *
18  *   (at your option) any later version.                                   *
19  *                                                                         *
20  *   This program is distributed in the hope that it will be useful,       *
21  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
22  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
23  *   GNU General Public License for more details.                          *
24  *                                                                         *
25  *   You should have received a copy of the GNU General Public License     *
26  *   along with this program; if not, write to the                         *
27  *   Free Software Foundation, Inc.,                                       *
28  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
29  ***************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "commands.h"
35
36 typedef struct cmd_queue_page_s
37 {
38         void *address;
39         size_t used;
40         struct cmd_queue_page_s *next;
41 } cmd_queue_page_t;
42
43 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
44 static cmd_queue_page_t *cmd_queue_pages = NULL;
45
46 jtag_command_t *jtag_command_queue = NULL;
47 static jtag_command_t **next_command_pointer = &jtag_command_queue;
48
49 void jtag_queue_command(jtag_command_t * cmd)
50 {
51         // this command goes on the end, so ensure the queue terminates
52         cmd->next = NULL;
53
54         jtag_command_t **last_cmd = next_command_pointer;
55         assert(NULL != last_cmd);
56         assert(NULL == *last_cmd);
57         *last_cmd = cmd;
58
59         // store location where the next command pointer will be stored
60         next_command_pointer = &cmd->next;
61 }
62
63 void* cmd_queue_alloc(size_t size)
64 {
65         cmd_queue_page_t **p_page = &cmd_queue_pages;
66         int offset;
67         uint8_t *t;
68
69         /*
70          * WARNING:
71          *    We align/round the *SIZE* per below
72          *    so that all pointers returned by
73          *    this function are reasonably well
74          *    aligned.
75          *
76          * If we did not, then an "odd-length" request would cause the
77          * *next* allocation to be at an *odd* address, and because
78          * this function has the same type of api as malloc() - we
79          * must also return pointers that have the same type of
80          * alignment.
81          *
82          * What I do not/have is a reasonable portable means
83          * to align by...
84          *
85          * The solution here, is based on these suggestions.
86          * http://gcc.gnu.org/ml/gcc-help/2008-12/msg00041.html
87          *
88          */
89         union worse_case_align {
90                 int i;
91                 long l;
92                 float f;
93                 void *v;
94         };
95 #define ALIGN_SIZE  (sizeof(union worse_case_align))
96
97         /* The alignment process. */
98         size = (size + ALIGN_SIZE -1) & (~(ALIGN_SIZE-1));
99         /* Done... */
100
101         if (*p_page)
102         {
103                 while ((*p_page)->next)
104                         p_page = &((*p_page)->next);
105                 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
106                         p_page = &((*p_page)->next);
107         }
108
109         if (!*p_page)
110         {
111                 *p_page = malloc(sizeof(cmd_queue_page_t));
112                 (*p_page)->used = 0;
113                 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
114                 (*p_page)->next = NULL;
115         }
116
117         offset = (*p_page)->used;
118         (*p_page)->used += size;
119
120         t = (uint8_t *)((*p_page)->address);
121         return t + offset;
122 }
123
124 void cmd_queue_free(void)
125 {
126         cmd_queue_page_t *page = cmd_queue_pages;
127
128         while (page)
129         {
130                 cmd_queue_page_t *last = page;
131                 free(page->address);
132                 page = page->next;
133                 free(last);
134         }
135
136         cmd_queue_pages = NULL;
137 }
138
139 void jtag_command_queue_reset(void)
140 {
141         cmd_queue_free();
142
143         jtag_command_queue = NULL;
144         next_command_pointer = &jtag_command_queue;
145 }
146
147 enum scan_type jtag_scan_type(const scan_command_t *cmd)
148 {
149         int i;
150         int type = 0;
151
152         for (i = 0; i < cmd->num_fields; i++)
153         {
154                 if (cmd->fields[i].in_value)
155                         type |= SCAN_IN;
156                 if (cmd->fields[i].out_value)
157                         type |= SCAN_OUT;
158         }
159
160         return type;
161 }
162
163 int jtag_scan_size(const scan_command_t *cmd)
164 {
165         int bit_count = 0;
166         int i;
167
168         /* count bits in scan command */
169         for (i = 0; i < cmd->num_fields; i++)
170         {
171                 bit_count += cmd->fields[i].num_bits;
172         }
173
174         return bit_count;
175 }
176
177 int jtag_build_buffer(const scan_command_t *cmd, uint8_t **buffer)
178 {
179         int bit_count = 0;
180         int i;
181
182         bit_count = jtag_scan_size(cmd);
183         *buffer = calloc(1,CEIL(bit_count, 8));
184
185         bit_count = 0;
186
187 #ifdef _DEBUG_JTAG_IO_
188         LOG_DEBUG("%s num_fields: %i", cmd->ir_scan ? "IRSCAN" : "DRSCAN", cmd->num_fields);
189 #endif
190
191         for (i = 0; i < cmd->num_fields; i++)
192         {
193                 if (cmd->fields[i].out_value)
194                 {
195 #ifdef _DEBUG_JTAG_IO_
196                         char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : cmd->fields[i].num_bits, 16);
197 #endif
198                         buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
199 #ifdef _DEBUG_JTAG_IO_
200                         LOG_DEBUG("fields[%i].out_value[%i]: 0x%s", i, cmd->fields[i].num_bits, char_buf);
201                         free(char_buf);
202 #endif
203                 }
204                 else
205                 {
206 #ifdef _DEBUG_JTAG_IO_
207                         LOG_DEBUG("fields[%i].out_value[%i]: NULL", i, cmd->fields[i].num_bits);
208 #endif
209                 }
210
211                 bit_count += cmd->fields[i].num_bits;
212         }
213
214 #ifdef _DEBUG_JTAG_IO_
215         //LOG_DEBUG("bit_count totalling: %i",  bit_count );
216 #endif
217
218         return bit_count;
219 }
220
221 int jtag_read_buffer(uint8_t *buffer, const scan_command_t *cmd)
222 {
223         int i;
224         int bit_count = 0;
225         int retval;
226
227         /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
228         retval = ERROR_OK;
229
230         for (i = 0; i < cmd->num_fields; i++)
231         {
232                 /* if neither in_value nor in_handler
233                  * are specified we don't have to examine this field
234                  */
235                 if (cmd->fields[i].in_value)
236                 {
237                         int num_bits = cmd->fields[i].num_bits;
238                         uint8_t *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
239
240 #ifdef _DEBUG_JTAG_IO_
241                         char *char_buf = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
242                         LOG_DEBUG("fields[%i].in_value[%i]: 0x%s", i, num_bits, char_buf);
243                         free(char_buf);
244 #endif
245
246                         if (cmd->fields[i].in_value)
247                         {
248                                 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
249                         }
250
251                         free(captured);
252                 }
253                 bit_count += cmd->fields[i].num_bits;
254         }
255
256         return retval;
257 }
258
259