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/
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "Common.h"
|
|
|
|
#include "FileUtil.h"
|
|
|
|
#include "DSPCodeUtil.h"
|
|
|
|
#include "assemble.h"
|
|
|
|
#include "disassemble.h"
|
|
|
|
|
|
|
|
|
|
|
|
bool Assemble(const char *text, std::vector<u16> *code)
|
|
|
|
{
|
|
|
|
const char *fname = "tmp.asm";
|
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
|
|
|
|
|
|
|
if (!File::WriteStringToFile(true, text, fname))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// TODO: fix the terrible api of the assembler.
|
2009-04-14 20:44:03 +00:00
|
|
|
DSPAssembler assembler(settings);
|
2009-04-12 14:48:55 +00:00
|
|
|
assembler.gd_ass_init_pass(1);
|
2009-04-14 20:44:03 +00:00
|
|
|
if (!assembler.gd_ass_file(fname, 1))
|
2009-04-12 13:12:42 +00:00
|
|
|
return false;
|
2009-04-12 14:48:55 +00:00
|
|
|
assembler.gd_ass_init_pass(2);
|
2009-04-14 20:44:03 +00:00
|
|
|
if (!assembler.gd_ass_file(fname, 2))
|
2009-04-12 13:12:42 +00:00
|
|
|
return false;
|
|
|
|
|
2009-04-14 20:44:03 +00:00
|
|
|
code->resize(assembler.gdg_buffer_size);
|
|
|
|
for (int i = 0; i < assembler.gdg_buffer_size; i++) {
|
|
|
|
(*code)[i] = *(u16 *)(assembler.gdg_buffer + i * 2);
|
2009-04-12 13:12:42 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-12 14:48:55 +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;
|
|
|
|
const char *tmp1 = "tmp1.bin";
|
|
|
|
const char *tmp2 = "tmp.txt";
|
|
|
|
|
|
|
|
// First we have to dump the code to a bin file.
|
|
|
|
FILE *f = fopen(tmp1, "wb");
|
|
|
|
fwrite(&code[0], 1, code.size() * 2, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
FILE* t = fopen(tmp2, "w");
|
|
|
|
if (t != NULL)
|
|
|
|
{
|
2009-04-14 20:44:03 +00:00
|
|
|
AssemblerSettings settings;
|
2009-04-12 13:12:42 +00:00
|
|
|
|
|
|
|
// These two prevent roundtripping.
|
2009-04-14 20:44:03 +00:00
|
|
|
settings.show_hex = false;
|
|
|
|
settings.show_pc = line_numbers;
|
|
|
|
settings.ext_separator = '\'';
|
|
|
|
settings.decode_names = false;
|
|
|
|
settings.decode_registers = true;
|
|
|
|
|
|
|
|
DSPDisassembler disasm(settings);
|
|
|
|
bool success = disasm.gd_dis_file(tmp1, t);
|
2009-04-12 13:12:42 +00:00
|
|
|
fclose(t);
|
|
|
|
|
|
|
|
File::ReadFileToString(true, tmp2, text);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
int count_equal = 0;
|
|
|
|
const int min_size = std::min(code1.size(), code2.size());
|
|
|
|
for (int i = 0; i < min_size; i++)
|
|
|
|
{
|
|
|
|
if (code1[i] == code2[i])
|
|
|
|
count_equal++;
|
|
|
|
else
|
2009-04-12 14:48:55 +00:00
|
|
|
printf("!! %i : %04x vs %04x\n", i, code1[i], code2[i]);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void GenRandomCode(int size, std::vector<u16> *code)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
(*code)[i] = rand() ^ (rand() << 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeToHeader(std::vector<u16> *code, const char *name, std::string *header)
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
header->clear();
|
|
|
|
header->reserve(code->size() * 4);
|
|
|
|
header->append("#ifndef _MSCVER\n");
|
|
|
|
sprintf(buffer, "const __declspec(align:64) unsigned short %s = {\n");
|
|
|
|
header->append(buffer);
|
|
|
|
header->append("#else\n");
|
|
|
|
sprintf(buffer, "const unsigned short %s __attribute__(aligned:64) = {\n");
|
|
|
|
header->append(buffer);
|
|
|
|
header->append("#endif\n\n ");
|
|
|
|
for (int i = 0; i < code->size(); i++)
|
|
|
|
{
|
|
|
|
if (((i + 1) & 15) == 0)
|
|
|
|
header->append("\n ");
|
|
|
|
sprintf(buffer, "%02x, ", code[i]);
|
|
|
|
header->append(buffer);
|
|
|
|
}
|
|
|
|
header->append("\n};\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeToBinaryStringBE(const std::vector<u16> &code, std::string *str)
|
|
|
|
{
|
|
|
|
str->resize(code.size() * 2);
|
|
|
|
for (int i = 0; i < 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 < code->size(); i++)
|
|
|
|
{
|
|
|
|
(*code)[i] = (str[i * 2 + 0] << 8) | (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;
|
|
|
|
}
|