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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 alirezamon
/* A library of routines that will talk to a design using
2
 * Altera's virtual_jtag interface.
3
 * The design must contain a communications layer like the
4
 * one that tmjportmux_gen creates.
5
 */
6
 
7
#include <sys/types.h>
8
#include <sys/stat.h>
9
#include <stdlib.h>
10
#include <stdio.h>
11
#include <string.h>
12
#include "jtag.h"
13
#include "pipe.c"
14
 
15
 
16
 
17
//#define DEBUG_JTAG
18
//#define PRINT_TO_XSCT
19
 
20
 
21
 
22
 
23
FILE *to_xsct, *from_xsct;
24
 
25
#ifdef PRINT_TO_XSCT
26
FILE *to_xsct_file;
27
#endif
28
 
29
 
30
int hexcut( char * hexstring, unsigned * val, int words ){
31
    size_t count = 0;
32
    int start;
33
    char word[8];
34
 
35
    if (*(hexstring+1)=='x' || *(hexstring+1)=='X') hexstring+=2;
36
    int size=strlen(hexstring);
37
    int hexnum= (size%8)? (size/8)+1 : size/8;
38
    for(count = 0; count < words; count++) val[count]=0;
39
    //printf("hexstring=%s\n",hexstring);       
40
    for(count = 1; count <= hexnum; count++) {
41
        start=(count*8>size)? 0 : size-(count*8);
42
        //start=0;
43
        //ptr=hexstring+start;
44
        strncpy( word, hexstring+start,8);
45
        //printf("** %s\n,",word);        
46
        sscanf(word, "%08x", &val[count-1]);
47
       // *(hexstring+start)=0;
48
        //printf("%x,",val[count-1]);
49
    }
50
 
51
   //printf("\nsize=%d, hexnum=%u\n",size,hexnum);
52
 
53
 
54
    return hexnum;
55
}
56
 
57
 
58
void hexgen( char * hexstring, unsigned * val, int words ){
59
    size_t count = 0;
60
    sprintf(hexstring,"0x");
61
    for(count = 0; count < words; count++) {
62
        if(count == 0)  sprintf((hexstring+2),"%x",val[words-count-1]);
63
        else            sprintf(hexstring,"%08x",val[words-count-1]);
64
         hexstring+=strlen(hexstring);
65
   }
66
 
67
 // return hexnum;
68
}
69
 
70
 
71
void jtag_reorder ( char * string_in, char *string_out ) {
72
 
73
  int size = strlen(string_in);
74
  int i;
75
  for (i=0;i<size;i+=2){
76
         string_out[i]=   string_in[size-i-2];
77
         string_out[i+1]= string_in[size-i-1];
78
  }
79
   string_out[i]=0;
80
}
81
 
82
 
83
unsigned char h_to_i ( unsigned char in){
84
 
85
        if (in >= '0' && in <= '9') {
86
          return (in - '0');
87
 
88
        }
89
     if (in >= 'A' && in <= 'F') {
90
            return  (in - 'A' + 10);
91
 
92
        }
93
     return in - 'a' + 10;
94
 }
95
 
96
void add_update_state (char * hexstring, unsigned char update_code,unsigned sz ){
97
        if(sz%4 == 0){
98
                sprintf (&hexstring[sz/4],"%x", update_code);
99
                return;
100
        }
101
 
102
        unsigned char s1 = sz%4;
103
        unsigned char s2 = 4-s1;
104
        unsigned char u1 = update_code>>s2;
105
        unsigned char u2 = update_code<<s1 & 0xf;
106
        unsigned char m  = (s1==1)? 0x01 : (s1==2)? 0x3 :  (s1==3)?  0x7: 0xf;
107
        unsigned char u3 =  h_to_i(hexstring[(sz/4)-1]) & m;
108
        u2 = u2 | u3;
109
        char tmp[2];
110
        sprintf (tmp,"%x", u1);
111
        hexstring[(sz/4)]=(unsigned char)tmp[0];
112
        sprintf (tmp, "%x",u2);
113
        hexstring[(sz/4)-1]=(unsigned char)tmp[0];
114
 
115
}
116
 
117
 
118
void hextostring_xsct( char * hexstring, unsigned * val, int words,unsigned sz){
119
    size_t count = 0;
120
 
121
    char tmp[100];
122
    char zeros[100];
123
    char *pointer = tmp;
124
 
125
    //sprintf(hexstring,"0x");
126
 
127
    for(count = 0; count < words; count++) {
128
        if(count == 0)  sprintf(pointer,"%x",val[words-count-1]);
129
        else            sprintf(pointer,"%08x",val[words-count-1]);
130
        pointer+=strlen(pointer);
131
   }
132
   unsigned o=sz/8;
133
 
134
   sz = (sz%8)? (o+1)*8:sz;// size needed to be multiplaction of 8 in xsct
135
   int digits= sz/4 ;
136
   char * buff;
137
   buff = (char *) malloc(sz+1);
138
 
139
   //printf("%d > %d", digits , strlen(tmp));
140
   if (digits > strlen(tmp)){
141
        for(count = 0; count < digits-strlen(tmp); count++) {
142
                zeros[count]='0';
143
         }
144
        zeros[count]=0;
145
        strcat(zeros,tmp);
146
        sprintf(buff,"%s",zeros);
147
 
148
   }else{
149
        sprintf(buff,"%s",tmp);
150
 
151
   }
152
   jtag_reorder(buff,hexstring);
153
}
154
 
155
 
156
 
157
//char end_tcl [10] = { 0x1b, 0x5b,[2]=30,[3]=6d,[4]=a,
158
 
159
char* remove_color_code_from_string ( char *buf, int z){
160
        int i=0;
161
        char * ptr=buf;
162
        if( *ptr != 0x1b ) return ptr;
163
        do{
164
                ptr ++;
165
                i++;
166
        } while ((*ptr != 'm') && (*ptr != 0x0) && i<z-1);
167
        ptr ++;
168
        return ptr;
169
}
170
 
171
 
172
 
173
void check_error_xsct_out(char * buf){
174
        char * err_message[] = {"ERROR","error:","target list is empty","invalid","no target(s) with id:","can't read",NULL};
175
        int i=0;
176
        //printf("buff:%s\n",buf);      
177
        while (err_message[i] !=NULL){
178
                if(strstr(buf, err_message[i]) != NULL) {
179
                        printf("\tERROR\n");
180
                        printf("'%s'\n", buf);
181
                        exit(1);
182
                }
183
                i++;
184
        }
185
}
186
 
187
 
188
 
189
void wait_for_pipe_output(char* buf,  const char * out){
190
 
191
 while(1) {
192
      //printf("check error\n"); 
193
      fflush(to_xsct);
194
      fgets(buf,200, from_xsct);
195
      check_error_xsct_out(buf);
196
      //printf("b=%s\n",buf);
197
 
198
      if(strstr(buf, out) != NULL) return;
199
     // if(!strcmp(ptr, "\n")) break;
200
      if(feof(from_xsct)) {
201
         fprintf(stderr, "saw eof from xsct\n");
202
         exit(1);
203
      }
204
      if(ferror(from_xsct)) {
205
         fprintf(stderr, "saw error from xsct\n");
206
         exit(1);
207
      }
208
 
209
   }
210
 
211
 
212
}
213
 
214
 
215
 
216
int jtag_init( ) {
217
 
218
//update chain code value
219
  chain_code=
220
                (chain_num==1)? 0x02:
221
                (chain_num==2)? 0x03:
222
                (chain_num==3)? 0x22:
223
                0x23;
224
 
225
 
226
#ifdef PRINT_TO_XSCT
227
        to_xsct_file = fopen("to_xsct.txt", "a");
228
        if (to_xsct_file == NULL) {
229
                printf("Error!");
230
                exit(1);
231
        }
232
#endif
233
 
234
 
235
   /* Create a xsct process, and connect t jtag device */
236
 
237
   int  f_to_xsct, f_from_xsct;
238
   char buf[1024];
239
  // char * ptr;
240
   char *command[] = {"xsdb", "-interactive", 0};
241
 
242
   if(from_xsct != (FILE *) NULL) {
243
      fclose(from_xsct);
244
      fclose(to_xsct);
245
   }
246
 
247
   piped_child(command, &f_from_xsct, &f_to_xsct);
248
 
249
   from_xsct = fdopen(f_from_xsct, "r");
250
   to_xsct = fdopen(f_to_xsct, "w");
251
 
252
   if(from_xsct == (FILE *) NULL || to_xsct == (FILE *) NULL) {
253
      fprintf(stderr, "jtag_init: can't communicate with xilinx xsct process\n");
254
      fclose(from_xsct);
255
      fclose(to_xsct);
256
      from_xsct = (FILE *) NULL;
257
      to_xsct = (FILE *) NULL;
258
      return(1);
259
   }
260
 
261
 
262
   while(1) {
263
 
264
       fgets(buf, sizeof(buf), from_xsct);
265
       check_error_xsct_out(buf);
266
 
267
 
268
      if(!strcmp(buf, "\n")){
269
         fgets(buf, sizeof(buf), from_xsct);
270
          //fgets(buf, sizeof(buf), from_xsct); 
271
         if(!strcmp(buf, "\n")) break;
272
 
273
        }
274
 
275
 
276
      if(feof(from_xsct)) {
277
         fprintf(stderr, "saw eof from xsct. Make sure path to xsxt is included in PATH linuc enviremet\n");
278
         exit(1);
279
      }
280
 
281
      if(ferror(from_xsct)) {
282
         fprintf(stderr, "saw error from xsct\n");
283
         exit(1);
284
      }
285
   }
286
 
287
   printf("connecting to jtag  and select index %x\n",index_num);
288
   fprintf(to_xsct, "set jseq [jtag sequence]\n");
289
   fprintf(to_xsct, "connect\n");
290
   wait_for_pipe_output( buf, "tcfchan");
291
 
292
   //printf("select jtag target\n"); 
293
   fprintf(to_xsct, "jtag targets %u; puts done\n",jtag_target_number);
294
   //fprintf(to_xsct, "jtag frequency 5000000");
295
   wait_for_pipe_output( buf, "done");
296
   //printf("Done!\n"); 
297
#ifdef PRINT_TO_XSCT
298
   fprintf(to_xsct_file, "set jseq [jtag sequence]\n");
299
   fprintf(to_xsct_file, "connect\n");
300
   fprintf(to_xsct_file, "jtag targets %u puts done\n",jtag_target_number);
301
 
302
#endif
303
 
304
 
305
//
306
 
307
 
308
 
309
 
310
 
311
 
312
        return 0;
313
 
314
}
315
 
316
 
317
void strreplace(char s[], char chr, char repl_chr)
318
{
319
     int i=0;
320
     while(s[i]!='\0')
321
     {
322
           if(s[i]==chr)
323
           {
324
               s[i]=repl_chr;
325
           }
326
           i++;
327
     }
328
          //printf("%s",s);
329
}
330
 
331
 
332
void  clean_xsct_buff (){
333
        char buf[2024];
334
        fprintf(to_xsct,"puts \"hi\"\n");
335
#ifdef PRINT_TO_XSCT
336
        fprintf(to_xsct_file,"puts \"hi\"\n");
337
#endif
338
        fflush(to_xsct);
339
        fgets(buf, sizeof(buf), from_xsct);
340
        check_error_xsct_out(buf);
341
        //printf("clean:%s\n",buf);
342
}
343
 
344
void remove_state( char * r){
345
        int s = strlen(r);
346
        r[s-2]=0;// remove the last byte
347
 
348
}
349
 
350
char * read_xsct (){
351
        char buf[2024];
352
        char * out;
353
        char * result=NULL;
354
        //char * ptr;
355
        fflush(to_xsct);
356
        while(1) {
357
                fgets(buf, sizeof(buf), from_xsct);
358
        //printf ("rdbuf=%s\n",buf);
359
                //ptr=remove_color_code_from_string(buf,sizeof(buf));        
360
                check_error_xsct_out(buf);
361
                if(strstr(buf, "RESULT:") != NULL) {
362
                result=strstr(buf, "RESULT:");
363
                        break;
364
                }
365
 
366
                if(!strcmp(buf, "\n")) break;
367
                //if(!strcmp(ptr, "\n")) break;
368
 
369
                if(feof(from_xsct)) {
370
                        fprintf(stderr, "saw eof from xsct\n");
371
                        exit(1);
372
                }
373
                if(ferror(from_xsct)) {
374
                        fprintf(stderr, "saw error from xsct\n");
375
                        exit(1);
376
              }
377
        }
378
        if(result){
379
                char * r= result+7;
380
                strreplace(r, '\n', 0);
381
                remove_state(r);
382
                out =(char*)malloc( sizeof(r)+2);
383
 
384
                jtag_reorder ( r, out );
385
                return out;
386
        }
387
        return 0;
388
}
389
 
390
 
391
 
392
 
393
void return_dr (unsigned *out) {
394
 
395
        char *ptr;
396
        fprintf(to_xsct,"puts \"RESULT:$data\"\n");
397
#ifdef PRINT_TO_XSCT
398
        fprintf(to_xsct_file,"puts \"RESULT:$data\"\n");
399
#endif
400
        ptr=read_xsct();
401
        //printf("saw: '%s'\n", ptr);
402
        //while(*ptr=='t' || *ptr=='c'  || *ptr=='l' || *ptr=='>' || *ptr==' ' ) ptr++;
403
 
404
        *out= strtol(ptr,NULL,16);
405
}
406
 
407
void return_dr_long (unsigned *out, int words) {
408
 
409
        char *ptr;
410
        fprintf(to_xsct,"puts \"RESULT:$data\"\n");
411
#ifdef PRINT_TO_XSCT
412
        fprintf(to_xsct_file,"puts \"RESULT:$data\"\n");
413
#endif
414
        ptr=read_xsct();
415
        //printf("saw: '%s'\n", ptr);
416
        //while(*ptr=='t' || *ptr=='c'  || *ptr=='l' || *ptr=='>' || *ptr==' ' ) ptr++;
417
 
418
        hexcut( ptr, out, words );
419
}
420
 
421
 
422
void send_to_jtag (char * hexstring) {
423
        fprintf(to_xsct,"$jseq clear\n");
424
        fprintf(to_xsct,"$jseq irshift -state IDLE -hex 6 %x\n",chain_code);
425
        fprintf(to_xsct,"$jseq drshift -state IDLE -hex %u %s\n",jtag_shift_reg_size,hexstring);
426
        //printf("$jseq drshift -state IDLE -hex %u %s\n",jtag_shift_reg_size,hexstring);
427
        fprintf(to_xsct,"$jseq run\n");
428
 
429
#ifdef PRINT_TO_XSCT
430
        fprintf(to_xsct_file,"$jseq clear\n");
431
        fprintf(to_xsct_file,"$jseq irshift -state IDLE -hex 6 %x\n",chain_code);
432
        fprintf(to_xsct_file,"$jseq drshift -state IDLE -hex %u %s\n",jtag_shift_reg_size,hexstring);
433
        fprintf(to_xsct_file,"$jseq run\n");
434
#endif
435
 
436
        //fflush(to_xsct);       
437
}
438
 
439
 
440
void send_capture_jtag (char * hexstring) {
441
        fprintf(to_xsct,"$jseq clear\n");
442
        fprintf(to_xsct,"$jseq irshift -state IDLE -hex 6 %x\n",chain_code);
443
        fprintf(to_xsct,"$jseq drshift -state IDLE -capture -hex %u %s\n",jtag_shift_reg_size,hexstring);
444
        //printf("$jseq drshift -state IDLE -capture -hex %u %s\n",jtag_shift_reg_size,hexstring);
445
        fprintf(to_xsct,"set data [$jseq run]\n");
446
        //fflush(to_xsct);     
447
#ifdef PRINT_TO_XSCT
448
        fprintf(to_xsct_file,"$jseq clear\n");
449
        fprintf(to_xsct_file,"$jseq irshift -state IDLE -hex 6 %x\n",chain_code);
450
        fprintf(to_xsct_file,"$jseq drshift -state IDLE -capture -hex %u %s\n",jtag_shift_reg_size,hexstring);
451
        fprintf(to_xsct_file,"set data [$jseq run]\n");
452
#endif
453
 
454
 
455
}
456
 
457
 
458
void jtag_vindex(unsigned vindex) {
459
 
460
        //fprintf(to_xsct,"device_lock -timeout 10000\n");
461
        char hexstring[1000];
462
        unsigned bits [10];
463
        bits [0] = vindex;
464
        hextostring_xsct( hexstring, bits,  WORDS_NUM, jtag_shift_reg_size );
465
        add_update_state (hexstring,UPDATE_INDEX,jtag_shift_reg_size);
466
        send_to_jtag (hexstring);
467
        clean_xsct_buff ();
468
}
469
 
470
 
471
 
472
 
473
 
474
 
475
void jtag_vir(unsigned vir) {
476
 
477
        //fprintf(to_xsct,"device_lock -timeout 10000\n");
478
        char hexstring[1000];
479
        unsigned bits [10];
480
        bits [0] = vir;
481
        hextostring_xsct( hexstring, bits,  WORDS_NUM, jtag_shift_reg_size );
482
        add_update_state (hexstring,UPDATE_IR,jtag_shift_reg_size);
483
        send_to_jtag (hexstring);
484
        clean_xsct_buff ();
485
}
486
 
487
 
488
 
489
 
490
 
491
void jtag_vdr(unsigned sz, unsigned bits, unsigned *out) {
492
        char hexstring[1000];
493
 
494
        hextostring_xsct( hexstring, &bits,  WORDS_NUM, jtag_shift_reg_size );
495
        add_update_state (hexstring,UPDATE_DAT,jtag_shift_reg_size);
496
        if (!out){
497
                send_to_jtag (hexstring);
498
                clean_xsct_buff();
499
        }else{
500
                send_capture_jtag(hexstring);
501
                return_dr (out);
502
        }
503
}
504
 
505
void jtag_vdr_long(unsigned sz, unsigned * bits, unsigned *out, int words) {
506
        char hexstring[1000];
507
 
508
        hextostring_xsct( hexstring, bits,  words, jtag_shift_reg_size );
509
        add_update_state (hexstring,UPDATE_DAT,jtag_shift_reg_size);
510
 
511
        if (!out){
512
                //printf("send_to_jtag (%s)\n",hexstring);
513
                send_to_jtag (hexstring);
514
                clean_xsct_buff();
515
        }else{
516
                //printf("send_capture_to_jtag (%s)\n",hexstring);
517
                send_capture_jtag(hexstring);
518
                return_dr_long (out,words);
519
        }
520
 
521
}
522
 
523
 
524
void closeport(){
525
        fprintf(to_xsct,"exit\n");
526
#ifdef PRINT_TO_XSCT
527
        fprintf(to_xsct_file,"exit\n");
528
#endif
529
        fflush(to_xsct);
530
}
531
 
532
 
533
 
534
#ifdef DEBUG_JTAG
535
 
536
void turn_on_led(){
537
        unsigned out;
538
        fprintf(to_xsct, "device_lock -timeout 10000\n");
539
        fprintf(to_xsct,"device_virtual_ir_shift -instance_index 127 -ir_value 1 -no_captured_ir_value\n");
540
        fprintf(to_xsct,"device_virtual_dr_shift -dr_value 3 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
541
        //fprintf(to_xsct,"device_virtual_dr_shift -dr_value 0 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
542
        fprintf(to_xsct,"catch {device_unlock}\n");
543
        jtag_vdr(2, 0, &out);
544
        fprintf(to_xsct, "device_lock -timeout 10000\n");
545
        fprintf(to_xsct,"device_virtual_ir_shift -instance_index 127 -ir_value 0 -no_captured_ir_value\n");
546
 
547
        printf("outs= %d \n",out);
548
        fprintf(to_xsct,"catch {device_unlock}\n");
549
        fflush(to_xsct);
550
        //run();
551
 
552
}
553
 
554
 
555
void turn_off_led(){
556
        unsigned out;
557
        fprintf(to_xsct, "device_lock -timeout 10000\n");
558
        fprintf(to_xsct, "device_virtual_ir_shift -instance_index 127 -ir_value 1 -no_captured_ir_value\n");
559
        fprintf(to_xsct, "device_virtual_dr_shift -dr_value 3 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
560
        //fprintf(to_xsct,"device_virtual_dr_shift -dr_value 2 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
561
        fprintf(to_xsct,"catch {device_unlock}\n");
562
        jtag_vdr(2, 3, &out);
563
        fprintf(to_xsct, "device_lock -timeout 10000\n");
564
        printf("outs= %d \n",out);
565
        fprintf(to_xsct,"device_virtual_ir_shift -instance_index 127 -ir_value 0 -no_captured_ir_value\n");
566
        //fprintf(to_xsct, "puts \"device_name is $chip_name\\n\";\n");
567
        fprintf(to_xsct,"catch {device_unlock}\n");
568
        fflush(to_xsct);
569
        //run();
570
}
571
 
572
 
573
 
574
 
575
 
576
int main(){
577
        int c=0;
578
        jtag_init("DE-SoC *","@2*"); // fpr DE10-nano
579
        while (c==0 || c== 1){
580
                 printf("Enter 1: to on, 0: to off, else to quit:\n");
581
                 scanf ("%d",&c);
582
                 if(c==0){printf("\toff\n"); turn_off_led();}
583
                 else if (c== 1){printf("\ton\n"); turn_on_led();}
584
                 else break;
585
 
586
        }
587
 
588
        closeport();
589
        fclose(from_xsct);
590
        fclose(to_xsct);
591
        from_xsct = (FILE *) NULL;
592
        to_xsct = (FILE *) NULL;
593
 
594
        return 0;
595
 
596
 
597
 
598
}
599
 
600
#endif
601
 
602
 

powered by: WebSVN 2.1.0

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