1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - rom.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2008 Tillin9 *
5 * Copyright (C) 2002 Hacktarux *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
21 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28 #define M64P_CORE_PROTOTYPES 1
29 #include "api/m64p_types.h"
30 #include "api/callbacks.h"
31 #include "api/config.h"
32 #include "api/m64p_config.h"
39 #include "memory/memory.h"
40 #include "osal/preproc.h"
45 #define CHUNKSIZE 1024*128 /* Read files 128KB at a time. */
47 static romdatabase_entry* ini_search_by_md5(md5_byte_t* md5);
49 static _romdatabase g_romdatabase;
51 /* Global loaded rom memory space. */
52 unsigned char* rom = NULL;
53 /* Global loaded rom size. */
56 unsigned char isGoldeneyeRom = 0;
58 extern int count_per_op;
60 m64p_rom_header ROM_HEADER;
61 rom_params ROM_PARAMS;
62 m64p_rom_settings ROM_SETTINGS;
64 static m64p_system_type rom_country_code_to_system_type(unsigned short country_code);
65 static int rom_system_type_to_ai_dac_rate(m64p_system_type system_type);
66 static int rom_system_type_to_vi_limit(m64p_system_type system_type);
68 /* Tests if a file is a valid N64 rom by checking the first 4 bytes. */
69 static int is_valid_rom(const unsigned char *buffer)
71 /* Test if rom is a native .z64 image with header 0x80371240. [ABCD] */
72 if((buffer[0]==0x80)&&(buffer[1]==0x37)&&(buffer[2]==0x12)&&(buffer[3]==0x40))
74 /* Test if rom is a byteswapped .v64 image with header 0x37804012. [BADC] */
75 else if((buffer[0]==0x37)&&(buffer[1]==0x80)&&(buffer[2]==0x40)&&(buffer[3]==0x12))
77 /* Test if rom is a wordswapped .n64 image with header 0x40123780. [DCBA] */
78 else if((buffer[0]==0x40)&&(buffer[1]==0x12)&&(buffer[2]==0x37)&&(buffer[3]==0x80))
84 /* If rom is a .v64 or .n64 image, byteswap or wordswap loadlength amount of
85 * rom data to native .z64 before forwarding. Makes sure that data extraction
86 * and MD5ing routines always deal with a .z64 image.
88 static void swap_rom(unsigned char* localrom, unsigned char* imagetype, int loadlength)
93 /* Btyeswap if .v64 image. */
96 *imagetype = V64IMAGE;
97 for (i = 0; i < loadlength; i+=2)
100 localrom[i]=localrom[i+1];
104 /* Wordswap if .n64 image. */
105 else if(localrom[0]==0x40)
107 *imagetype = N64IMAGE;
108 for (i = 0; i < loadlength; i+=4)
111 localrom[i]=localrom[i+3];
114 localrom[i+1]=localrom[i+2];
119 *imagetype = Z64IMAGE;
122 m64p_error open_rom(const unsigned char* romimage, unsigned int size)
125 md5_byte_t digest[16];
126 romdatabase_entry* entry;
128 unsigned char imagetype;
131 /* check input requirements */
134 DebugMessage(M64MSG_ERROR, "open_rom(): previous ROM image was not freed");
135 return M64ERR_INTERNAL;
137 if (romimage == NULL || !is_valid_rom(romimage))
139 DebugMessage(M64MSG_ERROR, "open_rom(): not a valid ROM image");
140 return M64ERR_INPUT_INVALID;
143 /* Clear Byte-swapped flag, since ROM is now deleted. */
144 g_MemHasBeenBSwapped = 0;
145 /* allocate new buffer for ROM and copy into this buffer */
147 rom = (unsigned char *) malloc(size);
149 return M64ERR_NO_MEMORY;
150 memcpy(rom, romimage, size);
151 swap_rom(rom, &imagetype, rom_size);
153 memcpy(&ROM_HEADER, rom, sizeof(m64p_rom_header));
155 /* Calculate MD5 hash */
157 md5_append(&state, (const md5_byte_t*)rom, rom_size);
158 md5_finish(&state, digest);
159 for ( i = 0; i < 16; ++i )
160 sprintf(buffer+i*2, "%02X", digest[i]);
162 strcpy(ROM_SETTINGS.MD5, buffer);
164 /* add some useful properties to ROM_PARAMS */
165 ROM_PARAMS.systemtype = rom_country_code_to_system_type(ROM_HEADER.Country_code);
166 ROM_PARAMS.vilimit = rom_system_type_to_vi_limit(ROM_PARAMS.systemtype);
167 ROM_PARAMS.aidacrate = rom_system_type_to_ai_dac_rate(ROM_PARAMS.systemtype);
169 memcpy(ROM_PARAMS.headername, ROM_HEADER.Name, 20);
170 ROM_PARAMS.headername[20] = '\0';
171 trim(ROM_PARAMS.headername); /* Remove trailing whitespace from ROM name. */
173 /* Look up this ROM in the .ini file and fill in goodname, etc */
174 if ((entry=ini_search_by_md5(digest)) != NULL ||
175 (entry=ini_search_by_crc(sl(ROM_HEADER.CRC1),sl(ROM_HEADER.CRC2))) != NULL)
177 strncpy(ROM_SETTINGS.goodname, entry->goodname, 255);
178 ROM_SETTINGS.goodname[255] = '\0';
179 ROM_SETTINGS.savetype = entry->savetype;
180 ROM_SETTINGS.status = entry->status;
181 ROM_SETTINGS.players = entry->players;
182 ROM_SETTINGS.rumble = entry->rumble;
183 delay_si = entry->delay_si;
184 count_per_op = entry->count_per_op;
188 strcpy(ROM_SETTINGS.goodname, ROM_PARAMS.headername);
189 strcat(ROM_SETTINGS.goodname, " (unknown rom)");
190 ROM_SETTINGS.savetype = NONE;
191 ROM_SETTINGS.status = 0;
192 ROM_SETTINGS.players = 0;
193 ROM_SETTINGS.rumble = 0;
198 /* print out a bunch of info about the ROM */
199 DebugMessage(M64MSG_INFO, "Goodname: %s", ROM_SETTINGS.goodname);
200 DebugMessage(M64MSG_INFO, "Name: %s", ROM_HEADER.Name);
201 imagestring(imagetype, buffer);
202 DebugMessage(M64MSG_INFO, "MD5: %s", ROM_SETTINGS.MD5);
203 DebugMessage(M64MSG_INFO, "CRC: %x %x", sl(ROM_HEADER.CRC1), sl(ROM_HEADER.CRC2));
204 DebugMessage(M64MSG_INFO, "Imagetype: %s", buffer);
205 DebugMessage(M64MSG_INFO, "Rom size: %d bytes (or %d Mb or %d Megabits)", rom_size, rom_size/1024/1024, rom_size/1024/1024*8);
206 DebugMessage(M64MSG_VERBOSE, "ClockRate = %x", sl(ROM_HEADER.ClockRate));
207 DebugMessage(M64MSG_INFO, "Version: %x", sl(ROM_HEADER.Release));
208 if(sl(ROM_HEADER.Manufacturer_ID) == 'N')
209 DebugMessage(M64MSG_INFO, "Manufacturer: Nintendo");
211 DebugMessage(M64MSG_INFO, "Manufacturer: %x", sl(ROM_HEADER.Manufacturer_ID));
212 DebugMessage(M64MSG_VERBOSE, "Cartridge_ID: %x", ROM_HEADER.Cartridge_ID);
213 countrycodestring(ROM_HEADER.Country_code, buffer);
214 DebugMessage(M64MSG_INFO, "Country: %s", buffer);
215 DebugMessage(M64MSG_VERBOSE, "PC = %x", sl((unsigned int)ROM_HEADER.PC));
216 DebugMessage(M64MSG_VERBOSE, "Save type: %d", ROM_SETTINGS.savetype);
217 if (delay_si>=0) DebugMessage(M64MSG_INFO, "Delay SI: %d", delay_si);
218 if (count_per_op>=0) DebugMessage(M64MSG_INFO, "Count Per OP: %d", count_per_op);
220 //Prepare Hack for GOLDENEYE
222 if(strcmp(ROM_PARAMS.headername, "GOLDENEYE") == 0)
225 return M64ERR_SUCCESS;
228 m64p_error close_rom(void)
231 return M64ERR_INVALID_STATE;
236 /* Clear Byte-swapped flag, since ROM is now deleted. */
237 g_MemHasBeenBSwapped = 0;
238 DebugMessage(M64MSG_STATUS, "Rom closed.");
240 return M64ERR_SUCCESS;
243 /********************************************************************************************/
244 /* ROM utility functions */
246 // Get the system type associated to a ROM country code.
247 static m64p_system_type rom_country_code_to_system_type(unsigned short country_code)
249 switch (country_code & 0xFF)
267 default: // Fallback for unknown codes
272 // Get the VI (vertical interrupt) limit associated to a ROM system type.
273 static int rom_system_type_to_vi_limit(m64p_system_type system_type)
287 static int rom_system_type_to_ai_dac_rate(m64p_system_type system_type)
301 /********************************************************************************************/
302 /* INI Rom database functions */
304 void romdatabase_open(void)
308 romdatabase_search* search = NULL;
309 romdatabase_search** next_search;
311 int counter, value, lineno;
313 const char *pathname = ConfigGetSharedDataFilepath("mupen64plus.ini");
315 if(g_romdatabase.have_database)
318 /* Open romdatabase. */
319 if (pathname == NULL || (fPtr = fopen(pathname, "rb")) == NULL)
321 DebugMessage(M64MSG_ERROR, "Unable to open rom database file '%s'.", pathname);
325 g_romdatabase.have_database = 1;
327 /* Clear premade indices. */
328 for(counter = 0; counter < 255; ++counter)
329 g_romdatabase.crc_lists[counter] = NULL;
330 for(counter = 0; counter < 255; ++counter)
331 g_romdatabase.md5_lists[counter] = NULL;
332 g_romdatabase.list = NULL;
334 next_search = &g_romdatabase.list;
336 /* Parse ROM database file */
337 for (lineno = 1; fgets(buffer, 255, fPtr) != NULL; lineno++)
340 ini_line l = ini_parse_line(&line);
346 if (!parse_hex(l.name, md5, 16))
348 DebugMessage(M64MSG_WARNING, "ROM Database: Invalid MD5 on line %i", lineno);
353 *next_search = (romdatabase_search*)malloc(sizeof(romdatabase_search));
354 search = *next_search;
355 next_search = &search->next_entry;
357 search->entry.goodname = NULL;
358 memcpy(search->entry.md5, md5, 16);
359 search->entry.refmd5 = NULL;
360 search->entry.crc1 = 0;
361 search->entry.crc2 = 0;
362 search->entry.status = 0; /* Set default to 0 stars. */
363 search->entry.savetype = DEFAULT;
364 search->entry.players = DEFAULT;
365 search->entry.rumble = DEFAULT;
367 search->entry.delay_si=-1;
368 search->entry.count_per_op=-1;
370 search->next_entry = NULL;
371 search->next_crc = NULL;
372 /* Index MD5s by first 8 bits. */
373 index = search->entry.md5[0];
374 search->next_md5 = g_romdatabase.md5_lists[index];
375 g_romdatabase.md5_lists[index] = search;
380 // This happens if there's stray properties before any section,
381 // or if some error happened on INI_SECTION (e.g. parsing).
384 DebugMessage(M64MSG_WARNING, "ROM Database: Ignoring property on line %i", lineno);
387 if(!strcmp(l.name, "GoodName"))
389 search->entry.goodname = strdup(l.value);
391 else if(!strcmp(l.name, "CRC"))
393 char garbage_sweeper;
394 if (sscanf(l.value, "%X %X%c", &search->entry.crc1,
395 &search->entry.crc2, &garbage_sweeper) == 2)
397 /* Index CRCs by first 8 bits. */
398 index = search->entry.crc1 >> 24;
399 search->next_crc = g_romdatabase.crc_lists[index];
400 g_romdatabase.crc_lists[index] = search;
404 search->entry.crc1 = search->entry.crc2 = 0;
405 DebugMessage(M64MSG_WARNING, "ROM Database: Invalid CRC on line %i", lineno);
408 else if(!strcmp(l.name, "RefMD5"))
411 if (parse_hex(l.value, md5, 16))
413 search->entry.refmd5 = (md5_byte_t*)malloc(16*sizeof(md5_byte_t));
414 memcpy(search->entry.refmd5, md5, 16);
417 DebugMessage(M64MSG_WARNING, "ROM Database: Invalid RefMD5 on line %i", lineno);
419 else if(!strcmp(l.name, "SaveType"))
421 if(!strcmp(l.value, "Eeprom 4KB"))
422 search->entry.savetype = EEPROM_4KB;
423 else if(!strcmp(l.value, "Eeprom 16KB"))
424 search->entry.savetype = EEPROM_16KB;
425 else if(!strcmp(l.value, "SRAM"))
426 search->entry.savetype = SRAM;
427 else if(!strcmp(l.value, "Flash RAM"))
428 search->entry.savetype = FLASH_RAM;
429 else if(!strcmp(l.value, "Controller Pack"))
430 search->entry.savetype = CONTROLLER_PACK;
431 else if(!strcmp(l.value, "None"))
432 search->entry.savetype = NONE;
434 DebugMessage(M64MSG_WARNING, "ROM Database: Invalid save type on line %i", lineno);
436 else if(!strcmp(l.name, "Status"))
438 if (string_to_int(l.value, &value) && value >= 0 && value < 6)
439 search->entry.status = value;
441 DebugMessage(M64MSG_WARNING, "ROM Database: Invalid status on line %i", lineno);
443 else if(!strcmp(l.name, "Players"))
445 if (string_to_int(l.value, &value) && value >= 0 && value < 8)
446 search->entry.players = value;
448 DebugMessage(M64MSG_WARNING, "ROM Database: Invalid player count on line %i", lineno);
450 else if(!strcmp(l.name, "Rumble"))
452 if(!strcmp(l.value, "Yes"))
453 search->entry.rumble = 1;
454 else if(!strcmp(l.value, "No"))
455 search->entry.rumble = 0;
457 DebugMessage(M64MSG_WARNING, "ROM Database: Invalid rumble string on line %i", lineno);
459 else if(!strcmp(l.name, "DelaySI"))
461 if(!strcmp(l.value, "True"))
462 search->entry.delay_si = 1;
463 else if(!strcmp(l.value, "False"))
464 search->entry.delay_si = 0;
466 DebugMessage(M64MSG_WARNING, "ROM Database: Invalid DelaySI string on line %i", lineno);
468 else if(!strcmp(l.name, "CountPerOp"))
470 if (string_to_int(l.value, &value) && value >= 0 && value < 8)
471 search->entry.count_per_op = value;
473 DebugMessage(M64MSG_WARNING, "ROM Database: Invalid CountPerOp on line %i", lineno);
477 DebugMessage(M64MSG_WARNING, "ROM Database: Unknown property on line %i", lineno);
487 /* Resolve RefMD5 references */
488 for (search = g_romdatabase.list; search != NULL; search = search->next_entry)
490 if (search->entry.refmd5 != NULL)
492 romdatabase_entry *ref = ini_search_by_md5(search->entry.refmd5);
495 if(ref->savetype!=DEFAULT)
496 search->entry.savetype = ref->savetype;
498 search->entry.status = ref->status;
499 if(ref->players!=DEFAULT)
500 search->entry.players = ref->players;
501 if(ref->rumble!=DEFAULT)
502 search->entry.rumble = ref->rumble;
505 DebugMessage(M64MSG_WARNING, "ROM Database: Error solving RefMD5s");
510 void romdatabase_close(void)
512 if (!g_romdatabase.have_database)
515 while (g_romdatabase.list != NULL)
517 romdatabase_search* search = g_romdatabase.list->next_entry;
518 if(g_romdatabase.list->entry.goodname)
519 free(g_romdatabase.list->entry.goodname);
520 if(g_romdatabase.list->entry.refmd5)
521 free(g_romdatabase.list->entry.refmd5);
522 free(g_romdatabase.list);
523 g_romdatabase.list = search;
527 static romdatabase_entry* ini_search_by_md5(md5_byte_t* md5)
529 romdatabase_search* search;
531 if(!g_romdatabase.have_database)
534 search = g_romdatabase.md5_lists[md5[0]];
536 while (search != NULL && memcmp(search->entry.md5, md5, 16) != 0)
537 search = search->next_md5;
542 return &(search->entry);
545 romdatabase_entry* ini_search_by_crc(unsigned int crc1, unsigned int crc2)
547 romdatabase_search* search;
549 if(!g_romdatabase.have_database)
552 search = g_romdatabase.crc_lists[((crc1 >> 24) & 0xff)];
554 while (search != NULL && search->entry.crc1 != crc1 && search->entry.crc2 != crc2)
555 search = search->next_crc;
560 return &(search->entry);