/* A minimal headless driver for mGBA's core, built by scripts/build_referees.py * against reference/build/mgba/libmgba.a and run by scripts/referee_run.py. * * Compile it with the -D flags mGBA itself was built with (build_referees.py * reads them from build.ninja): struct mCore's layout depends on them, and a * mismatch crashes on the first call through it. * * mgba-headless has no video buffer and its scripts cannot see the log, so the * referee runs use this instead: load a ROM (and the BIOS, if given), hold * keys over frame ranges, then print the log lines the ROM wrote through the * mGBA debug port, chosen registers and words, and save the last frame. * * mgba_driver ROM [--bios FILE] [--frames N] [--keys FRAME:MASK]... * [--png FILE] [--sram FILE] [--reg NAME]... [--word ADDRESS]... * * --keys sets the held-key mask (KEYINPUT bit order, 1 = pressed) from FRAME * on, until the next --keys. Output lines: "LOG : ", * "REG ", "WORD
", "FRAMES ". */ #include #include #include #include #include #include #include #include #include #include #define MAX_ITEMS 256 static void logLine(struct mLogger* logger, int category, enum mLogLevel level, const char* format, va_list args) { (void) logger; const char* name = mLogCategoryName(category); if (!name) { name = "?"; } if (level & (mLOG_DEBUG | mLOG_STUB)) { /* mGBA's own chatter; the ROMs' debug-port output arrives as INFO or above. */ if (strcmp(name, "GBA Debug") != 0) { return; } } printf("LOG %s: ", name); vprintf(format, args); printf("\n"); } int main(int argc, char** argv) { const char* rom = NULL; const char* bios = NULL; const char* png = NULL; const char* sram = NULL; unsigned frames = 600; unsigned keyFrame[MAX_ITEMS], keyMask[MAX_ITEMS]; int nKeys = 0; const char* regs[MAX_ITEMS]; int nRegs = 0; unsigned long words[MAX_ITEMS]; int nWords = 0; for (int i = 1; i < argc; ++i) { const char* next = i + 1 < argc ? argv[i + 1] : NULL; if (!strcmp(argv[i], "--bios") && next) { bios = argv[++i]; } else if (!strcmp(argv[i], "--frames") && next) { frames = strtoul(argv[++i], NULL, 0); } else if (!strcmp(argv[i], "--keys") && next && nKeys < MAX_ITEMS) { if (sscanf(argv[++i], "%u:%i", &keyFrame[nKeys], &keyMask[nKeys]) != 2) { fprintf(stderr, "bad --keys %s\n", argv[i]); return 2; } ++nKeys; } else if (!strcmp(argv[i], "--png") && next) { png = argv[++i]; } else if (!strcmp(argv[i], "--sram") && next) { sram = argv[++i]; } else if (!strcmp(argv[i], "--reg") && next && nRegs < MAX_ITEMS) { regs[nRegs++] = argv[++i]; } else if (!strcmp(argv[i], "--word") && next && nWords < MAX_ITEMS) { words[nWords++] = strtoul(argv[++i], NULL, 0); } else if (argv[i][0] != '-' && !rom) { rom = argv[i]; } else { fprintf(stderr, "usage: %s ROM [--bios FILE] [--frames N] [--keys FRAME:MASK]... [--png FILE] [--sram FILE] [--reg NAME]... [--word ADDRESS]...\n", argv[0]); return 2; } } if (!rom) { fprintf(stderr, "no ROM\n"); return 2; } struct mLogger logger = { .log = logLine }; mLogSetDefaultLogger(&logger); struct mCore* core = mCoreFind(rom); if (!core || !core->init(core)) { fprintf(stderr, "cannot load %s\n", rom); return 1; } unsigned width, height; core->baseVideoSize(core, &width, &height); mColor* video = calloc(width * height, sizeof(mColor)); core->setVideoBuffer(core, video, width); mCoreInitConfig(core, "gba-python"); /* No idle-loop skipping: the referee runs every cycle the way hardware does. */ mCoreConfigSetOverrideValue(&core->config, "idleOptimization", "ignore"); if (bios) { mCoreConfigSetOverrideIntValue(&core->config, "useBios", 1); mCoreConfigSetOverrideIntValue(&core->config, "skipBios", 0); } else { mCoreConfigSetOverrideIntValue(&core->config, "useBios", 0); mCoreConfigSetOverrideIntValue(&core->config, "skipBios", 1); } /* Only these overrides: never the user's ~/.config/mgba settings. */ mCoreLoadForeignConfig(core, &core->config); if (!mCoreLoadFile(core, rom)) { fprintf(stderr, "cannot load %s\n", rom); return 1; } struct VFile* save = sram ? VFileOpen(sram, O_CREAT | O_TRUNC | O_RDWR) : VFileMemChunk(NULL, 0); core->loadSave(core, save); if (bios) { struct VFile* vf = VFileOpen(bios, O_RDONLY); if (!vf || !core->loadBIOS(core, vf, 0)) { fprintf(stderr, "cannot load BIOS %s\n", bios); return 1; } } core->reset(core); unsigned held = 0; for (unsigned frame = 0; frame < frames; ++frame) { for (int k = 0; k < nKeys; ++k) { if (keyFrame[k] == frame) { held = keyMask[k]; } } core->setKeys(core, held); core->runFrame(core); } printf("FRAMES %u\n", frames); for (int r = 0; r < nRegs; ++r) { int32_t value; if (core->readRegister(core, regs[r], &value)) { printf("REG %s %08X\n", regs[r], (unsigned) value); } else { printf("REG %s ?\n", regs[r]); } } for (int w = 0; w < nWords; ++w) { printf("WORD %08lX %08X\n", words[w], core->rawRead32(core, words[w], -1)); } if (png) { struct VFile* vf = VFileOpen(png, O_CREAT | O_TRUNC | O_WRONLY); png_structp p = PNGWriteOpen(vf); png_infop info = PNGWriteHeader(p, width, height, mCOLOR_NATIVE); PNGWritePixels(p, width, height, width, video, mCOLOR_NATIVE); PNGWriteClose(p, info); vf->close(vf); } fflush(stdout); core->deinit(core); free(video); return 0; }