1 package net.mograsim.logic.model.verilog.converter;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
8 import java.util.Objects;
10 import java.util.stream.Collectors;
12 import com.google.gson.JsonElement;
14 import net.mograsim.logic.model.verilog.converter.VerilogEmulatedModelPin.Type;
15 import net.mograsim.logic.model.verilog.model.VerilogComponentDeclaration;
17 public class ModelComponentToVerilogComponentDeclarationMapping
19 private final String modelComponentID;
20 private final JsonElement modelComponentParams;
21 private final VerilogComponentDeclaration verilogComponentDeclaration;
22 private final Set<VerilogEmulatedModelPin> pinMapping;
24 private final Set<Set<PinNameBit>> internallyConnectedPins;
25 private final Map<PinNameBit, VerilogEmulatedModelPin> prePinMapping;
26 private final Map<PinNameBit, VerilogEmulatedModelPin> outPinMapping;
27 private final Map<PinNameBit, VerilogEmulatedModelPin> resPinMapping;
28 private final List<VerilogEmulatedModelPin> reversePinMapping;
30 public ModelComponentToVerilogComponentDeclarationMapping(String modelComponentID, JsonElement modelComponentParams,
31 VerilogComponentDeclaration verilogComponentDeclaration, Set<VerilogEmulatedModelPin> pinMapping)
33 this.modelComponentID = Objects.requireNonNull(modelComponentID);
34 this.modelComponentParams = Objects.requireNonNull(modelComponentParams);
35 this.verilogComponentDeclaration = Objects.requireNonNull(verilogComponentDeclaration);
36 this.pinMapping = Set.copyOf(pinMapping);
38 this.reversePinMapping = checkAndCalculateReversePinMapping();
40 this.internallyConnectedPins = calculateInternallyConnectedPins();
41 this.prePinMapping = filterPinMapping(Type.PRE);
42 this.outPinMapping = filterPinMapping(Type.OUT);
43 this.resPinMapping = filterPinMapping(Type.RES);
46 private List<VerilogEmulatedModelPin> checkAndCalculateReversePinMapping()
48 List<VerilogEmulatedModelPin> reverseMapping = new ArrayList<>(pinMapping.size());
49 for (int i = 0; i < pinMapping.size(); i++)
50 reverseMapping.add(null);
51 Map<Type, Set<PinNameBit>> usedPinNameBits = new HashMap<>();
52 for (Type t : Type.values())
53 usedPinNameBits.put(t, new HashSet<>());
54 for (VerilogEmulatedModelPin verilogEmulatedModelPin : pinMapping)
56 // TODO check if pre, out, res pins are consistent with each other
57 for (PinNameBit pinbit : verilogEmulatedModelPin.getPinbits())
58 if (!usedPinNameBits.get(verilogEmulatedModelPin.getType()).add(pinbit))
59 throw new IllegalArgumentException("Pinbit occurs twice: " + pinbit);
60 int verilogPinIndex = verilogEmulatedModelPin.getPortIndex();
61 if (verilogComponentDeclaration.getIOPorts().get(verilogPinIndex) != verilogEmulatedModelPin.getVerilogPort())
62 throw new IllegalArgumentException("Incorrect IO port index for port: " + verilogEmulatedModelPin);
63 VerilogEmulatedModelPin oldVerilogEmulatedModelPin = reverseMapping.set(verilogPinIndex, verilogEmulatedModelPin);
64 if (oldVerilogEmulatedModelPin != null)
65 throw new IllegalArgumentException("Port is used twice: " + verilogEmulatedModelPin.getVerilogPort());
67 for (int i = 0; i < reverseMapping.size(); i++)
68 if (reverseMapping.get(i) == null)
69 throw new IllegalArgumentException("Unused IO port: " + verilogComponentDeclaration.getIOPorts().get(i));
70 return reverseMapping;
73 private Set<Set<PinNameBit>> calculateInternallyConnectedPins()
75 return pinMapping.stream().map(VerilogEmulatedModelPin::getPinbits).collect(Collectors.toUnmodifiableSet());
78 private Map<PinNameBit, VerilogEmulatedModelPin> filterPinMapping(Type filteredType)
80 Map<PinNameBit, VerilogEmulatedModelPin> result = new HashMap<>();
81 for (VerilogEmulatedModelPin p : pinMapping)
82 if (p.getType() == filteredType)
83 for (PinNameBit pinbit : p.getPinbits())
84 result.put(pinbit, p);
85 return Map.copyOf(result);
88 public String getModelComponentID()
90 return modelComponentID;
93 public JsonElement getModelComponentParams()
95 return modelComponentParams;
98 public VerilogComponentDeclaration getVerilogComponentDeclaration()
100 return verilogComponentDeclaration;
103 public Set<Set<PinNameBit>> getInternallyConnectedPins()
105 return internallyConnectedPins;
108 public Set<VerilogEmulatedModelPin> getPinMapping()
113 public Map<PinNameBit, VerilogEmulatedModelPin> getPrePinMapping()
115 return prePinMapping;
118 public Map<PinNameBit, VerilogEmulatedModelPin> getOutPinMapping()
120 return outPinMapping;
123 public Map<PinNameBit, VerilogEmulatedModelPin> getResPinMapping()
125 return resPinMapping;
128 public List<VerilogEmulatedModelPin> getReversePinMapping()
130 return reversePinMapping;
134 public int hashCode()
136 final int prime = 31;
138 result = prime * result + ((modelComponentID == null) ? 0 : modelComponentID.hashCode());
139 result = prime * result + ((modelComponentParams == null) ? 0 : modelComponentParams.hashCode());
140 result = prime * result + ((pinMapping == null) ? 0 : pinMapping.hashCode());
141 result = prime * result + ((verilogComponentDeclaration == null) ? 0 : verilogComponentDeclaration.hashCode());
146 public boolean equals(Object obj)
152 if (getClass() != obj.getClass())
154 ModelComponentToVerilogComponentDeclarationMapping other = (ModelComponentToVerilogComponentDeclarationMapping) obj;
155 if (modelComponentID == null)
157 if (other.modelComponentID != null)
159 } else if (!modelComponentID.equals(other.modelComponentID))
161 if (modelComponentParams == null)
163 if (other.modelComponentParams != null)
165 } else if (!modelComponentParams.equals(other.modelComponentParams))
167 if (pinMapping == null)
169 if (other.pinMapping != null)
171 } else if (!pinMapping.equals(other.pinMapping))
173 if (verilogComponentDeclaration == null)
175 if (other.verilogComponentDeclaration != null)
177 } else if (!verilogComponentDeclaration.equals(other.verilogComponentDeclaration))