Skip to content

added verilog exporter to current version #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/logicsim/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
import java.util.Map;
import java.util.Objects;

import javax.swing.JOptionPane;

public class App {

public static final String APP_TITLE = "LogicSim";
public static final String CIRCUIT_FILE_SUFFIX = "lsc";
public static final String VERILOG_FILE_SUFFIX = "v";
public static final String MODULE_FILE_SUFFIX = "lsm";
public static final String GRAPHICS_FORMAT = "png";
public static boolean Running_From_Jar = false;
Expand Down Expand Up @@ -191,6 +194,20 @@ public static String getCircuitPath() {
}
return "";
}

public static String getVerilogPath() {
File f = new File("");
String fname = f.getAbsolutePath() + "/verilog/";
f = new File(fname);
if (f != null && f.exists() && f.isDirectory()) {
return new String(f.getAbsolutePath() + "/");
} else {
JOptionPane.showMessageDialog(null,
"Directory verilog not found.\nPlease run the program from its directory");
System.exit(0);
}
return "";
}

public static Gate getGate(String type) {
for (Category cat : cats) {
Expand Down
6 changes: 5 additions & 1 deletion src/logicsim/CircuitPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ public static int round(int num) {

public static final String TEXT = "text";

public String TEXT_DEFAULT = "";
public String TEXT_DEFAULT = "<Label>";

public String text;

public String type;

protected String label;

public CircuitPart(int x, int y) {
this.x = x;
this.y = y;
Expand Down
4 changes: 0 additions & 4 deletions src/logicsim/Gate.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public class Gate extends CircuitPart {

protected int height = 60;

protected String label;

protected int labelOffsetX;

protected int labelOffsetY;
Expand All @@ -67,8 +65,6 @@ public class Gate extends CircuitPart {
*/
public int rotate90 = 0;

protected String type;

protected boolean variableInputCountSupported = false;

protected int width = 60;
Expand Down
3 changes: 1 addition & 2 deletions src/logicsim/HTMLHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ private void initComponents() {
jButton_ok = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
jTextPane1.setContentType("text/html;charset=UTF-8");


jButton_ok.setText("OK");
jButton_ok.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Expand Down
102 changes: 84 additions & 18 deletions src/logicsim/LSFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class LSFrame extends JFrame implements ActionListener, CircuitChangedLis
private static final long serialVersionUID = -5281157929385660575L;

LogicSimFile lsFile;
VerilogFile vFile;

JMenuBar mnuBar;
JToolBar btnBar;
Expand All @@ -75,6 +76,7 @@ public LSFrame(String title) {

lsFile = new LogicSimFile(defaultCircuitFileName());
lsFile.circuit.setRepaintListener(lspanel);
vFile = new VerilogFile(defaultVerilogFileName());
lspanel.setChangeListener(this);

try {
Expand Down Expand Up @@ -109,6 +111,13 @@ private String defaultCircuitFileName() {
fn += "." + App.CIRCUIT_FILE_SUFFIX;
return fn;
}

private String defaultVerilogFileName() {
String fn = App.getVerilogPath();
fn += I18N.tr(Lang.UNNAMED);
fn += "." + App.VERILOG_FILE_SUFFIX;
return fn;
}

/** Component initialization */
private void createUI() {
Expand Down Expand Up @@ -183,6 +192,14 @@ public void actionPerformed(ActionEvent e) {
}
});
mnu.add(m);

m = createMenuItem(Lang.VEXPORT, 0, true);
m.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionVExport(e);
}
});
mnu.add(m);

m = createMenuItem(Lang.PRINT, 0, true);
m.addActionListener(new ActionListener() {
Expand Down Expand Up @@ -593,14 +610,14 @@ public void actionPerformed(ActionEvent e) {
});
btnBar.add(btnToggle, null);

// btnLS = new LSButton("select", Lang.SELECT);
// btnLS.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// lspanel.setAction(LSPanel.ACTION_SELECT);
// lspanel.requestFocusInWindow();
// }
// });
// btnBar.add(btnLS, null);
//btnLS = new LSButton("select", Lang.SELECT);
//btnLS.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// lspanel.setAction(LSPanel.ACTION_SELECT);
// lspanel.requestFocusInWindow();
// }
//});
//btnBar.add(btnLS, null);

btnBar.add(getMenuGap());

Expand Down Expand Up @@ -787,13 +804,10 @@ void actionSimulate(ActionEvent e) {
lspanel.circuit.deselectAll();
repaint();
Simulation.getInstance().start();
changedStatusText(I18N.tr(Lang.SIMULATION_STARTED));
}
} else {
if (Simulation.getInstance().isRunning()) {
if (Simulation.getInstance().isRunning())
Simulation.getInstance().stop();
changedStatusText(I18N.tr(Lang.SIMULATION_STOPPED));
}
}
}

Expand Down Expand Up @@ -913,6 +927,21 @@ void actionSave(ActionEvent e, boolean saveAs) {
lsFile.changed = false;
fillGateList();
}

void actionVExport(ActionEvent e) {
if (showVExportDialog() == false)
return;
vFile.circuit = lspanel.circuit;
try {
VerilogCreator.createVerilog(vFile);
} catch (RuntimeException err) {
System.err.println(err);
err.printStackTrace(System.err);
JOptionPane.showMessageDialog(this, I18N.tr(Lang.SAVEERROR) + " " + err.getMessage());
}
setStatusText(String.format(I18N.tr(Lang.SAVED), vFile.fileName));
fillGateList();
}

/**
* helper method to show the save dialog
Expand Down Expand Up @@ -947,6 +976,48 @@ public boolean showSaveDialog() {
} else
return false;
}

/**
* helper method to show the verilog-export dialog
*
* @return
*/
public boolean showVExportDialog() {
File file = new File(vFile.fileName);
String parentDirName = file.getParent();

JFileChooser chooser = new JFileChooser(parentDirName);
chooser.setDialogTitle(I18N.tr(Lang.SAVECIRCUIT));

String s = "Verilog Files (" + "." + App.VERILOG_FILE_SUFFIX + ")";
FileNameExtensionFilter filter = new FileNameExtensionFilter(s, App.VERILOG_FILE_SUFFIX);
chooser.setFileFilter(filter);

if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
vFile.fileName = chooser.getSelectedFile().getAbsolutePath();
// check fileName
int lastSeparator = vFile.fileName.lastIndexOf(File.separatorChar);
int lastDot = vFile.fileName.lastIndexOf(".");
if (lastDot < lastSeparator) {
// ending is missing
vFile.fileNameNE = vFile.fileName;
vFile.fileName += "." + App.VERILOG_FILE_SUFFIX;
}
else {
int iend = vFile.fileName.indexOf(".");
if (iend != -1) {
vFile.fileNameNE = vFile.fileName.substring(0 , iend);
}
}
int lastSlash = vFile.fileNameNE.lastIndexOf("/");
if (lastSlash == 0) {
lastSlash = vFile.fileNameNE.lastIndexOf("\\");
}
vFile.fileNameNE = vFile.fileNameNE.substring(lastSlash+1,vFile.fileNameNE.length());
return true;
} else
return false;
}

/**
* handles initial steps to create a new module
Expand Down Expand Up @@ -1044,9 +1115,6 @@ void fillGateList() {
* @param e
*/
void actionLstGatesSelected(ListSelectionEvent e) {
if (Simulation.getInstance().isRunning())
return;

int sel = lstParts.getSelectedIndex();
if (sel < 0)
return;
Expand Down Expand Up @@ -1241,11 +1309,9 @@ public void changedStatusText(String text) {
for (Component c : btnBar.getComponents()) {
if (c instanceof LSToggleButton) {
LSToggleButton b = (LSToggleButton) c;
if (!b.id.equals(I18N.langToStr(Lang.SIMULATE)))
b.setSelected(false);
b.setSelected(false);
}
}
return;
}
if (LSPanel.MSG_ABORTED.equals(text)) {
for (Component c : btnBar.getComponents()) {
Expand Down
14 changes: 7 additions & 7 deletions src/logicsim/LSPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ public void mousePressed(MouseEvent e) {
int rx = CircuitPart.round(e.getX());
int ry = CircuitPart.round(e.getY());

boolean simRunning = Simulation.getInstance().isRunning();
boolean sim = Simulation.getInstance().isRunning();
boolean expertMode = LSProperties.MODE_EXPERT
.equals(LSProperties.getInstance().getProperty(LSProperties.MODE, LSProperties.MODE_NORMAL));

if (simRunning) {
if (sim) {
currentAction = ACTION_NONE;
//fireStatusText(NOTHING);
fireStatusText(NOTHING);
}

if (currentAction == ACTION_SELECT) {
Expand All @@ -208,7 +208,7 @@ public void mousePressed(MouseEvent e) {
}
// if (cp != null)
// System.out.println(cp.toStringAll());
if (!simRunning && cp instanceof Pin && !e.isAltDown() && currentAction == ACTION_NONE) {
if (!sim && cp instanceof Pin && !e.isAltDown() && currentAction == ACTION_NONE) {
// we start a new wire if the pin we clicked is an output OR
// if we are in expert mode
if (((Pin) cp).isOutput() || expertMode)
Expand Down Expand Up @@ -325,7 +325,7 @@ public void mousePressed(MouseEvent e) {
return;
}
// check if the part is a connector
if (cp instanceof Pin && !e.isAltDown() && !simRunning) {
if (cp instanceof Pin && !e.isAltDown() && !sim) {
Pin pin = ((Pin) cp);
fireStatusText(I18N.tr(Lang.PIN) + " (" + cp.getId() + ")");
// modify input (inverted or high or low or revert to normal type)
Expand All @@ -349,7 +349,7 @@ public void mousePressed(MouseEvent e) {
else
fireStatusText(I18N.getString(type, I18N.DESCRIPTION) + " (" + cp.getId() + ")");

if (parts.length > 0 && !simRunning) {
if (parts.length > 0 && !sim) {
// check if we clicked a new gate
if (!cp.isSelected()) {
cp.select();
Expand All @@ -359,7 +359,7 @@ public void mousePressed(MouseEvent e) {
parts = circuit.getSelected();
}
}
} else if (cp instanceof Wire && !simRunning) {
} else if (cp instanceof Wire && !sim) {
String s = cp.getProperty(CircuitPart.TEXT);
String desc = I18N.tr(Lang.WIRE);
if (s != null)
Expand Down
3 changes: 1 addition & 2 deletions src/logicsim/LSToggleButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
public class LSToggleButton extends JToggleButton {

private static final long serialVersionUID = 4992541122998327288L;
public final String id;

public LSToggleButton(String iconName, Lang toolTip) {
this.setDoubleBuffered(true);
this.setIcon(getIcon(iconName));
this.setToolTipText(I18N.tr(toolTip));
this.id = I18N.langToStr(toolTip);
// this.setBorderPainted(true);
// this.setBorder(BorderFactory.createLineBorder(Color.black));
// this.addMouseListener(this);
Expand Down
2 changes: 1 addition & 1 deletion src/logicsim/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public enum Lang {
PRINT, PROPERTIES, QUESTION_CONFIRMDISCARD, QUESTION_CONFIRMSAVE, QUESTION_DELETE, READERROR, REMOVEGATE,
REMOVEPOINT, REMOVEPOINT_HELP, RESET, ROTATE, SAVE, SAVEAS, SAVECIRCUIT, SAVED, SAVEERROR, SELECT, SELECTALL,
SELECTNONE, SETTINGS, SIMULATE, TEXT, UNNAMED, WIRE, WIREEDIT, WIRENEW, WIRENEW_HELP, WIREPOINT, YES, ZOOMALL,
ZOOMIN, ZOOMOUT, SIMULATION_STARTED, SIMULATION_STOPPED
ZOOMIN, ZOOMOUT, VEXPORT
}
6 changes: 5 additions & 1 deletion src/logicsim/Pin.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class Pin extends CircuitPart {

public int number;



protected boolean level = false;
public int paintDirection = RIGHT;

Expand All @@ -55,8 +57,10 @@ public void setIoType(int ioType) {

public Pin(int x, int y, Gate gate, int number) {
super(x, y);
this.type = "pin";
this.parent = gate;
this.number = number;
loadProperties();
}

/**
Expand All @@ -79,7 +83,7 @@ private void drawLabel(Graphics2D g2) {
int y = getY();

g2.setFont(smallFont);
if (text != null && text.length() > 0) {
if (text != null && text.length() > 0 && text != "<Label>") {
if (paintDirection == RIGHT) {
if (POS_EDGE_TRIG.equals(text)) {
Polygon tr = new Polygon();
Expand Down
Loading