VGA Fonts: Difference between revisions

Fixing some bugs and misinformed errors, general clean up
[unchecked revision][unchecked revision]
(Created page with "== Preface == So you know how to display characters in text mode, and now you want to do it in graphics mode. It's not complicated, but definitely more complex than writing an...")
 
(Fixing some bugs and misinformed errors, general clean up)
Line 1:
== Preface ==
So you know how to display characters in text mode, and now you want to do it in graphics mode. It's not complicated, but definitely more complex than writing and ASCII code at a specific offset in memory. You'll have to do it pixel by pixel.
 
Line 5 ⟶ 4:
 
== Decoding of bitmap fonts ==
How is a character stored in memory? It's quite simple, 0 encodes background, 1 encodes foreground color. EachVGA fonts are always 8 bits wide so that each byte contains exactly one row.
For letter 'A' in the typical 16x8 font it would be (in binary):
<pre>
00000000b byte 0
Line 29 ⟶ 28:
There're several ways. You can have it in a file on your filesystem. You can hardcode it in an array. But sometimes 4k is so much that you cannot afford, and reading a file is not an option (like in a boot loader), in which case you'll have to read the one used by the card (to display text mode characters) from VGA RAM.
=== Store it in an array ===
Easiest way, but increases your code by 4k. There are several sources that provide the entire font in binary or source format so you do not need to manually write it out.
=== Store it in a file ===
Most modular way. You can use different fonts if you like. Downside you'll need a working filesystem implementation.
=== Get fromthe VGAcopy RAMstored viain the VGA BIOS ===
It's a standard BIOS call (you can trust it's exists and bugfree). If you're still in real mode, it's quite easy to use.
<source lang="asm">
;in: es:di=4k buffer
Line 49 ⟶ 48:
mov si, bp
mov cx, 256*16/4
repnzrep movsd
pop ds
</source>
=== Get from VGA RAM directly ===
Maybe you're already in protected mode, so cannot access BIOS functions. In this case you can still get the bitmap by programming VGA registers. Be careful that the VGA always reserves space for 8x32 fonts so you will need to either copy 8k of data or trim off the bottom 16 bytes of each character during the copy:
<source lang="asm">
;in: edi=4k8k buffer
;out: buffer filled with font
;clear even/odd mode
Line 73 ⟶ 72:
;copy charmap
mov esi, 0A0000h
mov cx, 256*1632/4
repnzrep movsd
;restore VGA state to normal operation
mov ax, 0302h
Line 86 ⟶ 85:
out dx, ax
</source>
It worth mentioning that it has to be done _BEFORE_'''before''' you switch to VBEa graphics mode, because the VGA font is wiped from memory during transition and especially in the case of VBE where VGA registers are usually not accessible afterwards.
 
== Set VGA fonts ==
It's only make sense ifIf you're still in text mode and want the VGA card to draw different glyphs, or you want to change back from a graphics mode, you can set the VGA font. It's worthless in graphics mode (because characters displayed by your code there, not by the card), I only wrote this section for completeness. Modifying the font bitmaps in VGA RAM isn't hard if you read carefully what's written so far. I'll left it to you as a homework.
 
=== Set fonts via BIOS ===
Hint: check Ralph Brown Interrupt list Int 10/AX=1110h.
 
=== Set fonts directly ===
Hint: use the same code as above, but swap source and destination for "repnzrep movsd".
 
== PrefaceSee Also ==
* [[VGA Hardware]]
 
[[Category:VGA]] [[Category:Video]]
1,490

edits