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