OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [src_c/] [jtag/] [jtag_libusb/] [jtag_libusb.c] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 38 alirezamon
#include <stdio.h>
2
#include <stdlib.h>
3
#include <ctype.h>
4
#include <stdint.h>
5
#include <unistd.h> // getopt
6
#include <inttypes.h>
7
#include <string.h>
8
#include "jtag.h"
9
 
10
/* You may want to change the VENDOR_ID and PRODUCT_ID
11
 * depending on your device.
12
 */
13
#define VENDOR_ID      0x09fb   // Altera
14
#define PRODUCT_ID     0x6001   // usb blaster (DE2-115) 
15
// Altera usb blaster  product IDs "6001", "6002", "6003", MODE="0666"   
16
// dose not work for USB-Blaster II "6010", "6810"
17
// run ./list_usb_dev  to see the list of all usb devices' vid and pid
18
 
19
unsigned usb_blaster_id = PRODUCT_ID;
20
 
21
 
22
#define UPDATE_WB_ADDR  0x7
23
#define UPDATE_WB_WR_DATA  0x6
24
#define UPDATE_WB_RD_DATA  0x5
25
#define RD_WR_STATUS    0x4
26
 
27
#define BIT_NUM         (word_width<<3) 
28
#define BYTE_NUM         word_width     
29
/* Global vars */
30
unsigned int index_num=126;
31
unsigned int word_width=4; // 
32
unsigned int write_verify=0;
33
unsigned int memory_offset=0;
34
unsigned int memory_boundary=0xFFFFFFFF;
35
 
36
 
37
 
38
char * binary_file_name=0;
39
char enable_binary_send=0;
40
char enable_binary_read=0;
41
char * write_data=0;
42
 
43
 
44
 
45
 
46
/* functions */
47
int send_binary_file();
48
int read_mem();
49
void usage();
50
void processArgs (int , char** );
51
int send_data ();
52
int hexcut( char *  , unsigned *  , int  );
53
int vdr_large (unsigned , char * , char *);
54
void hexgen( char * , unsigned *, int );
55
 
56
int main(int argc, char **argv) {
57
        //unsigned bits;
58
        //unsigned int val;
59
 
60
        //unsigned bits;
61
        //unsigned val;
62
 
63
        processArgs (argc, argv );
64
        printf("index num=%u\n",index_num);
65
        if (jtag_open_virtual_device(index_num,VENDOR_ID,usb_blaster_id)){
66
                fprintf (stderr, "Error openning jtag IP with %d index num\n",index_num);
67
                return -1;
68
        }
69
        if (enable_binary_send) {
70
                if( send_binary_file() == -1) return -1;
71
        }
72
 
73
        if (write_data!=0){
74
                printf("send %s to jtag\n",write_data);
75
                send_data();
76
 
77
 
78
        }
79
 
80
        return 0;
81
}
82
 
83
 
84
 
85
void usage(){
86
 
87
        printf ("usage:./jtag_main [-n  index number] [-i file_name][-c][-s rd/wr offset address][-d string]\n");
88
        printf ("\t-a   product_id in hex: Altera usb blasterI  product IDs \"6001\", \"6002\", \"6003\" . The default value is 0x6001\n");
89
        printf ("\t-n   index number: the target jtag IP core index number. The default number is 126\n");
90
        printf ("\t-i   file_name:  input binary file name (.bin file)\n");
91
        printf ("\t-r   read memory content and display in terminal\n");
92
        printf ("\t-w   bin file word width in byte. default is 4 bytes (32 bits)\n");
93
        printf ("\t-c   verify after write\n");
94
        printf ("\t-s   memory wr/rd offset address in byte (hex format). The default value is 0x0000000\n");
95
        printf ("\t-e   memory  boundary address in byte (hex format).  The default value is 0xFFFFFFFF\n");
96
        printf ("\t-d   string: use for setting instruction or data value to jtag tap.  string format : \"instr1,instr2,...,instrn\"\n \tinstri = I:instruct_num: send instruct_num to instruction register \n \tD:data_size_in_bit:data : send data in hex to data register\n  \tR:data_size_in_bit:data : Read data register and show it on screan then write given data in hex to data register\n");
97
 
98
}
99
 
100
void processArgs (int argc, char **argv )
101
{
102
   char c;
103
int p;
104
 
105
   /* don't want getopt to moan - I can do that just fine thanks! */
106
   opterr = 0;
107
   if (argc < 2)  usage();
108 48 alirezamon
   while ((c = getopt (argc, argv, "s:a:e:d:n:t:i:w:cr")) != -1)
109 38 alirezamon
      {
110
         switch (c)
111
            {
112
            case 'a':
113
                p=sscanf(optarg,"%x",&usb_blaster_id);
114
                if( p==0){
115
                         fprintf (stderr, "invalid usb_blaster_id format `%s'.\n", optarg);
116
                         usage();
117
                         exit(1);
118
                }else if(usb_blaster_id == 0x6010 ||  usb_blaster_id ==0x6810 ){
119
                         fprintf (stderr, "%x is a usbblasterII id which is not supported. Please use jtag_quartus_stp for usbblaster II\n", usb_blaster_id);
120
                         usage();
121
                         exit(1);
122
                }
123
 
124 48 alirezamon
            case 't':   /* chain number */
125
                //not used for Altera
126
               break;
127
 
128 38 alirezamon
            case 'n':   /* index number */
129
               index_num = atoi(optarg);
130
               break;
131
            case 'i':   /* input binary file name */
132
                binary_file_name = optarg;
133
                enable_binary_send=1;
134
                break;
135
             case 'r':  /* read memory */
136
                enable_binary_read=1;
137
                break;
138
            case 'w':   /* word width in byte */
139
                word_width= atoi(optarg);
140
                break;
141
            case 'c':   /* enable write verify */
142
                write_verify= 1;
143
                break;
144
            case 'd':   /* send string */
145
                write_data= optarg;
146
                break;
147
            case 's':   /* set offset address*/
148
 
149
                p=sscanf(optarg,"%x",&memory_offset);
150
                if( p==0){
151
                         fprintf (stderr, "invalid memory offset adress format `%s'.\n", optarg);
152
                         usage();
153
                         exit(1);
154
                }
155
                //printf("p=%d,memory_offset=%x\n",p,memory_offset);            
156
                break;
157
            case 'e':   /* word width in byte */
158
                p=sscanf(optarg,"%x",&memory_boundary);
159
                if( p==0){
160
                         fprintf (stderr, "invalid memory boundary adress format `%s'.\n", optarg);
161
                         usage();
162
                         exit(1);
163
                }
164
                break;
165
 
166
            case '?':
167
               if (isprint (optopt))
168
                  fprintf (stderr, "Unknown option `-%c'.\n", optopt);
169
               else
170
                  fprintf (stderr,
171
                           "Unknown option character `\\x%x'.\n",
172
                           optopt);
173
            default:
174
               usage();
175
               exit(1);
176
            }
177
      }
178
}
179
 
180
unsigned * read_file (FILE * fp, unsigned int  * n ){
181
 
182
        unsigned * buffer;
183
        unsigned val;
184
        unsigned char ch;
185
        unsigned int i=0;
186
        char cnt=0;
187
        unsigned int num=0;
188
        unsigned int width= (BYTE_NUM < sizeof(unsigned )) ? BYTE_NUM :  sizeof(unsigned ); //max is 4 then
189
        fseek(fp, 0, SEEK_END); // seek to end of file
190
        num = ftell(fp); // get current file pointer
191
        *n=num;// number of bytes from the beginning of the file        
192
 
193
 
194
        num=(num/width)+2;
195
        fseek(fp, 0, SEEK_SET);
196
        //printf ("num=%u\n",num);      
197
        buffer = (unsigned *) malloc(num * sizeof(unsigned ) );  //memory allocated using malloc
198
        if(buffer == NULL)
199
        {
200
                printf("Error! memory not allocated.");
201
                exit(0);
202
        }
203
        ch=fgetc(fp);
204
 
205
        while(!feof(fp)){
206
                val<<=8;
207
                val|=ch;
208
                cnt++;
209
                //printf("ch=%x\t",ch);
210
                if(cnt==width){
211
                        //printf("%d:%x\n",i,val);
212
                        buffer[i] = val;
213
                        val=0;
214
                        cnt=0;
215
                        i++;
216
                }
217
                ch=fgetc(fp);
218
        }
219
        if( cnt>0){
220
                val<<=(8 *(width-cnt));
221
                printf("%d:%x\n",i,val);
222
                buffer[i] = val;
223
 
224
        }
225
 
226
return buffer;
227
 
228
}
229
 
230
 
231
 
232
int send_data ()
233
{
234
        char * pch;
235
        char string[100];
236
        int bit=0,  inst=0, d=0;
237
        char out[100];
238
        pch = strtok (write_data,",");
239
        printf("%s\n",pch);
240
        while (pch != NULL)
241
        {
242
                while(1){
243
                         d=1;
244
                        if(sscanf( pch, "D:%d:%s", &bit, string )) break;
245
                        if(sscanf( pch, "d:%d:%s", &bit, string )) break;
246
                        //if(sscanf( pch, "D:%d:" PRIx64  , &bit, &data )) break;
247
                        //if(sscanf( pch, "d:%d:%016x", &bit, &data )) break;
248
                         d=2;
249
                        if(sscanf( pch, "R:%d:%s",&bit, string)) break;
250
                        if(sscanf( pch, "r:%d:%s",&bit, string)) break;
251
                         d=0;
252
                        if(sscanf( pch, "I:%d", &inst)) break;
253
                        if(sscanf( pch, "i:%d", &inst)) break;
254
                        printf("invalid format : %s\n",pch);
255
                        return -1;
256
 
257
                }
258
                if(d==1){
259
                        //printf ("(bit=%d, data=%s)",bit, string);
260
                        //jtag_vdr(bit, data, 0);
261
                        vdr_large(bit,string,0);
262 48 alirezamon
                }else if(d==2){
263 38 alirezamon
 
264
                        vdr_large(bit,string,out);
265
                        printf("###read data#%s###read data#\n",out);
266
                }else{
267
 
268
                        jtag_vir(inst);
269
                        //printf("%d\n",inst);
270
                }
271
 
272
                pch = strtok (NULL, ",");
273
 
274
        }
275
  return 0;
276
}
277
 
278
int compare_values( unsigned * val1, unsigned * val2, int words, unsigned int address){
279
 
280
        int i,error=0;
281
        for(i=0;i<words;i++){
282
                if (val1[i] != val2[i]) error=1;
283
        }
284
        if(error){
285
                 printf ("Error: missmatched at location %d. Expected 0X",address);
286
                 for(i=0;i<words;i++) printf("%08X",val1[i] );
287
                 printf (" but read 0X");
288
                 for(i=0;i<words;i++) printf("%08X",val2[i] );
289
                 printf ("\n");
290
 
291
        }
292
        return error;
293
 
294
 
295
}
296
 
297
void reorder_buffer(unsigned * buff, unsigned int words){
298
        unsigned tmp;
299
        unsigned int i;
300
        for(i=0;i<words/2;i++){
301
                tmp= buff[i];
302
                buff[i]=buff[i+words-1];
303
                buff[i+words-1]=tmp;
304
        }
305
}
306
 
307
 
308
 
309
 
310
int send_binary_file(){
311
        FILE *fp;
312
        int i=0;
313
        unsigned out;
314
        unsigned int file_size=0;
315
        unsigned int num=0;
316
        unsigned int mem_size;
317
        unsigned int memory_offset_in_word;
318
        unsigned * small_buff;
319
        int words= (BYTE_NUM % sizeof(unsigned )) ? (BYTE_NUM / sizeof(unsigned ) )+1 : (BYTE_NUM / sizeof(unsigned ));
320
 
321
        small_buff = (unsigned *) malloc(words * sizeof(unsigned ) );
322
        unsigned *  read_buff;
323
        read_buff  = (unsigned *) calloc(words , sizeof(unsigned ) );
324
 
325
 
326
        printf("send %s to the wishbone bus\n",binary_file_name);
327
        fp = fopen(binary_file_name,"rb");
328
        if (!fp) {
329
                fprintf (stderr,"Error: can not open %s file in read mode\n",binary_file_name);
330
                return -1;
331
        }
332
        unsigned * buffer;
333
        buffer=read_file (fp, &file_size);
334
        mem_size=memory_boundary-memory_offset;
335
        if(file_size>mem_size){
336
                printf("\n\n Warning:  %s file size (%x) is larger than the given memory size (%x). I will stop writing on end of memory address\n\n",binary_file_name,file_size,mem_size);
337
                file_size=mem_size;
338
        }
339
        fclose(fp);
340
        //disable the cpu
341
        jtag_vir(RD_WR_STATUS);
342
        jtag_vdr(BIT_NUM, 0x1, &out);
343
        //getchar();
344
        jtag_vir(UPDATE_WB_ADDR);
345
        // change memory sizes from byte to word        
346
        memory_offset_in_word=memory_offset /BYTE_NUM;
347
        //size of buffer
348
        num= (BYTE_NUM < sizeof(unsigned )) ? file_size /BYTE_NUM : file_size /sizeof(unsigned );
349
 
350
        jtag_vdr(BIT_NUM, memory_offset_in_word, 0);
351
        jtag_vir(UPDATE_WB_WR_DATA);
352
 
353
        printf ("start programing\n");
354
        //printf ("num=%d\n",num);
355
        for(i=0;i<num;i++){
356
                //printf("%d:%x\n",i,*buffer +i);
357
                if(BYTE_NUM <= sizeof(unsigned )){
358
                        jtag_vdr(BIT_NUM, buffer[i], 0);
359
                }else {
360
                        reorder_buffer(&buffer[i],words);
361
                        jtag_vdr_long(BIT_NUM, &buffer[i], 0, words);
362
                        i+= (words-1);
363
 
364
                }
365
 
366
 
367
        }
368
 
369
        //printf ("done programing\n");
370
        if(write_verify){
371
                if(!(fp = fopen(binary_file_name,"rb"))){
372
                        fprintf (stderr,"Error: can not open %s file in read mode\n",binary_file_name);
373
                        return -1;
374
                }
375
                buffer=read_file (fp, &file_size);
376
 
377
 
378
 
379
                //fclose(fp);
380
                jtag_vir(UPDATE_WB_RD_DATA);
381
                jtag_vdr(BIT_NUM, memory_offset_in_word+0, &out);
382
 
383
 
384
 
385
                if(BYTE_NUM <= sizeof(unsigned )){
386
                        for(i=1;i<=num; i++){
387
                                jtag_vdr(BIT_NUM, memory_offset_in_word+i, &out);
388
                                if(out!=buffer[i-1]) printf ("Error: missmatched at location %d. Expected %x but read %x\n",i-1,buffer[i-1], out);
389
                        }
390
                }
391
                else{
392
                        for(i=words;i<=num; i+=words){
393
                                read_buff[0]= memory_offset_in_word+i/words;
394
                                jtag_vdr_long(BIT_NUM, read_buff, small_buff, words);
395
                                reorder_buffer(&buffer[i-words],words);
396
                                compare_values(&buffer[i-words],small_buff,words,i/words-1);
397
 
398
                        }
399
 
400
                }
401
                printf ("write is verified\n");
402
 
403
        }
404
        //enable the cpu
405
        jtag_vir(RD_WR_STATUS);
406
        jtag_vdr(BIT_NUM, 0, &out);
407
        //printf ("status=%x\n",out);
408
        free(buffer);
409
        return 0;
410
}
411
 
412
void print_values( unsigned * val2, int words){
413
                 int i;
414
                 for(i=0;i<words;i++) printf("%08X",val2[words-i-1] );
415
                 printf ("\n");
416
}
417
 
418
 
419
 
420
int read_mem(){
421
        int i=0;
422
        unsigned int num=0;
423
        unsigned int mem_size;
424
        unsigned int memory_offset_in_word;
425
        unsigned out;
426
        unsigned * small_buff;
427
        int words= (BYTE_NUM % sizeof(unsigned )) ? (BYTE_NUM / sizeof(unsigned ) )+1 : (BYTE_NUM / sizeof(unsigned ));
428
 
429
        small_buff = (unsigned *) malloc(words * sizeof(unsigned ) );
430
        unsigned *  read_buff;
431
        read_buff  = (unsigned *) calloc(words , sizeof(unsigned ) );
432
        memory_offset_in_word=memory_offset /BYTE_NUM;
433
        mem_size=memory_boundary-memory_offset;
434
        num= (BYTE_NUM < sizeof(unsigned )) ? mem_size /BYTE_NUM : mem_size /sizeof(unsigned );
435
 
436
        jtag_vir(UPDATE_WB_RD_DATA);
437
        jtag_vdr(BIT_NUM, memory_offset_in_word+0, &out);
438
 
439
        printf("\n###read data#\n");
440
 
441
        if(BYTE_NUM <= sizeof(unsigned )){
442
                for(i=1;i<=num; i++){
443
                        jtag_vdr(BIT_NUM, memory_offset_in_word+i, &out);
444
                        printf("%X\n",out);
445
                }
446
        }
447
        else{
448
                for(i=words;i<=num; i+=words){
449
                        read_buff[0]= memory_offset_in_word+i/words;
450
                        jtag_vdr_long(BIT_NUM, read_buff, small_buff, words);
451
                        print_values(small_buff, words);
452
 
453
                }
454
 
455
        }
456
 
457
 
458
 
459
        printf("\n###read data#\n");
460
 
461
        //enable the cpu
462
        jtag_vir(RD_WR_STATUS);
463
        jtag_vdr(BIT_NUM, 0, &out);
464
        //printf ("status=%x\n",out);
465
        free(read_buff);
466
        return 0;
467
}
468
 
469
 
470
 
471
int vdr_large (unsigned sz, char * string, char *out){
472
        int words= (sz%32)? (sz/32)+1 : sz/32;
473
        unsigned  val[64],val_o[64];
474
        //printf("data=%s\n",string);
475
        hexcut(string, val, words );
476
 
477
 
478
        if( out == 0) return  jtag_vdr_long(sz,val,0,words);
479
        int u=  jtag_vdr_long(sz,val,val_o,words);
480
 
481
        hexgen( out, val_o, words );
482
 
483
        return u;
484
 
485
}
486
 
487
 
488
 
489
int hexcut( char * hexstring, unsigned * val, int words ){
490
    size_t count = 0;
491
    int start;
492
 
493
    if (*(hexstring+1)=='x' || *(hexstring+1)=='X') hexstring+=2;
494
    int size=strlen(hexstring);
495
    int hexnum= (size%8)? (size/8)+1 : size/8;
496
    for(count = 0; count < words; count++) val[count]=0;
497
 
498
    for(count = 1; count <= hexnum; count++) {
499
        start=(count*8>size)? 0 : size-count*8;
500
 
501
        sscanf(hexstring+start, "%08x", &val[count-1]);
502
        *(hexstring+start)=0;
503
    }
504
 
505
  //  printf("size=%d, hexnum=%u\n",size,hexnum);
506
 
507
 
508
    return hexnum;
509
}
510
 
511
 
512
void hexgen( char * hexstring, unsigned * val, int words ){
513
    size_t count = 0;
514
    sprintf(hexstring,"0x");
515
    for(count = 0; count < words; count++) {
516
        if(count == 0)  sprintf((hexstring+2),"%x",val[words-count-1]);
517
        else            sprintf(hexstring,"%08x",val[words-count-1]);
518
         hexstring+=strlen(hexstring);
519
   }
520
 
521
 // return hexnum;
522
}
523
 
524
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.