Specifications.
[pintos-anon] / specs / freevga / llintro.htm
1 <HTML>
2 <HEAD>
3    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
4    <META NAME="Author" CONTENT="Joshua Neal">
5    <META NAME="Description" CONTENT="Pure VGA/SVGA hardware programming (registers, identification, and otherlow-level stuff.)">
6    <META NAME="KeyWords" CONTENT="VGA SVGA hardware video programming">
7    <TITLE>FreeVGA -- Introduction to Low-level Programming</TITLE>
8 </HEAD>
9 <BODY>
10
11 <CENTER><A HREF="home.htm">Home</A> <A HREF="#intro">Intro</A> <A HREF="#know">Know</A>
12 <A HREF="#why">Why</A> <A HREF="#assembly">Assembly</A> <A HREF="#hex">Hex</A>
13 <A HREF="#conventions">Conventions</A> <A HREF="#memory">Memory</A> <A HREF="#access">Accessing</A>
14 <A HREF="home.htm#intro">Back</A>&nbsp;
15 <HR WIDTH="100%"><B>Hardware Level VGA and SVGA Video Programming Information
16 Page</B></CENTER>
17
18 <CENTER>Introduction to Low-level Programming&nbsp;
19 <HR WIDTH="100%"></CENTER>
20
21
22 <P><A NAME="intro"></A><B>Introduction</B>
23 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This section is intended
24 to give one a general background in low-level programming, specifically
25 related to video programming. It assumes that you already know how to program
26 in your intended programming environment, and answers questions such as:
27 <UL>
28 <LI>
29 <A HREF="#know">What do I need to know?</A></LI>
30
31 <LI>
32 <A HREF="#why">Why write hardware-level code?</A></LI>
33
34 <LI>
35 <A HREF="#assembly">Do I need to know assembly language?</A></LI>
36
37 <LI>
38 <A HREF="#hex">What are hex and binary numbers?</A></LI>
39
40 <LI>
41 <A HREF="#conventions">What are the numerical conventions used in this
42 reference?</A></LI>
43
44 <LI>
45 <A HREF="#memory">How do memory and I/O ports work?</A></LI>
46
47 <LI>
48 <A HREF="#access">How do I access these from my programming environment?</A></LI>
49 </UL>
50 <A NAME="know"></A><B>What do I need to know?</B>
51 <BR><B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </B>To program video
52 hardware at the lowest level you should have an understanding of hexadecimal
53 and binary numbers, how the CPU accesses memory and I/O ports, and finally
54 how to perform these operations in your particular programming environment.
55 In addition you need detailed descriptions of the particular graphics chipset
56 you are programming.
57
58 <P><A NAME="why"></A><B>Why write hardware-level code?</B>
59 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; One reason for writing hardware-level
60 code is to develop drivers for operating systems or applications. Another
61 reason is when existing drivers do not provide the required performance
62 or capabilities for your application, such as for programming games or
63 multimedia. Finally, the most important reason is enjoyment. There is a
64 whole programming scene dedicated to producing "demos" of what the VGA/SVGA
65 can do.
66
67 <P><A NAME="assembly"></A><B>Do I need to know assembly language?</B>
68 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; No, but it helps. Assembly
69 language is the basis all CPU operations. All functions of the processor
70 and computer are potentially accessible from assembly. With crafty use
71 of assembly language, one can write software that exceeds greatly the potential
72 of higher level languages. However, any programming environment that provides
73 direct access to I/O and memory will work.
74
75 <P><A NAME="hex"></A><B>What are hex and binary numbers?</B>
76 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Humans use a number system
77 using 10 different digits (0-9), probably because that is the number of
78 fingers we have. Each digit represents part of a number with the rightmost
79 digit being ones, the second to right being tens, then hundreds, thousands
80 and so on. These represent the powers of 10 and is called "base 10" or
81 "decimal."
82 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Computers are digital (and
83 don't usually have fingers) and use two states, either on or off to represent
84 numbers. The off state is represented by 0 and the on state is represented
85 by 1. Each digit (called a bit, short for Binary digIT) here represents
86 a power of 2, such as ones, twos, fours, and doubling each subsequent digit.
87 Thus this number system is called "base 2" or "binary."
88 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Computer researchers realized
89 that binary numbers are unwieldy for humans to deal with; for example,
90 a 32-bit number would be represented in binary as 11101101010110100100010010001101.
91 Converting decimal to binary or vice versa requires multiplication or division,
92 something early computers performed very slowly, and researchers instead
93 created a system where the numbers were broken into groups of 3 (such as
94 11 101 101 010 110 100 100 010 010 001 101) and assigned a number from
95 0-7 based on the triplet's decimal value. This number system is called
96 "base 8" or "octal."
97 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Computers deal with numbers
98 in groups of bits usually a length that is a power of 2, for example, four
99 bits is called a "nibble", eight bits is called a "byte", 16 bits is a
100 "word", and 32 bits is a "double word." These powers of two are not equally
101 divisible by 3, so as you see in the divided example a "double word" is
102 represented by 10 complete octal digits plus two-thirds of an octal digit.
103 It was then realized that by grouping bits into groups of four, a byte
104 could be accurately represented by two digits. Because a group of four
105 bits can represent 16 decimal numbers, they could not be represented by
106 simply using 0-9, so they simply created more digits, or rather re-used
107 the letters A-F (or a-f) to represent 10-15. So for example the rightmost
108 digits of our example binary number is 1101, which translates to 13 decimal
109 or D in this system, which is called "base 16" or "hexadecimal."
110 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Computers nowadays usually
111 speak decimal (multiplication and division is much faster now) but when
112 it comes to low-level and hardware stuff, hexadecimal or binary is usually
113 used instead. Programming environments require you to explicitly specify
114 the base a number is in when overriding the default decimal. In most assembler
115 packages this is accomplished by appending "h" for hex and "b" for binary
116 to end of the number, such as 2A1Eh or 011001b. In addition, if the hex
117 value starts with a letter, you have to add a "0" to the beginning of the
118 number to allow it to distinguish it from a label or identifier, such as
119 0BABEh. In C and many other languages the standard is to append "0x" to
120 the beginning of the hex number and to append "%" to the beginning of a
121 binary number. Consult your programming environment's documentation for
122 how specifically to do this.
123 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Historical Note: Another
124 possible explanation for the popularity of the octal system is that early
125 computers used 12 bit addressing instead of the 16 or 32 bit addressing
126 currently used by modern processors. Four octal digits conveniently covers
127 this range exactly, thus the historical architecture of early computers
128 may have been the cause for octal's popularity.
129
130 <P><A NAME="conventions"></A><B>What are the numerical conventions used
131 in this reference?</B>
132 <BR><B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </B>Decimal is used often
133 in this reference, as it is conventional to specify certain details about
134 the VGA's operation in decimal. Decimal numbers have no letter after them.
135 An example you might see would be a video mode described as 640x480z256.
136 This means that the video mode has 640 pixels across by 480 pixels down
137 with 256 possible simultaneous colors. Similarly an 80x25 text mode means
138 that the text mode has 80 characters wide (columns) by 25 characters high
139 (rows.) Binary is frequently used to specify fields within a particular
140 register and also for bitmap patterns, and has a trailing letter b (such
141 as 10011100b) to distinguish it from a decimal number containing only 0's
142 and 1's. Octal you are not likely to encounter in this reference, but if
143 used has a trailing letter o (such as 145o) to distinguish it from a decimal.
144 Hexadecimal is always used for addressing, such as when describing I/O
145 ports, memory offsets, or indexes. It is also often used in fields longer
146 than 3 bits, although in some cases it is conventional to utilize decimal
147 instead (for example in a hypothetical screen-width field.)
148 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Note: Decimal numbers in
149 the range 0-1 are also binary digits, and if only a single digit is present,
150 the decimal and binary numbers are equivalent. Similarly, for octal the
151 a single digit between 0-7 is equivalent to the decimal numbers in the
152 same range. With hexadecimal, the single-digit numbers 0-9 are equivalent
153 to decimal numbers 0-9. Under these circumstances, the number is often
154 given as decimal where another format would be conventional, as the number
155 is equivalent to the decimal value.
156 <BR>&nbsp;
157
158 <P><A NAME="memory"></A><B>How do memory and I/O ports work?</B>
159 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 80x86 machines have both
160 a memory address space and an input/output (I/O) address space. Most of
161 the memory is provided as system RAM on the motherboard and most of the
162 I/O devices are provided by cards (although the motherboard does provide
163 quite a bit of I/O capability, varying on the motherboard design.) Also
164 some cards also provide memory. The VGA and SVGA display adapters provide
165 memory in the form of video memory, and they also handle I/O addresses
166 for controlling the display, so you must learn to deal with both. An adapter
167 card could perform all of its functions using solely memory or I/O (and
168 some do), but I/O is usually used because the decoding circuitry is simpler
169 and memory is used when higher performance is required.
170 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The original PC design was
171 based upon the capabilities of the 8086/8088, which allowed for only 1
172 MB of memory, of which a small range (64K) was allotted for graphics memory.
173 Designers of high-resolution video cards needed to put more than 64K of
174 memory on their video adapters to support higher resolution modes, and
175 used a concept called "banking" which made the 64K available to the processor
176 into a "window" which shows a 64K chunk of video memory at once. Later
177 designs used multiple banks and other techniques to simplify programming.
178 Since modern 32-bit processors have 4 gigabytes of address space, some
179 designers allow you to map all of the video memory into a "linear frame
180 buffer" allowing access to the entire video memory at once without having
181 to change the current window pointer (which can be time consuming.) while
182 still providing support for the windowed method.
183 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memory can be accessed most
184 flexibly as it can be the source and/or target of almost every machine
185 language instruction the CPU is capable of executing, as opposed to a very
186 limited set of I/O instructions. I/O space is divided into 65536 addresses
187 in the range 0-65535. Most I/O devices are configured to use a limited
188 set of addresses that cannot conflict with another device. The primary
189 instructions for accessing I/O are the assembly instructions "IN" and "OUT",
190 simply enough. Most programming environments provide similarly named instructions,
191 functions, or procedures for accessing these.
192 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memory can be a bit confusing
193 though, because the CPU has two memory addressing modes, Real mode and
194 Protected mode. Real mode was the only method available on the 8086 and
195 is still the primary addressing mode used in DOS. Unfortunately, real mode
196 only provides access to the first 1 MB of memory. Protected mode is used
197 on the 80286 and up to allow access to more memory. (There are also other
198 details such as protection, virtual memory, and other aspects not particularly
199 applicable to this discussion.) Memory is accessed by the 80x86 processors
200 using segments and offsets. Segments tell the memory management unit where
201 in memory is located, and the offset is the displacement from that address.
202 In real mode offsets are limited to 64K, because of the 16-bit nature of
203 the 8086. In protected mode, segments can be any size up to the full address
204 capability of the machine. Segments are accessed via special segment registers
205 in the processor. In real mode, the segment address is shifted left four
206 bits and added to the offset, allowing for a 20 bit address (20 bits =
207 1 MB); in protected mode segments are offsets into a table in memory which
208 tells where the segment is located. Your particular programming environment
209 may create code for real and/or protected mode, and it is important to
210 know which mode is being used. An added difficulty is the fact that protected
211 mode provides for I/O and memory protection (hence protected mode), in
212 order to allow multiple programs to share one processor and prevent them
213 from corrupting other processes. This means that you may need to interact
214 with the operating system to gain rights to access the hardware directly.
215 If you write your own protected mode handler for DOS or are using a DOS
216 extender, then this should be simple, but it is much more complicated under
217 multi-tasking operating systems such as Windows or Linux.
218
219 <P><A NAME="access"></A><B>How do I access these from my programming environment?</B>
220 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; That is a very important
221 question, one that is very difficult to answer without knowing all of the
222 details of your programming environment. The documentation that accompanies
223 your particular development environment is best place to look for this
224 information, in particular the compiler, operating system, and/or the chip
225 specifications for the platform.
226
227 <P>Details for some common programming environments are given in:
228 <UL>
229 <LI>
230 Eli Zaretskii's <A HREF="http://www.delorie.com/djgpp/v2faq/faq133.html#Low-level">DJGPP
231 Frequently-Asked Questions List: Low-level DOS/BIOS and Hardware-oriented
232 Programming</A> -- details on accessing hardware in DJGPP, a free 32-bit
233 compiler and programming environment for MS-DOS.</LI>
234
235 <LI>
236 There were more links here, but they expired. If anyone wants to write
237 an article or has a link on this topic for their favorite environment,
238 I will gladly and thankfully put it on this site.</LI>
239 </UL>
240 Notice: All trademarks used or referred to on this page are the property
241 of their respective owners.
242 <BR>All pages are Copyright &copy; 1997, 1998, J. D. Neal, except where
243 noted. Permission for utilization and distribution is subject to the terms
244 of the <A HREF="license.htm">FreeVGA Project Copyright License</A>.
245 </BODY>
246 </HTML>