home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter28 / l28-1.asm next >
Assembly Source File  |  1997-06-18  |  6KB  |  239 lines

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