Some coding style fixes
[jpcrr.git] / org / jpc / emulator / Clock.java
blob3869dc2c33f1e48f5b2bc0051d1d88f3365a833f
1 /*
2 JPC-RR: A x86 PC Hardware Emulator
3 Release 1
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;
32 import org.jpc.emulator.Clock;
33 import java.io.*;
35 /**
37 * @author Ian Preston
39 public class Clock extends AbstractHardwareComponent
41 private TimerPriorityQueue timers;
42 private long currentTime;
43 private long lastUpdateAt;
44 private long currentMillisecs;
45 private long lastMillisecs;
47 public void dumpSRPartial(SRDumper output) throws IOException
49 super.dumpSRPartial(output);
50 output.dumpObject(timers);
51 output.dumpLong(currentTime);
54 public Clock(SRLoader input) throws IOException
56 super(input);
57 timers = (TimerPriorityQueue)input.loadObject();
58 currentTime = input.loadLong();
59 currentMillisecs = 0;
60 lastMillisecs = 0;
61 lastUpdateAt = 0;
64 public Clock()
66 timers = new TimerPriorityQueue(); // initial capacity to be revised
67 currentTime = 0;
70 public void dumpStatusPartial(StatusDumper output)
72 super.dumpStatusPartial(output);
73 output.println("\tcurrentTime " + currentTime);
74 output.println("\ttimers <object #" + output.objectNumber(timers) + ">"); if(timers != null) timers.dumpStatus(output);
77 public void dumpStatus(StatusDumper output)
79 if(output.dumped(this))
80 return;
82 output.println("#" + output.objectNumber(this) + ": Clock:");
83 dumpStatusPartial(output);
84 output.endObject();
87 public synchronized Timer newTimer(TimerResponsive object)
89 Timer tempTimer = new Timer(object, this);
90 return tempTimer;
93 public synchronized void update(Timer object)
95 timers.remove(object);
96 if(object.enabled())
97 timers.offer(object);
100 public long getTime()
102 return currentTime;
105 public long getTickRate()
107 return 1000000000L;
110 public void pause()
112 long curTime = System.currentTimeMillis();
113 currentMillisecs += (curTime - lastUpdateAt);
114 lastUpdateAt = curTime;
117 public void resume()
119 lastUpdateAt = System.currentTimeMillis();
122 public void reset()
126 public String toString()
128 return "Clock";
131 public static long timePasses(Clock c, int ticks)
133 if(c.currentTime % 1000000000 > (c.currentTime + ticks) % 1000000000) {
134 long curTime = System.currentTimeMillis();
135 c.currentMillisecs += (curTime - c.lastUpdateAt);
136 long dtreal = c.currentMillisecs - c.lastMillisecs;
137 if(dtreal < 1) dtreal = 1; /* Avoid Div-by-zero */
138 c.lastUpdateAt = curTime;
139 System.err.println("Informational: Timer ticked " + (c.currentTime + ticks) + ", realtime: " +
140 dtreal + "ms, " + (100000 / dtreal) + "%," +
141 " kips: " + (1000000000 / ticks) / dtreal + ".");
142 c.lastMillisecs = c.currentMillisecs;
144 c.currentTime += ticks;
146 while(true) {
147 Timer tempTimer;
148 tempTimer = c.timers.peek();
149 if((tempTimer == null) || !tempTimer.check(c.getTime())) {
150 if(tempTimer == null || !tempTimer.enabled())
151 return -1;
152 return tempTimer.getExpiry();
157 // When does next timer fire. -1 if there's nothing scheduled.
158 public long getNextEventTime()
160 Timer tempTimer;
161 tempTimer = timers.peek();
162 if(tempTimer == null || !tempTimer.enabled())
163 return -1;
164 return tempTimer.getExpiry();