source: xtideuniversalbios/trunk/Assembly_Library/Src/Keyboard/Keyboard.asm@ 213

Last change on this file since 213 was 145, checked in by krille_n_@…, 14 years ago

Minor size optimizations to all parts of the project (even the old Configurator).
Also added a new 'release' option to the makefiles of both versions of the Configurator. It invokes UPX and makes the Configurator programs ridiculously tiny.

File size: 8.3 KB
RevLine 
[41]1; Project name : Assembly Library
2; Description : Functions for managing keyboard.
3
4BUFFER_SIZE_FOR_WORD_INPUT EQU 6 ; 5 chars + NULL
5
6; Section containing code
7SECTION .text
8
9;--------------------------------------------------------------------
10; Reads user inputted word.
11; Function returns when ENTER or ESC will be pressed.
[133]12;
[41]13; Keyboard_ReadUserInputtedWordWhilePrinting
14; Parameters
15; BX: Numeric base (10 or 16)
16; Returns:
17; AX: User inputted word
18; ZF: Set if user cancellation
19; Corrupts registers:
20; DX
21;--------------------------------------------------------------------
[131]22%ifndef EXCLUDE_FROM_XTIDE_UNIVERSAL_BIOS
[41]23ALIGN JUMP_ALIGN
24Keyboard_ReadUserInputtedWordWhilePrinting:
[52]25 push ds
26 push si
[41]27 push cx
28
[52]29 mov cx, BUFFER_SIZE_FOR_WORD_INPUT
30 call Memory_ReserveCXbytesFromStackToDSSI
[41]31
32 call Char_GetFilterFunctionToDXforNumericBaseInBX
[54]33 call Registers_ExchangeDSSIwithESDI
[41]34 call Keyboard_ReadUserInputtedStringToESDIWhilePrinting
[54]35 call Registers_ExchangeDSSIwithESDI ; Does not modify FLAGS
[52]36 jz SHORT .CancelledByUser
[41]37
38 call String_ConvertWordToAXfromStringInDSSIwithBaseInBX
[52]39.CancelledByUser:
40 add sp, BYTE BUFFER_SIZE_FOR_WORD_INPUT
[54]41 test cx, cx ; Set ZF if string length is zero
[41]42 pop cx
[52]43 pop si
44 pop ds
[41]45 ret
[131]46%endif
[41]47
48
49;--------------------------------------------------------------------
50; Reads user inputted string to buffer. Character filter is
51; supported to ignore unwanted characters.
52; Function returns when ENTER or ESC will be pressed.
[133]53;
[41]54; Keyboard_ReadUserInputtedStringToESDIWhilePrinting
55; Parameters:
56; CX: Buffer size (with NULL)
57; ES:DI: Ptr to destination buffer
58; CS:DX: Ptr to character filter function:
59; Parameters:
60; AL: Character inputted by user
61; Returns:
62; CF: Set if character is accepted
63; Cleared if character is rejected
64; Corrupts registers:
65; Nothing
66; Returns:
67; CX: String length in characters (without NULL)
68; ZF: Set if user cancellation
69; Corrupts registers:
70; AX
71;--------------------------------------------------------------------
[131]72%ifndef EXCLUDE_FROM_XTIDE_UNIVERSAL_BIOS
[41]73ALIGN JUMP_ALIGN
74Keyboard_ReadUserInputtedStringToESDIWhilePrinting:
75 push di
76 push si
77 push bx
78 call .PrepareDisplayContextForKeyboardInput
79 jcxz .ReturnAfterUpdatingZF
80
81 xor bx, bx ; Zero character counter
82 dec cx ; Decrement buffer size for NULL
83 cld
84ALIGN JUMP_ALIGN
85.GetCharacterFromUser:
86 call Keyboard_GetKeystrokeToAXandWaitIfNecessary ; Get ASCII to AL
87 call .ProcessControlCharacter
88 jz SHORT .TerminateStringWithNULL
89 jc SHORT .PlayBellForRejectedCharacter
90 call dx ; Filter character
91 jnc SHORT .PlayBellForRejectedCharacter
92 inc bx ; Increment number of characters stored
93 stosb ; Store from AL to ES:DI
94 call Keyboard_PrintInputtedCharacter
95 loop .GetCharacterFromUser
96.PlayBellForRejectedCharacter:
[48]97 cmp al, BS ; No bell for backspace
98 je SHORT .GetCharacterFromUser
[41]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;--------------------------------------------------------------------
123ALIGN 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;--------------------------------------------------------------------
152ALIGN 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
[145]161 clc ; Clear CF (ZF is already cleared)
[41]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
[48]171 mov al, BS ; Restore character
[41]172.RejectCharacter:
173 test al, al ; Clear ZF...
174 stc ; ...and set CF
175 ret
176
177.CancelCharacterInput:
178 xor bx, bx
179.EndCharacterInput:
180 xor al, al ; Set ZF and clear CF
181 ret
[131]182%endif ; EXCLUDE_FROM_XTIDE_UNIVERSAL_BIOS
[41]183
184
185;--------------------------------------------------------------------
186; Keyboard_PrintBackspace
187; Parameters:
188; Nothing
189; Returns:
190; Nothing
191; Corrupts registers:
192; AX
193;--------------------------------------------------------------------
[131]194%ifndef EXCLUDE_FROM_XTIDE_UNIVERSAL_BIOS
[41]195ALIGN JUMP_ALIGN
196Keyboard_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
[131]203%endif
[41]204
205
206;--------------------------------------------------------------------
207; Keyboard_PlayBellForUnwantedKeystroke
208; Parameters:
209; Nothing
210; Returns:
211; Nothing
212; Corrupts registers:
213; AX
214;--------------------------------------------------------------------
[131]215%ifndef EXCLUDE_FROM_XTIDE_UNIVERSAL_BIOS
[41]216ALIGN JUMP_ALIGN
217Keyboard_PlayBellForUnwantedKeystroke:
218 mov al, BELL
219 ; Fall to Keyboard_PrintInputtedCharacter
[131]220%endif
[41]221
222;--------------------------------------------------------------------
223; Keyboard_PrintInputtedCharacter
224; Parameters:
225; AL: Character inputted by user
226; Returns:
227; Nothing
228; Corrupts registers:
229; AX
230;--------------------------------------------------------------------
[131]231%ifndef EXCLUDE_FROM_XTIDE_UNIVERSAL_BIOS
[41]232ALIGN JUMP_ALIGN
233Keyboard_PrintInputtedCharacter:
234 push di
235 CALL_DISPLAY_LIBRARY PrintCharacterFromAL
[48]236 CALL_DISPLAY_LIBRARY SynchronizeDisplayContextToHardware ; Hardware cursor
[41]237 pop di
238 ret
[131]239%endif
[41]240
241
242;--------------------------------------------------------------------
243; Keyboard_RemoveAllKeystrokesFromBuffer
244; Parameters:
245; Nothing
246; Returns:
247; Nothing
248; Corrupts registers:
249; AX
250;--------------------------------------------------------------------
[133]251%ifndef EXCLUDE_FROM_XTIDE_UNIVERSAL_BIOS ; Only used when debugging
[41]252ALIGN JUMP_ALIGN
253Keyboard_RemoveAllKeystrokesFromBuffer:
254 call Keyboard_GetKeystrokeToAX
255 jnz SHORT Keyboard_RemoveAllKeystrokesFromBuffer
256 ret
[133]257%endif
[41]258
259
260;--------------------------------------------------------------------
261; Keyboard_GetKeystrokeToAX
262; Keyboard_GetKeystrokeToAXandLeaveItToBuffer
263; Keyboard_GetKeystrokeToAXandWaitIfNecessary
264; Parameters:
265; Nothing
266; Returns:
267; AL: ASCII character (if keystroke available)
268; AH: BIOS scan code (if keystroke available)
269; ZF: Set if no keystroke available
270; Cleared if keystroke available
271; Corrupts registers:
272; Nothing
273;--------------------------------------------------------------------
274ALIGN JUMP_ALIGN
275Keyboard_GetKeystrokeToAXandLeaveItToBuffer:
276 mov ah, CHECK_FOR_KEYSTROKE
277 int BIOS_KEYBOARD_INTERRUPT_16h
278 ret
279ALIGN JUMP_ALIGN
[133]280Keyboard_GetKeystrokeToAX:
281 call Keyboard_GetKeystrokeToAXandLeaveItToBuffer
282 jz SHORT Keyboard_GetKeystrokeToAXReturn
283 ; Fall to Keyboard_GetKeystrokeToAXandWaitIfNecessary
284ALIGN JUMP_ALIGN
[41]285Keyboard_GetKeystrokeToAXandWaitIfNecessary:
286 xor ah, ah ; GET_KEYSTROKE
287 int BIOS_KEYBOARD_INTERRUPT_16h
288 test ax, ax ; Clear ZF
[133]289Keyboard_GetKeystrokeToAXReturn:
[41]290 ret
Note: See TracBrowser for help on using the repository browser.