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