Fix remaining bugs and fix indentation

This commit is contained in:
iLag 2017-03-25 20:28:08 -07:00 committed by notaz
parent 9a570a67ca
commit 126eb5f469

View file

@ -27,9 +27,9 @@
struct patch struct patch
{ {
unsigned int addr; unsigned int addr;
unsigned short data; unsigned short data;
unsigned char comp; unsigned char comp;
}; };
struct patch_inst *PicoPatches = NULL; struct patch_inst *PicoPatches = NULL;
@ -174,14 +174,15 @@ void genie_decode_ms(const char *code, struct patch *result)
/* Correct the address */ /* Correct the address */
result->addr = ((result->addr >> 4) | (result->addr << 12 & 0xF000)) ^ 0xF000; result->addr = ((result->addr >> 4) | (result->addr << 12 & 0xF000)) ^ 0xF000;
/* Optional: 3 digits for comp */ /* Optional: 3 digits for comp */
if (code[8]=='-'){ if (code[7]=='-')
{
for(i=8;i<11;++i) for(i=8;i<11;++i)
{ {
if (i==9) continue; /* 2nd character is ignored */ if (i==9) continue; /* 2nd character is ignored */
if(!(x = strchr(hex_chars, code[i]))) if(!(x = strchr(hex_chars, code[i])))
{ {
result->addr = result->data = -1; result->addr = result->data = -1;
return; return;
} }
result->comp = (result->comp << 4) | ((x - hex_chars) >> 1); result->comp = (result->comp << 4) | ((x - hex_chars) >> 1);
} }
@ -356,161 +357,161 @@ void decode(const char* code, struct patch* result)
void PicoPatchUnload(void) void PicoPatchUnload(void)
{ {
if (PicoPatches != NULL) if (PicoPatches != NULL)
{ {
free(PicoPatches); free(PicoPatches);
PicoPatches = NULL; PicoPatches = NULL;
} }
PicoPatchCount = 0; PicoPatchCount = 0;
} }
int PicoPatchLoad(const char *fname) int PicoPatchLoad(const char *fname)
{ {
FILE *f; FILE *f;
char buff[256]; char buff[256];
struct patch pt; struct patch pt;
int array_len = 0; int array_len = 0;
PicoPatchUnload(); PicoPatchUnload();
f = fopen(fname, "r"); f = fopen(fname, "r");
if (f == NULL) if (f == NULL)
{ {
return -1; return -1;
} }
while (fgets(buff, sizeof(buff), f)) while (fgets(buff, sizeof(buff), f))
{ {
int llen, clen; int llen, clen;
llen = strlen(buff); llen = strlen(buff);
for (clen = 0; clen < llen; clen++) for (clen = 0; clen < llen; clen++)
if (isspace_(buff[clen])) if (isspace_(buff[clen]))
break; break;
buff[clen] = 0; buff[clen] = 0;
if (clen > 11 || clen < 8) if (clen > 11 || clen < 8)
continue; continue;
decode(buff, &pt); decode(buff, &pt);
if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1) if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1)
continue; continue;
/* code was good, add it */ /* code was good, add it */
if (array_len < PicoPatchCount + 1) if (array_len < PicoPatchCount + 1)
{ {
void *ptr; void *ptr;
array_len *= 2; array_len *= 2;
array_len++; array_len++;
ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0])); ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
if (ptr == NULL) break; if (ptr == NULL) break;
PicoPatches = ptr; PicoPatches = ptr;
} }
strcpy(PicoPatches[PicoPatchCount].code, buff); strcpy(PicoPatches[PicoPatchCount].code, buff);
/* strip */ /* strip */
for (clen++; clen < llen; clen++) for (clen++; clen < llen; clen++)
if (!isspace_(buff[clen])) if (!isspace_(buff[clen]))
break; break;
for (llen--; llen > 0; llen--) for (llen--; llen > 0; llen--)
if (!isspace_(buff[llen])) if (!isspace_(buff[llen]))
break; break;
buff[llen+1] = 0; buff[llen+1] = 0;
strncpy(PicoPatches[PicoPatchCount].name, buff + clen, 51); strncpy(PicoPatches[PicoPatchCount].name, buff + clen, 51);
PicoPatches[PicoPatchCount].name[51] = 0; PicoPatches[PicoPatchCount].name[51] = 0;
PicoPatches[PicoPatchCount].active = 0; PicoPatches[PicoPatchCount].active = 0;
PicoPatches[PicoPatchCount].addr = pt.addr; PicoPatches[PicoPatchCount].addr = pt.addr;
PicoPatches[PicoPatchCount].data = pt.data; PicoPatches[PicoPatchCount].data = pt.data;
PicoPatches[PicoPatchCount].data_old = 0; PicoPatches[PicoPatchCount].data_old = 0;
PicoPatchCount++; PicoPatchCount++;
// fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data, // fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data,
// PicoPatches[PicoPatchCount-1].name); // PicoPatches[PicoPatchCount-1].name);
} }
fclose(f); fclose(f);
return 0; return 0;
} }
/* to be called when the Rom is loaded and byteswapped */ /* to be called when the Rom is loaded and byteswapped */
void PicoPatchPrepare(void) void PicoPatchPrepare(void)
{ {
int i; int i;
int addr; int addr;
for (i = 0; i < PicoPatchCount; i++) for (i = 0; i < PicoPatchCount; i++)
{ {
addr=PicoPatches[i].addr; addr=PicoPatches[i].addr;
addr &= ~1; addr &= ~1;
if (addr < Pico.romsize) if (addr < Pico.romsize)
PicoPatches[i].data_old = *(unsigned short *)(Pico.rom + addr); PicoPatches[i].data_old = *(unsigned short *)(Pico.rom + addr);
else
{
if(!(PicoAHW & PAHW_SMS))
PicoPatches[i].data_old = (unsigned short) m68k_read16(addr);
else else
; // wrong: PicoPatches[i].data_old = (unsigned char) PicoRead8_z80(addr); {
} if(!(PicoAHW & PAHW_SMS))
if (strstr(PicoPatches[i].name, "AUTO")) PicoPatches[i].data_old = (unsigned short) m68k_read16(addr);
PicoPatches[i].active = 1; else
} ;// wrong: PicoPatches[i].data_old = (unsigned char) PicoRead8_z80(addr);
}
if (strstr(PicoPatches[i].name, "AUTO"))
PicoPatches[i].active = 1;
}
} }
void PicoPatchApply(void) void PicoPatchApply(void)
{ {
int i, u; int i, u;
unsigned int addr; unsigned int addr;
for (i = 0; i < PicoPatchCount; i++) for (i = 0; i < PicoPatchCount; i++)
{ {
addr = PicoPatches[i].addr; addr = PicoPatches[i].addr;
if (addr < Pico.romsize) if (addr < Pico.romsize)
{
if (PicoPatches[i].active)
{ {
if (!(PicoAHW & PAHW_SMS)) if (PicoPatches[i].active)
*(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data; {
else if (!PicoPatches[i].comp || PicoPatches[i].comp == *(char *)(Pico.rom + addr)) if (!(PicoAHW & PAHW_SMS))
*(char *)(Pico.rom + addr) = (char) PicoPatches[i].data; *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data;
} else if (!PicoPatches[i].comp || PicoPatches[i].comp == *(char *)(Pico.rom + addr))
else *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data;
{ }
// if current addr is not patched by older patch, write back original val else
for (u = 0; u < i; u++) {
if (PicoPatches[u].addr == addr) break; // if current addr is not patched by older patch, write back original val
if (u == i) for (u = 0; u < i; u++)
{ if (PicoPatches[u].addr == addr) break;
if (!(PicoAHW & PAHW_SMS)) if (u == i)
*(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old; {
else if (!(PicoAHW & PAHW_SMS))
*(char *)(Pico.rom + addr) = (char) PicoPatches[i].data_old; *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
} else
} *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data_old;
}
}
// fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr, // fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr,
// *(unsigned short *)(Pico.rom + addr)); // *(unsigned short *)(Pico.rom + addr));
}
else
{
if (PicoPatches[i].active)
{
if (!(PicoAHW & PAHW_SMS))
m68k_write16(addr,PicoPatches[i].data);
else
;// wrong: PicoWrite8_z80(addr,PicoPatches[i].data);
} }
else else
{ {
// if current addr is not patched by older patch, write back original val if (PicoPatches[i].active)
for (u = 0; u < i; u++) {
if (PicoPatches[u].addr == addr) break; if (!(PicoAHW & PAHW_SMS))
if (u == i) m68k_write16(addr,PicoPatches[i].data);
{ else
if (!(PicoAHW & PAHW_SMS)) ;// wrong: PicoWrite8_z80(addr,PicoPatches[i].data);
m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old); }
else else
;// wrong: PicoWrite8_z80(PicoPatches[i].addr,PicoPatches[i].data_old); {
} // if current addr is not patched by older patch, write back original val
for (u = 0; u < i; u++)
if (PicoPatches[u].addr == addr) break;
if (u == i)
{
if (!(PicoAHW & PAHW_SMS))
m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old);
else
;// wrong: PicoWrite8_z80(PicoPatches[i].addr,PicoPatches[i].data_old);
}
}
} }
} }
}
} }