Video Cursor

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
Peter

Video Cursor

Post by Peter »

Hi people,

I wrote a simple function to clear the screen:

Code: Select all

void clrscr()
{
    unsigned char *vidmem = (unsigned char *)0xB8000;   // Video memory
   for (yPos; yPos<ROWS; yPos++) {
    for(xPos = 0; xPos<COLLUMS; xPos++)
    {
      *(vidmem + (xPos + yPos * COLLUMS) * 2) = 0;
      *(vidmem + (xPos + yPos * COLLUMS) * 2 + 1) = 0xF;
     }
   }
   yPos = 0;
   xPos = 0;
   
}
When I run the function the cursor is blinking in the middle of the screen. Why does the cursor stand on that place? and how can I hide the cursor or set it to point 0.0.

by the way: 80*25 resolution

Thanks :D

Peter
zyp

Re:Video Cursor

Post by zyp »

Use this snippet to put the cursor at 0,0:

Code: Select all

outb(0x3d4, 0x0e);
outb(0x3d5, 0);
outb(0x3d4, 0x0f);
outb(0x3d5, 0);
kofrad

Re:Video Cursor

Post by kofrad »

zyp wrote: Use this snippet to put the cursor at 0,0:

Code: Select all

outb(0x3d4, 0x0e);
outb(0x3d5, 0);
outb(0x3d4, 0x0f);
outb(0x3d5, 0);
I am going to use this code for my setcursorpos() function, but which one is the x coordinate and which is y? I think the first is the y coordinate because of something my old programming teacher said :P (havent done much vga programming as of yet and my kernel isnt far enough advanced that I can test the function out myself to see)
zyp

Re:Video Cursor

Post by zyp »

Neither is, it's the lower and higher byte of the linear position.

Code: Select all

unsigned short pos = x + y * 80;
outb(0x3d4, 0x0e);
outb(0x3d5, pos >> 8);
outb(0x3d4, 0x0f);
outb(0x3d5, pos & 0xff);
Peter

Re:Video Cursor

Post by Peter »

Thanks for the fast reactions! :)

I've got a question about the cursor. When I execute this piece of code, the cursor is set in the middle of the screen. But if you write to 0xB8000, the cursor is not moved, sn't it?

Code: Select all

int yPos = 0, xPos = 0;

void clrscr()
{
    unsigned char *vidmem = (unsigned char *)0xB8000;
   for (yPos; yPos<ROWS; yPos++) {
    for(xPos = 0; xPos<COLLUMS; xPos++)
    {
      *(vidmem + (xPos + yPos * COLLUMS) * 2) = 0;
      *(vidmem + (xPos + yPos * COLLUMS) * 2 + 1) = 0xF;
     }
   }
   yPos = 0;
   xPos = 0;

}
Peter
kofrad

Re:Video Cursor

Post by kofrad »

Hmmm, strange. I modified my code and will test once I can, which is hopefully soon, I think I'm pretty close to getting my print() function working.
zyp

Re:Video Cursor

Post by zyp »

You have to move the cursor yourself, it's not done automatically when writing to the screen buffer.
kofrad

Re:Video Cursor

Post by kofrad »

zyp wrote: You have to move the cursor yourself, it's not done automatically when writing to the screen buffer.
Yeah I know, I have that in my print function already, but I wasnt sure how to move it unless I wrote to the screen, so I wanted a standalone solution.
Peter

Re:Video Cursor

Post by Peter »

I think I got it!
Even when I not run a clrscr or printf function the cursor stand on that place. It is a standard place I think.
Thanks for your help
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:Video Cursor

Post by Colonel Kernel »

If you're using GRUB, the cursor is probably at the position where GRUB finished outputting text before booting your kernel. That's what happens to me, anyway...
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Video Cursor

Post by distantvoices »

btw, I'd only move the cursor upon user input at each keystroke, else I'd move it straight to the end of the recently output line (after all the output is done)

Saves some time. ;-)
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
viral

Re:Video Cursor

Post by viral »

Code: Select all

void  setcursor(byte i, byte j)
{
       unsigned short position = (j * width + i);

        // cursor LOW port to vga INDEX register
        outportb(0x3D4, 0x0F);
        outportb(0x3D5, (unsigned char)(position & 0xFF));

        // cursor HIGH port to vga INDEX register
        outportb(0x3D4, 0x0E);
       outportb(0x3D5, (unsigned char)((position >> 8) & 0xFF));
}


This will move your hardware cursor at desired X,Y position.
cabron

Re:Video Cursor

Post by cabron »

One thing that I just figured out, I'm sure it's obvious to some (most?), but the cursor will be displayed in whatever color the framebuffer has for that particular character...

I was clearing my screen with memset( buf, 0, 80*25*2 ) and wondering why my cursor had disappeared... :P
Post Reply