- added instructions how to update the online documentation
[bochs-mirror.git] / iodev / virt_timer.h
blob583455fb3c2b9c17a023d361cf4dced9f489ccd5
1 ////////////////////////////////////////////////////////////////////////
2 // $Id: virt_timer.h,v 1.15 2008/02/15 22:05:43 sshwarts Exp $
3 /////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (C) 2002 MandrakeSoft S.A.
6 //
7 // MandrakeSoft S.A.
8 // 43, rue d'Aboukir
9 // 75002 Paris - France
10 // http://www.linux-mandrake.com/
11 // http://www.mandrakesoft.com/
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2 of the License, or (at your option) any later version.
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /////////////////////////////////////////////////////////////////////////
28 #ifndef _BX_VIRT_TIMER_H
29 #define _BX_VIRT_TIMER_H
31 // should be adjusted if want to support more SMP processors
32 #define BX_MAX_VIRTUAL_TIMERS (32)
33 #define BX_NULL_VIRTUAL_TIMER_HANDLE 10000
35 #define BX_MAX_VIRTUAL_TIME (0x7fffffff)
37 class bx_virt_timer_c : public logfunctions {
38 private:
40 struct {
41 bx_bool inUse; // Timer slot is in-use (currently registered).
42 Bit64u period; // Timer periodocity in virtual useconds.
43 Bit64u timeToFire; // Time to fire next (in virtual useconds).
44 bx_bool active; // 0=inactive, 1=active.
45 bx_bool continuous; // 0=one-shot timer, 1=continuous periodicity.
46 bx_timer_handler_t funct; // A callback function for when the
47 // timer fires.
48 // This function MUST return.
49 void *this_ptr; // The this-> pointer for C++ callbacks
50 // has to be stored as well.
51 char id[BxMaxTimerIDLen]; // String ID of timer.
52 } timer[BX_MAX_VIRTUAL_TIMERS];
54 unsigned numTimers; // Number of currently allocated timers.
56 //Variables for the timer subsystem:
57 Bit64u current_timers_time;
58 Bit64u timers_next_event_time;
60 Bit64u last_sequential_time;
61 bx_bool in_timer_handler;
63 //Variables for the time sync subsystem:
64 Bit64u virtual_next_event_time;
65 Bit64u current_virtual_time;
67 //Real time variables:
68 Bit64u last_real_time;
69 Bit64u total_real_usec;
70 Bit64u last_realtime_delta;
71 //System time variables:
72 Bit64u last_usec;
73 Bit64u usec_per_second;
74 Bit64u stored_delta;
75 Bit64u last_system_usec;
76 Bit64u em_last_realtime;
77 //Virtual timer variables:
78 Bit64u total_ticks;
79 Bit64u last_realtime_ticks;
80 Bit64u ticks_per_second;
82 bx_bool init_done;
84 int system_timer_id;
86 //Whether or not to use virtual timers.
87 bx_bool use_virtual_timers;
88 bx_bool virtual_timers_realtime;
90 // A special null timer is always inserted in the timer[0] slot. This
91 // make sure that at least one timer is always active, and that the
92 // duration is always less than a maximum 32-bit integer, so a 32-bit
93 // counter can be used for the current countdown.
94 static const Bit64u NullTimerInterval;
95 static void nullTimer(void* this_ptr);
97 //Step the given number of cycles, optionally calling any timer handlers.
98 void periodic(Bit64u time_passed);
100 //Called when next_event_time changes.
101 void next_event_time_update(void);
103 //Called to advance the virtual time.
104 // calls periodic as needed.
105 void advance_virtual_time(Bit64u time_passed);
107 public:
109 bx_virt_timer_c();
110 virtual ~bx_virt_timer_c() {}
112 //Get the current virtual time.
113 // This may return the same value on subsequent calls.
114 Bit64u time_usec(void);
116 //Get the current virtual time.
117 // This will return a monotonically increasing value.
118 // MUST NOT be called from within a timer handler.
119 Bit64u time_usec_sequential(void);
121 //Register a timer handler to go off after a given interval.
122 //Register a timer handler to go off with a periodic interval.
123 int register_timer(void *this_ptr, bx_timer_handler_t handler,
124 Bit32u useconds,
125 bx_bool continuous, bx_bool active, const char *id);
127 //unregister a previously registered timer.
128 bx_bool unregisterTimer(unsigned timerID);
130 void start_timers(void);
132 //activate a deactivated but registered timer.
133 void activate_timer(unsigned timer_index, Bit32u useconds,
134 bx_bool continuous);
136 //deactivate (but don't unregister) a currently registered timer.
137 void deactivate_timer(unsigned timer_index);
140 //Timer handler passed to pc_system
141 static void pc_system_timer_handler(void* this_ptr);
143 //The real timer handler.
144 void timer_handler();
146 //Initialization step #1 in constructor and for cleanup
147 void setup(void);
149 //Initialization step #2
150 void init(void);
152 void register_state(void);
155 extern bx_virt_timer_c bx_virt_timer;
157 #endif // _BX_VIRT_TIMER_H