ok key release works now
[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         set_irq_vector KB_IRQ, kbintr
8
9         ; enable keyboard interrupt
10         mov eax, KB_CMD_GET_CMDBYTE
11         call send_cmd
12         call read_data
13         or eax, KB_CCB_KB_INTREN        ; set the INTREN flag
14         push eax
15         mov eax, KB_CMD_SET_CMDBYTE
16         call send_cmd
17         pop eax
18         call send_data
19
20         ; flush the read buffer
21         call wait_read
22         jz .skipread
23         call read_data
24 .skipread:
25
26         unmask_irq KB_IRQ
27         ret
28
29         ; keyboard interrupt handler
30 kbintr:
31         pusha
32         in al, KB_DATA_PORT
33         cmp al, 0e0h
34         jnz .noext
35         mov byte [key_ext], 1
36         jmp .eoi
37 .noext:
38         ; keystate[keycode] address in ebx
39         xor ebx, ebx
40         mov bl, al
41         and bl, 07fh
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