home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / xbase / library / clipper / screen / vga.doc < prev   
Text File  |  1992-04-15  |  22KB  |  533 lines

  1.                  VGA TEXT ATTRIBUTES AND SPECIAL EFFECTS
  2.  
  3.                         Written by Chris Dunford
  4.                           Edited by Jim Fowler
  5.  
  6.  
  7. This document is an attempt to explains how VGA text attributes work,
  8. and how the effects in FXCOLOR are possible.  It also provides tips for
  9. creating your own special effects.
  10.  
  11.  
  12. What's different with the VGA?
  13. ------------------------------
  14. The CGA implemented a direct relationship between a 4-bit attribute and
  15. a color.  Each attribute mapped to one color, and the color could not be
  16. changed.
  17.  
  18. That changed with the EGA (which had palette registers to play with),
  19. and changed again, radically, with the VGA.  The attribute number
  20. specifies the displayed color very indirectly; it is combined with bits
  21. and pieces of 5 different internal VGA registers before it appears on
  22. the screen as one of the 262,144 possible colors.  There are, therefore,
  23. several ways to change a character's color other than by changing its
  24. attribute.  And, of course, if you change the way an attribute is
  25. displayed, all characters with that attribute are affected.  Thus, it is
  26. possible to "design" attributes with interesting effects, and to alter
  27. the way entire screen fields are displayed very efficiently.
  28.  
  29.  
  30. Mapping attributes to colors
  31. ----------------------------
  32. Before we start, let me note that we are working here with 4-bit
  33. attributes.  The 8-bit attribute that's associated with a character is,
  34. of course, two 4-bit attributes: one for the foreground, and one for the
  35. background.  This document discusses how these 4-bit attributes become
  36. colors; everything here applies equally to foreground and background
  37. attributes (thus it is possible to use blinking backgrounds, etc.)
  38.  
  39. Screen colors in text modes are controlled by the VGA's attribute
  40. controller.  In one of its two modes (the one that the PS/2 BIOS sets
  41. by default), here is how a color is selected by an attribute:
  42.  
  43.                  AND
  44.     attribute |-------| color plane enable register
  45.                   |
  46.                   | selects
  47.                   |
  48.          palette register 0-15     color select register
  49.                   |                         |
  50.                   |                         |
  51.                   | bits 0-5       bits 6-7 |
  52.                   +------------+------------+
  53.                                |
  54.                                |      AND
  55.                                +-------+--------video DAC mask register
  56.                                        |
  57.                                        | selects
  58.                                        |
  59.                           video DAC color register 0-255
  60.  
  61. This appears complex, but it can be simplified.  First, notice the two
  62. logical AND operations.  Since both the color plane enable register and
  63. the video DAC mask register normally contain all ones, they tend to drop
  64. out of the picture, resulting in:
  65.  
  66.               attribute 0-15
  67.                   |
  68.                   | selects
  69.                   |
  70.          palette register 0-15     color select register
  71.                   |                         |
  72.                   |                         |
  73.                   | bits 0-5       bits 6-7 |
  74.                   +------------+------------+
  75.                                |
  76.                                | selects
  77.                                |
  78.                  video DAC color register 0-255
  79.  
  80. The 4-bit attribute (0-15) selects one of the 16 palette registers.  The
  81. palette register contains a number.  This number is combined with the
  82. contents of the color select register to form another number.  For
  83. example, suppose:
  84.  
  85.     attribute = 13
  86.     palreg[13] = 1Bh
  87.     CSR = 3
  88.  
  89. Attribute 13 selects palette register 13, which contains 1Bh.  The color
  90. select register contains 3.  We put the contents of the color select
  91. register in bits 6-7 of the final number, and the contents of palreg[13]
  92. in bits 0-5:
  93.  
  94.     3   1Bh
  95.     --  ------
  96.     11  011011
  97.  
  98. The resulting number is 11011011 binary, 219 decimal.  This number
  99. selects one of the 256 video DAC color registers.
  100.  
  101. The color that will be displayed is the color defined by the selected
  102. video DAC color register (DAC stands for Digital-to-Analog Converter).
  103. Each video DAC register is an 18-bit register that contains three 6-bit
  104. values: one for each of the red, green, and blue primary colors.  Each
  105. value specifies the intensity of that primary color on the screen.  A
  106. zero for a primary means that it's not included in the color; 63 is
  107. maximum intensity.
  108.  
  109. Suppose, in our example, that video DAC register 219 contains the
  110. numbers 0, 3Fh, and 3Fh (it's convenient to think of the register as
  111. three 6-bit values rather than one 18-bit value).  This means that red
  112. will be off, and green and blue will be at maximum intensity.  The color
  113. displayed by attribute 13 will be bright cyan.
  114.  
  115. Notice that you could change the color of a character with FG attribute
  116. N in four different ways:
  117.  
  118.     1. Change the attribute
  119.     2. Change the contents of palreg[N]
  120.     3. Change the contents of the video DAC register specified by
  121.        palreg[N] and the color select register
  122.     4. Change the contents of the color select register
  123.  
  124. These all have different effects on the total screen display.  Changing
  125. an attribute affects a single character; changing a palette register
  126. affects all characters with that attribute; changing a video DAC
  127. register affects any character whose attribute, when combined with color
  128. select, selects that register -- this could be several attributes;
  129. changing the color select register could very well affect everything on
  130. the screen.
  131.  
  132.  
  133. Another way to visualize color selection
  134. ----------------------------------------
  135. There is a simpler way to look at the process.  The two bits used from
  136. the color select register always end up in the high two bits of the
  137. video DAC color register number.  Thus, the selected DAC color register
  138. will always be one of:
  139.  
  140.     CSR=0: 00xx xxxx  (register range 0-63)
  141.     CSR=1: 01xx xxxx  (64-127)
  142.     CSR=2: 10xx xxxx  (128-191)
  143.     CSR=3: 11xx xxxx  (192-255)
  144.  
  145. The remaining 6 bits come directly from the selected palette register.
  146.  
  147. If we break the 256 video DAC color registers into four palettes of 64
  148. colors each, as follows:
  149.  
  150.     DAC registers   Palette
  151.          0-63          0
  152.        64-127          1
  153.       128-191          2
  154.       192-255          3
  155.  
  156. then the color select register selects one of the four 64-color
  157. palettes, and the palette register (which is selected by the attribute)
  158. selects one of the 64 colors of the CSR-selected palette.
  159.  
  160. Thus, it's simply stated: the attribute selects one of the 64 colors
  161. available from the palette selected by the color select register.
  162.  
  163.  
  164. Mode 1
  165. ------
  166. I mentioned that the above represents one of the two modes supported by
  167. the attribute controller; call it "mode 0".  The other mode (mode 1) is
  168. very similar; the only difference is in which bits are combined from the
  169. palette register and the color select register.  The simplified diagram
  170. is as follows:
  171.  
  172.               attribute 0-15
  173.                   |
  174.                   | selects
  175.                   |
  176.          palette register 0-15     color select register
  177.                   |                         |
  178.                   |                         |
  179.                   | bits 0-3       bits 4-7 |
  180.                   +------------+------------+
  181.                                |
  182.                                | selects
  183.                                |
  184.                  video DAC color register 0-255
  185.  
  186. The difference is that there are four bits each from the palette
  187. register and the color select register (instead of 6 and 2 under the
  188. mode 0).
  189.  
  190. The effect of mode 1 is that the 256 video DAC color registers are
  191. broken down into sixteen 16-color palettes instead of four 64-color
  192. palettes:
  193.  
  194.     DAC registers   Palette
  195.          0-15          0
  196.         16-31          1
  197.         32-47          2
  198.          ...
  199.       240-255         15
  200.  
  201. The palette register (still selected by the attribute) selects one of
  202. the 16 colors available in the palette selected by the current color
  203. select register.
  204.  
  205. The more complex effects (fading, pulsing, etc.) of FXCOLOR are
  206. generated under mode 1.  This is because all 256 colors defined by the
  207. video DAC color registers can be accessed by simply changing the
  208. contents of the color select register, which is very efficient (it
  209. takes less than 1/100th of a second).
  210.  
  211. Under mode 0, only 75% of the available colors can be accessed by just
  212. changing the color select register (this is because there are 64 colors
  213. per palette, but only 16 attributes).  To get to the remaining colors,
  214. you have to change the palette registers.  But there are 16 palette
  215. registers; if you need to alter more than one displayed color, this is
  216. less efficient than using the color select register.
  217.  
  218.  
  219. A mode 1 technique
  220. ------------------
  221. As mentioned, there are a number of ways to alter displayed colors.  I
  222. will concentrate on one technique: creating a series of palettes and
  223. then switching palettes by systematically changing the contents of the
  224. color select register.  This document concentrates on mode 1; some of
  225. the same effects (such as periodic intensification and simulated
  226. blinking) can be obtained in mode 0.
  227.  
  228. Note that the use of mode 1 largely dictates that you must reinitialize
  229. the palette registers.  The VGA BIOS initializes some of these registers
  230. with values greater than 15; these will be masked in mode 1 to the low
  231. 4 bits, yielding values in the range 0..15.  These values will probably
  232. not result in the colors you intend (two attributes will yield red
  233. foreground, for example).  The simplest solution is to just fill the
  234. palette registers sequentially with numbers from 0..15; this largely
  235. causes the palregs to drop out of the picture:  attribute N will map
  236. directly to color N within the palette selected by the color select
  237. register.
  238.  
  239. Assuming that the palette registers are set as described, create a
  240. "base" palette in palette 0.  That is, fill palette 0 (video DAC color
  241. registers 0-15) with 16 base color definitions.  It makes sense to set
  242. the standard color combinations for the 16 CGA-like colors that people
  243. will expect to see (color intensity values in hex):
  244.  
  245.     DAC regs  Attr  R   G   B   Color
  246.       0-2      0    0   0   0   Black
  247.       3-5      1    0   0  2A   Blue
  248.       6-8      2    0  2A   0   Green
  249.       9-11     3    0  2A  2A   Cyan
  250.       12-14    4   2A   0   0   Red
  251.       15-17    5   2A   0  2A   Magenta
  252.       18-20    6   15  2A   0   Brown
  253.       21-23    7   2A  2A  2A   White
  254.       24-26    8   15  15  15   Grey
  255.       27-29    9    0   0  3F   Bright Blue
  256.       30-32   10    0  3F   0   Bright Green
  257.       33-35   11    0  3F  3F   Bright Cyan
  258.       36-38   12    3F  0   0   Bright Red
  259.       39-41   13    3F  0  3F   Bright Magenta
  260.       42-44   14    3F 3F   0   Yellow
  261.       45-47   15    3F 3F  3F   Bright White
  262.  
  263. (In FXCOLOR, 35h is used in place of 3Fh.  This leaves some room to
  264. pulse even the bright colors by intensification.)
  265.  
  266. The second step is to duplicate palette 0 into palettes 1-15, and then
  267. vary the color definitions for attributes of interest in some systematic
  268. way.  For example, let's specify that attribute 1 is not going to be
  269. "blue"; it's going to be a green that continuously cycles from normal
  270. intensity to high intensity and back.  The effect is similar to blinking
  271. text, but more subtle.  To do this, set the colors for attribute 1 in
  272. each of the palettes as follows (RGB values in hex):
  273.  
  274.     Palette  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
  275.       R      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  276.       G     2A 2C 2E 31 33 35 36 37 38 39 3A 3B 3C 3D 3E 3F
  277.       B      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  278.  
  279. Note that the intensity of the green primary slowly increases from
  280. normal (2Ah) in palette 0 to maximum intensity (3Fh) in palette 15.
  281. Also note that attribute 1 no longer has anything to do with blue --
  282. it's green.
  283.  
  284. If these values are loaded into the video DAC color registers, and you
  285. then sequence through the palettes in a cyclical pattern (0->15->0),
  286. characters with attribute 1 will slowly "pulse" from green to bright
  287. green.  The effect is more subtle - and readable - than simply switching
  288. the characters off and on.
  289.  
  290. The palette change is easily and efficiently accomplished by setting the
  291. color select register -- say, in a timer tick intercept routine.  Using a
  292. timer intercept is an ideal way to accomplish this sort of special
  293. effect.
  294.  
  295. It is both efficient and flexible:  the speed of the effect can be
  296. adjusted by altering the values of either the incremental rate of the
  297. palette changes and/or the interval period of timer ticks before a change
  298. is made.  Increasing the increment value for palette changes causes the
  299. pulse rate to speed up by skipping palettes; increasing the interval
  300. period between timer ticks before making a change slows the pulsing by
  301. skipping clock ticks.
  302.  
  303.  
  304. Effects
  305. -------
  306. Many "special effects" are possible using this technique.
  307.  
  308.  
  309. PERIODIC INTENSIFICATION
  310.  
  311. Pulsing is a gradual change from a low intensity color to a high
  312. intensity color.  The effect can be hardened (resulting in blinking) by
  313. using the normal color for half of the palettes and an intensified
  314. version for the other half.  For example, the following palettes blink
  315. cyan to bright cyan (RGB values in hex):
  316.  
  317.     Palette  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
  318.       R      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  319.       G     2A 2A 2A 2A 2A 2A 2A 2A 3F 3F 3F 3F 3F 3F 3F 3F
  320.       B     2A 2A 2A 2A 2A 2A 2A 2A 3F 3F 3F 3F 3F 3F 3F 3F
  321.  
  322.       [ fx_Blink( "bg", "bg+" ) ]
  323.  
  324. The effect can be varied by altering the ratio of low intensity palettes
  325. to high intensity palettes ("beacons", below, are simply extreme cases
  326. of periodic intensification).
  327.  
  328.  
  329. COLOR CHANGES
  330.  
  331. Another effect is to blink text from one color to another.  This set of
  332. palettes blinks bright green to bright red:
  333.  
  334.     Palette  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
  335.       R      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  336.       G     3F 3F 3F 3F 3F 3F 3F 3F  0  0  0  0  0  0  0  0
  337.       B      0  0  0  0  0  0  0  0 3F 3F 3F 3F 3F 3F 3F 3F
  338.  
  339.       [ fx_Blink( "g+", "r+" ) ]
  340.  
  341.  
  342. GRADED COLOR CHANGES
  343.  
  344. An interesting effect is to "grade" (fade) one color into another.
  345. Rather than simply blinking green to red, change it gradually by fading
  346. out the green primary and fading in the red (RGB values in hex):
  347.  
  348.     Palette  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
  349.       R      0  2  4  6  9 0C 0F 12 15 18 1B 1E 21 24 27 2A
  350.       G     2A 27 24 21 1E 1B 18 15 12 0F 0C  9  6  4  2  0
  351.       B      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  352.  
  353.       [ fx_Fade( "g", "r" ) ]
  354.  
  355. This attribute will still change from green to red, but the change is
  356. not instantaneous; it slowly fades from one color to the other, passing
  357. through various other unnamed colors (all combinations of red and green)
  358. on the way.
  359.  
  360.  
  361. SIMULATED BLINKING
  362.  
  363. Regular hardware blinking can be simulated via a variant of the above
  364. scheme; simply use the background color for half of the palettes (or the
  365. foreground color, if you want to flash the background) (RGB values in
  366. hex):
  367.  
  368.     Palette  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
  369.       R     3F 3F 3F 3F 3F 3F 3F 3F  0  0  0  0  0  0  0  0
  370.       G     3F 3F 3F 3F 3F 3F 3F 3F  0  0  0  0  0  0  0  0
  371.       B      0  0  0  0  0  0  0  0 2A 2A 2A 2A 2A 2A 2A 2A
  372.  
  373.       [ fx_Blink( "gr+", "b" ) ]
  374.  
  375. The example flashes yellow text if displayed on a blue background.
  376.  
  377.  
  378. SOFTENED BLINKING
  379.  
  380. By placing the background attribute in half of the palettes (as for
  381. flashing) and fading out the foreground in the other half, a softer
  382. version of blinking is possible (RGB values in hex):
  383.  
  384.     Palette  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
  385.       R      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  386.       G     2A 24 1E 19 14 0F 0A  5  0  0  0  0  0  0  0  0
  387.       B     2A 24 1E 19 14 0F 0A  5  0  0  0  0  0  0  0  0
  388.  
  389.       [ fx_Blink( "bg", "n", .T. ) ]
  390.  
  391. The example shows softened blinking of cyan text on a black background.
  392. If any of the background primaries are also components of the
  393. foreground color, do not fade those (RGB values in hex):
  394.  
  395.     Palette  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
  396.       R      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  397.       G     2A 24 1E 19 14 0F 0A  5  0  0  0  0  0  0  0  0
  398.       B     2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A
  399.  
  400.       [ fx_Blink( "bg", "b", .T. ) ]
  401.  
  402. The example shows softened blinking of cyan on blue.
  403.  
  404.  
  405. FADING
  406.  
  407. This is an even softer version of blinking text.  It fades the
  408. foreground over the full 16 palettes by grading the foregound color
  409. into the background color (RGB values in hex):
  410.  
  411.     Palette  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
  412.       R      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  413.       G     2A 27 24 21 1E 1B 18 15 12 0F 0C  9  6  4  2  0
  414.       B      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  415.  
  416.       [ fx_Fade( "g", "n" ) ]
  417.  
  418. The example fades a green foreground into a black background.  If the
  419. background is a color other than black, a graded color change from the
  420. FG color to the BG color is used.
  421.  
  422. STROBES AND BEACONS
  423.  
  424. These are two other effects (not in the demo).  A strobe is a color
  425. that's invisible most of the time (BG=FG) but flashes briefly to maximum
  426. intensity.  The following palettes strobe yellow when used with a blue
  427. background (RGB values in hex):
  428.  
  429.     Palette  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
  430.       R      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 3F
  431.       G      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 3F
  432.       B     2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A  0
  433.  
  434. A beacon is a low intensity color that briefly flashes to maximum
  435. intensity.  The following palettes create a blue beacon (RGB values in
  436. hex):
  437.  
  438.     Palette  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
  439.       R      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  440.       G      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  441.       B     20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3F
  442.  
  443.  
  444. IMPORTANT:  Note that only palettes that are a multiple of the palette
  445. incremant rate are ever used.  E.g., if the palette increment rate is 2,
  446. palette 15 will not be used; put the strobe or beacon color in palette
  447. 14 instead.
  448.  
  449.  
  450. Advantages/disadvantages
  451. ------------------------
  452. Creating special effects in this manner has several advantages over
  453. using hardware effects (such as blinking), and some disdvantages.  Among
  454. the advantages are:
  455.  
  456.     - MANY more effects are possible; I have mentioned only a few of the
  457.       possibilities.
  458.     - Transition rates are under your control; the hardware blink rate
  459.       is fixed.
  460.     - Effects can be mixed on one screen (one attribute could be a
  461.       strobe, another a graded color change).
  462.     - Any effect can be applied to background as well as foreground.
  463.  
  464. Among the disadvantages are:
  465.  
  466.     - A background timer routine has some effect on system efficiency
  467.       (but it's small).
  468.     - Effects are associated with attribute numbers, not with bit
  469.       positions.
  470.  
  471. The last one bears some explanation.  Hardware blinking is associated
  472. with a single bit (bit 7) of the FG/BG attribute byte.  Thus, you have
  473. 16 FG colors, all of which can be blinked.  Software-controlled special
  474. effects, however, are associated with attributes.  If you set, say,
  475. attribute 6 to be pulsed green, you no longer have a brown attribute
  476. available.  Thus, the number of effective "base" colors is reduced for
  477. each special effect attribute you create.
  478.  
  479. Mode 0
  480. ------
  481. I have covered attribute control mode 1 in some detail, but some of the
  482. same effects can be accomplished in mode 0.  The primary difference is
  483. that you have only four palettes to work with.  The simpler effects such
  484. as color changes, periodic intensification, and simulated blink can
  485. easily be accomplished in mode 0 by putting the second color in palette
  486. 1, and then alternating between palettes 0 and 1.
  487.  
  488. More complex effects would require periodic alterations to the palette
  489. registers and/or color plane enable register.
  490.  
  491.  
  492. Programming
  493. -----------
  494. The programming required to accomplish these special effects is
  495. relatively straightforward.  The VGA BIOS provides the necessary
  496. services; but register-level programming is used in FXCOLOR to eliminate
  497. distracting "blips" caused when RGB values or palettes are changed.
  498.  
  499.  
  500. The linkable object module
  501. --------------------------
  502. FXCOLOR.OBJ(ASM) provides a linkable object module containing callable
  503. functions for Clipper 5.01 that can be used to generate all of the
  504. special effects described above, and others.
  505.  
  506. Be sure to call fx_Enable() when you start and fx_Disable() when you
  507. exit.
  508.  
  509. The demo program (FXDEMO.PRG) demonstrates use of the commands.
  510.  
  511.  
  512. Author and reference
  513. --------------------
  514. The original document is by:
  515.  
  516.      Chris Dunford
  517.      CompuServe ID# 76703,2002
  518.  
  519. The original document is copyright (C) 1989 by Chris Dunford.  Permission
  520. is granted to distribute freely by electronic or other means, but it may
  521. not be republished without permission.  This document is an edited version
  522. of the original and is presented here with permission.
  523.  
  524.  
  525. Edited by:
  526.  
  527.      Jim Fowler
  528.      CompuServe ID# 73340,3325
  529.  
  530.  
  531. Clipper is a registered trademark of Nantucket Corp.
  532.  
  533.