VGA: switch back from 320x200 to 80x25 -- font problem

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
deather
Posts: 5
Joined: Wed Jun 25, 2008 8:42 am

VGA: switch back from 320x200 to 80x25 -- font problem

Post by deather »

Hello all,

I am developing a small operating system for the x86 platform, which uses the protected mode (but no paging).
I'm trying to implement a simple VGA driver, without using the BIOS interrupt 10h. I know there is a lot of threads about it, but none of them helped me.

So, my driver is able to switch to mode "13h" (320x200 - 8 bits) by writing correct values to the various VGA registers , it works great, but the problem comes when trying to get back to text mode (i.e. 80x25).

The card seem to switch, but the font is completely wasted on Qemu, and horizontal black lines appears on Bochs and real PC hardware (see the screenshot below). The values I am writing to the VGA registers should be good, as everything is okay if I write them when I am actually in text mode.

I think the switch to graphical mode "erases" the font used by the text mode. I don't know much on how or where the card actually stores the font (the FreeVGA project didn't tell me a lot about it).

I'd like to know is anyone have been able to do it.

Here is a screenshot taken from Bochs, showing what is appearing after a switch-back to text mode from 320x200:

Image

Sorry not being able to finish my echo command, my keyboard driver is a bit broken on Bochs, and it suddenly died. :?

Thanks!
deather
Posts: 5
Joined: Wed Jun 25, 2008 8:42 am

Re: VGA: switch back from 320x200 to 80x25 -- font problem

Post by deather »

OK, the problem is really font-related.
I used a trampoline to real mode in order to use the BIOS int 10h with AH=11h, AL=01h, and BL=0h, in order to reload a font, and it works.

So now, i'm searching how to manually load a font into the VGA memory (plane 2 according to FreeVGA), without using anything from the BIOS.

Google isn't very handy, so if anyone had some links, it would be nice!

Thanks,
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: VGA: switch back from 320x200 to 80x25 -- font problem

Post by Combuster »

Did you notice every fourth line of the font went missing?

It is because of how the VGA stores it, or rather, works. The reason that bochs and qemu are different are because their VGA implementations differ from what a real VGA is supposed to be. VGA Hardware tells you what differences there are.

Nevertheless, the VGA font occupies 256*32 bytes at the start of plane 2. When in text mode, accesses to video memory are routed such that they go to plane 0 (font index) and plane 1 (character index) alternatively. Plane 2 and 3 are left untouched.

In graphics mode, or specifically, "linear" Chain-4 doubleword mode, you write to all the planes for each consecutive address, thus overwriting the font. The reason that only part of the font is missing is that for every write the address is separated in two, a plane index, and a memory offset. Bochs leaves the memory offset as is, writing to every fourth byte (and hence, every fourth row in the font), Qemu incorrectly shifts it, hence overwriting bytes in sequence.

As for the solution, you will need to save the font before you do any other writes to video memory. As a cheap solution, you can make a backup of entire video memory once you changed the video mode, and restore it before changing back. The more clean solution is to change to planar mode (chain-4 disabled), copy the font as it really is, then change to the mode you really want, then on the way back you can go via planar mode and restore the font. The advantage is that this way you have a exact copy of the font which you can use for text rendering in graphics mode.

Example source code: VGA test suite
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
deather
Posts: 5
Joined: Wed Jun 25, 2008 8:42 am

Re: VGA: switch back from 320x200 to 80x25 -- font problem

Post by deather »

Thanks a lot for your reply. You confirmed my thoughts.
I'm going to try saving the entire video memory, even if it is not optimal, it will also allow me to restore the screen as it was before the switch to graphical mode. :)

Thanks again!

Regards,
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: VGA: switch back from 320x200 to 80x25 -- font problem

Post by Dex »

In the enclosed .asm file, you will find a function to set fonts from pmode.
Attachments
vga.asm
(12.48 KiB) Downloaded 227 times
Post Reply