home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / asm / PJGRAPH.ZIP / CHAP10.2 < prev    next >
Encoding:
Text File  |  1989-09-26  |  9.0 KB  |  262 lines

  1. ; Program to demonstrate the two pages available in 320x400
  2. ; 256-color modes on a VGA.  Draws diagonal color bars in all
  3. ; 256 colors in page 0, then does the same in page 1 (but with
  4. ; the bars tilted the other way), and finally draws vertical
  5. ; color bars in page 0.
  6. ;
  7. ; Updated 6/29/89.
  8. ;
  9. VGA_SEGMENT     equ     0a000h
  10. SC_INDEX        equ     3c4h    ;Sequence Controller Index register
  11. GC_INDEX        equ     3ceh    ;Graphics Controller Index register
  12. CRTC_INDEX      equ     3d4h    ;CRT Controller Index register
  13. MAP_MASK        equ     2       ;Map Mask register index in SC
  14. MEMORY_MODE     equ     4       ;Memory Mode register index in SC
  15. MAX_SCAN_LINE   equ     9       ;Maximum Scan Line reg index in CRTC
  16. START_ADDRESS_HIGH equ  0ch     ;Start Address High reg index in CRTC
  17. UNDERLINE       equ     14h     ;Underline Location reg index in CRTC
  18. MODE_CONTROL    equ     17h     ;Mode Control register index in CRTC
  19. GRAPHICS_MODE   equ     5       ;Graphics Mode register index in GC
  20. MISCELLANEOUS   equ     6       ;Miscellaneous register index in GC
  21. SCREEN_WIDTH    equ     320     ;# of pixels across screen
  22. SCREEN_HEIGHT   equ     400     ;# of scan lines on screen
  23. WORD_OUTS_OK    equ     1       ;set to 0 to assemble for
  24.                                 ; computers that can't handle
  25.                                 ; word outs to indexed VGA registers
  26. ;
  27. stackseg        segment para stack 'STACK'
  28.         db      512 dup (?)
  29. stackseg        ends
  30. ;
  31. ; Macro to output a word value to a port.
  32. ;
  33. OUT_WORD        macro
  34. if WORD_OUTS_OK
  35.         out     dx,ax
  36. else
  37.         out     dx,al
  38.         inc     dx
  39.         xchg    ah,al
  40.         out     dx,al
  41.         dec     dx
  42.         xchg    ah,al
  43. endif
  44.         endm
  45. ;
  46. ; Macro to output a constant value to an indexed VGA register.
  47. ;
  48. CONSTANT_TO_INDEXED_REGISTER    macro   ADDRESS, INDEX, VALUE
  49.         mov     dx,ADDRESS
  50.         mov     ax,(VALUE shl 8) + INDEX
  51.         OUT_WORD
  52.         endm
  53. ;
  54. Code    segment
  55.         assume  cs:Code
  56. Start   proc    near
  57. ;
  58. ; Set 320x400 256-color mode.
  59. ;
  60.         call    Set320By400Mode
  61. ;
  62. ; We're in 320x400 256-color mode, with page 0 displayed.
  63. ; Let's fill page 0 with color bars slanting down and to the left.
  64. ;
  65.         sub     di,di           ;page 0 starts at address 0
  66.         mov     bl,1            ;make color bars slant down and
  67.                                 ; to the left
  68.         call    ColorBarsUp     ;draw the color bars
  69. ;
  70. ; Now do the same for page 1, but with the color bars
  71. ; tilting the other way.
  72. ;
  73.         mov     di,8000h        ;page 1 starts at address 8000h
  74.         mov     bl,-1           ;make color bars slant down and
  75.                                 ; to the right
  76.         call    ColorBarsUp     ;draw the color bars
  77. ;
  78. ; Wait for a key and flip to page 1 when one is pressed.
  79. ;
  80.         call    GetNextKey
  81.         CONSTANT_TO_INDEXED_REGISTER CRTC_INDEX,START_ADDRESS_HIGH,80h
  82.                                 ;set the Start Address High register
  83.                                 ; to 80h, for a start address of 8000h
  84. ;
  85. ; Draw vertical bars in page 0 while page 1 is displayed.
  86. ;
  87.         sub     di,di           ;page 0 starts at address 0
  88.         sub     bl,bl           ;make color bars vertical
  89.         call    ColorBarsUp     ;draw the color bars
  90. ;
  91. ; Wait for another key and flip back to page 0 when one is pressed.
  92. ;
  93.         call    GetNextKey
  94.         CONSTANT_TO_INDEXED_REGISTER CRTC_INDEX,START_ADDRESS_HIGH,00h
  95.                                 ;set the Start Address High register
  96.                                 ; to 00h, for a start address of 0000h
  97. ;
  98. ; Wait for yet another key and return to text mode and end when
  99. ; one is pressed.
  100. ;
  101.         call    GetNextKey
  102.         mov     ax,0003h
  103.         int     10h     ;text mode
  104.         mov     ah,4ch
  105.         int     21h     ;done
  106. ;
  107. Start   endp
  108. ;
  109. ; Sets up 320x400 256-color modes.
  110. ;
  111. ; Input: none
  112. ;
  113. ; Output: none
  114. ;
  115. Set320By400Mode proc    near
  116. ;
  117. ; First, go to normal 320x200 256-color mode, which is really a
  118. ; 320x400 256-color mode with each line scanned twice.
  119. ;
  120.         mov     ax,0013h  ;AH = 0 means mode set, AL = 13h selects
  121.                           ; 256-color graphics mode
  122.         int     10h       ;BIOS video interrupt
  123. ;
  124. ; Change CPU addressing of video memory to linear (not odd/even,
  125. ; chain, or chain 4), to allow us to access all 256K of display
  126. ; memory. When this is done, VGA memory will look just like memory
  127. ; in modes 10h and 12h, except that each byte of display memory will
  128. ; control one 256-color pixel, with 4 adjacent pixels at any given
  129. ; address, one pixel per plane.
  130. ;
  131.         mov     dx,SC_INDEX
  132.         mov     al,MEMORY_MODE
  133.         out     dx,al
  134.         inc     dx
  135.         in      al,dx
  136.         and     al,not 08h      ;turn off chain 4
  137.         or      al,04h          ;turn off odd/even
  138.         out     dx,al
  139.         mov     dx,GC_INDEX
  140.         mov     al,GRAPHICS_MODE
  141.         out     dx,al
  142.         inc     dx
  143.         in      al,dx
  144.         and     al,not 10h      ;turn off odd/even
  145.         out     dx,al
  146.         dec     dx
  147.         mov     al,MISCELLANEOUS
  148.         out     dx,al
  149.         inc     dx
  150.         in      al,dx
  151.         and     al,not 02h      ;turn off chain
  152.         out     dx,al
  153. ;
  154. ; Now clear the whole screen, since the mode 13h mode set only
  155. ; cleared 64K out of the 256K of display memory. Do this before
  156. ; we switch the CRTC out of mode 13h, so we don't see garbage
  157. ; on the screen when we make the switch.
  158. ;
  159.         CONSTANT_TO_INDEXED_REGISTER SC_INDEX,MAP_MASK,0fh
  160.                                 ;enable writes to all planes, so
  161.                                 ; we can clear 4 pixels at a time
  162.         mov     ax,VGA_SEGMENT
  163.         mov     es,ax
  164.         sub     di,di
  165.         mov     ax,di
  166.         mov     cx,8000h        ;# of words in 64K
  167.         cld
  168.         rep     stosw           ;clear all of display memory
  169. ;
  170. ; Tweak the mode to 320x400 256-color mode by not scanning each
  171. ; line twice.
  172. ;
  173.         mov     dx,CRTC_INDEX
  174.         mov     al,MAX_SCAN_LINE
  175.         out     dx,al
  176.         inc     dx
  177.         in      al,dx
  178.         and     al,not 1fh      ;set maximum scan line = 0
  179.         out     dx,al
  180.         dec     dx
  181. ;
  182. ; Change CRTC scanning from doubleword mode to byte mode, allowing
  183. ; the CRTC to scan more than 64K of video data.
  184. ;
  185.         mov     al,UNDERLINE
  186.         out     dx,al
  187.         inc     dx
  188.         in      al,dx
  189.         and     al,not 40h      ;turn off doubleword
  190.         out     dx,al
  191.         dec     dx
  192.         mov     al,MODE_CONTROL
  193.         out     dx,al
  194.         inc     dx
  195.         in      al,dx
  196.         or      al,40h  ;turn on the byte mode bit, so memory is
  197.                         ; scanned for video data in a purely
  198.                         ; linear way, just as in modes 10h and 12h
  199.         out     dx,al
  200.         ret
  201. Set320By400Mode endp
  202. ;
  203. ; Draws a full screen of slanting color bars in the specified page.
  204. ;
  205. ; Input:
  206. ;       DI = page start address
  207. ;       BL = 1 to make the bars slant down and to the left, -1 to
  208. ;               make them slant down and to the right, 0 to make
  209. ;               them vertical.
  210. ;
  211. ColorBarsUp     proc    near
  212.         mov     ax,VGA_SEGMENT
  213.         mov     es,ax   ;point to display memory
  214.         sub     bh,bh   ;start with color 0
  215.         mov     si,SCREEN_HEIGHT ;# of rows to do
  216.         mov     dx,SC_INDEX
  217.         mov     al,MAP_MASK
  218.         out     dx,al   ;point the SC Index reg to the Map Mask reg
  219.         inc     dx      ;point DX to the SC Data register
  220. RowLoop:
  221.         mov     cx,SCREEN_WIDTH/4
  222.                         ;there are 4 pixels at each address, so
  223.                         ; each 320-pixel row is 80 bytes wide
  224.                         ; in each plane
  225.         push    bx      ;save the row-start color and the slant
  226.                         ; direction
  227. ColumnLoop:
  228. MAP_SELECT = 1
  229.         rept    4       ;do all 4 pixels at this address with
  230.                         ; in-line code
  231.         mov     al,MAP_SELECT
  232.         out     dx,al   ;select planes 0, 1, 2, and 3 in turn
  233.         mov     es:[di],bh ;write this plane's pixel
  234.         inc     bh      ;set the color for the next pixel
  235. MAP_SELECT = MAP_SELECT shl 1
  236.         endm
  237.         inc     di      ;point to the address containing the next
  238.                         ; 4 pixels
  239.         loop    ColumnLoop ;do any remaining pixels on this line
  240.         pop     bx      ;get back the row-start color
  241.         add     bh,bl   ;select next row-start color (controls
  242.                         ; slanting of color bars)
  243.         dec     si      ;count down lines on the screen
  244.         jnz     RowLoop
  245.         ret
  246. ColorBarsUp     endp
  247. ;
  248. ; Waits for the next key and returns it in AX.
  249. ;
  250. GetNextKey      proc    near
  251. WaitKey:
  252.         mov     ah,1
  253.         int     16h
  254.         jz      WaitKey ;wait for a key to become available
  255.         sub     ah,ah
  256.         int     16h     ;read the key
  257.         ret
  258. GetNextKey      endp
  259. ;
  260. Code    ends
  261. ;
  262.         end     Start