home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter49 / l49-4.asm < prev    next >
Assembly Source File  |  1997-06-18  |  6KB  |  122 lines

  1. ; Mode X (320x240, 256 colors) system memory to display memory copy
  2. ; routine. Uses approach of changing the plane for each pixel copied;
  3. ; this is slower than copying all pixels in one plane, then all pixels
  4. ; in the next plane, and so on, but it is simpler; besides, images for
  5. ; which performance is critical should be stored in off-screen memory
  6. ; and copied to the screen via the latches. Copies up to but not
  7. ; including the column at SourceEndX and the row at SourceEndY. No
  8. ; clipping is performed.
  9. ; Tested with TASM 4.0 by Jim Mischel 12/16/94.
  10. ; C near-callable as:
  11. ;
  12. ;    void CopySystemToScreenX(int SourceStartX, int SourceStartY,
  13. ;       int SourceEndX, int SourceEndY, int DestStartX,
  14. ;       int DestStartY, char* SourcePtr, unsigned int DestPageBase,
  15. ;       int SourceBitmapWidth, int DestBitmapWidth);
  16.  
  17. SC_INDEX equ    03c4h   ;Sequence Controller Index register port
  18. MAP_MASK equ    02h     ;index in SC of Map Mask register
  19. SCREEN_SEG equ  0a000h  ;segment of display memory in Mode X
  20.  
  21. parms   struc
  22.         dw      2 dup (?) ;pushed BP and return address
  23. SourceStartX dw ?       ;X coordinate of upper left corner of source
  24. SourceStartY dw ?       ;Y coordinate of upper left corner of source
  25. SourceEndX   dw ?       ;X coordinate of lower right corner of source
  26.                         ; (the row at EndX is not copied)
  27. SourceEndY   dw ?       ;Y coordinate of lower right corner of source
  28.                         ; (the column at EndY is not copied)
  29. DestStartX   dw ?       ;X coordinate of upper left corner of dest
  30. DestStartY   dw ?       ;Y coordinate of upper left corner of dest
  31. SourcePtr    dw ?       ;pointer in DS to start of bitmap in which
  32.                         ; source resides
  33. DestPageBase dw ?       ;base offset in display memory of page in
  34.                         ; which dest resides
  35. SourceBitmapWidth dw ?  ;# of pixels across source bitmap
  36. DestBitmapWidth   dw ?  ;# of pixels across dest bitmap
  37.                         ; (must be a multiple of 4)
  38. parms   ends
  39.  
  40. RectWidth equ   -2      ;local storage for width of rectangle
  41. LeftMask equ    -4      ;local storage for left rect edge plane mask
  42. STACK_FRAME_SIZE equ 4
  43.  
  44.         .model  small
  45.         .code
  46.         public  _CopySystemToScreenX
  47. _CopySystemToScreenX proc    near
  48.         push    bp      ;preserve caller's stack frame
  49.         mov     bp,sp   ;point to local stack frame
  50.         sub     sp,STACK_FRAME_SIZE ;allocate space for local vars
  51.         push    si      ;preserve caller's register variables
  52.         push    di
  53.  
  54.         cld
  55.         mov     ax,SCREEN_SEG   ;point ES to display memory
  56.         mov     es,ax
  57.         mov     ax,[bp+SourceBitmapWidth]
  58.         mul     [bp+SourceStartY] ;top source rect scan line
  59.         add     ax,[bp+SourceStartX]
  60.         add     ax,[bp+SourcePtr] ;offset of first source rect pixel
  61.         mov     si,ax             ; in DS
  62.         
  63.         mov     ax,[bp+DestBitmapWidth]
  64.         shr     ax,1            ;convert to width in addresses
  65.         shr     ax,1
  66.         mov     [bp+DestBitmapWidth],ax ;remember address width
  67.         mul     [bp+DestStartY] ;top dest rect scan line
  68.         mov     di,[bp+DestStartX]
  69.         mov     cx,di
  70.         shr     di,1    ;X/4 = offset of first dest rect pixel in
  71.         shr     di,1    ; scan line
  72.         add     di,ax   ;offset of first dest rect pixel in page
  73.         add     di,[bp+DestPageBase] ;offset of first dest rect pixel
  74.                         ; in display memory
  75.         and     cl,011b ;CL = first dest pixel's plane
  76.         mov     al,11h  ;upper nibble comes into play when plane wraps
  77.                         ; from 3 back to 0
  78.         shl     al,cl   ;set the bit for the first dest pixel's plane
  79.         mov     [bp+LeftMask],al ; in each nibble to 1
  80.  
  81.         mov     cx,[bp+SourceEndX]   ;calculate # of pixels across
  82.         sub     cx,[bp+SourceStartX] ; rect
  83.         jle     CopyDone        ;skip if 0 or negative width
  84.         mov     [bp+RectWidth],cx
  85.         mov     bx,[bp+SourceEndY]
  86.         sub     bx,[bp+SourceStartY]  ;BX = height of rectangle
  87.         jle     CopyDone        ;skip if 0 or negative height
  88.         mov     dx,SC_INDEX     ;point to SC Index register
  89.         mov     al,MAP_MASK
  90.         out     dx,al           ;point SC Index reg to the Map Mask
  91.         inc     dx              ;point DX to SC Data reg
  92. CopyRowsLoop:
  93.         mov     ax,[bp+LeftMask]
  94.         mov     cx,[bp+RectWidth]
  95.         push    si      ;remember the start offset in the source
  96.         push    di      ;remember the start offset in the dest
  97. CopyScanLineLoop:
  98.         out     dx,al           ;set the plane for this pixel
  99.         movsb                   ;copy the pixel to the screen
  100.         rol     al,1            ;set mask for next pixel's plane
  101.         cmc                     ;advance destination address only when
  102.         sbb     di,0            ; wrapping from plane 3 to plane 0
  103.                                 ; (else undo INC DI done by MOVSB)
  104.         loop    CopyScanLineLoop
  105.         pop     di      ;retrieve the dest start offset
  106.         add     di,[bp+DestBitmapWidth] ;point to the start of the
  107.                                         ; next scan line of the dest
  108.         pop     si      ;retrieve the source start offset
  109.         add     si,[bp+SourceBitmapWidth] ;point to the start of the
  110.                                         ; next scan line of the source
  111.         dec     bx      ;count down scan lines
  112.         jnz     CopyRowsLoop
  113. CopyDone:
  114.         pop     di      ;restore caller's register variables
  115.         pop     si
  116.         mov     sp,bp   ;discard storage for local variables
  117.         pop     bp      ;restore caller's stack frame
  118.         ret
  119. _CopySystemToScreenX endp
  120.         end
  121.  
  122.