📄 write your own operating system - lesson 7.htm
字号:
addresses starting at address 0xB0000 (flat address from base of the physical
address space).<SPAN style="mso-spacerun: yes"> </SPAN>The region of
memory holding the text content of the screen starts at 0xB0000 for monochrome
displays and 0xB8000 for color displays.<SPAN style="mso-spacerun: yes">
</SPAN>Try the latter address first, and if you can抰 get it working, perhaps
try the former.<SPAN style="mso-spacerun: yes"> </SPAN>I will proceed
assuming the use of a color display.</P>
<P class=MsoNormal>The first byte (0xB8000) holds the ASCII code for the
upper-leftmost character on the screen.<SPAN style="mso-spacerun: yes">
</SPAN>The next byte (0xB8001) holds the color/style code for that
character.<SPAN style="mso-spacerun: yes"> </SPAN>These bytes are followed
by two more bytes that hold the ASCII code and color/style code for the next
character to the right.<SPAN style="mso-spacerun: yes"> </SPAN>The video
memory continues alternating ASCII code and style/color code to the end of the
first row of text.<SPAN style="mso-spacerun: yes"> </SPAN>The next bytes
after this represent the first character on the second row, and so on for the
rest of the screen.</P>
<P class=MsoNormal>So, all that is necessary to output text to the screen is to
write ASCII codes into this region of memory.<SPAN
style="mso-spacerun: yes"> </SPAN>(This is referred to as memory-mapped
I/O.)<SPAN style="mso-spacerun: yes"> </SPAN>However, you will now need to
keep track of the location of the cursor.</P>
<P class=MsoNormal>Speaking of the cursor, you can write characters to anywhere
on the screen (anywhere in video memory).<SPAN style="mso-spacerun: yes">
</SPAN>But it will look odd to the user if they are typing on the keyboard and
characters are appearing one place on the screen and the little blinking cursor
is elsewhere on the screen.<SPAN style="mso-spacerun: yes"> </SPAN>The
video controller chip (6845 on the IBM PC) takes care of drawing the blinking
cursor on the screen; we just need to tell it where to move the cursor.</P>
<P class=MsoNormal>The 6845 video controller is connected to I/O port
0x3B4-0x3B5 for a mono display and 0x3D4-0x3D5 for a color display. As far as I
can tell, the 6845 has various registers, and (assuming a color display) port
0x3D4 is used to indicate which port we would like to write to, and then we
write the data to port 0x3D5. Registers 14-15 tell the 6845 where to draw the
blinky cursor.<SPAN style="mso-spacerun: yes"> </SPAN>The following is
psuedo-code for moving the cursor.</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<DIV class=Section1><FONT face="Courier New" size=2>outbyte(0x3D4,
14);
// write to register 14 first</FONT></DIV>
<DIV class=Section1><FONT face="Courier New" size=2>outbyte(0x3D5,
(cursorpos>>8) & 0xFF); // output high byte</FONT></DIV>
<DIV class=Section1><FONT face="Courier New" size=2>outbyte(0x3D4,
15);
// again to register 15</FONT></DIV>
<DIV class=Section1><FONT face="Courier New" size=2>outbyte(0x3D5, cursorpos
& 0xFF); // low byte in this
register</FONT></DIV></BLOCKQUOTE>
<P class=MsoNormal>The cursor position (in cursorpos) is the character number,
starting with 0 and number all the characters in the order that they are
arranged in video memory.<SPAN style="mso-spacerun: yes"> </SPAN>(The
offset in video memory for a given cursor position is <SPAN
style="FONT-FAMILY: 'Courier New'">cursorpos*2</SPAN> for the ASCII code and
<SPAN style="FONT-FAMILY: 'Courier New'">(cursorpos*2)+1</SPAN> for the
color/style code.)</P>
<P class=MsoNormal>Using this region of video memory to output characters and
the I/O ports to tell the video controller where to draw the cursor, it is now
your job to write a text driver for your operating system. Create a set of
functions that you can call to output characters, strings, numbers, pointers,
etc to the screen without using BIOS (this means no software interrupts).<SPAN
style="mso-spacerun: yes"> </SPAN>Make sure that your text driver handles
scrolling the text on the screen upward before going off the bottom.<SPAN
style="mso-spacerun: yes"> </SPAN>Write a function to clear the
screen.</P>
<P class=MsoNormal>Try allowing the user to type characters with the keyboard
(you can still use BIOS for keyboard input for now), and echo each character to
the screen as it is typed.<SPAN style="mso-spacerun: yes"> </SPAN>If you
have any troubles writing your text driver, let me know and perhaps I can give
you some hints.</P>
<P class=MsoNormal>As far as the color/style byte is concerned, you can simply
use 07 (white on black) for most purposes, but for those of you who are curious,
I will explain the different color/style settings. The color/style of a
character is one byte.<SPAN style="mso-spacerun: yes"> </SPAN>Those 8 bits
are used as follows.</P>
<H2 class=Section1 style="MARGIN-LEFT: 0.5in"><A name=_Toc534120521>Bits 3-0 :
Foreground color</A></H2>
<DIV class=Section1>
<TABLE
style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN-LEFT: 36.35pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-border-alt: solid windowtext .5pt"
cellSpacing=0 cellPadding=0 border=1>
<TBODY>
<TR>
<TD vAlign=top width=55>
<H2><A name=_Toc534120522>Bit</A></H2></TD>
<TD vAlign=top width=78>
<P class=MsoNormal>3</P></TD>
<TD vAlign=top width=48>
<P class=MsoNormal>2</P></TD>
<TD vAlign=top width=62>
<P class=MsoNormal>1</P></TD>
<TD vAlign=top width=53>
<P class=MsoNormal>0</P></TD></TR>
<TR>
<TD vAlign=top width=55>
<H2><A name=_Toc534120523>Color</A></H2></TD>
<TD vAlign=top width=78>
<P class=MsoNormal>Intensity</P></TD>
<TD vAlign=top width=48>
<P class=MsoNormal>Red</P></TD>
<TD vAlign=top width=62>
<P class=MsoNormal>Green</P></TD>
<TD vAlign=top width=53>
<P class=MsoNormal>Blue</P></TD></TR></TBODY></TABLE></DIV>
<P class=MsoNormal><![if !supportEmptyParas]><![endif]> <o:p></o:p></P>
<P class=MsoNormal>These four bits can be used in any and all 16
combinations.<SPAN style="mso-spacerun: yes"> </SPAN>If bit 3 is 1, it
indicates full intensity, 0 indicates half intensity.<SPAN
style="mso-spacerun: yes"> </SPAN>For example, 3 would be cyan (blue +
green) while 11 would be bright cyan (intensity+blue+green).</P>
<H2 class=Section1 style="MARGIN-LEFT: 0.5in"><A name=_Toc534120524>Bit 5:
Reverse Video</A></H2>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in"><B
style="mso-bidi-font-weight: normal">Bit 6: Black on colored background, color
given by bits 3-0<o:p></o:p></B></P>
<H2 class=Section1 style="MARGIN-LEFT: 0.5in"><A name=_Toc534120525>Bit 7:
Blinking text</A></H2>
<DIV class=Section1
style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0in; BORDER-TOP: medium none; PADDING-LEFT: 0in; PADDING-BOTTOM: 1pt; BORDER-LEFT: medium none; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 0.75pt solid">
<P class=MsoNormal
style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0in; BORDER-TOP: medium none; PADDING-LEFT: 0in; PADDING-BOTTOM: 0in; BORDER-LEFT: medium none; PADDING-TOP: 0in; BORDER-BOTTOM: medium none; mso-padding-alt: 0in 0in 1.0pt 0in; mso-border-bottom-alt: solid windowtext .75pt">For
example a code of 0x2C (00101100bin) would be reverse video bright red.<SPAN
style="mso-bidi-font-weight: normal"><o:p></o:p> </SPAN></P></DIV>
<P class=MsoNormal><A
href="http://www.cse.unl.edu/~jgompert/OS/lesson6.htm">Previous Lesson</A>
| <A href="http://www.cse.unl.edu/~jgompert/OS/TableOfContents.htm">Table
of Contents</A> | Next Lesson</P>
<DIV class=Section1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -