// A per-instruction trace of NanoBoyAdvance 1.8.3, for scripts/nba_lockstep.py: // // nba_trace ROM --bios FILE [--skip N] [--count N] [--skip-bios] [--keys FRAME:MASK]... [--regs] // // Prints one line per executed instruction, " " in hex, where is the // scheduler's clock when the instruction starts (after an IRQ entry, if one is taken first) and // the instruction's address + 8 (ARM) or + 4 (Thumb), as the core's R15 reads then. The // first --skip instructions are run but not printed. With --regs each line also carries R0-R14 // (the current mode's view), so a value that differs without changing the timing shows up. It replays NBA's Core::Run loop (core.cpp) // one instruction at a time, which needs the core's private members: the reference tree is not // modified, the members are made reachable in this translation unit only. // The standard library and NBA's public headers first, so the access trick below reaches only // the core's own classes. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define private public #include "../../reference/src/NanoBoyAdvance/src/nba/src/core.hpp" #undef private using namespace nba; namespace { struct NullVideo final : VideoDevice { void Draw(u32*) override {} }; std::vector ReadFile(char const* path) { std::ifstream stream{path, std::ios::binary}; return {std::istreambuf_iterator(stream), {}}; } 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; } } // namespace int main(int argc, char** argv) { char const* rom_path = nullptr; char const* bios_path = nullptr; unsigned long long skip = 0, count = 1000000; std::vector> keys; bool regs = false; 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 == "--skip" && next) skip = std::stoull(argv[++i]); else if (arg == "--count" && next) count = std::stoull(argv[++i]); else if (arg == "--regs") regs = true; else if (arg == "--skip-bios") skip_bios = true; 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[0] != '-' && !rom_path) rom_path = argv[i]; else return 2; } if (!rom_path || !bios_path) { std::fprintf(stderr, "usage: nba_trace ROM --bios FILE [--skip N] [--count N]\n"); return 2; } auto config = std::make_shared(); config->video_dev = std::make_shared(); config->skip_bios = skip_bios; // NBA's SkipBootScreen, as the board's skip_bios auto base = CreateCore(config); auto& core = static_cast(*base); core.Attach(ReadFile(bios_path)); auto rom = ReadFile(rom_path); auto save = std::filesystem::temp_directory_path() / ("nba_trace_" + std::to_string(getpid()) + ".sav"); 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(); using HaltControl = core::Bus::Hardware::HaltControl; auto& scheduler = core.scheduler; auto& cpu = core.cpu; auto& bus = core.bus; unsigned long long executed = 0; unsigned frame = 0; u64 next_frame = 280896; unsigned held = 0; auto apply_keys = [&] { for (auto [at, value] : keys) { if (at == frame) held = value; } for (int k = 0; k < 10; ++k) core.SetKeyStatus(static_cast(k), held >> k & 1); }; apply_keys(); while (executed < skip + count) { if (scheduler.GetTimestampNow() >= next_frame) { ++frame; next_frame += 280896; apply_keys(); } if (bus.hw.haltcnt == HaltControl::Run) { if (cpu.IRQLine()) cpu.SignalIRQ(); if (executed >= skip) { std::printf("%llx %x", (unsigned long long)scheduler.GetTimestampNow(), cpu.state.r15); if (regs) { for (int r = 0; r < 15; ++r) std::printf(" %x", cpu.state.reg[r]); } std::printf("\n"); } ++executed; // Core::Run's instruction step, with the IRQ already taken above. auto instruction = cpu.pipe.opcode[0]; cpu.latch_irq_disable = cpu.state.cpsr.f.mask_irq; cpu.state.r15 &= ~1; if (cpu.state.cpsr.f.thumb) { cpu.pipe.opcode[0] = cpu.pipe.opcode[1]; cpu.pipe.opcode[1] = cpu.ReadHalf(cpu.state.r15, cpu.pipe.access); (cpu.*cpu.s_opcode_lut_16[instruction >> 6])(instruction); } else { cpu.pipe.opcode[0] = cpu.pipe.opcode[1]; cpu.pipe.opcode[1] = cpu.ReadWord(cpu.state.r15, cpu.pipe.access); if (cpu.CheckCondition(static_cast(instruction >> 28))) { int hash = ((instruction >> 16) & 0xFF0) | ((instruction >> 4) & 0x00F); (cpu.*cpu.s_opcode_lut_32[hash])(instruction); } else { cpu.pipe.access = core::Bus::Access::Code | core::Bus::Access::Sequential; cpu.state.r15 += 4; } } } else { while (!core.irq.ShouldUnhaltCPU()) { if (core.dma.IsRunning()) { core.dma.Run(); if (core.irq.ShouldUnhaltCPU()) continue; } bus.Step(scheduler.GetRemainingCycleCount()); } bus.Step(1); bus.hw.haltcnt = HaltControl::Run; } } std::filesystem::remove(save); return 0; }