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 "output.h" |
---|
20 | |
---|
21 | /********************************************************/ |
---|
22 | |
---|
23 | void store_ptf_parent(struct ptf_item *pi, void *arg) |
---|
24 | { |
---|
25 | struct ptf *p = pi->item[pi->level-1]; |
---|
26 | *(struct ptf **)arg = p; |
---|
27 | } |
---|
28 | |
---|
29 | /********************************************************/ |
---|
30 | |
---|
31 | void store_ptf_ptr(struct ptf_item *pi, void *arg) |
---|
32 | { |
---|
33 | struct ptf *p = pi->item[pi->level]; |
---|
34 | *(struct ptf **)arg = p; |
---|
35 | } |
---|
36 | |
---|
37 | /********************************************************/ |
---|
38 | |
---|
39 | void printf_ptf_value(struct ptf_item *pi, void *arg) |
---|
40 | { |
---|
41 | struct ptf *p = pi->item[pi->level]; |
---|
42 | printf(*(char **)arg, p->value); |
---|
43 | } |
---|
44 | |
---|
45 | /********************************************************/ |
---|
46 | |
---|
47 | void read_include_file(struct ptf_item *pi, void *arg) |
---|
48 | { |
---|
49 | struct ptf *inc, *next; |
---|
50 | struct ptf *p = pi->item[pi->level]; |
---|
51 | |
---|
52 | inc = ptf_parse_file(p->value); |
---|
53 | |
---|
54 | if(inc == NULL) |
---|
55 | { |
---|
56 | fprintf(stderr, "Warning: couldn't parse included '%s'.\n", p->value); |
---|
57 | return; |
---|
58 | }; |
---|
59 | |
---|
60 | printf("Successfully read included PTF file %s.\n", p->value); |
---|
61 | |
---|
62 | next = p->next; |
---|
63 | for(p->next = inc; p->next != NULL; p = p->next); |
---|
64 | p->next = next; |
---|
65 | } |
---|
66 | |
---|
67 | void usage() |
---|
68 | { |
---|
69 | printf( |
---|
70 | "Usage: nios2gen [PTFFILE]\n" |
---|
71 | "Generate BSP data based upon PTF file from SOPC Builder.\n" |
---|
72 | "\n" |
---|
73 | "Please specify the name of a nios2gen PTF file that describes where to\n" |
---|
74 | "find the system description PTF from SOPC Builder on the command line.\n" |
---|
75 | ); |
---|
76 | } |
---|
77 | |
---|
78 | void version() |
---|
79 | { |
---|
80 | printf( |
---|
81 | "RTEMS/NIOS nios2gen\n" |
---|
82 | " Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de\n" |
---|
83 | "\n" |
---|
84 | " The license and distribution terms for this file may be\n" |
---|
85 | " found in the file LICENSE in this distribution or at\n" |
---|
86 | " http://www.rtems.com/license/LICENSE.\n" |
---|
87 | ); |
---|
88 | } |
---|
89 | |
---|
90 | /********************************************************/ |
---|
91 | |
---|
92 | int main(int argc, char *argv[]) |
---|
93 | { |
---|
94 | struct ptf *sopc, *cfg, *p, *cpu; |
---|
95 | struct ptf_item pi; |
---|
96 | device_desc *devices; |
---|
97 | bus_bridge_pair *bridges; |
---|
98 | clock_desc *clocks; |
---|
99 | |
---|
100 | if (argc<2) |
---|
101 | { |
---|
102 | usage(); |
---|
103 | return -1; |
---|
104 | }; |
---|
105 | |
---|
106 | if ( !strcmp(argv[1], "--help") || !strcmp(argv[1],"-?") ) { |
---|
107 | usage(); |
---|
108 | return 0; |
---|
109 | }; |
---|
110 | |
---|
111 | if ( !strcmp(argv[1], "--version") ) { |
---|
112 | version(); |
---|
113 | return 0; |
---|
114 | }; |
---|
115 | |
---|
116 | cfg = ptf_parse_file(argv[1]); |
---|
117 | if(cfg == NULL) |
---|
118 | { |
---|
119 | fprintf(stderr, "Couldn't parse '%s'.\n", argv[1]); |
---|
120 | return -1; |
---|
121 | }; |
---|
122 | |
---|
123 | printf("Successfully read config PTF file %s.\n", argv[1]); |
---|
124 | |
---|
125 | /********************************************************/ |
---|
126 | |
---|
127 | { |
---|
128 | struct ptf include_item = { item, "INCLUDE", 0, 0, 0 }; |
---|
129 | struct ptf_item inc_file_spec = { 1, &include_item }; |
---|
130 | ptf_match(cfg, &inc_file_spec, read_include_file, NULL); |
---|
131 | } |
---|
132 | |
---|
133 | /********************************************************/ |
---|
134 | |
---|
135 | { |
---|
136 | struct ptf *p; |
---|
137 | struct ptf sopc_ptf_item = { item, "SOPC_PTF", 0, 0, 0 }; |
---|
138 | struct ptf_item sopc_ptf_spec = { 1, &sopc_ptf_item }; |
---|
139 | ptf_match(cfg, &sopc_ptf_spec, store_ptf_ptr, &p); |
---|
140 | |
---|
141 | if(p == NULL) |
---|
142 | { |
---|
143 | fprintf(stderr, "Missing 'SOPC_PTF' filename in %s!\n", argv[1]); |
---|
144 | return -1; |
---|
145 | }; |
---|
146 | |
---|
147 | sopc = ptf_parse_file(p->value); |
---|
148 | if(sopc == NULL) |
---|
149 | { |
---|
150 | fprintf(stderr, "Could not parse SOPC_PTF '%s'.\n", p->value); |
---|
151 | return -1; |
---|
152 | }; |
---|
153 | |
---|
154 | printf("Successfully read SOPC PTF file %s.\n", p->value); |
---|
155 | }; |
---|
156 | |
---|
157 | /********************************************************/ |
---|
158 | /* Find CPU */ |
---|
159 | |
---|
160 | printf("Looking for usable CPUs...\n"); |
---|
161 | |
---|
162 | { |
---|
163 | struct ptf modules = { section, "MODULES", 0, 0, 0 }; |
---|
164 | struct ptf cpu_def = { item, "CPU", 0, 0, 0 }; |
---|
165 | struct ptf_item cpu_spec = { 2, &modules, &cpu_def }; |
---|
166 | |
---|
167 | ptf_match(cfg, &cpu_spec, store_ptf_ptr, &cpu); |
---|
168 | }; |
---|
169 | |
---|
170 | { |
---|
171 | int cpu_count; |
---|
172 | struct ptf system = { section, "SYSTEM", 0, 0, 0 }; |
---|
173 | struct ptf module = { section, "MODULE", 0, 0, 0 }; |
---|
174 | struct ptf nios2_cpu_class = { item, "class", "altera_nios2", 0, 0 }; |
---|
175 | struct ptf_item class_spec = { 3, &system, &module, &nios2_cpu_class }; |
---|
176 | |
---|
177 | if(cpu) if(cpu->value) class_spec.item[1]->value = cpu->value; |
---|
178 | |
---|
179 | cpu_count = ptf_match(sopc, &class_spec, store_ptf_parent, &cpu); |
---|
180 | |
---|
181 | if(cpu_count > 1) |
---|
182 | { |
---|
183 | printf("There is more than one CPU. Please specify the one\n"); |
---|
184 | printf("you want to use with this BSP in your config file.\n"); |
---|
185 | printf("The available CPUs are named as follows:\n"); |
---|
186 | ptf_match(sopc, &class_spec, printf_ptf_value, " %s\n"); |
---|
187 | return -1; |
---|
188 | }; |
---|
189 | |
---|
190 | if(cpu_count == 0) |
---|
191 | { |
---|
192 | printf("There is no NIOS2 cpu in the system.\n"); |
---|
193 | return -1; |
---|
194 | } |
---|
195 | }; |
---|
196 | |
---|
197 | printf("Using NIOS II CPU '%s'.\n", cpu->value); |
---|
198 | printf("Only modules mastered by this CPU are considered now.\n"); |
---|
199 | |
---|
200 | /********************************************************/ |
---|
201 | /* Find clocks */ |
---|
202 | |
---|
203 | printf("Looking for clock definitions...\n"); |
---|
204 | |
---|
205 | clocks = find_clocks(sopc, cfg); |
---|
206 | |
---|
207 | if(clocks) |
---|
208 | { |
---|
209 | clock_desc *cs; |
---|
210 | for(cs = clocks; cs; cs = cs->next) |
---|
211 | { |
---|
212 | printf("Found clock: %s (%lu Hz)\n", cs->name, cs->freq); |
---|
213 | }; |
---|
214 | } |
---|
215 | else |
---|
216 | { |
---|
217 | printf("No clocks present.\n"); |
---|
218 | }; |
---|
219 | |
---|
220 | /********************************************************/ |
---|
221 | /* Find Bridges */ |
---|
222 | |
---|
223 | printf("Looking for bus bridges...\n"); |
---|
224 | |
---|
225 | bridges = find_bridges(sopc); |
---|
226 | |
---|
227 | if(bridges) |
---|
228 | { |
---|
229 | bus_bridge_pair *bbp; |
---|
230 | for(bbp = bridges; bbp; bbp=bbp->next) |
---|
231 | { |
---|
232 | printf("Found bridge: %s\n", bbp->mastered_by); |
---|
233 | printf(" \\_%s\n", bbp->bridges_to); |
---|
234 | }; |
---|
235 | } |
---|
236 | else |
---|
237 | { |
---|
238 | printf("No bridges present.\n"); |
---|
239 | }; |
---|
240 | |
---|
241 | /********************************************************/ |
---|
242 | /* Find other devices available to the selected CPU */ |
---|
243 | |
---|
244 | devices = find_devices(sopc, cfg, cpu, bridges); |
---|
245 | |
---|
246 | fwrite_header_file(stdout, cfg, devices, clocks); |
---|
247 | |
---|
248 | // ptf_printf(stdout, ptf, ""); |
---|
249 | // ptf_printf(stdout, cfg, ""); |
---|
250 | |
---|
251 | return 0; |
---|
252 | } |
---|
253 | |
---|
254 | /* vi:ts=4: |
---|
255 | */ |
---|
256 | |
---|
257 | |
---|