X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=Pico%2FCart.c;h=a0d38a0bd91ae2a6a569ea1a7e1c9619856b81c6;hb=0b35350da642c6de77de35a226e3692f1841d6e4;hp=6f97a4231f85b9f053396277a47312e903b2414c;hpb=ff6b7429bb03e37e2d44eabcd0d86bd5713d2fc1;p=picodrive.git diff --git a/Pico/Cart.c b/Pico/Cart.c index 6f97a42..a0d38a0 100644 --- a/Pico/Cart.c +++ b/Pico/Cart.c @@ -15,9 +15,13 @@ static char *rom_exts[] = { "bin", "gen", "smd", "iso" }; +void (*PicoCartUnloadHook)(void) = NULL; + void (*PicoCartLoadProgressCB)(int percent) = NULL; void (*PicoCDLoadProgressCB)(int percent) = NULL; // handled in Pico/cd/cd_file.c +static void PicoCartDetect(void); + /* cso struct */ typedef struct _cso_struct { @@ -133,6 +137,9 @@ zip_failed: if (f == NULL) goto cso_failed; + /* we use our own buffering */ + setvbuf(f, NULL, _IONBF, 0); + cso = malloc(sizeof(*cso)); if (cso == NULL) goto cso_failed; @@ -199,6 +206,7 @@ cso_failed: file->size = ftell(f); file->type = PMT_UNCOMPRESSED; fseek(f, 0, SEEK_SET); + return file; } @@ -400,7 +408,6 @@ static int DecodeSmd(unsigned char *data,int len) static unsigned char *cd_realloc(void *old, int filesize) { unsigned char *rom; - dprintf("sizeof(mcd_state): %i", sizeof(mcd_state)); rom=realloc(old, sizeof(mcd_state)); if (rom) memset(rom+0x20000, 0, sizeof(mcd_state)-0x20000); return rom; @@ -411,14 +418,13 @@ static unsigned char *PicoCartAlloc(int filesize) int alloc_size; unsigned char *rom; - if (PicoMCD & 1) return cd_realloc(NULL, filesize); + if (PicoAHW & PAHW_MCD) return cd_realloc(NULL, filesize); alloc_size=filesize+0x7ffff; if((filesize&0x3fff)==0x200) alloc_size-=0x200; alloc_size&=~0x7ffff; // use alloc size of multiples of 512K, so that memhandlers could be set up more efficiently if((filesize&0x3fff)==0x200) alloc_size+=0x200; else if(alloc_size-filesize < 4) alloc_size+=4; // padding for out-of-bound exec protection - //dprintf("alloc_size: %x\n", alloc_size); // Allocate space for the rom plus padding rom=(unsigned char *)malloc(alloc_size); @@ -468,13 +474,17 @@ int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize) } // maybe we are loading MegaCD BIOS? - if (!(PicoMCD&1) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) { - PicoMCD |= 1; + if (!(PicoAHW & PAHW_MCD) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) || + !strncmp((char *)rom+0x128, "BOOT", 4))) { + PicoAHW |= PAHW_MCD; rom = cd_realloc(rom, size); } // Check for SMD: - if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD + if (size >= 0x4200 && (size&0x3fff)==0x200 && + ((rom[0x2280] == 'S' && rom[0x280] == 'E') || (rom[0x280] == 'S' && rom[0x2281] == 'E'))) { + DecodeSmd(rom,size); size-=0x200; // Decode and byteswap SMD + } else Byteswap(rom,size); // Just byteswap if (prom) *prom=rom; @@ -483,33 +493,64 @@ int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize) return 0; } -// Insert/remove a cartridge: +// Insert a cartridge: int PicoCartInsert(unsigned char *rom,unsigned int romsize) { // notaz: add a 68k "jump one op back" opcode to the end of ROM. // This will hang the emu, but will prevent nasty crashes. // note: 4 bytes are padded to every ROM - if(rom != NULL) + if (rom != NULL) *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped Pico.rom=rom; Pico.romsize=romsize; - // setup correct memory map for loaded ROM - if (PicoMCD & 1) - PicoMemSetupCD(); - else PicoMemSetup(); + if (SRam.data) { + free(SRam.data); + SRam.data = NULL; + } + + if (PicoCartUnloadHook != NULL) { + PicoCartUnloadHook(); + PicoCartUnloadHook = NULL; + } + + PicoAHW &= PAHW_MCD; + + PicoMemResetHooks(); + PicoDmaHook = NULL; + PicoResetHook = NULL; + PicoLineHook = NULL; + PicoLoadStateHook = NULL; + carthw_chunks = NULL; + PicoMemReset(); - if (!(PicoMCD & 1)) + if (!(PicoAHW & PAHW_MCD)) PicoCartDetect(); - return PicoReset(1); + // setup correct memory map for loaded ROM + // call PicoMemReset again due to possible memmap change + switch (PicoAHW) { + default: + elprintf(EL_STATUS|EL_ANOMALY, "starting in unknown hw configuration: %x", PicoAHW); + case 0: + case PAHW_SVP: PicoMemSetup(); break; + case PAHW_MCD: PicoMemSetupCD(); break; + case PAHW_PICO: PicoMemSetupPico(); break; + } + PicoMemReset(); + + PicoPower(); + return 0; } -int PicoUnloadCart(unsigned char* romdata) +int PicoCartUnload(void) { - free(romdata); + if (Pico.rom != NULL) { + free(Pico.rom); + Pico.rom=NULL; + } return 0; } @@ -528,11 +569,13 @@ static int name_cmp(const char *name) return rom_strcmp(0x150, name); } -/* various cart-specific things, which can't be handled by generic code */ -void PicoCartDetect(void) +/* + * various cart-specific things, which can't be handled by generic code + * (maybe I should start using CRC for this stuff?) + */ +static void PicoCartDetect(void) { int sram_size = 0, csum; - if(SRam.data) free(SRam.data); SRam.data=0; Pico.m.sram_reg = 0; csum = PicoRead32(0x18c) & 0xffff; @@ -548,10 +591,12 @@ void PicoCartDetect(void) Pico.m.sram_reg |= 4; } else { // normal SRAM - SRam.start = PicoRead32(0x1B4) & 0xFFFF00; + SRam.start = PicoRead32(0x1B4) & ~0xff; SRam.end = PicoRead32(0x1B8) | 1; sram_size = SRam.end - SRam.start + 1; } + SRam.start &= ~0xff000000; + SRam.end &= ~0xff000000; Pico.m.sram_reg |= 0x10; // SRAM was detected } if (sram_size <= 0) @@ -564,10 +609,16 @@ void PicoCartDetect(void) sram_size = 0x004000; } + // this game actually doesn't have SRAM, but some weird protection + if (rom_strcmp(0x120, "PUGGSY") == 0) + { + SRam.start = SRam.end = sram_size = 0; + } + if (sram_size) { SRam.data = (unsigned char *) calloc(sram_size, 1); - if(!SRam.data) return; + if (SRam.data == NULL) return; } SRam.changed = 0; @@ -624,13 +675,53 @@ void PicoCartDetect(void) SRam.eeprom_bit_out= 7; } + // SVP detection + else if (name_cmp("Virtua Racing") == 0 || + name_cmp("VIRTUA RACING") == 0) + { + PicoSVPStartup(); + } + + // Pico + else if (rom_strcmp(0x100, "SEGA PICO") == 0 || + rom_strcmp(0x100, "IMA IKUNOUJYUKU") == 0) // what is that supposed to mean? + { + PicoInitPico(); + } + + // Detect 12-in-1 mapper + else if ((name_cmp("ROBOCOP 3") == 0 && Pico.romsize == 0x200000) || + (rom_strcmp(0x160, "FLICKY") == 0 && Pico.romsize >= 0x200000) || + (name_cmp(" SHOVE IT!") == 0 && Pico.romsize >= 0x200000) || + (name_cmp("MS PACMAN") == 0 && Pico.romsize >= 0x200000)) // bad dump? + { + carthw_12in1_startup(); + } + + // Realtec mapper + else if (Pico.romsize == 512*1024 && ( + rom_strcmp(0x94, "THE EARTH DEFEND") == 0 || + rom_strcmp(0xfe, "WISEGAME 11-03-1993") == 0 || // Funny World + rom_strcmp(0x95, "MALLET LEGEND ") == 0)) // Whac-A-Critter + { + carthw_realtec_startup(); + } + + // Radica mapper + else if (name_cmp("KID CHAMELEON") == 0 && Pico.romsize > 0x100000) + { + carthw_radica_startup(); + } + // Some games malfunction if SRAM is not filled with 0xff if (name_cmp("DINO DINI'S SOCCER") == 0 || name_cmp("MICRO MACHINES II") == 0) + { memset(SRam.data, 0xff, sram_size); + } // Unusual region 'code' - if (rom_strcmp(0x1f0, "EUROPE") == 0) + if (rom_strcmp(0x1f0, "EUROPE") == 0 || rom_strcmp(0x1f0, "Europe") == 0) *(int *) (Pico.rom+0x1f0) = 0x20204520; }