OpenCores
URL https://opencores.org/ocsvn/ao486/ao486/trunk

Subversion Repositories ao486

[/] [ao486/] [trunk/] [bochsDevs/] [iodev/] [virt_timer.cc] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
////////////////////////////////////////////////////////////////////////
2
// $Id: virt_timer.cc 11183 2012-05-17 09:11:48Z vruppert $
3
/////////////////////////////////////////////////////////////////////////
4
//
5
//  Copyright (C) 2002-2009  The Bochs Project
6
//
7
//  This library is free software; you can redistribute it and/or
8
//  modify it under the terms of the GNU Lesser General Public
9
//  License as published by the Free Software Foundation; either
10
//  version 2 of the License, or (at your option) any later version.
11
//
12
//  This library 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 GNU
15
//  Lesser General Public License for more details.
16
//
17
//  You should have received a copy of the GNU Lesser General Public
18
//  License along with this library; if not, write to the Free Software
19
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20
////////////////////////////////////////////////////////////////////////
21
 
22
/////////////////////////////////////////////////////////////////////////
23
//
24
//Realtime Algorithm (with gettimeofday)
25
//  HAVE:
26
//    Real number of usec.
27
//    Emulated number of usec.
28
//  WANT:
29
//    Number of ticks to use.
30
//    Number of emulated usec to wait until next try.
31
//
32
//  ticks=number of ticks needed to match total real usec.
33
//  if(desired ticks > max ticks for elapsed real time)
34
//     ticks = max ticks for elapsed real time.
35
//  if(desired ticks > max ticks for elapsed emulated usec)
36
//     ticks = max ticks for emulated usec.
37
//  next wait ticks = number of ticks until next event.
38
//  next wait real usec = (current ticks + next wait ticks) * usec per ticks
39
//  next wait emulated usec = next wait real usec * emulated usec / real usec
40
//  if(next wait emulated usec < minimum emulated usec for next wait ticks)
41
//     next wait emulated usec = minimum emulated usec for next wait ticks.
42
//  if(next wait emulated usec > max emulated usec wait)
43
//     next wait emulated usec = max emulated usec wait.
44
//
45
//  How to calculate elapsed real time:
46
//    store an unused time value whenever no ticks are used in a given time.
47
//    add this to the current elapsed time.
48
//  How to calculate elapsed emulated time:
49
//    same as above.
50
//  Above can be done by not updating last_usec and last_sec.
51
//
52
//  How to calculate emulated usec/real usec:
53
//    Each time there are actual ticks:
54
//      Alpha_product(old emulated usec, emulated usec);
55
//      Alpha_product(old real usec, real usec);
56
//    Divide resulting values.
57
//
58
/////////////////////////////////////////////////////////////////////////
59
 
60
#include "bochs.h"
61
#include "param_names.h"
62
#include "virt_timer.h"
63
 
64
//Important constant #defines:
65
#define USEC_PER_SECOND (1000000)
66
 
67
 
68
// define a macro to convert floating point numbers into 64-bit integers.
69
// In MSVC++ you can convert a 64-bit float into a 64-bit signed integer,
70
// but it will not convert a 64-bit float into a 64-bit unsigned integer.
71
// This macro works around that.
72
#define F2I(x)  ((Bit64u)(Bit64s) (x))
73
#define I2F(x)  ((double)(Bit64s) (x))
74
 
75
//CONFIGURATION #defines:
76
 
77
 
78
//MAINLINE Configuration (For realtime PIT):
79
 
80
//How much faster than real time we can go:
81
#define MAX_MULT (1.25)
82
 
83
//Minimum number of emulated useconds per second.
84
//  Now calculated using BX_MIN_IPS, the minimum number of
85
//   instructions per second.
86
#define MIN_USEC_PER_SECOND (((((Bit64u)USEC_PER_SECOND)*((Bit64u)BX_MIN_IPS))/((Bit64u)ips))+(Bit64u)1)
87
 
88
 
89
//DEBUG configuration:
90
 
91
//Debug with printf options.
92
#define DEBUG_REALTIME_WITH_PRINTF 0
93
 
94
 
95
#define GET_VIRT_REALTIME64_USEC() (bx_get_realtime64_usec())
96
//Set up Logging.
97
#define LOG_THIS bx_virt_timer.
98
 
99
//A single instance.
100
bx_virt_timer_c bx_virt_timer;
101
 
102
 
103
//USEC_ALPHA is multiplier for the past.
104
//USEC_ALPHA_B is 1-USEC_ALPHA, or multiplier for the present.
105
#define USEC_ALPHA ((double)(.8))
106
#define USEC_ALPHA_B ((double)(((double)1)-USEC_ALPHA))
107
#define USEC_ALPHA2 ((double)(.5))
108
#define USEC_ALPHA2_B ((double)(((double)1)-USEC_ALPHA2))
109
#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))))))
110
 
111
 
112
//Conversion between emulated useconds and optionally realtime ticks.
113
#define TICKS_TO_USEC(a) (((a)*usec_per_second)/ticks_per_second)
114
#define USEC_TO_TICKS(a) (((a)*ticks_per_second)/usec_per_second)
115
 
116
bx_virt_timer_c::bx_virt_timer_c()
117
{
118
  put("virt_timer", "VTIME");
119
 
120
  setup();
121
}
122
 
123
const Bit64u bx_virt_timer_c::NullTimerInterval = BX_MAX_VIRTUAL_TIME;
124
 
125
void bx_virt_timer_c::nullTimer(void* this_ptr)
126
{
127
  UNUSED(this_ptr);
128
}
129
 
130
void bx_virt_timer_c::periodic(Bit64u time_passed)
131
{
132
  //Assert that we haven't skipped any events.
133
  BX_ASSERT (time_passed <= timers_next_event_time);
134
  BX_ASSERT(!in_timer_handler);
135
 
136
  //Update time variables.
137
  timers_next_event_time -= time_passed;
138
  current_timers_time += time_passed;
139
 
140
  //If no events are occurring, just pass the time and we're done.
141
  if (time_passed < timers_next_event_time) return;
142
 
143
  //Starting timer handler calls.
144
  in_timer_handler = 1;
145
  //Otherwise, cause any events to occur that should.
146
  unsigned i;
147
  for (i=0;i<numTimers;i++) {
148
    if (timer[i].inUse && timer[i].active) {
149
      //Assert that we haven't skipped any timers.
150
      BX_ASSERT(current_timers_time <= timer[i].timeToFire);
151
      if (timer[i].timeToFire == current_timers_time) {
152
        if (timer[i].continuous) {
153
          timer[i].timeToFire += timer[i].period;
154
        } else {
155
          timer[i].active = 0;
156
        }
157
        //This function MUST return, or the timer mechanism
158
        // will be broken.
159
        timer[i].funct(timer[i].this_ptr);
160
      }
161
    }
162
  }
163
  //Finished timer handler calls.
164
  in_timer_handler = 0;
165
  //Use a second FOR loop so that a timer function call can
166
  //  change the behavior of another timer.
167
  //timers_next_event_time normally contains a cycle count, not a cycle time.
168
  //  here we use it as a temporary variable that IS a cycle time,
169
  //  but then convert it back to a cycle count afterwards.
170
  timers_next_event_time = current_timers_time + BX_MAX_VIRTUAL_TIME;
171
  for (i=0;i<numTimers;i++) {
172
    if (timer[i].inUse && timer[i].active && ((timer[i].timeToFire)<timers_next_event_time)) {
173
      timers_next_event_time = timer[i].timeToFire;
174
    }
175
  }
176
  timers_next_event_time -= current_timers_time;
177
  next_event_time_update();
178
  //FIXME
179
}
180
 
181
 
182
//Get the current virtual time.
183
//  This may return the same value on subsequent calls.
184
Bit64u bx_virt_timer_c::time_usec()
185
{
186
  //Update the time here only if we're not in a timer handler.
187
  //If we're in a timer handler we're up-to-date, and otherwise
188
  // this prevents call stack loops.
189
  if (!in_timer_handler) {
190
    timer_handler();
191
  }
192
  return current_timers_time;
193
}
194
 
195
//Get the current virtual time.
196
//  This will return a monotonically increasing value.
197
// MUST NOT be called from within a timer interrupt.
198
Bit64u bx_virt_timer_c::time_usec_sequential(void)
199
{
200
  //Can't prevent call stack loops here, so this
201
  // MUST NOT be called from within a timer handler.
202
  BX_ASSERT(timers_next_event_time>0);
203
  BX_ASSERT(!in_timer_handler);
204
 
205
  if (last_sequential_time >= current_timers_time) {
206
    periodic(1);
207
    last_sequential_time = current_timers_time;
208
  }
209
  return current_timers_time;
210
}
211
 
212
 
213
//Register a timer handler to go off after a given interval.
214
//Register a timer handler to go off with a periodic interval.
215
int bx_virt_timer_c::register_timer(void *this_ptr, bx_timer_handler_t handler,
216
                                    Bit32u useconds,
217
                                    bx_bool continuous, bx_bool active,
218
                                    const char *id)
219
{
220
  //We don't like starting with a zero period timer.
221
  BX_ASSERT((!active) || (useconds>0));
222
 
223
  //Search for an unused timer.
224
  unsigned int i;
225
  for (i=0; i < numTimers; i++) {
226
    if ((timer[i].inUse == 0) || (i == numTimers))
227
      break;
228
  }
229
  // If we didn't find a free slot, increment the bound, numTimers.
230
  if (i == numTimers)
231
    numTimers++; // One new timer installed.
232
  BX_ASSERT(numTimers<BX_MAX_VIRTUAL_TIMERS);
233
 
234
  timer[i].inUse = 1;
235
  timer[i].period = useconds;
236
  timer[i].timeToFire = current_timers_time + (Bit64u)useconds;
237
  timer[i].active = active;
238
  timer[i].continuous = continuous;
239
  timer[i].funct = handler;
240
  timer[i].this_ptr = this_ptr;
241
  strncpy(timer[i].id, id, BxMaxTimerIDLen);
242
  timer[i].id[BxMaxTimerIDLen-1]=0; //I like null terminated strings.
243
 
244
  if (useconds < timers_next_event_time) {
245
    timers_next_event_time = useconds;
246
    next_event_time_update();
247
    //FIXME
248
  }
249
  return i;
250
}
251
 
252
//unregister a previously registered timer.
253
bx_bool bx_virt_timer_c::unregisterTimer(unsigned timerID)
254
{
255
  BX_ASSERT(timerID < BX_MAX_VIRTUAL_TIMERS);
256
 
257
  if (timer[timerID].active) {
258
    BX_PANIC(("unregisterTimer: timer '%s' is still active!", timer[timerID].id));
259
    return(0); // Fail.
260
  }
261
 
262
  //No need to prevent doing this to unused timers.
263
  timer[timerID].inUse = 0;
264
  if (timerID == (numTimers-1)) numTimers--;
265
  return 1;
266
}
267
 
268
void bx_virt_timer_c::start_timers(void)
269
{
270
  //FIXME
271
}
272
 
273
//activate a deactivated but registered timer.
274
void bx_virt_timer_c::activate_timer(unsigned timer_index, Bit32u useconds,
275
                                     bx_bool continuous)
276
{
277
  BX_ASSERT(timer_index < BX_MAX_VIRTUAL_TIMERS);
278
 
279
  BX_ASSERT(timer[timer_index].inUse);
280
  BX_ASSERT(useconds>0);
281
 
282
  timer[timer_index].period = useconds;
283
  timer[timer_index].timeToFire = current_timers_time + (Bit64u)useconds;
284
  timer[timer_index].active = 1;
285
  timer[timer_index].continuous = continuous;
286
 
287
  if (useconds < timers_next_event_time) {
288
    timers_next_event_time = useconds;
289
    next_event_time_update();
290
    //FIXME
291
  }
292
}
293
 
294
//deactivate (but don't unregister) a currently registered timer.
295
void bx_virt_timer_c::deactivate_timer(unsigned timer_index)
296
{
297
  BX_ASSERT(timer_index < BX_MAX_VIRTUAL_TIMERS);
298
 
299
  //No need to prevent doing this to unused/inactive timers.
300
  timer[timer_index].active = 0;
301
}
302
 
303
void bx_virt_timer_c::advance_virtual_time(Bit64u time_passed)
304
{
305
  BX_ASSERT(time_passed <= virtual_next_event_time);
306
 
307
  current_virtual_time += time_passed;
308
  virtual_next_event_time -= time_passed;
309
 
310
  if (current_virtual_time > current_timers_time) {
311
    periodic(current_virtual_time - current_timers_time);
312
  }
313
}
314
 
315
//Called when next_event_time changes.
316
void bx_virt_timer_c::next_event_time_update(void)
317
{
318
  virtual_next_event_time = timers_next_event_time + current_timers_time - current_virtual_time;
319
  if (init_done) {
320
    bx_pc_system.deactivate_timer(system_timer_id);
321
    BX_ASSERT(virtual_next_event_time);
322
    bx_pc_system.activate_timer(system_timer_id,
323
                                (Bit32u)BX_MIN(0x7FFFFFFF,BX_MAX(1,TICKS_TO_USEC(virtual_next_event_time))),
324
                                0);
325
  }
326
}
327
 
328
void bx_virt_timer_c::setup(void)
329
{
330
  numTimers = 0;
331
  current_timers_time = 0;
332
  timers_next_event_time = BX_MAX_VIRTUAL_TIME;
333
  last_sequential_time = 0;
334
  in_timer_handler = 0;
335
  virtual_next_event_time = BX_MAX_VIRTUAL_TIME;
336
  current_virtual_time = 0;
337
 
338
  init_done = 0;
339
}
340
 
341
void bx_virt_timer_c::init(void)
342
{
343
  if ((SIM->get_param_enum(BXPN_CLOCK_SYNC)->get()!=BX_CLOCK_SYNC_REALTIME) &&
344
      (SIM->get_param_enum(BXPN_CLOCK_SYNC)->get()!=BX_CLOCK_SYNC_BOTH))
345
    virtual_timers_realtime = 0;
346
  else
347
    virtual_timers_realtime = 1;
348
 
349
  if (virtual_timers_realtime) {
350
    BX_INFO(("using 'realtime pit' synchronization method"));
351
  }
352
 
353
  // Local copy of IPS value to avoid reading it frequently in timer handler
354
  ips = SIM->get_param_num(BXPN_IPS)->get();
355
 
356
  register_timer(this, nullTimer, (Bit32u)NullTimerInterval, 1, 1, "Null Timer");
357
 
358
  system_timer_id = bx_pc_system.register_timer(this, pc_system_timer_handler,
359
                                                (Bit32u)virtual_next_event_time, 0, 1, "Virtual Timer");
360
 
361
  //Real time variables:
362
#if BX_HAVE_REALTIME_USEC
363
  last_real_time = GET_VIRT_REALTIME64_USEC();
364
#endif
365
  total_real_usec = 0;
366
  last_realtime_delta = 0;
367
  real_time_delay = 0;
368
  //System time variables:
369
  last_usec = 0;
370
  usec_per_second = USEC_PER_SECOND;
371
  stored_delta = 0;
372
  last_system_usec = 0;
373
  em_last_realtime = 0;
374
  //Virtual timer variables:
375
  total_ticks = 0;
376
  last_realtime_ticks = 0;
377
  ticks_per_second = USEC_PER_SECOND;
378
 
379
  init_done = 1;
380
}
381
 
382
void bx_virt_timer_c::register_state(void)
383
{
384
  bx_list_c *list = new bx_list_c(SIM->get_bochs_root(), "virt_timer", "Virtual Timer State");
385
  bx_list_c *vtimers = new bx_list_c(list, "timer");
386
  for (unsigned i = 0; i < numTimers; i++) {
387
    char name[4];
388
    sprintf(name, "%d", i);
389
    bx_list_c *bxtimer = new bx_list_c(vtimers, name);
390
    BXRS_PARAM_BOOL(bxtimer, inUse, timer[i].inUse);
391
    BXRS_DEC_PARAM_FIELD(bxtimer, period, timer[i].period);
392
    BXRS_DEC_PARAM_FIELD(bxtimer, timeToFire, timer[i].timeToFire);
393
    BXRS_PARAM_BOOL(bxtimer, active, timer[i].active);
394
    BXRS_PARAM_BOOL(bxtimer, continuous, timer[i].continuous);
395
  }
396
  BXRS_DEC_PARAM_SIMPLE(list, current_timers_time);
397
  BXRS_DEC_PARAM_SIMPLE(list, timers_next_event_time);
398
  BXRS_DEC_PARAM_SIMPLE(list, last_sequential_time);
399
  BXRS_DEC_PARAM_SIMPLE(list, virtual_next_event_time);
400
  BXRS_DEC_PARAM_SIMPLE(list, current_virtual_time);
401
  BXRS_DEC_PARAM_SIMPLE(list, last_real_time);
402
  BXRS_DEC_PARAM_SIMPLE(list, total_real_usec);
403
  BXRS_DEC_PARAM_SIMPLE(list, last_realtime_delta);
404
  BXRS_DEC_PARAM_SIMPLE(list, last_usec);
405
  BXRS_DEC_PARAM_SIMPLE(list, usec_per_second);
406
  BXRS_DEC_PARAM_SIMPLE(list, stored_delta);
407
  BXRS_DEC_PARAM_SIMPLE(list, last_system_usec);
408
  BXRS_DEC_PARAM_SIMPLE(list, em_last_realtime);
409
  BXRS_DEC_PARAM_SIMPLE(list, total_ticks);
410
  BXRS_DEC_PARAM_SIMPLE(list, last_realtime_ticks);
411
  BXRS_DEC_PARAM_SIMPLE(list, ticks_per_second);
412
 
413
}
414
 
415
void bx_virt_timer_c::timer_handler(void)
416
{
417
  if (!virtual_timers_realtime) {
418
    Bit64u temp_final_time = bx_pc_system.time_usec();
419
    temp_final_time -= current_virtual_time;
420
    while (temp_final_time) {
421
      if ((temp_final_time)>(virtual_next_event_time)) {
422
        temp_final_time -= virtual_next_event_time;
423
        advance_virtual_time(virtual_next_event_time);
424
      } else {
425
        advance_virtual_time(temp_final_time);
426
        temp_final_time -= temp_final_time;
427
      }
428
    }
429
    bx_pc_system.activate_timer(system_timer_id,
430
                                (Bit32u)BX_MIN(0x7FFFFFFF,(virtual_next_event_time>2)?(virtual_next_event_time-2):1),
431
                                0);
432
    return;
433
  }
434
 
435
  Bit64u usec_delta = bx_pc_system.time_usec()-last_usec;
436
 
437
  if (usec_delta) {
438
#if BX_HAVE_REALTIME_USEC
439
    Bit64u ticks_delta = 0;
440
    Bit64u real_time_delta = GET_VIRT_REALTIME64_USEC() - last_real_time - real_time_delay;
441
    Bit64u real_time_total = real_time_delta + total_real_usec;
442
    Bit64u system_time_delta = (Bit64u)usec_delta + (Bit64u)stored_delta;
443
    if (real_time_delta) {
444
      last_realtime_delta = real_time_delta;
445
      last_realtime_ticks = total_ticks;
446
    }
447
    ticks_per_second = USEC_PER_SECOND;
448
 
449
    //Start out with the number of ticks we would like
450
    // to have to line up with real time.
451
    ticks_delta = real_time_total - total_ticks;
452
    if (real_time_total < total_ticks) {
453
      //This slows us down if we're already ahead.
454
      //  probably only an issue on startup, but it solves some problems.
455
      ticks_delta = 0;
456
    }
457
    if (ticks_delta + total_ticks - last_realtime_ticks > (F2I(MAX_MULT * I2F(last_realtime_delta)))) {
458
      //This keeps us from going too fast in relation to real time.
459
#if 0
460
      ticks_delta = (F2I(MAX_MULT * I2F(last_realtime_delta))) + last_realtime_ticks - total_ticks;
461
#endif
462
      ticks_per_second = F2I(MAX_MULT * I2F(USEC_PER_SECOND));
463
    }
464
    if (ticks_delta > system_time_delta * USEC_PER_SECOND / MIN_USEC_PER_SECOND) {
465
      //This keeps us from having too few instructions between ticks.
466
      ticks_delta = system_time_delta * USEC_PER_SECOND / MIN_USEC_PER_SECOND;
467
    }
468
    if (ticks_delta > virtual_next_event_time) {
469
      //This keeps us from missing ticks.
470
      ticks_delta = virtual_next_event_time;
471
    }
472
 
473
    if (ticks_delta) {
474
 
475
#  if DEBUG_REALTIME_WITH_PRINTF
476
      //Every second print some info.
477
      if (((last_real_time + real_time_delta) / USEC_PER_SECOND) > (last_real_time / USEC_PER_SECOND)) {
478
        Bit64u temp1, temp2, temp3, temp4;
479
        temp1 = (Bit64u) total_real_usec;
480
        temp2 = (total_real_usec);
481
        temp3 = (Bit64u)total_ticks;
482
        temp4 = (Bit64u)((total_real_usec) - total_ticks);
483
        printf("useconds: " FMT_LL "u, ", temp1);
484
        printf("expect ticks: " FMT_LL "u, ", temp2);
485
        printf("ticks: " FMT_LL "u, ", temp3);
486
        printf("diff: "FMT_LL "u\n", temp4);
487
      }
488
#  endif
489
 
490
      last_real_time += real_time_delta;
491
      total_real_usec += real_time_delta;
492
      last_system_usec += system_time_delta;
493
      stored_delta = 0;
494
      total_ticks += ticks_delta;
495
    } else {
496
      stored_delta = system_time_delta;
497
    }
498
 
499
    Bit64u a = usec_per_second, b;
500
    if (real_time_delta) {
501
      //FIXME
502
      Bit64u em_realtime_delta = last_system_usec + stored_delta - em_last_realtime;
503
      b=((Bit64u)USEC_PER_SECOND * em_realtime_delta / real_time_delta);
504
      em_last_realtime = last_system_usec + stored_delta;
505
    } else {
506
      b=a;
507
    }
508
    usec_per_second = ALPHA_LOWER(a,b);
509
#else
510
    BX_ASSERT(0);
511
#endif
512
#if BX_HAVE_REALTIME_USEC
513
    advance_virtual_time(ticks_delta);
514
#endif
515
  }
516
 
517
  last_usec=last_usec + usec_delta;
518
  bx_pc_system.deactivate_timer(system_timer_id);
519
  BX_ASSERT(virtual_next_event_time);
520
  bx_pc_system.activate_timer(system_timer_id,
521
                              (Bit32u)BX_MIN(0x7FFFFFFF,BX_MAX(1,TICKS_TO_USEC(virtual_next_event_time))),
522
                              0);
523
}
524
 
525
void bx_virt_timer_c::pc_system_timer_handler(void* this_ptr)
526
{
527
  ((bx_virt_timer_c *)this_ptr)->timer_handler();
528
}
529
 
530
void bx_virt_timer_c::set_realtime_delay()
531
{
532
  if (virtual_timers_realtime) {
533
    real_time_delay = GET_VIRT_REALTIME64_USEC() - last_real_time;
534
  }
535
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.