video programming

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
sancho1980
Member
Member
Posts: 199
Joined: Fri Jul 13, 2007 6:37 am
Location: Stuttgart/Germany
Contact:

video programming

Post by sancho1980 »

hi i would like to write a screen driver (text mode)..is there a comprehensive list on video ports and how to program them (page-switching, cursor positioning, font-loading, etc?)..i find the information i get by googling rather confusing :-(
enrico_granata
Posts: 21
Joined: Tue Jul 24, 2007 1:19 am

Post by enrico_granata »

Brandon's tutorial is pretty good on this. I would send you my own code but it's not trivial because I adapted that to work through a SYSCALL like interface
Computer science: all about things that "should" work
sancho1980
Member
Member
Posts: 199
Joined: Fri Jul 13, 2007 6:37 am
Location: Stuttgart/Germany
Contact:

Post by sancho1980 »

ummm this is not really what im looking for...what i would like to do is write a function "print" which prints a string to the screen at the current cursor position, starting with the top of video memory..when we've arrived at the bottom of the screen, the start of the video memory should be shifted up by one line, thus achieving scrolling..this should be repeated until were at the end of the video ram, at which point there should be a wraparound ...i dont find the information, how to change the base of the displayed memory region...it would also be funny to define my own fonts and load them..i have seen this before but dont know how to go about...im also getting confused with all the different video standards..when the pc is turnt on, what state is the video adapter supposed to be in? which mode? is that cga, vga? there is a lot of info on this on the net, i know, but its all a bit unorderly with a little bit of it here and the other half there, which makes me rather confused
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

The PC is originally in 'text-mode' when it gets turned on. This allows easy rendering of text (console apps etc) using the default video font. See Bran's kernel development tutorial.

You can switch the monitor to VGA mode. That gives you per-pixel framebuffer access, which means you can create your own fonts, etc etc.
Check the wiki for this.

JamesM
enrico_granata
Posts: 21
Joined: Tue Jul 24, 2007 1:19 am

Post by enrico_granata »

this is a part of the video driver (mostly based on Bran's tutorial's one, I made the functions static and added _impl to their names so I could wrap this. there are other functions in my code but I omitted those)

Code: Select all

/* These define our textpointer, our background and foreground
*  colors (attributes), and x and y cursor coordinates */
unsigned short *textmemptr;
int attrib = 0x0F;
int csr_x = 0, csr_y = 0;

/* Scrolls the screen */
static void scroll_impl(void)
{
    unsigned blank, temp;

    /* A blank is defined as a space... we need to give it
    *  backcolor too */
    blank = 0x20 | (attrib << 8);

    /* Row 25 is the end, this means we need to scroll up */
    if(csr_y >= 25)
    {
        /* Move the current text chunk that makes up the screen
        *  back in the buffer by a line */
        temp = csr_y - 25 + 1;
        memcpy (textmemptr, textmemptr + temp * 80, (25 - temp) * 80 * 2);

        /* Finally, we set the chunk of memory that occupies
        *  the last line of text to our 'blank' character */
        memsetw (textmemptr + (25 - temp) * 80, blank, 80);
        csr_y = 25 - 1;
    }
}

/* Updates the hardware cursor: the little blinking line
*  on the screen under the last character pressed! */
static void move_csr_impl(void)
{
    unsigned temp;

    /* The equation for finding the index in a linear
    *  chunk of memory can be represented by:
    *  Index = [(y * width) + x] */
    temp = csr_y * 80 + csr_x;

    /* This sends a command to indicies 14 and 15 in the
    *  CRT Control Register of the VGA controller. These
    *  are the high and low bytes of the index that show
    *  where the hardware cursor is to be 'blinking'. To
    *  learn more, you should look up some VGA specific
    *  programming documents. A great start to graphics:
    *  http://www.brackeen.com/home/vga */
    outportb(0x3D4, 14);
    outportb(0x3D5, temp >> 8);
    outportb(0x3D4, 15);
    outportb(0x3D5, temp);
}

/* Clears the screen */
static void cls_impl()
{
    unsigned blank;
    int i;

    /* Again, we need the 'short' that will be used to
    *  represent a space with color */
    blank = 0x20 | (attrib << 8);

    /* Sets the entire screen to spaces in our current
    *  color */
    for(i = 0; i < 25; i++)
        memsetw (textmemptr + i * 80, blank, 80);

    /* Update out virtual cursor, and then move the
    *  hardware cursor */
    csr_x = 0;
    csr_y = 0;
    move_csr_impl();
}

/* Puts a single character on the screen */
static void putch_impl(char c)
{
    unsigned short *where;
    unsigned att = attrib << 8;

    /* Handle a backspace, by moving the cursor back one space */
    if(c == 0x08)
    {
        if(csr_x != 0) csr_x--;
    }
    /* Handles a tab by incrementing the cursor's x, but only
    *  to a point that will make it divisible by 8 */
    else if(c == 0x09)
    {
        csr_x = (csr_x + 8) & ~(8 - 1);
    }
    /* Handles a 'Carriage Return', which simply brings the
    *  cursor back to the margin */
    else if(c == '\r')
    {
        csr_x = 0;
    }
    /* We handle our newlines the way DOS and the BIOS do: we
    *  treat it as if a 'CR' was also there, so we bring the
    *  cursor to the margin and we increment the 'y' value */
    else if(c == '\n')
    {
        csr_x = 0;
        csr_y++;
    }
    /* Any character greater than and including a space, is a
    *  printable character. The equation for finding the index
    *  in a linear chunk of memory can be represented by:
    *  Index = [(y * width) + x] */
    else if(c >= ' ')
    {
        where = textmemptr + (csr_y * 80 + csr_x);
        *where = c | att;	/* Character AND attributes: color */
        csr_x++;
    }

    /* If the cursor has reached the edge of the screen's width, we
    *  insert a new line in there */
    if(csr_x >= 80)
    {
        csr_x = 0;
        csr_y++;
    }

    /* Scroll the screen if needed, and finally move the cursor */
    scroll_impl();
    move_csr_impl();
}

static void set_textattribute_impl(int att)
{
	attrib = att;
}
you should set

Code: Select all

textmemptr = (unsigned short *)0xB8000;
and be on your way. as for printing a string

Code: Select all

void print_string(char *str)
{
 while(*str)
   putch_impl(*str++);
}
Bran's tutorial itself has more on colors
Computer science: all about things that "should" work
sancho1980
Member
Member
Posts: 199
Joined: Fri Jul 13, 2007 6:37 am
Location: Stuttgart/Germany
Contact:

Post by sancho1980 »

JamesM wrote:The PC is originally in 'text-mode' when it gets turned on.
Yes, but as far as I can see there are several different kinds of text modes the graphics adapter has on offer and this is where I'm getting so confused: Which of all these is the one you're in on power-on? (cga,ega...)Which one should I choose to be in for my purposes (i.e. which is the one "normally used" in text UI's)?
JamesM wrote:You can switch the monitor to VGA mode. That gives you per-pixel framebuffer access, which means you can create your own fonts, etc etc.
But there is a way to define your own fonts even in text mode by storing your font somewhere in memory and have the "character engine" somehow point to your custom font rather than to the one stored in the BIOS, I *know that there is a way, only I cant find the necessary hardware doc on that...I want to do that...no reason, just because..curious...
enrico_granata wrote:this is a part of the video driver
without having taken the time to look at this in detail: does this make use of the full range of the video memory,i.e. does it span all 8 available pages when scrolling or does this wrap around after one page? and really, my question wasnt so much a programming question but rather asking for where to find a full reference on text mode programming, i.e. something in the way:

-if you want to switch to this video mode, store this byte at that port
-if you want to switch to video page 3, store this byte there
-if you want to move the cursor from here to there...etc...

I would rather like to put the pieces together myself...
enrico_granata
Posts: 21
Joined: Tue Jul 24, 2007 1:19 am

Post by enrico_granata »

mode is 80x25 (at least that's what GRUB puts you in unless otherwise instructed). if you need the BIOS id for that mode (though I can't see why you need that in pmode) any good BIOS reference should suffice

don't know how you change fonts that in text mode (though I guess there maybe some way, unless the fonts are stored in the video card)

the example code only uses 1 page, and then scrolls via software

I think you need a VGA reference
Computer science: all about things that "should" work
Post Reply