Subject: x-mode base info

From: [email-masked] (Milosz Danielewski)
Date: 22 Jun 1995 11:22:44 GMT

A few people asked me about VGA x-mode so I decided to write some info.
X-mode is a video mode which allows you to use all 256K of VGA's memory.
After switching in 13h mode ( 320x200 256 colors mode ) video memory is mapped 
>from address A0000h ( ie. A000:0000h ). Each byte is an index of pixel's
color. It's usefull because memory is linear ( each pixel has got own address).
After switching to x-mode video memory is still at A0000h but each address is
conected witch 4 pixels.So how to choose one pixel ? This macro shows the way:

             macro bank nr  
                 
                 mov ah,nr      ; which pixel going to be set  nr - mask
                 mov al,02h     ; Map Mask Register Index
                 mov dx,03C4h   ; dx = base address of sequencer
                 out dx,ax
             
             endm
 
 What does it mean that 'nr' is a mask ? Only four less significant bits
are important and each bit corresponds with pixel. Bit #0 with first pixel   
on the left , bit #1 with second , bit #2 with third and bit #3 with fourth. 

Example:      ; processor is in real mode
              ; es = 0A000h  di - offset on the screen
              ; We want to set all even pixels to color #15 
              
              mov ah,0101b  ; mask for pixels
              mov al,02h
              mov dx,03C4h
              out dx,ax
              
              xor di,di
              mov cx,80*200  ; screen size ( 80 columns , 200 rows )
              mov al,15
              cld
              rep stosb
          
( All examples are NOT speed optimized but clear )

         pixels at one address:

          OOO   ###   OOO  ###              ###  - modificated pixel
          OOO   ###   OOO  ###              ###
           \                |
            \____           |               OOO  - non modificated pixel
                 \          |               OOO
                  _\________|
                 /   \
               /       \
             /           \
           /               \
           1     0     1    0      =  mask

Bit complicated ?  In practice you should write algorithms  with minimal
number of mask changes ( because 'out' is slow ). At the beginning you
can have some troubles like drawing lines etc. but you need only some
expirience.

  Point of x-mode is that you can plot,paint,draw etc. onto invisible
page of video memory during the next is visible.How to change pages ?

              mov al,0Ch    ; Start address high register
              mov dx,03D4h
              out dx,al
              inc dx
              mov al,xx   ; xx = most significant part of page address
              out dx,al
              dec dx
              mov al,0Dh    ; start address low register
              out dx,al
              inc dx
              mov al,yy   ; yy = less significant part of page address
              out dx,al

Actually , page address is an offset, base address is always A0000h.
Changing start address you can scroll screen in each direction or change
visible page etc. ( If you increase start address by 80 , screen will
scroll up 1 line ).

  OK. How to switch into x-mode ? This part do it for us:

                 mov ax,13h
                 int 10h
                 mov dx,3C4h
                 mov al,4
                 out dx,al
                 inc dx
                 in al,dx
                 and al,0F7h
                 or al,4
                 out dx,al
                 mov dx,3CEh
                 mov al,5
                 out dx,al
                 inc dx
                 in al,dx
                 and al,0EFh
                 out dx,al
                 dec dx
                 mov al,6
                 out dx,al
                 inc dx
                 in al,dx
                 and al,0FDh
                 out dx,al
                 mov dx,03C4h
                 mov ax,0F02h
                 out dx,ax
                 mov ax,0A000h
                 mov es,ax
                 xor ax,ax
                 xor di,di
                 mov cx,32000
                 cld
                 rep stosw
                 mov dx,3D4h
                 mov al,14h
                 out dx,al
                 inc dx
                 in al,dx
                 and al,0BFh
                 out dx,al
                 dec dx
                 mov al,17h
                 out dx,al
                 inc dx
                 in al,dx
                 or al,40h
                 out dx,al

 How does it work ? Soon I'll post full registers description.

 Aspect ratio in 320x200 is weird so people made new mode: 320x240.                
Rules are same as in 320x200 x-mode. This one is ripped by Dobrov from 
Pinbal Fantasies:
                
                mov ax,0013h
                int 10h
                mov dx,3D4h
                mov ax,0011h
                out dx,ax
                mov dx,3C4h
                mov al,4
                out dx,al
                inc dx
                in  al,dx
                and al,0F6h
                out dx,al
                mov dx,3D4h
                mov al,14h
                out dx,ax
                inc dx
                in  al,dx
                and al,0BFh
                out dx,al
                mov dx,3d4h
                mov ax,0E317h
                out dx,ax
                mov dx,3CCh
                in  al,dx
                or  al,0C0h
                mov dx,3c2h
                out dx,al
                mov dx,3D4h
                mov al,11h
                out dx,al
                inc dx
                in  al,dx
                and al,7Fh
                out dx,al
                mov dx,3D4h
                mov ax,0D06h
                out dx,ax
                mov ax,3E07h
                out dx,ax
                mov ax,0C009h
                out dx,ax
                mov ax,0EA10h
                out dx,ax
                mov ax,0C11h
                out dx,ax
                mov ax,0DF12h
                out dx,ax
                mov ax,0014h
                out dx,ax
                mov ax,0EA15h
                out dx,ax
                mov ax,0616h
                out dx,ax
                mov ax,0E317h
                out dx,ax

Enough typing for me. See you ...

P.S  Many games and demos use x-mode ( DOOM etc.) but some groups on the demo
scene don't use x-modes, maybe it's a new fashion ?