|
| 1 | +/* test_cd_boot.c -- Minimal test harness for CD boot diagnostics. |
| 2 | + * Build: make -j4 && cc -o test/test_cd_boot test/test_cd_boot.c -L. -lvirtualjaguar_libretro -Wl,-rpath,. |
| 3 | + * Actually, just link against the dylib directly: |
| 4 | + * cc -o test/test_cd_boot test/test_cd_boot.c -ldl |
| 5 | + * Or use the simpler approach: include retro API and call it. */ |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <string.h> |
| 10 | +#include <dlfcn.h> |
| 11 | +#include <stdarg.h> |
| 12 | +#include <stdbool.h> |
| 13 | +#include "../libretro-common/include/libretro.h" |
| 14 | + |
| 15 | +/* Function pointers for the libretro API */ |
| 16 | +static void (*p_retro_init)(void); |
| 17 | +static void (*p_retro_deinit)(void); |
| 18 | +static void (*p_retro_set_environment)(retro_environment_t); |
| 19 | +static void (*p_retro_set_video_refresh)(retro_video_refresh_t); |
| 20 | +static void (*p_retro_set_audio_sample)(retro_audio_sample_t); |
| 21 | +static void (*p_retro_set_audio_sample_batch)(retro_audio_sample_batch_t); |
| 22 | +static void (*p_retro_set_input_poll)(retro_input_poll_t); |
| 23 | +static void (*p_retro_set_input_state)(retro_input_state_t); |
| 24 | +static bool (*p_retro_load_game)(const struct retro_game_info *); |
| 25 | +static void (*p_retro_unload_game)(void); |
| 26 | +static void (*p_retro_run)(void); |
| 27 | +static void (*p_retro_get_system_info)(struct retro_system_info *); |
| 28 | +static void (*p_retro_get_system_av_info)(struct retro_system_av_info *); |
| 29 | + |
| 30 | +static unsigned frame_count = 0; |
| 31 | +static uint32_t last_frame_hash = 0; |
| 32 | +static unsigned width_seen = 0, height_seen = 0; |
| 33 | +static bool got_video = false; |
| 34 | + |
| 35 | +static void video_refresh(const void *data, unsigned width, unsigned height, size_t pitch) |
| 36 | +{ |
| 37 | + if (!data) return; |
| 38 | + got_video = true; |
| 39 | + width_seen = width; |
| 40 | + height_seen = height; |
| 41 | + |
| 42 | + /* Simple hash of video buffer to detect changes */ |
| 43 | + const uint32_t *pixels = (const uint32_t *)data; |
| 44 | + uint32_t hash = 0; |
| 45 | + unsigned total = width * height; |
| 46 | + for (unsigned i = 0; i < total; i += 97) /* sample every 97th pixel */ |
| 47 | + hash = hash * 31 + pixels[i]; |
| 48 | + |
| 49 | + if (hash != last_frame_hash) |
| 50 | + { |
| 51 | + /* Check if frame is all black (or near-black) */ |
| 52 | + unsigned nonblack = 0; |
| 53 | + for (unsigned i = 0; i < total; i += 37) |
| 54 | + { |
| 55 | + uint32_t p = pixels[i] & 0x00FFFFFF; |
| 56 | + if (p > 0x010101) |
| 57 | + nonblack++; |
| 58 | + } |
| 59 | + printf(" Frame %u: %ux%u, hash=0x%08X, nonblack_samples=%u/%u\n", |
| 60 | + frame_count, width, height, hash, nonblack, total / 37); |
| 61 | + last_frame_hash = hash; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +static void audio_sample(int16_t left, int16_t right) { (void)left; (void)right; } |
| 66 | +static size_t audio_sample_batch(const int16_t *data, size_t frames) { (void)data; return frames; } |
| 67 | +static void input_poll(void) {} |
| 68 | +static int16_t input_state(unsigned port, unsigned device, unsigned index, unsigned id) |
| 69 | +{ |
| 70 | + (void)port; (void)device; (void)index; (void)id; |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +static void log_printf(enum retro_log_level level, const char *fmt, ...) |
| 75 | +{ |
| 76 | + va_list ap; |
| 77 | + const char *lvl_str[] = {"DEBUG", "INFO", "WARN", "ERROR"}; |
| 78 | + printf("[%s] ", lvl_str[level < 4 ? level : 3]); |
| 79 | + va_start(ap, fmt); |
| 80 | + vprintf(fmt, ap); |
| 81 | + va_end(ap); |
| 82 | +} |
| 83 | + |
| 84 | +static struct retro_log_callback log_cb = { log_printf }; |
| 85 | + |
| 86 | +static bool environment(unsigned cmd, void *data) |
| 87 | +{ |
| 88 | + switch (cmd) |
| 89 | + { |
| 90 | + case RETRO_ENVIRONMENT_GET_LOG_INTERFACE: |
| 91 | + *(struct retro_log_callback *)data = log_cb; |
| 92 | + return true; |
| 93 | + case RETRO_ENVIRONMENT_SET_PIXEL_FORMAT: |
| 94 | + return true; |
| 95 | + case RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY: |
| 96 | + /* Look for BIOS files in test/roms/private or current dir */ |
| 97 | + *(const char **)data = "test/roms/private"; |
| 98 | + return true; |
| 99 | + case RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY: |
| 100 | + *(const char **)data = "."; |
| 101 | + return true; |
| 102 | + case RETRO_ENVIRONMENT_SET_VARIABLES: |
| 103 | + case RETRO_ENVIRONMENT_SET_CORE_OPTIONS_V2: |
| 104 | + return true; |
| 105 | + case RETRO_ENVIRONMENT_GET_VARIABLE: |
| 106 | + { |
| 107 | + struct retro_variable *var = (struct retro_variable *)data; |
| 108 | + /* Force CD BIOS on */ |
| 109 | + if (var->key && strcmp(var->key, "virtualjaguar_bios") == 0) |
| 110 | + { |
| 111 | + var->value = "enabled"; |
| 112 | + return true; |
| 113 | + } |
| 114 | + if (var->key && strcmp(var->key, "virtualjaguar_usefastblitter") == 0) |
| 115 | + { |
| 116 | + var->value = "enabled"; |
| 117 | + return true; |
| 118 | + } |
| 119 | + var->value = NULL; |
| 120 | + return false; |
| 121 | + } |
| 122 | + case RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE: |
| 123 | + *(bool *)data = false; |
| 124 | + return true; |
| 125 | + default: |
| 126 | + return false; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +int main(int argc, char *argv[]) |
| 131 | +{ |
| 132 | + if (argc < 2) |
| 133 | + { |
| 134 | + fprintf(stderr, "Usage: %s <path-to-cue-or-chd> [num_frames]\n", argv[0]); |
| 135 | + return 1; |
| 136 | + } |
| 137 | + |
| 138 | + const char *image_path = argv[1]; |
| 139 | + unsigned num_frames = argc > 2 ? atoi(argv[2]) : 300; |
| 140 | + |
| 141 | + /* Load the core */ |
| 142 | + void *handle = dlopen("./virtualjaguar_libretro.dylib", RTLD_NOW); |
| 143 | + if (!handle) |
| 144 | + { |
| 145 | + fprintf(stderr, "Failed to load core: %s\n", dlerror()); |
| 146 | + return 1; |
| 147 | + } |
| 148 | + |
| 149 | +#define LOAD_SYM(sym) do { \ |
| 150 | + p_##sym = dlsym(handle, #sym); \ |
| 151 | + if (!p_##sym) { fprintf(stderr, "Missing symbol: %s\n", #sym); return 1; } \ |
| 152 | +} while(0) |
| 153 | + |
| 154 | + LOAD_SYM(retro_init); |
| 155 | + LOAD_SYM(retro_deinit); |
| 156 | + LOAD_SYM(retro_set_environment); |
| 157 | + LOAD_SYM(retro_set_video_refresh); |
| 158 | + LOAD_SYM(retro_set_audio_sample); |
| 159 | + LOAD_SYM(retro_set_audio_sample_batch); |
| 160 | + LOAD_SYM(retro_set_input_poll); |
| 161 | + LOAD_SYM(retro_set_input_state); |
| 162 | + LOAD_SYM(retro_load_game); |
| 163 | + LOAD_SYM(retro_unload_game); |
| 164 | + LOAD_SYM(retro_run); |
| 165 | + LOAD_SYM(retro_get_system_info); |
| 166 | + LOAD_SYM(retro_get_system_av_info); |
| 167 | + |
| 168 | + p_retro_set_environment(environment); |
| 169 | + p_retro_set_video_refresh(video_refresh); |
| 170 | + p_retro_set_audio_sample(audio_sample); |
| 171 | + p_retro_set_audio_sample_batch(audio_sample_batch); |
| 172 | + p_retro_set_input_poll(input_poll); |
| 173 | + p_retro_set_input_state(input_state); |
| 174 | + |
| 175 | + p_retro_init(); |
| 176 | + |
| 177 | + struct retro_game_info game = {0}; |
| 178 | + game.path = image_path; |
| 179 | + |
| 180 | + printf("Loading CD image: %s\n", image_path); |
| 181 | + if (!p_retro_load_game(&game)) |
| 182 | + { |
| 183 | + fprintf(stderr, "retro_load_game failed!\n"); |
| 184 | + p_retro_deinit(); |
| 185 | + dlclose(handle); |
| 186 | + return 1; |
| 187 | + } |
| 188 | + |
| 189 | + printf("Game loaded successfully. Running %u frames...\n", num_frames); |
| 190 | + |
| 191 | + /* Check initial RAM state */ |
| 192 | + /* Access jaguarMainRAM to read vectors */ |
| 193 | + uint8_t *(*get_ram)(void) = dlsym(handle, "GetRamPtr"); |
| 194 | + if (get_ram) |
| 195 | + { |
| 196 | + uint8_t *ram = get_ram(); |
| 197 | + uint32_t sp = (ram[0]<<24) | (ram[1]<<16) | (ram[2]<<8) | ram[3]; |
| 198 | + uint32_t pc = (ram[4]<<24) | (ram[5]<<16) | (ram[6]<<8) | ram[7]; |
| 199 | + printf("Initial vectors: SP=0x%08X, PC=0x%08X\n", sp, pc); |
| 200 | + } |
| 201 | + |
| 202 | + for (frame_count = 0; frame_count < num_frames; frame_count++) |
| 203 | + { |
| 204 | + p_retro_run(); |
| 205 | + |
| 206 | + /* Print status at key frames */ |
| 207 | + if (frame_count == 0 || frame_count == 10 || frame_count == 30 || |
| 208 | + frame_count == 60 || frame_count == 120 || frame_count == 299) |
| 209 | + { |
| 210 | + if (!got_video) |
| 211 | + printf(" Frame %u: no video output\n", frame_count); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + printf("\nDone. Total frames: %u\n", num_frames); |
| 216 | + |
| 217 | + p_retro_unload_game(); |
| 218 | + p_retro_deinit(); |
| 219 | + dlclose(handle); |
| 220 | + return 0; |
| 221 | +} |
0 commit comments