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_xilinx_xsct/] [jtag_xilinx_xsct-old.c] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 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
 
11
 
12
 
13
/* functions */
14
int send_binary_file();
15
int read_mem();
16
void usage();
17
void processArgs (int , char** );
18
int send_data ();
19
int hexcut( char *  , unsigned *  , int  );
20
void vdr_large (unsigned , char * , char *);
21
void hexgen( char * , unsigned *, int );
22
 
23
int main(int argc, char **argv) {
24
 
25
        processArgs (argc, argv );
26
        WORDS_NUM= (BYTE_NUM % sizeof(unsigned )) ? (BYTE_NUM / sizeof(unsigned ) )+1 : (BYTE_NUM / sizeof(unsigned ));
27
        printf("Initial Vjtag for index num=%u target num=%u and shift-reg size %u.\n",index_num,jtag_target_number,jtag_shift_reg_size);
28
        if (jtag_init()){
29
                fprintf (stderr, "Error opening jtag IP with %d index num\n",index_num);
30
                return -1;
31
        }
32
        jtag_vindex(index_num);
33
 
34
        //printf("jtag is initilized\n");
35
        if (enable_binary_send | enable_binary_verify) {
36
                if( send_binary_file() == -1) return -1;
37
        }
38
 
39
        if  (enable_binary_read){
40
                if( read_mem() == -1) return -1;
41
 
42
        }
43
 
44
        if (write_data!=0){
45
                printf("send %s to jtag\n",write_data);
46
                send_data();
47
 
48
 
49
        }
50
 
51
        return 0;
52
}
53
 
54
 
55
 
56
void usage(){
57
 
58
        printf ("usage:./jtag_main [-n  index number] -a jtag_target_number [-b jtag_shift_reg_size] [-i file_name][-c][-s rd/wr offset address][-d string] [-t Jtag_chain number]\n");
59
        printf ("\t-a   the order number of target device in jtag chain. Run jtag targets after \"connect\" command in xsct terminal to list all availble targets\n");
60
        printf ("\t-b   Jtag shiftreg data width. It should be the target device Data width + 4\n");
61
        printf ("\t-t   Jtag_chain number: the BSCANE2 tab number :1,2,3 or 4. The default is 4\n");
62
        printf ("\t-n   index number: the target jtag IP core index number. The default number is 126\n");
63
        printf ("\t-i   file_name:  input binary file name (.bin file)\n");
64
        printf ("\t-r   read memory content and display in terminal\n");
65
        printf ("\t-v   read memory content and verify with input binary file\n");
66
        printf ("\t-w   bin file word width in byte. default is 4 bytes (32 bits)\n");
67
        printf ("\t-c   verify after write\n");
68
        printf ("\t-s   memory wr/rd offset address in byte (hex format). The default value is 0x0000000\n");
69
        printf ("\t-e   memory  boundary address in byte (hex format).  The default value is 0xFFFFFFFF\n");
70
        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");
71
 
72
}
73
 
74
void processArgs (int argc, char **argv )
75
{
76
   char c;
77
int p;
78
 
79
        /* don't want getopt to moan - I can do that just fine thanks! */
80
        opterr = 0;
81
        if (argc < 2)  usage();
82
        while ((c = getopt (argc, argv, "s:e:d:n:t:i:v:w:a:b:cr")) != -1)
83
        {
84
           switch (c)
85
            {
86
            case 'a':   /* hardware_name */
87
               jtag_target_number = atoi(optarg);
88
               break;
89
 
90
            case 'b':   /* device number in chain */
91
               jtag_shift_reg_size = atoi(optarg);
92
               break;
93
 
94
            case 't':   /* Jtag_chain_num */
95
               chain_num = atoi(optarg);
96
               if (chain_num<1 || chain_num>4 ) {
97
                   fprintf (stderr, "Wrong jtag_chain_num the given %u value is out of valid range 1,2,3 or 4.\n\n", chain_num);
98
                   usage();
99
               }
100
               break;
101
 
102
            case 'n':   /* index number */
103
               index_num = atoi(optarg);
104
               break;
105
 
106
            case 'i':   /* input binary file name */
107
                binary_file_name = optarg;
108
                enable_binary_send=1;
109
                break;
110
 
111
            case 'v':   /* input binary file name */
112
                        binary_file_name = optarg;
113
                        enable_binary_verify=1;
114
                        write_verify= 1;
115
                        break;
116
 
117
 
118
            case 'r':   /* read memory */
119
                enable_binary_read=1;
120
                break;
121
 
122
            case 'w':   /* word width in byte */
123
                word_width= atoi(optarg);
124
                break;
125
 
126
            case 'c':   /* enable write verify */
127
                write_verify= 1;
128
                break;
129
 
130
            case 'd':   /* send string */
131
                write_data= optarg;
132
                break;
133
 
134
            case 's':   /* set offset address*/
135
 
136
                p=sscanf(optarg,"%x",&memory_offset);
137
                if( p==0){
138
                         fprintf (stderr, "invalid memory offset adress format `%s'.\n", optarg);
139
                         usage();
140
                         exit(1);
141
                }
142
                //printf("p=%d,memory_offset=%x\n",p,memory_offset);            
143
                break;
144
            case 'e':   /* wmemory  boundary address */
145
                p=sscanf(optarg,"%x",&memory_boundary);
146
                if( p==0){
147
                         fprintf (stderr, "invalid memory boundary adress format `%s'.\n", optarg);
148
                         usage();
149
                         exit(1);
150
                }
151
                break;
152
 
153
            case '?':
154
               if (isprint (optopt))
155
                  fprintf (stderr, "Unknown option `-%c'.\n", optopt);
156
               else
157
                  fprintf (stderr,
158
                           "Unknown option character `\\x%x'.\n",
159
                           optopt);
160
            default:
161
               usage();
162
               exit(1);
163
            }
164
      }
165
}
166
 
167
unsigned * read_file (FILE * fp, unsigned int  * n ){
168
 
169
        unsigned * buffer;
170
        unsigned val;
171
        unsigned char ch;
172
        unsigned int i=0;
173
        char cnt=0;
174
        unsigned int num=0;
175
        unsigned int width= (BYTE_NUM < sizeof(unsigned )) ? BYTE_NUM :  sizeof(unsigned ); //max is 4 then
176
        fseek(fp, 0, SEEK_END); // seek to end of file
177
        num = ftell(fp); // get current file pointer
178
        *n=num;// number of bytes from the beginning of the file
179
 
180
 
181
 
182
 
183
        num=(num/width)+2;
184
        fseek(fp, 0, SEEK_SET);
185
        //printf ("num=%u\n",num);      
186
        buffer = (unsigned *) malloc(num * sizeof(unsigned ) );  //memory allocated using malloc
187
        if(buffer == NULL)
188
        {
189
                printf("Error! memory not allocated.");
190
                exit(0);
191
        }
192
        ch=fgetc(fp);
193
 
194
        while(!feof(fp)){
195
                val<<=8;
196
                val|=ch;
197
                cnt++;
198
                //printf("ch=%x\t",ch);
199
                if(cnt==width){
200
                        //printf("%d:%x\n",i,val);
201
                        buffer[i] = val;
202
                        val=0;
203
                        cnt=0;
204
                        i++;
205
                }
206
                ch=fgetc(fp);
207
        }
208
        if( cnt>0){
209
                val<<=(8 *(width-cnt));
210
                printf("%d:%x\n",i,val);
211
                buffer[i] = val;
212
 
213
        }
214
 
215
return buffer;
216
 
217
}
218
 
219
 
220
 
221
int send_data ()
222
{
223
        char * pch;
224
        char string[100];
225
        int bit=0,  inst=0, d=0;
226
        char out[100];
227
        pch = strtok (write_data,",");
228
 
229
        while (pch != NULL)
230
        {
231
                //printf("pch=%s\n",pch);
232
                while(1){
233
                         d=1;
234
                        if(sscanf( pch, "D:%d:%s", &bit, string )) break;
235
                        if(sscanf( pch, "d:%d:%s", &bit, string )) break;
236
                        //if(sscanf( pch, "D:%d:" PRIx64  , &bit, &data )) break;
237
                        //if(sscanf( pch, "d:%d:%016x", &bit, &data )) break;
238
                         d=2;
239
                        if(sscanf( pch, "R:%d:%s",&bit, string)) break;
240
                        if(sscanf( pch, "r:%d:%s",&bit, string)) break;
241
                         d=0;
242
                        if(sscanf( pch, "I:%d", &inst)) break;
243
                        if(sscanf( pch, "i:%d", &inst)) break;
244
                        printf("invalid format : %s\n",pch);
245
                        return -1;
246
 
247
                }
248
                if(d==1){
249
                        //printf ("(bit=%d, data=%s)\n",bit, string);
250
                        //jtag_vdr(bit, data, 0);
251
                        vdr_large(bit,string,0);
252
 
253
                }else if(d==2){
254
                        vdr_large(bit,string,out);
255
                        vdr_large(bit,string,out);
256
                        vdr_large(bit,string,out);
257
                        printf("###read data#%s###read data#\n",out);
258
                }else{
259
 
260
                        //printf("I=%d\n",inst);                                
261
                        jtag_vir(inst);
262
 
263
                }
264
 
265
                pch = strtok (NULL, ",");
266
 
267
        }
268
  return 0;
269
}
270
 
271
int compare_values( unsigned * val1, unsigned * val2, int words, unsigned int address){
272
 
273
        int i,error=0;
274
        for(i=0;i<words;i++){
275
                if (val1[i] != val2[i]) error=1;
276
        }
277
        if(error){
278
                 printf ("Error: missmatched at location %d. Expected 0X",address);
279
                 for(i=0;i<words;i++) printf("%08X",val1[i] );
280
                 printf (" but read 0X");
281
                 for(i=0;i<words;i++) printf("%08X",val2[i] );
282
                 printf ("\n");
283
 
284
        }
285
        return error;
286
 
287
 
288
}
289
 
290
void print_values( unsigned * val2, int words){
291
                 int i;
292
                 for(i=0;i<words;i++) printf("%08X",val2[words-i-1] );
293
                 printf ("\n");
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
void read_multi_sequence (unsigned * buffer, unsigned int num, unsigned int memory_offset_in_word ) {
309
        int i;
310
        jseq_multi_init ();
311
 
312
        for(i=2;i<num; i++){
313
                jtag_vdr_multi_capture(BIT_NUM, memory_offset_in_word+i);
314
                        //if(out!=buffer[i-2]) printf ("Error: missmatched at location %d. Expected %x but read %x\n",i-2,buffer[i-2], out);
315
        }
316
                jtag_vdr_multi_capture(BIT_NUM, 0);
317
                //if(out!=buffer[i-2]) printf ("Error: missmatched at location %d. Expected %x but read %x\n",i-2,buffer[i-2], out);
318
                jtag_vdr_multi_capture(BIT_NUM, 1 );
319
                //if(out!=buffer[i-1]) printf ("Error: missmatched at location %d. Expected %x but read %x\n",i-1,buffer[i-1], out);
320
                jseq_multi_get_results();
321
                verify_memory_multi (buffer, num);
322
}
323
 
324
 
325
 
326
 
327
 
328
int send_binary_file(){
329
        FILE *fp;
330
        int i=0;
331
        unsigned out;
332
        unsigned int file_size=0;
333
        unsigned int num=0;
334
        unsigned int mem_size;
335
        unsigned int memory_offset_in_word;
336
        unsigned * small_buff;
337
        int words= (BYTE_NUM % sizeof(unsigned )) ? (BYTE_NUM / sizeof(unsigned ) )+1 : (BYTE_NUM / sizeof(unsigned ));
338
 
339
        small_buff = (unsigned *) malloc(words * sizeof(unsigned ) );
340
        unsigned *  read_buff;
341
        read_buff  = (unsigned *) calloc(words , sizeof(unsigned ) );
342
 
343
 
344
 
345
        fp = fopen(binary_file_name,"rb");
346
        if (!fp) {
347
                fprintf (stderr,"Error: can not open %s file in read mode\n",binary_file_name);
348
                return -1;
349
        }
350
        unsigned * buffer;
351
        buffer=read_file (fp, &file_size);
352
        mem_size=memory_boundary-memory_offset;
353
        if(file_size>mem_size){
354
                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);
355
                file_size=mem_size;
356
        }
357
        fclose(fp);
358
 
359
        //disable the cpu
360
        jtag_vir(RD_WR_STATUS);
361
        jtag_vdr(BIT_NUM, 0x1, &out);
362
 
363
 
364
        //printf("cpu is disabled.\n");
365
 
366
        // change memory sizes from byte to word        
367
        memory_offset_in_word=memory_offset /BYTE_NUM;
368
        //size of buffer
369
        num= (BYTE_NUM < sizeof(unsigned )) ? file_size /BYTE_NUM : file_size /sizeof(unsigned );
370
 
371
 
372
 
373
        if(enable_binary_send){
374
                jtag_vir(UPDATE_WB_ADDR);
375
                jtag_vdr(BIT_NUM, memory_offset_in_word, 0);
376
                jtag_vir(UPDATE_WB_WR_DATA);
377
                printf("send %s to the wishbone bus\n",binary_file_name);
378
                printf ("start programming. Will send %d values to memory\n",num);
379
                jseq_multi_init ();
380
                for(i=0;i<num;i++){
381
                        //printf("%d:%x\n",i,buffer[i]);
382
 
383
                        if(BYTE_NUM <= sizeof(unsigned )){
384
                                //printf("%d:%x\n",i,buffer[i]);
385
                                jtag_vdr_multi(BIT_NUM, buffer[i]);
386
                        }else {
387
                                //printf("%d:%x\n",i,buffer[i]);
388
                                reorder_buffer(&buffer[i],words);
389
                                jtag_vdr_long_multi(BIT_NUM, &buffer[i],  words);
390
                                i+= (words-1);
391
 
392
                        }
393
                }
394
                jseq_multi_end ();
395
                printf ("Write is done\n");
396
        }
397
 
398
        if(write_verify){
399
                if(!(fp = fopen(binary_file_name,"rb"))){
400
                        fprintf (stderr,"Error: can not open %s file in read mode\n",binary_file_name);
401
                        return -1;
402
                }
403
                buffer=read_file (fp, &file_size);
404
 
405
                //fclose(fp);
406
                jtag_vir(UPDATE_WB_RD_DATA);
407
                jtag_vdr(BIT_NUM,memory_offset_in_word+0, &out);
408
                jtag_vdr(BIT_NUM,memory_offset_in_word+1, &out);
409
 
410
 
411
                //create jseq for all memory
412
                if(BYTE_NUM <= sizeof(unsigned )){
413
 
414
                        read_multi_sequence ( buffer,  num,  memory_offset_in_word );
415
 
416
                }
417
                else{
418
                        //printf("vdr_long\n");
419
                        for(i=2*words;i<num; i+=words){
420
                                read_buff[0]= memory_offset_in_word+i/words;
421
                                jtag_vdr_long(BIT_NUM, read_buff, small_buff, words);
422
                                reorder_buffer(&buffer[i-2*words],words);
423
                                compare_values(&buffer[i-2*words],small_buff,words,i/words);
424
 
425
                        }
426
 
427
                }
428
 
429
 
430
 
431
 
432
 
433
/*
434
 
435
                if(BYTE_NUM <= sizeof(unsigned )){
436
                        //printf("vdr\n");
437
                        for(i=2;i<num; i++){
438
                                jtag_vdr(BIT_NUM, memory_offset_in_word+i, &out);
439
                                if(out!=buffer[i-2]) printf ("Error: missmatched at location %d. Expected %x but read %x\n",i-2,buffer[i-2], out);
440
                        }
441
                        jtag_vdr(BIT_NUM, 0, &out);
442
                        if(out!=buffer[i-2]) printf ("Error: missmatched at location %d. Expected %x but read %x\n",i-2,buffer[i-2], out);
443
                        jtag_vdr(BIT_NUM, 1, &out);
444
                        if(out!=buffer[i-1]) printf ("Error: missmatched at location %d. Expected %x but read %x\n",i-1,buffer[i-1], out);
445
 
446
                }
447
                else{
448
                        //printf("vdr_long\n");
449
                        for(i=2*words;i<num; i+=words){
450
                                read_buff[0]= memory_offset_in_word+i/words;
451
                                jtag_vdr_long(BIT_NUM, read_buff, small_buff, words);
452
                                reorder_buffer(&buffer[i-2*words],words);
453
                                compare_values(&buffer[i-2*words],small_buff,words,i/words);
454
 
455
                        }
456
 
457
                }
458
 
459
*/
460
 
461
                //check miss matched location
462
                if(miss == 0){
463
                        printf ("Memory content is verified. There was no difference!\n");
464
                }
465
                else if(miss<=MISS_RETRY_NUM && enable_binary_send==1){
466
                        printf ("Try to write miss matched values\n");
467
                        for(i=0; i<miss; i++){
468
 
469
                                int loc = mis_addr[i];
470
                                write_specefic_loc (loc+memory_offset_in_word,buffer[loc]);
471
                        }
472
                        jtag_vir(UPDATE_WB_RD_DATA);
473
                        jtag_vdr(BIT_NUM,memory_offset_in_word+0, &out);
474
                        jtag_vdr(BIT_NUM,memory_offset_in_word+1, &out);
475
                        read_multi_sequence ( buffer,  num,  memory_offset_in_word );
476
                        if(miss != 0){
477
                                                printf ("Error: write verification is failed!\n");
478
                        }
479
 
480
                }else{
481
                        printf ("Error: verification is failed!\n");
482
                }
483
 
484
 
485
 
486
 
487
        }
488
        //enable the cpu
489
        jtag_vir(RD_WR_STATUS);
490
        jtag_vdr(BIT_NUM, 0, &out);
491
        //printf ("status=%x\n",out);
492
        free(buffer);
493
        return 0;
494
}
495
 
496
 
497
int read_mem(){
498
        int i=0;
499
        unsigned int num=0;
500
        unsigned int mem_size;
501
        unsigned int memory_offset_in_word;
502
        unsigned out;
503
        unsigned * small_buff;
504
        int words= (BYTE_NUM % sizeof(unsigned )) ? (BYTE_NUM / sizeof(unsigned ) )+1 : (BYTE_NUM / sizeof(unsigned ));
505
 
506
        small_buff = (unsigned *) malloc(words * sizeof(unsigned ) );
507
        unsigned *  read_buff;
508
        read_buff  = (unsigned *) calloc(words , sizeof(unsigned ) );
509
        memory_offset_in_word=memory_offset /BYTE_NUM;
510
        mem_size=memory_boundary-memory_offset;
511
        num= (BYTE_NUM < sizeof(unsigned )) ? mem_size /BYTE_NUM : mem_size /sizeof(unsigned );
512
 
513
        jtag_vir(UPDATE_WB_RD_DATA);
514
        jtag_vdr(BIT_NUM, memory_offset_in_word+0, &out);
515
        jtag_vdr(BIT_NUM, memory_offset_in_word+1, &out);
516
 
517
        printf("\n###read data#\n");
518
 
519
        if(BYTE_NUM <= sizeof(unsigned )){
520
                        //printf("vdr\n");
521
                        for(i=2;i<num; i++){
522
                                jtag_vdr(BIT_NUM, memory_offset_in_word+i, &out);
523
                                printf("%X\n",out);
524
                        }
525
                        jtag_vdr(BIT_NUM, 0, &out);
526
                        printf("%X\n",out);
527
 
528
                        jtag_vdr(BIT_NUM, 1, &out);
529
                        printf("%X\n",out);
530
 
531
 
532
                }
533
                else{
534
                        //printf("vdr_long\n");
535
                        for(i=2*words;i<num+2; i+=words){
536
                                //printf("%d,%d,%d\n",i,words,num);
537
                                read_buff[0]= memory_offset_in_word+i/words;
538
                                jtag_vdr_long(BIT_NUM, read_buff, small_buff, words);
539
                                print_values(small_buff, words);
540
 
541
                        }
542
 
543
        }
544
        printf("\n###read data#\n");
545
 
546
        //enable the cpu
547
        jtag_vir(RD_WR_STATUS);
548
        jtag_vdr(BIT_NUM, 0, &out);
549
        //printf ("status=%x\n",out);
550
        free(read_buff);
551
        return 0;
552
}
553
 
554
 
555
 
556
void vdr_large (unsigned sz, char * string, char *out){
557
        int words= (sz%32)? (sz/32)+1 : sz/32;
558
        unsigned  val[64],val_o[64];
559
        //printf("data=%s\t",string);
560
        hexcut(string, val, words );
561
 
562
 
563
        if( out == 0) {
564
                  jtag_vdr_long(sz,val,0,words);
565
                return;
566
        }
567
        jtag_vdr_long(sz,val,val_o,words);
568
        //printf("rdata=%s\n",out);
569
        hexgen( out, val_o, words );
570
 
571
 
572
 
573
}
574
 
575
 
576
 
577
 
578
 
579
 

powered by: WebSVN 2.1.0

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