home *** CD-ROM | disk | FTP | other *** search
/ Best Objectech Shareware Selections / UNTITLED.iso / boss / grap / util / 020 / register.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-20  |  2.8 KB  |  121 lines

  1. /*
  2.     Register.CPP version 1.0
  3.     by Robert Schmidt of Ztiff Zox Softwear 1993
  4.  
  5.     Defines the member functions of the Register class declared in
  6.         Register.hpp.
  7.     Defines the stream operators >> and << to read and write register
  8.         info from istreams/to ostreams in *binary* format.
  9. */
  10.  
  11. #include <dos.h>
  12. #include <iostream.h>
  13. #include "Register.hpp"
  14.  
  15.  
  16. /*
  17.     Register::out()
  18.         Outputs the Register.value to the correct port/index.
  19.         It takes care of handling recognized special cases, like some
  20.         VGA ports, correctly.
  21. */
  22.  
  23. void Register::out()
  24.     {
  25.     switch (port)
  26.         {
  27.         // First handle special cases:
  28.  
  29.         case ATTRCON_ADDR:
  30.             inportb(STATUS_ADDR);          // reset read/write flip-flop
  31.             outportb(ATTRCON_ADDR, index | 0x20);
  32.                                         // ensure VGA output is enabled
  33.             outportb(ATTRCON_ADDR, value);
  34.             break;
  35.  
  36.         case SEQ_ADDR:
  37.         case GRACON_ADDR:
  38.         case CRTC_ADDR:
  39.             outportb(port, index);
  40.             outportb(port+1, value);
  41.             break;
  42.  
  43.         case MISC_ADDR:
  44.         case VGAENABLE_ADDR:
  45.         default:                        // Default is to write the byte
  46.             outportb(port, value);        //    directly to the port
  47.             break;
  48.         }
  49.     }
  50.  
  51. /*
  52.     Register::in()
  53.         Inputs Register.value to the associated hardware register.
  54.         It takes care of handling recognized special cases,
  55.         like some VGA ports, correctly.
  56. */
  57.  
  58. unsigned char Register::in()
  59.     {
  60.     switch (port)
  61.         {
  62.         // First handle special cases:
  63.  
  64.         case MISC_ADDR:
  65.             value = inportb(0x3cc);        // 0x3c2 is write-only, reading
  66.                                         // must be mapped to 0x3cc
  67.             break;
  68.  
  69.         case ATTRCON_ADDR:                // This 1 is odd.  First do a read to
  70.             inportb(STATUS_ADDR);        //    reset the index/data flip-flop.
  71.                                         //    Then give it the index, but:
  72.                                         //    set bit 5!  If cleared, VGA
  73.                                         //    output is disabled!
  74.             outportb(ATTRCON_ADDR, index | 0x20);
  75.             value = inportb(ATTRCON_ADDR+1);
  76.             break;
  77.  
  78.         case SEQ_ADDR:                    // These 3 are similar.  Give it the
  79.         case GRACON_ADDR:                //    register index, then read the
  80.         case CRTC_ADDR:                    //    byte from port+1.
  81.             outportb(port, index);
  82.             value = inportb(port+1);
  83.             break;
  84.  
  85.         case VGAENABLE_ADDR:
  86.         default:                        // Default is to read the byte
  87.             value = inportb(port);        //    directly from the port
  88.             break;
  89.         }
  90.  
  91.     return value;                            // Return value of first reg.
  92.     }
  93.  
  94. /*
  95.     The following stream operators operate on Registers in *binary*
  96.     format, i.e. they are not suitable for communicating with text streams
  97.     like the keyboard or the screen (cin/cout).
  98. */
  99.  
  100. istream& operator>> (istream &in, Register &r)
  101.     {
  102.     r.port = unsigned(in.get()) | (unsigned(in.get()) << 8);
  103.     r.index = in.get();
  104.     r.value = in.get();
  105.  
  106.     return in;
  107.     }
  108.  
  109. ostream& operator<< (ostream &out, Register &r)
  110.     {
  111.     out.put(char(r.port));
  112.     out.put(char(r.port>>8));
  113.     out.put(r.index);
  114.     out.put(r.value);
  115.  
  116.     return out;
  117.     }
  118.  
  119.  
  120.  
  121.