Coding style cleanup
[platform/kernel/u-boot.git] / board / MAI / bios_emulator / scitech / src / common / aalib.c
1 /****************************************************************************
2 *
3 *                     SciTech Nucleus Audio Architecture
4 *
5 *               Copyright (C) 1991-1998 SciTech Software, Inc.
6 *                            All rights reserved.
7 *
8 *  ======================================================================
9 *  |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
10 *  |                                                                    |
11 *  |This copyrighted computer code contains proprietary technology      |
12 *  |owned by SciTech Software, Inc., located at 505 Wall Street,        |
13 *  |Chico, CA 95928 USA (http://www.scitechsoft.com).                   |
14 *  |                                                                    |
15 *  |The contents of this file are subject to the SciTech Nucleus        |
16 *  |License; you may *not* use this file or related software except in  |
17 *  |compliance with the License. You may obtain a copy of the License   |
18 *  |at http://www.scitechsoft.com/nucleus-license.txt                   |
19 *  |                                                                    |
20 *  |Software distributed under the License is distributed on an         |
21 *  |"AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or      |
22 *  |implied. See the License for the specific language governing        |
23 *  |rights and limitations under the License.                           |
24 *  |                                                                    |
25 *  |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
26 *  ======================================================================
27 *
28 * Language:     ANSI C
29 * Environment:  Any 32-bit protected mode environment
30 *
31 * Description:  C module for the Graphics Accelerator Driver API. Uses
32 *               the SciTech PM library for interfacing with DOS
33 *               extender specific functions.
34 *
35 ****************************************************************************/
36
37 #include "nucleus/audio.h"
38 #ifdef __WIN32_VXD__
39 #include "sdd/sddhelp.h"
40 #else
41 #include <stdio.h>
42 #include <stdlib.h>
43 #endif
44
45 /*---------------------------- Global Variables ---------------------------*/
46
47 #ifdef  TEST_HARNESS
48 extern PM_imports   _VARAPI _PM_imports;
49 #else
50 AA_exports  _VARAPI _AA_exports;
51 static int          loaded = false;
52 static PE_MODULE    *hModBPD = NULL;
53
54 #ifdef  __DRIVER__
55 extern PM_imports _PM_imports;
56 #else
57 #include "pmimp.h"
58 #endif
59
60 static N_imports _N_imports = {
61     sizeof(N_imports),
62     _OS_delay,
63     };
64
65 #ifdef  __DRIVER__
66 extern AA_imports _AA_imports;
67 #else
68 static AA_imports _AA_imports = {
69     sizeof(AA_imports),
70     };
71 #endif
72 #endif
73
74 /*----------------------------- Implementation ----------------------------*/
75
76 #define DLL_NAME        "audio.bpd"
77
78 #ifndef TEST_HARNESS
79 /****************************************************************************
80 REMARKS:
81 Fatal error handler for non-exported AA_exports.
82 ****************************************************************************/
83 static void _AA_fatalErrorHandler(void)
84 {
85     PM_fatalError("Unsupported Nucleus export function called! Please upgrade your copy of Nucleus!\n");
86 }
87
88 /****************************************************************************
89 REMARKS:
90 Loads the Nucleus binary portable DLL into memory and initilises it.
91 ****************************************************************************/
92 static ibool LoadDriver(void)
93 {
94     AA_initLibrary_t    AA_initLibrary;
95     AA_exports          *aaExp;
96     char                filename[PM_MAX_PATH];
97     char                bpdpath[PM_MAX_PATH];
98     int                 i,max;
99     ulong               *p;
100
101     /* Check if we have already loaded the driver */
102     if (loaded)
103         return true;
104     PM_init();
105     _AA_exports.dwSize = sizeof(_AA_exports);
106
107     /* Open the BPD file */
108     if (!PM_findBPD(DLL_NAME,bpdpath))
109         return false;
110     strcpy(filename,bpdpath);
111     strcat(filename,DLL_NAME);
112     if ((hModBPD = PE_loadLibrary(filename,false)) == NULL)
113         return false;
114     if ((AA_initLibrary = (AA_initLibrary_t)PE_getProcAddress(hModBPD,"_AA_initLibrary")) == NULL)
115         return false;
116     bpdpath[strlen(bpdpath)-1] = 0;
117     if (strcmp(bpdpath,PM_getNucleusPath()) == 0)
118         strcpy(bpdpath,PM_getNucleusConfigPath());
119     else {
120         PM_backslash(bpdpath);
121         strcat(bpdpath,"config");
122         }
123     if ((aaExp = AA_initLibrary(bpdpath,filename,&_PM_imports,&_N_imports,&_AA_imports)) == NULL)
124         PM_fatalError("AA_initLibrary failed!\n");
125
126     /* Initialize all default imports to point to fatal error handler
127      * for upwards compatibility, and copy the exported functions.
128      */
129     max = sizeof(_AA_exports)/sizeof(AA_initLibrary_t);
130     for (i = 0,p = (ulong*)&_AA_exports; i < max; i++)
131         *p++ = (ulong)_AA_fatalErrorHandler;
132     memcpy(&_AA_exports,aaExp,MIN(sizeof(_AA_exports),aaExp->dwSize));
133     loaded = true;
134     return true;
135 }
136
137 /* The following are stub entry points that the application calls to
138  * initialise the Nucleus loader library, and we use this to load our
139  * driver DLL from disk and initialise the library using it.
140  */
141
142 /* {secret} */
143 int NAPI AA_status(void)
144 {
145     if (!loaded)
146         return nDriverNotFound;
147     return _AA_exports.AA_status();
148 }
149
150 /* {secret} */
151 const char * NAPI AA_errorMsg(
152     N_int32 status)
153 {
154     if (!loaded)
155         return "Unable to load Nucleus device driver!";
156     return _AA_exports.AA_errorMsg(status);
157 }
158
159 /* {secret} */
160 int NAPI AA_getDaysLeft(void)
161 {
162     if (!LoadDriver())
163         return -1;
164     return _AA_exports.AA_getDaysLeft();
165 }
166
167 /* {secret} */
168 int NAPI AA_registerLicense(uchar *license)
169 {
170     if (!LoadDriver())
171         return 0;
172     return _AA_exports.AA_registerLicense(license);
173 }
174
175 /* {secret} */
176 int NAPI AA_enumerateDevices(void)
177 {
178     if (!LoadDriver())
179         return 0;
180     return _AA_exports.AA_enumerateDevices();
181 }
182
183 /* {secret} */
184 AA_devCtx * NAPI AA_loadDriver(N_int32 deviceIndex)
185 {
186     if (!LoadDriver())
187         return NULL;
188     return _AA_exports.AA_loadDriver(deviceIndex);
189 }
190 #endif
191
192 typedef struct {
193     N_uint32    low;
194     N_uint32    high;
195     } AA_largeInteger;
196
197 void    NAPI _OS_delay8253(N_uint32 microSeconds);
198 ibool   NAPI _GA_haveCPUID(void);
199 uint    NAPI _GA_getCPUIDFeatures(void);
200 void    NAPI _GA_readTimeStamp(AA_largeInteger *time);
201 #define CPU_HaveRDTSC   0x00000010
202
203 /****************************************************************************
204 REMARKS:
205 This function delays for the specified number of microseconds
206 ****************************************************************************/
207 void NAPI _OS_delay(
208     N_uint32 microSeconds)
209 {
210     static ibool    inited = false;
211     LZTimerObject   tm;
212
213     if (_GA_haveCPUID() && (_GA_getCPUIDFeatures() & CPU_HaveRDTSC) != 0) {
214         if (!inited) {
215             ZTimerInit();
216             inited = true;
217             }
218         LZTimerOnExt(&tm);
219         while (LZTimerLapExt(&tm) < microSeconds)
220             ;
221         LZTimerOnExt(&tm);
222         }
223     else
224         _OS_delay8253(microSeconds);
225 }