SDL-1.2: atari: SDL_AtariXbios_Restore and SDL_AtariIkbd_Restore call Atari_UnhookXbra()

From 4f98aa28fdc7fcff1edd61414fccc4e6263c4854 Mon Sep 17 00:00:00 2001
From: Miro Kropacek <[EMAIL REDACTED]>
Date: Sun, 21 Apr 2019 19:54:11 +0200
Subject: [PATCH] atari: SDL_AtariXbios_Restore and SDL_AtariIkbd_Restore call
 Atari_UnhookXbra()

---
 src/video/ataricommon/SDL_atarixbra.c      | 16 +++++--
 src/video/ataricommon/SDL_atarixbra_c.h    |  2 +-
 src/video/ataricommon/SDL_ikbdinterrupt.S  | 38 ++++++----------
 src/video/ataricommon/SDL_xbiosinterrupt.S | 50 ++++++++++++++++------
 4 files changed, 64 insertions(+), 42 deletions(-)

diff --git a/src/video/ataricommon/SDL_atarixbra.c b/src/video/ataricommon/SDL_atarixbra.c
index ee575d1c..99017481 100644
--- a/src/video/ataricommon/SDL_atarixbra.c
+++ b/src/video/ataricommon/SDL_atarixbra.c
@@ -33,18 +33,28 @@
 
 /* Functions */
 
-XbraHandler Atari_UnhookXbra(Uint16 vecnum, Uint32 app_id, XbraHandler handler)
+XbraHandler Atari_UnhookXbra(Uint32 vecnum, Uint32 app_id, XbraHandler handler)
 {
 	XBRA *rx;
 	XbraHandler vecadr, *stepadr, ret = NULL;
 
-	vecadr = Setexc(vecnum, VEC_INQUIRE);
+	if (vecnum <= 0x16C) {	/* 0x5B0 is the last official system vector/variable */
+		vecadr = Setexc(vecnum, VEC_INQUIRE);
+	} else {
+		vecadr = *((volatile XbraHandler*)vecnum);
+	}
+
 	rx = (XBRA*)((Uint32)vecadr - sizeof(XBRA));
 
 	/* Special Case: Vector to remove is first in chain. */
 	if(rx->xbra_id == XBRA_ID && rx->app_id == app_id && vecadr == handler)
 	{
-		return Setexc(vecnum, rx->oldvec);
+		if (vecnum <= 0x16C) {
+			return Setexc(vecnum, rx->oldvec);
+		} else {
+			*((volatile XbraHandler*)vecnum) = rx->oldvec;
+			return vecadr;
+		}
 	}
 
 	stepadr = &rx->oldvec;
diff --git a/src/video/ataricommon/SDL_atarixbra_c.h b/src/video/ataricommon/SDL_atarixbra_c.h
index e1eb7c8d..72dfe279 100644
--- a/src/video/ataricommon/SDL_atarixbra_c.h
+++ b/src/video/ataricommon/SDL_atarixbra_c.h
@@ -47,6 +47,6 @@ typedef struct xbra
 
 /* Functions */
 
-extern XbraHandler Atari_UnhookXbra(Uint16 vecnum, Uint32 app_id, XbraHandler handler);
+extern XbraHandler Atari_UnhookXbra(Uint32 vecnum, Uint32 app_id, XbraHandler handler);
 
 #endif /* _SDL_ATARI_XBRA_H_ */
diff --git a/src/video/ataricommon/SDL_ikbdinterrupt.S b/src/video/ataricommon/SDL_ikbdinterrupt.S
index a822c89b..a916e0c0 100644
--- a/src/video/ataricommon/SDL_ikbdinterrupt.S
+++ b/src/video/ataricommon/SDL_ikbdinterrupt.S
@@ -39,6 +39,8 @@
 
 	.globl	_SDL_AtariIkbd_enabled
 
+	.globl	_Atari_UnhookXbra
+
 /*--- Install our IKBD vector ---*/
 
 _SDL_AtariIkbd_Install:
@@ -113,16 +115,6 @@ _SDL_AtariIkbd_Restore:
 	rts
 
 ikbd_installed:
-        /*
-         * even though SDL_AtariXbios_Restore() is called via Supexec(),
-         * it is also a etv_term vector which needs to preserve all registers
-         */
-#if defined(__mcoldfire__)
-        lea	sp@(-8),sp
-        moveml	d1/a0,sp@
-#else
-        moveml	d1/a0,sp@-
-#endif
 	| Disable interrupts
 
 	movew	sr,d1
@@ -144,12 +136,15 @@ ikbd_restoreierb:
 	bset	#6,a0@(0x15)
 ikbd_restoreimrb:
 
-#if defined(__mcoldfire__)
-	movel	old_ikbd,a0
-	movel	a0,0x118:w
-#else
-	movel	old_ikbd,0x118:w
-#endif
+	movew	d1,sp@-
+
+	pea	ikbd(pc)
+	movel	#0x4c53444c,sp@-	/* "LSDL" */
+	pea	0x118/4:w
+	jsr	_Atari_UnhookXbra
+	lea	sp@(12),sp
+
+	movew	sp@+,d1
 
 	| Clear keyboard buffer
 
@@ -168,13 +163,6 @@ ikbd_finbuffer:
 	| Interrupts done
 
 	clrw	_SDL_AtariIkbd_enabled
-
-#if defined(__mcoldfire__)
-        moveml	sp@,d1-/a0
-        lea	sp@(8),sp
-#else
-        moveml	sp@+,d1/a0
-#endif
 	rts
 
 	.bss
@@ -210,7 +198,7 @@ ikbd:
 	moveb	0xfffffc02:w,d0
 
 	| Joystick packet ?
-	
+
 	cmpb	#0xff,d0
 	beqs	ikbd_yes_joystick
 
@@ -401,7 +389,7 @@ ikbd_joystick:
 #endif
 
 	.data
-	
+
 	.even
 _SDL_AtariIkbd_enabled:
 	.word	0
diff --git a/src/video/ataricommon/SDL_xbiosinterrupt.S b/src/video/ataricommon/SDL_xbiosinterrupt.S
index 64dac210..c3ab09b7 100644
--- a/src/video/ataricommon/SDL_xbiosinterrupt.S
+++ b/src/video/ataricommon/SDL_xbiosinterrupt.S
@@ -39,6 +39,8 @@
 	.globl	_SDL_AtariXbios_installjoystickvector
 	.globl	_SDL_AtariXbios_joystick
 
+	.globl	_Atari_UnhookXbra
+
 /*--- Vector installer ---*/
 
 _SDL_AtariXbios_Install:
@@ -67,7 +69,6 @@ _SDL_AtariXbios_Install:
 
 	/* Save old mouse vector, set our routine */
 
-	clrl	oldmousevector
 	tstw	_SDL_AtariXbios_installmousevector
 	beqs	no_new_mouse_vector
 #if defined(__mcoldfire__)
@@ -83,7 +84,6 @@ no_new_mouse_vector:
 
 	/* Save old joystick vector, set our routine */
 
-	clrl	oldjoystickvector
 	tstw	_SDL_AtariXbios_installjoystickvector
 	beqs	no_new_joystick_vector
 #if defined(__mcoldfire__)
@@ -131,28 +131,52 @@ _SDL_AtariXbios_Restore:
 
 	/* Restore mouse vector */
 
-	movel	oldmousevector,d0
+	tstl	oldmousevector
 	beqs	no_restore_mouse
-	movel	d0,a0@(16)
+
+#if defined(__mcoldfire__)
+	lea	sp@(-8),sp
+	moveml	d1/a0,sp@
+#else
+	moveml	d1/a0,sp@-
+#endif
+	pea	newmousevector(pc)
+	movel	#0x4c53444c,sp@-	/* "LSDL" */
+	pea	a0@(16)
+	jsr	_Atari_UnhookXbra
+	lea	sp@(12),sp
+	clrl	oldmousevector
+
+#if defined(__mcoldfire__)
+	moveml	sp@,d1/a0
+	lea	sp@(8),sp
+#else
+	moveml	sp@+,d1/a0
+#endif
+
 no_restore_mouse:
 
 	/* Restore joystick vector */
 
-	movel	oldjoystickvector,d0
+	tstl	oldjoystickvector
 	beqs	no_restore_joystick
-	movel	d0,a0@(24)
+
+	movew	d1,sp@-
+
+	pea	newjoystickvector(pc)
+	movel	#0x4c53444c,sp@-	/* "LSDL" */
+	pea	a0@(24)
+	jsr	_Atari_UnhookXbra
+	lea	sp@(12),sp
+	clrl	oldjoystickvector
+
+	movew	sp@+,d1
+
 no_restore_joystick:
 
 	/* Restart interrupts */
 
 	movew	d1,sr
-
-#if defined(__mcoldfire__)
-	moveml	sp@,d0-d2/a0-a2
-	lea	sp@(24),sp
-#else
-	moveml	sp@+,d0-d2/a0-a2
-#endif
 	rts
 
 /*--- Our mouse vector ---*/