Add wait for PC to stop function to Lua
[jpcrr.git] / org / jpc / emulator / Clock.java
blob9dda8de4b320e29ab10dd1cde608bc5cbde8d661
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())
98 timers.offer(object);
102 public long getTime()
104 return currentTime;
107 public long getTickRate()
109 return 1000000000L;
112 public void pause()
114 long curTime = System.currentTimeMillis();
115 currentMillisecs += (curTime - lastUpdateAt);
116 lastUpdateAt = curTime;
119 public void resume()
121 lastUpdateAt = System.currentTimeMillis();
124 public void reset()
128 public String toString()
130 return "Clock";
133 public static long timePasses(Clock c, int ticks)
135 if(c.currentTime % 1000000000 > (c.currentTime + ticks) % 1000000000) {
136 long curTime = System.currentTimeMillis();
137 c.currentMillisecs += (curTime - c.lastUpdateAt);
138 long dtreal = c.currentMillisecs - c.lastMillisecs;
139 if(dtreal < 1) dtreal = 1; /* Avoid Div-by-zero */
140 c.lastUpdateAt = curTime;
141 System.err.println("Informational: Timer ticked " + (c.currentTime + ticks) + ", realtime: " +
142 dtreal + "ms, " + (100000 / dtreal) + "%," +
143 " kips: " + (1000000000 / ticks) / dtreal + ".");
144 c.lastMillisecs = c.currentMillisecs;
146 c.currentTime += ticks;
148 while(true) {
149 Timer tempTimer;
150 tempTimer = c.timers.peek();
151 if ((tempTimer == null) || !tempTimer.check(c.getTime())) {
152 if(tempTimer == null || !tempTimer.enabled())
153 return -1;
154 return tempTimer.getExpiry();
159 // When does next timer fire. -1 if there's nothing scheduled.
160 public long getNextEventTime()
162 Timer tempTimer;
163 tempTimer = timers.peek();
164 if(tempTimer == null || !tempTimer.enabled())
165 return -1;
166 return tempTimer.getExpiry();