Completely fixed DSPTool, on all of its memory leaks, bad API and bad C++. (compiling with include works perfectly)

More small leftover fixes

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@3071 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
XTra.KrazzY 2009-04-25 10:38:26 +00:00
parent 72febe7b8a
commit 1045fc7e98
14 changed files with 153 additions and 138 deletions

View File

@ -620,7 +620,7 @@ bool WriteStringToFile(bool text_file, const std::string &str, const char *filen
return true; return true;
} }
bool ReadFileToString(bool text_file, const char *filename, std::string *str) bool ReadFileToString(bool text_file, const char *filename, std::string &str)
{ {
FILE *f = fopen(filename, text_file ? "r" : "rb"); FILE *f = fopen(filename, text_file ? "r" : "rb");
if (!f) if (!f)
@ -630,7 +630,7 @@ bool ReadFileToString(bool text_file, const char *filename, std::string *str)
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
char *buf = new char[len + 1]; char *buf = new char[len + 1];
buf[fread(buf, 1, len, f)] = 0; buf[fread(buf, 1, len, f)] = 0;
*str = std::string(buf, len); str = std::string(buf, len);
fclose(f); fclose(f);
delete [] buf; delete [] buf;
return true; return true;

View File

@ -100,7 +100,7 @@ std::string GetBundleDirectory();
#endif #endif
bool WriteStringToFile(bool text_file, const std::string &str, const char *filename); bool WriteStringToFile(bool text_file, const std::string &str, const char *filename);
bool ReadFileToString(bool text_file, const char *filename, std::string *str); bool ReadFileToString(bool text_file, const char *filename, std::string &str);
} // namespace } // namespace

View File

@ -226,17 +226,19 @@ void SetLidOpen(bool _bOpen)
else else
dvdMem.CoverReg.Hex = 0x0; dvdMem.CoverReg.Hex = 0x0;
CPeripheralInterface::SetInterrupt(CPeripheralInterface::INT_CAUSE_DI, true); GenerateDVDInterrupt(INT_CVRINT);
//Todo: Make this work perhaps?
/* /*
Todo: Make this work perhaps?
if (_bOpen) if (_bOpen)
dvdMem.CoverReg.CVR = 1; dvdMem.CoverReg.CVR = 1;
else else
dvdMem.CoverReg.CVR = 0; dvdMem.CoverReg.CVR = 0;
*/
UpdateInterrupts(); UpdateInterrupts();
*/
} }
bool IsLidOpen() bool IsLidOpen()

View File

@ -15,6 +15,7 @@
// Official SVN repository and contact information can be found at // Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/ // http://code.google.com/p/dolphin-emu/
#include <iostream>
#include <vector> #include <vector>
#include "Common.h" #include "Common.h"
@ -24,7 +25,7 @@
#include "disassemble.h" #include "disassemble.h"
bool Assemble(const char *text, std::vector<u16> *code) bool Assemble(const char *text, std::vector<u16> &code)
{ {
AssemblerSettings settings; AssemblerSettings settings;
settings.pc = 0; settings.pc = 0;
@ -36,14 +37,14 @@ bool Assemble(const char *text, std::vector<u16> *code)
// TODO: fix the terrible api of the assembler. // TODO: fix the terrible api of the assembler.
DSPAssembler assembler(settings); DSPAssembler assembler(settings);
if (!assembler.Assemble(text, code)) { if (!assembler.Assemble(text, code)) {
printf("%s", assembler.GetErrorString().c_str()); std::cerr << assembler.GetErrorString() << std::endl;
return false; return false;
} }
return true; return true;
} }
bool Disassemble(const std::vector<u16> &code, bool line_numbers, std::string *text) bool Disassemble(const std::vector<u16> &code, bool line_numbers, std::string &text)
{ {
if (code.empty()) if (code.empty())
return false; return false;
@ -79,9 +80,9 @@ bool Compare(const std::vector<u16> &code1, const std::vector<u16> &code2)
{ {
std::string line1, line2; std::string line1, line2;
u16 pc = i; u16 pc = i;
disassembler.DisOpcode(&code1[0], 2, &pc, &line1); disassembler.DisOpcode(&code1[0], 2, &pc, line1);
pc = i; pc = i;
disassembler.DisOpcode(&code2[0], 2, &pc, &line2); disassembler.DisOpcode(&code2[0], 2, &pc, line2);
printf("!! %04x : %04x vs %04x - %s vs %s\n", i, code1[i], code2[i], line1.c_str(), line2.c_str()); printf("!! %04x : %04x vs %04x - %s vs %s\n", i, code1[i], code2[i], line1.c_str(), line2.c_str());
} }
} }
@ -93,7 +94,7 @@ bool Compare(const std::vector<u16> &code1, const std::vector<u16> &code2)
{ {
u16 pc = i; u16 pc = i;
std::string line; std::string line;
disassembler.DisOpcode(&longest[0], 2, &pc, &line); disassembler.DisOpcode(&longest[0], 2, &pc, line);
printf("!! %s\n", line.c_str()); printf("!! %s\n", line.c_str());
} }
} }
@ -101,64 +102,64 @@ bool Compare(const std::vector<u16> &code1, const std::vector<u16> &code2)
return code1.size() == code2.size() && code1.size() == count_equal; return code1.size() == code2.size() && code1.size() == count_equal;
} }
void GenRandomCode(int size, std::vector<u16> *code) void GenRandomCode(int size, std::vector<u16> &code)
{ {
code->resize(size); code.resize(size);
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
{ {
(*code)[i] = rand() ^ (rand() << 8); code[i] = rand() ^ (rand() << 8);
} }
} }
void CodeToHeader(const std::vector<u16> &code, const char *name, std::string *header) void CodeToHeader(const std::vector<u16> &code, const char *name, std::string &header)
{ {
std::vector<u16> code_copy = code; std::vector<u16> code_copy = code;
// Add some nops at the end to align the size a bit. // Add some nops at the end to align the size a bit.
while (code_copy.size() & 7) while (code_copy.size() & 7)
code_copy.push_back(0); code_copy.push_back(0);
char buffer[1024]; char buffer[1024];
header->clear(); header.clear();
header->reserve(code.size() * 4); header.reserve(code.size() * 4);
header->append("#ifndef _MSCVER\n"); header.append("#ifndef _MSCVER\n");
sprintf(buffer, "const unsigned short %s[0x1000] = {\n", name); sprintf(buffer, "const unsigned short %s[0x1000] = {\n", name);
header->append(buffer); header.append(buffer);
header->append("#else\n"); header.append("#else\n");
sprintf(buffer, "const unsigned short %s[0x1000] __attribute__ ((aligned (64))) = {\n", name); sprintf(buffer, "const unsigned short %s[0x1000] __attribute__ ((aligned (64))) = {\n", name);
header->append(buffer); header.append(buffer);
header->append("#endif\n\n "); header.append("#endif\n\n ");
for (int i = 0; i < code.size(); i++) for (int i = 0; i < code.size(); i++)
{ {
if (i && ((i & 15) == 0)) if (i && ((i & 15) == 0))
header->append("\n "); header.append("\n ");
sprintf(buffer, "0x%04x, ", code[i]); sprintf(buffer, "0x%04x, ", code[i]);
header->append(buffer); header.append(buffer);
} }
header->append("\n};\n"); header.append("\n};\n");
} }
void CodeToBinaryStringBE(const std::vector<u16> &code, std::string *str) void CodeToBinaryStringBE(const std::vector<u16> &code, std::string &str)
{ {
str->resize(code.size() * 2); str.resize(code.size() * 2);
for (int i = 0; i < code.size(); i++) for (int i = 0; i < code.size(); i++)
{ {
(*str)[i * 2 + 0] = code[i] >> 8; str[i * 2 + 0] = code[i] >> 8;
(*str)[i * 2 + 1] = code[i] & 0xff; str[i * 2 + 1] = code[i] & 0xff;
} }
} }
void BinaryStringBEToCode(const std::string &str, std::vector<u16> *code) void BinaryStringBEToCode(const std::string &str, std::vector<u16> &code)
{ {
code->resize(str.size() / 2); code.resize(str.size() / 2);
for (int i = 0; i < code->size(); i++) for (int i = 0; i < code.size(); i++)
{ {
(*code)[i] = ((u16)(u8)str[i * 2 + 0] << 8) | ((u16)(u8)str[i * 2 + 1]); code[i] = ((u16)(u8)str[i * 2 + 0] << 8) | ((u16)(u8)str[i * 2 + 1]);
} }
} }
bool LoadBinary(const char *filename, std::vector<u16> *code) bool LoadBinary(const char *filename, std::vector<u16> &code)
{ {
std::string buffer; std::string buffer;
if (!File::ReadFileToString(false, filename, &buffer)) if (!File::ReadFileToString(false, filename, buffer))
return false; return false;
BinaryStringBEToCode(buffer, code); BinaryStringBEToCode(buffer, code);
@ -168,7 +169,7 @@ bool LoadBinary(const char *filename, std::vector<u16> *code)
bool SaveBinary(const std::vector<u16> &code, const char *filename) bool SaveBinary(const std::vector<u16> &code, const char *filename)
{ {
std::string buffer; std::string buffer;
CodeToBinaryStringBE(code, &buffer); CodeToBinaryStringBE(code, buffer);
if (!File::WriteStringToFile(false, buffer, filename)) if (!File::WriteStringToFile(false, buffer, filename))
return false; return false;
return true; return true;

View File

@ -23,18 +23,18 @@
#include "Common.h" #include "Common.h"
bool Assemble(const char *text, std::vector<u16> *code); bool Assemble(const char *text, std::vector<u16> &code);
bool Disassemble(const std::vector<u16> &code, bool line_numbers, std::string *text); bool Disassemble(const std::vector<u16> &code, bool line_numbers, std::string &text);
bool Compare(const std::vector<u16> &code1, const std::vector<u16> &code2); bool Compare(const std::vector<u16> &code1, const std::vector<u16> &code2);
void GenRandomCode(int size, std::vector<u16> *code); void GenRandomCode(int size, std::vector<u16> &code);
void CodeToHeader(const std::vector<u16> &code, const char *name, std::string *header); void CodeToHeader(const std::vector<u16> &code, const char *name, std::string &header);
// Big-endian, for writing straight to file using File::WriteStringToFile. // Big-endian, for writing straight to file using File::WriteStringToFile.
void CodeToBinaryStringBE(const std::vector<u16> &code, std::string *str); void CodeToBinaryStringBE(const std::vector<u16> &code, std::string &str);
void BinaryStringBEToCode(const std::string &str, std::vector<u16> *code); void BinaryStringBEToCode(const std::string &str, std::vector<u16> &code);
// Load code (big endian binary). // Load code (big endian binary).
bool LoadBinary(const char *filename, std::vector<u16> *code); bool LoadBinary(const char *filename, std::vector<u16> &code);
bool SaveBinary(const std::vector<u16> &code, const char *filename); bool SaveBinary(const std::vector<u16> &code, const char *filename);
#endif // _DSPCODEUTIL_H #endif // _DSPCODEUTIL_H

View File

@ -35,7 +35,7 @@ void LabelMap::RegisterDefaults()
} }
} }
void LabelMap::RegisterLabel(const char *label, u16 lval, LabelType type) void LabelMap::RegisterLabel(const std::string &label, u16 lval, LabelType type)
{ {
u16 old_value; u16 old_value;
if (GetLabelValue(label, &old_value) && old_value != lval) if (GetLabelValue(label, &old_value) && old_value != lval)
@ -47,12 +47,12 @@ void LabelMap::RegisterLabel(const char *label, u16 lval, LabelType type)
labels.push_back(label_t(label, lval, type)); labels.push_back(label_t(label, lval, type));
} }
void LabelMap::DeleteLabel(const char *label) void LabelMap::DeleteLabel(const std::string &label)
{ {
for (std::vector<label_t>::iterator iter = labels.begin(); for (std::vector<label_t>::iterator iter = labels.begin();
iter != labels.end(); ++iter) iter != labels.end(); ++iter)
{ {
if (!strcmp(label, iter->name.c_str())) if (!label.compare(iter->name))
{ {
labels.erase(iter); labels.erase(iter);
return; return;
@ -60,11 +60,11 @@ void LabelMap::DeleteLabel(const char *label)
} }
} }
bool LabelMap::GetLabelValue(const char *label, u16 *value, LabelType type) const bool LabelMap::GetLabelValue(const std::string &label, u16 *value, LabelType type) const
{ {
for (int i = 0; i < labels.size(); i++) for (int i = 0; i < labels.size(); i++)
{ {
if (!strcmp(labels[i].name.c_str(), label)) if (!label.compare(labels[i].name))
{ {
if (type & labels[i].type) { if (type & labels[i].type) {
*value = labels[i].addr; *value = labels[i].addr;

View File

@ -35,7 +35,7 @@ class LabelMap
{ {
struct label_t struct label_t
{ {
label_t(const char *lbl, s32 address, LabelType ltype) : name(lbl), addr(address), type(ltype) {} label_t(const std::string &lbl, s32 address, LabelType ltype) : name(lbl), addr(address), type(ltype) {}
std::string name; std::string name;
s32 addr; s32 addr;
LabelType type; LabelType type;
@ -44,10 +44,11 @@ class LabelMap
public: public:
LabelMap(); LabelMap();
~LabelMap() { }
void RegisterDefaults(); void RegisterDefaults();
void RegisterLabel(const char *label, u16 lval, LabelType type = LABEL_VALUE); void RegisterLabel(const std::string &label, u16 lval, LabelType type = LABEL_VALUE);
void DeleteLabel(const char *label); void DeleteLabel(const std::string &label);
bool GetLabelValue(const char *label, u16 *value, LabelType type = LABEL_ANY) const; bool GetLabelValue(const std::string &label, u16 *value, LabelType type = LABEL_ANY) const;
void Clear(); void Clear();
}; };

View File

@ -37,11 +37,13 @@ Initial import
====================================================================*/ ====================================================================*/
#include <stdio.h> #include <cstdio>
#include <memory.h> #include <memory.h>
#include <stdlib.h> #include <cstdlib>
#include <map> #include <map>
#include <iostream>
#include <fstream>
#include "Common.h" #include "Common.h"
#include "FileUtil.h" #include "FileUtil.h"
@ -89,9 +91,10 @@ DSPAssembler::DSPAssembler(const AssemblerSettings &settings) :
DSPAssembler::~DSPAssembler() DSPAssembler::~DSPAssembler()
{ {
free(gdg_buffer);
} }
bool DSPAssembler::Assemble(const char *text, std::vector<u16> *code, std::vector<int> *line_numbers) bool DSPAssembler::Assemble(const char *text, std::vector<u16> &code, std::vector<int> *line_numbers)
{ {
if (line_numbers) if (line_numbers)
line_numbers->clear(); line_numbers->clear();
@ -101,13 +104,25 @@ bool DSPAssembler::Assemble(const char *text, std::vector<u16> *code, std::vecto
InitPass(1); InitPass(1);
if (!AssembleFile(fname, 1)) if (!AssembleFile(fname, 1))
return false; return false;
// We now have the size of the output buffer
if (m_totalSize > 0)
{
if(gdg_buffer)
free(gdg_buffer);
gdg_buffer = (char *)malloc(m_totalSize * sizeof(u16) + 4);
memset(gdg_buffer, 0, m_totalSize * sizeof(u16));
} else
return false;
InitPass(2); InitPass(2);
if (!AssembleFile(fname, 2)) if (!AssembleFile(fname, 2))
return false; return false;
code->resize(gdg_buffer_size); code.resize(m_totalSize);
for (int i = 0; i < gdg_buffer_size; i++) { for (int i = 0; i < m_totalSize; i++) {
(*code)[i] = *(u16 *)(gdg_buffer + i * 2); code[i] = *(u16 *)(gdg_buffer + i * 2);
} }
last_error_str = "(no errors)"; last_error_str = "(no errors)";
@ -124,10 +139,6 @@ void DSPAssembler::ShowError(err_t err_code, const char *extra_info)
buf_ptr += sprintf(buf_ptr, "%i : %s", code_line, cur_line.c_str()); buf_ptr += sprintf(buf_ptr, "%i : %s", code_line, cur_line.c_str());
if (!extra_info) if (!extra_info)
extra_info = "-"; extra_info = "-";
if (fsrc)
fclose(fsrc);
else
buf_ptr += sprintf(buf_ptr, "ERROR: %s : %s\n", err_string[err_code], extra_info);
if (m_current_param == 0) if (m_current_param == 0)
buf_ptr += sprintf(buf_ptr, "ERROR: %s Line: %d : %s\n", err_string[err_code], code_line, extra_info); buf_ptr += sprintf(buf_ptr, "ERROR: %s Line: %d : %s\n", err_string[err_code], code_line, extra_info);
@ -724,6 +735,7 @@ void DSPAssembler::InitPass(int pass)
aliases["S40"] = "SET40"; aliases["S40"] = "SET40";
} }
m_cur_addr = 0; m_cur_addr = 0;
m_totalSize = 0;
cur_segment = SEGMENT_CODE; cur_segment = SEGMENT_CODE;
segment_addr[SEGMENT_CODE] = 0; segment_addr[SEGMENT_CODE] = 0;
segment_addr[SEGMENT_DATA] = 0; segment_addr[SEGMENT_DATA] = 0;
@ -734,29 +746,29 @@ bool DSPAssembler::AssembleFile(const char *fname, int pass)
{ {
int disable_text = 0; // modified by Hermes int disable_text = 0; // modified by Hermes
fsrc = fopen(fname, "r"); std::ifstream fsrc(fname);
if (fsrc == NULL)
if (fsrc.fail())
{ {
fprintf(stderr, "Cannot open %s file\n", fname); std::cerr << "Cannot open file " << fname << std::endl;
return false; return false;
} }
fseek(fsrc, 0, SEEK_SET);
printf("%s: Pass %d\n", fname, pass); printf("%s: Pass %d\n", fname, pass);
code_line = 0; code_line = 0;
m_cur_pass = pass; m_cur_pass = pass;
#define LINEBUF_SIZE 1024 #define LINEBUF_SIZE 1024
char linebuffer[LINEBUF_SIZE]; char line[LINEBUF_SIZE] = {0};
while (!failed && !feof(fsrc)) while (!failed && !fsrc.fail() && !fsrc.eof())
{ {
int opcode_size = 0; int opcode_size = 0;
memset(linebuffer, 0, LINEBUF_SIZE); fsrc.getline(line, LINEBUF_SIZE);
if (!fgets(linebuffer, LINEBUF_SIZE, fsrc)) if(fsrc.fail())
break; break;
cur_line = linebuffer;
//printf("A: %s", linebuffer); cur_line = line;
//printf("A: %s\n", line);
code_line++; code_line++;
param_t params[10] = {{0}}; param_t params[10] = {{0}};
@ -764,16 +776,16 @@ bool DSPAssembler::AssembleFile(const char *fname, int pass)
for (int i = 0; i < LINEBUF_SIZE; i++) for (int i = 0; i < LINEBUF_SIZE; i++)
{ {
char c = linebuffer[i]; char c = line[i];
// This stuff handles /**/ and // comments. // This stuff handles /**/ and // comments.
// modified by Hermes : added // and /* */ for long commentaries // modified by Hermes : added // and /* */ for long commentaries
if (c == '/') if (c == '/')
{ {
if (i < 1023) if (i < 1023)
{ {
if (linebuffer[i+1] == '/') if (line[i+1] == '/')
c = 0x00; c = 0x00;
else if (linebuffer[i+1] == '*') else if (line[i+1] == '*')
{ {
// toggle comment mode. // toggle comment mode.
disable_text = !disable_text; disable_text = !disable_text;
@ -782,11 +794,11 @@ bool DSPAssembler::AssembleFile(const char *fname, int pass)
} }
else if (c == '*') else if (c == '*')
{ {
if (i < 1023 && linebuffer[i+1] == '/' && disable_text) if (i < 1023 && line[i+1] == '/' && disable_text)
{ {
disable_text = 0; disable_text = 0;
c = 32; c = 32;
linebuffer[i + 1] = 32; line[i + 1] = 32;
} }
} }
@ -799,37 +811,35 @@ bool DSPAssembler::AssembleFile(const char *fname, int pass)
c = ' '; c = ' ';
if (c >= 'a' && c <= 'z') // convert to uppercase if (c >= 'a' && c <= 'z') // convert to uppercase
c = c - 'a' + 'A'; c = c - 'a' + 'A';
linebuffer[i] = c; line[i] = c;
if (c == 0) if (c == 0)
break; // modified by Hermes break; // modified by Hermes
} }
char *ptr = linebuffer; char *ptr = line;
char *opcode = NULL; std::string label;
char *label = NULL;
char *col_ptr; size_t col_pos = std::string(line).find(":");
if ((col_ptr = strstr(ptr, ":")) != NULL) if (col_pos != std::string::npos)
{ {
int j; bool valid = true;
bool valid;
j = 0; for(int j = 0; j < (int)col_pos; j++)
valid = true;
while ((ptr+j) < col_ptr)
{ {
if (j == 0) if (j == 0)
if (!((ptr[j] >= 'A' && ptr[j] <= 'Z') || (ptr[j] == '_'))) if (!((ptr[j] >= 'A' && ptr[j] <= 'Z') || (ptr[j] == '_')))
valid = false; valid = false;
if (!((ptr[j] >= '0' && ptr[j] <= '9') || (ptr[j] >= 'A' && ptr[j] <= 'Z') || (ptr[j] == '_'))) if (!((ptr[j] >= '0' && ptr[j] <= '9') || (ptr[j] >= 'A' && ptr[j] <= 'Z') || (ptr[j] == '_')))
valid = false; valid = false;
j++;
} }
if (valid) if (valid)
{ {
label = strtok(ptr, ":\x20"); label = std::string(line).substr(0, col_pos);
ptr = col_ptr + 1; ptr += col_pos + 1;
} }
} }
char *opcode = NULL;
opcode = strtok(ptr, " "); opcode = strtok(ptr, " ");
char *opcode_ext = NULL; char *opcode_ext = NULL;
@ -868,7 +878,7 @@ bool DSPAssembler::AssembleFile(const char *fname, int pass)
params_count_ext = GetParams(paramstr_ext, params_ext); params_count_ext = GetParams(paramstr_ext, params_ext);
} }
if (label) if (!label.empty())
{ {
// there is a valid label so lets store it in labels table // there is a valid label so lets store it in labels table
u32 lval = m_cur_addr; u32 lval = m_cur_addr;
@ -893,7 +903,8 @@ bool DSPAssembler::AssembleFile(const char *fname, int pass)
if (params[0].type == P_STR) if (params[0].type == P_STR)
{ {
char *tmpstr; char *tmpstr;
FILE *thisSrc = fsrc; u32 thisCodeline = code_line;
if (include_dir.size()) if (include_dir.size())
{ {
tmpstr = (char *)malloc(include_dir.size() + strlen(params[0].str) + 2); tmpstr = (char *)malloc(include_dir.size() + strlen(params[0].str) + 2);
@ -904,8 +915,10 @@ bool DSPAssembler::AssembleFile(const char *fname, int pass)
tmpstr = (char *)malloc(strlen(params[0].str) + 1); tmpstr = (char *)malloc(strlen(params[0].str) + 1);
strcpy(tmpstr, params[0].str); strcpy(tmpstr, params[0].str);
} }
AssembleFile(tmpstr, pass); AssembleFile(tmpstr, pass);
fsrc = thisSrc;
code_line = thisCodeline;
free(tmpstr); free(tmpstr);
} }
@ -986,15 +999,11 @@ bool DSPAssembler::AssembleFile(const char *fname, int pass)
} }
m_cur_addr += opcode_size; m_cur_addr += opcode_size;
m_totalSize += opcode_size;
}; };
if (gdg_buffer == NULL) if (!failed)
{ fsrc.close();
gdg_buffer_size = m_cur_addr;
gdg_buffer = (char *)malloc(gdg_buffer_size * sizeof(u16) + 4);
memset(gdg_buffer, 0, gdg_buffer_size * sizeof(u16));
}
if (! failed)
fclose(fsrc);
return !failed; return !failed;
} }

View File

@ -73,7 +73,7 @@ public:
// one for each word of code, indicating the source assembler code line number it came from. // one for each word of code, indicating the source assembler code line number it came from.
// If returns false, call GetErrorString to get some text to present to the user. // If returns false, call GetErrorString to get some text to present to the user.
bool Assemble(const char *text, std::vector<u16> *code, std::vector<int> *line_numbers = NULL); bool Assemble(const char *text, std::vector<u16> &code, std::vector<int> *line_numbers = NULL);
std::string GetErrorString() const { return last_error_str; } std::string GetErrorString() const { return last_error_str; }
err_t GetError() const { return last_error; } err_t GetError() const { return last_error; }
@ -112,17 +112,16 @@ private:
void BuildCode(const opc_t *opc, param_t *par, u32 par_count, u16 *outbuf); void BuildCode(const opc_t *opc, param_t *par, u32 par_count, u16 *outbuf);
char *gdg_buffer; char *gdg_buffer;
int gdg_buffer_size;
std::string include_dir; std::string include_dir;
std::string cur_line; std::string cur_line;
u32 m_cur_addr; u32 m_cur_addr;
int m_totalSize;
u8 m_cur_pass; u8 m_cur_pass;
LabelMap labels; LabelMap labels;
FILE *fsrc;
u32 code_line; u32 code_line;
bool failed; bool failed;
std::string last_error_str; std::string last_error_str;

View File

@ -72,7 +72,7 @@ DSPDisassembler::~DSPDisassembler()
fclose(uo); fclose(uo);
} }
bool DSPDisassembler::Disassemble(int start_pc, const std::vector<u16> &code, std::string *text) bool DSPDisassembler::Disassemble(int start_pc, const std::vector<u16> &code, std::string &text)
{ {
const char *tmp1 = "tmp1.bin"; const char *tmp1 = "tmp1.bin";
const char *tmp2 = "tmp.txt"; const char *tmp2 = "tmp.txt";
@ -187,7 +187,7 @@ static void MakeLowerCase(char *ptr)
} }
} }
void DSPDisassembler::DisOpcode(const u16 *binbuf, int pass, u16 *pc, std::string *dest) void DSPDisassembler::DisOpcode(const u16 *binbuf, int pass, u16 *pc, std::string &dest)
{ {
char buffer[256]; char buffer[256];
char *buf = buffer; char *buf = buffer;
@ -200,7 +200,7 @@ void DSPDisassembler::DisOpcode(const u16 *binbuf, int pass, u16 *pc, std::strin
if ((*pc & 0x7fff) >= 0x1000) if ((*pc & 0x7fff) >= 0x1000)
{ {
*pc++; *pc++;
dest->append("; outside memory"); dest.append("; outside memory");
return; return;
} }
@ -318,10 +318,10 @@ void DSPDisassembler::DisOpcode(const u16 *binbuf, int pass, u16 *pc, std::strin
*pc += opc->size & ~P_EXT; *pc += opc->size & ~P_EXT;
if (pass == 2) if (pass == 2)
dest->append(buffer); dest.append(buffer);
} }
bool DSPDisassembler::DisFile(const char* name, int pass, std::string *output) bool DSPDisassembler::DisFile(const char* name, int pass, std::string &output)
{ {
FILE* in = fopen(name, "rb"); FILE* in = fopen(name, "rb");
if (in == NULL) if (in == NULL)
@ -342,7 +342,7 @@ bool DSPDisassembler::DisFile(const char* name, int pass, std::string *output)
{ {
DisOpcode(binbuf, pass, &pc, output); DisOpcode(binbuf, pass, &pc, output);
if (pass == 2) if (pass == 2)
output->append("\n"); output.append("\n");
} }
delete [] binbuf; delete [] binbuf;
return true; return true;

View File

@ -63,15 +63,15 @@ public:
DSPDisassembler(const AssemblerSettings &settings); DSPDisassembler(const AssemblerSettings &settings);
~DSPDisassembler(); ~DSPDisassembler();
bool Disassemble(int start_pc, const std::vector<u16> &code, std::string *text); bool Disassemble(int start_pc, const std::vector<u16> &code, std::string &text);
// Warning - this one is trickier to use right. // Warning - this one is trickier to use right.
// Use pass == 2 if you're just using it by itself. // Use pass == 2 if you're just using it by itself.
void DisOpcode(const u16 *binbuf, int pass, u16 *pc, std::string *dest); void DisOpcode(const u16 *binbuf, int pass, u16 *pc, std::string &dest);
private: private:
// Moves PC forward and writes the result to dest. // Moves PC forward and writes the result to dest.
bool DisFile(const char* name, int pass, std::string *output); bool DisFile(const char* name, int pass, std::string &output);
char* DisParams(const DSPOPCTemplate& opc, u16 op1, u16 op2, char* strbuf); char* DisParams(const DSPOPCTemplate& opc, u16 op1, u16 op2, char* strbuf);
std::map<u16, int> unk_opcodes; std::map<u16, int> unk_opcodes;

View File

@ -33,19 +33,19 @@ bool RoundTrip(const std::vector<u16> &code1)
{ {
std::vector<u16> code2; std::vector<u16> code2;
std::string text; std::string text;
if (!Disassemble(code1, false, &text)) if (!Disassemble(code1, false, text))
{ {
printf("RoundTrip: Disassembly failed.\n"); printf("RoundTrip: Disassembly failed.\n");
return false; return false;
} }
if (!Assemble(text.c_str(), &code2)) if (!Assemble(text.c_str(), code2))
{ {
printf("RoundTrip: Assembly failed.\n"); printf("RoundTrip: Assembly failed.\n");
return false; return false;
} }
if (!Compare(code1, code2)) if (!Compare(code1, code2))
{ {
Disassemble(code1, true, &text); Disassemble(code1, true, text);
printf("%s", text.c_str()); printf("%s", text.c_str());
} }
return true; return true;
@ -57,13 +57,13 @@ bool SuperTrip(const char *asm_code)
{ {
std::vector<u16> code1, code2; std::vector<u16> code1, code2;
std::string text; std::string text;
if (!Assemble(asm_code, &code1)) if (!Assemble(asm_code, code1))
{ {
printf("SuperTrip: First assembly failed\n"); printf("SuperTrip: First assembly failed\n");
return false; return false;
} }
printf("First assembly: %i words\n", (int)code1.size()); printf("First assembly: %i words\n", (int)code1.size());
if (!Disassemble(code1, false, &text)) if (!Disassemble(code1, false, text))
{ {
printf("SuperTrip: Disassembly failed\n"); printf("SuperTrip: Disassembly failed\n");
return false; return false;
@ -73,7 +73,7 @@ bool SuperTrip(const char *asm_code)
printf("Disass:\n"); printf("Disass:\n");
printf("%s", text.c_str()); printf("%s", text.c_str());
} }
if (!Assemble(text.c_str(), &code2)) if (!Assemble(text.c_str(), code2))
{ {
printf("SuperTrip: Second assembly failed\n"); printf("SuperTrip: Second assembly failed\n");
return false; return false;
@ -186,7 +186,7 @@ void RunAsmTests()
*/ */
std::string dsp_test; std::string dsp_test;
if (File::ReadFileToString(true, "Testdata/dsp_test.s", &dsp_test)) if (File::ReadFileToString(true, "Testdata/dsp_test.s", dsp_test))
fail = fail || !SuperTrip(dsp_test.c_str()); fail = fail || !SuperTrip(dsp_test.c_str());
if (!fail) if (!fail)
printf("All passed!\n"); printf("All passed!\n");
@ -256,10 +256,10 @@ int main(int argc, const char *argv[])
// Two binary inputs, let's diff. // Two binary inputs, let's diff.
std::string binary_code; std::string binary_code;
std::vector<u16> code1, code2; std::vector<u16> code1, code2;
File::ReadFileToString(false, input_name.c_str(), &binary_code); File::ReadFileToString(false, input_name.c_str(), binary_code);
BinaryStringBEToCode(binary_code, &code1); BinaryStringBEToCode(binary_code, code1);
File::ReadFileToString(false, output_name.c_str(), &binary_code); File::ReadFileToString(false, output_name.c_str(), binary_code);
BinaryStringBEToCode(binary_code, &code2); BinaryStringBEToCode(binary_code, code2);
Compare(code1, code2); Compare(code1, code2);
return 0; return 0;
} }
@ -273,10 +273,10 @@ int main(int argc, const char *argv[])
} }
std::string binary_code; std::string binary_code;
std::vector<u16> code; std::vector<u16> code;
File::ReadFileToString(false, input_name.c_str(), &binary_code); File::ReadFileToString(false, input_name.c_str(), binary_code);
BinaryStringBEToCode(binary_code, &code); BinaryStringBEToCode(binary_code, code);
std::string text; std::string text;
Disassemble(code, true, &text); Disassemble(code, true, text);
if (!output_name.empty()) if (!output_name.empty())
File::WriteStringToFile(true, text, output_name.c_str()); File::WriteStringToFile(true, text, output_name.c_str());
else else
@ -290,26 +290,29 @@ int main(int argc, const char *argv[])
return 1; return 1;
} }
std::string source; std::string source;
if (File::ReadFileToString(true, input_name.c_str(), &source)) if (File::ReadFileToString(true, input_name.c_str(), source))
{ {
std::vector<u16> code; std::vector<u16> code;
if(!Assemble(source.c_str(), &code)) {
if(!Assemble(source.c_str(), code)) {
printf("Assemble: Assembly failed due to errors\n"); printf("Assemble: Assembly failed due to errors\n");
return 1; return 1;
} }
if (!output_name.empty()) if (!output_name.empty())
{ {
std::string binary_code; std::string binary_code;
CodeToBinaryStringBE(code, &binary_code); CodeToBinaryStringBE(code, binary_code);
File::WriteStringToFile(false, binary_code, output_name.c_str()); File::WriteStringToFile(false, binary_code, output_name.c_str());
} }
if (!output_header_name.empty()) if (!output_header_name.empty())
{ {
std::string header; std::string header;
CodeToHeader(code, output_header_name.c_str(), &header); CodeToHeader(code, output_header_name.c_str(), header);
File::WriteStringToFile(true, header, (output_header_name + ".h").c_str()); File::WriteStringToFile(true, header, (output_header_name + ".h").c_str());
} }
} }
source.clear();
} }
printf("Assembly completed successfully!\n"); printf("Assembly completed successfully!\n");

View File

@ -219,7 +219,7 @@ void DSPDebuggerLLE::RebuildDisAsmListView()
DSPDisassembler disasm(settings); DSPDisassembler disasm(settings);
std::string op_str; std::string op_str;
disasm.DisOpcode(binbuf, 2, &settings.pc, &op_str); disasm.DisOpcode(binbuf, 2, &settings.pc, op_str);
const char* pParameter = NULL; const char* pParameter = NULL;
const char* pExtension = NULL; const char* pExtension = NULL;

View File

@ -48,7 +48,7 @@ bool DumpDSPCode(const u8 *code_be, int size_in_bytes, u32 crc)
// Load the binary back in. // Load the binary back in.
std::vector<u16> code; std::vector<u16> code;
LoadBinary(binFile, &code); LoadBinary(binFile, code);
AssemblerSettings settings; AssemblerSettings settings;
settings.show_hex = true; settings.show_hex = true;
@ -59,7 +59,7 @@ bool DumpDSPCode(const u8 *code_be, int size_in_bytes, u32 crc)
std::string text; std::string text;
DSPDisassembler disasm(settings); DSPDisassembler disasm(settings);
if (!disasm.Disassemble(0, code, &text)) if (!disasm.Disassemble(0, code, text))
return false; return false;
return File::WriteStringToFile(true, text, txtFile); return File::WriteStringToFile(true, text, txtFile);