1 | /* |
---|
2 | * Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.com/license/LICENSE. |
---|
7 | * |
---|
8 | * $Id$ |
---|
9 | */ |
---|
10 | |
---|
11 | #include <stdio.h> |
---|
12 | #include <stdlib.h> |
---|
13 | #include <string.h> |
---|
14 | |
---|
15 | #include "ptf.h" |
---|
16 | #include "bridges.h" |
---|
17 | #include "clocks.h" |
---|
18 | #include "devices.h" |
---|
19 | #include "memory.h" |
---|
20 | #include "output.h" |
---|
21 | #include "linkcmds.h" |
---|
22 | |
---|
23 | #define NIOS2GEN_PACKAGE PACKAGE |
---|
24 | #define NIOS2GEN_VERSION VERSION |
---|
25 | |
---|
26 | #include "getopt.h" |
---|
27 | |
---|
28 | /********************************************************/ |
---|
29 | |
---|
30 | void store_ptf_parent(struct ptf_item *pi, void *arg) |
---|
31 | { |
---|
32 | struct ptf *p = pi->item[pi->level-1]; |
---|
33 | *(struct ptf **)arg = p; |
---|
34 | } |
---|
35 | |
---|
36 | /********************************************************/ |
---|
37 | |
---|
38 | void store_ptf_ptr(struct ptf_item *pi, void *arg) |
---|
39 | { |
---|
40 | struct ptf *p = pi->item[pi->level]; |
---|
41 | *(struct ptf **)arg = p; |
---|
42 | } |
---|
43 | |
---|
44 | /********************************************************/ |
---|
45 | |
---|
46 | void printf_ptf_value(struct ptf_item *pi, void *arg) |
---|
47 | { |
---|
48 | struct ptf *p = pi->item[pi->level]; |
---|
49 | printf(*(char **)arg, p->value); |
---|
50 | } |
---|
51 | |
---|
52 | /********************************************************/ |
---|
53 | |
---|
54 | void read_include_file(struct ptf_item *pi, void *arg) |
---|
55 | { |
---|
56 | struct ptf *inc, *next; |
---|
57 | struct ptf *p = pi->item[pi->level]; |
---|
58 | |
---|
59 | inc = ptf_parse_file(p->value); |
---|
60 | |
---|
61 | if(inc == NULL) |
---|
62 | { |
---|
63 | fprintf(stderr, "Warning: couldn't parse included '%s'.\n", p->value); |
---|
64 | return; |
---|
65 | }; |
---|
66 | |
---|
67 | printf("Successfully read included PTF file %s.\n", p->value); |
---|
68 | |
---|
69 | next = p->next; |
---|
70 | for(p->next = inc; p->next != NULL; p = p->next); |
---|
71 | p->next = next; |
---|
72 | } |
---|
73 | |
---|
74 | /********************************************************/ |
---|
75 | |
---|
76 | void version(FILE *f) |
---|
77 | { |
---|
78 | fprintf(f, |
---|
79 | "nios2gen (" __DATE__ ")\n" |
---|
80 | " Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de\n" |
---|
81 | "\n" |
---|
82 | " The license and distribution terms for this file may be\n" |
---|
83 | " found in the file LICENSE in this distribution or at\n" |
---|
84 | " http://www.rtems.com/license/LICENSE.\n" |
---|
85 | , VERSION |
---|
86 | ); |
---|
87 | } |
---|
88 | |
---|
89 | void usage (FILE *f, char *errmsg) |
---|
90 | { |
---|
91 | char *info = "Purpose:\n Create RTEMS nios2 BSP configuration from Altera SOPC description\n"; |
---|
92 | if(errmsg) info=errmsg; |
---|
93 | version(f); |
---|
94 | fprintf(f,"\n%s\nUsage: nios2gen [OPTIONS] <SOPC PTF> [CONFIG PTF] [CONFIG PTF]...\n", info); |
---|
95 | fprintf(f," -h --help Print help and exit\n"); |
---|
96 | fprintf(f," -V --version Print version and exit\n"); |
---|
97 | fprintf(f," -bFILENAME --bspheader=FILENAME Output BSP configuration header file (default='sopc.h')\n"); |
---|
98 | fprintf(f," -B --no-bspheader Do not output BSP configuration header file\n"); |
---|
99 | fprintf(f," -lFILENAME --linkcmds=FILENAME Output linker script (default='linkcmds')\n"); |
---|
100 | fprintf(f," -L --no-linkcmds Do not output linker script\n"); |
---|
101 | fprintf(f," -pFILENAME --parsed=FILENAME Output PTF contents as long list (default: don't)\n"); |
---|
102 | fprintf(f," -P --no-parsed Do not output PTF contents as long list\n"); |
---|
103 | fprintf(f," -q --quiet Do not print progress info to stdout\n\n"); |
---|
104 | fprintf(f,"Using \"-\" as the FILENAME means standard output (stdout).\n"); |
---|
105 | } |
---|
106 | |
---|
107 | /********************************************************/ |
---|
108 | |
---|
109 | int main(int argc, char *argv[]) |
---|
110 | { |
---|
111 | struct ptf *sopc, *cfg, *p, *cpu; |
---|
112 | struct ptf_item pi; |
---|
113 | device_desc *devices; |
---|
114 | bus_bridge_pair *bridges; |
---|
115 | clock_desc *clocks; |
---|
116 | memory_desc *memory; |
---|
117 | |
---|
118 | int verbose = 1; |
---|
119 | int output_order = 127; |
---|
120 | char *parsed_filename = NULL; |
---|
121 | int output_parsed = 0; |
---|
122 | char *bspheader_filename = "sopc.h"; |
---|
123 | int output_bspheader = 1; |
---|
124 | char *linkcmds_filename = "linkcmds"; |
---|
125 | int output_linkcmds = 2; |
---|
126 | |
---|
127 | optarg = 0; |
---|
128 | optind = 1; |
---|
129 | opterr = 1; |
---|
130 | optopt = '?'; |
---|
131 | |
---|
132 | if(argc > 126) |
---|
133 | { |
---|
134 | usage(stderr,"Too many commandline arguments!\n"); |
---|
135 | return -1; |
---|
136 | }; |
---|
137 | |
---|
138 | while(1) |
---|
139 | { |
---|
140 | int c, long_index = 0; |
---|
141 | static char *optstring = "hVBb:Ll:Pp:q"; |
---|
142 | |
---|
143 | static struct option long_options[] = |
---|
144 | { |
---|
145 | { "help", 0, NULL, 'h' }, |
---|
146 | { "version", 0, NULL, 'V' }, |
---|
147 | { "no-bspheader", 0, NULL, 'B' }, |
---|
148 | { "bspheader", 1, NULL, 'b' }, |
---|
149 | { "no-linkcmds", 0, NULL, 'L' }, |
---|
150 | { "linkcmds", 1, NULL, 'l' }, |
---|
151 | { "no-parsed", 0, NULL, 'P' }, |
---|
152 | { "parsed", 1, NULL, 'p' }, |
---|
153 | { "quiet", 0, NULL, 'q' }, |
---|
154 | { NULL, 0, NULL, 0 } |
---|
155 | }; |
---|
156 | |
---|
157 | c = getopt_long (argc, argv, optstring, long_options, &long_index); |
---|
158 | |
---|
159 | if(c == -1) break; /* Exit from while(1) loop */ |
---|
160 | |
---|
161 | switch(c) |
---|
162 | { |
---|
163 | case 'q': /* Be quiet */ |
---|
164 | verbose = 0; |
---|
165 | break; |
---|
166 | |
---|
167 | case 'h': /* Print help and exit */ |
---|
168 | usage(stdout,NULL); |
---|
169 | return 0; |
---|
170 | |
---|
171 | case 'V': /* Print version and exit */ |
---|
172 | version(stdout); |
---|
173 | return 0; |
---|
174 | |
---|
175 | case 'B': /* Do not output BSP configuration header file */ |
---|
176 | output_bspheader = 0; |
---|
177 | break; |
---|
178 | |
---|
179 | case 'b': /* Output BSP configuration header file */ |
---|
180 | bspheader_filename = optarg; |
---|
181 | output_bspheader = output_order; |
---|
182 | output_order--; |
---|
183 | break; |
---|
184 | |
---|
185 | case 'L': /* Do not output linker script */ |
---|
186 | output_linkcmds = 0; |
---|
187 | break; |
---|
188 | |
---|
189 | case 'l': /* Output linker script */ |
---|
190 | linkcmds_filename = optarg; |
---|
191 | output_linkcmds = output_order; |
---|
192 | output_order--; |
---|
193 | break; |
---|
194 | |
---|
195 | case 'P': /* Do not output PTF contents */ |
---|
196 | output_parsed = 0; |
---|
197 | break; |
---|
198 | |
---|
199 | case 'p': /* Output PTF contents as long list */ |
---|
200 | parsed_filename = optarg; |
---|
201 | output_parsed = output_order; |
---|
202 | output_order--; |
---|
203 | break; |
---|
204 | |
---|
205 | case 0: |
---|
206 | case '?': |
---|
207 | return -1; |
---|
208 | |
---|
209 | default: |
---|
210 | fprintf(stderr, "%s: unknown option: %c\n", NIOS2GEN_PACKAGE, c); |
---|
211 | }; |
---|
212 | }; |
---|
213 | |
---|
214 | if(optind >= argc) |
---|
215 | { |
---|
216 | usage(stderr,"No PTF specified!\n"); |
---|
217 | return -1; |
---|
218 | }; |
---|
219 | |
---|
220 | /********************************************************/ |
---|
221 | |
---|
222 | sopc = ptf_parse_file(argv[optind]); |
---|
223 | if(sopc == NULL) |
---|
224 | { |
---|
225 | fprintf(stderr, "Could not parse system description PTF '%s'.\n", argv[optind]); |
---|
226 | return -1; |
---|
227 | }; |
---|
228 | |
---|
229 | if(verbose) printf("Successfully read SOPC PTF file %s.\n", argv[optind]); |
---|
230 | |
---|
231 | /********************************************************/ |
---|
232 | |
---|
233 | cfg = NULL; |
---|
234 | |
---|
235 | for(optind++;optind<argc;optind++) |
---|
236 | { |
---|
237 | struct ptf *morecfg = ptf_parse_file(argv[optind]); |
---|
238 | |
---|
239 | if(morecfg == NULL) |
---|
240 | { |
---|
241 | fprintf(stderr, "Couldn't parse '%s'.\n", argv[optind]); |
---|
242 | return -1; |
---|
243 | }; |
---|
244 | |
---|
245 | if(verbose) printf("Successfully read config PTF file %s.\n", argv[optind]); |
---|
246 | |
---|
247 | cfg = ptf_concat(cfg, morecfg); |
---|
248 | }; |
---|
249 | |
---|
250 | /********************************************************/ |
---|
251 | /* Pull in include files specified in the configs; */ |
---|
252 | /* Only one level is read; included files are not */ |
---|
253 | /* checked for further INCLUDEs */ |
---|
254 | |
---|
255 | { |
---|
256 | struct ptf include_item = { item, "INCLUDE", 0, 0, 0 }; |
---|
257 | struct ptf_item inc_file_spec = { 1, &include_item }; |
---|
258 | ptf_match(cfg, &inc_file_spec, read_include_file, NULL); |
---|
259 | } |
---|
260 | |
---|
261 | /********************************************************/ |
---|
262 | /* Find CPU */ |
---|
263 | |
---|
264 | if(verbose) printf("Looking for usable CPUs...\n"); |
---|
265 | |
---|
266 | /* Check if a CPU has been specified in the config PTF */ |
---|
267 | { |
---|
268 | struct ptf modules = { section, "MODULES", 0, 0, 0 }; |
---|
269 | struct ptf cpu_def = { item, "CPU", 0, 0, 0 }; |
---|
270 | struct ptf_item cpu_spec = { 2, &modules, &cpu_def }; |
---|
271 | |
---|
272 | ptf_match(cfg, &cpu_spec, store_ptf_ptr, &cpu); |
---|
273 | }; |
---|
274 | |
---|
275 | /* Look for CPUs in system description PTF */ |
---|
276 | { |
---|
277 | int cpu_count; |
---|
278 | struct ptf system = { section, "SYSTEM", 0, 0, 0 }; |
---|
279 | struct ptf module = { section, "MODULE", 0, 0, 0 }; |
---|
280 | struct ptf nios2_cpu_class = { item, "class", "altera_nios2", 0, 0 }; |
---|
281 | struct ptf_item class_spec = { 3, &system, &module, &nios2_cpu_class }; |
---|
282 | |
---|
283 | if(cpu) if(cpu->value) class_spec.item[1]->value = cpu->value; |
---|
284 | |
---|
285 | cpu_count = ptf_match(sopc, &class_spec, store_ptf_parent, &cpu); |
---|
286 | |
---|
287 | if(cpu_count > 1) |
---|
288 | { |
---|
289 | fprintf(stderr, "There is more than one CPU. Please specify the one\n"); |
---|
290 | fprintf(stderr, "you want to use with this BSP in your config file.\n"); |
---|
291 | fprintf(stderr, "The available CPUs are named as follows:\n"); |
---|
292 | ptf_match(sopc, &class_spec, printf_ptf_value, " %s\n"); |
---|
293 | return -1; |
---|
294 | }; |
---|
295 | |
---|
296 | if(cpu_count == 0) |
---|
297 | { |
---|
298 | fprintf(stderr, "There is no NIOS2 cpu in the system.\n"); |
---|
299 | return -1; |
---|
300 | } |
---|
301 | }; |
---|
302 | |
---|
303 | if(verbose) |
---|
304 | { |
---|
305 | printf("Using NIOS II CPU '%s'.\n", cpu->value); |
---|
306 | printf("Only modules mastered by this CPU are considered now.\n"); |
---|
307 | }; |
---|
308 | |
---|
309 | /********************************************************/ |
---|
310 | /* Find Bridges */ |
---|
311 | |
---|
312 | if(verbose) printf("Looking for bus bridges...\n"); |
---|
313 | |
---|
314 | bridges = find_bridges(sopc); |
---|
315 | |
---|
316 | if(verbose) |
---|
317 | { |
---|
318 | if(bridges) |
---|
319 | { |
---|
320 | bus_bridge_pair *bbp; |
---|
321 | for(bbp = bridges; bbp; bbp=bbp->next) |
---|
322 | { |
---|
323 | printf("Found bridge: %s\n", bbp->mastered_by); |
---|
324 | printf(" \\_%s\n", bbp->bridges_to); |
---|
325 | }; |
---|
326 | } |
---|
327 | else |
---|
328 | { |
---|
329 | printf("No bridges present.\n"); |
---|
330 | }; |
---|
331 | }; |
---|
332 | |
---|
333 | /********************************************************/ |
---|
334 | /* Find clocks */ |
---|
335 | |
---|
336 | if(verbose) printf("Looking for clock definitions...\n"); |
---|
337 | |
---|
338 | clocks = find_clocks(sopc, cfg); |
---|
339 | |
---|
340 | if(verbose) |
---|
341 | { |
---|
342 | if(clocks) |
---|
343 | { |
---|
344 | clock_desc *cs; |
---|
345 | for(cs = clocks; cs; cs = cs->next) |
---|
346 | { |
---|
347 | printf("Found clock \"%s\" (%lu Hz), naming it %s\n", cs->name, cs->freq, cs->cfgname); |
---|
348 | }; |
---|
349 | } |
---|
350 | else |
---|
351 | { |
---|
352 | printf("No clocks present.\n"); |
---|
353 | }; |
---|
354 | }; |
---|
355 | |
---|
356 | /********************************************************/ |
---|
357 | /* Find other devices available to the selected CPU */ |
---|
358 | |
---|
359 | if(verbose) printf("Looking for devices...\n"); |
---|
360 | |
---|
361 | devices = find_devices(sopc, cfg, cpu, bridges); |
---|
362 | |
---|
363 | if(verbose) |
---|
364 | { |
---|
365 | if(devices) |
---|
366 | { |
---|
367 | device_desc *dd; |
---|
368 | for(dd = devices; dd; dd=dd->next) |
---|
369 | { |
---|
370 | printf("Found device \"%s\", naming it %s\n", dd->ptf->value, dd->cfgname); |
---|
371 | }; |
---|
372 | } |
---|
373 | else |
---|
374 | { |
---|
375 | printf("No devices present.\n"); |
---|
376 | }; |
---|
377 | }; |
---|
378 | |
---|
379 | /********************************************************/ |
---|
380 | /* Find out which devices are actually memory */ |
---|
381 | |
---|
382 | if(verbose) printf("Looking for memory...\n"); |
---|
383 | |
---|
384 | memory = find_memory(devices); |
---|
385 | |
---|
386 | if(verbose) |
---|
387 | { |
---|
388 | if(memory) |
---|
389 | { |
---|
390 | memory_desc *md; |
---|
391 | for(md = memory; md; md=md->next) |
---|
392 | { |
---|
393 | printf("Found memory in \"%s\", base=0x%08X, size=%lu bytes\n", |
---|
394 | md->dev->cfgname, |
---|
395 | md->base, md->size); |
---|
396 | }; |
---|
397 | } |
---|
398 | else |
---|
399 | { |
---|
400 | printf("None of the devices seems to provide memory?!\n"); |
---|
401 | }; |
---|
402 | }; |
---|
403 | |
---|
404 | |
---|
405 | /********************************************************/ |
---|
406 | /* Output files in the order they were specified |
---|
407 | on the command line */ |
---|
408 | |
---|
409 | { |
---|
410 | int i; |
---|
411 | for(i=0;i<3;i++) |
---|
412 | { |
---|
413 | if(output_bspheader>0 |
---|
414 | && output_bspheader>=output_linkcmds |
---|
415 | && output_bspheader>=output_parsed) |
---|
416 | { |
---|
417 | output_bspheader = 0; |
---|
418 | if(bspheader_filename == NULL || (bspheader_filename[0]=='-' && bspheader_filename[1]==0)) |
---|
419 | { |
---|
420 | fwrite_header_file(stdout, cfg, devices, clocks); |
---|
421 | } |
---|
422 | else |
---|
423 | { |
---|
424 | FILE *f = fopen(bspheader_filename, "w"); |
---|
425 | if(!f) |
---|
426 | { |
---|
427 | perror(bspheader_filename); |
---|
428 | return -1; |
---|
429 | } |
---|
430 | else |
---|
431 | { |
---|
432 | fwrite_header_file(f, cfg, devices, clocks); |
---|
433 | fclose(f); |
---|
434 | } |
---|
435 | } |
---|
436 | }; |
---|
437 | if(output_linkcmds>0 |
---|
438 | && output_linkcmds>=output_bspheader |
---|
439 | && output_linkcmds>=output_parsed) |
---|
440 | { |
---|
441 | output_linkcmds = 0; |
---|
442 | if(linkcmds_filename == NULL || (linkcmds_filename[0]=='-' && linkcmds_filename[1]==0)) |
---|
443 | { |
---|
444 | fwrite_linkcmds_file(stdout, cfg, cpu, devices, memory); |
---|
445 | } |
---|
446 | else |
---|
447 | { |
---|
448 | FILE *f = fopen(linkcmds_filename, "w"); |
---|
449 | if(!f) |
---|
450 | { |
---|
451 | perror(linkcmds_filename); |
---|
452 | return -1; |
---|
453 | } |
---|
454 | else |
---|
455 | { |
---|
456 | fwrite_linkcmds_file(f, cfg, cpu, devices, memory); |
---|
457 | fclose(f); |
---|
458 | } |
---|
459 | } |
---|
460 | }; |
---|
461 | if(output_parsed>0 |
---|
462 | && output_parsed>=output_linkcmds |
---|
463 | && output_parsed>=output_bspheader) |
---|
464 | { |
---|
465 | output_parsed = 0; |
---|
466 | if(parsed_filename == NULL || (parsed_filename[0]=='-' && parsed_filename[1]==0)) |
---|
467 | { |
---|
468 | ptf_printf(stdout, sopc, ""); |
---|
469 | } |
---|
470 | else |
---|
471 | { |
---|
472 | FILE *f = fopen(parsed_filename, "w"); |
---|
473 | if(!f) |
---|
474 | { |
---|
475 | perror(parsed_filename); |
---|
476 | return -1; |
---|
477 | } |
---|
478 | else |
---|
479 | { |
---|
480 | ptf_printf(f, sopc, ""); |
---|
481 | fclose(f); |
---|
482 | } |
---|
483 | } |
---|
484 | }; |
---|
485 | } |
---|
486 | }; |
---|
487 | |
---|
488 | if(verbose) printf("Done.\n"); |
---|
489 | |
---|
490 | return 0; |
---|
491 | } |
---|
492 | |
---|
493 | /* vi:ts=4: |
---|
494 | */ |
---|
495 | |
---|
496 | |
---|