Skip to content

Commit 35ece81

Browse files
committed
鼠标键盘中断处理
1 parent 8205320 commit 35ece81

File tree

5 files changed

+106
-6
lines changed

5 files changed

+106
-6
lines changed

06_day/bootpack.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ void HariMain(void)
1111

1212
init_gdtidt();
1313
init_pic();
14-
14+
io_sti(); /* IDT/PICの初期化が終わったのでCPUの割り込み禁止を解除 */
15+
1516
init_palette();
1617
init_screen8(binfo->vram, binfo->scrnx, binfo->scrny);
1718
mx = (binfo->scrnx - 16) / 2; /* 计算画面的中心坐标*/
@@ -21,6 +22,9 @@ void HariMain(void)
2122
sprintf(s, "(%d, %d)", mx, my);
2223
putfonts8_asc(binfo->vram, binfo->scrnx, 0, 0, COL8_FFFFFF, s);
2324

25+
io_out8(PIC0_IMR, 0xf9); /* PIC1とキーボードを許可(11111001) */
26+
io_out8(PIC1_IMR, 0xef); /* マウスを許可(11101111) */
27+
2428
for (;;) {
2529
io_hlt();
2630
}

06_day/bootpack.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ struct BOOTINFO { /* 0x0ff0-0x0fff */
1212
/* naskfunc.nas */
1313
void io_hlt(void);
1414
void io_cli(void);
15+
void io_sti(void);
1516
void io_out8(int port, int data);
1617
int io_load_eflags(void);
1718
void io_store_eflags(int eflags);
1819
void load_gdtr(int limit, int addr);
1920
void load_idtr(int limit, int addr);
21+
void asm_inthandler21(void);
22+
void asm_inthandler27(void);
23+
void asm_inthandler2c(void);
2024

2125
/* graphic.c */
2226
void init_palette(void);
@@ -67,9 +71,13 @@ void set_gatedesc(struct GATE_DESCRIPTOR *gd, int offset, int selector, int ar);
6771
#define LIMIT_BOTPAK 0x0007ffff
6872
#define AR_DATA32_RW 0x4092
6973
#define AR_CODE32_ER 0x409a
74+
#define AR_INTGATE32 0x008e
7075

7176
/* int.c */
7277
void init_pic(void);
78+
void inthandler21(int *esp);
79+
void inthandler27(int *esp);
80+
void inthandler2c(int *esp);
7381
#define PIC0_ICW1 0x0020
7482
#define PIC0_OCW2 0x0020
7583
#define PIC0_IMR 0x0021
@@ -81,4 +89,4 @@ void init_pic(void);
8189
#define PIC1_IMR 0x00a1
8290
#define PIC1_ICW2 0x00a1
8391
#define PIC1_ICW3 0x00a1
84-
#define PIC1_ICW4 0x00a1
92+
#define PIC1_ICW4 0x00a1

06_day/dsctbl.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@ void init_gdtidt(void)
99
int i;
1010

1111
/* GDT初始化 */
12-
for (i = 0; i < 8192; i++) {
12+
for (i = 0; i <= LIMIT_GDT / 8; i++) {
1313
set_segmdesc(gdt + i, 0, 0, 0);
1414
}
1515
set_segmdesc(gdt + 1, 0xffffffff, 0x00000000, AR_DATA32_RW);
1616
set_segmdesc(gdt + 2, LIMIT_BOTPAK, ADR_BOTPAK, AR_CODE32_ER);
1717
load_gdtr(LIMIT_GDT, ADR_GDT);
1818

1919
/* IDT初始化 */
20-
for (i = 0; i < 256; i++) {
20+
for (i = 0; i <= LIMIT_IDT / 8; i++) {
2121
set_gatedesc(idt + i, 0, 0, 0);
2222
}
2323
load_idtr(LIMIT_IDT, ADR_IDT);
2424

25+
/* IDT设置*/
26+
set_gatedesc(idt + 0x21, (int) asm_inthandler21, 2 * 8, AR_INTGATE32);
27+
set_gatedesc(idt + 0x27, (int) asm_inthandler27, 2 * 8, AR_INTGATE32);
28+
set_gatedesc(idt + 0x2c, (int) asm_inthandler2c, 2 * 8, AR_INTGATE32);
29+
2530
return;
2631
}
2732

06_day/int.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,36 @@ void init_pic(void)
2323

2424
return;
2525
}
26+
27+
void inthandler21(int *esp)
28+
/* 来自PS/2键盘的中断 */
29+
{
30+
struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO;
31+
boxfill8(binfo->vram, binfo->scrnx, COL8_000000, 0, 0, 32 * 8 - 1, 15);
32+
putfonts8_asc(binfo->vram, binfo->scrnx, 0, 0, COL8_FFFFFF, "INT 21 (IRQ-1) : PS/2 keyboard");
33+
for (;;) {
34+
io_hlt();
35+
}
36+
}
37+
38+
void inthandler2c(int *esp)
39+
/* 来自PS/2鼠标的中断 */
40+
{
41+
struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO;
42+
boxfill8(binfo->vram, binfo->scrnx, COL8_000000, 0, 0, 32 * 8 - 1, 15);
43+
putfonts8_asc(binfo->vram, binfo->scrnx, 0, 0, COL8_FFFFFF, "INT 2C (IRQ-12) : PS/2 mouse");
44+
for (;;) {
45+
io_hlt();
46+
}
47+
}
48+
49+
void inthandler27(int *esp)
50+
/* PIC0中断的不完整策略 */
51+
/* 这个中断在Athlon64X2上通过芯片组提供的便利,只需执行一次 */
52+
/* 这个中断只是接收,不执行任何操作 */
53+
/* 为什么不处理?
54+
→ 因为这个中断可能是电气噪声引发的、只是处理一些重要的情况。*/
55+
{
56+
io_out8(PIC0_OCW2, 0x67); /* 通知PIC的IRQ-07(参考7-1) */
57+
return;
58+
}

06_day/naskfunc.nas

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
GLOBAL _io_out8, _io_out16, _io_out32
1212
GLOBAL _io_load_eflags, _io_store_eflags
1313
GLOBAL _load_gdtr, _load_idtr
14+
GLOBAL _asm_inthandler21, _asm_inthandler27, _asm_inthandler2c
15+
EXTERN _inthandler21, _inthandler27, _inthandler2c
1416

1517
[SECTION .text]
1618

@@ -76,7 +78,7 @@ _io_store_eflags: ; void io_store_eflags(int eflags);
7678
PUSH EAX
7779
POPFD ; POP EFLAGS
7880
RET
79-
81+
8082
_load_gdtr: ; void load_gdtr(int limit, int addr);
8183
MOV AX,[ESP+4] ; limit
8284
MOV [ESP+6],AX
@@ -87,4 +89,52 @@ _load_idtr: ; void load_idtr(int limit, int addr);
8789
MOV AX,[ESP+4] ; limit
8890
MOV [ESP+6],AX
8991
LIDT [ESP+6]
90-
RET
92+
RET
93+
94+
_asm_inthandler21:
95+
PUSH ES
96+
PUSH DS
97+
PUSHAD
98+
MOV EAX,ESP
99+
PUSH EAX
100+
MOV AX,SS
101+
MOV DS,AX
102+
MOV ES,AX
103+
CALL _inthandler21
104+
POP EAX
105+
POPAD
106+
POP DS
107+
POP ES
108+
IRETD
109+
110+
_asm_inthandler27:
111+
PUSH ES
112+
PUSH DS
113+
PUSHAD
114+
MOV EAX,ESP
115+
PUSH EAX
116+
MOV AX,SS
117+
MOV DS,AX
118+
MOV ES,AX
119+
CALL _inthandler27
120+
POP EAX
121+
POPAD
122+
POP DS
123+
POP ES
124+
IRETD
125+
126+
_asm_inthandler2c:
127+
PUSH ES
128+
PUSH DS
129+
PUSHAD
130+
MOV EAX,ESP
131+
PUSH EAX
132+
MOV AX,SS
133+
MOV DS,AX
134+
MOV ES,AX
135+
CALL _inthandler2c
136+
POP EAX
137+
POPAD
138+
POP DS
139+
POP ES
140+
IRETD

0 commit comments

Comments
 (0)