要了解如何从键盘获取输入数据,就必须首先了解PS/2 Controller(PS/2控制器),有关PS/2控制器的相关内容可以参考...
// zlox_keyboard.c implement interrupt and functions of keyboard .......................................... .......................................... ZLOX_VOID zlox_initKeyboard() { zlox_register_interrupt_callback(ZLOX_IRQ1,&zlox_keyboard_callback); led_status = 0; /* All leds off */ zlox_setleds(); } |
|
|
|
ZLOX_VOID zlox_setleds() { zlox_outb(0x60, 0xED); while(zlox_inb(0x64) & 2); zlox_outb(0x60, led_status); while(zlox_inb(0x64) & 2); } |
// zlox_keyboard.c implement interrupt and functions of keyboard ............................................... ............................................... static ZLOX_VOID zlox_keyboard_callback(/*ZLOX_ISR_REGISTERS * regs*/) { ZLOX_UINT32 key = zlox_inb(0x60); ZLOX_UINT32 key_ascii = 0; /* 'LED Keys', ie, Scroll lock, Num lock, and Caps lock */ if(key == 0x3A) /* Caps Lock */ { led_status ^= ZLOX_LED_CAPS_LOCK; zlox_setleds(); } if(key == 0x45) /* Num Lock */ { led_status ^= ZLOX_LED_NUM_LOCK; zlox_setleds(); } if(key == 0x46) /* Scroll Lock */ { led_status ^= ZLOX_LED_SCROLL_LOCK; zlox_setleds(); } if(key == 0x1D && !(control_keys & ZLOX_CK_CTRL)) /* Ctrl key */ control_keys |= ZLOX_CK_CTRL; if(key == 0x80 + 0x1D) /* Ctrl key depressed */ control_keys &= (0xFF - ZLOX_CK_CTRL); if((key == 0x2A || key == 0x36) && !(control_keys & ZLOX_CK_SHIFT)) /* Shift key */ control_keys |= ZLOX_CK_SHIFT; if((key == 0x80 + 0x2A) || (key == 0x80 + 0x36)) /* Shift key depressed */ control_keys &= (0xFF - ZLOX_CK_SHIFT); if(key == 0x38 && !(control_keys & ZLOX_CK_ALT)) control_keys |= ZLOX_CK_ALT; if(key == 0x80 + 0x38) control_keys &= (0xFF - ZLOX_CK_ALT); if((control_keys & ZLOX_CK_SHIFT) && (led_status & ZLOX_LED_CAPS_LOCK)) key_ascii = scanToAscii_table[key][6]; else if(control_keys & ZLOX_CK_SHIFT) key_ascii = scanToAscii_table[key][1]; else if(control_keys & ZLOX_CK_CTRL) key_ascii = scanToAscii_table[key][2]; else if(control_keys & ZLOX_CK_ALT) key_ascii = scanToAscii_table[key][3]; else if((control_keys & ZLOX_CK_SHIFT) && (led_status & ZLOX_LED_NUM_LOCK)) key_ascii = scanToAscii_table[key][7]; else if(led_status & ZLOX_LED_CAPS_LOCK) key_ascii = scanToAscii_table[key][5]; else if(led_status & ZLOX_LED_NUM_LOCK) key_ascii = scanToAscii_table[key][4]; else if(control_keys == 0) key_ascii = scanToAscii_table[key][0]; if(key_ascii != 0) { if(key_ascii <= 0xFF) { keyboard_buffer[keyboard_buffer_size] = key_ascii; keyboard_buffer_size++; zlox_monitor_put(key_ascii); } else { keyboard_buffer[keyboard_buffer_size] = (key_ascii & 0xFF); keyboard_buffer[keyboard_buffer_size+1] = (key_ascii & 0xFF00); keyboard_buffer_size += 2; } } } |
// zlox_keyboard.c implement interrupt and functions of keyboard ......................................... ......................................... ZLOX_UINT32 scanToAscii_table[][8] = { /* ASCII - Shift - Ctrl - Alt - Num - Caps - Shift Caps - Shift Num */ { 0, 0, 0, 0, 0, 0, 0, 0}, { 0x1B, 0x1B, 0x1B, 0, 0x1B, 0x1B, 0x1B, 0x1B}, /* 1 -> 9 */ { 0x31, 0x21, 0, 0x7800, 0x31, 0x31, 0x21, 0x21}, { 0x32, 0x40, 0x0300, 0x7900, 0x32, 0x32, 0x40, 0x40}, { 0x33, 0x23, 0, 0x7A00, 0x33, 0x33, 0x23, 0x23}, { 0x34, 0x24, 0, 0x7B00, 0x34, 0x34, 0x24, 0x24}, { 0x35, 0x25, 0, 0x7C00, 0x35, 0x35, 0x25, 0x25}, { 0x36, 0x5E, 0x1E, 0x7D00, 0x36, 0x36, 0x5E, 0x5E}, { 0x37, 0x26, 0, 0x7E00, 0x37, 0x37, 0x26, 0x26}, { 0x38, 0x2A, 0, 0x7F00, 0x38, 0x38, 0x2A, 0x2A}, { 0x39, 0x28, 0, 0x8000, 0x39, 0x39, 0x28, 0x28}, { 0x30, 0x29, 0, 0x8100, 0x30, 0x30, 0x29, 0x29}, /* -, =, Bksp, Tab */ { 0x2D, 0x5F, 0x1F, 0x8200, 0x2D, 0x2D, 0x5F, 0x5F}, { 0x3D, 0x2B, 0, 0x8300, 0x3D, 0x3D, 0x2B, 0x2B}, { 0x08, 0x08, 0x7F, 0, 0x08, 0x08, 0x08, 0x08}, { 0x09, 0x0F00, 0, 0, 0x09, 0x09, 0x0F00, 0x0F00}, /* QWERTYUIOP[] */ { 0x71, 0x51, 0x11, 0x1000, 0x71, 0x51, 0x71, 0x51}, { 0x77, 0x57, 0x17, 0x1100, 0x77, 0x57, 0x77, 0x57}, ......................................... ......................................... |
// zlox_keyboard.c implement interrupt and functions of keyboard ............................................ ............................................ static ZLOX_VOID zlox_keyboard_callback(/*ZLOX_ISR_REGISTERS * regs*/) { ............................................ ............................................ else if(led_status & ZLOX_LED_CAPS_LOCK) key_ascii = scanToAscii_table[key][5]; else if(led_status & ZLOX_LED_NUM_LOCK) key_ascii = scanToAscii_table[key][4]; else if(control_keys == 0) key_ascii = scanToAscii_table[key][0]; ............................................ ............................................ } |
// zlox_keyboard.c implement interrupt and functions of keyboard ............................................ ............................................ static ZLOX_VOID zlox_keyboard_callback(/*ZLOX_ISR_REGISTERS * regs*/) { ............................................ ............................................ if(key_ascii != 0) { if(key_ascii <= 0xFF) { keyboard_buffer[keyboard_buffer_size] = key_ascii; keyboard_buffer_size++; zlox_monitor_put(key_ascii); } else { keyboard_buffer[keyboard_buffer_size] = (key_ascii & 0xFF); keyboard_buffer[keyboard_buffer_size+1] = (key_ascii & 0xFF00); keyboard_buffer_size += 2; } } } |
/*zlox_kernel.c Defines the C-code kernel entry point, calls initialisation routines.*/ .......................................... .......................................... //zenglOX kernel main entry ZLOX_SINT32 zlox_kernel_main(ZLOX_MULTIBOOT * mboot_ptr, ZLOX_UINT32 initial_stack) { .......................................... .......................................... // 初始化系统调用 zlox_initialise_syscalls(); zlox_initKeyboard(); zlox_syscall_monitor_write("=========================\n"); zlox_syscall_monitor_write("Keyboard is init now!\n"); zlox_syscall_monitor_write("=========================\n"); // 切换到ring 3的用户模式 zlox_switch_to_user_mode(); zlox_syscall_monitor_write("I'm in user mode!\n"); zlox_syscall_monitor_write("Hello world!\nwelcome to zenglOX v0.0.10!\nplease input some char> "); for(;;) ; return 0; } |