Page 1 of 1
How do I get the cursor position?
Posted: Fri Jan 21, 2005 5:47 pm
by kinghajj
I know how to set it using the hardware ports, but I don't know how to get it's position. Can anyone give me a link to someplace that'll explain it, or have code for it?
Re:How do I get the cursor position?
Posted: Sat Jan 22, 2005 5:05 am
by XStream
Read the Cursor Location High/Low Indexes (0xE/0xF) of the CRTC Register which when combined to form a word will give you the location in characters of the cursor.
For example, setting the cursor to row 17 and column 2 you would get the word 0x0552, which is row * 80 + column, so to determine the location just do this:
Code: Select all
assume ax equals the location
xor dx, dx
div ax, 80
ax = row
dx = column
Now, seeing as though I am just starting to learn how to program the VGA registers, if you could show me how to set the cursor position that would be good.
Cheers.
Re:How do I get the cursor position?
Posted: Sat Jan 22, 2005 7:01 am
by AR
Changing cursor position is in the Wiki >
http://www.osdev.org/osfaq2/index.php/H ... 20print%3F
A general, and poorly explained, run down of the VGA Ports/Registers is here at bonafide:
http://www.osdever.net/documents/vga_po ... ?the_id=26
I found this page with a quick google:
http://www.osdever.net/tutorials/brunma ... ?the_id=65
Relevant part being:
Code: Select all
unsigned short offset;
out(0x3D4, 14);
offset = in(0x3D5) << 8;
out(0x3D4, 15);
offset |= in(0x3D5);
offset now contains the position of the cursor (X * Y). You'll need to divide by 80 to get Y and get the modulus of 80 for X.
As a side note, it is also recommended that you keep track of the position internally and don't continually re-read it as it is supposedly quite slow.
Re:How do I get the cursor position?
Posted: Sat Jan 22, 2005 7:54 am
by XStream
Thanks for that link to the Wiki, I really need to remember about that resource.
I did actually assume that writing the same indexes would set the location of the cursor but it never changed, oh well I must have done something wrong.
Cheers.
Re:How do I get the cursor position?
Posted: Sat Jan 22, 2005 11:29 am
by bubach
Code: Select all
; Get the cursor pos. OUT: DH = X DL = Y
;------------------------------------------
getcursorxy:
push eax
push ebx
call getcursor
mov ax, bx
mov dl, 80
div dl
mov dl, al
mov dh, ah
pop ebx
pop eax
ret
; Get the cursor pos. OUT: BX = Offset
;--------------------------------------
getcursor:
push eax
push edx
mov dx, 0x3D4
mov al, 0x0E
out dx, al
inc dx
in al, dx
mov bh, al
mov al, 0x0F
dec dx
out dx, al
inc dx
in al, dx
mov bl, al
pop edx
pop eax
ret
Re:How do I get the cursor position?
Posted: Sun Jan 23, 2005 5:20 am
by Pype.Clicker
there are little chance you actually *need* to get the cursor position! Only your kernel code may have set the position so you just have to remember that last location in some variable that you read back, and voil?.
Re:How do I get the cursor position?
Posted: Thu Jan 27, 2005 9:57 am
by dh
What I do is i have three variables in the screen.c module:
Code: Select all
int _X;
int _Y;
int _COLOR; // should be colour
What happens is when a kPrint, _Print or other /"non-positioning"/ function wants to put a character, it updates _X and _Y then calls kPutChar(chara, _Y, _X, _COLOR).
(Just as a small snippit, I got minimal boundary checking (can be turned on + off) which wraps the cursor to wherever it should be. So if the next character is the beginning of the line and a backspace character is printed, the _Print function (currently the only print avilable), sets the _X to the limit and _Y--.)
Cheers, DH