2 JPC-RR: A x86 PC Hardware Emulator
5 Copyright (C) 2007-2009 Isis Innovation Limited
6 Copyright (C) 2009 H. Ilari Liusvaara
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as published by
10 the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 Based on JPC x86 PC Hardware emulator,
22 A project from the Physics Dept, The University of Oxford
24 Details about original JPC can be found at:
26 www-jpc.physics.ox.ac.uk
30 package org
.jpc
.emulator
;
34 import org
.jpc
.emulator
.Clock
;
37 * This class provides for the triggering of events on <code>TimerResponsive</code>
38 * objects at defined and reconfigurable times.
39 * @author Chris Dennis
41 public class Timer
implements Comparable
<Timer
>, SRDumpable
43 private long expireTime
;
44 private TimerResponsive callback
;
45 private boolean enabled
;
46 private Clock myOwner
;
49 * Constructs a <code>Timer</code> which fires events on the specified
50 * <code>TimerReponsive</code> object using the specified <code>Clock</code>
51 * object as a time-source.
53 * The constructed timer is initially disabled.
54 * @param target object on which to fire callbacks.
55 * @param parent time-source used to test expiry.
57 public Timer(TimerResponsive target
, Clock parent
)
64 public void dumpStatusPartial(StatusDumper output
)
66 output
.println("\texpireTime " + expireTime
+ " enabled " + enabled
);
67 output
.println("\tcallback <object #" + output
.objectNumber(callback
) + ">"); if(callback
!= null) callback
.dumpStatus(output
);
68 output
.println("\tmyOwner <object #" + output
.objectNumber(myOwner
) + ">"); if(myOwner
!= null) myOwner
.dumpStatus(output
);
71 public void dumpStatus(StatusDumper output
)
73 if(output
.dumped(this))
76 output
.println("#" + output
.objectNumber(this) + ": Timer:");
77 dumpStatusPartial(output
);
81 public void dumpSRPartial(SRDumper output
) throws IOException
83 output
.dumpLong(expireTime
);
84 output
.dumpBoolean(enabled
);
85 output
.dumpObject(callback
);
86 output
.dumpObject(myOwner
);
89 public Timer(SRLoader input
) throws IOException
91 input
.objectCreated(this);
92 expireTime
= input
.loadLong();
93 enabled
= input
.loadBoolean();
94 callback
= (TimerResponsive
)input
.loadObject();
95 myOwner
= (Clock
)input
.loadObject();
98 public int getTimerType() {
99 return callback
.getTimerType();
103 * Returns <code>true</code> if this timer will expire at some point in the
105 * @return <code>true</code> if this timer is enabled.
107 public synchronized boolean enabled()
113 * Disables this timer. Following a call to <code>disable</code> the timer
114 * cannot ever fire again unless a call is made to <code>setExpiry</code>
116 public synchronized void disable()
122 * Sets the expiry time for and enables this timer.
124 * No restrictions are set on the value of the expiry time. Times in the past
125 * will fire a callback at the next check. Times in the future will fire on
126 * the first call to check after their expiry time has passed. Time units are
127 * decided by the implementation of <code>Clock</code> used by this timer.
128 * @param time absolute time of expiry for this timer.
130 public synchronized void setExpiry(long time
)
137 * Returns <code>true</code> and fires the targets callback method if this timer is enabled
138 * and its expiry time is earlier than the supplied time.
139 * @param time value of time to check against.
140 * @return <code>true</code> if timer had expired and callback was fired.
142 public synchronized boolean check(long time
)
144 if(this.enabled
&& (time
>= expireTime
)) {
152 private void setStatus(boolean status
)
155 myOwner
.update(this);
158 public long getExpiry()
163 public String
toString()
166 return "Timer for " + callback
.toString() + " [" + expireTime
+ "]";
168 return "Timer for " + callback
.toString() + " [Disabled]";
171 public int compareTo(Timer o
)
173 if(getExpiry() - o
.getExpiry() < 0)
175 else if(getExpiry() == o
.getExpiry())
181 public int hashCode()
184 hash
= 67 * hash
+ (int) (this.expireTime ^
(this.expireTime
>>> 32));
185 hash
= 67 * hash
+ (this.enabled ?
1 : 0);
189 public boolean equals(Object o
)
191 if(!(o
instanceof Timer
))
196 return (t
.enabled() == enabled()) && (t
.getExpiry() == getExpiry());