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 38

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
 
120
 
121
int jtag_init(char *hrdname, char *dvicname ) {
122
 
123
   /* Create a quartus_stp process, and get the list of ports */
124
 
125
   int  f_to_stp, f_from_stp;
126
   char buf[1024];
127
   char *command[] = {"quartus_stp", "-s", 0};
128
 
129
   if(from_stp != (FILE *) NULL) {
130
      fclose(from_stp);
131
      fclose(to_stp);
132
   }
133
 
134
   piped_child(command, &f_from_stp, &f_to_stp);
135
 
136
   from_stp = fdopen(f_from_stp, "r");
137
   to_stp = fdopen(f_to_stp, "w");
138
 
139
   if(from_stp == (FILE *) NULL || to_stp == (FILE *) NULL) {
140
      fprintf(stderr, "jtag_init: can't communicate with quartus_stp process\n");
141
      fclose(from_stp);
142
      fclose(to_stp);
143
      from_stp = (FILE *) NULL;
144
      to_stp = (FILE *) NULL;
145
      return(1);
146
   }
147
 
148
 
149
 
150
   while(1) {
151
      fgets(buf, sizeof(buf), from_stp);
152
        if(strstr(buf, "ERROR") != NULL) {
153
                printf("\tERROR\n");
154
                printf("'%s'\n", buf);
155
                exit(1);
156
        }
157
 
158
 
159
 
160
      if(!strcmp(buf, "\n"))
161
         break;
162
      if(feof(from_stp)) {
163
         fprintf(stderr, "saw eof from quartus_stp\n");
164
         exit(1);
165
      }
166
 
167
      if(ferror(from_stp)) {
168
         fprintf(stderr, "saw error from quartus_stp\n");
169
         exit(1);
170
      }
171
   }
172
 
173
   fprintf(to_stp, "foreach name [get_hardware_names] {\n");
174
   fprintf(to_stp, "  puts $name\n");
175
   fprintf(to_stp, "  if { [string match \"*%s*\" $name] } {\n", hrdname);
176
   fprintf(to_stp, "    set hardware_name $name\n");
177
   fprintf(to_stp, "  }\n");
178
   fprintf(to_stp, "}\n");
179
   fprintf(to_stp, "puts \"\\nhardware_name is $hardware_name\";\n");
180
   fprintf(to_stp, "foreach name [get_device_names -hardware_name $hardware_name] {\n");
181
   fprintf(to_stp, "  if { [string match \"*%s*\" $name] } {\n",dvicname);
182
   fprintf(to_stp, "    set chip_name $name\n");
183
   fprintf(to_stp, "  }\n");
184
   fprintf(to_stp, "}\n");
185
   fprintf(to_stp, "puts \"device_name is $chip_name\\n\";\n");
186
   fprintf(to_stp, "open_device -hardware_name $hardware_name -device_name $chip_name\n");
187
 
188
   fflush(to_stp);
189
 
190
   while(1) {
191
      fgets(buf, sizeof(buf), from_stp);
192
 
193
        if(strstr(buf, "ERROR") != NULL) {
194
                printf("\tERROR\n");
195
                printf("'%s'\n", buf);
196
                exit(1);
197
        }
198
 
199
      if(!strcmp(buf, "\n"))
200
         break;
201
      if(feof(from_stp)) {
202
         fprintf(stderr, "saw eof from quartus_stp\n");
203
         exit(1);
204
      }
205
      if(ferror(from_stp)) {
206
         fprintf(stderr, "saw error from quartus_stp\n");
207
         exit(1);
208
      }
209
   }
210
         return 0;
211
 
212
}
213
 
214
 
215
void strreplace(char s[], char chr, char repl_chr)
216
{
217
     int i=0;
218
     while(s[i]!='\0')
219
     {
220
           if(s[i]==chr)
221
           {
222
               s[i]=repl_chr;
223
           }
224
           i++;
225
     }
226
          //printf("%s",s);
227
}
228
 
229
char * read_stp (){
230
        char buf[1024];
231
        char * result=NULL;
232
        fflush(to_stp);
233
        while(1) {
234
                fgets(buf, sizeof(buf), from_stp);
235
                if(strstr(buf, "ERROR") != NULL) {
236
                        printf("\tERROR\n");
237
                        printf("'%s'\n", buf);
238
                        exit(1);
239
                }
240
                if(strstr(buf, "RESULT:") != NULL) {
241
                        result=strstr(buf, "RESULT:");
242
 
243
                        break;
244
 
245
                }
246
 
247
                if(!strcmp(buf, "\n"))   break;
248
                if(feof(from_stp)) {
249
                        fprintf(stderr, "saw eof from quartus_stp\n");
250
                        exit(1);
251
                }
252
                if(ferror(from_stp)) {
253
                        fprintf(stderr, "saw error from quartus_stp\n");
254
                        exit(1);
255
              }
256
        }
257
        if(result){
258
                char * r= result+7;
259
                strreplace(r, '\n', 0);
260
                return r;
261
        }
262
        return 0;
263
}
264
 
265
 
266
 
267
 
268
void return_dr (unsigned *out) {
269
 
270
        char *ptr;
271
        fprintf(to_stp,"puts \"RESULT:$data\"\n");
272
        ptr=read_stp();
273
        //printf("saw: '%s'\n", ptr);
274
        while(*ptr=='t' || *ptr=='c'  || *ptr=='l' || *ptr=='>' || *ptr==' ' ) ptr++;
275
 
276
        *out= strtol(ptr,NULL,16);
277
}
278
 
279
void return_dr_long (unsigned *out, int words) {
280
 
281
        char *ptr;
282
        fprintf(to_stp,"puts \"RESULT:$data\"\n");
283
        ptr=read_stp();
284
        //printf("saw: '%s'\n", ptr);
285
        while(*ptr=='t' || *ptr=='c'  || *ptr=='l' || *ptr=='>' || *ptr==' ' ) ptr++;
286
 
287
        hexcut( ptr, out, words );
288
}
289
 
290
 
291
void jtag_vir(unsigned vir) {
292
        fprintf(to_stp,"device_lock -timeout 10000\n");
293
        fprintf(to_stp,"device_virtual_ir_shift -instance_index %d -ir_value %x -no_captured_ir_value\n",index_num,vir);
294
        fprintf(to_stp,"catch {device_unlock}\n");
295
}
296
 
297
 
298
void jtag_vdr(unsigned sz, unsigned bits, unsigned *out) {
299
        char hexstring[1000];
300
 
301
        hextostring( hexstring, &bits,  1, sz );
302
        if (!out){
303
                fprintf(to_stp,"device_lock -timeout 10000\n");
304
                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);
305
                //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);
306
                fprintf(to_stp,"catch {device_unlock}\n");
307
        }else{
308
                fprintf(to_stp,"device_lock -timeout 10000\n");
309
                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);
310
                fprintf(to_stp,"catch {device_unlock}\n");
311
                return_dr (out);
312
        }
313
}
314
 
315
void jtag_vdr_long(unsigned sz, unsigned * bits, unsigned *out, int words) {
316
        char hexstring[1000];
317
 
318
        hextostring( hexstring, bits,  words, sz );
319
 
320
        if (!out){
321
                fprintf(to_stp,"device_lock -timeout 10000\n");
322
                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);
323
                //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);
324
 
325
                fprintf(to_stp,"catch {device_unlock}\n");
326
        }else{
327
                fprintf(to_stp,"device_lock -timeout 10000\n");
328
                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);
329
                fprintf(to_stp,"catch {device_unlock}\n");
330
                return_dr_long (out,words);
331
        }
332
 
333
}
334
 
335
 
336
void closeport(){
337
        fprintf(to_stp,"catch {device_unlock}\n");
338
        fprintf(to_stp,"catch {close_device}\n");
339
        fflush(to_stp);
340
}
341
 
342
 
343
 
344
#ifdef DEBUG_JTAG
345
 
346
void turn_on_led(){
347
        unsigned out;
348
        fprintf(to_stp, "device_lock -timeout 10000\n");
349
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 1 -no_captured_ir_value\n");
350
        fprintf(to_stp,"device_virtual_dr_shift -dr_value 3 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
351
        //fprintf(to_stp,"device_virtual_dr_shift -dr_value 0 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
352
        fprintf(to_stp,"catch {device_unlock}\n");
353
        jtag_vdr(2, 0, &out);
354
        fprintf(to_stp, "device_lock -timeout 10000\n");
355
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 0 -no_captured_ir_value\n");
356
 
357
        printf("outs= %d \n",out);
358
        fprintf(to_stp,"catch {device_unlock}\n");
359
        fflush(to_stp);
360
        //run();
361
 
362
}
363
 
364
 
365
void turn_off_led(){
366
        unsigned out;
367
        fprintf(to_stp, "device_lock -timeout 10000\n");
368
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 1 -no_captured_ir_value\n");
369
        fprintf(to_stp,"device_virtual_dr_shift -dr_value 3 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
370
        //fprintf(to_stp,"device_virtual_dr_shift -dr_value 2 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
371
        fprintf(to_stp,"catch {device_unlock}\n");
372
        jtag_vdr(2, 3, &out);
373
        fprintf(to_stp, "device_lock -timeout 10000\n");
374
        printf("outs= %d \n",out);
375
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 0 -no_captured_ir_value\n");
376
        //fprintf(to_stp, "puts \"device_name is $chip_name\\n\";\n");
377
        fprintf(to_stp,"catch {device_unlock}\n");
378
        fflush(to_stp);
379
        //run();
380
}
381
 
382
 
383
 
384
 
385
 
386
int main(){
387
        int c=0;
388
        jtag_init("DE-SoC *","@2*"); // fpr DE10-nano
389
        while (c==0 || c== 1){
390
                 printf("Enter 1: to on, 0: to off, else to quit:\n");
391
                 scanf ("%d",&c);
392
                 if(c==0){printf("\toff\n"); turn_off_led();}
393
                 else if (c== 1){printf("\ton\n"); turn_on_led();}
394
                 else break;
395
 
396
        }
397
 
398
        closeport();
399
        fclose(from_stp);
400
        fclose(to_stp);
401
        from_stp = (FILE *) NULL;
402
        to_stp = (FILE *) NULL;
403
 
404
        return 0;
405
 
406
 
407
 
408
}
409
 
410
#endif
411
 
412
 

powered by: WebSVN 2.1.0

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