forked from logisim-evolution/logisim-evolution
/
BoardWriterClass.java
215 lines (204 loc) · 8.95 KB
/
BoardWriterClass.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
/**
* This file is part of Logisim-evolution.
*
* Logisim-evolution is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Logisim-evolution 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 General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with Logisim-evolution. If not, see <http://www.gnu.org/licenses/>.
*
* Original code by Carl Burch (http://www.cburch.com), 2011.
* Subsequent modifications by:
* + Haute École Spécialisée Bernoise
* http://www.bfh.ch
* + Haute École du paysage, d'ingénierie et d'architecture de Genève
* http://hepia.hesge.ch/
* + Haute École d'Ingénierie et de Gestion du Canton de Vaud
* http://www.heig-vd.ch/
* + REDS Institute - HEIG-VD, Yverdon-les-Bains, Switzerland
* http://reds.heig-vd.ch
* This version of the project is currently maintained by:
* + Kevin Walsh (kwalsh@holycross.edu, http://mathcs.holycross.edu/~kwalsh)
*/
package com.bfh.logisim.fpgaboardeditor;
import java.awt.Image;
import java.io.File;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
class BoardWriterClass {
public static String BoardInformationSectionString = "BoardInformation";
public static String ClockInformationSectionString = "ClockInformation";
public static String[] ClockSectionStrings = { "Frequency", "FPGApin",
"PullBehavior", "IOStandard" };
public static String FPGAInformationSectionString = "FPGAInformation";
public static String[] FPGASectionStrings = { "Vendor", "Part", "Family",
"Package", "Speedgrade", "USBTMC", "JTAGPos", "FlashName", "FlashPos"};
public static String UnusedPinsString = "UnusedPins";
public static String ComponentsSectionString = "IOComponents";
public static String LocationXString = "LocationX";
public static String LocationYString = "LocationY";
public static String WidthString = "Width";
public static String HeightString = "Height";
public static String LabelString = "Label";
public static String PinLocationString = "FPGAPinName";
public static String ImageInformationString = "BoardPicture";
public static String MultiPinInformationString = "NrOfPins";
public static String MultiPinPrefixString = "FPGAPin_";
private DocumentBuilderFactory factory;
private DocumentBuilder parser;
private Document boardInfo;
public BoardWriterClass(BoardInformation BoardInfo, Image BoardImage) {
try {
// Create instance of DocumentBuilderFactory
factory = DocumentBuilderFactory.newInstance();
// Get the DocumentBuilder
parser = factory.newDocumentBuilder();
// Create blank DOM Document
boardInfo = parser.newDocument();
Element root = boardInfo.createElement(BoardInfo.getBoardName());
boardInfo.appendChild(root);
Element fpgainfo = boardInfo
.createElement(BoardInformationSectionString);
root.appendChild(fpgainfo);
Comment comment = boardInfo
.createComment("This section decribes the FPGA and its clock");
fpgainfo.appendChild(comment);
Element clkinfo = boardInfo
.createElement(ClockInformationSectionString);
clkinfo.setAttribute(ClockSectionStrings[0],
Long.toString(BoardInfo.fpga.getClockFrequency()));
Attr pin = boardInfo.createAttribute(ClockSectionStrings[1]);
pin.setValue(BoardInfo.fpga.getClockPinLocation().toUpperCase());
clkinfo.setAttributeNode(pin);
Attr pull = boardInfo.createAttribute(ClockSectionStrings[2]);
pull.setValue(PullBehaviors.Behavior_strings[BoardInfo.fpga
.getClockPull()]);
clkinfo.setAttributeNode(pull);
Attr IOS = boardInfo.createAttribute(ClockSectionStrings[3]);
IOS.setValue(IoStandards.Behavior_strings[BoardInfo.fpga
.getClockStandard()]);
clkinfo.setAttributeNode(IOS);
fpgainfo.appendChild(clkinfo);
Element FPGA = boardInfo
.createElement(FPGAInformationSectionString);
FPGA.setAttribute(FPGASectionStrings[0],
FPGAClass.Vendors[BoardInfo.fpga.getVendor()].toUpperCase());
Attr part = boardInfo.createAttribute(FPGASectionStrings[1]);
part.setValue(BoardInfo.fpga.getPart());
FPGA.setAttributeNode(part);
Attr tech = boardInfo.createAttribute(FPGASectionStrings[2]);
tech.setValue(BoardInfo.fpga.getTechnology());
FPGA.setAttributeNode(tech);
Attr box = boardInfo.createAttribute(FPGASectionStrings[3]);
box.setValue(BoardInfo.fpga.getPackage());
FPGA.setAttributeNode(box);
Attr speed = boardInfo.createAttribute(FPGASectionStrings[4]);
speed.setValue(BoardInfo.fpga.getSpeedGrade());
FPGA.setAttributeNode(speed);
Attr usbtmc = boardInfo.createAttribute(FPGASectionStrings[5]);
usbtmc.setValue(BoardInfo.fpga.USBTMCDownloadRequired().toString());
FPGA.setAttributeNode(usbtmc);
Attr jtagPos = boardInfo.createAttribute(FPGASectionStrings[6]);
jtagPos.setValue(String.valueOf(BoardInfo.fpga.getFpgaJTAGChainPosition()));
FPGA.setAttributeNode(jtagPos);
Attr flashName = boardInfo.createAttribute(FPGASectionStrings[7]);
flashName.setValue(String.valueOf(BoardInfo.fpga.getFlashName()));
FPGA.setAttributeNode(flashName);
Attr flashJtagPos = boardInfo.createAttribute(FPGASectionStrings[8]);
flashJtagPos.setValue(String.valueOf(BoardInfo.fpga.getFlashJTAGChainPosition()));
FPGA.setAttributeNode(flashJtagPos);
Element UnusedPins = boardInfo.createElement(UnusedPinsString);
fpgainfo.appendChild(FPGA);
UnusedPins.setAttribute("PullBehavior",
PullBehaviors.Behavior_strings[BoardInfo.fpga
.getUnusedPinsBehavior()]);
fpgainfo.appendChild(UnusedPins);
Element Components = boardInfo
.createElement(ComponentsSectionString);
root.appendChild(Components);
Comment Compcmd = boardInfo
.createComment("This section describes all Components present on the boards");
Components.appendChild(Compcmd);
for (FPGAIOInformationContainer comp : BoardInfo.GetAllComponents()) {
Components.appendChild(comp.GetDocumentElement(boardInfo));
}
ImageXmlFactory writer = new ImageXmlFactory();
writer.CreateStream(BoardImage);
Element BoardPicture = boardInfo
.createElement(ImageInformationString);
root.appendChild(BoardPicture);
Comment Pictcmd = boardInfo
.createComment("This section hold the board picture");
BoardPicture.appendChild(Pictcmd);
Element pictsize = boardInfo.createElement("PictureDimension");
pictsize.setAttribute("Width",
Integer.toString(BoardImage.getWidth(null)));
Attr height = boardInfo.createAttribute("Height");
height.setValue(Integer.toString(BoardImage.getHeight(null)));
pictsize.setAttributeNode(height);
BoardPicture.appendChild(pictsize);
Element CodeTable = boardInfo.createElement("CompressionCodeTable");
BoardPicture.appendChild(CodeTable);
CodeTable.setAttribute("TableData", writer.GetCodeTable());
Element PixelData = boardInfo.createElement("PixelData");
BoardPicture.appendChild(PixelData);
PixelData.setAttribute("PixelRGB", writer.GetCompressedString());
} catch (Exception e) {
/* TODO: handle exceptions */
System.err.printf(
"Exceptions not handled yet in BoardWriterClass(), but got an exception: %s\n",
e.getMessage());
}
}
public void PrintXml() {
try {
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
Source src = new DOMSource(boardInfo);
StreamResult dest = new StreamResult(new StringWriter());
aTransformer.transform(src, dest);
System.out.println(dest.getWriter().toString());
} catch (Exception e) {
/* TODO: handle exceptions */
System.err.printf(
"Exceptions not handled yet in PrintXml(), but got an exception: %s\n",
e.getMessage());
}
}
public void PrintXml(String filename) {
try {
TransformerFactory tranFactory = TransformerFactory.newInstance();
tranFactory.setAttribute("indent-number", 3);
Transformer aTransformer = tranFactory.newTransformer();
aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
Source src = new DOMSource(boardInfo);
File file = new File(filename);
Result dest = new StreamResult(file);
aTransformer.transform(src, dest);
} catch (Exception e) {
System.err.printf(
"Exceptions not handled yet in PrintXml(), but got an exception: %s\n",
e.getMessage());
}
}
}