keyboard interrupt (untested)
[ld45_start_nothing] / src / keyb.asm
1 ; vi:filetype=nasm ts=8 sts=8 sw=8:
2 %include "intr.inc"
3 %include "kbregs.inc"
4
5         global kb_init
6 kb_init:
7         push kbintr
8         push dword IRQ_TO_INTR(KB_IRQ)
9         call set_intr
10         add esp, 8
11
12         ; enable keyboard interrupt
13         mov eax, KB_CMD_GET_CMDBYTE
14         call send_cmd
15         call read_data
16         or eax, KB_CCB_KB_INTREN        ; set the INTREN flag
17         push eax
18         mov eax, KB_CMD_SET_CMDBYTE
19         call send_cmd
20         pop eax
21         call send_data
22
23         ; flush the read buffer
24         call wait_read
25         jz .skipread
26         call read_data
27 .skipread:
28         ret
29
30         ; keyboard interrupt handler
31 kbintr:
32         pusha
33         in al, KB_DATA_PORT
34         cmp al, 0e0h
35         jnz .noext
36         mov byte [key_ext], 1
37         jmp .eoi
38 .noext:
39         ; keystate[keycode] address in ebx
40         xor ebx, ebx
41         mov bl, al
42         add ebx, keystate
43
44         ; determine if it's a press or release
45         test al, 80h
46         jz .press
47         ; key release
48         mov byte [ebx], 0
49         jmp .eoi
50 .press: ; key press
51         mov byte [ebx], 1
52
53 .eoi:
54         cli
55         end_of_irq KB_IRQ
56
57         popa
58         iret
59
60 %macro iodelay 0
61         xor al, al
62         times 7 out 80h, al
63 %endmacro
64
65 wait_write:
66         push eax
67         push ecx
68         mov ecx, 32768
69 .loop:  in al, KB_STATUS_PORT
70         test al, KB_STAT_INBUF_FULL
71         jz .break
72         iodelay
73         dec ecx
74         jnz .loop
75 .break: pop ecx
76         pop eax
77         ret
78
79 wait_read:
80         push eax
81         push ecx
82         mov ecx, 32768
83 .loop:  in al, KB_STATUS_PORT
84         test al, KB_STAT_OUTBUF_FULL
85         jnz .break
86         iodelay
87         dec ecx
88         jnz .loop
89 .break: pop ecx
90         pop eax
91         ret
92
93         ; expects command in al
94 send_cmd:
95         call wait_write
96         out KB_CMD_PORT, al
97         ret
98
99 read_data:
100 send_data:
101
102         section .data
103         align 4
104 key_ext: db 0
105
106         global keystate
107 keystate: times 256 db 0