Search in sources :

Example 16 with Instance

use of com.cburch.logisim.instance.Instance 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]);
    }
}
Also used : Instance(com.cburch.logisim.instance.Instance) ArrayList(java.util.ArrayList) Entry(com.cburch.logisim.analyze.model.Entry) InstanceState(com.cburch.logisim.instance.InstanceState) Value(com.cburch.logisim.data.Value) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 17 with Instance

use of com.cburch.logisim.instance.Instance in project logisim-evolution by reds-heig.

the class Analyze method getPinLabels.

// 
// getPinLabels
// 
/**
 * Returns a sorted map from Pin objects to String objects, listed in
 * canonical order (top-down order, with ties broken left-right).
 */
public static SortedMap<Instance, String> getPinLabels(Circuit circuit) {
    Comparator<Instance> locOrder = new Comparator<Instance>() {

        public int compare(Instance ac, Instance bc) {
            Location a = ac.getLocation();
            Location b = bc.getLocation();
            if (a.getY() < b.getY())
                return -1;
            if (a.getY() > b.getY())
                return 1;
            if (a.getX() < b.getX())
                return -1;
            if (a.getX() > b.getX())
                return 1;
            return a.hashCode() - b.hashCode();
        }
    };
    SortedMap<Instance, String> ret = new TreeMap<Instance, String>(locOrder);
    // Put the pins into the TreeMap, with null labels
    for (Instance pin : circuit.getAppearance().getPortOffsets(Direction.EAST).values()) {
        ret.put(pin, null);
    }
    // Process first the pins that the user has given labels.
    ArrayList<Instance> pinList = new ArrayList<Instance>(ret.keySet());
    HashSet<String> labelsTaken = new HashSet<String>();
    for (Instance pin : pinList) {
        String label = pin.getAttributeSet().getValue(StdAttr.LABEL);
        label = toValidLabel(label);
        if (label != null) {
            if (labelsTaken.contains(label)) {
                int i = 2;
                while (labelsTaken.contains(label + i)) i++;
                label = label + i;
            }
            ret.put(pin, label);
            labelsTaken.add(label);
        }
    }
    // Now process the unlabeled pins.
    for (Instance pin : pinList) {
        if (ret.get(pin) != null)
            continue;
        String defaultList;
        if (Pin.FACTORY.isInputPin(pin)) {
            defaultList = Strings.get("defaultInputLabels");
            if (defaultList.indexOf(",") < 0) {
                defaultList = "a,b,c,d,e,f,g,h";
            }
        } else {
            defaultList = Strings.get("defaultOutputLabels");
            if (defaultList.indexOf(",") < 0) {
                defaultList = "x,y,z,u,v,w,s,t";
            }
        }
        String[] options = defaultList.split(",");
        String label = null;
        for (int i = 0; label == null && i < options.length; i++) {
            if (!labelsTaken.contains(options[i])) {
                label = options[i];
            }
        }
        if (label == null) {
            // This is an extreme measure that should never happen
            // if the default labels are defined properly and the
            // circuit doesn't exceed the maximum number of pins.
            int i = 1;
            do {
                i++;
                label = "x" + i;
            } while (labelsTaken.contains(label));
        }
        labelsTaken.add(label);
        ret.put(pin, label);
    }
    return ret;
}
Also used : Instance(com.cburch.logisim.instance.Instance) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) Comparator(java.util.Comparator) Location(com.cburch.logisim.data.Location) HashSet(java.util.HashSet)

Example 18 with Instance

use of com.cburch.logisim.instance.Instance 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;
}
Also used : InstanceState(com.cburch.logisim.instance.InstanceState) Instance(com.cburch.logisim.instance.Instance) Propagator(com.cburch.logisim.circuit.Propagator) Value(com.cburch.logisim.data.Value) ArrayList(java.util.ArrayList)

Example 19 with Instance

use of com.cburch.logisim.instance.Instance in project logisim-evolution by reds-heig.

the class TtyInterface method run.

public static void run(Startup args) {
    File fileToOpen = args.getFilesToOpen().get(0);
    Loader loader = new Loader(null);
    LogisimFile file;
    try {
        file = loader.openLogisimFile(fileToOpen, args.getSubstitutions());
    } catch (LoadFailedException e) {
        logger.error("{}", Strings.get("ttyLoadError", fileToOpen.getName()));
        System.exit(-1);
        return;
    }
    int format = args.getTtyFormat();
    if ((format & FORMAT_STATISTICS) != 0) {
        format &= ~FORMAT_STATISTICS;
        displayStatistics(file);
    }
    if (format == 0) {
        // no simulation remaining to perform, so just exit
        System.exit(0);
    }
    Project proj = new Project(file);
    Circuit circuit = file.getMainCircuit();
    Map<Instance, String> pinNames = Analyze.getPinLabels(circuit);
    ArrayList<Instance> outputPins = new ArrayList<Instance>();
    Instance haltPin = null;
    for (Map.Entry<Instance, String> entry : pinNames.entrySet()) {
        Instance pin = entry.getKey();
        String pinName = entry.getValue();
        if (!Pin.FACTORY.isInputPin(pin)) {
            outputPins.add(pin);
            if (pinName.equals("halt")) {
                haltPin = pin;
            }
        }
    }
    CircuitState circState = new CircuitState(proj, circuit);
    // we have to do our initial propagation before the simulation starts -
    // it's necessary to populate the circuit with substates.
    circState.getPropagator().propagate();
    if (args.getLoadFile() != null) {
        try {
            boolean loaded = loadRam(circState, args.getLoadFile());
            if (!loaded) {
                logger.error("{}", Strings.get("loadNoRamError"));
                System.exit(-1);
            }
        } catch (IOException e) {
            logger.error("{}: {}", Strings.get("loadIoError"), e.toString());
            System.exit(-1);
        }
    }
    int ttyFormat = args.getTtyFormat();
    int simCode = runSimulation(circState, outputPins, haltPin, ttyFormat);
    System.exit(simCode);
}
Also used : CircuitState(com.cburch.logisim.circuit.CircuitState) Instance(com.cburch.logisim.instance.Instance) ArrayList(java.util.ArrayList) Loader(com.cburch.logisim.file.Loader) Circuit(com.cburch.logisim.circuit.Circuit) IOException(java.io.IOException) Project(com.cburch.logisim.proj.Project) LogisimFile(com.cburch.logisim.file.LogisimFile) LogisimFile(com.cburch.logisim.file.LogisimFile) File(java.io.File) Map(java.util.Map) LoadFailedException(com.cburch.logisim.file.LoadFailedException)

Example 20 with Instance

use of com.cburch.logisim.instance.Instance 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");
    }
}
Also used : CircuitState(com.cburch.logisim.circuit.CircuitState) InstanceState(com.cburch.logisim.instance.InstanceState) Pin(com.cburch.logisim.std.wiring.Pin) TestException(com.cburch.logisim.data.TestException) Instance(com.cburch.logisim.instance.Instance) Component(com.cburch.logisim.comp.Component)

Aggregations

Instance (com.cburch.logisim.instance.Instance)28 ArrayList (java.util.ArrayList)14 Map (java.util.Map)10 Direction (com.cburch.logisim.data.Direction)9 Location (com.cburch.logisim.data.Location)9 HashMap (java.util.HashMap)9 Graphics (java.awt.Graphics)5 CanvasObject (com.cburch.draw.model.CanvasObject)4 Component (com.cburch.logisim.comp.Component)4 Bounds (com.cburch.logisim.data.Bounds)4 InstanceState (com.cburch.logisim.instance.InstanceState)4 List (java.util.List)4 TreeMap (java.util.TreeMap)4 Rectangle (com.cburch.draw.shapes.Rectangle)3 Text (com.cburch.draw.shapes.Text)3 CircuitState (com.cburch.logisim.circuit.CircuitState)3 Value (com.cburch.logisim.data.Value)3 Font (java.awt.Font)3 FontMetrics (java.awt.FontMetrics)3 Graphics2D (java.awt.Graphics2D)3