This code example provides a set of keyboard routines to control sound output while waiting for a user to enter a keyboard character. The advantage to this method is that a main routine can call these sound routines to play a sound sequence, and the sound routines will return control back to the main routine whenever the user enters keyboard data so that the main routine can continue computing while the sound plays in the background. The code example has two different code entry points for getting keyboard data. One code entry point is a standard get_keyinput call which will wait for a key and update the sound data until a key code is found. The other code entry point is the get_keyinput_to call, which will wait a set amount of time for a key code and if none is found, return with a no key code found condition. The calling routine puts a timeout counter value in register AX on entry. The counter value is based on the system clock which ticks at 18.2 times per second. The entry point start_table_sound is used to begin a background sound sequence. On entry, the register BX indexes a table of sound data. The table has a format of four byte entries and is terminated by a data word of zero. The four bytes are used as two words: the first is a duration count and the second is a tone value. There are two code entry points for turning the background sound off and on. There is also a utility to flush out the keyboard buffer that can be executed with a call to flush_keyboard.
;Set of keyboard routines with sound outputs .MODEL small .STACK 500 .DATA ;define table for sound output ;sample_sounds dw 8,45000 ;long low sound ; dw 2,2000 ;short high sound ; dw 0 ;end of sample sound table sound_table dw 0 sound_time_m dw 0 sound_time_l dw 0 sound_flag db 0 sound_on_flag db 0,0 key_time_out_m dw 0 key_time_out_l dw 0 .CODE ;************ ^^^^^^^^^^ ************* ;### code entry point ##### get_keyinput proc near ;this routine checks for keyboard data in BIOS buffer ; and returns with data if there ;else it updates sound output data and loops to check for ; keyboard data again until keyboard data found ;on exit AX has keyboard data public get_keyinput push bx push cx push dx get_keyinput_loop: mov ah,1 ;set AH for scan int 16H ;BIOS Call ;branch if no keyboard data jz sound_update mov ah,0 ;set AH for get key int 16H ;BIOS Call pop dx pop cx pop bx ret ;******* -------- ******* sound_update: cmp sound_flag,0 ;check for sound on???? jz get_keyinput_loop ;branch out if sound off mov cx,sound_time_m ;else check for sound update mov ax,sound_time_l call test_current_time ;is it time for update ?? jc get_keyinput_loop ;branch if not time mov bx,sound_table mov ax,[bx] ;get next sound update value or ax,ax ;?? end of sound ?? jz turn_sound_off ;branch if end sound call get_time_plus_ax ;reset sound duration mov sound_time_m,cx mov sound_time_l,ax inc bx inc bx mov ax,[bx] inc bx inc bx mov sound_table,bx call sound_out_ax ;go set sound frequency jmp get_keyinput_loop ;branch to keyboard loop turn_sound