Created model for the logic Clock
authorChristian Femers <femers@in.tum.de>
Sun, 1 Sep 2019 00:49:50 +0000 (02:49 +0200)
committerChristian Femers <femers@in.tum.de>
Sun, 1 Sep 2019 00:49:50 +0000 (02:49 +0200)
net.mograsim.logic.core/src/net/mograsim/logic/core/components/Clock.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIClock.java [new file with mode: 0644]
net.mograsim.logic.model/src/net/mograsim/logic/model/modeladapter/componentadapters/ClockAdapter.java [new file with mode: 0644]
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/standardComponentIDMapping.json

index 6562135..b691d7e 100644 (file)
@@ -1,7 +1,11 @@
 package net.mograsim.logic.core.components;
 
+import java.util.Collection;
+import java.util.HashSet;
 import java.util.List;
 
+import net.mograsim.logic.core.LogicObservable;
+import net.mograsim.logic.core.LogicObserver;
 import net.mograsim.logic.core.timeline.Timeline;
 import net.mograsim.logic.core.timeline.TimelineEvent;
 import net.mograsim.logic.core.timeline.TimelineEventHandler;
@@ -10,8 +14,9 @@ import net.mograsim.logic.core.wires.Wire;
 import net.mograsim.logic.core.wires.Wire.ReadEnd;
 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
 
-public class Clock extends Component implements TimelineEventHandler
+public class Clock extends Component implements TimelineEventHandler, LogicObservable
 {
+       private Collection<LogicObserver> observers;
        private boolean toggle = false;
        private ReadWriteEnd out;
        private int delta;
@@ -26,6 +31,7 @@ public class Clock extends Component implements TimelineEventHandler
                super(timeline);
                this.delta = delta;
                this.out = out;
+               this.observers = new HashSet<>();
                addToTimeline();
        }
 
@@ -35,6 +41,7 @@ public class Clock extends Component implements TimelineEventHandler
                addToTimeline();
                out.feedSignals(toggle ? Bit.ONE : Bit.ZERO);
                toggle = !toggle;
+               notifyObservers();
        }
 
        public ReadWriteEnd getOut()
@@ -42,6 +49,11 @@ public class Clock extends Component implements TimelineEventHandler
                return out;
        }
 
+       public boolean isOn()
+       {
+               return !toggle;
+       }
+
        private void addToTimeline()
        {
                timeline.addEvent(this, delta);
@@ -58,4 +70,22 @@ public class Clock extends Component implements TimelineEventHandler
        {
                return List.of(out);
        }
+
+       @Override
+       public void registerObserver(LogicObserver ob)
+       {
+               observers.add(ob);
+       }
+
+       @Override
+       public void deregisterObserver(LogicObserver ob)
+       {
+               observers.remove(ob);
+       }
+
+       @Override
+       public void notifyObservers()
+       {
+               observers.forEach(ob -> ob.update(this));
+       }
 }
diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIClock.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIClock.java
new file mode 100644 (file)
index 0000000..91a4f32
--- /dev/null
@@ -0,0 +1,161 @@
+package net.mograsim.logic.model.model.components.atomic;
+
+import org.eclipse.swt.graphics.Color;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonSyntaxException;
+
+import net.haspamelodica.swt.helper.gcs.GeneralGC;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
+import net.mograsim.logic.core.LogicObserver;
+import net.mograsim.logic.core.components.Clock;
+import net.mograsim.logic.model.model.ViewModelModifiable;
+import net.mograsim.logic.model.model.components.GUIComponent;
+import net.mograsim.logic.model.model.components.Orientation;
+import net.mograsim.logic.model.model.components.OrientationCalculator;
+import net.mograsim.logic.model.model.wires.Pin;
+import net.mograsim.logic.model.model.wires.PinUsage;
+import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
+import net.mograsim.logic.model.modeladapter.componentadapters.ClockAdapter;
+import net.mograsim.logic.model.serializing.IdentifierGetter;
+import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
+import net.mograsim.preferences.Preferences;
+
+public class GUIClock extends GUIComponent
+{
+       private static final double width = 20;
+       private static final double height = 20;
+       private static final double fontHeight = 5;
+
+       private final Pin outputPin;
+
+       private final LogicObserver logicObs;
+       private GUIClockParams params;
+       private OrientationCalculator oc;
+       private Clock clock;
+
+       public GUIClock(ViewModelModifiable model, GUIClockParams params)
+       {
+               this(model, params, null);
+       }
+
+       public GUIClock(ViewModelModifiable model, GUIClockParams params, String name)
+       {
+               super(model, name);
+               this.params = params;
+               logicObs = (i) -> model.requestRedraw();
+
+               oc = new OrientationCalculator(params.orientation, width, height);
+               setSize(oc.width(), oc.height());
+
+               this.outputPin = new Pin(this, "", 1, PinUsage.OUTPUT, oc.newX(width, height / 2), oc.newY(width, height / 2));
+               addPin(outputPin);
+       }
+
+       @Override
+       public void render(GeneralGC gc, Rectangle visibleRegion)
+       {
+               Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
+               if (foreground != null)
+                       gc.setForeground(foreground);
+               gc.drawRectangle(getBounds());
+               String label = clock == null ? "null" : (clock.isOn() ? "|" : "\u2015");
+               Font oldFont = gc.getFont();
+               Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
+               gc.setFont(labelFont);
+               Point textExtent = gc.textExtent(label);
+               Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
+               if (textColor != null)
+                       gc.setForeground(textColor);
+               gc.drawText(label, getPosX() + (oc.width() - textExtent.x) / 2, getPosY() + (oc.height() - textExtent.y) / 2, true);
+               gc.setFont(oldFont);
+       }
+
+       public void setLogicModelBinding(Clock clock)
+       {
+               if (this.clock != null)
+                       this.clock.deregisterObserver(logicObs);
+               this.clock = clock;
+               if (clock != null)
+                       clock.registerObserver(logicObs);
+       }
+
+       public boolean hasLogicModelBinding()
+       {
+               return clock != null;
+       }
+
+       @Override
+       public Object getHighLevelState(String stateID)
+       {
+               switch (stateID)
+               {
+               case "out":
+                       if (clock != null)
+                               return clock.getOut().getInputValues();
+                       return null;
+               default:
+                       return super.getHighLevelState(stateID);
+               }
+       }
+
+       @Override
+       public void setHighLevelState(String stateID, Object newState)
+       {
+               switch (stateID)
+               {
+               case "out":
+                       throw new UnsupportedOperationException("cannot set state of clock");
+               default:
+                       super.setHighLevelState(stateID, newState);
+               }
+       }
+
+       public Clock getClock()
+       {
+               return clock;
+       }
+
+       public Pin getOutputPin()
+       {
+               return outputPin;
+       }
+
+       public int getDelta()
+       {
+               return params.delta;
+       }
+
+       @Override
+       public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+       {
+               return new Gson().toJsonTree(params);
+       }
+
+       static
+       {
+               ViewLogicModelAdapter.addComponentAdapter(new ClockAdapter());
+               IndirectGUIComponentCreator.setComponentSupplier(GUIClock.class.getName(), (m, p, n) ->
+               {
+                       GUIClockParams params = new Gson().fromJson(p, GUIClockParams.class);
+                       if (params == null)
+                               throw new JsonSyntaxException("Invalid!!!");
+                       return new GUIClock(m, params, n);
+               });
+       }
+
+       public static class GUIClockParams
+       {
+               int delta;
+               Orientation orientation;
+
+               public GUIClockParams(int delta, Orientation orientation)
+               {
+                       this.delta = delta;
+                       this.orientation = orientation;
+               }
+       }
+}
diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/modeladapter/componentadapters/ClockAdapter.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/modeladapter/componentadapters/ClockAdapter.java
new file mode 100644 (file)
index 0000000..55d3edb
--- /dev/null
@@ -0,0 +1,30 @@
+package net.mograsim.logic.model.modeladapter.componentadapters;
+
+import java.util.Map;
+
+import net.mograsim.logic.core.components.Clock;
+import net.mograsim.logic.core.timeline.Timeline;
+import net.mograsim.logic.core.wires.Wire;
+import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
+import net.mograsim.logic.model.model.components.atomic.GUIClock;
+import net.mograsim.logic.model.model.wires.Pin;
+import net.mograsim.logic.model.modeladapter.LogicModelParameters;
+
+public class ClockAdapter implements ComponentAdapter<GUIClock>
+{
+
+       @Override
+       public Class<GUIClock> getSupportedClass()
+       {
+               return GUIClock.class;
+       }
+
+       @Override
+       public void createAndLinkComponent(Timeline timeline, LogicModelParameters params, GUIClock guiClock, Map<Pin, Wire> logicWiresPerPin)
+       {
+               ReadWriteEnd out = logicWiresPerPin.get(guiClock.getOutputPin()).createReadWriteEnd();
+               Clock c = new Clock(timeline, out, guiClock.getDelta());
+               guiClock.setLogicModelBinding(c);
+       }
+
+}
\ No newline at end of file
index 1913f72..ec15a5a 100644 (file)
@@ -56,6 +56,7 @@ mograsim version: 0.1.3
   "GUIsel4_12": "class:net.mograsim.logic.model.am2900.components.GUIsel4_12",
   "GUISplitter": "class:net.mograsim.logic.model.model.components.atomic.GUISplitter",
   "GUITriStateBuffer": "class:net.mograsim.logic.model.model.components.atomic.GUITriStateBuffer",
+  "GUIClock": "class:net.mograsim.logic.model.model.components.atomic.GUIClock",
   "GUIxor": "file:components/GUIxor.json",
   "TextComponent": "class:net.mograsim.logic.model.model.components.atomic.TextComponent",
   "WireCrossPoint": "class:net.mograsim.logic.model.model.wires.WireCrossPoint"