716c306ca1bed8abd07ea2095c4dc7608f60771a
[Mograsim.git] /
1 package net.mograsim.logic.model.modeladapter.componentadapters;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import net.mograsim.logic.core.LogicObserver;
7 import net.mograsim.logic.core.timeline.Timeline;
8 import net.mograsim.logic.core.wires.CoreWire;
9 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
10 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
11 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedModelComponent;
12 import net.mograsim.logic.model.model.wires.Pin;
13 import net.mograsim.logic.model.model.wires.PinUsage;
14 import net.mograsim.logic.model.modeladapter.CoreModelParameters;
15 import net.mograsim.logic.model.util.ObservableAtomicReference;
16
17 public class SimpleRectangularHardcodedModelComponentAdapter implements ComponentAdapter<SimpleRectangularHardcodedModelComponent>
18 {
19         @Override
20         public Class<SimpleRectangularHardcodedModelComponent> getSupportedClass()
21         {
22                 return SimpleRectangularHardcodedModelComponent.class;
23         }
24
25         @Override
26         public void createAndLinkComponent(Timeline timeline, CoreModelParameters params,
27                         SimpleRectangularHardcodedModelComponent modelComponent, Map<Pin, CoreWire> logicWiresPerPin)
28         {
29                 Map<String, ReadEnd> readEnds = new HashMap<>();
30                 Map<String, ReadWriteEnd> readWriteEnds = new HashMap<>();
31
32                 ObservableAtomicReference<Object> state = new ObservableAtomicReference<>();
33
34                 Runnable recalculate = () -> state.updateAndGet(s -> modelComponent.recalculate(s, readEnds, readWriteEnds));
35                 LogicObserver logicObs = c -> recalculate.run();
36
37                 modelComponent.setCoreModelBindingAndResetState(state, recalculate);
38
39                 for (Pin pin : modelComponent.getPins().values())
40                 {
41                         CoreWire wire = logicWiresPerPin.get(pin);
42                         ReadEnd end;
43                         if (pin.usage != PinUsage.INPUT)
44                         {
45                                 // TODO do this prettier
46                                 CoreWire pseudoWire = new CoreWire(timeline, wire.width, params.gateProcessTime);
47                                 CoreWire.fuse(wire, pseudoWire);
48                                 readWriteEnds.put(pin.name, pseudoWire.createReadWriteEnd());
49                         }
50                         end = wire.createReadOnlyEnd();
51                         readEnds.put(pin.name, end);
52                         if (pin.usage != PinUsage.OUTPUT)
53                                 end.registerObserver(logicObs);
54                 }
55         }
56 }