Search in sources :

Example 46 with Value

use of com.cburch.logisim.data.Value 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 47 with Value

use of com.cburch.logisim.data.Value in project logisim-evolution by reds-heig.

the class TestPanel method getRowData.

public void getRowData(int firstRow, int numRows, ValueTable.Cell[][] rowData) {
    Model model = getModel();
    TestException[] results = model.getResults();
    int numPass = model.getPass();
    int numFail = model.getFail();
    TestVector vec = model.getVector();
    int columns = vec.columnName.length;
    String[] msg = new String[columns];
    Value[] altdata = new Value[columns];
    String passMsg = Strings.get("passStatus");
    String failMsg = Strings.get("failStatus");
    for (int i = firstRow; i < firstRow + numRows; i++) {
        int row = model.sortedIndex(i);
        Value[] data = vec.data.get(row);
        String rowmsg = null;
        String status = null;
        boolean failed = false;
        if (row < numPass + numFail) {
            TestException err = results[row];
            if (err != null && err instanceof FailException) {
                failed = true;
                for (FailException e = (FailException) err; e != null; e = e.getMore()) {
                    int col = e.getColumn();
                    msg[col] = StringUtil.format(Strings.get("expectedValueMessage"), e.getExpected().toDisplayString(getColumnValueRadix(col + 1)));
                    altdata[col] = e.getComputed();
                }
            } else if (err != null) {
                failed = true;
                rowmsg = err.getMessage();
            }
            status = failed ? failMsg : passMsg;
        }
        rowData[i - firstRow][0] = new ValueTable.Cell(status, rowmsg != null ? failColor : null, null, rowmsg);
        for (int col = 0; col < columns; col++) {
            rowData[i - firstRow][col + 1] = new ValueTable.Cell(altdata[col] != null ? altdata[col] : data[col], msg[col] != null ? failColor : null, null, msg[col]);
            msg[col] = null;
            altdata[col] = null;
        }
    }
}
Also used : TestException(com.cburch.logisim.data.TestException) TestVector(com.cburch.logisim.data.TestVector) ValueTable(com.cburch.logisim.gui.log.ValueTable) Value(com.cburch.logisim.data.Value) FailException(com.cburch.logisim.data.FailException)

Example 48 with Value

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

Example 49 with Value

use of com.cburch.logisim.data.Value in project logisim-evolution by reds-heig.

the class CircuitWires method propagate.

// 
// utility methods
// 
void propagate(CircuitState circState, Set<Location> points) {
    BundleMap map = getBundleMap();
    // affected
    CopyOnWriteArraySet<WireThread> dirtyThreads = new CopyOnWriteArraySet<WireThread>();
    // threads
    // get state, or create a new one if current state is outdated
    State s = circState.getWireData();
    if (s == null || s.bundleMap != map) {
        // if it is outdated, we need to compute for all threads
        s = new State(map);
        for (WireBundle b : map.getBundles()) {
            WireThread[] th = b.threads;
            if (b.isValid() && th != null) {
                for (WireThread t : th) {
                    dirtyThreads.add(t);
                }
            }
        }
        circState.setWireData(s);
    }
    // determine affected threads, and set values for unwired points
    for (Location p : points) {
        WireBundle pb = map.getBundleAt(p);
        if (pb == null) {
            // point is not wired
            circState.setValueByWire(p, circState.getComponentOutputAt(p));
        } else {
            WireThread[] th = pb.threads;
            if (!pb.isValid() || th == null) {
                // immediately propagate NILs across invalid bundles
                CopyOnWriteArraySet<Location> pbPoints = pb.points;
                if (pbPoints == null) {
                    circState.setValueByWire(p, Value.NIL);
                } else {
                    for (Location loc2 : pbPoints) {
                        circState.setValueByWire(loc2, Value.NIL);
                    }
                }
            } else {
                for (WireThread t : th) {
                    dirtyThreads.add(t);
                }
            }
        }
    }
    if (dirtyThreads.isEmpty())
        return;
    // determine values of affected threads
    HashSet<ThreadBundle> bundles = new HashSet<ThreadBundle>();
    for (WireThread t : dirtyThreads) {
        Value v = getThreadValue(circState, t);
        s.thr_values.put(t, v);
        bundles.addAll(t.getBundles());
    }
    // now propagate values through circuit
    for (ThreadBundle tb : bundles) {
        WireBundle b = tb.b;
        Value bv = null;
        if (!b.isValid() || b.threads == null) {
            // do nothing
            ;
        } else if (b.threads.length == 1) {
            bv = s.thr_values.get(b.threads[0]);
        } else {
            Value[] tvs = new Value[b.threads.length];
            boolean tvs_valid = true;
            for (int i = 0; i < tvs.length; i++) {
                Value tv = s.thr_values.get(b.threads[i]);
                if (tv == null) {
                    tvs_valid = false;
                    break;
                }
                tvs[i] = tv;
            }
            if (tvs_valid)
                bv = Value.create(tvs);
        }
        if (bv != null) {
            for (Location p : b.points) {
                circState.setValueByWire(p, bv);
            }
        }
    }
}
Also used : CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Value(com.cburch.logisim.data.Value) Location(com.cburch.logisim.data.Location) HashSet(java.util.HashSet)

Example 50 with Value

use of com.cburch.logisim.data.Value in project logisim-evolution by reds-heig.

the class Propagator method checkComponentEnds.

// 
// private methods
// 
void checkComponentEnds(CircuitState state, Component comp) {
    for (EndData end : comp.getEnds()) {
        Location loc = end.getLocation();
        SetData oldHead = state.causes.get(loc);
        Value oldVal = computeValue(oldHead);
        SetData newHead = removeCause(state, oldHead, loc, comp);
        Value newVal = computeValue(newHead);
        Value wireVal = state.getValueByWire(loc);
        if (!newVal.equals(oldVal) || wireVal != null) {
            state.markPointAsDirty(loc);
        }
        if (wireVal != null)
            state.setValueByWire(loc, Value.NIL);
    }
}
Also used : EndData(com.cburch.logisim.comp.EndData) Value(com.cburch.logisim.data.Value) Location(com.cburch.logisim.data.Location)

Aggregations

Value (com.cburch.logisim.data.Value)89 BitWidth (com.cburch.logisim.data.BitWidth)30 Graphics (java.awt.Graphics)15 Location (com.cburch.logisim.data.Location)9 Color (java.awt.Color)8 Bounds (com.cburch.logisim.data.Bounds)7 InstanceDataSingleton (com.cburch.logisim.instance.InstanceDataSingleton)7 AttributeSet (com.cburch.logisim.data.AttributeSet)6 InstanceState (com.cburch.logisim.instance.InstanceState)6 Direction (com.cburch.logisim.data.Direction)3 Instance (com.cburch.logisim.instance.Instance)3 Port (com.cburch.logisim.instance.Port)3 CircuitState (com.cburch.logisim.circuit.CircuitState)2 Component (com.cburch.logisim.comp.Component)2 FailException (com.cburch.logisim.data.FailException)2 TestException (com.cburch.logisim.data.TestException)2 Dimension (java.awt.Dimension)2 FontMetrics (java.awt.FontMetrics)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2