use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.
the class TtyInterface method runSimulation.
private static int runSimulation(CircuitState circState, ArrayList<Instance> outputPins, Instance haltPin, int format) {
boolean showTable = (format & FORMAT_TABLE) != 0;
boolean showSpeed = (format & FORMAT_SPEED) != 0;
boolean showTty = (format & FORMAT_TTY) != 0;
boolean showHalt = (format & FORMAT_HALT) != 0;
ArrayList<InstanceState> keyboardStates = null;
StdinThread stdinThread = null;
if (showTty) {
keyboardStates = new ArrayList<InstanceState>();
boolean ttyFound = prepareForTty(circState, keyboardStates);
if (!ttyFound) {
logger.error("{}", Strings.get("ttyNoTtyError"));
System.exit(-1);
}
if (keyboardStates.isEmpty()) {
keyboardStates = null;
} else {
stdinThread = new StdinThread();
stdinThread.start();
}
}
int retCode;
long tickCount = 0;
long start = System.currentTimeMillis();
boolean halted = false;
ArrayList<Value> prevOutputs = null;
Propagator prop = circState.getPropagator();
while (true) {
ArrayList<Value> curOutputs = new ArrayList<Value>();
for (Instance pin : outputPins) {
InstanceState pinState = circState.getInstanceState(pin);
Value val = Pin.FACTORY.getValue(pinState);
if (pin == haltPin) {
halted |= val.equals(Value.TRUE);
} else if (showTable) {
curOutputs.add(val);
}
}
if (showTable) {
displayTableRow(prevOutputs, curOutputs);
}
if (halted) {
// normal exit
retCode = 0;
break;
}
if (prop.isOscillating()) {
// abnormal exit
retCode = 1;
break;
}
if (keyboardStates != null) {
char[] buffer = stdinThread.getBuffer();
if (buffer != null) {
for (InstanceState keyState : keyboardStates) {
Keyboard.addToBuffer(keyState, buffer);
}
}
}
prevOutputs = curOutputs;
tickCount++;
prop.tick();
prop.propagate();
}
long elapse = System.currentTimeMillis() - start;
if (showTty)
ensureLineTerminated();
if (showHalt || retCode != 0) {
if (retCode == 0) {
logger.error("{}", Strings.get("ttyHaltReasonPin"));
} else if (retCode == 1) {
logger.error("{}", Strings.get("ttyHaltReasonOscillation"));
}
}
if (showSpeed) {
displaySpeed(tickCount, elapse);
}
return retCode;
}
use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.
the class TestThread method matchPins.
void matchPins() throws TestException {
int n = vector.columnName.length;
pin = new Instance[n];
CircuitState state = new CircuitState(this.project, this.circuit);
for (int i = 0; i < n; i++) {
String columnName = vector.columnName[i];
for (Component comp : circuit.getNonWires()) {
if (!(comp.getFactory() instanceof Pin))
continue;
Instance inst = Instance.getInstanceFor(comp);
InstanceState pinState = state.getInstanceState(comp);
String label = pinState.getAttributeValue(StdAttr.LABEL);
if (label == null || !label.equals(columnName))
continue;
if (Pin.FACTORY.getWidth(inst).getWidth() != vector.columnWidth[i].getWidth())
throw new TestException("test vector column '" + columnName + "' has width " + vector.columnWidth[i] + ", but pin has width " + Pin.FACTORY.getWidth(inst));
pin[i] = inst;
break;
}
if (pin[i] == null)
throw new TestException("test vector column '" + columnName + "' has no matching pin");
}
}
use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.
the class SubcircuitFactory method propagate.
@Override
public void propagate(InstanceState superState) {
CircuitState subState = getSubstate(superState);
CircuitAttributes attrs = (CircuitAttributes) superState.getAttributeSet();
Instance[] pins = attrs.getPinInstances();
for (int i = 0; i < pins.length; i++) {
Instance pin = pins[i];
InstanceState pinState = subState.getInstanceState(pin);
if (Pin.FACTORY.isInputPin(pin)) {
Value newVal = superState.getPortValue(i);
Value oldVal = Pin.FACTORY.getValue(pinState);
if (!newVal.equals(oldVal)) {
Pin.FACTORY.setValue(pinState, newVal);
Pin.FACTORY.propagate(pinState);
}
} else {
// it is output-only
Value val = pinState.getPortValue(0);
superState.setPort(i, val, 1);
}
}
}
use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.
the class Circuit method doTestVector.
/**
* Code taken from Cornell's version of Logisim:
* http://www.cs.cornell.edu/courses/cs3410/2015sp/
*/
public void doTestVector(Project project, Instance[] pin, Value[] val) throws TestException {
CircuitState state = project.getCircuitState();
state.reset();
for (int i = 0; i < pin.length; ++i) {
if (Pin.FACTORY.isInputPin(pin[i])) {
InstanceState pinState = state.getInstanceState(pin[i]);
Pin.FACTORY.setValue(pinState, val[i]);
}
}
Propagator prop = state.getPropagator();
try {
prop.propagate();
} catch (Throwable thr) {
thr.printStackTrace();
}
if (prop.isOscillating())
throw new TestException("oscilation detected");
FailException err = null;
for (int i = 0; i < pin.length; i++) {
InstanceState pinState = state.getInstanceState(pin[i]);
if (Pin.FACTORY.isInputPin(pin[i]))
continue;
Value v = Pin.FACTORY.getValue(pinState);
if (!val[i].compatible(v)) {
if (err == null)
err = new FailException(i, pinState.getAttributeValue(StdAttr.LABEL), val[i], v);
else
err.add(new FailException(i, pinState.getAttributeValue(StdAttr.LABEL), val[i], v));
}
}
if (err != null) {
throw err;
}
}
use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.
the class VhdlSimulatorTclComp method generate.
public void generate() {
/* Do not generate if file is already valid */
if (valid)
return;
StringBuilder comp_files = new StringBuilder();
comp_files.append("Autogenerated by logisim");
comp_files.append(System.getProperty("line.separator"));
/* 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);
String componentName = comp.getFactory().getHDLTopName(state.getInstance().getAttributeSet());
comp_files.append("vcom -reportprogress 300 -work work ../src/" + componentName + ".vhdl");
comp_files.append(System.getProperty("line.separator"));
}
}
/*
* Replace template blocks by generated data
*/
String template;
try {
template = new String(FileUtil.getBytes(this.getClass().getResourceAsStream((VhdlSimulator.SIM_RESOURCES_PATH + "comp.templ"))));
template = template.replaceAll("%date%", LocaleManager.parserSDF.format(new Date()));
template = template.replaceAll("%comp_files%", comp_files.toString());
} catch (IOException e) {
logger.error("Could not read template : {}", e.getMessage());
return;
}
PrintWriter writer;
try {
writer = new PrintWriter(VhdlSimulator.SIM_PATH + "comp.tcl", "UTF-8");
writer.print(template);
writer.close();
} catch (FileNotFoundException e) {
logger.error("Could not create run.tcl file : {}", e.getMessage());
e.printStackTrace();
return;
} catch (UnsupportedEncodingException e) {
logger.error("Could not create run.tcl file : {}", e.getMessage());
e.printStackTrace();
return;
}
valid = true;
}
Aggregations