⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i8259.c

📁 从网上下载的一个自己编写的简单的操作系统源代码,对底层了解很有好处的
💻 C
字号:
/*
 *
 *  irq.c 
 *
 *   8259A  操作和irq 管理
 *
 *
 */
#include <arch.h>
#include <drv/kbd.h>

/*
 * @ Global Var
 */
static unsigned int _origin_mask =0xffff;  //all disabled , 记录当前8259 状态
#define _origin_21 __byte(0, _origin_mask)  /*0x21, first 8259 */
#define _origin_A1 __byte(1,_origin_mask)  /*0xA1, second 8259*/


/*
 *  @_init_8259  
 *   初始化8259, 把irq 0-7 映射到i386 Gate 20h-27h
 *                    irq 8-15 映射到i385 Gate 28h-2fh
 */
 void _init_8259()
 {
 /*
  *@Bios 设置的中断.
  *  port 21h(1st 8259)    IRQ    port A1h(2nd 8259)  IRQ 
  *  b0            IRQ 0 (timer)  b0  IRQ 8 (realtime clock) 
  *  b1         IRQ 1 (keyboard)  b1  IRQ 9 
  *b2 IRQ 2 (cascade; reserved for 2nd 8259)  b2  IRQ 10 
  *  b3         IRQ 3 (COM2,4)    b3  IRQ 11 
  *  b4         IRQ 4 (COM1,3)    b4  IRQ 12 (PS/2 mouse) 
  *  b5         IRQ 5 (LPT)       b5  IRQ 13 ('386 coprocessor) 
  *  b6         IRQ 6 (floppy)    b6  IRQ 14 (primary IDE drives) 
  *  b7         IRQ 7             b7  IRQ 15 (secondary IDE drives) 
  *
  *  The BIOS programs the 8259 chips so that IRQs 0-7 are mapped to interrupts 8-15.
  *  (In my sys bios not do that, ?!)
  */

   /*  re-program the 8259 chips so  IRQ 0-7 -> INT 20h-27h, IRQ 8-15 -> INT 28h-2Fh */

	outb(0x20, 0x11); /* ICW1 */
	outb(0xA0, 0x11);

	outb(0x21, 0x20); /* ICW2: route IRQs 0...7 to INTs 20h...27h */
	outb(0xA1, 0x28); /* ...IRQs 8...15 to INTs 28h...2Fh */

	outb(0x21, 0x04); /* ICW3 */
	outb(0xA1, 0x02);

	outb(0x21, 0x01); /* ICW4 */
	outb(0xA1, 0x01);

	/*Disable all 8259 irq*/
	outb_p(0x21, _origin_21);
	outb_p(0xA1, _origin_A1);
	
 }

/*
 *  @ _enirq_8259
 *   8259 irq enable
 *
 */
void _enirq_8259(int irq)
{
   	unsigned int mask = ~(1 << irq);

	_origin_mask &= mask;

    if (irq & 8) // irq&..001000b
		outb(0xA1, _origin_A1); //irq>8, second
	else
		outb(0x21, _origin_21); //irq<8, first 8259
	

}





/*
 * @ 临时的irq 处理函数
 *
 *
 */
extern char _graph;

void timer(int irq)
{
	volatile int a = g_row, b=g_color, c = g_col;
    
    g_color = 0x0b;
    g_row = 0;
    g_col =0 ;
    //live(); 

	//if(!_graph)
    //    kprintf("TimerCount: %05d", i++);



    g_row = a;
    g_color =b;
    g_col =c; 
    outb_p(0x20,0x20);//clear 8259 int      
}
void nop_irq(int irq)
{
	volatile int a = g_row, b=g_color, c = g_col;
    //static int i=0;
    g_color = 0x0b;
    g_row = 1;
    g_col = 0;

    
    kprintf("irq:%04d", irq);
    
    //kprintf("%08x", i++);

    g_row = a;
    g_color =b;
    g_col =c; 
    outb_p(0x20,0x20);//clear 8259 int      
}


void  kbd(int irq)
{
  static char c ='a';

   /* volatile int a = g_row, b=g_color, col = g_col; */
    /*  
    g_color = 0x0b;
    g_row = 0;
    */
    kprintf("%c",c++);
    /*
    g_row = a;
    g_color =b;
    g_col =col; 
    */
   
  inb(0x60); //renable keboard int cap

  outb_p(0x20,0x20);//clear 8259 int      

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -