981c8488dcf31689812757c70f706f5ff790815f
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / am2900 / components / Modelinc.java
1 package net.mograsim.logic.model.am2900.components;
2
3 import static net.mograsim.logic.core.types.Bit.U;
4 import static net.mograsim.logic.core.types.Bit.X;
5 import static net.mograsim.logic.core.types.Bit.Z;
6 import static net.mograsim.logic.core.types.Bit.ZERO;
7
8 import java.util.Arrays;
9 import java.util.Map;
10
11 import net.mograsim.logic.core.types.Bit;
12 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
13 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
14 import net.mograsim.logic.model.model.LogicModelModifiable;
15 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedModelComponent;
16 import net.mograsim.logic.model.model.wires.Pin;
17 import net.mograsim.logic.model.model.wires.PinUsage;
18 import net.mograsim.logic.model.serializing.IdentifyParams;
19 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
20 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
21
22 public class Modelinc extends SimpleRectangularHardcodedModelComponent
23 {
24         private final int logicWidth;
25
26         public Modelinc(LogicModelModifiable model, String name, int logicWidth)
27         {
28                 super(model, "inc", name, "Incrementer", false);
29                 this.logicWidth = logicWidth;
30                 setSize(40, 20);
31                 addPin(new Pin(model, this, "A", logicWidth, PinUsage.INPUT, 20, 20), Position.TOP);
32                 addPin(new Pin(model, this, "CI", 1, PinUsage.INPUT, 40, 10), Position.LEFT);
33                 addPin(new Pin(model, this, "Y", logicWidth, PinUsage.OUTPUT, 20, 0), Position.BOTTOM);
34
35                 init();
36         }
37
38         @Override
39         public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
40         {
41                 Bit[] ABits = readEnds.get("A").getValues().getBits();
42                 Bit CIVal = readEnds.get("CI").getValue();
43                 Bit[] YBits = new Bit[logicWidth];
44                 if (CIVal == X)
45                         Arrays.fill(YBits, X);
46                 else if (CIVal == U)
47                         Arrays.fill(YBits, U);
48                 else if (CIVal == Z)
49                         Arrays.fill(YBits, X);
50                 else if (CIVal == ZERO)
51                         YBits = ABits;
52                 else
53                 {
54                         Bit carry = Bit.ONE;
55                         // TODO extract to helper. This code almost also exists in ModelAm2910RegCntr.
56                         for (int i = logicWidth - 1; i >= 0; i--)
57                         {
58                                 Bit a = ABits[i];
59                                 YBits[i] = a.xor(carry);
60                                 carry = a.and(carry);
61                         }
62                 }
63                 readWriteEnds.get("Y").feedSignals(YBits);
64                 return null;
65         }
66
67         @Override
68         public Integer getParamsForSerializing(IdentifyParams idParams)
69         {
70                 return logicWidth;
71         }
72
73         static
74         {
75                 IndirectModelComponentCreator.setComponentSupplier(Modelinc.class.getCanonicalName(),
76                                 (m, p, n) -> new Modelinc(m, n, p.getAsInt()));
77         }
78 }