4 * Copyright 1996 Alexandre Julliard
9 #include "wine/winbase16.h"
10 #include "wine/winuser16.h"
13 #include "stackframe.h"
14 #include "builtin16.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(system);
21 SYSTEMTIMERPROC callback; /* NULL if not in use */
26 #define NB_SYS_TIMERS 8
27 #define SYS_TIMER_RATE 54925
29 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
30 static int SYS_NbTimers = 0;
31 static HANDLE SYS_Service = INVALID_HANDLE_VALUE;
34 /***********************************************************************
37 static void CALLBACK SYSTEM_TimerTick( ULONG_PTR arg )
41 for (i = 0; i < NB_SYS_TIMERS; i++)
43 if (!SYS_Timers[i].callback) continue;
44 if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
46 SYS_Timers[i].ticks += SYS_Timers[i].rate;
47 SYS_Timers[i].callback( i+1 );
52 /**********************************************************************
55 * Start the system tick timer.
57 static void SYSTEM_StartTicks(void)
59 if ( SYS_Service == INVALID_HANDLE_VALUE )
60 SYS_Service = SERVICE_AddTimer( (SYS_TIMER_RATE+500)/1000, SYSTEM_TimerTick, 0L );
64 /**********************************************************************
67 * Stop the system tick timer.
69 static void SYSTEM_StopTicks(void)
71 if ( SYS_Service != INVALID_HANDLE_VALUE )
73 SERVICE_Delete( SYS_Service );
74 SYS_Service = INVALID_HANDLE_VALUE;
79 /***********************************************************************
80 * InquireSystem (SYSTEM.1)
82 * Note: the function always takes 2 WORD arguments, contrary to what
83 * "Undocumented Windows" says.
85 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
91 case 0: /* Get timer resolution */
92 return SYS_TIMER_RATE;
94 case 1: /* Get drive type */
95 drivetype = GetDriveType16( arg );
96 return MAKELONG( drivetype, drivetype );
98 case 2: /* Enable one-drive logic */
99 FIXME("Case %d: set single-drive %d not supported\n", code, arg );
102 WARN("Unknown code %d\n", code );
107 /***********************************************************************
108 * CreateSystemTimer (SYSTEM.2)
110 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
113 for (i = 0; i < NB_SYS_TIMERS; i++)
114 if (!SYS_Timers[i].callback) /* Found one */
116 SYS_Timers[i].rate = (UINT)rate * 1000;
117 if (SYS_Timers[i].rate < SYS_TIMER_RATE)
118 SYS_Timers[i].rate = SYS_TIMER_RATE;
119 SYS_Timers[i].ticks = SYS_Timers[i].rate;
120 SYS_Timers[i].callback = callback;
121 if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
122 return i + 1; /* 0 means error */
127 /**********************************************************************/
129 static void SYSTEM_CallSystemTimerProc( FARPROC16 proc, WORD timer )
132 memset( &context, '\0', sizeof(context) );
134 context.SegCs = SELECTOROF( proc );
135 context.Eip = OFFSETOF( proc );
136 context.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
137 + (WORD)&((STACK16FRAME*)0)->bp;
139 AX_reg( &context ) = timer;
141 wine_call_to_16_regs_short( &context, 0 );
144 /**********************************************************************/
146 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
148 FARPROC thunk = THUNK_Alloc( proc, (RELAY)SYSTEM_CallSystemTimerProc );
149 WORD timer = CreateSystemTimer( rate, (SYSTEMTIMERPROC)thunk );
150 if (!timer) THUNK_Free( thunk );
155 /***********************************************************************
156 * KillSystemTimer (SYSTEM.3)
158 * Note: do not confuse this function with USER.182
160 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
162 if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback )
163 return timer; /* Error */
165 THUNK_Free( (FARPROC)SYS_Timers[timer-1].callback );
166 SYS_Timers[timer-1].callback = NULL;
168 if (!--SYS_NbTimers) SYSTEM_StopTicks();
173 /***********************************************************************
174 * EnableSystemTimers (SYSTEM.4)
176 void WINAPI EnableSystemTimers16(void)
178 if ( SYS_Service != INVALID_HANDLE_VALUE )
179 SERVICE_Enable( SYS_Service );
183 /***********************************************************************
184 * DisableSystemTimers (SYSTEM.5)
186 void WINAPI DisableSystemTimers16(void)
188 if ( SYS_Service != INVALID_HANDLE_VALUE )
189 SERVICE_Disable( SYS_Service );
193 /***********************************************************************
194 * Get80x87SaveSize16 (SYSTEM.7)
196 WORD WINAPI Get80x87SaveSize16(void)
202 /***********************************************************************
203 * Save80x87State16 (SYSTEM.8)
205 void WINAPI Save80x87State16( char *ptr )
208 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
213 /***********************************************************************
214 * Restore80x87State16 (SYSTEM.9)
216 void WINAPI Restore80x87State16( const char *ptr )
219 __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );