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;
11 import com.google.gson.JsonElement;
13 import net.mograsim.logic.model.verilog.converter.VerilogEmulatedModelPin.Type;
14 import net.mograsim.logic.model.verilog.model.VerilogComponentDeclaration;
16 public class ModelComponentToVerilogComponentDeclarationMapping
18 private final String modelComponentID;
19 private final JsonElement modelComponentParams;
20 private final VerilogComponentDeclaration verilogComponentDeclaration;
21 private final Set<VerilogEmulatedModelPin> pinMapping;
23 private final Map<PinNameBit, VerilogEmulatedModelPin> prePinMapping;
24 private final Map<PinNameBit, VerilogEmulatedModelPin> outPinMapping;
25 private final Map<PinNameBit, VerilogEmulatedModelPin> resPinMapping;
26 private final List<VerilogEmulatedModelPin> reversePinMapping;
28 public ModelComponentToVerilogComponentDeclarationMapping(String modelComponentID, JsonElement modelComponentParams,
29 VerilogComponentDeclaration verilogComponentDeclaration, Set<VerilogEmulatedModelPin> pinMapping)
31 this.modelComponentID = Objects.requireNonNull(modelComponentID);
32 this.modelComponentParams = Objects.requireNonNull(modelComponentParams);
33 this.verilogComponentDeclaration = Objects.requireNonNull(verilogComponentDeclaration);
34 this.pinMapping = Set.copyOf(pinMapping);
36 this.reversePinMapping = checkAndCalculateReversePinMapping();
38 this.prePinMapping = filterPinMapping(Type.PRE);
39 this.outPinMapping = filterPinMapping(Type.OUT);
40 this.resPinMapping = filterPinMapping(Type.RES);
43 private List<VerilogEmulatedModelPin> checkAndCalculateReversePinMapping()
45 List<VerilogEmulatedModelPin> reverseMapping = new ArrayList<>(pinMapping.size());
46 for (int i = 0; i < pinMapping.size(); i++)
47 reverseMapping.add(null);
48 Map<Type, Set<PinNameBit>> usedPinNameBits = new HashMap<>();
49 for (Type t : Type.values())
50 usedPinNameBits.put(t, new HashSet<>());
51 for (VerilogEmulatedModelPin verilogEmulatedModelPin : pinMapping)
53 // TODO check if pre, out, res pins are consistent with each other
54 for (PinNameBit pinbit : verilogEmulatedModelPin.getPinbits())
55 if (!usedPinNameBits.get(verilogEmulatedModelPin.getType()).add(pinbit))
56 throw new IllegalArgumentException("Pinbit occurs twice: " + pinbit);
57 int verilogPinIndex = verilogEmulatedModelPin.getPortIndex();
58 if (verilogComponentDeclaration.getIOPorts().get(verilogPinIndex) != verilogEmulatedModelPin.getVerilogPort())
59 throw new IllegalArgumentException("Incorrect IO port index for port: " + verilogEmulatedModelPin);
60 VerilogEmulatedModelPin oldVerilogEmulatedModelPin = reverseMapping.set(verilogPinIndex, verilogEmulatedModelPin);
61 if (oldVerilogEmulatedModelPin != null)
62 throw new IllegalArgumentException("Port is used twice: " + verilogEmulatedModelPin.getVerilogPort());
64 for (int i = 0; i < reverseMapping.size(); i++)
65 if (reverseMapping.get(i) == null)
66 throw new IllegalArgumentException("Unused IO port: " + verilogComponentDeclaration.getIOPorts().get(i));
67 return reverseMapping;
70 private Map<PinNameBit, VerilogEmulatedModelPin> filterPinMapping(Type filteredType)
72 Map<PinNameBit, VerilogEmulatedModelPin> result = new HashMap<>();
73 for (VerilogEmulatedModelPin p : pinMapping)
74 if (p.getType() == filteredType)
75 for (PinNameBit pinbit : p.getPinbits())
76 result.put(pinbit, p);
77 return Map.copyOf(result);
80 public String getModelComponentID()
82 return modelComponentID;
85 public JsonElement getModelComponentParams()
87 return modelComponentParams;
90 public VerilogComponentDeclaration getVerilogComponentDeclaration()
92 return verilogComponentDeclaration;
95 public Set<VerilogEmulatedModelPin> getPinMapping()
100 public Map<PinNameBit, VerilogEmulatedModelPin> getPrePinMapping()
102 return prePinMapping;
105 public Map<PinNameBit, VerilogEmulatedModelPin> getOutPinMapping()
107 return outPinMapping;
110 public Map<PinNameBit, VerilogEmulatedModelPin> getResPinMapping()
112 return resPinMapping;
115 public List<VerilogEmulatedModelPin> getReversePinMapping()
117 return reversePinMapping;
121 public int hashCode()
123 final int prime = 31;
125 result = prime * result + ((modelComponentID == null) ? 0 : modelComponentID.hashCode());
126 result = prime * result + ((pinMapping == null) ? 0 : pinMapping.hashCode());
127 result = prime * result + ((verilogComponentDeclaration == null) ? 0 : verilogComponentDeclaration.hashCode());
132 public boolean equals(Object obj)
138 if (getClass() != obj.getClass())
140 ModelComponentToVerilogComponentDeclarationMapping other = (ModelComponentToVerilogComponentDeclarationMapping) obj;
141 if (modelComponentID == null)
143 if (other.modelComponentID != null)
145 } else if (!modelComponentID.equals(other.modelComponentID))
147 if (pinMapping == null)
149 if (other.pinMapping != null)
151 } else if (!pinMapping.equals(other.pinMapping))
153 if (verilogComponentDeclaration == null)
155 if (other.verilogComponentDeclaration != null)
157 } else if (!verilogComponentDeclaration.equals(other.verilogComponentDeclaration))