How to read a single keystroke with NASM (USB Keyboard)
How to read a single keystroke with NASM (USB Keyboard)
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
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
- Combuster
- 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)
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)
(hint: google keywords)
Re: How to read a single keystroke with NASM (USB Keyboard)
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:if any keyboard button pressed -> call C function with parameter
A simple answer: PS/2 solutions should work.PrydeRage wrote:I am using a USB Keyboard, so I don't even know if PS\2 Keyboard solutions would work for me
By writing this you assured me that you have potential. I am not joking. I bet that you will succeed on your OSDev hobby.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?
- Bender
- 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)
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 not, then Google this term.
-Bender
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,
Would you mind if I ask whether you've heard of something called an IRQ?if any keyboard button pressed -> call C function with parameter
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)
(R3X Runtime VM)(CHIP8 Interpreter OS)
Re: How to read a single keystroke with NASM (USB Keyboard)
Yes I've read stuff about IRQs and I think that this is the way to go but I haven't foundBender wrote: Would you mind if I ask whether you've heard of something called an IRQ?
any (working) code example that could help me out.
Re: How to read a single keystroke with NASM (USB Keyboard)
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
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)
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
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)
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
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)
And don't forget to celebrate when successful!
Re: How to read a single keystroke with NASM (USB Keyboard)
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.
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.