【问题标题】:Keyboard interrupt handler for own kernel (C)自己内核的键盘中断处理程序 (C)
【发布时间】:2014-05-09 18:59:50
【问题描述】:

我正在编写一个微型操作系统,作为学校作业的一部分,但是在获取键盘输入时我被卡住了(按一个键 -> 在屏幕上显示它)。我正在使用来自 osdev.org 的 Bare Bones 教程(gcc 交叉编译器、GRUB 引导加载程序、ld 链接器),由于我处于保护模式,我不能使用 BIOS 中断作为输入,这就是为什么我必须编写自己的中断处理程序( ?) 但即使在阅读了一些 osdev 文章和论坛讨论之后,我也不知道该怎么做。非常相似的问题 (http://forum.osdev.org/viewtopic.php?f=1&t=9746),只是我不知道如何“设置中断”。

#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);
}

这是给我打印一个白框。如果我尝试监听端口 0x64,我会得到一些不同的字符。我不希望这会起作用,因为我没有中断。我觉得应该是这样的

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

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

感谢任何帮助。谢谢

【问题讨论】:

标签: gcc x86 kernel inline-assembly osdev


【解决方案1】:

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

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 变量包含按下按钮的代码。通过与每个代码相关联创建一个翻译数组,对应的 ASCII 代码,我可以打印写在按钮上的字母/数字。

按钮代码可以在这里找到:http://www.nondot.org/sabre/os/files/HCI/keyboard.txt

这里的 ASCII:http://www.ascii-code.com/

【讨论】:

  • terminal_putinput 里面是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-25
  • 2015-08-22
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
相关资源
最近更新 更多