Skip to content

Commit 8b8f4fa

Browse files
committed
Loader correctly handles roms with "DiskDude!" strings.
1 parent 960d58b commit 8b8f4fa

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

loader/nes_loader.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -202,24 +202,35 @@ int sendNES(fs::path p)
202202
ifstream f(p, ios::binary);
203203
if (!f.is_open()) { printf("File open fail\n"); return 1; }
204204

205+
// Reset NES machine
205206
{ char v = 1; writePacket(uart, 0x35, &v, 1); }
206207
{ char v = 0; writePacket(uart, 0x35, &v, 1); }
207208

208209
size_t total_read = 0xffffff; // max size 16MB
209210
size_t pos = 0;
210211

212+
bool first = true;
211213
while (pos < total_read) {
212214
streamsize want_read = (total_read - pos) > sizeof(sendbuf) ? sizeof(sendbuf) : (total_read - pos);
213215
f.read(sendbuf, want_read);
214216
streamsize n = f.gcount();
215217
// printf("want_read=%d, actual_read=%d\n", (int)want_read, (int)n);
216218
if (n > 0) {
219+
if (first && n > 16 && strncmp(&sendbuf[7], "DiskDude!", 9) == 0) {
220+
// "DiskDude!" work-around. See https://www.nesdev.org/wiki/INES
221+
// Older versions of the iNES emulator ignored bytes 7-15 and writes "DiskDude!" there,
222+
// corrupting byte 7 and results in 64 being added to the mapper number.
223+
printf("Old rom file detected with 'DiskDude!' string. Applying fix on-the-fly.\n");
224+
sendbuf[7] = 0; // simply setting byte 7 to 0 should fix it
225+
}
226+
217227
//printf("Write packet\n");
218228
writePacket(uart, 0x37, sendbuf, n);
219229
}
220230
if (f.eof())
221231
break;
222232
pos += n;
233+
first = false;
223234
}
224235
f.close();
225236

0 commit comments

Comments
 (0)