2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2009-02-08 12:05:52 +03:00
|
|
|
// $Id: slowdown_timer.cc,v 1.33 2009-02-08 09:05:52 vruppert Exp $
|
2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2003-08-19 04:10:39 +04:00
|
|
|
// Copyright (C) 2002 MandrakeSoft S.A.
|
|
|
|
//
|
|
|
|
// MandrakeSoft S.A.
|
|
|
|
// 43, rue d'Aboukir
|
|
|
|
// 75002 Paris - France
|
|
|
|
// http://www.linux-mandrake.com/
|
|
|
|
// http://www.mandrakesoft.com/
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
2009-02-08 12:05:52 +03:00
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-08-19 04:10:39 +04:00
|
|
|
//
|
2008-02-16 01:05:43 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2003-08-19 04:10:39 +04:00
|
|
|
|
2005-12-10 21:37:35 +03:00
|
|
|
#include "bochs.h"
|
|
|
|
#include "slowdown_timer.h"
|
|
|
|
|
2001-08-22 05:21:30 +04:00
|
|
|
#include <errno.h>
|
2008-11-10 01:04:14 +03:00
|
|
|
#if !defined(_MSC_VER)
|
2008-10-01 11:54:02 +04:00
|
|
|
#include <unistd.h>
|
2008-11-10 01:04:14 +03:00
|
|
|
#endif
|
2001-08-18 07:28:23 +04:00
|
|
|
|
2001-08-22 05:21:30 +04:00
|
|
|
//These need to stay printfs because they are useless in the log file.
|
2001-08-18 07:28:23 +04:00
|
|
|
#define BX_SLOWDOWN_PRINTF_FEEDBACK 0
|
|
|
|
|
2001-08-22 05:21:30 +04:00
|
|
|
#define SECINUSEC 1000000
|
|
|
|
#define usectosec(a) ((a)/SECINUSEC)
|
|
|
|
#define sectousec(a) ((a)*SECINUSEC)
|
|
|
|
#define nsectousec(a) ((a)/1000)
|
|
|
|
|
2003-08-27 00:24:36 +04:00
|
|
|
#define MSECINUSEC 1000
|
2003-08-20 10:26:27 +04:00
|
|
|
#define usectomsec(a) ((a)/MSECINUSEC)
|
|
|
|
|
2005-10-22 21:31:02 +04:00
|
|
|
#define Qval 1000
|
2001-08-22 05:21:30 +04:00
|
|
|
#define MAXMULT 1.5
|
|
|
|
#define REALTIME_Q SECINUSEC
|
|
|
|
|
2003-08-19 04:10:39 +04:00
|
|
|
#define LOG_THIS bx_slowdown_timer.
|
|
|
|
|
2001-08-18 07:28:23 +04:00
|
|
|
bx_slowdown_timer_c bx_slowdown_timer;
|
|
|
|
|
2005-10-22 21:31:02 +04:00
|
|
|
bx_slowdown_timer_c::bx_slowdown_timer_c()
|
|
|
|
{
|
2003-08-19 04:10:39 +04:00
|
|
|
put("STIMER");
|
|
|
|
|
2001-08-18 07:28:23 +04:00
|
|
|
s.start_time=0;
|
|
|
|
s.start_emulated_time=0;
|
|
|
|
s.timer_handle=BX_NULL_TIMER_HANDLE;
|
|
|
|
}
|
|
|
|
|
2006-05-27 19:54:49 +04:00
|
|
|
void bx_slowdown_timer_c::init(void)
|
2005-10-22 21:31:02 +04:00
|
|
|
{
|
2003-08-19 04:10:39 +04:00
|
|
|
// Return early if slowdown timer not selected
|
2006-02-19 18:43:03 +03:00
|
|
|
if ((SIM->get_param_enum(BXPN_CLOCK_SYNC)->get() != BX_CLOCK_SYNC_SLOWDOWN) &&
|
2008-02-16 01:05:43 +03:00
|
|
|
(SIM->get_param_enum(BXPN_CLOCK_SYNC)->get() != BX_CLOCK_SYNC_BOTH))
|
2003-08-19 04:10:39 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
BX_INFO(("using 'slowdown' timer synchronization method"));
|
2001-08-22 05:21:30 +04:00
|
|
|
s.MAXmultiplier=MAXMULT;
|
|
|
|
s.Q=Qval;
|
2001-08-18 07:28:23 +04:00
|
|
|
|
|
|
|
if(s.MAXmultiplier<1)
|
|
|
|
s.MAXmultiplier=1;
|
|
|
|
|
2001-08-22 05:21:30 +04:00
|
|
|
s.start_time=sectousec(time(NULL));
|
2001-08-18 07:28:23 +04:00
|
|
|
s.start_emulated_time = bx_pc_system.time_usec();
|
|
|
|
s.lasttime=0;
|
2002-10-06 23:04:47 +04:00
|
|
|
if (s.timer_handle == BX_NULL_TIMER_HANDLE) {
|
|
|
|
s.timer_handle=bx_pc_system.register_timer(this, timer_handler, 100 , 1, 1,
|
2005-10-22 21:31:02 +04:00
|
|
|
"slowdown_timer");
|
2002-10-06 23:04:47 +04:00
|
|
|
}
|
2001-08-18 07:28:23 +04:00
|
|
|
bx_pc_system.deactivate_timer(s.timer_handle);
|
2001-08-22 05:21:30 +04:00
|
|
|
bx_pc_system.activate_timer(s.timer_handle,(Bit32u)s.Q,0);
|
2002-08-27 23:54:46 +04:00
|
|
|
}
|
|
|
|
|
2006-09-16 23:30:56 +04:00
|
|
|
void bx_slowdown_timer_c::exit(void)
|
|
|
|
{
|
|
|
|
s.timer_handle = BX_NULL_TIMER_HANDLE;
|
|
|
|
}
|
|
|
|
|
2006-05-27 19:54:49 +04:00
|
|
|
void bx_slowdown_timer_c::after_restore_state(void)
|
|
|
|
{
|
|
|
|
s.start_emulated_time = bx_pc_system.time_usec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_slowdown_timer_c::timer_handler(void * this_ptr)
|
2002-08-27 23:54:46 +04:00
|
|
|
{
|
2001-08-18 07:28:23 +04:00
|
|
|
bx_slowdown_timer_c * class_ptr = (bx_slowdown_timer_c *) this_ptr;
|
|
|
|
class_ptr->handle_timer();
|
|
|
|
}
|
|
|
|
|
2006-05-27 19:54:49 +04:00
|
|
|
void bx_slowdown_timer_c::handle_timer()
|
2005-10-22 21:31:02 +04:00
|
|
|
{
|
2001-08-18 07:28:23 +04:00
|
|
|
Bit64u total_emu_time = (bx_pc_system.time_usec()) - s.start_emulated_time;
|
|
|
|
Bit64u wanttime = s.lasttime+s.Q;
|
2001-08-22 05:21:30 +04:00
|
|
|
Bit64u totaltime = sectousec(time(NULL)) - s.start_time;
|
2001-08-18 07:28:23 +04:00
|
|
|
Bit64u thistime=(wanttime>totaltime)?wanttime:totaltime;
|
|
|
|
|
|
|
|
#if BX_SLOWDOWN_PRINTF_FEEDBACK
|
|
|
|
printf("Entering slowdown timer handler.\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Decide if we're behind.
|
|
|
|
* Set interrupt interval accordingly. */
|
|
|
|
if(totaltime > total_emu_time) {
|
|
|
|
bx_pc_system.deactivate_timer(s.timer_handle);
|
2001-08-22 05:21:30 +04:00
|
|
|
bx_pc_system.activate_timer(s.timer_handle,
|
2005-10-22 21:31:02 +04:00
|
|
|
(Bit32u)(s.MAXmultiplier * (float)((Bit64s)s.Q)), 0);
|
2001-08-18 07:28:23 +04:00
|
|
|
#if BX_SLOWDOWN_PRINTF_FEEDBACK
|
|
|
|
printf("running at MAX speed\n");
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
bx_pc_system.deactivate_timer(s.timer_handle);
|
2005-06-04 21:44:59 +04:00
|
|
|
bx_pc_system.activate_timer(s.timer_handle,(Bit32u)s.Q,0);
|
2001-08-18 07:28:23 +04:00
|
|
|
#if BX_SLOWDOWN_PRINTF_FEEDBACK
|
|
|
|
printf("running at NORMAL speed\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure we took at least one time quantum. */
|
2001-08-22 05:21:30 +04:00
|
|
|
/* This is a little strange. I'll try to explain.
|
|
|
|
* We're running bochs one second ahead of real time.
|
|
|
|
* this gives us a very precise division on whether
|
|
|
|
* we're ahead or behind the second line.
|
|
|
|
* Basically, here's how it works:
|
|
|
|
* *****|******************|***********...
|
|
|
|
* Time Time+1sec
|
|
|
|
* <^Bochs doesn't delay.
|
|
|
|
* ^>Bochs delays.
|
|
|
|
* <^Bochs runs at MAX speed.
|
|
|
|
* ^>Bochs runs at normal
|
|
|
|
*/
|
|
|
|
if(wanttime > (totaltime+REALTIME_Q)) {
|
2004-02-07 01:28:00 +03:00
|
|
|
#if BX_HAVE_USLEEP
|
|
|
|
usleep(s.Q);
|
|
|
|
#elif BX_HAVE_MSLEEP
|
2005-06-07 00:14:50 +04:00
|
|
|
msleep(usectomsec((Bit32u)s.Q));
|
2004-02-07 01:28:00 +03:00
|
|
|
#elif BX_HAVE_SLEEP
|
|
|
|
sleep(usectosec(s.Q));
|
|
|
|
#else
|
|
|
|
#error do not know have to sleep
|
2003-08-20 10:26:27 +04:00
|
|
|
#endif //delay(wanttime-totaltime);
|
2005-10-22 21:31:02 +04:00
|
|
|
/* alternatively: delay(Q);
|
2001-08-18 07:28:23 +04:00
|
|
|
* This works okay because we share the delay between
|
|
|
|
* two time quantums.
|
|
|
|
*/
|
|
|
|
#if BX_SLOWDOWN_PRINTF_FEEDBACK
|
2001-08-22 05:21:30 +04:00
|
|
|
printf("DELAYING for a quantum\n");
|
2001-08-18 07:28:23 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
s.lasttime=thistime;
|
2001-08-22 05:21:30 +04:00
|
|
|
|
|
|
|
//Diagnostic info:
|
2002-03-06 04:19:50 +03:00
|
|
|
#if 0
|
2001-08-22 05:21:30 +04:00
|
|
|
if(wanttime > (totaltime+REALTIME_Q)) {
|
|
|
|
if(totaltime > total_emu_time) {
|
2002-03-06 04:19:50 +03:00
|
|
|
printf("Solving OpenBSD problem.\n");
|
2001-08-22 05:21:30 +04:00
|
|
|
} else {
|
2002-03-06 04:19:50 +03:00
|
|
|
printf("too fast.\n");
|
2001-08-22 05:21:30 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(totaltime > total_emu_time) {
|
2002-03-06 04:19:50 +03:00
|
|
|
printf("too slow.\n");
|
2001-08-22 05:21:30 +04:00
|
|
|
} else {
|
2002-03-06 04:19:50 +03:00
|
|
|
printf("sometimes invalid state, normally okay.\n");
|
2001-08-22 05:21:30 +04:00
|
|
|
}
|
|
|
|
}
|
2002-03-06 04:19:50 +03:00
|
|
|
#endif // Diagnostic info
|
2001-08-18 07:28:23 +04:00
|
|
|
}
|
|
|
|
|