home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 15 / MA_Cover_15.iso / source / winquake / sys_amiga.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-03-12  |  10.2 KB  |  531 lines

  1. /*
  2. Copyright (C) 2000 Peter McGavin.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20.  
  21. /* the SAS/C __AMIGADATE__ macro appears to have broken from 1st Jan 2000 */
  22. #ifdef __PPC__
  23. #ifdef __STORM__
  24. const char amigaversion[] = "$VER: awinquakewos 0.9 (12.3.100)\n";
  25. #elif __VBCC__
  26. const char amigaversion[] = "$VER: awinquakevbcc 0.9 (12.3.100)\n"; // __AMIGADATE__ ;
  27. #else
  28. const char amigaversion[] = "$VER: awinquakeppc 0.9 (12.3.100)\n"; // __AMIGADATE__ ;
  29. #endif
  30. #else
  31. const char amigaversion[] = "$VER: awinquake 0.9 (12.3.100)\n"; // __AMIGADATE__ ;
  32. #endif
  33.  
  34. #ifdef __SASC
  35. //long __oslibversion = 38;    /* we require at least OS3.0 for LoadRGB32() */
  36. //char __stdiowin[] = "CON:20/50/600/130/awinquake";
  37. //char __stdiov37[] = "/AUTO/CLOSE/WAIT";
  38. //long __stack = 500000;  /* increase stack size to at least 500000 bytes */
  39. #endif
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43.  
  44. #ifdef __SASC
  45. #include <dos.h>
  46. #endif
  47.  
  48. #ifndef __PPC__
  49. #include <time.h>
  50. #endif
  51.  
  52. #ifdef __PPC__
  53. #include <exec/types.h>
  54. #if defined(__STORM__) || defined(__VBCC__)
  55. #include <powerpc/powerpc.h>
  56. #include <clib/exec_protos.h>
  57. #include <clib/powerpc_protos.h>
  58. #endif
  59. #ifdef __SASC
  60. #include <PowerUP/ppclib/ppc.h>
  61. #include <proto/exec.h>
  62. #endif
  63. #endif
  64.  
  65. #include "quakedef.h"
  66. #include "errno.h"
  67.  
  68. #ifdef __PPC__
  69. #include "amiga_timer.h"
  70. #endif
  71.  
  72. qboolean isDedicated = FALSE;
  73.  
  74. #ifdef __PPC__
  75. static int cpu_type;
  76. static int bus_clock;
  77. static int bus_MHz;
  78. static double clocks2secs;
  79. #endif
  80.  
  81. /*
  82. ===============================================================================
  83.  
  84. FILE IO
  85.  
  86. ===============================================================================
  87. */
  88.  
  89. #define    MAX_HANDLES        10
  90. FILE    *sys_handles[MAX_HANDLES];
  91.  
  92. int        findhandle (void)
  93. {
  94.     int        i;
  95.     
  96.     for (i=1 ; i<MAX_HANDLES ; i++)
  97.         if (!sys_handles[i])
  98.             return i;
  99.     Sys_Error ("out of handles");
  100.     return -1;
  101. }
  102.  
  103. /*
  104. ================
  105. filelength
  106. ================
  107. */
  108. int filelength (FILE *f)
  109. {
  110.     int        pos;
  111.     int        end;
  112.  
  113.     pos = ftell (f);
  114.     fseek (f, 0, SEEK_END);
  115.     end = ftell (f);
  116.     fseek (f, pos, SEEK_SET);
  117.  
  118.     return end;
  119. }
  120.  
  121. int Sys_FileOpenRead (char *path, int *hndl)
  122. {
  123.     FILE    *f;
  124.     int        i;
  125.     
  126.     i = findhandle ();
  127.  
  128.     printf ("Opening '%s' for read\n", path);
  129.     f = fopen(path, "rb");
  130.     if (!f)
  131.     {
  132.         *hndl = -1;
  133.         return -1;
  134.     }
  135.     sys_handles[i] = f;
  136.     *hndl = i;
  137.     
  138.     return filelength(f);
  139. }
  140.  
  141. int Sys_FileOpenWrite (char *path)
  142. {
  143.     FILE    *f;
  144.     int        i;
  145.     
  146.     i = findhandle ();
  147.  
  148.     printf ("Opening '%s' for write\n", path);
  149.     f = fopen(path, "wb");
  150.     if (!f)
  151.         Sys_Error ("Error opening %s: %s", path,strerror(errno));
  152.     sys_handles[i] = f;
  153.     
  154.     return i;
  155. }
  156.  
  157. void Sys_FileClose (int handle)
  158. {
  159.     fclose (sys_handles[handle]);
  160.     sys_handles[handle] = NULL;
  161. }
  162.  
  163. void Sys_FileSeek (int handle, int position)
  164. {
  165.     /* printf ("%d: Seeking to %d\n", handle, position); */
  166.     if (fseek (sys_handles[handle], position, SEEK_SET) == -1)
  167.         Sys_Error ("Error in fseek()");
  168. }
  169.  
  170. int Sys_FileRead (int handle, void *dest, int count)
  171. {
  172.     /* printf ("%d: Reading %d to %08x\n", handle, count, dest); */
  173.     return (int)fread (dest, 1, count, sys_handles[handle]);
  174. }
  175.  
  176. int Sys_FileWrite (int handle, void *data, int count)
  177. {
  178.     return (int)fwrite (data, 1, count, sys_handles[handle]);
  179. }
  180.  
  181. int    Sys_FileTime (char *path)
  182. {
  183.     FILE    *f;
  184.     
  185.     f = fopen(path, "rb");
  186.     if (f)
  187.     {
  188.         fclose(f);
  189.         return 1;
  190.     }
  191.     
  192.     return -1;
  193. }
  194.  
  195. void Sys_mkdir (char *path)
  196. {
  197. }
  198.  
  199.  
  200. /*
  201. ===============================================================================
  202.  
  203. SYSTEM IO
  204.  
  205. ===============================================================================
  206. */
  207.  
  208. void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
  209. {
  210. }
  211.  
  212.  
  213. void Sys_DebugLog(char *file, char *fmt, ...)
  214. {
  215. }
  216.  
  217. void Sys_Error (char *error, ...)
  218. {
  219.     va_list        argptr;
  220.  
  221.     printf ("Sys_Error: ");    
  222.     va_start (argptr,error);
  223.     vprintf (error,argptr);
  224.     va_end (argptr);
  225.     printf ("\n");
  226.  
  227.     exit (20);
  228. }
  229.  
  230. void Sys_Printf (char *fmt, ...)
  231. {
  232.     va_list        argptr;
  233.     
  234.     va_start (argptr,fmt);
  235.     if (!con_initialized)
  236.       vprintf (fmt,argptr);
  237.     va_end (argptr);
  238. }
  239.  
  240. void Sys_Quit (void)
  241. {
  242.     exit (0);
  243. }
  244.  
  245. double Sys_FloatTime (void)
  246. {
  247. #ifndef __PPC__
  248.   static unsigned int basetime=0;
  249.   unsigned int clock[2];
  250.  
  251.   timer (clock);
  252.   if (!basetime)
  253.     basetime = clock[0];
  254.   return (clock[0]-basetime) + clock[1] / 1000000.0;
  255. #else
  256.   unsigned int clock[2];
  257.  
  258.   ppctimer (clock);
  259. #ifdef __VBCC__  /* work around bug in VBCC */
  260.   return (((double)clock[0]) * (2147483648.0 + 2147483648.0) +
  261.           (double)clock[1]) * clocks2secs;
  262. #else
  263.   return (((double)clock[0]) * 4294967296.0 + (double)clock[1]) *
  264.          clocks2secs;
  265. #endif
  266. #endif
  267. }
  268.  
  269. char *Sys_ConsoleInput (void)
  270. {
  271.     return NULL;
  272. }
  273.  
  274. void Sys_Sleep (void)
  275. {
  276. }
  277.  
  278. /*
  279. void Sys_SendKeyEvents (void)
  280. {
  281. }
  282. */
  283.  
  284. void Sys_HighFPPrecision (void)
  285. {
  286. }
  287.  
  288. void Sys_LowFPPrecision (void)
  289. {
  290. }
  291.  
  292. //=============================================================================
  293.  
  294. int main (int argc, char **argv)
  295. {
  296.   int j;
  297.   double time, oldtime, newtime;
  298.   quakeparms_t parms;
  299.  
  300. //  printf ("Stack size is %d\n", stacksize());
  301.  
  302.   memset (&parms, 0, sizeof(parms));
  303.  
  304.   COM_InitArgv (argc, argv);
  305.  
  306.   parms.memsize = 8*1024*1024;
  307.   j = COM_CheckParm("-mem");
  308.   if (j)
  309.     parms.memsize = (int) (Q_atof(com_argv[j+1]) * 1024 * 1024);
  310.  
  311.   if ((parms.membase = malloc (parms.memsize)) == NULL)
  312.     Sys_Error ("Can't allocate %d bytes", parms.memsize);
  313. //  parms.basedir = "QUAKE:";
  314. //  parms.basedir = "";
  315.   parms.basedir = "PROGDIR:";
  316.   parms.cachedir = "";
  317.  
  318.   parms.argc = com_argc;
  319.   parms.argv = com_argv;
  320.  
  321. #ifdef __PPC__
  322.  
  323. #ifdef __SASC
  324.   {
  325.   int i;
  326.   ULONG ipll, ipll2;
  327.   double pll;
  328.  
  329.   cpu_type = PPCGetAttr (PPCINFOTAG_CPU);
  330.   switch (cpu_type) {
  331.     case 3:
  332.       printf ("\nCPU is PPC603 ");
  333.       break;
  334.     case 4:
  335.       printf ("\nCPU is PPC604 ");
  336.       break;
  337.     case 5:
  338.       printf ("\nCPU is PPC602 ");
  339.       break;
  340.     case 6:
  341.       printf ("\nCPU is PPC603e ");
  342.       break;
  343.     case 7:
  344.       printf ("\nCPU is PPC603e+ ");
  345.       break;
  346.     case 9:
  347.       printf ("\nCPU is PPC604e ");
  348.       break;
  349.     default:
  350.       printf ("\nCPU is PPC ");
  351.       break;
  352.   }
  353.  
  354.   bus_clock = PPCGetAttr (PPCINFOTAG_CPUCLOCK);
  355.   printf ("running at %d MHz ", bus_clock);
  356.   if (!bus_clock)
  357.     bus_clock = 233333333;
  358.   else
  359.     bus_clock = bus_clock * 1000000;
  360.   ipll = PPCGetAttr (PPCINFOTAG_CPUPLL);
  361.   if ((ipll & 0xf0000000) && !(ipll & 0x0ffffff0))
  362.     ipll2 = ipll >> 28;     /* work around bug in ppc.library */
  363.   else
  364.     ipll2 = ipll & 0x0000000f;
  365.   switch (ipll2) {    /* see http://mx1.xoom.com/silicon/docs/ppc_pll.html */
  366.     case 0:
  367.     case 1:
  368.     case 2:
  369.     case 3:
  370.       pll = 1.0;            // PLL is 1:1 (or bypassed)
  371.       break;
  372.     case 4:
  373.     case 5:
  374.       pll = 2.0;            // PLL is 2:1
  375.       break;
  376.     case 6:
  377.       pll = 2.5;
  378.       break;
  379.     case 7:
  380.       pll = 4.5;
  381.       break;
  382.     case 8:
  383.     case 9:
  384.       pll = 3.0;            // PLL is 3:1
  385.       break;
  386.     case 10:
  387.       pll = 4.0;
  388.       break;
  389.     case 11:
  390.       pll = 5.0;
  391.       break;
  392.     case 12:
  393.       if ((cpu_type == 4) || (cpu_type == 8))
  394.         pll = 1.5;            // PLL is 1.5:1
  395.       else
  396.         pll = 4.0;            // PLL is 4:1
  397.       break;
  398.     case 13:
  399.       pll = 6.0;            // PLL is 6:1
  400.       break;
  401.     case 14:
  402.       pll = 3.5;            // PLL is 3.5:1
  403.       break;
  404.     default:
  405.       pll = 3.0;
  406.       break;
  407.   }
  408.   printf ("using a PLL divisor of %3.1f.  (%08x)\n", pll, ipll);
  409.  
  410.   i = COM_CheckParm ("-bus");
  411.   if (i && i < com_argc-1) {
  412.     bus_clock = atoi(com_argv[i+1]);
  413.   } else {
  414.     bus_clock = (int)((double)bus_clock / pll);
  415.   }
  416.  
  417.   bus_MHz = bus_clock / 1000000;
  418.   printf("Bus clock is %d MHz.\n\n", bus_MHz);
  419.  
  420.   clocks2secs = 4.0 / bus_clock;
  421.   }
  422. #endif /* __SASC */
  423.  
  424. #if defined(__STORM__) || defined(__VBCC__)
  425.   {
  426.     struct TagItem ti_cputype[] = {{GETINFO_CPU, 0}, {TAG_END, 0}};
  427.     struct TagItem ti_cpuclock[] = {{GETINFO_CPUCLOCK, 0}, {TAG_END, 0}};
  428.     struct TagItem ti_busclock[] = {{GETINFO_BUSCLOCK, 0}, {TAG_END, 0}};
  429.  
  430.     GetInfo (ti_cputype);
  431.     cpu_type = ti_cputype[0].ti_Data;
  432.     switch (cpu_type) {
  433.       case CPUF_603:
  434.         printf ("\nCPU is PPC603 ");
  435.         break;
  436.       case CPUF_604:
  437.         printf ("\nCPU is PPC604 ");
  438.         break;
  439.       case CPUF_603E:
  440.         printf ("\nCPU is PPC603e ");
  441.         break;
  442.       case CPUF_604E:
  443.         printf ("\nCPU is PPC604e ");
  444.         break;
  445.       case CPUF_620:
  446.         printf ("\nCPU is PPC620 ");
  447.         break;
  448.       default:
  449.         printf ("\nCPU is PPC ");
  450.         break;
  451.     }
  452.  
  453.     GetInfo (ti_cpuclock);
  454.     bus_clock = ti_cpuclock[0].ti_Data;
  455.     printf ("running at %d MHz\n", bus_clock / 1000000);
  456.  
  457.     GetInfo (ti_busclock);
  458.     bus_clock = ti_busclock[0].ti_Data;
  459.     bus_MHz = bus_clock / 1000000;
  460.     printf("Bus clock is %d MHz.\n\n", bus_MHz);
  461.  
  462.     clocks2secs = 4.0 / bus_clock;
  463.  
  464.   }
  465.  
  466. #endif /* __STORM__ */
  467.  
  468. #endif /* __PPC__ */
  469.  
  470. //    printf ("Host_Init\n");
  471.     Host_Init (&parms);
  472.  
  473. //    while (1)
  474. //    {
  475. //        Host_Frame ((float)0.1);
  476. //    }
  477.  
  478.     oldtime = Sys_FloatTime ();
  479.     while (1)
  480.     {
  481. #ifdef __SASC
  482.         chkabort ();
  483. #endif
  484.         newtime = Sys_FloatTime ();
  485.         time = newtime - oldtime;
  486.  
  487.         if (time < 0.0)
  488.             printf ("Negative time = %f!!\n", time);
  489.  
  490.         if (cls.state == ca_dedicated && (time<sys_ticrate.value))
  491.             continue;
  492.  
  493.         Host_Frame ((float)time);
  494.  
  495.         oldtime = newtime;
  496.     }
  497.     return 0;
  498. }
  499.  
  500. /**********************************************************************/
  501. #ifdef __SASC
  502. void _STD_Host_Shutdown (void)
  503. {
  504. //  printf ("_STD_Host_Shutdown\n");
  505.   S_Shutdown();
  506.   Host_Shutdown ();
  507. }
  508. #endif
  509.  
  510. /**********************************************************************/
  511. #ifdef __STORM__
  512. void EXIT_9_Host_Shutdown (void)
  513. {
  514. //  printf ("EXIT_9_Host_Shutdown\n");
  515.   S_Shutdown();
  516.   Host_Shutdown ();
  517. }
  518. #endif
  519.  
  520. /**********************************************************************/
  521. #ifdef __VBCC__
  522. void _EXIT_9_Host_Shutdown (void)
  523. {
  524. //  printf ("_EXIT_9_Host_Shutdown\n");
  525.   S_Shutdown();
  526.   Host_Shutdown ();
  527. }
  528. #endif
  529.  
  530. /**********************************************************************/
  531.