summaryrefslogtreecommitdiff
path: root/Source/Core/DSPCore/Src/DSPCodeUtil.cpp
blob: ca9c01d5dcbde52366f5a1100edd3e290f699218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (C) 2003 Dolphin Project.

// 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 <iostream>
#include <vector>

#include "Common.h"
#include "FileUtil.h"
#include "StringUtil.h"
#include "DSPCodeUtil.h"
#include "assemble.h"
#include "disassemble.h"


bool Assemble(const char *text, std::vector<u16> &code, bool force)
{
	AssemblerSettings settings;
	//	settings.pc = 0;
	// settings.decode_registers = false;
	// settings.decode_names = false;
	settings.force = force;
	//	settings.print_tabs = false;
	//	settings.ext_separator = '\'';

	// TODO: fix the terrible api of the assembler.
	DSPAssembler assembler(settings);
	if (!assembler.Assemble(text, code)) {
		std::cerr << assembler.GetErrorString() << std::endl;
		return false;
	}

	return true;
}

bool Disassemble(const std::vector<u16> &code, bool line_numbers, std::string &text)
{
	if (code.empty())
		return false;

	AssemblerSettings settings;

	// These two prevent roundtripping.
	settings.show_hex = true;
	settings.show_pc = line_numbers;
	settings.ext_separator = '\'';
	settings.decode_names = true;
	settings.decode_registers = true;

	DSPDisassembler disasm(settings);
	bool success = disasm.Disassemble(0, code, 0x0000, text);
	return success;
}

bool Compare(const std::vector<u16> &code1, const std::vector<u16> &code2)
{		
	if (code1.size() != code2.size())
		printf("Size difference! 1=%i 2=%i\n", (int)code1.size(), (int)code2.size());
	u32 count_equal = 0;
	const int min_size = (int)std::min(code1.size(), code2.size());

	AssemblerSettings settings;
	DSPDisassembler disassembler(settings);
	for (int i = 0; i < min_size; i++)
	{
		if (code1[i] == code2[i])
			count_equal++;
		else
		{
			std::string line1, line2;
			u16 pc = i;
			disassembler.DisOpcode(&code1[0], 0x0000, 2, &pc, line1);
			pc = i;
			disassembler.DisOpcode(&code2[0], 0x0000, 2, &pc, line2);
			printf("!! %04x : %04x vs %04x - %s  vs  %s\n", i, code1[i], code2[i], line1.c_str(), line2.c_str());
		}
	}
	if (code2.size() != code1.size())
	{
		printf("Extra code words:\n");
		const std::vector<u16> &longest = code1.size() > code2.size() ? code1 : code2;
		for (int i = min_size; i < (int)longest.size(); i++)
		{
			u16 pc = i;
			std::string line;
			disassembler.DisOpcode(&longest[0], 0x0000, 2, &pc, line);
			printf("!! %s\n", line.c_str());
		}
	}
	printf("Equal instruction words: %i / %i\n", count_equal, min_size);
	return code1.size() == code2.size() && code1.size() == count_equal;
}

void GenRandomCode(int size, std::vector<u16> &code) 
{
	code.resize(size);
	for (int i = 0; i < size; i++)
	{
		code[i] = rand() ^ (rand() << 8);
	}
}

void CodeToHeader(const std::vector<u16> &code, std::string _filename,
				  const char *name, std::string &header)
{
	std::vector<u16> code_padded = code;
	// Pad with nops to 32byte boundary
	while (code_padded.size() & 0x7f)
		code_padded.push_back(0);

	char buffer[1024];
	header.clear();
	header.reserve(code_padded.size() * 4);
	header.append("#define NUM_UCODES 1\n\n");
	std::string filename;
	SplitPath(_filename, NULL, &filename, NULL);
	header.append(StringFromFormat("const char* UCODE_NAMES[NUM_UCODES] = {\"%s\"};\n\n", filename.c_str()));
	header.append("const unsigned short dsp_code[NUM_UCODES][0x1000] = {\n");

	header.append("\t{\n\t\t");
	for (u32 j = 0; j < code_padded.size(); j++)
	{
		if (j && ((j & 15) == 0))
			header.append("\n\t\t");
		sprintf(buffer, "0x%04x, ", code_padded[j]);
		header.append(buffer);
	}
	header.append("\n\t},\n");

	header.append("};\n");
}

void CodesToHeader(const std::vector<u16> *codes, const std::vector<std::string>* filenames,
				   int numCodes, const char *name, std::string &header)
{
	std::vector<std::vector<u16> > codes_padded;
	char buffer[1024];
	int reserveSize = 0;
	for(int i = 0; i < numCodes; i++)
	{
		codes_padded.push_back(codes[i]);
		// Pad with nops to 32byte boundary
		while (codes_padded.at(i).size() & 0x7f)
			codes_padded.at(i).push_back(0);

		reserveSize += (int)codes_padded.at(i).size();
	}

	
	header.clear();
	header.reserve(reserveSize * 4);
	sprintf(buffer, "#define NUM_UCODES %d\n\n", numCodes);
	header.append(buffer);
	header.append("const char* UCODE_NAMES[NUM_UCODES] = {\n");
	for (int i = 0; i < numCodes; i++)
	{
		std::string filename;
		if (! SplitPath(filenames->at(i), NULL, &filename, NULL))
			filename = filenames->at(i);
		sprintf(buffer, "\t\"%s\",\n", filename.c_str());
		header.append(buffer);
	}
	header.append("};\n\n");
	header.append("const unsigned short dsp_code[NUM_UCODES][0x1000] = {\n");

	for(int i = 0; i < numCodes; i++)
	{
		if(codes[i].size() == 0)
			continue;

		header.append("\t{\n\t\t");
		for (u32 j = 0; j < codes_padded.at(i).size(); j++) 
		{
			if (j && ((j & 15) == 0))
				header.append("\n\t\t");
			sprintf(buffer, "0x%04x, ", codes_padded.at(i).at(j));
			header.append(buffer);
		}
		header.append("\n\t},\n");
	}
	header.append("};\n");
}

void CodeToBinaryStringBE(const std::vector<u16> &code, std::string &str)
{
	str.resize(code.size() * 2);
	for (int i = 0; i < (int)code.size(); i++)
	{
		str[i * 2 + 0] = code[i] >> 8;
		str[i * 2 + 1] = code[i] & 0xff;
	}
}

void BinaryStringBEToCode(const std::string &str, std::vector<u16> &code)
{
	code.resize(str.size() / 2);
	for (int i = 0; i < (int)code.size(); i++)
	{
		code[i] = ((u16)(u8)str[i * 2 + 0] << 8) | ((u16)(u8)str[i * 2 + 1]);
	}
}

bool LoadBinary(const char *filename, std::vector<u16> &code)
{
	std::string buffer;
	if (!File::ReadFileToString(false, filename, buffer))
		return false;

	BinaryStringBEToCode(buffer, code);
	return true;
}

bool SaveBinary(const std::vector<u16> &code, const char *filename)
{
	std::string buffer;
	CodeToBinaryStringBE(code, buffer);
	if (!File::WriteStringToFile(false, buffer, filename))
		return false;
	return true;
}