| 
									
										
										
										
											2009-03-29 21:00:26 +00:00
										 |  |  | // Copyright (C) 2003-2009 Dolphin Project.
 | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, version 2.0.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU General Public License 2.0 for more details.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // A copy of the GPL 2.0 should have been included with the program.
 | 
					
						
							|  |  |  | // If not, see http://www.gnu.org/licenses/
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Official SVN repository and contact information can be found at
 | 
					
						
							|  |  |  | // http://code.google.com/p/dolphin-emu/
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Common.h"
 | 
					
						
							|  |  |  | #include "Globals.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-14 22:30:31 +00:00
										 |  |  | #include "FileUtil.h"
 | 
					
						
							| 
									
										
										
										
											2009-05-01 20:06:24 +00:00
										 |  |  | #include "DSPCore.h"
 | 
					
						
							| 
									
										
										
										
											2009-04-14 22:30:31 +00:00
										 |  |  | #include "DSPCodeUtil.h"
 | 
					
						
							| 
									
										
										
										
											2009-04-03 19:29:15 +00:00
										 |  |  | #include "Tools.h"
 | 
					
						
							|  |  |  | #include "disassemble.h"
 | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | #include "gdsp_interpreter.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-14 22:30:31 +00:00
										 |  |  | bool DumpDSPCode(const u8 *code_be, int size_in_bytes, u32 crc) | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-04-14 22:30:31 +00:00
										 |  |  | 	char binFile[MAX_PATH]; | 
					
						
							|  |  |  | 	char txtFile[MAX_PATH]; | 
					
						
							|  |  |  | 	sprintf(binFile, "%sDSP_UC_%08X.bin", FULL_DSP_DUMP_DIR, crc); | 
					
						
							|  |  |  | 	sprintf(txtFile, "%sDSP_UC_%08X.txt", FULL_DSP_DUMP_DIR, crc); | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-14 22:30:31 +00:00
										 |  |  | 	FILE* pFile = fopen(binFile, "wb"); | 
					
						
							| 
									
										
										
										
											2009-04-05 15:46:47 +00:00
										 |  |  | 	if (pFile) | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-04-14 22:30:31 +00:00
										 |  |  | 		fwrite(code_be, size_in_bytes, 1, pFile); | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | 		fclose(pFile); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-04-14 22:30:31 +00:00
										 |  |  | 		PanicAlert("Cant open file (%s) to dump UCode!!", binFile); | 
					
						
							| 
									
										
										
										
											2009-04-03 19:29:15 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-14 22:30:31 +00:00
										 |  |  | 	// Load the binary back in.
 | 
					
						
							|  |  |  | 	std::vector<u16> code; | 
					
						
							| 
									
										
										
										
											2009-04-25 10:38:26 +00:00
										 |  |  | 	LoadBinary(binFile, code); | 
					
						
							| 
									
										
										
										
											2009-04-03 19:29:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-14 22:30:31 +00:00
										 |  |  | 	AssemblerSettings settings; | 
					
						
							|  |  |  | 	settings.show_hex = true; | 
					
						
							|  |  |  | 	settings.show_pc = true; | 
					
						
							| 
									
										
										
										
											2009-04-18 11:31:37 +00:00
										 |  |  | 	settings.ext_separator = '\''; | 
					
						
							| 
									
										
										
										
											2009-04-14 22:30:31 +00:00
										 |  |  | 	settings.decode_names = true; | 
					
						
							|  |  |  | 	settings.decode_registers = true; | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-14 22:30:31 +00:00
										 |  |  | 	std::string text; | 
					
						
							|  |  |  | 	DSPDisassembler disasm(settings); | 
					
						
							| 
									
										
										
										
											2009-06-07 11:06:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!disasm.Disassemble(0, code, 0x0000, text)) | 
					
						
							| 
									
										
										
										
											2009-04-03 19:29:15 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2009-04-14 22:30:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return File::WriteStringToFile(true, text, txtFile); | 
					
						
							| 
									
										
										
										
											2009-04-03 19:29:15 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-30 03:27:31 +00:00
										 |  |  | u32 GenerateCRC(const unsigned char* _pBuffer, int _pLength) | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	unsigned long CRC = 0xFFFFFFFF; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (_pLength--) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		unsigned long Temp = (unsigned long)((CRC & 0xFF) ^ *_pBuffer++); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (int j = 0; j < 8; j++) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (Temp & 0x1) | 
					
						
							|  |  |  | 				Temp = (Temp >> 1) ^ 0xEDB88320; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				Temp >>= 1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		CRC = (CRC >> 8) ^ Temp; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-09 14:33:34 +00:00
										 |  |  | 	return CRC ^ 0xFFFFFFFF; | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-03 22:23:15 +00:00
										 |  |  | // TODO make this useful :p
 | 
					
						
							| 
									
										
										
										
											2009-03-30 03:27:31 +00:00
										 |  |  | bool DumpCWCode(u32 _Address, u32 _Length) | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-04-03 18:04:08 +00:00
										 |  |  | 	char filename[256]; | 
					
						
							| 
									
										
										
										
											2009-04-03 22:23:15 +00:00
										 |  |  | 	sprintf(filename, "%sDSP_UCode.bin", FULL_DSP_DUMP_DIR); | 
					
						
							| 
									
										
										
										
											2009-04-03 18:04:08 +00:00
										 |  |  | 	FILE* pFile = fopen(filename, "wb"); | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (pFile != NULL) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		for (size_t i = _Address; i < _Address + _Length; i++) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-04-08 21:46:54 +00:00
										 |  |  | 			u16 val = g_dsp.iram[i]; | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | 			fprintf(pFile, "    cw 0x%04x \n", val); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fclose(pFile); | 
					
						
							| 
									
										
										
										
											2009-04-03 22:23:15 +00:00
										 |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-03 22:23:15 +00:00
										 |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2009-03-23 09:10:32 +00:00
										 |  |  | } |