在c内核保护模式下不获取键盘输入
Not Getting keyboard input in c kernel protected mode
我在 C 和 32 位汇编中制作一个 OS,在 James Molloy's tutorial 之后,直到 IRQ 和 PIT 步骤,我正在尝试获取键盘输入 我尝试将这段代码添加到教程的代码中,但我我无法把事情做好。
Keyboard.c:
#include "keyboard.h"
#include "common.h"
#include "monitor.h"
#include "isr.h"
//Keyboard Layout USA
unsigned char kblayout[128] =
{
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */
'9', '0', '-', '=', '\b', /* Backspace */
'\t', /* Tab */
'q', 'w', 'e', 'r', /* 19 */
't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */
0, /* 29 - Control */
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */
'\'', '`', 0, /* Left shift */
'\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */
'm', ',', '.', '/', 0, /* Right shift */
'*',
0, /* Alt */
' ', /* Space bar */
0, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* < ... F10 */
0, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
'-',
0, /* Left Arrow */
0,
0, /* Right Arrow */
'+',
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};
unsigned int restart_keyboard()
{
int data = inb(0x61);
outb(0x61,data | 0x80);//Disables the keyboard
outb(0x61,data & 0x7F);//Enables the keyboard
return 0;
}
unsigned char get_scancode()
{
unsigned char inputdata;
inputdata = inb(0x60);
return inputdata;
}
static void keyboard_handler(registers_t regs)
{
unsigned char scancode;
unsigned int shift_key = 0;
scancode = get_scancode();
if(scancode == 0x2A)
{
shift_key = 1;//Shift key is pressed
}
else if(scancode & 0xAA)
{
int shift_key= 0;//Shift Key is not pressed
}
else
{
if (scancode & 0x80)
{
int shiftaltctrl = 1;//Put anything to see what special keys were pressed
}
else
{
monitor_put(kblayout[scancode]); //Prints the character which was pressed
}
}
}
void keyboard_install()
{
register_interrupt_handler(33, keyboard_handler);
}
main.c: 内核
#include "monitor.h"
#include "descriptor_tables.h"
#include "timer.h"
#include "keyboard.h"
int main(struct multiboot *mboot_ptr)
{
// Initialise all the ISRs and segmentation
init_descriptor_tables();
// Initialise the screen (by clearing it)
monitor_clear();
// Write out a sample string
monitor_write("Hello, world!\n");
keyboard_install();
return 0;
}
所有其他文件都相同。
它运行正常,但它不打印输出,我不知道为什么 :(
我是运行这个用qemu.
它打印 hello world 但不打印击键
问题是我在内核的引导代码中禁用中断后没有恢复中断,我使用 asm voltile("sti")
恢复了它
我在 C 和 32 位汇编中制作一个 OS,在 James Molloy's tutorial 之后,直到 IRQ 和 PIT 步骤,我正在尝试获取键盘输入 我尝试将这段代码添加到教程的代码中,但我我无法把事情做好。
Keyboard.c:
#include "keyboard.h"
#include "common.h"
#include "monitor.h"
#include "isr.h"
//Keyboard Layout USA
unsigned char kblayout[128] =
{
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */
'9', '0', '-', '=', '\b', /* Backspace */
'\t', /* Tab */
'q', 'w', 'e', 'r', /* 19 */
't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */
0, /* 29 - Control */
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */
'\'', '`', 0, /* Left shift */
'\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */
'm', ',', '.', '/', 0, /* Right shift */
'*',
0, /* Alt */
' ', /* Space bar */
0, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* < ... F10 */
0, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
'-',
0, /* Left Arrow */
0,
0, /* Right Arrow */
'+',
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};
unsigned int restart_keyboard()
{
int data = inb(0x61);
outb(0x61,data | 0x80);//Disables the keyboard
outb(0x61,data & 0x7F);//Enables the keyboard
return 0;
}
unsigned char get_scancode()
{
unsigned char inputdata;
inputdata = inb(0x60);
return inputdata;
}
static void keyboard_handler(registers_t regs)
{
unsigned char scancode;
unsigned int shift_key = 0;
scancode = get_scancode();
if(scancode == 0x2A)
{
shift_key = 1;//Shift key is pressed
}
else if(scancode & 0xAA)
{
int shift_key= 0;//Shift Key is not pressed
}
else
{
if (scancode & 0x80)
{
int shiftaltctrl = 1;//Put anything to see what special keys were pressed
}
else
{
monitor_put(kblayout[scancode]); //Prints the character which was pressed
}
}
}
void keyboard_install()
{
register_interrupt_handler(33, keyboard_handler);
}
main.c: 内核
#include "monitor.h"
#include "descriptor_tables.h"
#include "timer.h"
#include "keyboard.h"
int main(struct multiboot *mboot_ptr)
{
// Initialise all the ISRs and segmentation
init_descriptor_tables();
// Initialise the screen (by clearing it)
monitor_clear();
// Write out a sample string
monitor_write("Hello, world!\n");
keyboard_install();
return 0;
}
所有其他文件都相同。 它运行正常,但它不打印输出,我不知道为什么 :( 我是运行这个用qemu.
它打印 hello world 但不打印击键
问题是我在内核的引导代码中禁用中断后没有恢复中断,我使用 asm voltile("sti")