home *** CD-ROM | disk | FTP | other *** search
/ Big Blue Disk 11 / bbd11.zip / VIDEO7.TXT < prev    next >
Text File  |  1987-06-19  |  21KB  |  359 lines

  1. |D╔══════════════════╗════════════════════════════════════════════════════════════
  2. |D║ |5The Happy Hacker |D║════════════════════════════════════════════════════════════
  3. |D╚══════════════════╝════════════════════════════════════════════════════════════
  4.  
  5. ^C^1A VIDEO TUTORIAL  --  Part 7
  6. ^Cby Joel Ellis Rea
  7.  
  8. ^CBIOS Video Services
  9.  
  10.    In the past two issues we discussed how to access features of the MDA and 
  11. CGA by directly manipulating both their own ports and the registers on the 
  12. Motorola 6845 CRTC chip.  Such direct access is the fastest way to operate 
  13. these, or any other, video boards.  However, they are not very standard, since 
  14. other systems which have similar operating systems and video display 
  15. capabilities (the PCjr, for example) may not have the same physical hardware in 
  16. the same addresses.  So direct access requires specific or very compatible 
  17. hardware. 
  18.  
  19.    The designers of the IBM PC recognized that this might cause problems, and 
  20. also that a programmer who simply wanted to initialize an 80-column by 25 row 
  21. text display might not want to have to set a bunch of CRTC registers and video 
  22. adapter ports manually.  For such video accesses, and many other things such as 
  23. low-level floppy disk control, printer access, etc., the designers provided a 
  24. special program called the Basic Input/Output Services program, or BIOS, which 
  25. resides in a special ROM chip on the motherboard of every IBM computer or 
  26. compatible.  The IBM BIOS is copyrighted by IBM, but "clone" makers either 
  27. write their own or contract a company such as Phoenix to develop one for them 
  28. that is compatible with, without infringing on, the copyrighted IBM BIOS.  The 
  29. measure of compatibility of a "clone's" BIOS is a major part of determining how 
  30. compatible the machine as a whole is. 
  31.  
  32.    First, some general information:  The 8086 CPU and its sisters 8088, 80186, 
  33. 80188, 80286, 80386 and 80486, all have certain registers (not to be confused 
  34. with the CRTC registers) and instructions.  The instruction of most concern to 
  35. us is the "software INTerrupt request" instruction, or "INT" for short.  It is 
  36. followed by a number from 00h (0) to 0FFh (255), thus allowing for 256 
  37. different interrupt requests.  The registers are as follows: 
  38.  
  39.   ┌ 8 bits ┬ 8 bits ┐                    ┌─── 16  bits ───┐
  40.   ╔════════╤════════╗                    ╔════════════════╗
  41.   ║  AH  -A│X-  AL  ║  Accumulator       ║       SP       ║  Stack Pointer
  42.   ╠════════╪════════╣                    ╠════════════════╣
  43.   ║  BH  -B│X-  BL  ║  Base              ║       BP       ║  Base Pointer
  44.   ╠════════╪════════╣                    ╠════════════════╣
  45.   ║  CH  -C│X-  CL  ║  Count             ║       SI       ║  Source Index
  46.   ╠════════╪════════╣                    ╠════════════════╣
  47.   ║  DH  -D│X-  DL  ║  Data              ║       DI       ║  Destination Index
  48.   ╚════════╧════════╝                    ╚════════════════╝
  49.   └─── -16 bits- ───┘ ┌─── 16  bits ───┐ └─── 16  bits ───┘
  50.                       ╔════════════════╗                    
  51.         Code Segment  ║       CS       ║                    
  52.                       ╠════════════════╣                    
  53.         Data Segment  ║       DS       ║                    
  54.                       ╠════════════════╣                    
  55.        Stack Segment  ║       SS       ║  
  56.                       ╠════════════════╣               
  57.        Extra Segment  ║       ES       ║               
  58.                       ╚════════════════╝
  59.  
  60.    Note that the four 16-bit registers AX, BX, CX and DX each consist of two 8-
  61. bit registers, xH and xL, where "x" is the first letter of the 16-bit register 
  62. name.  AX consists of AH and AL, for example.  The "H" means "High Byte" and 
  63. the "L" means "Low Byte".  So, the full name of the register "AH" is 
  64. "Accumulator Register High Byte", while "CL" is "Count Register Low Byte", etc.  
  65. We will use the abbreviations for the remainder of this article. 
  66.  
  67.    The INT instruction, as I said before, simulates an interrupt to the 
  68. processor.  It can also be thought of as a short subroutine call.  Unlike a 
  69. true subroutine call, the program does not need to know the address of the 
  70. subroutine in memory.  The INT instruction gets this information from a special 
  71. table of addresses in low memory.  This means that a compatible BIOS need not 
  72. have the same routines in the same memory as IBM's BIOS.  This is fortunate, 
  73. since it would be most difficult to make a compatible BIOS with the exact same 
  74. entry point addresses for all routines without violating IBM's copyright!  It 
  75. also means that BIOS ROMs can be updated easily without sacrificing 
  76. compatibility. 
  77.  
  78.    Each INT number is assigned a specific device or service to perform.  INT 
  79. 10h (16 decimal) is reserved for video purposes.  The registers must be loaded 
  80. prior to performing the INT 10h, according to the desired results.  Register AH 
  81. is the most important, since it tells the Video BIOS routines which video 
  82. related job to perform.  So to call the Video BIOS, one must load the AH 
  83. register with the number of the service to be performed, load any other 
  84. registers as needed, then do an INT 10h.  Upon return, certain registers may 
  85. hold the results of the call (if any).  Here is a list of the currently 
  86. implemented BIOS calls which are compatible with the MDA and CGA boards: 
  87.  
  88.    AH   Function
  89.    ──   ────────
  90.  
  91.    00h  Set video mode
  92.    01h  Set cursor type
  93.    02h  Set cursor position
  94.    03h  Read cursor position
  95.    04h  Read light pen position
  96.    05h  Select display page
  97.    06h  Initialize window or scroll window contents up
  98.    07h  Initialize window or scroll window contents down
  99.    08h  Read attribute and character at cursor
  100.    09h  Write attribute and character at cursor
  101.    0Ah  Write character only at cursor
  102.    0Bh  Set color palette
  103.    0Ch  Write graphics pixel (plot a dot)
  104.    0Dh  Read graphics pixel
  105.    0Eh  Write text in "teletype mode"
  106.    0Fh  Get current video mode
  107.    10h  Set palette registers (PCjr and EGA only)
  108.    11h  Reserved  (EGA:  Character generator functions) 
  109.    12h  Reserved  (EGA:  Alternate function select) 
  110.    13h  Write string (PC/AT and compatibles only)
  111.  
  112.    There are others specifically implemented for the EGA, and others for 
  113. Hercules boards and the like.  These will be discussed in a later installment.  
  114. For now, we will discuss Functions (AH) 00h through 0Fh: 
  115.  
  116.    Function 00h ("Set video mode") does the following:  It gets the desired 
  117. mode number from the AL register, then determines which board (if multiple 
  118. boards are installed in the system) to use.  It then blanks the appropriate 
  119. display temporarily by clearing the "Video" bit (bit 3) in the Control port 
  120. (03x8h) of the appropriate board (see last issue).  It then looks up in a table 
  121. within the ROM BIOS for a list of the appropriate CRTC mode values (see the 
  122. issue previous to the last one) and copies these to the 6845 CRTC chip by using 
  123. the "CRTC_ Index" (03x4h) and "CRTC_Data" (03x5h) ports.  It then determines 
  124. the new values for the "Control" and "Color/Palette" (color boards only) ports 
  125. and loads them in, re-enabling the video in the process by setting the "Video" 
  126. bit in the "Control" port.  Finally, it clears the appro- priate area of the 
  127. video display buffer memory if the high bit was set in the AL (display mode) 
  128. register.  The standard modes for CGA, MDA, PCjr (VGA) and EGA boards are as 
  129. follows: 
  130.  
  131.    AL   Video mode                                      Boards supported
  132.    ──   ───── ────                                      ────── ─────────
  133.  
  134.    00h  40x25 text, color burst off                     CGA, PCjr, EGA
  135.    01h  40x25 text, color burst on                      CGA, PCjr, EGA
  136.    02h  80x25 text, color burst off                     CGA, PCjr, EGA
  137.    03h  80x25 text, color burst on                      CGA, PCjr, EGA
  138.    04h  320x200 4-color graphics, color burst on        CGA, PCjr, EGA
  139.    05h  320x200 4-color graphics, color burst off       CGA, PCjr (EGA = 04h)
  140.    06h  640x200 2-color graphics, color burst off       CGA, PCjr, EGA
  141.    07h  80x25 monochrome text                           MDA
  142.    08h  160x200 16-color graphics                       PCjr (16k video RAM)
  143.    09h  320x200 16-color graphics                       PCjr (32k video RAM)
  144.    0Ah  640x200 4-color graphics                        PCjr (32k video RAM)
  145.    0Dh  320x200 16-color graphics                       EGA w/CGA or EGA monitor
  146.    0Eh  640x200 16-color graphics                       EGA w/CGA or EGA monitor
  147.    0Fh  640x350 4-attribute monochrome graphics         EGA w/MDA monitor
  148.    10h  640x350 4-color graphics                        EGA w/EGA monitor, 64K
  149.    10h  640x350 16-color graphics                       EGA w/EGA monitor, 128K+
  150.  
  151.    Function 01 ("Set cursor type") sets the starting and ending lines for the 
  152. blinking hardware cursor in text modes.  The default values for the cursor 
  153. which are set by Function 00h (see above) are 6 and 7 for EGA, or 11 and 12 for 
  154. MDA.  This is done by setting the 6845 CRTC registers "Cursor_Start" and 
  155. "Cursor_End". Register CH's low 4 bits hold the starting scan line value, and 
  156. register CL's low 4 bits hold the ending scan line value; both of which can 
  157. range from 0-15.  Setting the starting value to 15 will usually make the cursor 
  158. disappear. 
  159.  
  160.    Function 02h ("Set cursor position") moves the cursor to a specified 
  161. position on the text display, using text rows and columns as the coordinates.  
  162. This is far easier than using the CRTC "Cursor_High" and "Cursor_Low" 
  163. registers, since those registers point to the cursor's absolute memory location 
  164. within the display buffer.  The BIOS call automatically converts the specified 
  165. screen coordinates to the proper memory location, then loads the CRTC Cursor 
  166. registers appropriately.  Register BH contains the video page number (usually 
  167. 0).  Register DH contains the text row (0-24), while DL contains the text 
  168. column (0-39 or 0-79).  A more reliable way to hide the cursor than using 
  169. Function 01h (see above) is to use Function 02h to move the cursor off the 
  170. screen, say to Row 25. 
  171.  
  172.    Function 03h ("Read cursor position") does the reverse of Function 02h.  It 
  173. reads the CRTC "Cursor_High" and "Cursor_Low" registers, takes that absolute 
  174. address and converts it to a text row and column, and loads the DH and DL 
  175. registers with those coordinates.  Before the call, Register BH must be set to 
  176. the video page number (usually 0).  After the call, register DH will contain 
  177. the text row (0-24) while DL will contain the text column (0-39 or 0-79). 
  178.  
  179.    Function 04h ("Read light pen position") will be discussed at a future time, 
  180. if enough readers express interest in how to program a light pen. 
  181.  
  182.    Function 05h ("Select display page") selects which page to display in a 
  183. video board/mode combination that supports multiple pages.  For the CGA, it 
  184. does this by setting the CRTC "Start_High" and "Start_Low" registers to a 
  185. multiple of 2K (0800h) for 40-column text modes (8 pages, numbered 0-7), or 4K 
  186. (1000h) for 80-column text modes (4 pages, numbered 0-3).  The EGA supports 8 
  187. pages in the 80-column text modes as well, and also supports up to 8 pages in 
  188. the 320x200 16-color graphics mode, up to 4 in the 640x200 16-color mode, and 
  189. up to 2 in both 640x350 modes (these limits assume 256K of on-board video RAM).  
  190. For the standard PC and PC/AT, register AL holds the desired page number.  On 
  191. the PCjr, AL holds a sub-function code, and register BX either holds or returns 
  192. the desired page number(s).  Note that many functions which write to the 
  193. display allow the specification of a page number.  This means that the page 
  194. being displayed can be separate from the page being written to.  This allows 
  195. for smooth animation or fast screen-changing effects. 
  196.  
  197.    Function 06h ("Initialize window or scroll window contents up") and Function 
  198. 07h ("Initialize window or scroll window contents down") allow the 
  199. specification of a rectangular text area, or "window", which can then be 
  200. cleared or scrolled independently of the rest of the screen.  By setting the 
  201. window to cover the full screen, the entire screen is either scrolled or 
  202. cleared.  This is how normal screen-clearing and text-scrolling operations are 
  203. performed.  Register AL contains the number of lines to scroll, or 0 to clear 
  204. the window.  Register BH contains the attribute to be used for the area left 
  205. blank by the scroll or clear.  Registers CX and DX contain the bounds of the 
  206. window as follows: 
  207.  
  208.    Register     Range(s)        Meaning
  209.    ────────     ────────        ───────
  210.  
  211.    CH           (0-24)          Top text row, or upper left corner's Y. 
  212.    CL           (0-39/79)       Left text column, or upper left corner's X.
  213.    DH           (CH-24)         Bottom text row, or lower right corner's Y.
  214.    DL           (DL-39/79)      Right text column, or lower right corner's X.
  215.  
  216.    Of course, these two functions are identical except for the fact that 
  217. Function 06h scrolls up and Function 07h scrolls down.  Either can be used to 
  218. clear the window or screen.  Note that only the currently-active display page 
  219. is affected (see Function 05h above). 
  220.  
  221.    Both functions use some RAM locations to reset the Control and Palette ports 
  222. to legitimate values.  This causes any changes made to them to "vanish" after a 
  223. BIOS scroll or screen clear.  Next issue we will discuss a way to get around 
  224. this. 
  225.  
  226.    Function 08h ("Read attribute and character at cursor") loads Register AX 
  227. with the attribute byte (in AH) and the ASCII character code (in AL) at the 
  228. current cursor position in the text page specified by Register BH. 
  229.  
  230.    Function 09h ("Write attribute and character at cursor") places a desired 
  231. character (ASCII code in AL) with a specified attribute byte or color code (in 
  232. BL) at the current cursor location in the specified video page (in BH).  It 
  233. does this CX times, thus a row of a certain character can be "drawn" with a 
  234. single call.  The cursor is NOT moved by this call, and no characters are 
  235. considered "special" in any way -- not even return, line feed, backspace or 
  236. bell.  In graphics mode, the characters are "drawn" by plotting 8 pixels at a 
  237. time; and if the high bit (bit 7) of the attribute byte (in BL) is set, the 
  238. characters will be drawn in XOR mode, which means that they can overwrite a 
  239. complex graphics background, then simply re-plotting the text will seem to 
  240. erase them, restoring the previous contents of the graphics under the "text"; 
  241. and the bit patterns for characters whose ASCII codes are greater than 7Fh 
  242. (127) can be programmed. 
  243.  
  244.    Function 0Ah ("Write character only at cursor") is identical to Function 09h 
  245. (see above) except that the attribute at that character position remains the 
  246. way it was.  In graphics mode, the BL register's high bit can still be used to 
  247. specify XOR mode.  Other than that, regiester BL's contents are ignored 
  248.  
  249.    Function 0Bh ("Set color palette") on the CGA sets the "CGA_Palette" 
  250. register as follows:  If BH=0, BL contains the 4 bits to load into the low 
  251. nybble of the Palette register, thus setting the border color in text modes, 
  252. the background color in 320x200 graphics modes, and the foreground color in the 
  253. 640x200 graphics mode.  If BH=1, BL contains either a 0 or a 1, which is loaded 
  254. into the "CGA_P_Palette" bit of the "CGA_Palette" register, thus changing the 
  255. current 320x200 color graphics palette.  BL=0 selects the Green-Red-Yellow 
  256. palette, while BL=1 selects the Cyan-Magenta-White palette.  For the PCjr, this 
  257. function can be used to select individual palette colors. 
  258.  
  259.    Function 0Ch ("Write graphics pixel") plots a point on the graphics screen.  
  260. For CGA, location AL must be loaded with the value of the color to plot the dot 
  261. with (0-3 in 320x200 4-color modes, 0-1 in 640x200 2-color mode) -- the high 
  262. bit can be set to do the plot in XOR mode.  Register CX contains the 
  263. horizontal, or X-coordinate of the pixel (0-319 in 320-wide modes, 0-639 in 
  264. 640-wide modes); while Register DX contains the vertical, or Y-coordinate of 
  265. the pixel (0-199 in 200-line modes, 0-349 in 350-line EGA modes). 
  266.  
  267.    Function 0Dh ("Read graphics pixel") is used to determine the color of a 
  268. specified pixel in the graphics modes.  The coordinates must be loaded into 
  269. registers CX and DX the same as in Function 0Ch (see above).  After the call, 
  270. register AL holds the pixel value, or color. 
  271.  
  272.    Function 0Eh ("Write text in 'teletype' mode") is similar to Function 0Ah 
  273. ("Write character only at cursor" -- see above), except that the cursor IS 
  274. moved to the next position, unless the character is a linefeed (cursor is moved 
  275. down one line), carriage return (cursor is moved to the first position on the 
  276. line), backspace (cursor is moved backwards one position), or bell (cursor is 
  277. not moved, but the speaker sounds a tone).  Automatic line-wrapping and screen-
  278. scrolling are also provided by this function.  If the screen is scrolled, this 
  279. function automatically calls Function 06h ("Initialize window or scroll window 
  280. contents up") to scroll the screen, passing as the "fill" attribute the 
  281. attribute of the last character to be written before the scroll.  There is no 
  282. way to specify the attribute of a text character with this function.  If you 
  283. wish to set attributes, you should write each attribute with Function 09H 
  284. ("Write attribute and character at cursor" -- see above) with a space character 
  285. (20h, 32), followed by this Function to write the character with proper cursor 
  286. movements.  This is the BIOS call that MS-DOS uses for its console driver (CON) 
  287. output.  As in Functions 09h and 0Ah, register BH holds the desired text page, 
  288. and register BL holds the color to use in graphics mode text plotting.  The 
  289. repeat factor (CX) is not used here. 
  290.  
  291.    Function 0Fh ("Get current display mode") gets the currently active display 
  292. mode (set with Function 00h -- see above) and the current display page (set 
  293. with Function 05h -- see above).  No registers (except for AH containing the 
  294. Function code 0Fh, of course) need be set prior to the call.  Upon return, AL 
  295. contains the current mode (as per Function 00h) and BH contains the active 
  296. display page. 
  297.  
  298.    Performing INT calls from BASICA or GW-BASIC can be difficult.  It basically 
  299. requires a machine-language routine to be made available.  Turbo Pascal, on the 
  300. other hand, provides a nice programmer's interface to the INT function.  One 
  301. simply defines a RECORD VARiable to hold the registers, then call the "Intr" 
  302. PROCEDURE with the desired INT code and registers variable.  These lines will 
  303. define the TYPE and VARiable that the "Intr" (and "MsDos") PROCEDUREs expect: 
  304.  
  305.  
  306. ^1TYPE
  307.  
  308. ^1  ...
  309. ^1  iAPX = RECORD CASE Boolean OF
  310. ^1    False:  (AX, BX, CX, DX, BP, SI, DI, DS, ES, Flags):  Integer; 
  311. ^1    True:   (AL, AH, BL, BH, CL, CH, DL, DH):             Byte
  312. ^1  END;  { RECORD iAPX }
  313. ^1  ...
  314.  
  315. ^1VAR
  316.  
  317. ^1  ...
  318. ^1  registers:  iAPX
  319. ^1  ...
  320.  
  321.  
  322.    As an example, to scroll a window on the 80x25 text screen consisting of 
  323. rows 5-20 and columns 10-70 4 lines downward, filling the new blank lines with 
  324. a dark red background with a light yellow (but invisible, since there is no 
  325. text) foreground, you could do this: 
  326.  
  327.  
  328. ^1PROCEDURE scroll4down; 
  329.  
  330. ^1  BEGIN { PROCEDURE scroll4down }
  331.  
  332. ^1    WITH registers DO  BEGIN
  333. ^1      CH := 5;   { Top row of window } 
  334. ^1      DH := 20;  { Bottom row of window }
  335. ^1      CL := 10;  { Left column of window }
  336. ^1      DL := 70;  { Right column of window }
  337. ^1      BH := (Red SHR 4) OR Yellow;  { Fill attribute = $4E } 
  338. ^1      AL := 4;   { Lines to scroll }
  339. ^1      AH := $7   { "Initialize window or scroll window contents down" } 
  340. ^1    END;  { WITH registers }
  341.  
  342. ^1    Intr ($10, registers)  { BIOS call, $10 = Video services }
  343.  
  344. ^1  END;  { PROCEDURE scroll4down }
  345.  
  346.  
  347.    On this issue of Big Blue Disk is a demo program which demonstrates the 
  348. various BIOS calls discussed here.  Simply press the [─┘] key to run it from 
  349. here, or you can press [Esc] to get back to the menu, re-select this article, 
  350. and choose "Run it" from the menu. 
  351.  
  352.    Next issue we will discuss how the BIOS keeps track of certain information, 
  353. and how one can obtain very interesting effects by "tricking" the ROM BIOS.   
  354. Two effects in particular will be to enable bright background by sacrificing 
  355. the blink capability, but enable that effect to survive a Function 06h or 07h 
  356. scroll or screen clear; and to trick BIOS into thinking you've switched 
  357. graphics modes when you haven't, thus allowing both 40-column and 80-column 
  358. text on the screen at the same time in 640x200 graphics mode.
  359.