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