home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / rng-810.zip / start.cpp < prev    next >
C/C++ Source or Header  |  1994-01-28  |  6KB  |  192 lines

  1. /*
  2.  RANDDRV.SYS random number driver for RNG-810 random # or similar.
  3.  Copyright (C) 1994 Paul Elliot
  4.  
  5.  This program is free software; you can redistribute it and/or
  6.  modify it under the terms of the GNU General Public License
  7.  as published by the Free Software Foundation; either version 2
  8.  of the License, or (at your option) any later version.
  9.  
  10.  This program is distributed in the hope that it will be useful,
  11.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. Paul.Elliott@Hrnowl.LoneStar.Org
  20.  
  21. Paul Elliott
  22.  
  23. 3986 South Gessner #224 Houston TX 77063
  24.  
  25. */
  26.  
  27. /* This is initialization code required by this specific
  28. io driver */
  29. #include <stdio.h>
  30. #include <stdarg.h>
  31. #include <string.h>
  32. #include <mem.h>
  33. #include <dos.h>
  34. #include "randdrv.h"
  35. #include "rand.h"
  36.  
  37. #define majorVersion     1
  38. #define minorVersion    00
  39. #define revision        'a'
  40. // address indicates call back address used to call back device
  41. // helper routines.
  42.  
  43. extern dword near _devHlp;
  44.  
  45. extern word near Error( RequestPacket far *requestPacket );
  46. extern DispatchFunctionType dispatchTable[] ;
  47.  
  48. // status to return.
  49. const word done = 0x0100;                        //
  50. const word error = 0x8000;                        // status return bit masks
  51. const word generalDeviceError =        0x0c;        //
  52. const word unknownCommand =         0x03;        // status error codes
  53.  
  54. // port to access to read random data.
  55. extern unsigned int near port;
  56.  
  57. // size of random number buffers
  58. #define BUF_SIZE 512
  59. // buferr to hold random numbers comming form timer routine
  60. extern char near buf[BUF_SIZE];
  61. // start of above buffer
  62. extern char near * near  buf_start;
  63. // one byte past above buffer
  64. extern char near * near buf_end;
  65. // address of next character to read into the buffer
  66. extern char near * near current_input;
  67. // addres of next character in buffer to give to application.
  68. extern char near * near current_output;
  69.  
  70. // This is the number of bytes of the buffer that we are currently
  71. // moveing to various applications.
  72. extern volatile word near pending;
  73. // This is the number of bytes in buffer that
  74. // do not contain unused saved random numbers that have not been given
  75. // to a app yet
  76. extern volatile word near unused_in_buffer;
  77. // number of bytes this request is waiting for..
  78. // when this goes to zero, wake blocked thread.
  79. extern volatile signed int near waiting_for ;
  80.  
  81.  
  82.  
  83. // number of open files.
  84. extern unsigned short near open_count;
  85. // This is the number of bytes of the buffer that we are currently
  86. // moveing to various applications.
  87. extern volatile word near pending;
  88.  
  89.  
  90. // prints during init call.
  91. int printf( const char *fmt, ... ){
  92.     // printf that is re-entrant and doesn't call any floating point,
  93.     // uses OS/2 DosWrite and has a maximum string length of 255
  94.     // after % expansion
  95.  
  96.     va_list args;
  97.     static char buf[256];
  98.     int len;
  99.     unsigned int action;
  100.     va_start( args, fmt );
  101.     len = vsprintf( buf, fmt, args );
  102.     DosWrite( OS2stdout, buf, len, &action );
  103.     return len;
  104. }
  105.  
  106.  
  107. /***************************************************************************/
  108. // init routine called when driver is initialized.
  109. word Init( RequestPacket far *requestPacket ){
  110.     // Initialise the device driver. We are allowed to use 
  111.     // certain Dosxxx functions only while receiving an ini
  112.     // request packet. The init packet contains the address of the
  113.     // devHlp routine and we return the length of the code and data
  114.     // segments. Typically in assembly drivers the init handling code
  115.     // is jettisoned. Unfortunately if you wish to use things like 
  116.     // printf and other code in the libraries it is a bit harder to
  117.     // do this. It would be possible but the library code would have to
  118.     // be recompiled and the segment names or classes of code that
  119.     // would be removed would have to be changed and the order carefully
  120.     // inspected. 
  121.     
  122.     // save the device helper routine.
  123.     _devHlp = requestPacket->initEntry.devHlpEntry;    // save the devhlp pointer, needed by the devhlp functions
  124.  
  125.     // save the end of the code segment (offset).
  126.     requestPacket->initReturn.csLength = FP_OFF( &CodeEnd );
  127.     // save the end of the data segment (offset).
  128.     requestPacket->initReturn.dsLength = FP_OFF( &memoryEnd );
  129.  
  130.  
  131.     // The above means that data beond CodeEnd and memoryEnd
  132.     // will not exist after this call.
  133.  
  134.     
  135.     // initialize data shared with
  136.     // rand.asm
  137.  
  138.     // limits of our buffer
  139.     buf_start=buf;
  140.     buf_end=buf+BUF_SIZE;
  141.  
  142.     // current buffer input
  143.     current_input=buf;
  144.  
  145.     // current buffer output
  146.     current_output=buf;
  147.  
  148.     // number of bytes pending to be transfered to various read requests.
  149.     pending=0;
  150.  
  151.     // number of bytes in buffer that to not have unused, saved
  152.     // random bytes.
  153.     unused_in_buffer=BUF_SIZE;
  154.  
  155.  
  156.     // waiting for is the number of bytes request is waiting
  157.     // for when goes to 0 unblock blocked threads
  158.     waiting_for = 0;
  159.     
  160.     // number of open files
  161.     open_count=0;
  162.  
  163.     // port address
  164.     port=0x300;
  165.  
  166.     // read in port address from command line.
  167.     strcpy(buf,requestPacket->initEntry.initArgs);
  168.     sscanf(buf,"%*s%i",(int far *)&port);
  169.  
  170.     // print message
  171.     printf("\r\nRandom Number Device Driver Version: %d.%02d%c\r\n"
  172.         "RANDDRV version 00, Copyright (C) 1984 Paul Elliott\r\n"
  173.         "RANDDRV comes with ABSOLUTELY NO WARRANTY; for details\r\n"
  174.         "see documentation.  This is free software, and you are welcome\r\n"
  175.         "to redistribute it under certain conditions; see docs for details.\r\n" ,
  176.         majorVersion, minorVersion, revision );
  177.  
  178.     // start interrrupt timer.
  179.     // TimerOn();  // don't start here. start on open
  180.  
  181.     // init routine can only be called once
  182.     // goes out of memory when first called.
  183.     // remove the vector to this routine from dispatch table.
  184.     dispatchTable[0] = Error;
  185.     // this routine will cease to exist after this call
  186.     // its memory returned to system.
  187.  
  188.     return done;
  189. }
  190.  
  191.  
  192.