// SPDX-License-Identifier: GPL-3.0-or-later // A minimal headless driver for NanoBoyAdvance's core, the same command line // and output as scripts/mgba_driver.c so scripts/mgba_run.py can drive either: // // nba_driver ROM [--bios FILE] [--frames N] [--keys FRAME:MASK]... // [--png FILE] [--sram FILE] [--reg NAME]... [--word ADDRESS]... // // NanoBoyAdvance has no BIOS HLE: --bios is required. --word reads I/O only. #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace nba; namespace { struct Frame final : VideoDevice { std::vector pixels = std::vector(240 * 160); void Draw(u32* buffer) override { std::memcpy(pixels.data(), buffer, pixels.size() * 4); } }; std::vector ReadFile(char const* path) { std::ifstream stream{path, std::ios::binary}; return {std::istreambuf_iterator(stream), {}}; } // The save chip from the ROM's library string, as the Qt frontend does. std::unique_ptr MakeBackup(std::vector const& rom, std::filesystem::path const& save, CoreBase& core) { auto has = [&](std::string_view tag) { for (size_t i = 0; i + tag.size() <= rom.size(); i += 4) { if (std::memcmp(&rom[i], tag.data(), tag.size()) == 0) return true; } return false; }; if (has("FLASH1M_V")) return std::make_unique(save, FLASH::Size::SIZE_128K); if (has("FLASH_V") || has("FLASH512_V")) return std::make_unique(save, FLASH::Size::SIZE_64K); if (has("EEPROM_V")) return std::make_unique(save, EEPROM::Size::DETECT, core.GetScheduler()); if (has("SRAM_V") || has("SRAM_F_V")) return std::make_unique(save); return nullptr; } void WritePng(char const* path, std::vector const& pixels) { FILE* file = std::fopen(path, "wb"); png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); png_infop info = png_create_info_struct(png); png_init_io(png, file); png_set_IHDR(png, info, 240, 160, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_write_info(png, info); std::vector row(240 * 3); for (int y = 0; y < 160; ++y) { for (int x = 0; x < 240; ++x) { u32 argb = pixels[y * 240 + x]; row[x * 3] = argb >> 16; row[x * 3 + 1] = argb >> 8; row[x * 3 + 2] = argb; } png_write_row(png, row.data()); } png_write_end(png, nullptr); png_destroy_write_struct(&png, &info); std::fclose(file); } } // namespace int main(int argc, char** argv) { char const* rom_path = nullptr; char const* bios_path = nullptr; char const* png_path = nullptr; std::string sram_path; unsigned frames = 600; std::vector> keys; std::vector regs; std::vector words; for (int i = 1; i < argc; ++i) { std::string_view arg = argv[i]; char const* next = i + 1 < argc ? argv[i + 1] : nullptr; if (arg == "--bios" && next) bios_path = argv[++i]; else if (arg == "--frames" && next) frames = std::stoul(argv[++i]); else if (arg == "--keys" && next) { unsigned frame, mask; if (std::sscanf(argv[++i], "%u:%i", &frame, &mask) != 2) return 2; keys.emplace_back(frame, mask); } else if (arg == "--png" && next) png_path = argv[++i]; else if (arg == "--sram" && next) sram_path = argv[++i]; else if (arg == "--reg" && next) regs.emplace_back(argv[++i]); else if (arg == "--word" && next) words.push_back(std::stoul(argv[++i], nullptr, 0)); else if (arg[0] != '-' && !rom_path) rom_path = argv[i]; else { std::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_path || !bios_path) { std::fprintf(stderr, "a ROM and --bios are required (NanoBoyAdvance has no BIOS HLE)\n"); return 2; } auto config = std::make_shared(); auto frame = std::make_shared(); config->video_dev = frame; auto core = CreateCore(config); core->Attach(ReadFile(bios_path)); auto rom = ReadFile(rom_path); // A fresh save every run unless --sram names one. auto save = sram_path.empty() ? std::filesystem::temp_directory_path() / ("nba_driver_" + std::to_string(getpid()) + ".sav") : std::filesystem::path{sram_path}; if (sram_path.empty()) std::filesystem::remove(save); auto backup = MakeBackup(rom, save, *core); u32 mask = 1; while (mask < rom.size()) mask <<= 1; core->Attach(ROM{std::move(rom), std::move(backup), nullptr, mask - 1}); core->Reset(); unsigned held = 0; for (unsigned f = 0; f < frames; ++f) { for (auto [at, value] : keys) { if (at == f) held = value; } for (int k = 0; k < 10; ++k) core->SetKeyStatus(static_cast(k), held >> k & 1); core->RunForOneFrame(); } std::printf("FRAMES %u\n", frames); auto state = std::make_unique(); core->CopyState(*state); for (auto const& name : regs) { if (name.size() > 1 && name[0] == 'r') { std::printf("REG %s %08X\n", name.c_str(), state->arm.regs.gpr[std::stoi(name.substr(1))]); } else if (name == "pc") { std::printf("REG pc %08X\n", state->arm.regs.gpr[15]); } else { std::printf("REG %s ?\n", name.c_str()); } } for (auto address : words) { std::printf("WORD %08lX %08X\n", address, core->PeekWordIO(address)); } if (png_path) WritePng(png_path, frame->pixels); if (sram_path.empty()) std::filesystem::remove(save); return 0; }