sys_sun.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. // sys_sun.h -- Sun system driver
  16. #include "quakedef.h"
  17. #include "errno.h"
  18. #include <sys/types.h>
  19. #include <sys/time.h>
  20. #include <sys/stat.h>
  21. #include <sys/mman.h>
  22. #include <sys/param.h>
  23. #include <fcntl.h>
  24. #include <stddef.h>
  25. #include <sys/types.h>
  26. #include <fcntl.h>
  27. #include <sys/mman.h>
  28. #include <stdio.h>
  29. qboolean isDedicated;
  30. /*
  31. ===============================================================================
  32. FILE IO
  33. ===============================================================================
  34. */
  35. #define MAX_HANDLES 10
  36. typedef struct
  37. {
  38. FILE *hFile;
  39. char *pMap;
  40. int nLen;
  41. int nPos;
  42. } MEMFILE;
  43. MEMFILE sys_handles[MAX_HANDLES];
  44. int findhandle (void)
  45. {
  46. int i;
  47. for (i=1 ; i<MAX_HANDLES ; i++)
  48. if (!sys_handles[i].hFile)
  49. return i;
  50. Sys_Error ("out of handles");
  51. return -1;
  52. }
  53. /*
  54. ================
  55. filelength
  56. ================
  57. */
  58. int filelength (FILE *f)
  59. {
  60. int pos;
  61. int end;
  62. pos = ftell (f);
  63. fseek (f, 0, SEEK_END);
  64. end = ftell (f);
  65. fseek (f, pos, SEEK_SET);
  66. return end;
  67. }
  68. int Sys_FileOpenRead (char *path, int *hndl)
  69. {
  70. FILE *f;
  71. int i;
  72. i = findhandle ();
  73. f = fopen(path, "rb");
  74. if (!f)
  75. {
  76. *hndl = -1;
  77. return -1;
  78. }
  79. sys_handles[i].hFile = f;
  80. sys_handles[i].nLen = filelength(f);
  81. sys_handles[i].nPos = 0;
  82. sys_handles[i].pMap = mmap( 0, sys_handles[i].nLen, PROT_READ, MAP_SHARED, fileno( sys_handles[i].hFile ), 0 );
  83. if (!sys_handles[i].pMap || (sys_handles[i].pMap == (char *)-1))
  84. {
  85. printf( "mmap %s failed!", path );
  86. sys_handles[i].pMap = NULL;
  87. }
  88. *hndl = i;
  89. return( sys_handles[i].nLen );
  90. }
  91. int Sys_FileOpenWrite (char *path)
  92. {
  93. FILE *f;
  94. int i;
  95. i = findhandle ();
  96. f = fopen(path, "wb");
  97. if (!f)
  98. Sys_Error ("Error opening %s: %s", path,strerror(errno));
  99. sys_handles[i].hFile = f;
  100. sys_handles[i].nLen = 0;
  101. sys_handles[i].nPos = 0;
  102. sys_handles[i].pMap = NULL;
  103. return i;
  104. }
  105. void Sys_FileClose (int handle)
  106. {
  107. if (sys_handles[handle].pMap)
  108. if (munmap( sys_handles[handle].pMap, sys_handles[handle].nLen ) != 0)
  109. printf( "failed to unmap handle %d\n", handle );
  110. fclose (sys_handles[handle].hFile);
  111. sys_handles[handle].hFile = NULL;
  112. }
  113. void Sys_FileSeek (int handle, int position)
  114. {
  115. if (sys_handles[handle].pMap)
  116. {
  117. sys_handles[handle].nPos = position;
  118. }
  119. else fseek (sys_handles[handle].hFile, position, SEEK_SET);
  120. }
  121. int Sys_FileRead (int handle, void *dest, int count)
  122. {
  123. if (sys_handles[handle].pMap)
  124. {
  125. int nPos = sys_handles[handle].nPos;
  126. if (count < 0) count = 0;
  127. if (nPos + count > sys_handles[handle].nLen)
  128. count = sys_handles[handle].nLen - nPos;
  129. memcpy( dest, &sys_handles[handle].pMap[nPos], count );
  130. sys_handles[handle].nPos = nPos + count;
  131. return( count );
  132. }
  133. else return fread (dest, 1, count, sys_handles[handle].hFile);
  134. }
  135. int Sys_FileWrite (int handle, void *data, int count)
  136. {
  137. if (sys_handles[handle].pMap)
  138. Sys_Error( "Attempted to write to read-only file %d!\n", handle );
  139. return fwrite (data, 1, count, sys_handles[handle].hFile);
  140. }
  141. int Sys_FileTime (char *path)
  142. {
  143. FILE *f;
  144. f = fopen(path, "rb");
  145. if (f)
  146. {
  147. fclose(f);
  148. return 1;
  149. }
  150. return -1;
  151. }
  152. void Sys_mkdir (char *path)
  153. {
  154. mkdir( path, 0777 );
  155. }
  156. /*
  157. ===============================================================================
  158. SYSTEM IO
  159. ===============================================================================
  160. */
  161. void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
  162. {
  163. int r;
  164. unsigned long addr;
  165. int psize = getpagesize();
  166. addr = (startaddr & ~(psize-1)) - psize;
  167. // fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
  168. // addr, startaddr+length, length);
  169. r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
  170. if (r < 0)
  171. Sys_Error("Protection change failed\n");
  172. }
  173. void Sys_Error (char *error, ...)
  174. {
  175. va_list argptr;
  176. printf ("Sys_Error: ");
  177. va_start (argptr,error);
  178. vprintf (error,argptr);
  179. va_end (argptr);
  180. printf ("\n");
  181. Host_Shutdown();
  182. exit (1);
  183. }
  184. void Sys_Printf (char *fmt, ...)
  185. {
  186. va_list argptr;
  187. va_start (argptr,fmt);
  188. vprintf (fmt,argptr);
  189. va_end (argptr);
  190. }
  191. void Sys_Quit (void)
  192. {
  193. Host_Shutdown();
  194. exit (0);
  195. }
  196. double Sys_FloatTime (void)
  197. {
  198. struct timeval tp;
  199. struct timezone tzp;
  200. static int secbase;
  201. gettimeofday(&tp, &tzp);
  202. if (!secbase)
  203. {
  204. secbase = tp.tv_sec;
  205. return tp.tv_usec/1000000.0;
  206. }
  207. return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
  208. }
  209. char *Sys_ConsoleInput (void)
  210. {
  211. static char text[256];
  212. int len;
  213. fd_set readfds;
  214. int ready;
  215. struct timeval timeout;
  216. timeout.tv_sec = 0;
  217. timeout.tv_usec = 0;
  218. FD_ZERO(&readfds);
  219. FD_SET(0, &readfds);
  220. ready = select(1, &readfds, 0, 0, &timeout);
  221. if (ready>0)
  222. {
  223. len = read (0, text, sizeof(text));
  224. if (len >= 1)
  225. {
  226. text[len-1] = 0; // rip off the /n and terminate
  227. return text;
  228. }
  229. }
  230. return 0;
  231. }
  232. void Sys_Sleep (void)
  233. {
  234. }
  235. #if !id386
  236. void Sys_HighFPPrecision (void)
  237. {
  238. }
  239. void Sys_LowFPPrecision (void)
  240. {
  241. }
  242. #endif
  243. void Sys_Init(void)
  244. {
  245. #if id386
  246. Sys_SetFPCW();
  247. #endif
  248. }
  249. //=============================================================================
  250. int main (int argc, char **argv)
  251. {
  252. static quakeparms_t parms;
  253. float time, oldtime, newtime;
  254. parms.memsize = 16*1024*1024;
  255. parms.membase = malloc (parms.memsize);
  256. parms.basedir = ".";
  257. parms.cachedir = NULL;
  258. COM_InitArgv (argc, argv);
  259. parms.argc = com_argc;
  260. parms.argv = com_argv;
  261. printf ("Host_Init\n");
  262. Host_Init (&parms);
  263. Sys_Init();
  264. // unroll the simulation loop to give the video side a chance to see _vid_default_mode
  265. Host_Frame( 0.1 );
  266. VID_SetDefaultMode();
  267. oldtime = Sys_FloatTime();
  268. while (1)
  269. {
  270. newtime = Sys_FloatTime();
  271. Host_Frame (newtime - oldtime);
  272. oldtime = newtime;
  273. }
  274. return 0;
  275. }