2013-04-17 21:09:55 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2014-02-19 11:52:31 -07:00
|
|
|
#include "Common/Common.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "Core/DSP/DSPCodeUtil.h"
|
2014-03-09 11:00:51 -06:00
|
|
|
#include "Core/DSP/DSPHost.h"
|
2014-02-19 11:52:31 -07:00
|
|
|
#include "Core/DSP/DSPTables.h"
|
2009-07-05 20:10:26 -06:00
|
|
|
|
|
|
|
// Stub out the dsplib host stuff, since this is just a simple cmdline tools.
|
2014-03-09 11:00:51 -06:00
|
|
|
u8 DSPHost::ReadHostMemory(u32 addr) { return 0; }
|
|
|
|
void DSPHost::WriteHostMemory(u8 value, u32 addr) {}
|
|
|
|
void DSPHost::OSD_AddMessage(const std::string& str, u32 ms) {}
|
|
|
|
bool DSPHost::OnThread() { return false; }
|
|
|
|
bool DSPHost::IsWiiHost() { return false; }
|
|
|
|
void DSPHost::CodeLoaded(const u8 *ptr, int size) {}
|
|
|
|
void DSPHost::InterruptRequest() {}
|
|
|
|
void DSPHost::UpdateDebugger() {}
|
2009-07-05 20:10:26 -06:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
if (!Disassemble(code1, false, text))
|
|
|
|
{
|
|
|
|
printf("RoundTrip: Disassembly failed.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!Assemble(text.c_str(), code2))
|
|
|
|
{
|
|
|
|
printf("RoundTrip: Assembly failed.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!Compare(code1, code2))
|
|
|
|
{
|
|
|
|
Disassemble(code1, true, text);
|
|
|
|
printf("%s", text.c_str());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test goes from text ASM to binary to text ASM and once again back to binary.
|
|
|
|
// Very convenient for testing. Then the two binaries are compared.
|
|
|
|
bool SuperTrip(const char *asm_code)
|
|
|
|
{
|
|
|
|
std::vector<u16> code1, code2;
|
|
|
|
std::string text;
|
|
|
|
if (!Assemble(asm_code, code1))
|
|
|
|
{
|
|
|
|
printf("SuperTrip: First assembly failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
printf("First assembly: %i words\n", (int)code1.size());
|
|
|
|
if (!Disassemble(code1, false, text))
|
|
|
|
{
|
|
|
|
printf("SuperTrip: Disassembly failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Disass:\n");
|
|
|
|
printf("%s", text.c_str());
|
|
|
|
}
|
|
|
|
if (!Assemble(text.c_str(), code2))
|
|
|
|
{
|
|
|
|
printf("SuperTrip: Second assembly failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
std::string text2;
|
|
|
|
Disassemble(code1, true, &text1);
|
|
|
|
Disassemble(code2, true, &text2);
|
2013-11-04 04:33:41 -07:00
|
|
|
File::WriteStringToFile(text1, "code1.txt");
|
|
|
|
File::WriteStringToFile(text2, "code2.txt");
|
2009-07-05 20:10:26 -06:00
|
|
|
*/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunAsmTests()
|
|
|
|
{
|
|
|
|
bool fail = false;
|
|
|
|
#define CHK(a) if (!SuperTrip(a)) printf("FAIL\n%s\n", a), fail = true;
|
|
|
|
|
|
|
|
// Let's start out easy - a trivial instruction..
|
|
|
|
CHK(" NOP\n");
|
|
|
|
|
|
|
|
// Now let's do several.
|
|
|
|
CHK(" NOP\n"
|
|
|
|
" NOP\n"
|
|
|
|
" NOP\n");
|
|
|
|
|
|
|
|
// Turning it up a notch.
|
|
|
|
CHK(" SET16\n"
|
|
|
|
" SET40\n"
|
|
|
|
" CLR15\n"
|
|
|
|
" M0\n"
|
|
|
|
" M2\n");
|
|
|
|
|
|
|
|
// Time to try labels and parameters, and comments.
|
|
|
|
CHK("DIRQ_TEST: equ 0xfffb ; DSP Irq Request\n"
|
|
|
|
" si @0xfffc, #0x8888\n"
|
|
|
|
" si @0xfffd, #0xbeef\n"
|
|
|
|
" si @DIRQ_TEST, #0x0001\n");
|
|
|
|
|
|
|
|
// Let's see if registers roundtrip. Also try predefined labels.
|
|
|
|
CHK(" si @0xfffc, #0x8888\n"
|
|
|
|
" si @0xfffd, #0xbeef\n"
|
|
|
|
" si @DIRQ, #0x0001\n");
|
|
|
|
|
|
|
|
// Let's try some messy extended instructions.
|
|
|
|
//CHK(" MULMV'SN $AX0.L, $AX0.H, $ACC0 : @$AR2, $AC1.M\n");
|
|
|
|
|
|
|
|
//" ADDAXL'MV $ACC1, $AX1.L : $AX1.H, $AC1.M\n");
|
|
|
|
// Let's get brutal. We generate random code bytes and make sure that they can
|
|
|
|
// be roundtripped. We don't expect it to always succeed but it'll be sure to generate
|
|
|
|
|
|
|
|
// interesting test cases.
|
|
|
|
/*
|
|
|
|
std::vector<u16> hermes;
|
|
|
|
if (!LoadBinary("testdata/hermes.bin", &hermes))
|
|
|
|
PanicAlert("Failed to load hermes rom");
|
|
|
|
RoundTrip(hermes);
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
std::vector<u16> code;
|
|
|
|
std::string text_orig;
|
2013-11-04 04:33:41 -07:00
|
|
|
File::ReadFileToString("testdata/dsp_test.S", &text_orig);
|
2009-07-05 20:10:26 -06:00
|
|
|
if (!Assemble(text_orig.c_str(), &code))
|
|
|
|
{
|
|
|
|
printf("SuperTrip: First assembly failed\n");
|
|
|
|
return;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
std::vector<u16> code;
|
|
|
|
code.clear();
|
|
|
|
for (int i = 0; i < sizeof(dsp_test)/4; i++)
|
|
|
|
{
|
|
|
|
code.push_back(dsp_test[i] >> 16);
|
|
|
|
code.push_back(dsp_test[i] & 0xFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
SaveBinary(code, "dsp_test2.bin");
|
|
|
|
RoundTrip(code);
|
|
|
|
}*/
|
|
|
|
//if (Compare(code, hermes))
|
|
|
|
// printf("Successs\n");
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
std::vector<u16> code;
|
|
|
|
std::string text;
|
|
|
|
LoadBinary("testdata/dsp_test.bin", &code);
|
|
|
|
Disassemble(code, true, &text);
|
|
|
|
Assemble(text.c_str(), &code);
|
|
|
|
Disassemble(code, true, &text);
|
|
|
|
printf("%s", text.c_str());
|
|
|
|
}*/
|
|
|
|
/*
|
|
|
|
puts("Insane Random Code Test\n");
|
|
|
|
std::vector<u16> rand_code;
|
|
|
|
GenRandomCode(30, &rand_code);
|
|
|
|
std::string rand_code_text;
|
|
|
|
Disassemble(rand_code, true, &rand_code_text);
|
|
|
|
printf("%s", rand_code_text.c_str());
|
|
|
|
RoundTrip(rand_code);
|
|
|
|
|
|
|
|
|
2013-11-04 04:33:41 -07:00
|
|
|
if (File::ReadFileToString("C:/devkitPro/examples/wii/asndlib/dsptest/dsp_test.ds", &dsp_test))
|
2009-07-05 20:10:26 -06:00
|
|
|
SuperTrip(dsp_test.c_str());
|
|
|
|
|
2013-11-04 04:33:41 -07:00
|
|
|
//.File::ReadFileToString("C:/devkitPro/trunk/libogc/libasnd/dsp_mixer/dsp_mixer.s", &dsp_test);
|
2009-07-05 20:10:26 -06:00
|
|
|
// This is CLOSE to working. Sorry about the local path btw. This is preliminary code.
|
|
|
|
*/
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2009-07-05 20:10:26 -06:00
|
|
|
std::string dsp_test;
|
2013-11-04 04:33:41 -07:00
|
|
|
if (File::ReadFileToString("Testdata/dsp_test.s", dsp_test))
|
2009-07-05 20:10:26 -06:00
|
|
|
fail = fail || !SuperTrip(dsp_test.c_str());
|
|
|
|
if (!fail)
|
|
|
|
printf("All passed!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Usage:
|
|
|
|
// Run internal tests:
|
|
|
|
// dsptool test
|
|
|
|
// Disassemble a file:
|
|
|
|
// dsptool -d -o asdf.txt asdf.bin
|
|
|
|
// Disassemble a file, output to standard output:
|
|
|
|
// dsptool -d asdf.bin
|
|
|
|
// Assemble a file:
|
2009-07-19 14:14:09 -06:00
|
|
|
// dsptool [-f] -o asdf.bin asdf.txt
|
2009-07-05 20:10:26 -06:00
|
|
|
// Assemble a file, output header:
|
2009-07-19 14:14:09 -06:00
|
|
|
// dsptool [-f] -h asdf.h asdf.txt
|
|
|
|
// Print results from DSPSpy register dump
|
|
|
|
// dsptool -p dsp_dump0.bin
|
2009-07-05 20:10:26 -06:00
|
|
|
// So far, all this binary can do is test partially that itself works correctly.
|
|
|
|
int main(int argc, const char *argv[])
|
|
|
|
{
|
2014-03-10 05:30:55 -06:00
|
|
|
if (argc == 1 || (argc == 2 && (!strcmp(argv[1], "--help") || (!strcmp(argv[1], "-?")))))
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2009-07-19 14:14:09 -06:00
|
|
|
printf("USAGE: DSPTool [-?] [--help] [-f] [-d] [-m] [-p <FILE>] [-o <FILE>] [-h <FILE>] <DSP ASSEMBLER FILE>\n");
|
2009-07-05 20:10:26 -06:00
|
|
|
printf("-? / --help: Prints this message\n");
|
|
|
|
printf("-d: Disassemble\n");
|
|
|
|
printf("-m: Input file contains a list of files (Header assembly only)\n");
|
|
|
|
printf("-s: Print the final size in bytes (only)\n");
|
2009-07-19 14:14:09 -06:00
|
|
|
printf("-f: Force assembly (errors are not critical)\n");
|
2009-07-05 20:10:26 -06:00
|
|
|
printf("-o <OUTPUT FILE>: Results from stdout redirected to a file\n");
|
|
|
|
printf("-h <HEADER FILE>: Output assembly results to a header\n");
|
2009-07-19 14:14:09 -06:00
|
|
|
printf("-p <DUMP FILE>: Print results of DSPSpy register dump\n");
|
2010-03-17 18:18:36 -06:00
|
|
|
printf("-ps <DUMP FILE>: Print results of DSPSpy register dump (disable SR output)\n");
|
|
|
|
printf("-pm <DUMP FILE>: Print results of DSPSpy register dump (convert PROD values)\n");
|
|
|
|
printf("-psm <DUMP FILE>: Print results of DSPSpy register dump (convert PROD values/disable SR output)\n");
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2009-07-05 20:10:26 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 2 && !strcmp(argv[1], "test"))
|
|
|
|
{
|
|
|
|
RunAsmTests();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string input_name;
|
|
|
|
std::string output_header_name;
|
|
|
|
std::string output_name;
|
|
|
|
|
2013-10-28 23:23:17 -06:00
|
|
|
bool disassemble = false, compare = false, multiple = false, outputSize = false,
|
2010-03-17 18:18:36 -06:00
|
|
|
force = false, print_results = false, print_results_prodhack = false, print_results_srhack = false;
|
2009-07-05 20:10:26 -06:00
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (!strcmp(argv[i], "-d"))
|
|
|
|
disassemble = true;
|
|
|
|
else if (!strcmp(argv[i], "-o"))
|
|
|
|
output_name = argv[++i];
|
|
|
|
else if (!strcmp(argv[i], "-h"))
|
|
|
|
output_header_name = argv[++i];
|
|
|
|
else if (!strcmp(argv[i], "-c"))
|
|
|
|
compare = true;
|
|
|
|
else if (!strcmp(argv[i], "-s"))
|
|
|
|
outputSize = true;
|
|
|
|
else if (!strcmp(argv[i], "-m"))
|
|
|
|
multiple = true;
|
2009-07-19 04:08:25 -06:00
|
|
|
else if (!strcmp(argv[i], "-f"))
|
|
|
|
force = true;
|
2009-07-19 14:14:09 -06:00
|
|
|
else if (!strcmp(argv[i], "-p"))
|
|
|
|
print_results = true;
|
2010-03-17 18:18:36 -06:00
|
|
|
else if (!strcmp(argv[i], "-ps")) {
|
|
|
|
print_results = true;
|
|
|
|
print_results_srhack = true;
|
|
|
|
}
|
|
|
|
else if (!strcmp(argv[i], "-pm")) {
|
|
|
|
print_results = true;
|
|
|
|
print_results_prodhack = true;
|
|
|
|
}
|
|
|
|
else if (!strcmp(argv[i], "-psm")) {
|
|
|
|
print_results = true;
|
|
|
|
print_results_srhack = true;
|
|
|
|
print_results_prodhack = true;
|
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
else
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
|
|
|
if (!input_name.empty())
|
|
|
|
{
|
|
|
|
printf("ERROR: Can only take one input file.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
input_name = argv[i];
|
2011-02-28 20:06:14 -07:00
|
|
|
if (!File::Exists(input_name))
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
|
|
|
printf("ERROR: Input path does not exist.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-10 05:30:55 -06:00
|
|
|
if (multiple && (compare || disassemble || !output_name.empty() ||
|
2009-07-05 20:10:26 -06:00
|
|
|
input_name.empty())) {
|
|
|
|
printf("ERROR: Multiple files can only be used with assembly "
|
|
|
|
"and must compile a header file.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compare)
|
|
|
|
{
|
|
|
|
// Two binary inputs, let's diff.
|
|
|
|
std::string binary_code;
|
|
|
|
std::vector<u16> code1, code2;
|
2013-11-04 04:33:41 -07:00
|
|
|
File::ReadFileToString(input_name.c_str(), binary_code);
|
2009-07-05 20:10:26 -06:00
|
|
|
BinaryStringBEToCode(binary_code, code1);
|
2013-11-04 04:33:41 -07:00
|
|
|
File::ReadFileToString(output_name.c_str(), binary_code);
|
2009-07-05 20:10:26 -06:00
|
|
|
BinaryStringBEToCode(binary_code, code2);
|
|
|
|
Compare(code1, code2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-19 14:14:09 -06:00
|
|
|
if (print_results)
|
|
|
|
{
|
|
|
|
std::string dumpfile, results;
|
|
|
|
std::vector<u16> reg_vector;
|
|
|
|
|
2013-11-04 04:33:41 -07:00
|
|
|
File::ReadFileToString(input_name.c_str(), dumpfile);
|
2009-07-19 14:14:09 -06:00
|
|
|
BinaryStringBEToCode(dumpfile, reg_vector);
|
|
|
|
|
|
|
|
results.append("Start:\n");
|
|
|
|
for (int initial_reg = 0; initial_reg < 32; initial_reg++)
|
|
|
|
{
|
2009-08-03 10:09:03 -06:00
|
|
|
results.append(StringFromFormat("%02x %04x ", initial_reg, reg_vector.at(initial_reg)));
|
2009-07-19 14:14:09 -06:00
|
|
|
if ((initial_reg + 1) % 8 == 0)
|
|
|
|
results.append("\n");
|
|
|
|
}
|
|
|
|
results.append("\n");
|
|
|
|
results.append("Step [number]:\n[Reg] [last value] [current value]\n\n");
|
2010-02-23 20:38:36 -07:00
|
|
|
for (unsigned int step = 1; step < reg_vector.size()/32; step++)
|
2009-07-19 14:14:09 -06:00
|
|
|
{
|
|
|
|
bool changed = false;
|
2010-03-17 18:18:36 -06:00
|
|
|
u16 current_reg;
|
|
|
|
u16 last_reg;
|
|
|
|
u32 htemp;
|
|
|
|
//results.append(StringFromFormat("Step %3d: (CW 0x%04x) UC:%03d\n", step, 0x8fff+step, (step-1)/32));
|
2009-07-19 14:14:09 -06:00
|
|
|
results.append(StringFromFormat("Step %3d:\n", step));
|
|
|
|
for (int reg = 0; reg < 32; reg++)
|
|
|
|
{
|
2009-08-03 10:09:03 -06:00
|
|
|
if ((reg >= 0x0c) && (reg <= 0x0f)) continue;
|
2010-03-17 18:18:36 -06:00
|
|
|
if (print_results_srhack && (reg == 0x13)) continue;
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2010-03-17 18:18:36 -06:00
|
|
|
if ((print_results_prodhack) && (reg >= 0x15) && (reg <= 0x17)) {
|
|
|
|
switch (reg) {
|
|
|
|
case 0x15: //DSP_REG_PRODM
|
|
|
|
last_reg = reg_vector.at((step*32-32)+reg) + reg_vector.at((step*32-32)+reg+2);
|
2013-10-28 23:23:17 -06:00
|
|
|
current_reg = reg_vector.at(step*32+reg) + reg_vector.at(step*32+reg+2);
|
2010-03-17 18:18:36 -06:00
|
|
|
break;
|
|
|
|
case 0x16: //DSP_REG_PRODH
|
2013-10-28 23:23:17 -06:00
|
|
|
htemp = ((reg_vector.at(step*32+reg-1) + reg_vector.at(step*32+reg+1))&~0xffff)>>16;
|
2010-03-17 18:18:36 -06:00
|
|
|
current_reg = (u8)(reg_vector.at(step*32+reg) + htemp);
|
2013-10-28 23:23:17 -06:00
|
|
|
htemp = ((reg_vector.at(step*32-32+reg-1) + reg_vector.at(step*32-32+reg+1))&~0xffff)>>16;
|
2010-03-17 18:18:36 -06:00
|
|
|
last_reg = (u8)(reg_vector.at(step*32-32+reg) + htemp);
|
|
|
|
break;
|
|
|
|
case 0x17: //DSP_REG_PRODM2
|
2010-05-26 15:38:30 -06:00
|
|
|
default:
|
2010-03-17 18:18:36 -06:00
|
|
|
current_reg = 0;
|
|
|
|
last_reg = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
current_reg = reg_vector.at(step*32+reg);
|
|
|
|
last_reg = reg_vector.at((step*32-32)+reg);
|
|
|
|
}
|
2009-07-19 14:14:09 -06:00
|
|
|
if (last_reg != current_reg)
|
|
|
|
{
|
2009-08-03 10:09:03 -06:00
|
|
|
results.append(StringFromFormat("%02x %-7s: %04x %04x\n", reg, pdregname(reg), last_reg, current_reg));
|
2009-07-19 14:14:09 -06:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!changed)
|
|
|
|
results.append("No Change\n\n");
|
|
|
|
else
|
|
|
|
results.append("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!output_name.empty())
|
2013-11-04 04:33:41 -07:00
|
|
|
File::WriteStringToFile(results, output_name.c_str());
|
2009-07-19 14:14:09 -06:00
|
|
|
else
|
2010-02-23 20:38:36 -07:00
|
|
|
printf("%s", results.c_str());
|
2009-07-19 14:14:09 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-05 20:10:26 -06:00
|
|
|
if (disassemble)
|
|
|
|
{
|
|
|
|
if (input_name.empty())
|
|
|
|
{
|
|
|
|
printf("Disassemble: Must specify input.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
std::string binary_code;
|
|
|
|
std::vector<u16> code;
|
2013-11-04 04:33:41 -07:00
|
|
|
File::ReadFileToString(input_name.c_str(), binary_code);
|
2009-07-05 20:10:26 -06:00
|
|
|
BinaryStringBEToCode(binary_code, code);
|
|
|
|
std::string text;
|
|
|
|
Disassemble(code, true, text);
|
|
|
|
if (!output_name.empty())
|
2013-11-04 04:33:41 -07:00
|
|
|
File::WriteStringToFile(text, output_name.c_str());
|
2009-07-05 20:10:26 -06:00
|
|
|
else
|
|
|
|
printf("%s", text.c_str());
|
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
else
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
|
|
|
if (input_name.empty())
|
|
|
|
{
|
|
|
|
printf("Assemble: Must specify input.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
std::string source;
|
2013-11-04 04:33:41 -07:00
|
|
|
if (File::ReadFileToString(input_name.c_str(), source))
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2014-03-10 05:30:55 -06:00
|
|
|
if (multiple)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
|
|
|
// When specifying a list of files we must compile a header
|
|
|
|
// (we can't assemble multiple files to one binary)
|
|
|
|
// since we checked it before, we assume output_header_name isn't empty
|
|
|
|
int lines;
|
|
|
|
std::vector<u16> *codes;
|
|
|
|
std::vector<std::string> files;
|
|
|
|
std::string header, currentSource;
|
|
|
|
size_t lastPos = 0, pos = 0;
|
|
|
|
|
|
|
|
source.append("\n");
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2014-03-10 05:30:55 -06:00
|
|
|
while ((pos = source.find('\n', lastPos)) != std::string::npos)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
|
|
|
std::string temp = source.substr(lastPos, pos - lastPos);
|
2014-03-10 05:30:55 -06:00
|
|
|
if (!temp.empty())
|
2009-07-05 20:10:26 -06:00
|
|
|
files.push_back(temp);
|
|
|
|
lastPos = pos + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lines = (int)files.size();
|
|
|
|
|
2014-03-10 05:30:55 -06:00
|
|
|
if (lines == 0)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
|
|
|
printf("ERROR: Must specify at least one file\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2009-07-05 20:10:26 -06:00
|
|
|
codes = new std::vector<u16>[lines];
|
|
|
|
|
2013-10-28 23:23:17 -06:00
|
|
|
for (int i = 0; i < lines; i++)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2013-11-04 04:33:41 -07:00
|
|
|
if (!File::ReadFileToString(files[i].c_str(), currentSource))
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
|
|
|
printf("ERROR reading %s, skipping...\n", files[i].c_str());
|
|
|
|
lines--;
|
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
else
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2013-10-28 23:23:17 -06:00
|
|
|
if (!Assemble(currentSource.c_str(), codes[i], force))
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2013-10-28 23:23:17 -06:00
|
|
|
printf("Assemble: Assembly of %s failed due to errors\n",
|
2009-07-05 20:10:26 -06:00
|
|
|
files[i].c_str());
|
|
|
|
lines--;
|
|
|
|
}
|
2009-09-08 15:16:05 -06:00
|
|
|
if (outputSize) {
|
|
|
|
printf("%s: %d\n", files[i].c_str(), (int)codes[i].size());
|
|
|
|
}
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2009-07-05 20:10:26 -06:00
|
|
|
|
|
|
|
CodesToHeader(codes, &files, lines, output_header_name.c_str(), header);
|
2013-11-04 04:33:41 -07:00
|
|
|
File::WriteStringToFile(header, (output_header_name + ".h").c_str());
|
2009-07-05 20:10:26 -06:00
|
|
|
|
|
|
|
delete[] codes;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<u16> code;
|
|
|
|
|
2009-09-08 15:16:05 -06:00
|
|
|
if (!Assemble(source.c_str(), code, force)) {
|
2009-07-05 20:10:26 -06:00
|
|
|
printf("Assemble: Assembly failed due to errors\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-09-08 15:16:05 -06:00
|
|
|
if (outputSize) {
|
|
|
|
printf("%s: %d\n", input_name.c_str(), (int)code.size());
|
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2009-07-05 20:10:26 -06:00
|
|
|
if (!output_name.empty())
|
|
|
|
{
|
|
|
|
std::string binary_code;
|
|
|
|
CodeToBinaryStringBE(code, binary_code);
|
2013-11-04 04:33:41 -07:00
|
|
|
File::WriteStringToFile(binary_code, output_name.c_str());
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
|
|
|
if (!output_header_name.empty())
|
|
|
|
{
|
|
|
|
std::string header;
|
|
|
|
CodeToHeader(code, input_name, output_header_name.c_str(), header);
|
2013-11-04 04:33:41 -07:00
|
|
|
File::WriteStringToFile(header, (output_header_name + ".h").c_str());
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
source.clear();
|
|
|
|
}
|
|
|
|
|
2014-03-09 13:01:56 -06:00
|
|
|
if (disassemble)
|
|
|
|
{
|
|
|
|
printf("Disassembly completed successfully!\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!outputSize)
|
|
|
|
printf("Assembly completed successfully!\n");
|
|
|
|
}
|
2009-07-05 20:10:26 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|