ModelComponentToVerilogConverter can now convert TriStateBuffers
[Mograsim.git] / plugins / net.mograsim.logic.model.verilog / src / net / mograsim / logic / model / verilog / model / expressions / Constant.java
1 package net.mograsim.logic.model.verilog.model.expressions;
2
3 import java.util.Set;
4
5 import net.mograsim.logic.core.types.BitVector;
6 import net.mograsim.logic.model.verilog.model.signals.Signal;
7
8 public class Constant extends Expression
9 {
10         private final BitVector constant;
11
12         public Constant(BitVector constant)
13         {
14                 super(constant.length());
15                 this.constant = constant;
16
17                 check();
18         }
19
20         private void check()
21         {
22                 if (!constant.isBinary())
23                         throw new IllegalArgumentException("Constant is not binary: " + constant);
24         }
25
26         public BitVector getConstant()
27         {
28                 return constant;
29         }
30
31         @Override
32         public String toVerilogCode()
33         {
34                 return getWidth() + "'b" + constant.toBitstring();
35         }
36
37         @Override
38         public Set<Signal> getReferencedSignals()
39         {
40                 return Set.of();
41         }
42
43         @Override
44         public int hashCode()
45         {
46                 final int prime = 31;
47                 int result = super.hashCode();
48                 result = prime * result + ((constant == null) ? 0 : constant.hashCode());
49                 return result;
50         }
51
52         @Override
53         public boolean equals(Object obj)
54         {
55                 if (this == obj)
56                         return true;
57                 if (!super.equals(obj))
58                         return false;
59                 if (getClass() != obj.getClass())
60                         return false;
61                 Constant other = (Constant) obj;
62                 if (constant == null)
63                 {
64                         if (other.constant != null)
65                                 return false;
66                 } else if (!constant.equals(other.constant))
67                         return false;
68                 return true;
69         }
70 }