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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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