qasm/psuedo.cpp

569 lines
9.7 KiB
C++
Raw Normal View History

#include "psuedo.h"
2019-11-15 13:09:41 +00:00
#include "eval.h"
#define CLASS TPsuedoOp
CLASS::CLASS()
{
}
CLASS::~CLASS()
{
}
2019-11-16 17:27:24 +00:00
int CLASS::doDO(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
{
UNUSED(opinfo);
TEvaluator eval(a);
int64_t eval_result = 0;
uint8_t shift;
2019-11-16 18:48:01 +00:00
uint32_t result32;
2019-11-16 17:27:24 +00:00
int res = 0;
int err = 0;
std::string op = Poco::toUpper(line.opcode);
2019-11-16 19:14:51 +00:00
std::string oper = line.operand_expr;
result32 = 0xFFFFFFFF;
2019-11-16 17:27:24 +00:00
if (op == "IF")
{
if (oper == "")
{
err = errIllegalCharOperand;
}
goto out;
}
if (op == "DO")
{
2019-11-16 18:48:01 +00:00
a.DOstack.push(a.curDO);
2019-11-16 17:27:24 +00:00
if (oper == "")
{
err = errIllegalCharOperand;
2019-11-16 19:14:51 +00:00
a.curDO.doskip = false;
2019-11-16 17:27:24 +00:00
goto out;
}
shift = 0;
eval_result = 0;
2019-11-16 18:48:01 +00:00
int x = eval.evaluate(line.operand_expr, eval_result, shift);
2019-11-16 17:27:24 +00:00
if (x < 0)
{
2019-11-16 18:48:01 +00:00
a.curDO.doskip = false;
2019-11-16 17:27:24 +00:00
err = errBadLabel;
if (a.pass == 0)
{
err = errForwardRef;
}
2019-11-16 18:48:01 +00:00
goto out;
2019-11-16 17:27:24 +00:00
}
2019-11-16 18:48:01 +00:00
result32 = eval_result & 0xFFFFFFFF;
2019-11-16 19:14:51 +00:00
a.curDO.doskip = (result32 != 0) ? false : true;
2019-11-16 18:48:01 +00:00
2019-11-16 17:27:24 +00:00
goto out;
}
if (op == "ELSE")
{
2019-11-16 18:48:01 +00:00
if (a.DOstack.size() > 0)
{
//line.flags |= FLAG_NOLINEPRINT;
a.curDO.doskip = !a.curDO.doskip;
}
else
{
err = errUnexpectedOp;
}
2019-11-16 17:27:24 +00:00
goto out;
}
if (op == "FIN")
{
//line.flags |= FLAG_NOLINEPRINT;
if (a.DOstack.size() > 0)
{
a.curDO = a.DOstack.top();
a.DOstack.pop();
}
else
{
// kind of a silent error here, just make sure we reinitialize
2019-11-16 18:48:01 +00:00
err = errUnexpectedOp;
2019-11-16 19:14:51 +00:00
a.curDO.doskip = false;
2019-11-16 17:27:24 +00:00
}
goto out;
}
out:
2019-11-16 18:48:01 +00:00
//printf("DO eval: %08X %s\n", result32, a.curDO.doskip ? "true" : "false");
2019-11-16 17:27:24 +00:00
if (err > 0)
{
line.setError(err);
}
return (res);
}
2019-11-16 02:30:12 +00:00
int CLASS::doLUP(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
{
UNUSED(opinfo);
2019-11-16 06:27:43 +00:00
TEvaluator eval(a);
int64_t eval_result = 0;
uint8_t shift;
2019-11-16 02:30:12 +00:00
int lidx, len;
int res = 0;
int err = 0;
std::string op = Poco::toUpper(line.opcode);
if (op == "LUP")
{
2019-11-16 17:27:24 +00:00
line.flags |= FLAG_NOLINEPRINT;
2019-11-16 02:30:12 +00:00
len = line.lineno - 1; // MerlinLine line numbers are +1 from actual array idx
if (len >= 0)
{
2019-11-16 06:27:43 +00:00
shift = 0;
eval_result = 0;
2019-11-16 18:48:01 +00:00
int x = eval.evaluate(line.operand_expr, eval_result, shift);
2019-11-16 06:27:43 +00:00
2019-11-16 02:30:12 +00:00
a.LUPstack.push(a.curLUP);
a.curLUP.lupoffset = len;
2019-11-16 17:27:24 +00:00
a.curLUP.lupct = eval_result & 0xFFFF; // evaluate here
2019-11-16 02:30:12 +00:00
a.curLUP.luprunning++;
2019-11-16 17:27:24 +00:00
if ((x < 0) || (eval_result <= 0) || (eval_result > 0x8000))
{
// merlin just ignores LUP if the value is out of range
a.curLUP.lupct = 0;
a.curLUP.lupskip = true;
}
2019-11-16 02:30:12 +00:00
}
else
{
err = errUnexpectedOp;
}
}
if (op == "--^")
{
2019-11-16 17:27:24 +00:00
line.flags |= FLAG_NOLINEPRINT;
2019-11-16 02:30:12 +00:00
if (a.curLUP.luprunning > 0)
{
lidx = line.lineno - 1;
len = lidx - a.curLUP.lupoffset - 1;
if (a.curLUP.lupct > 0)
{
a.curLUP.lupct--;
if (a.curLUP.lupct != 0)
{
a.lineno = a.curLUP.lupoffset;
goto out;
}
}
2019-11-16 06:27:43 +00:00
// kind of a silent error here, just make sure we reinitialize
2019-11-16 17:27:24 +00:00
a.curLUP.luprunning = 0;
a.curLUP.lupct = 0;
a.curLUP.lupskip = false;
2019-11-16 02:30:12 +00:00
//printf("start=%d end=%d len=%d\n", a.curLUP.lupoffset, lidx, len);
if (a.LUPstack.size() > 0)
{
a.curLUP = a.LUPstack.top();
a.LUPstack.pop();
}
else
{
err = errUnexpectedOp;
}
}
else
{
2019-11-16 17:27:24 +00:00
a.curLUP.lupskip = false;
2019-11-16 06:27:43 +00:00
// SGQ - found a '--^' without a LUP, should we just ignore?
//err = errUnexpectedOp;
2019-11-16 02:30:12 +00:00
}
}
out:
if (err > 0)
{
line.setError(err);
}
return (res);
}
2019-11-15 07:35:04 +00:00
constexpr unsigned int strhash(const char *str, int h = 0)
{
return !str[h] ? 5381 : (strhash(str, h + 1) * 33) ^ str[h];
}
int CLASS::doDATA(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
{
2019-11-15 11:40:35 +00:00
UNUSED(opinfo);
2019-11-15 13:09:41 +00:00
TEvaluator eval(a);
int i;
2019-11-15 07:35:04 +00:00
int outct = 0;
int wordsize = 2;
int endian = 0;
2019-11-16 18:48:01 +00:00
std::string oper = line.operand_expr;
2019-11-15 07:35:04 +00:00
std::string op = Poco::toUpper(Poco::trim(line.opcode));
Poco::StringTokenizer tok(oper, ",", Poco::StringTokenizer::TOK_TRIM |
Poco::StringTokenizer::TOK_IGNORE_EMPTY);
const char *ptr = (const char *)op.c_str();
switch (strhash(ptr) )
{
case strhash((const char *)"DA"):
case strhash((const char *)"DW"):
wordsize = 2;
break;
case strhash((const char *)"DDB"):
wordsize = 2;
endian = 1;
break;
case strhash((const char *)"DFB"):
case strhash((const char *)"DB"):
wordsize = 1;
break;
case strhash((const char *)"ADR"):
wordsize = 3;
break;
case strhash((const char *)"ADRL"):
wordsize = 4;
break;
2019-11-15 13:09:41 +00:00
default:
2019-11-16 00:15:58 +00:00
wordsize = 0;
2019-11-15 13:09:41 +00:00
break;
2019-11-15 07:35:04 +00:00
}
for (auto itr = tok.begin(); itr != tok.end(); ++itr)
{
//printf("%s\n",(*itr).c_str());
//evaluate each of these strings, check for errors on pass 2
2019-11-15 13:09:41 +00:00
std::string expr = *itr;
int64_t eval_result = 0;
uint8_t shift;
int r;
uint8_t b;
2019-11-16 00:15:58 +00:00
shift = 0;
2019-11-15 13:09:41 +00:00
r = eval.evaluate(expr, eval_result, shift);
if (r < 0)
{
//printf("eval error %d |%s|\n", r,expr.c_str());
if (a.pass > 0)
{
line.setError(errBadEvaluation);
}
}
2019-11-16 00:15:58 +00:00
if (shift == '>')
2019-11-15 13:09:41 +00:00
{
2019-11-16 00:15:58 +00:00
eval_result = (eval_result) & 0xFF;
2019-11-15 13:09:41 +00:00
}
2019-11-16 00:15:58 +00:00
if (shift == '<')
2019-11-15 13:09:41 +00:00
{
2019-11-16 00:15:58 +00:00
eval_result = (eval_result >> 8) & 0xFF;
2019-11-15 13:09:41 +00:00
}
2019-11-16 00:15:58 +00:00
else if ((shift == '^') || (shift == '|'))
2019-11-15 13:09:41 +00:00
{
2019-11-16 00:15:58 +00:00
eval_result = (eval_result >> 16) & 0xFF;
2019-11-15 13:09:41 +00:00
}
2019-11-15 07:35:04 +00:00
outct += wordsize;
if (a.pass > 0)
{
if (!endian) // little endian
{
2019-11-15 13:09:41 +00:00
for (i = 0; i < wordsize; i++)
{
2019-11-16 00:15:58 +00:00
b = (eval_result >> (8 * i)) & 0xFF;
2019-11-15 13:09:41 +00:00
line.outbytes.push_back(b);
//printf("%02X\n",b);
}
2019-11-15 07:35:04 +00:00
}
else
{
// big endian
2019-11-15 13:09:41 +00:00
for (i = 0; i < wordsize; i++)
{
2019-11-16 00:15:58 +00:00
b = (eval_result >> ((wordsize - 1 - i) * 8)) & 0xFF;
2019-11-15 13:09:41 +00:00
line.outbytes.push_back(b);
//printf("%02X\n",b);
}
2019-11-15 07:35:04 +00:00
}
}
}
2019-11-16 00:15:58 +00:00
line.outbytect = outct;
2019-11-15 07:35:04 +00:00
return (outct);
}
int CLASS::doDS(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
{
2019-11-15 11:40:35 +00:00
UNUSED(opinfo);
int res = 0;
int32_t v = line.expr_value;
2019-11-15 01:04:35 +00:00
if (line.eval_result != 0)
{
line.setError(errForwardRef);
}
else if ((v < 0) || ((a.PC.currentpc + v) >= 0x10000)) // no neg, or crossing bank bound
{
line.setError(errOverflow);
}
else
{
res = v;
2019-11-13 23:45:39 +00:00
2019-11-15 07:35:04 +00:00
line.datafillbyte = line.eval_result & 0xFF;
line.datafillct = v;
2019-11-13 23:45:39 +00:00
}
return (res);
}
2019-11-13 04:32:10 +00:00
int CLASS::doDUM(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
{
2019-11-15 11:40:35 +00:00
UNUSED(opinfo);
int res = 0;
bool isdend = ((opinfo.opcode == P_DEND) ? true : false);
if (!isdend)
{
a.dumstart = 1;
a.dumstartaddr = line.expr_value;
}
else
{
a.dumstart = -1;
if (a.PCstack.size() == 0)
{
line.setError(errBadDUMop);
a.dumstart = 0;
}
}
return (res);
2019-11-13 04:32:10 +00:00
}
int CLASS::doLST(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
{
2019-11-15 11:40:35 +00:00
UNUSED(opinfo);
std::string s;
if (a.pass > 0)
{
2019-11-16 18:48:01 +00:00
s = Poco::toUpper(Poco::trim(line.operand_expr));
2019-11-17 06:48:24 +00:00
if (s=="")
{
a.listing=true;
a.skiplist=true;
}
else if (s == "RTN")
2019-11-16 19:14:51 +00:00
{
if (a.LSTstack.size())
{
a.listing = a.LSTstack.top();
a.LSTstack.pop();
}
}
else if ((s == "ON") || (line.expr_value > 0))
{
//printf("ON\n");
a.skiplist = true;
a.listing = true;
}
else if ((s == "OFF") || (line.expr_value == 0))
{
//printf("OFF\n");
a.skiplist = true;
a.listing = false;
}
}
return (0);
}
2019-11-16 19:14:51 +00:00
int CLASS::doTR(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
{
UNUSED(opinfo);
std::string s;
if (a.pass > 0)
{
s = Poco::toUpper(Poco::trim(line.operand_expr));
if (s == "ADR")
{
a.truncdata |= 0x03;
}
else if ((s == "ON") || (line.expr_value > 0))
{
a.truncdata |= 0x01;;
}
else if ((s == "OFF") || (line.expr_value == 0))
{
a.truncdata = 0x00;
}
}
return (0);
}
2019-11-15 01:04:35 +00:00
int CLASS::doHEX(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
{
2019-11-15 11:40:35 +00:00
UNUSED(opinfo);
2019-11-15 13:09:41 +00:00
2019-11-16 18:48:01 +00:00
std::string os = Poco::toUpper(Poco::trim(line.operand_expr));
2019-11-15 01:04:35 +00:00
2019-11-15 07:35:04 +00:00
uint32_t bytect = 0;
uint8_t b = 0;
uint8_t ct = 0;
2019-11-16 17:27:24 +00:00
if (os.length() == 0)
{
// case where HEX has no operand, Merlin does not flag as error
//line.setError(errIllegalCharOperand);
bytect = 0;
goto out;
}
2019-11-15 01:04:35 +00:00
for ( uint32_t i = 0; i < os.length(); ++i )
{
char c = os[i];
2019-11-15 07:35:04 +00:00
if ((c >= '0') && (c <= '9'))
2019-11-15 05:21:03 +00:00
{
2019-11-15 07:35:04 +00:00
c = c - '0';
2019-11-15 05:21:03 +00:00
}
2019-11-15 07:35:04 +00:00
else if ((c >= 'a') && (c <= 'f'))
2019-11-15 05:21:03 +00:00
{
2019-11-15 07:35:04 +00:00
c = c - 'a' + 10;
2019-11-15 05:21:03 +00:00
}
2019-11-15 07:35:04 +00:00
else if ((c >= 'A') && (c <= 'F'))
2019-11-15 05:21:03 +00:00
{
2019-11-15 07:35:04 +00:00
c = c - 'A' + 10;
2019-11-15 05:21:03 +00:00
}
2019-11-15 07:35:04 +00:00
else if (c == ',')
2019-11-15 01:04:35 +00:00
{
continue;
}
2019-11-15 05:21:03 +00:00
else
2019-11-15 01:04:35 +00:00
{
2019-11-16 17:27:24 +00:00
line.setError(errIllegalCharOperand);
bytect = 0;
goto out;
2019-11-15 01:04:35 +00:00
}
// Got a good char, append to hex string and see if we've got a byte
2019-11-15 07:35:04 +00:00
switch (ct)
2019-11-15 01:04:35 +00:00
{
2019-11-15 05:21:03 +00:00
case 0:
2019-11-15 07:35:04 +00:00
b = (c << 4);
2019-11-15 05:21:03 +00:00
break;
case 1:
2019-11-15 07:35:04 +00:00
b |= c;
2019-11-15 05:21:03 +00:00
break;
2019-11-15 01:04:35 +00:00
}
2019-11-15 07:35:04 +00:00
ct = (ct + 1) & 0x01;
2019-11-15 05:21:03 +00:00
if (!ct)
2019-11-15 01:04:35 +00:00
{
2019-11-15 07:35:04 +00:00
if (a.pass > 0)
2019-11-15 01:04:35 +00:00
{
2019-11-15 05:21:03 +00:00
line.outbytes.push_back(b);
2019-11-15 01:04:35 +00:00
}
2019-11-15 07:35:04 +00:00
b = 0;
2019-11-15 05:21:03 +00:00
bytect++;
2019-11-15 01:04:35 +00:00
}
2019-11-16 00:15:58 +00:00
}
if (ct & 0x01) // we got an odd number of nibbles
{
2019-11-16 17:27:24 +00:00
line.setError(errBadOperand);
2019-11-16 00:15:58 +00:00
bytect = 0;
2019-11-15 01:04:35 +00:00
}
2019-11-16 17:27:24 +00:00
out:
2019-11-15 07:35:04 +00:00
line.outbytect = bytect;
2019-11-15 05:21:03 +00:00
return bytect;
2019-11-15 01:04:35 +00:00
}
int CLASS::ProcessOpcode(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
{
int res = 0;
switch (opinfo.opcode)
{
default:
res = -1; // undefined p-op
line.setError(errUnimplemented);
break;
case P_DS:
res = doDS(a, line, opinfo);
break;
2019-11-13 23:45:39 +00:00
case P_PUT:
case P_USE:
// both of these are handled by the input file processor, just allow them to be
// processed with no errors here
break;
2019-11-13 04:32:10 +00:00
case P_DUM:
case P_DEND:
res = doDUM(a, line, opinfo);
2019-11-16 06:27:43 +00:00
line.flags |= FLAG_FORCEADDRPRINT;
2019-11-16 02:51:30 +00:00
break;
case P_ORG:
2019-11-16 18:48:01 +00:00
if (line.operand_expr.length() > 0)
{
2019-11-15 01:04:35 +00:00
a.PC.orgsave = a.PC.currentpc;
a.PC.currentpc = line.expr_value;
2019-11-15 01:04:35 +00:00
line.startpc = line.expr_value;
}
else
{
a.PC.currentpc = a.PC.orgsave;
2019-11-15 01:04:35 +00:00
line.startpc = a.PC.orgsave;
}
2019-11-16 06:27:43 +00:00
line.flags |= FLAG_FORCEADDRPRINT;
break;
case P_SAV:
2019-11-15 01:04:35 +00:00
a.savepath = a.processFilename(line.operand, Poco::Path::current(), 0);
break;
case P_LST:
res = doLST(a, line, opinfo);
break;
2019-11-15 01:04:35 +00:00
case P_HEX:
res = doHEX(a, line, opinfo);
break;
2019-11-15 07:35:04 +00:00
case P_DATA:
res = doDATA(a, line, opinfo);
break;
2019-11-16 02:30:12 +00:00
case P_LUP:
res = doLUP(a, line, opinfo);
break;
2019-11-16 17:27:24 +00:00
case P_DO:
res = doDO(a, line, opinfo);
break;
2019-11-16 19:14:51 +00:00
case P_TR:
res=doTR(a,line,opinfo);
break;
2019-11-16 17:27:24 +00:00
}
return (res);
}