Reformatted everything. Eclipse built-in Linewrapping/Comments 140 chars
[Mograsim.git] / era.mi / src / era / mi / logic / components / TriStateBuffer.java
1 package era.mi.logic.components;\r
2 \r
3 import java.util.Arrays;\r
4 import java.util.Collections;\r
5 import java.util.List;\r
6 \r
7 import era.mi.logic.Bit;\r
8 import era.mi.logic.wires.WireArray;\r
9 import era.mi.logic.wires.WireArray.WireArrayInput;\r
10 \r
11 public class TriStateBuffer extends BasicComponent {\r
12         WireArray in, enable;\r
13         WireArrayInput outI;\r
14 \r
15         public TriStateBuffer(int processTime, WireArray in, WireArray out, WireArray enable) {\r
16                 super(processTime);\r
17                 if (in.length != out.length)\r
18                         throw new IllegalArgumentException(\r
19                                         "Tri-state output must have the same amount of bits as the input. Input: " + in.length + " Output: " + out.length);\r
20                 if (enable.length != 1)\r
21                         throw new IllegalArgumentException("Tri-state enable must have exactly one bit, not " + enable.length + ".");\r
22                 this.in = in;\r
23                 in.addObserver(this);\r
24                 this.enable = enable;\r
25                 enable.addObserver(this);\r
26                 outI = out.createInput();\r
27         }\r
28 \r
29         @Override\r
30         protected void compute() {\r
31                 if (enable.getValue() == Bit.ONE)\r
32                         outI.feedSignals(in.getValues());\r
33                 else\r
34                         outI.clearSignals();\r
35         }\r
36 \r
37         @Override\r
38         public List<WireArray> getAllInputs() {\r
39                 return Collections.unmodifiableList(Arrays.asList(in, enable));\r
40         }\r
41 \r
42         @Override\r
43         public List<WireArray> getAllOutputs() {\r
44                 return Collections.unmodifiableList(Arrays.asList(outI.owner));\r
45         }\r
46 \r
47 }\r