home *** CD-ROM | disk | FTP | other *** search
/ D!Zone (Collector's Edition) / D_ZONE_CD.ISO / programs / editors / serial28 / source.exe / SERSETUP.C < prev    next >
C/C++ Source or Header  |  1994-03-08  |  12KB  |  676 lines

  1. // sersetup.c
  2.  
  3. #include "sersetup.h"
  4. #include "DoomNet.h"
  5.  
  6. extern    que_t        inque, outque;
  7.  
  8. void jump_start( void );
  9. extern int     uart;
  10.  
  11. int            usemodem;
  12. char        startup[256], shutdown[256], baudrate[256], hardhand[256], extirq[256], extport[256],extractics[256];
  13. unsigned char    bauddivl, usehardhand;
  14. int useextirq, useextport, useextractics;
  15. extern int accessMCR;
  16. extern int accessLSR;
  17. extern long intRX;
  18. extern long intTX;
  19.  
  20. void ModemCommand (char *str);
  21.  
  22.  
  23. /*
  24. ================
  25. =
  26. = write_buffer
  27. =
  28. ================
  29. */
  30.  
  31. void write_buffer( char *buffer, unsigned int count )
  32. {
  33.     int    i;
  34.  
  35. // if this would overrun the buffer, throw everything else out
  36.     if (outque.head-outque.tail+count > QUESIZE)
  37.         outque.tail = outque.head;
  38.  
  39.     while (count--)
  40.     {
  41.         outque.data[outque.head&(QUESIZE-1)]=*buffer++;
  42.         outque.head++;
  43.     }
  44.  
  45.     if ( INPUT(accessLSR) & 0x40)
  46.         jump_start();
  47. }
  48.  
  49.  
  50. /*
  51. =================
  52. =
  53. = Error
  54. =
  55. = For abnormal program terminations
  56. =
  57. =================
  58. */
  59.  
  60. void Error (char *error, ...)
  61. {
  62.     va_list argptr;
  63.  
  64.     if (usemodem)
  65.     {
  66.         printf ("\n");
  67.         printf ("\nDropping DTR\n");
  68.         OUTPUT( uart + MODEM_CONTROL_REGISTER
  69.             , INPUT( uart + MODEM_CONTROL_REGISTER ) & ~MCR_DTR );
  70.         delay (1250);
  71.         OUTPUT( uart + MODEM_CONTROL_REGISTER
  72.             , INPUT( uart + MODEM_CONTROL_REGISTER ) | MCR_DTR );
  73.         delay (250);
  74.         ModemCommand("+++");
  75.         delay (1250);
  76.         ModemCommand("AT");
  77.         delay (250);
  78.         ModemCommand("AT");
  79.         delay (250);
  80.         ModemCommand(shutdown);
  81.         delay (1250);
  82.  
  83.     }
  84.  
  85.     ShutdownPort ();
  86.  
  87.     if (vectorishooked)
  88.     {
  89.         printf("Vector is hooked\n");
  90.         setvect (doomcom.intnum,olddoomvect);
  91.     }
  92.  
  93.     printf("Diagnostic Data:\n");
  94.     printf("TX Interrupts: %ld\n",intTX);
  95.     printf("TX Characters: %ld\n",outque.tail);
  96.     printf("TX FIFO utilization: %ld%%\n",intTX*100/(outque.tail+1));
  97.  
  98.     printf("RX Interrupts: %ld\n",intRX);
  99.     printf("RX Characters: %ld\n",inque.tail);
  100.     printf("RX FIFO utilization: %ld%%\n",intRX*100/(inque.head+1));
  101.  
  102.  
  103.     if (error)
  104.     {
  105.         va_start (argptr,error);
  106.         vprintf (error,argptr);
  107.         va_end (argptr);
  108.         printf ("\n");
  109.         exit (1);
  110.     }
  111.  
  112.     printf ("Clean exit from SERSETUP\n");
  113.     exit (0);
  114. }
  115.  
  116.  
  117. /*
  118. ================
  119. =
  120. = ReadPacket
  121. =
  122. ================
  123. */
  124.  
  125. #define MAXPACKET    512
  126. #define    FRAMECHAR    0x70
  127.  
  128. char    packet[MAXPACKET];
  129. int        packetlen;
  130. int        inescape;
  131. int        newpacket;
  132.  
  133. boolean ReadPacket (void)
  134. {
  135.     int    c;
  136.  
  137. // if the buffer has overflowed, throw everything out
  138.  
  139.     if (inque.head-inque.tail > QUESIZE - 4)    // check for buffer overflow
  140.     {
  141.         inque.tail = inque.head;
  142.         newpacket = true;
  143.         return false;
  144.     }
  145.  
  146.     if (newpacket)
  147.     {
  148.         packetlen = 0;
  149.         newpacket = 0;
  150.     }
  151.  
  152.     do
  153.     {
  154.         if(inque.tail>=inque.head) return false;
  155.         c=inque.data[inque.tail&(QUESIZE-1)];
  156.         inque.tail++;
  157.         if ((inque.head-inque.tail)<(QUESIZE/2))
  158.             OUTPUT( accessMCR,INPUT(accessMCR) | MCR_RTS);
  159.  
  160.         if (inescape)
  161.         {
  162.             inescape = false;
  163.             if (c!=FRAMECHAR)
  164.             {
  165.                 newpacket = 1;
  166.                 return true;    // got a good packet
  167.             }
  168.         }
  169.         else if (c==FRAMECHAR)
  170.         {
  171.             inescape = true;
  172.             continue;            // don't know yet if it is a terminator
  173.         }                        // or a literal FRAMECHAR
  174.  
  175.         if (packetlen >= MAXPACKET)
  176.             continue;            // oversize packet
  177.         packet[packetlen] = c;
  178.         packetlen++;
  179.     } while (1);
  180.  
  181. }
  182.  
  183.  
  184. /*
  185. =============
  186. =
  187. = WritePacket
  188. =
  189. =============
  190. */
  191.  
  192.  
  193.  
  194. void WritePacket (char *buffer, int len)
  195. {
  196.     if (len > MAXPACKET)
  197.         return;
  198.  
  199.     if (outque.head-outque.tail+len+len>QUESIZE)
  200.             outque.tail=outque.head;
  201.  
  202.     while (len--)
  203.     {
  204.         if (*buffer == FRAMECHAR)
  205.         {
  206.             outque.data[outque.head&(QUESIZE-1)]=FRAMECHAR;
  207.             outque.head++;
  208.         }
  209.         outque.data[outque.head&(QUESIZE-1)]=*buffer++;
  210.         outque.head++;
  211.     }
  212.     outque.data[outque.head&(QUESIZE-1)]=FRAMECHAR;
  213.     outque.head++;
  214.     outque.data[outque.head&(QUESIZE-1)]=0;
  215.     outque.head++;
  216.  
  217.     if ( INPUT( accessLSR ) & 0x40)
  218.         jump_start();
  219. }
  220.  
  221.  
  222. /*
  223. =============
  224. =
  225. = NetISR
  226. =
  227. =============
  228. */
  229.  
  230. void interrupt NetISR (void)
  231. {
  232.     if (doomcom.command == CMD_SEND)
  233.     {
  234.         WritePacket ((char *)&doomcom.data, doomcom.datalength);
  235.     }
  236.     else if (doomcom.command == CMD_GET)
  237.     {
  238.         if (ReadPacket () && packetlen <= sizeof(doomcom.data) )
  239.         {
  240.             doomcom.remotenode = 1;
  241.             doomcom.datalength = packetlen;
  242.             memcpy (&doomcom.data, &packet, packetlen);
  243.         }
  244.         else
  245.             doomcom.remotenode = -1;
  246.     }
  247. }
  248.  
  249.  
  250.  
  251.  
  252. /*
  253. =================
  254. =
  255. = Connect
  256. =
  257. = Figures out who is player 0 and 1
  258. =================
  259. */
  260.  
  261. void Connect (void)
  262. {
  263.     struct time        time;
  264.     int                oldsec;
  265.     int        localstage, remotestage;
  266.     char    str[20];
  267.  
  268. //
  269. // wait for a good packet
  270. //
  271.     printf ("Attempting to connect across serial link, press escape to abort.\n");
  272.  
  273.     oldsec = -1;
  274.     localstage = remotestage = 0;
  275.  
  276.     do
  277.     {
  278.         while ( bioskey(1) )
  279.         {
  280.             if ( (bioskey (0) & 0xff) == 27)
  281.                 Error ("\n\nNetwork game synchronization aborted.");
  282.         }
  283.  
  284.         while (ReadPacket ())
  285.         {
  286.             packet[packetlen] = 0;
  287. //            printf ("read: %s\n",packet);
  288.             if (packetlen != 7)
  289.                 goto badpacket;
  290.             if (strncmp(packet,"PLAY",4) )
  291.                 goto badpacket;
  292.             remotestage = packet[6] - '0';
  293.             localstage = remotestage+1;
  294.             if (packet[4] == '0'+doomcom.consoleplayer)
  295.             {
  296.                 doomcom.consoleplayer ^= 1;
  297.                 localstage = remotestage = 0;
  298.             }
  299.             oldsec = -1;
  300.         }
  301. badpacket:
  302.  
  303.         gettime (&time);
  304.         if (time.ti_sec != oldsec)
  305.         {
  306.             oldsec = time.ti_sec;
  307.             sprintf (str,"PLAY%i_%i",doomcom.consoleplayer,localstage);
  308.             WritePacket (str,strlen(str));
  309. //            printf ("wrote: %s\n",str);
  310.         }
  311.  
  312.     } while (remotestage < 1);
  313.  
  314. //
  315. // flush out any extras
  316. //
  317.     while (ReadPacket ())
  318.     ;
  319. }
  320.  
  321.  
  322.  
  323. /*
  324. ==============
  325. =
  326. = ModemCommand
  327. =
  328. ==============
  329. */
  330.  
  331. void ModemCommand (char *str)
  332. {
  333.     int len;
  334.     printf ("Modem command : %s\n",str);
  335.     for(len=0;len<strlen(str); len++)
  336.     {
  337.      delay(20);
  338.      write_buffer(str+len,1);
  339.     }
  340.     delay(20);
  341.     write_buffer ("\r",1);
  342. }
  343.  
  344.  
  345. /*
  346. ==============
  347. =
  348. = ModemResponse
  349. =
  350. = Waits for OK, RING, CONNECT, etc
  351. ==============
  352. */
  353.  
  354. char    response[80];
  355.  
  356. void ModemResponse (char *resp)
  357. {
  358.     int        c;
  359.     int        respptr;
  360.  
  361.     do
  362.     {
  363.         printf ("Modem response: ");
  364.         respptr=0;
  365.         do
  366.         {
  367.             while ( bioskey(1) )
  368.             {
  369.                 if ( (bioskey (0) & 0xff) == 27)
  370.                     Error ("\nModem response aborted.");
  371.             }
  372.             c = read_byte ();
  373.             if (c==-1)
  374.                 continue;
  375. //            printf("<%c>",c);
  376.  
  377.             if (c=='\n' || respptr == 79)
  378.             {
  379.                 response[respptr] = 0;
  380.                 printf ("%s\n",response);
  381.                 break;
  382.             }
  383.             if (c>=' ')
  384.             {
  385.                 response[respptr] = c;
  386.                 respptr++;
  387.             }
  388.         } while (1);
  389.  
  390.     } while (strncmp(response,resp,strlen(resp)));
  391. }
  392.  
  393.  
  394. /*
  395. =============
  396. =
  397. = ReadLine
  398. =
  399. =============
  400. */
  401.  
  402. void ReadLine (FILE *f, char *dest)
  403. {
  404.     int    c;
  405.  
  406.     do
  407.     {
  408.         c = fgetc (f);
  409.         if (c == EOF)
  410.             Error ("EOF in modem.cfg");
  411.         if (c == '\r' || c == '\n')
  412.             break;
  413.         *dest++ = c;
  414.     } while (1);
  415.     *dest = 0;
  416. }
  417.  
  418.  
  419. /*
  420. =============
  421. =
  422. = SnagBaud
  423. =
  424. =============
  425. */
  426.  
  427. void SnagBaud (void)
  428. {
  429.     int        mcr;
  430.     FILE    *f;
  431.     unsigned long baudtouse;
  432.     unsigned long bauddiv;
  433.  
  434.     f = fopen ("modem.cfg","r");
  435.     if (!f)
  436.         Error ("Couldn't read MODEM.CFG");
  437.     ReadLine (f, startup);
  438.     ReadLine (f, shutdown);
  439.     ReadLine (f, baudrate);
  440.     ReadLine (f, hardhand);
  441.     ReadLine (f, extirq);
  442.     ReadLine (f, extport);
  443.     ReadLine (f, extractics);
  444.     fclose (f);
  445.     sscanf(baudrate,"%ld",&baudtouse);
  446.     if (hardhand[0]=='0') usehardhand=0; else usehardhand=1;
  447.     if ((baudtouse!=9600)&&
  448.             (baudtouse!=19200)&&
  449.             (baudtouse!=38400)&&
  450.             (baudtouse!=57600)&&
  451.             (baudtouse!=115200)) baudtouse=9600;
  452.             bauddiv=1843200/(16*baudtouse);
  453.     bauddivl=(unsigned char)(bauddiv%0x0F);
  454.     printf("Using %ld baud to modem. BaudDivisorL=%u\n",baudtouse,bauddivl);
  455.     if (usehardhand)
  456.         printf("Using hardware handshaking.\n");
  457.     else
  458.         printf("Not using hardware handshaking.\n");
  459.     sscanf(extirq,"%d",&useextirq);
  460.     if (!((useextirq>=2)&&(useextirq<=15)))
  461.         useextirq=0;
  462.     if (useextirq)
  463.         printf("Using irq: %x from modem.cfg file.\n");
  464.     sscanf(extport,"%x",&useextport);
  465.     if (useextport==0) sscanf(extport,"%X",&useextport);
  466.     if (useextport)
  467.         printf("Using port: 0x%x from mode