1 | ; File name : keys.asm |
---|
2 | ; Project name : Keyboard library |
---|
3 | ; Created date : 17.11.2009 |
---|
4 | ; Last update : 31.12.2009 |
---|
5 | ; Author : Tomi Tilli |
---|
6 | ; Description : ASM library to for keyboard related functions. |
---|
7 | |
---|
8 | ;--------------- Equates ----------------------------- |
---|
9 | |
---|
10 | ; String library function to include |
---|
11 | %define USE_KEYS_STROKE ; Keys_GetStroke, Keys_IsStroke and Keys_WaitStroke |
---|
12 | ;%define USE_KEYS_PRNTGETUINT ; Keys_PrintGetUint |
---|
13 | ;%define USE_KEYS_PRNTGETYN ; Keys_PrintGetYN |
---|
14 | %define USE_KEYS_CLRBUFFER ; Keys_ClrBuffer |
---|
15 | |
---|
16 | ; Some BIOS Scan Codes |
---|
17 | KEY_BSC_F1 EQU 3Bh |
---|
18 | KEY_BSC_F2 EQU 3Ch |
---|
19 | KEY_BSC_F3 EQU 3Dh |
---|
20 | KEY_BSC_F4 EQU 3Eh |
---|
21 | KEY_BSC_F5 EQU 3Fh |
---|
22 | KEY_BSC_F6 EQU 40h |
---|
23 | KEY_BSC_F7 EQU 41h |
---|
24 | KEY_BSC_F8 EQU 42h |
---|
25 | KEY_BSC_F9 EQU 43h |
---|
26 | KEY_BSC_F10 EQU 44h |
---|
27 | KEY_BSC_F11 EQU 57h |
---|
28 | KEY_BSC_F12 EQU 58h |
---|
29 | |
---|
30 | |
---|
31 | ;-------------- Private global variables ------------- |
---|
32 | ; Section containing initialized data |
---|
33 | ;SECTION .data |
---|
34 | |
---|
35 | |
---|
36 | ;-------------- Public functions --------------------- |
---|
37 | ; Section containing code |
---|
38 | SECTION .text |
---|
39 | |
---|
40 | |
---|
41 | ;-------------------------------------------------------------------- |
---|
42 | ; Checks is keystroke available in keyboard buffer. |
---|
43 | ; |
---|
44 | ; Keys_GetStroke Removes key from buffer if keystroke available |
---|
45 | ; Keys_IsStroke Does not remove key from buffer |
---|
46 | ; Keys_WaitStroke Waits until keystroke is available and removes it from buffer |
---|
47 | ; Parameters: |
---|
48 | ; Nothing |
---|
49 | ; Returns: |
---|
50 | ; AL: ASCII character (if keystroke available) |
---|
51 | ; AH: BIOS scan code (if keystroke available) |
---|
52 | ; ZF: Set if no keystroke available |
---|
53 | ; Cleared if keystroke available |
---|
54 | ; Corrupts registers: |
---|
55 | ; Nothing |
---|
56 | ;-------------------------------------------------------------------- |
---|
57 | %ifdef USE_KEYS_STROKE |
---|
58 | ALIGN JUMP_ALIGN |
---|
59 | Keys_GetStroke: |
---|
60 | call Keys_IsStroke |
---|
61 | jnz Keys_WaitStroke |
---|
62 | ret |
---|
63 | ALIGN JUMP_ALIGN |
---|
64 | Keys_IsStroke: |
---|
65 | mov ah, 01h ; Check for Keystroke |
---|
66 | int 16h |
---|
67 | ret |
---|
68 | ALIGN JUMP_ALIGN |
---|
69 | Keys_WaitStroke: |
---|
70 | xor ah, ah ; Get Keystroke |
---|
71 | int 16h |
---|
72 | test ax, ax ; Clear ZF |
---|
73 | ret |
---|
74 | %endif |
---|
75 | |
---|
76 | |
---|
77 | ;-------------------------------------------------------------------- |
---|
78 | ; Reads keystokes, prints them and converts them to unsigned WORD |
---|
79 | ; after user has pressed ENTER. Returned value is always valid unless |
---|
80 | ; user has cancelled by pressing ESC. |
---|
81 | ; |
---|
82 | ; Keys_PrintGetUint |
---|
83 | ; Parameters: |
---|
84 | ; CX: Base (10=dec, 16=hex etc.) |
---|
85 | ; DX: Max number of characters wanted |
---|
86 | ; Returns: |
---|
87 | ; DX:AX: Unsigned DWORD that the user has entered |
---|
88 | ; CF: Set if unsigned DWORD read successfully |
---|
89 | ; Cleared is user has cancelled |
---|
90 | ; Corrupts registers: |
---|
91 | ; BX |
---|
92 | ;-------------------------------------------------------------------- |
---|
93 | %ifdef USE_KEYS_PRNTGETUINT |
---|
94 | ALIGN JUMP_ALIGN |
---|
95 | Keys_PrintGetUint: |
---|
96 | push es |
---|
97 | push di |
---|
98 | push si |
---|
99 | |
---|
100 | ; Reserve stack for buffer |
---|
101 | inc dx ; Increment for STOP char |
---|
102 | push ss ; Copy SS... |
---|
103 | pop es ; ...to ES |
---|
104 | mov si, sp ; Backup SP |
---|
105 | sub sp, dx ; Reserve buffer from stack |
---|
106 | mov di, sp ; ES:DI now points to buffer |
---|
107 | |
---|
108 | ; Get user inputted string |
---|
109 | mov bx, String_IsBaseChar |
---|
110 | call Keys_GetStrToBuffer |
---|
111 | jnc .Return ; Return if user cancellation |
---|
112 | |
---|
113 | ; Convert string to DWORD |
---|
114 | call String_StrToUInt ; Get DWORD to DX:AX |
---|
115 | |
---|
116 | ; Return |
---|
117 | ALIGN JUMP_ALIGN |
---|
118 | .Return: |
---|
119 | mov sp, si ; Destroy stack buffer |
---|
120 | pop si |
---|
121 | pop di |
---|
122 | pop es |
---|
123 | ret |
---|
124 | %endif |
---|
125 | |
---|
126 | |
---|
127 | ;-------------------------------------------------------------------- |
---|
128 | ; Reads keystokes until Y or N is pressed. |
---|
129 | ; CF will be set to match selection. |
---|
130 | ; |
---|
131 | ; Keys_PrintGetYN |
---|
132 | ; Parameters: |
---|
133 | ; Nothing |
---|
134 | ; Returns: |
---|
135 | ; AX: 'Y' if Y pressed |
---|
136 | ; 'N' if N pressed |
---|
137 | ; Zero if ESC pressed |
---|
138 | ; CF: Set if Y pressed |
---|
139 | ; Cleared if N or ESC pressed |
---|
140 | ; Corrupts registers: |
---|
141 | ; Nothing |
---|
142 | ;-------------------------------------------------------------------- |
---|
143 | %ifdef USE_KEYS_PRNTGETYN |
---|
144 | ALIGN JUMP_ALIGN |
---|
145 | Keys_PrintGetYN: |
---|
146 | call Keys_WaitStroke ; Wait until keystroke |
---|
147 | call String_ToLower ; Char in AL to lower case |
---|
148 | cmp al, 'y' ; Y pressed? |
---|
149 | je .RetY ; If so, return |
---|
150 | cmp al, 'n' ; N pressed? |
---|
151 | je .RetN ; If so, return |
---|
152 | cmp al, ESC ; ESC pressed? |
---|
153 | je .RetEsc ; If so, return N |
---|
154 | call Keys_Bell ; Play error sound |
---|
155 | jmp Keys_PrintGetYN ; Loop until right key pressed |
---|
156 | ALIGN JUMP_ALIGN |
---|
157 | .RetY: |
---|
158 | mov ax, 'Y' |
---|
159 | stc |
---|
160 | ret |
---|
161 | ALIGN JUMP_ALIGN |
---|
162 | .RetN: |
---|
163 | mov ax, 'N' |
---|
164 | ret |
---|
165 | ALIGN JUMP_ALIGN |
---|
166 | .RetEsc: |
---|
167 | xor ax, ax |
---|
168 | ret |
---|
169 | %endif |
---|
170 | |
---|
171 | |
---|
172 | ;-------------------------------------------------------------------- |
---|
173 | ; Clears BIOS keystroke buffer. |
---|
174 | ; |
---|
175 | ; Keys_ClrBuffer |
---|
176 | ; Parameters: |
---|
177 | ; Nothing |
---|
178 | ; Returns: |
---|
179 | ; Nothing |
---|
180 | ; Corrupts registers: |
---|
181 | ; AX |
---|
182 | ;-------------------------------------------------------------------- |
---|
183 | %ifdef USE_KEYS_CLRBUFFER |
---|
184 | ALIGN JUMP_ALIGN |
---|
185 | Keys_ClrBuffer: |
---|
186 | call Keys_GetStroke ; Get keystroke |
---|
187 | jnz Keys_ClrBuffer ; Loop if keystroke found in buffer |
---|
188 | ret |
---|
189 | %endif |
---|
190 | |
---|
191 | |
---|
192 | ;-------------- Private functions --------------------- |
---|
193 | |
---|
194 | ;-------------------------------------------------------------------- |
---|
195 | ; Reads user inputted string to buffer. Character verification is |
---|
196 | ; supported to ignore unwanted characters. |
---|
197 | ; Function returns when ENTER or ESC will be pressed. |
---|
198 | ; |
---|
199 | ; Keys_GetStrToBuffer |
---|
200 | ; Parameters: |
---|
201 | ; DX: Buffer length in characters with STOP included |
---|
202 | ; ES:DI: Ptr to buffer |
---|
203 | ; CS:BX: Ptr to char verification function: |
---|
204 | ; Parameters: |
---|
205 | ; AL: Character to verify |
---|
206 | ; CX: Anything passed to Keys_GetStrToBuffer |
---|
207 | ; Returns: |
---|
208 | ; CF: Set if character is wanted type |
---|
209 | ; Cleared if character is not wanted |
---|
210 | ; Corrupts registers: |
---|
211 | ; Nothing |
---|
212 | ; Returns: |
---|
213 | ; AX: String length in characters without STOP |
---|
214 | ; CF: Set if string has been read successfully |
---|
215 | ; Cleared if user cancellation |
---|
216 | ; Corrupts registers: |
---|
217 | ; DX |
---|
218 | ;-------------------------------------------------------------------- |
---|
219 | %ifdef USE_KEYS_PRNTGETUINT |
---|
220 | ALIGN JUMP_ALIGN |
---|
221 | Keys_GetStrToBuffer: |
---|
222 | push di |
---|
223 | push si |
---|
224 | dec dx ; Decrement for STOP char |
---|
225 | xor si, si ; Zero char counter |
---|
226 | cld ; STOSB to increment DI |
---|
227 | ALIGN JUMP_ALIGN |
---|
228 | .CharLoop: |
---|
229 | call Keys_WaitStroke ; Wait for keystroke |
---|
230 | cmp al, CR ; Enter pressed? |
---|
231 | je .End ; If so, return (CF cleared) |
---|
232 | cmp al, ESC ; Esc pressed? |
---|
233 | je .Cancel ; If so, jump to cancel |
---|
234 | cmp al, BS ; Backspace pressed? |
---|
235 | je .Backspace ; If so, jump to process it |
---|
236 | call bx ; Is wanted character? |
---|
237 | jnc .Bell ; If not, play error sound and wait next key |
---|
238 | |
---|
239 | ; Limit char count |
---|
240 | cmp si, dx ; Max number of chars entered? |
---|
241 | jae .CharLoop ; If so, wait for ENTER or BACKSPACE |
---|
242 | |
---|
243 | ; Write base character in AL |
---|
244 | stosb ; Store char from AL to [ES:DI] |
---|
245 | inc si ; Increment char count |
---|
246 | push dx |
---|
247 | mov dl, al ; Copy char to DL |
---|
248 | PRINT_CHAR ; Print character |
---|
249 | pop dx |
---|
250 | jmp .CharLoop ; Jump to wait next char |
---|
251 | ALIGN JUMP_ALIGN |
---|
252 | .Bell: |
---|
253 | call Keys_Bell |
---|
254 | jmp .CharLoop ; Jump to wait next char |
---|
255 | ALIGN JUMP_ALIGN |
---|
256 | .Backspace: |
---|
257 | call Keys_Backspace |
---|
258 | jmp .CharLoop ; Jump to wait next char |
---|
259 | ALIGN JUMP_ALIGN |
---|
260 | .Cancel: |
---|
261 | xor si, si ; Zero string length (clear CF) |
---|
262 | jmp .Return |
---|
263 | ALIGN JUMP_ALIGN |
---|
264 | .End: |
---|
265 | stc ; Set CF since success |
---|
266 | .Return: |
---|
267 | mov al, STOP ; End string |
---|
268 | stosb |
---|
269 | mov ax, si ; String length |
---|
270 | pop si |
---|
271 | pop di |
---|
272 | ret |
---|
273 | %endif |
---|
274 | |
---|
275 | |
---|
276 | ;-------------------------------------------------------------------- |
---|
277 | ; Plays bell sound for invalid key presses. |
---|
278 | ; |
---|
279 | ; Keys_Bell |
---|
280 | ; Parameters: |
---|
281 | ; Nothing |
---|
282 | ; Returns: |
---|
283 | ; Nothing |
---|
284 | ; Corrupts registers: |
---|
285 | ; AX |
---|
286 | ;-------------------------------------------------------------------- |
---|
287 | %ifdef USE_KEYS_PRNTGETUINT or USE_KEYS_PRNTGETYN |
---|
288 | ALIGN JUMP_ALIGN |
---|
289 | Keys_Bell: |
---|
290 | push dx |
---|
291 | mov dl, BELL |
---|
292 | PRINT_CHAR |
---|
293 | pop dx |
---|
294 | ret |
---|
295 | %endif |
---|
296 | |
---|
297 | |
---|
298 | ;-------------------------------------------------------------------- |
---|
299 | ; Handles backspace key by removing last written character. |
---|
300 | ; |
---|
301 | ; Keys_Backspace |
---|
302 | ; Parameters: |
---|
303 | ; SI: Character counter |
---|
304 | ; ES:DI: Ptr to buffer |
---|
305 | ; Returns: |
---|
306 | ; Nothing |
---|
307 | ; Corrupts registers: |
---|
308 | ; AX |
---|
309 | ;-------------------------------------------------------------------- |
---|
310 | %ifdef USE_KEYS_PRNTGETUINT |
---|
311 | ALIGN JUMP_ALIGN |
---|
312 | Keys_Backspace: |
---|
313 | push dx |
---|
314 | test si, si ; At the beginning? |
---|
315 | jz .Return ; If so, return |
---|
316 | dec si ; Decrement char counter |
---|
317 | dec di ; Decrement offset to buffer |
---|
318 | mov dl, BS ; Write backspace |
---|
319 | PRINT_CHAR |
---|
320 | mov dl, ' ' ; Write space |
---|
321 | PRINT_CHAR |
---|
322 | mov dl, BS ; Back again |
---|
323 | PRINT_CHAR |
---|
324 | ALIGN JUMP_ALIGN |
---|
325 | .Return: |
---|
326 | pop dx |
---|
327 | ret |
---|
328 | %endif |
---|