// 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]... // [--wav FILE] [--raw FILE [--raw-from N]] // // NanoBoyAdvance has no BIOS HLE: --bios is required. --word reads I/O only. // // --wav writes the run's sound, 16-bit stereo at 32,768 Hz: the mixer's own rate, so its // resampler passes each sample through and the file can be compared sample for sample with // the board's (scripts/sound_diff.py). // // --raw appends every picture from frame N on (default 1) to FILE as 240 x 160 RGB bytes: // scripts/first_frame.py finds the first picture that differs from the board's. // // A frame is one picture: the run stops just after the PPU's Nth 227 -> 0 wrap (its Nth Draw), // as the board's GBA.run_frame and mGBA's runFrame do. Core::RunForOneFrame would not do: // it runs 280,896 cycles from wherever the last call overran, so the boundary drifts later // every frame and after ~650 frames a run holds one more picture than its count. #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); unsigned draws = 0; void Draw(u32* buffer) override { std::memcpy(pixels.data(), buffer, pixels.size() * 4); ++draws; } }; // The sound NanoBoyAdvance would hand a sound card, pulled by the driver instead: the core // fills a ring buffer and the device's callback drains it. The driver asks for exactly the // samples the mixer has made (one per 512 cycles), less a margin for the resampler's delay, // because the callback repeats old samples when asked for more than there are. struct Sound final : AudioDevice { void* userdata = nullptr; Callback callback = nullptr; std::vector samples; // interleaved left, right u64 pulled = 0; auto GetSampleRate() -> int override { return 32768; } auto GetBlockSize() -> int override { return 4096; } bool Open(void* data, Callback function) override { userdata = data; callback = function; return true; } void SetPause(bool) override {} void Close() override {} void Pull(u64 cycles, u64 margin) { u64 due = cycles / 512; if (!callback || due <= pulled + margin) return; size_t count = due - margin - pulled; size_t at = samples.size(); samples.resize(at + count * 2); callback(userdata, samples.data() + at, static_cast(count * 4)); pulled += count; } }; void WriteWav(char const* path, std::vector const& samples) { FILE* file = std::fopen(path, "wb"); u32 bytes = static_cast(samples.size() * 2); auto put32 = [&](u32 v) { std::fwrite(&v, 4, 1, file); }; auto put16 = [&](u16 v) { std::fwrite(&v, 2, 1, file); }; std::fwrite("RIFF", 4, 1, file); put32(36 + bytes); std::fwrite("WAVEfmt ", 8, 1, file); put32(16); put16(1); // PCM put16(2); put32(32768); put32(32768 * 4); put16(4); put16(16); std::fwrite("data", 4, 1, file); put32(bytes); std::fwrite(samples.data(), 2, samples.size(), file); std::fclose(file); } 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; char const* wav_path = nullptr; char const* raw_path = nullptr; unsigned raw_from = 1; std::string sram_path; unsigned frames = 600; std::vector> keys; std::vector regs; std::vector words; bool skip_bios = false; 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 == "--wav" && next) wav_path = argv[++i]; else if (arg == "--raw" && next) raw_path = argv[++i]; else if (arg == "--raw-from" && next) raw_from = std::stoul(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 == "--skip-bios") skip_bios = true; 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] [--wav FILE] [--sram FILE] [--reg NAME]... [--word ADDRESS]... [--skip-bios]\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 sound = std::make_shared(); if (wav_path) config->audio_dev = sound; config->skip_bios = skip_bios; // NBA's SkipBootScreen, as the board's skip_bios (smoke runs) 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(); FILE* raw = raw_path ? std::fopen(raw_path, "wb") : nullptr; std::vector rgb(240 * 160 * 3); 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); // A line at a time until the next picture is out (the board's run_frame). for (unsigned target = frame->draws + 1; frame->draws < target;) { core->Run(1232); sound->Pull(core->GetScheduler().GetTimestampNow(), 64); } if (raw && f + 1 >= raw_from) { for (int i = 0; i < 240 * 160; ++i) { u32 argb = frame->pixels[i]; rgb[i * 3] = argb >> 16; rgb[i * 3 + 1] = argb >> 8; rgb[i * 3 + 2] = argb; } std::fwrite(rgb.data(), 1, rgb.size(), raw); } } sound->Pull(core->GetScheduler().GetTimestampNow(), 8); if (raw) std::fclose(raw); if (wav_path) WriteWav(wav_path, sound->samples); 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; }