* @param bitA The bit of {@link Wire} a to be fused
* @param bitB The bit of {@link Wire} b to be fused
*/
- private static void fuse(Wire a, Wire b, int bitA, int bitB)
+ public static void fuse(Wire a, Wire b, int bitA, int bitB)
{
if (bitA >= a.width)
throw new IllegalArgumentException("No bit " + bitA + " in " + a + " (width " + a.width + ")");
testbench.setOutlineRenderer(new Renderer()
{
@Override
- public Object getParamsForSerializing(IdentifierGetter idGetter)
+ public Void getParamsForSerializing(IdentifierGetter idGetter)
{
return null;
}
public Optional<ComponentInfo> reqCopy(Point refPoint)
{
return Optional.of(new ComponentInfo(parent.getPosX() - refPoint.x, parent.getPosY() - refPoint.y, Editor.getIdentifier(parent),
- parent.getParamsForSerializing(new IdentifierGetter())));
+ parent.getParamsForSerializingJSON(new IdentifierGetter())));
}
@Override
import java.util.Map;
import java.util.function.Consumer;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonNull;
-
import net.haspamelodica.swt.helper.gcs.GeneralGC;
import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
import net.mograsim.logic.model.model.ViewModelModifiable;
// serializing
@Override
- public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+ public Object getParamsForSerializing(IdentifierGetter idGetter)
{
- return JsonNull.INSTANCE;
+ return null;
}
// listeners
import org.eclipse.swt.graphics.Color;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-
import net.haspamelodica.swt.helper.gcs.GeneralGC;
import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
}
@Override
- public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+ public Integer getParamsForSerializing(IdentifierGetter idGetter)
{
- return new JsonPrimitive(logicWidth);
+ return logicWidth;
}
static
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.mograsim.logic.model.modeladapter.componentadapters.ClockAdapter;
import net.mograsim.logic.model.serializing.IdentifierGetter;
import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
+import net.mograsim.logic.model.util.JsonHandler;
import net.mograsim.preferences.Preferences;
public class GUIClock extends GUIComponent
}
@Override
- public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+ public GUIClockParams getParamsForSerializing(IdentifierGetter idGetter)
{
- return new Gson().toJsonTree(params);
+ return params;
}
static
ViewLogicModelAdapter.addComponentAdapter(new ClockAdapter());
IndirectGUIComponentCreator.setComponentSupplier(GUIClock.class.getName(), (m, p, n) ->
{
- GUIClockParams params = new Gson().fromJson(p, GUIClockParams.class);
+ GUIClockParams params = JsonHandler.fromJsonTree(p, GUIClockParams.class);
if (params == null)
throw new JsonSyntaxException("Invalid!!!");
return new GUIClock(m, params, n);
import org.eclipse.swt.graphics.Color;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-
import net.haspamelodica.swt.helper.gcs.GeneralGC;
import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
}
@Override
- public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+ public Integer getParamsForSerializing(IdentifierGetter idGetter)
{
- return new JsonPrimitive(logicWidth);
+ return logicWidth;
}
static
import org.eclipse.swt.SWT;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-
import net.haspamelodica.swt.helper.gcs.GeneralGC;
import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
import net.mograsim.logic.core.types.BitVectorFormatter;
gc.setForeground(ColorManager.current().toColor(c));
double outLineY = posY + (logicWidth - 1) * heightPerPin / 2;
gc.drawLine(posX + width / 2, outLineY, posX + width, outLineY);
- double inputHeight = posY + (logicWidth - 1) * heightPerPin;
- for (int i = 0; i < logicWidth; i++, inputHeight -= 10)
+ double inputHeight = posY;
+ for (int i = 0; i < logicWidth; i++, inputHeight += 10)
{
c = BitVectorFormatter.formatAsColor(inputEnds[i]);
if (c != null)
}
@Override
- public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+ public Integer getParamsForSerializing(IdentifierGetter idGetter)
{
- return new JsonPrimitive(logicWidth);
+ return logicWidth;
}
public void setLogicModelBinding(ReadEnd[] inputEnds, ReadEnd outputEnd)
import org.eclipse.swt.SWT;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-
import net.haspamelodica.swt.helper.gcs.GeneralGC;
import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
import net.mograsim.logic.core.types.BitVectorFormatter;
gc.setForeground(ColorManager.current().toColor(c));
double inLineY = posY + (logicWidth - 1) * heightPerPin / 2;
gc.drawLine(posX, inLineY, posX + width / 2, inLineY);
- double outputHeight = posY + (logicWidth - 1) * heightPerPin;
- for (int i = 0; i < logicWidth; i++, outputHeight -= 10)
+ double outputHeight = posY;
+ for (int i = 0; i < logicWidth; i++, outputHeight += 10)
{
c = BitVectorFormatter.formatAsColor(outputEnds[i]);
if (c != null)
}
@Override
- public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+ public Integer getParamsForSerializing(IdentifierGetter idGetter)
{
- return new JsonPrimitive(logicWidth);
+ return logicWidth;
}
public void setLogicModelBinding(ReadEnd inputEnd, ReadEnd[] outputEnds)
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.mograsim.logic.model.modeladapter.componentadapters.TriStateBufferAdapter;
import net.mograsim.logic.model.serializing.IdentifierGetter;
import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
+import net.mograsim.logic.model.util.JsonHandler;
import net.mograsim.preferences.Preferences;
public class GUITriStateBuffer extends GUIComponent
}
@Override
- public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+ public GUITriStateBufferParams getParamsForSerializing(IdentifierGetter idGetter)
{
- return new Gson().toJsonTree(params);
+ return params;
}
static
ViewLogicModelAdapter.addComponentAdapter(new TriStateBufferAdapter());
IndirectGUIComponentCreator.setComponentSupplier(GUITriStateBuffer.class.getName(), (m, p, n) ->
{
- GUITriStateBufferParams params = new Gson().fromJson(p, GUITriStateBufferParams.class);
+ GUITriStateBufferParams params = JsonHandler.fromJsonTree(p, GUITriStateBufferParams.class);
if (params == null)
throw new JsonSyntaxException("Invalid!!!");
return new GUITriStateBuffer(m, params, n);
import org.eclipse.swt.graphics.Color;
-import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import net.haspamelodica.swt.helper.gcs.GeneralGC;
* @see GUIComponent#getParamsForSerializing()
*/
@Override
- public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+ public Integer getParamsForSerializing(IdentifierGetter idGetter)
{
- return new JsonPrimitive(logicWidth);
+ return logicWidth;
}
}
\ No newline at end of file
import org.eclipse.swt.graphics.Color;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-
import net.haspamelodica.swt.helper.gcs.GeneralGC;
import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
// serializing
@Override
- public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+ public String getParamsForSerializing(IdentifierGetter idGetter)
{
- return new JsonPrimitive(text);
+ return text;
}
static
import net.mograsim.logic.model.model.wires.MovablePin;
import net.mograsim.logic.model.model.wires.Pin;
import net.mograsim.logic.model.model.wires.PinUsage;
+import net.mograsim.logic.model.serializing.IdentifierGetter;
+import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
+import net.mograsim.logic.model.serializing.SubmodelComponentParams;
+import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
import net.mograsim.logic.model.snippets.Renderer;
+import net.mograsim.logic.model.util.JsonHandler;
/**
* A {@link GUIComponent} consisting of another model. A <code>SubmodelComponent</code> can have so-called "interface pins" connecting the
* inner and outer models.
*/
-//TODO override getParams
public abstract class SubmodelComponent extends GUIComponent
{
public static final String SUBMODEL_INTERFACE_NAME = "_submodelinterface";
return outlineRenderer;
}
+ @Override
+ public boolean clicked(double x, double y)
+ {
+ double scaledX = (x - getPosX()) / submodelScale;
+ double scaledY = (y - getPosY()) / submodelScale;
+ for (GUIComponent component : submodel.getComponentsByName().values())
+ if (component.getBounds().contains(scaledX, scaledY) && component.clicked(scaledX, scaledY))
+ return true;
+ return false;
+ }
+
@Override
public void render(GeneralGC gc, Rectangle visibleRegion)
{
return mapMin + (val - valMin) * (mapMax - mapMin) / (valMax - valMin);
}
+ // serializing
+
@Override
- public boolean clicked(double x, double y)
+ public SubmodelComponentParams getParamsForSerializing(IdentifierGetter idGetter)
{
- double scaledX = (x - getPosX()) / submodelScale;
- double scaledY = (y - getPosY()) / submodelScale;
- for (GUIComponent component : submodel.getComponentsByName().values())
- if (component.getBounds().contains(scaledX, scaledY) && component.clicked(scaledX, scaledY))
- return true;
- return false;
+ return SubmodelComponentSerializer.serialize(this, idGetter);
}
// operations no longer supported
{
throw new UnsupportedOperationException("Can't remove pins of a SubmodelComponent directly, call removeSubmodelInterface instead");
}
+
+ static
+ {
+ IndirectGUIComponentCreator.setComponentSupplier(SubmodelComponent.class.getCanonicalName(),
+ (m, p, n) -> SubmodelComponentSerializer.deserialize(m, JsonHandler.fromJsonTree(p, SubmodelComponentParams.class), n));
+ }
}
\ No newline at end of file
*/
public class Pin
{
- // TODO introduce input/output/tristate hints
/**
* The {@link GUIComponent} this pin belongs to.
*/
package net.mograsim.logic.model.model.wires;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-
import net.haspamelodica.swt.helper.gcs.GeneralGC;
import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
import net.mograsim.logic.core.LogicObserver;
// serializing
@Override
- public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+ public Integer getParamsForSerializing(IdentifierGetter idGetter)
{
- return new JsonPrimitive(logicWidth);
+ return logicWidth;
}
static
ReadEnd[] inputEnds = new ReadEnd[guiComponent.logicWidth];
for (int i = 0; i < guiComponent.logicWidth; i++)
{
- Wire input = logicWiresPerPin.get(guiComponent.getPin("I" + i));
- Wire.fuse(input, output, 0, guiComponent.logicWidth - 1 - i, 1);
+ Wire input = logicWiresPerPin.get(guiComponent.getPin("I" + (guiComponent.logicWidth - 1 - i)));
+ Wire.fuse(input, output, 0, i);
inputEnds[i] = input.createReadOnlyEnd();
}
guiComponent.setLogicModelBinding(inputEnds, output.createReadOnlyEnd());
ReadEnd[] outputEnds = new ReadEnd[guiComponent.logicWidth];
for (int i = 0; i < guiComponent.logicWidth; i++)
{
- Wire output = logicWiresPerPin.get(guiComponent.getPin("O" + i));
- Wire.fuse(input, output, guiComponent.logicWidth - 1 - i, 0, 1);
+ Wire output = logicWiresPerPin.get(guiComponent.getPin("O" + (guiComponent.logicWidth - 1 - i)));
+ Wire.fuse(input, output, i, 0);
outputEnds[i] = output.createReadOnlyEnd();
}
guiComponent.setLogicModelBinding(input.createReadOnlyEnd(), outputEnds);
} else
{
innerComponentParams.id = idGetter.componentIDs.apply(innerComponent);
- innerComponentParams.params = innerComponent.getParamsForSerializing(idGetter);
+ innerComponentParams.params = innerComponent.getParamsForSerializingJSON(idGetter);
}
innerComponentParams.name = innerComponent.name;
i1++;
} else
{
compParams.id = idGetter.componentIDs.apply(component);
- compParams.params = component.getParamsForSerializing(idGetter);
+ compParams.params = component.getParamsForSerializingJSON(idGetter);
}
compParams.name = component.name;
}
return parser.fromJson(rawJson, type);
}
+ public static <T> T fromJsonTree(JsonElement src, Class<T> type)
+ {
+ return parser.fromJson(src, type);
+ }
+
public static void writeJson(Object o, String path) throws IOException
{
try (FileWriter writer = new FileWriter(path))
import org.eclipse.swt.graphics.Color;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-
import net.haspamelodica.swt.helper.gcs.GeneralGC;
import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
import net.mograsim.logic.model.serializing.IdentifierGetter;
import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
+import net.mograsim.logic.model.util.JsonHandler;
import net.mograsim.machine.MainMemoryDefinition;
-import net.mograsim.machine.standard.memory.WordAddressableMemoryComponent;
import net.mograsim.preferences.Preferences;
public class GUIMemoryWA extends GUIComponent
{
- private final MainMemoryDefinition definition;
- private final Pin addrPin, dataPin, rWPin;
- private WordAddressableMemoryComponent memory;
- private final static int width = 100, height = 300;
-
- private final static String addrKey = "addrBits", cellWidthKey = "cellWidth", minAddrKey = "minAddr", maxAddrKey = "maxAddr";
+ private final MainMemoryDefinition definition;
+ private final Pin addrPin, dataPin, rWPin;
+ private WordAddressableMemoryComponent memory;
+ private final static int width = 100, height = 300;
public GUIMemoryWA(ViewModelModifiable model, MainMemoryDefinition definition, String name)
{
{
return rWPin;
}
-
+
public void setLogicModelBinding(WordAddressableMemoryComponent memory)
{
this.memory = memory;
}
-
+
public MainMemoryDefinition getDefinition()
{
return definition;
}
-
+
public WordAddressableMemoryComponent getMemory()
{
return memory;
{
// TODO This is copied from SimpleRectangularGUIGate; do this via delegation instead
Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
- if (foreground != null)
+ if(foreground != null)
gc.setForeground(foreground);
gc.drawRectangle(getPosX(), getPosY(), width, height);
Font oldFont = gc.getFont();
String label = "RAM";
Point textExtent = gc.textExtent(label);
Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
- if (textColor != null)
+ if(textColor != null)
gc.setForeground(textColor);
gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
gc.setFont(oldFont);
}
@Override
- public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+ public GUIMemoryWAParams getParamsForSerializing(IdentifierGetter idGetter)
{
- JsonObject o = new JsonObject();
- o.addProperty(addrKey, definition.getMemoryAddressBits());
- o.addProperty(cellWidthKey, definition.getCellWidth());
- o.addProperty(maxAddrKey, definition.getMaximalAddress());
- o.addProperty(minAddrKey, definition.getMinimalAddress());
- return o;
+ GUIMemoryWAParams params = new GUIMemoryWAParams();
+ params.addrBits = definition.getMemoryAddressBits();
+ params.cellWidth = definition.getCellWidth();
+ params.minAddr = definition.getMinimalAddress();
+ params.maxAddr = definition.getMaximalAddress();
+ return params;
}
static
ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) ->
{
- JsonObject o = (JsonObject) p;
- int addressBits = o.get(addrKey).getAsInt();
- int cellWidth = o.get(cellWidthKey).getAsInt();
- long maxAddr = o.get(maxAddrKey).getAsLong();
- long minAddr = o.get(minAddrKey).getAsLong();
- return new GUIMemoryWA(m, MainMemoryDefinition.create(addressBits, cellWidth, minAddr, maxAddr), n);
+ GUIMemoryWAParams params = JsonHandler.fromJsonTree(p, GUIMemoryWAParams.class);
+ return new GUIMemoryWA(m, MainMemoryDefinition.create(params.addrBits, params.cellWidth, params.minAddr, params.maxAddr), n);
});
}
+
+ public static class GUIMemoryWAParams
+ {
+ public int addrBits;
+ public int cellWidth;
+ public long minAddr;
+ public long maxAddr;
+ }
}