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