Read user input in my C kernel

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

Read user input in my C kernel

Post 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?
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Read user input in my C kernel

Post 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.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
GhostSniper
Posts: 12
Joined: Wed Jul 08, 2009 4:57 pm

Re: Read user input in my C kernel

Post by GhostSniper »

Alright. Is there any tutorials here or else where that you would reccomend?
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: Read user input in my C kernel

Post by 01000101 »

clange
Member
Member
Posts: 163
Joined: Sun Oct 05, 2008 5:00 am
Location: Copenhagen, Denmark
Contact:

Re: Read user input in my C kernel

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

Re: Read user input in my C kernel

Post 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'
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: Read user input in my C kernel

Post by pcmattman »

You will have to write inb/outb, of course :)
clange
Member
Member
Posts: 163
Joined: Sun Oct 05, 2008 5:00 am
Location: Copenhagen, Denmark
Contact:

Re: Read user input in my C kernel

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

Re: Read user input in my C kernel

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

Re: Read user input in my C kernel

Post 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.
If I had an OS, there would be a link here.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Read user input in my C kernel

Post 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 ;)
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Read user input in my C kernel

Post by JamesM »

Read the bloody wiki, that's what it's there for.

Don't be so damn lazy.
Locked