VGA Data Flow: Barrel Shifter & Bit Mask
VGA Data Flow: Barrel Shifter & Bit Mask
chapter 25
*&4‘#
hiker, Bit Mask, and
Set/Reset
In the last chapter, amined a simplified model of data flow within the GC por-
tion of the VGA, .latches and ALUs. Now we’re ready to expand that
modelto
include ifter, bit mask, and
the
set/reset capabilities, leaving
only the write mo lored over thenext few chapters.
tation
expanded model of GC data flow, featuring the barrel shifter
and bit mask circui Let’s look at the barrel shifter first. A barrel shifter is circuitry
capable of shifting-ok rotating, in the VGAs case-data an arbitrary number of bits
in a single operation, as opposed to being able to shift only one bit position at a time.
The barrel shifter in theVGA can rotate incoming CPU data up to seven bits to the
right (toward the least significant bit), with bit 0 wrapping back to bit 7, after which
the VGA continues processing the rotatedbytejust as it normally processes unrotated
CPU data. Thanks to the nature of barrel shifters, this rotation requires no extra
processing time over unrotated VGA operations. The number of bits by which CPU
data is shifted is controlled by bits 2-0 of GC register 3, the Data Rotate register,
which also contains the ALU function select bits (data unmodified, AND, OR, and
XOR) that we looked at in thelast chapter.
463
Data
Data flow
flow through
through the
the Graphics
Graphics Controller:
Figure 25.1
Figure 25.1
The barrelshifter is powerful, but (as sometimes happens in this business) it sounds
more useful than it really is. This is because the GC can only rotate CPU data, atask
that the CPU itself is perfectly capable of performing. Two OUTs are needed to
select a given rotation: one to set the GC Index register, and one to set the Data
Rotate register. However, with careful programmingit’s sometimes possible to leave
the GC Index always pointing to the Data Rotate register, so only one OUT is needed.
Even so, it’s often easier and/or faster to simply have the CPU rotate the data of
interest CL times than to set the Data Rotate register. (Bear in mind that a single
OUT takes from 11 to 31 cycles on a 486-and longer if the VGA is sluggish at re-
sponding to OUTS, as many VGAs are.) If only the VGA could rotate latched data,
then therewould be all sorts of useful applications for rotation, but, sadly, only CPU
data canbe rotated.
The drawing ofbit-mapped textis one use for thebarrel shifter, and I’ll demonstrate that
application below. In general, though, don’tknock yourself out trylng to figure out
how to workdata rotation into your programs-itjust isn’t allthat useful in most cases.
464 Chapter 25
Bit mask operation.
Figure 25.2
they’re generally referred to in the singular, as“the bit mask.” Figure 25.2 illustrates
the operation of one bit of the bit mask for one plane. This circuitry occurs eight in times
the bit maskfor a given plane, once for each bit of the byte written to display memory.
Briefly, the bit mask determines on a bit-by-bit basiswhether thesource for each byte
written to display memory isthe ALU for that plane or the latch for that plane.
The bit mask iscontrolled by GC register 8, the Bit Maskregister. If a given bit of the
Bit Maskregister is 1, then the correspondingbit of data from theALUs iswritten to
display memory for all four planes, while if that bit is 0, then the correspondingbit
of data from thelatches for the fourplanes is written to display memory unchanged.
(In write mode 3, the actual bit mask that’sapplied to data written to display memory
is the logical AND of the contentsof the Bit Maskregister and the data written by the
CPU, as we’ll seein Chapter 26.)
The most common use of the bit mask is to allow updating of selected bits within a
display memory byte. This works as follows: The display memory byte of interest is
latched; thebit mask is set to preserve all but thebit or bits to be changed; theCPU
writes to display memory, with the bit mask preserving the indicated latchedbits and
allowing ALU data through to change the other bits. Remember, though, that it is
not possible to alterselected bits in a display memory bytedirectly; the byte must first
be latched by a CPU read, and then the bit mask can keep selected bits ofthe latched
byte unchanged.
Listing 25.1 showsa program that uses the bit mask data rotation capabilities of the
GC to draw bitmapped text at any screen location. The BIOS only draws characters
: VGA r e g i s t e re q u a t e s .
Teststring lbaybteel
db 'Helw l oo, r l d ! ':.tOesst rt pi tnroign t .
d dF o n t P o i n t e r ? offset :font
ends
dseg
c s es ge g m epnat pr au b l i c 'CODE'
assume cs:cseg, ds:dseg
npsertaoarcr 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 mode.
mov ax.012h
int 10h
: S e td r i v e r t o u s et h e8 x 8f o n t .
466 Chapter 25
mov [Link] :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 ,
mov a1 .30h : r e t u r ni n f os u b f u n c t i o n
p o i nf ot mov
enrt 8 x 8 : g e bt h , 3
int 10h
cSea ecl llt F o n t
: P r i n tt h et e s ts t r i n g .
mov . so if f sTeet s t S t r i n g
mov bx.TEST_TEXT_ROW
mov cx.TEST_TEXT_COL
StringOutLoop:
1 odsb
and a1 .a1
jz StringOutDone
call DrawChar
add cx.TEST_TEXT_WIDTH
S t r i n g jOmupt L o o p
StringOutDone:
: R e s e tt h ed a t ar o t a t ea n db i t mask r e g i s t e r s .
SETGC
GC-ROTATE. 0
SETGC GC_EJT_MASK, O f f h
: W a i tf o r a keystroke.
mov ah.1
int 21h
: R e t u r nt ot e x t mode
mov ax,03h
int 10h
: E x i t t o DOS.
mov ah.4ch
int 21h
Setnadr tp
: 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 mode
: (ODh, OEh. OFh. 0 1 0 h0.1 2 h ) .
: 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
: EX
-
c h a r a c t e rt od r a w
-
r o w t o d r a wt e x tc h a r a c t e ra t
: C X - column t od r a wt e x tc h a r a c t e ra t
: F o r c e s ALU f u n c t i o n t o "move".
DrawChar n e pa r o c
push ax
push bx
push cx
push dx
push si
push di
push bP
push ds
: 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 .
: 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 .
[Link] sub
bx.1 shl ;assumes8 b y t e sp e rc h a r a c t e r :u s e
bx.1 shl : a m u l t i p l yo t h e r w i s e
bx.1 shl : o f f s e ti nf o n to fc h a r a c t e r
add .: bos xif f soisnenetg mceohnfat r a c t e r
: S e tu pt h e GC r o t a t i o n .
: Setup BH as b i t mask f o r l e f t h a l f ,
: EL as r o t a t i o n f o r r i g h t h a l f .
mov bx.0ffffh
[Link] shr
cl neg
c1.8 add
, c bl sl h l
: 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 gt h eb i t mask t o g e t t h e
: p r o p e rp o r t i o no ft h ec h a r a c t e ri n t oe a c hb y t e .
; 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 .
mov [Link]-CHARACTER-SIZE
mov d x , GC-INDEX
POP
swcbirdaet;cehgkne t c x
cx dec
cx
dec ; - 2 because do tbwyeo
ft aoec crhs ha r
468 Chapter 25
CharacterLoop:
: 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 a1 .GC..BIT-MASK
mov [Link]
dx,ax out
mov a1 , [ s i ] ; g e tc h a r a c t e rb y t e
mov [Link]:[dil ; l o a dl a t c h e s
stosb ; w r i t ec h a r a c t e rb y t e
; S e tt h eb i t 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 a1 .GC~LBIT_MASK
mov [Link]
[Link] out
: G e tt h ec h a r a c t e rb y t ea g a i n & w r i t e i t t o d i s p l a y memory.
: ( R i g h th a l fo fc h a r a c t e r . )
1 odsb ; g e tc h a r a c t e rb y t e
mov [Link]:[dil : l o a dl a t c h e s
stosb : 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 r i n d i s p l a y memory.
add . c xd i
bp dec
C h a r a cj nt ez r L o o p
POP ds
POP bp
pop di
pop si
POP dx
POP cx
POP bx
POP ax
ret
endp 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 ES:BP.
enceptS
arForeocln t
mov [ Fw
po tnor tr P
d o i n t e r: ps] .aobvipnet e r
mov w[ poFtrordn t P o i n t e r + Z ] . e s
ret
eSnedlpe c t F o n t
ends
cseg
startend
The bit mask can be used for much more than bit-aligned fonts. For example, the bit
mask is useful for fast pixel drawing, such as that performedwhen drawing lines, as
The example code in Listing 25.1 is designed to illustrate the use of the Data Rotate
and Bit Mask registers, and is not as fast or as complete as it might be. The case
where text is byte-aligned could be detected and performed much faster, without the
use of the Bit Maskor Data Rotate registers and with onlyone display memory access
per fontbyte (to write the font byte), rather than fourread (to display memory and
write the font byte to each of the two bytes the character spans). Likewise, non-
aligned text drawing could be streamlined to one display memory access per byte by
having the CPU rotate and combine the fontdata directly, rather than setting up the
VGA‘s hardware to do it. (Listing 25.1 was designed to illustrate VGA data rotation
and bit masking rather than thefastest way to draw text. We’ll see faster text-drawing
code soon.) Oneexcellent rule of thumb is to minimize display memory accesses of
all types, especiallyreads, which tend to be considerably slower than writes. Also, in
470 Chapter 25
Listing 25.1 it would be faster to use a table lookup to calculate the bit masks for the
two halves of each character rather than theshifts used in the example.
For another (and morecomplex) example of drawing bit-mapped text on theVGA,
seeJohn Cockerham’s article, “Pixel AlignmentEGA of Fonts,”PC TechJournaZ,January,
198’7. Parenthetically, I’d like to pass along John’s comment about the VGA “When
programming theVGA, everything is complex.”
He’s got a point there.
; EGA r e g i s t e re q u a t e s .
; Macro t o s e t i n d e x e d r e g i s t e r I N D E X o f SC c h i p t o SETTING.
c s es eg g m epnatpr au b l i c 'CODE'
assume cs:cseg
472 Chapter 25
n pesratoar cr t
: S e l e c t6 4 0 x 4 8 0g r a p h i c s mode.
mov ax.012h
int 10h
mov [Link]-VIDEO-SEGMENT
video mov
t o ; p o i nets . a x memory
: D r a w2 41 0 - s c a n - l i n eh i g hh o r i z o n t a lb a r si ng r e e n ,1 0s c a nl i n e sa p a r t .
SETSC
SC-MAP_MASK.OLh :map mask s e t et innagbolnelsy
: p l a n e 1. t h eg r e e np l a n e
b: [Link] inbodi fen og memory
mov a1 . O f f h
mov bp.24 d r a:#
wt ob a r s
HorzBarLoop:
mov cx.80*10 ; I bh p
yotereirzs obnat ar l
bar ; d r a ws t o s b r e p
add d i .80*10 : p osit tnonaoterbf xt at r
bp dec
H o r z B janrzL o o p
: F i l ls c r e e nw i t hb l u e ,u s i n g Map Mask r e g i s t e r t o e n a b l e w r i t e s
: t ob l u ep l a n eo n l y .
[Link]
SETSC :map mask s e tet ni nagb l e s on1 y
: p l a n e 0. t h eb l u ep l a n e
, d idsiu b
mov 8 0 * 4c8x0, s c:#
r epeebnry t e s
mov a1 . O f f h
r e ps t o s b ; p e r f o r m fill ( a f f e c t so n l y
: p l a n e 0. t h eb l u ep l a n e )
: W a i tf o r a keystroke.
mov ah.1
in t 21h
: R e s t o r et e x t mode.
mov ax.03h
in t 10h
: E x i t t o 00s.
mov ah.4ch
in t 21h
start endp
cseg ends
end start
[Link]
; P r o g r a mt oi l l u s t r a t eo p e r a t i o n o f s e t / r e s e tc i r c u i t r yt of o r c e
; s e t t i n g o f memory t h a ta l r e a d yc o n t a i n sd a t a .
; By M i c h a e A
l brash.
; EGA r e g i s t e re q u a t e s .
; Macro t o s e t i n d e x e d r e g i s t e r I N D E X o f SC c h i p t o SETTING.
474 Chapter 25
dx inc
mov ,SETTING
a1
[Link] out
dx dec
endm
; Macro t o s e t i n d e x e d r e g i s t e r I N D E X o f GC c h i p t o SETTING.
c s es ge g m epnat pr au b l i c 'CODE'
assume cs:cseg
n pesratoar cr t
; S e l e c t6 4 0 x 4 8 0g r a p h i c s mode.
mov ax.012h
int 10h
mov [Link]-VIDEO-SEGMENT
v i d e o mov
t o ; p o i ne ts . a x memory
; D r a w2 41 0 - s c a n - l i n eh i g hh o r i z o n t a lb a r si ng r e e n , 10 s c a nl i n e sa p a r t .
SETSC
SC-MAP-MASK.02h ;map mask s e t tei n ag bolnelsy
; p l a n e 1. t h eg r e e np l a n e
b; [Link] efn og memory
mov a1 . O f f h
mov bp.24 draw
; tI o b a r s
HorzBarLoop:
mov cx.80*10 ; # bh p
yotereirzs obnat ar l
bar ;drawstosb rep
add d i .80*10 ; p ostitnnoaoetbrfxtatr
bp dec
H o r z B aj nr Lz o o p
; T u r no f fs e t / r e s e t .
SETGC GC-ENABLELSET-RESET.0
; W a i tf o r a keystroke.
mov ah,l
int 21h
; R e s t o r et e x t mode.
mov ax,03h
int 10h
; Exitto 00s.
mov ah.4ch
int 21h
start endp
cseg ends
end start
[Link]
; Program t oi l l u s t r a t eo p e r a t i o no fs e t / r e s e tc i r c u i t r yi nc o n j u n c t i o n
; w i t h CPU d a t a t o m o d i f y s e t t i n g o f memory t h a ta l r e a d yc o n t a i n sd a t a
; By M i c h a eA
l brash.
; EGA r e g i s t e re q u a t e s .
SC-INDEX 3c4h
equ ;SC i nrdeegxi s t e r
SC-MAP-MASK equ 2 ; S C map mask r e g i s t e r
476 Chapter 25
GC-INDEX equ 3ceh :GC i n d e x r e g i s t e r
GC-SET-RESET equ 0 :GC s e t / r e s e tr e g i s t e r
GC-ENABLELSET-RESET equ 1 ;GC e n a b l e s e t / r e s e t r e g i s t e r
: Macro t o s e t i n d e x e d r e g i s t e r I N D E X o f SC c h i p t o SETTING.
; Macro t o s e t i n d e x e d r e g i s t e r I N D E X o f GC c h i p t o SETTING.
c s es eg g m epnatpr au b l i c 'CODE'
assume cs:cseg
n pesratoracr t
: S e l e c t6 4 0 x 3 5 0g r a p h i c s mode.
mov ax.010h
int 10h
mov ax,EGA-VIDEO-SEGMENT
mov e s .; ap xvoti odn et o memory
: Draw 1 81 0 - s c a n - l i n eh i g hh o r i z o n t a l b a r si ng r e e n ,1 0s c a nl i n e sa p a r t .
SC-MAP_MASK,OEh
SETSC :mapmask s e t t i n ge n a b l e so n l y
: p l a n e 1. t h eg r e e n
plane
. d dis iu b : s t a r ta tb e g i n n i n go fv i d e o memory
mov a1 . O f f h
mov bp.18 ; # b a r st od r a w
HorzBarLoop:
mov 80*10
cx, :# b y t e sp e rh o r i z o n t a lb a r
r e ps t o s b : d r a wb a r
. 8 0d*ai1d0d : p o i n tt os t a r t o f n e x tb a r
bp dec
H o r z B aj nr Lz o o p
: F i l ls c r e e nw i t ha l t e r n a t i n gb a r so f r e da n db r o w n ,u s i n g CPU d a t a
: t os e tp l a n e 1 a n ds e t / r e s e tt os e tp l a n e s 0 . 2 & 3.
; T u r no f fs e t / r e s e t .
SETGC GC-ENABLE-SET-RESET.0
: W a i tf o r a keystroke.
mov ah.1
int 21h
: R e s t o r et e x t mode.
mov ax.03h
int 10h
: E x i t t o DOS.
mov ah.4ch
int 21h
start endp
cseg ends
start end
There is no clearly defined role for the set/resetcircuitry, asthere is for, say, the bit
mask. In many cases,set/reset is largelyinterchangeable with CPU data, particularly
with CPU data written in write mode 2 (write mode 2 operates similarly to the set/
reset circuitry, as we’llsee in Chapter27). The most powerful use of set/reset, in my
experience, is in applications such as the exampleof Listing 25.4,where it is used to
force the value written to certain planes while the CPU data is written to other planes.
In general, though, thinkof set/reset as one moretool you have at your disposal in
getting the VGA to do what you need done,in this case a tool that lets you force all
bits in each plane to either zero or one, orpass CPU data through unchanged, on
each write to display memory. As tools go, set/reset is a handy one, andit’ll pop up
often in this book.
Notes on Set/Reset
The set/reset circuitry is not active in write modes 1 or 2. The Enable Set/Reset
register is inactive in write mode 3, but the Set/Reset register provides the primary
drawing color in write mode 3, as discussed in the next chapter.
478 Chapter 25
Previous Home Next
Be aware that because setheset directly replaces CPU data, it does not necessarily
have to force an entire display memory byte to 0 or OFFH, even when setlreset is
replacing CPU datafor allplanes. For example, ifthe Bit Mask registeris set to 80H,
the setheset circuitry can only modlfi bit 7 of the destination byte in each plane,
since the other seven bits will comefrom the latchesfor each plane. Similarly, the
setheset valuefor each plane can be modifiedby that plane b ALU Once again, this
illustrates that setheset merely replaces the CPU datafor selectedplanes; theset/
reset value is then processed in exactly the same way that CPU data normally is.