VESA 显示模式:VBE 函数、模式选择与常见错误
文章目录
在传统 BIOS(以及处于传统 BIOS 模式的支持 CSM 的 UEFI 系统)下,你可以使用标准化的 VESA/VBE 函数来访问显卡功能。
而在 UEFI 模式下,则使用 GOP (Graphics Output Protocol,图形输出协议)以标准化方式访问显卡。
VESA 函数
你需要在 VESA VBE 文档中查找这些函数: 所有 VESA 函数在受支持时会在 AL 中返回 0x4F,并使用 AH 作为状态标志,其中 0x00 表示成功。这意味着每次 VESA 调用后都应检查 AX 是否为 0x004F,以确认调用是否成功。
- INT 0x10, AX=0x4F00
- 获取控制器信息。该函数返回所有受支持显示模式的数组。
struct VbeInfoBlock { char VbeSignature[4]; // == "VESA" uint16_t VbeVersion; // == 0x0300 for VBE 3.0 uint16_t OemStringPtr[2]; // isa vbeFarPtr uint8_t Capabilities[4]; uint16_t VideoModePtr[2]; // isa vbeFarPtr uint16_t TotalMemory; // as # of 64KB blocks uint8_t Reserved[492]; } __attribute__((packed)); VbeInfoBlock *vib = dos_alloc(512); v86_bios(0x10, {ax:0x4f00, es:SEG(vib), di:OFF(vib)}, &out); if (out.ax!=0x004f) die("Something wrong with VBE get info");
汇编代码(NASM):
struc VesaInfoBlock ; VesaInfoBlock_size = 512 bytes .Signature resb 4 ; must be 'VESA' .Version resw 1 .OEMNamePtr resd 1 .Capabilities resd 1 .VideoModesOffset resw 1 .VideoModesSegment resw 1 .CountOf64KBlocks resw 1 .OEMSoftwareRevision resw 1 .OEMVendorNamePtr resd 1 .OEMProductNamePtr resd 1 .OEMProductRevisionPtr resd 1 .Reserved resb 222 .OEMData resb 256 endstruc Main: push ds pop es mov di, VesaInfoBlockBuffer call get_vesa_info jmp $ ; in: ; es:di - 512-byte buffer ; out: ; cf - set on error get_vesa_info: clc mov ax, 0x4f00 int 0x10 cmp ax, 0x004f jne .failed ret .failed: stc ret ALIGN(4) VesaInfoBlockBuffer: istruc VesaInfoBlock at VesaInfoBlock.Signature, db "VESA" times 508 db 0 iend
INT 0x10, AX=0x4F01, CX=mode, ES:DI=256 字节缓冲区
- 获取模式信息。对模式数组中的每个成员调用此函数,以了解该模式的详细信息。256 字节缓冲区将由模式信息块填充。
struct vbe_mode_info_structure { uint16_t attributes; // deprecated, only bit 7 should be of interest to you, and it indicates the mode supports a linear frame buffer. uint8_t window_a; // deprecated uint8_t window_b; // deprecated uint16_t granularity; // deprecated; used while calculating bank numbers uint16_t window_size; uint16_t segment_a; uint16_t segment_b; uint32_t win_func_ptr; // deprecated; used to switch banks from protected mode without returning to real mode uint16_t pitch; // number of bytes per horizontal line uint16_t width; // width in pixels uint16_t height; // height in pixels uint8_t w_char; // unused... uint8_t y_char; // ... uint8_t planes; uint8_t bpp; // bits per pixel in this mode uint8_t banks; // deprecated; total number of banks in this mode uint8_t memory_model; uint8_t bank_size; // deprecated; size of a bank, almost always 64 KB but may be 16 KB... uint8_t image_pages; uint8_t reserved0; uint8_t red_mask; uint8_t red_position; uint8_t green_mask; uint8_t green_position; uint8_t blue_mask; uint8_t blue_position; uint8_t reserved_mask; uint8_t reserved_position; uint8_t direct_color_attributes; uint32_t framebuffer; // physical address of the linear frame buffer; write here to draw to the screen uint32_t off_screen_mem_off; uint16_t off_screen_mem_size; // size of memory in the framebuffer but not being displayed on the screen uint8_t reserved1[206]; } __attribute__ ((packed));
汇编代码 NASM:
struc VesaModeInfoBlock ; VesaModeInfoBlock_size = 256 bytes .ModeAttributes resw 1 .FirstWindowAttributes resb 1 .SecondWindowAttributes resb 1 .WindowGranularity resw 1 ; in KB .WindowSize resw 1 ; in KB .FirstWindowSegment resw 1 ; 0 if not supported .SecondWindowSegment resw 1 ; 0 if not supported .WindowFunctionPtr resd 1 .BytesPerScanLine resw 1 ; Added in Revision 1.2 .Width resw 1 ; in pixels(graphics)/columns(text) .Height resw 1 ; in pixels(graphics)/columns(text) .CharWidth resb 1 ; in pixels .CharHeight resb 1 ; in pixels .PlanesCount resb 1 .BitsPerPixel resb 1 .BanksCount resb 1 .MemoryModel resb 1 ; http://www.ctyme.com/intr/rb-0274.htm#Table82 .BankSize resb 1 ; in KB .ImagePagesCount resb 1 ; count - 1 .Reserved1 resb 1 ; equals 0 in Revision 1.0-2.0, 1 in 3.0 .RedMaskSize resb 1 .RedFieldPosition resb 1 .GreenMaskSize resb 1 .GreenFieldPosition resb 1 .BlueMaskSize resb 1 .BlueFieldPosition resb 1 .ReservedMaskSize resb 1 .ReservedMaskPosition resb 1 .DirectColorModeInfo resb 1 ; Added in Revision 2.0 .LFBAddress resd 1 .OffscreenMemoryOffset resd 1 .OffscreenMemorySize resw 1 ; in KB .Reserved2 resb 206 ; available in Revision 3.0, but useless for now endstruc Main: ; after getting VesaInfoBlock: push word [VesaInfoBlockBuffer + VesaInfoBlock.VideoModesSegment] pop es mov di, VesaModeInfoBlockBuffer mov bx, [VesaInfoBlockBuffer + VesaInfoBlock.VideoModesOffset] ; get video modes list address mov cx, [bx] ; get first video mode number cmp cx, 0xffff ; vesa modes list empty je .NoModes call get_vesa_mode_info .NoModes: jmp $ ; in: ; cx - VESA mode number ; es:di - 256-byte buffer ; out: ; cf - set on error get_vesa_mode_info: clc mov ax, 0x4f01 int 0x10 cmp ax, 0x004f jne .failed ret .failed: stc ret ALIGN(4) VesaModeInfoBlockBuffer: istruc VesaModeInfoBlock times VesaModeInfoBlock_size db 0 iend
INT 0x10, AX=0x4F02, BX=mode, ES:DI=CRTCInfoBlock
- 设置显示模式。使用你决定采用的模式编号调用此函数。如果你选择的模式使用线性帧缓冲区,应将模式编号与 0x4000 进行 OR 运算。这会在模式编号中设置“Use LFB”位。将 BX 的位 11 置位,以指示 BIOS 使用传入的 CRTCInfoBlock 结构,详见规范了解更多信息。
在 Bochs 中能工作吗?
要让 VBE 在 Bochs 中工作,你需要 "VGABIOS-lgpl" BIOS,并且 Bochs 的版本需要在编译时启用了 --enable-vbe 选项……更多信息请参阅 Bochs 中的 Vesa 信息 帖子。另请阅读关于 Bochs 图形适配器.
如何选择我想要的模式?
VESA 很久以前就不再为显示模式分配代码了——取而代之的是,他们标准化了一种更好的方案:你可以查询显卡支持哪些模式,并查询每个模式的属性。在你的操作系统中,可以编写一个函数,传入所需的宽度、高度和色深,它返回对应的显示模式编号(或最接近的匹配)。然后,只需设置该模式即可。
下面是一段示例代码,假设你已经有一个 VirtualMonitor……基本上,你会扫描 VbeInfoBlock.videomodes[] 引用的“模式列表”,然后对每个模式调用“get mode info”。接着可以将每个模式的宽度、高度和色深与所需值进行比较。
uint16_t findMode(int x, int y, int d) { struct VbeInfoBlock *ctrl = (VbeInfoBlock *)0x2000; struct ModeInfoBlock *inf = (ModeInfoBlock *)0x3000; uint16_t *modes; int i; uint16_t best = 0x13; int pixdiff, bestpixdiff = DIFF(320 * 200, x * y); int depthdiff, bestdepthdiff = 8 >= d ? 8 - d : (d - 8) * 2; strncpy(ctrl->VbeSignature, "VBE2", 4); intV86(0x10, "ax,es:di", 0x4F00, 0, ctrl); // Get Controller Info if ( (uint16_t)v86.tss.eax != 0x004F ) return best; modes = (uint16_t*)REALPTR(ctrl->VideoModePtr); for ( i = 0 ; modes[i] != 0xFFFF ; ++i ) { intV86(0x10, "ax,cx,es:di", 0x4F01, modes[i], 0, inf); // Get Mode Info if ( (uint16_t)v86.tss.eax != 0x004F ) continue; // Check if this is a graphics mode with linear frame buffer support if ( (inf->attributes & 0x80) != 0x80 ) continue; // Check if this is a packed pixel or direct color mode if ( inf->memory_model != 4 && inf->memory_model != 6 ) continue; // Check if this is exactly the mode we're looking for if ( x == inf->XResolution && y == inf->YResolution && d == inf->BitsPerPixel ) return modes[i]; // Otherwise, compare to the closest match so far, remember if best pixdiff = DIFF(inf->Xres * inf->Yres, x * y); depthdiff = (inf->bpp >= d)? inf->bpp - d : (d - inf->bpp) * 2; if ( bestpixdiff > pixdiff || (bestpixdiff == pixdiff && bestdepthdiff > depthdiff) ) { best = modes[i]; bestpixdiff = pixdiff; bestdepthdiff = depthdiff; } } if ( x == 640 && y == 480 && d == 1 ) return 0x11; return best; }
常见错误
初学者刚开始使用 VBE 时,似乎经常犯一些错误。
VESA 定义的模式编号
对于较早版本的 VBE 规范(VBE 1.0、VBE 1.1 和 VBE 1.2),VESA 定义了一些标准模式编号。例如,模式 0x0113 是 800 * 600 * 15-bpp。在 VBE 2.0 中,这些模式编号被弃用——VESA 决定不再定义更多此类编号,告知显卡制造商无需再使用旧的标准模式编号,并告知程序员应搜索所需模式而不依赖任何旧的标准模式编号。尽管如此,网上仍可能找到过时的信息,建议使用这些旧的标准模式编号。
使用“行间字节数”
人们往往假设像素数据是连续的(例如第 1 行,然后第 2 行,然后第 3 行,……)。情况并非总是如此,由于各种原因,行与行之间可能存在填充(例如第 1 行,填充,然后第 2 行,填充,然后第 3 行,……)。“Get Mode Information” VBE 函数返回行之间的字节数,应使用该值。例如,对于 640 * 480 * 16-bpp 的显示模式,某一行的偏移量应通过 "offset = line * bytes_per_line" 计算,而不是 "offset = line * (640 * 2)"。同样地,你不能用一次 "rep stosb/w/d/q" 或 "memset()" 填充整个显示区域,而应逐行填充,以避免写入任何填充区域。
注意,(对于 VBE 3.0)“Get Mode Information” VBE 函数可能返回 2 个“行间字节数”值。第一个用于显示模式设置为分组切换(bank switching)的情况,第二个用于使用线性帧缓冲区的情况。对于较早版本的 VBE,没有“线性帧缓冲区的行间字节数”值,“分组切换的行间字节数”值同时用于分组切换和线性帧缓冲区。
不要假设像素格式
通常像素数据是 "RGB" 格式,但情况并非总是如此。像素可能是 "BGR" 格式或其他任何格式,甚至可能不使用红、绿、蓝分量——它可能使用 "YUV",其中有一个亮度/明度分量(Y)和两个色度分量(UV)。务必检查 "Get Mode Information" VBE 函数返回的结构中的 "memory_model" 字段,以及(对于 15-bpp 及更高色深的显示模式)所有“分量掩码”和“字段位置”字段(例如 "red_mask_size"、"red_field_position" 等)。
不要假设未使用位是未使用的
对于某些颜色深度,像素数据中存在"未使用"的位(在 15-bpp 模式下每个像素有一个"未使用"位,在 32-bpp 模式下每个像素有 8 个"未使用"位)。这些未使用的位实际上可能被某些显卡用于各种额外功能。举个例子,我知道有一款显卡(在 15-bpp 模式下),如果最高位/未使用位被置位,它会将像素数据中最低的 8 位用作调色板的索引。
不要假设 VGA 兼容性
对于 8-bpp 显示模式,人们假设可以使用 VGA I/O 端口来更改调色板。使用 VBE 函数要好得多,但如果你不这样做,至少应通过测试 "Get Mode Information" VBE 函数返回的 "mode_attributes" 字段中的“VGA compatible mode”标志(位 5)来检查该显示模式是否使用 VGA I/O 端口。
类似地,对于 4-bpp 的“平面(planar)”显示模式,有些人假设可以使用 VGA I/O 端口在平面之间切换,而不是先测试“VGA compatible mode”标志是否置位。
不要假设显示器支持某个显示模式
我们都喜欢漂亮的图形和高分辨率的显示模式。遗憾的是,即使 VBE 说某个显示模式受显卡支持,也不意味着该显示模式也受显示器支持。VESA 定义了 2 种显示模式时序,旨在被所有显示器支持(640 * 480 标准 VGA 时序和 720 * 480 标准 VGA 时序)。对于所有其他显示模式,你应使用 EDID 来查明显示器是否支持该显示模式的时序,或者提供一种方式让用户测试显示模式并在不起作用时更改它。这是 Windows 采用的方法,会弹出一个对话框,提供接受或还原的选项。15 秒内无操作则还原。只是个想法 ;).
为此目的使用 EDID 很复杂。除非你在设置显示模式时提供 "CRTC information block" 结构,否则你无法完全确定显卡将使用什么时序;而且只有部分支持 VBE 3.0 的显卡能正确支持 "CRTC information"。
另一种选择是只使用(应该)使用 640 * 480 和 720 * 480 标准 VGA 时序的显示模式。对于水平分辨率较低的显示模式,显卡会将每个像素发送两次;对于垂直分辨率较低的显示模式,显卡会将每行像素发送两次(称为“双扫描”);在这两种情况下,视频时序与相应较高分辨率显示模式的时序相同。由此得到以下“安全”分辨率列表:
- 720 * 480
- 640 * 480
- 360 * 480(实际使用 "720 * 480" 时序)
- 320 * 480(实际使用 "640 * 480" 时序)
- 720 * 240(实际使用 "720 * 480" 时序)
- 640 * 240(实际使用 "640 * 480" 时序)
- 360 * 240(实际使用 "720 * 480" 时序)
- 320 * 240(实际使用 "640 * 480" 时序)
色深不会/不应影响视频时序信号。这意味着最佳的可能安全显示模式是 720 * 480 * 24/32-bpp。
不要从显存读取
从显存读取非常慢!请改用 双缓冲 。
另请参阅
文章
帖子
- VESA,更高模式 - 初始帖子,由 Dreamsmith(又名 DaidalosGuy)回复
外部链接
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:VESA Video Modes;离线修订号:30513。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论