对于自己的内核键盘中断处理程序(C) [英] Keyboard interrupt handler for own kernel (C)

查看:276
本文介绍了对于自己的内核键盘中断处理程序(C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个小OS作为学校的assigment的一部分,但是当涉及到​​获取键盘输入(preSS键 - >在屏幕上显示出来)我被困。我使用从osdev.org梗概教程(GCC交叉编译器,GRUB引导装载程序,LD链接),因为我是在保护模式下无法使用BIOS中断输入,这就是为什么我写我自己的中断处理程序( ?),但我不知道怎么做,即使我读了一些osdev文章和论坛讨论。非常类似的问题( http://forum.osdev.org/viewtopic.php ?F = 1&安培; T = 9746 ),只是我不知道如何设置中断

I am writing a tiny OS as part of an assigment for school,but I got stuck when it comes to get keyboard input (press a key -> display it on screen). I am using the Bare Bones tutorial from osdev.org (gcc cross-compiler, GRUB bootloader, ld linker) and since I am in protected mode I can not use BIOS interrupts for input, that's why I have to write my own interrupt handler (?) but I'm not sure how to do that even after I read some osdev articles and forum discussions. Very similar problem (http://forum.osdev.org/viewtopic.php?f=1&t=9746) except that I don't know how to "set up the interrupts".

#if !defined(__cplusplus)
#include <stdbool.h> /* C doesn't have booleans by default. */
#endif
#include <stddef.h>
#include <stdint.h>
#define INT_DISABLE 0
#define INT_ENABLE  0x200
#define PIC1 0x20
#define PIC2 0xA0

#define ICW1 0x11
#define ICW4 0x01

void outb( unsigned short port, unsigned char val )
{
   asm volatile("outb %0, %1" : : "a"(val), "Nd"(port) );
}

static __inline unsigned char inb (unsigned short int port)
{
  unsigned char _v;

  __asm__ __volatile__ ("inb %w1,%0":"=a" (_v):"Nd" (port));
  return _v;
}

void init_pics(int pic1, int pic2)
{
   /* send ICW1 */
   outb(PIC1, ICW1);
   outb(PIC2, ICW1);

   /* send ICW2 */
   outb(PIC1 + 1, pic1);   
   outb(PIC2 + 1, pic2);   

   /* send ICW3 */
   outb(PIC1 + 1, 4);   
   outb(PIC2 + 1, 2);

   /* send ICW4 */
   outb(PIC1 + 1, ICW4);
   outb(PIC2 + 1, ICW4);

   /* disable all IRQs */
   outb(PIC1 + 1, 0xFF);
}

/*irrelevant code*/


#if defined(__cplusplus)
extern "C" /* Use C linkage for kernel_main. */
#endif
void kernel_main()
{
    terminal_initialize();

    char c;     
    init_pics(0x20, 0x28);
    c = inb(0x60);
    terminal_putchar(c);
}

这是我打印白色box.If我尝试监听端口0x64我得到一些不同的角色。我不希望这个工作,因为我没有中断。我想应该是这样的。

This is printing me a white box.If I try listening to port 0x64 I get some different character. I don't expect this to work, because I don't have the interrupt. I think it should be something like

void _interrupt button_pressed()
{
/*code*/
}

if(button_pressed)
{
    c = inb(0x60);
    //code to translate the char to ASCII
    terminal_putchar(asciiChar);
}

任何帮助是AP preciated。谢谢

Any help is appreciated. Thanks

推荐答案

如果有感兴趣的人我是如何解决这个问题,这里是解决方案

If there is someone interested how I solved the problem, here is the solution

char c = 0;
init_pics(0x20, 0x28);
do
{

if(inb(0x60)!=c) //PORT FROM WHICH WE READ
{
    c = inb(0x60);
    if(c>0)
        {

            terminal_putinput(c); //print on screen

        }
    }


}
while(c!=1); // 1= ESCAPE

C 变量包含pressed按钮code。通过关联到每个code创建翻译数组,对应的ASCII code,我可以打印哪些是按键写的字母/数字。

c variable contains the code of the pressed button. Creating a translation array by associating to each code, the corresponding ASCII code, I can print the letter/number which is written on button.

按钮code可以在这里找到:<一href=\"http://www.nondot.org/sabre/os/files/HCI/keyboard.txt\">http://www.nondot.org/sabre/os/files/HCI/keyboard.txt

The buttons code can be found here: http://www.nondot.org/sabre/os/files/HCI/keyboard.txt

这里的ASCII: HTTP://www.ascii-$c$c.com/

这篇关于对于自己的内核键盘中断处理程序(C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆