Import upstream u-boot 1.1.4
[u-boot.git] / board / MAI / bios_emulator / scitech / src / pm / common / _joy.asm
1 ;****************************************************************************
2 ;*
3 ;*                  SciTech OS Portability Manager Library
4 ;*
5 ;*  ========================================================================
6 ;*
7 ;*    The contents of this file are subject to the SciTech MGL Public
8 ;*    License Version 1.0 (the "License"); you may not use this file
9 ;*    except in compliance with the License. You may obtain a copy of
10 ;*    the License at http://www.scitechsoft.com/mgl-license.txt
11 ;*
12 ;*    Software distributed under the License is distributed on an
13 ;*    "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 ;*    implied. See the License for the specific language governing
15 ;*    rights and limitations under the License.
16 ;*
17 ;*    The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
18 ;*
19 ;*    The Initial Developer of the Original Code is SciTech Software, Inc.
20 ;*    All Rights Reserved.
21 ;*
22 ;*  ========================================================================
23 ;*
24 ;* Language:    80386 Assembler
25 ;* Environment: Intel x86, any OS
26 ;*
27 ;* Description: Assembly language support routines for reading analogue
28 ;*              joysticks.
29 ;*
30 ;****************************************************************************
31
32         ideal
33
34 include "scitech.mac"           ; Memory model macros
35
36 ifdef flatmodel
37
38 header  _joy                    ; Set up memory model
39
40 begcodeseg  _joy                ; Start of code segment
41
42 ;----------------------------------------------------------------------------
43 ; initTimer
44 ;----------------------------------------------------------------------------
45 ; Sets up 8253 timer 2 (PC speaker) to start timing, but not produce output.
46 ;----------------------------------------------------------------------------
47 cprocstatic initTimer
48
49 ; Start timer 2 counting
50
51         in      al,61h
52         and     al,0FDh             ; Disable speaker output (just in case)
53         or      al,1
54         out     61h,al
55
56 ; Set the timer 2 count to 0 again to start the timing interval.
57
58         mov     al,10110100b        ; set up to load initial (timer 2)
59         out     43h,al              ; timer count
60         sub     al,al
61         out     42h,al              ; load count lsb
62         out     42h,al              ; load count msb
63         ret
64
65 cprocend
66
67 ;----------------------------------------------------------------------------
68 ; readTimer2
69 ;----------------------------------------------------------------------------
70 ; Reads the number of ticks from the 8253 timer chip using channel 2 (PC
71 ; speaker). This is non-destructive and does not screw up other libraries.
72 ;----------------------------------------------------------------------------
73 cprocstatic readTimer
74
75         xor     al,al               ; Latch timer 0 command
76         out     43h,al              ; Latch timer
77         in      al,42h              ; least significant byte
78         mov     ah,al
79         in      al,42h              ; most significant byte
80         xchg    ah,al
81         and     eax,0FFFFh
82         ret
83
84 cprocend
85
86 ;----------------------------------------------------------------------------
87 ; exitTimer
88 ;----------------------------------------------------------------------------
89 ; Stops the 8253 timer 2 (PC speaker) counting
90 ;----------------------------------------------------------------------------
91 cprocstatic exitTimer
92
93 ; Stop timer 2 from counting
94
95         push    eax
96         in      al,61h
97         and     al,0FEh
98         out     61h,al
99         
100 ; Some programs have a problem if we change the control port; better change it
101 ; to something they expect (mode 3 - square wave generator)...
102         mov     al,0B6h
103         out     43h,al
104         
105         pop     eax
106         ret
107
108 cprocend
109
110 ;----------------------------------------------------------------------------
111 ; int _EVT_readJoyAxis(int jmask,int *axis);
112 ;----------------------------------------------------------------------------
113 ; Function to poll the joystick to read the current axis positions.
114 ;----------------------------------------------------------------------------
115 cprocstart  _EVT_readJoyAxis
116
117         ARG     jmask:UINT, axis:DPTR
118
119         LOCAL   firstTick:UINT, lastTick:UINT, totalTicks:UINT = LocalSize
120
121         enter_c
122
123         mov     ebx,[jmask]
124         mov     edi,[axis]
125         mov     ecx,(1193180/100)
126         and     ebx,01111b          ; Mask out supported axes
127         mov     dx,201h             ; DX := joystick I/O port
128         call    initTimer           ; Start timer 2 counting
129         call    readTimer           ; Returns counter in EAX
130         mov     [lastTick],eax
131
132 @@WaitStable:
133         in      al,dx
134         and     al,bl               ; Wait for the axes in question to be
135         jz      @@Stable            ;  done reading...
136         call    readTimer           ; Returns counter in EAX
137         xchg    eax,[lastTick]
138         cmp     eax,[lastTick]
139         jb      @@1
140         sub     eax,[lastTick]
141 @@1:    add     [totalTicks],eax
142         cmp     [totalTicks],ecx    ; Check for timeout
143         jae     @@Stable
144         jmp     @@WaitStable
145
146 @@Stable:
147         mov     al,0FFh
148         out     dx,al               ; Start joystick reading
149         call    initTimer           ; Start timer 2 counting
150         call    readTimer           ; Returns counter in EAX
151         mov     [firstTick],eax     ; Store initial count
152         mov     [lastTick],eax
153         mov     [DWORD totalTicks],0
154         cli
155
156 @@PollLoop:
157         in      al,dx               ; Read Joystick port
158         not     al
159         and     al,bl               ; Mask off channels we don't want to read
160         jnz     @@AxisFlipped       ; See if any of the channels flipped
161         call    readTimer           ; Returns counter in EAX
162         xchg    eax,[lastTick]
163         cmp     eax,[lastTick]
164         jb      @@2
165         sub     eax,[lastTick]
166 @@2:    add     [totalTicks],eax
167         cmp     [totalTicks],ecx    ; Check for timeout
168         jae     @@TimedOut
169         jmp     @@PollLoop
170
171 @@AxisFlipped:
172         xor     esi,esi
173         mov     ah,1
174         test    al,ah
175         jnz     @@StoreCount        ; Joystick 1, X axis flipped
176         add     esi,4
177         mov     ah,2
178         test    al,ah
179         jnz     @@StoreCount        ; Joystick 1, Y axis flipped
180         add     esi,4
181         mov     ah,4
182         test    al,ah
183         jnz     @@StoreCount        ; Joystick 2, X axis flipped
184         add     esi,4               ; Joystick 2, Y axis flipped
185         mov     ah,8
186
187 @@StoreCount:
188         or      bh,ah               ; Indicate this axis is active
189         xor     bl,ah               ; Unmark the channels that just tripped
190         call    readTimer           ; Returns counter in EAX
191         xchg    eax,[lastTick]
192         cmp     eax,[lastTick]
193         jb      @@3
194         sub     eax,[lastTick]
195 @@3:    add     [totalTicks],eax
196         mov     eax,[totalTicks]
197         mov     [edi+esi],eax       ; Record the time this channel flipped
198         cmp     bl,0                ; If there are more channels to read,
199         jne     @@PollLoop          ;   keep looping
200
201 @@TimedOut:
202         sti
203         call    exitTimer           ; Stop timer 2 counting
204         movzx   eax,bh              ; Return the mask of working axes
205         leave_c
206         ret
207
208 cprocend
209
210 ;----------------------------------------------------------------------------
211 ; int _EVT_readJoyButtons(void);
212 ;----------------------------------------------------------------------------
213 ; Function to poll the current joystick buttons
214 ;----------------------------------------------------------------------------
215 cprocstart  _EVT_readJoyButtons
216
217         mov     dx,0201h
218         in      al,dx
219         shr     al,4
220         not     al
221         and     eax,0Fh
222         ret
223
224 cprocend
225
226 endcodeseg  _joy
227
228 endif
229
230         END                         ; End of module