home *** CD-ROM | disk | FTP | other *** search
/ Assembly 1994 - The 3rd Phase / ASMROM94.mdf / sources / modex.txt < prev    next >
Text File  |  1994-11-12  |  23KB  |  580 lines

  1. _GRAPHICS PROGRAMMING COLUMN_
  2. by Michael Abrash
  3.  
  4.  
  5. [LISTING ONE]
  6.  
  7. ; Mode X (320x240, 256 colors) mode set routine. Works on all VGAs.
  8. ; C near-callable as:
  9. ;       void Set320x240Mode(void);
  10. ; Tested with TASM 2.0.
  11. ; Modified from public-domain mode set code by John Bridges.
  12.  
  13. SC_INDEX equ    03c4h   ;Sequence Controller Index
  14. CRTC_INDEX equ  03d4h   ;CRT Controller Index
  15. MISC_OUTPUT equ 03c2h   ;Miscellaneous Output register
  16. SCREEN_SEG equ  0a000h  ;segment of display memory in mode X
  17.  
  18.         .model  small
  19.         .data
  20. ; Index/data pairs for CRT Controller registers that differ between
  21. ; mode 13h and mode X.
  22. CRTParms label  word
  23.         dw      00d06h  ;vertical total
  24.         dw      03e07h  ;overflow (bit 8 of vertical counts)
  25.         dw      04109h  ;cell height (2 to double-scan)
  26.         dw      0ea10h  ;v sync start
  27.         dw      0ac11h  ;v sync end and protect cr0-cr7
  28.         dw      0df12h  ;vertical displayed
  29.         dw      00014h  ;turn off dword mode
  30.         dw      0e715h  ;v blank start
  31.         dw      00616h  ;v blank end
  32.         dw      0e317h  ;turn on byte mode
  33. CRT_PARM_LENGTH equ     (($-CRTParms)/2)
  34.  
  35.         .code
  36.         public  _Set320x240Mode
  37. _Set320x240Mode proc    near
  38.         push    bp      ;preserve caller's stack frame
  39.         push    si      ;preserve C register vars
  40.         push    di      ; (don't count on BIOS preserving anything)
  41.  
  42.         mov     ax,13h  ;let the BIOS set standard 256-color
  43.         int     10h     ; mode (320x200 linear)
  44.  
  45.         mov     dx,SC_INDEX
  46.         mov     ax,0604h
  47.         out     dx,ax   ;disable chain4 mode
  48.         mov     ax,0100h
  49.         out     dx,ax   ;synchronous reset while switching clocks
  50.  
  51.         mov     dx,MISC_OUTPUT
  52.         mov     al,0e7h
  53.         out     dx,al   ;select 28 MHz dot clock & 60 Hz scanning rate
  54.  
  55.         mov     dx,SC_INDEX
  56.         mov     ax,0300h
  57.         out     dx,ax   ;undo reset (restart sequencer)
  58.  
  59.         mov     dx,CRTC_INDEX ;reprogram the CRT Controller
  60.         mov     al,11h  ;VSync End reg contains register write
  61.         out     dx,al   ; protect bit
  62.         inc     dx      ;CRT Controller Data register
  63.         in      al,dx   ;get current VSync End register setting
  64.         and     al,7fh  ;remove write protect on various
  65.         out     dx,al   ; CRTC registers
  66.         dec     dx      ;CRT Controller Index
  67.         cld
  68.         mov     si,offset CRTParms ;point to CRT parameter table
  69.         mov     cx,CRT_PARM_LENGTH ;# of table entries
  70. SetCRTParmsLoop:
  71.         lodsw           ;get the next CRT Index/Data pair
  72.         out     dx,ax   ;set the next CRT Index/Data pair
  73.         loop    SetCRTParmsLoop
  74.  
  75.         mov     dx,SC_INDEX
  76.         mov     ax,0f02h
  77.         out     dx,ax   ;enable writes to all four planes
  78.         mov     ax,SCREEN_SEG ;now clear all display memory, 8 pixels
  79.         mov     es,ax         ; at a time
  80.         sub     di,di   ;point ES:DI to display memory
  81.         sub     ax,ax   ;clear to zero-value pixels
  82.         mov     cx,8000h ;# of words in display memory
  83.         rep     stosw   ;clear all of display memory
  84.  
  85.         pop     di      ;restore C register vars
  86.         pop     si
  87.         pop     bp      ;restore caller's stack frame
  88.         ret
  89. _Set320x240Mode endp
  90.         end
  91.  
  92.  
  93.  
  94. [LISTING TWO]
  95.  
  96. ; Mode X (320x240, 256 colors) write pixel routine. Works on all VGAs.
  97. ; No clipping is performed.
  98. ; C near-callable as:
  99. ;    void WritePixelX(int X, int Y, unsigned int PageBase, int Color);
  100.  
  101. SC_INDEX equ    03c4h   ;Sequence Controller Index
  102. MAP_MASK equ    02h     ;index in SC of Map Mask register
  103. SCREEN_SEG equ  0a000h  ;segment of display memory in mode X
  104. SCREEN_WIDTH equ 80     ;width of screen in bytes from one scan line
  105.                         ; to the next
  106.  
  107. parms   struc
  108.         dw      2 dup (?) ;pushed BP and return address
  109. X       dw      ?       ;X coordinate of pixel to draw
  110. Y       dw      ?       ;Y coordinate of pixel to draw
  111. PageBase dw     ?       ;base offset in display memory of page in
  112.                         ; which to draw pixel
  113. Color   dw      ?       ;color in which to draw pixel
  114. parms   ends
  115.  
  116.         .model  small
  117.         .code
  118.         public  _WritePixelX
  119. _WritePixelX    proc    near
  120.         push    bp      ;preserve caller's stack frame
  121.         mov     bp,sp   ;point to local stack frame
  122.  
  123.         mov     ax,SCREEN_WIDTH
  124.         mul     [bp+Y]  ;offset of pixel's scan line in page
  125.         mov     bx,[bp+X]
  126.         shr     bx,1
  127.         shr     bx,1    ;X/4 = offset of pixel in scan line
  128.         add     bx,ax   ;offset of pixel in page
  129.         add     bx,[bp+PageBase] ;offset of pixel in display memory
  130.         mov     ax,SCREEN_SEG
  131.         mov     es,ax   ;point ES:BX to the pixel's address
  132.  
  133.         mov     cl,byte ptr [bp+X]
  134.         and     cl,011b ;CL = pixel's plane
  135.         mov     ax,0100h + MAP_MASK ;AL = index in SC of Map Mask reg
  136.         shl     ah,cl   ;set only the bit for the pixel's plane to 1
  137.         mov     dx,SC_INDEX ;set the Map Mask to enable only the
  138.         out     dx,ax       ; pixel's plane
  139.  
  140.         mov     al,byte ptr [bp+Color]
  141.         mov     es:[bx],al ;draw the pixel in the desired color
  142.  
  143.         pop     bp      ;restore caller's stack frame
  144.         ret
  145. _WritePixelX    endp
  146.         end
  147.  
  148.  
  149.  
  150.  
  151. [LISTING THREE]
  152.  
  153. ; Mode X (320x240, 256 colors) read pixel routine. Works on all VGAs.
  154. ; No clipping is performed.
  155. ; C near-callable as:
  156. ;    unsigned int ReadPixelX(int X, int Y, unsigned int PageBase);
  157.  
  158. GC_INDEX equ    03ceh   ;Graphics Controller Index
  159. READ_MAP equ    04h     ;index in GC of the Read Map register
  160. SCREEN_SEG equ  0a000h  ;segment of display memory in mode X
  161. SCREEN_WIDTH equ 80     ;width of screen in bytes from one scan line
  162.                         ; to the next
  163. parms   struc
  164.         dw      2 dup (?) ;pushed BP and return address
  165. X       dw      ?       ;X coordinate of pixel to read
  166. Y       dw      ?       ;Y coordinate of pixel to read
  167. PageBase dw     ?       ;base offset in display memory of page from
  168.                         ; which to read pixel
  169. parms   ends
  170.  
  171.         .model  small
  172.         .code
  173.         public  _ReadPixelX
  174. _ReadPixelX     proc    near
  175.         push    bp      ;preserve caller's stack frame
  176.         mov     bp,sp   ;point to local stack frame
  177.  
  178.         mov     ax,SCREEN_WIDTH
  179.         mul     [bp+Y]  ;offset of pixel's scan line in page
  180.         mov     bx,[bp+X]
  181.         shr     bx,1
  182.         shr     bx,1    ;X/4 = offset of pixel in scan line
  183.         add     bx,ax   ;offset of pixel in page
  184.         add     bx,[bp+PageBase] ;offset of pixel in display memory
  185.         mov     ax,SCREEN_SEG
  186.         mov     es,ax   ;point ES:BX to the pixel's address
  187.  
  188.         mov     ah,byte ptr [bp+X]
  189.         and     ah,011b ;AH = pixel's plane
  190.         mov     al,READ_MAP ;AL = index in GC of the Read Map reg
  191.         mov     dx,GC_INDEX ;set the Read Map to read the pixel's
  192.         out     dx,ax       ; plane
  193.  
  194.         mov     al,es:[bx] ;read the pixel's color
  195.         sub     ah,ah   ;convert it to an unsigned int
  196.  
  197.         pop     bp      ;restore caller's stack frame
  198.         ret
  199. _ReadPixelX     endp
  200.         end
  201.  
  202.  
  203.  
  204. [LISTING FOUR]
  205.  
  206. ; Mode X (320x240, 256 colors) rectangle fill routine. Works on all
  207. ; VGAs. Uses slow approach that selects the plane explicitly for each
  208. ; pixel. Fills up to but not including the column at EndX and the row
  209. ; at EndY. No clipping is performed.
  210. ; C near-callable as:
  211. ;    void FillRectangleX(int StartX, int StartY, int EndX, int EndY,
  212. ;       unsigned int PageBase, int Color);
  213.  
  214. SC_INDEX equ    03c4h   ;Sequence Controller Index
  215. MAP_MASK equ    02h     ;index in SC of Map Mask register
  216. SCREEN_SEG equ  0a000h  ;segment of display memory in mode X
  217. SCREEN_WIDTH equ 80     ;width of screen in bytes from one scan line
  218.                         ; to the next
  219. parms   struc
  220.         dw      2 dup (?) ;pushed BP and return address
  221. StartX  dw      ?       ;X coordinate of upper left corner of rect
  222. StartY  dw      ?       ;Y coordinate of upper left cor