Compiles now. I need to add some more stuff to test it.

This commit is contained in:
Gregory Alexander 2003-02-17 03:20:13 +00:00
parent 163c6be18d
commit d9e1b2c8b7
2 changed files with 207 additions and 23 deletions

View File

@ -1,15 +1,88 @@
#include "bochs.h"
//Important constant #defines:
#define USEC_PER_SECOND (1000000)
// define a macro to convert floating point numbers into 64-bit integers.
// In MSVC++ you can convert a 64-bit float into a 64-bit signed integer,
// but it will not convert a 64-bit float into a 64-bit unsigned integer.
// This macro works around that.
#define F2I(x) ((Bit64u)(Bit64s) (x))
#define I2F(x) ((double)(Bit64s) (x))
//CONFIGURATION #defines:
//MAINLINE Configuration (For realtime PIT):
//How much faster than real time we can go:
#define MAX_MULT (1.25)
//Minimum number of emulated useconds per second.
// Now calculated using BX_MIN_IPS, the minimum number of
// instructions per second.
#define MIN_USEC_PER_SECOND (((((Bit64u)USEC_PER_SECOND)*((Bit64u)BX_MIN_IPS))/((Bit64u)(bx_options.Oips->get())))+(Bit64u)1)
//DEBUG configuration:
//Debug with printf options.
#define DEBUG_REALTIME_WITH_PRINTF 0
//Use to test execution at multiples of real time.
#define TIME_DIVIDER (1)
#define TIME_MULTIPLIER (1)
#define TIME_HEADSTART (0)
#define GET_VIRT_REALTIME64_USEC() (((bx_get_realtime64_usec()*(Bit64u)TIME_MULTIPLIER/(Bit64u)TIME_DIVIDER)))
//Set up Logging.
#define LOG_THIS bx_virt_timer.
//A single instance.
bx_virt_timer_c bx_virt_timer;
//Generic MAX and MIN Functions
#define BX_MAX(a,b) ( ((a)>(b))?(a):(b) )
#define BX_MIN(a,b) ( ((a)>(b))?(b):(a) )
//USEC_ALPHA is multiplier for the past.
//USEC_ALPHA_B is 1-USEC_ALPHA, or multiplier for the present.
#define USEC_ALPHA ((double)(.8))
#define USEC_ALPHA_B ((double)(((double)1)-USEC_ALPHA))
#define USEC_ALPHA2 ((double)(.5))
#define USEC_ALPHA2_B ((double)(((double)1)-USEC_ALPHA2))
#define ALPHA_LOWER(old,new) ((Bit64u)((old<new)?((USEC_ALPHA*(I2F(old)))+(USEC_ALPHA_B*(I2F(new)))):((USEC_ALPHA2*(I2F(old)))+(USEC_ALPHA2_B*(I2F(new))))))
//Conversion between emulated useconds and optionally realtime ticks.
#define TICKS_TO_USEC(a) ( ((a)*usec_per_second)/ticks_per_second )
#define USEC_TO_TICKS(a) ( ((a)*ticks_per_second)/usec_per_second )
bx_virt_timer_c::bx_virt_timer_c( void )
{
put("VTIMER");
settype(VTIMERLOG);
}
bx_virt_timer_c::~bx_virt_timer_c( void )
{
}
const Bit64u bx_pc_system_c::NullTimerInterval = BX_MAX_VIRTUAL_TIME;
static void
void
bx_virt_timer_c::nullTimer(void* this_ptr) {
UNUSED(this_ptr);
}
void periodic(Bit64u time_passed) {
void
bx_virt_timer_c::periodic(Bit64u time_passed) {
//Assert that we haven't skipped any events.
BX_ASSERT (time_passed <= timers_next_event_time);
BX_ASSERT(!in_timer_handler);
@ -25,6 +98,7 @@ void periodic(Bit64u time_passed) {
//Starting timer handler calls.
in_timer_handler = 1;
//Otherwise, cause any events to occur that should.
int i;
for(i=0;i<numTimers;i++) {
if( timer[i].inUse && timer[i].active ) {
//Assert that we haven't skipped any timers.
@ -37,7 +111,7 @@ void periodic(Bit64u time_passed) {
}
//This function MUST return, or the timer mechanism
// will be broken.
timer[i].funct(this_ptr);
timer[i].funct(timer[i].this_ptr);
}
}
}
@ -108,7 +182,7 @@ bx_virt_timer_c::register_timer( void *this_ptr, bx_timer_handler_t handler,
}
//We don't like starting with a zero period timer.
BX_ASSERT((!active) || (period>0));
BX_ASSERT((!active) || (useconds>0));
//Search for an unused timer.
int i;
@ -129,10 +203,10 @@ bx_virt_timer_c::register_timer( void *this_ptr, bx_timer_handler_t handler,
timer[i].funct = handler;
timer[i].this_ptr = this_ptr;
strncpy(timer[i].id, id, BxMaxTimerIDLen);
id[BxMaxTimerIDLen]=0; //I like null terminated strings.
timer[i].id[BxMaxTimerIDLen]=0; //I like null terminated strings.
if(period < timers_next_event_time) {
timers_next_event_time = period;
if(useconds < timers_next_event_time) {
timers_next_event_time = useconds;
//FIXME
}
}
@ -171,14 +245,15 @@ bx_virt_timer_c::activate_timer( unsigned timer_index, Bit32u useconds,
BX_ASSERT(timer_index < BX_MAX_VIRTUAL_TIMERS);
BX_ASSERT(timer[timer_index].inUse);
BX_ASSERT(useconds>0);
timer[timer_index].period=useconds;
timer[timer_index].timeToFire = current_timers_time + (Bit64u)useconds;
timer[timer_index].active=1;
timer[timer_index].continuous=continuous;
if(period < timers_next_event_time) {
timers_next_event_time = period;
if(useconds < timers_next_event_time) {
timers_next_event_time = useconds;
//FIXME
}
}
@ -199,7 +274,7 @@ bx_virt_timer_c::deactivate_timer( unsigned timer_index ) {
void
bx_virt_timer_c::advance_virtual_time(Bit64u time_passed) {
ASSERT(time_passed <= virtual_next_event_time);
BX_ASSERT(time_passed <= virtual_next_event_time);
current_virtual_time += time_passed;
virtual_next_event_time -= time_passed;
@ -253,10 +328,97 @@ bx_virt_timer_c::timer_handler(void) {
return;
}
Bit64u usec_delta = bx_pc_system.time_usec()-last_usec;
if (usec_delta) {
#if BX_HAVE_REALTIME_USEC
Bit64u ticks_delta = 0;
Bit64u real_time_delta = GET_VIRT_REALTIME64_USEC() - last_real_time;
Bit64u real_time_total = real_time_delta + total_real_usec;
Bit64u system_time_delta = (Bit64u)usec_delta + (Bit64u)stored_delta;
if(real_time_delta) {
last_realtime_delta = real_time_delta;
last_realtime_ticks = total_ticks;
}
ticks_per_second = USEC_PER_SECOND;
//Start out with the number of ticks we would like
// to have to line up with real time.
ticks_delta = real_time_total - total_ticks;
if(real_time_total < total_ticks) {
//This slows us down if we're already ahead.
// probably only an issue on startup, but it solves some problems.
ticks_delta = 0;
}
if(ticks_delta + total_ticks - last_realtime_ticks > (F2I(MAX_MULT * I2F(last_realtime_delta)))) {
//This keeps us from going too fast in relation to real time.
ticks_delta = (F2I(MAX_MULT * I2F(last_realtime_delta))) + last_realtime_ticks - total_ticks;
ticks_per_second = F2I(MAX_MULT * I2F(USEC_PER_SECOND));
}
if(ticks_delta > system_time_delta * USEC_PER_SECOND / MIN_USEC_PER_SECOND) {
//This keeps us from having too few instructions between ticks.
ticks_delta = system_time_delta * USEC_PER_SECOND / MIN_USEC_PER_SECOND;
}
if(ticks_delta > virtual_next_event_time) {
//This keeps us from missing ticks.
ticks_delta = virtual_next_event_time;
}
if(ticks_delta) {
# if DEBUG_REALTIME_WITH_PRINTF
//Every second print some info.
if(((last_real_time + real_time_delta) / USEC_PER_SECOND) > (last_real_time / USEC_PER_SECOND)) {
Bit64u temp1, temp2, temp3, temp4;
temp1 = (Bit64u) total_real_usec;
temp2 = (total_real_usec);
temp3 = (Bit64u)total_ticks;
temp4 = (Bit64u)((total_real_usec) - total_ticks);
printf("useconds: %llu, ",temp1);
printf("expect ticks: %llu, ",temp2);
printf("ticks: %llu, ",temp3);
printf("diff: %llu\n",temp4);
}
# endif
last_real_time += real_time_delta;
total_real_usec += real_time_delta;
last_system_usec += system_time_delta;
stored_delta = 0;
total_ticks += ticks_delta;
} else {
stored_delta = system_time_delta;
}
Bit64u a,b;
a=(usec_per_second);
if(real_time_delta) {
//FIXME
Bit64u em_realtime_delta = last_system_usec + stored_delta - em_last_realtime;
b=((Bit64u)USEC_PER_SECOND * em_realtime_delta / real_time_delta);
em_last_realtime = last_system_usec + stored_delta;
} else {
b=a;
}
usec_per_second = ALPHA_LOWER(a,b);
#else
BX_ASSERT(0);
#endif
advance_virtual_time(ticks_delta);
}
last_usec=last_usec + usec_delta;
bx_pc_system.deactivate_timer(system_timer_id);
BX_ASSERT(virtual_next_event_time);
bx_pc_system.activate_timer(system_timer_id,
BX_MAX(1,TICKS_TO_USEC(virtual_next_event_time)),
0);
}
static void
void
bx_virt_timer_c::pc_system_timer_handler(void* this_ptr) {
((bx_virt_timer_c *)this_ptr)->timer_handler();
}

View File

@ -1,4 +1,8 @@
#ifndef _BX_VIRT_TIMER_H
#define _BX_VIRT_TIMER_H
#define BX_USE_VIRTUAL_TIMERS 0
#define BX_VIRTUAL_TIMERS_REALTIME 0
@ -7,7 +11,7 @@
#define BX_MAX_VIRTUAL_TIME (0x7fffffff)
class bx_virt_timer_c {
class bx_virt_timer_c : public logfunctions {
private:
struct {
@ -37,6 +41,21 @@ class bx_virt_timer_c {
Bit64u virtual_next_event_time;
Bit64u current_virtual_time;
//Real time variables:
Bit64u last_real_time;
Bit64u total_real_usec;
Bit64u last_realtime_delta;
//System time variables:
Bit64u last_usec;
Bit64u usec_per_second;
Bit64u stored_delta;
Bit64u last_system_usec;
Bit64u em_last_realtime;
//Virtual timer variables:
Bit64u total_ticks;
Bit64u last_realtime_ticks;
Bit64u ticks_per_second;
int system_timer_id;
//Whether or not to use virtual timers.
@ -51,11 +70,6 @@ class bx_virt_timer_c {
static void nullTimer(void* this_ptr);
//Cycle count until the next timer handler needs to be called.
void update_next_virtual_event_time(void){
return timers_next_event_time + current_timers_time - current_virtual_time;
}
//Step the given number of cycles, optionally calling any timer handlers.
void periodic(Bit64u time_passed);
@ -67,9 +81,6 @@ class bx_virt_timer_c {
// calls periodic as needed.
void advance_virtual_time(Bit64u time_passed);
//The real timer handler.
void timer_handler();
public:
@ -102,9 +113,20 @@ class bx_virt_timer_c {
//Timer handler passed to pc_system
void pc_system_timer_handler(void* this_ptr);
static void pc_system_timer_handler(void* this_ptr);
//The real timer handler.
void timer_handler();
//Initialization
void init(void);
bx_virt_timer_c(void);
~bx_virt_timer_c(void);
};
}
extern bx_virt_timer_c bx_virt_timer;
#endif // _BX_VIRT_TIMER_H