From ac58a8736a9d057f827dd72d7d1bbc84ad74c231 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 13 Oct 2017 18:47:19 +0100 Subject: [PATCH] #18 Aleck64 games now boot get_cic() refactored to include Aleck64 roms moved CIC functions to dedicated module --- inc/cic.h | 12 ++++++ inc/rom.h | 10 +---- inc/sys.h | 2 - src/cic.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 5 +-- src/rom.c | 25 +---------- src/sys.c | 43 ------------------- src/utils.c | 8 +--- 8 files changed, 133 insertions(+), 89 deletions(-) create mode 100644 inc/cic.h create mode 100644 src/cic.c diff --git a/inc/cic.h b/inc/cic.h new file mode 100644 index 0000000..c8311df --- /dev/null +++ b/inc/cic.h @@ -0,0 +1,12 @@ + +#define CIC_6101 1 +#define CIC_6102 2 +#define CIC_6103 3 +#define CIC_5101 4 //aleck64 +//#define CIC_6104 6104 //Unused in any game so used for Aleck64 instead +#define CIC_6105 5 +#define CIC_6106 6 +#define CIC_5167 7 //64dd conv + +int get_cic(unsigned char *rom_data); + diff --git a/inc/rom.h b/inc/rom.h index 5da9d16..75c521a 100644 --- a/inc/rom.h +++ b/inc/rom.h @@ -148,14 +148,7 @@ #define FRAM_STATUS_REG 0xA8000000 #define FRAM_COMMAND_REG 0xA8010000 -#define CIC_6101 1 -#define CIC_6102 2 -#define CIC_6103 3 -#define CIC_5101 4 //aleck64 -//#define CIC_6104 6104 //Unused in any game so used for Aleck64 instead -#define CIC_6105 5 -#define CIC_6106 6 -#define CIC_5167 7 //64dd conv + //void romFill(...); void pif_boot(); @@ -164,6 +157,5 @@ int is_valid_rom(unsigned char *buffer); void swap_header(unsigned char* header, int loadlength); u8 getCicType(u8 bios_cic); -int get_cic(unsigned char *buffer); #endif diff --git a/inc/sys.h b/inc/sys.h index 923391f..da80c0b 100644 --- a/inc/sys.h +++ b/inc/sys.h @@ -19,8 +19,6 @@ void dma_write_sram(void* src, u32 offset, u32 size); void dma_read_sram(void *dest, u32 offset, u32 size); u8 getSaveType(); -unsigned int CRC_Calculate(unsigned int crc, unsigned char* buf, unsigned int len); - typedef struct SP_regs_s { u32 mem_addr; diff --git a/src/cic.c b/src/cic.c new file mode 100644 index 0000000..2186a3c --- /dev/null +++ b/src/cic.c @@ -0,0 +1,117 @@ +// +// si/cic.c: PIF CIC security/lock out algorithms. +// +// CEN64: Cycle-Accurate Nintendo 64 Emulator. +// Copyright (C) 2015, Tyler J. Stachecki. +// +// This file is subject to the terms and conditions defined in +// 'LICENSE', which is part of this source code package. +// + +//#include "common.h" +//#include "si/cic.h" +#include +#include +#include "cic.h" + + +// CIC seeds and status bits passed from PIF to IPL through PIF RAM +// Bits | Description +// 00080000 | ROM type (0 = Game Pack, 1 = DD) +// 00040000 | Version +// 00020000 | Reset Type (0 = cold reset, 1 = NMI) +// 0000FF00 | CIC IPL3 seed value +// 000000FF | CIC IPL2 seed value +// #define CIC_SEED_NUS_5101 0x0000AC00U +// #define CIC_SEED_NUS_6101 0x00043F3FU +// #define CIC_SEED_NUS_6102 0x00003F3FU +// #define CIC_SEED_NUS_6103 0x0000783FU +// #define CIC_SEED_NUS_6105 0x0000913FU +// #define CIC_SEED_NUS_6106 0x0000853FU +// #define CIC_SEED_NUS_8303 0x0000DD00U + +#define CRC_NUS_5101 0x587BD543U +#define CRC_NUS_6101 0x6170A4A1U +#define CRC_NUS_7102 0x009E9EA3U +#define CRC_NUS_6102 0x90BB6CB5U +#define CRC_NUS_6103 0x0B050EE0U +#define CRC_NUS_6105 0x98BC2C86U +#define CRC_NUS_6106 0xACC8580AU +#define CRC_NUS_8303 0x0E018159U + +static uint32_t si_crc32(const uint8_t *data, size_t size); + +// Determines the CIC seed for a cart, given the ROM data. +//int get_cic_seed(const uint8_t *rom_data, uint32_t *cic_seed) { +int get_cic(unsigned char *rom_data) { + uint32_t crc = si_crc32(rom_data + 0x40, 0x1000 - 0x40); + uint32_t aleck64crc = si_crc32(rom_data + 0x40, 0xC00 - 0x40); + + if (aleck64crc == CRC_NUS_5101) + return 4;//*cic_seed = CIC_SEED_NUS_5101; + else + { + switch (crc) { + case CRC_NUS_6101: + case CRC_NUS_7102: + //*cic_seed = CIC_SEED_NUS_6101; + return 1; + break; + + case CRC_NUS_6102: + //*cic_seed = CIC_SEED_NUS_6102; + return 2; + break; + + case CRC_NUS_6103: + //*cic_seed = CIC_SEED_NUS_6103; + return 3; + break; + + case CRC_NUS_6105: + //*cic_seed = CIC_SEED_NUS_6105; + return 5; + break; + + case CRC_NUS_6106: + //*cic_seed = CIC_SEED_NUS_6106; + return 6; + break; + + //case CRC_NUS_8303: //not sure if this is necessary as we are using cart conversions + //*cic_seed = CIC_SEED_NUS_8303; + //return 7; + //break; + + default: + break; + } + } + return 2; +} + +uint32_t si_crc32(const uint8_t *data, size_t size) { + uint32_t table[256]; + unsigned n, k; + uint32_t c; + + for (n = 0; n < 256; n++) { + c = (uint32_t) n; + + for (k = 0; k < 8; k++) { + if (c & 1) + c = 0xEDB88320L ^ (c >> 1); + else + c = c >> 1; + } + + table[n] = c; + } + + c = 0L ^ 0xFFFFFFFF; + + for (n = 0; n < size; n++) + c = table[(c ^ data[n]) & 0xFF] ^ (c >> 8); + + return c ^ 0xFFFFFFFF; +} diff --git a/src/main.c b/src/main.c index 9554b26..6f2f771 100644 --- a/src/main.c +++ b/src/main.c @@ -47,6 +47,7 @@ #include "rom.h" #include "memorypak.h" #include "menu.h" +#include "cic.h" #ifdef USE_TRUETYPE #define STB_TRUETYPE_IMPLEMENTATION @@ -2331,16 +2332,12 @@ void alterRomConfig(int type, int mode) //down if (rom_config[1] < max_cic) rom_config[1]++; - if (rom_config[1] == 3) - rom_config[1]++; } else if (mode == 2) { //up if (rom_config[1] > min_cic) rom_config[1]--; - if (rom_config[1] == 3) - rom_config[1]--; } //end cic break; diff --git a/src/rom.c b/src/rom.c index 2cf30c4..6f1e56c 100644 --- a/src/rom.c +++ b/src/rom.c @@ -8,6 +8,7 @@ #include "everdrive.h" #include "sys.h" #include "rom.h" +#include "cic.h" void pif_boot() { @@ -76,27 +77,3 @@ u8 getCicType(u8 bios_cic) { return cic_chip; } - - - -int get_cic(unsigned char *buffer) { - unsigned int crc; - // figure out the CIC - crc = CRC_Calculate(0, buffer, 1000); - switch(crc) { - case 0x303faac9: - case 0xf0da3d50: - return 1; - case 0xf3106101: - return 2; - case 0xe7cd9d51: - return 3; - case 0x7ae65c9: - return 5; - case 0x86015f8f: - return 6; - //case 0x86015f8f: TODO: need to find the correct number - //return 5167; 64dd - } - return 2; -} \ No newline at end of file diff --git a/src/sys.c b/src/sys.c index 07dcc40..1067bcf 100644 --- a/src/sys.c +++ b/src/sys.c @@ -114,50 +114,7 @@ void dma_write_sram(void* src, u32 offset, u32 size) { } -//TODO: look at https://github.com/mamedev/mame/blob/master/3rdparty/zlib/crc32.c -#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8); -#define DO2(buf) DO1(buf); DO1(buf); -#define DO4(buf) DO2(buf); DO2(buf); -#define DO8(buf) DO4(buf); DO4(buf); -unsigned int CRC_Calculate(unsigned int crc, unsigned char* buf, unsigned int len) { - static unsigned int crc_table[256]; - static int make_crc_table = 1; - - if (make_crc_table) { - unsigned int c, n; - int k; - unsigned int poly; - const unsigned char p[] = {0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26}; - - /* make exclusive-or pattern from polynomial (0xedb88320L) */ - poly = 0L; - for (n = 0; n < sizeof (p) / sizeof (unsigned char); n++) - poly |= 1L << (31 - p[n]); - - for (n = 0; n < 256; n++) { - c = n; - for (k = 0; k < 8; k++) - c = c & 1 ? poly ^ (c >> 1) : c >> 1; - crc_table[n] = c; - } - make_crc_table = 0; - } - - if (buf == (void*) 0) return 0L; - - crc = crc ^ 0xffffffffL; - while (len >= 8) { - DO8(buf); - len -= 8; - } - if (len) - do { - DO1(buf); - } while (--len); - - return crc ^ 0xffffffffL; -} diff --git a/src/utils.c b/src/utils.c index eb23feb..d6b2d0f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -13,6 +13,7 @@ #include "utils.h" #include "sram.h" #include "rom.h" +#include "cic.h" extern short int gCheats; /* 0 = off, 1 = select, 2 = all */ @@ -434,13 +435,6 @@ void restoreTiming(void) { } -//#define CIC_6101 1 -//#define CIC_6102 2 -//#define CIC_6103 3 -//#define CIC_6104 4 -//#define CIC_6105 5 -//#define CIC_6106 6 - #define ROM ((vu32 *)0xB0000000) #define EXE_START 0xB0001000 #define EXE_SIZE 0x00100004