From 63ed22ba679b4b371294dfc71b1664fa015ff260 Mon Sep 17 00:00:00 2001
From: nmlgc <nmlgc@nmlgc.net>
Date: Tue, 10 Mar 2020 23:10:36 +0100
Subject: [PATCH] [Reverse-engineering] [th01] .GRP palette

Part of P0082, funded by Ember2528.
---
 th01/formats/grp.h                |  1 +
 th01/formats/grp_palette[bss].asm |  2 +
 th01_fuuin.asm                    | 68 ++++++++++---------------------
 th01_op.asm                       | 10 ++---
 th01_reiiden.asm                  | 14 +++----
 th01_reiiden_2.inc                |  8 ++--
 6 files changed, 40 insertions(+), 63 deletions(-)
 create mode 100644 th01/formats/grp.h
 create mode 100644 th01/formats/grp_palette[bss].asm

diff --git a/th01/formats/grp.h b/th01/formats/grp.h
new file mode 100644
index 00000000..aa1ffd2c
--- /dev/null
+++ b/th01/formats/grp.h
@@ -0,0 +1 @@
+extern Palette4 grp_palette;
diff --git a/th01/formats/grp_palette[bss].asm b/th01/formats/grp_palette[bss].asm
new file mode 100644
index 00000000..6894ca0a
--- /dev/null
+++ b/th01/formats/grp_palette[bss].asm
@@ -0,0 +1,2 @@
+public _grp_palette
+_grp_palette	palette_t <?>
diff --git a/th01_fuuin.asm b/th01_fuuin.asm
index cae8fe2c..533ef491 100644
--- a/th01_fuuin.asm
+++ b/th01_fuuin.asm
@@ -5132,8 +5132,8 @@ loc_C86B:
 		cmp	[bp+arg_0], 64h	; 'd'
 		jle	short loc_C8A9
 		mov	bx, si
-		imul	bx, 3
-		mov	al, [bx+di+170Ah]
+		imul	bx, size rgb_t
+		mov	al, byte ptr _grp_palette[bx+di]
 		cbw
 		mov	dx, 0Fh
 		sub	dx, ax
@@ -5148,16 +5148,16 @@ loc_C86B:
 		idiv	bx
 		mov	[bp+var_2], ax
 		mov	bx, si
-		imul	bx, 3
-		mov	al, [bx+di+170Ah]
+		imul	bx, size rgb_t
+		mov	al, byte ptr _grp_palette[bx+di]
 		add	al, byte ptr [bp+var_2]
 		jmp	short loc_C8DE
 ; ---------------------------------------------------------------------------
 
 loc_C8A9:
 		mov	bx, si
-		imul	bx, 3
-		mov	al, [bx+di+170Ah]
+		imul	bx, size rgb_t
+		mov	al, byte ptr _grp_palette[bx+di]
 		cbw
 		mov	[bp+var_2], ax
 		mov	ax, 64h	; 'd'
@@ -5170,20 +5170,20 @@ loc_C8A9:
 		idiv	bx
 		mov	[bp+var_2], ax
 		mov	bx, si
-		imul	bx, 3
-		mov	al, [bx+di+170Ah]
+		imul	bx, size rgb_t
+		mov	al, byte ptr _grp_palette[bx+di]
 		mov	dl, byte ptr [bp+var_2]
 		neg	dl
 		add	al, dl
 
 loc_C8DE:
 		mov	bx, si
-		imul	bx, 3
+		imul	bx, size rgb_t
 		mov	_z_Palettes[bx+di], al
 		inc	di
 
 loc_C8E8:
-		cmp	di, 3
+		cmp	di, size rgb_t
 		jge	short loc_C8F0
 		jmp	loc_C86B
 ; ---------------------------------------------------------------------------
@@ -5192,7 +5192,7 @@ loc_C8F0:
 		inc	si
 
 loc_C8F1:
-		cmp	si, 10h
+		cmp	si, COLOR_COUNT
 		jge	short loc_C8F9
 		jmp	loc_C867
 ; ---------------------------------------------------------------------------
@@ -5435,14 +5435,8 @@ arg_2		= word ptr  8
 ; ---------------------------------------------------------------------------
 
 loc_EA34:
-		push	0
-		push	12h
-		push	0
-		call	file_seek
-		push	ds
-		push	offset unk_141AA
-		push	30h ; '0'
-		call	file_read
+		call	file_seek pascal, 0, 12h, 0
+		call	file_read pascal, ds, offset _grp_palette, size palette_t
 		push	word_13507
 		call	sub_C842
 		call	file_close
@@ -5477,14 +5471,8 @@ arg_2		= word ptr  8
 ; ---------------------------------------------------------------------------
 
 loc_EA75:
-		push	0
-		push	12h
-		push	0
-		call	file_seek
-		push	ds
-		push	offset unk_141AA
-		push	30h ; '0'
-		call	file_read
+		call	file_seek pascal, 0, 12h, 0
+		call	file_read pascal, ds, offset _grp_palette, size palette_t
 		call	file_close
 		xor	ax, ax
 		pop	bp
@@ -5580,24 +5568,24 @@ loc_EB0D:
 
 loc_EB11:
 		mov	ax, dx
-		imul	ax, 3
+		imul	ax, size rgb_t
 		les	bx, [bp+arg_0]
 		add	bx, ax
 		add	bx, cx
 		mov	al, es:[bx]
 		mov	bx, dx
-		imul	bx, 3
+		imul	bx, size rgb_t
 		add	bx, cx
-		mov	[bx+170Ah], al
+		mov	byte ptr _grp_palette[bx], al
 		inc	cx
 
 loc_EB2C:
-		cmp	cx, 3
+		cmp	cx, size rgb_t
 		jl	short loc_EB11
 		inc	dx
 
 loc_EB32:
-		cmp	dx, 10h
+		cmp	dx, COLOR_COUNT
 		jl	short loc_EB0D
 		pop	bp
 		retf
@@ -6309,7 +6297,7 @@ include th01/hardware/graph[bss].asm
 		dd    ?
 		dd    ?
 		dd    ?
-unk_141AA	db    ?	;
+include th01/formats/grp_palette[bss].asm
 		dd    ?
 		dd    ?
 		dd    ?
@@ -6594,20 +6582,6 @@ unk_141AA	db    ?	;
 		dd    ?
 		dd    ?
 		dd    ?
-		dd    ?
-		dd    ?
-		dd    ?
-		dd    ?
-		dd    ?
-		dd    ?
-		dd    ?
-		dd    ?
-		dd    ?
-		dd    ?
-		dd    ?
-		db    ?	;
-		db    ?	;
-		db    ?	;
 ; void (*off_1464A)(void)
 off_1464A	dw ?
 word_1464C	dw ?
diff --git a/th01_op.asm b/th01_op.asm
index fddc7b4f..60310dc6 100644
--- a/th01_op.asm
+++ b/th01_op.asm
@@ -2975,8 +2975,8 @@ arg_0		= dword	ptr  6
 
 loc_D9F8:
 		call	file_seek pascal, large 12h, 0
-		call	file_read pascal, ds, offset palette_136D2, size palette_t
-		call	_z_palette_set_all_show c, offset palette_136D2, ds
+		call	file_read pascal, ds, offset _grp_palette, size palette_t
+		call	_z_palette_set_all_show c, offset _grp_palette, ds
 		call	file_close
 		xor	ax, ax
 		pop	bp
@@ -3005,7 +3005,7 @@ arg_0		= dword	ptr  6
 
 loc_DA37:
 		call	file_seek pascal, large 12h, 0
-		call	file_read pascal, ds, offset palette_136D2, size palette_t
+		call	file_read pascal, ds, offset _grp_palette, size palette_t
 		call	file_close
 		xor	ax, ax
 		pop	bp
@@ -3083,7 +3083,7 @@ loc_DAC4:
 		mov	bx, dx
 		imul	bx, size rgb_t
 		add	bx, cx
-		mov	byte ptr palette_136D2[bx], al
+		mov	byte ptr _grp_palette[bx], al
 		inc	cx
 
 loc_DADF:
@@ -3731,7 +3731,7 @@ include th01/hardware/vram_planes[bss].asm
 		dd    ?
 		dd    ?
 		dd    ?
-palette_136D2	palette_t <?>
+include th01/formats/grp_palette[bss].asm
 		dd    ?
 		dd    ?
 		dd    ?
diff --git a/th01_reiiden.asm b/th01_reiiden.asm
index cb13900a..10967f60 100644
--- a/th01_reiiden.asm
+++ b/th01_reiiden.asm
@@ -6247,8 +6247,8 @@ arg_0		= dword	ptr  6
 
 loc_10ABA:
 		call	file_seek pascal, large 18, 0
-		call	file_read pascal, ds, offset palette_38988, size palette_t
-		call	_z_palette_set_all_show c, offset palette_38988, ds
+		call	file_read pascal, ds, offset _grp_palette, size palette_t
+		call	_z_palette_set_all_show c, offset _grp_palette, ds
 		call	file_close
 		xor	ax, ax
 		pop	bp
@@ -6280,7 +6280,7 @@ loc_10AF9:
 		push	0
 		call	file_seek
 		push	ds
-		push	offset palette_38988
+		push	offset _grp_palette
 		push	30h ; '0'
 		call	file_read
 		call	file_close
@@ -6378,7 +6378,7 @@ loc_10B86:
 		mov	bx, dx
 		imul	bx, size rgb_t
 		add	bx, cx
-		mov	byte ptr palette_38988[bx], al
+		mov	byte ptr _grp_palette[bx], al
 		inc	cx
 
 loc_10BA1:
@@ -9024,7 +9024,7 @@ loc_12EC7:
 		mov	bx, [bp+var_C]
 		imul	bx, size rgb_t
 		add	bx, [bp+var_E]
-		cmp	al, byte ptr palette_38988[bx]
+		cmp	al, byte ptr _grp_palette[bx]
 		jz	short loc_12F1B
 		mov	bx, [bp+var_C]
 		imul	bx, 3
@@ -9032,7 +9032,7 @@ loc_12EC7:
 		mov	bx, [bp+var_C]
 		imul	bx, 3
 		add	bx, [bp+var_E]
-		cmp	al, byte ptr palette_38988[bx]
+		cmp	al, byte ptr _grp_palette[bx]
 		jge	short loc_12EFF
 		mov	al, 1
 		jmp	short loc_12F01
@@ -27784,7 +27784,7 @@ include th01/hardware/vram_planes[bss].asm
 		dd    ?
 		dd    ?
 		dd    ?
-palette_38988	palette_t <?>
+include th01/formats/grp_palette[bss].asm
 		dd    ?
 		dd    ?
 		dd    ?
diff --git a/th01_reiiden_2.inc b/th01_reiiden_2.inc
index a833a041..23825c1e 100644
--- a/th01_reiiden_2.inc
+++ b/th01_reiiden_2.inc
@@ -30788,14 +30788,14 @@ loc_2C9C6:
 ; ---------------------------------------------------------------------------
 
 loc_2C9D4:
-		mov	byte ptr palette_38988.colors[15 * size rgb_t][si], 0
+		mov	byte ptr _grp_palette.colors[15 * size rgb_t][si], 0
 		inc	si
 
 loc_2C9DA:
 		cmp	si, size rgb_t
 		jb	short loc_2C9D4
 		push	ds
-		push	offset palette_38988
+		push	offset _grp_palette
 		push	ds
 		push	offset _z_Palettes
 		push	0
@@ -31504,7 +31504,7 @@ loc_2D011:
 		mov	bx, [bp+var_4]
 		imul	bx, size rgb_t
 		add	bx, [bp+var_6]
-		mov	al, byte ptr palette_38988[bx]
+		mov	al, byte ptr _grp_palette[bx]
 		mov	bx, [bp+var_4]
 		imul	bx, size rgb_t
 		add	bx, [bp+var_6]
@@ -35916,7 +35916,7 @@ loc_2FB22:
 		mov	bx, [bp+var_2]
 		imul	bx, size rgb_t
 		add	bx, [bp+var_4]
-		mov	al, byte ptr palette_38988[bx]
+		mov	al, byte ptr _grp_palette[bx]
 		mov	bx, [bp+var_2]
 		imul	bx, size rgb_t
 		add	bx, [bp+var_4]