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 48

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

powered by: WebSVN 2.1.0

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