home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l231 / 3.ddi / SAMPLES / DEMOS / COMMON.AS$ / COMMON
Encoding:
Text File  |  1992-11-12  |  15.2 KB  |  429 lines

  1.         .MODEL  small, pascal
  2.         INCLUDE demo.inc
  3.  
  4.         .DATA
  5. vconfig VIDCONFIG <>           ; Global video configuration structure
  6.  
  7.         .CODE
  8.  
  9. ;* GetVidConfig - Determines current video configuration and initializes
  10. ;* the vconfig structure.
  11. ;*
  12. ;* Shows:   BIOS Interrupt - 10h, Function 0 (Set Video Mode)
  13. ;*                           10h, Function 0Fh (Get Current Video Mode)
  14. ;*                           10h, Function 1Ah (Video Display Combination)
  15. ;*
  16. ;* Uses:    vconfig - Video configuration structure, declared in the
  17. ;*          DEMO.INC include file.
  18. ;*
  19. ;* Params:  None
  20. ;*
  21. ;* Return:  None
  22.  
  23. GetVidConfig PROC
  24.  
  25.         mov     ax, 1A00h               ; Get video info for VGA
  26.         int     10h
  27. chkVGA:
  28.         cmp     al, 1Ah                 ; Is VGA or MCGA present?
  29.         jne     chkEGA                  ; No?  Then check for EGA
  30.  
  31.         cmp     bl, 2                   ; If VGA exists as secondary adapter,
  32.         je      isCGA                   ;   check for CGA and mono as primary
  33.         jb      isMONO
  34.         cmp     bl, 5                   ; If EGA is primary, do normal
  35.         jbe     chkEGA                  ;   EGA checking
  36. chkMCGA:
  37.         mov     vconfig.adapter, MCGA   ; Yes?  Assume MCGA
  38.         mov     vconfig.display, COLOR
  39.         cmp     bl, 8                   ; Correct assumption?
  40.         ja      gotmode                 ; Yes?  Continue
  41. isVGA:
  42.         mov     vconfig.adapter, VGA    ; Assume it's VGA color
  43.         je      gotmode                 ; Yes?  Continue
  44.         mov     vconfig.display, MONO   ; No?  Must be VGA mono
  45.         jmp     gotmode                 ; Finished with VGA, so jump
  46. chkEGA:
  47.         mov     ah, 12h                 ; Call EGA status function
  48.         mov     bl, 10h
  49.         sub     cx, cx                  ; Clear status bits
  50.         int     10h
  51.         jcxz    chkCGA                  ; If CX is unchanged, not EGA
  52. isEGA:
  53.         mov     vconfig.adapter, EGA    ; Set structure fields for EGA
  54.         mov     vconfig.display, MONO   ; Assume EGA mono
  55.         or      bh, bh                  ; Correct assumption?
  56.         jnz     gotmode                 ; Yes?  Continue
  57.         mov     vconfig.display, COLOR  ; No?  Must be EGA color
  58.         jmp     gotmode                 ; Finished with EGA, so jump
  59. chkCGA:
  60.         int     11h                     ; Get equipment list
  61.         and     al, 30h                 ; If bits 4-5 set, monochrome
  62.         cmp     al, 30h                 ; Monochrome text mode?
  63.         je      isMONO                  ; Yes?  Continue
  64. isCGA:
  65.         mov     vconfig.adapter, CGA    ; No?  Must be CGA
  66.         mov     vconfig.display, COLOR
  67.         jmp     gotmode
  68. isMONO:
  69.         mov     vconfig.adapter, MDA    ; Set MONO
  70.         mov     vconfig.display, MONO
  71. gotmode:
  72.         mov     ah, 0Fh
  73.         int     10h                     ; Get current mode
  74.         mov     vconfig.mode, al        ; Record mode
  75.         mov     vconfig.dpage, bh       ;   and current page
  76.         mov     al, vconfig.display     ; Multiply display value
  77.         cbw                             ;   (which is either 0 or 1)
  78.         mov     bx, 800h                ;   by 800h, then add to 0B000h
  79.         mul     bx                      ;   for segment address of
  80.         add     ax, 0B000h              ;   video buffer
  81.         add     ah, vconfig.dpage       ; Adding display page gives
  82.         mov     vconfig.sgmnt, ax       ;   address of current page
  83.  
  84.         sub     ax, ax
  85.         mov     es, ax
  86.         mov     al, es:[44Ah]           ; Get number of display cols
  87.         mov     vconfig.cols, al        ; Store in structure
  88.         mov     vconfig.rows, 24        ; Assume bottom row # = 24
  89.         cmp     vconfig.adapter, EGA    ; EGA or VGA?
  90.         jl      exit                    ; No?  Exit
  91.         mov     ax, 1130h               ; Yes?  Request character info
  92.         sub     bh, bh                  ; Set BH to valid value
  93.         push    bp                      ; BP will change, so save it
  94.         int     10h                     ; Get number of rows/screen
  95.         mov     vconfig.rows, dl        ; Keep in structure
  96.         pop     bp                      ; Restore BP
  97. exit:
  98.         ret
  99.  
  100. GetVidConfig ENDP
  101.  
  102.  
  103. ;* GetCurPos - Gets current cursor position.
  104. ;*
  105. ;* Uses:    vconfig - Video configuration structure (initialized
  106. ;*          by calling the GetVidConfig procedure)
  107. ;*
  108. ;* Params:  None
  109. ;*
  110. ;* Return:  Short integer with high byte = row, low byte = column
  111.  
  112. GetCurPos PROC USES bx dx
  113.  
  114.         mov     ah, 3                   ; Function 3
  115.         mov     bh, vconfig.dpage
  116.         int     10h                     ; Get cursor position
  117.         mov     ax, dx
  118.         ret
  119.  
  120. GetCurPos ENDP
  121.  
  122.  
  123. ;* SetCurPos - Sets cursor position.
  124. ;*
  125. ;* Shows:   BIOS Interrupt - 10h, Function 2 (Set Cursor Position)
  126. ;*
  127. ;* Uses:    vconfig - Video configuration structure (initialized
  128. ;*          by calling the GetVidConfig procedure)
  129. ;*
  130. ;* Params:  Row - Target row
  131. ;*          Col - Target column
  132. ;*
  133. ;* Return:  None
  134.  
  135. SetCurPos PROC USES bx dx,
  136.         Row:WORD,
  137.         Col:WORD
  138.  
  139.         mov     dh, BYTE PTR Row        ; DH = row
  140.         mov     dl, BYTE ptr Col        ; DL = column
  141.         mov     ah, 2                   ; Function 2
  142.         mov     bh, vconfig.dpage       ; Current page
  143.         int     10h                     ; Set cursor position
  144.         ret
  145.  
  146. SetCurPos ENDP
  147.  
  148.  
  149. ;* StrWrite - Writes ASCIIZ string to video memory at specified row/column.
  150. ;*
  151. ;* Shows:   Instructions - lodsb     stosb
  152. ;*
  153. ;* Uses:    vconfig - Video configuration structure (initialized
  154. ;*          by calling the GetVidConfig procedure)
  155. ;*
  156. ;* Params:  Row - Row coordinate
  157. ;*          Col - Column coordinate
  158. ;*          Sptr - Pointer to string
  159. ;*
  160. ;* Return:  None
  161.  
  162. StrWrite PROC USES ds si di,
  163.         Row:WORD,
  164.         Col:WORD,
  165.         Sptr:PTR BYTE
  166.  
  167.         GetVidOffset Row, Col           ; Get video offset for these coords
  168.         mov     di, ax                  ; Copy to DI
  169.         LoadPtr ds, si, Sptr            ; DS:SI points to string
  170.         mov     es, vconfig.sgmnt       ; ES:DI points to video RAM
  171.         .WHILE  1                       ; Loop forever (or until break)
  172.         lodsb                           ; Get 1 character from string
  173.         .BREAK .IF al == 0              ; Quit if null terminator
  174.  
  175. ; For CGA systems, StrWrite waits for the video to begin a horizontal
  176. ; retrace before writing a character to memory. This avoids the problem
  177. ; of video snow inherent with some (though not all) color/graphics adapters.
  178. ; It also demonstrates a somewhat different approach to the problem than the
  179. ; one taken in the WinOpen and WinClose procedures.
  180.  
  181.         .IF vconfig.adapter != CGA      ; If not CGA, skip this step
  182.         push    ax                      ; Save character
  183.         mov     dx, 3DAh                ; Address of status register
  184.         cli                             ; Disallow interruptions
  185.         .REPEAT
  186.         in      al, dx                  ; Read current video status
  187.         .UNTIL  !(al & 1)               ; Until horizontal retrace done
  188.  
  189.         .REPEAT
  190.         in      al, dx                  ; No?  Read status again
  191.         .UNTIL  al & 1                  ; Until retrace starts
  192.         pop     ax                      ; Recover character
  193.         .ENDIF  ; CGA only
  194.  
  195.         stosb                           ; Write char to video buffer
  196.         sti                             ; Reenable interrupts in case CGA
  197.         inc     di                      ; Skip attribute byte
  198.         .ENDW
  199.         ret
  200.  
  201. StrWrite ENDP
  202.  
  203.  
  204. ;* StrInput - Gets input string from keyboard using BIOS. Signals idle
  205. ;* state by calling interrupt 28h while polling for keypress, making
  206. ;* the procedure useful in TSR programs. Terminates when ENTER or ESC
  207. ;* keys pressed.
  208. ;*
  209. ;* Shows:   DOS interrupt - Interrupt 28h (DOS Idle Interrupt)
  210. ;*
  211. ;* Params:  Row - Row coordinate
  212. ;*          Col - Column coordinate
  213. ;*          Max - Maximum allowable string length
  214. ;*          Sptr - Pointer to string
  215. ;*
  216. ;* Return:  Short integer with terminating char
  217.  
  218. StrInput PROC USES ds si,
  219.         Row:WORD,
  220.         Col:WORD,
  221.         Max:WORD,
  222.         Sptr:PBYTE
  223.  
  224.         LoadPtr ds, si, Sptr            ; DS:SI points to string
  225.         add     Max, si
  226.         dec     Max                     ; MAX now points to string limit
  227.  
  228.         .WHILE  1                       ; Get key until break or continue
  229. loop1:
  230.         INVOKE  StrWrite,               ; Display input string
  231.                 Row,
  232.                 Col,
  233.                 si
  234.         mov     bx, si
  235.         mov     dx, Col                 ; DL = cursor column
  236.  
  237.         .WHILE  (BYTE PTR [bx] != 0)    ; Scan string for null terminator
  238.         inc     bx                      ; Else try next character
  239.         inc     dx                      ;   and increment cursor column
  240.         .ENDW
  241.  
  242. ; Set cursor position, pass row and column
  243.         INVOKE  SetCurPos,
  244.                 Row,
  245.                 dx
  246.  
  247.         call    GetKey                  ; Poll for keypress
  248.  
  249.         .BREAK  .IF al == ESCAPE || \   ; Exit if ESC or
  250.                     al == CR            ;   ENTER key
  251.  
  252.         .IF     al == BACKSP || \       ; If BACKSPACE or LEFT,
  253.                 ah == LEFT              ;   handle it
  254.         cmp     bx, si                  ; At first letter?
  255.         jbe     loop1                   ; Yes?  Ignore BACKSPACE
  256.         dec     bx                      ; No?  Point to preceding char
  257.         dec     dx                      ; Decrement column
  258.         mov     BYTE PTR [bx], ' '      ; Blank char
  259.         push    bx                      ; Preserve pointer
  260.         INVOKE  StrWrite,               ; Overwrite last char with blank
  261.                 Row,
  262.                 dx,
  263.                 bx
  264.  
  265.         pop     bx
  266.         mov     BYTE PTR [bx], 0        ; Make last char the new terminator
  267.         .CONTINUE                       ; Continue polling for keystrokes
  268.         .ENDIF
  269.  
  270.         .CONTINUE .IF bx > Max          ; Ignore key if too many letters
  271.         sub     ah, ah
  272.         mov     [bx], ax                ; Store letter and null terminator
  273.         .ENDW
  274.  
  275.         ret
  276.  
  277. StrInput ENDP
  278.  
  279.  
  280. ;* ClearBox - Clears portion of screen with specified fill attribute.
  281. ;*
  282. ;* Shows:   BIOS Interrupt - 10h, Function 6 (Scroll Up)
  283. ;*
  284. ;* Params:  Attr - Fill attribute
  285. ;*          Row1 - Top screen row of cleared section
  286. ;*          Col1 - Left column of cleared section
  287. ;*          Row2 - Bottom screen row of cleared section
  288. ;*          Col2 - Right column of cleared section
  289. ;*
  290. ;* Return:  None
  291.  
  292. ClearBox PROC,
  293.         Attr:WORD,
  294.         Row1:WORD,
  295.         Col1:WORD,
  296.         Row2:WORD,
  297.         Col2:WORD
  298.  
  299.         mov     ax, 0600h               ; Scroll service
  300.         mov     bh, BYTE PTR Attr       ; BH = fill attribute
  301.         mov     ch, BYTE PTR Row1       ; CH = top row of clear area
  302.         mov     cl, BYTE PTR Col1       ; CL = left column 
  303.         mov     dh, BYTE PTR Row2       ; DH = bottom row of clear area
  304.         mov     dl, BYTE PTR Col2       ; DL = right column
  305.         int     10h                     ; Clear screen by scrolling up
  306.         ret
  307.  
  308. ClearBox ENDP
  309.  
  310.  
  311. ;* DisableCga - Disables CGA video by reprogramming the control register.
  312. ;*
  313. ;* Shows:   Instructions - cli     sti
  314. ;*
  315. ;* Params:  None
  316. ;*
  317. ;* Return:  None
  318.  
  319. DisableCga PROC USES ax cx dx           ; Preserve registers
  320.  
  321.         mov     cx, -1                  ; Set maximum loop count
  322.         mov     dx, 03DAh               ; Address of status register
  323.  
  324.         .REPEAT
  325.         in      al, dx                  ; Get video status
  326.         .UNTILCXZ !(al & 8)             ; Until retrace end/timeout
  327.         cli                             ; Disallow interruptions
  328.         mov     cx, -1                  ; Reset loop count
  329.  
  330.         .REPEAT
  331.         in      al, dx                  ; Get video status
  332.         .UNTILCXZ al & 8                ; Until retrace start/timeout
  333.  
  334.         sub     dx, 2                   ; DX = address of control reg
  335.         mov     al, 1                   ; Value to disable CGA video
  336.         out     dx, al                  ; Disable video
  337.         sti                             ; Reenable interrupts
  338.         ret
  339.  
  340. DisableCga ENDP
  341.  
  342.  
  343. ;* EnableCga - Enables CGA video by reprogramming the control register.
  344. ;*
  345. ;* Params:  None
  346. ;*
  347. ;* Return:  None
  348.  
  349. EnableCga PROC USES ax dx es                    ; Preserve registers
  350.  
  351.         sub     ax, ax
  352.         mov     es, ax                          ; Point ES to low memory
  353.         mov     al, es:[0465h]                  ; Get former mode setting
  354.         mov     dx, 03D8h                       ; Address of control register
  355.         out     dx, al                          ; Enable video
  356.         ret
  357.  
  358. EnableCga ENDP
  359.  
  360.  
  361. ;* GetVer - Gets DOS version.
  362. ;*
  363. ;* Shows:   DOS Function - 30h (Get MS-DOS Version Number)
  364. ;*
  365. ;* Params:  None
  366. ;*
  367. ;* Return:  Short integer of form (M*100)+m, where M is major
  368. ;*          version number and m is minor version, or integer 
  369. ;*          is 0 if DOS version earlier than 2.0
  370.  
  371. GetVer  PROC
  372.  
  373.         mov     ah, 30h                 ; DOS Function 30h
  374.         int     21h                     ; Get MS-DOS version number
  375.         .IF     al == 0                 ; If version, version 1
  376.         sub     ax, ax                  ; Set AX to 0
  377.         .ELSE                           ; Version 2.0 or higher
  378.         sub     ch, ch                  ; Zero CH and move minor
  379.         mov     cl, ah                  ;   version number into CX
  380.         mov     bl, 100
  381.         mul     bl                      ; Multiply major by 10
  382.         add     ax, cx                  ; Add minor to major*10
  383.         .ENDIF
  384.         ret                             ; Return result in AX
  385.  
  386. GetVer  ENDP
  387.  
  388.  
  389. ;* GetKey - Polls for keypress, calling Interrupt 28h while waiting. This
  390. ;* routine simulates Interrupt 16h, service 0, working directly with the
  391. ;* keyboard buffer. It should be used only by TSR programs.
  392. ;*
  393. ;* Bypassing BIOS to access the keyboard buffer is generally not recommended;
  394. ;* however, some applications incorrectly set up a handler for Interrupt 16h,
  395. ;* making them incompatible with TSRs that require keyboard input.
  396. ;*
  397. ;* Shows:   BIOS keyboard data area
  398. ;*
  399. ;* Params:  None
  400. ;*
  401. ;* Return:  AH = scan code
  402. ;*          AL = character
  403.  
  404. GetKey  PROC USES ds bx
  405.  
  406.         mov     ax, 40h                 ; Point DS to data area (see
  407.         mov     ds, ax                  ;   "Keyboard Data Area" in Help)
  408.         mov     ax, WORD PTR ds:[1Ah]   ; AX = pointer to head of kb buffer
  409.  
  410.         .REPEAT
  411.         int     28h                     ; Call Int 28h to signal idle state
  412.         cmp     ax, WORD PTR ds:[1Ch]   ; If head = tail, no key waiting,
  413.         .UNTIL  !zero?                  ;   so continue polling
  414.  
  415.         mov     bx, ax
  416.         mov     ax, WORD PTR [bx]       ; Get keypress from buffer head
  417.         inc     bx                      ; Advance head position
  418.         inc     bx
  419.         .IF     bx >= WORD PTR ds:[82h] ; If advanced past end of buffer,
  420.         mov     bx, WORD PTR ds:[80h]   ;   reset to beginning of buffer
  421.         .ENDIF
  422.         mov     WORD PTR ds:[1Ah], bx   ; Set new head pointer
  423.  
  424.         ret
  425.  
  426. GetKey  ENDP
  427.         
  428.         END
  429.