Updating cursor position?

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
Triumph

Updating cursor position?

Post by Triumph »

Hi!

I've just begun developing my kernel, and i've wrote a printf function today, but now i also want my printf function to update the cursor position after use...

I believe to remember, that it's done by writing something to port 0x3D4 og 0x3D5, but how do i calculate what im going to send to those ports?

I have a variable called offset, which contains the offset from the 0xB8000 mark...

Triumph
Dreamsmith

Re:Updating cursor position?

Post by Dreamsmith »

My current OS project doesn't support text screens, but digging through some old code from another era...

Code: Select all

   outportb(0x3D4, 0x0F);
   outportb(0x3D5, console.y * console.width + console.x);
   outportb(0x3D4, 0x0E);
   outportb(0x3D5, (console.y * console.width + console.x) >> 8);
If you have an offset from screen base, I'm guessing from this you'd need to replace the calculations above with "offset / 2" and "(offset / 2) >> 8", or less clearly but more efficiently (or not, depending on how good the optimizer is), "offset >> 1" and "offset >> 9". (Ignore my last comment, I learned to code in an era when the optimizer couldn't be trusted to actually do anything useful.)
Triumph

Re:Updating cursor position?

Post by Triumph »

Okay, but how does the CPU calculate the cursor position out of this?
I cant seem to figure out what it does with these numbers :(

Lets take for an example i have 6 in my offset variable, then i store 6 >> 1 in the 0x0E var, and 6 >> 9 in the 0x0F var...
I would calculate these to numbers to 3 and 0,01171875, but how does we go along from here?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Updating cursor position?

Post by Brendan »

Triumph wrote: Okay, but how does the CPU calculate the cursor position out of this?
I cant seem to figure out what it does with these numbers :(

Lets take for an example i have 6 in my offset variable, then i store 6 >> 1 in the 0x0E var, and 6 >> 9 in the 0x0F var...
I would calculate these to numbers to 3 and 0,01171875, but how does we go along from here?
The CRT controller registers 0x0F and 0x0E are high and low bytes, which form the 16 bit "character number" (where character number 0 would be the top left character, 1 is the top second from left, 80 would be the left-most character on the second row).

So, first you need to calculate the "character number", which would be the "display memory offset / 2" or alternatively "cursorY * screenSizeX + cursorX".

Once you've got the (16 bit) character number you need to split it into a low byte (for CRT controller register 0x0F) and a high byte (for CRT controller register 0x0E).

Also, if you're working with integer numbers there isn't anything after the decimal point (no fractions). Therefore 6 >> 9 = 0, or if your display memory offset is 6 then you'd use 3 as the low byte and 0 as the high byte.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Updating cursor position?

Post by Candy »

Brendan wrote:
Lets take for an example i have 6 in my offset variable, then i store 6 >> 1 in the 0x0E var, and 6 >> 9 in the 0x0F var...
I would calculate these to numbers to 3 and 0,01171875, but how does we go along from here?
The CRT controller registers 0x0F and 0x0E are high and low bytes, which form the 16 bit "character number" (where character number 0 would be the top left character, 1 is the top second from left, 80 would be the left-most character on the second row).

<snip>

Also, if you're working with integer numbers there isn't anything after the decimal point (no fractions). Therefore 6 >> 9 = 0, or if your display memory offset is 6 then you'd use 3 as the low byte and 0 as the high byte.
Uhm... almost.

Drinking something with caffeine does wonders :)

If you want the high byte, take (X >> 8 ) & 0xFF. If you want the low byte, take X & 0xFF.

For this example that leaves 0 and 6 :)

HTH [edit] that wasn't a smiley. [/edit]
Triumph

Re:Updating cursor position?

Post by Triumph »

Sorry, but i dont quite understand this :-[
Cant you give me an example, with the calculations of eg. 120...?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Updating cursor position?

Post by Pype.Clicker »

okay, let's say you want to set the cursor on colum 40 of row 12 (roughly the center of the screen).

The so-called "cursor offset" is 40 + 12 * 80 as there are 80 characters per row and your "current vram writing offset" is 80+12*160 (as there are 2 bytes per character in the VRAM). Clearly, c_offset=vw_offset/2 or c_offset=vw_offset>>1.


now, you need to extract the lowest byte of the cursor offset, that is c_offset%256 (remainder of integer division) or c_offset&0xff (masking higher bits)

and the highest byte will be extracted with c_offset/256 (or c_offset>>8, using a shift)
Triumph

Re:Updating cursor position?

Post by Triumph »

Ohh... Now i understand :)
The reason i had trouble was for me to use the or operation, and not the and operation, when i tried to calculate the numbers i binary :D

Thanks!!
Post Reply