mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-19 09:54:49 -04:00
added export of truth table as hex file
This commit is contained in:
parent
f7f8ea1dcc
commit
d6ee37a1c1
@ -9,6 +9,7 @@ import de.neemann.digital.analyse.expression.ExpressionException;
|
||||
import de.neemann.digital.analyse.expression.Variable;
|
||||
import de.neemann.digital.analyse.quinemc.BoolTable;
|
||||
import de.neemann.digital.analyse.quinemc.BoolTableIntArray;
|
||||
import de.neemann.digital.analyse.quinemc.ThreeStateValue;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
@ -52,6 +53,42 @@ public class TruthTable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the table as hex file to be loaded in a ROM or LUT element.
|
||||
*
|
||||
* @param filename filename
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public void saveHex(File filename) throws IOException {
|
||||
try (Writer out = new OutputStreamWriter(new FileOutputStream(filename), "utf-8")) {
|
||||
saveHex(out);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the table as hex file to be loaded in a ROM or LUT element.
|
||||
*
|
||||
* @param writer the filename to use
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public void saveHex(Writer writer) throws IOException {
|
||||
writer.write("v2.0 raw\n");
|
||||
int count = results.get(0).getValues().size();
|
||||
for (int i = 0; i < count; i++) {
|
||||
int val = 0;
|
||||
int mask = 1;
|
||||
for (Result r : results) {
|
||||
ThreeStateValue v = r.getValues().get(i);
|
||||
if (v == ThreeStateValue.one)
|
||||
val |= mask;
|
||||
mask *= 2;
|
||||
}
|
||||
writer.write(Integer.toHexString(val));
|
||||
writer.write('\n');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static XStream getxStream() {
|
||||
XStream xStream = new XStream(new StaxDriver());
|
||||
xStream.alias("truthTable", TruthTable.class);
|
||||
|
@ -257,6 +257,24 @@ public class TableDialog extends JDialog {
|
||||
}
|
||||
});
|
||||
|
||||
fileMenu.add(new ToolTipAction(Lang.get("menu_table_exportHex")) {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fc = new JFileChooser();
|
||||
if (TableDialog.this.filename != null)
|
||||
fc.setSelectedFile(Main.checkSuffix(TableDialog.this.filename, "hex"));
|
||||
if (fc.showSaveDialog(TableDialog.this) == JFileChooser.APPROVE_OPTION) {
|
||||
try {
|
||||
File file = Main.checkSuffix(fc.getSelectedFile(), "hex");
|
||||
model.getTable().saveHex(file);
|
||||
} catch (IOException e1) {
|
||||
new ErrorMessage().addCause(e1).show(TableDialog.this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}.setToolTip(Lang.get("menu_table_exportHex_tt")).createJMenuItem());
|
||||
|
||||
|
||||
createJK = new JCheckBoxMenuItem(Lang.get("menu_table_JK"));
|
||||
createJK.addActionListener(e -> calculateExpressions());
|
||||
fileMenu.add(createJK);
|
||||
|
@ -330,6 +330,8 @@ Zur Analyse können Sie die Schaltung im Gatterschrittmodus ausführen.</string>
|
||||
<string name="menu_table_create_jedec">JEDEC (*.jed)</string>
|
||||
<string name="menu_table_create_jedec_tt">Erzeugt eine JEDEC Datei für den Baustein</string>
|
||||
<string name="menu_table_exportTableLaTeX">Export LaTeX</string>
|
||||
<string name="menu_table_exportHex">Erzeuge HEX</string>
|
||||
<string name="menu_table_exportHex_tt">Die HEX-Datei kann in ein ROM oder eine LUT geladen werden.</string>
|
||||
<string name="menu_table_inputs">Eingänge</string>
|
||||
<string name="menu_table_new">Neu</string>
|
||||
<string name="menu_table_newColumns">Spalten hinzufügen</string>
|
||||
|
@ -330,6 +330,8 @@ To analyse you can run the circuit in single gate step mode.</string>
|
||||
<string name="menu_table_create_jedec">JEDEC (*.jed)</string>
|
||||
<string name="menu_table_create_jedec_tt">Creates a JEDEC file for the device</string>
|
||||
<string name="menu_table_exportTableLaTeX">Export LaTeX</string>
|
||||
<string name="menu_table_exportHex">Export HEX</string>
|
||||
<string name="menu_table_exportHex_tt">You can load the HEX file to a ROM or a LUT.</string>
|
||||
<string name="menu_table_inputs">Inputs</string>
|
||||
<string name="menu_table_new">New</string>
|
||||
<string name="menu_table_newColumns">Add Columns</string>
|
||||
|
@ -5,6 +5,7 @@ import de.neemann.digital.analyse.expression.Variable;
|
||||
import de.neemann.digital.analyse.quinemc.BoolTableIntArray;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
@ -32,4 +33,53 @@ public class TruthTableTest extends TestCase {
|
||||
assertEquals(i % 3, t.getByContext(0, fc));
|
||||
}
|
||||
}
|
||||
|
||||
public void testHexExportSingle() throws Exception {
|
||||
ArrayList<Variable> vars = Variable.vars(3);
|
||||
TruthTable t = new TruthTable(vars).addResult();
|
||||
BoolTableIntArray result = (BoolTableIntArray) t.getResult(0);
|
||||
for (int i = 0; i < t.getRows(); i++) {
|
||||
result.set(i, i % 2);
|
||||
}
|
||||
StringWriter w = new StringWriter();
|
||||
t.saveHex(w);
|
||||
w.close();
|
||||
|
||||
assertEquals("v2.0 raw\n" +
|
||||
"0\n" +
|
||||
"1\n" +
|
||||
"0\n" +
|
||||
"1\n" +
|
||||
"0\n" +
|
||||
"1\n" +
|
||||
"0\n" +
|
||||
"1\n", w.toString());
|
||||
}
|
||||
|
||||
public void testHexExportTwo() throws Exception {
|
||||
ArrayList<Variable> vars = Variable.vars(3);
|
||||
TruthTable t = new TruthTable(vars).addResult();
|
||||
BoolTableIntArray result = (BoolTableIntArray) t.getResult(0);
|
||||
for (int i = 0; i < t.getRows(); i++) {
|
||||
result.set(i, i % 2);
|
||||
}
|
||||
t.addResult();
|
||||
result = (BoolTableIntArray) t.getResult(1);
|
||||
for (int i = 0; i < t.getRows(); i++) {
|
||||
result.set(i, (i + 1) % 2);
|
||||
}
|
||||
StringWriter w = new StringWriter();
|
||||
t.saveHex(w);
|
||||
w.close();
|
||||
|
||||
assertEquals("v2.0 raw\n" +
|
||||
"2\n" +
|
||||
"1\n" +
|
||||
"2\n" +
|
||||
"1\n" +
|
||||
"2\n" +
|
||||
"1\n" +
|
||||
"2\n" +
|
||||
"1\n", w.toString());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user