// SPDX-License-Identifier: GPL-3.0-or-later // Links NanoBoyAdvance (Copyright (C) fleroviux, GPL-3.0-or-later): a closed-box referee tool, // built locally and never distributed; see referees/README.md. // Run NanoBoyAdvance's ARM7TDMI on SingleStepTests ARM7TDMI cases and score the agreement. // // The corpus was generated from a fork of NanoBoyAdvance, so this measures how // far today's NBA has drifted from the corpus and, more usefully, records what // the corpus leaves out: NBA calls Bus::Idle() for every internal (I) cycle, so // this referee prints the full bus sequence with I cycles in chip order. // // nba_sst FILE.json.bin [--dump N] [--seq N] // // prints one JSON object: cases, state agreement (registers, CPSR, SPSRs, the // banks not currently mapped), bus agreement (kind, size, address, write data, // N/S and code bits, in order, ignoring the corpus's experimental cycle field), // and a histogram of I cycles per instruction. --dump N prints the first N // disagreements to stderr; --seq N prints N cases' full sequences to stderr. // // Built by scripts/build_referees.py against the pinned NBA tree. This file is // MIT like the rest of the repository; the binary it builds links GPL-3.0 NBA // code and is never distributed. #include #include #include #include #include #include #include #define private public #include "arm/arm7tdmi.hh" #undef private using namespace nba; using namespace nba::core; using namespace nba::core::arm; namespace { struct Tx { u32 kind, size, addr, data, cycle, access; }; struct State { u32 v[40]; // R[16], R_fiq[7], svc/abt/irq/und R13-R14, CPSR, SPSR[5], pipeline[2], access }; struct Case { State initial, final; std::vector txs; u32 opcode, base; }; // What the core did, in order; kind 3 is an internal cycle. std::vector g_seen; const Case* g_case; std::vector g_used; long g_unmatched_reads; u32 lookup(u32 kind, u32 address) { for(size_t i = 0; i < g_case->txs.size(); i++) { auto const& t = g_case->txs[i]; if(!g_used[i] && t.kind == kind && t.addr == address) { g_used[i] = true; return t.data; } } g_unmatched_reads++; return kind == 0 ? address : 0; } u32 read(u32 address, int access, u32 size) { u32 kind = (access & Bus::Code) ? 0 : 1; u32 data = lookup(kind, address); if(size == 1) data &= 0xFF; if(size == 2) data &= 0xFFFF; g_seen.push_back({kind, size, address, data, 0, (u32)access}); return data; } void write(u32 address, u32 value, int access, u32 size) { g_seen.push_back({2, size, address, value, 0, (u32)access}); } Scheduler g_scheduler; } // namespace namespace nba::core { auto Bus::ReadByte(u32 a, int acc) -> u8 { return read(a, acc, 1); } auto Bus::ReadHalf(u32 a, int acc) -> u16 { return read(a, acc, 2); } auto Bus::ReadWord(u32 a, int acc) -> u32 { return read(a, acc, 4); } void Bus::WriteByte(u32 a, u8 v, int acc) { write(a, v, acc, 1); } void Bus::WriteHalf(u32 a, u16 v, int acc) { write(a, v, acc, 2); } void Bus::WriteWord(u32 a, u32 v, int acc) { write(a, v, acc, 4); } void Bus::Idle() { g_seen.push_back({3, 0, 0, 0, 0, 0}); } } // namespace nba::core #include "arm/tablegen/tablegen.cc" namespace { std::vector load(const char* path) { std::ifstream in(path, std::ios::binary); std::vector buf((std::istreambuf_iterator(in)), {}); auto u32at = [&](size_t p) { u32 v; std::memcpy(&v, &buf[p], 4); return v; }; if(buf.size() < 8 || u32at(0) != 0xD33DBAE0) { std::fprintf(stderr, "%s: not a SingleStepTests ARM7TDMI file\n", path); std::exit(1); } std::vector cases(u32at(4)); size_t ptr = 8; for(auto& c : cases) { size_t full = u32at(ptr), p = ptr + 4; for(State* s : {&c.initial, &c.final}) { size_t size = u32at(p); std::memcpy(s->v, &buf[p + 8], sizeof s->v); p += size; } size_t size = u32at(p), n = u32at(p + 8); c.txs.resize(n); std::memcpy(c.txs.data(), &buf[p + 12], n * sizeof(Tx)); p += size; c.opcode = u32at(p + 8); c.base = u32at(p + 12); ptr += full; } return cases; } const int kBankOf[] = {BANK_FIQ, BANK_SVC, BANK_ABT, BANK_IRQ, BANK_UND}; // Corpus layout (measured, not documented): R8-R14 in R are always the // user/system bank, and R_fiq, R_svc, ... always hold their own mode's // registers, whatever the current mode is. NBA keeps the current mode's // registers in reg[] and the others in bank[], so put() and get() translate. void put(ARM7TDMI& cpu, State const& s) { auto& st = cpu.state; for(int i = 0; i < 7; i++) st.bank[BANK_FIQ][i] = s.v[16 + i]; for(int b = 1; b < 5; b++) { st.bank[kBankOf[b]][5] = s.v[23 + 2 * (b - 1)]; st.bank[kBankOf[b]][6] = s.v[24 + 2 * (b - 1)]; } for(int i = 0; i < 7; i++) st.bank[BANK_NONE][i] = s.v[8 + i]; st.cpsr.v = s.v[31]; for(int b = 0; b < 5; b++) st.spsr[kBankOf[b]].v = s.v[32 + b]; // R is the current mode's view; point the SPSR at the current bank. auto mode = static_cast(s.v[31] & 0x1F); auto bank = cpu.GetRegisterBankByMode(mode); cpu.p_spsr = (bank == BANK_NONE || bank == BANK_INVALID) ? &st.cpsr : &st.spsr[bank]; cpu.cpu_mode_is_invalid = bank == BANK_INVALID; for(int i = 0; i < 16; i++) st.reg[i] = s.v[i]; if(bank == BANK_FIQ) for(int i = 0; i < 7; i++) st.reg[8 + i] = st.bank[BANK_FIQ][i]; else if(bank != BANK_NONE && bank != BANK_INVALID) for(int i = 5; i < 7; i++) st.reg[8 + i] = st.bank[bank][i]; cpu.pipe.opcode[0] = s.v[37]; cpu.pipe.opcode[1] = s.v[38]; cpu.pipe.access = s.v[39]; cpu.irq_line = false; cpu.latch_irq_disable = st.cpsr.f.mask_irq; cpu.ldm_usermode_conflict = false; } void get(ARM7TDMI& cpu, State& s) { auto& st = cpu.state; auto bank = cpu.GetRegisterBankByMode(static_cast(st.cpsr.v & 0x1F)); u32 saved[BANK_COUNT][7]; std::memcpy(saved, st.bank, sizeof saved); // Write the mapped registers back into their bank, as SwitchMode would. if(bank == BANK_FIQ) for(int i = 0; i < 7; i++) saved[BANK_FIQ][i] = st.reg[8 + i]; else if(bank != BANK_INVALID) for(int i = 5; i < 7; i++) saved[bank][i] = st.reg[8 + i]; if(bank != BANK_FIQ && bank != BANK_INVALID) for(int i = 0; i < 5; i++) saved[BANK_NONE][i] = st.reg[8 + i]; for(int i = 0; i < 8; i++) s.v[i] = st.reg[i]; for(int i = 8; i < 15; i++) s.v[i] = saved[BANK_NONE][i - 8]; s.v[15] = st.reg[15]; for(int i = 0; i < 7; i++) s.v[16 + i] = saved[BANK_FIQ][i]; for(int b = 1; b < 5; b++) { s.v[23 + 2 * (b - 1)] = saved[kBankOf[b]][5]; s.v[24 + 2 * (b - 1)] = saved[kBankOf[b]][6]; } s.v[31] = st.cpsr.v; for(int b = 0; b < 5; b++) s.v[32 + b] = st.spsr[kBankOf[b]].v; s.v[37] = cpu.pipe.opcode[0]; s.v[38] = cpu.pipe.opcode[1]; s.v[39] = cpu.pipe.access; } // Which state words are compared: all of them, since every bank is stored. std::vector fields(u32 cpsr) { std::vector f; for(int i = 0; i < 16; i++) f.push_back(i); (void)cpsr; for(int i = 16; i < 31; i++) f.push_back(i); f.push_back(31); for(int b = 0; b < 5; b++) f.push_back(32 + b); f.push_back(37); f.push_back(38); f.push_back(39); return f; } const char* kName[40] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "fiq_r8", "fiq_r9", "fiq_r10", "fiq_r11", "fiq_r12", "fiq_r13", "fiq_r14", "svc_r13", "svc_r14", "abt_r13", "abt_r14", "irq_r13", "irq_r14", "und_r13", "und_r14", "cpsr", "spsr_fiq", "spsr_svc", "spsr_abt", "spsr_irq", "spsr_und", "pipe0", "pipe1", "access"}; void print_seq(FILE* out, std::vector const& seq) { for(auto const& t : seq) { if(t.kind == 3) { std::fprintf(out, " I"); continue; } std::fprintf(out, " %s%s%u@%08x", t.kind == 0 ? "C" : t.kind == 1 ? "R" : "W", (t.access & 1) ? "S" : "N", t.size * 8, t.addr); } std::fprintf(out, "\n"); } } // namespace int main(int argc, char** argv) { if(argc < 2) { std::fprintf(stderr, "usage: nba_sst FILE.json.bin [--dump N] [--seq N]\n"); return 2; } int dump = 0, seq = 0; for(int i = 2; i + 1 < argc; i += 2) { if(!std::strcmp(argv[i], "--dump")) dump = std::atoi(argv[i + 1]); if(!std::strcmp(argv[i], "--seq")) seq = std::atoi(argv[i + 1]); } auto cases = load(argv[1]); Bus bus; ARM7TDMI cpu(g_scheduler, bus); long state_ok = 0, bus_ok = 0, both_ok = 0, order_ok = 0, ns_ok = 0; std::map field_bad; std::map idle_hist; for(size_t n = 0; n < cases.size(); n++) { auto const& c = cases[n]; g_case = &c; g_used.assign(c.txs.size(), false); g_seen.clear(); put(cpu, c.initial); cpu.Run(); cpu.ldm_usermode_conflict = false; State out; get(cpu, out); bool state_same = true; for(int f : fields(c.final.v[31])) { if(out.v[f] != c.final.v[f]) { state_same = false; field_bad[kName[f]]++; } } std::vector mem; int idles = 0; for(auto const& t : g_seen) (t.kind == 3 ? idles++ : (mem.push_back(t), 0)); idle_hist[idles]++; bool same_order = mem.size() == c.txs.size(); bool same_ns = same_order; bool bus_same = same_order; for(size_t i = 0; same_order && i < mem.size(); i++) { auto const& a = mem[i]; auto const& b = c.txs[i]; if(a.kind != b.kind || a.size != b.size || a.addr != b.addr) { same_order = same_ns = bus_same = false; break; } if((a.access & 3) != (b.access & 3)) same_ns = false; if(a.kind == 2 && a.data != b.data) bus_same = false; if((a.access & 0xB) != (b.access & 0xB)) bus_same = false; } state_ok += state_same; bus_ok += bus_same; order_ok += same_order; ns_ok += same_ns; both_ok += state_same && bus_same; if(dump > 0 && !(state_same && bus_same)) { dump--; std::fprintf(stderr, "case %zu opcode %08x cpsr %08x:", n, c.opcode, c.initial.v[31]); for(int f : fields(c.final.v[31])) if(out.v[f] != c.final.v[f]) std::fprintf(stderr, " %s nba=%08x sst=%08x", kName[f], out.v[f], c.final.v[f]); std::fprintf(stderr, "\n nba:"); print_seq(stderr, g_seen); std::fprintf(stderr, " sst:"); print_seq(stderr, c.txs); } if(n < (size_t)seq) { std::fprintf(stderr, "%08x cpsr %08x:", c.opcode, c.initial.v[31]); print_seq(stderr, g_seen); } } std::printf("{\"cases\": %zu, \"state\": %ld, \"bus\": %ld, \"bus_order\": %ld, \"bus_ns\": %ld, \"both\": %ld, " "\"unmatched_reads\": %ld, \"fields\": {", cases.size(), state_ok, bus_ok, order_ok, ns_ok, both_ok, g_unmatched_reads); bool first = true; for(auto const& [k, v] : field_bad) { std::printf("%s\"%s\": %ld", first ? "" : ", ", k.c_str(), v); first = false; } std::printf("}, \"idle_cycles\": {"); first = true; for(auto const& [k, v] : idle_hist) { std::printf("%s\"%d\": %ld", first ? "" : ", ", k, v); first = false; } std::printf("}}\n"); return 0; }