First version of the new Verilog exporter
[Mograsim.git] / plugins / net.mograsim.logic.model.verilog / src / net / mograsim / logic / model / verilog / converter / VerilogEmulatedModelPin.java
1 package net.mograsim.logic.model.verilog.converter;
2
3 import java.util.Objects;
4
5 import net.mograsim.logic.model.verilog.model.IOPort;
6 import net.mograsim.logic.model.verilog.model.Signal;
7
8 public class VerilogEmulatedModelPin
9 {
10         private final IOPort verilogPort;
11         private final int portIndex;
12         private final PinNameBit pinbit;
13         private final Type type;
14
15         public VerilogEmulatedModelPin(IOPort verilogPort, int portIndex, PinNameBit pinbit, Type type)
16         {
17                 this.verilogPort = Objects.requireNonNull(verilogPort);
18                 this.portIndex = portIndex;
19                 this.pinbit = Objects.requireNonNull(pinbit);
20                 this.type = Objects.requireNonNull(type);
21
22                 check();
23         }
24
25         private void check()
26         {
27                 if (verilogPort.getWidth() != 2)
28                         throw new IllegalArgumentException("Every Verilog port has to have width 2");
29                 if (portIndex < 0)
30                         throw new IllegalArgumentException("Negative port index can't be negative");
31                 switch (type)
32                 {
33                 case PRE:
34                         if (verilogPort.getType() != Signal.Type.IO_INPUT)
35                                 throw new IllegalArgumentException("A PRE pin has to be an input");
36                         break;
37                 case OUT:
38                         if (verilogPort.getType() != Signal.Type.IO_OUTPUT)
39                                 throw new IllegalArgumentException("A OUT pin has to be an output");
40                         break;
41                 case RES:
42                         if (verilogPort.getType() != Signal.Type.IO_INPUT)
43                                 throw new IllegalArgumentException("A RES pin has to be an input");
44                         break;
45                 default:
46                         throw new IllegalStateException("Unknown enum constant: " + type);
47                 }
48         }
49
50         public IOPort getVerilogPort()
51         {
52                 return verilogPort;
53         }
54
55         public int getPortIndex()
56         {
57                 return portIndex;
58         }
59
60         public PinNameBit getPinbit()
61         {
62                 return pinbit;
63         }
64
65         public Type getType()
66         {
67                 return type;
68         }
69
70         @Override
71         public int hashCode()
72         {
73                 final int prime = 31;
74                 int result = 1;
75                 result = prime * result + ((pinbit == null) ? 0 : pinbit.hashCode());
76                 result = prime * result + portIndex;
77                 result = prime * result + ((type == null) ? 0 : type.hashCode());
78                 result = prime * result + ((verilogPort == null) ? 0 : verilogPort.hashCode());
79                 return result;
80         }
81
82         @Override
83         public boolean equals(Object obj)
84         {
85                 if (this == obj)
86                         return true;
87                 if (obj == null)
88                         return false;
89                 if (getClass() != obj.getClass())
90                         return false;
91                 VerilogEmulatedModelPin other = (VerilogEmulatedModelPin) obj;
92                 if (pinbit == null)
93                 {
94                         if (other.pinbit != null)
95                                 return false;
96                 } else if (!pinbit.equals(other.pinbit))
97                         return false;
98                 if (portIndex != other.portIndex)
99                         return false;
100                 if (type != other.type)
101                         return false;
102                 if (verilogPort == null)
103                 {
104                         if (other.verilogPort != null)
105                                 return false;
106                 } else if (!verilogPort.equals(other.verilogPort))
107                         return false;
108                 return true;
109         }
110
111         public static enum Type
112         {
113                 PRE, OUT, RES;
114         }
115 }