Page 1 of 1

Read user input in my C kernel

Posted: Wed Jul 08, 2009 5:15 pm
by GhostSniper
Im wanting to read input in my C kernel and not just print out characters. Here is my current code so far. I can print out strings and clear the screen.

Code: Select all

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);
  return;
  
}
So how do I get input from the keyboard?

Re: Read user input in my C kernel

Posted: Wed Jul 08, 2009 5:31 pm
by gravaera
You need to write a keyboard driver. There's lots of documentation on a basic kb driver all over. It's one of the basic steps, so there's plenty example source too.

Good luck.

Re: Read user input in my C kernel

Posted: Wed Jul 08, 2009 5:42 pm
by GhostSniper
Alright. Is there any tutorials here or else where that you would reccomend?

Re: Read user input in my C kernel

Posted: Wed Jul 08, 2009 5:54 pm
by 01000101

Re: Read user input in my C kernel

Posted: Wed Jul 08, 2009 5:55 pm
by clange
The OSDev.org Wiki - Got a question? Search this first!

http://forum.osdev.org/viewtopic.php?f=1&t=16944
http://wiki.osdev.org/PS2_Keyboard

clange

EDIT: 1 minute too late :D

Re: Read user input in my C kernel

Posted: Wed Jul 08, 2009 6:13 pm
by GhostSniper
Ok thanks. I read up on it and found http://forum.osdev.org/viewtopic.php?f= ... 6&start=15
A user talks about using inb to read from port 0x60. Well i try to use inb in my kernel and i get an error message when linking.

Code: Select all

 undefined reference to `inb'

Re: Read user input in my C kernel

Posted: Wed Jul 08, 2009 6:25 pm
by pcmattman
You will have to write inb/outb, of course :)

Re: Read user input in my C kernel

Posted: Wed Jul 08, 2009 6:26 pm
by clange
Well, maybe you should use more time to try to solve your problems. 20 minutes is a bit of searching, copying some code and trying to compile it. Read and investigate more - then ask smart questions.

clange

Re: Read user input in my C kernel

Posted: Wed Jul 08, 2009 6:51 pm
by GhostSniper
Thanks for the replies. I was well on my way. Reading up on inb, outb. Then i came upon what appears to be a broken link. http://www.osdev.org/osfaq2/index.php/W ... %20C%20%3F
It was referenced from here http://forum.osdev.org/viewtopic.php?p=61727#p61727 and it seems the guy there solved his problem using that page that I can no longer access. so im stumped now on getting the inb and outb functions.

EDIT: I found these functions

Code: Select all

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));
}
Are these the ones im looking for?

Re: Read user input in my C kernel

Posted: Wed Jul 08, 2009 10:27 pm
by alethiophile
Yes. Though you'll want to rewrite them for better practice (like specifying return types).

For reference, here's my inportb/outportb:

Code: Select all

void outportb(u16int port, u8int value) {
  asm volatile ("outb %1, %0" : : "dN" (port), "a" (value));
}

u8int inportb(u16int port) {
  u8int ret;
  asm volatile ("inb %1, %0" : "=a" (ret) : "dN" (port));
  return ret;
}
Pretty much like the one above, but in proper ANSI C.

Re: Read user input in my C kernel

Posted: Thu Jul 09, 2009 5:42 am
by gravaera
When developing an OS, it is imperative that you get used to excessive reading. It is inescapable.

There'll come a time when, if you manage to forge ahead with your kernel, you'll have no way to come onto the forum and ask: "Hey, how to do so and so?".

At that time, Google will be your only friend. You'll be reading full time.

EDIT: I forgot: that ZIP contains a full C++ tutorial on how to set up a C++ kernel. Although you say you use C, it is still highly relevant, and you can browse through the source or the keyboard.h and keyboard.cpp files, and learn from them. He also has interrupts, and whatnot set up. Read and learn. Enjoy ;)

Re: Read user input in my C kernel

Posted: Thu Jul 09, 2009 9:12 am
by JamesM
Read the bloody wiki, that's what it's there for.

Don't be so damn lazy.