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/] [test.c] - Blame information for rev 38

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
 
13
#include "pipe.c"
14
 
15
 
16
 
17
 
18
 
19
 
20
 
21
#define DEBUG_JTAG
22
 
23
int index_num = 127;
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
 
47
    if (*(hexstring+1)=='x' || *(hexstring+1)=='X') hexstring+=2;
48
    int size=strlen(hexstring);
49
    int hexnum= (size%8)? (size/8)+1 : size/8;
50
    for(count = 0; count < words; count++) val[count]=0;
51
 
52
    for(count = 1; count <= hexnum; count++) {
53
        start=(count*8>size)? 0 : size-count*8;
54
 
55
        sscanf(hexstring+start, "%08x", &val[count-1]);
56
        *(hexstring+start)=0;
57
    }
58
 
59
  //  printf("size=%d, hexnum=%u\n",size,hexnum);
60
 
61
 
62
    return hexnum;
63
}
64
 
65
 
66
void hexgen( char * hexstring, unsigned * val, int words ){
67
    size_t count = 0;
68
    sprintf(hexstring,"0x");
69
    for(count = 0; count < words; count++) {
70
        if(count == 0)  sprintf((hexstring+2),"%x",val[words-count-1]);
71
        else            sprintf(hexstring,"%08x",val[words-count-1]);
72
         hexstring+=strlen(hexstring);
73
   }
74
 
75
 // return hexnum;
76
}
77
 
78
void hextostring( char * hexstring, unsigned * val, int words ){
79
    size_t count = 0;
80
    //sprintf(hexstring,"0x");
81
    for(count = 0; count < words; count++) {
82
        if(count == 0)  sprintf((hexstring),"%x",val[words-count-1]);
83
        else            sprintf(hexstring,"%08x",val[words-count-1]);
84
         hexstring+=strlen(hexstring);
85
   }
86
 
87
 // return hexnum;
88
}
89
 
90
 
91
int jtag_init(char *hrdname, char *dvicname ) {
92
 
93
   /* Create a quartus_stp process, and get the list of ports */
94
 
95
   int  f_to_stp, f_from_stp;
96
   char buf[1024];
97
   char *command[] = {"quartus_stp", "-s", 0};
98
 
99
   if(from_stp != (FILE *) NULL) {
100
      fclose(from_stp);
101
      fclose(to_stp);
102
   }
103
 
104
   piped_child(command, &f_from_stp, &f_to_stp);
105
 
106
   from_stp = fdopen(f_from_stp, "r");
107
   to_stp = fdopen(f_to_stp, "w");
108
 
109
   if(from_stp == (FILE *) NULL || to_stp == (FILE *) NULL) {
110
      fprintf(stderr, "jtag_init: can't communicate with quartus_stp process\n");
111
      fclose(from_stp);
112
      fclose(to_stp);
113
      from_stp = (FILE *) NULL;
114
      to_stp = (FILE *) NULL;
115
      return(1);
116
   }
117
 
118
 
119
 
120
   while(1) {
121
      fgets(buf, sizeof(buf), from_stp);
122
        if(strstr(buf, "ERROR") != NULL) {
123
                printf("\tERROR\n");
124
                printf("'%s'\n", buf);
125
                exit(1);
126
        }
127
 
128
 
129
 
130
      if(!strcmp(buf, "\n"))
131
         break;
132
      if(feof(from_stp)) {
133
         fprintf(stderr, "saw eof from quartus_stp\n");
134
         exit(1);
135
      }
136
 
137
      if(ferror(from_stp)) {
138
         fprintf(stderr, "saw error from quartus_stp\n");
139
         exit(1);
140
      }
141
   }
142
 
143
   fprintf(to_stp, "foreach name [get_hardware_names] {\n");
144
   fprintf(to_stp, "  puts $name\n");
145
   fprintf(to_stp, "  if { [string match \"*%s*\" $name] } {\n", hrdname);
146
   fprintf(to_stp, "    set hardware_name $name\n");
147
   fprintf(to_stp, "  }\n");
148
   fprintf(to_stp, "}\n");
149
   fprintf(to_stp, "puts \"\\nhardware_name is $hardware_name\";\n");
150
   fprintf(to_stp, "foreach name [get_device_names -hardware_name $hardware_name] {\n");
151
   fprintf(to_stp, "  if { [string match \"*%s*\" $name] } {\n",dvicname);
152
   fprintf(to_stp, "    set chip_name $name\n");
153
   fprintf(to_stp, "  }\n");
154
   fprintf(to_stp, "}\n");
155
   fprintf(to_stp, "puts \"device_name is $chip_name\\n\";\n");
156
   fprintf(to_stp, "open_device -hardware_name $hardware_name -device_name $chip_name\n");
157
 
158
   fflush(to_stp);
159
 
160
   while(1) {
161
      fgets(buf, sizeof(buf), from_stp);
162
 
163
        if(strstr(buf, "ERROR") != NULL) {
164
                printf("\tERROR\n");
165
                printf("'%s'\n", buf);
166
                exit(1);
167
        }
168
 
169
      if(!strcmp(buf, "\n"))
170
         break;
171
      if(feof(from_stp)) {
172
         fprintf(stderr, "saw eof from quartus_stp\n");
173
         exit(1);
174
      }
175
      if(ferror(from_stp)) {
176
         fprintf(stderr, "saw error from quartus_stp\n");
177
         exit(1);
178
      }
179
   }
180
         return 0;
181
 
182
}
183
 
184
 
185
 
186
 
187
 
188
 
189
void return_dr (unsigned *out) {
190
        char buf[1024];
191
        char *ptr=buf;
192
        fprintf(to_stp,"puts $data\n");
193
        fflush(to_stp);
194
        fgets(buf, sizeof(buf), from_stp);
195
        while(*ptr=='t' || *ptr=='c'  || *ptr=='l' || *ptr=='>' || *ptr==' ' ) ptr++;
196
        //printf("saw: '%s'\n", ptr);
197
        *out= strtol(ptr,NULL,16);
198
}
199
 
200
void return_dr_long (unsigned *out, int words) {
201
        char buf[1024];
202
        char *ptr=buf;
203
        fprintf(to_stp,"puts $data\n");
204
        fflush(to_stp);
205
        fgets(buf, sizeof(buf), from_stp);
206
        while(*ptr=='t' || *ptr=='c'  || *ptr=='l' || *ptr=='>' || *ptr==' ' ) ptr++;
207
        //printf("saw: '%s'\n", ptr);
208
        hexcut( ptr, out, words );
209
}
210
 
211
 
212
void jtag_vir(unsigned vir) {
213
        fprintf(to_stp,"device_lock -timeout 10000\n");
214
        fprintf(to_stp,"device_virtual_ir_shift -instance_index %d -ir_value %x -no_captured_ir_value\n",index_num,vir);
215
        fprintf(to_stp,"catch {device_unlock}\n");
216
}
217
 
218
 
219
void jtag_vdr(unsigned sz, unsigned bits, unsigned *out) {
220
        if (!out){
221
                fprintf(to_stp,"device_lock -timeout 10000\n");
222
                fprintf(to_stp,"device_virtual_dr_shift -dr_value %x -instance_index %d  -length %d -no_captured_dr_value -value_in_hex\n",bits,index_num,sz);
223
                fprintf(to_stp,"catch {device_unlock}\n");
224
        }else{
225
                fprintf(to_stp,"device_lock -timeout 10000\n");
226
                fprintf(to_stp,"set data [device_virtual_dr_shift -dr_value %x -instance_index %d  -length %d  -value_in_hex]\n",bits,index_num,sz);
227
                fprintf(to_stp,"catch {device_unlock}\n");
228
                return_dr (out);
229
        }
230
}
231
 
232
void jtag_vdr_long(unsigned sz, unsigned * bits, unsigned *out, int words) {
233
        char hexstring[1000];
234
        //printf("jtag_vdr_long(unsigned %d, unsigned %s, unsigned %s, int %d)",sz,bits,out,words );
235
        hextostring( hexstring, bits,  words );
236
 
237
        if (!out){
238
                fprintf(to_stp,"device_lock -timeout 10000\n");
239
                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);
240
                //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);
241
                fprintf(to_stp,"catch {device_unlock}\n");
242
        }else{
243
                fprintf(to_stp,"device_lock -timeout 10000\n");
244
                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);
245
                fprintf(to_stp,"catch {device_unlock}\n");
246
                return_dr_long (out,words);
247
        }
248
 
249
}
250
 
251
 
252
void closeport(){
253
        fprintf(to_stp,"catch {device_unlock}\n");
254
        fprintf(to_stp,"catch {close_device}\n");
255
        fflush(to_stp);
256
}
257
 
258
 
259
 
260
void vdr_large (unsigned sz, char * string, char *out){
261
        int words= (sz%32)? (sz/32)+1 : sz/32;
262
        unsigned  val[64],val_o[64];
263
        printf("data=%s\n",string);
264
        hexcut(string, val, words );
265
 
266
 
267
        if( out == 0) {
268
                  jtag_vdr_long(sz,val,0,words);
269
                return;
270
        }
271
        jtag_vdr_long(sz,val,val_o,words);
272
 
273
        hexgen( out, val_o, words );
274
 
275
 
276
 
277
}
278
 
279
void turn_on_led(){
280
        unsigned out;
281
        //fprintf(to_stp, "device_lock -timeout 10000\n");
282
        //fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 1 -no_captured_ir_value\n");
283
        jtag_vir(1);
284
        //fprintf(to_stp,"catch {device_unlock}\n");            
285
        //fprintf(to_stp,"device_virtual_dr_shift -dr_value 3 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
286
        char string[100]="0";
287
        vdr_large(2, string, NULL);
288
        //fprintf(to_stp, "device_lock -timeout 10000\n");
289
        //fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 0 -no_captured_ir_value\n");
290
        jtag_vir(0);
291
        //printf("outs= %d \n",out);
292
        //fprintf(to_stp,"catch {device_unlock}\n");
293
        fflush(to_stp);
294
        //run();
295
 
296
}
297
 
298
 
299
void turn_off_led(){
300
        unsigned out;
301
/*
302
        fprintf(to_stp, "device_lock -timeout 10000\n");
303
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 1 -no_captured_ir_value\n");
304
        fprintf(to_stp,"device_virtual_dr_shift -dr_value 3 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
305
        fprintf(to_stp,"device_virtual_dr_shift -dr_value 2 -instance_index 127  -length 2 -no_captured_dr_value -value_in_hex\n");
306
        fprintf(to_stp,"device_virtual_ir_shift -instance_index 127 -ir_value 0 -no_captured_ir_value\n");
307
        fprintf(to_stp,"catch {device_unlock}\n");
308
 
309
*/
310
        //run();
311
 
312
        char string[100]="3";
313
        jtag_vir(1);
314
        //vdr_large(2, string, NULL);
315
        jtag_vdr(2,3,NULL);
316
        jtag_vir(0);
317
        //printf("outs= %d \n",out);
318
 
319
        fflush(to_stp);
320
}
321
 
322
 
323
 
324
 
325
 
326
int main(){
327
        int c=0;
328
        jtag_init("DE-SoC *","@2*"); // fpr DE10-nano
329
        while (c==0 || c== 1){
330
                 printf("Enter 1: to on, 0: to off, else to quit:\n");
331
                 scanf ("%d",&c);
332
                 if(c==0){printf("\toff\n"); turn_off_led();}
333
                 else if (c== 1){printf("\ton\n"); turn_on_led();}
334
                 else break;
335
 
336
        }
337
 
338
        closeport();
339
        fclose(from_stp);
340
        fclose(to_stp);
341
        from_stp = (FILE *) NULL;
342
        to_stp = (FILE *) NULL;
343
 
344
        return 0;
345
 
346
 
347
 
348
}
349
 
350
 
351
 
352
 

powered by: WebSVN 2.1.0

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