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.c] - Blame information for rev 45

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 38 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
 
18
 
19
 
20
 
21
//#define DEBUG_JTAG
22
 
23
 
24
 
25
 
26
 
27
 
28
 
29
 
30
 
31
 
32
 
33
#define DEFAULT_TM4HOST "DE-SoC *"
34
#define DEFAULT_TMNUM   2
35
 
36
 
37
 
38
 
39
FILE *to_stp, *from_stp;
40
 
41
 
42
 
43
int hexcut( char * hexstring, unsigned * val, int words ){
44
    size_t count = 0;
45
    int start;
46
    char word[8];
47
 
48
    if (*(hexstring+1)=='x' || *(hexstring+1)=='X') hexstring+=2;
49
    int size=strlen(hexstring);
50
    int hexnum= (size%8)? (size/8)+1 : size/8;
51
    for(count = 0; count < words; count++) val[count]=0;
52
    //printf("hexstring=%s\n",hexstring);       
53
    for(count = 1; count <= hexnum; count++) {
54
        start=(count*8>size)? 0 : size-(count*8);
55
        //start=0;
56
        //ptr=hexstring+start;
57
        strncpy( word, hexstring+start,8);
58
        //printf("** %s\n,",word);        
59
        sscanf(word, "%08x", &val[count-1]);
60
       // *(hexstring+start)=0;
61
        //printf("%x,",val[count-1]);
62
    }
63
 
64
   //printf("\nsize=%d, hexnum=%u\n",size,hexnum);
65
 
66
 
67
    return hexnum;
68
}
69
 
70
 
71
void hexgen( char * hexstring, unsigned * val, int words ){
72
    size_t count = 0;
73
    sprintf(hexstring,"0x");
74
    for(count = 0; count < words; count++) {
75
        if(count == 0)  sprintf((hexstring+2),"%x",val[words-count-1]);
76
        else            sprintf(hexstring,"%08x",val[words-count-1]);
77
         hexstring+=strlen(hexstring);
78
   }
79
 
80
 // return hexnum;
81
}
82
 
83
void hextostring( char * hexstring, unsigned * val, int words,unsigned sz){
84
    size_t count = 0;
85
 
86
    char tmp[100];
87
    char zeros[100];
88
    char *pointer = tmp;
89
 
90
    //sprintf(hexstring,"0x");
91
 
92
    for(count = 0; count < words; count++) {
93
        if(count == 0)  sprintf(pointer,"%x",val[words-count-1]);
94
        else            sprintf(pointer,"%08x",val[words-count-1]);
95
        pointer+=strlen(pointer);
96
   }
97
 
98
   int digits=(sz%4)? sz/4 +1 : sz/4 ;
99
   //printf("%d > %d", digits , strlen(tmp));
100
   if (digits > strlen(tmp)){
101
        for(count = 0; count < digits-strlen(tmp); count++) {
102
                zeros[count]='0';
103
         }
104
        zeros[count]=0;
105
        strcat(zeros,tmp);
106
        sprintf(hexstring,"%s",zeros);
107
 
108
   }else{
109
        sprintf(hexstring,"%s",tmp);
110
 
111
   }
112
 
113
 
114
 
115
 // return hexnum;
116
}
117
 
118
 
119 43 alirezamon
//char end_tcl [10] = { 0x1b, 0x5b,[2]=30,[3]=6d,[4]=a,
120 38 alirezamon
 
121 43 alirezamon
char* remove_color_code_from_string ( char *buf, int z){
122
        int i=0;
123
        char * ptr=buf;
124
        if( *ptr != 0x1b ) return ptr;
125
        do{
126
                ptr ++;
127
                i++;
128
        } while ((*ptr != 'm') && (*ptr != 0x0) && i<z-1);
129
        ptr ++;
130
        return ptr;
131
}
132 38 alirezamon
 
133
int jtag_init(char *hrdname, char *dvicname ) {
134
 
135
   /* Create a quartus_stp process, and get the list of ports */
136
 
137
   int  f_to_stp, f_from_stp;
138
   char buf[1024];
139 43 alirezamon
   char * ptr;
140 38 alirezamon
   char *command[] = {"quartus_stp", "-s", 0};
141
 
142
   if(from_stp != (FILE *) NULL) {
143
      fclose(from_stp);
144
      fclose(to_stp);
145
   }
146
 
147
   piped_child(command, &f_from_stp, &f_to_stp);
148
 
149
   from_stp = fdopen(f_from_stp, "r");
150
   to_stp = fdopen(f_to_stp, "w");
151
 
152
   if(from_stp == (FILE *) NULL || to_stp == (FILE *) NULL) {
153
      fprintf(stderr, "jtag_init: can't communicate with quartus_stp process\n");
154
      fclose(from_stp);
155
      fclose(to_stp);
156
      from_stp = (FILE *) NULL;
157
      to_stp = (FILE *) NULL;
158
      return(1);
159
   }
160
 
161
 
162
 
163
   while(1) {
164 43 alirezamon
      fgets(buf, sizeof(buf), from_stp);
165
      ptr=remove_color_code_from_string(buf,sizeof(buf));
166
 
167
 
168
 
169
 
170 38 alirezamon
        if(strstr(buf, "ERROR") != NULL) {
171
                printf("\tERROR\n");
172
                printf("'%s'\n", buf);
173
                exit(1);
174
        }
175
 
176
 
177
 
178 43 alirezamon
      if(!strcmp(buf, "\n")) break;
179
      if(!strcmp(ptr, "\n")) break;
180
 
181 38 alirezamon
      if(feof(from_stp)) {
182
         fprintf(stderr, "saw eof from quartus_stp\n");
183
         exit(1);
184
      }
185
 
186
      if(ferror(from_stp)) {
187
         fprintf(stderr, "saw error from quartus_stp\n");
188
         exit(1);
189
      }
190
   }
191
 
192
   fprintf(to_stp, "foreach name [get_hardware_names] {\n");
193
   fprintf(to_stp, "  puts $name\n");
194
   fprintf(to_stp, "  if { [string match \"*%s*\" $name] } {\n", hrdname);
195
   fprintf(to_stp, "    set hardware_name $name\n");
196
   fprintf(to_stp, "  }\n");
197
   fprintf(to_stp, "}\n");
198
   fprintf(to_stp, "puts \"\\nhardware_name is $hardware_name\";\n");
199
   fprintf(to_stp, "foreach name [get_device_names -hardware_name $hardware_name] {\n");
200
   fprintf(to_stp, "  if { [string match \"*%s*\" $name] } {\n",dvicname);
201
   fprintf(to_stp, "    set chip_name $name\n");
202
   fprintf(to_stp, "  }\n");
203
   fprintf(to_stp, "}\n");
204
   fprintf(to_stp, "puts \"device_name is $chip_name\\n\";\n");
205
   fprintf(to_stp, "open_device -hardware_name $hardware_name -device_name $chip_name\n");
206
 
207
   fflush(to_stp);
208
 
209
   while(1) {
210
      fgets(buf, sizeof(buf), from_stp);
211 43 alirezamon
      ptr=remove_color_code_from_string(buf,sizeof(buf));
212 38 alirezamon
        if(strstr(buf, "ERROR") != NULL) {
213
                printf("\tERROR\n");
214
                printf("'%s'\n", buf);
215
                exit(1);
216
        }
217
 
218 43 alirezamon
      if(!strcmp(buf, "\n")) break;
219
      if(!strcmp(ptr, "\n")) break;
220 38 alirezamon
      if(feof(from_stp)) {
221
         fprintf(stderr, "saw eof from quartus_stp\n");
222
         exit(1);
223
      }
224
      if(ferror(from_stp)) {
225
         fprintf(stderr, "saw error from quartus_stp\n");
226
         exit(1);
227
      }
228
   }
229
         return 0;
230
 
231
}
232
 
233
 
234
void strreplace(char s[], char chr, char repl_chr)
235
{
236
     int i=0;
237
     while(s[i]!='\0')
238
     {
239
           if(s[i]==chr)
240
           {
241
               s[i]=repl_chr;
242
           }
243
           i++;
244
     }
245
          //printf("%s",s);
246
}
247
 
248 45 alirezamon
 
249
void  clean_stp_buff (){
250
        char buf[1024];
251
        fflush(to_stp);
252
        fgets(buf, sizeof(buf), from_stp);
253
        //printf("%s\n",buf);
254
}
255
 
256
 
257
 
258 38 alirezamon
char * read_stp (){
259
        char buf[1024];
260
        char * result=NULL;
261 43 alirezamon
        char * ptr;
262 38 alirezamon
        fflush(to_stp);
263
        while(1) {
264 43 alirezamon
                fgets(buf, sizeof(buf), from_stp);
265
                ptr=remove_color_code_from_string(buf,sizeof(buf));
266 38 alirezamon
                if(strstr(buf, "ERROR") != NULL) {
267
                        printf("\tERROR\n");
268
                        printf("'%s'\n", buf);
269
                        exit(1);
270
                }
271
                if(strstr(buf, "RESULT:") != NULL) {
272
                        result=strstr(buf, "RESULT:");
273
 
274
                        break;
275
 
276
                }
277
 
278 43 alirezamon
                if(!strcmp(buf, "\n")) break;
279
                if(!strcmp(ptr, "\n")) break;
280
 
281 38 alirezamon
                if(feof(from_stp)) {
282
                        fprintf(stderr, "saw eof from quartus_stp\n");
283
                        exit(1);
284
                }
285
                if(ferror(from_stp)) {
286
                        fprintf(stderr, "saw error from quartus_stp\n");
287
                        exit(1);
288
              }
289
        }
290
        if(result){
291
                char * r= result+7;
292
                strreplace(r, '\n', 0);
293
                return r;
294
        }
295
        return 0;
296
}
297
 
298
 
299
 
300
 
301
void return_dr (unsigned *out) {
302
 
303
        char *ptr;
304
        fprintf(to_stp,"puts \"RESULT:$data\"\n");
305
        ptr=read_stp();
306
        //printf("saw: '%s'\n", ptr);
307
        while(*ptr=='t' || *ptr=='c'  || *ptr=='l' || *ptr=='>' || *ptr==' ' ) ptr++;
308
 
309
        *out= strtol(ptr,NULL,16);
310
}
311
 
312
void return_dr_long (unsigned *out, int words) {
313
 
314
        char *ptr;
315
        fprintf(to_stp,"puts \"RESULT:$data\"\n");
316
        ptr=read_stp();
317
        //printf("saw: '%s'\n", ptr);
318
        while(*ptr=='t' || *ptr=='c'  || *ptr=='l' || *ptr=='>' || *ptr==' ' ) ptr++;
319
 
320
        hexcut( ptr, out, words );
321
}
322
 
323
 
324
void jtag_vir(unsigned vir) {
325
        fprintf(to_stp,"device_lock -timeout 10000\n");
326
        fprintf(to_stp,"device_virtual_ir_shift -instance_index %d -ir_value %x -no_captured_ir_value\n",index_num,vir);
327
        fprintf(to_stp,"catch {device_unlock}\n");
328
}
329
 
330
 
331
void jtag_vdr(unsigned sz, unsigned bits, unsigned *out) {
332
        char hexstring[1000];
333
 
334
        hextostring( hexstring, &bits,  1, sz );
335
        if (!out){
336
                fprintf(to_stp,"device_lock -timeout 10000\n");
337 45 alirezamon
 
338 38 alirezamon
                fprintf(to_stp,"device_virtual_dr_shift -dr_value %s -instance_index %d  -length %d -no_captured_dr_value -value_in_hex\n", hexstring,index_num,sz);
339 45 alirezamon
 
340 38 alirezamon
                //printf("device_virtual_dr_shift -dr_value %s -instance_index %d  -length %d -no_captured_dr_value -value_in_hex\n",hexstring,index_num,sz);
341
                fprintf(to_stp,"catch {device_unlock}\n");
342 45 alirezamon
                clean_stp_buff();
343
//fflush(to_stp);
344 38 alirezamon
        }else{
345
                fprintf(to_stp,"device_lock -timeout 10000\n");
346 45 alirezamon
 
347 38 alirezamon
                fprintf(to_stp,"set data [device_virtual_dr_shift -dr_value %s -instance_index %d  -length %d  -value_in_hex]\n", hexstring,index_num,sz);
348 45 alirezamon
 
349
                fprintf(to_stp,"catch {device_unlock}\n");
350 38 alirezamon
                return_dr (out);
351
        }
352
}
353
 
354
void jtag_vdr_long(unsigned sz, unsigned * bits, unsigned *out, int words) {
355
        char hexstring[1000];
356
 
357
        hextostring( hexstring, bits,  words, sz );
358
 
359
        if (!out){
360
                fprintf(to_stp,"device_lock -timeout 10000\n");
361
                fprintf(to_stp,"device_virtual_dr_shift -dr_value %s -instance_index %d  -length %d -no_captured_dr_value -value_in_hex\n",hexstring,index_num,sz);
362
                //printf("device_virtual_dr_shift -dr_value %s -instance_index %d  -length %d -no_captured_dr_value -value_in_hex\n",hexstring,index_num,sz);
363
 
364
                fprintf(to_stp,"catch {device_unlock}\n");
365 45 alirezamon
                clean_stp_buff();
366 38 alirezamon
        }else{
367
                fprintf(to_stp,"device_lock -timeout 10000\n");
368
                fprintf(to_stp,"set data [device_virtual_dr_shift -dr_value %s -instance_index %d  -length %d  -value_in_hex]\n",hexstring,index_num,sz);
369
                fprintf(to_stp,"catch {device_unlock}\n");
370
                return_dr_long (out,words);
371
        }
372
 
373
}
374
 
375
 
376
void closeport(){
377
        fprintf(to_stp,"catch {device_unlock}\n");
378
        fprintf(to_stp,"catch {close_device}\n");
379
        fflush(to_stp);
380
}
381
 
382
 
383
 
384
#ifdef DEBUG_JTAG
385
 
386
void turn_on_led(){
387
        unsigned out;
388
        fprintf(to_stp, "device_lock -timeout 10000\n");
389
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 1 -no_captured_ir_value\n");
390
        fprintf(to_stp,"device_virtual_dr_shift -dr_value 3 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
391
        //fprintf(to_stp,"device_virtual_dr_shift -dr_value 0 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
392
        fprintf(to_stp,"catch {device_unlock}\n");
393
        jtag_vdr(2, 0, &out);
394
        fprintf(to_stp, "device_lock -timeout 10000\n");
395
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 0 -no_captured_ir_value\n");
396
 
397
        printf("outs= %d \n",out);
398
        fprintf(to_stp,"catch {device_unlock}\n");
399
        fflush(to_stp);
400
        //run();
401
 
402
}
403
 
404
 
405
void turn_off_led(){
406
        unsigned out;
407
        fprintf(to_stp, "device_lock -timeout 10000\n");
408
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 1 -no_captured_ir_value\n");
409
        fprintf(to_stp,"device_virtual_dr_shift -dr_value 3 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
410
        //fprintf(to_stp,"device_virtual_dr_shift -dr_value 2 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
411
        fprintf(to_stp,"catch {device_unlock}\n");
412
        jtag_vdr(2, 3, &out);
413
        fprintf(to_stp, "device_lock -timeout 10000\n");
414
        printf("outs= %d \n",out);
415
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 0 -no_captured_ir_value\n");
416
        //fprintf(to_stp, "puts \"device_name is $chip_name\\n\";\n");
417
        fprintf(to_stp,"catch {device_unlock}\n");
418
        fflush(to_stp);
419
        //run();
420
}
421
 
422
 
423
 
424
 
425
 
426
int main(){
427
        int c=0;
428
        jtag_init("DE-SoC *","@2*"); // fpr DE10-nano
429
        while (c==0 || c== 1){
430
                 printf("Enter 1: to on, 0: to off, else to quit:\n");
431
                 scanf ("%d",&c);
432
                 if(c==0){printf("\toff\n"); turn_off_led();}
433
                 else if (c== 1){printf("\ton\n"); turn_on_led();}
434
                 else break;
435
 
436
        }
437
 
438
        closeport();
439
        fclose(from_stp);
440
        fclose(to_stp);
441
        from_stp = (FILE *) NULL;
442
        to_stp = (FILE *) NULL;
443
 
444
        return 0;
445
 
446
 
447
 
448
}
449
 
450
#endif
451
 
452
 

powered by: WebSVN 2.1.0

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