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
;
32 import org
.jpc
.emulator
.Clock
;
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
57 timers
= (TimerPriorityQueue
)input
.loadObject();
58 currentTime
= input
.loadLong();
66 timers
= new TimerPriorityQueue(); // initial capacity to be revised
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))
82 output
.println("#" + output
.objectNumber(this) + ": Clock:");
83 dumpStatusPartial(output
);
87 public synchronized Timer
newTimer(TimerResponsive object
)
89 Timer tempTimer
= new Timer(object
, this);
93 public synchronized void update(Timer object
)
95 timers
.remove(object
);
102 public long getTime()
107 public long getTickRate()
114 long curTime
= System
.currentTimeMillis();
115 currentMillisecs
+= (curTime
- lastUpdateAt
);
116 lastUpdateAt
= curTime
;
121 lastUpdateAt
= System
.currentTimeMillis();
128 public String
toString()
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
;
150 tempTimer
= c
.timers
.peek();
151 if ((tempTimer
== null) || !tempTimer
.check(c
.getTime())) {
152 if(tempTimer
== null || !tempTimer
.enabled())
154 return tempTimer
.getExpiry();
159 // When does next timer fire. -1 if there's nothing scheduled.
160 public long getNextEventTime()
163 tempTimer
= timers
.peek();
164 if(tempTimer
== null || !tempTimer
.enabled())
166 return tempTimer
.getExpiry();