DSP asm/disasm: improve the shift instructions so they at least round-trip. not 100% sure it's correct though.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@2957 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
hrydgard 2009-04-12 13:43:12 +00:00
parent f6474b98a8
commit d4055b971b
4 changed files with 19 additions and 126 deletions

View File

@ -154,8 +154,8 @@ const DSPOPCTemplate opcodes[] =
{"SBCLR", 0x1200, 0xfff8, DSPInterpreter::sbclr, nop, 1, 1, {{P_IMM, 1, 0, 0, 0x0007}}, NULL, NULL},
{"SBSET", 0x1300, 0xfff8, DSPInterpreter::sbset, nop, 1, 1, {{P_IMM, 1, 0, 0, 0x0007}}, NULL, NULL},
{"LSL", 0x1400, 0xfec0, DSPInterpreter::lsl, nop, 1, 2, {{P_ACCM, 1, 0, 8, 0x0100}, {P_IMM, 1, 0, 0, 0x003f}}, NULL, NULL}, // 0x007f?
{"LSR", 0x1440, 0xfec0, DSPInterpreter::lsr, nop, 1, 2, {{P_ACCM, 1, 0, 8, 0x0100}, {P_IMM, 1, 0, 0, 0x003f}}, NULL, NULL}, // 0x007f?
{"LSL", 0x1400, 0xfec0, DSPInterpreter::lsl, nop, 1, 2, {{P_ACCM, 1, 0, 8, 0x0100}, {P_IMM, 1, 0, 0, 0x007f}}, NULL, NULL}, // 0x007f?
{"LSR", 0x1440, 0xfec0, DSPInterpreter::lsr, nop, 1, 2, {{P_ACCM, 1, 0, 8, 0x0100}, {P_IMM, 1, 0, 0, 0x007f}}, NULL, NULL}, // 0x007f?
{"ASL", 0x1480, 0xfec0, DSPInterpreter::asl, nop, 1, 2, {{P_ACCM, 1, 0, 8, 0x0100}, {P_IMM, 1, 0, 0, 0x007f}}, NULL, NULL},
{"ASR", 0x14c0, 0xfec0, DSPInterpreter::asr, nop, 1, 2, {{P_ACCM, 1, 0, 8, 0x0100}, {P_IMM, 1, 0, 0, 0x007f}}, NULL, NULL},

View File

@ -213,12 +213,12 @@ s32 strtoval(const char *str)
if (ptr[0] == '#')
{
ptr++;
negative = true; // Wow! Double # (needed one to get in here) ]negates???
negative = true; // Wow! Double # (needed one to get in here) negates???
}
if (ptr[0] == '-')
{
ptr++;
negative = true; // Wow! # negates???
negative = true;
}
if (ptr[0] == '0')
{

View File

@ -46,10 +46,7 @@ char* gd_dis_params(gd_globals_t* gdg, const DSPOPCTemplate* opc, u16 op1, u16 o
for (int j = 0; j < opc->param_count; j++)
{
if (j > 0)
{
sprintf(buf, ", ");
buf += strlen(buf);
}
buf += sprintf(buf, ", ");
if (opc->params[j].loc >= 1)
val = op2;
@ -63,8 +60,7 @@ char* gd_dis_params(gd_globals_t* gdg, const DSPOPCTemplate* opc, u16 op1, u16 o
else
val = val >> opc->params[j].lshift;
u32 type;
type = opc->params[j].type;
u32 type = opc->params[j].type;
if ((type & 0xff) == 0x10)
type &= 0xff00;
@ -106,7 +102,7 @@ char* gd_dis_params(gd_globals_t* gdg, const DSPOPCTemplate* opc, u16 op1, u16 o
if (opc->params[j].size != 2)
{
if (opc->params[j].mask == 0x007f) // LSL, LSR, ASL, ASR
sprintf(buf, "#%d", val < 64 ? val : -(0x80 - (s32)val));
sprintf(buf, "#%d", (val & 0x40) ? (val | 0xFFFFFFC0) : val);
else
sprintf(buf, "#0x%02x", val);
}

View File

@ -17,8 +17,7 @@
#include "Common.h"
#include "FileUtil.h"
#include "assemble.h"
#include "disassemble.h"
#include "DSPCodeUtil.h"
// Stub out the dsplib host stuff, since this is just a simple cmdline tools.
u8 DSPHost_ReadHostMemory(u32 addr) { return 0; }
@ -26,124 +25,22 @@ bool DSPHost_OnThread() { return false; }
bool DSPHost_Running() { return true; }
u32 DSPHost_CodeLoaded(const u8 *ptr, int size) {return 0x1337c0de;}
bool Assemble(const char *text, std::vector<u16> *code)
{
const char *fname = "tmp.asm";
gd_globals_t gdg;
memset(&gdg, 0, sizeof(gdg));
gdg.pc = 0;
// gdg.decode_registers = false;
// gdg.decode_names = false;
gdg.print_tabs = false;
gdg.ext_separator = '\'';
gdg.buffer = 0;
if (!File::WriteStringToFile(true, text, fname))
return false;
// TODO: fix the terrible api of the assembler.
gd_ass_init_pass(1);
if (!gd_ass_file(&gdg, fname, 1))
return false;
gd_ass_init_pass(2);
if (!gd_ass_file(&gdg, fname, 2))
return false;
code->resize(gdg.buffer_size);
for (int i = 0; i < gdg.buffer_size; i++) {
(*code)[i] = *(u16 *)(gdg.buffer + i * 2);
}
return true;
}
bool Disassemble(const std::vector<u16> &code, std::string *text)
{
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)
{
gd_globals_t gdg;
memset(&gdg, 0, sizeof(gdg));
// These two prevent roundtripping.
gdg.show_hex = false;
gdg.show_pc = false;
gdg.ext_separator = '\'';
gdg.decode_names = false;
gdg.decode_registers = true;
bool success = gd_dis_file(&gdg, tmp1, t);
fclose(t);
File::ReadStringFromFile(true, tmp2, text);
return success;
}
return false;
}
void 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());
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
printf("!! %04x : %04x vs %04x\n", i, code1[i], code2[i]);
}
printf("Equal instruction words: %i / %i\n", count_equal, min_size);
}
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(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");
}
// This test goes from text ASM to binary to text ASM and once again back to binary.
// Then the two binaries are compared.
bool RoundTrip(const std::vector<u16> &code1)
{
std::vector<u16> code2;
std::string text;
Disassemble(code1, &text);
Assemble(text.c_str(), &code2);
if (!Disassemble(code1, &text))
{
printf("RoundTrip: Disassembly failed.\n");
return false;
}
if (!Assemble(text.c_str(), &code2))
{
printf("RoundTrip: Assembly failed.\n");
return false;
}
Compare(code1, code2);
return true;
}
@ -227,7 +124,7 @@ void RunAsmTests()
std::string dsp_test;
//File::ReadStringFromFile(true, "C:/devkitPro/examples/wii/asndlib/dsptest/dsp_test.ds", &dsp_test);
File::ReadStringFromFile(true, "C:/devkitPro/trunk/libogc/libasnd/dsp_mixer/dsp_mixer.s", &dsp_test);
File::ReadFileToString(true, "C:/devkitPro/trunk/libogc/libasnd/dsp_mixer/dsp_mixer.s", &dsp_test);
// This is CLOSE to working. Sorry about the local path btw. This is preliminary code.
SuperTrip(dsp_test.c_str());