1 ; THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
2 ; SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
3 ; END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
4 ; ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
5 ; IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
6 ; SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
7 ; FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
8 ; CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
9 ; AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
10 ; COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
11 ;***************************************************************************
12 ;***************************************************************************
15 ;***** K E Y . A S M *****
17 ;***** Contains routines to get, buffer, and check key presses. *****
20 ;***** PROCEDURES *****
22 ;***** key_init() - Activates the keyboard package. *****
23 ;***** key_close() - Deactivates the keyboard package. *****
24 ;***** key_check() - Returns 1 if a buffered key is waiting. *****
25 ;***** key_getch() - Waits for and returns a buffered keypress. *****
26 ;***** key_flush() - Clears buffers and state array. *****
27 ;***** key_time() - Index by scan code. Contains the time key has been *****
28 ;***** held down. NOT DONE YET. *****
31 ;***** VARIABLES *****
33 ;***** keyd_buffer_type -Set to 0 and key_getch() always returns 0. *****
34 ;***** Set to 1 to so that ASCII codes are returned *****
35 ;***** by key_getch(). Set to 2 and key_getch() returns*****
36 ;***** the buffered keyboard scan codes. *****
37 ;***** keyd_repeat - Set to 0 to not allow repeated keys in the *****
38 ;***** keyboard buffer. Set to 1 to allow repeats. *****
39 ;***** keyd_pressed[] -Index by scan code. Contains 1 if key down else 0*****
42 ;***** CONSTANTS *****
44 ;***** Setting the DEBUG to 1 at compile time passes SysReq through *****
45 ;***** to the debugger, and when the debugger is active, it will give *****
46 ;***** the debugger any keys that are pressed. Note that this only *****
47 ;***** works with the Watcom VIDEO debugger at this time. Setting *****
48 ;***** DEBUG to 0 takes out all the debugging stuff. *****
50 ;***************************************************************************
51 ;***************************************************************************
56 ;************************************************************************
57 ;**************** FLAT MODEL DATA SEGMENT STUFF *************************
58 ;************************************************************************
60 _DATA SEGMENT BYTE PUBLIC USE32 'DATA'
62 PUBLIC _keyd_pressed ; Must start with a _ so C can see the variable.
64 _keyd_pressed db 256 dup (?)
66 keybuffer dw 256 dup (?) ; Use 256 so an inc wraps around
68 TimeKeyWentDown dd 256 dup(0)
69 TimeKeyHeldDown dd 256 dup(0)
70 NumDowns dd 256 dup(0)
75 PUBLIC _keyd_last_pressed
76 _keyd_last_pressed db 0
77 PUBLIC _keyd_last_released
78 _keyd_last_released db 0
84 PUBLIC _keyd_buffer_type
86 _keyd_buffer_type db ? ; 0=No buffer, 1=buffer ASCII, 2=buffer scans
101 ;************************************************************************
102 ;**************** FLAT MODEL CODE SEGMENT STUFF *************************
103 ;************************************************************************
105 _TEXT SEGMENT BYTE PUBLIC USE32 'CODE'
111 ;************************************************************************
112 ;************************************************************************
114 ;***** K E Y _ T O _ A S C I I _ *****
116 ;************************************************************************
117 ;************************************************************************
134 and ah, 01b ; take away ctrl and alt codes
138 mov al, byte ptr key1[eax]
148 public key_clear_times_,key_clear_counts_
150 ;clear the array of key down times.
151 key_clear_times_: push eax
156 lea edi,TimeKeyHeldDown
157 rep stosd ;clear array
163 ;clear the arrays of key down counts
164 key_clear_counts_: push eax
170 rep stosd ;clear array
173 rep stosd ;clear array
180 PUBLIC key_down_time_
184 EXTERNDEF timer_get_milliseconds_:NEAR
193 cmp _keyd_pressed[ebx], 0
196 call get_modifiers ;shift,alt,ctrl?
203 mov ecx, TimeKeyWentDown[ebx*4]
205 call timer_get_milliseconds_
206 mov TimeKeyWentDown[ebx*4], eax
207 sub eax, ecx ; EAX = time held since last
210 add eax, TimeKeyHeldDown[ebx*4]
211 mov TimeKeyHeldDown[ebx*4], 0
219 PUBLIC key_down_count_
224 mov eax, NumDowns[ebx*4]
225 mov NumDowns[ebx*4], 0
235 mov eax, NumUps[ebx*4]
243 ;************************************************************************
244 ;************************************************************************
246 ;***** K E Y _ F L U S H *****
248 ;************************************************************************
249 ;************************************************************************
263 ; Clear the keyboard array
264 mov edi, offset _keyd_pressed
275 ;************************************************************************
276 ;************************************************************************
278 ;***** K E Y _ I N I T *****
280 ;************************************************************************
281 ;************************************************************************
291 ;**************************************************************
292 ;******************* INITIALIZE key QUEUE **********************
293 ;**************************************************************
296 mov _keyd_buffer_type,1
300 ; Clear the keyboard array
307 ;**************************************************************
308 ;******************* SAVE OLD INT9 HANDLER ********************
309 ;**************************************************************
313 mov eax, 03509h ; DOS Get Vector 09h
315 mov org_int_sel, es ; Save old interrupt selector
316 mov org_int_off, ebx ; Save old interrupt offset
319 ;**************************************************************
320 ;***************** INSTALL NEW INT9 HANDLER *******************
321 ;**************************************************************
323 mov eax, 02509h ; DOS Set Vector 09h
324 mov edx, offset key_handler ; Point DS:EDX to new handler
326 mov MyCodeSegment, bx
341 ;************************************************************************
342 ;************************************************************************
344 ;***** K E Y _ C L O S E _ *****
346 ;************************************************************************
347 ;************************************************************************
361 ;**************************************************************
362 ;***************** RESTORE OLD INT9 HANDLER *******************
363 ;**************************************************************
367 ; Clear the BIOS buffer
369 mov al, byte ptr [ebx]
371 mov byte ptr [ebx], al
373 mov eax, 02509h ; DOS Set Vector 09h
385 ;************************************************************************
386 ;************************************************************************
388 ;***** K E Y _ C H E C K _ *****
390 ;************************************************************************
391 ;************************************************************************
393 PUBLIC key_checkch_ ; Must end with a _ so C can see the function.
414 ;************************************************************************
415 ;************************************************************************
417 ;***** K E Y _ D E B U G *****
419 ;************************************************************************
420 ;************************************************************************
428 ;************************************************************************
429 ;************************************************************************
431 ;***** K E Y _ G E T C H _ *****
433 ;************************************************************************
434 ;************************************************************************
436 PUBLIC key_getch_ ; Must end with a _ so C can see the function.
449 cli ; Critical section
456 cli ; Critical section
459 mov ax, word ptr keybuffer[ebx*2]
467 ;************************************************************************
468 ;************************************************************************
470 ;***** K E Y _ I N K E Y _ *****
472 ;************************************************************************
473 ;************************************************************************
475 PUBLIC key_inkey_ ; Must end with a _ so C can see the function.
486 cli ; Critical section
493 cli ; Critical section
495 mov ax, word ptr keybuffer[ebx*2]
502 PUBLIC key_peekkey_ ; Must end with a _ so C can see the function.
513 cli ; Critical section
520 cli ; Critical section
522 mov ax, word ptr keybuffer[ebx*2]
530 ;************************************************************************
531 ;************************************************************************
533 ;***** K E Y _ H A N D L E R *****
535 ;************************************************************************
536 ;************************************************************************
538 PUBLIC key_handler ; Must end with a _ so C can see the function.
541 EXTERNDEF timer_get_milliseconds_:NEAR
543 pushfd ; Save flags in case we have to chain to original
550 mov ax, DGROUP ; Point to our data segment, since this is an
551 mov ds, ax ; interrupt and we don't know where we were.
554 call CheckForDebugger
556 mov eax, 0b0000h+78*2
557 mov byte ptr [eax], 'D'
558 jmp SkipBuffer ; If debugger is active, then skip buffer
559 @@: mov eax, 0b0000h+78*2
560 mov byte ptr [eax], 'I'
562 ; Clear the BIOS buffer
564 mov al, byte ptr [ebx]
566 mov byte ptr [ebx], al
569 ;**************************************************************
570 ;****************** READ IN THE SCAN CODE *********************
571 ;**************************************************************
572 ; Reads the scan code from the keyboard and masks off the
573 ; scan code and puts it in EAX.
576 in al, 060h ; Get scan code from keyboard
586 mov bl,al ; Save scan code in BL
590 xor bh,bh ; clear for index use
591 and al,10000000b ; keep break bit, if set
592 xor al,10000000b ; flip bit - 1 means pressed
594 rol al,1 ; put it in bit 0
598 ; BX = 1 if pressed, 0 if released.
602 ;**************************************************************
603 ;******************* HANDLE A RELEASED KEY ********************
604 ;**************************************************************
605 ; Unmarks the key press in EAX from the scancode array.
608 mov _keyd_last_released, al
609 mov byte ptr _keyd_pressed[eax], 0
615 or ah,ah ;check modifiers
621 call timer_get_milliseconds_
624 sub edx, TimeKeyWentDown[eax*4]
625 add TimeKeyHeldDown[eax*4], edx
633 ;**************************************************************
634 ;****************** HANDLE A NEWLY PRESSED KEY ****************
635 ;**************************************************************
636 ;Marks the key press in EAX in the scancode array.
639 mov _keyd_last_pressed, al
640 ; Check if the key is repeating or if it just got pressed.
641 cmp byte ptr _keyd_pressed[eax], 1
644 mov byte ptr _keyd_pressed[eax], 1
649 call timer_get_milliseconds_
652 mov TimeKeyWentDown[eax*4], edx
659 ;**************************************************************
660 ;******************** HANDLE A PRESSED KEY ********************
661 ;**************************************************************
662 ; Adds key scan code in EAX to the keybuffer array.
669 cmp _keyd_buffer_type, 0
670 je SkipBuffer ; Buffer = 0 means don't buffer anything.
672 ; Dont buffer shift, ctrl, or alt keys.
673 ;cmp al, 02ah ; Right Shift
675 ;cmp al, 036h ; Left Shift
677 ;cmp al, 038h ; Left Alt
679 ;cmp al, 0b8h ; Right Alt
681 ;cmp al, 01dh ; Left Ctrl
683 ;cmp al, 09dh ' Right Ctrl
686 cmp al, 0AAh ; garbage key
689 call get_modifiers ;returns ah
698 ; If the buffer is full then don't buffer this key
703 mov word ptr keybuffer[ebx*2], ax
711 ;**************************************************************
712 ;*************** FINISH UP THE KEYBOARD INTERRUPT *************
713 ;**************************************************************
715 ; If in debugger, pass control to dos interrupt.
717 pop ds ; Nothing left on stack but flags
723 sub esp, 8 ; Save space for IRETD frame
724 push ds ; Save registers we use.
727 mov ds, ax ; Set DS to our data segment
728 mov eax, org_int_off ; put original handler address
729 mov [esp+8], eax ; in the IRETD frame
730 movzx eax, org_int_sel
732 pop eax ; Restore registers
734 iretd ; Chain to previous handler
737 ; Resets the keyboard, PIC, restores stack, returns.
738 in al, 61h ; Get current port 61h state
739 or al, 10000000b ; Turn on bit 7 to signal clear keybrd
740 out 61h, al ; Send to port
741 and al, 01111111b ; Turn off bit 7 to signal break
742 out 61h, al ; Send to port
743 mov al, 20h ; Reset interrupt controller
745 sti ; Reenable interrupts
747 pop edx ; Restore all of the saved registers.
752 iretd ; Interrupt must return with IRETD
754 ;returns ah=bitmask of shift,ctrl,alt keys
755 get_modifiers: push ecx
759 ; Check the shift keys
760 mov cl, _keyd_pressed[ 036h ]
761 or cl, _keyd_pressed[ 02ah ]
765 mov cl, _keyd_pressed[ 038h ]
766 or cl, _keyd_pressed[ 0b8h ]
771 mov cl, _keyd_pressed[ 01dh ]
772 or cl, _keyd_pressed[ 09dh ]
781 ; Returns CF=0 if debugger isn't active
782 ; CF=1 if debugger is active
784 ;*************************** DEBUG ******************************
785 ; When we're in the VIDEO debugger, we want to pass control to
786 ; the original interrupt. So, to tell if the debugger is active,
787 ; I check if video page 1 is the active page since that is what
788 ; page the debugger uses, and if that works, I check the top of
789 ; the screen to see if the texxt "Control" is there, which should
790 ; only be there when we're in the debugger.
793 ;mov eax, 0462h ; Address 0462 stores BIOS current page
794 ;cmp BYTE PTR [eax], 1
795 ;jne NoDebuggerOnColor
796 ;mov eax, 0b8000h+4096 ; 4096 = offset to 2nd video mem page
797 ;cmp BYTE PTR [eax+2],'C'
798 ;jne NoDebuggerOnColor
799 ;cmp BYTE PTR [eax+4],'o'
800 ;jne NoDebuggerOnColor
801 ;cmp BYTE PTR [eax+6],'n'
802 ;jne NoDebuggerOnColor
803 ;cmp BYTE PTR [eax+8],'t'
804 ;jne NoDebuggerOnColor
805 ;cmp BYTE PTR [eax+10],'r'
806 ;jne NoDebuggerOnColor
807 ;cmp BYTE PTR [eax+12],'o'
808 ;jne NoDebuggerOnColor
809 ;cmp BYTE PTR [eax+14],'l'
810 ;jne NoDebuggerOnColor
813 ; First, see if there is a mono debugger...
815 ;mov eax, 0b0000h ; 4096 = offset to mono video mem
816 ;cmp BYTE PTR [eax+2],'C'
817 ;jne NoActiveDebugger
818 ;cmp BYTE PTR [eax+4],'o'
819 ;jne NoActiveDebugger
820 ;cmp BYTE PTR [eax+6],'n'
821 ;jne NoActiveDebugger
822 ;cmp BYTE PTR [eax+8],'t'
823 ;jne NoActiveDebugger
824 ;cmp BYTE PTR [eax+10],'r'
825 ;jne NoActiveDebugger
826 ;cmp BYTE PTR [eax+12],'o'
827 ;jne NoActiveDebugger
828 ;cmp BYTE PTR [eax+14],'l'
829 ;jne NoActiveDebugger
831 mov eax, 0b0000h ; 4096 = offset to mono video mem
835 cmp BYTE PTR [eax+0],'D'
837 cmp BYTE PTR [eax+2],'B'
839 cmp BYTE PTR [eax+4],'G'
841 cmp BYTE PTR [eax+6],'>'
844 ;Found DBG>, so consider debugger active:
848 cmp BYTE PTR [eax+14],'<'
850 cmp BYTE PTR [eax+16],'i'
852 cmp BYTE PTR [eax+18],'>'
854 cmp BYTE PTR [eax+20],' '
856 cmp BYTE PTR [eax+22],'-'
859 ; Found <i> - , so consider debugger active:
863 cmp BYTE PTR [eax+0], 200
865 cmp BYTE PTR [eax+2], 27
867 cmp BYTE PTR [eax+4], 17
870 ; Found either the help screen or view screen, so consider
875 ; Now we see if its active by looking for the "Executing..."
876 ; text on the bottom of the mono screen
877 ;mov eax, 0b0000h ; 4096 = offset to mono video mem
879 ;cmp BYTE PTR [eax+0],'E'
881 ;cmp BYTE PTR [eax+2],'x'
883 ;cmp BYTE PTR [eax+4],'e'
885 ;cmp BYTE PTR [eax+6],'c'