--- nono/vm/rom.cpp 2026/04/29 17:04:28 1.1.1.1 +++ nono/vm/rom.cpp 2026/04/29 17:04:29 1.1.1.2 @@ -6,6 +6,7 @@ #include #include #include +#include #include "rom.h" #include "configfile.h" @@ -38,18 +39,39 @@ ROMDevice::~ROMDevice() bool ROMDevice::LoadROM(const char *name, int size) { + struct stat st; + filename = gConfig->SearchFile(name); if (filename.empty()) { - warn("%s: cannot find %s", devname, filename.c_str()); + warnx("%s not found", name); return false; } // オープン fd = open(filename.c_str(), O_RDONLY); if (fd == -1) { - warn("%s: cannot open %s", devname, filename.c_str()); + warn("ROM \"%s\" open failed", filename.c_str()); + return false; + } + + // ファイルサイズをチェック。 + // ファイルが短くても mmap はしれっと成功してしまうようだ。 + if (fstat(fd, &st) == -1) { + warn("ROM \"%s\" fstat failed", filename.c_str()); + close(fd); return false; } + if (st.st_size < size) { + warnx("ROM \"%s\" is too short (filesize is expected %d but %d)", + filename.c_str(), size, (int)st.st_size); + close(fd); + return false; + } + if (st.st_size > size) { + // 大きすぎるほうはワーニングだけでいいか + warnx("ROM \"%s\" is too large (ignore %d bytes)", + filename.c_str(), (int)st.st_size - size); + } // mmap // ファイルには書き戻さないけど、オンメモリでハックを入れることが @@ -58,7 +80,7 @@ ROMDevice::LoadROM(const char *name, int file = (uint8 *)mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); if (file == MAP_FAILED) { - warn("%s: cannot mmap %s", devname, filename.c_str()); + warn("ROM \"%s\" mmap failed", filename.c_str()); close(fd); return false; }