Page 1 of 1

Cursor code

Posted: Sun Jan 12, 2003 4:31 pm
by FireDemon
//Devices.h
//System
__inline__ unsigned inportb(unsigned port)
{
   unsigned ret_val;

   __asm__ __volatile__("inb %w1,%b0"
      : "=a"(ret_val)
      : "d"(port));

   return ret_val;
}

__inline__ void outportb(unsigned port, unsigned val)
{
   __asm__ __volatile__("outb %b0,%w1"
      :
      : "a"(val), "d"(port));
}

//Cursor.h
class Cursor
{
public:
Cursor()
{
y=0;
x=0;
}

~Cursor()
{
}

void resetcursor()
{
positioncursor(0,0);
}

void positioncursor(int pcx,int pcy)
{
// cursor LOW port to vga INDEX register
outportb( 0x3D4, 0x0F );
outportb( 0x3D5, ( unsigned char ) ( ( pcy * 80 + pcx ) & 0xFF ) );

// cursor HIGH port to vga INDEX register
outportb( 0x3D4, 0x0E );
    outportb( 0x3D5, ( unsigned char ) ( ( pcy * 80 + pcx ) & 0xFF ) );

    x = x;
y = y;
}

unsigned int getx()
{
return x;
}

unsigned int gety()
{
return y;
}

private:
unsigned int y;
unsigned int x;
}; //don't forget the semicolon!

Does not work for some reason

Re:Cursor code

Posted: Sun Jan 12, 2003 4:35 pm
by jrfritz
Here is my poscursor...it's function is called gotoxy():
// data:
int cur_cursorX, cur_cursorY;            // this is for keeping track of the text mode cursor
static unsigned short *vgaadr;            // the VGA address
static unsigned textattrib, scrwidth, scrheight;   // the text color, the screen width, & height
// use this function to set up all the variables needed. call this once and it is needed before you use gotoxy.
void start_textmode()
{
   cur_cursorX = 0;
   cur_cursorY = 0;

   gotoxy( 0, 0 );

   vgaadr = ( unsigned short * ) 0xb8000;

   textattrib = WHITE_BLACK;      // white on black
   scrwidth = 80;
   scrheight = 25;
}
void gotoxy( int x, int y )
{
USHORT   position = ( y * scrwidth + x );

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

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

   cur_cursorX = x;
   cur_cursorY = y;
}

Re:Cursor code

Posted: Sun Jan 12, 2003 5:00 pm
by df
FireDemon wrote: void positioncursor(int pcx,int pcy)
{
// cursor LOW port to vga INDEX register
outportb( 0x3D4, 0x0F );
outportb( 0x3D5, ( unsigned char ) ( ( pcy * 80 + pcx ) & 0xFF ) );

// cursor HIGH port to vga INDEX register
outportb( 0x3D4, 0x0E );
outportb( 0x3D5, ( unsigned char ) ( ( pcy * 80 + pcx ) & 0xFF ) );

x = x;
y = y;
}
dont forget to add the video memory address to the value!
so its + 0xB8000

this is how I do it;

Code: Select all

void UpdateCursor(text_screen *vc)
{
   int   xx=(vc->intCurrentRow * vc->intMaximumColumn)+vc->intCurrentColumn;
   xx+=(int)(vc->intVideoMemoryBuffer)>>1;

   out_byte(TEXT_INDEX, TEXT_CURSOR_LO);   /* access lo cursor data reg */
   out_byte(TEXT_DATA, xx&0xFF);
   out_byte(TEXT_INDEX, TEXT_CURSOR_HI);   /* access hi cursor data reg */
   out_byte(TEXT_DATA, (xx>>8)&0xFF);
}
you can see I do a + video memory then divide by 2, to get the proper address

Re:Cursor code

Posted: Sun Jan 12, 2003 5:09 pm
by FireDemon
Thanks, I forgot to shift the code. I used the FritzOS code.
Using the above code,how could I change it to get the mouse position.

Re:Cursor code

Posted: Sun Jan 12, 2003 5:10 pm
by jrfritz
Don't you mean the text cursor pos? If so...I have code...

Re:Cursor code

Posted: Sun Jan 12, 2003 5:20 pm
by FireDemon
I got the text position already, I need to know how to get data from a mouse

Re:Cursor code

Posted: Sun Jan 12, 2003 5:24 pm
by nullify
You need to write mouse driver code then, e.g., to track the mouse position.

Re:Cursor code

Posted: Sun Jan 12, 2003 5:27 pm
by FireDemon
Can someone show me a mouse function to get the x and y pos

Re:Cursor code

Posted: Mon Jan 13, 2003 1:25 pm
by Tim
You're welcome to study my PS/2 mouse driver.

Re:Cursor code

Posted: Mon Jan 13, 2003 6:08 pm
by FireDemon
Man, can it get anymore complex then this.
Maybe I will do the mouse last.

Re:Cursor code

Posted: Mon Jan 13, 2003 6:20 pm
by Tim
Yes, I think that's a wise idea.

Re:Cursor code

Posted: Tue Jan 14, 2003 3:14 am
by DarylD
As I always say, leave things like the keyboard and mouse until later on when you have a scheduler/memory manager etc in place.

What is the use of reading in keyboard presses or mouse movements when you have no OS kernel to have actions acted upon?!!

To be honest, I have a working keyboard driver but it will be no use until I have an elf loader which needs an IO system/cache manager which in my case means I will have a good chunk of the kernel done before I even process my first keystroke.

Daryl.

Re:Cursor code

Posted: Tue Jan 14, 2003 5:59 pm
by FireDemon
I am really not making an OS.
It is a game that will work on all OS systems.

Re:Cursor code

Posted: Tue Jan 14, 2003 6:07 pm
by jrfritz
That would not always work...some OSes don't allow ports to be read like that...

Re:Cursor code

Posted: Wed Jan 15, 2003 1:57 am
by Pype.Clicker
Solar Ray wrote: That would not always work...some OSes don't allow ports to be read like that...
The only OS i know that allow such kind of direct hardware access is MS-DOS and clones.