home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / msc7.zip / DPMIINT.CPP < prev    next >
C/C++ Source or Header  |  1992-02-17  |  3KB  |  137 lines

  1. //
  2. // (c) Copyright 1992, Qualitas, Inc.  All Rights Reserved
  3. //
  4. // dpmiint.cpp - member functions for InterruptHandler and 
  5. //         RealInterruptHandler classes
  6. //
  7. #include <dos.h>
  8. #include "dpmiint.h"
  9.  
  10. boolean dispatchInitialized=FALSE;
  11.  
  12. extern "C" {
  13.     void initDispatch(void);
  14.     void (_interrupt *setDispatch(int ord, void (*)(dpmiRegs_t), 
  15.                     int, void (**)(dpmiRegs_t)))();
  16.     void callHandler(dpmiRegs_t *, void far *);
  17. };
  18.  
  19.  
  20. //
  21. // InterruptHandler class
  22. //
  23. //     Constructor
  24. //
  25. InterruptHandler::InterruptHandler(uChar ord, void (*isrAddress)(dpmiRegs_t))
  26. {
  27.     selector_t codeAlias;
  28.     uShort far *pFixup;
  29.  
  30.     if (!dispatchInitialized)
  31.     {
  32.         initDispatch();
  33.         dispatchInitialized = TRUE;
  34.     }
  35.     ordinal = ord;
  36.     mode = 1;
  37.  
  38.     // get the current vector for the interrupt from DPMI;
  39.  
  40.     DPMIGetProtInterruptVector(ord,
  41.             (void (_interrupt **)())&previousVector);
  42.  
  43.     // assume (since this is a small model implementation) that
  44.     // the vector being set up is in the same code segment
  45.  
  46.     DPMISetProtInterruptVector(ord, 
  47.             setDispatch(ord, isrAddress, 1, &prevDispatch));
  48. }
  49.  
  50. //
  51. //  This constructor is provided only as a base constructor for the
  52. //  RealInterruptHandler class.  
  53. //
  54. InterruptHandler::InterruptHandler(uChar ord)
  55. {
  56.     ordinal = ord;
  57. }
  58.  
  59. //
  60. // Destructor
  61. //
  62. InterruptHandler::~InterruptHandler(void)
  63. {
  64.     if (FP_SEG(previousVector) == theCodeSel)
  65.       setDispatch(ordinal, prevDispatch, 1, &prevDispatch);
  66.  
  67.     if (previousVector)
  68.         DPMISetProtInterruptVector(ordinal, 
  69.                     (void (_interrupt*)())previousVector);
  70. }
  71.  
  72.  
  73. //
  74. //
  75. // Call to previous handler
  76. // 
  77. // The previous handler is called after setting up the register as in
  78. // the passed structure.  Care is taken to maintain the internal
  79. // dispatch tables.
  80. //
  81. // This method is inherited and used by the RealInterruptHandler class.
  82. //
  83. void InterruptHandler::callPrevious(dpmiRegs_t& dRegs)
  84. {
  85.     void (*saveDispatch)(dpmiRegs_t);
  86.  
  87.     if (FP_SEG(previousVector) == theCodeSel)
  88.     {
  89.         setDispatch(ordinal, prevDispatch, mode, &saveDispatch);
  90.         callHandler(&dRegs, previousVector);
  91.         setDispatch(ordinal, saveDispatch, mode, &prevDispatch);
  92.     }
  93.     else
  94.         callHandler(&dRegs, previousVector);
  95. }
  96.  
  97.  
  98. //
  99. //
  100. //  Constructor for real mode interrupt handler
  101. //
  102. RealInterruptHandler::RealInterruptHandler(uChar ord,void (*isr)(dpmiRegs_t)):
  103.         InterruptHandler(ord)
  104. {
  105.     if (!dispatchInitialized)
  106.     {
  107.         initDispatch();
  108.         dispatchInitialized = TRUE;
  109.     }
  110.  
  111.     DPMIGetRealInterruptVector(ord, (void (far* *)())&previousVector);
  112.     DPMISetRealInterruptVector(ord,
  113.             (void (far *)())setDispatch(ord, isr, 0, &prevDispatch));
  114.     mode = 0;
  115. }
  116.  
  117. //
  118. //  Destructor for real mode interrupt handler
  119. //
  120. RealInterruptHandler::~RealInterruptHandler(void)
  121. {
  122.     uLong codeBase;
  123.     uShort codePara;
  124.  
  125.     DPMIGetSegmentBase(theCodeSel, &codeBase);
  126.     codePara = codeBase >> 4;
  127.  
  128.     DPMITestDisableInts();
  129.  
  130.     if (FP_SEG(previousVector) == codePara)
  131.       setDispatch(ordinal, prevDispatch, 0, &prevDispatch);
  132.  
  133.     DPMISetRealInterruptVector(ordinal,(void (far*)())previousVector);
  134.  
  135.     previousVector=0;
  136. }
  137.