9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] gba: fix flash chip id for 128k and add setting
@ 2022-02-09  1:11 qwx
  2022-02-10  0:35 ` qwx
  0 siblings, 1 reply; 2+ messages in thread
From: qwx @ 2022-02-09  1:11 UTC (permalink / raw)
  To: 9front

Hello,

Some gba cartridges use flash chips produced by a few different
manufacturers for backup memory.  A game rom may then try to read the
device id for its flash chip and break if it's the wrong one.  There's
no way for us to know which chip is supposed to be there, save for
looking it up in some database.  These chips are either 64k or 128k
big.  gba(1) hardcodes the id for the SST 64k chip, but games like the
Pokémon ones use a 128k Macronix chip and expect a different id,
completely breaking them.

This patch sets a different default for 128k flash backups, and gives
the ability to set the exact type when creating the save file if it's
really necessary.  This new default fixes the Pokémons.  The type is
saved in the save file, but this will *not* break existing ones.  It
may be that we don't need to worry about the type and just have an id
that corresponds to the right chip size, which is why these defaults
make sense, but I haven't tested more than a dozen roms, mostly since
I don't really know which games use 128k flash.

Thoughts/comments?

Thanks,
qwx


diff 4ab2d149d46c6762c9b8d9fd24d0bf92b10704f6 uncommitted
--- a//sys/man/1/nintendo
+++ b//sys/man/1/nintendo
@@ -112,6 +112,10 @@
 .TP
 .B -s
 Save format used by the original game. Valid formats are: eeprom4, eeprom64, sram256, flash512, flash1024. The number corresponds to the size, in kilobits, of the save file. By default, the emulator attempts to automatically detect the save format, but does not always succeed.
+Some roms require a specific flash device id which may need to be set manually for them to work.
+Valid formats and corresponding ids are:
+flash512 (SST), flash512mx (Macronix 64K), flash512pan (Panasonic), flash512atm (Atmel),
+flash1024 (Macronix 128K), flash1024san (Sanyo).
 .PP
 .B nes
 options:
--- a/sys/src/games/gba/dat.h
+++ b/sys/src/games/gba/dat.h
@@ -9,6 +9,7 @@
 extern u16int reg[];
 extern uchar *rom, *back;
 extern int nrom, nback, backup;
+extern int flashid;
 
 extern int hblank, ppuy;
 
--- a/sys/src/games/gba/gba.c
+++ b/sys/src/games/gba/gba.c
@@ -15,6 +15,15 @@
 
 char *biosfile = "/sys/games/lib/gbabios.bin";
 
+enum {
+	IDSST = 0xd4bf,
+	IDMacronix64 = 0x1cc2,
+	IDPanasonic = 0x1b32,
+	IDAtmel = 0x3d1f,
+	IDSanyo = 0x1362,
+	IDMacronix128 = 0x09c2,
+};
+
 void
 writeback(void)
 {
@@ -65,10 +74,12 @@
 		if(v == s3.u){
 			if(memcmp(p - 1, "FLASH_V", 7) == 0 || memcmp(p - 1, "FLASH512_V", 10) == 0){
 				*size = 64*KB;
+				flashid = IDSST;
 				return FLASH;
 			}
 			if(memcmp(p - 1, "FLASH1M_V", 9) == 0){
 				*size = 128*KB;
+				flashid = IDMacronix128;
 				return FLASH;
 			}
 		}
@@ -90,10 +101,28 @@
 		return SRAM;
 	}else if(strcmp(s, "flash512") == 0){
 		*size = 64*KB;
+		flashid = IDSST;
 		return FLASH;
+	}else if(strcmp(s, "flash512mx") == 0){
+		*size = 64*KB;
+		flashid = IDMacronix64;
+		return FLASH;
+	}else if(strcmp(s, "flash512pan") == 0){
+		*size = 64*KB;
+		flashid = IDPanasonic;
+		return FLASH;
+	}else if(strcmp(s, "flash512atm") == 0){
+		*size = 64*KB;
+		flashid = IDAtmel;
+		return FLASH;
 	}else if(strcmp(s, "flash1024") == 0){
 		*size = 128*KB;
+		flashid = IDMacronix128;
 		return FLASH;
+	}else if(strcmp(s, "flash1024san") == 0){
+		*size = 128*KB;
+		flashid = IDSanyo;
+		return FLASH;
 	}else
 		return NOBACK;
 }
@@ -101,7 +130,8 @@
 void
 typename(char *s, int type, int size)
 {
-	char *st;
+	char *st, *id;
+	id = "";
 	switch(type){
 	case EEPROM:
 		st = "eeprom";
@@ -108,6 +138,12 @@
 		break;
 	case FLASH:
 		st = "flash";
+		switch(flashid){
+		case IDMacronix64: id = "mx"; break;
+		case IDPanasonic: id = "pan"; break;
+		case IDAtmel: id = "atm"; break;
+		case IDSanyo: id = "san"; break;
+		}
 		break;
 	case SRAM:
 		st = "sram";
@@ -116,7 +152,7 @@
 		sysfatal("typestr: unknown type %d -- shouldn't happen", type);
 		return;
 	}
-	snprint(s, BACKTYPELEN, "%s%d", st, size/128);
+	snprint(s, BACKTYPELEN, "%s%d%s", st, size/128, id);
 }
 
 void
--- a/sys/src/games/gba/mem.c
+++ b/sys/src/games/gba/mem.c
@@ -572,7 +572,7 @@
 	}
 }
 
-int flashstate, flashmode, flashbank;
+int flashstate, flashmode, flashbank, flashid;
 
 enum {
 	FLASHCMD0,
@@ -589,7 +589,7 @@
 flashread(u16int a)
 {
 	if((flashmode & FLASHID) != 0)
-		return (a & 1) != 0 ? 0xd4 : 0xbf;
+		return (a & 1) != 0 ? flashid >> 8 : flashid;
 	return back[(flashbank << 16) + a];
 }
 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-02-10  0:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-09  1:11 [9front] gba: fix flash chip id for 128k and add setting qwx
2022-02-10  0:35 ` qwx

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).