Debian Codegraph @

Source/DSPTool/DSPTool.cpp

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