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

  1. ; Program to illustrate the use of the Read Map register in read mode 0.
  2. ; Animates by copying a 16-color image from VGA memory to system memory,
  3. ; one plane at a time, then copying the image back to a new location
  4. ; in VGA memory.
  5. ;
  6. ; By Michael Abrash 4/2/88
  7. ;
  8. stackseg        segment word stack 'STACK'
  9.         db      512 dup (?)
  10. stackseg        ends
  11. ;
  12. data    segment word 'DATA'
  13. IMAGE_WIDTH     EQU     4       ;in bytes
  14. IMAGE_HEIGHT    EQU     32      ;in pixels
  15. LEFT_BOUND      EQU     10      ;in bytes
  16. RIGHT_BOUND     EQU     66      ;in bytes
  17. VGA_SEGMENT     EQU     0a000h
  18. SCREEN_WIDTH    EQU     80      ;in bytes
  19. SC_INDEX        EQU     3c4h    ;Sequence Controller Index register
  20. GC_INDEX        EQU     3ceh    ;Graphics Controller Index register
  21. MAP_MASK        EQU     2       ;Map Mask register index in SC
  22. READ_MAP        EQU     4       ;Read Map register index in GC
  23. ;
  24. ; Base pattern for 16-color image.
  25. ;
  26. PatternPlane0   label   byte
  27.         db      32 dup (0ffh,0ffh,0,0)
  28. PatternPlane1   label   byte
  29.         db      32 dup (0ffh,0,0ffh,0)
  30. PatternPlane2   label   byte
  31.         db      32 dup (0f0h,0f0h,0f0h,0f0h)
  32. PatternPlane3   label   byte
  33.         db      32 dup (0cch,0cch,0cch,0cch)
  34. ;
  35. ; Temporary storage for 16-color image during animation.
  36. ;
  37. ImagePlane0     db      32*4 dup (?)
  38. ImagePlane1     db      32*4 dup (?)
  39. ImagePlane2     db      32*4 dup (?)
  40. ImagePlane3     db      32*4 dup (?)
  41. ;
  42. ; Current image location & direction.
  43. ;
  44. ImageX          dw      40      ;in bytes
  45. ImageY          dw      100     ;in pixels
  46. ImageXDirection dw      1       ;in bytes
  47. data    ends
  48. ;
  49. code    segment word 'CODE'
  50.         assume  cs:code,ds:data
  51. Start   proc    near
  52.         cld
  53.         mov     ax,data
  54.         mov     ds,ax
  55. ;
  56. ; Select graphics mode 10h.
  57. ;
  58.         mov     ax,10h
  59.         int     10h
  60. ;
  61. ; Draw the initial image.
  62. ;
  63.         mov     si,offset PatternPlane0
  64.         call    DrawImage
  65. ;
  66. ; Loop to animate by copying the image from VGA memory to system memory,
  67. ; erasing the image, and copying the image from system memory to a new
  68. ; location in VGA memory. Ends when a key is hit.
  69. ;
  70. AnimateLoop:
  71. ;
  72. ; Copy the image from VGA memory to system memory.
  73. ;
  74.         mov     di,offset ImagePlane0
  75.         call    GetImage
  76. ;
  77. ; Clear the image from VGA memory.
  78. ;
  79.         call    EraseImage
  80. ;
  81. ; Advance the image X coordinate, reversing direction if either edge
  82. ; of the screen has been reached.
  83. ;
  84.         mov     ax,[ImageX]
  85.         cmp     ax,LEFT_BOUND
  86.         jz      ReverseDirection
  87.         cmp     ax,RIGHT_BOUND
  88.         jnz     SetNewX
  89. ReverseDirection:
  90.         neg     [ImageXDirection]
  91. SetNewX:
  92.         add     ax,[ImageXDirection]
  93.         mov     [ImageX],ax
  94. ;
  95. ; Draw the image by copying it from system memory to VGA memory.
  96. ;
  97.         mov     si,offset ImagePlane0
  98.         call    DrawImage
  99. ;
  100. ; Slow things down a bit for visibility.
  101. ;
  102.         mov     cx,1000h
  103. DelayLoop:
  104.         loop    DelayLoop
  105. ;
  106. ; See if a key has been hit, ending the program.
  107. ;
  108.         mov     ah,1
  109.         int     16h
  110.         jz      AnimateLoop
  111. ;
  112. ; Clear the key, return to text mode, and return to DOS.
  113. ;
  114.         sub     ah,ah
  115.         int     16h
  116.         mov     ax,3
  117.         int     10h
  118.         mov     ah,4ch
  119.         int     21h
  120. Start   endp
  121. ;
  122. ; Draws the image at offset DS:SI to the current image location in
  123. ; VGA memory.
  124. ;
  125. DrawImage       proc    near
  126.         mov     ax,VGA_SEGMENT
  127.         mov     es,ax
  128.         call    GetImageOffset  ;ES:DI is the destination address for the
  129.                                 ; image in VGA memory
  130.         mov     dx,SC_INDEX
  131.         mov     al,1            ;do plane 0 first
  132. DrawImagePlaneLoop:
  133.         push    di              ;image is drawn at the same offset in
  134.                                 ; each plane
  135.         push    ax              ;preserve plane select
  136.         mov     al,MAP_MASK     ;Map Mask index
  137.         out     dx,al           ;point SC Index to the Map Mask register
  138.         pop     ax              ;get back plane select
  139.         inc     dx              ;point to SC index register
  140.         out     dx,al           ;set up the Map Mask to allow writes to
  141.                                 ; the plane of interest
  142.         dec     dx              ;point back to SC Data register
  143.         mov     bx,IMAGE_HEIGHT ;# of scan lines in image
  144. DrawImageLoop:
  145.         mov     cx,IMAGE_WIDTH  ;# of bytes across image
  146.         rep     movsb
  147.         add     di,SCREEN_WIDTH-IMAGE_WIDTH
  148.                                 ;point to next scan line of image
  149.         dec     bx              ;any more scan lines?
  150.         jnz     DrawImageLoop
  151.         pop     di              ;get back image start offset in VGA memory
  152.         shl     al,1            ;Map Mask setting for next plane
  153.         cmp     al,10h          ;have we done all four planes?
  154.         jnz     DrawImagePlaneLoop
  155.         ret
  156. DrawImage       endp
  157. ;
  158. ; Copies the image from its current location in VGA memory into the
  159. ; buffer at DS:DI.
  160. ;
  161. GetImage        proc    near
  162.         mov     si,di   ;move destination offset into SI
  163.         call    GetImageOffset  ;DI is offset of image in VGA memory
  164.         xchg    si,di   ;SI is offset of image, DI is destination offset
  165.         push    ds
  166.         pop     es      ;ES:DI is destination
  167.         mov     ax,VGA_SEGMENT
  168.         mov     ds,ax   ;DS:SI is source
  169. ;
  170.         mov     dx,GC_INDEX
  171.         sub     al,al           ;do plane 0 first
  172. GetImagePlaneLoop:
  173.         push    si              ;image comes from same offset in
  174.                                 ; each plane
  175.         push    ax              ;preserve plane select
  176.         mov     al,READ_MAP     ;Read Map index
  177.         out     dx,al           ;point GC Index to Read Map register
  178.         pop     ax              ;get back plane select
  179.         inc     dx              ;point to GC Index register
  180.         out     dx,al           ;set up the Read Map to select reads from
  181.                                 ; the plane of interest
  182.         dec     dx              ;point back to GC data register
  183.         mov     bx,IMAGE_HEIGHT ;# of scan lines in image
  184. GetImageLoop:
  185.         mov     cx,IMAGE_WIDTH  ;# of bytes across image
  186.         rep     movsb
  187.         add     si,SCREEN_WIDTH-IMAGE_WIDTH
  188.                                 ;point to next scan line of image
  189.         dec     bx              ;any more scan lines?
  190.         jnz     GetImageLoop
  191.         pop     si              ;get back image start offset
  192.         inc     al              ;Read Map setting for next plane
  193.         cmp     al,4            ;have we done all four planes?
  194.         jnz     GetImagePlaneLoop
  195.         push    es
  196.         pop     ds              ;restore original DS
  197.         ret
  198. GetImage        endp
  199. ;
  200. ; Erases the image at its current location.
  201. ;
  202. EraseImage      proc    near
  203.         mov     dx,SC_INDEX
  204.         mov     al,MAP_MASK
  205.         out     dx,al   ;point SC Index to the Map Mask register
  206.         inc     dx      ;point to SC Data register
  207.         mov     al,0fh
  208.         out     dx,al   ;set up the Map Mask to allow writes to go to
  209.                         ; all 4 planes
  210.         mov     ax,VGA_SEGMENT
  211.         mov     es,ax
  212.         call    GetImageOffset  ;ES:DI points to the start address
  213.                                 ; of the image
  214.         sub     al,al           ;erase with zeros
  215.         mov     bx,IMAGE_HEIGHT;# of scan lines in image
  216. EraseImageLoop:
  217.         mov     cx,IMAGE_WIDTH  ;# of bytes across image
  218.         rep     stosb
  219.         add     di,SCREEN_WIDTH-IMAGE_WIDTH
  220.                                 ;point to next scan line of image
  221.         dec     bx              ;any more scan lines?
  222.         jnz     EraseImageLoop
  223.         ret
  224. EraseImage      endp
  225. ;
  226. ; Returns the current offset of the image in the VGA segment in DI.
  227. ;
  228. GetImageOffset  proc    near
  229.         mov     ax,SCREEN_WIDTH
  230.         mul     [ImageY]
  231.         add     ax,[ImageX]
  232.         mov     di,ax
  233.         ret
  234. GetImageOffset  endp
  235. code    ends
  236.         end     Start