1/*
2    SDL - Simple DirectMedia Layer
3    Copyright (C) 1997-2012 Sam Lantinga
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19    Sam Lantinga
20    slouken@libsdl.org
21*/
22
23/*
24 *	IKBD 6301 interrupt routine
25 *
26 *	Patrice Mandin
27 */
28
29	.text
30
31	.globl	_SDL_AtariIkbdInstall
32	.globl	_SDL_AtariIkbdUninstall
33
34	.globl	_SDL_AtariIkbd_keyboard
35	.globl	_SDL_AtariIkbd_mouseb
36	.globl	_SDL_AtariIkbd_mousex
37	.globl	_SDL_AtariIkbd_mousey
38	.globl	_SDL_AtariIkbd_joystick
39
40	.globl	_SDL_AtariIkbd_enabled
41
42/*--- Install our IKBD vector ---*/
43
44_SDL_AtariIkbdInstall:
45#if defined(__mcoldfire__)
46	lea	sp@(-16),sp
47	moveml	d0-d1/a0-a1,sp@
48#else
49	moveml	d0-d1/a0-a1,sp@-
50#endif
51
52	| Disable interrupts
53
54	movew	#0x2700,sr
55
56	| Save MFP registers used for keyboard
57
58	lea	0xfffffa00:w,a0
59	btst	#6,a0@(0x09)
60#if defined(__mcoldfire__)
61	sne	d0
62	move.b	d0,ikbd_ierb
63#else
64	sne	ikbd_ierb
65#endif
66	btst	#6,a0@(0x15)
67#if defined(__mcoldfire__)
68	sne	d0
69	move.b	d0,ikbd_imrb
70#else
71	sne	ikbd_imrb
72#endif
73
74	| Set our routine
75
76#if defined(__mcoldfire__)
77	movel	0x118:w,d0
78	movel	d0,old_ikbd
79	lea	ikbd,a0
80	movel	a0,0x118:w
81	moveql	#6,d0
82	bset	d0,0xfffffa09:w	| IERB
83	bset	d0,0xfffffa15:w	| IMRB
84#else
85	movel	0x118:w,old_ikbd
86	movel	#ikbd,0x118:w
87	bset	#6,0xfffffa09:w	| IERB
88	bset	#6,0xfffffa15:w	| IMRB
89#endif
90
91	| Set mouse relative mode
92
93#if defined(__mcoldfire__)
94	moveql	#8,d0
95	moveb	d0,0xfffffc02:w
96#else
97	moveb	#8,0xfffffc02:w
98#endif
99
100	| Reenable interrupts
101
102	movew	#0x2300,sr
103
104	| Interrupts done
105
106#if defined(__mcoldfire__)
107	movel	#0xffff,d0
108	movew	d0,_SDL_AtariIkbd_enabled
109
110	moveml	sp@,d0-d1/a0-a1
111	lea	sp@(16),sp
112#else
113	movew	#0xffff,_SDL_AtariIkbd_enabled
114
115	moveml	sp@+,d0-d1/a0-a1
116#endif
117	rts
118
119/*--- Uninstall our IKBD vector ---*/
120
121_SDL_AtariIkbdUninstall:
122	movel	a0,sp@-
123
124	| Disable interrupts
125
126	movew	#0x2700,sr
127
128	| Restore previous MFP registers
129
130	lea	0xfffffa00:w,a0
131
132	bclr	#6,a0@(0x09)
133	tstb	ikbd_ierb
134	beqs	ikbd_restoreierb
135	bset	#6,a0@(0x09)
136ikbd_restoreierb:
137
138	bclr	#6,a0@(0x15)
139	tstb	ikbd_imrb
140	beqs	ikbd_restoreimrb
141	bset	#6,a0@(0x15)
142ikbd_restoreimrb:
143
144#if defined(__mcoldfire__)
145	movel	old_ikbd,a0
146	movel	a0,0x118:w
147#else
148	movel	old_ikbd,0x118:w
149#endif
150
151	| Clear keyboard buffer
152
153	lea	0xfffffc00:w,a0
154ikbd_videbuffer:
155	btst	#0,a0@
156	beqs	ikbd_finbuffer
157	tstb	a0@(0x02)
158	bras	ikbd_videbuffer
159ikbd_finbuffer:
160
161	| Reenable interrupts
162
163	movew	#0x2300,sr
164
165	movel	sp@+,a0
166	rts
167
168	.bss
169
170	.even
171	.comm	ikbd_ierb,1
172	.comm	ikbd_imrb,1
173
174/*--- Our custom IKBD vector ---*/
175
176	.text
177	.even
178	.ascii	"XBRA"
179	.ascii	"LSDL"
180	.comm	old_ikbd,4*1
181ikbd:
182#if defined(__mcoldfire__)
183	lea	sp@(-12),sp
184	moveml	d0-d1/a0,sp@
185#else
186	moveml	d0-d1/a0,sp@-
187#endif
188
189	| Check if source is IKBD or MIDI
190#if defined(__mcoldfire__)
191	moveql	#0,d0
192	btst	d0,0xfffffc00.w
193#else
194	btst	#0,0xfffffc00.w
195#endif
196	beqs	ikbd_oldmidi
197
198	moveb	0xfffffc02:w,d0
199
200	| Joystick packet ?
201
202	cmpb	#0xff,d0
203	beqs	ikbd_yes_joystick
204
205	| Mouse packet ?
206
207	cmpb	#0xf8,d0
208	bmis	ikbd_no_mouse
209	cmpb	#0xfc,d0
210	bpls	ikbd_no_mouse
211
212	| Mouse packet, byte #1
213
214ikbd_yes_mouse:
215#if defined(__mcoldfire__)
216	andl	#3,d0
217#else
218	andw	#3,d0
219#endif
220	movew	d0,_SDL_AtariIkbd_mouseb
221
222#if defined(__mcoldfire__)
223	movel	#ikbd_mousex,d0
224	movel	d0,0x118:w
225#else
226	movel	#ikbd_mousex,0x118:w
227#endif
228	bras	ikbd_endit_stack
229
230	| Joystick packet, byte #1
231
232ikbd_yes_joystick:
233#if defined(__mcoldfire__)
234	movel	#ikbd_joystick,d0
235	movel	d0,0x118:w
236#else
237	movel	#ikbd_joystick,0x118:w
238#endif
239	bras	ikbd_endit_stack
240
241	| Keyboard press/release
242
243ikbd_no_mouse:
244	moveb	d0,d1
245	lea		_SDL_AtariIkbd_keyboard,a0
246#if defined(__mcoldfire__)
247	andl	#0x7f,d1
248	btst	#7,d0
249	spl	d0
250	moveb	d0,a0@(0,d1:l)
251#else
252	andw	#0x7f,d1
253	tas	d0
254	spl	a0@(0,d1:w)
255#endif
256
257	| End of interrupt
258
259ikbd_endit_stack:
260#if defined(__mcoldfire__)
261	moveql	#6,d0
262	bclr	d0,0xfffffa11:w
263
264	moveml	sp@,d0-d1/a0
265	lea	sp@(12),sp
266#else
267	moveml	sp@+,d0-d1/a0
268
269	bclr	#6,0xfffffa11:w
270#endif
271	rte
272
273	| Call old MIDI interrupt
274
275ikbd_oldmidi:
276#if defined(__mcoldfire__)
277	moveml	sp@,d0-d1/a0
278	lea	sp@(12),sp
279#else
280	moveml	sp@+,d0-d1/a0
281#endif
282
283	movel	old_ikbd,sp@-
284	rts
285
286	| Mouse packet, byte #2
287
288ikbd_mousex:
289#if defined(__mcoldfire__)
290	lea	sp@(-12),sp
291	moveml	d0-d1/a0,sp@
292#else
293	moveml	d0-d1/a0,sp@-
294#endif
295
296	| Check if source is IKBD or MIDI
297#if defined(__mcoldfire__)
298	moveql	#0,d0
299	btst	d0,0xfffffc00.w
300#else
301	btst	#0,0xfffffc00.w
302#endif
303	beqs	ikbd_oldmidi
304
305	moveb	0xfffffc02:w,d0
306	extw	d0
307#if defined(__mcoldfire__)
308	movew	_SDL_AtariIkbd_mousex,d1
309	addl	d1,d0
310	movew	d0,_SDL_AtariIkbd_mousex
311
312	movel	#ikbd_mousey,d0
313	movel	d0,0x118:w
314#else
315	addw	d0,_SDL_AtariIkbd_mousex
316
317	movel	#ikbd_mousey,0x118:w
318#endif
319	bras	ikbd_endit_stack
320
321	| Mouse packet, byte #3
322
323ikbd_mousey:
324#if defined(__mcoldfire__)
325	lea	sp@(-12),sp
326	moveml	d0-d1/a0,sp@
327#else
328	moveml	d0-d1/a0,sp@-
329#endif
330
331	| Check if source is IKBD or MIDI
332#if defined(__mcoldfire__)
333	moveql	#0,d0
334	btst	d0,0xfffffc00.w
335#else
336	btst	#0,0xfffffc00.w
337#endif
338	beqs	ikbd_oldmidi
339
340	moveb	0xfffffc02:w,d0
341	extw	d0
342#if defined(__mcoldfire__)
343	movew	_SDL_AtariIkbd_mousey,d1
344	addl	d1,d0
345	movew	d0,_SDL_AtariIkbd_mousey
346
347	movel	#ikbd,d0
348	movel	d0,0x118:w
349#else
350	addw	d0,_SDL_AtariIkbd_mousey
351
352	movel	#ikbd,0x118:w
353#endif
354	bras	ikbd_endit_stack
355
356	| Joystick packet, byte #2
357
358ikbd_joystick:
359#if defined(__mcoldfire__)
360	lea	sp@(-12),sp
361	moveml	d0-d1/a0,sp@
362#else
363	moveml	d0-d1/a0,sp@-
364#endif
365
366	| Check if source is IKBD or MIDI
367#if defined(__mcoldfire__)
368	moveql	#0,d0
369	btst	d0,0xfffffc00.w
370#else
371	btst	#0,0xfffffc00.w
372#endif
373	beqs	ikbd_oldmidi
374
375#if defined(__mcoldfire__)
376	moveb	0xfffffc02:w,d0
377	moveb	d0,_SDL_AtariIkbd_joystick+1
378
379	movel	#ikbd,d0
380	movel	d0,0x118:w
381
382	bra	ikbd_endit_stack
383#else
384	moveb	0xfffffc02:w,_SDL_AtariIkbd_joystick+1
385
386	movel	#ikbd,0x118:w
387
388	bras	ikbd_endit_stack
389#endif
390
391	.data
392
393	.even
394_SDL_AtariIkbd_enabled:
395	.word	0
396
397	.bss
398
399	.even
400	.comm	_SDL_AtariIkbd_keyboard,128
401	.comm	_SDL_AtariIkbd_mousex,2
402	.comm	_SDL_AtariIkbd_mousey,2
403	.comm	_SDL_AtariIkbd_mouseb,2
404	.comm	_SDL_AtariIkbd_joystick,2
405