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 43

Go to most recent revision | 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
char * read_stp (){
249
        char buf[1024];
250
        char * result=NULL;
251 43 alirezamon
        char * ptr;
252 38 alirezamon
        fflush(to_stp);
253
        while(1) {
254 43 alirezamon
                fgets(buf, sizeof(buf), from_stp);
255
                ptr=remove_color_code_from_string(buf,sizeof(buf));
256 38 alirezamon
                if(strstr(buf, "ERROR") != NULL) {
257
                        printf("\tERROR\n");
258
                        printf("'%s'\n", buf);
259
                        exit(1);
260
                }
261
                if(strstr(buf, "RESULT:") != NULL) {
262
                        result=strstr(buf, "RESULT:");
263
 
264
                        break;
265
 
266
                }
267
 
268 43 alirezamon
                if(!strcmp(buf, "\n")) break;
269
                if(!strcmp(ptr, "\n")) break;
270
 
271 38 alirezamon
                if(feof(from_stp)) {
272
                        fprintf(stderr, "saw eof from quartus_stp\n");
273
                        exit(1);
274
                }
275
                if(ferror(from_stp)) {
276
                        fprintf(stderr, "saw error from quartus_stp\n");
277
                        exit(1);
278
              }
279
        }
280
        if(result){
281
                char * r= result+7;
282
                strreplace(r, '\n', 0);
283
                return r;
284
        }
285
        return 0;
286
}
287
 
288
 
289
 
290
 
291
void return_dr (unsigned *out) {
292
 
293
        char *ptr;
294
        fprintf(to_stp,"puts \"RESULT:$data\"\n");
295
        ptr=read_stp();
296
        //printf("saw: '%s'\n", ptr);
297
        while(*ptr=='t' || *ptr=='c'  || *ptr=='l' || *ptr=='>' || *ptr==' ' ) ptr++;
298
 
299
        *out= strtol(ptr,NULL,16);
300
}
301
 
302
void return_dr_long (unsigned *out, int words) {
303
 
304
        char *ptr;
305
        fprintf(to_stp,"puts \"RESULT:$data\"\n");
306
        ptr=read_stp();
307
        //printf("saw: '%s'\n", ptr);
308
        while(*ptr=='t' || *ptr=='c'  || *ptr=='l' || *ptr=='>' || *ptr==' ' ) ptr++;
309
 
310
        hexcut( ptr, out, words );
311
}
312
 
313
 
314
void jtag_vir(unsigned vir) {
315
        fprintf(to_stp,"device_lock -timeout 10000\n");
316
        fprintf(to_stp,"device_virtual_ir_shift -instance_index %d -ir_value %x -no_captured_ir_value\n",index_num,vir);
317
        fprintf(to_stp,"catch {device_unlock}\n");
318
}
319
 
320
 
321
void jtag_vdr(unsigned sz, unsigned bits, unsigned *out) {
322
        char hexstring[1000];
323
 
324
        hextostring( hexstring, &bits,  1, sz );
325
        if (!out){
326
                fprintf(to_stp,"device_lock -timeout 10000\n");
327
                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);
328
                //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);
329
                fprintf(to_stp,"catch {device_unlock}\n");
330
        }else{
331
                fprintf(to_stp,"device_lock -timeout 10000\n");
332
                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);
333
                fprintf(to_stp,"catch {device_unlock}\n");
334
                return_dr (out);
335
        }
336
}
337
 
338
void jtag_vdr_long(unsigned sz, unsigned * bits, unsigned *out, int words) {
339
        char hexstring[1000];
340
 
341
        hextostring( hexstring, bits,  words, sz );
342
 
343
        if (!out){
344
                fprintf(to_stp,"device_lock -timeout 10000\n");
345
                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);
346
                //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);
347
 
348
                fprintf(to_stp,"catch {device_unlock}\n");
349
        }else{
350
                fprintf(to_stp,"device_lock -timeout 10000\n");
351
                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);
352
                fprintf(to_stp,"catch {device_unlock}\n");
353
                return_dr_long (out,words);
354
        }
355
 
356
}
357
 
358
 
359
void closeport(){
360
        fprintf(to_stp,"catch {device_unlock}\n");
361
        fprintf(to_stp,"catch {close_device}\n");
362
        fflush(to_stp);
363
}
364
 
365
 
366
 
367
#ifdef DEBUG_JTAG
368
 
369
void turn_on_led(){
370
        unsigned out;
371
        fprintf(to_stp, "device_lock -timeout 10000\n");
372
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 1 -no_captured_ir_value\n");
373
        fprintf(to_stp,"device_virtual_dr_shift -dr_value 3 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
374
        //fprintf(to_stp,"device_virtual_dr_shift -dr_value 0 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
375
        fprintf(to_stp,"catch {device_unlock}\n");
376
        jtag_vdr(2, 0, &out);
377
        fprintf(to_stp, "device_lock -timeout 10000\n");
378
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 0 -no_captured_ir_value\n");
379
 
380
        printf("outs= %d \n",out);
381
        fprintf(to_stp,"catch {device_unlock}\n");
382
        fflush(to_stp);
383
        //run();
384
 
385
}
386
 
387
 
388
void turn_off_led(){
389
        unsigned out;
390
        fprintf(to_stp, "device_lock -timeout 10000\n");
391
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 1 -no_captured_ir_value\n");
392
        fprintf(to_stp,"device_virtual_dr_shift -dr_value 3 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
393
        //fprintf(to_stp,"device_virtual_dr_shift -dr_value 2 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
394
        fprintf(to_stp,"catch {device_unlock}\n");
395
        jtag_vdr(2, 3, &out);
396
        fprintf(to_stp, "device_lock -timeout 10000\n");
397
        printf("outs= %d \n",out);
398
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 0 -no_captured_ir_value\n");
399
        //fprintf(to_stp, "puts \"device_name is $chip_name\\n\";\n");
400
        fprintf(to_stp,"catch {device_unlock}\n");
401
        fflush(to_stp);
402
        //run();
403
}
404
 
405
 
406
 
407
 
408
 
409
int main(){
410
        int c=0;
411
        jtag_init("DE-SoC *","@2*"); // fpr DE10-nano
412
        while (c==0 || c== 1){
413
                 printf("Enter 1: to on, 0: to off, else to quit:\n");
414
                 scanf ("%d",&c);
415
                 if(c==0){printf("\toff\n"); turn_off_led();}
416
                 else if (c== 1){printf("\ton\n"); turn_on_led();}
417
                 else break;
418
 
419
        }
420
 
421
        closeport();
422
        fclose(from_stp);
423
        fclose(to_stp);
424
        from_stp = (FILE *) NULL;
425
        to_stp = (FILE *) NULL;
426
 
427
        return 0;
428
 
429
 
430
 
431
}
432
 
433
#endif
434
 
435
 

powered by: WebSVN 2.1.0

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