First version of the new Verilog exporter
[Mograsim.git] / plugins / net.mograsim.logic.model.verilog / src / net / mograsim / logic / model / verilog / converter / ModelComponentToVerilogComponentDeclarationMapping.java
1 package net.mograsim.logic.model.verilog.converter;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Objects;
7 import java.util.Set;
8 import java.util.function.Function;
9 import java.util.stream.Collectors;
10
11 import com.google.gson.JsonElement;
12
13 import net.mograsim.logic.model.verilog.converter.VerilogEmulatedModelPin.Type;
14 import net.mograsim.logic.model.verilog.model.VerilogComponentDeclaration;
15
16 public class ModelComponentToVerilogComponentDeclarationMapping
17 {
18         private final String modelComponentID;
19         private final JsonElement modelComponentParams;
20         private final VerilogComponentDeclaration verilogComponentDeclaration;
21         private final Set<VerilogEmulatedModelPin> pinMapping;
22
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;
27
28         public ModelComponentToVerilogComponentDeclarationMapping(String modelComponentID, JsonElement modelComponentParams,
29                         VerilogComponentDeclaration verilogComponentDeclaration, Set<VerilogEmulatedModelPin> pinMapping)
30         {
31                 this.modelComponentID = Objects.requireNonNull(modelComponentID);
32                 this.modelComponentParams = Objects.requireNonNull(modelComponentParams);
33                 this.verilogComponentDeclaration = Objects.requireNonNull(verilogComponentDeclaration);
34                 this.pinMapping = Set.copyOf(pinMapping);
35
36                 this.reversePinMapping = checkAndCalculateReversePinMapping();
37
38                 this.prePinMapping = filterPinMapping(Type.PRE);
39                 this.outPinMapping = filterPinMapping(Type.OUT);
40                 this.resPinMapping = filterPinMapping(Type.RES);
41         }
42
43         private List<VerilogEmulatedModelPin> checkAndCalculateReversePinMapping()
44         {
45                 List<VerilogEmulatedModelPin> reverseMapping = new ArrayList<>(pinMapping.size());
46                 for (int i = 0; i < pinMapping.size(); i++)
47                         reverseMapping.add(null);
48                 for (VerilogEmulatedModelPin verilogEmulatedModelPin : pinMapping)
49                 {
50                         int verilogPinIndex = verilogEmulatedModelPin.getPortIndex();
51                         if (verilogComponentDeclaration.getIOPorts().get(verilogPinIndex) != verilogEmulatedModelPin.getVerilogPort())
52                                 throw new IllegalArgumentException("Incorrect IO port index for port: " + verilogEmulatedModelPin);
53                         VerilogEmulatedModelPin oldVerilogEmulatedModelPin = reverseMapping.set(verilogPinIndex, verilogEmulatedModelPin);
54                         if (oldVerilogEmulatedModelPin != null)
55                                 throw new IllegalArgumentException("Port is used twice: " + verilogEmulatedModelPin.getVerilogPort());
56                 }
57                 for (int i = 0; i < reverseMapping.size(); i++)
58                         if (reverseMapping.get(i) == null)
59                                 throw new IllegalArgumentException("Unused IO port: " + verilogComponentDeclaration.getIOPorts().get(i));
60                 return reverseMapping;
61         }
62
63         private Map<PinNameBit, VerilogEmulatedModelPin> filterPinMapping(Type filteredType)
64         {
65                 return pinMapping.stream().filter(p -> p.getType() == filteredType)
66                                 .collect(Collectors.toMap(VerilogEmulatedModelPin::getPinbit, Function.identity()));
67         }
68
69         public String getModelComponentID()
70         {
71                 return modelComponentID;
72         }
73
74         public JsonElement getModelComponentParams()
75         {
76                 return modelComponentParams;
77         }
78
79         public VerilogComponentDeclaration getVerilogComponentDeclaration()
80         {
81                 return verilogComponentDeclaration;
82         }
83
84         public Set<VerilogEmulatedModelPin> getPinMapping()
85         {
86                 return pinMapping;
87         }
88
89         public Map<PinNameBit, VerilogEmulatedModelPin> getPrePinMapping()
90         {
91                 return prePinMapping;
92         }
93
94         public Map<PinNameBit, VerilogEmulatedModelPin> getOutPinMapping()
95         {
96                 return outPinMapping;
97         }
98
99         public Map<PinNameBit, VerilogEmulatedModelPin> getResPinMapping()
100         {
101                 return resPinMapping;
102         }
103
104         public List<VerilogEmulatedModelPin> getReversePinMapping()
105         {
106                 return reversePinMapping;
107         }
108
109         @Override
110         public int hashCode()
111         {
112                 final int prime = 31;
113                 int result = 1;
114                 result = prime * result + ((modelComponentID == null) ? 0 : modelComponentID.hashCode());
115                 result = prime * result + ((pinMapping == null) ? 0 : pinMapping.hashCode());
116                 result = prime * result + ((verilogComponentDeclaration == null) ? 0 : verilogComponentDeclaration.hashCode());
117                 return result;
118         }
119
120         @Override
121         public boolean equals(Object obj)
122         {
123                 if (this == obj)
124                         return true;
125                 if (obj == null)
126                         return false;
127                 if (getClass() != obj.getClass())
128                         return false;
129                 ModelComponentToVerilogComponentDeclarationMapping other = (ModelComponentToVerilogComponentDeclarationMapping) obj;
130                 if (modelComponentID == null)
131                 {
132                         if (other.modelComponentID != null)
133                                 return false;
134                 } else if (!modelComponentID.equals(other.modelComponentID))
135                         return false;
136                 if (pinMapping == null)
137                 {
138                         if (other.pinMapping != null)
139                                 return false;
140                 } else if (!pinMapping.equals(other.pinMapping))
141                         return false;
142                 if (verilogComponentDeclaration == null)
143                 {
144                         if (other.verilogComponentDeclaration != null)
145                                 return false;
146                 } else if (!verilogComponentDeclaration.equals(other.verilogComponentDeclaration))
147                         return false;
148                 return true;
149         }
150 }