Page 1 of 1

Reading keypresses fails

Posted: Thu Jul 09, 2009 12:24 am
by GhostSniper
Yeah i know. Its really sloppy code, but none the less it should still print the character pressed. Shouldn't it?
It does nothing though. I dont see why it doesnt print the characters pressed to the screen.

Code: Select all

#include "kb.c"
inb (unsigned short port)
{
  unsigned char value;
  asm volatile ("inb    %w1, %0" : "=a" (value) : "Nd" (port));
  return value;
}
outb (unsigned short port, unsigned char value)
{
asm volatile ("outb   %b0, %w1" : : "a" (value), "Nd" (port));
}void clear_screen(char clear_to, char attrib)
{
  char *text_video = (char*)0xB8000;
  int pos=0;

  while(pos<(80*25*2))
  {
    *text_video = clear_to;
    *text_video++;
    *text_video = attrib;
    pos++;
  }
}
void WriteText(char *str,char attrib)
{
	  int num;
  char ch;

  char *text_video = (char*)0xB8000;

  while(*str!=0)
  {
    *text_video = *str;
    *text_video++;
    *text_video = attrib;
    *text_video++;
    *str++;
  }
  return;
}
void kmain( )
{
  clear_screen(0,0x02);
  WriteText("The BCS kernel has been loaded!",0x02);
	for(;;)
	{
    unsigned char scancode;
    scancode = inb(0x60);
    if (scancode & 0x80)
    {
    }
    else
    {
        WriteText(kbdus[scancode],0x02);
    }
    
}
   return;
}

Re: Reading keypresses fails

Posted: Thu Jul 09, 2009 12:50 am
by pcmattman

Code: Select all

void WriteText(char *str,char attrib)

Code: Select all

WriteText(kbdus[scancode],0x02);
You've shown an aptitude for copy & paste, but do you really know C?

Re: Reading keypresses fails

Posted: Thu Jul 09, 2009 12:54 am
by TheGuy
pcmattman wrote:

Code: Select all

void WriteText(char *str,char attrib)

Code: Select all

WriteText(kbdus[scancode],0x02);
You've shown an aptitude for copy & paste, but do you really know C?
What he is trying to say is make a method for writing one character.

Re: Reading keypresses fails

Posted: Thu Jul 09, 2009 1:22 am
by GhostSniper
Alright. You got me. I did however right this function.

Code: Select all

  void WriteChar(char letter,char attrib)
{
   unsigned char *writer = (unsigned char *) 0xB8000;
   writer[0] = letter;
   writer[1] = attrib; 

}
It does work however, It overwrites the last character placed on the screen.
How could I leave the last character as well? I suppose that is because it is using the same byte loaded each time the function is called? Yes I know C, but I don't know C with out my regular include files.

Re: Reading keypresses fails

Posted: Thu Jul 09, 2009 2:10 am
by salil_bhagurkar

Code: Select all

void WriteText(char *str,char attrib)
{
     int num;
  char ch;

  char *text_video = (char*)0xB8000;

  while(*str!=0)
  {
    *text_video = *str;
    *text_video++;
    *text_video = attrib;
    *text_video++;
    *str++;
  }
  return;
}

Code: Select all

void WriteChar(char letter,char attrib)
{
   unsigned char *writer = (unsigned char *) 0xB8000;
   writer[0] = letter;
   writer[1] = attrib;

}
There is a difference between how the above two codes write to the screen. There is something you are missing out. Think.

Re: Reading keypresses fails

Posted: Thu Jul 09, 2009 2:14 am
by TheGuy
salil_bhagurkar wrote:

Code: Select all

void WriteText(char *str,char attrib)
{
     int num;
  char ch;

  char *text_video = (char*)0xB8000;

  while(*str!=0)
  {
    *text_video = *str;
    *text_video++;
    *text_video = attrib;
    *text_video++;
    *str++;
  }
  return;
}

Code: Select all

void WriteChar(char letter,char attrib)
{
   unsigned char *writer = (unsigned char *) 0xB8000;
   writer[0] = letter;
   writer[1] = attrib;

}
There is a difference between how the above two codes write to the screen. There is something you are missing out. Think.
Think adding. (he is offline)

Re: Reading keypresses fails

Posted: Thu Jul 09, 2009 11:18 am
by yemista
GhostSniper wrote:Alright. You got me. I did however right this function.

Code: Select all

  void WriteChar(char letter,char attrib)
{
   unsigned char *writer = (unsigned char *) 0xB8000;
   writer[0] = letter;
   writer[1] = attrib; 

}
You didnt write right. this code will keep writing a character at 0xb8000, and thats not right, right? You gotta write it to keep track of the cursor position, thats the right way to do it.

Re: Reading keypresses fails

Posted: Thu Jul 09, 2009 11:45 am
by Brendan
Hi,

Another issue is that your code would display the same keypress *many* times. You don't notice this now because it puts it at the same place on the screen, but after you've got your code working properly it'll probably end up llllllloooooooooookkkkkiiiiinggggggg llllllliiiiiikkkkkkkeeeee tttttttthhhhhiiiiisssssss!!!!!!!!!!!

The problem is that "scancode = inb(0x60);" will get the last byte sent from the keyboard to the keyboard controller. You need to wait until the next byte is sent from the keyboard to the keyboard controller and then get that byte (and display it) once.


Cheers,

Brendan

Re: Reading keypresses fails

Posted: Thu Jul 09, 2009 9:01 pm
by alethiophile
The problem with your WriteChar function is that it always writes to 0xb8000 exactly. Remember, video memory only starts at 0xb8000; it's a big chunk of space from 0xb8000 to (0xb8000 + (80 * 25 * 2)). You've got to keep track of where you're writing to, and write to 0xb8000 + the offset to your target cell.

Re: Reading keypresses fails

Posted: Thu Jul 09, 2009 9:48 pm
by GhostSniper
How would I go about getting the current cell I'm writing to.

Re: Reading keypresses fails

Posted: Thu Jul 09, 2009 9:55 pm
by alethiophile
Keep track of your row and column (use static or global variables). In WriteChar, increment your row, and if it's end-of-line zero it and increment your column. If you want, then you can implement a scroll_screen and call that when you hit the bottom row; it's up to you. Anyway, when you find your write address, you use 0xb8000 + ((column * 80) + row) * 2. (* 2 is there because a character is 2 bytes.)

Re: Reading keypresses fails

Posted: Thu Jul 09, 2009 10:28 pm
by GhostSniper
I'm sorry but I'm lost. I'm nearly certain this is wrong. I came up with

Code: Select all

int row = 0;
int column = 1;
void WriteChar(char scancode,char attrib)
{
unsigned char *writer = (unsigned char *) 0xb8000 + ((column * 80) + row) * 2;
row++;
   writer[row] = scancode;
   writer[column] = attrib; 
}

Re: Reading keypresses fails

Posted: Fri Jul 10, 2009 1:11 am
by Combuster
And you haven't tested it, obviously, which you should have done.

Again, you failed to ask smart questions, and worse, do you even have the necessary (programming) skills to be taking on OS development since you fail at even the most basic programming tasks.