DSPTool.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. // Copyright 2009 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <string>
  4. #include <utility>
  5. #include <vector>
  6. #include <fmt/format.h>
  7. #include "Common/CommonTypes.h"
  8. #include "Common/FileUtil.h"
  9. #include "Common/StringUtil.h"
  10. #include "Core/DSP/DSPCodeUtil.h"
  11. #include "Core/DSP/DSPDisassembler.h"
  12. #include "Core/DSP/DSPHost.h"
  13. #include "Core/DSP/DSPTables.h"
  14. // Stub out the dsplib host stuff, since this is just a simple cmdline tools.
  15. u8 DSP::Host::ReadHostMemory(u32 addr)
  16. {
  17. return 0;
  18. }
  19. void DSP::Host::WriteHostMemory(u8 value, u32 addr)
  20. {
  21. }
  22. void DSP::Host::DMAToDSP(u16* dst, u32 addr, u32 size)
  23. {
  24. }
  25. void DSP::Host::DMAFromDSP(const u16* src, u32 addr, u32 size)
  26. {
  27. }
  28. void DSP::Host::OSD_AddMessage(std::string str, u32 ms)
  29. {
  30. }
  31. bool DSP::Host::OnThread()
  32. {
  33. return false;
  34. }
  35. bool DSP::Host::IsWiiHost()
  36. {
  37. return false;
  38. }
  39. void DSP::Host::CodeLoaded(DSPCore& dsp, u32 addr, size_t size)
  40. {
  41. }
  42. void DSP::Host::CodeLoaded(DSPCore& dsp, const u8* ptr, size_t size)
  43. {
  44. }
  45. void DSP::Host::InterruptRequest()
  46. {
  47. }
  48. void DSP::Host::UpdateDebugger()
  49. {
  50. }
  51. static std::string CodeToHeader(const std::vector<u16>& code, const std::string& filename)
  52. {
  53. std::vector<u16> code_padded = code;
  54. // Pad with nops to 32byte boundary
  55. while (code_padded.size() & 0x7f)
  56. code_padded.push_back(0);
  57. std::string header;
  58. header.reserve(code_padded.size() * 4);
  59. header.append("#define NUM_UCODES 1\n\n");
  60. std::string filename_without_extension;
  61. SplitPath(filename, nullptr, &filename_without_extension, nullptr);
  62. header.append(fmt::format("const char* UCODE_NAMES[NUM_UCODES] = {{\"{}\"}};\n\n",
  63. filename_without_extension));
  64. header.append("alignas(0x20) const unsigned short dsp_code[NUM_UCODES][0x1000] = {\n");
  65. header.append("\t{\n\t\t");
  66. for (u32 j = 0; j < code_padded.size(); j++)
  67. {
  68. if (j && ((j & 15) == 0))
  69. header.append("\n\t\t");
  70. header.append(fmt::format("{:#06x}, ", code_padded[j]));
  71. }
  72. header.append("\n\t},\n");
  73. header.append("};\n");
  74. return header;
  75. }
  76. static std::string CodesToHeader(const std::vector<std::vector<u16>>& codes,
  77. const std::vector<std::string>& filenames)
  78. {
  79. std::vector<std::vector<u16>> codes_padded;
  80. std::size_t reserve_size = 0;
  81. for (std::size_t i = 0; i < codes.size(); i++)
  82. {
  83. codes_padded.push_back(codes[i]);
  84. // Pad with nops to 32byte boundary
  85. while (codes_padded[i].size() & 0x7f)
  86. codes_padded[i].push_back(0);
  87. reserve_size += codes_padded[i].size();
  88. }
  89. std::string header;
  90. header.reserve(reserve_size * 4);
  91. header.append(fmt::format("#define NUM_UCODES {}\n\n", codes.size()));
  92. header.append("const char* UCODE_NAMES[NUM_UCODES] = {\n");
  93. for (const std::string& in_filename : filenames)
  94. {
  95. std::string filename;
  96. if (!SplitPath(in_filename, nullptr, &filename, nullptr))
  97. filename = in_filename;
  98. header.append(fmt::format("\t\"{}\",\n", filename));
  99. }
  100. header.append("};\n\n");
  101. header.append("const unsigned short dsp_code[NUM_UCODES][0x1000] = {\n");
  102. for (std::size_t i = 0; i < codes.size(); i++)
  103. {
  104. if (codes[i].empty())
  105. continue;
  106. header.append("\t{\n\t\t");
  107. for (std::size_t j = 0; j < codes_padded[i].size(); j++)
  108. {
  109. if (j && ((j & 15) == 0))
  110. header.append("\n\t\t");
  111. header.append(fmt::format("{:#06x}, ", codes_padded[i][j]));
  112. }
  113. header.append("\n\t},\n");
  114. }
  115. header.append("};\n");
  116. return header;
  117. }
  118. static bool PerformBinaryComparison(const std::string& lhs, const std::string& rhs)
  119. {
  120. std::string binary_code;
  121. File::ReadFileToString(lhs, binary_code);
  122. const std::vector<u16> code1 = DSP::BinaryStringBEToCode(binary_code);
  123. File::ReadFileToString(rhs, binary_code);
  124. const std::vector<u16> code2 = DSP::BinaryStringBEToCode(binary_code);
  125. return DSP::Compare(code1, code2);
  126. }
  127. static void PrintResults(const std::string& input_name, const std::string& output_name,
  128. bool print_results_srhack, bool print_results_prodhack)
  129. {
  130. std::string dumpfile;
  131. File::ReadFileToString(input_name, dumpfile);
  132. const std::vector<u16> reg_vector = DSP::BinaryStringBEToCode(dumpfile);
  133. std::string results("Start:\n");
  134. for (int initial_reg = 0; initial_reg < 32; initial_reg++)
  135. {
  136. results.append(fmt::format("{:02x} {:04x} ", initial_reg, reg_vector.at(initial_reg)));
  137. if ((initial_reg + 1) % 8 == 0)
  138. results.append("\n");
  139. }
  140. results.append("\n");
  141. results.append("Step [number]:\n[Reg] [last value] [current value]\n\n");
  142. for (unsigned int step = 1; step < reg_vector.size() / 32; step++)
  143. {
  144. bool changed = false;
  145. u16 current_reg;
  146. u16 last_reg;
  147. u32 htemp;
  148. // results.append(fmt::format("Step {:3d}: (CW {:#06x}) UC:{:03d}\n", step, 0x8fff+step,
  149. // (step-1)/32));
  150. results.append(fmt::format("Step {:3d}:\n", step));
  151. for (int reg = 0; reg < 32; reg++)
  152. {
  153. if (reg >= 0x0c && reg <= 0x0f)
  154. continue;
  155. if (print_results_srhack && reg == 0x13)
  156. continue;
  157. if (print_results_prodhack && reg >= 0x15 && reg <= 0x17)
  158. {
  159. switch (reg)
  160. {
  161. case 0x15: // DSP_REG_PRODM
  162. last_reg =
  163. reg_vector.at((step * 32 - 32) + reg) + reg_vector.at((step * 32 - 32) + reg + 2);
  164. current_reg = reg_vector.at(step * 32 + reg) + reg_vector.at(step * 32 + reg + 2);
  165. break;
  166. case 0x16: // DSP_REG_PRODH
  167. htemp = ((reg_vector.at(step * 32 + reg - 1) + reg_vector.at(step * 32 + reg + 1)) &
  168. ~0xffff) >>
  169. 16;
  170. current_reg = (u8)(reg_vector.at(step * 32 + reg) + htemp);
  171. htemp =
  172. ((reg_vector.at(step * 32 - 32 + reg - 1) + reg_vector.at(step * 32 - 32 + reg + 1)) &
  173. ~0xffff) >>
  174. 16;
  175. last_reg = (u8)(reg_vector.at(step * 32 - 32 + reg) + htemp);
  176. break;
  177. case 0x17: // DSP_REG_PRODM2
  178. default:
  179. current_reg = 0;
  180. last_reg = 0;
  181. break;
  182. }
  183. }
  184. else
  185. {
  186. current_reg = reg_vector.at(step * 32 + reg);
  187. last_reg = reg_vector.at((step * 32 - 32) + reg);
  188. }
  189. if (last_reg != current_reg)
  190. {
  191. results.append(fmt::format("{:02x} {:7s}: {:04x} {:04x}\n", reg, DSP::pdregname(reg),
  192. last_reg, current_reg));
  193. changed = true;
  194. }
  195. }
  196. if (changed)
  197. results.append("\n");
  198. else
  199. results.append("No Change\n\n");
  200. }
  201. if (output_name.empty())
  202. printf("%s", results.c_str());
  203. else
  204. File::WriteStringToFile(output_name, results);
  205. }
  206. static bool PerformDisassembly(const std::string& input_name, const std::string& output_name)
  207. {
  208. if (input_name.empty())
  209. {
  210. printf("Disassemble: Must specify input.\n");
  211. return false;
  212. }
  213. std::string binary_code;
  214. File::ReadFileToString(input_name, binary_code);
  215. const std::vector<u16> code = DSP::BinaryStringBEToCode(binary_code);
  216. std::string text;
  217. DSP::Disassemble(code, true, text);
  218. if (output_name.empty())
  219. printf("%s", text.c_str());
  220. else
  221. File::WriteStringToFile(output_name, text);
  222. printf("Disassembly completed successfully!\n");
  223. return true;
  224. }
  225. static std::vector<std::string> GetAssemblerFiles(const std::string& source)
  226. {
  227. std::vector<std::string> files;
  228. std::size_t last_pos = 0;
  229. std::size_t pos = 0;
  230. while ((pos = source.find('\n', last_pos)) != std::string::npos)
  231. {
  232. std::string temp = source.substr(last_pos, pos - last_pos);
  233. if (!temp.empty())
  234. files.push_back(std::move(temp));
  235. last_pos = pos + 1;
  236. }
  237. return files;
  238. }
  239. static bool PerformAssembly(const std::string& input_name, const std::string& output_name,
  240. const std::string& output_header_name, bool multiple, bool force,
  241. bool output_size)
  242. {
  243. if (input_name.empty())
  244. {
  245. printf("Assemble: Must specify input.\n");
  246. return false;
  247. }
  248. std::string source;
  249. if (File::ReadFileToString(input_name, source))
  250. {
  251. if (multiple)
  252. {
  253. source.append("\n");
  254. // When specifying a list of files we must compile a header
  255. // (we can't assemble multiple files to one binary)
  256. // since we checked it before, we assume output_header_name isn't empty
  257. std::string currentSource;
  258. const std::vector<std::string> files = GetAssemblerFiles(source);
  259. std::size_t lines = files.size();
  260. if (lines == 0)
  261. {
  262. printf("ERROR: Must specify at least one file\n");
  263. return false;
  264. }
  265. std::vector<std::vector<u16>> codes(lines);
  266. for (std::size_t i = 0; i < lines; i++)
  267. {
  268. if (!File::ReadFileToString(files[i], currentSource))
  269. {
  270. printf("ERROR reading %s, skipping...\n", files[i].c_str());
  271. lines--;
  272. }
  273. else
  274. {
  275. if (!DSP::Assemble(currentSource, codes[i], force))
  276. {
  277. printf("Assemble: Assembly of %s failed due to errors\n", files[i].c_str());
  278. lines--;
  279. }
  280. if (output_size)
  281. {
  282. printf("%s: %zu\n", files[i].c_str(), codes[i].size());
  283. }
  284. }
  285. }
  286. const std::string header = CodesToHeader(codes, files);
  287. File::WriteStringToFile(output_header_name + ".h", header);
  288. }
  289. else
  290. {
  291. std::vector<u16> code;
  292. if (!DSP::Assemble(source, code, force))
  293. {
  294. printf("Assemble: Assembly failed due to errors\n");
  295. return false;
  296. }
  297. if (output_size)
  298. {
  299. printf("%s: %zu\n", input_name.c_str(), code.size());
  300. }
  301. if (!output_name.empty())
  302. {
  303. const std::string binary_code = DSP::CodeToBinaryStringBE(code);
  304. File::WriteStringToFile(output_name, binary_code);
  305. }
  306. if (!output_header_name.empty())
  307. {
  308. const std::string header = CodeToHeader(code, input_name);
  309. File::WriteStringToFile(output_header_name + ".h", header);
  310. }
  311. }
  312. }
  313. source.clear();
  314. if (!output_size)
  315. printf("Assembly completed successfully!\n");
  316. return true;
  317. }
  318. static bool IsHelpFlag(const std::string& argument)
  319. {
  320. return argument == "--help" || argument == "-?";
  321. }
  322. // Usage:
  323. // Disassemble a file:
  324. // dsptool -d -o asdf.txt asdf.bin
  325. // Disassemble a file, output to standard output:
  326. // dsptool -d asdf.bin
  327. // Assemble a file:
  328. // dsptool [-f] -o asdf.bin asdf.txt
  329. // Assemble a file, output header:
  330. // dsptool [-f] -h asdf.h asdf.txt
  331. // Print results from DSPSpy register dump
  332. // dsptool -p dsp_dump0.bin
  333. int main(int argc, const char* argv[])
  334. {
  335. if (argc == 1 || (argc == 2 && IsHelpFlag(argv[1])))
  336. {
  337. printf("USAGE: DSPTool [-?] [--help] [-f] [-d] [-m] [-p <FILE>] [-o <FILE>] [-h <FILE>] <DSP "
  338. "ASSEMBLER FILE>\n");
  339. printf("-? / --help: Prints this message\n");
  340. printf("-d: Disassemble\n");
  341. printf("-m: Input file contains a list of files (Header assembly only)\n");
  342. printf("-s: Print the final size in bytes (only)\n");
  343. printf("-f: Force assembly (errors are not critical)\n");
  344. printf("-o <OUTPUT FILE>: Results from stdout redirected to a file\n");
  345. printf("-h <HEADER FILE>: Output assembly results to a header\n");
  346. printf("-p <DUMP FILE>: Print results of DSPSpy register dump\n");
  347. printf("-ps <DUMP FILE>: Print results of DSPSpy register dump (disable SR output)\n");
  348. printf("-pm <DUMP FILE>: Print results of DSPSpy register dump (convert PROD values)\n");
  349. printf("-psm <DUMP FILE>: Print results of DSPSpy register dump (convert PROD values/disable "
  350. "SR output)\n");
  351. return 0;
  352. }
  353. std::string input_name;
  354. std::string output_header_name;
  355. std::string output_name;
  356. bool disassemble = false, compare = false, multiple = false, outputSize = false, force = false,
  357. print_results = false, print_results_prodhack = false, print_results_srhack = false;
  358. for (int i = 1; i < argc; i++)
  359. {
  360. const std::string argument = argv[i];
  361. if (argument == "-d")
  362. {
  363. disassemble = true;
  364. }
  365. else if (argument == "-o")
  366. {
  367. if (++i < argc)
  368. output_name = argv[i];
  369. }
  370. else if (argument == "-h")
  371. {
  372. if (++i < argc)
  373. output_header_name = argv[i];
  374. }
  375. else if (argument == "-c")
  376. {
  377. compare = true;
  378. }
  379. else if (argument == "-s")
  380. {
  381. outputSize = true;
  382. }
  383. else if (argument == "-m")
  384. {
  385. multiple = true;
  386. }
  387. else if (argument == "-f")
  388. {
  389. force = true;
  390. }
  391. else if (argument == "-p")
  392. {
  393. print_results = true;
  394. }
  395. else if (argument == "-ps")
  396. {
  397. print_results = true;
  398. print_results_srhack = true;
  399. }
  400. else if (argument == "-pm")
  401. {
  402. print_results = true;
  403. print_results_prodhack = true;
  404. }
  405. else if (argument == "-psm")
  406. {
  407. print_results = true;
  408. print_results_srhack = true;
  409. print_results_prodhack = true;
  410. }
  411. else
  412. {
  413. if (!input_name.empty())
  414. {
  415. printf("ERROR: Can only take one input file.\n");
  416. return 1;
  417. }
  418. input_name = argv[i];
  419. if (!File::Exists(input_name))
  420. {
  421. printf("ERROR: Input path does not exist.\n");
  422. return 1;
  423. }
  424. }
  425. }
  426. if (multiple && (compare || disassemble || !output_name.empty() || input_name.empty()))
  427. {
  428. printf("ERROR: Multiple files can only be used with assembly "
  429. "and must compile a header file.\n");
  430. return 1;
  431. }
  432. if (compare)
  433. {
  434. return PerformBinaryComparison(input_name, output_name) ? 0 : 1;
  435. }
  436. if (print_results)
  437. {
  438. PrintResults(input_name, output_name, print_results_srhack, print_results_prodhack);
  439. return 0;
  440. }
  441. if (disassemble)
  442. {
  443. if (!PerformDisassembly(input_name, output_name))
  444. return 1;
  445. }
  446. else
  447. {
  448. if (!PerformAssembly(input_name, output_name, output_header_name, multiple, force, outputSize))
  449. return 1;
  450. }
  451. return 0;
  452. }