home *** CD-ROM | disk | FTP | other *** search
/ modiromppu / modiromppu.iso / PROGRAMS / ORGPACKS / MPG12304.ZIP / MIRQ.C < prev    next >
C/C++ Source or Header  |  1995-12-12  |  2KB  |  100 lines

  1. /*
  2.  
  3. Name:
  4. MDMA.C
  5.  
  6. Description:
  7. Some general purpose IRQ routines
  8.  
  9. Portability:
  10.  
  11. MSDOS:    BC(y)    Watcom(y)    DJGPP(y)
  12. Win95:    n
  13. Os2:    n
  14. Linux:    n
  15.  
  16. (y) - yes
  17. (n) - no (not possible or not useful)
  18. (?) - may be possible, but not tested
  19.  
  20. */
  21. #include <dos.h>
  22. #include <conio.h>
  23. #include "mirq.h"
  24.  
  25. #define OCR1    0x20            /* 8259-1 Operation control register */
  26. #define IMR1    0x21            /* 8259-1 Mask register */
  27.  
  28. #define OCR2    0xA0            /* 8259-2 Operation control register */
  29. #define IMR2    0xA1            /* 8259-2 Mask register */
  30.  
  31.  
  32. BOOL MIrq_IsEnabled(UBYTE irqno)
  33. /*
  34.     Returns true if the specified hardware irq is enabled.
  35. */
  36. {
  37.     UBYTE imr=(irqno>7) ? IMR2 : IMR1;        /* interrupt mask register */
  38.     UBYTE msk=1<<(irqno&7);                    /* interrupt mask */
  39.     return((inportb(imr) & msk) == 0);
  40. }
  41.  
  42.  
  43. BOOL MIrq_OnOff(UBYTE irqno,UBYTE onoff)
  44. /*
  45.     Use to enable or disable the specified irq.
  46. */
  47. {
  48.     UBYTE imr=(irqno>7) ? IMR2 : IMR1;        /* interrupt mask register */
  49.     UBYTE ocr=(irqno>7) ? OCR2 : OCR1;        /* ocr */
  50.     UBYTE msk=1<<(irqno&7);                    /* interrupt mask */
  51.     UBYTE eoi=0x60|(irqno&7);                /* specific end-of-interrupt */
  52.     BOOL oldstate;
  53.  
  54.     /* save current setting of this irq */
  55.     oldstate=((inportb(imr) & msk) == 0);
  56.  
  57.     if(onoff){
  58.         outportb(imr,inportb(imr) & ~msk);
  59.         outportb(ocr,eoi);
  60.         if(irqno>7) MIrq_OnOff(2,1);
  61.     }
  62.     else{
  63.         outportb(imr,inportb(imr) | msk);
  64.     }
  65.  
  66.     return oldstate;
  67. }
  68.  
  69.  
  70. void MIrq_EOI(UBYTE irqno)
  71. /*
  72.     Clears the specified interrupt request at the interrupt controller.
  73. */
  74. {
  75.         outportb(0x20,0x20);
  76.         if(irqno>7) outportb(0xa0,0x20);
  77. }
  78.  
  79.  
  80. PVI MIrq_SetHandler(UBYTE irqno,PVI handler)
  81. {
  82. #ifdef __DJGPP__
  83.     _go32_dpmi_seginfo seginfo;
  84. #endif
  85.     PVI oldvect;
  86.     int vecno=(irqno>7) ? irqno+0x68 : irqno+0x8;
  87. #ifdef __DJGPP__
  88.     _go32_dpmi_get_protected_mode_interrupt_vector(vecno, &seginfo);
  89.     oldvect = seginfo.pm_offset;
  90.     seginfo.pm_offset = handler;
  91.     seginfo.pm_selector = _go32_my_cs();
  92.     _go32_dpmi_allocate_iret_wrapper(&seginfo);
  93.     _go32_dpmi_set_protected_mode_interrupt_vector(vecno, &seginfo);
  94. #else
  95.     oldvect=_dos_getvect(vecno);
  96.     _dos_setvect(vecno,handler);
  97. #endif
  98.     return oldvect;
  99. }
  100.