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 45

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 45 alirezamon
        printf("cpu is disabled.\n");
318 38 alirezamon
 
319
        // change memory sizes from byte to word        
320
        memory_offset_in_word=memory_offset /BYTE_NUM;
321
        //size of buffer
322
        num= (BYTE_NUM < sizeof(unsigned )) ? file_size /BYTE_NUM : file_size /sizeof(unsigned );
323
 
324
        jtag_vdr(BIT_NUM, memory_offset_in_word, 0);
325
        jtag_vir(UPDATE_WB_WR_DATA);
326
 
327
        printf ("start programing\n");
328 45 alirezamon
        printf ("Will send %d values to memory\n",num);
329 38 alirezamon
        for(i=0;i<num;i++){
330
                //printf("%d:%x\n",i,buffer[i]);
331
 
332
                if(BYTE_NUM <= sizeof(unsigned )){
333
                        //printf("%d:%x\n",i,buffer[i]);
334
                        jtag_vdr(BIT_NUM, buffer[i], 0);
335
                }else {
336
                        //printf("%d:%x\n",i,buffer[i]);
337
                        reorder_buffer(&buffer[i],words);
338
                        jtag_vdr_long(BIT_NUM, &buffer[i], 0, words);
339
                        i+= (words-1);
340
 
341
                }
342
        }
343
 
344 45 alirezamon
        printf ("done programing\n");
345 38 alirezamon
        if(write_verify){
346
                if(!(fp = fopen(binary_file_name,"rb"))){
347
                        fprintf (stderr,"Error: can not open %s file in read mode\n",binary_file_name);
348
                        return -1;
349
                }
350
                buffer=read_file (fp, &file_size);
351
 
352
 
353
 
354
                //fclose(fp);
355
                jtag_vir(UPDATE_WB_RD_DATA);
356
                jtag_vdr(BIT_NUM,memory_offset_in_word+0, &out);
357
                jtag_vdr(BIT_NUM,memory_offset_in_word+1, &out);
358
 
359
 
360
                if(BYTE_NUM <= sizeof(unsigned )){
361
                        //printf("vdr\n");
362
                        for(i=2;i<=num; i++){
363
                                jtag_vdr(BIT_NUM, memory_offset_in_word+i, &out);
364
                                if(out!=buffer[i-2]) printf ("Error: missmatched at location %d. Expected %x but read %x\n",i-2,buffer[i-2], out);
365
                        }
366
                        jtag_vdr(BIT_NUM, 0, &out);
367 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);
368 38 alirezamon
                        jtag_vdr(BIT_NUM, 1, &out);
369 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);
370 38 alirezamon
 
371
                }
372
                else{
373
                        //printf("vdr_long\n");
374
                        for(i=2*words;i<=num; i+=words){
375
                                read_buff[0]= memory_offset_in_word+i/words;
376
                                jtag_vdr_long(BIT_NUM, read_buff, small_buff, words);
377
                                reorder_buffer(&buffer[i-2*words],words);
378
                                compare_values(&buffer[i-2*words],small_buff,words,i/words);
379
 
380
                        }
381
 
382
                }
383
                printf ("write is verified\n");
384
 
385
        }
386
        //enable the cpu
387
        jtag_vir(RD_WR_STATUS);
388
        jtag_vdr(BIT_NUM, 0, &out);
389
        //printf ("status=%x\n",out);
390
        free(buffer);
391
        return 0;
392
}
393
 
394
 
395
int read_mem(){
396
        int i=0;
397
        unsigned int num=0;
398
        unsigned int mem_size;
399
        unsigned int memory_offset_in_word;
400
        unsigned out;
401
        unsigned * small_buff;
402
        int words= (BYTE_NUM % sizeof(unsigned )) ? (BYTE_NUM / sizeof(unsigned ) )+1 : (BYTE_NUM / sizeof(unsigned ));
403
 
404
        small_buff = (unsigned *) malloc(words * sizeof(unsigned ) );
405
        unsigned *  read_buff;
406
        read_buff  = (unsigned *) calloc(words , sizeof(unsigned ) );
407
        memory_offset_in_word=memory_offset /BYTE_NUM;
408
        mem_size=memory_boundary-memory_offset;
409
        num= (BYTE_NUM < sizeof(unsigned )) ? mem_size /BYTE_NUM : mem_size /sizeof(unsigned );
410
 
411
        jtag_vir(UPDATE_WB_RD_DATA);
412
        jtag_vdr(BIT_NUM, memory_offset_in_word+0, &out);
413
        jtag_vdr(BIT_NUM, memory_offset_in_word+1, &out);
414
 
415
        printf("\n###read data#\n");
416
 
417
        if(BYTE_NUM <= sizeof(unsigned )){
418
                        //printf("vdr\n");
419
                        for(i=2;i<=num; i++){
420
                                jtag_vdr(BIT_NUM, memory_offset_in_word+i, &out);
421
                                printf("%X\n",out);
422
                        }
423
                        jtag_vdr(BIT_NUM, 0, &out);
424
                        printf("%X\n",out);
425
 
426
                        jtag_vdr(BIT_NUM, 1, &out);
427
                        printf("%X\n",out);
428
 
429
 
430
                }
431
                else{
432
                        //printf("vdr_long\n");
433
                        for(i=2*words;i<=num+2; i+=words){
434
                                //printf("%d,%d,%d\n",i,words,num);
435
                                read_buff[0]= memory_offset_in_word+i/words;
436
                                jtag_vdr_long(BIT_NUM, read_buff, small_buff, words);
437
                                print_values(small_buff, words);
438
 
439
                        }
440
 
441
        }
442
        printf("\n###read data#\n");
443
 
444
        //enable the cpu
445
        jtag_vir(RD_WR_STATUS);
446
        jtag_vdr(BIT_NUM, 0, &out);
447
        //printf ("status=%x\n",out);
448
        free(read_buff);
449
        return 0;
450
}
451
 
452
 
453
 
454
void vdr_large (unsigned sz, char * string, char *out){
455
        int words= (sz%32)? (sz/32)+1 : sz/32;
456
        unsigned  val[64],val_o[64];
457
        //printf("data=%s\t",string);
458
        hexcut(string, val, words );
459
 
460
 
461
        if( out == 0) {
462
                  jtag_vdr_long(sz,val,0,words);
463
                return;
464
        }
465
        jtag_vdr_long(sz,val,val_o,words);
466
        //printf("rdata=%s\n",out);
467
        hexgen( out, val_o, words );
468
 
469
 
470
 
471
}
472
 
473
 
474
 
475
 
476
 
477
 

powered by: WebSVN 2.1.0

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