VGA Text Mode

发布时间 2023-09-29 20:20:27作者: ImreW
Feb 26, 2018

The VGA text mode is a simple way to print text to the screen. In this post, we create an interface that makes its usage safe and simple by encapsulating all unsafety in a separate module. We also implement support for Rust’s formatting macros.

VGA 文本模式是一种将文本打印到屏幕的简单方法。 在这篇文章中,我们创建了一个接口,通过将所有不安全因素封装在一个单独的模块中,使其使用安全且简单。 我们还实现了对 Rust 格式化宏的支持。

This blog is openly developed on GitHub. If you have any problems or questions, please open an issue there. You can also leave comments at the bottom. The complete source code for this post can be found in the post-03 branch.

该博客是在 GitHub 上公开开发的。 如果您有任何问题或疑问,请在那里提出问题。 也可以在底部留言评论。 这篇文章的完整源代码可以在 post-03 分支中找到。

The VGA Text Buffer

To print a character to the screen in VGA text mode, one has to write it to the text buffer of the VGA hardware. The VGA text buffer is a two-dimensional array with typically 25 rows and 80 columns, which is directly rendered to the screen. Each array entry describes a single screen character through the following format:

要在 VGA 文本模式下将字符打印到屏幕上,必须将其写入 VGA 硬件的文本缓冲区。 VGA文本缓冲区是一个二维数组,通常有25行和80列,直接渲染到屏幕上。 每个数组条目通过以下格式描述一个屏幕字符:

Bit(s)Value
0-7 ASCII code point
8-11 Foreground color
12-14 Background color
15 Blink

The first byte represents the character that should be printed in the ASCII encoding. To be more specific, it isn’t exactly ASCII, but a character set named code page 437 with some additional characters and slight modifications. For simplicity, we will proceed to call it an ASCII character in this post.

第一个字节表示应以 ASCII 编码打印的字符。 更具体地说,它并不完全是 ASCII,而是一个名为代码页 437 的字符集,带有一些附加字符和轻微修改。 为简单起见,我们将在本文中将其称为 ASCII 字符。

The second byte defines how the character is displayed. The first four bits define the foreground color, the next three bits the background color, and the last bit whether the character should blink. The following colors are available:

第二个字节定义字符的显示方式。 前四位定义前景色,接下来的三位定义背景色,最后一位定义字符是否应该闪烁。 有以下颜色可供选择: