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
);
100 public long getTime()
105 public long getTickRate()
112 long curTime
= System
.currentTimeMillis();
113 currentMillisecs
+= (curTime
- lastUpdateAt
);
114 lastUpdateAt
= curTime
;
119 lastUpdateAt
= System
.currentTimeMillis();
126 public String
toString()
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
;
148 tempTimer
= c
.timers
.peek();
149 if((tempTimer
== null) || !tempTimer
.check(c
.getTime())) {
150 if(tempTimer
== null || !tempTimer
.enabled())
152 return tempTimer
.getExpiry();
157 // When does next timer fire. -1 if there's nothing scheduled.
158 public long getNextEventTime()
161 tempTimer
= timers
.peek();
162 if(tempTimer
== null || !tempTimer
.enabled())
164 return tempTimer
.getExpiry();