Page 1 of 2

Taking color mode for granted?

Posted: Fri Jan 18, 2013 12:03 pm
by CocaCola
I have seen that two OS tutorials (Friesen's and Molloy's), as well as OSDev Wiki pages, assume that color mode is available and the correct address to write to is 0xB8000.

Questions:
  1. Shouldn't the programmer check that the color mode is available?
  2. If yes, does the GRUB structure help, or must the VGA card be asked instead?
I realize that my questions are noobish, so I will happily accept RTFM replies: as long as a link is included.
Thank you for reading.

Re: Taking color mode for granted?

Posted: Fri Jan 18, 2013 12:21 pm
by Brendan
Hi,
CocaCola wrote:I have seen that two OS tutorials (Friesen's and Molloy's), as well as OSDev Wiki pages, assume that color mode is available and the correct address to write to is 0xB8000.
These are tutorials, and any example code is only intended to illustrate concepts in a concise/simple manner that can be easily understood by people that need a tutorial. If the tutorial tried to do things like a real OS would, then it'd be too complicated for people trying to learn. Basically; the example code in any decent tutorial is not intended for an actual OS and is not useful for an actual OS.

For a real OS (rather than a tutorial) you wouldn't bother with crappy text modes - you'd use VBE to setup a high resolution graphics mode, implement a font renderer, etc.


Cheers,

Brendan

Re: Taking color mode for granted?

Posted: Fri Jan 18, 2013 12:34 pm
by linguofreak
Also, I'm willing to bet that any computers that only have MDA text mode are old enough that most modern OS's won't work on them anyways. 20 or 30 years ago you would have had to check, now I'd say you can safely decide not to support MDA text mode.

Re: Taking color mode for granted?

Posted: Fri Jan 18, 2013 1:42 pm
by egos
CocaCola, I use following steps during video initialization in the kernel:
1) in RM, check VGA BIOS, find video card, set mode 3 using VGA BIOS;
2) in PM, reset similar mode using built-in driver.

VGA hardware allows to use other address of video buffer than 0xB8000. For example, I use all 128 Kb starting at 0xA0000.

Brendan, I'm still using text modes in my OS and I'm satisfied!

linguofreak, I removed EGA support from my kernel few years ago, so only VGA compatible card is held good in requirements. MDA was never used.

Re: Taking color mode for granted?

Posted: Fri Jan 18, 2013 6:48 pm
by linguofreak
egos wrote:linguofreak, I removed EGA support from my kernel few years ago, so only VGA compatible card is held good in requirements. MDA was never used.
I'd frankly be surprised if MDA has been supported in any new operating system since 1990 or so. Support may have been maintained past 1990 in a few old OSes like DOS simply because it was already there and no need was seen to remove it, but I could be talking out of my rear.

Re: Taking color mode for granted?

Posted: Sat Jan 19, 2013 3:30 am
by egos
Yes, it is normal. It is interesting what TS meant when he asked about "color mode".

Re: Taking color mode for granted?

Posted: Sun Jan 20, 2013 3:43 am
by turdus
You can use BDA Equipment word [at 0:0x410] bits 5-4 to detect monochrome output (usually at 0xB0000), and use 0xB8000 otherwise.
But it's better to ask the VGA card to tell where it's framebuffer is. This code will return it in edi.

Code: Select all

		mov			dx, 03ceh
		mov			al, 6
		out			dx, al
		inc			dx
		in			 al, dx
		shr			al, 2
		and			al, 3
		mov			edi, 0A0000h
		cmp			al, 2
		jl			 @f
		mov			edi, 0B0000h
		cmp			al, 2
		je			 @f
		mov			edi, 0B8000h
@@:

Re: Taking color mode for granted?

Posted: Sun Jan 20, 2013 5:15 am
by Gigasoft
Your VGA card will always implement the same set of features no matter what type of monitor is connected, so you don't really have to care. The BIOS won't map VRAM at B0000h just because you have a monochrome monitor, either. The only difference it makes is that the palette might be initialized with grays. However, if the current video mode is anything other than what you want, it's probably best to initialize the card to a known state.

Re: Taking color mode for granted?

Posted: Fri Jan 25, 2013 4:49 pm
by CocaCola
Thank you for your suggestions, however I don't want to go back into Real Mode to do this.
I'm currently relying on GRUB to boot my kernel into Protected Mode.
Gigasoft wrote:Your VGA card will always implement the same set of features no matter what type of monitor is connected, so you don't really have to care. The BIOS won't map VRAM at B0000h just because you have a monochrome monitor, either. The only difference it makes is that the palette might be initialized with grays. However, if the current video mode is anything other than what you want, it's probably best to initialize the card to a known state.
So the conclusion is that I should check if the video mode is what I want?
Otherwise by saying that I "don't really have to care", you are implying that there must be some safe defaults, correct?
linguofreak wrote:Also, I'm willing to bet that any computers that only have MDA text mode are old enough that most modern OS's won't work on them anyways. 20 or 30 years ago you would have had to check, now I'd say you can safely decide not to support MDA text mode.
Good point, although I'd rather support MDA text mode just to be able to print a silly "Upgrade hardware" error message...

... and with that goal, by reading through the FreeVGA manual, I think I should:
  1. check Misc Output register, I/O AS (for setting up CRTC and Input Status addresses)
  2. check Graphics registers (Misc Graphics register), memory map select
And then I should somehow check if the mode is 80x25 characters, but I don't know how.

Please tell me if whatever I'm doing is stupid, and if so, why? Thanks!

Re: Taking color mode for granted?

Posted: Fri Jan 25, 2013 5:47 pm
by Combuster
Why would you check the video cards configuration in the first place when you'll be changing everything that doesn't read what you want?

For the paranoid, the steps would be roughly as follows:
- Check if a video card exists (if it's not a VGA, this is the next likely option) and stop if it isn't
- Check if it's a VGA card and stop if it isn't (preferably without probing)
- Call the bios to fix the video mode (a video card in an SVGA mode might not respond as a VGA).
- Check if all the documented VGA registers now have their standard values and stop if it isn't (it'd be broken in either hardware or software, or it was lying about being a VGA)

Or if you're practical you can just assume you have a VGA and be left with an equally unusable system for the sad people that don't follow system requirements. :wink:

In the end, what matters is the time you want to invest to what end - after all, the majority of OS developers will never encounter the case where assuming a VGA on a 386+ PC is wrong.

Re: Taking color mode for granted?

Posted: Sat Jan 26, 2013 7:22 am
by Gigasoft
Otherwise by saying that I "don't really have to care", you are implying that there must be some safe defaults, correct?
I'm saying that the type of monitor that is connected won't change anything. But some BIOSes might set up the card in a graphics mode when they boot, so you should perform a check anyway.
And then I should somehow check if the mode is 80x25 characters, but I don't know how.
Check if the byte at 449h equals 2 or 3. To be sure about the number of rows, you can also check that the byte at 484h equals 24.

Checking the Misc Output and Misc Graphics registers is just silly. You can't connect a VGA card to an MDA monitor. Even if you could, through a converter, there is no way for the BIOS to know. A BIOS will never ever place a VGA card in MDA mode when the computer boots, as it's completely pointless. The only reason that MDA mode exists is for the unlikely event that someone would need to run an application written before most of us were born.

Re: Taking color mode for granted?

Posted: Tue Feb 05, 2013 5:45 am
by tom9876543
linguofreak wrote:Also, I'm willing to bet that any computers that only have MDA text mode are old enough that most modern OS's won't work on them anyways. 20 or 30 years ago you would have had to check, now I'd say you can safely decide not to support MDA text mode.
Why the hell would anyone even bother discussing MDA graphics mode.
You may as well build your OS for an 8086 CPU and 8 Inch floppy drives.

If your bootloader is loading via BIOS then you can safely assume the computer has a VGA graphics card.
If you are using GRUB then read the GRUB documentation, I am assuming it explains what to do.
Its not complicated. If you can't figure this stuff out good luck with the rest of your OS.

Re: Taking color mode for granted?

Posted: Tue Feb 05, 2013 10:36 am
by rdos
Why would anybody bother with text-mode? The simple answer is that this is the mode BIOS sets during boot, and thus it is the best mode to debug new hardware in. If you use VBE to switch to some video-mode, this might not work because of a buggy VBE interface, and then you have missed one opportunity to try to get this PC working with you OS. Debugging why a PC doesn't boot (and ending up with a corrupt display) is not a hit. Its much easier to debug this from the shell, or remotely while running a graphics application. Remote debug requires some other functional hardware (often an ethernet card). If the ethernet card also doesn't work (or is unsupported), you are out of luck if your screen is a mess.

As for the address, 0xB0000 was used in two-display configurations a long time ago. Nowadays I think it is safe to assume that 0xB8000 is the text-mode buffer.

Re: Taking color mode for granted?

Posted: Tue Feb 05, 2013 10:42 am
by rdos
linguofreak wrote:
egos wrote:linguofreak, I removed EGA support from my kernel few years ago, so only VGA compatible card is held good in requirements. MDA was never used.
I'd frankly be surprised if MDA has been supported in any new operating system since 1990 or so. Support may have been maintained past 1990 in a few old OSes like DOS simply because it was already there and no need was seen to remove it, but I could be talking out of my rear.
I have no plans whatsoever to stop supporting MDA (text mode). If I encounter some PC in the future that doesn't support it, I'll put an emulator in place to emulate the behavior using a supported graphics mode. Since I also have no problems with dynamic VBE changes even with long mode (since the mode switch can run in protected mode), that is not something that will make me stop supporting MDA and text mode either.

Re: Taking color mode for granted?

Posted: Tue Feb 05, 2013 11:55 am
by Antti
rdos wrote:I have no plans whatsoever to stop supporting MDA (text mode).
You probably know what you are talking about. However, can I ask a question? If I understood correctly, MDA was the "original" video card of IBM PC. Then there were CGA/EGA/VGA. I guess the term MDA is not relevant anymore (in any context) for modern OS developers. Is it even possible to have an 80386-based computer with MDA? Your OS surely needs an 80368 CPU or newer? Did you mean that VGA compatible cards support the MDA mode (e.g. "BIOS video mode 7h") and you support that in addition to a VGA text mode? In short: what you meant by supporting MDA?

I just want to make clear to myself that MDA and text-modes (in general) are not the same thing.