5050
5151// Externs
5252extern GameFileRecord * pActiveGameData ;
53+ extern char multicart_start ;
54+ extern char multicart_end ;
5355
5456//Memory for the menu ROM and the running cartridge.
5557//We keep both in memory so we can quickly exchange them when a reset has been detected.
@@ -74,6 +76,8 @@ system_options sys_opt;
7476char * sysData = (char * )& sys_opt ;
7577uint8_t checkDevMode = 0 ;
7678
79+ FILINFO cart_file_info ;
80+
7781/*
7882//Pinning:
7983A0-A14,SWCTL - PC0-PC15
@@ -101,12 +105,19 @@ void uart_output_func(unsigned char c){
101105extern void romemu (void );
102106
103107// Load a ROM into cartridge memory
108+ void loadMenu () {
109+ bool use_embedded_menu = (f_stat ("/multicart.bin" , & cart_file_info ) != FR_OK );
110+
111+ loadRomWithHighScore ("/multicart.bin" , false, use_embedded_menu );
112+ }
113+
104114void loadRom (char * fn ) {
105- loadRomWithHighScore (fn , false); // by default, do not load high score mode
115+ loadRomWithHighScore (fn , false, false ); // by default, do not load high score mode
106116}
107- void loadRomWithHighScore (char * fn , bool load_hs_mode ) {
117+
118+ void loadRomWithHighScore (char * fn , bool load_hs_mode , bool use_embedded_menu ) {
108119 FIL f ;
109- FRESULT fr ;
120+ FRESULT fr = FR_NO_FILE ; // assume no file, so we can test if we ever opened the file later
110121 UINT r = 0 ;
111122 int n ;
112123 UINT x ;
@@ -117,14 +128,20 @@ void loadRomWithHighScore(char *fn, bool load_hs_mode) {
117128 // loaded menu data
118129 n = sizeof (menuData );
119130 }
120- fr = f_open (& f , fn , FA_READ );
121- if (fr ) {
122- xprintf ("Error opening file: %d\n" , fr );
131+ if (romData == menuData && use_embedded_menu == true) {
132+ xprintf ("Copying %lu bytes of menu data... " , & multicart_end - & multicart_start );
133+ memcpy (menuData , & multicart_start , & multicart_end - & multicart_start );
134+ xprintf ("OK!\n" );
123135 } else {
124- xprintf ("Opened file: %s\n" , fn );
136+ fr = f_open (& f , fn , FA_READ );
137+ if (fr ) {
138+ xprintf ("Error opening file: %d\n" , fr );
139+ } else {
140+ xprintf ("Opened file: %s\n" , fn );
141+ }
142+ f_read (& f , romData , 64 * 1024 , & r );
143+ xprintf ("Read %d bytes of rom data.\n" , r );
125144 }
126- f_read (& f , romData , 64 * 1024 , & r );
127- xprintf ("Read %d bytes of rom data.\n" , r );
128145 // It's a game and it's <= 32KB
129146 if (n > 32 * 1024 && r <= 32 * 1024 ) {
130147 // pad with 0x01 for Mine Storm II and Polar Rescue (and any
@@ -192,7 +209,9 @@ void loadRomWithHighScore(char *fn, bool load_hs_mode) {
192209 parmRam [0xe0 ] = 0x66 ; // High Score flag (IDLE:0x66, LOAD2VEC:0x77, SAVE2STM:0x88)
193210 }
194211 }
195- f_close (& f );
212+ if (fr != FR_NO_FILE ) {
213+ f_close (& f );
214+ }
196215}
197216
198217//Stream data, for the Bad Apple demo
@@ -258,7 +277,7 @@ void doChangeRom(char* basedir, int i) {
258277
259278 romData = c_and_l .cartData ;
260279 xprintf ("Going to read rom image %s\n" , buff );
261- loadRomWithHighScore (buff , true);
280+ loadRomWithHighScore (buff , true, false );
262281 }
263282}
264283
@@ -409,7 +428,6 @@ void ledsOff() {
409428}
410429
411430static FATFS FatFs ;
412- FILINFO cart_file_info ;
413431FIL cart_file ;
414432void doRamDisk () {
415433 /**
@@ -482,7 +500,7 @@ void doRamDisk() {
482500
483501 (void ) highScoreOpenFile (); // Create/Open highscore file
484502 romData = menuData ; // Explicitly setting this here so we know WTF is going on in the background
485- loadRom ( "/multicart.bin" );
503+ loadMenu ( );
486504 loadListing (menuDir , & c_and_l .listing , menuIndex + 1 , menuIndex + 1 + 0x200 , romData );
487505 }
488506 } else if (sys_opt .usb_dev == USB_DEV_EXIT ) {
@@ -497,7 +515,7 @@ void doRamDisk() {
497515 (void ) highScoreOpenFile (); // Create/Open highscore file
498516 sys_opt .usb_dev = USB_DEV_DISABLED ;
499517 romData = menuData ; // Explicitly setting this here so we know WTF is going on in the background
500- loadRom ( "/multicart.bin" );
518+ loadMenu ( );
501519 loadListing (menuDir , & c_and_l .listing , menuIndex + 1 , menuIndex + 1 + 0x200 , romData );
502520 }
503521
@@ -720,7 +738,7 @@ int main(void) {
720738
721739 // Load the Menu
722740 romData = menuData ; // Explicitly setting this here so we know WTF is going on in the background
723- loadRom ( "/multicart.bin" );
741+ loadMenu ( );
724742 loadListing (menuDir , & c_and_l .listing , menuIndex + 1 , menuIndex + 1 + 0x200 , romData );
725743 sys_opt .usb_dev = USB_DEV_DISABLED ;
726744
0 commit comments