Some work on the Bochs timer code

- add timer parameter to the save/restore tree
- change timer parameter type to Bit32u
- don't call timer function if set to NULL
- optimized loop that calls the timer functions
- minor cleanups
This commit is contained in:
Volker Ruppert 2014-01-24 17:22:34 +00:00
parent 407681c98a
commit 35c0724c50
2 changed files with 26 additions and 26 deletions

View File

@ -241,6 +241,7 @@ void bx_pc_system_c::register_state(void)
BXRS_DEC_PARAM_FIELD(bxtimer, timeToFire, timer[i].timeToFire);
BXRS_PARAM_BOOL(bxtimer, active, timer[i].active);
BXRS_PARAM_BOOL(bxtimer, continuous, timer[i].continuous);
BXRS_DEC_PARAM_FIELD(bxtimer, param, timer[i].param);
}
}
@ -270,17 +271,17 @@ int bx_pc_system_c::register_timer_ticks(void* this_ptr, bx_timer_handler_t func
ticks = MinAllowableTimerPeriod;
}
// search for new timer for i=1, i=0 is reserved for NullTimer
for (i=1; i < numTimers; i++) {
// search for new timer (i = 0 is reserved for NullTimer)
for (i = 1; i < numTimers; i++) {
if (timer[i].inUse == 0)
break;
}
#if BX_TIMER_DEBUG
if (i==0)
BX_PANIC(("register_timer: cannot register NullTimer again!"));
if (numTimers >= BX_MAX_TIMERS)
if (numTimers >= BX_MAX_TIMERS) {
BX_PANIC(("register_timer: too many registered timers"));
return -1;
}
#if BX_TIMER_DEBUG
if (this_ptr == NULL)
BX_PANIC(("register_timer_ticks: this_ptr is NULL!"));
if (funct == NULL)
@ -314,12 +315,12 @@ int bx_pc_system_c::register_timer_ticks(void* this_ptr, bx_timer_handler_t func
numTimers++; // One new timer installed.
// Return timer id.
return(i);
return i;
}
void bx_pc_system_c::countdownEvent(void)
{
unsigned i;
unsigned i, first = numTimers, last = 0;
Bit64u minTimeToFire;
bx_bool triggered[BX_MAX_TIMERS];
@ -335,7 +336,7 @@ void bx_pc_system_c::countdownEvent(void)
ticksTotal += Bit64u(currCountdownPeriod);
minTimeToFire = (Bit64u) -1;
for (i=0; i < numTimers; i++) {
for (i = 0; i < numTimers; i++) {
triggered[i] = 0; // Reset triggered flag.
if (timer[i].active) {
#if BX_TIMER_DEBUG
@ -350,15 +351,15 @@ void bx_pc_system_c::countdownEvent(void)
if (timer[i].continuous==0) {
// If triggered timer is one-shot, deactive.
timer[i].active = 0;
}
else {
} else {
// Continuous timer, increment time-to-fire by period.
timer[i].timeToFire += timer[i].period;
if (timer[i].timeToFire < minTimeToFire)
minTimeToFire = timer[i].timeToFire;
}
}
else {
if (i < first) first = i;
last = i;
} else {
// This timer is not ready to fire yet.
if (timer[i].timeToFire < minTimeToFire)
minTimeToFire = timer[i].timeToFire;
@ -372,10 +373,10 @@ void bx_pc_system_c::countdownEvent(void)
currCountdown = currCountdownPeriod =
Bit32u(minTimeToFire - ticksTotal);
for (i=0; i < numTimers; i++) {
for (i = first; i <= last; i++) {
// Call requested timer function. It may request a different
// timer period or deactivate etc.
if (triggered[i]) {
if (triggered[i] && (timer[i].funct != NULL)) {
triggeredTimer = i;
timer[i].funct(timer[i].this_ptr);
triggeredTimer = 0;
@ -439,9 +440,9 @@ Bit64u bx_pc_system_c::time_usec_sequential()
Bit64u diff_usec = this_time_usec-lastTimeUsec;
lastTimeUsec = this_time_usec;
if(diff_usec >= usecSinceLast) {
usecSinceLast = 0;
usecSinceLast = 0;
} else {
usecSinceLast -= diff_usec;
usecSinceLast -= diff_usec;
}
}
usecSinceLast++;
@ -504,8 +505,7 @@ void bx_pc_system_c::activate_timer(unsigned i, Bit32u useconds, bx_bool continu
// else set new period from useconds
if (useconds==0) {
ticks = timer[i].period;
}
else {
} else {
// convert useconds to number of ticks
ticks = (Bit64u) (double(useconds) * m_ips);
@ -548,7 +548,7 @@ bx_bool bx_pc_system_c::unregisterTimer(unsigned timerIndex)
if (timer[timerIndex].active) {
BX_PANIC(("unregisterTimer: timer '%s' is still active!", timer[timerIndex].id));
return(0); // Fail.
return 0; // Fail.
}
// Reset timer fields for good measure.
@ -560,12 +560,12 @@ bx_bool bx_pc_system_c::unregisterTimer(unsigned timerIndex)
timer[timerIndex].this_ptr = NULL;
memset(timer[timerIndex].id, 0, BxMaxTimerIDLen);
if (timerIndex == (numTimers-1)) numTimers--;
if (timerIndex == (numTimers - 1)) numTimers--;
return(1); // OK
return 1; // OK
}
void bx_pc_system_c::setTimerParam(unsigned timerIndex, Bit8u param)
void bx_pc_system_c::setTimerParam(unsigned timerIndex, Bit32u param)
{
#if BX_TIMER_DEBUG
if (timerIndex >= numTimers)

View File

@ -53,7 +53,7 @@ private:
// has to be stored as well.
#define BxMaxTimerIDLen 32
char id[BxMaxTimerIDLen]; // String ID of timer.
Bit8u param; // Device-specific value assigned to timer (optional)
Bit32u param; // Device-specific value assigned to timer (optional)
} timer[BX_MAX_TIMERS];
unsigned numTimers; // Number of currently allocated timers.
@ -92,14 +92,14 @@ public:
int register_timer(void *this_ptr, bx_timer_handler_t, Bit32u useconds,
bx_bool continuous, bx_bool active, const char *id);
bx_bool unregisterTimer(unsigned timerID);
void setTimerParam(unsigned timerID, Bit8u param);
void setTimerParam(unsigned timerID, Bit32u param);
void start_timers(void);
void activate_timer(unsigned timer_index, Bit32u useconds, bx_bool continuous);
void deactivate_timer(unsigned timer_index);
unsigned triggeredTimerID(void) {
return triggeredTimer;
}
Bit8u triggeredTimerParam(void) {
Bit32u triggeredTimerParam(void) {
return timer[triggeredTimer].param;
}
static BX_CPP_INLINE void tick1(void) {