1919
2020#include <stdio.h>
2121#include <stdlib.h>
22+ #include <string.h>
2223#include "h8300.h"
2324#include "memory.h"
2425
@@ -64,6 +65,7 @@ uint8 memtype[65536];
6465register_funcs port [0x10000 - 0xff88 ];
6566int wait_states ;
6667
68+ char * rom_file_name = NULL ;
6769
6870
6971
@@ -178,44 +180,33 @@ void SET_WORD(uint16 addr, uint16 val) {
178180 * \return 1 on success, 0 otherwise.
179181 */
180182int read_rom () {
181- char * brickemu_dir ;
182- char filename [ 512 ] ;
183+ int result = 0 ;
184+ char * rom_file_ext = NULL ;
183185 FILE * romfile ;
184- brickemu_dir = getenv ("BRICKEMU_DIR" );
185- if (!brickemu_dir )
186- brickemu_dir = "." ;
187186
188- snprintf (filename , sizeof (filename ), "%s/rom.coff" , brickemu_dir );
189- romfile = fopen (filename , "rb" );
190-
191- if (romfile && coff_init (romfile )) {
192- coff_read (romfile , 0 );
193- coff_symbols (romfile , 0 );
194- fclose (romfile );
195- return 1 ;
196- }
197-
198- snprintf (filename , sizeof (filename ), "%s/rom.bin" , brickemu_dir );
199- romfile = fopen (filename , "rb" );
200-
201- if (romfile ) {
202- fread (memory , 0x4000 , 1 , romfile );
203- fclose (romfile );
204- return 1 ;
205- }
187+ if (rom_file_name && (rom_file_ext = strrchr (rom_file_name , '.' ))) {
206188
189+ if ((strcmp (rom_file_ext , ".coff" ) == 0 ) && (romfile = fopen (rom_file_name , "rb" ))) {
190+ if (coff_init (romfile )) {
191+ coff_read (romfile , 0 );
192+ coff_symbols (romfile , 0 );
193+ result = 1 ;
194+ }
195+ } else if ((strcmp (rom_file_ext , ".bin" ) == 0 ) && (romfile = fopen (rom_file_name , "rb" ))) {
196+ fread (memory , 0x4000 , 1 , romfile );
197+ result = 1 ;
198+ } else if ((strcmp (rom_file_ext , ".srec" ) == 0 ) && (romfile = fopen (rom_file_name , "r" ))) {
199+ if (srec_read (romfile , 0 ) >= 0 ) {
200+ result = 1 ;
201+ }
202+ }
207203
208- snprintf (filename , sizeof (filename ), "%s/rom.srec" , brickemu_dir );
209- romfile = fopen (filename , "r" );
210- if (romfile ) {
211- if (srec_read (romfile , 0 ) >= 0 ) {
204+ if (romfile ) {
212205 fclose (romfile );
213- return 1 ;
214206 }
215- fclose (romfile );
216207 }
217208
218- return 0 ;
209+ return result ;
219210}
220211
221212/** \brief initialize brick memory
@@ -227,12 +218,14 @@ int read_rom() {
227218 * must be named "rom.bin" or "rom.srec".
228219 * \return 1 on success, 0 otherwise.
229220 */
230- void mem_init () {
221+ void mem_init (char * rom_file ) {
231222 int i ;
232223
224+ rom_file_name = rom_file ;
233225 if (!read_rom ()) {
234226 fprintf (stderr , "Please provide a ROM image (coff, binary, or srec format).\n" );
235227 fprintf (stderr , "If extracting the ROM image, save it to a rom.bin or rom.srec file.\n" );
228+ fflush (stderr );
236229 abort ();
237230 }
238231
0 commit comments