import era.mi.logic.Simulation;
import era.mi.logic.types.BitVector;
-import era.mi.logic.wires.Wire;
+import era.mi.logic.wires.Wire.ReadEnd;
import era.mi.logic.wires.WireObserver;
/**
}
@Override
- public void update(Wire initiator, BitVector oldValues)
+ public void update(ReadEnd initiator, BitVector oldValues)
{
Simulation.TIMELINE.addEvent(e -> compute(), processTime);
}
import era.mi.logic.types.Bit;
import era.mi.logic.types.BitVector;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
public class BitDisplay extends BasicComponent
{
- private final WireEnd in;
+ private final ReadEnd in;
private BitVector displayedValue;
- public BitDisplay(WireEnd in)
+ public BitDisplay(ReadEnd in)
{
super(1);
this.in = in;
}
@Override
- public List<WireEnd> getAllInputs()
+ public List<ReadEnd> getAllInputs()
{
return List.of(in);
}
@Override
- public List<WireEnd> getAllOutputs()
+ public List<ReadWriteEnd> getAllOutputs()
{
return List.of();
}
import era.mi.logic.timeline.TimelineEventHandler;
import era.mi.logic.types.Bit;
import era.mi.logic.wires.Wire;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
public class Clock implements TimelineEventHandler, Component
{
private boolean toggle = false;
- private WireEnd out;
+ private ReadWriteEnd out;
private int delta;
/**
* @param out {@link Wire} the clock's impulses are fed into
* @param delta ticks between rising and falling edge
*/
- public Clock(WireEnd out, int delta)
+ public Clock(ReadWriteEnd out, int delta)
{
this.delta = delta;
this.out = out;
toggle = !toggle;
}
- public WireEnd getOut()
+ public ReadWriteEnd getOut()
{
return out;
}
}
@Override
- public List<WireEnd> getAllInputs()
+ public List<ReadEnd> getAllInputs()
{
return List.of();
}
@Override
- public List<WireEnd> getAllOutputs()
+ public List<ReadWriteEnd> getAllOutputs()
{
return List.of(out);
}
import java.util.List;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
public interface Component
{
* Returns immutable list of all inputs to the {@link Component} (including e.g. the select bits to a MUX). Intended for visualization
* in the UI.
*/
- public List<WireEnd> getAllInputs();
+ public List<ReadEnd> getAllInputs();
/**
* Returns immutable list of all outputs to the {@link Component}. Intended for visualization in the UI.
*/
- public List<WireEnd> getAllOutputs();
+ public List<ReadWriteEnd> getAllOutputs();
}
import era.mi.logic.Simulation;
import era.mi.logic.types.BitVector;
-import era.mi.logic.wires.Wire;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
import era.mi.logic.wires.WireObserver;
public class Connector implements WireObserver, Component
{
private boolean connected;
- private final WireEnd a;
- private final WireEnd b;
+ private final ReadWriteEnd a;
+ private final ReadWriteEnd b;
- public Connector(WireEnd a, WireEnd b)
+ public Connector(ReadWriteEnd a, ReadWriteEnd b)
{
if (a.length() != b.length())
throw new IllegalArgumentException(String.format("WireArray width does not match: %d, %d", a.length(), b.length()));
public void connect()
{
connected = true;
- update(a.getWire());
- update(b.getWire());
+ update(a);
+ update(b);
}
public void disconnect()
}
@Override
- public void update(Wire initiator, BitVector oldValues)
+ public void update(ReadEnd initiator, BitVector oldValues)
{
if (connected)
Simulation.TIMELINE.addEvent(e -> update(initiator), 1);
}
- private void update(Wire initiator)
+ private void update(ReadEnd initiator)
{
- if (initiator == a.getWire())
+ if (initiator == a)
b.feedSignals(a.wireValuesExcludingMe());
else
a.feedSignals(b.wireValuesExcludingMe());
}
@Override
- public List<WireEnd> getAllInputs()
+ public List<ReadEnd> getAllInputs()
{
return List.of(a, b);
}
@Override
- public List<WireEnd> getAllOutputs()
+ public List<ReadWriteEnd> getAllOutputs()
{
return List.of(a, b);
}
import java.util.List;
import era.mi.logic.wires.Wire;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
/**
* Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the
*/
public class Demux extends BasicComponent
{
- private final WireEnd select, in;
- private final WireEnd[] outputs;
+ private final ReadEnd select, in;
+ private final ReadWriteEnd[] outputs;
private final int outputSize;
private int selected = -1;
* @param select Indexes the output array to which the input is mapped. Must have enough bits to index all outputs.
* @param outputs One of these outputs receives the input signal, depending on the select bits
*/
- public Demux(int processTime, WireEnd in, WireEnd select, WireEnd... outputs)
+ public Demux(int processTime, ReadEnd in, ReadEnd select, ReadWriteEnd... outputs)
{
super(processTime);
outputSize = in.length();
}
@Override
- public List<WireEnd> getAllInputs()
+ public List<ReadEnd> getAllInputs()
{
return List.of(in, select);
}
@Override
- public List<WireEnd> getAllOutputs()
+ public List<ReadWriteEnd> getAllOutputs()
{
return List.of(outputs);
}
import java.util.List;
import era.mi.logic.types.Bit;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
/**
* This class models a simple on/off (ONE/ZERO) switch for user interaction.
*/
public class ManualSwitch implements Component
{
- private WireEnd output;
+ private ReadWriteEnd output;
private boolean isOn;
- public ManualSwitch(WireEnd output)
+ public ManualSwitch(ReadWriteEnd output)
{
if (output.length() != 1)
throw new IllegalArgumentException("Switch output can be only a single wire");
}
@Override
- public List<WireEnd> getAllInputs()
+ public List<ReadEnd> getAllInputs()
{
return List.of();
}
@Override
- public List<WireEnd> getAllOutputs()
+ public List<ReadWriteEnd> getAllOutputs()
{
return List.of(output);
}
import era.mi.logic.types.BitVector;
import era.mi.logic.wires.Wire;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
import era.mi.logic.wires.WireObserver;
public class Merger implements WireObserver, Component
{
- private WireEnd out;
- private WireEnd[] inputs;
+ private ReadWriteEnd out;
+ private ReadEnd[] inputs;
private int[] beginningIndex;
/**
* @param union The output of merging n {@link Wire}s into one. Must have length = a1.length() + a2.length() + ... + an.length().
* @param inputs The inputs to be merged into the union
*/
- public Merger(WireEnd union, WireEnd... inputs)
+ public Merger(ReadWriteEnd union, ReadEnd... inputs)
{
this.inputs = inputs;
this.out = union;
"The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length().");
}
- public WireEnd getInput(int index)
+ public ReadEnd getInput(int index)
{
return inputs[index];
}
- public WireEnd getUnion()
+ public ReadEnd getUnion()
{
return out;
}
@Override
- public void update(Wire initiator, BitVector oldValues)
+ public void update(ReadEnd initiator, BitVector oldValues)
{
int index = find(initiator);
int beginning = beginningIndex[index];
out.feedSignals(beginning, inputs[index].getValues());
}
- private int find(Wire w)
+ private int find(ReadEnd r)
{
for (int i = 0; i < inputs.length; i++)
- if (inputs[i].getWire() == w)
+ if (inputs[i] == r)
return i;
return -1;
}
- public WireEnd[] getInputs()
+ public ReadEnd[] getInputs()
{
return inputs.clone();
}
@Override
- public List<WireEnd> getAllInputs()
+ public List<ReadEnd> getAllInputs()
{
return List.of(inputs);
}
@Override
- public List<WireEnd> getAllOutputs()
+ public List<ReadWriteEnd> getAllOutputs()
{
return List.of(out);
}
import java.util.List;
import era.mi.logic.wires.Wire;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
/**
* Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the
*/
public class Mux extends BasicComponent
{
- private WireEnd select;
- private WireEnd out;
- private WireEnd[] inputs;
+ private ReadEnd select;
+ private ReadWriteEnd out;
+ private ReadEnd[] inputs;
private final int outputSize;
/**
* @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.
* @param inputs One of these inputs is mapped to the output, depending on the select bits
*/
- public Mux(int processTime, WireEnd out, WireEnd select, WireEnd... inputs)
+ public Mux(int processTime, ReadWriteEnd out, ReadEnd select, ReadEnd... inputs)
{
super(processTime);
outputSize = out.length();
this.out = out;
}
- public WireEnd getOut()
+ public ReadEnd getOut()
{
return out;
}
- public WireEnd getSelect()
+ public ReadEnd getSelect()
{
return select;
}
return;
}
- WireEnd active = inputs[selectValue];
+ ReadEnd active = inputs[selectValue];
out.feedSignals(active.getValues());
}
@Override
- public List<WireEnd> getAllInputs()
+ public List<ReadEnd> getAllInputs()
{
- ArrayList<WireEnd> wires = new ArrayList<WireEnd>(Arrays.asList(inputs));
+ ArrayList<ReadEnd> wires = new ArrayList<ReadEnd>(Arrays.asList(inputs));
wires.add(select);
return Collections.unmodifiableList(wires);
}
@Override
- public List<WireEnd> getAllOutputs()
+ public List<ReadWriteEnd> getAllOutputs()
{
return List.of(out);
}
package era.mi.logic.components;
+import java.util.List;
+
import era.mi.logic.types.BitVector;
-import era.mi.logic.wires.Wire;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
import era.mi.logic.wires.WireObserver;
-public class Splitter implements WireObserver
+public class Splitter implements WireObserver, Component
{
- private WireEnd input;
- private WireEnd[] outputs;
+ private ReadEnd input;
+ private ReadWriteEnd[] outputs;
- public Splitter(WireEnd input, WireEnd... outputs)
+ public Splitter(ReadEnd input, ReadWriteEnd... outputs)
{
this.input = input;
this.outputs = outputs;
input.addObserver(this);
int length = 0;
- for (WireEnd out : outputs)
+ for (ReadEnd out : outputs)
length += out.length();
if (input.length() != length)
}
@Override
- public void update(Wire initiator, BitVector oldValues)
+ public void update(ReadEnd initiator, BitVector oldValues)
{
compute();
}
+
+ @Override
+ public List<ReadEnd> getAllInputs()
+ {
+ return List.of(input);
+ }
+
+ @Override
+ public List<ReadWriteEnd> getAllOutputs()
+ {
+ return List.of(outputs);
+ }
}
import java.util.List;
import era.mi.logic.types.Bit;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
public class TriStateBuffer extends BasicComponent
{
- WireEnd in, enable;
- WireEnd out;
+ ReadEnd in, enable;
+ ReadWriteEnd out;
- public TriStateBuffer(int processTime, WireEnd in, WireEnd out, WireEnd enable)
+ public TriStateBuffer(int processTime, ReadEnd in, ReadWriteEnd out, ReadEnd enable)
{
super(processTime);
if (in.length() != out.length())
}
@Override
- public List<WireEnd> getAllInputs()
+ public List<ReadEnd> getAllInputs()
{
return List.of(in, enable);
}
@Override
- public List<WireEnd> getAllOutputs()
+ public List<ReadWriteEnd> getAllOutputs()
{
return List.of(out);
}
package era.mi.logic.components.gates;
import era.mi.logic.types.BitVector.BitVectorMutator;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
public class AndGate extends MultiInputGate
{
- public AndGate(int processTime, WireEnd out, WireEnd... in)
+ public AndGate(int processTime, ReadWriteEnd out, ReadEnd... in)
{
super(processTime, BitVectorMutator::and, out, in);
}
import era.mi.logic.components.BasicComponent;
import era.mi.logic.types.BitVector.BitVectorMutator;
import era.mi.logic.types.MutationOperation;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
public abstract class MultiInputGate extends BasicComponent
{
- protected WireEnd[] in;
- protected WireEnd out;
+ protected ReadEnd[] in;
+ protected ReadWriteEnd out;
protected final int length;
protected MutationOperation op;
- protected MultiInputGate(int processTime, MutationOperation op, WireEnd out, WireEnd... in)
+ protected MultiInputGate(int processTime, MutationOperation op, ReadWriteEnd out, ReadEnd... in)
{
super(processTime);
this.op = op;
this.in = in.clone();
if (in.length < 1)
throw new IllegalArgumentException(String.format("Cannot create gate with %d wires.", in.length));
- for (WireEnd w : in)
+ for (ReadEnd w : in)
{
if (w.length() != length)
throw new IllegalArgumentException("All wires connected to the gate must be of uniform length.");
}
@Override
- public List<WireEnd> getAllInputs()
+ public List<ReadEnd> getAllInputs()
{
return List.of(in);
}
@Override
- public List<WireEnd> getAllOutputs()
+ public List<ReadWriteEnd> getAllOutputs()
{
return List.of(out);
}
protected void compute()
{
BitVectorMutator mutator = BitVectorMutator.empty();
- for (WireEnd w : in)
+ for (ReadEnd w : in)
op.apply(mutator, w.getValues());
out.feedSignals(mutator.get());
}
import java.util.List;
import era.mi.logic.components.BasicComponent;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
public class NotGate extends BasicComponent
{
- private WireEnd in;
- private WireEnd out;
+ private ReadEnd in;
+ private ReadWriteEnd out;
- public NotGate(int processTime, WireEnd in, WireEnd out)
+ public NotGate(int processTime, ReadEnd in, ReadWriteEnd out)
{
super(processTime);
this.in = in;
out.feedSignals(in.getValues().not());
}
- public WireEnd getIn()
+ public ReadEnd getIn()
{
return in;
}
- public WireEnd getOut()
+ public ReadEnd getOut()
{
return out;
}
@Override
- public List<WireEnd> getAllInputs()
+ public List<ReadEnd> getAllInputs()
{
return List.of(in);
}
@Override
- public List<WireEnd> getAllOutputs()
+ public List<ReadWriteEnd> getAllOutputs()
{
return List.of(out);
}
package era.mi.logic.components.gates;
import era.mi.logic.types.BitVector.BitVectorMutator;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
public class OrGate extends MultiInputGate
{
- public OrGate(int processTime, WireEnd out, WireEnd... in)
+ public OrGate(int processTime, ReadWriteEnd out, ReadEnd... in)
{
super(processTime, BitVectorMutator::or, out, in);
}
package era.mi.logic.components.gates;
import era.mi.logic.types.BitVector.BitVectorMutator;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
/**
* Outputs 1 when the number of 1 inputs is odd.
*/
public class XorGate extends MultiInputGate
{
- public XorGate(int processTime, WireEnd out, WireEnd... in)
+ public XorGate(int processTime, ReadWriteEnd out, ReadEnd... in)
{
super(processTime, BitVectorMutator::xor, out, in);
}
package era.mi.logic.tests;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.fail;
import java.util.function.LongConsumer;
import era.mi.logic.types.Bit;
import era.mi.logic.types.BitVector;
import era.mi.logic.wires.Wire;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
-@SuppressWarnings("unused")
class ComponentTest
{
new TriStateBuffer(1, a.createReadOnlyEnd(), b.createEnd(), en.createReadOnlyEnd());
new TriStateBuffer(1, b.createReadOnlyEnd(), a.createEnd(), notEn.createReadOnlyEnd());
- WireEnd enI = en.createEnd(), aI = a.createEnd(), bI = b.createEnd();
+ ReadWriteEnd enI = en.createEnd(), aI = a.createEnd(), bI = b.createEnd();
enI.feedSignals(Bit.ONE);
aI.feedSignals(Bit.ONE);
bI.feedSignals(Bit.Z);
{
Simulation.TIMELINE.reset();
Wire a = new Wire(4, 3), b = new Wire(4, 6), c = new Wire(4, 4), select = new Wire(2, 5), out = new Wire(4, 1);
- WireEnd selectIn = select.createEnd();
+ ReadWriteEnd selectIn = select.createEnd();
selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
a.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
{
Simulation.TIMELINE.reset();
Wire a = new Wire(4, 3), b = new Wire(4, 6), c = new Wire(4, 4), select = new Wire(2, 5), in = new Wire(4, 1);
- WireEnd selectIn = select.createEnd();
+ ReadWriteEnd selectIn = select.createEnd();
selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
in.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
new NotGate(1, t2.createReadOnlyEnd(), q.createEnd());
new NotGate(1, t1.createReadOnlyEnd(), nq.createEnd());
- WireEnd sIn = s.createEnd(), rIn = r.createEnd();
+ ReadWriteEnd sIn = s.createEnd(), rIn = r.createEnd();
sIn.feedSignals(Bit.ONE);
rIn.feedSignals(Bit.ZERO);
{
Simulation.TIMELINE.reset();
Wire w = new Wire(2, 1);
- WireEnd wI1 = w.createEnd(), wI2 = w.createEnd();
+ ReadWriteEnd wI1 = w.createEnd(), wI2 = w.createEnd();
wI1.feedSignals(Bit.ONE, Bit.Z);
wI2.feedSignals(Bit.Z, Bit.X);
Simulation.TIMELINE.executeAll();
assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
wI2.feedSignals(Bit.ONE, Bit.Z);
- w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
+ ReadEnd rE = w.createReadOnlyEnd();
+ rE.addObserver((i, oldValues) -> fail("WireEnd notified observer, although value did not change."));
Simulation.TIMELINE.executeAll();
+ rE.close();
+ wI1.feedSignals(Bit.X, Bit.X);
+ Simulation.TIMELINE.executeAll();
+ wI1.addObserver((i, oldValues) -> fail("WireEnd notified observer, although it was closed."));
+ wI1.close();
assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
}
Wire a = new Wire(1, 2);
Wire b = new Wire(1, 2);
Wire c = new Wire(1, 2);
- WireEnd aI = a.createEnd();
- WireEnd bI = b.createEnd();
- WireEnd cI = c.createEnd();
+ ReadWriteEnd aI = a.createEnd();
+ ReadWriteEnd bI = b.createEnd();
+ ReadWriteEnd cI = c.createEnd();
TestBitDisplay test = new TestBitDisplay(c.createReadOnlyEnd());
TestBitDisplay test2 = new TestBitDisplay(a.createReadOnlyEnd());
import era.mi.logic.Simulation;
import era.mi.logic.components.BitDisplay;
import era.mi.logic.types.Bit;
-import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.Wire.ReadEnd;
public final class TestBitDisplay extends BitDisplay
{
- public TestBitDisplay(WireEnd in)
+ public TestBitDisplay(ReadEnd in)
{
super(in);
}
package era.mi.logic.wires;
-import static era.mi.logic.types.Bit.*;
+import static era.mi.logic.types.Bit.U;
+import static era.mi.logic.types.Bit.Z;
import java.util.ArrayList;
import java.util.List;
{
private BitVector values;
public final int travelTime;
- private List<WireObserver> observers = new ArrayList<WireObserver>();
+ private List<ReadEnd> attached = new ArrayList<ReadEnd>();
public final int length;
- private List<WireEnd> inputs = new ArrayList<WireEnd>();
+ private List<ReadWriteEnd> inputs = new ArrayList<ReadWriteEnd>();
public Wire(int length, int travelTime)
{
private void recalculateMultipleInputs()
{
BitVectorMutator mutator = BitVectorMutator.empty();
- for (WireEnd wireArrayEnd : inputs)
+ for (ReadWriteEnd wireArrayEnd : inputs)
mutator.join(wireArrayEnd.getInputValues());
setNewValues(mutator.get());
}
case Z:
case X:
return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
- // Random number?
case ONE:
val |= mask;
break;
*
* @author Fabian Stemmler
*/
- public boolean addObserver(WireObserver ob)
+ private void attachEnd(ReadEnd end)
{
- return observers.add(ob);
+ attached.add(end);
+ }
+
+ private void detachEnd(ReadEnd end)
+ {
+ attached.remove(end);
}
private void notifyObservers(BitVector oldValues)
{
- for (WireObserver o : observers)
- o.update(this, oldValues);
+ for (ReadEnd o : attached)
+ o.update(oldValues);
}
/**
- * Create and register a {@link WireEnd} object, which is tied to this {@link Wire}.
+ * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
*/
- public WireEnd createEnd()
+ public ReadWriteEnd createEnd()
{
- return new WireEnd(false);
+ return new ReadWriteEnd();
}
/**
- * Create a {@link WireEnd} object, which is tied to this {@link Wire}. This {@link WireEnd} cannot written to.
+ * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
*/
- public WireEnd createReadOnlyEnd()
+ public ReadEnd createReadOnlyEnd()
{
- return new WireEnd(true);
+ return new ReadEnd();
}
- private void registerInput(WireEnd toRegister)
+ private void registerInput(ReadWriteEnd toRegister)
{
inputs.add(toRegister);
}
/**
- * A {@link WireEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
+ * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
* {@link Wire}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs than Z 0
* and 1 turn into X when they are mixed
*
* @author Fabian Stemmler
*/
- public class WireEnd
+ public class ReadEnd
+ {
+ private List<WireObserver> observers = new ArrayList<WireObserver>();
+
+ private ReadEnd()
+ {
+ super();
+ Wire.this.attachEnd(this);
+ }
+
+ public void update(BitVector oldValues)
+ {
+ for (WireObserver ob : observers)
+ ob.update(this, oldValues);
+ }
+
+ /**
+ * Included for convenient use on {@link Wire}s of length 1.
+ *
+ * @return The value of bit 0.
+ *
+ * @author Fabian Stemmler
+ */
+ public Bit getValue()
+ {
+ return Wire.this.getValue();
+ }
+
+ /**
+ * @param index Index of the requested bit.
+ * @return The value of the indexed bit.
+ *
+ * @author Fabian Stemmler
+ */
+ public Bit getValue(int index)
+ {
+ return Wire.this.getValue(index);
+ }
+
+ /**
+ * @param index Index of the requested bit.
+ * @return The value of the indexed bit.
+ *
+ * @author Fabian Stemmler
+ */
+ public BitVector getValues()
+ {
+ return Wire.this.getValues();
+ }
+
+ /**
+ * @param start Start of the wanted segment. (inclusive)
+ * @param end End of the wanted segment. (exclusive)
+ * @return The values of the segment of {@link Bit}s indexed.
+ *
+ * @author Fabian Stemmler
+ */
+ public BitVector getValues(int start, int end)
+ {
+ return Wire.this.getValues(start, end);
+ }
+
+ /**
+ * The {@link Wire} is interpreted as an unsigned integer with n bits.
+ *
+ * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the
+ * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
+ *
+ * @author Fabian Stemmler
+ */
+ public boolean hasNumericValue()
+ {
+ return Wire.this.hasNumericValue();
+ }
+
+ /**
+ * The {@link Wire} is interpreted as an unsigned integer with n bits.
+ *
+ * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
+ *
+ * @author Fabian Stemmler
+ */
+ public long getUnsignedValue()
+ {
+ return Wire.this.getUnsignedValue();
+ }
+
+ /**
+ * The {@link Wire} is interpreted as a signed integer with n bits.
+ *
+ * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
+ *
+ * @author Fabian Stemmler
+ */
+ public long getSignedValue()
+ {
+ return Wire.this.getSignedValue();
+ }
+
+ @Override
+ public String toString()
+ {
+ return Wire.this.toString();
+ }
+
+ public void close()
+ {
+ inputs.remove(this);
+ detachEnd(this);
+ recalculate();
+ }
+
+ public int length()
+ {
+ return length;
+ }
+
+ public boolean addObserver(WireObserver ob)
+ {
+ return observers.add(ob);
+ }
+
+ public Wire getWire()
+ {
+ return Wire.this;
+ }
+ }
+
+ public class ReadWriteEnd extends ReadEnd
{
private boolean open;
private BitVector inputValues;
- private WireEnd(boolean readOnly)
+ private ReadWriteEnd()
{
super();
- open = !readOnly; // TODO: that makes sense, doesn't it?
+ open = true;
initValues();
- if (!readOnly)
- registerInput(this);
+ registerInput(this);
}
private void initValues()
}
/**
- * @return The value (of bit 0) the {@link WireEnd} is currently feeding into the associated {@link Wire}.
+ * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
*/
public Bit getInputValue()
{
}
/**
- * @return The value which the {@link WireEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
+ * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
*/
public Bit getInputValue(int index)
{
}
/**
- * @return A copy (safe to modify) of the values the {@link WireEnd} is currently feeding into the associated {@link Wire}.
+ * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
*/
public BitVector getInputValues()
{
}
/**
- * {@link WireEnd} now feeds Z into the associated {@link Wire}.
+ * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
*/
public void clearSignals()
{
public BitVector wireValuesExcludingMe()
{
BitVectorMutator mutator = BitVectorMutator.empty();
- for (WireEnd wireEnd : inputs)
+ for (ReadWriteEnd wireEnd : inputs)
{
if (wireEnd == this)
continue;
return mutator.get();
}
- /**
- * Included for convenient use on {@link Wire}s of length 1.
- *
- * @return The value of bit 0.
- *
- * @author Fabian Stemmler
- */
- public Bit getValue()
- {
- return Wire.this.getValue();
- }
-
- /**
- * @param index Index of the requested bit.
- * @return The value of the indexed bit.
- *
- * @author Fabian Stemmler
- */
- public Bit getValue(int index)
- {
- return Wire.this.getValue(index);
- }
-
- /**
- * @param index Index of the requested bit.
- * @return The value of the indexed bit.
- *
- * @author Fabian Stemmler
- */
- public BitVector getValues()
- {
- return Wire.this.getValues();
- }
-
- /**
- * @param start Start of the wanted segment. (inclusive)
- * @param end End of the wanted segment. (exclusive)
- * @return The values of the segment of {@link Bit}s indexed.
- *
- * @author Fabian Stemmler
- */
- public BitVector getValues(int start, int end)
- {
- return Wire.this.getValues(start, end);
- }
-
- /**
- * The {@link Wire} is interpreted as an unsigned integer with n bits.
- *
- * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the
- * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
- *
- * @author Fabian Stemmler
- */
- public boolean hasNumericValue()
- {
- return Wire.this.hasNumericValue();
- }
-
- /**
- * The {@link Wire} is interpreted as an unsigned integer with n bits.
- *
- * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
- *
- * @author Fabian Stemmler
- */
- public long getUnsignedValue()
- {
- return Wire.this.getUnsignedValue();
- }
-
- /**
- * The {@link Wire} is interpreted as a signed integer with n bits.
- *
- * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
- *
- * @author Fabian Stemmler
- */
- public long getSignedValue()
- {
- return Wire.this.getSignedValue();
- }
-
@Override
public String toString()
{
return inputValues.toString();
- // return String.format("%s \nFeeding: %s", WireArray.this.toString(), Arrays.toString(inputValues));
- }
-
- public void close()
- {
- inputs.remove(this);
- open = false;
- }
-
- public int length()
- {
- return length;
- }
-
- public boolean addObserver(WireObserver ob)
- {
- return Wire.this.addObserver(ob);
- }
-
- public Wire getWire()
- {
- return Wire.this;
}
}
// Arrays.toString(values), inputs.stream().map(i -> Arrays.toString(i.inputValues)).reduce((s1, s2) -> s1 + s2)
}
- public static WireEnd[] extractEnds(Wire[] w)
+ public static ReadEnd[] extractEnds(Wire[] w)
{
- WireEnd[] inputs = new WireEnd[w.length];
+ ReadEnd[] inputs = new ReadEnd[w.length];
for (int i = 0; i < w.length; i++)
inputs[i] = w[i].createEnd();
return inputs;
package era.mi.logic.wires;
import era.mi.logic.types.BitVector;
+import era.mi.logic.wires.Wire.ReadEnd;
public interface WireObserver
{
- public void update(Wire initiator, BitVector oldValues);
+ public void update(ReadEnd initiator, BitVector oldValues);
}