// bozo-runtime-tests: the runtime core (bozo/runtime.h): the guest addresses with memory behind them, and the // function table with its overrides looked up first. #include "bozo/runtime.h" #include "bozo/ops_fpu.h" #include #include namespace { int g_failures = 0; void eq(const char *what, long long got, long long want) { if (got == want) return; ++g_failures; std::printf("FAIL %s: 0x%llx, expected 0x%llx\n", what, got, want); } int g_calls[3] = {}; void fnA(uint8_t *, R5900Context *, Runtime *) { ++g_calls[0]; } void fnB(uint8_t *, R5900Context *, Runtime *) { ++g_calls[1]; } uint32_t g_overridePc = 0; void over(uint8_t *, R5900Context *ctx, Runtime *) { ++g_calls[2]; g_overridePc = ctx->pc; } struct NoServices final : bozo::RuntimeServices { uint32_t unknown = 0; void unknownTarget(uint8_t *, R5900Context *, Runtime *, uint32_t t) override { unknown = t; } void yield(uint8_t *, R5900Context *, Runtime *) override {} void syscall(uint8_t *, R5900Context *, Runtime *, uint32_t) override {} void breakpoint(uint8_t *, R5900Context *, Runtime *, uint32_t) override {} void trap(uint8_t *, R5900Context *, Runtime *) override {} void unimplemented(uint8_t *, R5900Context *, Runtime *, uint32_t) override {} void eret(uint8_t *, R5900Context *, Runtime *) override {} uint32_t cop0Read(R5900Context *, Runtime *, uint32_t) override { return 0; } void cop0Write(R5900Context *, Runtime *, uint32_t, uint32_t) override {} bool cop0Condition(R5900Context *, Runtime *) override { return false; } void vu0CallMicroprogram(uint8_t *, R5900Context *, Runtime *, uint32_t) override {} uint32_t vu0ControlRead(R5900Context *, Runtime *, uint32_t) override { return 0; } void vu0ControlWrite(R5900Context *, Runtime *, uint32_t, uint32_t) override {} void ioRead(R5900Context *, Runtime *, uint32_t, uint32_t, uint64_t v[2]) override { v[0] = v[1] = 0; } void ioWrite(R5900Context *, Runtime *, uint32_t, uint32_t, const uint64_t[2]) override {} }; } int main() { using bozo::GuestMemory; // The flat space: RAM through every view, the scratchpad, VU0 data; nothing else. eq("RAM", GuestMemory::flatOffset(0x00123450u, 4), 0x123450); eq("KSEG0", GuestMemory::flatOffset(0x80123450u, 4), 0x123450); eq("KSEG1", GuestMemory::flatOffset(0xA0123450u, 4), 0x123450); eq("uncached", GuestMemory::flatOffset(0x20123450u, 4), 0x123450); eq("uncached accelerated", GuestMemory::flatOffset(0x30123450u, 4), 0x123450); eq("RAM end", GuestMemory::flatOffset(0x01FFFFFCu, 4), 0x1FFFFFC); eq("past RAM", GuestMemory::flatOffset(0x02000000u, 4), -1); eq("scratchpad", GuestMemory::flatOffset(0x70000010u, 16), 0x2000010); eq("scratchpad end", GuestMemory::flatOffset(0x70003FFCu, 8), -1); eq("VU0 data", GuestMemory::flatOffset(0x11004FF0u, 16), 0x2004FF0); eq("VU1 data (I/O)", GuestMemory::flatOffset(0x1100C000u, 4), -1); eq("a hardware register", GuestMemory::flatOffset(0x10009000u, 4), -1); eq("the BIOS", GuestMemory::flatOffset(0xBFC00000u, 4), -1); // Overrides first, then generated code, then the services. static const BozoFunctionEntry table[] = {{0x100000u, fnA}, {0x100100u, fnB}}; auto rt = std::make_unique(); NoServices services; rt->services = &services; rt->functions.setGenerated(table, 2); rt->functions.setOverride(0x100100u, over); R5900Context ctx; bozo_call_indirect(rt->memory.ram(), &ctx, rt.get(), 0x100000u); bozo_call_indirect(rt->memory.ram(), &ctx, rt.get(), 0x100100u); bozo_call_indirect(rt->memory.ram(), &ctx, rt.get(), 0x100200u); eq("generated", g_calls[0], 1); eq("overridden generated", g_calls[1], 0); eq("override", g_calls[2], 1); eq("override sees its address in pc", g_overridePc, 0x100100u); eq("unknown target to the services", services.unknown, 0x100200u); // Two statically compiled modules share a slot. Only the resident image may execute or resume. const BozoFunctionEntry modA[] = {{0x200000u, fnA}}; const BozoFunctionEntry modB[] = {{0x200000u, fnB}}; const BozoResumeEntry resumeA[] = {{0x200000u, 0x200100u, fnA}}; const BozoResumeEntry resumeB[] = {{0x200000u, 0x200100u, fnB}}; uint8_t resident[] = {1, 2}; rt->functions.addModule(0x200000, 0x200100, resident, {1, 2}, modA, 1, resumeA, 1); rt->functions.addModule(0x200000, 0x200100, resident, {3, 4}, modB, 1, resumeB, 1); eq("resident module A", rt->functions.generated(0x200000) == fnA, true); eq("resume module A", rt->functions.resumeEntry(0x200040) == &resumeA[0], true); resident[0] = 3; resident[1] = 4; eq("resident module B", rt->functions.generated(0x200000) == fnB, true); eq("resume module B", rt->functions.resumeEntry(0x200040) == &resumeB[0], true); resident[0] = 0; eq("unloaded module cannot execute", rt->functions.generated(0x200000) == nullptr, true); eq("unloaded module cannot resume", rt->functions.resumeEntry(0x200040) == nullptr, true); eq("module does not mask resident code", rt->functions.generated(0x100000) == fnA, true); rt->functions.clearModules(); // A module loaded into another's span (JC2: a battle effect at 0x96B000 over the tail of the Arena's script at // 0x96A800): where both are resident, the higher one owns its part, also right after the lower one was looked up. const BozoFunctionEntry lowFns[] = {{0x300000u, fnA}, {0x300140u, fnA}}; const BozoFunctionEntry highFns[] = {{0x300140u, fnB}}; uint8_t lowResident[] = {5, 6}, highResident[] = {7, 8}; rt->functions.addModule(0x300000, 0x300200, lowResident, {5, 6}, lowFns, 2, nullptr, 0); rt->functions.addModule(0x300100, 0x300180, highResident, {7, 8}, highFns, 1, nullptr, 0); eq("lower module's own part", rt->functions.generated(0x300000) == fnA, true); eq("higher module over the lower's tail", rt->functions.generated(0x300140) == fnB, true); highResident[0] = 0; eq("the lower module again once the higher is gone", rt->functions.generated(0x300140) == fnA, true); rt->functions.clearModules(); // ei / di toggle Status.EIE; mfc0 12 reads Status. bozo_set_interrupts(rt->memory.ram(), &ctx, rt.get(), true); eq("ei", bozo_cop0_read(&ctx, rt.get(), 12), 0x10000); bozo_set_interrupts(rt->memory.ram(), &ctx, rt.get(), false); eq("di", bozo_cop0_read(&ctx, rt.get(), 12), 0); // ps2f's host shortcut (normal operands and result) at the bottom of the normals: a product just below 2^-126 // rounds up to 2^-126 on the host; the PS2 flushes it to zero with the underflow flags. { const uint32_t cases[][4] = {{0x00800000u, 0x3f7fffffu, 0x00000000u, 0x4008u}, {0x00800000u, 0xbf7fffffu, 0x80000000u, 0x4008u}, {0x3effffffu, 0x01000000u, 0x00000000u, 0x4008u}, {0x3f800000u, 0x00800000u, 0x00800000u, 0u}, {0x3f000000u, 0x01000000u, 0x00800000u, 0u}}; for (const auto &c : cases) { uint32_t flags = 0; eq("ps2f mul at the smallest normal", bozo::ps2f::mul(c[0], c[1], flags), c[2]); eq("ps2f mul's underflow flags", flags, c[3]); } } std::printf("runtime tests: %s\n", g_failures ? "FAILED" : "passed"); return g_failures ? 1 : 0; }