home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 4.ddi / C / ISSETISR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  5.1 KB  |  168 lines

  1. /**
  2. *
  3. * Name        issetisr -- Install C-TOOLS-2-style interrupt service
  4. *                routine.
  5. *        (Formerly called PCSETISR.)
  6. *
  7. * Synopsis    ercode = issetisr(intype,pfunc,stksize,pvector,ppisrblk);
  8. *
  9. *        int ercode      Error return code
  10. *        unsigned (*pfunc)()
  11. *                  Pointer to interrupt service routine
  12. *        int intype      Interrupt type number (0 to 255)
  13. *        int stksize      Stack size (in bytes) needed for ISR
  14. *                  (at least 128)
  15. *        ADS *pvector      Previous interrupt vector for the
  16. *                  specified interrupt type.
  17. *        ISRCTRL **ppisrblk
  18. *                  Address of pointer to ISR control block
  19. *
  20. * Description    ISINSTAL installs a C-TOOLS-2-style interrupt service
  21. *        routine (ISR) in the interrupt vector of the specified
  22. *        type.  This includes (1) allocating an ISR control block
  23. *        and setting its values; (2) arranging for ISCT2INT to
  24. *        pass control to the ISR; (3) storing the address of the
  25. *        ISR control block in the interrupt vector.
  26. *
  27. *        The ISR is in effect (and may be invoked) as soon as
  28. *        operation (3) is complete, i.e., even before ISSETISR
  29. *        returns to its caller.
  30. *
  31. *        The calling function must first use ISISRSTK (formerly
  32. *        called PCISRSTK) to allocate space to be used for the
  33. *        ISR's stack.
  34. *
  35. *        The previous value of the interrupt vector is returned
  36. *        in the ADS structure pointed to by pvector.  This may be
  37. *        used to restore the previous handling of this type of
  38. *        interrupt.
  39. *
  40. *        The address of the ISR control block is returned in
  41. *        *ppisrblk.  The space occupied by the control block may
  42. *        be freed after the ISR is no longer in use (see example
  43. *        below).
  44. *
  45. *        ISISRSTK and ISSETISR provide compatibility with ISRs
  46. *        built with C TOOLS 2.  Newer programs should use
  47. *        ISINSTAL instead.
  48. *
  49. * Limitations    Since C TOOLS 2 ISRs were not re-entrant, only one level
  50. *        of call is allowed to ISRs installed by ISSETISR.  If
  51. *        the ISR is called while it is already executing, the
  52. *        second call just returns without taking any action.
  53. *
  54. *        ISISRSTK allocates a pool of space to all C TOOLS 2
  55. *        ISRs.  Under C TOOLS 2 this space was used dynamically
  56. *        by all ISRs.  Now under C TOOLS PLUS each ISR installed
  57. *        by ISSETISR is allocated a fixed section of that pool.
  58. *        Therefore this pool must contain enough space for all
  59. *        ISRs installed by ISSETISR or ercode will be returned
  60. *        with a value of 3.
  61. *
  62. * Example    The following code fragment illustrates one way to
  63. *        declare the arguments to ISSETISR, to use ISSETVEC to
  64. *        restore the contents of the interrupt vector when the
  65. *        ISR is no longer in use, and to free the space allocated
  66. *        to the control block.
  67. *
  68. *            #include <bisr.h>
  69. *            #if LAT300
  70. *            #include <stdlib.h>
  71. *            #endif
  72. *            #if MSC300
  73. *            #include <malloc.h>
  74. *            #include <process.h>
  75. *            #endif
  76. *
  77. *            #define MY_INTYPE        0x60
  78. *            #define MY_STK_SIZE     2000
  79. *
  80. *            unsigned my_isr(void);
  81. *            ISRCTRL  *pisrblk;
  82. *            ADS      prev_vector;
  83. *
  84. *            if (isisrstk(MY_STK_SIZE))
  85. *            exit(4);
  86. *
  87. *            if (issetisr(MY_INTYPE,my_isr,MY_STK_SIZE,
  88. *                 &prev_vector,&pisrblk))
  89. *            exit(5);
  90. *
  91. *             .......    (Use the ISR.)
  92. *
  93. *            issetvec(intype,&prev_vector);
  94. *            free((char *) pisrblk);
  95. *
  96. * Returns    ercode          The return code is 0 if the vector is
  97. *                  is successfully set; other values are:
  98. *                  1 - Interrupt type out of range
  99. *                  2 - Cannot allocate space for ISR control
  100. *                      block
  101. *                  3 - Insufficient stack space allocated
  102. *                      by ISISRSTK
  103. *        *pvector      Previous contents of interrupt vector
  104. *        *ppisrblk      Address of ISR control block
  105. *        b_istksize      The size (in bytes) of the remaining
  106. *                  stack space for C TOOLS 2 ISRs.
  107. *        b_isrpstk      Pointer to next available stack space.
  108. *
  109. *  Version    3.0  (C)Copyright Blaise Computing Inc.  1986
  110. *
  111. **/
  112.  
  113. #include <bisr.h>
  114.  
  115. #if LAT300
  116. #include <stdlib.h>
  117. #endif
  118. #if MSC300
  119. #include <malloc.h>
  120. #endif
  121.  
  122. #define OK        0          /* Error return codes          */
  123. #define BAD_INTYPE    1
  124. #define NO_HEAP_LEFT    2
  125. #define NO_STACKS_LEFT    3
  126.  
  127. int issetisr(intype,pfunc,stksize,pvector,ppisrblk)
  128. int     intype;
  129. unsigned (*pfunc)();
  130. int     stksize;
  131. ADS     *pvector;
  132. ISRCTRL  **ppisrblk;
  133. {
  134.     extern void isct2int();          /* Interface routine for          */
  135.                       /* C-TOOLS-2-style ISRs.          */
  136.     int result;
  137.  
  138.     if (isretvec(intype,pvector))     /* Fetch previous value of      */
  139.     return BAD_INTYPE;          /* interrupt vector.          */
  140.  
  141.     if (b_istksize < stksize)          /* Make sure there's enough     */
  142.     return NO_STACKS_LEFT;          /* space for this ISR's stack.  */
  143.  
  144.                       /* Create the control block.    */
  145.     if (NIL == (*ppisrblk = utalloc(ISRCTRL)))
  146.     return NO_HEAP_LEFT;
  147.  
  148.                       /* Address of the ISR itself.   */
  149.     utcodptr(pfunc,&(*ppisrblk)->ct2_isr);
  150.  
  151.                       /* Install the ISR.          */
  152.     result = isinstal(intype,
  153.               isct2int,
  154.               CT2ISR_IDENT,
  155.               *ppisrblk,
  156.               b_isrpstk,
  157.               stksize,
  158.               1);
  159.  
  160.     if (result == 0)              /* If successfully installed,   */
  161.     {                      /* record change in available   */
  162.     b_istksize -= stksize;          /* stack area.              */
  163.     b_isrpstk  += stksize;
  164.     }
  165.  
  166.     return result;
  167. }
  168.