sys_dos.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <signal.h>
  18. #include <stdlib.h>
  19. #include <limits.h>
  20. #include <sys/time.h>
  21. #include <sys/types.h>
  22. #include <dir.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <sys/stat.h>
  28. #include <string.h>
  29. #include <dpmi.h>
  30. #include <sys/nearptr.h>
  31. #include <conio.h>
  32. #include "quakedef.h"
  33. #include "dosisms.h"
  34. #define MINIMUM_WIN_MEMORY 0x800000
  35. #define MINIMUM_WIN_MEMORY_LEVELPAK (MINIMUM_WIN_MEMORY + 0x100000)
  36. int end_of_memory;
  37. qboolean lockmem, lockunlockmem, unlockmem;
  38. static int win95;
  39. #define STDOUT 1
  40. #define KEYBUF_SIZE 256
  41. static unsigned char keybuf[KEYBUF_SIZE];
  42. static int keybuf_head=0;
  43. static int keybuf_tail=0;
  44. static quakeparms_t quakeparms;
  45. int sys_checksum;
  46. static double curtime = 0.0;
  47. static double lastcurtime = 0.0;
  48. static double oldtime = 0.0;
  49. static qboolean isDedicated;
  50. static int minmem;
  51. float fptest_temp;
  52. extern char start_of_memory __asm__("start");
  53. //=============================================================================
  54. // this is totally dependent on cwsdpmi putting the stack right after tge
  55. // global data
  56. // This does evil things in a Win95 DOS box!!!
  57. #if 0
  58. extern byte end;
  59. #define CHECKBYTE 0xed
  60. void Sys_InitStackCheck (void)
  61. {
  62. int i;
  63. for (i=0 ; i<128*1024 ; i++)
  64. (&end)[i] = CHECKBYTE;
  65. }
  66. void Sys_StackCheck (void)
  67. {
  68. int i;
  69. for (i=0 ; i<128*1024 ; i++)
  70. if ( (&end)[i] != CHECKBYTE )
  71. break;
  72. Con_Printf ("%i undisturbed stack bytes\n", i);
  73. if (end != CHECKBYTE)
  74. Sys_Error ("System stack overflow!");
  75. }
  76. #endif
  77. //=============================================================================
  78. byte scantokey[128] =
  79. {
  80. // 0 1 2 3 4 5 6 7
  81. // 8 9 A B C D E F
  82. 0 , 27, '1', '2', '3', '4', '5', '6',
  83. '7', '8', '9', '0', '-', '=', K_BACKSPACE, 9, // 0
  84. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  85. 'o', 'p', '[', ']', 13 , K_CTRL,'a', 's', // 1
  86. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  87. '\'' , '`', K_SHIFT,'\\', 'z', 'x', 'c', 'v', // 2
  88. 'b', 'n', 'm', ',', '.', '/', K_SHIFT,'*',
  89. K_ALT,' ', 0 , K_F1, K_F2, K_F3, K_F4, K_F5, // 3
  90. K_F6, K_F7, K_F8, K_F9, K_F10,0 , 0 , K_HOME,
  91. K_UPARROW,K_PGUP,'-',K_LEFTARROW,'5',K_RIGHTARROW,'+',K_END, //4
  92. K_DOWNARROW,K_PGDN,K_INS,K_DEL,0,0, 0, K_F11,
  93. K_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
  94. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
  95. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
  96. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
  97. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
  98. };
  99. byte shiftscantokey[128] =
  100. {
  101. // 0 1 2 3 4 5 6 7
  102. // 8 9 A B C D E F
  103. 0 , 27, '!', '@', '#', '$', '%', '^',
  104. '&', '*', '(', ')', '_', '+', K_BACKSPACE, 9, // 0
  105. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
  106. 'O', 'P', '{', '}', 13 , K_CTRL,'A', 'S', // 1
  107. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
  108. '"' , '~', K_SHIFT,'|', 'Z', 'X', 'C', 'V', // 2
  109. 'B', 'N', 'M', '<', '>', '?', K_SHIFT,'*',
  110. K_ALT,' ', 0 , K_F1, K_F2, K_F3, K_F4, K_F5, // 3
  111. K_F6, K_F7, K_F8, K_F9, K_F10,0 , 0 , K_HOME,
  112. K_UPARROW,K_PGUP,'_',K_LEFTARROW,'%',K_RIGHTARROW,'+',K_END, //4
  113. K_DOWNARROW,K_PGDN,K_INS,K_DEL,0,0, 0, K_F11,
  114. K_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
  115. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
  116. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
  117. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
  118. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
  119. };
  120. void TrapKey(void)
  121. {
  122. // static int ctrl=0;
  123. keybuf[keybuf_head] = dos_inportb(0x60);
  124. dos_outportb(0x20, 0x20);
  125. /*
  126. if (scantokey[keybuf[keybuf_head]&0x7f] == K_CTRL)
  127. ctrl=keybuf[keybuf_head]&0x80;
  128. if (ctrl && scantokey[keybuf[keybuf_head]&0x7f] == 'c')
  129. Sys_Error("ctrl-c hit\n");
  130. */
  131. keybuf_head = (keybuf_head + 1) & (KEYBUF_SIZE-1);
  132. }
  133. #define SC_UPARROW 0x48
  134. #define SC_DOWNARROW 0x50
  135. #define SC_LEFTARROW 0x4b
  136. #define SC_RIGHTARROW 0x4d
  137. #define SC_LEFTSHIFT 0x2a
  138. #define SC_RIGHTSHIFT 0x36
  139. #define SC_RIGHTARROW 0x4d
  140. void MaskExceptions (void);
  141. void Sys_InitFloatTime (void);
  142. void Sys_PushFPCW_SetHigh (void);
  143. void Sys_PopFPCW (void);
  144. #define LEAVE_FOR_CACHE (512*1024) //FIXME: tune
  145. #define LOCKED_FOR_MALLOC (128*1024) //FIXME: tune
  146. void Sys_DetectWin95 (void)
  147. {
  148. __dpmi_regs r;
  149. r.x.ax = 0x160a; /* Get Windows Version */
  150. __dpmi_int(0x2f, &r);
  151. if(r.x.ax || r.h.bh < 4) /* Not windows or earlier than Win95 */
  152. {
  153. win95 = 0;
  154. lockmem = true;
  155. lockunlockmem = false;
  156. unlockmem = true;
  157. }
  158. else
  159. {
  160. win95 = 1;
  161. lockunlockmem = COM_CheckParm ("-winlockunlock");
  162. if (lockunlockmem)
  163. lockmem = true;
  164. else
  165. lockmem = COM_CheckParm ("-winlock");
  166. unlockmem = lockmem && !lockunlockmem;
  167. }
  168. }
  169. void *dos_getmaxlockedmem(int *size)
  170. {
  171. __dpmi_free_mem_info meminfo;
  172. __dpmi_meminfo info;
  173. int working_size;
  174. void *working_memory;
  175. int last_locked;
  176. int extra, i, j, allocsize;
  177. static char *msg = "Locking data...";
  178. int m, n;
  179. byte *x;
  180. // first lock all the current executing image so the locked count will
  181. // be accurate. It doesn't hurt to lock the memory multiple times
  182. last_locked = __djgpp_selector_limit + 1;
  183. info.size = last_locked - 4096;
  184. info.address = __djgpp_base_address + 4096;
  185. if (lockmem)
  186. {
  187. if(__dpmi_lock_linear_region(&info))
  188. {
  189. Sys_Error ("Lock of current memory at 0x%lx for %ldKb failed!\n",
  190. info.address, info.size/1024);
  191. }
  192. }
  193. __dpmi_get_free_memory_information(&meminfo);
  194. if (!win95) /* Not windows or earlier than Win95 */
  195. {
  196. working_size = meminfo.maximum_locked_page_allocation_in_pages * 4096;
  197. }
  198. else
  199. {
  200. working_size = meminfo.largest_available_free_block_in_bytes -
  201. LEAVE_FOR_CACHE;
  202. }
  203. working_size &= ~0xffff; /* Round down to 64K */
  204. working_size += 0x10000;
  205. do
  206. {
  207. working_size -= 0x10000; /* Decrease 64K and try again */
  208. working_memory = sbrk(working_size);
  209. } while (working_memory == (void *)-1);
  210. extra = 0xfffc - ((unsigned)sbrk(0) & 0xffff);
  211. if (extra > 0)
  212. {
  213. sbrk(extra);
  214. working_size += extra;
  215. }
  216. // now grab the memory
  217. info.address = last_locked + __djgpp_base_address;
  218. if (!win95)
  219. {
  220. info.size = __djgpp_selector_limit + 1 - last_locked;
  221. while (info.size > 0 && __dpmi_lock_linear_region(&info))
  222. {
  223. info.size -= 0x1000;
  224. working_size -= 0x1000;
  225. sbrk(-0x1000);
  226. }
  227. }
  228. else
  229. { /* Win95 section */
  230. j = COM_CheckParm("-winmem");
  231. if (standard_quake)
  232. minmem = MINIMUM_WIN_MEMORY;
  233. else
  234. minmem = MINIMUM_WIN_MEMORY_LEVELPAK;
  235. if (j)
  236. {
  237. allocsize = ((int)(Q_atoi(com_argv[j+1]))) * 0x100000 +
  238. LOCKED_FOR_MALLOC;
  239. if (allocsize < (minmem + LOCKED_FOR_MALLOC))
  240. allocsize = minmem + LOCKED_FOR_MALLOC;
  241. }
  242. else
  243. {
  244. allocsize = minmem + LOCKED_FOR_MALLOC;
  245. }
  246. if (!lockmem)
  247. {
  248. // we won't lock, just sbrk the memory
  249. info.size = allocsize;
  250. goto UpdateSbrk;
  251. }
  252. // lock the memory down
  253. write (STDOUT, msg, strlen (msg));
  254. for (j=allocsize ; j>(minmem + LOCKED_FOR_MALLOC) ;
  255. j -= 0x100000)
  256. {
  257. info.size = j;
  258. if (!__dpmi_lock_linear_region(&info))
  259. goto Locked;
  260. write (STDOUT, ".", 1);
  261. }
  262. // finally, try with the absolute minimum amount
  263. for (i=0 ; i<10 ; i++)
  264. {
  265. info.size = minmem + LOCKED_FOR_MALLOC;
  266. if (!__dpmi_lock_linear_region(&info))
  267. goto Locked;
  268. }
  269. Sys_Error ("Can't lock memory; %d Mb lockable RAM required. "
  270. "Try shrinking smartdrv.", info.size / 0x100000);
  271. Locked:
  272. UpdateSbrk:
  273. info.address += info.size;
  274. info.address -= __djgpp_base_address + 4; // ending point, malloc align
  275. working_size = info.address - (int)working_memory;
  276. sbrk(info.address-(int)sbrk(0)); // negative adjustment
  277. }
  278. if (lockunlockmem)
  279. {
  280. __dpmi_unlock_linear_region (&info);
  281. printf ("Locked and unlocked %d Mb data\n", working_size / 0x100000);
  282. }
  283. else if (lockmem)
  284. {
  285. printf ("Locked %d Mb data\n", working_size / 0x100000);
  286. }
  287. else
  288. {
  289. printf ("Allocated %d Mb data\n", working_size / 0x100000);
  290. }
  291. // touch all the memory to make sure it's there. The 16-page skip is to
  292. // keep Win 95 from thinking we're trying to page ourselves in (we are
  293. // doing that, of course, but there's no reason we shouldn't)
  294. x = (byte *)working_memory;
  295. for (n=0 ; n<4 ; n++)
  296. {
  297. for (m=0 ; m<(working_size - 16 * 0x1000) ; m += 4)
  298. {
  299. sys_checksum += *(int *)&x[m];
  300. sys_checksum += *(int *)&x[m + 16 * 0x1000];
  301. }
  302. }
  303. // give some of what we locked back for malloc before returning. Done
  304. // by cheating and passing a negative value to sbrk
  305. working_size -= LOCKED_FOR_MALLOC;
  306. sbrk( -(LOCKED_FOR_MALLOC));
  307. *size = working_size;
  308. return working_memory;
  309. }
  310. /*
  311. ============
  312. Sys_FileTime
  313. returns -1 if not present
  314. ============
  315. */
  316. int Sys_FileTime (char *path)
  317. {
  318. struct stat buf;
  319. if (stat (path,&buf) == -1)
  320. return -1;
  321. return buf.st_mtime;
  322. }
  323. void Sys_mkdir (char *path)
  324. {
  325. mkdir (path, 0777);
  326. }
  327. void Sys_Sleep(void)
  328. {
  329. }
  330. char *Sys_ConsoleInput(void)
  331. {
  332. static char text[256];
  333. static int len = 0;
  334. char ch;
  335. if (!isDedicated)
  336. return NULL;
  337. if (! kbhit())
  338. return NULL;
  339. ch = getche();
  340. switch (ch)
  341. {
  342. case '\r':
  343. putch('\n');
  344. if (len)
  345. {
  346. text[len] = 0;
  347. len = 0;
  348. return text;
  349. }
  350. break;
  351. case '\b':
  352. putch(' ');
  353. if (len)
  354. {
  355. len--;
  356. putch('\b');
  357. }
  358. break;
  359. default:
  360. text[len] = ch;
  361. len = (len + 1) & 0xff;
  362. break;
  363. }
  364. return NULL;
  365. }
  366. void Sys_Init(void)
  367. {
  368. MaskExceptions ();
  369. Sys_SetFPCW ();
  370. dos_outportb(0x43, 0x34); // set system timer to mode 2
  371. dos_outportb(0x40, 0); // for the Sys_FloatTime() function
  372. dos_outportb(0x40, 0);
  373. Sys_InitFloatTime ();
  374. _go32_interrupt_stack_size = 4 * 1024;;
  375. _go32_rmcb_stack_size = 4 * 1024;
  376. }
  377. void Sys_Shutdown(void)
  378. {
  379. if (!isDedicated)
  380. dos_restoreintr(9);
  381. if (unlockmem)
  382. {
  383. dos_unlockmem (&start_of_memory,
  384. end_of_memory - (int)&start_of_memory);
  385. dos_unlockmem (quakeparms.membase, quakeparms.memsize);
  386. }
  387. }
  388. #define SC_RSHIFT 0x36
  389. #define SC_LSHIFT 0x2a
  390. void Sys_SendKeyEvents (void)
  391. {
  392. int k, next;
  393. int outkey;
  394. // get key events
  395. while (keybuf_head != keybuf_tail)
  396. {
  397. k = keybuf[keybuf_tail++];
  398. keybuf_tail &= (KEYBUF_SIZE-1);
  399. if (k==0xe0)
  400. continue; // special / pause keys
  401. next = keybuf[(keybuf_tail-2)&(KEYBUF_SIZE-1)];
  402. if (next == 0xe1)
  403. continue; // pause key bullshit
  404. if (k==0xc5 && next == 0x9d)
  405. {
  406. Key_Event (K_PAUSE, true);
  407. continue;
  408. }
  409. // extended keyboard shift key bullshit
  410. if ( (k&0x7f)==SC_LSHIFT || (k&0x7f)==SC_RSHIFT )
  411. {
  412. if ( keybuf[(keybuf_tail-2)&(KEYBUF_SIZE-1)]==0xe0 )
  413. continue;
  414. k &= 0x80;
  415. k |= SC_RSHIFT;
  416. }
  417. if (k==0xc5 && keybuf[(keybuf_tail-2)&(KEYBUF_SIZE-1)] == 0x9d)
  418. continue; // more pause bullshit
  419. outkey = scantokey[k & 0x7f];
  420. if (k & 0x80)
  421. Key_Event (outkey, false);
  422. else
  423. Key_Event (outkey, true);
  424. }
  425. }
  426. // =======================================================================
  427. // General routines
  428. // =======================================================================
  429. /*
  430. ================
  431. Sys_Printf
  432. ================
  433. */
  434. void Sys_Printf (char *fmt, ...)
  435. {
  436. va_list argptr;
  437. char text[1024];
  438. va_start (argptr,fmt);
  439. vsprintf (text,fmt,argptr);
  440. va_end (argptr);
  441. if (cls.state == ca_dedicated)
  442. fprintf(stderr, "%s", text);
  443. }
  444. void Sys_AtExit (void)
  445. {
  446. // shutdown only once (so Sys_Error can call this function to shutdown, then
  447. // print the error message, then call exit without exit calling this function
  448. // again)
  449. Sys_Shutdown();
  450. }
  451. void Sys_Quit (void)
  452. {
  453. byte screen[80*25*2];
  454. byte *d;
  455. char ver[6];
  456. int i;
  457. // load the sell screen before shuting everything down
  458. if (registered.value)
  459. d = COM_LoadHunkFile ("end2.bin");
  460. else
  461. d = COM_LoadHunkFile ("end1.bin");
  462. if (d)
  463. memcpy (screen, d, sizeof(screen));
  464. // write the version number directly to the end screen
  465. sprintf (ver, " v%4.2f", VERSION);
  466. for (i=0 ; i<6 ; i++)
  467. screen[0*80*2 + 72*2 + i*2] = ver[i];
  468. Host_Shutdown();
  469. // do the text mode sell screen
  470. if (d)
  471. {
  472. memcpy ((void *)real2ptr(0xb8000), screen,80*25*2);
  473. // set text pos
  474. regs.x.ax = 0x0200;
  475. regs.h.bh = 0;
  476. regs.h.dl = 0;
  477. regs.h.dh = 22;
  478. dos_int86 (0x10);
  479. }
  480. else
  481. printf ("couldn't load endscreen.\n");
  482. exit(0);
  483. }
  484. void Sys_Error (char *error, ...)
  485. {
  486. va_list argptr;
  487. char string[1024];
  488. va_start (argptr,error);
  489. vsprintf (string,error,argptr);
  490. va_end (argptr);
  491. Host_Shutdown();
  492. fprintf(stderr, "Error: %s\n", string);
  493. // Sys_AtExit is called by exit to shutdown the system
  494. exit(0);
  495. }
  496. int Sys_FileOpenRead (char *path, int *handle)
  497. {
  498. int h;
  499. struct stat fileinfo;
  500. h = open (path, O_RDONLY|O_BINARY, 0666);
  501. *handle = h;
  502. if (h == -1)
  503. return -1;
  504. if (fstat (h,&fileinfo) == -1)
  505. Sys_Error ("Error fstating %s", path);
  506. return fileinfo.st_size;
  507. }
  508. int Sys_FileOpenWrite (char *path)
  509. {
  510. int handle;
  511. umask (0);
  512. handle = open(path,O_RDWR | O_BINARY | O_CREAT | O_TRUNC
  513. , 0666);
  514. if (handle == -1)
  515. Sys_Error ("Error opening %s: %s", path,strerror(errno));
  516. return handle;
  517. }
  518. void Sys_FileClose (int handle)
  519. {
  520. close (handle);
  521. }
  522. void Sys_FileSeek (int handle, int position)
  523. {
  524. lseek (handle, position, SEEK_SET);
  525. }
  526. int Sys_FileRead (int handle, void *dest, int count)
  527. {
  528. return read (handle, dest, count);
  529. }
  530. int Sys_FileWrite (int handle, void *data, int count)
  531. {
  532. return write (handle, data, count);
  533. }
  534. /*
  535. ================
  536. Sys_MakeCodeWriteable
  537. ================
  538. */
  539. void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
  540. {
  541. // it's always writeable
  542. }
  543. /*
  544. ================
  545. Sys_FloatTime
  546. ================
  547. */
  548. double Sys_FloatTime (void)
  549. {
  550. int r;
  551. unsigned t, tick;
  552. double ft, time;
  553. static int sametimecount;
  554. Sys_PushFPCW_SetHigh ();
  555. //{static float t = 0; t=t+0.05; return t;} // DEBUG
  556. t = *(unsigned short*)real2ptr(0x46c) * 65536;
  557. dos_outportb(0x43, 0); // latch time
  558. r = dos_inportb(0x40);
  559. r |= dos_inportb(0x40) << 8;
  560. r = (r-1) & 0xffff;
  561. tick = *(unsigned short*)real2ptr(0x46c) * 65536;
  562. if ((tick != t) && (r & 0x8000))
  563. t = tick;
  564. ft = (double) (t+(65536-r)) / 1193200.0;
  565. time = ft - oldtime;
  566. oldtime = ft;
  567. if (time < 0)
  568. {
  569. if (time > -3000.0)
  570. time = 0.0;
  571. else
  572. time += 3600.0;
  573. }
  574. curtime += time;
  575. if (curtime == lastcurtime)
  576. {
  577. sametimecount++;
  578. if (sametimecount > 100000)
  579. {
  580. curtime += 1.0;
  581. sametimecount = 0;
  582. }
  583. }
  584. else
  585. {
  586. sametimecount = 0;
  587. }
  588. lastcurtime = curtime;
  589. Sys_PopFPCW ();
  590. return curtime;
  591. }
  592. /*
  593. ================
  594. Sys_InitFloatTime
  595. ================
  596. */
  597. void Sys_InitFloatTime (void)
  598. {
  599. int j;
  600. Sys_FloatTime ();
  601. oldtime = curtime;
  602. j = COM_CheckParm("-starttime");
  603. if (j)
  604. {
  605. curtime = (double) (Q_atof(com_argv[j+1]));
  606. }
  607. else
  608. {
  609. curtime = 0.0;
  610. }
  611. lastcurtime = curtime;
  612. }
  613. /*
  614. ================
  615. Sys_GetMemory
  616. ================
  617. */
  618. void Sys_GetMemory(void)
  619. {
  620. int j, tsize;
  621. j = COM_CheckParm("-mem");
  622. if (j)
  623. {
  624. quakeparms.memsize = (int) (Q_atof(com_argv[j+1]) * 1024 * 1024);
  625. quakeparms.membase = malloc (quakeparms.memsize);
  626. }
  627. else
  628. {
  629. quakeparms.membase = dos_getmaxlockedmem (&quakeparms.memsize);
  630. }
  631. fprintf(stderr, "malloc'd: %d\n", quakeparms.memsize);
  632. if (COM_CheckParm ("-heapsize"))
  633. {
  634. tsize = Q_atoi (com_argv[COM_CheckParm("-heapsize") + 1]) * 1024;
  635. if (tsize < quakeparms.memsize)
  636. quakeparms.memsize = tsize;
  637. }
  638. }
  639. /*
  640. ================
  641. Sys_PageInProgram
  642. walks the text, data, and bss to make sure it's all paged in so that the
  643. actual physical memory detected by Sys_GetMemory is correct.
  644. ================
  645. */
  646. void Sys_PageInProgram(void)
  647. {
  648. int i, j;
  649. end_of_memory = (int)sbrk(0);
  650. if (lockmem)
  651. {
  652. if (dos_lockmem ((void *)&start_of_memory,
  653. end_of_memory - (int)&start_of_memory))
  654. Sys_Error ("Couldn't lock text and data");
  655. }
  656. if (lockunlockmem)
  657. {
  658. dos_unlockmem((void *)&start_of_memory,
  659. end_of_memory - (int)&start_of_memory);
  660. printf ("Locked and unlocked %d Mb image\n",
  661. (end_of_memory - (int)&start_of_memory) / 0x100000);
  662. }
  663. else if (lockmem)
  664. {
  665. printf ("Locked %d Mb image\n",
  666. (end_of_memory - (int)&start_of_memory) / 0x100000);
  667. }
  668. else
  669. {
  670. printf ("Loaded %d Mb image\n",
  671. (end_of_memory - (int)&start_of_memory) / 0x100000);
  672. }
  673. // touch the entire image, doing the 16-page skip so Win95 doesn't think we're
  674. // trying to page ourselves in
  675. for (j=0 ; j<4 ; j++)
  676. {
  677. for(i=(int)&start_of_memory ; i<(end_of_memory - 16 * 0x1000) ; i += 4)
  678. {
  679. sys_checksum += *(int *)i;
  680. sys_checksum += *(int *)(i + 16 * 0x1000);
  681. }
  682. }
  683. }
  684. /*
  685. ================
  686. Sys_NoFPUExceptionHandler
  687. ================
  688. */
  689. void Sys_NoFPUExceptionHandler(int whatever)
  690. {
  691. printf ("\nError: Quake requires a floating-point processor\n");
  692. exit (0);
  693. }
  694. /*
  695. ================
  696. Sys_DefaultExceptionHandler
  697. ================
  698. */
  699. void Sys_DefaultExceptionHandler(int whatever)
  700. {
  701. }
  702. /*
  703. ================
  704. main
  705. ================
  706. */
  707. int main (int c, char **v)
  708. {
  709. double time, oldtime, newtime;
  710. extern void (*dos_error_func)(char *, ...);
  711. static char cwd[1024];
  712. printf ("Quake v%4.2f\n", VERSION);
  713. // make sure there's an FPU
  714. signal(SIGNOFP, Sys_NoFPUExceptionHandler);
  715. signal(SIGABRT, Sys_DefaultExceptionHandler);
  716. signal(SIGALRM, Sys_DefaultExceptionHandler);
  717. signal(SIGKILL, Sys_DefaultExceptionHandler);
  718. signal(SIGQUIT, Sys_DefaultExceptionHandler);
  719. signal(SIGINT, Sys_DefaultExceptionHandler);
  720. if (fptest_temp >= 0.0)
  721. fptest_temp += 0.1;
  722. COM_InitArgv (c, v);
  723. quakeparms.argc = com_argc;
  724. quakeparms.argv = com_argv;
  725. dos_error_func = Sys_Error;
  726. Sys_DetectWin95 ();
  727. Sys_PageInProgram ();
  728. Sys_GetMemory ();
  729. atexit (Sys_AtExit); // in case we crash
  730. getwd (cwd);
  731. if (cwd[Q_strlen(cwd)-1] == '/') cwd[Q_strlen(cwd)-1] = 0;
  732. quakeparms.basedir = cwd; //"f:/quake";
  733. isDedicated = (COM_CheckParm ("-dedicated") != 0);
  734. Sys_Init ();
  735. if (!isDedicated)
  736. dos_registerintr(9, TrapKey);
  737. //Sys_InitStackCheck ();
  738. Host_Init(&quakeparms);
  739. //Sys_StackCheck ();
  740. //Con_Printf ("Top of stack: 0x%x\n", &time);
  741. oldtime = Sys_FloatTime ();
  742. while (1)
  743. {
  744. newtime = Sys_FloatTime ();
  745. time = newtime - oldtime;
  746. if (cls.state == ca_dedicated && (time<sys_ticrate.value))
  747. continue;
  748. Host_Frame (time);
  749. //Sys_StackCheck ();
  750. oldtime = newtime;
  751. }
  752. }