1 | /****************************************************************************/
|
---|
2 | /* */
|
---|
3 | /* Module: jbistub.c */
|
---|
4 | /* */
|
---|
5 | /* Copyright (C) Altera Corporation 1997-2001 */
|
---|
6 | /* */
|
---|
7 | /* Description: Jam STAPL ByteCode Player main source file */
|
---|
8 | /* */
|
---|
9 | /* Supports Altera ByteBlaster hardware download cable */
|
---|
10 | /* on Windows 95 and Windows NT operating systems. */
|
---|
11 | /* (A device driver is required for Windows NT.) */
|
---|
12 | /* */
|
---|
13 | /* Also supports BitBlaster hardware download cable on */
|
---|
14 | /* Windows 95, Windows NT, and UNIX platforms. */
|
---|
15 | /* */
|
---|
16 | /* Revisions: 1.1 fixed control port initialization for ByteBlaster */
|
---|
17 | /* 2.0 added support for STAPL bytecode format, added code */
|
---|
18 | /* to get printer port address from Windows registry */
|
---|
19 | /* 2.1 improved messages, fixed delay-calibration bug in */
|
---|
20 | /* 16-bit DOS port, added support for "alternative */
|
---|
21 | /* cable X", added option to control whether to reset */
|
---|
22 | /* the TAP after execution, moved porting macros into */
|
---|
23 | /* jbiport.h */
|
---|
24 | /* 2.2 added support for static memory */
|
---|
25 | /* fixed /W4 warnings */
|
---|
26 | /* */
|
---|
27 | /****************************************************************************/
|
---|
28 |
|
---|
29 | #include <windows.h>
|
---|
30 |
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <string.h>
|
---|
34 | #include <io.h>
|
---|
35 | #include <fcntl.h>
|
---|
36 | #include <process.h>
|
---|
37 | #include <malloc.h>
|
---|
38 | #define POINTER_ALIGNMENT sizeof(BYTE)
|
---|
39 | #include <time.h>
|
---|
40 | #include <conio.h>
|
---|
41 | #include <ctype.h>
|
---|
42 | #include <sys/types.h>
|
---|
43 | #include <sys/stat.h>
|
---|
44 |
|
---|
45 | #include "jbiexprt.h"
|
---|
46 |
|
---|
47 | /************************************************************************
|
---|
48 | *
|
---|
49 | * Global variables
|
---|
50 | */
|
---|
51 |
|
---|
52 | unsigned char *file_buffer = NULL;
|
---|
53 | long file_pointer = 0L;
|
---|
54 | long file_length = 0L;
|
---|
55 |
|
---|
56 | /* delay count for one millisecond delay */
|
---|
57 | long one_ms_delay = 0L;
|
---|
58 |
|
---|
59 | BOOL jtag_hardware_initialized = FALSE;
|
---|
60 |
|
---|
61 | /* function prototypes to allow forward reference */
|
---|
62 | extern void delay_loop(long count);
|
---|
63 |
|
---|
64 | BOOL verbose = FALSE;
|
---|
65 |
|
---|
66 | /******************************************************************/
|
---|
67 |
|
---|
68 | #include "ftd2xx.h"
|
---|
69 |
|
---|
70 | static FT_HANDLE ftdih = NULL;
|
---|
71 |
|
---|
72 | BYTE usb_buf[64];
|
---|
73 | int usb_buf_pos;
|
---|
74 |
|
---|
75 | /******************************************************************/
|
---|
76 |
|
---|
77 | int usb_blaster_buf_write(BYTE *buf, int size, DWORD* bytes_written)
|
---|
78 | {
|
---|
79 | FT_STATUS status;
|
---|
80 | DWORD dw_bytes_written;
|
---|
81 | if((size == 1) && (buf[0]&0x01)) printf("(tms tdi) -> (%d %d)\n", (buf[0]&0x02) >> 1, (buf[0]&0x10) >> 4);
|
---|
82 | if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
|
---|
83 | {
|
---|
84 | *bytes_written = dw_bytes_written;
|
---|
85 | printf("FT_Write returned: %ld\n", status);
|
---|
86 | return 0;
|
---|
87 | }
|
---|
88 | else
|
---|
89 | {
|
---|
90 | *bytes_written = dw_bytes_written;
|
---|
91 | return 1;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | /******************************************************************/
|
---|
96 |
|
---|
97 | int usb_blaster_buf_read(BYTE* buf, int size, DWORD* bytes_read)
|
---|
98 | {
|
---|
99 | DWORD dw_bytes_read;
|
---|
100 | FT_STATUS status;
|
---|
101 | if ((status = FT_Read(ftdih, buf, size, &dw_bytes_read)) != FT_OK)
|
---|
102 | {
|
---|
103 | *bytes_read = dw_bytes_read;
|
---|
104 | printf("FT_Read returned: %ld", status);
|
---|
105 | return 0;
|
---|
106 | }
|
---|
107 | *bytes_read = dw_bytes_read;
|
---|
108 | return 1;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /******************************************************************/
|
---|
112 |
|
---|
113 | int usb_blaster_init()
|
---|
114 | {
|
---|
115 | BYTE latency_timer;
|
---|
116 |
|
---|
117 | FT_STATUS status;
|
---|
118 |
|
---|
119 | usb_buf_pos = 0;
|
---|
120 |
|
---|
121 | if ((status = FT_OpenEx("USB-Blaster", FT_OPEN_BY_DESCRIPTION, &ftdih)) != FT_OK)
|
---|
122 | {
|
---|
123 | printf("unable to open ftdi device: %ld\n", status);
|
---|
124 | return 0;
|
---|
125 | }
|
---|
126 |
|
---|
127 | if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
|
---|
128 | {
|
---|
129 | printf("unable to set latency timer: %ld\n", status);
|
---|
130 | return 0;
|
---|
131 | }
|
---|
132 |
|
---|
133 | if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
|
---|
134 | {
|
---|
135 | printf("unable to get latency timer: %ld\n", status);
|
---|
136 | return 0;
|
---|
137 | }
|
---|
138 | else
|
---|
139 | {
|
---|
140 | printf("current latency timer: %d\n", latency_timer);
|
---|
141 | }
|
---|
142 |
|
---|
143 | if ((status = FT_SetBitMode(ftdih, 0x00, 0)) != FT_OK)
|
---|
144 | {
|
---|
145 | printf("unable to disable bit i/o mode: %ld\n", status);
|
---|
146 | return 0;
|
---|
147 | }
|
---|
148 |
|
---|
149 | return 1;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /******************************************************************/
|
---|
153 |
|
---|
154 | int usb_blaster_quit()
|
---|
155 | {
|
---|
156 | FT_STATUS status;
|
---|
157 |
|
---|
158 | status = FT_Close(ftdih);
|
---|
159 |
|
---|
160 | return 1;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /******************************************************************/
|
---|
164 |
|
---|
165 | void usb_blaster_wait(int count, int tms)
|
---|
166 | {
|
---|
167 | DWORD cnt;
|
---|
168 |
|
---|
169 | BYTE buf[64];
|
---|
170 |
|
---|
171 | int i, len, extra;
|
---|
172 |
|
---|
173 | printf("usb_blaster_wait (count tms) -> (%d %d)\n", count, tms);
|
---|
174 | fflush(stdout);
|
---|
175 |
|
---|
176 | if (count <= 0) return;
|
---|
177 |
|
---|
178 | memset(buf, 0, 64);
|
---|
179 |
|
---|
180 | extra = (count & 7);
|
---|
181 |
|
---|
182 | buf[0] = 0x0C | (tms ? 0x02 : 0);
|
---|
183 | usb_blaster_buf_write(buf, 1, &cnt);
|
---|
184 |
|
---|
185 | while (count > 0)
|
---|
186 | {
|
---|
187 | if (count > 504)
|
---|
188 | {
|
---|
189 | len = 63;
|
---|
190 | count -= 504;
|
---|
191 | }
|
---|
192 | else
|
---|
193 | {
|
---|
194 | len = count >> 3;
|
---|
195 | count = 0;
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (len > 0)
|
---|
199 | {
|
---|
200 | buf[0] = len | 0x80;
|
---|
201 | usb_blaster_buf_write(buf, len + 1, &cnt);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | for (i = 0; i < extra; i++)
|
---|
205 | {
|
---|
206 | jbi_jtag_io(tms, 0, 0);
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | /******************************************************************/
|
---|
211 |
|
---|
212 | void usb_blaster_scan(int count, unsigned char *tdi, unsigned char *tdo)
|
---|
213 | {
|
---|
214 | DWORD cnt;
|
---|
215 |
|
---|
216 | BYTE buf[64];
|
---|
217 |
|
---|
218 | unsigned char extra_bits;
|
---|
219 |
|
---|
220 | int i, len, pos, extra, tdo_bit, read_tdo;
|
---|
221 |
|
---|
222 | read_tdo = (tdo != NULL);
|
---|
223 |
|
---|
224 | printf("usb_blaster_scan (count read) -> (%d %d)\n", count, read_tdo);
|
---|
225 | fflush(stdout);
|
---|
226 |
|
---|
227 | if (count <= 0) return;
|
---|
228 |
|
---|
229 | pos = 0;
|
---|
230 | extra = (count & 7);
|
---|
231 |
|
---|
232 | if (extra == 0)
|
---|
233 | {
|
---|
234 | count -= 8;
|
---|
235 | extra = 8;
|
---|
236 | }
|
---|
237 |
|
---|
238 | extra_bits = tdi[count >> 3];
|
---|
239 |
|
---|
240 | while (count > 0)
|
---|
241 | {
|
---|
242 | if (count > 504)
|
---|
243 | {
|
---|
244 | len = 63;
|
---|
245 | count -= 504;
|
---|
246 | }
|
---|
247 | else
|
---|
248 | {
|
---|
249 | len = count >> 3;
|
---|
250 | count = 0;
|
---|
251 | }
|
---|
252 |
|
---|
253 | if (len > 0)
|
---|
254 | {
|
---|
255 | buf[0] = len | 0x80 | (read_tdo ? 0x40 : 0);
|
---|
256 | memcpy(buf + 1, tdi + pos, len);
|
---|
257 | usb_blaster_buf_write(buf, len + 1, &cnt);
|
---|
258 | if (read_tdo)
|
---|
259 | {
|
---|
260 | usb_blaster_buf_read(tdo + pos, len, &cnt);
|
---|
261 | }
|
---|
262 | pos += len;
|
---|
263 | }
|
---|
264 | }
|
---|
265 | for (i = 0; i < extra; i++)
|
---|
266 | {
|
---|
267 | tdo_bit = jbi_jtag_io(
|
---|
268 | (i == extra - 1),
|
---|
269 | extra_bits & (1 << (i & 7)),
|
---|
270 | read_tdo);
|
---|
271 |
|
---|
272 | if (read_tdo)
|
---|
273 | {
|
---|
274 | if (tdo_bit)
|
---|
275 | {
|
---|
276 | tdo[pos] |= (1 << (i & 7));
|
---|
277 | }
|
---|
278 | else
|
---|
279 | {
|
---|
280 | tdo[pos] &= ~(unsigned int) (1 << (i & 7));
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | /************************************************************************
|
---|
287 | *
|
---|
288 | * Customized interface functions for Jam STAPL ByteCode Player I/O:
|
---|
289 | *
|
---|
290 | * jbi_jtag_io()
|
---|
291 | * jbi_message()
|
---|
292 | * jbi_delay()
|
---|
293 | */
|
---|
294 |
|
---|
295 | int jbi_jtag_io(int tms, int tdi, int read_tdo)
|
---|
296 | {
|
---|
297 | BYTE buf[3];
|
---|
298 | DWORD count;
|
---|
299 |
|
---|
300 | int data = 0;
|
---|
301 | int tdo = 0;
|
---|
302 |
|
---|
303 | if (!jtag_hardware_initialized)
|
---|
304 | {
|
---|
305 | usb_blaster_init();
|
---|
306 | jtag_hardware_initialized = TRUE;
|
---|
307 | }
|
---|
308 |
|
---|
309 | data = (tdi ? 0x10 : 0) | (tms ? 0x02 : 0);
|
---|
310 |
|
---|
311 | buf[0] = data | 0x0C | (read_tdo ? 0x40 : 0);
|
---|
312 | buf[1] = data | 0x0C | 0x01;
|
---|
313 | buf[2] = data | 0x0C;
|
---|
314 | usb_blaster_buf_write(buf, 3, &count);
|
---|
315 |
|
---|
316 | if (read_tdo)
|
---|
317 | {
|
---|
318 | usb_blaster_buf_read(buf, 1, &count);
|
---|
319 | tdo = buf[0]&0x01;
|
---|
320 | }
|
---|
321 |
|
---|
322 | return (tdo);
|
---|
323 | }
|
---|
324 |
|
---|
325 | void jbi_message(char *message_text)
|
---|
326 | {
|
---|
327 | puts(message_text);
|
---|
328 | fflush(stdout);
|
---|
329 | }
|
---|
330 |
|
---|
331 | void jbi_export_integer(char *key, long value)
|
---|
332 | {
|
---|
333 | if (verbose)
|
---|
334 | {
|
---|
335 | printf("Export: key = \"%s\", value = %ld\n", key, value);
|
---|
336 | fflush(stdout);
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | #define HEX_LINE_CHARS 72
|
---|
341 | #define HEX_LINE_BITS (HEX_LINE_CHARS * 4)
|
---|
342 |
|
---|
343 | char conv_to_hex(unsigned long value)
|
---|
344 | {
|
---|
345 | char c;
|
---|
346 |
|
---|
347 | if (value > 9)
|
---|
348 | {
|
---|
349 | c = (char) (value + ('A' - 10));
|
---|
350 | }
|
---|
351 | else
|
---|
352 | {
|
---|
353 | c = (char) (value + '0');
|
---|
354 | }
|
---|
355 |
|
---|
356 | return (c);
|
---|
357 | }
|
---|
358 |
|
---|
359 | void jbi_export_boolean_array(char *key, unsigned char *data, long count)
|
---|
360 | {
|
---|
361 | char string[HEX_LINE_CHARS + 1];
|
---|
362 | long i, offset;
|
---|
363 | unsigned long size, line, lines, linebits, value, j, k;
|
---|
364 |
|
---|
365 | if (verbose)
|
---|
366 | {
|
---|
367 | if (count > HEX_LINE_BITS)
|
---|
368 | {
|
---|
369 | printf("Export: key = \"%s\", %ld bits, value = HEX\n", key, count);
|
---|
370 | lines = (count + (HEX_LINE_BITS - 1)) / HEX_LINE_BITS;
|
---|
371 |
|
---|
372 | for (line = 0; line < lines; ++line)
|
---|
373 | {
|
---|
374 | if (line < (lines - 1))
|
---|
375 | {
|
---|
376 | linebits = HEX_LINE_BITS;
|
---|
377 | size = HEX_LINE_CHARS;
|
---|
378 | offset = count - ((line + 1) * HEX_LINE_BITS);
|
---|
379 | }
|
---|
380 | else
|
---|
381 | {
|
---|
382 | linebits = count - ((lines - 1) * HEX_LINE_BITS);
|
---|
383 | size = (linebits + 3) / 4;
|
---|
384 | offset = 0L;
|
---|
385 | }
|
---|
386 |
|
---|
387 | string[size] = '\0';
|
---|
388 | j = size - 1;
|
---|
389 | value = 0;
|
---|
390 |
|
---|
391 | for (k = 0; k < linebits; ++k)
|
---|
392 | {
|
---|
393 | i = k + offset;
|
---|
394 | if (data[i >> 3] & (1 << (i & 7))) value |= (1 << (i & 3));
|
---|
395 | if ((i & 3) == 3)
|
---|
396 | {
|
---|
397 | string[j] = conv_to_hex(value);
|
---|
398 | value = 0;
|
---|
399 | --j;
|
---|
400 | }
|
---|
401 | }
|
---|
402 | if ((k & 3) > 0) string[j] = conv_to_hex(value);
|
---|
403 |
|
---|
404 | printf("%s\n", string);
|
---|
405 | }
|
---|
406 |
|
---|
407 | fflush(stdout);
|
---|
408 | }
|
---|
409 | else
|
---|
410 | {
|
---|
411 | size = (count + 3) / 4;
|
---|
412 | string[size] = '\0';
|
---|
413 | j = size - 1;
|
---|
414 | value = 0;
|
---|
415 |
|
---|
416 | for (i = 0; i < count; ++i)
|
---|
417 | {
|
---|
418 | if (data[i >> 3] & (1 << (i & 7))) value |= (1 << (i & 3));
|
---|
419 | if ((i & 3) == 3)
|
---|
420 | {
|
---|
421 | string[j] = conv_to_hex(value);
|
---|
422 | value = 0;
|
---|
423 | --j;
|
---|
424 | }
|
---|
425 | }
|
---|
426 | if ((i & 3) > 0) string[j] = conv_to_hex(value);
|
---|
427 |
|
---|
428 | printf("Export: key = \"%s\", %ld bits, value = HEX %s\n",
|
---|
429 | key, count, string);
|
---|
430 | fflush(stdout);
|
---|
431 | }
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | void jbi_delay(long microseconds)
|
---|
436 | {
|
---|
437 | delay_loop(microseconds *
|
---|
438 | ((one_ms_delay / 1000L) + ((one_ms_delay % 1000L) ? 1 : 0)));
|
---|
439 | }
|
---|
440 |
|
---|
441 | void *jbi_malloc(unsigned int size)
|
---|
442 | {
|
---|
443 | unsigned int n_bytes_to_allocate =
|
---|
444 | (POINTER_ALIGNMENT * ((size + POINTER_ALIGNMENT - 1) / POINTER_ALIGNMENT));
|
---|
445 |
|
---|
446 | unsigned char *ptr = 0;
|
---|
447 |
|
---|
448 | ptr = (unsigned char *) malloc(n_bytes_to_allocate);
|
---|
449 |
|
---|
450 | return ptr;
|
---|
451 | }
|
---|
452 |
|
---|
453 | void jbi_free(void *ptr)
|
---|
454 | {
|
---|
455 | if (ptr != 0)
|
---|
456 | {
|
---|
457 | free(ptr);
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | void calibrate_delay(void)
|
---|
462 | {
|
---|
463 | one_ms_delay = 0L;
|
---|
464 | }
|
---|
465 |
|
---|
466 | char *error_text[] =
|
---|
467 | {
|
---|
468 | /* JBIC_SUCCESS 0 */ "success",
|
---|
469 | /* JBIC_OUT_OF_MEMORY 1 */ "out of memory",
|
---|
470 | /* JBIC_IO_ERROR 2 */ "file access error",
|
---|
471 | /* JAMC_SYNTAX_ERROR 3 */ "syntax error",
|
---|
472 | /* JBIC_UNEXPECTED_END 4 */ "unexpected end of file",
|
---|
473 | /* JBIC_UNDEFINED_SYMBOL 5 */ "undefined symbol",
|
---|
474 | /* JAMC_REDEFINED_SYMBOL 6 */ "redefined symbol",
|
---|
475 | /* JBIC_INTEGER_OVERFLOW 7 */ "integer overflow",
|
---|
476 | /* JBIC_DIVIDE_BY_ZERO 8 */ "divide by zero",
|
---|
477 | /* JBIC_CRC_ERROR 9 */ "CRC mismatch",
|
---|
478 | /* JBIC_INTERNAL_ERROR 10 */ "internal error",
|
---|
479 | /* JBIC_BOUNDS_ERROR 11 */ "bounds error",
|
---|
480 | /* JAMC_TYPE_MISMATCH 12 */ "type mismatch",
|
---|
481 | /* JAMC_ASSIGN_TO_CONST 13 */ "assignment to constant",
|
---|
482 | /* JAMC_NEXT_UNEXPECTED 14 */ "NEXT unexpected",
|
---|
483 | /* JAMC_POP_UNEXPECTED 15 */ "POP unexpected",
|
---|
484 | /* JAMC_RETURN_UNEXPECTED 16 */ "RETURN unexpected",
|
---|
485 | /* JAMC_ILLEGAL_SYMBOL 17 */ "illegal symbol name",
|
---|
486 | /* JBIC_VECTOR_MAP_FAILED 18 */ "vector signal name not found",
|
---|
487 | /* JBIC_USER_ABORT 19 */ "execution cancelled",
|
---|
488 | /* JBIC_STACK_OVERFLOW 20 */ "stack overflow",
|
---|
489 | /* JBIC_ILLEGAL_OPCODE 21 */ "illegal instruction code",
|
---|
490 | /* JAMC_PHASE_ERROR 22 */ "phase error",
|
---|
491 | /* JAMC_SCOPE_ERROR 23 */ "scope error",
|
---|
492 | /* JBIC_ACTION_NOT_FOUND 24 */ "action not found",
|
---|
493 | };
|
---|
494 |
|
---|
495 | #define MAX_ERROR_CODE (int)((sizeof(error_text)/sizeof(error_text[0]))+1)
|
---|
496 |
|
---|
497 | /************************************************************************/
|
---|
498 |
|
---|
499 | int main(int argc, char **argv)
|
---|
500 | {
|
---|
501 | BOOL help = FALSE;
|
---|
502 | BOOL error = FALSE;
|
---|
503 | char *filename = NULL;
|
---|
504 | long offset = 0L;
|
---|
505 | long error_address = 0L;
|
---|
506 | JBI_RETURN_TYPE crc_result = JBIC_SUCCESS;
|
---|
507 | JBI_RETURN_TYPE exec_result = JBIC_SUCCESS;
|
---|
508 | unsigned short expected_crc = 0;
|
---|
509 | unsigned short actual_crc = 0;
|
---|
510 | char key[33] = {0};
|
---|
511 | char value[257] = {0};
|
---|
512 | int exit_status = 0;
|
---|
513 | int arg = 0;
|
---|
514 | int exit_code = 0;
|
---|
515 | int format_version = 0;
|
---|
516 | time_t start_time = 0;
|
---|
517 | time_t end_time = 0;
|
---|
518 | int time_delta = 0;
|
---|
519 | char *workspace = NULL;
|
---|
520 | char *action = NULL;
|
---|
521 | char *init_list[10];
|
---|
522 | int init_count = 0;
|
---|
523 | FILE *fp = NULL;
|
---|
524 | struct stat sbuf;
|
---|
525 | long workspace_size = 0;
|
---|
526 | char *exit_string = NULL;
|
---|
527 | int reset_jtag = 1;
|
---|
528 | int execute_program = 1;
|
---|
529 | int action_count = 0;
|
---|
530 | int procedure_count = 0;
|
---|
531 | int index = 0;
|
---|
532 | char *action_name = NULL;
|
---|
533 | char *description = NULL;
|
---|
534 | JBI_PROCINFO *procedure_list = NULL;
|
---|
535 | JBI_PROCINFO *procptr = NULL;
|
---|
536 |
|
---|
537 | verbose = FALSE;
|
---|
538 |
|
---|
539 | init_list[0] = NULL;
|
---|
540 |
|
---|
541 | /* print out the version string and copyright message */
|
---|
542 | fprintf(stderr, "Jam STAPL ByteCode Player Version 2.2\nCopyright (C) 1998-2001 Altera Corporation\n\n");
|
---|
543 |
|
---|
544 | for (arg = 1; arg < argc; arg++)
|
---|
545 | {
|
---|
546 | if (argv[arg][0] == '-')
|
---|
547 | {
|
---|
548 | switch(toupper(argv[arg][1]))
|
---|
549 | {
|
---|
550 | case 'A': /* set action name */
|
---|
551 | if (action == NULL)
|
---|
552 | {
|
---|
553 | action = &argv[arg][2];
|
---|
554 | }
|
---|
555 | else
|
---|
556 | {
|
---|
557 | error = TRUE;
|
---|
558 | }
|
---|
559 | break;
|
---|
560 |
|
---|
561 | case 'D': /* initialization list */
|
---|
562 | if (argv[arg][2] == '"')
|
---|
563 | {
|
---|
564 | init_list[init_count] = &argv[arg][3];
|
---|
565 | }
|
---|
566 | else
|
---|
567 | {
|
---|
568 | init_list[init_count] = &argv[arg][2];
|
---|
569 | }
|
---|
570 | init_list[++init_count] = NULL;
|
---|
571 | break;
|
---|
572 |
|
---|
573 | case 'R': /* don't reset the JTAG chain after use */
|
---|
574 | reset_jtag = 0;
|
---|
575 | break;
|
---|
576 |
|
---|
577 | case 'M': /* set memory size */
|
---|
578 | if (sscanf(&argv[arg][2], "%ld", &workspace_size) != 1)
|
---|
579 | error = TRUE;
|
---|
580 | if (workspace_size == 0) error = TRUE;
|
---|
581 | break;
|
---|
582 |
|
---|
583 | case 'H': /* help */
|
---|
584 | help = TRUE;
|
---|
585 | break;
|
---|
586 |
|
---|
587 | case 'V': /* verbose */
|
---|
588 | verbose = TRUE;
|
---|
589 | break;
|
---|
590 |
|
---|
591 | case 'I': /* show info only, do not execute */
|
---|
592 | verbose = TRUE;
|
---|
593 | execute_program = 0;
|
---|
594 | break;
|
---|
595 |
|
---|
596 | default:
|
---|
597 | error = TRUE;
|
---|
598 | break;
|
---|
599 | }
|
---|
600 | }
|
---|
601 | else
|
---|
602 | {
|
---|
603 | /* it's a filename */
|
---|
604 | if (filename == NULL)
|
---|
605 | {
|
---|
606 | filename = argv[arg];
|
---|
607 | }
|
---|
608 | else
|
---|
609 | {
|
---|
610 | /* error -- we already found a filename */
|
---|
611 | error = TRUE;
|
---|
612 | }
|
---|
613 | }
|
---|
614 |
|
---|
615 | if (error)
|
---|
616 | {
|
---|
617 | fprintf(stderr, "Illegal argument: \"%s\"\n", argv[arg]);
|
---|
618 | help = TRUE;
|
---|
619 | error = FALSE;
|
---|
620 | }
|
---|
621 | }
|
---|
622 |
|
---|
623 | if (help || (filename == NULL))
|
---|
624 | {
|
---|
625 | fprintf(stderr, "Usage: jbi [options] <filename>\n");
|
---|
626 | fprintf(stderr, "\nAvailable options:\n");
|
---|
627 | fprintf(stderr, " -h : show help message\n");
|
---|
628 | fprintf(stderr, " -v : show verbose messages\n");
|
---|
629 | fprintf(stderr, " -i : show file info only - does not execute any action\n");
|
---|
630 | fprintf(stderr, " -a<action> : specify an action name (Jam STAPL)\n");
|
---|
631 | fprintf(stderr, " -d<var=val> : initialize variable to specified value (Jam 1.1)\n");
|
---|
632 | fprintf(stderr, " -d<proc=1> : enable optional procedure (Jam STAPL)\n");
|
---|
633 | fprintf(stderr, " -d<proc=0> : disable recommended procedure (Jam STAPL)\n");
|
---|
634 | fprintf(stderr, " -r : don't reset JTAG TAP after use\n");
|
---|
635 | exit_status = 1;
|
---|
636 | }
|
---|
637 | else if ((workspace_size > 0) &&
|
---|
638 | ((workspace = (char *) jbi_malloc((size_t) workspace_size)) == NULL))
|
---|
639 | {
|
---|
640 | fprintf(stderr, "Error: can't allocate memory (%d Kbytes)\n",
|
---|
641 | (int) (workspace_size / 1024L));
|
---|
642 | exit_status = 1;
|
---|
643 | }
|
---|
644 | else if (access(filename, 0) != 0)
|
---|
645 | {
|
---|
646 | fprintf(stderr, "Error: can't access file \"%s\"\n", filename);
|
---|
647 | exit_status = 1;
|
---|
648 | }
|
---|
649 | else
|
---|
650 | {
|
---|
651 | /* get length of file */
|
---|
652 | if (stat(filename, &sbuf) == 0) file_length = sbuf.st_size;
|
---|
653 |
|
---|
654 | if ((fp = fopen(filename, "rb")) == NULL)
|
---|
655 | {
|
---|
656 | fprintf(stderr, "Error: can't open file \"%s\"\n", filename);
|
---|
657 | exit_status = 1;
|
---|
658 | }
|
---|
659 | else
|
---|
660 | {
|
---|
661 | /*
|
---|
662 | * Read entire file into a buffer
|
---|
663 | */
|
---|
664 | file_buffer = (unsigned char *) jbi_malloc((size_t) file_length);
|
---|
665 |
|
---|
666 | if (file_buffer == NULL)
|
---|
667 | {
|
---|
668 | fprintf(stderr, "Error: can't allocate memory (%d Kbytes)\n",
|
---|
669 | (int) (file_length / 1024L));
|
---|
670 | exit_status = 1;
|
---|
671 | }
|
---|
672 | else
|
---|
673 | {
|
---|
674 | if (fread(file_buffer, 1, (size_t) file_length, fp) !=
|
---|
675 | (size_t) file_length)
|
---|
676 | {
|
---|
677 | fprintf(stderr, "Error reading file \"%s\"\n", filename);
|
---|
678 | exit_status = 1;
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | fclose(fp);
|
---|
683 | }
|
---|
684 |
|
---|
685 | if (exit_status == 0)
|
---|
686 | {
|
---|
687 | /*
|
---|
688 | * Calibrate the delay loop function
|
---|
689 | */
|
---|
690 | calibrate_delay();
|
---|
691 |
|
---|
692 | /*
|
---|
693 | * Check CRC
|
---|
694 | */
|
---|
695 | crc_result = jbi_check_crc(file_buffer, file_length,
|
---|
696 | &expected_crc, &actual_crc);
|
---|
697 |
|
---|
698 | if (verbose || (crc_result == JBIC_CRC_ERROR))
|
---|
699 | {
|
---|
700 | switch (crc_result)
|
---|
701 | {
|
---|
702 | case JBIC_SUCCESS:
|
---|
703 | printf("CRC matched: CRC value = %04X\n", actual_crc);
|
---|
704 | break;
|
---|
705 |
|
---|
706 | case JBIC_CRC_ERROR:
|
---|
707 | printf("CRC mismatch: expected %04X, actual %04X\n",
|
---|
708 | expected_crc, actual_crc);
|
---|
709 | break;
|
---|
710 |
|
---|
711 | case JBIC_UNEXPECTED_END:
|
---|
712 | printf("Expected CRC not found, actual CRC value = %04X\n",
|
---|
713 | actual_crc);
|
---|
714 | break;
|
---|
715 |
|
---|
716 | case JBIC_IO_ERROR:
|
---|
717 | printf("Error: File format is not recognized.\n");
|
---|
718 | exit(1);
|
---|
719 | break;
|
---|
720 |
|
---|
721 | default:
|
---|
722 | printf("CRC function returned error code %d\n", crc_result);
|
---|
723 | break;
|
---|
724 | }
|
---|
725 | }
|
---|
726 |
|
---|
727 | if (verbose)
|
---|
728 | {
|
---|
729 | /*
|
---|
730 | * Display file format version
|
---|
731 | */
|
---|
732 | jbi_get_file_info(file_buffer, file_length,
|
---|
733 | &format_version, &action_count, &procedure_count);
|
---|
734 |
|
---|
735 | printf("File format is %s ByteCode format\n",
|
---|
736 | (format_version == 2) ? "Jam STAPL" : "pre-standardized Jam 1.1");
|
---|
737 |
|
---|
738 | /*
|
---|
739 | * Dump out NOTE fields
|
---|
740 | */
|
---|
741 | while (jbi_get_note(file_buffer, file_length,
|
---|
742 | &offset, key, value, 256) == 0)
|
---|
743 | {
|
---|
744 | printf("NOTE \"%s\" = \"%s\"\n", key, value);
|
---|
745 | }
|
---|
746 |
|
---|
747 | /*
|
---|
748 | * Dump the action table
|
---|
749 | */
|
---|
750 | if ((format_version == 2) && (action_count > 0))
|
---|
751 | {
|
---|
752 | printf("\nActions available in this file:\n");
|
---|
753 |
|
---|
754 | for (index = 0; index < action_count; ++index)
|
---|
755 | {
|
---|
756 | jbi_get_action_info(file_buffer, file_length,
|
---|
757 | index, &action_name, &description, &procedure_list);
|
---|
758 |
|
---|
759 | if (description == NULL)
|
---|
760 | {
|
---|
761 | printf("%s\n", action_name);
|
---|
762 | }
|
---|
763 | else
|
---|
764 | {
|
---|
765 | printf("%s \"%s\"\n", action_name, description);
|
---|
766 | }
|
---|
767 |
|
---|
768 | procptr = procedure_list;
|
---|
769 | while (procptr != NULL)
|
---|
770 | {
|
---|
771 | if (procptr->attributes != 0)
|
---|
772 | {
|
---|
773 | printf(" %s (%s)\n", procptr->name,
|
---|
774 | (procptr->attributes == 1) ?
|
---|
775 | "optional" : "recommended");
|
---|
776 | }
|
---|
777 |
|
---|
778 | procedure_list = procptr->next;
|
---|
779 | jbi_free(procptr);
|
---|
780 | procptr = procedure_list;
|
---|
781 | }
|
---|
782 | }
|
---|
783 |
|
---|
784 | /* add a blank line before execution messages */
|
---|
785 | if (execute_program) printf("\n");
|
---|
786 | }
|
---|
787 | }
|
---|
788 |
|
---|
789 | if (execute_program)
|
---|
790 | {
|
---|
791 | /*
|
---|
792 | * Execute the Jam STAPL ByteCode program
|
---|
793 | */
|
---|
794 | time(&start_time);
|
---|
795 | exec_result = jbi_execute(file_buffer, file_length, workspace,
|
---|
796 | workspace_size, action, init_list, reset_jtag,
|
---|
797 | &error_address, &exit_code, &format_version);
|
---|
798 | time(&end_time);
|
---|
799 |
|
---|
800 | if (exec_result == JBIC_SUCCESS)
|
---|
801 | {
|
---|
802 | if (format_version == 2)
|
---|
803 | {
|
---|
804 | switch (exit_code)
|
---|
805 | {
|
---|
806 | case 0: exit_string = "Success"; break;
|
---|
807 | case 1: exit_string = "Checking chain failure"; break;
|
---|
808 | case 2: exit_string = "Reading IDCODE failure"; break;
|
---|
809 | case 3: exit_string = "Reading USERCODE failure"; break;
|
---|
810 | case 4: exit_string = "Reading UESCODE failure"; break;
|
---|
811 | case 5: exit_string = "Entering ISP failure"; break;
|
---|
812 | case 6: exit_string = "Unrecognized device"; break;
|
---|
813 | case 7: exit_string = "Device revision is not supported"; break;
|
---|
814 | case 8: exit_string = "Erase failure"; break;
|
---|
815 | case 9: exit_string = "Device is not blank"; break;
|
---|
816 | case 10: exit_string = "Device programming failure"; break;
|
---|
817 | case 11: exit_string = "Device verify failure"; break;
|
---|
818 | case 12: exit_string = "Read failure"; break;
|
---|
819 | case 13: exit_string = "Calculating checksum failure"; break;
|
---|
820 | case 14: exit_string = "Setting security bit failure"; break;
|
---|
821 | case 15: exit_string = "Querying security bit failure"; break;
|
---|
822 | case 16: exit_string = "Exiting ISP failure"; break;
|
---|
823 | case 17: exit_string = "Performing system test failure"; break;
|
---|
824 | default: exit_string = "Unknown exit code"; break;
|
---|
825 | }
|
---|
826 | }
|
---|
827 | else
|
---|
828 | {
|
---|
829 | switch (exit_code)
|
---|
830 | {
|
---|
831 | case 0: exit_string = "Success"; break;
|
---|
832 | case 1: exit_string = "Illegal initialization values"; break;
|
---|
833 | case 2: exit_string = "Unrecognized device"; break;
|
---|
834 | case 3: exit_string = "Device revision is not supported"; break;
|
---|
835 | case 4: exit_string = "Device programming failure"; break;
|
---|
836 | case 5: exit_string = "Device is not blank"; break;
|
---|
837 | case 6: exit_string = "Device verify failure"; break;
|
---|
838 | case 7: exit_string = "SRAM configuration failure"; break;
|
---|
839 | default: exit_string = "Unknown exit code"; break;
|
---|
840 | }
|
---|
841 | }
|
---|
842 |
|
---|
843 | printf("Exit code = %d... %s\n", exit_code, exit_string);
|
---|
844 | }
|
---|
845 | else if ((format_version == 2) &&
|
---|
846 | (exec_result == JBIC_ACTION_NOT_FOUND))
|
---|
847 | {
|
---|
848 | if ((action == NULL) || (*action == '\0'))
|
---|
849 | {
|
---|
850 | printf("Error: no action specified for Jam STAPL file.\nProgram terminated.\n");
|
---|
851 | }
|
---|
852 | else
|
---|
853 | {
|
---|
854 | printf("Error: action \"%s\" is not supported for this Jam STAPL file.\nProgram terminated.\n", action);
|
---|
855 | }
|
---|
856 | }
|
---|
857 | else if (exec_result < MAX_ERROR_CODE)
|
---|
858 | {
|
---|
859 | printf("Error at address %ld: %s.\nProgram terminated.\n",
|
---|
860 | error_address, error_text[exec_result]);
|
---|
861 | }
|
---|
862 | else
|
---|
863 | {
|
---|
864 | printf("Unknown error code %d\n", exec_result);
|
---|
865 | }
|
---|
866 |
|
---|
867 | /*
|
---|
868 | * Print out elapsed time
|
---|
869 | */
|
---|
870 | if (verbose)
|
---|
871 | {
|
---|
872 | time_delta = (int) (end_time - start_time);
|
---|
873 | printf("Elapsed time = %02u:%02u:%02u\n",
|
---|
874 | time_delta / 3600, /* hours */
|
---|
875 | (time_delta % 3600) / 60, /* minutes */
|
---|
876 | time_delta % 60); /* seconds */
|
---|
877 | }
|
---|
878 | }
|
---|
879 | }
|
---|
880 | }
|
---|
881 |
|
---|
882 | if (jtag_hardware_initialized) usb_blaster_quit();
|
---|
883 |
|
---|
884 | if (workspace != NULL) jbi_free(workspace);
|
---|
885 | if (file_buffer != NULL) jbi_free(file_buffer);
|
---|
886 |
|
---|
887 | return (exit_status);
|
---|
888 | }
|
---|
889 |
|
---|
890 | void delay_loop(long count)
|
---|
891 | {
|
---|
892 | while (count != 0L) count--;
|
---|
893 | }
|
---|