Previous
chapter 26
vga write mode 3
Home
Next
ode That Grows on You Over the last three' overed the VGA's write path from stem to sternwith one exceptio only looked at how writes work in write mode 0, the straightforward, de in which each byte that the CPU writes to display memory fans ur planes. (Actually, we also took a quick look at write mode 1, in whi& the latches are always copied unmodified, but since exactly the same result c a n h achieved by setting the Bit Mask register to 0 in write mode 0, a1 significance.) eful mode, butsome of VGA's most interesting capabilities odes that we have yet to examine: write mode 1, and, espe1 get to write mode 1 in the next chapter, but right now I want to focus on w i t &mode 3, which can be confusing at first, but turns out to be quite a bit morepowerful than onemight initially think.
A Mode Born in Strangeness Write mode 3 is strange indeed, andits use is not immediately obvious. The first time I encountered write mode 3, I understood immediately how it functioned,but could think of very few usefulapplications for it. As time passed, and as I came to understand the atrocious performance characteristics of OUT instructions, and the importance of text and pattern drawing as well, write mode 3 grew considerably in my estimation. In fact, my esteem for this mode ultimately reached the pointwhere
483
in the last major chunk of 16-color graphics code I wrote, write mode 3 was used more than write mode 0 overall, excluding simple pixel copying.So write mode 3 is well worth using, but to use it you must firstunderstand it. Here's how it works. In write mode 3, set/reset is automatically enabled for all four planes (the Enable Set/Reset register is ignored). The CPU data byte is rotated and then ANDed with the contents of the Bit Mask register, and the result of this operation is used as the contents of the Bit Mask register alone would normally beused. (If this is Greek to you, havea look backat Chapters 23 through 25. There's no way to understand write mode 3 without understanding the rest of the VGA's write data path first.) That's what write mode 3 does-but what is it for? It turns outthat write mode 3 is excellent for a surprisingly large number of purposes, because it makes it possible to avoid the bane of VGA performance, OUTS.Some uses for write mode 3 include lines, circles,and solid and two-color pattern fills. Most importantly, writemode 3 is ideal for transparent text; that is, it makes it possible to draw text in l k o l o r graphics mode quickly without wiping out the background in the process. (As we'll see at the end of this chapter, write mode 3 is potentially terrific for opaque text-text drawn with the character box filled in with a solid color-as well.) Listing 26.1 isa modification of code I presented in Chapter 25. That code used the data rotate and bit mask features of the VGA to draw bit-mapped text in write mode 0. Listing 26.1 uses write mode 3 in place of the bit mask to draw bit-mapped text, and in the process gainsthe useful abilityto preserve the background into which the text is being drawn. Where the original text-drawing code drew the entire character box for each character, with 0 bits in the font patterncausing a black box to appear around each character, the code in Listing 26.1 affects displaymemory only when 1 bits in the font pattern are drawn. As a result, the characters appear to be painted into the background, rather than over it. Another advantage of the code in Listing 26.1 is that the characters can be drawn in any of the 16 available colors. LISTING 26.1 126-
1.ASM
: Program t o i l l u s t r a t e o p e r a t i o n o f w r i t e mode 3 o f t h e VGA. ; Draws 8x8 c h a r a c t e r s a t a r b i t r a r yl o c a t i o n sw i t h o u td i s t u r b i n g ; the b a c k g r o u n du, s i n g VGA's 8x8 ROM f o n tD. e s i g n e d ; f o ru s ew i t h modes ODh. OEh. OFh. 10h.and12h. ; Runs o n l y on VGAs ( i n Models 50 & upand I B M D i s p l a yA d a p t e r ; a n 1d 0 0 % compatibles).
; A s s e m b l e dw i t h
MASM
; By M i c h a e A l brash
s t a cske g m e npta rsat a c k 'STACK' 512 d u p ( ? ) db set na dc ks VGA-VIDEO-SEGMENT SCREEN-WIDTH-IN-BYTES FONT-CHARACTER-SIZE
: VGA r e g i s t e re q u a t e s .
484
Chapter 26
equ OaOOOh e0q4u4; oa fhfos fe t 8 equ
;VGA d i s p l a y memory segment BIOS v a r i a b l e ;#ebfcayoihcn tnaehtrs
: S C i n d e xr e g i s t e r :SC map mask r e g i s t e r i n d e x : G C i n d e xr e g i s t e r :GC s e t / r e s e tr e g i s t e ri n d e x :GC e n a b l es e t / r e s e tr e g i s t e ri n d e x :GC d a t ar o t a t e / l o g i c a lf u n c t i o n : r e g i s t e ri n d e x :GC Mode r e g i s t e r :GC b i t mask r e g i s t e r i n d e x
SC-INDEX 3c4h equ SC-MAP-MASK equ 2 GC-INDEX 3ceh equ GC-SET-RESET equ 0 GC-ENABLE-SET-RESET equ 1 GC-ROTATE equ 3
GC-MODE GC-BIT-MASK
equ equ
5 8
dseg segment para common 'DATA' 69 :row dt ios p l taetyseatxt t TEST-TEXT-ROW equ TEST-TEXT-COL :column 17 equ dt ios p l taetyseatxt t TEST-TEXTLWIOTH equ 8 ; w i dotfh a c h a r a cpti ienxre l s Tbey stl eat sbter li n g ' Hdew bl loor ,l d;p!st' ret. itrO snoi tnt .g ? offset ;font d dF o n t P o i n t e r ends dseg c s es ge g m epnat pr au b l i c assume cs:cseg. ds:dseg npesratoarcr t mov ax.dseg mov ds,ax
: S e l e c t6 4 0 x 4 8 0g r a p h i c s mov int
'CODE'
mode.
ax.0lZh 10h
: S e tt h es c r e e nt oa l lb l u e .u s i n gt h er e a d a b i l i t yo f
VGA r e g i s t e r s
: t op r e s e r v er e s e r v e db i t s .
dx.GC-INDEX mov mov a1 .GC-SET-RESET dx,al out dx inc in a1 . d x and a1 .OfOh or a1 .1 dx.al out dx dec al.GC_ENABLE-SET-RESET mov dx.al out dx inc in a1 .dx and a1 ,OfOh or a1 p:lea.snO naeaelftlbhs/flroeers e t dx.al out dx.VGA-VIDEO-SEGMENT mov d i s mov p tl:aopyo .i dn xt e s mov d i .O mov 8000h cx, mov a x . :0sbfeefvtfc/aforhatlefuhuseseet . stosw rep
orteshseoep;etnrlb.atsl ynu e
memory ;fill a lwords l 32k
: w r i t t e na c t u a l l yd o e s n ' tm a t t e r :fill blue with
: S e td r i v e rt ou s et h e8 x 8f o n t . mov mov
ah.llh a1 .30h
:VGA B I O S c h a r a c t e r g e n e r a t o r f u n c t i o n , : r e t u r ni n f os u b f u n c t i o n
VGA Write Mode 3
485
mov bh.3 int 10h S e l ec ac tl lF o n t
; g e t8 x 8f o n tp o i n t e r
; P r i n tt h et e s ts t r i n g ,c y c l i n gt h r o u g hc o l o r s .
mov si .offset Teststring mov bx.TEST-TEXT-ROW mov cx,TEST-TEXT-COL c omov l owr i t;hs t a ar th . 0 StringOutLoop: 1 odsb and a1 ,a1 StringOutDone jz push ax call DrawChar ax POP inc ah and ah.0fh add cx.TEST-TEXT-WIDTH StringOutLoop jmp StringOutDone:
a k e y ,t h e ns e tt ot e x t
; Wait f o r
mov int mov int ; E x i tt o
0
; p r e s e r v ec o l o r ;restorecolor ; n e x tc o l o r ; c o l o r sr a n g ef r o m
0 t o 15
mode & end.
ah.1
21h
; w a i t for a key
ax.3 10h
it reexst t o r e
mode
DOS.
mov int
ah.4ch 21h
Se tnadr pt ; S u b r o u t i n et od r a w
a t e x tc h a r a c t e ri n a l i n e a rg r a p h i c s (ODh. OEh. OFh. 0 1 0 h 0. 1 2 h ) B . a c k g r o u n da r o u n dt h ep i x e l st h a t ; make u pt h ec h a r a c t e ri sp r e s e r v e d . ; F o n tu s e ds h o u l db ep o i n t e dt ob yF o n t P o i n t e r .
:
---
; Input: ; AL c h a r a c t e rt od r a w ; AH c o l o rt od r a wc h a r a c t e ri n( 0 - 1 5 )
: BX
near
row t o d r a wt e x tc h a r a c t e ra t column t o d r a wt e x tc h a r a c t e r
;
CX
; ;
Forces ALU f u n c t i o nt o F o r c e sw r i t e mode 3.
proc DrawChar ax push bx push cx push dx push push si push di bp push ds push p u;apsxrhe s e rcvhea r a cdtreoianrw
486 Chapter 26
at
"move".
AL
mode
: S e tu ps e t / r e s e tt op r o d u c ec h a r a c t e rc o l o r ,u s i n gt h er e a d a b i l i t y : o f VGA r e g i s t e r t o p r e s e r v e t h e s e t t i n g o f r e s e r v e d b i t s dx.GC-INDEX mov mov dx.al out dx inc in and ah.0fhand or dx.al out
: S e l e c tw r i t e
7-4.
a1 .GC_SETLRESET
a1 .dx a1 .OfOh a1 ,ah mode 3 . u s i n g t h e r e a d a b i l i t y o f VGA r e g i s t e r s mode b i t s unchanged.
: t ol e a v eb i t so t h e rt h a nt h ew r i t e mov mov out dx inc in al.3 or dx,al out
dx, GC-I NDEX a1 .GC_MODE dx,al a1 . d x
: S e t DS:SI t o p o i n t t o f o n t
and ES t o p o i n t t o d i s p l a y
s i . [ F o n t P of ion: nptt teo ri ]n t Ids dx.VGA-VIDEO-SEGMENT mov d i s p l atyo : pmov o i n.td x e s
memory.
memory
: C a l c u l a t es c r e e na d d r e s so fb y t ec h a r a c t e rs t a r t s
in.
POP cb:haigdancerarkatatxwoc t e r t:op o i dn spt u s h dx.dx sub mov ax.bx xchg mov
At
BIOS segment data .dxds
di,ds:[SCREEN-WIDTH-IN-BYTES]
POP ds mu1 r:oc waosldfcti auoorlf taf st e t ws icdartsehied:nse e t d i p u s h mov c oatlhs:useim de. cetnx d i and . Oc l; lkl beoetncphol yel u imn n- b ya tded r e s s .1 ds ih r ds ih r ,1 :dsdihi. vrl ci do el ubmy n add b y tpte:ooa ,inandxt d i
: r e t r i e v e BIOS : s c r e e nw i d t h
8 t o make a baydt de r e s s
: C a l c u l a t ef o n ta d d r e s so fc h a r a c t e r . bh.bh sub
shl b x . 1s h l c h a or aff oc tn:eotbi rnfxf .s1he lt add
bx,l
:.obfsfxofi snnet t
:assumes 8c hbpayer tareucst e r : : a moutlht ieprlwy i s e segmentc hoaf r a c t e r
: S e tu pt h e GC r o t a t i o n . I n w r i t e mode 3, t h i s i s t h e r o t a t i o n : o f CPU d a t ab e f o r e i t i s ANDed w i t h t h e B i t Mask r e g i s t e r t o
VGA Write Mode 3
487
: f o r m t h e b i t mask.Forcethe ALU f u n c t i o n t o "move".Uses the : r e a d a b i l i t y o f VGA r e g i s t e r st ol e a v er e s e r v e db i t su n c h a n g e d . dx.GC-INDEX mov mov dx.al out dx inc
in and or dx.al out
: S e tu p
a1 ,GC-ROTATE a1 .dx a1 .OeOh a1 . c l
BH as b i t mask f o r l e f t h a l f .
mov bh.cl shr neg add bl.cl shl
BL as r o t a t i o n f o r r i g h t h a l f .
bx.0ffffh cl c l .0
: Draw t h e c h a r a c t e r , l e f t h a l f f i r s t . t h e n r i g h t h a l f i n t h e : s u c c e e d i n gb y t e ,u s i n gt h ed a t ar o t a t i o nt op o s i t i o nt h ec h a r a c t e r
: a c r o s st h eb y t eb o u n d a r ya n dt h e nu s i n gw r i t e mode 3 t o c o m b i n et h e : c h a r a c t e rd a t aw i t ht h eb i t mask t o a l l o w t h e s e t / r e s e t v a l u e ( t h e
: c h a r a c t e rc o l o r )t h r o u g ho n l yf o rt h ep r o p e rp o r t i o n( w h e r et h e : f o n tb i t sf o rt h ec h a r a c t e ra r e
1) o ft h ec h a r a c t e rf o re a c hb y t e .
: W h e r e v e rt h ef o n tb i t sf o rt h ec h a r a c t e ra r e 0. t h eb a c k g r o u n d : c o l o ri sp r e s e r v e d . : Does n o tc h e c kf o rc a s ew h e r ec h a r a c t e ri sb y t e - a l i g n e da n d ;
n or o t a t i o na n do n l yo n ew r i t ei sr e q u i r e d .
bp.FONT-CHARACTER-SIZE mov dx.GC-INDEX mov wsi cd rt behPOP ae cn k: g e t c x cx dec cdxe c CharacterLoop:
: S e tt h eb i t mov mov dx.ax out
mask f o r t h e l e f t h a l f o f t h e c h a r a c t e r . a1 .GC-BIT-MASK ah.bh
: G e tt h en e x tc h a r a c t e rb y t e : ( L e f th a l f o f character.)
. .
mov mov stosb
: Setthebit mov mov dx.ax out
a1 , [ s i ] ah.es:[di]
; g e tc h a r a c t e rb y t e :1 oad 1 a t c h e s : w r i t ec h a r a c t e rb y t e
a1 .GC-BIT-MASK ah.bl
; G e tt h ec h a r a c t e rb y t ea g a i n
Chapter 26
& w r i t e i t t o d i s p l a y memory.
mask f o r t h e r i g h t h a l f o f t h e c h a r a c t e r .
: ( R i g h th a l fo fc h a r a c t e r . )
488
: - 2 b ebctacfyhwoactadoerhuross e
& w r i t e it t o d i s p l a y memory.
1 odsb mov stosb
; g e tc h a r a c t e rb y t e ah.es:[dil
:1 oad 1 a t c h e s : w r i t ec h a r a c t e rb y t e
; P o i n tt on e x tl i n eo fc h a r a c t e ri nd i s p l a y
add
memory.
, c xd i
bp dec C h a r a jcnt ze r L o o p POP POP pop pop POP POP POP POP ret
endp
ds bP di si dx cx bx ax
DrawChar
: S e tt h ep o i n t e rt ot h ef o n tt od r a wf r o mt o n e aSr eplreocct F o n t mov mov ret eSn ed lpe c t F o n t
ES:BP.
wordC Fpot rn t P o i n t e r:1ps,aobvipnet e r word [pFtor n t P o i n t e r + 2 ] . e s
ends cseg end
start
The key to understanding Listing 26.1 is understanding the effect of ANDing the rotated CPU data with the contents of the Bit Mask register. The CPU data is the pattern for the character to be drawn, with bitsequal to 1indicating where character pixels are to appear. The Data Rotate register is set to rotate the CPU data to pixelalign it, since without rotation characters could only be drawn on byte boundaries.
p
As Ipointed out in Chapter 25, the CPUisperfect&capable of rotating the data itseCf; and it b often the case that that b more efficient. The problem with using the Data Rotate register is that the OUT that sets that register is time-consuming, especially forproportional text, which requires a different rotationfor each character. Also, ifthe code performs full-byteaccesses to display memoly-that is, ifit combines pieces of two adjacent characters into one byte-whenever possible for efficiency, the CPUgenerally has to do extra worktoprepare the data so the VGAk rotator can handle it.
At the same time that the Data Rotate register is set, the Bit Mask register is set to allow the CPU to modify onlythat portion of the display memory byte accessed that the pixel-aligned character falls in, so that other characters and/or graphics data won’t be wiped out. The result of ANDing the rotated CPU data byte with the contents of the Bit Mask register is a bit mask that allows only the bits equal to 1 in the original
VGA Write Mode 3
489
character pattern (rotated and masked to provide pixel alignment) to be modified by the CPU; all other bits come straight from the latches. The latches should have previously been loaded from the target address, so the effect of the ultimate synthesized bit mask value isto allow the CPU to modi* only those pixels in displaymemory that correspond to the 1 bits in that part of the pixel-aligned character that falls in the currently addressed byte. The color of the pixels setby the CPU is determined by the contents of the Set/Reset register. Whew. It sounds complex, but given an understanding of what the data rotator, set/ reset, and the bit mask do, it's not that bad. One good way to make sense of it is to refer to the original text-drawing program in Listing 25.1 back in Chapter 25, and then see how Listing 26.1 differs from that program. It's worth noting that the results generated by Listing 26.1 could have been accomplished without write mode 3. Write mode 0 could have been used instead,but at a significant performance cost. Insteadof letting write mode 3 rotate the CPU data and AND it with the contents ofthe Bit Mask register, the CPU could simply haverotated the CPU data directly and ANDed it with the value destined for the Bit Mask register and then set the Bit Mask register to the resulting value. Additionally, enableset/reset could have been forced on for all planes, emulating what write mode 3 does to provide pixel colors. The write mode 3 approach used in Listing 26.1 can be efficiently extended to drawing large blocks of text. For example, suppose that we were to draw a line of 8-pixel-wide bit-mapped text 40 characters long. We could then set up the bit mask and data rotation as appropriate for the left portion of each bit-aligned character (the portion of each character to the left of the byte boundary) and then draw the left portions only of all 40characters in write mode 3. Then the bit mask could be set up for theright portion of each character, and the right portions of all 40characters do all rotation, and the only could be drawn. The VGA's fast rotator would be used to OUTS required would be those required to set the bit mask and data rotation. This technique could well outperform single-character bit-mapped text drivers such as the one in Listing 26.1by a significant margin. Listing 26.2illustrates one implementation of such an approach. Incidentally, note the use ofthe 8x14 ROM font in Listing 26.2, rather than the 8x8 ROM font used in Listing 26.1.There is also an 8x16 font stored in ROM,along with the tables used to alter the 8x14 and 8x16 ROM fonts into 9x14 and 9x16 fonts.
LISTING26.2126-2.ASM
: Program t oi l l u s t r a t eh i g h - s p e e dt e x t - d r a w i n go p e r a t i o no f : ;
: ; ; ; ;
:
490
w r i t e mode 3 o ft h e VGA. Draws a s t r i n go f8 x 1 4c h a r a c t e r sa ta r b i t r a r yl o c a t i o n s w i t h o u td i s t u r b i n gt h eb a c k g r o u n d ,u s i n g VGA's 8x14 RDM f o n t . D e s i g n e df o ru s ew i t h modes ODh. OEh, OFh, 10h.and12h. Runs o n l y on VGAs ( i n Models 50 & upand I B M D i s p l a yA d a p t e r a n1d0 0 % compatibles). A s s e m b l e dw i t h MASM By M i c h a e A l brash
Chapter 26
s t a cske g m e npta rsat a c k dup(?) 512 db setnadcsk
'STACK'
VGA-VIDEO-SEGMENT SCREEN-WIDTH-IN-BYTES FONT-CHARACTER-SIZE
equ equ equ
OaOOOh 044ah 14
;VGA d i s p l a y memorysegment : o f f s e t o f BIOS v a r i a b l e :I b y t e s i n e a c h f o n t c h a r
equ equ equ equ equ equ
3c4h
:SC i n d e xr e g i s t e r
equ equ
5
: VGA r e g i s t e re q u a t e s . SC-INDEX SC-MAP-MASK GC-INDEX GC-SET-RESET GC-ENABLE-SET-RESET GC-ROTATE GC-MODE GC-BIT-MASK
8
dseg segment para TEST-TEXT-ROW TEST-TEXT-COL TEST-TEXT-COLOR Ofh T b ye tsl eat sb ter li n g 'w H oe rldl odb,! ' . O F od nd t P o i n t e r ends dseg
1
3
common 'DATA' 69 equ equ 17 equ
: S e l e c t6 4 0 x 4 8 0g r a p h i c s
mode.
ax.012h 10h
: t op r e s e r v er e s e r v e db i t s .
in and or dx.al out mov
;row t o d i s p l a y t e s t t e x t a t :column t o d i s p l a y t e s t t e x t a t : h i g hi n t e n s i t yw h i t e
'CODE'
: S e tt h es c r e e nt oa l lb l u e ,u s i n gt h er e a d a b i l i t yo f mov mov dx.al out dx inc in .OfOh a1and or dx.al out dx dec mov dx,al out dx inc
;SC map mask r e g i s t e r i n d e x :GC i n d e x r e g i s t e r :GC s e t / r e s e tr e g i s t e ri n d e x :GC e n a b l es e t / r e s e tr e g i s t e ri n d e x ;GC d a t ar o t a t e / l o g i c a lf u n c t i o n ; r e g i s t e ri n d e x :GC Mode r e g i s t e r :GC b i t mask r e g i s t e r i n d e x
:teststringtoprint. :fontoffset
?
c s es ge g m epnat pr au b l i c assume cs:cseg, ds:dseg n pesratoracr t mov ax.dseg mov ds.ax
mov int
2 3ceh 0
VGA r e g i s t e r s
d x , GC-I NDEX a1 .GC-SETLRESET
a1 ,d x a1 .1
orteshseeope:trnlb.tasllynu e
a1 .GC-ENABLE-SET-RESET
a1 . d x a1 .OfOh a1p: el. aO nsaneaflehtbl/sflroeers e t dx.VGA-VIDEO-SEGMENT
VGA Write Mode 3
491
mov mov mov mov
es,dx d i ,O 8000h cx, ax.0ffffh
:pointtodisplay
memory
;fill a l l 32kwords : b e c a u s eo fs e t / r e s e t .t h ev a l u e : w r i t t e na c t u a l l yd o e s n ' tm a t t e r ;fill w i t hb l u e
r e ps t o s w ; S e td r i v e rt ou s et h e8 x 1 4f o n t .
mov mov mov int S e l ec ac tl lF o n t
ah.llh a1 .30h bh.2 10h
:VGA B I O S c h a r a c t e rg e n e r a t o rf u n c t i o n . ; r e t u r ni n f os u b f u n c t i o n
; g e t8 x 1 4f o n tp o i n t e r
; P r i n tt h et e s ts t r i n g .
mov . soif f sTeet s t S t r i n g bx.TEST-TEXT-ROW mov mov cx.TEST-TEXT-COL ah.TEST-TEXT-COLOR mov D r acwasl lt r i n g
: W a i tf o r
mode & end.
a k e y .t h e ns e tt ot e x t
mov ah.1 i n tf o r; w a i2t 1 h mov ax.3 i n t t :er xe ts t o1 r0eh
: E x i tt o
mode
DOS.
mov int endp
Start
a key
ah.4ch 21h
: S u b r o u t i n et od r a w a textstringleft-to-rightin a linear : g r a p h i c s mode (ODh. OEh. OFh. 0 1 0 h 0. 1 2 h w ) i t h8 - d o t - w i d e :
c h a r a c t e r s .B a c k g r o u n da r o u n dt h ep i x e l st h a t
; c h a r a c t e r isp sreserved. ; F o n tu s e ds h o u l db ep o i n t e dt ob yF o n t P o i n t e r .
--
; Input: ; AH c o l o rt od r a ws t r i n gi n ; EX row t o d r a ws t r i n g on ; CX column t o s t a r t s t r i n g
- s t r i n gt o
:
DS:SI
;
Forces ALU f u n c t i o nt o F o r c e sw r i t e mode 3.
;
n e aDr rparw o cs t r i n g ax push bx push cx push dx push push push bp push ds push
492
Chapter 26
si di
at
draw "move".
make up t h e
: S e tu ps e t / r e s e tt op r o d u c ec h a r a c t e rc o l o r ,u s i n gt h er e a d a b i l i t y
: o f VGA r e g i s t e r t o p r e s e r v e t h e s e t t i n g o f r e s e r v e d b i t s dx.GC-INDEX mov mov dx,al out dx inc in .OfOh a1and ah.0fh and or dx,al out
7-4.
a1 .GC-SETLRESET
a1 .dx
a1 ,ah
: S e l e c tw r i t e
mode 3 . u s i n g t h e r e a d a b i l i t y o f VGA r e g i s t e r s : t ol e a v eb i t so t h e rt h a nt h ew r i t e mode b i t s unchanged. dx.GC-INDEX mov mov a 1 ,GC-MODE dx.al out dx inc in a1 .dx or a1 . 3 dx.al out dx.VGA-VIDEO-SEGMENT mov dmov i s p l at yo: p oesi n. dt x
memory
: C a l c u l a t es c r e e na d d r e s so fb y t ec h a r a c t e rs t a r t si n . to ;point ds push dx.dx sub mov mov
BIOS segment data ,dxds
di,ds:[SCREEN-WIDTH-IN-BYTES]
POP ds mov bx ax, mu1 r: oc w aosdl cfti auoorlf atf st e t ws ciapush drstehied; nsee t d i mov c oatlhs:useim de. cetnx d i and . Oc l;lkl beoetncphloyel u imn n- b ya tded r e s s di.l shr di.1 shr :dsdihi. vlrci do el ubmy n add b y t ptde;ooai i,nnadtx
: r e t r i e v e BIOS : s c r e e nw i d t h
:row
: S e tu pt h e
GC r o t a t i o n . I n w r i t e
8 t o make a baydt de r e s s
mode 3 . t h i s i s t h e r o t a t i o n
: o f CPU d a t ab e f o r e i t i s ANDed w i t h t h e B i t Mask r e g i s t e r t o : f o r m t h e b i t m a s k .F o r c et h e ALU f u n c t i o n t o "move".Uses the : r e a d a b i l i t yo f mov mov dx.al out dx inc in and or dx.al out
: Setup
VGA r e g i s t e r st ol e a v er e s e r v e db i t su n c h a n g e d .
dx.GC_INDEX a1 .GC-ROTATE
a1 . d x a1 .OeOh a1 . c l
BH as b i t mask f o r l e f t h a l f ,
B L as r o t a t i o n f o r r i g h t h a l f .
VGA Write Mode 3
493
mov bh.cl shr neg add . c lb sl h l
: : : : : : :
: : :
bx.0ffffh cl c l .8
D r a w a l lc h a r a c t e r s ,l e f tp o r t i o nf i r s t ,t h e nr i g h tp o r t i o ni nt h e s u c c e e d i n gb y t e ,u s i n gt h ed a t ar o t a t i o nt op o s i t i o nt h ec h a r a c t e r a c r o s st h eb y t eb o u n d a r ya n dt h e nu s i n gw r i t e mode 3 t o c o m b i n et h e c h a r a c t e rd a t aw i t ht h eb i t mask t o a l l o w t h e s e t / r e s e t v a l u e ( t h e c h a r a c t e rc o l o r )t h r o u g ho n l yf o rt h ep r o p e rp o r t i o n( w h e r et h e f o n tb i t sf o rt h ec h a r a c t e ra r e 1) o f t h e c h a r a c t e r f o r e a c h b y t e . W h e r e v e rt h ef o n tb i t sf o rt h ec h a r a c t e ra r e 0. t h eb a c k g r o u n d c o l o ri sp r e s e r v e d . Does n o tc h e c kf o rc a s ew h e r ec h a r a c t e ri sb y t e - a l i g n e da n d n or o t a t i o n and o n l y one w r i t e i s r e q u i r e d .
: Draw t h e l e f t p o r t i o n o f e a c h c h a r a c t e r i n t h e s t r i n g . wsi dc rt hebPOP ea nc k: g e t c x push si push di bx push ; S e tt h eb i t
mask f o r t h e l e f t h a l f o f t h e c h a r a c t e r .
mov mov mov dx.ax out LeftHalfLoop: lodsb and
dx.GC-INDEX a1 .GC-BIT-MASK ah.bh
a1 .a1 L e f t H a l f LoopDone
jz
C h acr a lcl t e r u p c hnaerl xoatct: opat eot iriondnti i n c L e f t H aj m l f pL o o p LeftHalfLoopDone: POP bx pop di POP si
: D r a w t h er i g h tp o r t i o no fe a c hc h a r a c t e ri nt h es t r i n g . ; erpciahogdacarohitchirftinaroioccsnst es r
: b y t eb o u n d a r y
: Setthebit
mask f o r t h e r i g h t h a l f o f t h e c h a r a c t e r .
mov mov mov dx.ax out RightHalfLoop: 1 odsb .a1a1and
jz
C h acr a lcl t e r u p c hnaerlxoattc: opat eot ir ondnti i n c R i g h t Hj ma pl f L o o p
494
Chapter 26
dx.GC-INDEX a1 .GC-BIT-MASK ah.bl
RightHalfLoopDone
RightHalfLoopDone: POP POP
pop pop POP POP POP
POP ret eD n dr apw s t r i n g
ds bp di si dx cx bx ax
: Draw a c h a r a c t e r .
-- -
: Input: : AL character
: CX :
ES:DI
s c r e e nw i d t h a d d r e s st od r a wc h a r a c t e ra t
Cnheaarprarcotce r u p cx push push push ds push
si di
: S e t DS:SI t o p o i n t t o f o n t a n d
ES t o p o i n t t o d i s p l a y
memory.
, [ F so inI dt Ps ofion; nptt eto ri ]n t
: C a l c u l a t ef o n ta d d r e mov mu1 add bp.FDN mov cx dec CharacterLoop: 1 odsb mov stosb
s o fc h a r a c t e r .
. 1 4b l bl . a sx i
; 1 4b y t e sp e rc h a r a c t e r :offsetinfont
-CHARACTER-SIZE : -1 b e c a u s eo n eb y t ep e rc h a r ; g e tc h a r a c t e rb y t e ah.es:[di]
:1 oad 1 a t c h e s : w r i t ec h a r a c t e rb y t e
: Pointtonextlineofcharacterindisplay add
d i ,c x
dec jnz
bP CharacterLoop
POP POP POP POP ret Characterup
memory.
dS
di si cx endp
: S e tt h ep o i n t e rt ot h ef o n tt od r a wf r o mt o n e aSrepl reocct F o n t mov
segment o f c h a r a c t e r
ES:BP.
word[ F po tnr t P o i n t e r: ]sp.aobvipnet e r
VGA Write Mode 3
495
mov
word [ pF tor n t P o i n t e r + E ] . e s
ret eSn ed lpe c t F o n t cseg
ends
end
start
In this chapter, I’ve tried to give you a feel for how write mode 3 works and what it might be used for, rather thanproviding polished, optimized, plug-it-in-and-gocode. Like the rest of the VGAs writepath, write mode 3 is a resource thatcan be used in a remarkablevariety of ways,and I don’twant to lockyou into thinkingof it as useful in just one context. Instead, you should take the time to thoroughly understand what write mode 3 does, and then, when you do VGA programming, think about how write mode 3 can best be applied to the task at hand. Because I focused on illustrating the operationof write mode 3, neither listing in this chapter is the fastest way to accomplish the desired result. For example, Listing 26.2 could be made nearly twice asfast by simply having the CPU rotate, mask, and join the bytes from adjacent characters, then draw the combined bytes to display memoryin a single operation. Similarly, Listing 26.1is designed to illustrate write mode 3 and its interaction with the rest of the VGA as a contrastto Listing 25.1in Chapter 25, rather than formaximum speed, and it couldbe made considerably more efficient. If we were going for performance, we’d have the CPU not only rotate the bytes into position, but also do the masking by ANDingin software. Even more significantly,we would havethe CPU combine adjacent characters into complete, rotated bytes whenever possible, so that only one drawing operation would be required per byte of display memory modified. By doing this, we would eliminate all per-character OUTS,and would minimize display memory accesses,approximately doubling text-drawing speed. As a final note, consider that non-transparent text could also be acceleratedwith write mode 3. The latches could be filled with the background (text box)color, set/reset could be set the to foreground (text) color, and write mode 3 could then be used toturn monochrome text bytes written by the CPU into characters on the screen with just one write per byte. There arecomplications, such as drawing partial bytes, and rotating the bytes to align the characters, which we’ll revisitlater on in Chapter55, while we’re working through the details of the X-Sharp library. Nonetheless, the performance benefit of this approach can be a speedup of as much as four times-all thanks to the decidedly quirky but surprisingly powerful and flexible write mode 3.
A Note on Preserving Register Bits If you take a quick look, you’ll see that the code in Listing 26.1 uses the readable register feature of the VGA to preserve reserved bits and bits other thanthose being modified. Older adapterssuch as the CGA and EGA had few readable registers, so it was necessaryto set all bits ainregister wheneverthat register was modified. Happily, all
496
Chapter 26
Previous
Home
Next
VGA registers are readable, which makes it possible to change only those bits of immediate interest, and, in general, I highly recommend doing exactly that, since IBM (or clone manufacturers) may well someday use some of those reserved bits or change the meanings of some of the bits that are currently in use.
VGA Write Mode 3
497