shithub: pokecrystal

Download patch

ref: ad6effee825a145a58805648a7da6e6cc0e75d05
parent: 4613bcfadf2df5da711646f1ee54e2e4982309c7
author: Rangi <[email protected]>
date: Mon Nov 30 06:32:19 EST 2020

Don't hardcode CRCs within Crystal base data

--- a/tools/stadium.c
+++ b/tools/stadium.c
@@ -1,9 +1,9 @@
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdbool.h>
-#include <getopt.h>
 #include <string.h>
 #include <stdint.h>
+#include <stdbool.h>
+#include <getopt.h>
 
 #include "common.h"
 
@@ -17,25 +17,29 @@
 #define HEADERSIZE (N64PS3SIZE + 2)
 // Checksum every half-bank
 #define CHECKSIZE 0x2000
-// The CRC initial value (also used for checksums)
-#define CRC_INIT 0xFEFE
 // The CRC polynomial value
 #define CRC_POLY 0xC387
+// The CRC initial value (also used for checksums)
+#define CRC_INIT 0xFEFE
+// The CRC initial value for Crystal base data
+#define CRC_INIT_BASE 0xACDE
 
 typedef enum Base { BASE_NONE, BASE_US, BASE_EU, BASE_DEBUG } Base;
 
-uint8_t us_base[BASESIZE] = {'b', 'a', 's', 'e',
-	0x01, 0x00, 0xBF, 0x6B, 0x40, 0x11, 0x00, 0x22, 0x00, 0x3A,
-	0xF3, 0x38, 0x18, 0xFF, 0xFF, 0x0F, 0x07, 0x10, 0x68, 0x07};
+// Base data format: "base", 1, version, CRC (big-endian), 16 unknown bytes
 
-uint8_t eu_base[BASESIZE] = {'b', 'a', 's', 'e',
-	0x01, 0x01, 0x1E, 0xCF, 0x00, 0x10, 0x00, 0x00, 0x00, 0x0C,
-	0xA3, 0x38, 0x00, 0xFF, 0xFF, 0x07, 0x00, 0x00, 0x00, 0x14};
+uint8_t us_base[BASESIZE] = {'b', 'a', 's', 'e', 1, 0, 0, 0,
+	0x40, 0x11, 0x00, 0x22, 0x00, 0x3A, 0xF3, 0x38,
+	0x18, 0xFF, 0xFF, 0x0F, 0x07, 0x10, 0x68, 0x07};
 
-uint8_t dbg_base[BASESIZE] = {'b', 'a', 's', 'e',
-	0x01, 0x00, 0x07, 0x82, 0x40, 0x10, 0x00, 0x22, 0x00, 0x3A,
-	0xE3, 0x38, 0x00, 0xFF, 0xFF, 0x07, 0x07, 0x10, 0x68, 0x06};
+uint8_t eu_base[BASESIZE] = {'b', 'a', 's', 'e', 1, 1, 0, 0,
+	0x00, 0x10, 0x00, 0x00, 0x00, 0x0C, 0xA3, 0x38,
+	0x00, 0xFF, 0xFF, 0x07, 0x00, 0x00, 0x00, 0x14};
 
+uint8_t dbg_base[BASESIZE] = {'b', 'a', 's', 'e', 1, 0, 0, 0,
+	0x40, 0x10, 0x00, 0x22, 0x00, 0x3A, 0xE3, 0x38,
+	0x00, 0xFF, 0xFF, 0x07, 0x07, 0x10, 0x68, 0x06};
+
 uint8_t n64ps3[N64PS3SIZE] = {'N', '6', '4', 'P', 'S', '3'};
 
 static void usage(void) {
@@ -81,6 +85,18 @@
 	int DATASIZE = HEADERSIZE + NUMCHECKS * 2; // 2 bytes per checksum
 	int ORIGIN = filesize - DATASIZE; // Stadium data goes at the end of the file
 
+	// Initialize the CRC table
+	uint16_t crc_table[256];
+	for (int i = 0; i < 256; i++) {
+		uint16_t c = i;
+		uint16_t rem = 0;
+		for (int y = 0; y < 8; y++) {
+			rem = (rem >> 1) ^ ((rem ^ c) & 1 ? CRC_POLY : 0);
+			c >>= 1;
+		}
+		crc_table[i] = rem;
+	}
+
 	// Clear the global checksum
 	SET_U16BE(file, GLOBALOFF, 0);
 
@@ -94,6 +110,15 @@
 		memcpy(file + BASEOFF, dbg_base, BASESIZE);
 	}
 
+	// Calculate the CRC of the base data, or none
+	if (base) {
+		uint16_t crc = CRC_INIT_BASE;
+		for (int i = BASEOFF; i < BASEOFF + BASESIZE; i++) {
+			crc = (crc >> 8) ^ crc_table[(crc & 0xFF) ^ file[i]];
+		}
+		SET_U16BE(file, BASEOFF + 6, crc);
+	}
+
 	// Initialize the Stadium data (this should be free space anyway)
 	memset(file + ORIGIN, 0, DATASIZE);
 	memcpy(file + ORIGIN, n64ps3, N64PS3SIZE);
@@ -105,18 +130,6 @@
 			checksum += file[i * CHECKSIZE + j];
 		}
 		SET_U16BE(file, ORIGIN + HEADERSIZE + i * 2, checksum);
-	}
-
-	// Initialize the CRC table
-	uint16_t crc_table[256];
-	for (int i = 0; i < 256; i++) {
-		uint16_t c = i;
-		uint16_t rem = 0;
-		for (int y = 0; y < 8; y++) {
-			rem = (rem >> 1) ^ ((rem ^ c) & 1 ? CRC_POLY : 0);
-			c >>= 1;
-		}
-		crc_table[i] = rem;
 	}
 
 	// Calculate the CRC of the half-bank checksums