[41] | 1 | ; File name : Keyboard.asm |
---|
| 2 | ; Project name : Assembly Library |
---|
| 3 | ; Created date : 5.7.2010 |
---|
| 4 | ; Last update : 12.8.2010 |
---|
| 5 | ; Author : Tomi Tilli |
---|
| 6 | ; Description : Functions for managing keyboard. |
---|
| 7 | |
---|
| 8 | BUFFER_SIZE_FOR_WORD_INPUT EQU 6 ; 5 chars + NULL |
---|
| 9 | |
---|
| 10 | ; Section containing code |
---|
| 11 | SECTION .text |
---|
| 12 | |
---|
| 13 | ;-------------------------------------------------------------------- |
---|
| 14 | ; Reads user inputted word. |
---|
| 15 | ; Function returns when ENTER or ESC will be pressed. |
---|
| 16 | ; |
---|
| 17 | ; Keyboard_ReadUserInputtedWordWhilePrinting |
---|
| 18 | ; Parameters |
---|
| 19 | ; BX: Numeric base (10 or 16) |
---|
| 20 | ; Returns: |
---|
| 21 | ; AX: User inputted word |
---|
| 22 | ; ZF: Set if user cancellation |
---|
| 23 | ; Corrupts registers: |
---|
| 24 | ; DX |
---|
| 25 | ;-------------------------------------------------------------------- |
---|
| 26 | ALIGN JUMP_ALIGN |
---|
| 27 | Keyboard_ReadUserInputtedWordWhilePrinting: |
---|
| 28 | push es |
---|
| 29 | push di |
---|
| 30 | push cx |
---|
| 31 | |
---|
| 32 | eENTER_STRUCT BUFFER_SIZE_FOR_WORD_INPUT |
---|
| 33 | call Memory_CopySSBPtoESDI |
---|
| 34 | |
---|
| 35 | mov cx, BUFFER_SIZE_FOR_WORD_INPUT |
---|
| 36 | call Char_GetFilterFunctionToDXforNumericBaseInBX |
---|
| 37 | call Keyboard_ReadUserInputtedStringToESDIWhilePrinting |
---|
| 38 | jz SHORT .Return |
---|
| 39 | |
---|
| 40 | call Memory_ExchangeDSSIwithESDI |
---|
| 41 | call String_ConvertWordToAXfromStringInDSSIwithBaseInBX |
---|
| 42 | call Memory_ExchangeDSSIwithESDI |
---|
| 43 | .Return: |
---|
| 44 | eLEAVE_STRUCT BUFFER_SIZE_FOR_WORD_INPUT |
---|
| 45 | test cx, cx ; Set ZF if string length is zero |
---|
| 46 | pop cx |
---|
| 47 | pop di |
---|
| 48 | pop es |
---|
| 49 | ret |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | ;-------------------------------------------------------------------- |
---|
| 53 | ; Reads user inputted string to buffer. Character filter is |
---|
| 54 | ; supported to ignore unwanted characters. |
---|
| 55 | ; Function returns when ENTER or ESC will be pressed. |
---|
| 56 | ; |
---|
| 57 | ; Keyboard_ReadUserInputtedStringToESDIWhilePrinting |
---|
| 58 | ; Parameters: |
---|
| 59 | ; CX: Buffer size (with NULL) |
---|
| 60 | ; ES:DI: Ptr to destination buffer |
---|
| 61 | ; CS:DX: Ptr to character filter function: |
---|
| 62 | ; Parameters: |
---|
| 63 | ; AL: Character inputted by user |
---|
| 64 | ; Returns: |
---|
| 65 | ; CF: Set if character is accepted |
---|
| 66 | ; Cleared if character is rejected |
---|
| 67 | ; Corrupts registers: |
---|
| 68 | ; Nothing |
---|
| 69 | ; Returns: |
---|
| 70 | ; CX: String length in characters (without NULL) |
---|
| 71 | ; ZF: Set if user cancellation |
---|
| 72 | ; Corrupts registers: |
---|
| 73 | ; AX |
---|
| 74 | ;-------------------------------------------------------------------- |
---|
| 75 | ALIGN JUMP_ALIGN |
---|
| 76 | Keyboard_ReadUserInputtedStringToESDIWhilePrinting: |
---|
| 77 | push di |
---|
| 78 | push si |
---|
| 79 | push bx |
---|
| 80 | call .PrepareDisplayContextForKeyboardInput |
---|
| 81 | jcxz .ReturnAfterUpdatingZF |
---|
| 82 | |
---|
| 83 | xor bx, bx ; Zero character counter |
---|
| 84 | dec cx ; Decrement buffer size for NULL |
---|
| 85 | cld |
---|
| 86 | ALIGN JUMP_ALIGN |
---|
| 87 | .GetCharacterFromUser: |
---|
| 88 | call Keyboard_GetKeystrokeToAXandWaitIfNecessary ; Get ASCII to AL |
---|
| 89 | call .ProcessControlCharacter |
---|
| 90 | jz SHORT .TerminateStringWithNULL |
---|
| 91 | jc SHORT .PlayBellForRejectedCharacter |
---|
| 92 | call dx ; Filter character |
---|
| 93 | jnc SHORT .PlayBellForRejectedCharacter |
---|
| 94 | inc bx ; Increment number of characters stored |
---|
| 95 | stosb ; Store from AL to ES:DI |
---|
| 96 | call Keyboard_PrintInputtedCharacter |
---|
| 97 | loop .GetCharacterFromUser |
---|
| 98 | .PlayBellForRejectedCharacter: |
---|
| 99 | call Keyboard_PlayBellForUnwantedKeystroke |
---|
| 100 | jmp SHORT .GetCharacterFromUser |
---|
| 101 | |
---|
| 102 | .TerminateStringWithNULL: |
---|
| 103 | stosb ; Terminate string with NULL |
---|
| 104 | mov cx, bx ; String length now in CX |
---|
| 105 | |
---|
| 106 | .ReturnAfterUpdatingZF: |
---|
| 107 | CALL_DISPLAY_LIBRARY PopDisplayContext |
---|
| 108 | test cx, cx ; Clear or set ZF |
---|
| 109 | pop bx |
---|
| 110 | pop si |
---|
| 111 | pop di |
---|
| 112 | ret |
---|
| 113 | |
---|
| 114 | ;-------------------------------------------------------------------- |
---|
| 115 | ; .PrepareDisplayContextForKeyboardInput |
---|
| 116 | ; Parameters: |
---|
| 117 | ; Nothing |
---|
| 118 | ; Returns: |
---|
| 119 | ; Nothing (Display context pushed to stack) |
---|
| 120 | ; Corrupts registers: |
---|
| 121 | ; AX, BX, SI |
---|
| 122 | ;-------------------------------------------------------------------- |
---|
| 123 | ALIGN JUMP_ALIGN |
---|
| 124 | .PrepareDisplayContextForKeyboardInput: |
---|
| 125 | pop bx ; Pop return address to BX |
---|
| 126 | mov si, di |
---|
| 127 | |
---|
| 128 | CALL_DISPLAY_LIBRARY PushDisplayContext |
---|
| 129 | mov ax, CURSOR_NORMAL |
---|
| 130 | CALL_DISPLAY_LIBRARY SetCursorShapeFromAX |
---|
| 131 | CALL_DISPLAY_LIBRARY SynchronizeDisplayContextToHardware |
---|
| 132 | |
---|
| 133 | mov di, si |
---|
| 134 | jmp bx |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | ;-------------------------------------------------------------------- |
---|
| 138 | ; .ProcessControlCharacter |
---|
| 139 | ; Parameters: |
---|
| 140 | ; AL: Character inputted by user |
---|
| 141 | ; CX: Number of bytes left in buffer |
---|
| 142 | ; BX: Total number of characters inputted |
---|
| 143 | ; ES:DI: Ptr where to store next character |
---|
| 144 | ; Returns: |
---|
| 145 | ; AL: Character inputted by user or NULL if end of input |
---|
| 146 | ; BX: Cleared if user cancellation |
---|
| 147 | ; ZF: Set if user has ended or cancelled key input |
---|
| 148 | ; CF: Set if character is rejected |
---|
| 149 | ; Corrupts registers: |
---|
| 150 | ; AH |
---|
| 151 | ;-------------------------------------------------------------------- |
---|
| 152 | ALIGN JUMP_ALIGN |
---|
| 153 | .ProcessControlCharacter: |
---|
| 154 | cmp al, CR ; ENTER to terminate string? |
---|
| 155 | je SHORT .EndCharacterInput |
---|
| 156 | cmp al, ESC ; Cancel input? |
---|
| 157 | je SHORT .CancelCharacterInput |
---|
| 158 | cmp al, BS ; Backspace? |
---|
| 159 | je SHORT .Backspace |
---|
| 160 | jcxz .RejectCharacter |
---|
| 161 | test al, al ; Clear ZF and CF |
---|
| 162 | ret |
---|
| 163 | |
---|
| 164 | .Backspace: |
---|
| 165 | test bx, bx ; At the beginning? |
---|
| 166 | jz SHORT .RejectCharacter |
---|
| 167 | inc cx ; Increment bytes left |
---|
| 168 | dec bx ; Decrement characters inputted |
---|
| 169 | dec di |
---|
| 170 | call Keyboard_PrintBackspace |
---|
| 171 | .RejectCharacter: |
---|
| 172 | test al, al ; Clear ZF... |
---|
| 173 | stc ; ...and set CF |
---|
| 174 | ret |
---|
| 175 | |
---|
| 176 | .CancelCharacterInput: |
---|
| 177 | xor bx, bx |
---|
| 178 | .EndCharacterInput: |
---|
| 179 | xor al, al ; Set ZF and clear CF |
---|
| 180 | ret |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | ;-------------------------------------------------------------------- |
---|
| 184 | ; Keyboard_PrintBackspace |
---|
| 185 | ; Parameters: |
---|
| 186 | ; Nothing |
---|
| 187 | ; Returns: |
---|
| 188 | ; Nothing |
---|
| 189 | ; Corrupts registers: |
---|
| 190 | ; AX |
---|
| 191 | ;-------------------------------------------------------------------- |
---|
| 192 | ALIGN JUMP_ALIGN |
---|
| 193 | Keyboard_PrintBackspace: |
---|
| 194 | mov al, BS |
---|
| 195 | call Keyboard_PrintInputtedCharacter |
---|
| 196 | mov al, ' ' |
---|
| 197 | call Keyboard_PrintInputtedCharacter |
---|
| 198 | mov al, BS |
---|
| 199 | jmp SHORT Keyboard_PrintInputtedCharacter |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | ;-------------------------------------------------------------------- |
---|
| 203 | ; Keyboard_PlayBellForUnwantedKeystroke |
---|
| 204 | ; Parameters: |
---|
| 205 | ; Nothing |
---|
| 206 | ; Returns: |
---|
| 207 | ; Nothing |
---|
| 208 | ; Corrupts registers: |
---|
| 209 | ; AX |
---|
| 210 | ;-------------------------------------------------------------------- |
---|
| 211 | ALIGN JUMP_ALIGN |
---|
| 212 | Keyboard_PlayBellForUnwantedKeystroke: |
---|
| 213 | mov al, BELL |
---|
| 214 | ; Fall to Keyboard_PrintInputtedCharacter |
---|
| 215 | |
---|
| 216 | ;-------------------------------------------------------------------- |
---|
| 217 | ; Keyboard_PrintInputtedCharacter |
---|
| 218 | ; Parameters: |
---|
| 219 | ; AL: Character inputted by user |
---|
| 220 | ; Returns: |
---|
| 221 | ; Nothing |
---|
| 222 | ; Corrupts registers: |
---|
| 223 | ; AX |
---|
| 224 | ;-------------------------------------------------------------------- |
---|
| 225 | ALIGN JUMP_ALIGN |
---|
| 226 | Keyboard_PrintInputtedCharacter: |
---|
| 227 | push di |
---|
| 228 | CALL_DISPLAY_LIBRARY PrintCharacterFromAL |
---|
| 229 | CALL_DISPLAY_LIBRARY SynchronizeDisplayContextToHardware |
---|
| 230 | pop di |
---|
| 231 | ret |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | ;-------------------------------------------------------------------- |
---|
| 235 | ; Keyboard_RemoveAllKeystrokesFromBuffer |
---|
| 236 | ; Parameters: |
---|
| 237 | ; Nothing |
---|
| 238 | ; Returns: |
---|
| 239 | ; Nothing |
---|
| 240 | ; Corrupts registers: |
---|
| 241 | ; AX |
---|
| 242 | ;-------------------------------------------------------------------- |
---|
| 243 | ALIGN JUMP_ALIGN |
---|
| 244 | Keyboard_RemoveAllKeystrokesFromBuffer: |
---|
| 245 | call Keyboard_GetKeystrokeToAX |
---|
| 246 | jnz SHORT Keyboard_RemoveAllKeystrokesFromBuffer |
---|
| 247 | ret |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | ;-------------------------------------------------------------------- |
---|
| 251 | ; Keyboard_GetKeystrokeToAX |
---|
| 252 | ; Keyboard_GetKeystrokeToAXandLeaveItToBuffer |
---|
| 253 | ; Keyboard_GetKeystrokeToAXandWaitIfNecessary |
---|
| 254 | ; Parameters: |
---|
| 255 | ; Nothing |
---|
| 256 | ; Returns: |
---|
| 257 | ; AL: ASCII character (if keystroke available) |
---|
| 258 | ; AH: BIOS scan code (if keystroke available) |
---|
| 259 | ; ZF: Set if no keystroke available |
---|
| 260 | ; Cleared if keystroke available |
---|
| 261 | ; Corrupts registers: |
---|
| 262 | ; Nothing |
---|
| 263 | ;-------------------------------------------------------------------- |
---|
| 264 | ALIGN JUMP_ALIGN |
---|
| 265 | Keyboard_GetKeystrokeToAX: |
---|
| 266 | call Keyboard_GetKeystrokeToAXandLeaveItToBuffer |
---|
| 267 | jnz SHORT Keyboard_GetKeystrokeToAXandWaitIfNecessary |
---|
| 268 | ret |
---|
| 269 | ALIGN JUMP_ALIGN |
---|
| 270 | Keyboard_GetKeystrokeToAXandLeaveItToBuffer: |
---|
| 271 | mov ah, CHECK_FOR_KEYSTROKE |
---|
| 272 | int BIOS_KEYBOARD_INTERRUPT_16h |
---|
| 273 | ret |
---|
| 274 | ALIGN JUMP_ALIGN |
---|
| 275 | Keyboard_GetKeystrokeToAXandWaitIfNecessary: |
---|
| 276 | xor ah, ah ; GET_KEYSTROKE |
---|
| 277 | int BIOS_KEYBOARD_INTERRUPT_16h |
---|
| 278 | test ax, ax ; Clear ZF |
---|
| 279 | ret |
---|