VESA high res linear mode example
Posted: Wed Mar 15, 2006 4:53 pm
It has been discussed numerous times here how to work with higher resolution graphics. Since I wanted to do some tests I thought I'd write a small sample.
The idea would be to set a suitable graphics mode (through VBE [VESA BIOS Extensions]) and then use that through the linear frame buffer once you are in protected / long-mode etc.
The sample is exactly 512 bytes long (so you can stick it on a bootable floppy) and does the following:
- check for VBE 2.0 or higher
- find a suitable mode (graphics 800 x 600 in either 24 or 32-bits, [direct]color, linear framebuffer, available)
- switch to that mode
- enable A20 and unreal mode to be able to access the frame buffer
- draw a gradient like background
- draw the graphics card manufacturer string on top (with alpha blending) of this background
This should illustrate various things (like font writing, using a builtin 8x8 ROM font) and is hopefully of some use.
There are some restrictions. I've built it with MASM (Microsoft) and it probably won't compile with NASM/FASM etc. Perhaps someone else is willing too look into that. You will also need at least a 486 with a fast A20 enable option.
And of course a VBE 2.0 compliant video card with the proper video mode. For some reason I could not get it to run in QEMU, but it has been verified to run under Bochs, Virtual PC and three actual computers with various graphics cards.
The archive includes the full source (free to use), ISO and floppy image, build options (if you have MASM) and mkisofs to create an ISO file from the floppy image.
Download the whole archive here:
www.visuar.com/projects/VBETest.zip
I've also put two screenshots up anyone just wants to see the end result:
www.visuar.com/projects/VBEBochs.png (Bochs, 24-bit)
www.visuar.com/projects/VBEVirtualPC.png (MS Virtual PC, 32-bit)
This code is far from optimal in performance and perhaps in size (especially since it uses a lot of 32-bit instructions/registers in 16-bit code). However, I think it is quite clear and pretty well documented.
If someone is going to work with VBE do get the latest specification and read it *thoroughly*! There are a lot of minor things to get right. Don't worry, there are still lots of things unclear (that's why the test). Especially on how to write the 24 and 32-bit data (see my code).
Get the version 3 spec here (PDF):
http://www.vesa.org/public/VBE/vbe3.pdf
I hope this is useful to someone and please let me know what everyone thinks!
p.s. in case you are wondering, a lot of optimization can be done in real protected mode or long-mode (this would work great with the 64-bit and extra(!) registers). Pentium U & V pipe optimization, sse / mmx, lookup tables, converting the font bit-table to a true color alpha mask only once, etc. Do keep in mind that if you gonna de optimization you only do it if it is really needed and you PROFILE to make sure it improves!
p.p.s. you definitely want to change at least two things in this source. Get rid of the 64 conditional jumps per character and use at least double buffering (don't draw directly to video memory, except to copy your prepared page [in one fell swoop with mmx / sse / dma])
The idea would be to set a suitable graphics mode (through VBE [VESA BIOS Extensions]) and then use that through the linear frame buffer once you are in protected / long-mode etc.
The sample is exactly 512 bytes long (so you can stick it on a bootable floppy) and does the following:
- check for VBE 2.0 or higher
- find a suitable mode (graphics 800 x 600 in either 24 or 32-bits, [direct]color, linear framebuffer, available)
- switch to that mode
- enable A20 and unreal mode to be able to access the frame buffer
- draw a gradient like background
- draw the graphics card manufacturer string on top (with alpha blending) of this background
This should illustrate various things (like font writing, using a builtin 8x8 ROM font) and is hopefully of some use.
There are some restrictions. I've built it with MASM (Microsoft) and it probably won't compile with NASM/FASM etc. Perhaps someone else is willing too look into that. You will also need at least a 486 with a fast A20 enable option.
And of course a VBE 2.0 compliant video card with the proper video mode. For some reason I could not get it to run in QEMU, but it has been verified to run under Bochs, Virtual PC and three actual computers with various graphics cards.
The archive includes the full source (free to use), ISO and floppy image, build options (if you have MASM) and mkisofs to create an ISO file from the floppy image.
Download the whole archive here:
www.visuar.com/projects/VBETest.zip
I've also put two screenshots up anyone just wants to see the end result:
www.visuar.com/projects/VBEBochs.png (Bochs, 24-bit)
www.visuar.com/projects/VBEVirtualPC.png (MS Virtual PC, 32-bit)
This code is far from optimal in performance and perhaps in size (especially since it uses a lot of 32-bit instructions/registers in 16-bit code). However, I think it is quite clear and pretty well documented.
If someone is going to work with VBE do get the latest specification and read it *thoroughly*! There are a lot of minor things to get right. Don't worry, there are still lots of things unclear (that's why the test). Especially on how to write the 24 and 32-bit data (see my code).
Get the version 3 spec here (PDF):
http://www.vesa.org/public/VBE/vbe3.pdf
I hope this is useful to someone and please let me know what everyone thinks!
p.s. in case you are wondering, a lot of optimization can be done in real protected mode or long-mode (this would work great with the 64-bit and extra(!) registers). Pentium U & V pipe optimization, sse / mmx, lookup tables, converting the font bit-table to a true color alpha mask only once, etc. Do keep in mind that if you gonna de optimization you only do it if it is really needed and you PROFILE to make sure it improves!
p.p.s. you definitely want to change at least two things in this source. Get rid of the 64 conditional jumps per character and use at least double buffering (don't draw directly to video memory, except to copy your prepared page [in one fell swoop with mmx / sse / dma])