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

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
PrydeRage
Posts: 2
Joined: Mon Feb 17, 2014 9:38 am

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

Post 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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

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

Post 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)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

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

Post 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.
User avatar
Bender
Member
Member
Posts: 449
Joined: Wed Aug 21, 2013 3:53 am
Libera.chat IRC: bender|
Location: Asia, Singapore

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

Post 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
"In a time of universal deceit - telling the truth is a revolutionary act." -- George Orwell
(R3X Runtime VM)(CHIP8 Interpreter OS)
PrydeRage
Posts: 2
Joined: Mon Feb 17, 2014 9:38 am

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

Post 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.
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post 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
freecrac
Member
Member
Posts: 69
Joined: Thu Sep 20, 2012 5:11 am
Location: germany hamburg

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

Post 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
SirRahbek
Posts: 3
Joined: Sun Feb 16, 2014 11:05 am

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

Post 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
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

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

Post by qw »

And don't forget to celebrate when successful!
rdos
Member
Member
Posts: 3306
Joined: Wed Oct 01, 2008 1:55 pm

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

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