]> icculus.org git repositories - btb/d2x.git/blob - unused/bios/joy.asm
portability
[btb/d2x.git] / unused / bios / joy.asm
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 .386
12
13 _DATA   SEGMENT BYTE PUBLIC USE32 'DATA'
14
15 rcsid   db      "$Id: joy.asm,v 1.1.1.2 2001-01-19 03:33:50 bradleyb Exp $"
16
17         LastTick        dd      0
18         TotalTicks      dd      0
19         
20 _DATA   ENDS
21
22 DGROUP  GROUP _DATA
23
24
25 _TEXT   SEGMENT BYTE PUBLIC USE32 'CODE'
26
27         ASSUME  ds:_DATA
28         ASSUME  cs:_TEXT
29
30         JOY_PORT        EQU     0201h
31         TDATA           EQU     40h
32         TCOMMAND        EQU     43h
33
34 joy_get_timer:
35         xor     al, al                  ; Latch timer 0 command
36         out     TCOMMAND, al            ; Latch timer
37         in      al, TDATA               ; Read lo byte
38         mov     ah, al
39         in      al, TDATA               ; Read hi byte
40         xchg    ah, al
41         and     eax, 0ffffh
42         ret     
43
44 PUBLIC joy_read_stick_asm_
45
46 joy_read_stick_asm_:    
47                 ; ebx = read mask
48                 ; edi = pointer to event buffer
49                 ; ecx = timeout value
50                 ; returns in eax the number of events
51                 and     ebx, 01111b             ; Make sure we only check the right values                                                                              
52                                                 ; number of events we found will be in bh, so this also clears it to zero.
53
54                 mov     dx, JOY_PORT
55
56                 cli                             ; disable interrupts while reading time...
57                 call    joy_get_timer           ; Returns counter in EAX
58                 sti                             ; enable interrupts after reading time...
59                 mov     LastTick, eax
60
61 waitforstable:  in      al, dx
62                 and     al, bl
63                 jz      ready                   ; Wait for the port in question to be done reading...
64                 
65                 cli                             ; disable interrupts while reading time...
66                 call    joy_get_timer           ; Returns counter in EAX
67                 sti                             ; enable interrupts after reading time...
68                 xchg    eax, LastTick
69                 cmp     eax, LastTick
70                 jb      @f
71                 sub     eax, LastTick
72 @@:             ; Higher...
73                 add     TotalTicks, eax
74                 cmp     TotalTicks, ecx         ; Timeout at 1/200'th of a second
75                 jae     ready
76                 jmp     waitforstable
77                 
78 ready:
79                 cli
80                 mov     al, 0ffh
81                 out     dx, al                  ; Start joystick a readin'
82
83                 call    joy_get_timer           ; Returns counter in EAX
84                 mov     LastTick, eax
85                 mov     TotalTicks, 0
86                 
87                 mov     [edi], eax              ; Store initial count
88                 add     edi, 4          
89
90         again:  in      al, dx                  ; Read Joystick port
91                 not     al
92                 and     al, bl                  ; Mask off channels we don't want to read
93                 jnz     flip                    ; See if any of the channels flipped
94
95                 call    joy_get_timer           ; Returns counter in EAX
96                 
97                 xchg    eax, LastTick
98                 cmp     eax, LastTick
99                 jb      @f
100                 sub     eax, LastTick
101 @@:             ; Higher...
102                 add     TotalTicks, eax
103                 cmp     TotalTicks, ecx         ; Timeout at 1/200'th of a second
104                 jae     timedout
105                 jmp     again
106
107         flip:   and     eax, 01111b             ; Only care about axis values
108                 mov     [edi], eax              ; Record what channel(s) flipped
109                 add     edi, 4  
110                 xor     bl, al                  ; Unmark the channels that just tripped
111
112                 call    joy_get_timer           ; Returns counter in EAX
113                 mov     [edi], eax              ; Record the time this channel flipped
114                 add     edi, 4          
115                 inc     bh                      ; Increment number of events
116
117                 cmp     bl, 0   
118                 jne     again                   ; If there are more channels to read, keep looping
119
120         timedout:
121                 movzx   eax, bh                 ; Return number of events
122
123                 sti
124                 ret
125
126
127 PUBLIC joy_read_stick_polled_
128
129 joy_read_stick_polled_: 
130                 ; ebx = read mask
131                 ; edi = pointer to event buffer
132                 ; ecx = timeout value
133                 ; returns in eax the number of events
134                 and     ebx, 01111b             ; Make sure we only check the right values                                                                              
135                                                 ; number of events we found will be in bh, so this also clears it to zero.
136
137                 mov     dx, JOY_PORT
138
139                 mov     TotalTicks, 0
140
141 waitforstable1: in      al, dx
142                 and     al, bl
143                 jz      ready1                  ; Wait for the port in question to be done reading...
144                 
145                 inc     TotalTicks
146                 cmp     TotalTicks, ecx         ; Timeout at 1/200'th of a second
147                 jae     ready1
148                 jmp     waitforstable1
149 ready1:
150                 cli
151                 mov     al, 0ffh
152                 out     dx, al                  ; Start joystick a readin'
153
154                 mov     TotalTicks, 0
155
156                 mov     dword ptr [edi], 0              ; Store initial count
157                 add     edi, 4          
158
159         again1: in      al, dx                  ; Read Joystick port
160                 not     al
161                 and     al, bl                  ; Mask off channels we don't want to read
162                 jnz     flip1                   ; See if any of the channels flipped
163
164                 inc     TotalTicks
165                 cmp     TotalTicks, ecx         ; Timeout at 1/200'th of a second
166                 jae     timedout1
167                 jmp     again1
168
169         flip1:  and     eax, 01111b             ; Only care about axis values
170                 mov     [edi], eax              ; Record what channel(s) flipped
171                 add     edi, 4  
172                 xor     bl, al                  ; Unmark the channels that just tripped
173
174                 mov     eax, TotalTicks
175                 mov     [edi], eax              ; Record the time this channel flipped
176                 add     edi, 4          
177                 inc     bh                      ; Increment number of events
178
179                 cmp     bl, 0   
180                 jne     again1                  ; If there are more channels to read, keep looping
181
182         timedout1:
183                 movzx   eax, bh                 ; Return number of events
184
185                 sti
186                 ret
187
188 PUBLIC joy_read_stick_bios_
189
190 joy_read_stick_bios_:   
191                 ; ebx = read mask
192                 ; edi = pointer to event buffer
193                 ; ecx = timeout value
194                 ; returns in eax the number of events
195                 
196                 pusha
197
198                 mov     dword ptr [edi], 0
199                         
200                 mov     eax, 08400h
201                 mov     edx, 1
202                 cli
203                 int     15h
204                 sti
205         
206                 mov     dword ptr [edi+4], 1    ;       Axis 1          
207                 and     eax, 0ffffh
208                 mov     [edi+8], eax            ;       Axis 1 value
209
210                 mov     dword ptr [edi+12], 2   ;       Axis 2
211                 and     ebx, 0ffffh
212                 mov     [edi+16], ebx           ;       Axis 2 value
213
214                 mov     dword ptr [edi+20], 4   ;       Axis 3
215                 and     ecx, 0ffffh
216                 mov     [edi+24], ecx           ;       Axis 3 value
217
218                 mov     dword ptr [edi+28], 8   ;       Axis 3
219                 and     edx, 0ffffh
220                 mov     [edi+32], edx           ;       Axis 3 value
221
222                 popa
223                 mov     eax, 4                  ; 4 events
224
225                 ret
226
227
228 PUBLIC joy_read_buttons_bios_
229
230 joy_read_buttons_bios_: 
231                 ; returns in eax the button settings
232                 
233                 push    ebx
234                 push    ecx
235                 push    edx
236                 mov     eax, 08400h
237                 mov     edx, 0  ; Read switches
238                 int     15h
239                 pop     edx
240                 pop     ecx
241                 pop     ebx
242                 
243                 shr     eax, 4
244                 not     eax
245                 and     eax, 01111b
246                 ret
247
248
249 _TEXT   ENDS
250
251
252                 END