Drawing in 32bit colour modes in protected & long mode

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
obiwac
Member
Member
Posts: 149
Joined: Fri Jan 27, 2017 12:15 pm
Libera.chat IRC: obiwac
Location: Belgium

Drawing in 32bit colour modes in protected & long mode

Post by obiwac »

I am making a protected mode OS (I will probably also want to do long mode in the future) and I want to draw a pixel in 24 or 32bit colour modes. I would like to know if it is a good idea to switch to unreal mode. If so, how could I do it? Are there any other options? I am quite new to making OSes but I have a lot of experience with C. I am using the standard gcc compiler. Also, is it possible to do HDMI instead of VGA?
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Drawing in 32bit colour modes in protected & long mode

Post by SpyderTL »

Switching to 16-bit mode is, I would say, a last resort. I would only do that if you had no other choice.

If you are in 32 or 64 bit mode, drawing pixels to the screen is fairly easy.

What is not easy in 32/64 bit mode is switching from text mode to graphics mode. If you can do that with GRUB or in your custom 16-bit boot loader, that would be the recommended approach.

So, switch to your desired graphics mode when booting, before you switch to 32-bit/64-bit mode, and do everything else without going back to 16-bit mode, if at all possible.

HDMI is handled automatically by the video card. You shouldn't have to do anything special to get HDMI to work.

Once you are in 32/64-bit mode, you simply find the address of the video card frame buffer, and start changing memory address values, and the pixels will appear on the screen.

Details are on the wiki page here: Drawing In Protected Mode

Let us know if you have any questions or run into any problems.

Good luck.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
User avatar
obiwac
Member
Member
Posts: 149
Joined: Fri Jan 27, 2017 12:15 pm
Libera.chat IRC: obiwac
Location: Belgium

Re: Drawing in 32bit colour modes in protected & long mode

Post by obiwac »

Thanks so much!!! :D But I tried to change the grub .cfg file with "set gfxmode = 1024x768x32", but it didn't work though.

EDIT:
I found out that you can't have spaces between the gfxmode and the =. But now GRUB is in the right resolution but my OS does not keep it while booting. I did try gfxpayload=keep, but I can't seem to manage to get it work.
User avatar
obiwac
Member
Member
Posts: 149
Joined: Fri Jan 27, 2017 12:15 pm
Libera.chat IRC: obiwac
Location: Belgium

Re: Drawing in 32bit colour modes in protected & long mode

Post by obiwac »

You can actually do this:

Code: Select all

section .text
	align 4
	
	dd 0x1BADB002
	dd 0x04
	dd -(0x1BADB002 + 0x04)
	
	dd 0 ; skip some flags
	dd 0
	dd 0
	dd 0
	dd 0
	
	dd 0 ; sets it to graphical mode
	dd 800 ; sets the width
	dd 600 ; sets the height
	dd 32 ; sets the bits per pixel
and before calling main function just add

Code: Select all

push ebx
.

And in the grub .cfg you just need to add set

Code: Select all

set gfxmode=800x600x32
.
Plotting the pixels is easy:

- Go to the grub command line (with c)
- Type

Code: Select all

lspci -i
- Under VGA controller, take the 8 last digits of the hexadecimal string, and that's your memory pointer for plotting pixels.

Hope this helped anyone!
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: Drawing in 32bit colour modes in protected & long mode

Post by BrightLight »

obiwac wrote:Plotting the pixels is easy:

- Go to the grub command line (with c)
- Type

Code: Select all

lspci -i
- Under VGA controller, take the 8 last digits of the hexadecimal string, and that's your memory pointer for plotting pixels.

Hope this helped anyone!
This won't help anyone. You should never hard-code the pointer to the physical frame buffer, because it is different on each PC and on most emulators. For example, Bochs and VirtualBox have the frame buffer at 0xE0000000. QEMU has it at 0xFD000000. My laptop has it at 0xD0000000 and my two test PCs at 0xE4000000.
Instead, GRUB passes you a "VBE mode information structure" which is documented in the VBE specs. That structure contains a reliable pointer to the physical frame buffer that would work on all VBE-compatible PCs.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
User avatar
obiwac
Member
Member
Posts: 149
Joined: Fri Jan 27, 2017 12:15 pm
Libera.chat IRC: obiwac
Location: Belgium

Re: Drawing in 32bit colour modes in protected & long mode

Post by obiwac »

Just wanted to complete this post:

http://forum.osdev.org/viewtopic.php?f=1&t=31700
Post Reply