+++ /dev/null
-era.mi/bin
-
+++ /dev/null
-[submodule "SWTHelper"]
- path = SWTHelper
- url = https://github.com/Haspamelodica/SWTHelper.git
+++ /dev/null
-# Rechnerarchitektur GP 2019
-#### Modularer Grafischer Schaltkreis Simulator (für die MI-Maschine)
-
-*Ihr könnt hier gerne nach belieben Dinge ändern, haltet euch nicht zurück^^*
-
-## Anforderungen
-
-### Pflichtfeatures
-1. Emulation der MI-Maschine (siehe Am2900ME)
-2. Visualisierung der MI-Maschine sehr ähnlich dem Bild der VL
-3. sehr gute Website, Dokumentation, Hilfe aus dem Kontextmenü heraus
-4. Hilfreiche Fehlermeldungen, Warnungen bei gefährlichen Operationen
- für uns/Entwickler: gutes Loggen, Absturzberichte
-5. Immer hilfreiche Tooltips zu fast allem.
-
-
-### Sollte möglich sein
-1. Export der Ansicht
-2. Sicht des Assemblerprogrammierers, spezifizieren einer ISA und Nutzung
-3. Farben, Schriftgröße & Co. einstellbar (für Präsentation, Hell/Dunkel, Farbenblind/…)
-4. Entwurf eines Schaltkreises mit einem Editor
-5. Sprache ändern (für nicht Muttersprachler, internat. Potenzial, Erweiterungsmögl.)
-6. Sehr einfache Installation (Eclipse Marketplace)
-7. Editor sollte tooltip Einstellung erlauben
-8. Rückgängig
-
-
-### Wunschfeatures
-1. Debuggen im Sinne von Breakpoints & Co.
-2. interaktives Tutorial in der GUI
-3. Zurückspulen der Simulation / Zeitleiste
-4. Wiederherstellungspunkt (Status)
-
-### Weiterführende Ideen
-1. x86 Modell
-2. ARM Modell
-3. Signalgraphen
-
-### Use Cases
-<details><summary><strong>Alle Use Cases anzeigen</strong></summary><p>
-
-#### 01. Installation
-Das Programm lässt sich entweder über den Eclipse Marketplace einfach installieren (oder als Eclipse Projekt) oder als eigenes Eclipse RPC Projekt direkt herunterladen (ggf. entpacken) und starten.<sup>[A]</sup> Das Programm / Plugin lässt sich auf Windows (7+), MacOS und Linux ausführen; alle verbreitete Hardware nach 2010 mit 64 Bit OS sollte funktionieren.<sup>[B]</sup> Es muss auf den Rechnern in den TUM Rechnerhallen funktionieren.<sup>[C]</sup>
-
-#### 02. Deinstallation
-Das Programm muss sich ohne nennenswerten Aufwand oder Kentnisse vom Benutzer rückstandslos entfernen lassen.<sup>[A]</sup>
-
-#### 03. Programmstart
-Das Programm bzw. Plugin startet schnell (unter 10 sec.; besser unter 5 sec).<sup>[A]</sup> Bei dem ersten Programmstart ist ein Willkommensfenster zu sehen, das auf erklärende Resourcen verweist, den Nutzer grüßt und ggf. auf die (Sprach-)Einstellungen hinweist.<sup>[B]</sup> Es wird bestenfalls auf ein Beispielprojekt verwiesen (kann erstellt werden?) um dem Nutzer zu ermöglichen, sich damit vertraut zu machen.<sup>[C]</sup> Ein schnell Link zum Erstellen eines neuen Projekts/Simulation/... wird angezeigt.<sup>[D]</sup>
-
-#### 04. Programmende
-Sollten bei Beenden des Programms oder Programmabsturz nicht persistierte Daten vorliegen, muss dafür gesorgt werden, dass diese nicht verloren gehen.<sup>[A]</sup> Enweder durch einen Dialog<sup>[B]</sup>, oder durch erzeugte Recovery-Dateien<sup>[C]</sup>. Bei Absturz des Programms wird eine passende und aufschlussreiche Fehlermeldung angezeigt, bestenfalls mit Link zu einer Report-Möglichkeit.<sup>[D]</sup>
-
-#### 05. Einstellungen
-Die Hauptextfarbe und Schriftgröße muss einstellbar sein<sup>[A]</sup>, besser noch Hintergrund- und Markierungsfarben und Schriftstil.<sup>[B]</sup> Die Einstellung der Sprache muss möglich sein.<sup>[C]</sup>
-
-#### 06. Einstieg
-Das Einstiegsprojekt zeigt zu Beginn die passende Ansicht an, die Mikroprogrammierung ist wie bei dem Am2900ME ungehindert möglich.<sup>[A]</sup> Bestenfalls ist dieses beim (erstmaligen) Start direkt geöffnet.<sup>[B]</sup> Wichtige Fenster (v.a. die grafische Darstellung) sind direkt sichtbar, oder zumindest in einem Tab geöffnet (müssen nicht gesucht werden).<sup>[C]</sup>
-
-#### 07. Nutzung der Visualisierung
-Die Visualisierung soll möglichst der Vorlesung entsprechen.<sup>[A]</sup> Sie lässt sich transformieren (z.B. Zoom mit dem Mausrad, Verschieben mit Mausklick oder Mausklick und Alt) und wieder zurücksetzen.<sup>[B]</sup>
-
-#### 08. Hilfestellung
-Mit dem Produkt wird eine offline-Dokumentation mitgeliefert. (bestenfalls DE und EN)<sup>[A]</sup> Mit Rechtsklick lässt sich über ein Kontextmenü der Teil der Dokumentation öffnen, der das angeklickte und seine Funktion beschreibt (Vorbild Cinema4D). Dies sollte mit fast allem möglich sein.<sup>[B]</sup> Bleibt man mit der Maus länger über einem GUI-Bestandteil, wird eine kurze Beschreibung/Hilfe dazu angezeigt (Tooltip).<sup>[C]</sup>
-
-#### 09. Dateihandhabung
-Die Dateien sollten möglichst menschenlesbar sein, z.B. in CSV-Format, um Kommunikation mit anderer Software und Änderungen durch den Nutzer zu ermöglichen.<sup>[A]</sup> Die Dateien enthalten eine Versionsnummer, um Abwärtskompatibilität und hilfreiche Fehlermeldungen zu ermöglichen.<sup>[B]</sup>
-
-#### 10. Entwicklerunterstützung
-Das Projekt soll sich einfach bauen/kompilieren/exportieren lassen, es muss beschrieben werden wie das möglich ist.<sup>[A]</sup> Die Verbreitung zur Anwendung soll ebenfalls unproblematisch sein.<sup>[B]</sup>
-
-#### Fügt hier eure eigenen dazu, fortlaufend nummeriert und ggf. mit <sup>[A,B,C,..]</sup> genauer spezifiziert.
-
-</p></details>
-
-### UML und Ähnliches
-#### Grobe Struktur
-![image](https://user-images.githubusercontent.com/11130248/57186855-aae24600-6ee6-11e9-88b4-371622d43adc.png)
-
-## Technologie-Stack
-* Git (anscheinend über Gitlab)
-* Eclipse RCP
- - (noch zu testen)
-* Java
- - Version 8?
- - Version 10?
- - Version 12?
-* Bibliotheken
- - JUnit 5.4.x ? AssertJ? [http://joel-costigliola.github.io/assertj/index.html](http://joel-costigliola.github.io/assertj/index.html)
- - (Apache Commons?)
- Sehr weit verbreitete und gut designte & getestete Lib;
- aber zusätzliche Dep.
- - Sonstiges? Logging? Log4j
-- Automatischer Build mit Maven? Wohl eher nicht, wegen ERPC Build
-
-## Meilensteine
-
-## Außenwirkung
-### Name, Abkürzung
-### Signet, Logo
-### Beschreibung
-
-## Sonstiges
-### Nützliche Links
-- [https://wiki.eclipse.org/Rich_Client_Platform](https://wiki.eclipse.org/Rich_Client_Platform) -Links
-- [https://wiki.eclipse.org/User_Interface_Guidelines](https://wiki.eclipse.org/User_Interface_Guidelines) - etwas alt, aber nützlich
-- [https://download.eclipse.org/eclipse/downloads/](https://download.eclipse.org/eclipse/downloads/) - Eclipse Project
-- [https://www.vogella.com/tutorials/EclipseRCP/article.html](https://www.vogella.com/tutorials/EclipseRCP/article.html) - **Eclipse RPC Tutorial**
-- [https://github.com/eclipse/gef/wiki](https://github.com/eclipse/gef/wiki) - **Wiki für das Grafikframework**
-- [https://github.com/eclipse/MaisiKoleni/Am2900ME](https://github.com/eclipse/MaisiKoleni/Am2900ME) - der geistige Vorgänger
-- [https://github.com/Haspamelodica/Am2900ME_swt](https://github.com/Haspamelodica/Am2900ME_swt) - ... auch mit SWT
-
-### Rechtliches
-<details><summary>
-MIT Lizenz? - https://choosealicense.com/licenses/mit/
-
-</summary><p>
-
->MIT License
->
->Copyright (c) 2019 Christian Femers, Daniel Kirschten, Fabian Stemmler
->
->
->
->Permission is hereby granted, free of charge, to any person obtaining a copy
->of this software and associated documentation files (the "Software"), to deal
->in the Software without restriction, including without limitation the rights
->to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
->copies of the Software, and to permit persons to whom the Software is
->furnished to do so, subject to the following conditions:
->
->The above copyright notice and this permission notice shall be included in all
->copies or substantial portions of the Software.
->
->THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
->IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
->FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
->AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
->LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
->OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
->SOFTWARE.
-
-</p></details>
-Muss ggf. Eclipse Public License verwendet werden wegen Copyleft?
+++ /dev/null
-Subproject commit 0275db10c78d3c3d98bf197eca5f29e4556ffb90
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="src" path="src"/>
- <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-10">
- <attributes>
- <attribute name="module" value="true"/>
- </attributes>
- </classpathentry>
- <classpathentry kind="output" path="bin"/>
-</classpath>
+++ /dev/null
-*.class
-*.log
-
-# package files
-*.jar
-*.war
-*.nar
-*.ear
-*.zip
-*.tar.gz
-*.rar
-
-# vm crash logs
-hs_err_pid*
\ No newline at end of file
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
- <name>era.mi</name>
- <comment></comment>
- <projects>
- </projects>
- <buildSpec>
- <buildCommand>
- <name>org.eclipse.jdt.core.javabuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- </buildSpec>
- <natures>
- <nature>org.eclipse.jdt.core.javanature</nature>
- </natures>
-</projectDescription>
+++ /dev/null
-eclipse.preferences.version=1
-org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=10
-org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
-org.eclipse.jdt.core.compiler.compliance=10
-org.eclipse.jdt.core.compiler.debug.lineNumber=generate
-org.eclipse.jdt.core.compiler.debug.localVariable=generate
-org.eclipse.jdt.core.compiler.debug.sourceFile=generate
-org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
-org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
-org.eclipse.jdt.core.compiler.release=enabled
-org.eclipse.jdt.core.compiler.source=10
+++ /dev/null
-package era.mi.logic;\r
-\r
-import java.util.Arrays;\r
-\r
-public enum Bit\r
-{\r
- ONE, ZERO, Z, X;\r
-\r
- public static Bit and(Bit a, Bit b)\r
- {\r
- return a.and(b);\r
- }\r
-\r
- public Bit and(Bit other)\r
- {\r
- if (equals(Bit.ZERO) || other.equals(Bit.ZERO))\r
- return Bit.ZERO;\r
- else if (equals(other) && equals(Bit.ONE))\r
- return Bit.ONE;\r
- else\r
- return Bit.X;\r
- }\r
-\r
- public static Bit or(Bit a, Bit b)\r
- {\r
- return a.or(b);\r
- }\r
-\r
- public Bit or(Bit other)\r
- {\r
- if (equals(Bit.ONE) || other.equals(Bit.ONE))\r
- return Bit.ONE;\r
- else if (equals(other) && equals(Bit.ZERO))\r
- return Bit.ZERO;\r
- else\r
- return Bit.X;\r
- }\r
-\r
- public static Bit xor(Bit a, Bit b)\r
- {\r
- return a.xor(b);\r
- }\r
-\r
- public Bit xor(Bit other)\r
- {\r
- if(this == Bit.X || this == Bit.Z\r
- || other == Bit.X || other == Bit.Z)\r
- return Bit.X;\r
- else\r
- return this == other ? Bit.ZERO : Bit.ONE;\r
- }\r
-\r
- public Bit not()\r
- {\r
- switch (this)\r
- {\r
- case ONE:\r
- return Bit.ZERO;\r
- case ZERO:\r
- return Bit.ONE;\r
- default:\r
- return Bit.X;\r
- }\r
- }\r
- \r
- public Bit[] makeArray(int length)\r
- {\r
- Bit[] bits = new Bit[length];\r
- Arrays.fill(bits, this);\r
- return bits;\r
- }\r
-\r
- /**\r
- * Rules for two bits that get directly connected<br>\r
- * <code><table>\r
- * <tbody>\r
- * <tr><td><td>X<td>0<td>1<td>Z</tr>\r
- * <tr><td>X<td>X<td>X<td>X<td>X</tr>\r
- * <tr><td>0<td>X<td>0<td>X<td>0</tr>\r
- * <tr><td>1<td>X<td>X<td>1<td>1</tr>\r
- * <tr><td>Z<td>X<td>0<td>1<td>Z</tr>\r
- * </tbody>\r
- * </table><code>\r
- * \r
- * @return the result according to the table\r
- * \r
- * @author Christian Femers\r
- */\r
- public Bit combineWith(Bit other)\r
- {\r
- if (this == other)\r
- return this;\r
- if (this == X || other == X)\r
- return X;\r
- if (other == Z)\r
- return this;\r
- if (this == Z)\r
- return other;\r
- return X;\r
- }\r
-\r
- public static Bit combine(Bit a, Bit b)\r
- {\r
- return a.combineWith(b);\r
- }\r
-}\r
+++ /dev/null
-package era.mi.logic;
-
-import era.mi.logic.timeline.Timeline;
-
-public class Simulation
-{
- public final static Timeline TIMELINE = new Timeline(11);
-
- public static void main(String[] args)
- {
- }
-}
\ No newline at end of file
+++ /dev/null
-package era.mi.logic;
-
-import java.util.Arrays;
-
-public final class Util
-{
-
- @SuppressWarnings("unchecked")
- public static <T> T[] concat(T[]... arrays)
- {
- if (arrays.length == 0)
- throw new IllegalArgumentException("Cannot concatenate 0 arrays.");
-
- int length = 0;
- for (T[] array : arrays)
- length += array.length;
-
- T[] newArray = Arrays.copyOf(arrays[0], length);
- int appendIndex = arrays[0].length;
- for (int i = 1; i < arrays.length; i++)
- {
- System.arraycopy(arrays[i], 0, newArray, appendIndex, arrays[i].length);
- appendIndex += arrays[i].length;
- }
-
- return newArray;
- }
-
-// @SuppressWarnings("unchecked")
-// public static <T> T[][] split(T[] array, int... lengths)
-// {
-// //TODO: implement array split again; This version contains an illegal cast
-// int totalLength = 0;
-// for(int length : lengths)
-// totalLength += length;
-//
-// if(totalLength != array.length)
-// throw new IllegalArgumentException(); //TODO: add proper error message
-//
-// Object[][] newArray = new Object[lengths.length][];
-// int splitIndex = 0;
-// for(int i = 0; i < lengths.length; i++)
-// {
-// System.arraycopy(array, splitIndex, newArray, 0, lengths[i]);
-// splitIndex += lengths[i];
-// }
-//
-// return (T[][]) newArray;
-// }
-
- public static Bit[] and(Bit[] a, Bit[] b)
- {
- return binBitOp(a, b, (bA, bB) -> Bit.and(bA, bB));
- }
-
- public static Bit[] or(Bit[] a, Bit[] b)
- {
- return binBitOp(a, b, (bA, bB) -> Bit.or(bA, bB));
- }
-
- public static Bit[] xor(Bit[] a, Bit[] b)
- {
- return binBitOp(a, b, (bA, bB) -> Bit.xor(bA, bB));
- }
-
- private static Bit[] binBitOp(Bit[] a, Bit[] b, BitOp op)
- {
- if (a.length != b.length)
- throw new IllegalArgumentException("Bit Arrays were not of equal length.");
- Bit[] out = new Bit[a.length];
- for (int i = 0; i < a.length; i++)
- {
- out[i] = op.execute(a[i], b[i]);
- }
- return out;
- }
-
- public static Bit[] not(Bit[] a)
- {
- Bit[] out = new Bit[a.length];
- for (int i = 0; i < a.length; i++)
- {
- out[i] = a[i].not();
- }
- return out;
- }
-
- /**
- * uses the {@link Bit#combineWith(Bit)} method, does not create a new array,
- * the result is stored in the first array.
- *
- * @author Christian Femers
- */
- public static Bit[] combineInto(Bit[] dest, Bit[] addition)
- {
- if (dest.length != addition.length)
- throw new IllegalArgumentException("Bit Arrays were not of equal length.");
- for (int i = 0; i < addition.length; i++) {
- dest[i] = dest[i].combineWith(addition[i]);
- }
- return dest;
- }
-
- interface BitOp
- {
- Bit execute(Bit a, Bit b);
- }
-}
+++ /dev/null
-package era.mi.logic.components;
-
-import era.mi.logic.Bit;
-import era.mi.logic.Simulation;
-import era.mi.logic.wires.WireArray;
-import era.mi.logic.wires.WireArrayObserver;
-
-/**
- * A basic component that recomputes all outputs (with a delay), when it is updated.
- * @author Fabian Stemmler
- */
-public abstract class BasicComponent implements WireArrayObserver, Component
-{
- private int processTime;
-
- /**
- *
- * @param processTime Amount of time this component takes to update its outputs. Must be more than 0, otherwise 1 is assumed.
- *
- * @author Fabian Stemmler
- */
- public BasicComponent(int processTime)
- {
- this.processTime = processTime > 0 ? processTime : 1;
- }
-
- @Override
- public void update(WireArray initiator, Bit[] oldValues)
- {
- Simulation.TIMELINE.addEvent((e) -> {compute();}, processTime);
- }
-
- protected abstract void compute();
-}
+++ /dev/null
-package era.mi.logic.components;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import era.mi.logic.Bit;\r
-import era.mi.logic.wires.WireArray;\r
-\r
-public class BitDisplay extends BasicComponent\r
-{\r
- private final WireArray in;\r
- private Bit[] displayedValue;\r
-\r
- public BitDisplay(WireArray in)\r
- {\r
- super(1);\r
- this.in = in;\r
- in.addObserver(this);\r
- compute();\r
- }\r
-\r
- @Override\r
- protected void compute()\r
- {\r
- displayedValue = in.getValues();\r
- }\r
-\r
- public Bit[] getDisplayedValue()\r
- {\r
- return displayedValue;\r
- }\r
-\r
- public boolean isDisplaying(Bit... values)\r
- {\r
- return Arrays.equals(displayedValue, values);\r
- }\r
-\r
- @Override\r
- public List<WireArray> getAllInputs()\r
- {\r
- return Collections.unmodifiableList(Arrays.asList(in));\r
- }\r
-\r
- @Override\r
- public List<WireArray> getAllOutputs()\r
- {\r
- return Collections.unmodifiableList(new ArrayList<WireArray>());\r
- }\r
-}\r
+++ /dev/null
-package era.mi.logic.components;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-import era.mi.logic.Bit;
-import era.mi.logic.Simulation;
-import era.mi.logic.timeline.TimelineEvent;
-import era.mi.logic.timeline.TimelineEventHandler;
-import era.mi.logic.wires.WireArray;
-import era.mi.logic.wires.WireArray.WireArrayInput;
-
-public class Clock implements TimelineEventHandler, Component
-{
- private boolean toggle = false;
- private WireArrayInput outI;
- private int delta;
-
- /**
- *
- * @param out {@link WireArray} the clock's impulses are fed into
- * @param delta ticks between rising and falling edge
- */
- public Clock(WireArray out, int delta)
- {
- this.delta = delta;
- this.outI = out.createInput();
- Simulation.TIMELINE.addEvent(this, 50);
- }
-
- @Override
- public void handle(TimelineEvent e)
- {
- addToTimeline();
- outI.feedSignals(new Bit[] { toggle ? Bit.ONE : Bit.ZERO });
- toggle = !toggle;
- }
-
- public WireArray getOut()
- {
- return outI.owner;
- }
-
- private void addToTimeline()
- {
- Simulation.TIMELINE.addEvent(this, delta);
- }
-
- @Override
- public List<WireArray> getAllInputs()
- {
- return Collections.unmodifiableList(Arrays.asList());
- }
-
- @Override
- public List<WireArray> getAllOutputs()
- {
- return Collections.unmodifiableList(Arrays.asList(outI.owner));
- }
-}
+++ /dev/null
-package era.mi.logic.components;\r
-\r
-import java.util.List;\r
-\r
-import era.mi.logic.wires.WireArray;\r
-\r
-public interface Component\r
-{\r
-\r
- /**\r
- * Returns immutable list of all inputs to the {@link Component} (including e.g. the select bits to a MUX).\r
- * Intended for visualization in the UI.\r
- */\r
- public List<WireArray> getAllInputs();\r
- \r
- /**\r
- * Returns immutable list of all outputs to the {@link Component}.\r
- * Intended for visualization in the UI.\r
- */\r
- public List<WireArray> getAllOutputs();\r
-}\r
+++ /dev/null
-package era.mi.logic.components;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-import era.mi.logic.wires.WireArray;
-import era.mi.logic.wires.WireArray.WireArrayInput;
-
-/**
- * Models a multiplexer. Takes an arbitrary amount of input {@link WireArray}s, one of which,
- * as determined by select, is put through to the output.
- * @author Fabian Stemmler
- *
- */
-public class Demux extends BasicComponent
-{
- private final WireArray select, in;
- private final WireArray[] outputs;
- private final WireArrayInput[] outputsI;
- private final int outputSize;
- private int selected = -1;
-
- /**
- * Input {@link WireArray}s and out must be of uniform length
- * @param out Must be of uniform length with all inputs.
- * @param select Indexes the input array which is to be mapped to the output. Must have enough bits
- * to index all inputs.
- * @param outputs One of these inputs is mapped to the output, depending on the select bits
- */
- public Demux(int processTime, WireArray in, WireArray select, WireArray... outputs)
- {
- super(processTime);
- outputSize = in.length;
-
- this.in = in;
- this.outputs = outputs;
- this.outputsI = new WireArrayInput[outputs.length];
- for(int i = 0; i < this.outputsI.length; i++)
- {
- if(outputs[i].length != outputSize)
- throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");
- this.outputsI[i] = outputs[i].createInput();
- }
-
- this.select = select;
- select.addObserver(this);
-
- int maxInputs = 1 << select.length;
- if(this.outputsI.length > maxInputs)
- throw new IllegalArgumentException("There are more outputs ("
- + this.outputsI.length + ") to the DEMUX than supported by "
- + select.length + " select bits (" + maxInputs + ").");
- in.addObserver(this);
- }
-
- @Override
- public void compute() {
- int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
- if(selectValue >= outputsI.length)
- selectValue = -1;
-
- if(selected != selectValue && selected != -1)
- outputsI[selected].clearSignals();
-
- selected = selectValue;
-
- if(selectValue != -1)
- outputsI[selectValue].feedSignals(in.getValues());
- }
-
- @Override
- public List<WireArray> getAllInputs()
- {
- return Collections.unmodifiableList(Arrays.asList(in, select));
- }
-
- @Override
- public List<WireArray> getAllOutputs()
- {
- return Collections.unmodifiableList(Arrays.asList(outputs));
- }
-}
+++ /dev/null
-package era.mi.logic.components;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-import era.mi.logic.Bit;
-import era.mi.logic.wires.WireArray;
-import era.mi.logic.wires.WireArray.WireArrayInput;
-import era.mi.logic.wires.WireArrayObserver;
-
-public class Merger implements WireArrayObserver, Component
-{
- private WireArrayInput outI;
- private WireArray[] inputs;
- private int[] beginningIndex;
-
- /**
- *
- * @param union The output of merging n {@link WireArray}s into one. Must have
- * length = a1.length() + a2.length() + ... + an.length().
- * @param inputs The inputs to be merged into the union
- */
- public Merger(WireArray union, WireArray... inputs)
- {
- this.inputs = inputs;
- this.outI = union.createInput();
- this.beginningIndex = new int[inputs.length];
-
- int length = 0;
- for (int i = 0; i < inputs.length; i++)
- {
- beginningIndex[i] = length;
- length += inputs[i].length;
- inputs[i].addObserver(this);
- }
-
- if (length != union.length)
- throw new IllegalArgumentException(
- "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length().");
- }
-
- public WireArray getInput(int index)
- {
- return inputs[index];
- }
-
- public WireArray getUnion()
- {
- return outI.owner;
- }
-
- @Override
- public void update(WireArray initiator, Bit[] oldValues)
- {
- int index = find(initiator);
- int beginning = beginningIndex[index];
- outI.feedSignals(beginning, initiator.getValues());
- }
-
- private int find(WireArray w)
- {
- for (int i = 0; i < inputs.length; i++)
- if (inputs[i] == w)
- return i;
- return -1;
- }
-
- public WireArray[] getInputs()
- {
- return inputs.clone();
- }
-
- @Override
- public List<WireArray> getAllInputs()
- {
- return Collections.unmodifiableList(Arrays.asList(inputs));
- }
-
- @Override
- public List<WireArray> getAllOutputs()
- {
- return Collections.unmodifiableList(Arrays.asList(outI.owner));
- }
-}
+++ /dev/null
-package era.mi.logic.components;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-import era.mi.logic.wires.WireArray;
-import era.mi.logic.wires.WireArray.WireArrayInput;
-
-/**
- * Models a multiplexer. Takes an arbitrary amount of input {@link WireArray}s, one of which,
- * as determined by select, is put through to the output.
- * @author Fabian Stemmler
- *
- */
-public class Mux extends BasicComponent
-{
- private WireArray select;
- private WireArrayInput outI;
- private WireArray[] inputs;
- private final int outputSize;
- /**
- * Input {@link WireArray}s and out must be of uniform length
- * @param out Must be of uniform length with all inputs.
- * @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, WireArray out, WireArray select, WireArray... inputs)
- {
- super(processTime);
- outputSize = out.length;
-
- this.inputs = inputs.clone();
- for(int i = 0; i < this.inputs.length; i++)
- {
- if(inputs[i].length != outputSize)
- throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!");
- inputs[i].addObserver(this);
- }
-
- this.select = select;
- select.addObserver(this);
-
- int maxInputs = 1 << select.length;
- if(this.inputs.length > maxInputs)
- throw new IllegalArgumentException("There are more inputs ("
- + this.inputs.length + ") to the MUX than supported by "
- + select.length + " select bits (" + maxInputs + ").");
-
- outI = out.createInput();
- }
-
- public WireArray getOut()
- {
- return outI.owner;
- }
-
- public WireArray getSelect()
- {
- return select;
- }
-
- @Override
- public void compute() {
- int selectValue;
- if(!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length)
- {
- outI.clearSignals();
- return;
- }
-
- WireArray active = inputs[selectValue];
- outI.feedSignals(active.getValues());
- }
-
- @Override
- public List<WireArray> getAllInputs()
- {
- ArrayList<WireArray> wires = new ArrayList<WireArray>(Arrays.asList(inputs));
- wires.add(select);
- return Collections.unmodifiableList(wires);
- }
-
- @Override
- public List<WireArray> getAllOutputs()
- {
- return Collections.unmodifiableList(Arrays.asList(outI.owner));
- }
-}
+++ /dev/null
-package era.mi.logic.components;
-
-import era.mi.logic.Bit;
-import era.mi.logic.wires.WireArray;
-import era.mi.logic.wires.WireArray.WireArrayInput;
-import era.mi.logic.wires.WireArrayObserver;
-
-public class Splitter implements WireArrayObserver
-{
- private WireArray input;
- private WireArrayInput[] outputs;
-
- public Splitter(WireArray input, WireArray... outputs)
- {
- this.input = input;
- this.outputs = WireArray.extractInputs(outputs);
- input.addObserver(this);
- int length = 0;
- for(WireArray out : outputs)
- length += out.length;
-
- if(input.length != length)
- throw new IllegalArgumentException("The input of splitting one into n WireArrays must have length = a1.length() + a2.length() + ... + an.length().");
- }
-
- protected void compute()
- {
- int startIndex = 0;
- Bit[] inputBits = input.getValues();
- for(int i = 0; i < outputs.length; i++)
- {
- Bit[] outputBits = new Bit[outputs[i].owner.length];
- System.arraycopy(inputBits, startIndex, outputBits, 0, outputs[i].owner.length);
- outputs[i].feedSignals(outputBits);
- startIndex += outputs[i].owner.length;
- }
- }
-
- @Override
- public void update(WireArray initiator, Bit[] oldValues)
- {
- compute();
- }
-}
+++ /dev/null
-package era.mi.logic.components;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-import era.mi.logic.Bit;
-import era.mi.logic.wires.WireArray;
-import era.mi.logic.wires.WireArray.WireArrayInput;
-
-public class TriStateBuffer extends BasicComponent{
- WireArray in, enable;
- WireArrayInput outI;
-
- public TriStateBuffer(int processTime, WireArray in, WireArray out, WireArray enable) {
- super(processTime);
- if(in.length != out.length)
- throw new IllegalArgumentException("Tri-state output must have the same amount of bits as the input. Input: " + in.length + " Output: " + out.length);
- if(enable.length != 1)
- throw new IllegalArgumentException("Tri-state enable must have exactly one bit, not " + enable.length + ".");
- this.in = in;
- in.addObserver(this);
- this.enable = enable;
- enable.addObserver(this);
- outI = out.createInput();
- }
-
- @Override
- protected void compute()
- {
- if(enable.getValue() == Bit.ONE)
- outI.feedSignals(in.getValues());
- else
- outI.clearSignals();
- }
-
- @Override
- public List<WireArray> getAllInputs()
- {
- return Collections.unmodifiableList(Arrays.asList(in, enable));
- }
-
- @Override
- public List<WireArray> getAllOutputs()
- {
- return Collections.unmodifiableList(Arrays.asList(outI.owner));
- }
-
-}
+++ /dev/null
-package era.mi.logic.components.gates;
-
-import era.mi.logic.Util;
-import era.mi.logic.wires.WireArray;
-
-public class AndGate extends MultiInputGate
-{
- public AndGate(int processTime, WireArray out, WireArray... in)
- {
- super(processTime, Util::and, out, in);
- }
-}
+++ /dev/null
-package era.mi.logic.components.gates;\r
-\r
-import java.util.Arrays;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import era.mi.logic.Bit;\r
-import era.mi.logic.components.BasicComponent;\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayInput;\r
-\r
-public abstract class MultiInputGate extends BasicComponent\r
-{\r
- protected WireArray[] in;\r
- protected WireArray out;\r
- protected WireArrayInput outI;\r
- protected final int length;\r
- protected Operation op;\r
- \r
- protected MultiInputGate(int processTime, Operation op, WireArray out, WireArray... in)\r
- {\r
- super(processTime);\r
- this.op = op;\r
- length = out.length;\r
- this.in = in.clone();\r
- if(in.length < 1)\r
- throw new IllegalArgumentException(String.format("Cannot create gate with %d wires.", in.length));\r
- for(WireArray w : in)\r
- {\r
- if(w.length != length)\r
- throw new IllegalArgumentException("All wires connected to the gate must be of uniform length.");\r
- w.addObserver(this);\r
- }\r
- this.out = out;\r
- outI = out.createInput();\r
- }\r
-\r
-\r
- @Override\r
- public List<WireArray> getAllInputs()\r
- {\r
- return Collections.unmodifiableList(Arrays.asList(in));\r
- }\r
-\r
- @Override\r
- public List<WireArray> getAllOutputs()\r
- {\r
- return Collections.unmodifiableList(Arrays.asList(out));\r
- }\r
- \r
- protected void compute()\r
- {\r
- Bit[] result = in[0].getValues();\r
- for(int i = 1; i < in.length; i++)\r
- result = op.execute(result, in[i].getValues());\r
- outI.feedSignals(result);\r
- }\r
- \r
- protected interface Operation\r
- {\r
- public Bit[] execute(Bit[] a, Bit[] b);\r
- }\r
-}\r
+++ /dev/null
-package era.mi.logic.components.gates;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-import era.mi.logic.Util;
-import era.mi.logic.components.BasicComponent;
-import era.mi.logic.wires.WireArray;
-import era.mi.logic.wires.WireArray.WireArrayInput;
-
-public class NotGate extends BasicComponent
-{
- private WireArray in, out;
- private WireArrayInput outI;
-
-
- public NotGate(int processTime, WireArray in, WireArray out)
- {
- super(processTime);
- this.in = in;
- in.addObserver(this);
- this.out = out;
- outI = out.createInput();
- }
-
- public void compute()
- {
- outI.feedSignals(Util.not(in.getValues()));
- }
-
- public WireArray getIn()
- {
- return in;
- }
-
- public WireArray getOut()
- {
- return out;
- }
-
- @Override
- public List<WireArray> getAllInputs()
- {
- return Collections.unmodifiableList(Arrays.asList(in));
- }
-
- @Override
- public List<WireArray> getAllOutputs()
- {
- return Collections.unmodifiableList(Arrays.asList(out));
- }
-}
+++ /dev/null
-package era.mi.logic.components.gates;
-
-import era.mi.logic.Util;
-import era.mi.logic.wires.WireArray;
-
-public class OrGate extends MultiInputGate
-{
- public OrGate(int processTime, WireArray out, WireArray... in)
- {
- super(processTime, Util::or, out, in);
- }
-}
+++ /dev/null
-package era.mi.logic.components.gates;
-
-import era.mi.logic.Util;
-import era.mi.logic.wires.WireArray;
-
-/**
- * Outputs 1 when the number of 1 inputs is odd.
- * @author Fabian Stemmler
- */
-public class XorGate extends MultiInputGate
-{
- public XorGate(int processTime, WireArray out, WireArray... in)
- {
- super(processTime, Util::xor, out, in);
- }
-
-}
+++ /dev/null
-package era.mi.logic.tests;\r
-\r
-import static org.junit.jupiter.api.Assertions.*;\r
-\r
-import java.util.Arrays;\r
-import java.util.function.LongConsumer;\r
-\r
-import org.junit.jupiter.api.Test;\r
-\r
-import era.mi.logic.Bit;\r
-import era.mi.logic.Simulation;\r
-import era.mi.logic.components.Demux;\r
-import era.mi.logic.components.Merger;\r
-import era.mi.logic.components.Mux;\r
-import era.mi.logic.components.Splitter;\r
-import era.mi.logic.components.TriStateBuffer;\r
-import era.mi.logic.components.gates.AndGate;\r
-import era.mi.logic.components.gates.NotGate;\r
-import era.mi.logic.components.gates.OrGate;\r
-import era.mi.logic.components.gates.XorGate;\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayInput;\r
-\r
-class ComponentTest\r
-{\r
- \r
- @Test\r
- void circuitExampleTest()\r
- {\r
- Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), c = new WireArray(1, 10), d = new WireArray(2, 1), e = new WireArray(1, 1),\r
- f = new WireArray(1, 1), g = new WireArray(1, 1), h = new WireArray(2, 1), i = new WireArray(2, 1), j = new WireArray(1, 1), k = new WireArray(1, 1);\r
- new AndGate(1, f, a, b);\r
- new NotGate(1, f, g);\r
- new Merger(h, c, g);\r
- new Mux(1, i, e, h, d);\r
- new Splitter(i, k, j);\r
- \r
- a.createInput().feedSignals(Bit.ZERO);\r
- b.createInput().feedSignals(Bit.ONE);\r
- c.createInput().feedSignals(Bit.ZERO);\r
- d.createInput().feedSignals(Bit.ONE, Bit.ONE);\r
- e.createInput().feedSignals(Bit.ZERO);\r
- \r
- Simulation.TIMELINE.executeAll();\r
- \r
- assertEquals(Bit.ONE, j.getValue());\r
- assertEquals(Bit.ZERO, k.getValue());\r
- }\r
-\r
- @Test\r
- void splitterTest()\r
- {\r
- Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), in = new WireArray(8, 1);\r
- in.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
- new Splitter(in, a, b, c);\r
-\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);\r
- assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO);\r
- assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);\r
- }\r
-\r
- @Test\r
- void mergerTest()\r
- {\r
- Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);\r
- a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);\r
- b.createInput().feedSignals(Bit.ONE, Bit.ZERO);\r
- c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);\r
-\r
- new Merger(out, a, b, c);\r
-\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertTrue(Arrays.equals(out.getValues(),\r
- new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));\r
- }\r
-\r
- @Test\r
- void triStateBufferTest()\r
- {\r
- WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), en = new WireArray(1, 1),\r
- notEn = new WireArray(1, 1);\r
- new NotGate(1, en, notEn);\r
- new TriStateBuffer(1, a, b, en);\r
- new TriStateBuffer(1, b, a, notEn);\r
-\r
- WireArrayInput enI = en.createInput(), aI = a.createInput(), bI = b.createInput();\r
- enI.feedSignals(Bit.ONE);\r
- aI.feedSignals(Bit.ONE);\r
-\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertEquals(Bit.ONE, b.getValue());\r
-\r
- bI.feedSignals(Bit.ZERO);\r
-\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertEquals(Bit.X, b.getValue());\r
- assertEquals(Bit.ONE, a.getValue());\r
-\r
- aI.clearSignals();\r
- enI.feedSignals(Bit.ZERO);\r
-\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertEquals(Bit.ZERO, a.getValue());\r
-\r
- }\r
-\r
- @Test\r
- void muxTest()\r
- {\r
- Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4),\r
- select = new WireArray(2, 5), out = new WireArray(4, 1);\r
- WireArrayInput selectIn = select.createInput();\r
-\r
- selectIn.feedSignals(Bit.ZERO, Bit.ZERO);\r
- a.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
- c.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
-\r
- new Mux(1, out, select, a, b, c);\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
- selectIn.feedSignals(Bit.ZERO, Bit.ONE);\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
-\r
- selectIn.feedSignals(Bit.ONE, Bit.ONE);\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
-\r
- }\r
-\r
- @Test\r
- void demuxTest()\r
- {\r
- Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4),\r
- select = new WireArray(2, 5), in = new WireArray(4, 1);\r
- WireArrayInput selectIn = select.createInput();\r
-\r
- selectIn.feedSignals(Bit.ZERO, Bit.ZERO);\r
- in.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
-\r
- new Demux(1, in, select, a, b, c);\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
- assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
- assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
- selectIn.feedSignals(Bit.ZERO, Bit.ONE);\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
- assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
- assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
-\r
- selectIn.feedSignals(Bit.ONE, Bit.ONE);\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
- assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
- assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);\r
-\r
- }\r
- \r
- @Test\r
- void andTest()\r
- {\r
- Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);\r
- new AndGate(1, c, a, b);\r
- a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);\r
- b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
-\r
- Simulation.TIMELINE.executeAll();\r
- \r
- assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);\r
- }\r
-\r
- @Test\r
- void orTest()\r
- {\r
- Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);\r
- new OrGate(1, c, a, b);\r
- a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);\r
- b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
-\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);\r
- }\r
- \r
- @Test\r
- void xorTest()\r
- {\r
- Simulation.TIMELINE.reset();\r
- WireArray a = new WireArray(3, 1), b = new WireArray(3, 2), c = new WireArray(3, 1), d = new WireArray(3, 1);\r
- new XorGate(1, d, a, b, c);\r
- a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);\r
- b.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);\r
- c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);\r
-\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);\r
- }\r
-\r
- @Test\r
- void rsLatchCircuitTest()\r
- {\r
- Simulation.TIMELINE.reset();\r
- WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),\r
- q = new WireArray(1, 1), nq = new WireArray(1, 1);\r
-\r
- new OrGate(1, t2, r, nq);\r
- new OrGate(1, t1, s, q);\r
- new NotGate(1, t2, q);\r
- new NotGate(1, t1, nq);\r
-\r
- WireArrayInput sIn = s.createInput(), rIn = r.createInput();\r
-\r
- sIn.feedSignals(Bit.ONE);\r
- rIn.feedSignals(Bit.ZERO);\r
-\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertEquals(Bit.ONE, q.getValue());\r
- assertEquals(Bit.ZERO, nq.getValue());\r
-\r
- sIn.feedSignals(Bit.ZERO);\r
-\r
- Simulation.TIMELINE.executeAll();\r
- assertEquals(Bit.ONE, q.getValue());\r
- assertEquals(Bit.ZERO, nq.getValue());\r
-\r
- rIn.feedSignals(Bit.ONE);\r
-\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertEquals(Bit.ZERO, q.getValue());\r
- assertEquals(Bit.ONE, nq.getValue());\r
- }\r
-\r
- @Test\r
- void numericValueTest()\r
- {\r
- Simulation.TIMELINE.reset();\r
-\r
- WireArray a = new WireArray(4, 1);\r
- a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);\r
-\r
- Simulation.TIMELINE.executeAll();\r
-\r
- assertEquals(15, a.getUnsignedValue());\r
- assertEquals(-1, a.getSignedValue());\r
- }\r
-\r
- @Test\r
- void multipleInputs()\r
- {\r
- Simulation.TIMELINE.reset();\r
- WireArray w = new WireArray(2, 1);\r
- WireArrayInput wI1 = w.createInput(), wI2 = w.createInput();\r
- wI1.feedSignals(Bit.ONE, Bit.Z);\r
- wI2.feedSignals(Bit.Z, Bit.X);\r
- Simulation.TIMELINE.executeAll();\r
- assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);\r
-\r
- wI2.feedSignals(Bit.ZERO, Bit.Z);\r
- Simulation.TIMELINE.executeAll();\r
- assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);\r
-\r
- wI2.feedSignals(Bit.Z, Bit.Z);\r
- Simulation.TIMELINE.executeAll();\r
- assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);\r
-\r
- wI2.feedSignals(Bit.ONE, Bit.Z);\r
- w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));\r
- Simulation.TIMELINE.executeAll();\r
- assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);\r
- }\r
-\r
- @Test\r
- void wireConnections()\r
- {\r
- // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde\r
- \r
- Simulation.TIMELINE.reset();\r
-\r
- WireArray a = new WireArray(1, 2);\r
- WireArray b = new WireArray(1, 2);\r
- WireArray c = new WireArray(1, 2);\r
- WireArrayInput aI = a.createInput();\r
- WireArrayInput bI = b.createInput();\r
- WireArrayInput cI = c.createInput();\r
-\r
- TestBitDisplay test = new TestBitDisplay(c);\r
- TestBitDisplay test2 = new TestBitDisplay(a);\r
- LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);\r
-\r
- cI.feedSignals(Bit.ONE);\r
- test.assertAfterSimulationIs(print, Bit.ONE);\r
-\r
- cI.feedSignals(Bit.X);\r
- test.assertAfterSimulationIs(print, Bit.X);\r
-\r
- cI.feedSignals(Bit.X);\r
- cI.feedSignals(Bit.Z);\r
- test.assertAfterSimulationIs(print, Bit.Z);\r
-\r
- new Connector(b, c);\r
- test.assertAfterSimulationIs(print, Bit.Z);\r
- System.err.println("ONE");\r
- bI.feedSignals(Bit.ONE);\r
- test.assertAfterSimulationIs(print, Bit.ONE);\r
- System.err.println("ZERO");\r
- bI.feedSignals(Bit.ZERO);\r
- test.assertAfterSimulationIs(print, Bit.ZERO);\r
- System.err.println("Z");\r
- bI.feedSignals(Bit.Z);\r
- test.assertAfterSimulationIs(print, Bit.Z);\r
- \r
- new Connector(a, b);\r
- System.err.println("Z 2");\r
- aI.feedSignals(Bit.Z);\r
- test.assertAfterSimulationIs(print, Bit.Z);\r
- test2.assertAfterSimulationIs(Bit.Z);\r
- System.err.println("ONE 2");\r
- aI.feedSignals(Bit.ONE);\r
- test.assertAfterSimulationIs(print, Bit.ONE);\r
- test2.assertAfterSimulationIs(Bit.ONE);\r
- System.err.println("ZERO 2");\r
- aI.feedSignals(Bit.ZERO);\r
- test.assertAfterSimulationIs(print, Bit.ZERO);\r
- test2.assertAfterSimulationIs(Bit.ZERO);\r
- System.err.println("Z 2 II");\r
- aI.feedSignals(Bit.Z);\r
- test.assertAfterSimulationIs(print, Bit.Z);\r
- test2.assertAfterSimulationIs(Bit.Z);\r
- \r
- System.err.println("No Conflict yet");\r
- bI.feedSignals(Bit.ONE);\r
- test.assertAfterSimulationIs(print, Bit.ONE);\r
- test2.assertAfterSimulationIs(Bit.ONE);\r
- aI.feedSignals(Bit.ONE);\r
- test.assertAfterSimulationIs(print, Bit.ONE);\r
- test2.assertAfterSimulationIs(Bit.ONE);\r
- System.err.println("Conflict");\r
- aI.feedSignals(Bit.ZERO);\r
- test.assertAfterSimulationIs(print, Bit.X);\r
- test2.assertAfterSimulationIs(Bit.X);\r
- aI.feedSignals(Bit.ONE);\r
- test.assertAfterSimulationIs(print, Bit.ONE);\r
- test2.assertAfterSimulationIs(Bit.ONE);\r
- }\r
-\r
- private static void assertBitArrayEquals(Bit[] actual, Bit... expected)\r
- {\r
- assertArrayEquals(expected, actual);\r
- }\r
-}\r
+++ /dev/null
-package era.mi.logic.tests;\r
-\r
-import era.mi.logic.Bit;\r
-import era.mi.logic.Simulation;\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayInput;\r
-import era.mi.logic.wires.WireArrayObserver;\r
-\r
-public class Connector implements WireArrayObserver\r
-{\r
- private final WireArray a;\r
-// private final WireArray b;\r
- private final WireArrayInput aI;\r
- private final WireArrayInput bI;\r
-\r
- public Connector(WireArray a, WireArray b)\r
- {\r
- if (a.length != b.length)\r
- throw new IllegalArgumentException(String.format("WireArray width does not match: %d, %d", a.length, b.length));\r
- this.a = a;\r
-// this.b = b;\r
- a.addObserver(this);\r
- b.addObserver(this);\r
- aI = a.createInput();\r
- bI = b.createInput();\r
- }\r
-\r
- @Override\r
- public void update(WireArray initiator, Bit[] oldValues)\r
- {\r
- Simulation.TIMELINE.addEvent((e) ->\r
- {\r
- if (initiator == a)\r
- bI.feedSignals(aI.wireValuesExcludingMe());\r
- else\r
- aI.feedSignals(bI.wireValuesExcludingMe());\r
- }, 1);\r
- }\r
-}\r
+++ /dev/null
-package era.mi.logic.tests;\r
-\r
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;\r
-\r
-import java.util.Arrays;\r
-import java.util.function.LongConsumer;\r
-\r
-import era.mi.logic.Bit;\r
-import era.mi.logic.Simulation;\r
-import era.mi.logic.components.BitDisplay;\r
-import era.mi.logic.wires.WireArray;\r
-\r
-public final class TestBitDisplay extends BitDisplay\r
-{\r
-\r
- public TestBitDisplay(WireArray in)\r
- {\r
- super(in);\r
- }\r
-\r
- public void assertDisplays(Bit... expected)\r
- {\r
- assertArrayEquals(expected, getDisplayedValue());\r
- }\r
-\r
- public void assertAfterSimulationIs(Bit... expected)\r
- {\r
- Simulation.TIMELINE.executeAll();\r
- assertDisplays(expected);\r
- }\r
-\r
- public void assertAfterSimulationIs(LongConsumer r, Bit... expected)\r
- {\r
- while (Simulation.TIMELINE.hasNext())\r
- {\r
- Simulation.TIMELINE.executeNext();\r
- r.accept(Simulation.TIMELINE.getSimulationTime());\r
- }\r
- assertDisplays(expected);\r
- }\r
-\r
- @Override\r
- protected void compute()\r
- {\r
- super.compute();\r
- System.out.println("update: value is " + Arrays.toString(getDisplayedValue()));\r
- }\r
-}\r
+++ /dev/null
-package era.mi.logic.timeline;\r
-\r
-import java.util.PriorityQueue;\r
-\r
-/**\r
- * Orders Events by the time they are due to be executed. Can execute Events individually.\r
- * @author Fabian Stemmler\r
- *\r
- */\r
-public class Timeline\r
-{\r
- private PriorityQueue<InnerEvent> events;\r
- private long currentTime = 0;\r
- \r
- public Timeline(int initCapacity)\r
- {\r
- events = new PriorityQueue<InnerEvent>(initCapacity, (a, b) -> {\r
- long difference = a.getTiming() - b.getTiming();\r
- if(difference == 0)\r
- return 0;\r
- return difference < 0 ? -1 : 1;\r
- });\r
- }\r
- \r
- public boolean hasNext()\r
- {\r
- return !events.isEmpty();\r
- }\r
-\r
- public void executeNext()\r
- {\r
- InnerEvent first = events.poll();\r
- currentTime = first.getTiming();\r
- first.run();\r
- }\r
- \r
- public void executeAll()\r
- {\r
- while (hasNext())\r
- executeNext();\r
- }\r
-\r
- public long getSimulationTime()\r
- {\r
- return currentTime;\r
- }\r
- \r
- public void reset()\r
- {\r
- events.clear();\r
- currentTime = 0;\r
- }\r
- \r
- /**\r
- * Adds an Event to the {@link Timeline}\r
- * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline.\r
- * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time.\r
- */\r
- public void addEvent(TimelineEventHandler function, int relativeTiming)\r
- {\r
- long timing = currentTime + relativeTiming;\r
- events.add(new InnerEvent(function, new TimelineEvent(timing), timing));\r
- }\r
- \r
- private class InnerEvent\r
- {\r
-\r
- private final long timing;\r
- private final TimelineEventHandler function;\r
- private final TimelineEvent event;\r
- \r
- /**\r
- * Creates an {@link InnerEvent}\r
- * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs\r
- * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed;\r
- */\r
- InnerEvent(TimelineEventHandler function, TimelineEvent event, long timing)\r
- {\r
- this.function = function;\r
- this.event = event;\r
- this.timing = timing;\r
- }\r
-\r
- public long getTiming()\r
- {\r
- return timing;\r
- }\r
- \r
- public void run()\r
- {\r
- function.handle(event);\r
- }\r
- \r
- @Override\r
- public String toString()\r
- {\r
- return event.toString();\r
- }\r
- }\r
- \r
- @Override\r
- public String toString()\r
- {\r
- return "simulation time: " + currentTime + ", " + events.toString();\r
- }\r
- \r
- public static long toNanoseconds(long ticks)\r
- {\r
- return ticks; //TODO: Alter this when it has been determined how ticks should relate to real time.\r
- }\r
-}
\ No newline at end of file
+++ /dev/null
-package era.mi.logic.timeline;\r
-\r
-/**\r
- * A class that stores all relevant information about an event in the {@link Timeline}. Currently, there is not much relevant information to store.\r
- * @author Fabian Stemmler\r
- *\r
- */\r
-public class TimelineEvent\r
-{\r
- private final long timing;\r
- \r
- TimelineEvent(long timing)\r
- {\r
- super();\r
- this.timing = timing;\r
- }\r
-\r
- public long getTiming()\r
- {\r
- return timing;\r
- }\r
- \r
- public String toString()\r
- {\r
- return "timestamp: " + timing;\r
- }\r
-}
\ No newline at end of file
+++ /dev/null
-package era.mi.logic.timeline;
-
-public interface TimelineEventHandler
-{
- public void handle(TimelineEvent e);
-}
\ No newline at end of file
+++ /dev/null
-package era.mi.logic.wires;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-\r
-import era.mi.logic.Bit;\r
-import era.mi.logic.Simulation;\r
-import era.mi.logic.Util;\r
-\r
-/**\r
- * Represents an array of wires that can store n bits of information.\r
- * \r
- * @author Fabian Stemmler\r
- *\r
- */\r
-public class WireArray\r
-{\r
- private Bit[] values;\r
- public final int travelTime;\r
- private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();\r
- public final int length;\r
- private List<WireArrayInput> inputs = new ArrayList<WireArrayInput>();\r
-\r
- public WireArray(int length, int travelTime)\r
- {\r
- if (length < 1)\r
- throw new IllegalArgumentException(String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));\r
- this.length = length;\r
- this.travelTime = travelTime;\r
- initValues();\r
- }\r
-\r
- private void initValues()\r
- {\r
- values = Bit.Z.makeArray(length);\r
- }\r
-\r
- private void recalculateSingleInput()\r
- {\r
- WireArrayInput input = inputs.get(0);\r
- if (!Arrays.equals(input.getValues(), values))\r
- {\r
- Bit[] oldValues = values.clone();\r
- System.arraycopy(input.getValues(), 0, values, 0, length);\r
- notifyObservers(oldValues);\r
- }\r
- }\r
-\r
- private void recalculateMultipleInputs()\r
- {\r
- Iterator<WireArrayInput> it = inputs.iterator();\r
- Bit[] newValues = it.next().inputValues.clone();\r
-\r
- while (it.hasNext())\r
- {\r
- WireArrayInput input = it.next();\r
- Bit[] bits = input.getValues();\r
- for (int i = 0; i < length; i++)\r
- {\r
- if (Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i]))\r
- continue;\r
- else if (Bit.Z.equals(newValues[i]))\r
- newValues[i] = bits[i];\r
- else\r
- newValues[i] = Bit.X;\r
- }\r
- }\r
-\r
- if (!Arrays.equals(newValues, values))\r
- {\r
- Bit[] oldValues = values;\r
- values = newValues;\r
- notifyObservers(oldValues);\r
- }\r
- }\r
-\r
- private void recalculate()\r
- {\r
- switch (inputs.size())\r
- {\r
- case 0:\r
- return;\r
- case 1:\r
- recalculateSingleInput();\r
- break;\r
- default:\r
- recalculateMultipleInputs();\r
- }\r
- }\r
-\r
- /**\r
- * The WireArray is interpreted as an unsigned integer with n bits.\r
- * \r
- * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or\r
- * <code>Bit.ZERO</code> (they do not all have to have the same value),\r
- * not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is\r
- * returned otherwise.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public boolean hasNumericValue()\r
- {\r
- for (Bit b : values)\r
- {\r
- if (b != Bit.ZERO && b != Bit.ONE)\r
- return false;\r
- }\r
- return true;\r
- }\r
-\r
- /**\r
- * The WireArray is interpreted as an unsigned integer with n bits.\r
- * \r
- * @return The unsigned value of the {@link WireArray}'s bits, where value 0\r
- * corresponds with 2^0, value 1 is 2^1 and so on.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public long getUnsignedValue()\r
- {\r
- long val = 0;\r
- long mask = 1;\r
- for (int i = 0; i < length; i++)\r
- {\r
- switch (values[i])\r
- {\r
- default:\r
- case Z:\r
- case X:\r
- return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;\r
- // Random number?\r
- case ONE:\r
- val |= mask;\r
- break;\r
- case ZERO:\r
- }\r
- mask = mask << 1;\r
- }\r
- return val;\r
- }\r
-\r
- /**\r
- * The WireArray is interpreted as a signed integer with n bits.\r
- * \r
- * @return The signed value of the {@link WireArray}'s bits, where value 0\r
- * corresponds with 2^0, value 1 is 2^1 and so on.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public long getSignedValue()\r
- {\r
- long val = getUnsignedValue();\r
- long mask = 1 << (length - 1);\r
- if ((mask & val) != 0)\r
- {\r
- int shifts = 64 - length;\r
- return (val << shifts) >> shifts;\r
- }\r
- return val;\r
- }\r
-\r
- /**\r
- * Included for convenient use on {@link WireArray}s of length 1.\r
- * \r
- * @return The value of bit 0.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public Bit getValue()\r
- {\r
- return getValue(0);\r
- }\r
-\r
- /**\r
- * \r
- * @param index Index of the requested bit.\r
- * @return The value of the indexed bit.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public Bit getValue(int index)\r
- {\r
- return values[index];\r
- }\r
-\r
- public Bit[] getValues(int start, int end)\r
- {\r
- int length = end - start;\r
- Bit[] bits = new Bit[length];\r
- System.arraycopy(values, start, bits, 0, length);\r
- return bits;\r
- }\r
-\r
- /**\r
- * @return An array of length n containing the values of the n bits in the\r
- * {@link WireArray}. Can be safely modified.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public Bit[] getValues()\r
- {\r
- return values.clone();\r
- }\r
-\r
- /**\r
- * Adds an {@link WireArrayObserver}, who will be notified when the value of the\r
- * {@link WireArray} is updated.\r
- * \r
- * @param ob The {@link WireArrayObserver} to be notified of changes.\r
- * @return true if the given {@link WireArrayObserver} was not already\r
- * registered, false otherwise\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public boolean addObserver(WireArrayObserver ob)\r
- {\r
- return observers.add(ob);\r
- }\r
-\r
- private void notifyObservers(Bit[] oldValues)\r
- {\r
- for (WireArrayObserver o : observers)\r
- o.update(this, oldValues);\r
- }\r
-\r
- /**\r
- * Create and register a {@link WireArrayInput} object, which is tied to this\r
- * {@link WireArray}.\r
- */\r
- public WireArrayInput createInput()\r
- {\r
- return new WireArrayInput(this);\r
- }\r
-\r
- private void registerInput(WireArrayInput toRegister)\r
- {\r
- inputs.add(toRegister);\r
- }\r
-\r
- /**\r
- * A {@link WireArrayInput} feeds a constant signal into the {@link WireArray}\r
- * it is tied to. The combination of all inputs determines the\r
- * {@link WireArray}s final value. X dominates all other inputs Z does not\r
- * affect the final value, unless there are no other inputs than Z 0 and 1 turn\r
- * into X when they are mixed\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public class WireArrayInput\r
- {\r
- public final WireArray owner;\r
- private Bit[] inputValues;\r
-\r
- private WireArrayInput(WireArray owner)\r
- {\r
- super();\r
- this.owner = owner;\r
- initValues();\r
- owner.registerInput(this);\r
- }\r
-\r
- private void initValues()\r
- {\r
- inputValues = Bit.Z.makeArray(length);\r
- }\r
-\r
- /**\r
- * Sets the wires values. This takes up time, as specified by the\r
- * {@link WireArray}s travel time.\r
- * \r
- * @param newValues The new values the wires should take on.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public void feedSignals(Bit... newValues)\r
- {\r
- if (newValues.length == length)\r
- {\r
- feedSignals(0, newValues);\r
- } else\r
- throw new IllegalArgumentException(String.format("Attempted to input %o bits instead of %o bits.", newValues.length, length));\r
- }\r
-\r
- /**\r
- * Sets values of a subarray of wires. This takes up time, as specified by the\r
- * {@link WireArray}s travel time.\r
- * \r
- * @param newValues The new values the wires should take on.\r
- * @param startingBit The first index of the subarray of wires.\r
- * \r
- * @author Fabian Stemmler\r
- */\r
- public void feedSignals(int startingBit, Bit... newValues)\r
- {\r
- Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);\r
- }\r
-\r
- private void setValues(int startingBit, Bit... newValues)\r
- {\r
- int exclLastIndex = startingBit + newValues.length;\r
- if (length < exclLastIndex)\r
- throw new ArrayIndexOutOfBoundsException(String.format("Attempted to input bits from index %o to %o when there are only %o wires.", startingBit, exclLastIndex - 1, length));\r
- if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length))\r
- {\r
- System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);\r
- owner.recalculate();\r
- }\r
- }\r
-\r
- /**\r
- * Returns a copy (safe to modify) of the values the {@link WireArrayInput} is currently feeding into the associated {@link WireArray}.\r
- */\r
- public Bit[] getValues()\r
- {\r
- return inputValues.clone();\r
- }\r
-\r
- /**\r
- * {@link WireArrayInput} now feeds Z into the associated {@link WireArray}.\r
- */\r
- public void clearSignals()\r
- {\r
- feedSignals(Bit.Z.makeArray(length));\r
- }\r
-\r
- public Bit[] wireValuesExcludingMe() \r
- {\r
- Bit[] bits = Bit.Z.makeArray(length);\r
- for (WireArrayInput wai : inputs) \r
- {\r
- if(wai == this)\r
- continue;\r
- Util.combineInto(bits, wai.getValues());\r
- }\r
- return bits;\r
- }\r
- \r
- @Override\r
- public String toString()\r
- {\r
- return Arrays.toString(inputValues);\r
- }\r
- }\r
-\r
- @Override\r
- public String toString()\r
- {\r
- return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);\r
- }\r
-\r
- public static WireArrayInput[] extractInputs(WireArray[] w)\r
- {\r
- WireArrayInput[] inputs = new WireArrayInput[w.length];\r
- for (int i = 0; i < w.length; i++)\r
- inputs[i] = w[i].createInput();\r
- return inputs;\r
- }\r
-}
\ No newline at end of file
+++ /dev/null
-package era.mi.logic.wires;
-
-import era.mi.logic.Bit;
-
-public interface WireArrayObserver
-{
- public void update(WireArray initiator, Bit[] oldValues);
-}