2009-04-14 20:44:03 +00:00
|
|
|
/*====================================================================
|
|
|
|
|
|
|
|
$Id: assemble.cpp,v 1.3 2008-11-11 01:04:26 wntrmute Exp $
|
|
|
|
|
|
|
|
project: GameCube DSP Tool (gcdsp)
|
|
|
|
mail: duddie@walla.com
|
|
|
|
|
|
|
|
Copyright (c) 2005 Duddie
|
|
|
|
|
|
|
|
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; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
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 for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
Revision 1.4 2008/10/04 10:30:00 Hermes
|
|
|
|
added function to export the code to .h file
|
|
|
|
added support for '/ *' '* /' and '//' for comentaries
|
|
|
|
added some sintax detection when use registers
|
|
|
|
|
|
|
|
$Log: not supported by cvs2svn $
|
|
|
|
Revision 1.2 2005/09/14 02:19:29 wntrmute
|
|
|
|
added header guards
|
|
|
|
use standard main function
|
|
|
|
|
|
|
|
Revision 1.1 2005/08/24 22:13:34 wntrmute
|
|
|
|
Initial import
|
|
|
|
|
|
|
|
|
|
|
|
====================================================================*/
|
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2009-04-14 20:44:03 +00:00
|
|
|
|
|
|
|
#include <map>
|
2009-04-25 10:38:26 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2009-04-14 20:44:03 +00:00
|
|
|
|
|
|
|
#include "Common.h"
|
2009-04-14 22:30:31 +00:00
|
|
|
#include "FileUtil.h"
|
2009-04-14 20:44:03 +00:00
|
|
|
#include "DSPInterpreter.h"
|
|
|
|
#include "DSPTables.h"
|
|
|
|
#include "disassemble.h"
|
|
|
|
#include "assemble.h"
|
|
|
|
|
|
|
|
static const char *err_string[] =
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
"Unknown Error",
|
|
|
|
"Unknown opcode",
|
|
|
|
"Not enough parameters",
|
|
|
|
"Too many parameters",
|
|
|
|
"Wrong parameter",
|
|
|
|
"Expected parameter of type 'string'",
|
|
|
|
"Expected parameter of type 'value'",
|
|
|
|
"Expected parameter of type 'register'",
|
|
|
|
"Expected parameter of type 'memory pointer'",
|
|
|
|
"Expected parameter of type 'immediate'",
|
|
|
|
"Incorrect binary value",
|
|
|
|
"Incorrect hexadecimal value",
|
|
|
|
"Incorrect decimal value",
|
|
|
|
"Label already exists",
|
|
|
|
"Label not defined",
|
|
|
|
"No matching brackets",
|
|
|
|
"This opcode cannot be extended",
|
|
|
|
"Given extending params for non extensible opcode",
|
|
|
|
"Wrong parameter: must be accumulator register",
|
|
|
|
"Wrong parameter: must be mid accumulator register",
|
|
|
|
"Invalid register",
|
|
|
|
"Number out of range"
|
|
|
|
};
|
|
|
|
|
2009-04-24 14:36:31 +00:00
|
|
|
DSPAssembler::DSPAssembler(const AssemblerSettings &settings) :
|
|
|
|
m_cur_addr(0),
|
|
|
|
m_cur_pass(0),
|
|
|
|
m_current_param(0),
|
2009-04-25 10:44:42 +00:00
|
|
|
settings_(settings),
|
|
|
|
gdg_buffer(NULL)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DSPAssembler::~DSPAssembler()
|
|
|
|
{
|
2009-04-25 10:44:42 +00:00
|
|
|
if(gdg_buffer)
|
|
|
|
free(gdg_buffer);
|
2009-04-14 22:30:31 +00:00
|
|
|
}
|
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
bool DSPAssembler::Assemble(const char *text, std::vector<u16> &code, std::vector<int> *line_numbers)
|
2009-04-14 22:30:31 +00:00
|
|
|
{
|
|
|
|
if (line_numbers)
|
|
|
|
line_numbers->clear();
|
|
|
|
const char *fname = "tmp.asm";
|
|
|
|
if (!File::WriteStringToFile(true, text, fname))
|
|
|
|
return false;
|
|
|
|
InitPass(1);
|
|
|
|
if (!AssembleFile(fname, 1))
|
|
|
|
return false;
|
2009-04-25 10:38:26 +00:00
|
|
|
|
|
|
|
// We now have the size of the output buffer
|
|
|
|
if (m_totalSize > 0)
|
|
|
|
{
|
|
|
|
gdg_buffer = (char *)malloc(m_totalSize * sizeof(u16) + 4);
|
2009-04-25 10:44:42 +00:00
|
|
|
if(!gdg_buffer)
|
|
|
|
return false;
|
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
memset(gdg_buffer, 0, m_totalSize * sizeof(u16));
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
|
2009-04-14 22:30:31 +00:00
|
|
|
InitPass(2);
|
|
|
|
if (!AssembleFile(fname, 2))
|
|
|
|
return false;
|
2009-04-14 20:44:03 +00:00
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
code.resize(m_totalSize);
|
|
|
|
for (int i = 0; i < m_totalSize; i++) {
|
|
|
|
code[i] = *(u16 *)(gdg_buffer + i * 2);
|
2009-04-14 22:30:31 +00:00
|
|
|
}
|
|
|
|
|
2009-04-25 10:44:42 +00:00
|
|
|
if(gdg_buffer) {
|
|
|
|
free(gdg_buffer);
|
|
|
|
gdg_buffer = NULL;
|
|
|
|
}
|
|
|
|
|
2009-04-14 22:30:31 +00:00
|
|
|
last_error_str = "(no errors)";
|
|
|
|
last_error = ERR_OK;
|
2009-04-25 02:13:33 +00:00
|
|
|
|
2009-04-14 22:30:31 +00:00
|
|
|
return true;
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
2009-04-18 11:31:37 +00:00
|
|
|
void DSPAssembler::ShowError(err_t err_code, const char *extra_info)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
failed = true;
|
2009-04-14 22:30:31 +00:00
|
|
|
char error_buffer[1024];
|
|
|
|
char *buf_ptr = error_buffer;
|
2009-06-11 07:11:05 +00:00
|
|
|
buf_ptr += sprintf(buf_ptr, "%i : %s ", code_line, cur_line.c_str());
|
2009-04-14 20:44:03 +00:00
|
|
|
if (!extra_info)
|
|
|
|
extra_info = "-";
|
|
|
|
|
2009-04-24 14:36:31 +00:00
|
|
|
if (m_current_param == 0)
|
2009-04-14 22:30:31 +00:00
|
|
|
buf_ptr += sprintf(buf_ptr, "ERROR: %s Line: %d : %s\n", err_string[err_code], code_line, extra_info);
|
2009-04-14 20:44:03 +00:00
|
|
|
else
|
2009-04-14 22:30:31 +00:00
|
|
|
buf_ptr += sprintf(buf_ptr, "ERROR: %s Line: %d Param: %d : %s\n",
|
2009-04-24 14:36:31 +00:00
|
|
|
err_string[err_code], code_line, m_current_param, extra_info);
|
2009-04-14 22:30:31 +00:00
|
|
|
last_error_str = error_buffer;
|
|
|
|
last_error = err_code;
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *skip_spaces(char *ptr)
|
|
|
|
{
|
|
|
|
while (*ptr == ' ')
|
|
|
|
ptr++;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *skip_spaces(const char *ptr)
|
|
|
|
{
|
|
|
|
while (*ptr == ' ')
|
|
|
|
ptr++;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a standalone value - it can be a number in one of several formats or a label.
|
2009-04-18 11:31:37 +00:00
|
|
|
s32 DSPAssembler::ParseValue(const char *str)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
bool negative = false;
|
|
|
|
s32 val = 0;
|
|
|
|
const char *ptr = str;
|
|
|
|
|
|
|
|
if (ptr[0] == '#')
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
negative = true; // Wow! Double # (needed one to get in here) negates???
|
|
|
|
}
|
|
|
|
if (ptr[0] == '-')
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
negative = true;
|
|
|
|
}
|
|
|
|
if (ptr[0] == '0')
|
|
|
|
{
|
|
|
|
if (ptr[1] >= '0' && ptr[1] <= '9')
|
|
|
|
{
|
|
|
|
for (int i = 0; ptr[i] != 0; i++)
|
|
|
|
{
|
|
|
|
val *= 10;
|
|
|
|
if (ptr[i] >= '0' && ptr[i] <= '9')
|
|
|
|
val += ptr[i] - '0';
|
|
|
|
else
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_INCORRECT_DEC, str);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (ptr[1])
|
|
|
|
{
|
|
|
|
case 'X': // hex
|
|
|
|
for (int i = 2 ; ptr[i] != 0 ; i++)
|
|
|
|
{
|
|
|
|
val <<= 4;
|
|
|
|
if (ptr[i] >= 'a' && ptr[i] <= 'f')
|
|
|
|
val += (ptr[i]-'a'+10);
|
|
|
|
else if (ptr[i] >= 'A' && ptr[i] <= 'F')
|
|
|
|
val += (ptr[i]-'A'+10);
|
|
|
|
else if (ptr[i] >= '0' && ptr[i] <= '9')
|
|
|
|
val += (ptr[i] - '0');
|
|
|
|
else
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_INCORRECT_HEX, str);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\'': // binary
|
|
|
|
for (int i = 2; ptr[i] != 0; i++)
|
|
|
|
{
|
|
|
|
val *=2;
|
|
|
|
if(ptr[i] >= '0' && ptr[i] <= '1')
|
|
|
|
val += ptr[i] - '0';
|
|
|
|
else
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_INCORRECT_BIN, str);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// value is 0 or error
|
|
|
|
val = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Symbol starts with a digit - it's a dec number.
|
|
|
|
if (ptr[0] >= '0' && ptr[0] <= '9')
|
|
|
|
{
|
|
|
|
for (int i = 0; ptr[i] != 0; i++)
|
|
|
|
{
|
|
|
|
val *= 10;
|
|
|
|
if (ptr[i] >= '0' && ptr[i] <= '9')
|
|
|
|
val += ptr[i] - '0';
|
|
|
|
else
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_INCORRECT_DEC, str);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Everything else is a label.
|
|
|
|
{
|
|
|
|
// Lookup label
|
2009-04-18 11:31:37 +00:00
|
|
|
u16 value;
|
|
|
|
if (labels.GetLabelValue(ptr, &value))
|
|
|
|
return value;
|
2009-04-24 14:36:31 +00:00
|
|
|
if (m_cur_pass == 2)
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_UNKNOWN_LABEL, str);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (negative)
|
|
|
|
return -val;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modifies both src and dst!
|
|
|
|
// What does it do, really??
|
2009-04-18 11:31:37 +00:00
|
|
|
char *DSPAssembler::FindBrackets(char *src, char *dst)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
s32 len = (s32) strlen(src);
|
|
|
|
s32 first = -1;
|
|
|
|
s32 count = 0;
|
|
|
|
s32 i, j;
|
|
|
|
j = 0;
|
|
|
|
for (i = 0 ; i < len ; i++)
|
|
|
|
{
|
|
|
|
if (src[i] == '(')
|
|
|
|
{
|
|
|
|
if (first < 0)
|
|
|
|
{
|
|
|
|
count = 1;
|
|
|
|
src[i] = 0x0;
|
|
|
|
first = i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
dst[j++] = src[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (src[i] == ')')
|
|
|
|
{
|
|
|
|
if (--count == 0)
|
|
|
|
{
|
|
|
|
dst[j] = 0;
|
|
|
|
return &src[i+1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dst[j++] = src[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (first >= 0)
|
|
|
|
dst[j++] = src[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count)
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_NO_MATCHING_BRACKETS);
|
2009-04-14 20:44:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bizarre in-place expression evaluator.
|
2009-04-18 11:31:37 +00:00
|
|
|
u32 DSPAssembler::ParseExpression(const char *ptr)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
char *pbuf;
|
|
|
|
s32 val = 0;
|
|
|
|
|
|
|
|
char *d_buffer = (char *)malloc(1024);
|
|
|
|
char *s_buffer = (char *)malloc(1024);
|
|
|
|
strcpy(s_buffer, ptr);
|
|
|
|
|
2009-04-18 11:31:37 +00:00
|
|
|
while ((pbuf = FindBrackets(s_buffer, d_buffer)) != NULL)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
2009-04-18 11:31:37 +00:00
|
|
|
val = ParseExpression(d_buffer);
|
2009-04-14 20:44:03 +00:00
|
|
|
sprintf(d_buffer, "%s%d%s", s_buffer, val, pbuf);
|
|
|
|
strcpy(s_buffer, d_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
int j = 0;
|
|
|
|
for (int i = 0; i < ((s32)strlen(s_buffer) + 1) ; i++)
|
|
|
|
{
|
|
|
|
char c = s_buffer[i];
|
|
|
|
if (c != ' ')
|
|
|
|
d_buffer[j++] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < ((s32)strlen(d_buffer) + 1) ; i++)
|
|
|
|
{
|
|
|
|
char c = d_buffer[i];
|
|
|
|
if (c == '-')
|
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
c = '#';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (d_buffer[i - 1])
|
|
|
|
{
|
|
|
|
case '/':
|
|
|
|
case '%':
|
|
|
|
case '*':
|
|
|
|
c = '#';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d_buffer[i] = c;
|
|
|
|
}
|
2009-04-18 11:31:37 +00:00
|
|
|
|
2009-04-14 20:44:03 +00:00
|
|
|
while ((pbuf = strstr(d_buffer, "+")) != NULL)
|
|
|
|
{
|
|
|
|
*pbuf = 0x0;
|
2009-04-18 11:31:37 +00:00
|
|
|
val = ParseExpression(d_buffer) + ParseExpression(pbuf+1);
|
2009-04-14 20:44:03 +00:00
|
|
|
sprintf(d_buffer, "%d", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((pbuf = strstr(d_buffer, "-")) != NULL)
|
|
|
|
{
|
|
|
|
*pbuf = 0x0;
|
2009-04-18 11:31:37 +00:00
|
|
|
val = ParseExpression(d_buffer) - ParseExpression(pbuf+1);
|
2009-04-14 20:44:03 +00:00
|
|
|
if (val < 0)
|
|
|
|
{
|
|
|
|
val = 0x10000 + (val & 0xffff); // ATTENTION: avoid a terrible bug!!! number cannot write with '-' in sprintf
|
|
|
|
fprintf(stderr, "WARNING: Number Underflow at Line: %d \n", code_line);
|
|
|
|
}
|
|
|
|
sprintf(d_buffer, "%d", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((pbuf = strstr(d_buffer, "*")) != NULL)
|
|
|
|
{
|
|
|
|
*pbuf = 0x0;
|
2009-04-18 11:31:37 +00:00
|
|
|
val = ParseExpression(d_buffer) * ParseExpression(pbuf+1);
|
2009-04-14 20:44:03 +00:00
|
|
|
sprintf(d_buffer, "%d", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((pbuf = strstr(d_buffer, "/")) != NULL)
|
|
|
|
{
|
|
|
|
*pbuf = 0x0;
|
2009-04-18 11:31:37 +00:00
|
|
|
val = ParseExpression(d_buffer) / ParseExpression(pbuf+1);
|
2009-04-14 20:44:03 +00:00
|
|
|
sprintf(d_buffer, "%d", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((pbuf = strstr(d_buffer, "|")) != NULL)
|
|
|
|
{
|
|
|
|
*pbuf = 0x0;
|
2009-04-18 11:31:37 +00:00
|
|
|
val = ParseExpression(d_buffer) | ParseExpression(pbuf+1);
|
2009-04-14 20:44:03 +00:00
|
|
|
sprintf(d_buffer, "%d", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((pbuf = strstr(d_buffer, "&")) != NULL)
|
|
|
|
{
|
|
|
|
*pbuf = 0x0;
|
2009-04-18 11:31:37 +00:00
|
|
|
val = ParseExpression(d_buffer) & ParseExpression(pbuf+1);
|
2009-04-14 20:44:03 +00:00
|
|
|
sprintf(d_buffer, "%d", val);
|
|
|
|
}
|
|
|
|
|
2009-04-18 11:31:37 +00:00
|
|
|
val = ParseValue(d_buffer);
|
2009-04-14 20:44:03 +00:00
|
|
|
free(d_buffer);
|
|
|
|
free(s_buffer);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroys parstr
|
2009-04-18 11:31:37 +00:00
|
|
|
u32 DSPAssembler::GetParams(char *parstr, param_t *par)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
u32 count = 0;
|
|
|
|
char *tmpstr = skip_spaces(parstr);
|
|
|
|
tmpstr = strtok(tmpstr, ",\x00");
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
if (tmpstr == NULL)
|
|
|
|
break;
|
|
|
|
tmpstr = skip_spaces(tmpstr);
|
|
|
|
if (strlen(tmpstr) == 0)
|
|
|
|
break;
|
|
|
|
if (tmpstr)
|
|
|
|
count++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
par[i].type = P_NONE;
|
|
|
|
switch (tmpstr[0])
|
|
|
|
{
|
|
|
|
case '"':
|
|
|
|
par[i].str = strtok(tmpstr, "\"");
|
|
|
|
par[i].type = P_STR;
|
|
|
|
break;
|
|
|
|
case '#':
|
2009-04-18 11:31:37 +00:00
|
|
|
par[i].val = ParseExpression(tmpstr + 1);
|
2009-04-14 20:44:03 +00:00
|
|
|
par[i].type = P_IMM;
|
|
|
|
break;
|
|
|
|
case '@':
|
|
|
|
if (tmpstr[1] == '$')
|
|
|
|
{
|
2009-04-18 11:31:37 +00:00
|
|
|
par[i].val = ParseExpression(tmpstr + 2);
|
2009-04-14 20:44:03 +00:00
|
|
|
par[i].type = P_PRG;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-04-18 11:31:37 +00:00
|
|
|
par[i].val = ParseExpression(tmpstr + 1);
|
2009-04-14 20:44:03 +00:00
|
|
|
par[i].type = P_MEM;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '$':
|
2009-04-18 11:31:37 +00:00
|
|
|
par[i].val = ParseExpression(tmpstr + 1);
|
2009-04-14 20:44:03 +00:00
|
|
|
par[i].type = P_REG;
|
|
|
|
break;
|
2009-04-18 11:31:37 +00:00
|
|
|
|
2009-04-14 20:44:03 +00:00
|
|
|
default:
|
2009-04-18 11:31:37 +00:00
|
|
|
par[i].val = ParseExpression(tmpstr);
|
2009-04-14 20:44:03 +00:00
|
|
|
par[i].type = P_VAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmpstr = strtok(NULL, ",\x00");
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2009-04-18 11:31:37 +00:00
|
|
|
const opc_t *DSPAssembler::FindOpcode(const char *opcode, u32 par_count, const opc_t * const opcod, int opcod_size)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
if (opcode[0] == 'C' && opcode[1] == 'W')
|
|
|
|
return &cw;
|
|
|
|
|
|
|
|
AliasMap::const_iterator alias_iter = aliases.find(opcode);
|
|
|
|
if (alias_iter != aliases.end())
|
|
|
|
opcode = alias_iter->second.c_str();
|
|
|
|
for (int i = 0; i < opcod_size; i++)
|
|
|
|
{
|
|
|
|
const opc_t *opc = &opcod[i];
|
|
|
|
if (strcmp(opc->name, opcode) == 0)
|
|
|
|
{
|
|
|
|
if (par_count < opc->param_count)
|
|
|
|
{
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_NOT_ENOUGH_PARAMETERS);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
if (par_count > opc->param_count)
|
|
|
|
{
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_TOO_MANY_PARAMETERS);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
return opc;
|
|
|
|
}
|
|
|
|
}
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_UNKNOWN_OPCODE);
|
2009-04-14 20:44:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// weird...
|
|
|
|
u16 get_mask_shifted_down(u16 mask)
|
|
|
|
{
|
|
|
|
while (!(mask & 1))
|
|
|
|
mask >>= 1;
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2009-04-18 11:31:37 +00:00
|
|
|
bool DSPAssembler::VerifyParams(const opc_t *opc, param_t *par, int count, bool ext)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
const int current_param = i + 1; // just for display.
|
|
|
|
if (opc->params[i].type != par[i].type || (par[i].type & P_REG))
|
|
|
|
{
|
2009-04-18 11:31:37 +00:00
|
|
|
if (par[i].type == P_VAL &&
|
|
|
|
(opc->params[i].type == P_ADDR_I || opc->params[i].type == P_ADDR_D))
|
|
|
|
{
|
|
|
|
// Data and instruction addresses are valid as VAL values.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-04-14 20:44:03 +00:00
|
|
|
if ((opc->params[i].type & P_REG) && (par[i].type & P_REG))
|
|
|
|
{
|
|
|
|
// Just a temp. Should be replaced with more purposeful vars.
|
|
|
|
int value;
|
|
|
|
|
|
|
|
// modified by Hermes: test the register range
|
|
|
|
switch ((unsigned)opc->params[i].type)
|
|
|
|
{
|
|
|
|
case P_REG18:
|
|
|
|
case P_REG19:
|
|
|
|
case P_REG1A:
|
|
|
|
value = (opc->params[i].type >> 8) & 31;
|
|
|
|
if ((int)par[i].val < value ||
|
|
|
|
(int)par[i].val > value + get_mask_shifted_down(opc->params[i].mask))
|
|
|
|
{
|
|
|
|
if (ext) fprintf(stderr, "(ext) ");
|
|
|
|
fprintf(stderr, "%s (param %i)", cur_line.c_str(), current_param);
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_INVALID_REGISTER);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case P_PRG:
|
|
|
|
if ((int)par[i].val < 0 || (int)par[i].val > 0x3)
|
|
|
|
{
|
|
|
|
if (ext) fprintf(stderr, "(ext) ");
|
|
|
|
fprintf(stderr, "%s (param %i)", cur_line.c_str(), current_param);
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_INVALID_REGISTER);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case P_ACC:
|
|
|
|
if ((int)par[i].val < 0x20 || (int)par[i].val > 0x21)
|
|
|
|
{
|
|
|
|
if (ext) fprintf(stderr, "(ext) ");
|
2009-04-18 11:31:37 +00:00
|
|
|
if (par[i].val >= 0x1e && par[i].val <= 0x1f) {
|
|
|
|
fprintf(stderr, "%i : %s", code_line, cur_line.c_str());
|
2009-04-24 14:36:31 +00:00
|
|
|
fprintf(stderr, "WARNING: $ACM%d register used instead of $ACC%d register Line: %d Param: %d Ext: %d\n",
|
2009-04-14 20:44:03 +00:00
|
|
|
(par[i].val & 1), (par[i].val & 1), code_line, current_param, ext);
|
2009-04-18 11:31:37 +00:00
|
|
|
}
|
|
|
|
else if (par[i].val >= 0x1c && par[i].val <= 0x1d) {
|
2009-04-14 20:44:03 +00:00
|
|
|
fprintf(stderr, "WARNING: $ACL%d register used instead of $ACC%d register Line: %d Param: %d\n",
|
|
|
|
(par[i].val & 1), (par[i].val & 1), code_line, current_param);
|
2009-04-18 11:31:37 +00:00
|
|
|
}
|
2009-04-14 20:44:03 +00:00
|
|
|
else
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_WRONG_PARAMETER_ACC);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case P_ACCM:
|
|
|
|
if ((int)par[i].val < 0x1e || (int)par[i].val > 0x1f)
|
|
|
|
{
|
|
|
|
if (ext) fprintf(stderr, "(ext) ");
|
|
|
|
if (par[i].val >= 0x1c && par[i].val <= 0x1d)
|
|
|
|
fprintf(stderr, "WARNING: $ACL%d register used instead of $ACM%d register Line: %d Param: %d\n",
|
|
|
|
(par[i].val & 1), (par[i].val & 1), code_line, current_param);
|
|
|
|
else if (par[i].val >= 0x20 && par[i].val <= 0x21)
|
|
|
|
fprintf(stderr, "WARNING: $ACC%d register used instead of $ACM%d register Line: %d Param: %d\n",
|
|
|
|
(par[i].val & 1), (par[i].val & 1), code_line, current_param);
|
|
|
|
else
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_WRONG_PARAMETER_ACC);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case P_ACCL:
|
|
|
|
if ((int)par[i].val < 0x1c || (int)par[i].val > 0x1d)
|
|
|
|
{
|
|
|
|
if (ext) fprintf(stderr, "(ext) ");
|
|
|
|
if (par[i].val >= 0x1e && par[i].val <= 0x1f)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s", cur_line.c_str());
|
|
|
|
fprintf(stderr, "WARNING: $ACM%d register used instead of $ACL%d register Line: %d Param: %d\n",
|
|
|
|
(par[i].val & 1), (par[i].val & 1), code_line, current_param);
|
|
|
|
}
|
|
|
|
else if (par[i].val >= 0x20 && par[i].val <= 0x21) {
|
|
|
|
fprintf(stderr, "%s", cur_line.c_str());
|
|
|
|
fprintf(stderr, "WARNING: $ACC%d register used instead of $ACL%d register Line: %d Param: %d\n",
|
|
|
|
(par[i].val & 1), (par[i].val & 1), code_line, current_param);
|
|
|
|
}
|
|
|
|
else
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_WRONG_PARAMETER_ACC);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* case P_ACCM_D: //P_ACC_MID:
|
|
|
|
if ((int)par[i].val < 0x1e || (int)par[i].val > 0x1f)
|
|
|
|
{
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_WRONG_PARAMETER_MID_ACC);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
break;*/
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-04-18 11:31:37 +00:00
|
|
|
switch (par[i].type & (P_REG | 7))
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
case P_REG:
|
|
|
|
if (ext) fprintf(stderr, "(ext) ");
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_EXPECTED_PARAM_REG);
|
2009-04-14 20:44:03 +00:00
|
|
|
break;
|
|
|
|
case P_MEM:
|
|
|
|
if (ext) fprintf(stderr, "(ext) ");
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_EXPECTED_PARAM_MEM);
|
2009-04-14 20:44:03 +00:00
|
|
|
break;
|
|
|
|
case P_VAL:
|
|
|
|
if (ext) fprintf(stderr, "(ext) ");
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_EXPECTED_PARAM_VAL);
|
2009-04-14 20:44:03 +00:00
|
|
|
break;
|
|
|
|
case P_IMM:
|
|
|
|
if (ext) fprintf(stderr, "(ext) ");
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_EXPECTED_PARAM_IMM);
|
2009-04-14 20:44:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_WRONG_PARAMETER);
|
2009-04-14 20:44:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if ((opc->params[i].type & 3) != 0 && (par[i].type & 3) != 0)
|
|
|
|
{
|
|
|
|
// modified by Hermes: test NUMBER range
|
|
|
|
int value = get_mask_shifted_down(opc->params[i].mask);
|
|
|
|
unsigned int valueu = 0xffff & ~(value >> 1);
|
|
|
|
if ((int)par[i].val < 0)
|
|
|
|
{
|
|
|
|
if (value == 7) // value 7 por sbclr/sbset
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Value must be from 0x0 to 0x%x\n", value);
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_OUT_RANGE_NUMBER);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
else if (opc->params[i].type == P_MEM)
|
|
|
|
{
|
|
|
|
if (value < 256)
|
|
|
|
fprintf(stderr, "Address value must be from 0x%x to 0x%x\n",valueu, (value>>1));
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Address value must be from 0x0 to 0x%x\n", value);
|
|
|
|
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_OUT_RANGE_NUMBER);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
else if ((int)par[i].val < -((value >> 1) + 1))
|
|
|
|
{
|
|
|
|
if (value < 128)
|
|
|
|
fprintf(stderr, "Value must be from -0x%x to 0x%x, is %i\n",
|
|
|
|
(value >> 1) + 1, value >> 1, par[i].val);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Value must be from -0x%x to 0x%x or 0x0 to 0x%x, is %i\n",
|
|
|
|
(value >> 1) + 1, value >> 1, value, par[i].val);
|
|
|
|
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_OUT_RANGE_NUMBER);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (value == 7) // value 7 por sbclr/sbset
|
|
|
|
{
|
|
|
|
if (par[i].val > (unsigned)value)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Value must be from 0x%x to 0x%x, is %i\n",valueu, value, par[i].val);
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_OUT_RANGE_NUMBER);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (opc->params[i].type == P_MEM)
|
|
|
|
{
|
|
|
|
if (value < 256)
|
|
|
|
value >>= 1; // addressing 8 bit with sign
|
|
|
|
if (par[i].val > (unsigned)value &&
|
|
|
|
(par[i].val < valueu || par[i].val > (unsigned)0xffff))
|
|
|
|
{
|
|
|
|
if (value < 256)
|
|
|
|
fprintf(stderr,"Address value must be from 0x%x to 0x%x, is %04x\n", valueu, value, par[i].val);
|
|
|
|
else
|
|
|
|
fprintf(stderr,"Address value must be minor of 0x%x\n", value+1);
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_OUT_RANGE_NUMBER);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (value < 128)
|
|
|
|
value >>= 1; // special case ASL/ASR/LSL/LSR
|
|
|
|
if (par[i].val > (unsigned)value)
|
|
|
|
{
|
|
|
|
if (value < 64)
|
|
|
|
fprintf(stderr,"Value must be from -0x%x to 0x%x, is %i\n", (value + 1), value, par[i].val);
|
|
|
|
else
|
|
|
|
fprintf(stderr,"Value must be minor of 0x%x, is %i\n", value + 1, par[i].val);
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_OUT_RANGE_NUMBER);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2009-04-24 14:36:31 +00:00
|
|
|
m_current_param = 0;
|
2009-04-14 20:44:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Merge opcode with params.
|
2009-04-18 11:31:37 +00:00
|
|
|
void DSPAssembler::BuildCode(const opc_t *opc, param_t *par, u32 par_count, u16 *outbuf)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
2009-04-24 14:36:31 +00:00
|
|
|
outbuf[m_cur_addr] |= opc->opcode;
|
2009-04-14 20:44:03 +00:00
|
|
|
for (u32 i = 0; i < par_count; i++)
|
|
|
|
{
|
|
|
|
// Ignore the "reverse" parameters since they are implicit.
|
|
|
|
if (opc->params[i].type != P_ACC_D && opc->params[i].type != P_ACCM_D)
|
|
|
|
{
|
2009-04-24 14:36:31 +00:00
|
|
|
u16 t16 = outbuf[m_cur_addr + opc->params[i].loc];
|
2009-04-14 20:44:03 +00:00
|
|
|
u16 v16 = par[i].val;
|
|
|
|
if (opc->params[i].lshift > 0)
|
|
|
|
v16 <<= opc->params[i].lshift;
|
|
|
|
else
|
|
|
|
v16 >>= -opc->params[i].lshift;
|
|
|
|
v16 &= opc->params[i].mask;
|
2009-04-24 14:36:31 +00:00
|
|
|
outbuf[m_cur_addr + opc->params[i].loc] = t16 | v16;
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-14 22:30:31 +00:00
|
|
|
void DSPAssembler::InitPass(int pass)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
failed = false;
|
|
|
|
if (pass == 1)
|
|
|
|
{
|
|
|
|
// Reset label table. Pre-populate with hw addresses and registers.
|
2009-04-18 11:31:37 +00:00
|
|
|
labels.Clear();
|
|
|
|
labels.RegisterDefaults();
|
2009-04-14 20:44:03 +00:00
|
|
|
aliases.clear();
|
|
|
|
aliases["S15"] = "SET15";
|
|
|
|
aliases["S16"] = "SET16";
|
|
|
|
aliases["S40"] = "SET40";
|
|
|
|
}
|
2009-04-24 14:36:31 +00:00
|
|
|
m_cur_addr = 0;
|
2009-04-25 10:38:26 +00:00
|
|
|
m_totalSize = 0;
|
2009-04-14 20:44:03 +00:00
|
|
|
cur_segment = SEGMENT_CODE;
|
|
|
|
segment_addr[SEGMENT_CODE] = 0;
|
|
|
|
segment_addr[SEGMENT_DATA] = 0;
|
|
|
|
segment_addr[SEGMENT_OVERLAY] = 0;
|
|
|
|
}
|
|
|
|
|
2009-04-14 22:30:31 +00:00
|
|
|
bool DSPAssembler::AssembleFile(const char *fname, int pass)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
int disable_text = 0; // modified by Hermes
|
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
std::ifstream fsrc(fname);
|
|
|
|
|
|
|
|
if (fsrc.fail())
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
2009-04-25 10:38:26 +00:00
|
|
|
std::cerr << "Cannot open file " << fname << std::endl;
|
2009-04-14 20:44:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-06-15 16:48:34 +00:00
|
|
|
//printf("%s: Pass %d\n", fname, pass);
|
2009-04-14 20:44:03 +00:00
|
|
|
code_line = 0;
|
2009-04-24 14:36:31 +00:00
|
|
|
m_cur_pass = pass;
|
2009-04-14 20:44:03 +00:00
|
|
|
|
|
|
|
#define LINEBUF_SIZE 1024
|
2009-04-25 10:38:26 +00:00
|
|
|
char line[LINEBUF_SIZE] = {0};
|
|
|
|
while (!failed && !fsrc.fail() && !fsrc.eof())
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
int opcode_size = 0;
|
2009-04-25 10:38:26 +00:00
|
|
|
fsrc.getline(line, LINEBUF_SIZE);
|
|
|
|
if(fsrc.fail())
|
2009-04-14 20:44:03 +00:00
|
|
|
break;
|
2009-04-25 10:38:26 +00:00
|
|
|
|
|
|
|
cur_line = line;
|
|
|
|
//printf("A: %s\n", line);
|
2009-04-14 20:44:03 +00:00
|
|
|
code_line++;
|
|
|
|
|
2009-04-24 14:36:31 +00:00
|
|
|
param_t params[10] = {{0}};
|
|
|
|
param_t params_ext[10] = {{0}};
|
2009-05-01 12:15:48 +00:00
|
|
|
|
|
|
|
bool upper = true;
|
2009-04-14 20:44:03 +00:00
|
|
|
for (int i = 0; i < LINEBUF_SIZE; i++)
|
|
|
|
{
|
2009-04-25 10:38:26 +00:00
|
|
|
char c = line[i];
|
2009-04-14 20:44:03 +00:00
|
|
|
// This stuff handles /**/ and // comments.
|
2009-04-25 02:13:33 +00:00
|
|
|
// modified by Hermes : added // and /* */ for long commentaries
|
2009-04-14 20:44:03 +00:00
|
|
|
if (c == '/')
|
|
|
|
{
|
|
|
|
if (i < 1023)
|
|
|
|
{
|
2009-04-25 10:38:26 +00:00
|
|
|
if (line[i+1] == '/')
|
2009-04-14 20:44:03 +00:00
|
|
|
c = 0x00;
|
2009-04-25 10:38:26 +00:00
|
|
|
else if (line[i+1] == '*')
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
// toggle comment mode.
|
|
|
|
disable_text = !disable_text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (c == '*')
|
|
|
|
{
|
2009-04-25 10:38:26 +00:00
|
|
|
if (i < 1023 && line[i+1] == '/' && disable_text)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
disable_text = 0;
|
|
|
|
c = 32;
|
2009-04-25 10:38:26 +00:00
|
|
|
line[i + 1] = 32;
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// turn text into spaces if disable_text is on (in a comment).
|
|
|
|
if (disable_text && ((unsigned char)c) > 32) c = 32;
|
|
|
|
|
|
|
|
if (c == 0x0a || c == 0x0d || c == ';')
|
|
|
|
c = 0x00;
|
|
|
|
if (c == 0x09) // tabs to spaces
|
|
|
|
c = ' ';
|
2009-05-01 12:15:48 +00:00
|
|
|
if (c == '"')
|
|
|
|
upper = !upper;
|
|
|
|
if (upper && c >= 'a' && c <= 'z') // convert to uppercase
|
2009-04-14 20:44:03 +00:00
|
|
|
c = c - 'a' + 'A';
|
2009-04-25 10:38:26 +00:00
|
|
|
line[i] = c;
|
2009-04-14 20:44:03 +00:00
|
|
|
if (c == 0)
|
|
|
|
break; // modified by Hermes
|
|
|
|
}
|
2009-04-25 10:38:26 +00:00
|
|
|
char *ptr = line;
|
2009-04-14 20:44:03 +00:00
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
std::string label;
|
|
|
|
|
|
|
|
size_t col_pos = std::string(line).find(":");
|
|
|
|
if (col_pos != std::string::npos)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
2009-04-25 10:38:26 +00:00
|
|
|
bool valid = true;
|
|
|
|
|
|
|
|
for(int j = 0; j < (int)col_pos; j++)
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
if (j == 0)
|
|
|
|
if (!((ptr[j] >= 'A' && ptr[j] <= 'Z') || (ptr[j] == '_')))
|
|
|
|
valid = false;
|
|
|
|
if (!((ptr[j] >= '0' && ptr[j] <= '9') || (ptr[j] >= 'A' && ptr[j] <= 'Z') || (ptr[j] == '_')))
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
if (valid)
|
|
|
|
{
|
2009-04-25 10:38:26 +00:00
|
|
|
label = std::string(line).substr(0, col_pos);
|
|
|
|
ptr += col_pos + 1;
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
char *opcode = NULL;
|
2009-04-14 20:44:03 +00:00
|
|
|
opcode = strtok(ptr, " ");
|
|
|
|
char *opcode_ext = NULL;
|
|
|
|
|
|
|
|
u32 params_count = 0;
|
|
|
|
u32 params_count_ext = 0;
|
|
|
|
if (opcode)
|
|
|
|
{
|
|
|
|
if ((opcode_ext = strstr(opcode, "'")) != NULL)
|
|
|
|
{
|
|
|
|
opcode_ext[0] = '\0';
|
|
|
|
opcode_ext++;
|
|
|
|
if (strlen(opcode_ext) == 0)
|
|
|
|
opcode_ext = NULL;
|
|
|
|
}
|
|
|
|
// now we have opcode and label
|
|
|
|
|
|
|
|
params_count = 0;
|
|
|
|
params_count_ext = 0;
|
|
|
|
|
|
|
|
char *paramstr = strtok(NULL, "\0");
|
|
|
|
char *paramstr_ext = 0;
|
|
|
|
// there is valid opcode so probably we have parameters
|
|
|
|
|
|
|
|
if (paramstr)
|
|
|
|
{
|
|
|
|
if ((paramstr_ext = strstr(paramstr, ":")) != NULL)
|
|
|
|
{
|
|
|
|
paramstr_ext[0] = '\0';
|
|
|
|
paramstr_ext++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (paramstr)
|
2009-04-18 11:31:37 +00:00
|
|
|
params_count = GetParams(paramstr, params);
|
2009-04-14 20:44:03 +00:00
|
|
|
if (paramstr_ext)
|
2009-04-18 11:31:37 +00:00
|
|
|
params_count_ext = GetParams(paramstr_ext, params_ext);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
if (!label.empty())
|
2009-04-14 20:44:03 +00:00
|
|
|
{
|
|
|
|
// there is a valid label so lets store it in labels table
|
2009-04-24 14:36:31 +00:00
|
|
|
u32 lval = m_cur_addr;
|
2009-04-14 20:44:03 +00:00
|
|
|
if (opcode)
|
|
|
|
{
|
|
|
|
if (strcmp(opcode, "EQU") == 0)
|
|
|
|
{
|
|
|
|
lval = params[0].val;
|
|
|
|
opcode = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pass == 1)
|
2009-04-18 11:31:37 +00:00
|
|
|
labels.RegisterLabel(label, lval);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opcode == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// check if opcode is reserved compiler word
|
|
|
|
if (strcmp("INCLUDE", opcode) == 0)
|
|
|
|
{
|
|
|
|
if (params[0].type == P_STR)
|
|
|
|
{
|
|
|
|
char *tmpstr;
|
2009-04-25 10:38:26 +00:00
|
|
|
u32 thisCodeline = code_line;
|
|
|
|
|
2009-04-14 20:44:03 +00:00
|
|
|
if (include_dir.size())
|
|
|
|
{
|
|
|
|
tmpstr = (char *)malloc(include_dir.size() + strlen(params[0].str) + 2);
|
|
|
|
sprintf(tmpstr, "%s/%s", include_dir.c_str(), params[0].str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmpstr = (char *)malloc(strlen(params[0].str) + 1);
|
|
|
|
strcpy(tmpstr, params[0].str);
|
|
|
|
}
|
2009-04-25 10:38:26 +00:00
|
|
|
|
2009-04-14 22:30:31 +00:00
|
|
|
AssembleFile(tmpstr, pass);
|
2009-04-25 10:38:26 +00:00
|
|
|
|
|
|
|
code_line = thisCodeline;
|
2009-04-25 02:13:33 +00:00
|
|
|
|
2009-04-14 20:44:03 +00:00
|
|
|
free(tmpstr);
|
|
|
|
}
|
|
|
|
else
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_EXPECTED_PARAM_STR);
|
2009-04-14 20:44:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp("INCDIR", opcode) == 0)
|
|
|
|
{
|
|
|
|
if (params[0].type == P_STR)
|
|
|
|
include_dir = params[0].str;
|
|
|
|
else
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_EXPECTED_PARAM_STR);
|
2009-04-14 20:44:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp("ORG", opcode) == 0)
|
|
|
|
{
|
|
|
|
if (params[0].type == P_VAL)
|
2009-04-24 14:36:31 +00:00
|
|
|
m_cur_addr = params[0].val;
|
2009-04-14 20:44:03 +00:00
|
|
|
else
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_EXPECTED_PARAM_VAL);
|
2009-04-14 20:44:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp("SEGMENT", opcode) == 0)
|
|
|
|
{
|
|
|
|
if (params[0].type == P_STR)
|
|
|
|
{
|
2009-04-24 14:36:31 +00:00
|
|
|
segment_addr[cur_segment] = m_cur_addr;
|
2009-04-14 20:44:03 +00:00
|
|
|
if (strcmp("DATA", params[0].str) == 0)
|
|
|
|
cur_segment = SEGMENT_DATA;
|
|
|
|
if (strcmp("CODE", params[0].str) == 0)
|
|
|
|
cur_segment = SEGMENT_CODE;
|
2009-04-24 14:36:31 +00:00
|
|
|
m_cur_addr = segment_addr[cur_segment];
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
else
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_EXPECTED_PARAM_STR);
|
2009-04-14 20:44:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-04-18 11:31:37 +00:00
|
|
|
const opc_t *opc = FindOpcode(opcode, params_count, opcodes, opcodes_size);
|
2009-04-14 20:44:03 +00:00
|
|
|
if (!opc)
|
|
|
|
opc = &cw;
|
|
|
|
|
|
|
|
opcode_size = opc->size & ~P_EXT;
|
|
|
|
|
2009-04-18 11:31:37 +00:00
|
|
|
VerifyParams(opc, params, params_count);
|
2009-04-14 20:44:03 +00:00
|
|
|
|
|
|
|
const opc_t *opc_ext = NULL;
|
|
|
|
// Check for opcode extensions.
|
|
|
|
if (opc->size & P_EXT)
|
|
|
|
{
|
|
|
|
if (opcode_ext)
|
|
|
|
{
|
2009-04-18 11:31:37 +00:00
|
|
|
opc_ext = FindOpcode(opcode_ext, params_count_ext, opcodes_ext, opcodes_ext_size);
|
|
|
|
VerifyParams(opc_ext, params_ext, params_count_ext, true);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
else if (params_count_ext)
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_EXT_PAR_NOT_EXT);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (opcode_ext)
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_EXT_CANT_EXTEND_OPCODE);
|
2009-04-14 20:44:03 +00:00
|
|
|
if (params_count_ext)
|
2009-04-18 11:31:37 +00:00
|
|
|
ShowError(ERR_EXT_PAR_NOT_EXT);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pass == 2)
|
|
|
|
{
|
|
|
|
// generate binary
|
2009-04-24 14:36:31 +00:00
|
|
|
((u16 *)gdg_buffer)[m_cur_addr] = 0x0000;
|
2009-04-18 11:31:37 +00:00
|
|
|
BuildCode(opc, params, params_count, (u16 *)gdg_buffer);
|
2009-04-14 20:44:03 +00:00
|
|
|
if (opc_ext)
|
2009-04-18 11:31:37 +00:00
|
|
|
BuildCode(opc_ext, params_ext, params_count_ext, (u16 *)gdg_buffer);
|
2009-04-14 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
2009-04-24 14:36:31 +00:00
|
|
|
m_cur_addr += opcode_size;
|
2009-04-25 10:38:26 +00:00
|
|
|
m_totalSize += opcode_size;
|
2009-04-14 20:44:03 +00:00
|
|
|
};
|
2009-04-25 02:13:33 +00:00
|
|
|
|
2009-04-25 10:38:26 +00:00
|
|
|
if (!failed)
|
|
|
|
fsrc.close();
|
|
|
|
|
2009-04-14 20:44:03 +00:00
|
|
|
return !failed;
|
|
|
|
}
|