home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctchnqs / 1991 / number4 / ports.h < prev    next >
Text File  |  1991-08-12  |  2KB  |  71 lines

  1. // This code is public domain. 
  2.  
  3. // Get prototypes for inport and outport functions. 
  4. #include <dos.h> 
  5.  
  6. typedef unsigned char byte; 
  7. typedef unsigned short word; 
  8.  
  9. // Byte port. 
  10. class bytePort { 
  11.   word _addr;   // Port address. 
  12.  
  13.   public: 
  14.  
  15.   // Constructors and destructor. 
  16.   bytePort(word addr) : _addr(addr) { } 
  17.   bytePort(const bytePort &p) : _addr(p._addr) { } 
  18.   bytePort() { } 
  19.   ~bytePort() { } 
  20.  
  21.   // Set port address. 
  22.   void addr(word addr) { 
  23.   _addr = addr; 
  24.   } 
  25.  
  26.   // Get port address. 
  27.   word addr() const { 
  28.   return _addr; 
  29.   } 
  30.  
  31.   // Read byte value from port. 
  32.   operator byte() const { 
  33.   return inportb(_addr); 
  34.   } 
  35.  
  36.   // Write byte value to port. 
  37.   bytePort& operator=(byte value) const { 
  38.   outportb(_addr, value); 
  39.   return *this; 
  40.   } 
  41.  
  42.   // Set bits at port. 
  43.   bytePort& operator|=(byte value) const { 
  44.   outportb(_addr, inportb(_addr) | value); 
  45.   return *this; 
  46.   } 
  47.  
  48.   // Mask bits at port. 
  49.   bytePort& operator&=(byte value) const { 
  50.   outportb(_addr, inportb(_addr) & value); 
  51.   return *this; 
  52.   } 
  53.  
  54.   // Toggle bits at port. 
  55.   bytePort& operator^=(byte value) const { 
  56.   outportb(_addr, inportb(_addr) ^ value); 
  57.   return *this; 
  58.   } 
  59.  
  60.   // Copy value from one port to another. 
  61.   bytePort& operator=(const bytePort& p) const { 
  62.   outportb(_addr, inportb(p._addr)); 
  63.   return *this; 
  64.   } 
  65.  
  66.   // Treat port as base of an array of ports. 
  67.   bytePort operator[](int index) const { 
  68.   return _addr + index; 
  69.   } 
  70.   }; 
  71.