Implementing a freestanding Getchar() for my own OS

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.
Post Reply
stillfly122
Posts: 2
Joined: Sun Nov 13, 2016 2:54 pm
Libera.chat IRC: stillfly

Implementing a freestanding Getchar() for my own OS

Post 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 :)
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

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

Post 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.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Ch4ozz
Member
Member
Posts: 170
Joined: Mon Jul 18, 2016 2:46 pm
Libera.chat IRC: esi

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

Post 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.
stillfly122
Posts: 2
Joined: Sun Nov 13, 2016 2:54 pm
Libera.chat IRC: stillfly

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

Post 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
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

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

Post 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.
If a trainstation is where trains stop, what is a workstation ?
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

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

Post 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
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
mallard
Member
Member
Posts: 280
Joined: Tue May 13, 2014 3:02 am
Location: Private, UK

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

Post 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...
Image
Post Reply