How to draw a pixel by changing video memory map directly in a C program (without library functions)

回眸只為那壹抹淺笑 提交于 2021-02-08 04:05:25

问题


Is it possible to display a black dot by changing values in the screen(video ie monitor) memory map in RAM using a c program?

I don't want to use any library functions as my primary aim is to learn how to develop a simple OS. I tried accessing the starting screen memory map ie 0xA0000 (in C).

I tried to run the program but got a Segmentation Fault since no direct access is provided. In super user, the program gets executed without any change.

Currently I am testing in VirtualBox.


回答1:


A "real" operating system will not use the framebuffer at address 0xA0000, so you can't draw on the screen by writing to it directly. Instead your OS probably has proper video drivers that will talk to the hardware in various very involved ways. In short there's no easy way to do what you want to do on a modern OS.

On the other hand, if you want to learn how to write your own OS, then it would be very good practice to try to write a minimal kernel that can output to the VGA text framebuffer at 0xB8000 and maybe then the VGA graphic framebuffer at 0xA0000.

You can start using those framebuffers and drawing on the screen almost immediately after the BIOS jumps to your kernel, with a minimal amount of setting up. You could do that directly from real mode in maybe a hundred lines of assembler tops, or perhaps in C with a couple lines of assembler glue first. Even simpler would be to have GRUB set up the hardware, boot your minimal kernel, and you can directly write to it in a couple lines.




回答2:


Short answer is no because the frame buffer on modern operating systems is setup as determined by the vbios and kernel driver(s). It depends on amount of VRAM present on the board, the size of the GART, physical Ram present and a whole bunch of other stuff (VRAM reservation, whether it should be visible to CPU or not, etc). On top of this, modern OS's are utilizing multiple back buffers and flipping the HW to display between these buffers, so even if you could directly poke to the frame buffer, the address would change from frame to frame.

If you are interesting in do this for learning purposes, I would recommend creating a simple OGL or D3D (for example) 'function' which takes a 'fake' system allocated frame buffer and presents it to the screen using regular HW operations.

You could even set the refresh up on a timer to fake update.

Then your fake OS would just write pixels to the fake system memory buffer and this rendering function would take care of displaying it as if it were real.



来源:https://stackoverflow.com/questions/28841555/how-to-draw-a-pixel-by-changing-video-memory-map-directly-in-a-c-program-withou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!