// Opt-in ROM diagnosis. No ROM data is compiled into this program or the APK. #include #include #include #include #include #include #include #include #include #include #include "rom_compatibility.h" #include "sha256.h" #include "video_conversion.h" #include "video_viewport.h" namespace { #ifdef PROBE_NESTOPIA constexpr auto core = neslite::CoreKind::Nestopia; #else constexpr auto core = neslite::CoreKind::Fceumm; #endif neslite::RomCompatibility profile = neslite::RomCompatibility::Default; std::map variables; std::string directory, output; unsigned frame = 0, pixel_format = 0, video_count = 0, changes = 0; std::uint64_t nonzero_audio = 0; std::uint32_t video_hash = 0; std::vector pixels; unsigned width = 0, height = 0; retro_system_av_info av{}; void logger(enum retro_log_level, const char* format, ...) { va_list args; va_start(args, format); vfprintf(stderr, format, args); va_end(args); } bool environment(unsigned command, void* data) { switch (command) { case RETRO_ENVIRONMENT_GET_CAN_DUPE: *static_cast(data) = true; return true; case RETRO_ENVIRONMENT_GET_INPUT_BITMASKS: return true; case RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY: case RETRO_ENVIRONMENT_GET_CONTENT_DIRECTORY: case RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY: *static_cast(data) = directory.c_str(); return true; case RETRO_ENVIRONMENT_SET_PIXEL_FORMAT: pixel_format = *static_cast(data); return pixel_format <= 2; case RETRO_ENVIRONMENT_GET_CORE_OPTIONS_VERSION: *static_cast(data) = 0; return true; case RETRO_ENVIRONMENT_SET_VARIABLES: { for (auto* v = static_cast(data); v && v->key; ++v) { std::string value = v->value ? v->value : ""; const auto separator = value.find(';'); if (separator != std::string::npos) value.erase(0, separator + 1); value.erase(0, value.find_first_not_of(' ')); variables[v->key] = value.substr(0, value.find('|')); if (const auto override = neslite::compatibility_core_option(profile, core, v->key)) { variables[v->key] = *override; } } return true; } case RETRO_ENVIRONMENT_GET_VARIABLE: { auto* v = static_cast(data); const auto it = variables.find(v->key ? v->key : ""); v->value = it == variables.end() ? nullptr : it->second.c_str(); return it != variables.end(); } case RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE: *static_cast(data) = false; return true; case RETRO_ENVIRONMENT_SET_VARIABLE: { const auto* v = static_cast(data); if (!v || !v->key || !v->value) return false; variables[v->key] = v->value; return true; } case RETRO_ENVIRONMENT_GET_LOG_INTERFACE: static_cast(data)->log = logger; return true; case RETRO_ENVIRONMENT_GET_LANGUAGE: *static_cast(data) = RETRO_LANGUAGE_ENGLISH; return true; case RETRO_ENVIRONMENT_GET_MESSAGE_INTERFACE_VERSION: *static_cast(data) = 1; return true; case RETRO_ENVIRONMENT_GET_TARGET_REFRESH_RATE: *static_cast(data) = 60; return true; case RETRO_ENVIRONMENT_GET_TARGET_SAMPLE_RATE: *static_cast(data) = 48000; return true; case RETRO_ENVIRONMENT_SET_GEOMETRY: av.geometry = *static_cast(data); return true; case RETRO_ENVIRONMENT_SET_SYSTEM_AV_INFO: av = *static_cast(data); return true; case RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL: case RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS: case RETRO_ENVIRONMENT_SET_CONTROLLER_INFO: case RETRO_ENVIRONMENT_SET_CONTENT_INFO_OVERRIDE: case RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS: case RETRO_ENVIRONMENT_SET_MEMORY_MAPS: case RETRO_ENVIRONMENT_SET_MESSAGE: case RETRO_ENVIRONMENT_SET_MESSAGE_EXT: return true; default: return false; } } void dump_frame(unsigned number) { if (pixels.empty()) return; std::ofstream file(output + "-" + std::to_string(number) + ".ppm", std::ios::binary); file << "P6\n" << width << " " << height << "\n255\n"; for (const auto pixel : pixels) { const char rgb[] = {static_cast(((pixel >> 11) & 31) * 255 / 31), static_cast(((pixel >> 5) & 63) * 255 / 63), static_cast((pixel & 31) * 255 / 31)}; file.write(rgb, 3); } if (!file) throw std::runtime_error("Could not save diagnostic frame"); } void video(const void* data, unsigned w, unsigned h, size_t pitch) { if (!data) return; if (!neslite::convert_to_rgb565(data, w, h, pitch, pixel_format, &pixels)) { throw std::runtime_error("Unsupported or invalid video frame"); } ++video_count; width = w; height = h; std::uint32_t hash = 2166136261u; for (const auto pixel : pixels) hash = (hash ^ pixel) * 16777619u; if (hash != video_hash) ++changes; video_hash = hash; if (frame == 179 || frame == 599 || frame == 1199) { std::cout << "frame=" << frame << " colors=" << std::set(pixels.begin(), pixels.end()).size() << " hash=" << video_hash << '\n'; dump_frame(frame); } } void audio(int16_t left, int16_t right) { if (left || right) ++nonzero_audio; } size_t batch(const int16_t* data, size_t count) { for (size_t i = 0; i < count; ++i) audio(data[i * 2], data[i * 2 + 1]); return count; } void poll() {} int16_t input(unsigned port, unsigned, unsigned, unsigned id) { unsigned mask = 0; if (port == 0) { if ((frame >= 180 && frame < 183) || (frame >= 420 && frame < 423)) mask = 1u << RETRO_DEVICE_ID_JOYPAD_START; if (frame >= 720 && frame < 1623 && (frame - 720) % 180 < 3) mask = 1u << RETRO_DEVICE_ID_JOYPAD_A; } return id == RETRO_DEVICE_ID_JOYPAD_MASK ? mask : id < 16 ? (mask >> id) & 1 : 0; } } // namespace int main(int argc, char** argv) { if (argc < 3 || argc > 5) { std::cerr << "Usage: probe_ ASCII_ROM_PATH OUTPUT_PREFIX [frames:60..3600] [original-defaults]\n"; return 2; } const unsigned frames = argc > 3 ? static_cast(std::stoul(argv[3])) : 1800; if (frames < 60 || frames > 3600) return 2; const std::filesystem::path rom_path = argv[1]; const auto size = std::filesystem::file_size(rom_path); if (size < 16 || size > 64U * 1024U * 1024U) return 3; std::ifstream file(rom_path, std::ios::binary); std::vector rom(size); if (!file.read(reinterpret_cast(rom.data()), static_cast(size))) return 3; const auto hash = neslite::sha256_hex(rom); if (argc < 5 || std::string(argv[4]) != "original-defaults") profile = neslite::rom_compatibility_for(hash); output = argv[2]; directory = std::filesystem::absolute(std::filesystem::path(output).parent_path()).string(); std::filesystem::create_directories(directory); std::cout << "sha256=" << hash << " profile=" << neslite::compatibility_profile_name(profile) << '\n'; retro_set_environment(environment); retro_set_video_refresh(video); retro_set_audio_sample(audio); retro_set_audio_sample_batch(batch); retro_set_input_poll(poll); retro_set_input_state(input); retro_init(); retro_set_controller_port_device(0, RETRO_DEVICE_JOYPAD); retro_game_info game{argv[1], rom.data(), rom.size(), nullptr}; if (!retro_load_game(&game)) { retro_deinit(); return 4; } retro_get_system_av_info(&av); std::cout << "fps=" << av.timing.fps << " format=" << pixel_format << " battery=" << retro_get_memory_size(RETRO_MEMORY_SAVE_RAM) << '\n'; for (frame = 0; frame < frames; ++frame) retro_run(); dump_frame(frames); std::cout << "frames=" << video_count << " changes=" << changes << " audio_nonzero=" << nonzero_audio << '\n'; const auto viewport = neslite::calculate_video_viewport(1664, 1248, av.geometry.base_width, av.geometry.base_height, true); std::cout << "texture=" << width << 'x' << height << " base=" << av.geometry.base_width << 'x' << av.geometry.base_height << " integer_viewport=" << viewport.width << 'x' << viewport.height << " at=" << viewport.x << ',' << viewport.y << " surface=1664x1248\n"; // One deterministic core-state round trip; this is not an Android save-file test. std::vector state(retro_serialize_size()); bool roundtrip = !state.empty() && retro_serialize(state.data(), state.size()); if (roundtrip) { for (frame = frames; frame < frames + 30; ++frame) retro_run(); const auto expected = video_hash; roundtrip = retro_unserialize(state.data(), state.size()); if (roundtrip) { for (frame = frames; frame < frames + 30; ++frame) retro_run(); roundtrip = expected == video_hash; } } std::cout << "core_state_roundtrip=" << (roundtrip ? "pass" : "fail") << '\n'; retro_unload_game(); retro_deinit(); return roundtrip ? 0 : 5; }