ROTTNET.C 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. Copyright (C) 1994-1995 Apogee Software, Ltd.
  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 <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <process.h>
  19. #include <dos.h>
  20. #include <conio.h>
  21. #include "c:\merge\rottnet.h"
  22. #include "global.h"
  23. #if ROTTSER
  24. #include "port.h"
  25. #endif
  26. rottcom_t rottcom;
  27. int vectorishooked=0;
  28. boolean pause;
  29. void interrupt (*oldrottvect) (void);
  30. static char * rottnet_stack;
  31. static unsigned short rottnet_stacksegment;
  32. static unsigned short rottnet_stackpointer;
  33. static unsigned short old_stacksegment;
  34. static unsigned short old_stackpointer;
  35. #define ROTTNET_STACKSIZE (2048)
  36. #define ROTTNET_SAFETYMARGIN (8)
  37. /*
  38. =============
  39. =
  40. = SetupROTTCOM
  41. =
  42. =============
  43. */
  44. void SetupROTTCOM ( void )
  45. {
  46. unsigned char far *vectorptr;
  47. long vector;
  48. char * topofstack;
  49. vector=GetVector();
  50. /* Get an interrupt vector if not already set */
  51. if (vector == -1)
  52. {
  53. for (vector = 0x60 ; vector <= 0x66 ; vector++)
  54. {
  55. vectorptr = *(unsigned char far * far *)(vector*4);
  56. if ( !vectorptr || *vectorptr == 0xcf )
  57. break;
  58. }
  59. if (vector == 0x67)
  60. {
  61. printf ("Warning: no NULL or iret interrupt vectors were found in the 0x60 to 0x66\n"
  62. "range. You can specify a vector with the -vector parameter\n"
  63. "Press a key to continue...\n");
  64. getch ();
  65. printf ("Using default vector 0x66\n");
  66. vector = 0x66;
  67. }
  68. }
  69. rottcom.intnum = (short)vector;
  70. // allocate the rottnet stack
  71. rottnet_stack = malloc(ROTTNET_STACKSIZE);
  72. if (!rottnet_stack)
  73. Error("Could not allocate stack");
  74. // Calculate top of stack
  75. topofstack = rottnet_stack + ROTTNET_STACKSIZE - ROTTNET_SAFETYMARGIN;
  76. // Determine stack segment and pointer
  77. rottnet_stacksegment = FP_SEG( (char huge *)topofstack );
  78. rottnet_stackpointer = FP_OFF( (char huge *)topofstack );
  79. }
  80. /*
  81. =============
  82. =
  83. = ShutdownROTTCOM
  84. =
  85. =============
  86. */
  87. void ShutdownROTTCOM ( void )
  88. {
  89. if (vectorishooked)
  90. setvect (rottcom.intnum,oldrottvect);
  91. vectorishooked=0;
  92. free ( rottnet_stack );
  93. }
  94. /*
  95. =============
  96. =
  97. = GetVector
  98. =
  99. =============
  100. */
  101. long GetVector (void)
  102. {
  103. unsigned char far * vector;
  104. long intnum;
  105. if (CheckParm ("-vector"))
  106. {
  107. intnum = sscanf ("0x%x",_argv[CheckParm("-vector")+1]);
  108. vector = *(unsigned char far * far *)(intnum*4);
  109. if (vector != NULL && *vector != 0xcf)
  110. Error("The specified vector (0x%02x) was already hooked.\n", intnum);
  111. return intnum;
  112. }
  113. else
  114. return -1;
  115. }
  116. /*
  117. =============
  118. =
  119. = ROTTNET_ISR
  120. =
  121. =============
  122. */
  123. #define GetStack(a,b) \
  124. { \
  125. *a = _SS; \
  126. *b = _SP; \
  127. }
  128. #define SetStack(a,b) \
  129. { \
  130. _SS=a; \
  131. _SP=b; \
  132. }
  133. void interrupt ROTTNET_ISR (void)
  134. {
  135. //
  136. // Get current stack
  137. //
  138. GetStack( &old_stacksegment, &old_stackpointer );
  139. //
  140. // Set the local stack
  141. //
  142. SetStack( rottnet_stacksegment, rottnet_stackpointer );
  143. //
  144. // call the interrupt service routine
  145. //
  146. NetISR();
  147. //
  148. // Restore the old stack
  149. //
  150. SetStack( old_stacksegment, old_stackpointer );
  151. }
  152. /*
  153. =============
  154. =
  155. = LaunchROTT
  156. =
  157. =============
  158. */
  159. void LaunchROTT (void)
  160. {
  161. char *newargs[99];
  162. char adrstring[10];
  163. long flatadr;
  164. int argnum = 1;
  165. int i;
  166. SetupROTTCOM ();
  167. // prepare for ROTT
  168. oldrottvect = getvect (rottcom.intnum);
  169. setvect (rottcom.intnum,ROTTNET_ISR);
  170. vectorishooked = 1;
  171. // set ticstep
  172. if (rottcom.gametype == 0) // modem game
  173. {
  174. rottcom.ticstep = 2; // skip every other tic
  175. }
  176. else // must be a network game
  177. {
  178. rottcom.ticstep = 1; // use every tic
  179. }
  180. // build the argument list for ROTT, adding "-net" and the address of rottcom.
  181. for (i=1;i<_argc;i++)
  182. newargs [argnum++] = _argv[i];
  183. newargs [argnum++] = "now";
  184. #if ROTTSER
  185. if (Is8250())
  186. newargs [argnum++] = "IS8250";
  187. #endif
  188. newargs [argnum++] = "-net";
  189. /* Add address of rottcom structure */
  190. flatadr = (long)_DS*16 + (unsigned)&rottcom;
  191. sprintf (adrstring,"%lu",flatadr);
  192. newargs [argnum++] = adrstring;
  193. newargs [argnum] = NULL;
  194. newargs [0] = ROTTLAUNCHER;
  195. if (pause==true)
  196. {
  197. printf ("About to launch %s -- Passing these arguments:\n",ROTTLAUNCHER);
  198. for (i = 0; i < argnum; i++)
  199. printf (" arg %d = %s\n", i, newargs [i]);
  200. printf (" player = %d\n", rottcom.consoleplayer);
  201. printf ("\nPress ESC to abort, or any other key to continue...");
  202. if (getch () == ESC)
  203. {
  204. printf ("\n\n");
  205. return;
  206. }
  207. }
  208. spawnv (P_WAIT, ROTTLAUNCHER, newargs);
  209. printf ("\nReturned from ROTT\n\n");
  210. ShutdownROTTCOM();
  211. }