SDL-1.2: atari: SDL_AtariXbios_Restore as etv_term

From e26333091a3646b79d52091c9fc88dac94b79352 Mon Sep 17 00:00:00 2001
From: Miro Kropacek <[EMAIL REDACTED]>
Date: Mon, 27 Nov 2023 19:15:29 +0100
Subject: [PATCH] atari: SDL_AtariXbios_Restore as etv_term

---
 src/video/ataricommon/SDL_xbiosevents.c      | 35 ++---------
 src/video/ataricommon/SDL_xbiosinterrupt.S   | 64 +++++++++++++++++---
 src/video/ataricommon/SDL_xbiosinterrupt_s.h |  8 +--
 3 files changed, 64 insertions(+), 43 deletions(-)

diff --git a/src/video/ataricommon/SDL_xbiosevents.c b/src/video/ataricommon/SDL_xbiosevents.c
index 2bb0bc9a6..3dd26a8f1 100644
--- a/src/video/ataricommon/SDL_xbiosevents.c
+++ b/src/video/ataricommon/SDL_xbiosevents.c
@@ -40,15 +40,12 @@ SDL_bool SDL_AtariXbios_enabled=SDL_FALSE;
 
 /* Local variables */
 
-static _KBDVECS *kbdvecs;		/* Pointer to access system vectors */
 static Uint16 atari_prevmouseb;	/* buttons */
 
 /* Functions */
 
 void SDL_AtariXbios_InstallVectors(int vectors_mask)
 {
-	void *oldpile;
-
 	/* Clear variables */
 	SDL_AtariXbios_mouselock =
 		SDL_AtariXbios_mouseb =
@@ -62,44 +59,24 @@ void SDL_AtariXbios_InstallVectors(int vectors_mask)
 		return;
 	}
 
-	/* Read IKBD vectors base */
-	kbdvecs=Kbdvbase();
-
-	/* Go to supervisor mode */
-	oldpile=(void *)Super(0);
-
 	/* Install our vectors */
-	SDL_AtariXbios_Install(
-		kbdvecs,
-		(vectors_mask & ATARI_XBIOS_MOUSEEVENTS) ? SDL_AtariXbios_MouseVector : NULL,
-		(vectors_mask & ATARI_XBIOS_JOYSTICKEVENTS) ? SDL_AtariXbios_JoystickVector : NULL
-	);
-
-	/* Back to user mode */
-	SuperToUser(oldpile);
+	SDL_AtariXbios_installmousevector = (vectors_mask & ATARI_XBIOS_MOUSEEVENTS) != 0;
+	SDL_AtariXbios_installjoystickvector = (vectors_mask & ATARI_XBIOS_JOYSTICKEVENTS) != 0;
+	Supexec(SDL_AtariXbios_Install);
+	/* SDL_AtariXbios_Restore() doesn't need SDL_AtariXbios_enabled */
+	Setexc(VEC_PROCTERM, SDL_AtariXbios_Restore);
 
 	SDL_AtariXbios_enabled=SDL_TRUE;
 }
 
 void SDL_AtariXbios_RestoreVectors(void)
 {
-	void *oldpile;
-
 	if (!SDL_AtariXbios_enabled) {
 		return;
 	}
 
-	/* Read IKBD vectors base */
-	kbdvecs=Kbdvbase();
-
-	/* Go to supervisor mode */
-	oldpile=(void *)Super(NULL);
-
 	/* Reinstall system vector */
-	SDL_AtariXbios_Restore(kbdvecs);
-
-	/* Back to user mode */
-	SuperToUser(oldpile);
+	Supexec(SDL_AtariXbios_Restore);
 }
 
 static int atari_GetButton(int button)
diff --git a/src/video/ataricommon/SDL_xbiosinterrupt.S b/src/video/ataricommon/SDL_xbiosinterrupt.S
index 3b6fc6329..64dac2109 100644
--- a/src/video/ataricommon/SDL_xbiosinterrupt.S
+++ b/src/video/ataricommon/SDL_xbiosinterrupt.S
@@ -30,19 +30,35 @@
 
 	.globl	_SDL_AtariXbios_Install
 	.globl	_SDL_AtariXbios_Restore
-	.globl	_SDL_AtariXbios_MouseVector
-	.globl	_SDL_AtariXbios_JoystickVector
 
+	.globl	_SDL_AtariXbios_installmousevector
 	.globl	_SDL_AtariXbios_mouselock
 	.globl	_SDL_AtariXbios_mouseb
 	.globl	_SDL_AtariXbios_mousex
 	.globl	_SDL_AtariXbios_mousey
+	.globl	_SDL_AtariXbios_installjoystickvector
 	.globl	_SDL_AtariXbios_joystick
 
 /*--- Vector installer ---*/
 
 _SDL_AtariXbios_Install:
-	movel	sp@(4),a0
+#if defined(__mcoldfire__)
+	lea	sp@(-8),sp
+	moveml	d2/a2,sp@
+#else
+	moveml	d2/a2,sp@-
+#endif
+	/* KBDVBASE *Kbdvbase(void); */
+	movew	#34,sp@-
+	trap	#14
+	addql	#2,sp
+#if defined(__mcoldfire__)
+	moveml	sp@,d2/a2
+	lea	sp@(8),sp
+#else
+	moveml	sp@+,d2/a2
+#endif
+	movel	d0,a0
 
 	/* Stop interrupts */
 
@@ -52,29 +68,33 @@ _SDL_AtariXbios_Install:
 	/* Save old mouse vector, set our routine */
 
 	clrl	oldmousevector
-	movel	sp@(8),d0
+	tstw	_SDL_AtariXbios_installmousevector
 	beqs	no_new_mouse_vector
 #if defined(__mcoldfire__)
 	movel	a0@(16),a1
 	movel	a1,oldmousevector
+	lea	newmousevector(pc),a1
+	movel	a1,a0@(16)
 #else
 	movel	a0@(16),oldmousevector
+	movel	#newmousevector,a0@(16)
 #endif
-	movel	d0,a0@(16)
 no_new_mouse_vector:
 
 	/* Save old joystick vector, set our routine */
 
 	clrl	oldjoystickvector
-	movel	sp@(12),d0
+	tstw	_SDL_AtariXbios_installjoystickvector
 	beqs	no_new_joystick_vector
 #if defined(__mcoldfire__)
 	movel	a0@(24),a1
 	movel	a1,oldjoystickvector
+	lea	newjoystickvector(pc),a1
+	movel	a1,a0@(24)
 #else
 	movel	a0@(24),oldjoystickvector
+	movel	#newjoystickvector,a0@(24)
 #endif
-	movel	d0,a0@(24)
 no_new_joystick_vector:
 
 	/* Restart interrupts */
@@ -86,7 +106,23 @@ no_new_joystick_vector:
 /*--- Vector restorer ---*/
 
 _SDL_AtariXbios_Restore:
-	movel	sp@(4),a0
+#if defined(__mcoldfire__)
+	lea	sp@(-8),sp
+	moveml	d2/a2,sp@
+#else
+	moveml	d2/a2,sp@-
+#endif
+	/* KBDVBASE *Kbdvbase(void); */
+	movew	#34,sp@-
+	trap	#14
+	addql	#2,sp
+#if defined(__mcoldfire__)
+	moveml	sp@,d2/a2
+	lea	sp@(8),sp
+#else
+	moveml	sp@+,d2/a2
+#endif
+	movel	d0,a0
 
 	/* Stop interrupts */
 
@@ -111,6 +147,12 @@ no_restore_joystick:
 
 	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 ---*/
@@ -120,7 +162,7 @@ no_restore_joystick:
 	.ascii "XBRA"
 	.ascii "LSDL"
 	.comm	oldmousevector,4*1
-_SDL_AtariXbios_MouseVector:
+newmousevector:
 #if defined(__mcoldfire__)
 	lea	sp@(-8),sp
 	moveml	d0-d1,sp@
@@ -179,6 +221,7 @@ no_mouse_lock:
 
 	.data
 	.even
+	.comm	_SDL_AtariXbios_installmousevector,2*1
 	.comm	_SDL_AtariXbios_mouselock,2*1
 	.comm	_SDL_AtariXbios_mousex,2*1
 	.comm	_SDL_AtariXbios_mousey,2*1
@@ -191,7 +234,7 @@ no_mouse_lock:
 	.ascii "XBRA"
 	.ascii "LSDL"
 	.comm	oldjoystickvector,4*1
-_SDL_AtariXbios_JoystickVector:
+newjoystickvector:
 	movel	d0,sp@-
 
 	/* New joystick state */
@@ -211,4 +254,5 @@ _SDL_AtariXbios_JoystickVector:
 
 	.data
 	.even
+	.comm	_SDL_AtariXbios_installjoystickvector,2*1
 	.comm	_SDL_AtariXbios_joystick,2*1
diff --git a/src/video/ataricommon/SDL_xbiosinterrupt_s.h b/src/video/ataricommon/SDL_xbiosinterrupt_s.h
index dfb31c218..efaf85b29 100644
--- a/src/video/ataricommon/SDL_xbiosinterrupt_s.h
+++ b/src/video/ataricommon/SDL_xbiosinterrupt_s.h
@@ -30,22 +30,22 @@
 #ifndef _SDL_XBIOSINTERRUPT_S_H_
 #define _SDL_XBIOSINTERRUPT_S_H_
 
-#include <mint/osbind.h>
-
 #include "SDL_stdinc.h"
 
 /* Variables */
 
+extern Uint16 SDL_AtariXbios_installmousevector;	/* flag for SDL_AtariXbios_Install() */
 extern volatile Uint16 SDL_AtariXbios_mouselock;	/* mouse lock position */
 extern volatile Uint16 SDL_AtariXbios_mouseb;	/* buttons */
 extern volatile Sint16 SDL_AtariXbios_mousex;	/* X relative motion */
 extern volatile Sint16 SDL_AtariXbios_mousey;	/* Y relative motion */
+extern Uint16 SDL_AtariXbios_installjoystickvector;	/* flag for SDL_AtariXbios_Install() */
 extern volatile Uint16 SDL_AtariXbios_joystick;	/* Joystick */
 
 /* Functions */ 
 
-extern void SDL_AtariXbios_Install(_KBDVECS *kbdvecs,void *newmousevector,void *newjoystickvector);
-extern void SDL_AtariXbios_Restore(_KBDVECS *kbdvecs);
+extern void SDL_AtariXbios_Install(void);
+extern void SDL_AtariXbios_Restore(void);
 extern void SDL_AtariXbios_MouseVector(void *buf);
 extern void SDL_AtariXbios_JoystickVector(void *buf);