Mapper30 cleanup

This commit is contained in:
retro-wertz
2017-10-20 12:20:00 +08:00
parent ce7fe391b9
commit 7a9d85fa7b
2 changed files with 64 additions and 77 deletions

View File

@@ -30,12 +30,13 @@
* Study Hall (128K PRG (in 512K flash chip), 8K CHR RAM, Horizontal Mirroring, Flash enabled) * Study Hall (128K PRG (in 512K flash chip), 8K CHR RAM, Horizontal Mirroring, Flash enabled)
* Although Xmas 2013 uses a different board, where LEDs can be controlled (with writes to the $8000-BFFF space), * Although Xmas 2013 uses a different board, where LEDs can be controlled (with writes to the $8000-BFFF space),
* it otherwise functions identically. * it otherwise functions identically.
*
* 17-10-20 - Works with Mystic Origins (Demo)
*/ */
#include "mapinc.h" #include "mapinc.h"
#include "../ines.h"
extern uint32 ROM_size;
static uint8 latche, latcheinit, bus_conflict, chrram_mask, software_id=0; static uint8 latche, latcheinit, bus_conflict, chrram_mask, software_id=0;
static uint16 latcha; static uint16 latcha;
static uint8 *flashdata; static uint8 *flashdata;
@@ -44,7 +45,7 @@ static uint8 *FlashPage[32];
/* static uint32 *FlashWriteCountPage[32]; */ /* static uint32 *FlashWriteCountPage[32]; */
/* static uint8 flashloaded = 0; */ /* static uint8 flashloaded = 0; */
static uint8 flash_save=0, flash_state=0, flash_mode=0, flash_bank; static uint8 flash_save = 0, flash_state = 0, flash_mode = 0, flash_bank;
static void (*WLSync)(void); static void (*WLSync)(void);
static void (*WHSync)(void); static void (*WHSync)(void);
@@ -67,22 +68,22 @@ void setfprg16(uint32 A, uint32 V) {
V &= PRGmask16[0]; V &= PRGmask16[0];
setfpageptr(16, A, flashdata ? (&flashdata[V << 14]) : 0); setfpageptr(16, A, flashdata ? (&flashdata[V << 14]) : 0);
} else { } else {
uint32 VA = V << 3;
int x; int x;
uint32 VA = V << 3;
for (x = 0; x < 8; x++) for (x = 0; x < 8; x++)
setfpageptr(2, A + (x << 11), flashdata ? (&flashdata[((VA + x) & PRGmask2[0]) << 11]) : 0); setfpageptr(2, A + (x << 11), flashdata ? (&flashdata[((VA + x) & PRGmask2[0]) << 11]) : 0);
} }
} }
void inc_flash_write_count(uint8 bank, uint32 A) { void inc_flash_write_count(uint8 bank, uint32 A) {
flash_write_count[(bank*4) + ((A&0x3000)>>12)]++; flash_write_count[(bank * 4) + ((A & 0x3000) >> 12)]++;
if(!flash_write_count[(bank*4) + ((A&0x3000)>>12)]) if (!flash_write_count[(bank * 4) + ((A & 0x3000) >> 12)])
flash_write_count[(bank*4) + ((A&0x3000)>>12)]++; flash_write_count[(bank * 4) + ((A & 0x3000) >> 12)]++;
} }
uint32 GetFlashWriteCount(uint8 bank, uint32 A) { uint32 GetFlashWriteCount(uint8 bank, uint32 A) {
return flash_write_count[(bank*4) + ((A&0x3000)>>12)]; return flash_write_count[(bank * 4) + ((A & 0x3000) >> 12)];
} }
static void StateRestore(int version) { static void StateRestore(int version) {
@@ -105,24 +106,19 @@ static DECLFW(UNROM512HLatchWrite) {
} }
static DECLFR(UNROM512LatchRead) { static DECLFR(UNROM512LatchRead) {
uint8 flash_id[3]={0xB5,0xB6,0xB7}; uint8 flash_id[3] = { 0xB5, 0xB6, 0xB7 };
if(software_id) if (software_id) {
{ if (A & 1)
if(A&1) return flash_id[ROM_size >> 4];
return flash_id[ROM_size>>4];
else else
return 0xBF; return 0xBF;
} }
if(flash_save) if (flash_save) {
{ if (A < 0xC000) {
if(A < 0xC000) if (GetFlashWriteCount(flash_bank, A))
{
if(GetFlashWriteCount(flash_bank,A))
return FlashPage[A >> 11][A]; return FlashPage[A >> 11][A];
} } else {
else if (GetFlashWriteCount(ROM_size - 1, A))
{
if(GetFlashWriteCount(ROM_size-1,A))
return FlashPage[A >> 11][A]; return FlashPage[A >> 11][A];
} }
} }
@@ -133,19 +129,19 @@ static void UNROM512LatchPower(void) {
latche = latcheinit; latche = latcheinit;
WHSync(); WHSync();
SetReadHandler(0x8000, 0xFFFF, UNROM512LatchRead); SetReadHandler(0x8000, 0xFFFF, UNROM512LatchRead);
if(!flash_save) if (!flash_save)
SetWriteHandler(0x8000, 0xFFFF, UNROM512HLatchWrite); SetWriteHandler(0x8000, 0xFFFF, UNROM512HLatchWrite);
else else
{ {
SetWriteHandler(0x8000,0xBFFF,UNROM512LLatchWrite); SetWriteHandler(0x8000, 0xBFFF, UNROM512LLatchWrite);
SetWriteHandler(0xC000,0xFFFF,UNROM512HLatchWrite); SetWriteHandler(0xC000, 0xFFFF, UNROM512HLatchWrite);
} }
} }
static void UNROM512LatchClose(void) { static void UNROM512LatchClose(void) {
if(flash_write_count) if (flash_write_count)
FCEU_gfree(flash_write_count); FCEU_gfree(flash_write_count);
if(flashdata) if (flashdata)
FCEU_gfree(flashdata); FCEU_gfree(flashdata);
flash_write_count = NULL; flash_write_count = NULL;
flashdata = NULL; flashdata = NULL;
@@ -153,76 +149,64 @@ static void UNROM512LatchClose(void) {
static void UNROM512LSync() { static void UNROM512LSync() {
int erase_a[5]={0x9555,0xAAAA,0x9555,0x9555,0xAAAA}; int erase_a[5] = { 0x9555, 0xAAAA, 0x9555, 0x9555, 0xAAAA };
int erase_d[5]={0xAA,0x55,0x80,0xAA,0x55}; int erase_d[5] = { 0xAA, 0x55, 0x80, 0xAA, 0x55 };
int erase_b[5]={1,0,1,1,0}; int erase_b[5] = { 1, 0, 1, 1, 0 };
if(flash_mode==0) if (flash_mode==0) {
{ if ((latcha == erase_a[flash_state]) && (latche == erase_d[flash_state]) && (flash_bank == erase_b[flash_state])) {
if((latcha == erase_a[flash_state]) && (latche == erase_d[flash_state]) && (flash_bank == erase_b[flash_state]))
{
flash_state++; flash_state++;
if(flash_state == 5) if (flash_state == 5) {
{ flash_mode = 1;
flash_mode=1;
} }
} }
else if ((flash_state==2)&&(latcha==0x9555)&&(latche==0xA0)&&(flash_bank==1)) else if ((flash_state == 2) && (latcha == 0x9555) && (latche == 0xA0) && (flash_bank == 1)) {
{
flash_state++; flash_state++;
flash_mode=2; flash_mode = 2;
} }
else if ((flash_state==2)&&(latcha==0x9555)&&(latche==0x90)&&(flash_bank==1)) else if ((flash_state == 2) && (latcha == 0x9555) && (latche == 0x90) && (flash_bank == 1)) {
{ flash_state = 0;
flash_state=0; software_id = 1;
software_id=1; } else {
} if (latche == 0xF0)
else software_id = 0;
{ flash_state = 0;
if(latche==0xF0)
software_id=0;
flash_state=0;
} }
} }
else if(flash_mode==1) /* Chip Erase or Sector Erase */ else if (flash_mode == 1) { /* Chip Erase or Sector Erase */
{ if (latche == 0x30) {
if(latche==0x30)
{
inc_flash_write_count(flash_bank,latcha); inc_flash_write_count(flash_bank,latcha);
memset(&FlashPage[(latcha & 0xF000) >> 11][latcha & 0xF000],0xFF,0x1000); memset(&FlashPage[(latcha & 0xF000) >> 11][latcha & 0xF000], 0xFF, 0x1000);
} }
else if (latche==0x10) else if (latche == 0x10) {
{ uint32 i;
uint32 i; for(i = 0; i < (ROM_size * 4); i++)
for(i=0;i<(ROM_size*4);i++) inc_flash_write_count(i >> 2,i << 12);
inc_flash_write_count(i>>2,i<<12); memset(flashdata, 0xFF, ROM_size * 0x4000); /* Erasing the rom chip as instructed. Crash rate calulated to be 99.9% :) */
memset(flashdata,0xFF,ROM_size*0x4000); /* Erasing the rom chip as instructed. Crash rate calulated to be 99.9% :) */
} }
flash_state=0; flash_state = 0;
flash_mode=0; flash_mode = 0;
} }
else if(flash_mode==2) /* Byte Program */ else if (flash_mode == 2) { /* Byte Program */
{ if (!GetFlashWriteCount(flash_bank, latcha)) {
if(!GetFlashWriteCount(flash_bank,latcha)) inc_flash_write_count(flash_bank, latcha);
{ memcpy(&FlashPage[(latcha & 0xF000) >> 11][latcha & 0xF000], &Page[(latcha & 0xF000) >> 11][latcha & 0xF000], 0x1000);
inc_flash_write_count(flash_bank,latcha);
memcpy(&FlashPage[(latcha & 0xF000) >> 11][latcha & 0xF000],&Page[(latcha & 0xF000)>>11][latcha & 0xF000],0x1000);
} }
FlashPage[latcha>>11][latcha]&=latche; FlashPage[latcha >> 11][latcha] &= latche;
flash_state=0; flash_state = 0;
flash_mode=0; flash_mode = 0;
} }
} }
static void UNROM512HSync() { static void UNROM512HSync() {
flash_bank=latche&(ROM_size-1); flash_bank=latche&(ROM_size - 1);
setprg16(0x8000, flash_bank); setprg16(0x8000, flash_bank);
setprg16(0xc000, ~0); setprg16(0xc000, ~0);
setfprg16(0x8000, flash_bank); setfprg16(0x8000, flash_bank);
setfprg16(0xC000, ~0); setfprg16(0xC000, ~0);
setchr8r(0, (latche & chrram_mask) >> 5); setchr8r(0, (latche & chrram_mask) >> 5);
setmirror(MI_0+(latche>>7)); setmirror(MI_0 + (latche >> 7));
} }
void UNROM512_Init(CartInfo *info) { void UNROM512_Init(CartInfo *info) {
@@ -230,7 +214,7 @@ void UNROM512_Init(CartInfo *info) {
flash_bank = 0; flash_bank = 0;
flash_save = info->battery; flash_save = info->battery;
if(info->vram_size == 8192) if (info->vram_size == 8192)
chrram_mask = 0; chrram_mask = 0;
else if (info->vram_size == 16384) else if (info->vram_size == 16384)
chrram_mask = 0x20; chrram_mask = 0x20;
@@ -245,7 +229,7 @@ void UNROM512_Init(CartInfo *info) {
info->Power = UNROM512LatchPower; info->Power = UNROM512LatchPower;
info->Close = UNROM512LatchClose; info->Close = UNROM512LatchClose;
GameStateRestore = StateRestore; GameStateRestore = StateRestore;
if(flash_save) if (flash_save)
{ {
flashdata = (uint8*)FCEU_gmalloc(ROM_size * 0x4000); flashdata = (uint8*)FCEU_gmalloc(ROM_size * 0x4000);
flash_write_count = (uint32*)FCEU_gmalloc(ROM_size * 4 * sizeof(uint32)); flash_write_count = (uint32*)FCEU_gmalloc(ROM_size * 4 * sizeof(uint32));

View File

@@ -22,6 +22,9 @@
#ifndef _FCEU_INES_H #ifndef _FCEU_INES_H
#define _FCEU_INES_H #define _FCEU_INES_H
extern uint32 ROM_size;
extern uint32 VROM_size;
typedef struct { typedef struct {
char ID[4]; /*NES^Z*/ char ID[4]; /*NES^Z*/
uint8 ROM_size; uint8 ROM_size;