Page 1 of 1
Updating cursor position?
Posted: Mon Oct 11, 2004 11:09 am
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
Re:Updating cursor position?
Posted: Mon Oct 11, 2004 1:51 pm
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.)
Re:Updating cursor position?
Posted: Mon Oct 11, 2004 3:57 pm
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?
Re:Updating cursor position?
Posted: Mon Oct 11, 2004 10:20 pm
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
Re:Updating cursor position?
Posted: Mon Oct 11, 2004 11:27 pm
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]
Re:Updating cursor position?
Posted: Tue Oct 12, 2004 9:52 am
by Triumph
Sorry, but i dont quite understand this :-[
Cant you give me an example, with the calculations of eg. 120...?
Re:Updating cursor position?
Posted: Tue Oct 12, 2004 10:20 am
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)
Re:Updating cursor position?
Posted: Tue Oct 12, 2004 12:11 pm
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
Thanks!!