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

  1. //
  2. // (C) Copyright 1992 Qualitas, Inc.  All rights reserved.
  3. //
  4. // xintr.cpp - example usage of interrupt classes in DPMI class library
  5. //
  6.  
  7. #include <iostream.h>
  8. #include <iomanip.h>
  9. #include <dos.h>
  10. #include "dpmihost.h"
  11. #include "dpmiint.h"
  12.  
  13. InterruptHandler* pTimerHandler;    // pointer to timer handler object
  14. RealInterruptHandler* pKeyboardHandler; // pointer to keybd handler object
  15.  
  16. void click(void);
  17. void speakerOn(uShort);
  18. void speakerOff(void);
  19.  
  20. unsigned volatile nTicks=0;
  21. unsigned volatile nKeys=0;
  22.  
  23. //
  24. // This is the protected mode timer tick interrupt handler
  25. //
  26. void  tickHandler(dpmiRegs_t iFrame)
  27. {
  28.     pTimerHandler->callPrevious(iFrame);
  29.     nTicks++;                // inc tick counter
  30.     speakerOff();
  31. }
  32.  
  33. //
  34. // This is the real mode keyboard interrupt handler
  35. //
  36. void keybHandler(dpmiRegs_t iFrame)
  37. {
  38.     pKeyboardHandler->callPrevious(iFrame);
  39.  
  40.     nKeys++;        // inc key counter
  41.  
  42.     if (nKeys % 2)        // make a click every other interrupt
  43.         click();
  44. }
  45.  
  46. //
  47. // Interrupt test.  This function creates a protected mode timer tick
  48. // handler and a real mode keyboard interrupt handler.  The example
  49. // counts the number of ticks and keystrokes that occur during the
  50. // input of a string.
  51. //
  52. void TestInterrupts(void)
  53. {
  54.     char buf[256];
  55.  
  56.     // install protected mode timer tick handler
  57.     pTimerHandler    = new InterruptHandler(8, tickHandler);
  58.  
  59.     // install real mode keyboard interrupt handler
  60.     pKeyboardHandler = new RealInterruptHandler(9, keybHandler);
  61.  
  62.     cout << "Enter your name: ";
  63.     nTicks = 0;
  64.     nKeys = 0;
  65.     cin >> buf;
  66.  
  67.     // unhook handlers
  68.     delete pTimerHandler;
  69.     delete pKeyboardHandler;
  70.     speakerOff();
  71.  
  72.     cout << "It took you " << (nTicks*10)/182 << "." 
  73.          <<  ((nTicks*100)/182) % 10 << " seconds and " << nKeys/2 
  74.          << " keystrokes\n";
  75. }
  76.  
  77. void main(void)
  78. {
  79.     DPMIhost dpmi;
  80.  
  81.     if (dpmi.getStatus() == DPMIok)
  82.     {
  83.         dpmi.enterProtectedMode();
  84.         TestInterrupts();
  85.     }
  86.     else
  87.         cout << "DPMI host not present\n";
  88. }
  89.     
  90.  
  91. void speakerOn(uShort freq)
  92. {
  93.     _asm in  al, 61h
  94.     _asm test al, 3
  95.     _asm jnz setF
  96.     _asm or  al, 3
  97.     _asm out 61h, al
  98. setF:
  99.     _asm mov dx, freq
  100.     _asm mov al, 0b6h
  101.     _asm out 43h, al
  102.     _asm mov al, dl
  103.     _asm out 42h, al
  104.     _asm mov al, dh
  105.     _asm out 42h, al
  106. }
  107.  
  108. void speakerOff(void)
  109. {
  110.     _asm in  al, 61h
  111.     _asm and al, 0fch
  112.     _asm out 61h, al
  113. }
  114.  
  115. void click(void)
  116. {
  117.     unsigned ticks = nTicks;
  118.  
  119.     ticks = nTicks;
  120.     _asm sti
  121.  
  122.     while (ticks == nTicks)
  123.         ;
  124.     speakerOn(0x9000);
  125. }
  126.  
  127.