2009-04-12 13:12:42 +00:00
|
|
|
// Copyright (C) 2003-2009 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/
|
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
#include <iostream>
|
2009-04-12 13:12:42 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "Common.h"
|
|
|
|
#include "FileUtil.h"
|
2009-06-13 04:55:58 +00:00
|
|
|
#include "StringUtil.h"
|
2009-04-12 13:12:42 +00:00
|
|
|
#include "DSPCodeUtil.h"
|
|
|
|
#include "assemble.h"
|
|
|
|
#include "disassemble.h"
|
|
|
|
|
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
bool Assemble(const char *text, std::vector<u16> &code)
|
2009-04-12 13:12:42 +00:00
|
|
|
{
|
2009-04-14 20:44:03 +00:00
|
|
|
AssemblerSettings settings;
|
|
|
|
settings.pc = 0;
|
|
|
|
// settings.decode_registers = false;
|
|
|
|
// settings.decode_names = false;
|
|
|
|
settings.print_tabs = false;
|
|
|
|
settings.ext_separator = '\'';
|
2009-04-12 13:12:42 +00:00
|
|
|
|
|
|
|
// TODO: fix the terrible api of the assembler.
|
2009-04-14 20:44:03 +00:00
|
|
|
DSPAssembler assembler(settings);
|
2009-04-24 15:31:13 +00:00
|
|
|
if (!assembler.Assemble(text, code)) {
|
2009-04-25 10:38:26 +00:00
|
|
|
std::cerr << assembler.GetErrorString() << std::endl;
|
2009-04-24 15:31:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-04-12 13:12:42 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
bool Disassemble(const std::vector<u16> &code, bool line_numbers, std::string &text)
|
2009-04-12 13:12:42 +00:00
|
|
|
{
|
|
|
|
if (code.empty())
|
|
|
|
return false;
|
|
|
|
|
2009-04-14 22:30:31 +00:00
|
|
|
AssemblerSettings settings;
|
2009-04-14 20:44:03 +00:00
|
|
|
|
2009-04-14 22:30:31 +00:00
|
|
|
// These two prevent roundtripping.
|
|
|
|
settings.show_hex = false;
|
|
|
|
settings.show_pc = line_numbers;
|
|
|
|
settings.ext_separator = '\'';
|
|
|
|
settings.decode_names = false;
|
|
|
|
settings.decode_registers = true;
|
2009-04-12 13:12:42 +00:00
|
|
|
|
2009-04-14 22:30:31 +00:00
|
|
|
DSPDisassembler disasm(settings);
|
2009-06-07 11:06:40 +00:00
|
|
|
bool success = disasm.Disassemble(0, code, 0x0000, text);
|
2009-04-14 22:30:31 +00:00
|
|
|
return success;
|
2009-04-12 13:12:42 +00:00
|
|
|
}
|
|
|
|
|
2009-04-12 14:48:55 +00:00
|
|
|
bool Compare(const std::vector<u16> &code1, const std::vector<u16> &code2)
|
2009-04-12 13:12:42 +00:00
|
|
|
{
|
|
|
|
if (code1.size() != code2.size())
|
|
|
|
printf("Size difference! 1=%i 2=%i\n", (int)code1.size(), (int)code2.size());
|
2009-06-17 21:44:03 +00:00
|
|
|
u32 count_equal = 0;
|
2009-04-14 22:30:31 +00:00
|
|
|
const int min_size = (int)std::min(code1.size(), code2.size());
|
2009-04-18 12:27:24 +00:00
|
|
|
|
|
|
|
AssemblerSettings settings;
|
|
|
|
DSPDisassembler disassembler(settings);
|
2009-04-12 13:12:42 +00:00
|
|
|
for (int i = 0; i < min_size; i++)
|
|
|
|
{
|
|
|
|
if (code1[i] == code2[i])
|
|
|
|
count_equal++;
|
|
|
|
else
|
2009-04-18 12:27:24 +00:00
|
|
|
{
|
|
|
|
std::string line1, line2;
|
|
|
|
u16 pc = i;
|
2009-06-07 11:06:40 +00:00
|
|
|
disassembler.DisOpcode(&code1[0], 0x0000, 2, &pc, line1);
|
2009-04-18 12:27:24 +00:00
|
|
|
pc = i;
|
2009-06-07 11:06:40 +00:00
|
|
|
disassembler.DisOpcode(&code2[0], 0x0000, 2, &pc, line2);
|
2009-04-18 12:27:24 +00:00
|
|
|
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;
|
2009-05-03 11:15:17 +00:00
|
|
|
for (int i = min_size; i < (int)longest.size(); i++)
|
2009-04-18 12:27:24 +00:00
|
|
|
{
|
|
|
|
u16 pc = i;
|
|
|
|
std::string line;
|
2009-06-07 11:06:40 +00:00
|
|
|
disassembler.DisOpcode(&longest[0], 0x0000, 2, &pc, line);
|
2009-04-18 12:27:24 +00:00
|
|
|
printf("!! %s\n", line.c_str());
|
|
|
|
}
|
2009-04-12 13:12:42 +00:00
|
|
|
}
|
|
|
|
printf("Equal instruction words: %i / %i\n", count_equal, min_size);
|
2009-04-12 14:48:55 +00:00
|
|
|
return code1.size() == code2.size() && code1.size() == count_equal;
|
2009-04-12 13:12:42 +00:00
|
|
|
}
|
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
void GenRandomCode(int size, std::vector<u16> &code)
|
2009-04-12 13:12:42 +00:00
|
|
|
{
|
2009-04-25 10:38:26 +00:00
|
|
|
code.resize(size);
|
2009-04-12 14:48:55 +00:00
|
|
|
for (int i = 0; i < size; i++)
|
2009-04-12 13:12:42 +00:00
|
|
|
{
|
2009-04-25 10:38:26 +00:00
|
|
|
code[i] = rand() ^ (rand() << 8);
|
2009-04-12 13:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-13 04:55:58 +00:00
|
|
|
void CodeToHeader(const std::vector<u16> &code, std::string _filename,
|
|
|
|
const char *name, std::string &header)
|
2009-04-12 13:12:42 +00:00
|
|
|
{
|
2009-04-18 12:27:24 +00:00
|
|
|
std::vector<u16> code_copy = code;
|
|
|
|
// Add some nops at the end to align the size a bit.
|
|
|
|
while (code_copy.size() & 7)
|
|
|
|
code_copy.push_back(0);
|
2009-04-12 13:12:42 +00:00
|
|
|
char buffer[1024];
|
2009-04-25 10:38:26 +00:00
|
|
|
header.clear();
|
|
|
|
header.reserve(code.size() * 4);
|
2009-06-11 07:11:05 +00:00
|
|
|
header.append("#define NUM_UCODES 1\n\n");
|
2009-06-13 04:55:58 +00:00
|
|
|
std::string filename;
|
|
|
|
SplitPath(_filename, NULL, &filename, NULL);
|
2009-06-21 11:12:48 +00:00
|
|
|
header.append(StringFromFormat("const char* UCODE_NAMES[NUM_UCODES] = {\"%s\"};\n\n", filename.c_str()));
|
2009-04-25 10:38:26 +00:00
|
|
|
header.append("#ifndef _MSCVER\n");
|
2009-06-18 21:15:49 +00:00
|
|
|
header.append("const unsigned short dsp_code[NUM_UCODES][0x1000] = {\n");
|
2009-04-25 10:38:26 +00:00
|
|
|
header.append("#else\n");
|
2009-06-18 21:15:49 +00:00
|
|
|
header.append("const unsigned short dsp_code[NUM_UCODES][0x1000] __attribute__ ((aligned (64))) = {\n");
|
2009-06-08 21:15:01 +00:00
|
|
|
header.append("#endif\n\n");
|
|
|
|
|
2009-06-11 07:11:05 +00:00
|
|
|
header.append("\t{\n\t\t");
|
2009-06-17 21:44:03 +00:00
|
|
|
for (u32 j = 0; j < code.size(); j++)
|
2009-06-11 07:11:05 +00:00
|
|
|
{
|
|
|
|
if (j && ((j & 15) == 0))
|
|
|
|
header.append("\n\t\t");
|
|
|
|
sprintf(buffer, "0x%04x, ", code[j]);
|
|
|
|
header.append(buffer);
|
|
|
|
}
|
|
|
|
header.append("\n\t},\n");
|
|
|
|
|
|
|
|
header.append("};\n");
|
|
|
|
}
|
|
|
|
|
2009-06-17 21:44:03 +00:00
|
|
|
void CodesToHeader(const std::vector<u16> *codes, const std::vector<std::string>* filenames,
|
2009-06-13 04:55:58 +00:00
|
|
|
int numCodes, const char *name, std::string &header)
|
2009-06-11 07:11:05 +00:00
|
|
|
{
|
2009-06-17 23:36:04 +00:00
|
|
|
char buffer[1024];
|
2009-06-11 07:11:05 +00:00
|
|
|
int reserveSize = 0;
|
|
|
|
for(int i = 0; i < numCodes; i++)
|
|
|
|
reserveSize += (int)codes[i].size();
|
|
|
|
|
|
|
|
|
|
|
|
header.clear();
|
|
|
|
header.reserve(reserveSize * 4);
|
|
|
|
sprintf(buffer, "#define NUM_UCODES %d\n\n", numCodes);
|
|
|
|
header.append(buffer);
|
2009-06-17 23:36:04 +00:00
|
|
|
header.append("const char* UCODE_NAMES[NUM_UCODES] = {\n");
|
2009-06-13 04:55:58 +00:00
|
|
|
for (int i = 0; i < numCodes; i++)
|
|
|
|
{
|
|
|
|
std::string filename;
|
2009-06-17 23:36:04 +00:00
|
|
|
if (! SplitPath(filenames->at(i), NULL, &filename, NULL))
|
|
|
|
filename = filenames->at(i);
|
|
|
|
sprintf(buffer, "\t\"%s\",\n", filename.c_str());
|
|
|
|
header.append(buffer);
|
2009-06-13 04:55:58 +00:00
|
|
|
}
|
2009-06-17 23:36:04 +00:00
|
|
|
header.append("};\n\n");
|
2009-06-11 07:11:05 +00:00
|
|
|
header.append("#ifndef _MSCVER\n");
|
2009-06-18 20:47:02 +00:00
|
|
|
header.append("const unsigned short dsp_ucode[NUM_UCODES][0x1000] = {\n");
|
2009-06-11 07:11:05 +00:00
|
|
|
header.append("#else\n");
|
2009-06-18 20:47:02 +00:00
|
|
|
header.append("const unsigned short dsp_ucode[NUM_UCODES][0x1000] __attribute__ ((aligned (64))) = {\n");
|
2009-06-11 07:11:05 +00:00
|
|
|
header.append("#endif\n\n");
|
|
|
|
|
|
|
|
for(int i = 0; i < numCodes; i++) {
|
|
|
|
if(codes[i].size() == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::vector<u16> code_copy = codes[i];
|
|
|
|
// Add some nops at the end to align the size a bit.
|
|
|
|
while (code_copy.size() & 7)
|
|
|
|
code_copy.push_back(0);
|
|
|
|
|
2009-06-08 21:15:01 +00:00
|
|
|
header.append("\t{\n\t\t");
|
2009-06-17 21:44:03 +00:00
|
|
|
for (u32 j = 0; j < codes[i].size(); j++)
|
2009-06-08 21:15:01 +00:00
|
|
|
{
|
|
|
|
if (j && ((j & 15) == 0))
|
|
|
|
header.append("\n\t\t");
|
2009-06-11 07:11:05 +00:00
|
|
|
sprintf(buffer, "0x%04x, ", codes[i][j]);
|
2009-06-08 21:15:01 +00:00
|
|
|
header.append(buffer);
|
|
|
|
}
|
|
|
|
header.append("\n\t},\n");
|
2009-04-12 13:12:42 +00:00
|
|
|
}
|
2009-06-08 21:15:01 +00:00
|
|
|
header.append("};\n");
|
2009-04-12 13:12:42 +00:00
|
|
|
}
|
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
void CodeToBinaryStringBE(const std::vector<u16> &code, std::string &str)
|
2009-04-12 13:12:42 +00:00
|
|
|
{
|
2009-04-25 10:38:26 +00:00
|
|
|
str.resize(code.size() * 2);
|
2009-05-03 11:15:17 +00:00
|
|
|
for (int i = 0; i < (int)code.size(); i++)
|
2009-04-12 13:12:42 +00:00
|
|
|
{
|
2009-04-25 10:38:26 +00:00
|
|
|
str[i * 2 + 0] = code[i] >> 8;
|
|
|
|
str[i * 2 + 1] = code[i] & 0xff;
|
2009-04-12 13:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
void BinaryStringBEToCode(const std::string &str, std::vector<u16> &code)
|
2009-04-12 13:12:42 +00:00
|
|
|
{
|
2009-04-25 10:38:26 +00:00
|
|
|
code.resize(str.size() / 2);
|
2009-05-03 11:15:17 +00:00
|
|
|
for (int i = 0; i < (int)code.size(); i++)
|
2009-04-12 13:12:42 +00:00
|
|
|
{
|
2009-04-25 10:38:26 +00:00
|
|
|
code[i] = ((u16)(u8)str[i * 2 + 0] << 8) | ((u16)(u8)str[i * 2 + 1]);
|
2009-04-12 13:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
bool LoadBinary(const char *filename, std::vector<u16> &code)
|
2009-04-12 13:12:42 +00:00
|
|
|
{
|
|
|
|
std::string buffer;
|
2009-04-25 10:38:26 +00:00
|
|
|
if (!File::ReadFileToString(false, filename, buffer))
|
2009-04-12 13:12:42 +00:00
|
|
|
return false;
|
2009-04-14 22:30:31 +00:00
|
|
|
|
2009-04-12 13:12:42 +00:00
|
|
|
BinaryStringBEToCode(buffer, code);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SaveBinary(const std::vector<u16> &code, const char *filename)
|
|
|
|
{
|
|
|
|
std::string buffer;
|
2009-04-25 10:38:26 +00:00
|
|
|
CodeToBinaryStringBE(code, buffer);
|
2009-04-12 13:12:42 +00:00
|
|
|
if (!File::WriteStringToFile(false, buffer, filename))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|