added demux; added getAllInputs() and getAllOutputs() for all components
[Mograsim.git] / era.mi / src / era / mi / logic / components / Clock.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.Simulation;
9 import era.mi.logic.timeline.TimelineEvent;
10 import era.mi.logic.timeline.TimelineEventHandler;
11 import era.mi.logic.wires.WireArray;
12 import era.mi.logic.wires.WireArray.WireArrayInput;
13
14 public class Clock implements TimelineEventHandler, Component
15 {
16         private boolean toggle = false;
17         private WireArrayInput outI;
18         private int delta;
19         
20         /**
21          * 
22          * @param out {@link WireArray} the clock's impulses are fed into
23          * @param delta ticks between rising and falling edge
24          */
25         public Clock(WireArray out, int delta)
26         {
27                 this.delta = delta;
28                 this.outI = out.createInput();
29                 Simulation.TIMELINE.addEvent(this, 50);
30         }
31
32         @Override
33         public void handle(TimelineEvent e)
34         {
35                 addToTimeline();
36                 outI.feedSignals(new Bit[] { toggle ? Bit.ONE : Bit.ZERO });
37                 toggle = !toggle;
38         }
39
40         public WireArray getOut()
41         {
42                 return outI.owner;
43         }
44         
45         private void addToTimeline()
46         {
47                 Simulation.TIMELINE.addEvent(this, delta);
48         }
49
50         @Override
51         public List<WireArray> getAllInputs()
52         {
53                 return Collections.unmodifiableList(Arrays.asList());
54         }
55
56         @Override
57         public List<WireArray> getAllOutputs()
58         {
59                 return Collections.unmodifiableList(Arrays.asList(outI.owner));
60         }
61 }