| 1 | // Copyright 2009 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | // This is a test program for running code on the Wii DSP, with full control over input |
| 5 | // and automatic compare with output. VERY useful for figuring out what those little |
| 6 | // ops actually do. |
| 7 | // It's very unpolished though |
| 8 | // Use Dolphin's dsptool to generate a new dsp_code.h. |
| 9 | // Originally written by duddie and modified by FIRES. Then further modified by ector. |
| 10 | |
| 11 | #include <array> |
| 12 | #include <debug.h> |
| 13 | #include <fat.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <gccore.h> |
| 16 | #include <malloc.h> |
| 17 | #include <network.h> |
| 18 | #include <ogcsys.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <time.h> |
| 23 | |
| 24 | #include <ogc/color.h> |
| 25 | #include <ogc/consol.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #ifdef _MSC_VER |
| 29 | // Just for easy looking :) |
| 30 | #define HW_RVL // HW_DOL |
| 31 | #endif |
| 32 | |
| 33 | #ifdef HW_RVL |
| 34 | #include <sdcard/wiisd_io.h> |
| 35 | #include <wiiuse/wpad.h> |
| 36 | #else |
| 37 | #include <sdcard/gcsd.h> |
| 38 | #endif |
| 39 | |
| 40 | #include "ConsoleHelper.h" |
| 41 | |
| 42 | #include "dspregs.h" |
| 43 | |
| 44 | // This is where the DSP binary is. |
| 45 | #include "dsp_code.h" |
| 46 | #include "mem_dump.h" |
| 47 | |
| 48 | // Communication with the real DSP and with the DSP emulator. |
| 49 | #include "dsp_interface.h" |
| 50 | #include "real_dsp.h" |
| 51 | // #include "virtual_dsp.h" |
| 52 | |
| 53 | // Used for communications with the DSP, such as dumping registers etc. |
| 54 | u16 dspbuffer[16 * 1024] __attribute__((aligned(0x4000))); |
| 55 | |
| 56 | static void* xfb = nullptr; |
| 57 | void (*reboot)() = (void (*)())0x80001800; |
| 58 | GXRModeObj* rmode; |
| 59 | |
| 60 | static vu16* const _dspReg = (u16*)0xCC005000; |
| 61 | |
| 62 | u16* dspbufP; |
| 63 | u16* dspbufC; |
| 64 | u32* dspbufU; |
| 65 | |
| 66 | u16 dspreg_in[32] = { |
| 67 | 0x0410, 0x0510, 0x0610, 0x0710, 0x0810, 0x0910, 0x0a10, 0x0b10, 0xFFFF, 0xFFFF, 0xFFFF, |
| 68 | 0xFFFF, 0x0855, 0x0966, 0x0a77, 0x0b88, 0x0014, 0xfff5, 0x00ff, 0x2200, 0x0000, 0x0000, |
| 69 | 0x0000, 0x0000, 0x0003, 0x0004, 0x8000, 0x000C, 0x0007, 0x0008, 0x0009, 0x000a, |
| 70 | }; /// ax_h_1 ax_h_1 |
| 71 | |
| 72 | /* ttt ? |
| 73 | |
| 74 | u16 dspreg_in[32] = { |
| 75 | 0x0e4c, 0x03c0, 0x0bd9, 0x06a3, 0x0c06, 0x0240, 0x0010, 0x0ecc, |
| 76 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0322, 0x0000, 0x0000, 0x0000, |
| 77 | 0x0000, 0x0000, 0x00ff, 0x1b41, 0x0000, 0x0040, 0x00ff, 0x0000, |
| 78 | 0x1000, 0x96cc, 0x0000, 0x0000, 0x3fc0, 0x96cc, 0x0000, 0x0000, |
| 79 | }; */ |
| 80 | |
| 81 | // if i set bit 0x4000 of SR my tests crashes :( |
| 82 | |
| 83 | /* |
| 84 | // zelda 0x00da |
| 85 | u16 dspreg_in[32] = { |
| 86 | 0x0a50, 0x0ca2, 0x04f8, 0x0ab0, 0x8039, 0x0000, 0x0000, 0x0000, |
| 87 | 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x03d1, 0x0000, 0x0418, 0x0002, // r08 must have a value ... no |
| 88 | idea why (ector: it's the looped addressing regs) |
| 89 | 0x0000, 0x0000, 0x00ff, 0x1804, 0xdb70, 0x4ddb, 0x0000, 0x0000, |
| 90 | 0x0000, 0x0000, 0x0000, 0xde6d, 0x0000, 0x0000, 0x0000, 0x004e, |
| 91 | };*/ |
| 92 | |
| 93 | u16 dspreg_out[1000][32]; |
| 94 | |
| 95 | /* |
| 96 | // gba ucode dmas result here |
| 97 | u32 SecParams_out[2] __attribute__ ((aligned (0x20))) = { |
| 98 | 0x11223344, // key |
| 99 | 0x55667788 // bootinfo |
| 100 | }; |
| 101 | |
| 102 | // ripped from demo |
| 103 | u32 SecParams_in[8] __attribute__ ((aligned (0x20))) = { |
| 104 | 0xDB967E0F, // key from gba |
| 105 | 0x00000002, |
| 106 | 0x00000002, |
| 107 | 0x00001078, |
| 108 | (u32)SecParams_out, //0x80075060, // ptr to receiving buffer |
| 109 | // padding? |
| 110 | 0x00000000, |
| 111 | 0x00000000, |
| 112 | 0x00000000 |
| 113 | }; |
| 114 | */ |
| 115 | |
| 116 | // UI (interactive register editing) |
| 117 | u32 ui_mode; |
| 118 | |
| 119 | enum |
| 120 | { |
| 121 | UIM_SEL = 1, |
| 122 | UIM_EDIT_REG = 2, |
| 123 | UIM_EDIT_BIN = 4, |
| 124 | }; |
| 125 | |
| 126 | // Currently selected register. |
| 127 | s32 cursor_reg = 0; |
| 128 | // Currently selected digit. |
| 129 | s32 small_cursor_x; |
| 130 | // Value currently being edited. |
| 131 | u16* reg_value; |
| 132 | |
| 133 | RealDSP real_dsp; |
| 134 | |
| 135 | // Currently running microcode |
| 136 | int curUcode = 0, runningUcode = 1; |
| 137 | |
| 138 | int dsp_steps = 0; |
| 139 | |
| 140 | constexpr std::array reg_names = { |
| 141 | "ar0", "ar1", "ar2", "ar3", "ix0", "ix1", "ix2", "ix3", "wr0", "wr1", "wr2", |
| 142 | "wr3", "st0", "st1", "st2", "st3", "c0h", "c1h", "cr ", "sr ", "pl ", "pm1", |
| 143 | "ph ", "pm2", "x0l", "x1l", "x0h", "x1h", "c0l", "c1l", "c0m", "c1m", |
| 144 | }; |
| 145 | |
| 146 | void print_reg_block(int x, int y, int sel, const u16* regs, const u16* compare_regs) |
| 147 | { |
| 148 | for (int j = 0; j < 4; j++) |
| 149 | { |
| 150 | for (int i = 0; i < 8; i++) |
| 151 | { |
| 152 | const int reg = j * 8 + i; |
| 153 | u8 color1 = regs[reg] == compare_regs[reg] ? CON_BRIGHT_WHITE : CON_BRIGHT_RED; |
| 154 | CON_SetColor(sel == reg ? CON_BRIGHT_YELLOW : CON_GREEN); |
| 155 | CON_Printf(x + j * 9, i + y, "%s ", reg_names[reg]); |
| 156 | for (int k = 0; k < 4; k++) |
| 157 | { |
| 158 | if (sel == reg && k == small_cursor_x && ui_mode == UIM_EDIT_REG) |
| 159 | CON_SetColor(CON_BRIGHT_CYAN); |
| 160 | else |
| 161 | CON_SetColor(color1); |
| 162 | CON_Printf(x + 4 + j * 9 + k, i + y, "%01x", (regs[reg] >> ((3 - k) * 4)) & 0xf); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | CON_SetColor(CON_WHITE); |
| 167 | |
| 168 | CON_Printf(x + 2, y + 9, "ACC0: %02x %04x %04x", regs[DSP_REG_ACH0] & 0xff, regs[DSP_REG_ACM0], |
| 169 | regs[DSP_REG_ACL0]); |
| 170 | CON_Printf(x + 2, y + 10, "ACC1: %02x %04x %04x", regs[DSP_REG_ACH1] & 0xff, regs[DSP_REG_ACM1], |
| 171 | regs[DSP_REG_ACL1]); |
| 172 | CON_Printf(x + 2, y + 11, "AX0: %04x %04x", regs[DSP_REG_AXH0], regs[DSP_REG_AXL0]); |
| 173 | CON_Printf(x + 2, y + 12, "AX1: %04x %04x", regs[DSP_REG_AXH1], regs[DSP_REG_AXL1]); |
| 174 | |
| 175 | u64 prod = (u64(regs[DSP_REG_PRODH]) << 32) + (u64(regs[DSP_REG_PRODM]) << 16) + |
| 176 | (u64(regs[DSP_REG_PRODM2]) << 16) + u64(regs[DSP_REG_PRODL]); |
| 177 | u8 prod_h = (prod >> 32) & 0xff; |
| 178 | u16 prod_m = (prod >> 16) & 0xffff; |
| 179 | u16 prod_l = prod & 0xffff; |
| 180 | CON_Printf(x + 2, y + 13, "PROD: %02x %04x %04x", prod_h, prod_m, prod_l); |
| 181 | |
| 182 | CON_Printf(x + 2, y + 14, "SR:"); |
| 183 | for (int i = 0; i < 16; ++i) |
| 184 | CON_Printf(x + 6 + i + i / 4, y + 14, "%c", regs[DSP_REG_SR] & (1 << (15 - i)) ? '1' : '0'); |
| 185 | CON_Printf(x + 21, y + 15, "SZOC"); |
| 186 | } |
| 187 | |
| 188 | void print_regs(int _step, int _dsp_steps) |
| 189 | { |
| 190 | const u16* regs = _step == 0 ? dspreg_in : dspreg_out[_step - 1]; |
| 191 | const u16* regs2 = dspreg_out[_step]; |
| 192 | |
| 193 | print_reg_block(0, 2, _step == 0 ? cursor_reg : -1, regs, regs2); |
| 194 | print_reg_block(38, 2, -1, regs2, regs); |
| 195 | |
| 196 | CON_SetColor(CON_WHITE); |
| 197 | CON_Printf(33, 17, "%i / %i ", _step + 1, _dsp_steps); |
| 198 | |
| 199 | return; |
| 200 | |
| 201 | static int count = 0; |
| 202 | int x = 0, y = 16; |
| 203 | if (count > 2) |
| 204 | CON_Clear(); |
| 205 | count = 0; |
| 206 | CON_SetColor(CON_WHITE); |
| 207 | for (int i = 0x0; i < 0xf70; i++) |
| 208 | { |
| 209 | if (dspbufC[i] != mem_dump[i]) |
| 210 | { |
| 211 | CON_Printf(x, y, "%04x=%04x", i, dspbufC[i]); |
| 212 | count++; |
| 213 | x += 10; |
| 214 | if (x >= 60) |
| 215 | { |
| 216 | x = 0; |
| 217 | y++; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | CON_Printf(4, 25, "%08x", count); |
| 222 | } |
| 223 | |
| 224 | void UpdateLastMessage(const char* msg) |
| 225 | { |
| 226 | CON_PrintRow(4, 24, msg); |
| 227 | } |
| 228 | |
| 229 | void DumpDSP_ROMs(const u16* rom, const u16* coef) |
| 230 | { |
| 231 | char filename[260] = {0}; |
| 232 | sprintf(filename, "sd:/dsp_rom.bin"); |
| 233 | FILE* fROM = fopen(filename, "wb"); |
| 234 | sprintf(filename, "sd:/dsp_coef.bin"); |
| 235 | FILE* fCOEF = fopen(filename, "wb"); |
| 236 | |
| 237 | if (fROM && fCOEF) |
| 238 | { |
| 239 | fwrite(MEM_PHYSICAL_TO_K0(rom), 0x2000, 1, fROM); |
| 240 | fwrite(MEM_PHYSICAL_TO_K0(coef), 0x1000, 1, fCOEF); |
| 241 | |
| 242 | UpdateLastMessage("DSP ROMs dumped to SD"); |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | UpdateLastMessage("SD Write Error"); |
| 247 | } |
| 248 | |
| 249 | if (fROM) |
| 250 | fclose(fROM); |
| 251 | if (fCOEF) |
| 252 | fclose(fCOEF); |
| 253 | } |
| 254 | |
| 255 | void ui_pad_sel(void) |
| 256 | { |
| 257 | #ifdef HW_RVL |
| 258 | if (WPAD_ButtonsDown(0) & WPAD_BUTTON_RIGHT) |
| 259 | cursor_reg += 8; |
| 260 | if (WPAD_ButtonsDown(0) & WPAD_BUTTON_LEFT) |
| 261 | cursor_reg -= 8; |
| 262 | if (WPAD_ButtonsDown(0) & WPAD_BUTTON_UP) |
| 263 | cursor_reg--; |
| 264 | if (WPAD_ButtonsDown(0) & WPAD_BUTTON_DOWN) |
| 265 | cursor_reg++; |
| 266 | cursor_reg &= 0x1f; |
| 267 | if (WPAD_ButtonsDown(0) & WPAD_BUTTON_A) |
| 268 | { |
| 269 | ui_mode = UIM_EDIT_REG; |
| 270 | reg_value = &dspreg_in[cursor_reg]; |
| 271 | } |
| 272 | #endif |
| 273 | if (PAD_ButtonsDown(0) & PAD_BUTTON_RIGHT) |
| 274 | cursor_reg += 8; |
| 275 | if (PAD_ButtonsDown(0) & PAD_BUTTON_LEFT) |
| 276 | cursor_reg -= 8; |
| 277 | if (PAD_ButtonsDown(0) & PAD_BUTTON_UP) |
| 278 | cursor_reg--; |
| 279 | if (PAD_ButtonsDown(0) & PAD_BUTTON_DOWN) |
| 280 | cursor_reg++; |
| 281 | cursor_reg &= 0x1f; |
| 282 | if (PAD_ButtonsDown(0) & PAD_BUTTON_A) |
| 283 | { |
| 284 | ui_mode = UIM_EDIT_REG; |
| 285 | reg_value = &dspreg_in[cursor_reg]; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void ui_pad_edit_reg(void) |
| 290 | { |
| 291 | #ifdef HW_RVL |
| 292 | if (WPAD_ButtonsDown(0) & WPAD_BUTTON_RIGHT) |
| 293 | small_cursor_x++; |
| 294 | if (WPAD_ButtonsDown(0) & WPAD_BUTTON_LEFT) |
| 295 | small_cursor_x--; |
| 296 | small_cursor_x &= 0x3; |
| 297 | |
| 298 | if (WPAD_ButtonsDown(0) & WPAD_BUTTON_UP) |
| 299 | *reg_value += 0x1 << (4 * (3 - small_cursor_x)); |
| 300 | if (WPAD_ButtonsDown(0) & WPAD_BUTTON_DOWN) |
| 301 | *reg_value -= 0x1 << (4 * (3 - small_cursor_x)); |
| 302 | if (WPAD_ButtonsDown(0) & WPAD_BUTTON_A) |
| 303 | ui_mode = UIM_SEL; |
| 304 | if (WPAD_ButtonsDown(0) & WPAD_BUTTON_1) |
| 305 | *reg_value = 0; |
| 306 | if (WPAD_ButtonsDown(0) & WPAD_BUTTON_2) |
| 307 | *reg_value = 0xffff; |
| 308 | #endif |
| 309 | if (PAD_ButtonsDown(0) & PAD_BUTTON_RIGHT) |
| 310 | small_cursor_x++; |
| 311 | if (PAD_ButtonsDown(0) & PAD_BUTTON_LEFT) |
| 312 | small_cursor_x--; |
| 313 | small_cursor_x &= 0x3; |
| 314 | |
| 315 | if (PAD_ButtonsDown(0) & PAD_BUTTON_UP) |
| 316 | *reg_value += 0x1 << (4 * (3 - small_cursor_x)); |
| 317 | if (PAD_ButtonsDown(0) & PAD_BUTTON_DOWN) |
| 318 | *reg_value -= 0x1 << (4 * (3 - small_cursor_x)); |
| 319 | if (PAD_ButtonsDown(0) & PAD_BUTTON_A) |
| 320 | ui_mode = UIM_SEL; |
| 321 | if (PAD_ButtonsDown(0) & PAD_BUTTON_X) |
| 322 | *reg_value = 0; |
| 323 | if (PAD_ButtonsDown(0) & PAD_BUTTON_Y) |
| 324 | *reg_value = 0xffff; |
| 325 | } |
| 326 | |
| 327 | void handle_dsp_mail(void) |
| 328 | { |
| 329 | // Should put a loop around this too. |
| 330 | if (DSP_CheckMailFrom()) |
| 331 | { |
| 332 | u32 mail = DSP_ReadMailFrom(); |
| 333 | |
| 334 | if (mail == 0x8071feed) |
| 335 | { |
| 336 | // DSP ready for task. Let's send one. |
| 337 | // First, prepare data. |
| 338 | for (int n = 0; n < 32; n++) |
| 339 | dspbufC[0x00 + n] = dspreg_in[n]; |
| 340 | DCFlushRange(dspbufC, 0x2000); |
| 341 | // Then send the code. |
| 342 | DCFlushRange((void*)dsp_code[curUcode], 0x2000); |
| 343 | // DMA ucode to iram base, entry point is just after exception vectors...0x10 |
| 344 | // NOTE: for any ucode made by dsptool, the block length will be 8191 |
| 345 | real_dsp.SendTask((void*)MEM_VIRTUAL_TO_PHYSICAL(dsp_code[curUcode]), 0, |
| 346 | sizeof(dsp_code[curUcode]) - 1, 0x10); |
| 347 | |
| 348 | runningUcode = curUcode + 1; |
| 349 | |
| 350 | // Clear exception status since we've loaded a new ucode |
| 351 | CON_BlankRow(25); |
| 352 | } |
| 353 | else if ((mail & 0xffff0000) == 0x8bad0000) |
| 354 | { |
| 355 | // dsp_base.inc is reporting an exception happened |
| 356 | CON_PrintRow(4, 25, "%s caused exception %x at step %i", UCODE_NAMES[curUcode], mail & 0xff, |
| 357 | dsp_steps); |
| 358 | } |
| 359 | else if (mail == 0x8888dead) |
| 360 | { |
| 361 | // Send memory dump (DSP DRAM from someone's GameCube?) |
| 362 | // not really sure why this is important - I guess just to try to keep tests predictable |
| 363 | u16* tmpBuf = (u16*)MEM_VIRTUAL_TO_PHYSICAL(mem_dump); |
| 364 | |
| 365 | while (real_dsp.CheckMailTo()) |
| 366 | ; |
| 367 | real_dsp.SendMailTo((u32)tmpBuf); |
| 368 | while (real_dsp.CheckMailTo()) |
| 369 | ; |
| 370 | } |
| 371 | else if (mail == 0x8888beef) |
| 372 | { |
| 373 | // Provide register base to DSP (if using dsp_base.inc, it will DMA them to the correct place) |
| 374 | while (real_dsp.CheckMailTo()) |
| 375 | ; |
| 376 | real_dsp.SendMailTo((u32)dspbufP); |
| 377 | while (real_dsp.CheckMailTo()) |
| 378 | ; |
| 379 | } |
| 380 | else if (mail == 0x8888feeb) |
| 381 | { |
| 382 | // We got a stepful of registers. |
| 383 | DCInvalidateRange(dspbufC, 0x2000); |
| 384 | for (int i = 0; i < 32; i++) |
| 385 | dspreg_out[dsp_steps][i] = dspbufC[0xf80 + i]; |
| 386 | |
| 387 | dsp_steps++; |
| 388 | |
| 389 | while (real_dsp.CheckMailTo()) |
| 390 | ; |
| 391 | real_dsp.SendMailTo(0x8000dead); |
| 392 | while (real_dsp.CheckMailTo()) |
| 393 | ; |
| 394 | } |
| 395 | else if (mail == 0x80050000) |
| 396 | { |
| 397 | CON_PrintRow(4, 25, "ACCOV at step %i", dsp_steps); |
| 398 | } |
| 399 | |
| 400 | // ROM dumping mails |
| 401 | else if (mail == 0x8888c0de) |
| 402 | { |
| 403 | // DSP has copied irom to its DRAM...send address so it can dma it back |
| 404 | while (real_dsp.CheckMailTo()) |
| 405 | ; |
| 406 | real_dsp.SendMailTo((u32)dspbufP); |
| 407 | while (real_dsp.CheckMailTo()) |
| 408 | ; |
| 409 | } |
| 410 | else if (mail == 0x8888da7a) |
| 411 | { |
| 412 | // DSP has copied coef to its DRAM...send address so it can DMA it back |
| 413 | while (real_dsp.CheckMailTo()) |
| 414 | ; |
| 415 | real_dsp.SendMailTo((u32)&dspbufP[0x1000]); |
| 416 | while (real_dsp.CheckMailTo()) |
| 417 | ; |
| 418 | |
| 419 | // Now we can do something useful with the buffer :) |
| 420 | DumpDSP_ROMs(dspbufP, &dspbufP[0x1000]); |
| 421 | } |
| 422 | |
| 423 | // SDK status mails |
| 424 | /* |
| 425 | // GBA ucode |
| 426 | else if (mail == 0xdcd10000) // DSP_INIT |
| 427 | { |
| 428 | real_dsp.SendMailTo(0xabba0000); |
| 429 | while (real_dsp.CheckMailTo()); |
| 430 | DCFlushRange(SecParams_in, sizeof(SecParams_in)); |
| 431 | CON_PrintRow(4, 25, "SecParams_out = %x", SecParams_in[4]); |
| 432 | real_dsp.SendMailTo((u32)SecParams_in); |
| 433 | while (real_dsp.CheckMailTo()); |
| 434 | } |
| 435 | else if (mail == 0xdcd10003) // DSP_DONE |
| 436 | { |
| 437 | real_dsp.SendMailTo(0xcdd1babe); // custom mail to tell DSP to halt (calls end_of_test) |
| 438 | while (real_dsp.CheckMailTo()); |
| 439 | |
| 440 | DCInvalidateRange(SecParams_out, sizeof(SecParams_out)); |
| 441 | CON_PrintRow(4, 26, "SecParams_out: %08x %08x", |
| 442 | SecParams_out[0], SecParams_out[1]); |
| 443 | } |
| 444 | */ |
| 445 | |
| 446 | CON_PrintRow(2, 1, "UCode: %d/%d %s, Last mail: %08x", curUcode + 1, NUM_UCODES, |
| 447 | UCODE_NAMES[curUcode], mail); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void dump_all_ucodes(bool fastmode) |
| 452 | { |
| 453 | char filename[260] = {0}; |
| 454 | char temp[100]; |
| 455 | u32 written; |
| 456 | |
| 457 | sprintf(filename, "sd:/dsp_dump_all.bin"); |
| 458 | FILE* f2 = fopen(filename, "wb"); |
| 459 | fclose(f2); |
| 460 | |
| 461 | for (int UCodeToDump = 0; UCodeToDump < NUM_UCODES; UCodeToDump++) |
| 462 | { |
| 463 | // First, change the microcode |
| 464 | dsp_steps = 0; |
| 465 | curUcode = UCodeToDump; |
| 466 | runningUcode = 0; |
| 467 | |
| 468 | DCInvalidateRange(dspbufC, 0x2000); |
| 469 | DCFlushRange(dspbufC, 0x2000); |
| 470 | |
| 471 | real_dsp.Reset(); |
| 472 | |
| 473 | // Loop over handling mail until we've stopped stepping |
| 474 | // dsp_steps-3 compensates for mails to setup the ucode |
| 475 | for (int steps_cache = dsp_steps - 3; steps_cache <= dsp_steps; steps_cache++) |
| 476 | { |
| 477 | VIDEO_WaitVSync(); |
| 478 | handle_dsp_mail(); |
| 479 | } |
| 480 | VIDEO_WaitVSync(); |
| 481 | |
| 482 | sprintf(filename, "sd:/dsp_dump_all.bin"); |
| 483 | FILE* f2 = fopen(filename, "ab"); |
| 484 | |
| 485 | if (fastmode == false) |
| 486 | { |
| 487 | // Then write microcode dump to file |
| 488 | sprintf(filename, "sd:/dsp_dump%d.bin", UCodeToDump); |
| 489 | FILE* f = fopen(filename, "wb"); |
| 490 | if (f) |
| 491 | { |
| 492 | // First write initial regs |
| 493 | written = fwrite(dspreg_in, 1, 32 * 2, f); |
| 494 | |
| 495 | // Then write all the dumps. |
| 496 | written += fwrite(dspreg_out, 1, dsp_steps * 32 * 2, f); |
| 497 | fclose(f); |
| 498 | } |
| 499 | else |
| 500 | { |
| 501 | UpdateLastMessage("SD Write Error"); |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | if (f2) // all in 1 dump file (extra) |
| 507 | { |
| 508 | if (UCodeToDump == 0) |
| 509 | { |
| 510 | // First write initial regs |
| 511 | written = fwrite(dspreg_in, 1, 32 * 2, f2); |
| 512 | written += fwrite(dspreg_out, 1, dsp_steps * 32 * 2, f2); |
| 513 | } |
| 514 | else |
| 515 | { |
| 516 | written = fwrite(dspreg_out, 1, dsp_steps * 32 * 2, f2); |
| 517 | } |
| 518 | |
| 519 | fclose(f2); |
| 520 | |
| 521 | if (UCodeToDump < NUM_UCODES - 1) |
| 522 | { |
| 523 | sprintf(temp, "Dump %d Successful. Wrote %d bytes, steps: %d", UCodeToDump + 1, written, |
| 524 | dsp_steps); |
| 525 | UpdateLastMessage(temp); |
| 526 | } |
| 527 | else |
| 528 | { |
| 529 | UpdateLastMessage("DUMPING DONE!"); |
| 530 | } |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | UpdateLastMessage("SD Write Error"); |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | // Shove common, un-dsp-ish init things here |
| 541 | void InitGeneral() |
| 542 | { |
| 543 | // Initialize the video system |
| 544 | VIDEO_Init(); |
| 545 | |
| 546 | // This function initializes the attached controllers |
| 547 | PAD_Init(); |
| 548 | #ifdef HW_RVL |
| 549 | WPAD_Init(); |
| 550 | #endif |
| 551 | |
| 552 | // Obtain the preferred video mode from the system |
| 553 | // This will correspond to the settings in the Wii Menu |
| 554 | rmode = VIDEO_GetPreferredMode(nullptr); |
| 555 | |
| 556 | // Allocate memory for the display in the uncached region |
| 557 | xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode)); |
| 558 | |
| 559 | // Set up the video registers with the chosen mode |
| 560 | VIDEO_Configure(rmode); |
| 561 | // Tell the video hardware where our display memory is |
| 562 | VIDEO_SetNextFramebuffer(xfb); |
| 563 | // Make the display visible |
| 564 | VIDEO_SetBlack(FALSE); |
| 565 | // Flush the video register changes to the hardware |
| 566 | VIDEO_Flush(); |
| 567 | // Wait for Video setup to complete |
| 568 | VIDEO_WaitVSync(); |
| 569 | if (rmode->viTVMode & VI_NON_INTERLACE) |
| 570 | VIDEO_WaitVSync(); |
| 571 | |
| 572 | // Initialize the console, required for printf |
| 573 | CON_Init(xfb, 20, 64, rmode->fbWidth, rmode->xfbHeight, rmode->fbWidth * VI_DISPLAY_PIX_SZ); |
| 574 | |
| 575 | #ifdef HW_RVL |
| 576 | // Initialize FAT so we can write to SD. |
| 577 | __io_wiisd.startup(); |
| 578 | fatMountSimple("sd", &__io_wiisd); |
| 579 | #else |
| 580 | // Initialize FAT so we can write to SD Gecko in slot B. |
| 581 | fatMountSimple("sd", &__io_gcsdb); |
| 582 | |
| 583 | // Init debug over BBA...change IPs to suite your needs |
| 584 | tcp_localip = "192.168.1.103"; |
| 585 | tcp_netmask = "255.255.255.0"; |
| 586 | tcp_gateway = "192.168.1.2"; |
| 587 | DEBUG_Init(GDBSTUB_DEVICE_TCP, GDBSTUB_DEF_TCPPORT); |
| 588 | #endif |
| 589 | } |
| 590 | |
| 591 | void ExitToLoader() |
| 592 | { |
| 593 | fatUnmount("sd"); |
| 594 | #ifdef HW_RVL |
| 595 | __io_wiisd.shutdown(); |
| 596 | #endif |
| 597 | |
| 598 | UpdateLastMessage("Exiting..."); |
| 599 | real_dsp.Reset(); |
| 600 | reboot(); |
| 601 | } |
| 602 | |
| 603 | int main() |
| 604 | { |
| 605 | InitGeneral(); |
| 606 | |
| 607 | ui_mode = UIM_SEL; |
| 608 | |
| 609 | dspbufP = (u16*)MEM_VIRTUAL_TO_PHYSICAL(dspbuffer); // physical |
| 610 | dspbufC = dspbuffer; // cached |
| 611 | dspbufU = (u32*)(MEM_K0_TO_K1(dspbuffer)); // uncached |
| 612 | |
| 613 | DCInvalidateRange(dspbuffer, 0x2000); |
| 614 | for (int j = 0; j < 0x800; j++) |
| 615 | dspbufU[j] = 0xffffffff; |
| 616 | |
| 617 | // Initialize DSP. |
| 618 | real_dsp.Init(); |
| 619 | |
| 620 | int show_step = 0; |
| 621 | while (true) |
| 622 | { |
| 623 | handle_dsp_mail(); |
| 624 | |
| 625 | VIDEO_WaitVSync(); |
| 626 | |
| 627 | PAD_ScanPads(); |
| 628 | if (PAD_ButtonsDown(0) & PAD_BUTTON_START) |
| 629 | ExitToLoader(); |
| 630 | #ifdef HW_RVL |
| 631 | WPAD_ScanPads(); |
| 632 | if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME) |
| 633 | ExitToLoader(); |
| 634 | |
| 635 | CON_Printf(2, 18, "Controls:"); |
| 636 | CON_Printf(4, 19, "+/- (GC:'L'/'R') to move"); |
| 637 | CON_Printf(4, 20, "A (GC:'A') to edit register; B (GC:'B') to start over"); |
| 638 | CON_Printf(4, 21, "1 (GC:'Z') to move next microcode"); |
| 639 | CON_Printf(4, 22, |
| 640 | "2 (GC:'X') dump results to SD; UP (GC:'Y') dump results to SD (SINGLE FILE)"); |
| 641 | CON_Printf(4, 23, "Home (GC:'START') to exit"); |
| 642 | #else |
| 643 | CON_Printf(2, 18, "Controls:"); |
| 644 | CON_Printf(4, 19, "L/R to move"); |
| 645 | CON_Printf(4, 20, "A to edit register, B to start over"); |
| 646 | CON_Printf(4, 21, "Z to move to next microcode"); |
| 647 | CON_Printf(4, 22, "Start to exit"); |
| 648 | #endif |
| 649 | |
| 650 | print_regs(show_step, dsp_steps); |
| 651 | |
| 652 | switch (ui_mode) |
| 653 | { |
| 654 | case UIM_SEL: |
| 655 | ui_pad_sel(); |
| 656 | break; |
| 657 | case UIM_EDIT_REG: |
| 658 | ui_pad_edit_reg(); |
| 659 | break; |
| 660 | case UIM_EDIT_BIN: |
| 661 | // ui_pad_edit_bin(); |
| 662 | break; |
| 663 | default: |
| 664 | break; |
| 665 | } |
| 666 | DCFlushRange(xfb, 0x200000); |
| 667 | |
| 668 | // Use B to start over. |
| 669 | #ifdef HW_RVL |
| 670 | if ((WPAD_ButtonsDown(0) & WPAD_BUTTON_B) || (PAD_ButtonsDown(0) & PAD_BUTTON_B)) |
| 671 | #else |
| 672 | if (PAD_ButtonsDown(0) & PAD_BUTTON_B) |
| 673 | #endif |
| 674 | { |
| 675 | dsp_steps = |
| 676 | 0; // Let's not add the new steps after the original ones. That was just annoying. |
| 677 | |
| 678 | DCInvalidateRange(dspbufC, 0x2000); |
| 679 | DCFlushRange(dspbufC, 0x2000); |
| 680 | |
| 681 | // Reset the DSP. |
| 682 | real_dsp.Reset(); |
| 683 | UpdateLastMessage("OK"); |
| 684 | } |
| 685 | |
| 686 | // Navigate between results using + and - buttons. |
| 687 | #ifdef HW_RVL |
| 688 | if ((WPAD_ButtonsDown(0) & WPAD_BUTTON_PLUS) || (PAD_ButtonsDown(0) & PAD_TRIGGER_R)) |
| 689 | #else |
| 690 | if (PAD_ButtonsDown(0) & PAD_TRIGGER_R) |
| 691 | #endif |
| 692 | { |
| 693 | show_step++; |
| 694 | if (show_step >= dsp_steps) |
| 695 | show_step = 0; |
| 696 | UpdateLastMessage("OK"); |
| 697 | } |
| 698 | #ifdef HW_RVL |
| 699 | if ((WPAD_ButtonsDown(0) & WPAD_BUTTON_MINUS) || (PAD_ButtonsDown(0) & PAD_TRIGGER_L)) |
| 700 | #else |
| 701 | if (PAD_ButtonsDown(0) & PAD_TRIGGER_L) |
| 702 | #endif |
| 703 | { |
| 704 | show_step--; |
| 705 | if (show_step < 0) |
| 706 | show_step = dsp_steps - 1; |
| 707 | UpdateLastMessage("OK"); |
| 708 | } |
| 709 | |
| 710 | #ifdef HW_RVL |
| 711 | if ((WPAD_ButtonsDown(0) & WPAD_BUTTON_1) || (PAD_ButtonsDown(0) & PAD_TRIGGER_Z)) |
| 712 | #else |
| 713 | if (PAD_ButtonsDown(0) & PAD_TRIGGER_Z) |
| 714 | #endif |
| 715 | { |
| 716 | curUcode++; |
| 717 | if (curUcode == NUM_UCODES) |
| 718 | curUcode = 0; |
| 719 | |
| 720 | // Reset step counters since we're in a new ucode. |
| 721 | show_step = 0; |
| 722 | dsp_steps = 0; |
| 723 | |
| 724 | DCInvalidateRange(dspbufC, 0x2000); |
| 725 | for (int n = 0; n < 0x2000; n++) |
| 726 | { |
| 727 | // dspbufU[n/2] = 0; dspbufC[n] = 0; |
| 728 | } |
| 729 | DCFlushRange(dspbufC, 0x2000); |
| 730 | |
| 731 | // Reset the DSP. |
| 732 | real_dsp.Reset(); |
| 733 | UpdateLastMessage("OK"); |
| 734 | |
| 735 | // Waiting for video to synchronize (enough time to set our new microcode) |
| 736 | VIDEO_WaitVSync(); |
| 737 | } |
| 738 | |
| 739 | #ifdef HW_RVL |
| 740 | // Probably could offer to save to sd gecko or something on gc... |
| 741 | // The future is web-based reporting ;) |
| 742 | if ((WPAD_ButtonsDown(0) & WPAD_BUTTON_2) || (PAD_ButtonsDown(0) & PAD_BUTTON_X)) |
| 743 | { |
| 744 | dump_all_ucodes(false); |
| 745 | } |
| 746 | |
| 747 | // Dump all results into 1 file (skip file per ucode part) = FAST because of LIBFAT filecreate |
| 748 | // bug |
| 749 | if ((WPAD_ButtonsDown(0) & WPAD_BUTTON_UP) || (PAD_ButtonsDown(0) & PAD_BUTTON_Y)) |
| 750 | { |
| 751 | dump_all_ucodes(true); |
| 752 | } |
| 753 | #endif |
| 754 | |
| 755 | } // end main loop |
| 756 | |
| 757 | ExitToLoader(); |
| 758 | |
| 759 | // Will never reach here, but just to be sure.. |
| 760 | exit(0); |
| 761 | return 0; |
| 762 | } |