From 19cd46f7f835412f01c72282dc576c886e790b3f Mon Sep 17 00:00:00 2001
From: tseng0201 <99745983+tseng0201@users.noreply.github.com>
Date: Fri, 13 Jan 2023 04:32:28 +0800
Subject: [PATCH 1/3] IMPLEMENT COMBINE SCREENS AND CUT WIPE SCREEN EFFECT
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Reduce memory use for doom
Control flag "DISABLE_WIPES" and "COMBINE_SCREENS" are define in doomdef.h.
DISABLE_WIPES will cut wipe screen effect, COMBINE_SCREENS will discard screen buffer_1、buffer_2、buffer_3.
V_Init() function make four screen buffers pointer to same address(address of screen buffer #0) In v_video.c.
DOOMHEAP are replaced by static array and it size are controled by DOOM_HEAP_SIZE, both are in i_system.c.
Screen buffers are replaced by static array and I_AllocLow will return screen buffers directly in i_system.c.
If cut wipe screen effect, in f_wipe.c only wipe_StartScreen()、wipe_EndScreen()、wipe_ScreenWipe() will remain,
wipe_ScreenWipe() only copy data from screen buffer_3 to screen buffer_0 directly.
---
 src/doomdef.h        |  9 +++++++++
 src/f_wipe.c         | 19 ++++++++++++++++++-
 src/riscv/i_system.c | 28 +++++++++++++++++++++++-----
 src/v_video.c        |  9 ++++++++-
 4 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/src/doomdef.h b/src/doomdef.h
index ebd51f0..404ce8e 100644
--- a/src/doomdef.h
+++ b/src/doomdef.h
@@ -27,6 +27,15 @@
 #include <stdio.h>
 #include <string.h>
 
+// Disable screens wipe effect, you will get worse game experience, 
+// but it can save about 130KB on DOOMHEAP.
+#define DISABLE_WIPES
+
+// Discard screen buffers, any screen effect will direct output on your screen, 
+// It can save 192kb on heap. 
+// Remeber you can only use this when disable screens wipe.
+#define COMBINE_SCREENS
+
 // The packed attribute forces structures to be packed into the minimum
 // space necessary.  If this is not done, the compiler may align structure
 // fields differently to optimise memory access, inflating the overall
diff --git a/src/f_wipe.c b/src/f_wipe.c
index d46607b..cf20792 100644
--- a/src/f_wipe.c
+++ b/src/f_wipe.c
@@ -46,7 +46,7 @@ static byte*    wipe_scr_start;
 static byte*    wipe_scr_end;
 static byte*    wipe_scr;
 
-
+#ifndef DISABLE_WIPES
 void
 wipe_shittyColMajorXform
 ( short*        array,
@@ -233,6 +233,7 @@ wipe_exitMelt
     Z_Free(y);
     return 0;
 }
+#endif
 
 int
 wipe_StartScreen
@@ -246,6 +247,7 @@ wipe_StartScreen
     return 0;
 }
 
+#ifndef DISABLE_WIPES
 int
 wipe_EndScreen
 ( int   x,
@@ -301,3 +303,18 @@ wipe_ScreenWipe
     return !go;
 
 }
+#else
+int
+wipe_ScreenWipe
+( int	wipeno,
+  int	x,
+  int	y,
+  int	width,
+  int	height,
+  int	ticks )
+{
+	//Because we don't need wipe effct, just move screens[3] to screens[0]
+	memcpy( screens[0], wipe_scr_end, width*height );
+	return 1;
+}
+#endif
diff --git a/src/riscv/i_system.c b/src/riscv/i_system.c
index 2ed5c50..e9084e6 100644
--- a/src/riscv/i_system.c
+++ b/src/riscv/i_system.c
@@ -39,6 +39,21 @@
 
 #include "console.h"
 
+#ifdef COMBINE_SCREENS
+unsigned char CombinedScreens[SCREENWIDTH*SCREENHEIGHT];
+#else
+unsigned char CombinedScreens[SCREENWIDTH*SCREENHEIGHT*4];
+#endif
+
+/* Original 6M - wipe function (130560 bytes) */
+#ifdef DISABLE_WIPES
+#define DOOM_HEAP_SIZE  6*1024*1024 - 130560
+#else
+#define DOOM_HEAP_SIZE  6*1024*1024
+#endif
+
+unsigned char DOOMHeap[DOOM_HEAP_SIZE];
+
 enum {
 	KEY_EVENT = 0,
 	MOUSE_MOTION_EVENT = 1,
@@ -139,9 +154,9 @@ I_Init(void)
 byte *
 I_ZoneBase(int *size)
 {
-	/* Give 6M to DOOM */
-	*size = 6 * 1024 * 1024;
-	return (byte *) malloc (*size);
+	/* Give DOOM_HEAP_SIZE to DOOM */
+	*size = DOOM_HEAP_SIZE;
+	return (byte *) DOOMHeap;
 }
 
 
@@ -313,8 +328,11 @@ I_Quit(void)
 byte *
 I_AllocLow(int length)
 {
-	/* FIXME: check if memory allocation succeeds */
-	return calloc(1, length);
+	/* Return screen buffer */
+	byte*	mem;
+	mem = CombinedScreens;
+	memset (mem,0,length);
+	return mem;
 }
 
 
diff --git a/src/v_video.c b/src/v_video.c
index 881f4ab..301b835 100644
--- a/src/v_video.c
+++ b/src/v_video.c
@@ -463,9 +463,16 @@ void V_Init (void)
     byte*       base;
 
     // stick these in low dos memory on PCs
-
+    
+#ifndef COMBINE_SCREENS
     base = I_AllocLow (SCREENWIDTH*SCREENHEIGHT*4);
 
     for (i=0 ; i<4 ; i++)
         screens[i] = base + i*SCREENWIDTH*SCREENHEIGHT;
+#else
+    base = I_AllocLow (SCREENWIDTH*SCREENHEIGHT);
+
+    for (i=0 ; i<4 ; i++)
+        screens[i] = base;
+#endif
 }

From 3f47238667a91cac7263c8d69065025f6a2cacd2 Mon Sep 17 00:00:00 2001
From: tseng0201 <99745983+tseng0201@users.noreply.github.com>
Date: Sat, 14 Jan 2023 00:01:11 +0900
Subject: [PATCH 2/3] PROVIDE THE PROOF FOR MEMORY REDUCION

provide the proof for memory reduction, detail in doomdef.h
---
 src/doomdef.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/doomdef.h b/src/doomdef.h
index 404ce8e..ef545ad 100644
--- a/src/doomdef.h
+++ b/src/doomdef.h
@@ -29,10 +29,19 @@
 
 // Disable screens wipe effect, you will get worse game experience, 
 // but it can save about 130KB on DOOMHEAP.
+// In f_wipe.c, only wipe_initMelt() and wipe_shittyColMajorXform() call Z_Malloc ask for some sapce,
+// wipe_initMelt() ask 320 * 8(width * sizeof(int)) = 2560 bytes,
+// wipe_shittyColMajorXform() ask 160 * 200 * 2(width/2 * height*2) = 64000 bytes,
+// it will be call twice so it will totaly ask 128000 bytes,
+// it means we can save 2560 + 128000 = 130560 bytes on DOOMHEAP when disable screens wipe.
 #define DISABLE_WIPES
 
 // Discard screen buffers, any screen effect will direct output on your screen, 
-// It can save 192kb on heap. 
+// It can save 192kb memory usage.
+// Each screen buffer is 320 * 200 * 1 = 64000 bytes, 
+// COMBINE_SCREENS will make four screen buffers merge to one,
+// so we can save (4 - 1) * 320 * 200 = 192000 bytes.
+// In this Implement it will set "CombinedScreens[SCREENWIDTH*SCREENHEIGHT]" to replace original "CombinedScreens[SCREENWIDTH*SCREENHEIGHT*4]".
 // Remeber you can only use this when disable screens wipe.
 #define COMBINE_SCREENS
 

From 39c9238cac9a44d46b611f09cdbab876d6832e9e Mon Sep 17 00:00:00 2001
From: tseng0201 <99745983+tseng0201@users.noreply.github.com>
Date: Sat, 14 Jan 2023 01:43:47 +0900
Subject: [PATCH 3/3] ADD STATIC MODIFIER FOR HELP COMPILER OPTIMIZATION

Add static modifier on CombinedScreens and DOOMHeap in i_system.c for help compiler optimization.
No other file use both array directly and they use them by pointer.
---
 src/riscv/i_system.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/riscv/i_system.c b/src/riscv/i_system.c
index e9084e6..b08cfcc 100644
--- a/src/riscv/i_system.c
+++ b/src/riscv/i_system.c
@@ -40,9 +40,9 @@
 #include "console.h"
 
 #ifdef COMBINE_SCREENS
-unsigned char CombinedScreens[SCREENWIDTH*SCREENHEIGHT];
+static unsigned char CombinedScreens[SCREENWIDTH*SCREENHEIGHT];
 #else
-unsigned char CombinedScreens[SCREENWIDTH*SCREENHEIGHT*4];
+static unsigned char CombinedScreens[SCREENWIDTH*SCREENHEIGHT*4];
 #endif
 
 /* Original 6M - wipe function (130560 bytes) */
@@ -52,7 +52,7 @@ unsigned char CombinedScreens[SCREENWIDTH*SCREENHEIGHT*4];
 #define DOOM_HEAP_SIZE  6*1024*1024
 #endif
 
-unsigned char DOOMHeap[DOOM_HEAP_SIZE];
+static unsigned char DOOMHeap[DOOM_HEAP_SIZE];
 
 enum {
 	KEY_EVENT = 0,