SDL-1.2: atari:video:xbios: align XBIOS_shadowscreen to 256 bytes for further optimisation

From ebfa9d7f4299af98aa786d9613822fddf716a114 Mon Sep 17 00:00:00 2001
From: Miro Kropacek <[EMAIL REDACTED]>
Date: Tue, 12 Dec 2023 22:44:13 +0100
Subject: [PATCH] atari:video:xbios: align XBIOS_shadowscreen to 256 bytes for
 further optimisation

---
 src/video/xbios/SDL_xbios.c     | 17 ++++++++++-------
 src/video/xbios/SDL_xbios.h     | 22 ++++++++++++----------
 src/video/xbios/SDL_xbios_f30.c |  2 +-
 src/video/xbios/SDL_xbios_st.c  |  5 +++--
 4 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/src/video/xbios/SDL_xbios.c b/src/video/xbios/SDL_xbios.c
index 8c4a20f8..aa2c2a7f 100644
--- a/src/video/xbios/SDL_xbios.c
+++ b/src/video/xbios/SDL_xbios.c
@@ -395,10 +395,11 @@ static void XBIOS_FreeBuffers(_THIS)
 {
 	(*XBIOS_freeVbuffers)(this);
 
-	if (XBIOS_shadowscreen) {
-		Mfree(XBIOS_shadowscreen);
-		XBIOS_shadowscreen=NULL;
+	if (XBIOS_shadowscreenmem) {
+		Mfree(XBIOS_shadowscreenmem);
+		XBIOS_shadowscreenmem=NULL;
 	}
+	XBIOS_shadowscreen=NULL;
 }
 
 static SDL_Surface *XBIOS_SetVideoMode(_THIS, SDL_Surface *current,
@@ -442,16 +443,18 @@ static SDL_Surface *XBIOS_SetVideoMode(_THIS, SDL_Surface *current,
 	lineWidth = (*XBIOS_getLineWidth)(this, new_video_mode, width, new_depth);
 
 	new_screen_size = lineWidth * height;
-	new_screen_size += 256; /* To align on a 256 byte adress */
+	new_screen_size += 255; /* To align on a 256 byte adress */
 
 	if (new_video_mode->flags & (XBIOSMODE_C2P | XBIOSMODE_SHADOWCOPY)) {
-		XBIOS_shadowscreen = Atari_SysMalloc(new_screen_size, MX_PREFTTRAM);
+		XBIOS_shadowscreenmem = Atari_SysMalloc(new_screen_size, MX_PREFTTRAM);
 
-		if (XBIOS_shadowscreen == NULL) {
+		if (XBIOS_shadowscreenmem == NULL) {
 			SDL_SetError("Can not allocate %d KB for shadow buffer", new_screen_size>>10);
 			return (NULL);
 		}
-		SDL_memset(XBIOS_shadowscreen, 0, new_screen_size);
+		SDL_memset(XBIOS_shadowscreenmem, 0, new_screen_size);
+
+		XBIOS_shadowscreen=(void *) (( (long) XBIOS_shadowscreenmem+255) & 0xFFFFFF00UL);
 	}
 
 	/* Output buffer needs to be twice in size for the software double-line mode */
diff --git a/src/video/xbios/SDL_xbios.h b/src/video/xbios/SDL_xbios.h
index b3faa8bd..ba4c2240 100644
--- a/src/video/xbios/SDL_xbios.h
+++ b/src/video/xbios/SDL_xbios.h
@@ -47,16 +47,17 @@ typedef struct
 #define NUM_MODELISTS	4		/* 8, 16, 24, and 32 bits-per-pixel */
 
 struct SDL_PrivateVideoData {
-	long old_video_mode;				/* Old video mode before entering SDL */
-	void *old_video_base;			/* Old pointer to screen buffer */
-	void *old_palette;				/* Old palette */
-	Uint32 old_num_colors;			/* Nb of colors in saved palette */
+	long old_video_mode;		/* Old video mode before entering SDL */
+	void *old_video_base;		/* Old pointer to screen buffer */
+	void *old_palette;		/* Old palette */
+	Uint32 old_num_colors;		/* Nb of colors in saved palette */
 
 	void *screens[2];		/* Pointers to aligned screen buffer */
-	void *screensmem[2];	/* Pointers to screen buffer */
-	void *shadowscreen;		/* Shadow screen for c2p conversion */
+	void *screensmem[2];		/* Pointers to screen buffer */
+	void *shadowscreen;		/* Pointer to aligned shadow screen buffer */
+	void *shadowscreenmem;		/* Pointers to shadow screen buffer */
 	int frame_number;		/* Number of frame for double buffer */
-	int pitch;				/* Destination line width for C2P */
+	int pitch;			/* Destination line width for C2P */
 
 	const xbiosmode_t *current;	/* Current set mode */
 	int SDL_nummodes[NUM_MODELISTS];
@@ -113,8 +114,9 @@ enum {
 #define XBIOS_screens		(this->hidden->screens)
 #define XBIOS_screensmem	(this->hidden->screensmem)
 #define XBIOS_shadowscreen	(this->hidden->shadowscreen)
-#define XBIOS_fbnum			(this->hidden->frame_number)
-#define XBIOS_pitch			(this->hidden->pitch)
+#define XBIOS_shadowscreenmem	(this->hidden->shadowscreenmem)
+#define XBIOS_fbnum		(this->hidden->frame_number)
+#define XBIOS_pitch		(this->hidden->pitch)
 #define XBIOS_current		(this->hidden->current)
 #define XBIOS_recoffset		(this->hidden->recalc_offset)
 
@@ -125,7 +127,7 @@ enum {
 #define XBIOS_saveMode		(this->hidden->saveMode)
 #define XBIOS_setMode		(this->hidden->setMode)
 #define XBIOS_restoreMode	(this->hidden->restoreMode)
-#define XBIOS_vsync			(this->hidden->vsync)
+#define XBIOS_vsync		(this->hidden->vsync)
 #define XBIOS_getScreenFormat	(this->hidden->getScreenFormat)
 #define XBIOS_getLineWidth	(this->hidden->getLineWidth)
 #define XBIOS_swapVbuffers	(this->hidden->swapVbuffers)
diff --git a/src/video/xbios/SDL_xbios_f30.c b/src/video/xbios/SDL_xbios_f30.c
index 29d4c693..d3038483 100644
--- a/src/video/xbios/SDL_xbios_f30.c
+++ b/src/video/xbios/SDL_xbios_f30.c
@@ -278,7 +278,7 @@ static int allocVbuffers_SV(_THIS, const xbiosmode_t *new_video_mode, int num_bu
 		SDL_memset(XBIOS_screensmem[i], 0, bufsize);
 
 		/* Align on 256byte boundary and map to Supervidel memory */
-		tmp = ( (Uint32) XBIOS_screensmem[i]+256) & 0xFFFFFF00UL;
+		tmp = ( (Uint32) XBIOS_screensmem[i]+255) & 0xFFFFFF00UL;
 		tmp |= 0xA0000000UL;	/* Map to SV memory */
 		XBIOS_screens[i] = (void *) tmp;
 	}
diff --git a/src/video/xbios/SDL_xbios_st.c b/src/video/xbios/SDL_xbios_st.c
index fed70b87..aaf8210d 100644
--- a/src/video/xbios/SDL_xbios_st.c
+++ b/src/video/xbios/SDL_xbios_st.c
@@ -183,7 +183,7 @@ static int allocVbuffers(_THIS, const xbiosmode_t *new_video_mode, int num_buffe
 		}
 		SDL_memset(XBIOS_screensmem[i], 0, bufsize);
 
-		XBIOS_screens[i]=(void *) (( (long) XBIOS_screensmem[i]+256) & 0xFFFFFF00UL);
+		XBIOS_screens[i]=(void *) (( (long) XBIOS_screensmem[i]+255) & 0xFFFFFF00UL);
 	}
 
 	return (1);
@@ -196,8 +196,9 @@ static void freeVbuffers(_THIS)
 	for (i=0;i<2;i++) {
 		if (XBIOS_screensmem[i]) {
 			Mfree(XBIOS_screensmem[i]);
+			XBIOS_screensmem[i]=NULL;
 		}
-		XBIOS_screensmem[i]=NULL;
+		XBIOS_screens[i]=NULL;
 	}
 }