Page 1 of 1

How to read a single keystroke with NASM (USB Keyboard)

Posted: Mon Feb 17, 2014 9:52 am
by PrydeRage
Hello everyone!
I am totally new to OS development, and I've just recently managed to finish the bare bones tutorial.
So my current OS is really just the wiki page but I have the newline \n already implemented.
Also I am using NASM instead of GAS because I've got more experience with the Intel 8086 Syntax.

Here's what I want to do:
if any keyboard button pressed -> call C function with parameter

I have been searching the entire day for a solution. I have gone through tons of pages on this forum using the search engine
and I've read the Wiki pages regarding PS/2 Keyboards and Interrupts, but I just cannot translate any of the information
into usable code. Also, I am using a USB Keyboard, so I don't even know if PS\2 Keyboard solutions would work for me.

I have been asking myself all day this: If managing keyboard input is already too difficult for me, should I even continue developing my OS?

Also if this is important:
I'm on Ubuntu and I run the OS using QEmu (because UnetBootin is being stupid -.-)

Greetings, PrydeRage

Re: How to read a single keystroke with NASM (USB Keyboard)

Posted: Mon Feb 17, 2014 10:31 am
by Combuster
You pretty much managed to ask one of my required knowledge exam questions. If you're truly routined in every core requirement, you should be able to get a passing grade without looking stuff up. But that's also the whole point of the thing: you can look things up in everyday life anyway.

(hint: google keywords)

Re: How to read a single keystroke with NASM (USB Keyboard)

Posted: Mon Feb 17, 2014 12:01 pm
by Antti
PrydeRage wrote:if any keyboard button pressed -> call C function with parameter
I recommend that you forget C for now and get back to it later. There is a lot of experiments to be done in plain assembly.
PrydeRage wrote:I am using a USB Keyboard, so I don't even know if PS\2 Keyboard solutions would work for me
A simple answer: PS/2 solutions should work.
PrydeRage wrote:I have been asking myself all day this: If managing keyboard input is already too difficult for me, should I even continue developing my OS?
By writing this you assured me that you have potential. I am not joking. I bet that you will succeed on your OSDev hobby.

Re: How to read a single keystroke with NASM (USB Keyboard)

Posted: Thu Feb 20, 2014 2:45 am
by Bender
Hi,
Most USB Keyboards today provide PS/2 Emulation (at least mine does), not only keyboards but I've seen that PS/2 Mouse code works fine on USB Mouses.
USB, is a massive undertaking IMO seeing that you have just finished the Bare Bones tutorial, USB is long way.
And if you haven't checked these out:
http://wiki.osdev.org/USB_Human_Input_Devices
http://wiki.osdev.org/USB
Also,
if any keyboard button pressed -> call C function with parameter
Would you mind if I ask whether you've heard of something called an IRQ?
If not, then Google this term.
-Bender

Re: How to read a single keystroke with NASM (USB Keyboard)

Posted: Thu Feb 20, 2014 3:07 am
by PrydeRage
Bender wrote: Would you mind if I ask whether you've heard of something called an IRQ?
Yes I've read stuff about IRQs and I think that this is the way to go but I haven't found
any (working) code example that could help me out.

Re: How to read a single keystroke with NASM (USB Keyboard)

Posted: Thu Feb 20, 2014 3:39 am
by iansjack
Forget the USB stuff. As you are using qemu then as far as it is concerned you have a PS/2 keyboard.

There are several sources of information as to how to handle keyboard input. You could do worse than by starting with this Wiki: http://wiki.osdev.org/PS2_Keyboard

Re: How to read a single keystroke with NASM (USB Keyboard)

Posted: Thu Feb 20, 2014 4:54 am
by freecrac
Hello.
Maybe there is a usb lagacy option in the mainboard bios for to enable.
It is for a USB keyboard and a USB pointing device for working like a PS2-device, so that the keyboard and mouse data will be present and redirect to the ports 60H/64H of the keybord controller and for the IRQ1 and IRQ12.

Dirk

Re: How to read a single keystroke with NASM (USB Keyboard)

Posted: Thu Feb 20, 2014 4:55 am
by SirRahbek
To summarize what you want to do, in order to get input from a PS/2 Keyboard, which the USB keyboard most likely emulates
Here's what you need to figure out how to do:
-Set up your interrupt descriptor table
-Remap the PIC to a valid offset in the IDT (0x20 is very often used)
-Write a handler for IRQ1 (or IDT 0x21 if you use offset 0x20)
-Get the scan code from the keyboard at port 0x60
-Convert to ASCII code (yes, you have to create a conversion table, which is super boring)
Serve the freshly baked byte in a register while still hot. :)

Writing the IDT: http://wiki.osdev.org/IDT
PIC remapping: http://wiki.osdev.org/8259_PIC
Scan code table: http://www.ee.bgu.ac.il/~microlab/Micro ... nCodes.htm

Re: How to read a single keystroke with NASM (USB Keyboard)

Posted: Thu Feb 20, 2014 6:07 am
by qw
And don't forget to celebrate when successful!

Re: How to read a single keystroke with NASM (USB Keyboard)

Posted: Mon Feb 24, 2014 1:39 am
by rdos
There are 3 options for handling USB keyboard (in order of complexity):
1. Set it up for PS/2 emulation and do a PS/2 driver (much easier as it doesn't require USB support)
2. Set the keyboard to boot emulation (requires an USB-stack but not a HID implementation)
3. Write a HID driver and as a bonus also get support for modern touch-screens and USB mouse.

Typically, an OS will go from 1 to 3 as it matures.