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 43

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

powered by: WebSVN 2.1.0

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