Revise macros in tools

This commit is contained in:
Rangi 2022-10-12 21:43:00 -04:00
parent ce004be92c
commit 89a888277a
3 changed files with 49 additions and 45 deletions

View File

@ -19,6 +19,8 @@
#error Define USAGE_OPTS before including common.h! #error Define USAGE_OPTS before including common.h!
#endif #endif
#define COUNTOF(...) (sizeof(__VA_ARGS__) / sizeof(*(__VA_ARGS__)))
#define error_exit(...) exit((fprintf(stderr, PROGRAM_NAME ": " __VA_ARGS__), 1)) #define error_exit(...) exit((fprintf(stderr, PROGRAM_NAME ": " __VA_ARGS__), 1))
noreturn void usage_exit(int status) { noreturn void usage_exit(int status) {

View File

@ -165,7 +165,7 @@ int strfind(const char *s, const char *list[], int count) {
return -1; return -1;
} }
#define vstrfind(s, ...) strfind(s, (const char *[]){__VA_ARGS__}, sizeof (const char *[]){__VA_ARGS__} / sizeof(const char *)) #define vstrfind(s, ...) strfind(s, (const char *[]){__VA_ARGS__}, COUNTOF((const char *[]){__VA_ARGS__}))
int parse_arg_value(const char *arg, bool absolute, const struct Symbol *symbols, const char *patch_name) { int parse_arg_value(const char *arg, bool absolute, const struct Symbol *symbols, const char *patch_name) {
// Comparison operators for "ConditionValueB" evaluate to their particular values // Comparison operators for "ConditionValueB" evaluate to their particular values

View File

@ -24,34 +24,36 @@ void parse_args(int argc, char *argv[], bool *european) {
} }
// A matching ROM has 128 banks // A matching ROM has 128 banks
#define NUMBANKS 128 #define NUM_BANKS 128
// ROM banks are 0x4000 bytes // ROM banks are 0x4000 bytes
#define BANKSIZE 0x4000 #define BANK_SIZE 0x4000
// A matching ROM is 2 MB // A matching ROM is 2 MB
#define ROMSIZE (NUMBANKS * BANKSIZE) #define ROM_SIZE (NUM_BANKS * BANK_SIZE)
// The Game Boy cartridge header stores a global checksum at 0x014E-0x014F // The Game Boy cartridge header stores a global checksum at 0x014E-0x014F
#define GLOBALOFF 0x014E #define GLOBAL_OFF 0x014E
// ASCII "base" header (Crystal only) // ASCII "base" header + 2-byte version (Crystal only)
#define BASE10SIZE 6 uint8_t base10[6] = {'b', 'a', 's', 'e', 1, 0};
uint8_t base10[BASE10SIZE] = {'b', 'a', 's', 'e', 1, 0}; // "base" header + version + 2-byte CRC
// "base" + 2-byte version + 2-byte CRC #define BASE_HEADER_SIZE (COUNTOF(base10) + 2)
#define BASEHEADERSIZE (BASE10SIZE + 2) // bit flags per bank indicating if it matches the base ROM's bank
// "base" + 2-byte version + 2-byte CRC + per-bank bit flags #define BASE_DATA_SIZE (NUM_BANKS / 8)
#define BASEDATASIZE (BASEHEADERSIZE + NUMBANKS / 8) // "base" header + version + CRC + bank bit flags
#define BASE_TOTAL_SIZE (BASE_HEADER_SIZE + BASE_DATA_SIZE)
// The base data is stored before the Stadium data // The base data is stored before the Stadium data
#define BASEDATAOFF (N64PS3DATAOFF - BASEDATASIZE) #define BASE_OFF (N64PS3_OFF - BASE_TOTAL_SIZE)
// ASCII "N64PS3" header (Stadium G/S was the third Japanese Stadium release for N64) // ASCII "N64PS3" header (Stadium G/S was the third Japanese Stadium release for N64)
#define N64PS3SIZE 6 uint8_t n64ps3[6] = {'N', '6', '4', 'P', 'S', '3'};
uint8_t n64ps3[N64PS3SIZE] = {'N', '6', '4', 'P', 'S', '3'}; // "N64PS3" header + 2-byte CRC
// "N64PS3" + 2-byte CRC #define N64PS3_HEADER_SIZE (COUNTOF(n64ps3) + 2)
#define N64PS3HEADERSIZE (N64PS3SIZE + 2) // 2-byte checksums per half-bank
// "N64PS3" + 2-byte CRC + per-half-bank 2-byte checksums #define N64PS3_DATA_SIZE (NUM_BANKS * 2 * 2)
#define N64PS3DATASIZE (N64PS3HEADERSIZE + NUMBANKS * 2 * 2) // "N64PS3" header + CRC + half-bank checksums
#define N64PS3_TOTAL_SIZE (N64PS3_HEADER_SIZE + N64PS3_DATA_SIZE)
// The Stadium data is stored at the end of the ROM // The Stadium data is stored at the end of the ROM
#define N64PS3DATAOFF (ROMSIZE - N64PS3DATASIZE) #define N64PS3_OFF (ROM_SIZE - N64PS3_TOTAL_SIZE)
// The CRC polynomial value // The CRC polynomial value
#define CRC_POLY 0xC387 #define CRC_POLY 0xC387
@ -62,13 +64,13 @@ uint8_t n64ps3[N64PS3SIZE] = {'N', '6', '4', 'P', 'S', '3'};
// The CRC lookup table // The CRC lookup table
uint16_t crc_table[256]; uint16_t crc_table[256];
#define SET_U16BE(file, off, v) do { \ #define SET_U16BE(file, v) do { \
file[(off) + 0] = (uint8_t)(((v) & 0xFF00) >> 8); \ (file)[0] = (uint8_t)(((v) & 0xFF00) >> 8); \
file[(off) + 1] = (uint8_t)(((v) & 0x00FF) >> 0); \ (file)[1] = (uint8_t)(((v) & 0x00FF) >> 0); \
} while (0) } while (0)
// CRCs of every bank in the base ROM, crystal_base0.bin // CRCs of every bank in the base ROM, crystal_base0.bin
uint16_t base0_crcs[NUMBANKS] = { uint16_t base0_crcs[NUM_BANKS] = {
0x9650, 0x8039, 0x2D8F, 0xD75A, 0xAC50, 0x5D55, 0xE94B, 0x9886, 0x9650, 0x8039, 0x2D8F, 0xD75A, 0xAC50, 0x5D55, 0xE94B, 0x9886,
0x2A46, 0xB5AC, 0xC3D3, 0x79C4, 0xCE55, 0xA95E, 0xEF78, 0x9B50, 0x2A46, 0xB5AC, 0xC3D3, 0x79C4, 0xCE55, 0xA95E, 0xEF78, 0x9B50,
0x82BA, 0x8716, 0x5895, 0xAD33, 0x4EF0, 0xE434, 0xC521, 0xBFB1, 0x82BA, 0x8716, 0x5895, 0xAD33, 0x4EF0, 0xE434, 0xC521, 0xBFB1,
@ -88,7 +90,7 @@ uint16_t base0_crcs[NUMBANKS] = {
}; };
// CRCs of every bank in the European base ROM, crystal_base1.bin // CRCs of every bank in the European base ROM, crystal_base1.bin
uint16_t base1_crcs[NUMBANKS] = { uint16_t base1_crcs[NUM_BANKS] = {
0x5416, 0xFD37, 0xC4A4, 0xBC37, 0x9458, 0xB489, 0xE94B, 0x9906, 0x5416, 0xFD37, 0xC4A4, 0xBC37, 0x9458, 0xB489, 0xE94B, 0x9906,
0x2A46, 0xDEA9, 0x17F4, 0xF447, 0xCE55, 0xD843, 0xC5B2, 0xAE13, 0x2A46, 0xDEA9, 0x17F4, 0xF447, 0xCE55, 0xD843, 0xC5B2, 0xAE13,
0x4E91, 0x3984, 0xD984, 0xD02F, 0x77B8, 0x4D8D, 0x1F8C, 0x7185, 0x4E91, 0x3984, 0xD984, 0xD02F, 0x77B8, 0x4D8D, 0x1F8C, 0x7185,
@ -123,7 +125,7 @@ uint16_t calculate_crc(uint16_t crc, uint8_t *file, size_t size) {
void calculate_checksums(uint8_t *file, bool european) { void calculate_checksums(uint8_t *file, bool european) {
// Initialize the CRC table // Initialize the CRC table
for (uint16_t i = 0; i < sizeof(crc_table) / sizeof(*crc_table); i++) { for (uint16_t i = 0; i < COUNTOF(crc_table); i++) {
uint16_t rem = 0; uint16_t rem = 0;
for (uint16_t y = 0, c = i; y < 8; y++, c >>= 1) { for (uint16_t y = 0, c = i; y < 8; y++, c >>= 1) {
rem = (rem >> 1) ^ ((rem ^ c) & 1 ? CRC_POLY : 0); rem = (rem >> 1) ^ ((rem ^ c) & 1 ? CRC_POLY : 0);
@ -132,52 +134,52 @@ void calculate_checksums(uint8_t *file, bool european) {
} }
// Clear the global checksum // Clear the global checksum
SET_U16BE(file, GLOBALOFF, 0); SET_U16BE(file + GLOBAL_OFF, 0);
// Initialize the base data (this should be free space anyway) // Initialize the base data (this should be free space anyway)
memset(file + BASEDATAOFF, 0, BASEDATASIZE); memset(file + BASE_OFF, 0, BASE_TOTAL_SIZE);
memcpy(file + BASEDATAOFF, base10, BASE10SIZE); memcpy(file + BASE_OFF, base10, COUNTOF(base10));
// Use the appropriate base CRCs // Use the appropriate base CRCs
uint16_t *base_crcs = base0_crcs; uint16_t *base_crcs = base0_crcs;
if (european) { if (european) {
base_crcs = base1_crcs; base_crcs = base1_crcs;
file[BASEDATAOFF + BASE10SIZE - 1] = 1; file[BASE_OFF + COUNTOF(base10) - 1] = 1;
} }
// Calculate the base data bits using bank CRCs // Calculate the base data bits using bank CRCs
// Bits indicate if the bank CRC matches the base one // Bits indicate if the bank CRC matches the base one
for (size_t i = 0; i < BASEDATASIZE - BASEHEADERSIZE; i++) { for (size_t i = 0; i < BASE_DATA_SIZE; i++) {
uint8_t bits = 0; uint8_t bits = 0;
for (size_t j = 0; j < 8; j++) { for (size_t j = 0; j < 8; j++) {
size_t bank = i * 8 + j; size_t bank = i * 8 + j;
uint16_t crc = calculate_crc(CRC_INIT, file + bank * BANKSIZE, BANKSIZE); uint16_t crc = calculate_crc(CRC_INIT, file + bank * BANK_SIZE, BANK_SIZE);
bits |= (crc == base_crcs[bank]) << j; bits |= (crc == base_crcs[bank]) << j;
} }
file[BASEDATAOFF + BASEHEADERSIZE + i] = bits; file[BASE_OFF + BASE_HEADER_SIZE + i] = bits;
} }
// Calculate the CRC of the base data // Calculate the CRC of the base data
uint16_t crc = calculate_crc(CRC_INIT_BASE, file + BASEDATAOFF, BASEDATASIZE); uint16_t crc = calculate_crc(CRC_INIT_BASE, file + BASE_OFF, BASE_TOTAL_SIZE);
SET_U16BE(file, BASEDATAOFF + BASEHEADERSIZE - 2, crc); SET_U16BE(file + BASE_OFF + BASE_HEADER_SIZE - 2, crc);
// Initialize the Stadium data (this should be free space anyway) // Initialize the Stadium data (this should be free space anyway)
memset(file + N64PS3DATAOFF, 0, N64PS3DATASIZE); memset(file + N64PS3_OFF, 0, N64PS3_TOTAL_SIZE);
memcpy(file + N64PS3DATAOFF, n64ps3, N64PS3SIZE); memcpy(file + N64PS3_OFF, n64ps3, COUNTOF(n64ps3));
// Calculate the half-bank checksums // Calculate the half-bank checksums
for (size_t i = 0; i < NUMBANKS * 2; i++) { for (size_t i = 0; i < NUM_BANKS * 2; i++) {
uint16_t checksum = calculate_checksum(CRC_INIT, file + i * BANKSIZE / 2, BANKSIZE / 2); uint16_t checksum = calculate_checksum(CRC_INIT, file + i * BANK_SIZE / 2, BANK_SIZE / 2);
SET_U16BE(file, N64PS3DATAOFF + N64PS3HEADERSIZE + i * 2, checksum); SET_U16BE(file + N64PS3_OFF + N64PS3_HEADER_SIZE + i * 2, checksum);
} }
// Calculate the CRC of the half-bank checksums // Calculate the CRC of the half-bank checksums
crc = calculate_crc(CRC_INIT, file + N64PS3DATAOFF + N64PS3HEADERSIZE, N64PS3DATASIZE - N64PS3HEADERSIZE); crc = calculate_crc(CRC_INIT, file + N64PS3_OFF + N64PS3_HEADER_SIZE, N64PS3_DATA_SIZE);
SET_U16BE(file, N64PS3DATAOFF + N64PS3HEADERSIZE - 2, crc); SET_U16BE(file + N64PS3_OFF + N64PS3_HEADER_SIZE - 2, crc);
// Calculate the global checksum // Calculate the global checksum
uint16_t globalsum = calculate_checksum(0, file, ROMSIZE); uint16_t globalsum = calculate_checksum(0, file, ROM_SIZE);
SET_U16BE(file, GLOBALOFF, globalsum); SET_U16BE(file + GLOBAL_OFF, globalsum);
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
@ -193,7 +195,7 @@ int main(int argc, char *argv[]) {
char *filename = argv[0]; char *filename = argv[0];
long filesize; long filesize;
uint8_t *file = read_u8(filename, &filesize); uint8_t *file = read_u8(filename, &filesize);
if (filesize == ROMSIZE) { if (filesize == ROM_SIZE) {
calculate_checksums(file, european); calculate_checksums(file, european);
} }
write_u8(filename, file, filesize); write_u8(filename, file, filesize);