pm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /****************************************************************************
  2. *
  3. * SciTech OS Portability Manager Library
  4. *
  5. * ========================================================================
  6. *
  7. * The contents of this file are subject to the SciTech MGL Public
  8. * License Version 1.0 (the "License"); you may not use this file
  9. * except in compliance with the License. You may obtain a copy of
  10. * the License at http://www.scitechsoft.com/mgl-license.txt
  11. *
  12. * Software distributed under the License is distributed on an
  13. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  14. * implied. See the License for the specific language governing
  15. * rights and limitations under the License.
  16. *
  17. * The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
  18. *
  19. * The Initial Developer of the Original Code is SciTech Software, Inc.
  20. * All Rights Reserved.
  21. *
  22. * ========================================================================
  23. *
  24. * Language: ANSI C
  25. * Environment: BeOS
  26. *
  27. * Description: Implementation for the OS Portability Manager Library, which
  28. * contains functions to implement OS specific services in a
  29. * generic, cross platform API. Porting the OS Portability
  30. * Manager library is the first step to porting any SciTech
  31. * products to a new platform.
  32. *
  33. ****************************************************************************/
  34. #include "pmapi.h"
  35. #include "drvlib/os/os.h"
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. /* TODO: Include any BeOS specific headers here! */
  40. /*--------------------------- Global variables ----------------------------*/
  41. static void (PMAPIP fatalErrorCleanup)(void) = NULL;
  42. /*----------------------------- Implementation ----------------------------*/
  43. void PMAPI PM_init(void)
  44. {
  45. /* TODO: Do any initialisation in here. This includes getting IOPL */
  46. /* access for the process calling PM_init. This will get called */
  47. /* more than once. */
  48. /* TODO: If you support the supplied MTRR register stuff (you need to */
  49. /* be at ring 0 for this!), you should initialise it in here. */
  50. /* MTRR_init(); */
  51. }
  52. long PMAPI PM_getOSType(void)
  53. { return _OS_BEOS; }
  54. int PMAPI PM_getModeType(void)
  55. { return PM_386; }
  56. void PMAPI PM_backslash(char *s)
  57. {
  58. uint pos = strlen(s);
  59. if (s[pos-1] != '/') {
  60. s[pos] = '/';
  61. s[pos+1] = '\0';
  62. }
  63. }
  64. void PMAPI PM_setFatalErrorCleanup(
  65. void (PMAPIP cleanup)(void))
  66. {
  67. fatalErrorCleanup = cleanup;
  68. }
  69. void PMAPI PM_fatalError(const char *msg)
  70. {
  71. /* TODO: If you are running in a GUI environment without a console, */
  72. /* this needs to be changed to bring up a fatal error message */
  73. /* box and terminate the program. */
  74. if (fatalErrorCleanup)
  75. fatalErrorCleanup();
  76. fprintf(stderr,"%s\n", msg);
  77. exit(1);
  78. }
  79. void * PMAPI PM_getVESABuf(uint *len,uint *rseg,uint *roff)
  80. {
  81. /* No BIOS access for the BeOS */
  82. return NULL;
  83. }
  84. int PMAPI PM_kbhit(void)
  85. {
  86. /* TODO: This function checks if a key is available to be read. This */
  87. /* should be implemented, but is mostly used by the test programs */
  88. /* these days. */
  89. return true;
  90. }
  91. int PMAPI PM_getch(void)
  92. {
  93. /* TODO: This returns the ASCII code of the key pressed. This */
  94. /* should be implemented, but is mostly used by the test programs */
  95. /* these days. */
  96. return 0xD;
  97. }
  98. int PMAPI PM_openConsole(void)
  99. {
  100. /* TODO: Opens up a fullscreen console for graphics output. If your */
  101. /* console does not have graphics/text modes, this can be left */
  102. /* empty. The main purpose of this is to disable console switching */
  103. /* when in graphics modes if you can switch away from fullscreen */
  104. /* consoles (if you want to allow switching, this can be done */
  105. /* elsewhere with a full save/restore state of the graphics mode). */
  106. return 0;
  107. }
  108. int PMAPI PM_getConsoleStateSize(void)
  109. {
  110. /* TODO: Returns the size of the console state buffer used to save the */
  111. /* state of the console before going into graphics mode. This is */
  112. /* used to restore the console back to normal when we are done. */
  113. return 1;
  114. }
  115. void PMAPI PM_saveConsoleState(void *stateBuf,int console_id)
  116. {
  117. /* TODO: Saves the state of the console into the state buffer. This is */
  118. /* used to restore the console back to normal when we are done. */
  119. /* We will always restore 80x25 text mode after being in graphics */
  120. /* mode, so if restoring text mode is all you need to do this can */
  121. /* be left empty. */
  122. }
  123. void PMAPI PM_restoreConsoleState(const void *stateBuf,int console_id)
  124. {
  125. /* TODO: Restore the state of the console from the state buffer. This is */
  126. /* used to restore the console back to normal when we are done. */
  127. /* We will always restore 80x25 text mode after being in graphics */
  128. /* mode, so if restoring text mode is all you need to do this can */
  129. /* be left empty. */
  130. }
  131. void PMAPI PM_closeConsole(int console_id)
  132. {
  133. /* TODO: Close the console when we are done, going back to text mode. */
  134. }
  135. void PM_setOSCursorLocation(int x,int y)
  136. {
  137. /* TODO: Set the OS console cursor location to the new value. This is */
  138. /* generally used for new OS ports (used mostly for DOS). */
  139. }
  140. void PM_setOSScreenWidth(int width,int height)
  141. {
  142. /* TODO: Set the OS console screen width. This is generally unused for */
  143. /* new OS ports. */
  144. }
  145. ibool PMAPI PM_setRealTimeClockHandler(PM_intHandler ih, int frequency)
  146. {
  147. /* TODO: Install a real time clock interrupt handler. Normally this */
  148. /* will not be supported from most OS'es in user land, so an */
  149. /* alternative mechanism is needed to enable software stereo. */
  150. /* Hence leave this unimplemented unless you have a high priority */
  151. /* mechanism to call the 32-bit callback when the real time clock */
  152. /* interrupt fires. */
  153. return false;
  154. }
  155. void PMAPI PM_setRealTimeClockFrequency(int frequency)
  156. {
  157. /* TODO: Set the real time clock interrupt frequency. Used for stereo */
  158. /* LC shutter glasses when doing software stereo. Usually sets */
  159. /* the frequency to around 2048 Hz. */
  160. }
  161. void PMAPI PM_restoreRealTimeClockHandler(void)
  162. {
  163. /* TODO: Restores the real time clock handler. */
  164. }
  165. char * PMAPI PM_getCurrentPath(
  166. char *path,
  167. int maxLen)
  168. {
  169. return getcwd(path,maxLen);
  170. }
  171. char PMAPI PM_getBootDrive(void)
  172. { return '/'; }
  173. const char * PMAPI PM_getVBEAFPath(void)
  174. { return PM_getNucleusConfigPath(); }
  175. const char * PMAPI PM_getNucleusPath(void)
  176. {
  177. char *env = getenv("NUCLEUS_PATH");
  178. return env ? env : "/usr/lib/nucleus";
  179. }
  180. const char * PMAPI PM_getNucleusConfigPath(void)
  181. {
  182. static char path[256];
  183. strcpy(path,PM_getNucleusPath());
  184. PM_backslash(path);
  185. strcat(path,"config");
  186. return path;
  187. }
  188. const char * PMAPI PM_getUniqueID(void)
  189. {
  190. /* TODO: Return a unique ID for the machine. If a unique ID is not */
  191. /* available, return the machine name. */
  192. static char buf[128];
  193. gethostname(buf, 128);
  194. return buf;
  195. }
  196. const char * PMAPI PM_getMachineName(void)
  197. {
  198. /* TODO: Return the network machine name for the machine. */
  199. static char buf[128];
  200. gethostname(buf, 128);
  201. return buf;
  202. }
  203. void * PMAPI PM_getBIOSPointer(void)
  204. {
  205. /* No BIOS access on the BeOS */
  206. return NULL;
  207. }
  208. void * PMAPI PM_getA0000Pointer(void)
  209. {
  210. static void *bankPtr;
  211. if (!bankPtr)
  212. bankPtr = PM_mapPhysicalAddr(0xA0000,0xFFFF,true);
  213. return bankPtr;
  214. }
  215. void * PMAPI PM_mapPhysicalAddr(ulong base,ulong limit,ibool isCached)
  216. {
  217. /* TODO: This function maps a physical memory address to a linear */
  218. /* address in the address space of the calling process. */
  219. /* NOTE: This function *must* be able to handle any phsyical base */
  220. /* address, and hence you will have to handle rounding of */
  221. /* the physical base address to a page boundary (ie: 4Kb on */
  222. /* x86 CPU's) to be able to properly map in the memory */
  223. /* region. */
  224. /* NOTE: If possible the isCached bit should be used to ensure that */
  225. /* the PCD (Page Cache Disable) and PWT (Page Write Through) */
  226. /* bits are set to disable caching for a memory mapping used */
  227. /* for MMIO register access. We also disable caching using */
  228. /* the MTRR registers for Pentium Pro and later chipsets so if */
  229. /* MTRR support is enabled for your OS then you can safely ignore */
  230. /* the isCached flag and always enable caching in the page */
  231. /* tables. */
  232. return NULL;
  233. }
  234. void PMAPI PM_freePhysicalAddr(void *ptr,ulong limit)
  235. {
  236. /* TODO: This function will free a physical memory mapping previously */
  237. /* allocated with PM_mapPhysicalAddr() if at all possible. If */
  238. /* you can't free physical memory mappings, simply do nothing. */
  239. }
  240. ulong PMAPI PM_getPhysicalAddr(void *p)
  241. {
  242. /* TODO: This function should find the physical address of a linear */
  243. /* address. */
  244. return 0xFFFFFFFFUL;
  245. }
  246. void PMAPI PM_sleep(ulong milliseconds)
  247. {
  248. /* TODO: Put the process to sleep for milliseconds */
  249. }
  250. int PMAPI PM_getCOMPort(int port)
  251. {
  252. /* TODO: Re-code this to determine real values using the Plug and Play */
  253. /* manager for the OS. */
  254. switch (port) {
  255. case 0: return 0x3F8;
  256. case 1: return 0x2F8;
  257. }
  258. return 0;
  259. }
  260. int PMAPI PM_getLPTPort(int port)
  261. {
  262. /* TODO: Re-code this to determine real values using the Plug and Play */
  263. /* manager for the OS. */
  264. switch (port) {
  265. case 0: return 0x3BC;
  266. case 1: return 0x378;
  267. case 2: return 0x278;
  268. }
  269. return 0;
  270. }
  271. void * PMAPI PM_mallocShared(long size)
  272. {
  273. /* TODO: This is used to allocate memory that is shared between process */
  274. /* that all access the common Nucleus drivers via a common display */
  275. /* driver DLL. If your OS does not support shared memory (or if */
  276. /* the display driver does not need to allocate shared memory */
  277. /* for each process address space), this should just call PM_malloc. */
  278. return PM_malloc(size);
  279. }
  280. void PMAPI PM_freeShared(void *ptr)
  281. {
  282. /* TODO: Free the shared memory block. This will be called in the context */
  283. /* of the original calling process that allocated the shared */
  284. /* memory with PM_mallocShared. Simply call free if you do not */
  285. /* need this. */
  286. PM_free(ptr);
  287. }
  288. void * PMAPI PM_mapToProcess(void *base,ulong limit)
  289. {
  290. /* TODO: This function is used to map a physical memory mapping */
  291. /* previously allocated with PM_mapPhysicalAddr into the */
  292. /* address space of the calling process. If the memory mapping */
  293. /* allocated by PM_mapPhysicalAddr is global to all processes, */
  294. /* simply return the pointer. */
  295. return base;
  296. }
  297. void * PMAPI PM_mapRealPointer(uint r_seg,uint r_off)
  298. {
  299. /* No BIOS access on the BeOS */
  300. return NULL;
  301. }
  302. void * PMAPI PM_allocRealSeg(uint size,uint *r_seg,uint *r_off)
  303. {
  304. /* No BIOS access on the BeOS */
  305. return NULL;
  306. }
  307. void PMAPI PM_freeRealSeg(void *mem)
  308. {
  309. /* No BIOS access on the BeOS */
  310. }
  311. void PMAPI DPMI_int86(int intno, DPMI_regs *regs)
  312. {
  313. /* No BIOS access on the BeOS */
  314. }
  315. int PMAPI PM_int86(int intno, RMREGS *in, RMREGS *out)
  316. {
  317. /* No BIOS access on the BeOS */
  318. return 0;
  319. }
  320. int PMAPI PM_int86x(int intno, RMREGS *in, RMREGS *out,
  321. RMSREGS *sregs)
  322. {
  323. /* No BIOS access on the BeOS */
  324. return 0;
  325. }
  326. void PMAPI PM_callRealMode(uint seg,uint off, RMREGS *in,
  327. RMSREGS *sregs)
  328. {
  329. /* No BIOS access on the BeOS */
  330. }
  331. void PMAPI PM_availableMemory(ulong *physical,ulong *total)
  332. {
  333. /* TODO: Report the amount of available memory, both the amount of */
  334. /* physical memory left and the amount of virtual memory left. */
  335. /* If the OS does not provide these services, report 0's. */
  336. *physical = *total = 0;
  337. }
  338. void * PMAPI PM_allocLockedMem(uint size,ulong *physAddr,ibool contiguous,ibool below16Meg)
  339. {
  340. /* TODO: Allocate a block of locked, physical memory of the specified */
  341. /* size. This is used for bus master operations. If this is not */
  342. /* supported by the OS, return NULL and bus mastering will not */
  343. /* be used. */
  344. return NULL;
  345. }
  346. void PMAPI PM_freeLockedMem(void *p,uint size,ibool contiguous)
  347. {
  348. /* TODO: Free a memory block allocated with PM_allocLockedMem. */
  349. }
  350. void PMAPI PM_setBankA(int bank)
  351. {
  352. /* No BIOS access on the BeOS */
  353. }
  354. void PMAPI PM_setBankAB(int bank)
  355. {
  356. /* No BIOS access on the BeOS */
  357. }
  358. void PMAPI PM_setCRTStart(int x,int y,int waitVRT)
  359. {
  360. /* No BIOS access on the BeOS */
  361. }
  362. ibool PMAPI PM_enableWriteCombine(ulong base,ulong length,uint type)
  363. {
  364. /* TODO: This function should enable Pentium Pro and Pentium II MTRR */
  365. /* write combining for the passed in physical memory base address */
  366. /* and length. Normally this is done via calls to an OS specific */
  367. /* device driver as this can only be done at ring 0. */
  368. /* */
  369. /* NOTE: This is a *very* important function to implement! If you do */
  370. /* not implement, graphics performance on the latest Intel chips */
  371. /* will be severly impaired. For sample code that can be used */
  372. /* directly in a ring 0 device driver, see the MSDOS implementation */
  373. /* which includes assembler code to do this directly (if the */
  374. /* program is running at ring 0). */
  375. return false;
  376. }
  377. ibool PMAPI PM_doBIOSPOST(ushort axVal,ulong BIOSPhysAddr,void *mappedBIOS)
  378. {
  379. /* TODO: This function is used to run the BIOS POST code on a secondary */
  380. /* controller to initialise it for use. This is not necessary */
  381. /* for multi-controller operation, but it will make it a lot */
  382. /* more convenicent for end users (otherwise they have to boot */
  383. /* the system once with the secondary controller as primary, and */
  384. /* then boot with both controllers installed). */
  385. /* */
  386. /* Even if you don't support full BIOS access, it would be */
  387. /* adviseable to be able to POST the secondary controllers in the */
  388. /* system using this function as a minimum requirement. Some */
  389. /* graphics hardware has registers that contain values that only */
  390. /* the BIOS knows about, which makes bring up a card from cold */
  391. /* reset difficult if the BIOS has not POST'ed it. */
  392. return false;
  393. }
  394. /****************************************************************************
  395. REMARKS:
  396. Function to find the first file matching a search criteria in a directory.
  397. ****************************************************************************/
  398. ulong PMAPI PM_findFirstFile(
  399. const char *filename,
  400. PM_findData *findData)
  401. {
  402. (void)filename;
  403. (void)findData;
  404. return PM_FILE_INVALID;
  405. }
  406. /****************************************************************************
  407. REMARKS:
  408. Function to find the next file matching a search criteria in a directory.
  409. ****************************************************************************/
  410. ibool PMAPI PM_findNextFile(
  411. ulong handle,
  412. PM_findData *findData)
  413. {
  414. (void)handle;
  415. (void)findData;
  416. return false;
  417. }
  418. /****************************************************************************
  419. REMARKS:
  420. Function to close the find process
  421. ****************************************************************************/
  422. void PMAPI PM_findClose(
  423. ulong handle)
  424. {
  425. (void)handle;
  426. }
  427. /****************************************************************************
  428. REMARKS:
  429. Function to determine if a drive is a valid drive or not. Under Unix this
  430. function will return false for anything except a value of 3 (considered
  431. the root drive, and equivalent to C: for non-Unix systems). The drive
  432. numbering is:
  433. 1 - Drive A:
  434. 2 - Drive B:
  435. 3 - Drive C:
  436. etc
  437. ****************************************************************************/
  438. ibool PMAPI PM_driveValid(
  439. char drive)
  440. {
  441. if (drive == 3)
  442. return true;
  443. return false;
  444. }
  445. /****************************************************************************
  446. REMARKS:
  447. Function to get the current working directory for the specififed drive.
  448. Under Unix this will always return the current working directory regardless
  449. of what the value of 'drive' is.
  450. ****************************************************************************/
  451. void PMAPI PM_getdcwd(
  452. int drive,
  453. char *dir,
  454. int len)
  455. {
  456. (void)drive;
  457. getcwd(dir,len);
  458. }
  459. /****************************************************************************
  460. REMARKS:
  461. Function to change the file attributes for a specific file.
  462. ****************************************************************************/
  463. void PMAPI PM_setFileAttr(
  464. const char *filename,
  465. uint attrib)
  466. {
  467. /* TODO: Set the file attributes for a file */
  468. (void)filename;
  469. (void)attrib;
  470. }