Page 1 of 4

Writing to the screen without BIOS

Posted: Thu May 27, 2021 10:44 am
by PavelChekov
Babystep4 and 8 show how to write to the screen without BIOS interrupts in 16- and 32-bit real and protected modes, respectively. I am trying to write a 64-bit OS, and I want to avoid using BIOS interrupts as much as I can. Is there a way to write to the screen without BIOS in a 64-bit OS? Is there such thing as 64-bit protected mode?

Thanks

Re: Writing to the screen without BIOS

Posted: Thu May 27, 2021 11:01 am
by Octocontrabass
PavelCheckov wrote:Is there a way to write to the screen without BIOS in a 64-bit OS?
Writing to the VGA text mode screen works the same regardless of CPU mode. I suggest you read more about VGA text mode if you don't understand the babystep code.

UEFI doesn't have VGA text mode, so if you want to support UEFI in the future you'll have to write new code to support a linear frame buffer. (You can also have a linear frame buffer with BIOS.)
PavelCheckov wrote:Is there such thing as 64-bit protected mode?
It's called "long mode".

Re: Writing to the screen without BIOS

Posted: Thu May 27, 2021 11:31 am
by PavelChekov
Octocontrabass wrote:Writing to the VGA text mode screen works the same regardless of CPU mode. I suggest you read more about VGA text mode if you don't understand the babystep code.
The only reason I ask is because it says that the code is for 32-bit protected mode. I was also hoping to use FHD as the default, which doesn't have text mode, or to my knoweledge, any tutorial.
Octocontrabass wrote:UEFI doesn't have VGA text mode, so if you want to support UEFI in the future you'll have to write new code to support a linear frame buffer. (You can also have a linear frame buffer with BIOS.)
I was hoping to write my OS independent of a specific BIOS (e.g. UEFI).

Octocontrabass wrote:It's called "long mode".
How do I enter long mode?

Re: Writing to the screen without BIOS

Posted: Thu May 27, 2021 11:54 am
by Octocontrabass
PavelCheckov wrote:I was also hoping to use FHD as the default, which doesn't have text mode, or to my knoweledge, any tutorial.
So, you want a linear frame buffer. There's quite a bit of information on how to use one of those. Please ignore the page title - it was probably written before long mode existed.
PavelCheckov wrote:I was hoping to write my OS independent of a specific BIOS (e.g. UEFI).
You want a linear frame buffer.
PavelCheckov wrote:How do I enter long mode?
Let the bootloader do it for you.

...But, you can find an overview on the wiki in case your bootloader doesn't already do it. If you search, you'll probably find example code too.

Re: Writing to the screen without BIOS

Posted: Sun Aug 15, 2021 10:34 am
by PavelChekov
All the examples are for VGA, and I am still unclear how to write text to an XGA screen (via linear frame buffer).

Re: Writing to the screen without BIOS

Posted: Sun Aug 15, 2021 1:08 pm
by Octocontrabass
Which examples are you looking at? I provided a link to the wiki page that explains how to draw text to a linear frame buffer. It works the same for all linear frame buffers.

Re: Writing to the screen without BIOS

Posted: Sun Aug 15, 2021 2:14 pm
by PavelChekov
I am looking at the link provided, but it appears to be for VGA and BIOS, however I am programming with XGA and UEFI.

Re: Writing to the screen without BIOS

Posted: Sun Aug 15, 2021 2:25 pm
by Octocontrabass

Re: Writing to the screen without BIOS

Posted: Sun Aug 15, 2021 2:48 pm
by PavelChekov
How would I write a printf function with this?

Re: Writing to the screen without BIOS

Posted: Sun Aug 15, 2021 3:07 pm
by iansjack
You write a function to draw a single character to the screen. From then on, writing printf is exactly the same as if you were using BIOS calls to write characters to the screen.

Re: Writing to the screen without BIOS

Posted: Mon Aug 16, 2021 11:37 am
by bzt
PavelCheckov wrote:How would I write a printf function with this?
To display a character, take a look at the example on PC Screen Font wiki page. You just repeat that code for every character and adjust the cursor position. A very very minimalistic string display example would be:

Code: Select all

// PSF2 header
typedef struct {
    uint32_t magic;
    uint32_t version;
    uint32_t headersize;
    uint32_t flags;
    uint32_t numglyph;
    uint32_t bytesperglyph;
    uint32_t height;
    uint32_t width;
    uint8_t glyphs;
} __attribute__((packed)) psf2_t;
extern volatile unsigned char _binary_font_psf_start;

void puts(char *s)
{
    psf2_t *font = (psf2_t*)&_binary_font_psf_start;
    int x,y,kx=0,line,mask,offs;
    int bpl=(font->width+7)/8;
    while(*s) {
        unsigned char *glyph = (unsigned char*)&_binary_font_psf_start + font->headersize +
            (*s>0&&*s<font->numglyph?*s:0)*font->bytesperglyph;
        offs = (kx * (font->width+1) * 4);
        for(y=0;y<font->height;y++) {
            line=offs; mask=1<<(font->width-1);
            for(x=0;x<font->width;x++) {
                *((uint32_t*)(framebuffer_address+line))=((int)*glyph) & (mask)?0xFFFFFF:0;
                mask>>=1; line+=4;
            }
            *((uint32_t*)((uint64_t)&fb+line))=0; glyph+=bpl; offs+=framebuffer_scanline;
        }
        s++; kx++;
    }
}
As for the arguments, you should implement sprintf, construct the output buffer and pass that to this "puts" function.

You probably want more than this simple example, like handling newline characters, interpret UTF-8, proportional fonts etc. For these, you can find a ready-to-use, dependency free library, Scalable Screen Font. It has a minimalistic routine, comparable to the PC Screen Font example above, see ssfn_putc.

Cheers,
bzt

Re: Writing to the screen without BIOS

Posted: Mon Aug 16, 2021 12:34 pm
by nexos
PavelCheckov wrote:How would I write a printf function with this?
Basically, you write a routine that renders a font to screen (PC Screen Font is fine, just don't copy n' paste blindly, understand the code first before copying). After that, read up on the ISO C standard or POSIX specification about printf and how it works, and then write it.

Or, in the words of Bill Joy:
It's very simple — you read the protocol and write the code

Re: Writing to the screen without BIOS

Posted: Mon Aug 16, 2021 6:01 pm
by PavelChekov
Will this code work, modified for XGA, with GOP, even though it was written for VESA? (From the article on drawing in the linear frame buffer)

Code: Select all

/* example for 320x200 VGA */
void putpixel(int pos_x, int pos_y, unsigned char VGA_COLOR)
{
    unsigned char* location = (unsigned char*)0xA0000 + 320 * pos_y + pos_x;
    *location = VGA_COLOR;
}
(https://wiki.osdev.org/Drawing_In_Protected_Mode)

Re: Writing to the screen without BIOS

Posted: Mon Aug 16, 2021 6:21 pm
by Octocontrabass
That code was written for VGA, not VBE. Code written for VGA will not work with GOP. (GOP does not support indexed color modes.)
Actually, I read your post wrong. If you modify it, then yes, you can make it work. The article already has an example of one way you could modify it:

Code: Select all

static void putpixel(unsigned char* screen, int x,int y, int color) {
    unsigned where = x*pixelwidth + y*pitch;
    screen[where] = color & 255;              // BLUE
    screen[where + 1] = (color >> 8) & 255;   // GREEN
    screen[where + 2] = (color >> 16) & 255;  // RED
}

Re: Writing to the screen without BIOS

Posted: Tue Aug 17, 2021 7:18 am
by PavelChekov
So, that code would work in GOP even thought it was meant for VESA? And truecolor?