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_quartus_stp/] [jtag_quartus_stp.c] - Blame information for rev 38

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

powered by: WebSVN 2.1.0

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