Search code examples
coperating-systemkernelosdev

What is the correct form for a Character struct in a VESA/VGA early kernel console?


I am currently working on a kernel for x86 (just for fun). I am trying to implement a fairly usable early console, to report on the loading of drivers and to allow me to play with memory during boot if I need to, with the 80x25 character VESA/VGA console located at 0xB8000. I would like to do this with a struct representing a character and its attribute byte. However, I am wondering how to correctly format my Character struct. I currently have:

#define CONSOLE_SIZE_X  80  //The length of a line in the console
#define CONSOLE_SIZE_Y  25  //The number of lines in the console
#define CONSOLE_MEMLOC  0xB8000 //The position of the console in system memory

#define ATTR_DEFAULT    0x07 //DOS Default
#define ATTR_ERROR  0x1F //BSOD Colors
#define ATTR_PHOSPHOR   0x02 //"Phosphor" colors, green on black

typedef struct {
    char character = (char) 0x0000;
    char attribute = (char) 0x0000;
} Character; //A VESA VGA character

typedef struct {
    int pos_x = 0;
    int pos_y = 0;

    char defaultAttrib = ATTR_DEFAULT;

    Character buffer[CONSOLE_SIZE_Y][CONSOLE_SIZE_X];


} VESAConsole;

The VESAConsole struct is for logical purposes only (i.e. it does not represent any important set of positions in RAM); its Character buffer[][] will be copied to the actual location of the console by a function cFlushBuffer(Character* console, Character* buffer). This will allow me to implement multiple consoles in early mode for easier debugging on my part (in the manner of screen or tmux).

So, I really have 2 questions: Is my Character struct correct, and are there any gotchas that I need to be aware of when dealing with the early VESA/VGA console?


Solution

  • Firstly yes, your Character struct is correct, assuming no padding.

    And then as to the gotchas, there are two situations:

    • Either you used some known-good code to setup the VGA hardware (e.g. asked the BIOS to do it, asked GRUB to do it, took an example from another OS, ...),
    • Or you did the hardware setup yourself.

    In the first case, you're good to go. There aren't any really evil gotchas once the setup is done correctly. Just write directly to memory and let the hardware handle it.

    In the second, there is an (almost) infinite variety of ways things could go wrong. Graphics hardware is notoriously hard to setup, and although VGA is nothing compared to what modern graphic cards use, it's still far from easy.

    Possible side effects include, but are not limited to, blank screens, crashes, burning and/or exploding CRT monitors, etc ..

    If you're interested in further reading, you may want to take a look at the various resources on the OSDev Wiki.