home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 420.lha / Example_device / Ex_Support.asm < prev    next >
Assembly Source File  |  1990-09-29  |  12KB  |  220 lines

  1. ;*********************************************************************
  2. ;*                                                                   *
  3. ;* All changes I made are hereby in the Public Domain.  Commodore    *
  4. ;* retains its copyright, if any, of the original code in the Rom    *
  5. ;* Kernel manuals since this is a derivative work.  -Jeff Rush       *
  6. ;*                                                                   *
  7. ;*********************************************************************
  8. ;*                           History                                 *
  9. ;*                                                                   *
  10. ;* 20Sep89 jrr Original crib from Amiga Rom Kernel Manual.           *
  11. ;* 23Jul90 jrr Extensive descriptive prose added.                    *
  12. ;*                                                                   *
  13. ;*********************************************************************
  14.         TITLE   Example Device Driver for Tutorial Use
  15.         SMALLOBJ    ; Use PC-Relative Addressing
  16.         OPTIMON     ; Enable C.A.P.E. 68K Optimizations
  17.  
  18.         SECTION TheOnlySection
  19.  
  20.         ;************
  21.         ;* Includes *
  22.         ;************
  23.         NOLIST
  24.         INCLUDE "exec/types.i"
  25.         INCLUDE "exec/devices.i"
  26.         INCLUDE "exec/initializers.i"
  27.         INCLUDE "exec/memory.i"
  28.         INCLUDE "exec/resident.i"
  29.         INCLUDE "exec/io.i"
  30.         INCLUDE "exec/ables.i"
  31.         INCLUDE "exec/errors.i"
  32.         INCLUDE "exec/tasks.i"
  33.         INCLUDE "exec/semaphores.i"
  34.         INCLUDE "hardware/intbits.i"
  35.  
  36.         INCLUDE "asmsupp.i"
  37.  
  38.         INCLUDE "Example.i"     ; Device-Specific Definitions
  39.         LIST
  40.  
  41.         ;*************
  42.         ;* Constants *
  43.         ;*************
  44.  
  45.         ;******************
  46.         ;* Public Symbols *
  47.         ;******************
  48.         XDEF    EndCode         ; Label at End of Device Code
  49.         XDEF    LockGlobal      ; Obtain Exclusive Access to Device Global Data
  50.         XDEF    UnlockGlobal    ; Release Exclusive Access to Device Global Data
  51.         XDEF    PerformIO       ; Common Fork or Dispatch to Handle Device Commands
  52.         XDEF    TermIO          ; Common Join from Device Commands to Do Common Cleanup
  53.  
  54.         ;********************
  55.         ;* External Symbols *
  56.         ;********************
  57.         ; This Name is for Debugging Use, to Annotate Serial-Debug Output
  58.         IFNE    INFO_LEVEL      ; If Any Debugging Enabled at All
  59.         XREF    subSysName
  60.         ENDC
  61.  
  62.         ; Defined in Ex_Main.Asm
  63.         XREF    CmdTable    ; Device Global Command Dispatch Table
  64.  
  65. ;*********************************************************************
  66. ;*        LockGlobal Helper Routine (for Open, Close, etc.)          *
  67. ;*                                                                   *
  68. ;* This functions uses a semaphore defined in the device global data *
  69. ;* to obtain exclusive access to the device global data area.  It is *
  70. ;* used by DevOpen and DevClose to setup and takedown items that may *
  71. ;* break a Forbid().  Its use is optional and is not required if you *
  72. ;* have very simple opens/closes, *and* multiple copies of your unit *
  73. ;* tasks are not attempting to alter the device global area          *
  74. ;* concurrently.  They may all read it however, as long as no one is *
  75. ;* actually trying to -change- it while others are reading from it.  *
  76. ;*                                                                   *
  77. ;* Inputs:                                                           *
  78. ;*   A6 - Ptr to our Device node.                                    *
  79. ;*                                                                   *
  80. ;* Outputs:                                                          *
  81. ;*   None                                                            *
  82. ;*                                                                   *
  83. ;*      * All Registers are Preserved, Including D0-D1/A0-A1 *       *
  84. ;*                                                                   *
  85. ;* Notes:                                                            *
  86. ;*   Calling task blocks until exclusive access can be granted.      *
  87. ;*                                                                   *
  88. ;*********************************************************************
  89. LockGlobal:
  90.         ;
  91.         ; Lock Device Global Data for Exclusive Access
  92.         MOVEM.L D0-D1/A0-A1,-(SP)   ; Preserve Registers During Lock
  93.         LEA.L   md_SemLock(A6),A0   ; Pickup Ptr to the Arbitration Semaphore
  94.         LINKSYS ObtainSemaphore,md_SysLib(A6)   ; Get Control of Device Node
  95.         MOVEM.L (SP)+,D0-D1/A0-A1   ; Restore Registers
  96.         RTS
  97.  
  98. ;*********************************************************************
  99. ;*       UnlockGlobal Helper Routine (for Open, Close, etc.)         *
  100. ;*                                                                   *
  101. ;* This functions uses a semaphore defined in the device global data *
  102. ;* to release exclusive access to the device global data area.  It   *
  103. ;* is used by DevOpen and DevClose to setup and takedown items that  *
  104. ;* may break a Forbid().  Its use is optional and is not required if *
  105. ;* you have very simple opens/closes, *and* multiple copies of your  *
  106. ;* unit tasks are not attempting to alter the device global area     *
  107. ;* concurrently.  They may all read it however, as long as no one is *
  108. ;* actually trying to -change- it while others are reading from it.  *
  109. ;*                                                                   *
  110. ;* Inputs:                                                           *
  111. ;*   A6 - Ptr to our Device node.                                    *
  112. ;*                                                                   *
  113. ;* Outputs:                                                          *
  114. ;*   None                                                            *
  115. ;*                                                                   *
  116. ;*      * All Registers are Preserved, Including D0-D1/A0-A1 *       *
  117. ;*                                                                   *
  118. ;* Notes:                                                            *
  119. ;*   Calling task blocks until exclusive access can be granted.      *
  120. ;*                                                                   *
  121. ;*********************************************************************
  122. UnlockGlobal:
  123.         ;
  124.         ; Unlock Device Global Data from Exclusive Access
  125.         MOVEM.L D0-D1/A0-A1,-(SP)   ; Preserve Registers During Unlock
  126.         LEA.L   md_SemLock(A6),A0   ; Pickup Ptr to the Arbitration Semaphore
  127.         LINKSYS ReleaseSemaphore,md_SysLib(A6)   ; Release Control of Device Node
  128.         MOVEM.L (SP)+,D0-D1/A0-A1   ; Restore Registers
  129.         RTS
  130.  
  131. ;*********************************************************************
  132. ;*              PerformIO Helper Routine (for BeginIO)               *
  133. ;*                                                                   *
  134. ;* PerformIO actually dispatches an I/O request.  It might be called *
  135. ;* from within the unit task, or directly from BeginIO (and thus on  *
  136. ;* the callers's schedule).                                          *
  137. ;*                                                                   *
  138. ;* Inputs:                                                           *
  139. ;*   A6 - Ptr to our Device node.                                    *
  140. ;*   A1 - Ptr to the caller's I/O request block as initialized by    *
  141. ;*        OpenDevice().                                              *
  142. ;*   A3 - Ptr to the Unit Descriptor.                                *
  143. ;*                                                                   *
  144. ;*   (Bounds checking was done on the inputs by the caller)          *
  145. ;*                                                                   *
  146. ;* Outputs:                                                          *
  147. ;*   IO_ERROR  - Error code of operation (zero means success).       *
  148. ;*                                                                   *
  149. ;*********************************************************************
  150. PerformIO:
  151.         IFGE    INFO_LEVEL-150
  152.         CLR.L   -(SP)               ; Allocate and Clear a 32-bit Scratch Word
  153.         MOVE.W  IO_COMMAND(A1),2(SP)  ; Pick up Command Code
  154.         PUTMSG  150,<'%s/PerformIO -- %ld'> ; Output a Debugging Message
  155.         ADDQ.L  #4,SP               ; Deallocate the 32-bit Scratch Word
  156.         ENDC
  157.  
  158.         MOVEQ   #0,D0
  159.         MOVE.B  D0,IO_ERROR(A1)     ; Initially Clear the Error Field¨to 'Ok'
  160.         MOVE.B  IO_COMMAND+1(A1),D0 ; Pick up Command Byte
  161.         LSL.W   #2,D0               ; Multiply by 4 to Get Table Offset
  162.         LEA.L   CmdTable(PC),A0     ; Pick up Base of Command Table in A0
  163.         MOVE.L  0(A0,D0.W),A0       ; Index into Table and Get Ptr to Handler Code
  164.         ;
  165.         ; Branch into Appropriate Handler Code for Desired Command
  166.         ;   A1 - Ptr to I/O Request Block
  167.         ;   A3 - Ptr to Unit Descriptor
  168.         ;   A6 - Ptr to Device Node
  169.         JMP     (A0)
  170.  
  171. ;*********************************************************************
  172. ;*               TermIO Helper Routine (for BeginIO)                 *
  173. ;*                                                                   *
  174. ;* TermIO sends the I/O request back to the user.  It knows not to   *
  175. ;* mark the device as inactive if this was an immediate request or   *
  176. ;* if the request was started from the unit task.                    *
  177. ;*                                                                   *
  178. ;* Inputs:                                                           *
  179. ;*   A6 - Ptr to our Device node.                                    *
  180. ;*   A1 - Ptr to the caller's I/O request block as initialized by    *
  181. ;*        OpenDevice().                                              *
  182. ;*   A3 - Ptr to the Unit Descriptor.                                *
  183. ;*                                                                   *
  184. ;*   (Bounds checking was done on the inputs by the caller)          *
  185. ;*                                                                   *
  186. ;* Outputs:                                                          *
  187. ;*   IO_ERROR  - Error code of operation (zero means success).       *
  188. ;*                                                                   *
  189. ;*********************************************************************
  190. TermIO:
  191.         PUTMSG  160,<'%s/TermIO'>
  192.         MOVE.W  IO_COMMAND(A1),D0   ; Pick up Command Code in D0
  193.  
  194.         MOVE.L  #IMMEDIATES,D1      ; Pick up Immediate Command Classification Mask
  195.         BTST    D0,D1               ; Test Command for an Immediate Type
  196.         BNE.S   TermIO_Immediate    ; I/O was Immediate, Skip Task Handling Code
  197.  
  198.         BTST    #UNITB_INTASK,UNIT_FLAGS(A3)    ; Did I/O Come from Task?
  199.         BNE.S   TermIO_Immediate    ; Yes, Don't Clear the Task-Active Flag
  200.  
  201.         BCLR    #UNITB_ACTIVE,UNIT_FLAGS(A3)    ; Clear the Task-Active Flag
  202.  
  203. TermIO_Immediate:
  204.         BTST    #IOB_QUICK,IO_FLAGS(A1) ; Is the I/O Quick Bit Still Set?
  205.         BNE.S   TermIO_End  ; Yes, Request Was Not Queued, Skip Reply Process
  206.         ;
  207.         ; Reply the Message Back to the Original Issuing Task
  208.         LINKSYS ReplyMsg,md_SysLib(A6)      ; Ptr to Message:A1
  209.         ; Note: The ReplyMsg() Function Sets the LN_TYPE to NT_REPLYMSG
  210. TermIO_End:
  211.         RTS
  212.  
  213. ;*********************************************************************
  214. ;* 'EndCode' is a marker that shows the end of your code.  Make sure *
  215. ;* it does not span hunks and is not before the Romtag.              *
  216. ;*********************************************************************
  217. EndCode:
  218.         END
  219.  
  220.