forked from altera-opensource/sopc2dts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSopc2DTS.java
558 lines (538 loc) · 18.3 KB
/
Sopc2DTS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
sopc2dts - Devicetree generation for Altera systems
Copyright (C) 2011 - 2015 Walter Goossens <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JFrame;
import org.xml.sax.SAXException;
import sopc2dts.LogEntry;
import sopc2dts.LogListener;
import sopc2dts.Logger;
import sopc2dts.Logger.LogLevel;
import sopc2dts.generators.AbstractSopcGenerator;
import sopc2dts.generators.GeneratorFactory;
import sopc2dts.generators.SopcCreateHeaderFilesImitator;
import sopc2dts.gui.Sopc2DTSGui;
import sopc2dts.lib.AvalonSystem;
import sopc2dts.lib.BoardInfo;
import sopc2dts.lib.BoardInfo.AltrStyle;
import sopc2dts.lib.components.BasicComponent;
import sopc2dts.lib.components.base.SICBridge;
import sopc2dts.lib.SopcComponentLib;
import sopc2dts.parsers.BasicSystemLoader;
public class Sopc2DTS implements LogListener {
private boolean useStdOutErr = true;
protected Vector<CommandLineOption> vOptions = new Vector<CommandLineOption>();
protected CLParameter excludeTimeStamp = new CLParameter("" + false);
protected CLParameter showHelp = new CLParameter(""+false);
protected CLParameter showVersion = new CLParameter("" + false);
protected CLParameter verbose = new CLParameter("" + false);
protected CLParameter mimicAlteraTools = new CLParameter("" + false);
protected CLParameter inputFileName = new CLParameter("");
protected CLParameter boardFileName = new CLParameter("");
protected CLParameter bridgeRemoval = new CLParameter("balanced");
protected CLParameter outputFileName = new CLParameter("");
protected CLParameter outputType = new CLParameter("dts");
protected CLParameter overlayTarget = new CLParameter("");
protected CLParameter externalFpgaConfig = new CLParameter("");
protected CLParameter firmwareName = new CLParameter("");
protected CLParameter pov = new CLParameter("");
protected CLParameter povType = new CLParameter("cpu");
protected CLParameter bootargs = new CLParameter("");
protected CLParameter sopcParameters = new CLParameter("none");
protected CLParameter sort = new CLParameter("");
protected CLParameter ranges = new CLParameter("");
protected CLParameter forceALTR = new CLParameter("" + false);
protected CLParameter forcealtr = new CLParameter("" + false);
protected CLParameter gui = new CLParameter("" + false);
protected CLParameter showClocks = new CLParameter(""+false);
protected CLParameter showConduit = new CLParameter(""+false);
protected CLParameter showReset = new CLParameter(""+false);
protected CLParameter showStreaming = new CLParameter(""+false);
protected CLParameter extraComponentLibs = new CLParameter("");
protected static final String programName = "sopc2dts";
private static String programVersion = Package.getPackage("sopc2dts").getImplementationVersion();;
private Vector<String> vInfoFileNames = new Vector<String>();
private Vector<String> vdtsiFileNames = new Vector<String>();
private Vector<String> vExtraComponentLibs = new Vector<String>();
/**
* @param args
*/
public static void main(String[] args) {
Sopc2DTS s2d = new Sopc2DTS();
int res = 1;
try {
if(s2d.parseCmdLine(args))
{
res = s2d.go();
}
} catch(Exception e) {
Logger.logException(e);
s2d.printUsage();
}
if(res>=0)
{
System.exit(res);
}
}
public Sopc2DTS() {
Logger.addLogListener(this);
vOptions.add(new CommandLineOption("board", "b", boardFileName, true, false,"board description file (can be used multiple times)", "boardinfo file"));
vOptions.add(new CommandLineOption("bridge-ranges", null, ranges, true, false,"What to describe in bridges address translations", "{none,bridge,child}"));
vOptions.add(new CommandLineOption("bridge-removal", null, bridgeRemoval, true, false,"Bridge removal strategy", "{all,balanced,none}"));
vOptions.add(new CommandLineOption("help", "h", showHelp, false,false,"Show this usage info and exit",null));
vOptions.add(new CommandLineOption("verbose", "v", verbose, false,false,"Show Lots of debugging info", null));
vOptions.add(new CommandLineOption("force-ALTR",null,forceALTR, false,false,"Try to make sure all compatibles and property names use uppercase 'ALTR'", null));
vOptions.add(new CommandLineOption("force-altr",null,forcealtr, false,false,"Try to make sure all compatibles and property names use lowercase 'altr'", null));
vOptions.add(new CommandLineOption("gui", "g", gui, false,false,"Run in gui mode", null));
vOptions.add(new CommandLineOption("clocks", "c", showClocks, false,false,"Show clocks in Device Tree Source / graph", null));
vOptions.add(new CommandLineOption("conduits", null, showConduit, false,false,"Show conduit interfaces in graph", null));
vOptions.add(new CommandLineOption("version", null,showVersion, false,false,"Show version information and exit", null));
vOptions.add(new CommandLineOption("mimic-sopc-create-header-files" ,"m", mimicAlteraTools, false,false,"Try to (mis)behave like sopc-create-header-files does", null));
vOptions.add(new CommandLineOption("no-timestamp", null, excludeTimeStamp, false, false, "Don't add a timestamp to generated files", null));
vOptions.add(new CommandLineOption("input", "i", inputFileName, true, true, "The sopcinfo file (optional in gui mode)", "sopcinfo file"));
vOptions.add(new CommandLineOption("output", "o", outputFileName, true, false,"The output filename","filename"));
vOptions.add(new CommandLineOption("overlay-target",null, overlayTarget,true, false,"Target PATH for dt-overlay","node-label"));
vOptions.add(new CommandLineOption("firmware-name",null, firmwareName,true, false,"DTS overlay FPGA configuration RBF name, if not specified we assume external configuration","node-label"));
vOptions.add(new CommandLineOption("pov", "p", pov, true, false,"The point of view to generate from. Defaults to the first cpu found", "component name"));
vOptions.add(new CommandLineOption("pov-type", null, povType, true, false,"The point of view device type", "{cpu,pci,overlay}"));
vOptions.add(new CommandLineOption("reset", null, showReset, false,false,"Show reset interfaces in graph", null));
vOptions.add(new CommandLineOption("sort", "s", sort, true, false,"Sort components by", "{none,address,name,label}"));
vOptions.add(new CommandLineOption("streaming", null, showStreaming, false,false,"Show streaming interfaces in graph", null));
vOptions.add(new CommandLineOption("type", "t", outputType, true, false,"The type of output to generate", "{dtb,dtb-hex8,dtb-hex32,dtb-char-arr,dts,uboot,kernel}"));
vOptions.add(new CommandLineOption("bootargs", null,bootargs, true, false,"Default kernel arguments for the \"chosen\" section of the DTS", "kernel-args"));
vOptions.add(new CommandLineOption("sopc-parameters", null,sopcParameters, true, false,"What sopc-parameters to include in DTS", "{node,cmacro,all}"));
vOptions.add(new CommandLineOption("extra-component-libs", "e", extraComponentLibs, true, false,"Extra component libraries (see source for examples) (can be used many times)", "sopc component xml file"));
if(programVersion==null) {
programVersion = "unknown-version";
}
}
protected int go()
{
int res = 0;
BoardInfo bInfo = null;
Sopc2DTSGui s2dgui = null;
File f;
for(String fn : vExtraComponentLibs)
{
SopcComponentLib.getInstance().loadComponentLibFile(fn);
}
if(vInfoFileNames.size()>0)
{
bInfo = new BoardInfo();
for (String fn : vInfoFileNames) {
try {
bInfo.load(new File(fn));
} catch (FileNotFoundException e) {
Logger.logException(e);
} catch (SAXException e) {
Logger.logException(e);
} catch (IOException e) {
Logger.logException(e);
}
}
}
if(bInfo==null)
{
bInfo = new BoardInfo();
}
SICBridge.setRemovalStrategy(bridgeRemoval.value);
bInfo.setOverlayTarget(overlayTarget.value);
bInfo.setFirmwareName(firmwareName.value);
if(pov.value.length()>0)
{
bInfo.setPov(pov.value);
}
bInfo.setPovType(povType.value);
bInfo.setIncludeTime(!Boolean.parseBoolean(excludeTimeStamp.value));
if(sort.value.length()>0) {
bInfo.setSortType(sort.value);
}
if(ranges.value.length()>0) {
bInfo.setRangesStyle(ranges.value);
}
if(sopcParameters.value.equalsIgnoreCase("none"))
{
bInfo.setDumpParameters(BasicComponent.parameter_action.NONE);
} else if(sopcParameters.value.equalsIgnoreCase("cmacro"))
{
bInfo.setDumpParameters(BasicComponent.parameter_action.CMACRCO);
} else if(sopcParameters.value.equalsIgnoreCase("all"))
{
bInfo.setDumpParameters(BasicComponent.parameter_action.ALL);
}
if (Boolean.parseBoolean(showClocks.value)) {
bInfo.showClockTree();
}
if (Boolean.parseBoolean(showConduit.value)) {
bInfo.showConduits();
}
if (Boolean.parseBoolean(showReset.value)) {
bInfo.showResets();
}
if (Boolean.parseBoolean(showStreaming.value)) {
bInfo.showStreaming();
}
if (Boolean.parseBoolean(forcealtr.value)) {
bInfo.setAltrStyle(AltrStyle.FORCE_LOWER);
} else if (Boolean.parseBoolean(forceALTR.value)) {
bInfo.setAltrStyle(AltrStyle.FORCE_UPPER);
}
if(Boolean.parseBoolean(gui.value))
{
if(!Boolean.parseBoolean(mimicAlteraTools.value))
{
s2dgui = new Sopc2DTSGui(inputFileName.value, bInfo);
useStdOutErr = false;
s2dgui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s2dgui.setVisible(true);
res = -1;
} else {
Logger.logln("GUI-Mode is not supported when mimic-sopc-create-header-files is set",
LogLevel.ERROR);
res = 1;
}
} else {
if(inputFileName.value.length()==0)
{
System.out.println("No input file specified!");
printUsage();
return 1;
}
f = new File(inputFileName.value);
if(f.exists())
{
try {
AvalonSystem sys = BasicSystemLoader.loadSystem(f);
sys.setSopc2DtsVer(programVersion);
if(bInfo.getPov().length()==0)
{
for(int i=0; (i<sys.getSystemComponents().size()) && (bInfo.getPov().length()==0); i++)
{
if(sys.getSystemComponents().get(i).getScd().getGroup().equalsIgnoreCase("cpu"))
{
bInfo.setPov(sys.getSystemComponents().get(i).getInstanceName());
}
}
}
if(Boolean.parseBoolean(mimicAlteraTools.value)) {
SopcCreateHeaderFilesImitator fake = new SopcCreateHeaderFilesImitator(sys);
Vector<BasicComponent> vMasters = sys.getMasterComponents();
for(BasicComponent master : vMasters)
{
BufferedWriter out = new BufferedWriter(new FileWriter(master.getInstanceName()+".h"));
bInfo.setPov(master.getInstanceName());
out.write(fake.getTextOutput(bInfo));
out.close();
}
} else {
String generatedText = null;
byte[] generatedBinary = null;
if(bootargs.value.length()>0)
{
bInfo.setBootArgs(bootargs.value);
}
AbstractSopcGenerator gen = GeneratorFactory.createGeneratorFor(sys, outputType.value);
if(gen == null)
{
Logger.logln("Unable to find generator for type '" +
outputType.value + "'", LogLevel.ERROR);
} else {
if(gen.isTextOutput())
{
generatedText = gen.getTextOutput(bInfo);
} else {
generatedBinary = gen.getBinaryOutput(bInfo);
}
}
if(outputFileName.value.length()==0)
{
if(generatedText!=null)
{
System.out.println(generatedText);
} else if(generatedBinary!=null)
{
Logger.logln("Generated data is binary. Unable to display. Please supply an outputfilename.", LogLevel.WARNING);
} else {
Logger.logln("Nothing was generated.", LogLevel.ERROR);
res = 1;
}
} else {
if(generatedText != null)
{
BufferedWriter out = new BufferedWriter(new FileWriter(outputFileName.value));
out.write(generatedText);
out.close();
} else if(generatedBinary != null)
{
FileOutputStream out = new FileOutputStream(outputFileName.value);
out.write(generatedBinary);
out.close();
} else {
Logger.logln("Nothing was generated.", LogLevel.ERROR);
res = 1;
}
}
}
} catch (FileNotFoundException e) {
Logger.logException(e);
res = 1;
} catch (IOException e) {
Logger.logException(e);
res = 1;
}
} else {
Logger.logln("Inputfile " + inputFileName.value + " not found",
LogLevel.ERROR);
res = 1;
}
}
return res;
}
protected String[] intelliSplit(String inp, int splitChar)
{
Vector<String> vRes = new Vector<String>();
boolean escape=false;
boolean quoted=false;
String tmp = "";
int i=0;
while(i<inp.length())
{
if(inp.charAt(i) == '\"') {
if(!escape) quoted = !quoted;
tmp+=inp.charAt(i);
escape=false;
} else if(inp.charAt(i) == '\\') {
escape=true;
tmp += inp.charAt(i);
} else if((inp.charAt(i) == splitChar)&& !escape && !quoted) {
if(tmp.length()>0)
{
vRes.add(tmp);
tmp = "";
}
} else {
tmp += inp.charAt(i);
escape=false;
}
i++;
}
if(tmp.length()>0)
{
vRes.add(tmp);
}
return vRes.toArray(new String[0]);
}
protected boolean parseCmdLine(String[] args) throws Exception
{
int argPos = 0;
int oldPos = 0;
String cmdLine = "";
while(argPos<args.length)
{
cmdLine += args[argPos++] + ' ';
}
argPos=0;
args=intelliSplit(cmdLine, ' ');
while(argPos<args.length)
{
oldPos = argPos;
for(int iHandler=0; (iHandler<vOptions.size()) && (argPos<args.length); iHandler++)
{
argPos = vOptions.get(iHandler).scanCmdLine(args, argPos);
}
if(oldPos == argPos)
{
//No parser found!!!
System.out.println("Option " + args[argPos] +" not understood.");
printUsage();
return false;
}
}
if(Boolean.parseBoolean(showHelp.value))
{
printUsage();
return false;
}
if(Boolean.parseBoolean(showVersion.value))
{
printVersion();
return false;
}
return true;
}
protected void printUsage()
{
printVersion();
System.out.println("Usage: " + programName + " <arguments>");
System.out.println("Required Arguments:");
String longOpts = "";
String shortOpts = "";
for(int i=0; i<vOptions.size(); i++)
{
if(vOptions.get(i).isRequired())
{
longOpts += vOptions.get(i).getDesc();
shortOpts += vOptions.get(i).getShortDesc();
}
}
System.out.println(longOpts);
System.out.println(shortOpts);
System.out.println("Optional Arguments:");
longOpts = "";
shortOpts = "";
for(int i=0; i<vOptions.size(); i++)
{
if(!vOptions.get(i).isRequired())
{
longOpts += vOptions.get(i).getDesc();
shortOpts += vOptions.get(i).getShortDesc();
}
}
System.out.println(longOpts);
System.out.println(shortOpts);
}
public void printVersion()
{
System.out.println(programName + " - " + programVersion);
}
public void messageLogged(LogEntry log) {
if(log.getLevel().compareTo(Logger.getVerbosity())<=0)
{
if(useStdOutErr)
{
if(log.getLevel().equals(LogLevel.ERROR))
{
System.err.println(log);
StackTraceElement[] trace = log.getTrace();
for(StackTraceElement ste : trace) {
System.err.println("\tat " + ste.toString());
}
} else {
System.out.println(log);
}
}
}
}
protected class CLParameter
{
public String value;
public CLParameter(String val)
{
value = val;
}
}
protected class CommandLineOption
{
String option;
String shortOption;
String helpDesc;
CLParameter parameter;
String parameterDesc;
boolean isRequired;
boolean hasValue;
public CommandLineOption(String opt, String shortOpt, CLParameter param, boolean hasVal, boolean req, String help, String paramDesc)
{
option = opt;
shortOption = shortOpt;
helpDesc = help;
parameter = param;
parameterDesc = paramDesc;
isRequired = req;
hasValue = hasVal;
if(isRequired)
{
helpDesc += (" (Required)");
} else {
helpDesc += (" (Optional)");
}
}
int scanCmdLine(String[] opts, int index) throws Exception
{
if(opts[index].charAt(0)!='-')
{
//Assume it's the sopcinfo file
inputFileName.value = opts[index];
index++;
} else {
String[] tmpOpts = intelliSplit(opts[index],'=');
if((tmpOpts[0].equals("--" + option)) ||
((shortOption!=null)&&(tmpOpts[0].equals('-' + shortOption))))
{
if(parameter!=null)
{
if(hasValue)
{
String val;
if(tmpOpts.length==1)
{
if(index<(opts.length-1))
{
index++;
} else {
throw new Exception("Missing argument");
}
val = opts[index];
} else {
val = tmpOpts[1];
}
parameter.value = val;
} else {
parameter.value = "" + true;
}
}
if(parameter==verbose)
{
if(Boolean.parseBoolean(parameter.value))
{
Logger.increaseVerbosity();
}
} else if (parameter==boardFileName) {
vInfoFileNames.add(parameter.value);
} else if (parameter==extraComponentLibs) {
vExtraComponentLibs.add(parameter.value);
}
Logger.logln("Scanned option " + option + '(' + shortOption + ") with" +
(hasValue ? " value " + parameter.value : "out value."),
LogLevel.DEBUG);
index++;
}
}
return index;
}
boolean isRequired()
{
return isRequired;
}
String getDesc()
{
return " --" + option + ((!hasValue ?"\t\t" : " <" + parameterDesc + ">\t")) + helpDesc + "\n";
}
String getShortDesc()
{
if(shortOption!=null)
{
return " -" + shortOption + ((!hasValue ? "\t\t" : " <" + parameterDesc + ">\t")) + "Short for --" + option + "\n";
} else {
return "";
}
}
}
}