4 Module for loading in the different ROM image types (.bin/.smd)
14 static const char *rom_path
= "roms";
16 void set_rom_path(const char *path
)
22 WHAT YOU FIND IN THE 512 BYTES HEADER:
31 1: This first byte should have the number of 16KB blocks the rom has.
32 The header isn't part of the formula, so this number is:
33 [size of rom-512]/16384
34 If the size is more than 255, the value should be H00.
36 2: This byte indicates if the ROM is a part of a splitted rom series. If
37 the rom is the last part of the series (or isn't a splitted rom at all),
38 this byte should be H00. In other cases should be H40. See "CREATING
39 SPLITTED ROMS" for details on this format.
43 Allocate a buffer and stuff the named ROM inside. If rom_size is non-NULL,
44 store the ROM size there.
47 uint8_t *load_rom(size_t *rom_size
, const char *name
)
57 file
= dgen_fopen(rom_path
, name
, (DGEN_READ
| DGEN_CURRENT
));
59 fprintf(stderr
, "%s: can't open ROM file.\n", name
);
63 /* A valid ROM will surely not be bigger than 64MB. */
64 rom
= load(&context
, &size
, file
, (64 * 1024 * 1024));
68 fprintf(stderr
, "%s: unable to load ROM: %s.\n", name
,
71 fprintf(stderr
, "%s: no valid ROM found.\n",
73 load_finish(&context
);
78 /* ROM file too small */
83 If "SEGA" isn't found at 0x100 and the total size minus 512 is a
84 multiple of 0x4000, it probably is a SMD.
86 if (memcmp(&rom
[0x100], "SEGA", 4)) {
88 uint8_t *src
= &rom
[0x200];
89 size_t chunks
= ((size
- 0x200) / 0x4000);
91 if (((size
- 0x200) & (0x4000 - 1)) != 0)
94 /* Corrupt ROM? Complain and continue anyway. */
95 if (((rom
[0] != 0x00) && (rom
[0] != chunks
)) ||
96 (rom
[8] != 0xaa) || (rom
[9] != 0xbb))
97 fprintf(stderr
, "%s: corrupt SMD header.\n", name
);
99 De-interleave ROM, overwrite SMD header with the result.
105 memcpy(tmp
, src
, 0x2000);
107 for (i
= 0; (i
!= 0x2000); ++i
) {
113 /* Does it look like a valid ROM now? */
114 if (memcmp(&rom
[0x100], "SEGA", 4)) {
121 load_finish(&context
);
123 if (rom_size
!= NULL
)
128 void unload_rom(uint8_t *rom
)