4 * Copyright 1996 Alexandre Julliard
7 #include "wine/winbase16.h"
8 #include "wine/winuser16.h"
11 #include "stackframe.h"
12 #include "builtin16.h"
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(system)
19 SYSTEMTIMERPROC callback; /* NULL if not in use */
24 #define NB_SYS_TIMERS 8
25 #define SYS_TIMER_RATE 54925
27 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
28 static int SYS_NbTimers = 0;
29 static HANDLE SYS_Service = INVALID_HANDLE_VALUE;
32 /***********************************************************************
35 static void CALLBACK SYSTEM_TimerTick( ULONG_PTR arg )
39 for (i = 0; i < NB_SYS_TIMERS; i++)
41 if (!SYS_Timers[i].callback) continue;
42 if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
44 SYS_Timers[i].ticks += SYS_Timers[i].rate;
45 SYS_Timers[i].callback( i+1 );
50 /**********************************************************************
53 * Start the system tick timer.
55 static void SYSTEM_StartTicks(void)
57 if ( SYS_Service == INVALID_HANDLE_VALUE )
58 SYS_Service = SERVICE_AddTimer( SYS_TIMER_RATE, SYSTEM_TimerTick, 0L );
62 /**********************************************************************
65 * Stop the system tick timer.
67 static void SYSTEM_StopTicks(void)
69 if ( SYS_Service != INVALID_HANDLE_VALUE )
71 SERVICE_Delete( SYS_Service );
72 SYS_Service = INVALID_HANDLE_VALUE;
77 /***********************************************************************
78 * InquireSystem (SYSTEM.1)
80 * Note: the function always takes 2 WORD arguments, contrary to what
81 * "Undocumented Windows" says.
83 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
89 case 0: /* Get timer resolution */
90 return SYS_TIMER_RATE;
92 case 1: /* Get drive type */
93 drivetype = GetDriveType16( arg );
94 return MAKELONG( drivetype, drivetype );
96 case 2: /* Enable one-drive logic */
97 FIXME("Case %d: set single-drive %d not supported\n", code, arg );
100 WARN("Unknown code %d\n", code );
105 /***********************************************************************
106 * CreateSystemTimer (SYSTEM.2)
108 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
111 for (i = 0; i < NB_SYS_TIMERS; i++)
112 if (!SYS_Timers[i].callback) /* Found one */
114 SYS_Timers[i].rate = (UINT)rate * 1000;
115 if (SYS_Timers[i].rate < SYS_TIMER_RATE)
116 SYS_Timers[i].rate = SYS_TIMER_RATE;
117 SYS_Timers[i].ticks = SYS_Timers[i].rate;
118 SYS_Timers[i].callback = callback;
119 if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
120 return i + 1; /* 0 means error */
125 static void SYSTEM_CallSystemTimerProc( FARPROC16 proc, WORD timer )
128 memset( &context, '\0', sizeof(context) );
130 CS_reg( &context ) = SELECTOROF( proc );
131 EIP_reg( &context ) = OFFSETOF( proc );
132 EBP_reg( &context ) = OFFSETOF( NtCurrentTeb()->cur_stack )
133 + (WORD)&((STACK16FRAME*)0)->bp;
135 AX_reg( &context ) = timer;
137 CallTo16RegisterShort( &context, 0 );
140 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
142 FARPROC thunk = THUNK_Alloc( proc, (RELAY)SYSTEM_CallSystemTimerProc );
143 WORD timer = CreateSystemTimer( rate, (SYSTEMTIMERPROC)thunk );
144 if (!timer) THUNK_Free( thunk );
149 /***********************************************************************
150 * KillSystemTimer (SYSTEM.3)
152 * Note: do not confuse this function with USER.182
154 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
156 if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback )
157 return timer; /* Error */
159 THUNK_Free( (FARPROC)SYS_Timers[timer-1].callback );
160 SYS_Timers[timer-1].callback = NULL;
162 if (!--SYS_NbTimers) SYSTEM_StopTicks();
167 /***********************************************************************
168 * EnableSystemTimers (SYSTEM.4)
170 void WINAPI EnableSystemTimers16(void)
172 if ( SYS_Service != INVALID_HANDLE_VALUE )
173 SERVICE_Enable( SYS_Service );
177 /***********************************************************************
178 * DisableSystemTimers (SYSTEM.5)
180 void WINAPI DisableSystemTimers16(void)
182 if ( SYS_Service != INVALID_HANDLE_VALUE )
183 SERVICE_Disable( SYS_Service );
187 /***********************************************************************
188 * Get80x87SaveSize16 (SYSTEM.7)
190 WORD WINAPI Get80x87SaveSize16(void)
196 /***********************************************************************
197 * Save80x87State16 (SYSTEM.8)
199 void WINAPI Save80x87State16( char *ptr )
202 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
207 /***********************************************************************
208 * Restore80x87State16 (SYSTEM.9)
210 void WINAPI Restore80x87State16( const char *ptr )
213 __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );