use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.
the class Joystick method propagate.
@Override
public void propagate(InstanceState state) {
BitWidth bits = state.getAttributeValue(ATTR_WIDTH);
int dx;
int dy;
State s = (State) state.getData();
if (s == null) {
dx = 0;
dy = 0;
} else {
dx = s.xPos;
dy = s.yPos;
}
int steps = (1 << bits.getWidth()) - 1;
dx = (dx + 14) * steps / 29 + 1;
dy = (dy + 14) * steps / 29 + 1;
if (bits.getWidth() > 4) {
if (dx >= steps / 2)
dx++;
if (dy >= steps / 2)
dy++;
}
state.setPort(0, Value.createKnown(bits, dx), 1);
state.setPort(1, Value.createKnown(bits, dy), 1);
}
use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.
the class VhdlSimulatorVhdlTop method generate.
public void generate() {
/* Do not generate if file is already valid */
if (valid)
return;
String[] type = { "inout", "in", "out" };
StringBuilder ports = new StringBuilder();
ports.append("Autogenerated by logisim --");
ports.append(System.getProperty("line.separator"));
StringBuilder components = new StringBuilder();
components.append("Autogenerated by logisim --");
components.append(System.getProperty("line.separator"));
StringBuilder map = new StringBuilder();
map.append("Autogenerated by logisim --");
map.append(System.getProperty("line.separator"));
Boolean firstPort = true, firstComp = true, firstMap = true;
/* For each vhdl entity */
for (Component comp : VhdlSimulator.getVhdlComponents(vhdlSimulator.getProject().getCircuitState())) {
if (comp.getFactory().getClass().equals(VhdlEntity.class)) {
InstanceState state = vhdlSimulator.getProject().getCircuitState().getInstanceState(comp);
VhdlContent content = state.getAttributeValue(VhdlEntity.CONTENT_ATTR);
String vhdlEntityName = comp.getFactory().getHDLTopName(state.getInstance().getAttributeSet());
/*
* Create ports
*/
for (Port port : content.getPorts()) {
if (!firstPort) {
ports.append(";");
ports.append(System.getProperty("line.separator"));
} else {
firstPort = false;
}
String portName = vhdlEntityName + "_" + port.getToolTip();
ports.append(" " + portName + " : " + type[port.getType()] + " std_logic");
int width = port.getFixedBitWidth().getWidth();
if (width > 1) {
ports.append("_vector(" + (width - 1) + " downto 0)");
}
}
/*
* Create components
*/
components.append(" component " + vhdlEntityName);
components.append(System.getProperty("line.separator"));
components.append(" port (");
components.append(System.getProperty("line.separator"));
firstComp = true;
for (Port port : content.getPorts()) {
if (!firstComp) {
components.append(";");
components.append(System.getProperty("line.separator"));
} else
firstComp = false;
components.append(" " + port.getToolTip() + " : " + type[port.getType()] + " std_logic");
int width = port.getFixedBitWidth().getWidth();
if (width > 1) {
components.append("_vector(" + (width - 1) + " downto 0)");
}
}
components.append(System.getProperty("line.separator"));
components.append(" );");
components.append(System.getProperty("line.separator"));
components.append(" end component ;");
components.append(System.getProperty("line.separator"));
components.append(" ");
components.append(System.getProperty("line.separator"));
/*
* Create port map
*/
map.append(" " + vhdlEntityName + "_map : " + vhdlEntityName + " port map (");
map.append(System.getProperty("line.separator"));
firstMap = true;
for (Port port : content.getPorts()) {
if (!firstMap) {
map.append(",");
map.append(System.getProperty("line.separator"));
} else
firstMap = false;
map.append(" " + port.getToolTip() + " => " + vhdlEntityName + "_" + port.getToolTip());
}
map.append(System.getProperty("line.separator"));
map.append(" );");
map.append(System.getProperty("line.separator"));
map.append(" ");
map.append(System.getProperty("line.separator"));
}
}
ports.append(System.getProperty("line.separator"));
ports.append(" ---------------------------");
ports.append(System.getProperty("line.separator"));
components.append(" ---------------------------");
components.append(System.getProperty("line.separator"));
map.append(" ---------------------------");
map.append(System.getProperty("line.separator"));
/*
* Replace template blocks by generated datas
*/
String template;
try {
template = new String(FileUtil.getBytes(this.getClass().getResourceAsStream(VhdlSimulator.VHDL_TEMPLATES_PATH + "top_sim.templ")));
} catch (IOException e) {
logger.error("Could not read template : {}", e.getMessage());
return;
}
template = template.replaceAll("%date%", LocaleManager.parserSDF.format(new Date()));
template = template.replaceAll("%ports%", ports.toString());
template = template.replaceAll("%components%", components.toString());
template = template.replaceAll("%map%", map.toString());
PrintWriter writer;
try {
writer = new PrintWriter(VhdlSimulator.SIM_SRC_PATH + VhdlSimulator.SIM_TOP_FILENAME, "UTF-8");
writer.print(template);
writer.close();
} catch (FileNotFoundException e) {
logger.error("Could not create top_sim file : {}", e.getMessage());
e.printStackTrace();
return;
} catch (UnsupportedEncodingException e) {
logger.error("Could not create top_sim file : {}", e.getMessage());
e.printStackTrace();
return;
}
valid = true;
}
use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.
the class TtyInterface method loadRam.
private static boolean loadRam(CircuitState circState, File loadFile) throws IOException {
if (loadFile == null)
return false;
boolean found = false;
for (Component comp : circState.getCircuit().getNonWires()) {
if (comp.getFactory() instanceof Ram) {
Ram ramFactory = (Ram) comp.getFactory();
InstanceState ramState = circState.getInstanceState(comp);
ramFactory.loadImage(ramState, loadFile);
found = true;
}
}
for (CircuitState sub : circState.getSubstates()) {
found |= loadRam(sub, loadFile);
}
return found;
}
use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.
the class TtyInterface method prepareForTty.
private static boolean prepareForTty(CircuitState circState, ArrayList<InstanceState> keybStates) {
boolean found = false;
for (Component comp : circState.getCircuit().getNonWires()) {
Object factory = comp.getFactory();
if (factory instanceof Tty) {
Tty ttyFactory = (Tty) factory;
InstanceState ttyState = circState.getInstanceState(comp);
ttyFactory.sendToStdout(ttyState);
found = true;
} else if (factory instanceof Keyboard) {
keybStates.add(circState.getInstanceState(comp));
found = true;
}
}
for (CircuitState sub : circState.getSubstates()) {
found |= prepareForTty(sub, keybStates);
}
return found;
}
use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.
the class Analyze method computeTable.
//
// ComputeTable
//
/**
* Returns a truth table corresponding to the circuit.
*/
public static void computeTable(AnalyzerModel model, Project proj, Circuit circuit, Map<Instance, String> pinLabels) {
ArrayList<Instance> inputPins = new ArrayList<Instance>();
ArrayList<String> inputNames = new ArrayList<String>();
ArrayList<Instance> outputPins = new ArrayList<Instance>();
ArrayList<String> outputNames = new ArrayList<String>();
for (Map.Entry<Instance, String> entry : pinLabels.entrySet()) {
Instance pin = entry.getKey();
if (Pin.FACTORY.isInputPin(pin)) {
inputPins.add(pin);
inputNames.add(entry.getValue());
} else {
outputPins.add(pin);
outputNames.add(entry.getValue());
}
}
int inputCount = inputPins.size();
int rowCount = 1 << inputCount;
Entry[][] columns = new Entry[outputPins.size()][rowCount];
for (int i = 0; i < rowCount; i++) {
CircuitState circuitState = new CircuitState(proj, circuit);
for (int j = 0; j < inputCount; j++) {
Instance pin = inputPins.get(j);
InstanceState pinState = circuitState.getInstanceState(pin);
boolean value = TruthTable.isInputSet(i, j, inputCount);
Pin.FACTORY.setValue(pinState, value ? Value.TRUE : Value.FALSE);
}
Propagator prop = circuitState.getPropagator();
prop.propagate();
if (prop.isOscillating()) {
for (int j = 0; j < columns.length; j++) {
columns[j][i] = Entry.OSCILLATE_ERROR;
}
} else {
for (int j = 0; j < columns.length; j++) {
Instance pin = outputPins.get(j);
InstanceState pinState = circuitState.getInstanceState(pin);
Entry out;
Value outValue = Pin.FACTORY.getValue(pinState).get(0);
if (outValue == Value.TRUE)
out = Entry.ONE;
else if (outValue == Value.FALSE)
out = Entry.ZERO;
else if (outValue == Value.ERROR)
out = Entry.BUS_ERROR;
else
out = Entry.DONT_CARE;
columns[j][i] = out;
}
}
}
model.setVariables(inputNames, outputNames);
for (int i = 0; i < columns.length; i++) {
model.getTruthTable().setOutputColumn(i, columns[i]);
}
}
Aggregations