Page 1 of 1

Implementing a freestanding Getchar() for my own OS

Posted: Sun Nov 13, 2016 3:00 pm
by stillfly122
Hello! I am brand new to these forums, although i've been surfing this forum for quite a long time, I have just now decided to create an account.

Currently, I am messing around with developing an Operating System, as a hobby, as well as to gain great knowledge on the C language.
I already have sets of functions required for my operating system to boot up, and i've gotten it to the stage where my Console code Takes over,
prints welcome messages to the screen, and prints the prompt, though I cannot for the life of me find a way to implement a getchar() or scanf()
function into my kernel, without the use of libraries! Would anybody know how to go about this? Code is very much appreciated, as is advice that
points me in the right direction. I have been stuck at this point for quite some time now, and decided it was time to reach out for some guidance.

Thank you all!
Your advice and knowledge is greatly appreciated!

~Nick

PS - If need be, I can show you certain functions needed to implement it under my own kernel. Just ask for it if you need it :)

Re: Implementing a freestanding Getchar() for my own OS

Posted: Sun Nov 13, 2016 3:43 pm
by Octacone
It is very simple: you need a keyboard driver.
You have two possibilities:
1.Pooling (while loop + inportb/outportb) not recommended but easier
2.Interrupts (IDT, IRQ, ISR) recommended but harder to code

As you can see you have to code everything yourself. If you need any help feel free to ask.

Re: Implementing a freestanding Getchar() for my own OS

Posted: Sun Nov 13, 2016 4:09 pm
by Ch4ozz
Code some data structures like queues, array lists etc so you can use the full set of features from the beginning.
You also might create some basic kind of windowing system if your OS will 100% contain windows.

Re: Implementing a freestanding Getchar() for my own OS

Posted: Sun Nov 13, 2016 6:31 pm
by stillfly122
Thank you all for your replies. I am not looking to do any kind of windowing until I have a fully developed command line.

As for the first reply - Could you show me an example of what this inb outb loop would look like? I have inb and outb implemented into my system,
but no matter how hard I try to get my keyboard driver to work, when I boot, it doesn't do anything. It just sits on the welcome messages, and the keyboard
doesn't input anything at all to the console. I'm stumped and would very much appreciate a working example that I could study (I repeat, Study - I do not intend
to use it in my OS )

Again, thank you;
~Nick

Re: Implementing a freestanding Getchar() for my own OS

Posted: Sun Nov 13, 2016 9:04 pm
by gerryg400
You need a way of reading keys from the keyboard. In a simple system this is usually accomplished using an interrupt routine that reads keys from the keyboard as they become available and stores them in a queue.

Some method is then used to tell the foreground task that's waiting for the key that there is a key in the queue.

Are you familiar at all with this concept? I'm not sure how much information you need.

Re: Implementing a freestanding Getchar() for my own OS

Posted: Mon Nov 14, 2016 2:58 pm
by Octacone
Do not use "print string", use "print character" method instead. You can get only one character at the time.

Take a look at this:
IO Port Access Type Purpose
0x60 Read/Write Data Port
0x64 Read Status Register
0x64 Write Command Register

Here is all the info you need:
http://wiki.osdev.org/"8042"_PS/2_Controller

Re: Implementing a freestanding Getchar() for my own OS

Posted: Tue Nov 15, 2016 4:35 am
by mallard
I'd highly recommend not putting any sort of user interface (command-line or otherwise) directly in the kernel. Not even MS-DOS did that.

Ideally, the command-line interpreter (your OS's equivalent of COMMAND.COM, CMD.EXE, bash, etc.) would be an ordinary user-space application and would use a system call (e.g. a "read" on a device object/file representing the keyboard or terminal) and the keyboard driver would respond by copying data from its internal buffer (where the ISR puts received keystrokes) into the buffer provided by the application or block until the requested number of keystrokes is available.

There are other things to consider; the keyboard sends "scancodes" to the controller which will need to be converted into characters at some stage (but you'll also need to consider non-character keys such as the function keys, etc.). You may want to convert scancodes into a hardware-independent "keycode" as an intermediate step. You'll also need to consider differing keyboard layouts and even the possibility that multiple keyboards with different layouts could be connected to the same computer...