Search in sources :

Example 1 with InstanceState

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

Example 2 with InstanceState

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;
}
Also used : Port(com.cburch.logisim.instance.Port) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Date(java.util.Date) InstanceState(com.cburch.logisim.instance.InstanceState) Component(com.cburch.logisim.comp.Component) PrintWriter(java.io.PrintWriter)

Example 3 with InstanceState

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;
}
Also used : CircuitState(com.cburch.logisim.circuit.CircuitState) InstanceState(com.cburch.logisim.instance.InstanceState) Component(com.cburch.logisim.comp.Component) Ram(com.cburch.logisim.std.memory.Ram)

Example 4 with InstanceState

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;
}
Also used : CircuitState(com.cburch.logisim.circuit.CircuitState) InstanceState(com.cburch.logisim.instance.InstanceState) Keyboard(com.cburch.logisim.std.io.Keyboard) Tty(com.cburch.logisim.std.io.Tty) Component(com.cburch.logisim.comp.Component)

Example 5 with InstanceState

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]);
    }
}
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)

Aggregations

InstanceState (com.cburch.logisim.instance.InstanceState)13 Component (com.cburch.logisim.comp.Component)5 Value (com.cburch.logisim.data.Value)5 Instance (com.cburch.logisim.instance.Instance)4 CircuitState (com.cburch.logisim.circuit.CircuitState)3 TestException (com.cburch.logisim.data.TestException)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Entry (com.cburch.logisim.analyze.model.Entry)1 Propagator (com.cburch.logisim.circuit.Propagator)1 BitWidth (com.cburch.logisim.data.BitWidth)1 FailException (com.cburch.logisim.data.FailException)1 Port (com.cburch.logisim.instance.Port)1 Keyboard (com.cburch.logisim.std.io.Keyboard)1 Tty (com.cburch.logisim.std.io.Tty)1 Ram (com.cburch.logisim.std.memory.Ram)1