Reading keypresses fails

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.
Locked
GhostSniper
Posts: 12
Joined: Wed Jul 08, 2009 4:57 pm

Reading keypresses fails

Post 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;
}
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Reading keypresses fails

Post 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?
TheGuy
Posts: 13
Joined: Wed Jul 08, 2009 11:14 pm

Re: Reading keypresses fails

Post 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.
GhostSniper
Posts: 12
Joined: Wed Jul 08, 2009 4:57 pm

Re: Reading keypresses fails

Post 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.
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: Reading keypresses fails

Post 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.
TheGuy
Posts: 13
Joined: Wed Jul 08, 2009 11:14 pm

Re: Reading keypresses fails

Post 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)
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: Reading keypresses fails

Post 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.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Reading keypresses fails

Post 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
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
alethiophile
Member
Member
Posts: 90
Joined: Sat May 30, 2009 10:28 am

Re: Reading keypresses fails

Post 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.
If I had an OS, there would be a link here.
GhostSniper
Posts: 12
Joined: Wed Jul 08, 2009 4:57 pm

Re: Reading keypresses fails

Post by GhostSniper »

How would I go about getting the current cell I'm writing to.
User avatar
alethiophile
Member
Member
Posts: 90
Joined: Sat May 30, 2009 10:28 am

Re: Reading keypresses fails

Post 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.)
If I had an OS, there would be a link here.
GhostSniper
Posts: 12
Joined: Wed Jul 08, 2009 4:57 pm

Re: Reading keypresses fails

Post 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; 
}
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Reading keypresses fails

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Locked