问题描述
我想制作一个(极其简单的)操作系统.我目前正在学习图形卡.
I want to make an (extremely simple) operating system. I am currently learning about graphics cards.
这是我到目前为止所知道的(如果我错了,请纠正我):
This is what I know so far (please correct me if I am wrong):
- 图形卡有两种模式:文本模式和图形模式.
- 您可以使用BIOS(而不是直接访问图形卡)将数据写入图形卡.
我要做的是不使用BIOS而直接写入图形卡的视频内存(因为我想了解事情的原理).所以我有以下问题:
What I want to do is to write directly to the graphics card's video memory without using BIOS (because I want to understand how things work). So I have the following questions:
- 我怎么知道图形卡视频存储器的基址是什么?通过探测PCI总线以获取基地址来完成,还是基地址是固定的(例如,就像COM端口的基地址是固定的一样?)?
- 是否以相同方式访问所有图形卡,还是必须为所有可用图形卡创建设备驱动程序?
- How do I know what is the base address of the video memory of the graphics card, is thisdone by probing the PCI bus to get the base address, or is the base address fixed (just like the COM ports base addresses is fixed for example)?
- Are all graphics cards accessed in the same way, or do I have to create device drivers for all available graphics cards?
:我正在使用x86.
I am using x86.
推荐答案
简介
图形卡是一个非常复杂的主题,我有信心地说,它们是PC上最复杂的子系统.
如果您发现自己丢失了 XHCI (USB 3.0)或旧版网络接口卡,因为这要复杂得多.
Introduction
Graphics cards are a very complex topic, I'm confident in saying that they are the most complex subsystem you'll find on a PC.
If you ever found yourself lost programming an XHCI (USB 3.0) or an old RTL8239A network interface card then be prepared because this is much more complex.
图形控制器是竞争非常激烈的营销产品-很少有供应商打开规范,当这样做时,它故意提供了不良支持.
如果您补充说硬件本身可以处理:编解码器,音频(是的,音频流也是如此), 3D 可编程管道,视频信号和视频输出,表面格式,媒体格式, DMA和内存重新映射,您会发现对视频卡进行编程并不是一件容易的事.
Graphics controllers are the products of a very competitive marketing - rarely a vendor opens the specifications and when it does, it gives an intentionally poor support.
If you add that the hardware itself deals with: codecs, audio (yes, audio streams too), 3D programmable pipelines, video signals and video outputs, surface formats, media formats, DMA and memory remapping then you can see that it is not an easy task to program a video card.
我认为,更好的方法是追溯视频卡的历史".从 MDA 开始,然后移至 CGA ,然后 EGA ,最后 VGA .
The better approach, in my opinion, is to "retrace the history" of the video cards.Start from the MDA then move to CGA then EGA and finally to VGA.
仍支持VGA传统,可在此处或找到规范在此PDF 的第一部分中来自英特尔.
您可以在没有BIOS的情况下轻松地"对VGA进行编程-这意味着它已经是众所周知的且已记录在案的硬件体系结构(但不一定易于配置).
我不记得以前的适配器是否是VGA的子集,否则可能不再受支持.
您可以尝试使用虚拟机或仿真器.
The VGA legacy is still supported, the specifications can be found here or in the first part of this PDF from Intel.
You can program the VGA without the BIOS "easily" - meaning that it is an already well-known and documented hardware architecture (but not necessarily easy to configure).
I don't remember if the previous adapters were subsets of the VGA or not, if not they aren't supported anymore probably.
You can try with a virtual machine or an emulator.
当您对VGA感到满意时,可以转到 SVGA .
麻烦就来了:正如维基百科所确认的那样,VGA是最后一个真正标准化的视频卡/适配器接口:
When you are satisfied with the VGA you can move to the SVGA.
Here come the troubles: as Wikipedia confirms, the VGA was the last truly standardised video card/adapter interface:
组织VESA标准化了名为视频BIOS扩展的BIOS API,以允许使用SVGA卡到无驱动程序的操作系统,但这不是您想要的.
您可以尝试对VBE BIOS进行反向工程,但我认为这将是一场噩梦-对IO端口和MMIO进行无意义的写入流.
在没有任何参考的情况下了解十分之一的配置寄存器几乎是不可能的.
The organisation VESA standardised a BIOS API called Video BIOS Extensions to allow the use of SVGA cards to driverless OSes but that's not what you were looking for.
You can try reverse engineering a VBE BIOS but I think it will be a nightmare - a senseless stream of writes to IO ports and MMIOs.
Making sense of tenths of configuration registers without any reference is almost impossible.
请注意,到目前为止,我们仍在谈论1998年的技术.
在VESA VBE努力之后,不再发布标准接口-编程少于20年的视频卡的唯一可靠方法是与供应商签署NDA.
Note that we are still talking about 1998 technology up to this point.
After the VESA VBE effort, no more standard interfaces have been published - the only reliable way to program a video card with less than 20 years is by signing an NDA with its vendor.
幸运的是,最近(实际上已经不复存在了),英特尔凭借其Intel GFX(又称Intel HD Graphics)卡进入了市场.
英特尔从来没有打算制造一流的视频卡,甚至没有紧密地制造它-因为它们不是他们的核心业务,所以它们可以对自己的体系结构持开放态度.
结果就是这套奇妙的编程参考手册,它们描述了视频的功能卡.
用(传统)最少的信息完成编程.
Luckily, recently (actually, not anymore), Intel entered the market with its Intel GFX (a.k.a. Intel HD Graphics) cards.
Intel never aimed to manufacture top-of-the-notch video cards, not even closely - so they can be open about their architecture since that's not their core business.
The result is this marvellous set of Programming Reference Manuals that describe the functionality of their video cards.
Complete with (traditionalistic) minimal information to program them.
通常,由于硬件变得非常复杂且工作量很大,因此爱好者会在此之前停止(在SVGA检查点).
例如,我的Haswell集成视频卡记录有17个PDF,每个PDF约250页(平均).
显示部分自己以PDF格式进行了记录,为实现显示表面而消失了帧缓冲区,而仅硬件的显示部分是这样的:
In general, hobbyists stops before this point (at the SVGA checkpoint), because the hardware has become very complex and the efforts very huge.
For example, my Haswell integrated video card is documented with 17 PDFs of about 250 pages each (on average).
The display part is documented in a PDF on its own, the framebuffer has disappeared in favour of Display surface and the display part alone of the hardware is this:
尽管这可能不是很容易理解,但足以了解程序员在对现代视频卡进行编程之前必须理解的众多技术.
While this may not be very comprehensible, it should suffice to get an idea of the numerous technology that a programmer must understand before programming a modern video card.
您当然可以看一下Linux源代码,但要注意,即使对于简单的控制器,Linux内核通常也不会立即了解-它不是玩具OS,它是具有自己的API和接口的真实OS.必须适合硬件接口(实际上反之亦然).
此外,只有Intel和AMD视频驱动程序才是真正的开源程序,其他驱动程序既是专有程序,也只是一堆未公开的代码.
You can surely take a look at the Linux source code but beware that the Linux kernel is no usually of immediate understanding even for simple controllers - it is not a toy OS, it is a real OS with its own API and interface that must fit the hardware interface (actually the other way around).
Furthermore, only the Intel and AMD video drivers are really open source, the others are either proprietary or just a bunch of undocumented code.
如果您只想对VGA进行编程(确实是一项非常受人尊敬的任务!),可以从设置视频模式 03h(文本模式)或13h(图形模式).
If you just want to program the VGA (a very respectable task indeed!) you can start by setting the video modes 03h (text mode) or 13h (graphics mode).
视频模式03h
帧缓冲区位于0b8000h(物理地址),通常以0b800h:0000h进行访问,因为它易于获得零偏移.
屏幕由80x25个字符组成,每个字符在帧缓冲区中占据一个字(16位).
低位字节是字符代码-使用的字符映射会将字形与代码相关联(例如41h至A).
高阶字节是属性字节-低半字节是前景色,高半字节是背景色.
在上面的EGA/CGA/VGA链接中可以找到更多信息.
Video mode 03h
The frame buffer is at 0b8000h (physical address), usually accessed as 0b800h:0000h as it is handy to have a zero offset.
The screen is made up of 80x25 characters, each characters occupy a word (16-bit) in the frame buffer.
The low byte is the character code - the character map used will associate a glyph to a code (e.g. 41h to A).
The high order byte is the attribute byte - the low nibble is the foreground colour, the high nibble is the background colour.
More information can be found in the EGA/CGA/VGA links above.
视频模式13小时
这是一种图形模式,具有320x200像素,出于上述相同原因,帧缓冲区通常位于0a0000h(物理地址)处,即0a000h:0000h.
每个像素都是一个字节,该字节的值选择像素的颜色.
可以通过编程DAC寄存器(VGA适配器为3c7h,3c8h,3c9h)来更改默认调色板.
Video mode 13h
It is a graphical mode with 320x200 pixel, the frame buffer is at 0a0000h (physical address) usually accessed as 0a000h:0000h for the same reason of above.
Each pixel is a single byte, the value of the byte selects the colour of the pixel.
The default palette can be changed by programming the DAC registers (3c7h, 3c8h, 3c9h for the VGA adapter).
不一定,今天这种区别可能不再存在.
MDA只有文本模式.
EGA,CGA和VGA和SVGA都具备.
Not necessarily, today this distinction may not exist anymore.
The MDA had only a text mode.
EGA, CGA and VGA and SVGA had both.
现代的方法是在启动过程中或在特定情况下绘制文本(例如 BSOD )使用文本模式下的基本视频驱动程序.
由于视频驱动程序可能不可用/不可靠,因此该驱动程序可能使用BIOS服务.
The modern approach is to draw the text, however during boot or during particular situations (e.g. BSOD) a basic video driver in text mode is used.
This driver probably uses a BIOS service since the video driver may not be available/reliable.
直到SVGA时代,然后才停止对BIOS的支持.
Up to the SVGA era, then BIOS support was discontinued.
视频卡已通过历史记录连接到ISA,PCI,AGP和PCIe总线.
只有ISA总线是不可配置的(至少不是从一开始就无法配置),其他的每个功能都具有可配置的BAR(基地址寄存器)(PCI总线中最小的可寻址实体).
Video cards have been connected through the history to the ISA, PCI, AGP and PCIe buses.
Only the ISA bus wasn't configurable (at least not from the beginning), the others had configurable BARs (Base Address Registers) per function (the smallest addressable entity in the PCI bus).
为了获取视频卡MMIO寄存器的基地址,必须将PCI或PCIe总线枚举,并且必须读取/设置配置空间中的标准寄存器.
In order to get the base address of the MMIO registers of a video card the PCI or PCIe bus must be enumerated and the standard registers in the configuration space must be read/set.
处理PCIe并不像处理PCI那样容易.
Dealing with PCIe is not as easy as dealing with PCI.
请注意,即使UART也不具有固定地址,它们在默认情况下已配置为映射到旧版(3f8h,2f8h,3e8h和2e8h)地址,但是硬件在 SuperIO 芯片位于PCI-to-LPC桥接器后面,可模拟PCI-to-ISA桥接器.
随着英特尔平台集线器体系结构的出现(即南北桥的消亡),SuperIO芯片最终进入了 PCH 或移到 SPI控制器的后面.
Note that not even the UARTs have a fixed address, they are configured by default to map to the legacy (3f8h, 2f8h, 3e8h and 2e8h) addresses but the hardware was (is?) in a SuperIO chip behind a PCI-to-LPC bridge that emulated a PCI-to-ISA bridge.
With the advent of the Intel platform hub architecture (i.e. the death of the north and south bridge) the SuperIO chip eventually made it into the PCH or moved behind the SPI controller.
每个图形卡本身就是一个美丽的恶性生物.
每个型号都需要一个设备驱动程序.
某些驱动程序可以在整个模型系列中重用,但这通常是不正确的.
Each graphic card is a beautiful vicious creature on its own.
A device driver is needed for each model.
Some driver can be reused for a whole family of models but this is not true in general.
这篇关于如何在不使用BIOS的情况下将数据写入图形卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!