home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / os2x2 / ox2.asm next >
Encoding:
Assembly Source File  |  1989-06-21  |  14.4 KB  |  455 lines

  1.     TITLE    AUX Monochrome Device Driver for OS/2
  2.     PAGE    60, 132
  3.     COMMENT +
  4.     File:        OX2.ASM
  5.     File type:    Microsoft Macro Assembler (MASM) Version 5.1 or higher
  6.     Author:     John W. Cocula (parts by Mike Geary)
  7.     Date:        June, 1989
  8.  
  9.     Purpose:
  10.     This device directs AUX output to the monochrome display under OS/2.
  11.  
  12.     This driver is in the public domain.  If you find it useful,
  13.     how about returning the favor and putting some of your own
  14.     favorite utilities in the public domain, too?
  15.     (***with source code!!!***)
  16.     +
  17.  
  18.     .286c
  19.     .sall
  20.  
  21.     DATA SEGMENT WORD PUBLIC 'DATA'
  22.     SUBTTL Device Driver Header
  23.     PAGE +
  24.  
  25. ; ***********************************************************************
  26. ; *                DEVICE HEADER                *
  27. ; ***********************************************************************
  28.  
  29. PtrToNextHeader dd    0FFFFFFFFh    ; Indicates loadable device driver
  30. DeviceAttribute dw    1000100010000000b
  31.                     ; Bit 15 - Character device
  32.                     ; Bit 11 - Supports Open/Close
  33.                     ; Bits 9-7 - OS/2 device driver
  34. StrategyOffset    dw    CODE:Strategy    ; Offset to the Strategy routine
  35. IDCOffset    dw    -1        ; unused--must be -1
  36. DeviceName    db    'AUX     '    ; Name of the device
  37. ReservedBlock2    db    8 dup (0)    ; Reserved words
  38.  
  39. ; ***********************************************************************
  40. ; *                DEVHLP SUPPORT                *
  41. ; ***********************************************************************
  42.  
  43.         .XLIST
  44. ;
  45. ; These constants define the DevHlp commands:
  46. ;
  47. ;DevHlp_SchedClockAddr EQU 0    ; 0h  Get system clock routine
  48. ;DevHlp_DevDone        EQU   1    ; 1h  Device I/O Complete
  49. ;DevHlp_Yield        EQU   2    ; 2h  Yield the CPU
  50. ;DevHlp_TCYield        EQU   3    ; 3h  Yield the CPU to time-critical
  51. ;DevHlp_Block        EQU   4    ; 4h  Block thread on event
  52. ;DevHlp_Run        EQU   5    ; 5h  Unblock thread
  53. ;DevHlp_SemRequest   EQU   6    ; 6h  Claim a semaphore
  54. ;DevHlp_SemClear     EQU   7    ; 7h  Release a semaphore
  55. ;DevHlp_SemHandle    EQU   8    ; 8h  Get a semaphore handle
  56. ;DevHlp_PushReqPacket EQU  9    ; 9h  Add request to list
  57. ;DevHlp_PullReqPacket EQU 10    ; Ah  Remove request from list
  58. ;DevHlp_PullParticular EQU 11    ; Bh  Remove a specific request from list
  59. ;DevHlp_SortReqPacket EQU 12    ; Ch  Insert request in sorted order to list
  60. ;DevHlp_AllocReqPacket EQU 13    ; Dh  Get a request packet
  61. ;DevHlp_FreeReqPacket EQU 14    ; Eh  Free request packet
  62. ;DevHlp_QueueInit    EQU  15    ; Fh  Initialize character queue
  63. ;DevHlp_QueueFlush   EQU  16    ;10h  Clear character queue
  64. ;DevHlp_QueueWrite   EQU  17    ;11h  Put a char in the queue
  65. ;DevHlp_QueueRead    EQU  18    ;12h  Get a char from the queue
  66. ;DevHlp_Lock        EQU  19    ;13h  Lock segment
  67. ;DevHlp_Unlock        EQU  20    ;14h  Unlock segment
  68. DevHlp_PhysToVirt   EQU  21    ;15h  Map physical address to virtual
  69. DevHlp_VirtToPhys   EQU  22    ;16h  Map virtual-to-physical address
  70. ;DevHlp_PhysToUVirt  EQU  23    ;17h  Map physical-to-user virtual
  71. ;DevHlp_AllocPhys    EQU  24    ;18h  Allocate physical memory
  72. ;DevHlp_FreePhys     EQU  25    ;19h  Free physical memory
  73. ;DevHlp_SetROMVector EQU  26    ;1Ah  Set software interrupt vector
  74. ;DevHlp_SetIRQ        EQU  27    ;1Bh  Set a hardware interrupt handler
  75. ;DevHlp_UnSetIRQ     EQU  28    ;1Ch  Reset a hardware interrupt handler
  76. ;DevHlp_SetTimer     EQU  29    ;1Dh  Set timer request handler
  77. ;DevHlp_ResetTimer   EQU  30    ;1Eh  Remove a timer handler
  78. ;DevHlp_MonitorCreate EQU 31    ;1Fh  Create a monitor
  79. ;DevHlp_Register     EQU  32    ;20h  Install a monitor
  80. ;DevHlp_DeRegister   EQU  33    ;21h  Remove a monitor
  81. ;DevHlp_MonWrite     EQU  34    ;22h  Pass data records to monitor
  82. ;DevHlp_MonFlush     EQU  35    ;23h  Remove all data from stream
  83. ;DevHlp_GetDOSVar    EQU  36    ;24h  Return pointer to DOS variable
  84. ;DevHlp_SendEvent    EQU  37    ;25h  Indicate an event
  85. ;DevHlp_ROMCritSection EQU 38    ;26h  ROM BIOS critical section
  86. ;DevHlp_VerifyAccess EQU  39    ;27h  Verify access to memory
  87. DevHlp_AllocGDTSelector EQU 45    ;2Dh  Allocate GDT selectors
  88. DevHlp_PhysToGDTSelector EQU 46 ;2Eh  Map physical to virtual address
  89. ;DevHlp_RealToProt   EQU  47    ;2Fh  Real Mode to Protect Mode
  90. ;DevHlp_ProtToReal   EQU  48    ;30h  Protect Mode to Real Mode
  91. ;DevHlp_EOI        EQU  49    ;31h  Issue an End-Of-Interrupt
  92. DevHlp_UnPhysToVirt EQU  50    ;32h  Mark PhysToVirt complete
  93. ;DevHlp_TickCount    EQU  51    ;33h  Modify timer
  94. ;DevHlp_GetLIDEntry  EQU  52    ;34h  Get Logical ID
  95. ;DevHlp_FreeLIDEntry EQU  53    ;35h  Release Logical ID
  96. ;DevHlp_ABIOSCall    EQU  54    ;36h  Invoke ABIOS function
  97. ;DevHlp_ABIOSCommonEntry EQU 55    ;37h  Invoke ABIOS Common Entry Point
  98.  
  99. ;
  100. ; This macro simplifies calling a DevHlp service by doing a load of the
  101. ; function code into DL and calling pfnDevHlp.
  102. ;
  103.  
  104. @DevHlp MACRO name
  105.     mov  dl, DevHlp_&name
  106.     call pfnDevHlp
  107.     ENDM
  108.  
  109.         .LIST
  110.  
  111.     SUBTTL Structures/Codes for Request Packets
  112.     PAGE +
  113. ; ***********************************************************************
  114. ; *             REQUEST PACKET STRUCTURE            *
  115. ; ***********************************************************************
  116.  
  117. PKTMAX        EQU    18        ; Maximum size of packet
  118.  
  119. PacketStruc    STRUC
  120.  PktLen     db    ?        ; Length in bytes of packet
  121.  PktUnit    db    ?        ; Subunit number of block device
  122.  PktCmd     db    ?        ; Command code
  123.  PktStatus    dw    ?        ; Status word
  124.  PktDOSLink    dd    ?        ; Reserved
  125.  PktDevLink    dd    ?        ; Device multiple-request link
  126.  PktData    db    PKTMAX dup (?)    ; Data pertaining to specific packet
  127. PacketStruc    ENDS
  128.  
  129. ; ***********************************************************************
  130. ; *           CODES FOR STATUS OF REQUEST PACKET            *
  131. ; ***********************************************************************
  132.  
  133. ; Bit positions in Status field:
  134. StatRecord    RECORD    StatError:1,StatDevError:1,StatRes:4,StatBusy:1,\
  135.             StatDone:1,StatErrCode:8
  136.  
  137. ; Error Codes:
  138. StatWrProtVio    EQU    00h        ; WRITE PROTECT VIOLATION
  139. StatUnknownCmd    EQU    03h        ; UNKNOWN COMMAND
  140. StatGenFailure    EQU    0Ch        ; GENERAL FAILURE
  141. StatCharIOIntd    EQU    11h        ; CHARACTER I/O INTERRUPTED
  142. StatMonNotSupp    EQU    12h        ; MONITORS NOT SUPPORTED
  143.  
  144. ; ***********************************************************************
  145. ; *           POINTER CONSTANTS FOR REQUEST PACKET         *
  146. ; ***********************************************************************
  147.  
  148. Packet        EQU    ES:[BX]
  149.  
  150. ; INIT arguments:
  151.  
  152. InitpEnd    EQU    DWORD PTR PktData+1    ; Pointer to freemem after dev
  153. IOpData        EQU    DWORD PTR PktData+1    ; Transfer address
  154. IOcount        EQU    WORD  PTR PktData+5    ; Count of bytes/sectors
  155. InitArgs    EQU    DWORD PTR PktData+5    ; Pointer to CONFIG.SYS args
  156.  
  157. ; ***********************************************************************
  158. ; *            DATA USED BY THE STRATEGY ROUTINE            *
  159. ; ***********************************************************************
  160.  
  161. pfnDevHlp    dd    ?    ; Holds address of the DevHlp function entry
  162. fbError        db    0    ; Error flag used to signal to the Strategy
  163.                 ;  routine a return with error from another
  164.                 ;  subroutine.
  165.  
  166. SCRMINOFF    EQU    160*24
  167. SCRMAXOFF    EQU    160*25
  168.  
  169. ptrMonoMem    LABEL    DWORD
  170. offMonoMem    dw    SCRMINOFF
  171. selMonoMem    dw    ?
  172.  
  173. BS        EQU    08h
  174. TAB        EQU    09h
  175. LF        EQU    0Ah
  176. CR        EQU    0Dh
  177.  
  178. ; ***********************************************************************
  179. ; * A call table is set up to call a particular subroutine depending on *
  180. ; * the command code field within the request packet.            *
  181. ; * The table of offsets must be stored in the data segment.        *
  182. ; ***********************************************************************
  183.  
  184. StrategyFuncTable dw    INIT        ;  0    INIT
  185.         dw    BadCommand    ;  1    MEDIA CHECK - N/A
  186.         dw    BadCommand    ;  2    BUILD BPB - N/A
  187.         dw    BadCommand    ;  3    reserved
  188.         dw    BadCommand    ;  4    READ (input) - N/A
  189.         dw    BadCommand    ;  5    NONDES READ NO WAIT - N/A
  190.         dw    BadCommand    ;  6    INPUT STATUS - N/A
  191.         dw    BadCommand    ;  7    INPUT FLUSH - N/A
  192.         dw    WRITE        ;  8    WRITE (output)
  193.         dw    WRITE        ;  9    WRITE WITH VERIFY - N/A
  194.         dw    STATUS        ;  A    OUTPUT STATUS
  195.         dw    FLUSH        ;  B    OUTPUT FLUSH
  196.         dw    BadCommand    ;  C    Reserved
  197.         dw    OPEN        ;  D    DEVICE OPEN
  198.         dw    CLOSE        ;  E    DEVICE CLOSE
  199.         dw    BadCommand    ;  F    REMOVABLE MEDIA - N/A
  200.         dw    BadCommand    ; 10    GENERIC IOCtl - N/A
  201.         dw    BadCommand    ; 11    RESET MEDIA - N/A
  202.         dw    BadCommand    ; 12    GET LOGICAL DRIVE MAP - N/A
  203.         dw    BadCommand    ; 13    SET LOGICAL DRIVE MAP - N/A
  204.         dw    BadCommand    ; 14    DEINSTALL - N/A
  205. MaxCommandCode    EQU    (($-StrategyFuncTable) / 2) - 1
  206.  
  207.  
  208. END_OF_DATA    EQU  $
  209.  
  210. DATA ENDS
  211.  
  212. CODE      SEGMENT  WORD PUBLIC 'CODE'
  213.       ASSUME   CS:CODE, DS:DATA, ES:NOTHING
  214.  
  215. ; ***********************************************************************
  216. ; *                STRATEGY ROUTINE                *
  217. ; *                                    *
  218. ; * Far procedure Strategy gets the request packet and based on its    *
  219. ; * command code branches to the appropriate subroutine. Upon return    *
  220. ; * from the subroutine, the request is signalled serviced with or    *
  221. ; * without error.                            *
  222. ; ***********************************************************************
  223.  
  224. Strategy PROC FAR
  225.     push    es        ; save the pointers to the request packet
  226.     push    bx
  227.     mov    al, BYTE PTR Packet.PktCmd
  228.                 ; Command code is moved into al
  229.     cmp    al, MaxCommandCode
  230.                 ; If index larger than those supported
  231.     ja    UNSUPPORTED    ; Jump to indicate that the command is
  232.                 ;  unsupported
  233.     cbw            ; Byte in al is converted to word
  234.     mov    di, ax        ; It is then moved to di
  235.     shl    di, 1        ; Converted to an index
  236.     mov    fbError, 0    ; Zero out the fbError flag prior to the call
  237.     call    WORD PTR StrategyFuncTable[di]
  238.                 ; Jump to appropriate offset
  239.     pop    bx        ; Restore the pointers to the request packet
  240.     pop    es
  241.     cmp    fbError, 0    ; Upon return, check if subroutine returned
  242.                 ;  an error
  243.     jz    ERROR_FREE    ; If no error, jump to signal devdone
  244.     or    Packet.PktStatus, MASK StatError + StatGenFailure
  245.                 ; Set the error condition in the Req Packet
  246.                 ; Set the error to General Failure
  247.  
  248. ERROR_FREE:
  249.     or    Packet.PktStatus, MASK StatDone
  250.     jmp    FIN
  251.  
  252. UNSUPPORTED:
  253.     pop    bx        ; Retrieve the pushed values of bx and es
  254.     pop    es
  255.     call    BadCommand
  256.  
  257. FIN:
  258.     ret
  259. Strategy ENDP
  260.  
  261. ; ***********************************************************************
  262. ; *                  WRITE ROUTINE                *
  263. ; ***********************************************************************
  264.  
  265. WRITE PROC NEAR
  266.     cld
  267.     mov    cx, WORD PTR Packet.IOcount    ; CX = # bytes to write
  268.  
  269.     mov    ax, WORD PTR Packet.IOpData+2    ; hi word of physical address
  270.     mov    dx, ax
  271.     mov    bx, WORD PTR Packet.IOpData    ; low word of physical address
  272.     mov    bp, bx                ; NOTE: "Packet." is trashed!
  273.     les    di, ptrMonoMem            ; point ES:DI to mono memory
  274.     mov    dh, 0                ; 0 == DS:SI is virt address
  275.     push    ds
  276.     @DevHlp    PhysToVirt    
  277.     jnc    outLoop
  278.     mov    fbError, 1        ; If failed, signal error writing
  279.     jmp    write_term        ; Jump to write_term
  280. outLoop:
  281.     lodsb                ; Load a byte to write
  282.     cmp    al, CR
  283.     ja    outChar
  284.     je    outCR
  285.     cmp    al, LF
  286.     je    outLF
  287.     cmp    al, TAB
  288.     je    outTAB
  289.     cmp    al, BS
  290.     je    outBS
  291. outChar:
  292.     stosb                ; write the char
  293.     inc    di            ; advance over the attribute
  294.     cmp    di, SCRMAXOFF        ; are we at end of line?
  295.     jb    outDone            ; no, done with this char
  296.     mov    di, SCRMINOFF        ; yes, carriage return and ...
  297. outLF:
  298.     push    es            ; save pointers and count
  299.     push    di
  300.     push    ds
  301.     push    si
  302.     push    cx
  303.  
  304.     mov    ax, es
  305.     mov    ds, ax
  306.  
  307.     xor    di, di            ; Scroll screen
  308.     mov    si, 160
  309.     mov    cx, 24*80
  310.     rep    movsw
  311.  
  312.     mov    ax, 0720h        ; Blank bottom line
  313.     mov    cx, 80
  314.     rep    stosw
  315.  
  316. ; Remap DS to the user's physical buffer.
  317. ;    mov    ax, dx            ; saved hi  word of phys. addr
  318. ;    mov    bx, bp            ; saved low word of phys. addr
  319. ;    mov    dh, 0            ; use DS:SI
  320. ;    @DevHlp    PhysToVirt
  321.  
  322.     pop    cx
  323.     pop    si
  324.     pop    ds
  325.     pop    di
  326.     pop    es
  327.     jmp    outDone
  328. outBS:
  329.     cmp    di, SCRMINOFF
  330.     je    outDone
  331.     sub    di, 2
  332.     jmp    outDone
  333. outTAB:
  334.     or    di, 0Eh
  335.     add    di, 2
  336.     jmp    outDone
  337. outCR:
  338.     mov    di, SCRMINOFF
  339. outDone:
  340.     loop    outLoop
  341. write_term:
  342.     pop    ds            ; restore driver's DS register
  343.     @DevHlp    UnPhysToVirt        ; mark completion of PhysToVirt
  344.     mov    WORD PTR ptrMonoMem, di    ; save position on screen
  345. ; ------------------------------------------------------------------------
  346. ; Set new cursor position.
  347. ; ------------------------------------------------------------------------
  348.     mov    dx, 03B4h
  349.     shr    di, 1
  350.     mov    ax, di
  351.     mov    al, 0Eh
  352.     out    dx, ax
  353.     mov    ax, di
  354.     xchg    ah, al
  355.     mov    al, 0Fh
  356.     out    dx, ax
  357.     ret
  358. WRITE ENDP
  359.  
  360. OPEN PROC NEAR
  361.     mov    dx, 03B4h        ; Set standard monochrome cursor.
  362.     mov    ax, 0B0Ah
  363.     out    dx, ax
  364.     mov    ax, 0C0Bh
  365.     out    dx, ax
  366.     ret
  367. OPEN ENDP
  368.  
  369. CLOSE PROC NEAR
  370.     ret
  371. CLOSE ENDP
  372.  
  373. STATUS PROC NEAR
  374.     ret
  375. STATUS ENDP
  376.  
  377. FLUSH PROC NEAR
  378.     ret
  379. FLUSH ENDP
  380.  
  381. NO_OP PROC NEAR
  382.     ret
  383. NO_OP ENDP
  384.  
  385. BadCommand PROC NEAR
  386.     or Packet.PktStatus, MASK StatError + StatUnknownCmd
  387.     ret
  388. BadCommand ENDP
  389.  
  390.     SUBTTL Initialization Code
  391.     page +
  392.  
  393. END_OF_CODE  EQU $
  394.  
  395. ; ***********************************************************************
  396. ; *                 INIT ROUTINE                *
  397. ; *                                    *
  398. ; * INIT procedure is placed at the end of the code so it can go away    *
  399. ; * once initialization has been done.                    *
  400. ; ***********************************************************************
  401.  
  402. INIT PROC NEAR
  403. ; ========================================================================
  404. ; Save the pointer to the DevHlp routines passed in the request packet.
  405. ; ========================================================================
  406.     mov    ax, WORD PTR Packet.InitpEnd ; Save pointer to DevHlp
  407.     mov    WORD PTR pfnDevHlp, ax
  408.     mov    ax, WORD PTR Packet.InitpEnd+2
  409.     mov    WORD PTR pfnDevHlp+2, ax
  410. ; ========================================================================
  411. ; Obtain a GDT selector and map it to the monochrome monitor's memory
  412. ; at B 0000.  The WRITE routine uses this memory area.
  413. ; ========================================================================
  414.     push    es            ; save ptr to request packet
  415.     push    bx
  416.  
  417.     mov    ax, ds            ; point ES:DI to GDT sel array
  418.     mov    es, ax
  419.     mov    di, OFFSET ptrMonoMem+2
  420.     mov    cx, 1            ; number of selectors
  421.     @DevHlp    AllocGDTSelector    ; allocate the selector
  422.     jc    init_error
  423.     mov    ax, 000Bh
  424.     mov    bx, 0000h
  425.     mov    cx, 4000
  426.     mov    si, WORD PTR ptrMonoMem+2
  427.     @DevHlp    PhysToGDTSelector    ; map the selector to memory
  428.     jc    init_error
  429. ; ------------------------------------------------------------------------
  430. ; ptrMonoMem now refers to the monochrome adapter's memory.
  431. ; ------------------------------------------------------------------------
  432. ; ------------------------------------------------------------------------
  433. ; Set the ending offsets to unload initialization code and data.
  434. ; ------------------------------------------------------------------------
  435. @@:    pop    bx
  436.     pop    es
  437.     lea    ax, END_OF_CODE        ; Place end of code segment into
  438.     mov    WORD PTR Packet.InitpEnd, ax ; request packet
  439.     lea    ax, END_OF_DATA        ; Place end of data segment into
  440.     mov    WORD PTR Packet.InitpEnd+2, ax ; request packet
  441.     jmp    init_done
  442. init_error:
  443.     pop    bx            ; restore ptr to request packet
  444.     pop    es
  445.     mov    fbError, 1
  446.     mov    WORD PTR Packet.InitpEnd, 0
  447.     mov    WORD PTR Packet.InitpEnd+2, 0
  448. init_done:
  449.     ret
  450. INIT ENDP
  451.  
  452. CODE    ENDS
  453.  
  454.     END
  455.