mirror of
https://github.com/pret/pokegold.git
synced 2026-09-30 10:56:37 -05:00
Merge pull request #59 from Rangi42/master
Generate the Stadium 2 checksum data with pfero's tool instead of raw INCBINs
This commit is contained in:
1
Makefile
1
Makefile
@@ -132,6 +132,7 @@ pokesilver_debug_opt = -cjsv -t POKEMON_SLV -i AAXE -k 01 -l 0x33 -m 0x10 -r 3 -
|
||||
%.gbc: $$(%_obj) layout.link
|
||||
$(RGBLINK) -n $*.sym -m $*.map -l layout.link -o $@ $(filter %.o,$^)
|
||||
$(RGBFIX) $($*_opt) $@
|
||||
tools/stadium $@
|
||||
|
||||
|
||||
### LZ compression rules
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
N64PS3ènZãÝÔ[ —0ƒ5ÊiBušQ]0U`¢5Ѷ*L¸P%'gìER^]×’îƒÝþþ@jtYÀ„Ôìi¼O8Ѐ²SXõþþXxÃþþþþDTÉ.<1B>*ŽçÄœáš6ë3Ü4Vò_“Uð„-N7<19>¿óœ„Ke—œRÄÞÔ1ܦz<C2A6>)—þþþþ§ã^‡@
‰ýKÅó9’øþþþþþþþþþþþþþþš{muì‡Å¨þþþþþþþþÂôbqþþþþGò6_W›*YÁ‚'.õEþŠþþþþþþþþ)gþþˆyÖÆÇyYk·iuÞ%„þH¬ËKmÀͼB>A²aþþŒ½0¹¬ðÆÑ¥þþü扟&#\RÕµ°&U+y:;þþ$ÛÎiþþ-òC)=>ˆþþXüþþø¸·Ê·i‰*=þþäéþþe/þþålþþþYþþiÉþþA–ÃíI_þþþþ7þþ©þþby•¶\jþþ<^þþÍa©Ôr}þþ´ÍiØÌþþ¥Ëþþþþþþk쵩Œ1rhê¯þþþþþþâ:þþêËþþДþþmÂþþÔ®þþ±>7á…‚þþþþþþ™þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþU%
|
||||
@@ -1 +0,0 @@
|
||||
N64PS3<EFBFBD>JZßÞnøkl—,ƒ5É]B£u–Q˜1P`¢5Ѷ*L¸P&gìER^]×’îƒóþþ@jtYÀ„Õi¼K8ü–²SXõþþXxÃþþþþD|É.<1B>*ŽçÄœáš6ë3Ü4Vò_“Uð„-N7<19>¿óœ„Ke—œRÄÞÔ1ܦz<C2A6>,Ôþþþþ§ù^‡C‡‰ýKÅó{’øþþþþþþþþþþþþþþš{muì‡Å¨þþþþþþþþÂôbqþþþþGò6_W›*YÁ‚'.õEþŠþþþþþþþþ)gþþˆyÖÆÇyYjÎiuÞ%„þH¬ËKmÀͼB>A²aþþŒ½0¹¬ðÆÑ¥þþü扟&#\RÕµ°&U+y:;þþ$ÛÎiþþ-òC)=>ˆþþXüþþø¸·Ê·i‰*=þþäéþþe/þþålþþþYþþiÉþþA–ÃíI_þþþþ7þþ©þþby•¶\jþþ<^þþÍa©Ôr}þþ´ÍiØÌþþ¥Ëþþþþþþk쵩Œ1rhê¯þþþþþþâ:þþêËþþДþþmÂþþÔ®þþ±>7á…‚þþþþþþÍ|6øþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþUJ
|
||||
@@ -1,2 +0,0 @@
|
||||
N64PS3yPZ©Ý1VB÷–Þ|ÍÇé<%v1Pe0-`<60>5Ѷ*L¸P%'gìE2^]Öõ§ƒžþþ@jtYÀ„Ô²i¼8$€²SX§þþ¿4±äþþþþCdÉ.h#¿Vå6â6Q©þ2)´w³hdÀ΢™%
|
||||
ñå—«e‘ØëQ<pŽ`„¹$(z<>){þþþþ¨‘^?ó‰ýKÅórÆôþþþþþþþþþþþþþþš{muì‡Å¨þþþþþþþþÝÄbþþþþGò6_W›*UÁ‚'.õCþŠþþþþþþþþ)gþþˆyÖÆÇyKŒ)}çÞ%„þH¬ËKmÀͼB>A²aþþŒ¹0¹¬ðÆÑ¥þþü扟&#\RÕµ°&U+y:;þþ$ÛÎiþþ-òC)=>ˆþþXüþþø¸·Ê·i‰*=þþäéþþe/þþålþþþYþþiÉþþA–ÃíI_þþþþ7þþ©þþby•¶\jþþ<^þþÍa©Ôr}þþ´ÍiØÌþþ¥Ëþþþþþþk쵩Œ1rhê¯þþþþþþÃ"þþäïþþÝÊþþ{þþÔ®þþ±>7á…‚þþþþþþš¬þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþR¹
|
||||
@@ -1,2 +0,0 @@
|
||||
N64PS3ø¡Z¥ÝËÎR®–Ú|ÍÈÛ<©v-P 1(`<60>5Ѷ*L¸P&gìE2^]Öõ§ƒ´þþ@jtYÀ„ÔÈi¼8P–²SX§þþ¿4±äþþþþCŒÉ.h#¿Vå6â6Q©þ2)´w³hdÀ΢™%
|
||||
ñå—«e‘ØëQ<pŽ`„¹$(z<>,¸þþþþ¨§^Cm‰ýKÅòµÆôþþþþþþþþþþþþþþš{muì‡Å¨þþþþþþþþÝÄbþþþþGò6_W›*UÁ‚'.õCþŠþþþþþþþþ)gþþˆyÖÆÇyKŒ?}çÞ%„þH¬ËKmÀͼB>A²aþþŒ¹0¹¬ðÆÑ¥þþü扟&#\RÕµ°&U+y:;þþ$ÛÎiþþ-òC)=>ˆþþXüþþø¸·Ê·i‰*=þþäéþþe/þþålþþþYþþiÉþþA–ÃíI_þþþþ7þþ©þþby•¶\jþþ<^þþÍa©Ôr}þþ´ÍiØÌþþ¥Ëþþþþþþk쵩Œ1rhê¯þþþþþþÃ"þþäïþþÝÊþþ{þþÔ®þþ±>7á…‚þþþþþþÍ|7¦þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþSÇ
|
||||
@@ -251,7 +251,7 @@ ROMX $70
|
||||
"bank70_2"
|
||||
ROMX $7f
|
||||
org $7df8
|
||||
"Stadium Data"
|
||||
"Stadium 2 Checksums"
|
||||
WRAM0
|
||||
"Audio RAM"
|
||||
"WRAM"
|
||||
|
||||
22
main.asm
22
main.asm
@@ -439,18 +439,12 @@ INCBIN "gfx/pokegear/pokegear.2bpp.lz"
|
||||
INCLUDE "data/credits_strings.asm"
|
||||
|
||||
|
||||
SECTION "Stadium Data", ROMX
|
||||
SECTION "Stadium 2 Checksums", ROMX[$7DF8], BANK[$7F]
|
||||
|
||||
IF DEF(_GOLD)
|
||||
IF DEF(_DEBUG)
|
||||
INCBIN "data/stadium/stadium_gold_debug.bin"
|
||||
ELSE
|
||||
INCBIN "data/stadium/stadium_gold.bin"
|
||||
ENDC
|
||||
ELIF DEF(_SILVER)
|
||||
IF DEF(_DEBUG)
|
||||
INCBIN "data/stadium/stadium_silver_debug.bin"
|
||||
ELSE
|
||||
INCBIN "data/stadium/stadium_silver.bin"
|
||||
ENDC
|
||||
ENDC
|
||||
; The end of the ROM is taken up by checksums of the content, apparently used
|
||||
; by Pokémon Stadium 2 due to the checksums' "N64PS3" header. (In Japan,
|
||||
; Pokémon Stadium Gold and Silver was the third Stadium release for N64.)
|
||||
; This SECTION reserves space for those checksums.
|
||||
; If it is removed, also remove the "tools/stadium" command in the Makefile.
|
||||
|
||||
ds $208
|
||||
|
||||
@@ -7,7 +7,8 @@ tools := \
|
||||
lzcomp \
|
||||
png_dimensions \
|
||||
scan_includes \
|
||||
gfx
|
||||
gfx \
|
||||
stadium
|
||||
all: $(tools)
|
||||
@:
|
||||
|
||||
|
||||
155
tools/stadium.c
Normal file
155
tools/stadium.c
Normal file
@@ -0,0 +1,155 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <getopt.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
// The Game Boy cartridge header stores a global checksum at 0x014E-0x014F
|
||||
#define GLOBALOFF 0x014E
|
||||
// "base" data; Crystal-only
|
||||
#define BASESIZE 24
|
||||
// ASCII "N64PS3" header
|
||||
#define N64PS3SIZE 6
|
||||
// N64PS3 + CRC
|
||||
#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
|
||||
|
||||
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};
|
||||
|
||||
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 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 n64ps3[N64PS3SIZE] = {'N', '6', '4', 'P', 'S', '3'};
|
||||
|
||||
static void usage(void) {
|
||||
fprintf(stderr, "Usage: stadium [-h|--help] [-b|--base us|eu|dbg] romfile\n");
|
||||
}
|
||||
|
||||
void parse_args(int argc, char *argv[], Base *b) {
|
||||
struct option long_options[] = {
|
||||
{"base", required_argument, 0, 'b'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{0}
|
||||
};
|
||||
for (int opt = 0; opt != -1;) {
|
||||
switch (opt = getopt_long(argc, argv, "hb:", long_options)) {
|
||||
case 'h':
|
||||
usage();
|
||||
exit(0);
|
||||
break;
|
||||
case 'b':
|
||||
*b = !strcmp(optarg, "us") ? BASE_US :
|
||||
!strcmp(optarg, "eu") ? BASE_EU :
|
||||
!strcmp(optarg, "dbg") ? BASE_DEBUG :
|
||||
BASE_NONE;
|
||||
break;
|
||||
case 0:
|
||||
case -1:
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
exit(1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define SET_U16BE(file, off, v) do { \
|
||||
file[(off) + 0] = (uint8_t)(((v) & 0xFF00) >> 8); \
|
||||
file[(off) + 1] = (uint8_t)(((v) & 0x00FF) >> 0); \
|
||||
} while (0)
|
||||
|
||||
void calculate_checksums(uint8_t *file, int filesize, Base base) {
|
||||
int NUMCHECKS = filesize / CHECKSIZE;
|
||||
int DATASIZE = HEADERSIZE + NUMCHECKS * 2; // 2 bytes per checksum
|
||||
int ORIGIN = filesize - DATASIZE; // Stadium data goes at the end of the file
|
||||
|
||||
// Clear the global checksum
|
||||
SET_U16BE(file, GLOBALOFF, 0);
|
||||
|
||||
// Write the appropriate base data, or none
|
||||
int BASEOFF = ORIGIN - BASESIZE;
|
||||
if (base == BASE_US) {
|
||||
memcpy(file + BASEOFF, us_base, BASESIZE);
|
||||
} else if (base == BASE_EU) {
|
||||
memcpy(file + BASEOFF, eu_base, BASESIZE);
|
||||
} else if (base == BASE_DEBUG) {
|
||||
memcpy(file + BASEOFF, dbg_base, BASESIZE);
|
||||
}
|
||||
|
||||
// Initialize the Stadium data (this should be free space anyway)
|
||||
memset(file + ORIGIN, 0, DATASIZE);
|
||||
memcpy(file + ORIGIN, n64ps3, N64PS3SIZE);
|
||||
|
||||
// Calculate the half-bank checksums
|
||||
for (int i = 0; i < NUMCHECKS; i++) {
|
||||
uint16_t checksum = CRC_INIT;
|
||||
for (int j = 0; j < CHECKSIZE; j++) {
|
||||
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
|
||||
uint16_t crc = CRC_INIT;
|
||||
for (int i = ORIGIN + HEADERSIZE; i < ORIGIN + DATASIZE; i++) {
|
||||
crc = (crc >> 8) ^ crc_table[(crc & 0xFF) ^ file[i]];
|
||||
}
|
||||
SET_U16BE(file, ORIGIN + HEADERSIZE - 2, crc);
|
||||
|
||||
// Calculate the global checksum
|
||||
uint16_t globalsum = 0;
|
||||
for (int i = 0; i < filesize; i++) {
|
||||
globalsum += file[i];
|
||||
}
|
||||
SET_U16BE(file, GLOBALOFF, globalsum);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
Base base = BASE_NONE;
|
||||
parse_args(argc, argv, &base);
|
||||
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
if (argc < 1) {
|
||||
usage();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char *filename = argv[0];
|
||||
int filesize;
|
||||
uint8_t *file = read_u8(filename, &filesize);
|
||||
calculate_checksums(file, filesize, base);
|
||||
write_u8(filename, file, filesize);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user