1 | ; File name : Keyboard.asm |
---|
2 | ; Project name : Assembly Library |
---|
3 | ; Created date : 5.7.2010 |
---|
4 | ; Last update : 7.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 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 | cmp al, BS ; No bell for backspace |
---|
100 | je SHORT .GetCharacterFromUser |
---|
101 | call Keyboard_PlayBellForUnwantedKeystroke |
---|
102 | jmp SHORT .GetCharacterFromUser |
---|
103 | |
---|
104 | .TerminateStringWithNULL: |
---|
105 | stosb ; Terminate string with NULL |
---|
106 | mov cx, bx ; String length now in CX |
---|
107 | |
---|
108 | .ReturnAfterUpdatingZF: |
---|
109 | CALL_DISPLAY_LIBRARY PopDisplayContext |
---|
110 | test cx, cx ; Clear or set ZF |
---|
111 | pop bx |
---|
112 | pop si |
---|
113 | pop di |
---|
114 | ret |
---|
115 | |
---|
116 | ;-------------------------------------------------------------------- |
---|
117 | ; .PrepareDisplayContextForKeyboardInput |
---|
118 | ; Parameters: |
---|
119 | ; Nothing |
---|
120 | ; Returns: |
---|
121 | ; Nothing (Display context pushed to stack) |
---|
122 | ; Corrupts registers: |
---|
123 | ; AX, BX, SI |
---|
124 | ;-------------------------------------------------------------------- |
---|
125 | ALIGN JUMP_ALIGN |
---|
126 | .PrepareDisplayContextForKeyboardInput: |
---|
127 | pop bx ; Pop return address to BX |
---|
128 | mov si, di |
---|
129 | |
---|
130 | CALL_DISPLAY_LIBRARY PushDisplayContext |
---|
131 | mov ax, CURSOR_NORMAL |
---|
132 | CALL_DISPLAY_LIBRARY SetCursorShapeFromAX |
---|
133 | CALL_DISPLAY_LIBRARY SynchronizeDisplayContextToHardware |
---|
134 | |
---|
135 | mov di, si |
---|
136 | jmp bx |
---|
137 | |
---|
138 | |
---|
139 | ;-------------------------------------------------------------------- |
---|
140 | ; .ProcessControlCharacter |
---|
141 | ; Parameters: |
---|
142 | ; AL: Character inputted by user |
---|
143 | ; CX: Number of bytes left in buffer |
---|
144 | ; BX: Total number of characters inputted |
---|
145 | ; ES:DI: Ptr where to store next character |
---|
146 | ; Returns: |
---|
147 | ; AL: Character inputted by user or NULL if end of input |
---|
148 | ; BX: Cleared if user cancellation |
---|
149 | ; ZF: Set if user has ended or cancelled key input |
---|
150 | ; CF: Set if character is rejected |
---|
151 | ; Corrupts registers: |
---|
152 | ; AH |
---|
153 | ;-------------------------------------------------------------------- |
---|
154 | ALIGN JUMP_ALIGN |
---|
155 | .ProcessControlCharacter: |
---|
156 | cmp al, CR ; ENTER to terminate string? |
---|
157 | je SHORT .EndCharacterInput |
---|
158 | cmp al, ESC ; Cancel input? |
---|
159 | je SHORT .CancelCharacterInput |
---|
160 | cmp al, BS ; Backspace? |
---|
161 | je SHORT .Backspace |
---|
162 | jcxz .RejectCharacter |
---|
163 | test al, al ; Clear ZF and CF |
---|
164 | ret |
---|
165 | |
---|
166 | .Backspace: |
---|
167 | test bx, bx ; At the beginning? |
---|
168 | jz SHORT .RejectCharacter |
---|
169 | inc cx ; Increment bytes left |
---|
170 | dec bx ; Decrement characters inputted |
---|
171 | dec di |
---|
172 | call Keyboard_PrintBackspace |
---|
173 | mov al, BS ; Restore character |
---|
174 | .RejectCharacter: |
---|
175 | test al, al ; Clear ZF... |
---|
176 | stc ; ...and set CF |
---|
177 | ret |
---|
178 | |
---|
179 | .CancelCharacterInput: |
---|
180 | xor bx, bx |
---|
181 | .EndCharacterInput: |
---|
182 | xor al, al ; Set ZF and clear CF |
---|
183 | ret |
---|
184 | |
---|
185 | |
---|
186 | ;-------------------------------------------------------------------- |
---|
187 | ; Keyboard_PrintBackspace |
---|
188 | ; Parameters: |
---|
189 | ; Nothing |
---|
190 | ; Returns: |
---|
191 | ; Nothing |
---|
192 | ; Corrupts registers: |
---|
193 | ; AX |
---|
194 | ;-------------------------------------------------------------------- |
---|
195 | ALIGN JUMP_ALIGN |
---|
196 | Keyboard_PrintBackspace: |
---|
197 | mov al, BS |
---|
198 | call Keyboard_PrintInputtedCharacter |
---|
199 | mov al, ' ' |
---|
200 | call Keyboard_PrintInputtedCharacter |
---|
201 | mov al, BS |
---|
202 | jmp SHORT Keyboard_PrintInputtedCharacter |
---|
203 | |
---|
204 | |
---|
205 | ;-------------------------------------------------------------------- |
---|
206 | ; Keyboard_PlayBellForUnwantedKeystroke |
---|
207 | ; Parameters: |
---|
208 | ; Nothing |
---|
209 | ; Returns: |
---|
210 | ; Nothing |
---|
211 | ; Corrupts registers: |
---|
212 | ; AX |
---|
213 | ;-------------------------------------------------------------------- |
---|
214 | ALIGN JUMP_ALIGN |
---|
215 | Keyboard_PlayBellForUnwantedKeystroke: |
---|
216 | mov al, BELL |
---|
217 | ; Fall to Keyboard_PrintInputtedCharacter |
---|
218 | |
---|
219 | ;-------------------------------------------------------------------- |
---|
220 | ; Keyboard_PrintInputtedCharacter |
---|
221 | ; Parameters: |
---|
222 | ; AL: Character inputted by user |
---|
223 | ; Returns: |
---|
224 | ; Nothing |
---|
225 | ; Corrupts registers: |
---|
226 | ; AX |
---|
227 | ;-------------------------------------------------------------------- |
---|
228 | ALIGN JUMP_ALIGN |
---|
229 | Keyboard_PrintInputtedCharacter: |
---|
230 | push di |
---|
231 | CALL_DISPLAY_LIBRARY PrintCharacterFromAL |
---|
232 | CALL_DISPLAY_LIBRARY SynchronizeDisplayContextToHardware ; Hardware cursor |
---|
233 | pop di |
---|
234 | ret |
---|
235 | |
---|
236 | |
---|
237 | ;-------------------------------------------------------------------- |
---|
238 | ; Keyboard_RemoveAllKeystrokesFromBuffer |
---|
239 | ; Parameters: |
---|
240 | ; Nothing |
---|
241 | ; Returns: |
---|
242 | ; Nothing |
---|
243 | ; Corrupts registers: |
---|
244 | ; AX |
---|
245 | ;-------------------------------------------------------------------- |
---|
246 | ALIGN JUMP_ALIGN |
---|
247 | Keyboard_RemoveAllKeystrokesFromBuffer: |
---|
248 | call Keyboard_GetKeystrokeToAX |
---|
249 | jnz SHORT Keyboard_RemoveAllKeystrokesFromBuffer |
---|
250 | ret |
---|
251 | |
---|
252 | |
---|
253 | ;-------------------------------------------------------------------- |
---|
254 | ; Keyboard_GetKeystrokeToAX |
---|
255 | ; Keyboard_GetKeystrokeToAXandLeaveItToBuffer |
---|
256 | ; Keyboard_GetKeystrokeToAXandWaitIfNecessary |
---|
257 | ; Parameters: |
---|
258 | ; Nothing |
---|
259 | ; Returns: |
---|
260 | ; AL: ASCII character (if keystroke available) |
---|
261 | ; AH: BIOS scan code (if keystroke available) |
---|
262 | ; ZF: Set if no keystroke available |
---|
263 | ; Cleared if keystroke available |
---|
264 | ; Corrupts registers: |
---|
265 | ; Nothing |
---|
266 | ;-------------------------------------------------------------------- |
---|
267 | ALIGN JUMP_ALIGN |
---|
268 | Keyboard_GetKeystrokeToAX: |
---|
269 | call Keyboard_GetKeystrokeToAXandLeaveItToBuffer |
---|
270 | jnz SHORT Keyboard_GetKeystrokeToAXandWaitIfNecessary |
---|
271 | ret |
---|
272 | ALIGN JUMP_ALIGN |
---|
273 | Keyboard_GetKeystrokeToAXandLeaveItToBuffer: |
---|
274 | mov ah, CHECK_FOR_KEYSTROKE |
---|
275 | int BIOS_KEYBOARD_INTERRUPT_16h |
---|
276 | ret |
---|
277 | ALIGN JUMP_ALIGN |
---|
278 | Keyboard_GetKeystrokeToAXandWaitIfNecessary: |
---|
279 | xor ah, ah ; GET_KEYSTROKE |
---|
280 | int BIOS_KEYBOARD_INTERRUPT_16h |
---|
281 | test ax, ax ; Clear ZF |
---|
282 | ret |
---|