2013-02-19 16:37:14 +00:00
|
|
|
// xasm 3.1.0 by Piotr Fusik <fox@scene.pl>
|
2007-04-22 19:16:22 +00:00
|
|
|
// http://xasm.atari.org
|
2017-02-08 18:32:42 +00:00
|
|
|
// Can be compiled with DMD v2.073.
|
2009-10-17 19:47:06 +00:00
|
|
|
|
|
|
|
// Poetic License:
|
|
|
|
//
|
|
|
|
// This work 'as-is' we provide.
|
|
|
|
// No warranty express or implied.
|
|
|
|
// We've done our best,
|
|
|
|
// to debug and test.
|
|
|
|
// Liability for damages denied.
|
|
|
|
//
|
|
|
|
// Permission is granted hereby,
|
|
|
|
// to copy, share, and modify.
|
|
|
|
// Use as is fit,
|
|
|
|
// free or for profit.
|
|
|
|
// These rights, on this notice, rely.
|
2005-05-22 12:28:58 +00:00
|
|
|
|
2014-02-21 22:29:58 +00:00
|
|
|
import std.algorithm;
|
2013-02-19 16:37:14 +00:00
|
|
|
import std.array;
|
|
|
|
import std.conv;
|
2007-04-22 19:16:22 +00:00
|
|
|
import std.math;
|
2013-02-19 16:37:14 +00:00
|
|
|
import std.path;
|
|
|
|
import std.stdio;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
|
|
|
version (Windows) {
|
2017-02-08 18:32:42 +00:00
|
|
|
import core.sys.windows.windows;
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 14:42:54 +00:00
|
|
|
int readByte(File *file) {
|
|
|
|
char c;
|
|
|
|
if (file.readf("%c", &c) != 1)
|
|
|
|
return -1;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
const string TITLE = "xasm 3.1.0";
|
2005-05-22 12:28:58 +00:00
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
string sourceFilename = null;
|
2010-12-28 12:44:00 +00:00
|
|
|
bool[26] options;
|
2013-02-19 16:37:14 +00:00
|
|
|
string[26] optionParameters;
|
|
|
|
string[] commandLineDefinitions = null;
|
|
|
|
string makeTarget;
|
2014-02-21 22:29:58 +00:00
|
|
|
string[] makeSources = null;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
int exitCode = 0;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
|
|
|
int totalLines;
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
bool pass2 = false;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
bool optionFill; // opt f
|
|
|
|
bool option5200; // opt g
|
|
|
|
bool optionHeaders; // opt h
|
|
|
|
bool optionListing; // opt l
|
|
|
|
bool optionObject; // opt o
|
2014-04-30 07:19:49 +00:00
|
|
|
bool optionUnusedLabels; // opt u
|
2005-05-22 12:28:58 +00:00
|
|
|
|
2014-02-21 22:06:03 +00:00
|
|
|
string currentFilename;
|
|
|
|
int lineNo;
|
|
|
|
int includeLevel = 0;
|
2013-02-19 16:37:14 +00:00
|
|
|
string line;
|
2005-05-22 12:28:58 +00:00
|
|
|
int column;
|
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
bool foundEnd;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
|
|
|
class AssemblyError : Exception {
|
2010-12-28 12:44:00 +00:00
|
|
|
this(string msg) {
|
2005-05-22 12:28:58 +00:00
|
|
|
super(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Label {
|
|
|
|
int value;
|
2010-12-28 12:44:00 +00:00
|
|
|
bool unused = true;
|
|
|
|
bool unknownInPass1 = false;
|
|
|
|
bool passed = false;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
|
|
|
this(int value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
Label[string] labelTable;
|
2005-05-22 12:28:58 +00:00
|
|
|
Label currentLabel;
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
alias int function(int a, int b) OperatorFunction;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
bool inOpcode = false;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
|
|
|
struct ValOp {
|
|
|
|
int value;
|
|
|
|
OperatorFunction func;
|
|
|
|
int priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValOp[] valOpStack;
|
|
|
|
|
|
|
|
int value;
|
2010-12-28 12:44:00 +00:00
|
|
|
bool unknownInPass1;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
|
|
|
enum AddrMode {
|
|
|
|
ACCUMULATOR = 0,
|
|
|
|
IMMEDIATE = 1,
|
|
|
|
ABSOLUTE = 2,
|
|
|
|
ZEROPAGE = 3,
|
|
|
|
ABSOLUTE_X = 4,
|
|
|
|
ZEROPAGE_X = 5,
|
|
|
|
ABSOLUTE_Y = 6,
|
|
|
|
ZEROPAGE_Y = 7,
|
|
|
|
INDIRECT_X = 8,
|
|
|
|
INDIRECT_Y = 9,
|
|
|
|
INDIRECT = 10,
|
|
|
|
ABS_OR_ZP = 11, // temporarily used in readAddrMode
|
|
|
|
STANDARD_MASK = 15,
|
|
|
|
INCREMENT = 0x20,
|
|
|
|
DECREMENT = 0x30,
|
|
|
|
ZERO = 0x40
|
|
|
|
}
|
|
|
|
|
|
|
|
AddrMode addrMode;
|
|
|
|
|
2014-12-31 13:30:09 +00:00
|
|
|
enum OrgModifier {
|
|
|
|
NONE,
|
|
|
|
FORCE_HEADER,
|
|
|
|
FORCE_FFFF,
|
|
|
|
RELOCATE
|
|
|
|
}
|
|
|
|
|
2005-05-22 12:28:58 +00:00
|
|
|
int origin = -1;
|
|
|
|
int loadOrigin;
|
|
|
|
int loadingOrigin;
|
|
|
|
ushort[] blockEnds;
|
|
|
|
int blockIndex;
|
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
bool repeating; // line
|
2005-05-22 12:28:58 +00:00
|
|
|
int repeatCounter; // line
|
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
bool instructionBegin;
|
|
|
|
bool pairing;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
bool willSkip;
|
|
|
|
bool skipping;
|
2005-05-22 12:28:58 +00:00
|
|
|
ushort[] skipOffsets;
|
|
|
|
int skipOffsetsIndex = 0;
|
|
|
|
|
|
|
|
int repeatOffset; // instruction repeat
|
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
bool wereManyInstructions;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
alias void function(int move) MoveFunction;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
|
|
|
int value1;
|
|
|
|
AddrMode addrMode1;
|
|
|
|
int value2;
|
|
|
|
AddrMode addrMode2;
|
|
|
|
|
|
|
|
struct IfContext {
|
2010-12-28 12:44:00 +00:00
|
|
|
bool condition;
|
|
|
|
bool wasElse;
|
|
|
|
bool aConditionMatched;
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IfContext[] ifContexts;
|
|
|
|
|
2013-03-19 10:12:12 +00:00
|
|
|
File listingStream;
|
2005-05-22 12:28:58 +00:00
|
|
|
char[32] listingLine;
|
|
|
|
int listingColumn;
|
2013-02-19 16:37:14 +00:00
|
|
|
string lastListedFilename = null;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
2013-03-19 10:12:12 +00:00
|
|
|
File objectStream;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
|
|
|
int objectBytes = 0;
|
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
bool getOption(char letter) {
|
2005-05-22 12:28:58 +00:00
|
|
|
assert(letter >= 'a' && letter <= 'z');
|
|
|
|
return options[letter - 'a'];
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
void warning(string msg, bool error = false) {
|
2010-12-28 12:44:00 +00:00
|
|
|
stdout.flush();
|
2009-10-17 19:47:06 +00:00
|
|
|
version (Windows) {
|
2013-02-19 16:37:14 +00:00
|
|
|
HANDLE stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
|
2009-10-17 19:47:06 +00:00
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
2013-02-19 16:37:14 +00:00
|
|
|
GetConsoleScreenBufferInfo(stderrHandle, &csbi);
|
|
|
|
SetConsoleTextAttribute(stderrHandle, (csbi.wAttributes & ~0xf) | (error ? 12 : 14));
|
|
|
|
scope (exit) SetConsoleTextAttribute(stderrHandle, csbi.wAttributes);
|
2009-10-17 19:47:06 +00:00
|
|
|
}
|
2013-02-19 16:37:14 +00:00
|
|
|
if (line !is null)
|
2010-12-28 12:44:00 +00:00
|
|
|
stderr.writeln(line);
|
2014-02-21 22:06:03 +00:00
|
|
|
stderr.writefln("%s (%d) %s: %s", currentFilename, lineNo, error ? "ERROR" : "WARNING", msg);
|
2005-05-22 12:28:58 +00:00
|
|
|
exitCode = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void illegalCharacter() {
|
|
|
|
throw new AssemblyError("Illegal character");
|
|
|
|
}
|
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
bool eol() {
|
2005-05-22 12:28:58 +00:00
|
|
|
return column >= line.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
char readChar() {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (eol())
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Unexpected end of line");
|
|
|
|
return line[column++];
|
|
|
|
}
|
|
|
|
|
|
|
|
int readDigit(int base) {
|
|
|
|
if (eol()) return -1;
|
|
|
|
int r = line[column];
|
|
|
|
if (r >= '0' && r <= '9') {
|
|
|
|
r -= '0';
|
|
|
|
} else {
|
|
|
|
r &= 0xdf;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (r >= 'A' && r <= 'Z')
|
2005-05-22 12:28:58 +00:00
|
|
|
r -= 'A' - 10;
|
2013-02-19 16:37:14 +00:00
|
|
|
else
|
2005-05-22 12:28:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (r < base) {
|
|
|
|
column++;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int readNumber(int base) {
|
|
|
|
long r = readDigit(base);
|
2013-02-19 16:37:14 +00:00
|
|
|
if (r < 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
illegalCharacter();
|
|
|
|
do {
|
|
|
|
int d = readDigit(base);
|
2013-02-19 16:37:14 +00:00
|
|
|
if (d < 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
return cast(int) r;
|
|
|
|
r = r * base + d;
|
|
|
|
} while (r <= 0x7fffffff);
|
|
|
|
throw new AssemblyError("Number too big");
|
|
|
|
}
|
|
|
|
|
|
|
|
void readSpaces() {
|
|
|
|
switch (readChar()) {
|
|
|
|
case '\t':
|
|
|
|
case ' ':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new AssemblyError("Space expected");
|
|
|
|
}
|
|
|
|
while (!eol()) {
|
|
|
|
switch (line[column]) {
|
|
|
|
case '\t':
|
|
|
|
case ' ':
|
|
|
|
column++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
string readLabel() {
|
|
|
|
string label;
|
2005-05-22 12:28:58 +00:00
|
|
|
while (!eol()) {
|
|
|
|
char c = line[column++];
|
|
|
|
if (c >= '0' && c <= '9' || c == '_') {
|
|
|
|
label ~= c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
c &= 0xdf;
|
|
|
|
if (c >= 'A' && c <= 'Z') {
|
|
|
|
label ~= c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
column--;
|
|
|
|
break;
|
|
|
|
}
|
2007-04-22 19:16:22 +00:00
|
|
|
return label >= "A" ? label : null;
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void readComma() {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (readChar() != ',')
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Bad or missing function parameter");
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
string readInstruction() {
|
|
|
|
string r;
|
2005-05-22 12:28:58 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
char c = readChar() & 0xdf;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (c < 'A' || c > 'Z')
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Illegal instruction");
|
|
|
|
r ~= c;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
string readFunction() {
|
2010-12-28 12:44:00 +00:00
|
|
|
if (column + 5 >= line.length) return null;
|
|
|
|
if (line[column + 3] != '(') return null;
|
2013-02-19 16:37:14 +00:00
|
|
|
string r;
|
2005-05-22 12:28:58 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
char c = line[column + i] & 0xdf;
|
2010-12-28 12:44:00 +00:00
|
|
|
if (c < 'A' || c > 'Z') return null;
|
2005-05-22 12:28:58 +00:00
|
|
|
r ~= c;
|
|
|
|
}
|
|
|
|
column += 4;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
string readFilename() {
|
2005-05-22 12:28:58 +00:00
|
|
|
readSpaces();
|
|
|
|
char delimiter = readChar();
|
|
|
|
switch (delimiter) {
|
|
|
|
case '"':
|
|
|
|
case '\'':
|
2013-02-19 16:37:14 +00:00
|
|
|
string filename;
|
2005-05-22 12:28:58 +00:00
|
|
|
char c;
|
2013-02-19 16:37:14 +00:00
|
|
|
while ((c = readChar()) != delimiter)
|
2005-05-22 12:28:58 +00:00
|
|
|
filename ~= c;
|
|
|
|
return filename;
|
|
|
|
default:
|
|
|
|
illegalCharacter();
|
|
|
|
}
|
2010-12-28 12:44:00 +00:00
|
|
|
assert(0);
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void readStringChar(char c) {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (readChar() != c)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("String error");
|
|
|
|
}
|
|
|
|
|
|
|
|
ubyte[] readString() {
|
|
|
|
if (eol()) return null;
|
|
|
|
char delimiter = readChar();
|
|
|
|
switch (delimiter) {
|
|
|
|
case '"':
|
|
|
|
case '\'':
|
2013-02-19 16:37:14 +00:00
|
|
|
ubyte[] r;
|
2005-05-22 12:28:58 +00:00
|
|
|
for (;;) {
|
|
|
|
char c = readChar();
|
|
|
|
if (c == delimiter) {
|
|
|
|
if (eol()) return r;
|
|
|
|
if (line[column] != delimiter) {
|
|
|
|
if (line[column] == '*') {
|
|
|
|
column++;
|
2013-02-19 16:37:14 +00:00
|
|
|
foreach (ref ubyte b; r)
|
2005-05-22 12:28:58 +00:00
|
|
|
b ^= 0x80;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
column++;
|
|
|
|
}
|
|
|
|
r ~= cast(ubyte) c;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
column--;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkNoExtraCharacters() {
|
|
|
|
if (eol()) return;
|
|
|
|
switch (line[column]) {
|
|
|
|
case '\t':
|
|
|
|
case ' ':
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
throw new AssemblyError("Extra characters on line");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkOriginDefined() {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (origin < 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("No ORG specified");
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorPlus(int a, int b) {
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorMinus(int a, int b) {
|
|
|
|
return -b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorLow(int a, int b) {
|
|
|
|
return b & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorHigh(int a, int b) {
|
|
|
|
return (b >> 8) & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorLogicalNot(int a, int b) {
|
|
|
|
return !b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorBitwiseNot(int a, int b) {
|
|
|
|
return ~b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorAdd(int a, int b) {
|
|
|
|
long r = cast(long) a + b;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (r < -0x80000000L || r > 0x7fffffffL)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Arithmetic overflow");
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorSubtract(int a, int b) {
|
|
|
|
long r = cast(long) a - b;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (r < -0x80000000L || r > 0x7fffffffL)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Arithmetic overflow");
|
|
|
|
return a - b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorMultiply(int a, int b) {
|
|
|
|
long r = cast(long) a * b;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (r < -0x80000000L || r > 0x7fffffffL)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Arithmetic overflow");
|
|
|
|
return a * b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorDivide(int a, int b) {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (b == 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Divide by zero");
|
|
|
|
return a / b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorModulus(int a, int b) {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (b == 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Divide by zero");
|
|
|
|
return a % b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorAnd(int a, int b) {
|
|
|
|
return a & b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorOr(int a, int b) {
|
|
|
|
return a | b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorXor(int a, int b) {
|
|
|
|
return a ^ b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorEqual(int a, int b) {
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorNotEqual(int a, int b) {
|
|
|
|
return a != b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorLess(int a, int b) {
|
|
|
|
return a < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorGreater(int a, int b) {
|
|
|
|
return a > b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorLessEqual(int a, int b) {
|
|
|
|
return a <= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorGreaterEqual(int a, int b) {
|
|
|
|
return a >= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorShiftLeft(int a, int b) {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (b < 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
return operatorShiftRight(a, -b);
|
2013-02-19 16:37:14 +00:00
|
|
|
if (a != 0 && b >= 32)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Arithmetic overflow");
|
|
|
|
long r = cast(long) a << b;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (r & 0xffffffff00000000L)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Arithmetic overflow");
|
|
|
|
return a << b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorShiftRight(int a, int b) {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (b < 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
return operatorShiftLeft(a, -b);
|
2013-02-19 16:37:14 +00:00
|
|
|
if (b >= 32)
|
2005-05-22 12:28:58 +00:00
|
|
|
b = 31;
|
|
|
|
return a >> b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorLogicalAnd(int a, int b) {
|
|
|
|
return a && b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operatorLogicalOr(int a, int b) {
|
|
|
|
return a || b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pushValOp(int value, OperatorFunction func, int priority) {
|
|
|
|
ValOp valOp;
|
|
|
|
valOp.value = value;
|
|
|
|
valOp.func = func;
|
|
|
|
valOp.priority = priority;
|
|
|
|
valOpStack ~= valOp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void readValue() {
|
|
|
|
assert(valOpStack.length == 0);
|
|
|
|
unknownInPass1 = false;
|
|
|
|
int priority = 0;
|
|
|
|
do {
|
|
|
|
int operand;
|
|
|
|
char c = readChar();
|
|
|
|
switch (c) {
|
|
|
|
case '[':
|
|
|
|
priority += 10;
|
|
|
|
continue;
|
|
|
|
case '+':
|
|
|
|
pushValOp(0, &operatorPlus, priority + 8);
|
|
|
|
continue;
|
|
|
|
case '-':
|
|
|
|
pushValOp(0, &operatorMinus, priority + 8);
|
|
|
|
continue;
|
|
|
|
case '<':
|
|
|
|
pushValOp(0, &operatorLow, priority + 8);
|
|
|
|
continue;
|
|
|
|
case '>':
|
|
|
|
pushValOp(0, &operatorHigh, priority + 8);
|
|
|
|
continue;
|
|
|
|
case '!':
|
|
|
|
pushValOp(0, &operatorLogicalNot, priority + 4);
|
|
|
|
continue;
|
|
|
|
case '~':
|
|
|
|
pushValOp(0, &operatorBitwiseNot, priority + 8);
|
|
|
|
continue;
|
|
|
|
case '(':
|
|
|
|
throw new AssemblyError("Use square brackets instead");
|
|
|
|
case '*':
|
|
|
|
checkOriginDefined();
|
|
|
|
operand = origin;
|
|
|
|
break;
|
|
|
|
case '#':
|
2013-02-19 16:37:14 +00:00
|
|
|
if (!repeating)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("'#' is allowed only in repeated lines");
|
|
|
|
operand = repeatCounter;
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
case '"':
|
|
|
|
operand = readChar();
|
2013-02-19 16:37:14 +00:00
|
|
|
if (operand == c)
|
2005-05-22 12:28:58 +00:00
|
|
|
readStringChar(c);
|
|
|
|
readStringChar(c);
|
|
|
|
if (!eol() && line[column] == '*') {
|
|
|
|
column++;
|
|
|
|
operand ^= 0x80;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
switch (readChar()) {
|
|
|
|
case '0':
|
|
|
|
operand = option5200 ? 0xc000 : 0xd000;
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
operand = option5200 ? 0xc010 : 0xd010;
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
operand = option5200 ? 0xe800 : 0xd200;
|
|
|
|
break;
|
|
|
|
case '3':
|
2013-02-19 16:37:14 +00:00
|
|
|
if (option5200)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("There's no PIA chip in Atari 5200");
|
|
|
|
operand = 0xd300;
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
operand = 0xd400;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalCharacter();
|
|
|
|
}
|
|
|
|
int d = readDigit(16);
|
2013-02-19 16:37:14 +00:00
|
|
|
if (d < 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
illegalCharacter();
|
|
|
|
operand += d;
|
|
|
|
break;
|
|
|
|
case '{':
|
2013-02-19 16:37:14 +00:00
|
|
|
if (inOpcode)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Nested opcodes not supported");
|
|
|
|
ValOp[] savedValOpStack = valOpStack;
|
|
|
|
AddrMode savedAddrMode = addrMode;
|
2010-12-28 12:44:00 +00:00
|
|
|
bool savedUnknownInPass1 = unknownInPass1;
|
|
|
|
bool savedInstructionBegin = instructionBegin;
|
2005-05-22 12:28:58 +00:00
|
|
|
valOpStack.length = 0;
|
|
|
|
inOpcode = true;
|
|
|
|
assemblyInstruction(readInstruction());
|
2013-02-19 16:37:14 +00:00
|
|
|
if (readChar() != '}')
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Missing '}'");
|
|
|
|
assert(!instructionBegin);
|
|
|
|
inOpcode = false;
|
|
|
|
valOpStack = savedValOpStack;
|
|
|
|
addrMode = savedAddrMode;
|
|
|
|
unknownInPass1 = savedUnknownInPass1;
|
|
|
|
instructionBegin = savedInstructionBegin;
|
|
|
|
operand = value;
|
|
|
|
break;
|
|
|
|
case '$':
|
|
|
|
operand = readNumber(16);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
operand = readNumber(2);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
column--;
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
operand = readNumber(10);
|
|
|
|
break;
|
|
|
|
}
|
2013-02-19 16:37:14 +00:00
|
|
|
string label = readLabel();
|
|
|
|
if (label is null)
|
2005-05-22 12:28:58 +00:00
|
|
|
illegalCharacter();
|
|
|
|
if (label in labelTable) {
|
|
|
|
Label l = labelTable[label];
|
|
|
|
operand = l.value;
|
|
|
|
l.unused = false;
|
|
|
|
if (pass2) {
|
|
|
|
if (l.passed) {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (l.unknownInPass1)
|
2005-05-22 12:28:58 +00:00
|
|
|
unknownInPass1 = true;
|
|
|
|
} else {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (l.unknownInPass1)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Illegal forward reference");
|
|
|
|
unknownInPass1 = true;
|
|
|
|
}
|
|
|
|
} else {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (l.unknownInPass1)
|
2005-05-22 12:28:58 +00:00
|
|
|
unknownInPass1 = true;
|
|
|
|
}
|
|
|
|
} else {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (pass2)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Undeclared label: " ~ label);
|
|
|
|
unknownInPass1 = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (!eol() && line[column] == ']') {
|
|
|
|
column++;
|
|
|
|
priority -= 10;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (priority < 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Unmatched bracket");
|
|
|
|
}
|
|
|
|
if (eol()) {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (priority != 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Unmatched bracket");
|
|
|
|
pushValOp(operand, &operatorPlus, 1);
|
|
|
|
} else {
|
|
|
|
switch (line[column++]) {
|
|
|
|
case '+':
|
|
|
|
pushValOp(operand, &operatorAdd, priority + 6);
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
pushValOp(operand, &operatorSubtract, priority + 6);
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
pushValOp(operand, &operatorMultiply, priority + 7);
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
pushValOp(operand, &operatorDivide, priority + 7);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
pushValOp(operand, &operatorModulus, priority + 7);
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
switch (readChar()) {
|
|
|
|
case '<':
|
|
|
|
pushValOp(operand, &operatorShiftLeft, priority + 7);
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
pushValOp(operand, &operatorLessEqual, priority + 5);
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
pushValOp(operand, &operatorNotEqual, priority + 5);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
column--;
|
|
|
|
pushValOp(operand, &operatorLess, priority + 5);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
switch (readChar()) {
|
|
|
|
default:
|
|
|
|
column--;
|
|
|
|
goto case '=';
|
|
|
|
case '=':
|
|
|
|
pushValOp(operand, &operatorEqual, priority + 5);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
switch (readChar()) {
|
|
|
|
case '>':
|
|
|
|
pushValOp(operand, &operatorShiftRight, priority + 7);
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
pushValOp(operand, &operatorGreaterEqual, priority + 5);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
column--;
|
|
|
|
pushValOp(operand, &operatorGreater, priority + 5);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
switch (readChar()) {
|
|
|
|
case '=':
|
|
|
|
pushValOp(operand, &operatorNotEqual, priority + 5);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalCharacter();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
switch (readChar()) {
|
|
|
|
case '&':
|
|
|
|
pushValOp(operand, &operatorLogicalAnd, priority + 3);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
column--;
|
|
|
|
pushValOp(operand, &operatorAnd, priority + 7);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
switch (readChar()) {
|
|
|
|
case '|':
|
|
|
|
pushValOp(operand, &operatorLogicalOr, priority + 2);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
column--;
|
|
|
|
pushValOp(operand, &operatorOr, priority + 6);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
pushValOp(operand, &operatorXor, priority + 6);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
column--;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (priority != 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Unmatched bracket");
|
|
|
|
pushValOp(operand, &operatorPlus, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (;;) {
|
2013-03-19 10:13:26 +00:00
|
|
|
immutable sp = valOpStack.length - 1;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (sp <= 0 || valOpStack[sp].priority > valOpStack[sp - 1].priority)
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
int operand1 = valOpStack[sp - 1].value;
|
|
|
|
OperatorFunction func1 = valOpStack[sp - 1].func;
|
|
|
|
valOpStack[sp - 1] = valOpStack[sp];
|
|
|
|
valOpStack.length = sp;
|
|
|
|
if (pass2 || !unknownInPass1) { // skip operations if unknown operands
|
|
|
|
valOpStack[sp - 1].value = func1(operand1, valOpStack[sp - 1].value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (valOpStack.length != 1 || valOpStack[0].priority != 1);
|
|
|
|
value = valOpStack[0].value;
|
|
|
|
valOpStack.length = 0;
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
debug int testValue(string l) {
|
|
|
|
line = l;
|
2005-05-22 12:28:58 +00:00
|
|
|
column = 0;
|
|
|
|
readValue();
|
2013-02-19 16:37:14 +00:00
|
|
|
writefln("Value of %s is %x", l, value);
|
2005-05-22 12:28:58 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest {
|
|
|
|
assert(testValue("123") == 123);
|
|
|
|
assert(testValue("$1234abCd") == 0x1234abcd);
|
|
|
|
assert(testValue("%101") == 5);
|
|
|
|
assert(testValue("^07") == 0xd007);
|
|
|
|
assert(testValue("^1f") == 0xd01f);
|
|
|
|
assert(testValue("^23") == 0xd203);
|
|
|
|
assert(testValue("^31") == 0xd301);
|
|
|
|
assert(testValue("^49") == 0xd409);
|
|
|
|
assert(testValue("!0") == 1);
|
|
|
|
assert(testValue("<$1234") == 0x34);
|
|
|
|
assert(testValue(">$1234567") == 0x45);
|
|
|
|
assert(testValue("1+2") == 3);
|
|
|
|
assert(testValue("1+2*3") == 7);
|
|
|
|
assert(testValue("[1+2]*3") == 9);
|
|
|
|
assert(testValue("{nop}") == 0xea);
|
|
|
|
assert(testValue("{CLC}+{sec}") == 0x50);
|
|
|
|
assert(testValue("{Jsr}") == 0x20);
|
|
|
|
assert(testValue("{bit a:}") == 0x2c);
|
|
|
|
assert(testValue("{bIt $7d}") == 0x24);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mustBeKnownInPass1() {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (unknownInPass1)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Label not defined before");
|
|
|
|
}
|
|
|
|
|
|
|
|
void readWord() {
|
|
|
|
readValue();
|
2013-02-19 16:37:14 +00:00
|
|
|
if ((!unknownInPass1 || pass2) && (value < -0xffff || value > 0xffff))
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Value out of range");
|
|
|
|
}
|
|
|
|
|
|
|
|
void readUnsignedWord() {
|
|
|
|
readWord();
|
2013-02-19 16:37:14 +00:00
|
|
|
if ((!unknownInPass1 || pass2) && value < 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Value out of range");
|
|
|
|
}
|
|
|
|
|
|
|
|
void readKnownPositive() {
|
|
|
|
readValue();
|
|
|
|
mustBeKnownInPass1();
|
2013-02-19 16:37:14 +00:00
|
|
|
if (value <= 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Value out of range");
|
|
|
|
}
|
|
|
|
|
|
|
|
void optionalIncDec() {
|
|
|
|
if (eol()) return;
|
|
|
|
switch (line[column]) {
|
|
|
|
case '+':
|
|
|
|
column++;
|
|
|
|
addrMode += AddrMode.INCREMENT;
|
|
|
|
return;
|
|
|
|
case '-':
|
|
|
|
column++;
|
|
|
|
addrMode += AddrMode.DECREMENT;
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void readAddrMode() {
|
|
|
|
readSpaces();
|
|
|
|
char c = readChar();
|
|
|
|
switch (c) {
|
|
|
|
case '@':
|
|
|
|
addrMode = AddrMode.ACCUMULATOR;
|
|
|
|
return;
|
|
|
|
case '#':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
addrMode = AddrMode.IMMEDIATE;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (inOpcode && line[column] == '}')
|
2005-05-22 12:28:58 +00:00
|
|
|
return;
|
|
|
|
readWord();
|
2013-02-19 16:37:14 +00:00
|
|
|
final switch (c) {
|
2005-05-22 12:28:58 +00:00
|
|
|
case '#':
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
value &= 0xff;
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
value = (value >> 8) & 0xff;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case '(':
|
|
|
|
if (inOpcode) {
|
|
|
|
switch (readChar()) {
|
|
|
|
case ',':
|
|
|
|
switch (readChar()) {
|
|
|
|
case 'X':
|
|
|
|
case 'x':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalCharacter();
|
|
|
|
}
|
2013-02-19 16:37:14 +00:00
|
|
|
if (readChar() != ')')
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Need parenthesis");
|
|
|
|
addrMode = AddrMode.INDIRECT_X;
|
|
|
|
return;
|
|
|
|
case ')':
|
|
|
|
if (readChar() == ',') {
|
|
|
|
switch (readChar()) {
|
|
|
|
case 'Y':
|
|
|
|
case 'y':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalCharacter();
|
|
|
|
}
|
|
|
|
addrMode = AddrMode.INDIRECT_Y;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
column--;
|
|
|
|
addrMode = AddrMode.INDIRECT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
column--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
readUnsignedWord();
|
|
|
|
switch (readChar()) {
|
|
|
|
case ',':
|
|
|
|
switch (readChar()) {
|
|
|
|
case 'X':
|
|
|
|
case 'x':
|
|
|
|
addrMode = AddrMode.INDIRECT_X;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
addrMode = cast(AddrMode) (AddrMode.INDIRECT_X + AddrMode.ZERO);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalCharacter();
|
|
|
|
}
|
2013-02-19 16:37:14 +00:00
|
|
|
if (readChar() != ')')
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Need parenthesis");
|
|
|
|
return;
|
|
|
|
case ')':
|
|
|
|
if (eol()) {
|
|
|
|
addrMode = AddrMode.INDIRECT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (line[column] == ',') {
|
|
|
|
column++;
|
|
|
|
switch (readChar()) {
|
|
|
|
case 'Y':
|
|
|
|
case 'y':
|
|
|
|
addrMode = AddrMode.INDIRECT_Y;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
addrMode = cast(AddrMode) (AddrMode.INDIRECT_Y + AddrMode.ZERO);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalCharacter();
|
|
|
|
}
|
|
|
|
optionalIncDec();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
addrMode = AddrMode.INDIRECT;
|
2007-04-22 19:16:22 +00:00
|
|
|
return;
|
|
|
|
default:
|
2005-05-22 12:28:58 +00:00
|
|
|
illegalCharacter();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
case 'a':
|
|
|
|
if (!eol() && line[column] == ':') {
|
|
|
|
column++;
|
|
|
|
addrMode = AddrMode.ABSOLUTE;
|
|
|
|
} else {
|
|
|
|
addrMode = AddrMode.ABS_OR_ZP;
|
|
|
|
column--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
case 'z':
|
|
|
|
if (!eol() && line[column] == ':') {
|
|
|
|
column++;
|
|
|
|
addrMode = AddrMode.ZEROPAGE;
|
|
|
|
} else {
|
|
|
|
addrMode = AddrMode.ABS_OR_ZP;
|
|
|
|
column--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
addrMode = AddrMode.ABS_OR_ZP;
|
|
|
|
column--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// absolute or zeropage addressing, optionally indexed
|
|
|
|
if (inOpcode && (addrMode == AddrMode.ABSOLUTE || addrMode == AddrMode.ZEROPAGE)) {
|
|
|
|
switch (readChar()) {
|
|
|
|
case '}':
|
|
|
|
column--;
|
|
|
|
return;
|
|
|
|
case ',':
|
|
|
|
switch (readChar()) {
|
|
|
|
case 'X':
|
|
|
|
case 'x':
|
|
|
|
addrMode += cast(AddrMode) (AddrMode.ABSOLUTE_X - AddrMode.ABSOLUTE);
|
|
|
|
return;
|
|
|
|
case 'Y':
|
|
|
|
case 'y':
|
|
|
|
addrMode += cast(AddrMode) (AddrMode.ABSOLUTE_Y - AddrMode.ABSOLUTE);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
illegalCharacter();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
column--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
readUnsignedWord();
|
|
|
|
if (addrMode == AddrMode.ABS_OR_ZP) {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (unknownInPass1 || value > 0xff)
|
2005-05-22 12:28:58 +00:00
|
|
|
addrMode = AddrMode.ABSOLUTE;
|
2013-02-19 16:37:14 +00:00
|
|
|
else
|
2005-05-22 12:28:58 +00:00
|
|
|
addrMode = AddrMode.ZEROPAGE;
|
|
|
|
}
|
|
|
|
if (eol()) return;
|
|
|
|
if (line[column] == ',') {
|
|
|
|
column++;
|
|
|
|
switch (readChar()) {
|
|
|
|
case 'X':
|
|
|
|
case 'x':
|
|
|
|
addrMode += cast(AddrMode) (AddrMode.ABSOLUTE_X - AddrMode.ABSOLUTE);
|
|
|
|
optionalIncDec();
|
|
|
|
return;
|
|
|
|
case 'Y':
|
|
|
|
case 'y':
|
|
|
|
addrMode += cast(AddrMode) (AddrMode.ABSOLUTE_Y - AddrMode.ABSOLUTE);
|
|
|
|
optionalIncDec();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
illegalCharacter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void readAbsoluteAddrMode() {
|
|
|
|
if (inOpcode && readChar() == '}') {
|
|
|
|
column--;
|
|
|
|
} else {
|
|
|
|
readAddrMode();
|
|
|
|
switch (addrMode) {
|
|
|
|
case AddrMode.ABSOLUTE:
|
|
|
|
case AddrMode.ZEROPAGE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addrMode = AddrMode.ABSOLUTE;
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
debug AddrMode testAddrMode(string l) {
|
|
|
|
line = l;
|
2005-05-22 12:28:58 +00:00
|
|
|
column = 0;
|
|
|
|
readAddrMode();
|
2013-02-19 16:37:14 +00:00
|
|
|
writefln("Addressing mode of \"%s\" is %x", l, addrMode);
|
2005-05-22 12:28:58 +00:00
|
|
|
return addrMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest {
|
|
|
|
assert(testAddrMode(" @") == AddrMode.ACCUMULATOR);
|
|
|
|
assert(testAddrMode(" #0") == AddrMode.IMMEDIATE);
|
|
|
|
assert(testAddrMode(" $abc,x-") == cast(AddrMode) (AddrMode.ABSOLUTE_X + AddrMode.DECREMENT));
|
|
|
|
assert(testAddrMode(" $ab,Y+") == cast(AddrMode) (AddrMode.ZEROPAGE_Y + AddrMode.INCREMENT));
|
|
|
|
assert(testAddrMode(" (0,x)") == AddrMode.INDIRECT_X);
|
|
|
|
assert(testAddrMode(" ($ff),Y+") == cast(AddrMode) (AddrMode.INDIRECT_Y + AddrMode.INCREMENT));
|
|
|
|
assert(testAddrMode(" ($abcd)") == AddrMode.INDIRECT);
|
|
|
|
inOpcode = true;
|
|
|
|
assert(testAddrMode(" a:}") == AddrMode.ABSOLUTE);
|
|
|
|
assert(testAddrMode(" z:}") == AddrMode.ZEROPAGE);
|
|
|
|
assert(testAddrMode(" a:,x}") == AddrMode.ABSOLUTE_X);
|
|
|
|
assert(testAddrMode(" z:,y}") == AddrMode.ZEROPAGE_Y);
|
|
|
|
assert(testAddrMode(" (,X)}") == AddrMode.INDIRECT_X);
|
|
|
|
assert(testAddrMode(" (),y}") == AddrMode.INDIRECT_Y);
|
|
|
|
assert(testAddrMode(" ()}") == AddrMode.INDIRECT);
|
|
|
|
inOpcode = false;
|
|
|
|
}
|
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
bool inFalseCondition() {
|
2005-05-22 12:28:58 +00:00
|
|
|
foreach (IfContext ic; ifContexts) {
|
|
|
|
if (!ic.condition) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
string makeEscape(string s) {
|
|
|
|
return s.replace("$", "$$");
|
2009-10-17 19:47:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-19 10:12:12 +00:00
|
|
|
File openInputFile(string filename) {
|
2014-02-21 22:29:58 +00:00
|
|
|
if (find(makeSources, filename).empty)
|
|
|
|
makeSources ~= filename;
|
2005-05-22 12:28:58 +00:00
|
|
|
try {
|
2013-03-19 10:12:12 +00:00
|
|
|
return File(filename);
|
|
|
|
} catch (Exception e) {
|
2013-02-19 16:37:14 +00:00
|
|
|
throw new AssemblyError(e.msg);
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-19 10:12:12 +00:00
|
|
|
File openOutputFile(char letter, string defaultExt) {
|
2013-02-19 16:37:14 +00:00
|
|
|
string filename = optionParameters[letter - 'a'];
|
|
|
|
if (filename is null)
|
|
|
|
filename = sourceFilename.setExtension(defaultExt);
|
|
|
|
if (letter == 'o')
|
2009-10-17 19:47:06 +00:00
|
|
|
makeTarget = makeEscape(filename);
|
2005-05-22 12:28:58 +00:00
|
|
|
try {
|
2013-03-19 10:12:12 +00:00
|
|
|
return File(filename, "wb");
|
|
|
|
} catch (Exception e) {
|
2013-02-19 16:37:14 +00:00
|
|
|
throw new AssemblyError(e.msg);
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
void ensureListingFileOpen(char letter, string msg) {
|
2013-03-19 10:12:12 +00:00
|
|
|
if (!listingStream.isOpen) {
|
2013-02-19 16:37:14 +00:00
|
|
|
listingStream = openOutputFile(letter, "lst");
|
|
|
|
if (!getOption('q'))
|
2010-12-28 12:44:00 +00:00
|
|
|
write(msg);
|
2013-03-19 10:12:12 +00:00
|
|
|
listingStream.writeln(TITLE);
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void listNibble(int x) {
|
2010-12-28 12:44:00 +00:00
|
|
|
listingLine[listingColumn++] = cast(char) (x <= 9 ? x + '0' : x + ('A' - 10));
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void listByte(ubyte x) {
|
|
|
|
listNibble(x >> 4);
|
|
|
|
listNibble(x & 0xf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void listWord(ushort x) {
|
|
|
|
listByte(cast(ubyte) (x >> 8));
|
|
|
|
listByte(cast(ubyte) x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void listLine() {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (!optionListing || !getOption('l') || line is null)
|
2005-05-22 12:28:58 +00:00
|
|
|
return;
|
|
|
|
assert(pass2);
|
2013-02-19 16:37:14 +00:00
|
|
|
if (inFalseCondition() && !getOption('c'))
|
2005-05-22 12:28:58 +00:00
|
|
|
return;
|
2014-02-21 22:06:03 +00:00
|
|
|
if (getOption('i') && includeLevel > 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
return;
|
|
|
|
ensureListingFileOpen('l', "Writing listing file...\n");
|
2014-02-21 22:06:03 +00:00
|
|
|
if (currentFilename != lastListedFilename) {
|
|
|
|
listingStream.writeln("Source: ", currentFilename);
|
|
|
|
lastListedFilename = currentFilename;
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
int i = 4;
|
2014-02-21 22:06:03 +00:00
|
|
|
int x = lineNo;
|
2005-05-22 12:28:58 +00:00
|
|
|
while (x > 0 && i >= 0) {
|
2013-02-19 16:37:14 +00:00
|
|
|
listingLine[i--] = '0' + x % 10;
|
2005-05-22 12:28:58 +00:00
|
|
|
x /= 10;
|
|
|
|
}
|
2013-02-19 16:37:14 +00:00
|
|
|
while (i >= 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
listingLine[i--] = ' ';
|
|
|
|
listingLine[5] = ' ';
|
2013-02-19 16:37:14 +00:00
|
|
|
while (listingColumn < 32)
|
2005-05-22 12:28:58 +00:00
|
|
|
listingLine[listingColumn++] = ' ';
|
2010-12-28 12:44:00 +00:00
|
|
|
listingStream.writefln("%.32s%s", listingLine, line);
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void listCommentLine() {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (currentLabel is null)
|
2005-05-22 12:28:58 +00:00
|
|
|
listingColumn = 6;
|
2013-02-19 16:37:14 +00:00
|
|
|
else {
|
2005-05-22 12:28:58 +00:00
|
|
|
assert(!inFalseCondition());
|
|
|
|
checkOriginDefined();
|
|
|
|
}
|
|
|
|
listLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
void listLabelTable() {
|
2013-03-19 10:12:12 +00:00
|
|
|
if (optionParameters['t' - 'a'] !is null && listingStream.isOpen)
|
2005-05-22 12:28:58 +00:00
|
|
|
listingStream.close();
|
|
|
|
ensureListingFileOpen('t', "Writing label table...\n");
|
2013-03-19 10:12:12 +00:00
|
|
|
listingStream.writeln("Label table:");
|
2017-02-08 18:32:42 +00:00
|
|
|
foreach (string name; sort(labelTable.keys)) {
|
2005-05-22 12:28:58 +00:00
|
|
|
Label l = labelTable[name];
|
|
|
|
listingStream.write(l.unused ? 'n' : ' ');
|
|
|
|
listingStream.write(l.unknownInPass1 ? '2' : ' ');
|
|
|
|
int value = l.value;
|
|
|
|
char sign = ' ';
|
|
|
|
if (value < 0) {
|
|
|
|
sign = '-';
|
|
|
|
value = -value;
|
|
|
|
}
|
2010-12-28 12:44:00 +00:00
|
|
|
listingStream.writefln(
|
|
|
|
(l.value & 0xffff0000) != 0 ? " %s%08X %s" : " %s%04X %s",
|
2013-02-19 16:37:14 +00:00
|
|
|
sign, value, name);
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug ubyte[] objectBuffer;
|
|
|
|
|
|
|
|
void objectByte(ubyte b) {
|
|
|
|
debug {
|
|
|
|
objectBuffer ~= b;
|
|
|
|
} else {
|
|
|
|
assert(pass2);
|
|
|
|
if (!optionObject) return;
|
2013-03-19 10:12:12 +00:00
|
|
|
if (!objectStream.isOpen) {
|
2013-02-19 16:37:14 +00:00
|
|
|
objectStream = openOutputFile('o', "obx");
|
|
|
|
if (!getOption('q'))
|
2010-12-28 12:44:00 +00:00
|
|
|
writeln("Writing object file...");
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
try {
|
2014-09-26 14:42:54 +00:00
|
|
|
objectStream.write(cast(char) b);
|
2013-03-19 10:12:12 +00:00
|
|
|
} catch (Exception e) {
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Error writing object file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
objectBytes++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void objectWord(ushort w) {
|
|
|
|
objectByte(cast(ubyte) w);
|
|
|
|
objectByte(cast(ubyte) (w >> 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
void putByte(ubyte b) {
|
|
|
|
if (inOpcode) {
|
|
|
|
if (instructionBegin) {
|
|
|
|
value = b;
|
|
|
|
instructionBegin = false;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (willSkip) {
|
|
|
|
assert(!pass2);
|
|
|
|
willSkip = false;
|
|
|
|
skipping = true;
|
|
|
|
}
|
|
|
|
if (skipping) {
|
|
|
|
assert(!pass2);
|
2013-02-19 16:37:14 +00:00
|
|
|
skipOffsets[$ - 1]++;
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
if (instructionBegin) {
|
|
|
|
repeatOffset = -2;
|
|
|
|
instructionBegin = false;
|
|
|
|
}
|
|
|
|
repeatOffset--;
|
|
|
|
if (optionFill && loadingOrigin >= 0 && loadingOrigin != loadOrigin) {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (loadingOrigin > loadOrigin)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Can't fill from higher to lower memory location");
|
|
|
|
if (pass2) {
|
|
|
|
while (loadingOrigin < loadOrigin) {
|
|
|
|
objectByte(0xff);
|
|
|
|
loadingOrigin++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug {
|
|
|
|
objectByte(b);
|
|
|
|
}
|
|
|
|
if (pass2) {
|
2007-04-22 19:16:22 +00:00
|
|
|
debug {
|
2017-07-29 12:06:15 +00:00
|
|
|
} else {
|
2005-05-22 12:28:58 +00:00
|
|
|
objectByte(b);
|
|
|
|
}
|
|
|
|
if (listingColumn < 29) {
|
|
|
|
listByte(b);
|
|
|
|
listingLine[listingColumn++] = ' ';
|
|
|
|
} else {
|
|
|
|
listingLine[29] = '+';
|
|
|
|
listingColumn = 30;
|
|
|
|
}
|
|
|
|
}
|
2007-04-22 19:16:22 +00:00
|
|
|
if (optionHeaders) {
|
|
|
|
if (origin < 0)
|
|
|
|
throw new AssemblyError("No ORG specified");
|
2005-05-22 12:28:58 +00:00
|
|
|
assert(blockIndex >= 0);
|
|
|
|
if (!pass2) {
|
|
|
|
blockEnds[blockIndex] = cast(ushort) loadOrigin;
|
|
|
|
}
|
2007-04-22 19:16:22 +00:00
|
|
|
}
|
|
|
|
if (origin >= 0) {
|
2005-05-22 12:28:58 +00:00
|
|
|
origin++;
|
|
|
|
loadingOrigin = ++loadOrigin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void putWord(ushort w) {
|
|
|
|
putByte(cast(ubyte) w);
|
|
|
|
putByte(cast(ubyte) (w >> 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
void putCommand(ubyte b) {
|
|
|
|
putByte(b);
|
|
|
|
if (inOpcode) return;
|
|
|
|
switch (addrMode & AddrMode.STANDARD_MASK) {
|
|
|
|
case AddrMode.ACCUMULATOR:
|
|
|
|
break;
|
|
|
|
case AddrMode.IMMEDIATE:
|
|
|
|
case AddrMode.ZEROPAGE:
|
|
|
|
case AddrMode.ZEROPAGE_X:
|
|
|
|
case AddrMode.ZEROPAGE_Y:
|
|
|
|
case AddrMode.INDIRECT_X:
|
|
|
|
case AddrMode.INDIRECT_Y:
|
2013-02-19 16:37:14 +00:00
|
|
|
if (pass2 && (value < -0xff || value > 0xff))
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Value out of range");
|
|
|
|
putByte(cast(ubyte) value);
|
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE:
|
|
|
|
case AddrMode.ABSOLUTE_X:
|
|
|
|
case AddrMode.ABSOLUTE_Y:
|
|
|
|
case AddrMode.INDIRECT:
|
2010-12-28 12:44:00 +00:00
|
|
|
putWord(cast(ushort) value);
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
2013-02-19 16:37:14 +00:00
|
|
|
default:
|
|
|
|
assert(0);
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
switch (addrMode) {
|
|
|
|
case cast(AddrMode) (AddrMode.ABSOLUTE_X + AddrMode.INCREMENT):
|
|
|
|
case cast(AddrMode) (AddrMode.ZEROPAGE_X + AddrMode.INCREMENT):
|
|
|
|
putByte(0xe8);
|
|
|
|
break;
|
|
|
|
case cast(AddrMode) (AddrMode.ABSOLUTE_X + AddrMode.DECREMENT):
|
|
|
|
case cast(AddrMode) (AddrMode.ZEROPAGE_X + AddrMode.DECREMENT):
|
|
|
|
putByte(0xca);
|
|
|
|
break;
|
|
|
|
case cast(AddrMode) (AddrMode.ABSOLUTE_Y + AddrMode.INCREMENT):
|
|
|
|
case cast(AddrMode) (AddrMode.ZEROPAGE_Y + AddrMode.INCREMENT):
|
|
|
|
case cast(AddrMode) (AddrMode.INDIRECT_Y + AddrMode.INCREMENT):
|
|
|
|
case cast(AddrMode) (AddrMode.INDIRECT_Y + AddrMode.INCREMENT + AddrMode.ZERO):
|
|
|
|
putByte(0xc8);
|
|
|
|
break;
|
|
|
|
case cast(AddrMode) (AddrMode.ABSOLUTE_Y + AddrMode.DECREMENT):
|
|
|
|
case cast(AddrMode) (AddrMode.ZEROPAGE_Y + AddrMode.DECREMENT):
|
|
|
|
case cast(AddrMode) (AddrMode.INDIRECT_Y + AddrMode.DECREMENT):
|
|
|
|
case cast(AddrMode) (AddrMode.INDIRECT_Y + AddrMode.DECREMENT + AddrMode.ZERO):
|
|
|
|
putByte(0x88);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void noOpcode() {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (inOpcode)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Can't get opcode of this");
|
|
|
|
}
|
|
|
|
|
|
|
|
void directive() {
|
|
|
|
noOpcode();
|
2013-02-19 16:37:14 +00:00
|
|
|
if (repeating)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Can't repeat this directive");
|
2013-02-19 16:37:14 +00:00
|
|
|
if (pairing)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Can't pair this directive");
|
|
|
|
}
|
|
|
|
|
|
|
|
void noRepeatSkipDirective() {
|
|
|
|
directive();
|
2013-02-19 16:37:14 +00:00
|
|
|
if (willSkip)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Can't skip over this");
|
|
|
|
repeatOffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void illegalAddrMode() {
|
|
|
|
throw new AssemblyError("Illegal addressing mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
void addrModeForMove(int move) {
|
2013-02-19 16:37:14 +00:00
|
|
|
final switch (move) {
|
2005-05-22 12:28:58 +00:00
|
|
|
case 0:
|
|
|
|
readAddrMode();
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
value = value1;
|
|
|
|
addrMode = addrMode1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
value = value2;
|
|
|
|
addrMode = addrMode2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyAccumulator(ubyte b, ubyte prefix, int move) {
|
|
|
|
addrModeForMove(move);
|
2013-02-19 16:37:14 +00:00
|
|
|
if (prefix != 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
putByte(prefix);
|
|
|
|
switch (addrMode & AddrMode.STANDARD_MASK) {
|
|
|
|
case AddrMode.ACCUMULATOR:
|
|
|
|
case AddrMode.INDIRECT:
|
|
|
|
illegalAddrMode();
|
2017-02-08 18:32:42 +00:00
|
|
|
break;
|
2005-05-22 12:28:58 +00:00
|
|
|
case AddrMode.IMMEDIATE:
|
|
|
|
if (b == 0x80) {
|
|
|
|
// STA #
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 9));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE:
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 0xd));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE:
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 5));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE_X:
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 0x1d));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE_X:
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 0x15));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE_Y:
|
|
|
|
addrMode -= 1;
|
|
|
|
goto case AddrMode.ABSOLUTE_Y;
|
|
|
|
case AddrMode.ABSOLUTE_Y:
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 0x19));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case AddrMode.INDIRECT_X:
|
2013-02-19 16:37:14 +00:00
|
|
|
if ((addrMode & AddrMode.ZERO) != 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
putWord(0x00a2);
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 1));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case AddrMode.INDIRECT_Y:
|
2013-02-19 16:37:14 +00:00
|
|
|
if ((addrMode & AddrMode.ZERO) != 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
putWord(0x00a0);
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 0x11));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
2013-02-19 16:37:14 +00:00
|
|
|
default:
|
|
|
|
assert(0);
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyShift(ubyte b) {
|
|
|
|
readAddrMode();
|
|
|
|
switch (addrMode & AddrMode.STANDARD_MASK) {
|
|
|
|
case AddrMode.ACCUMULATOR:
|
|
|
|
if (b == 0xc0 || b == 0xe0) {
|
|
|
|
// INC @, DEC @
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
2010-12-28 12:44:00 +00:00
|
|
|
putByte(cast(ubyte) (b + 0xa));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE:
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 0xe));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE:
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 6));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE_X:
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 0x1e));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE_X:
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 0x16));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyCompareIndex(ubyte b) {
|
|
|
|
readAddrMode();
|
|
|
|
switch (addrMode) {
|
|
|
|
case AddrMode.IMMEDIATE:
|
|
|
|
putCommand(b);
|
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE:
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 0xc));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE:
|
2010-12-28 12:44:00 +00:00
|
|
|
putCommand(cast(ubyte) (b + 4));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyLda(int move) {
|
|
|
|
assemblyAccumulator(0xa0, 0, move);
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyLdx(int move) {
|
|
|
|
addrModeForMove(move);
|
|
|
|
switch (addrMode & AddrMode.STANDARD_MASK) {
|
|
|
|
case AddrMode.IMMEDIATE:
|
|
|
|
putCommand(0xa2);
|
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE:
|
|
|
|
putCommand(0xae);
|
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE:
|
|
|
|
putCommand(0xa6);
|
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE_Y:
|
|
|
|
putCommand(0xbe);
|
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE_Y:
|
|
|
|
putCommand(0xb6);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyLdy(int move) {
|
|
|
|
addrModeForMove(move);
|
|
|
|
switch (addrMode & AddrMode.STANDARD_MASK) {
|
|
|
|
case AddrMode.IMMEDIATE:
|
|
|
|
putCommand(0xa0);
|
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE:
|
|
|
|
putCommand(0xac);
|
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE:
|
|
|
|
putCommand(0xa4);
|
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE_X:
|
|
|
|
putCommand(0xbc);
|
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE_X:
|
|
|
|
putCommand(0xb4);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblySta(int move) {
|
|
|
|
assemblyAccumulator(0x80, 0, move);
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyStx(int move) {
|
|
|
|
addrModeForMove(move);
|
|
|
|
switch (addrMode & AddrMode.STANDARD_MASK) {
|
|
|
|
case AddrMode.ABSOLUTE:
|
|
|
|
putCommand(0x8e);
|
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE:
|
|
|
|
putCommand(0x86);
|
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE_Y:
|
|
|
|
addrMode += 1;
|
|
|
|
goto case AddrMode.ZEROPAGE_Y;
|
|
|
|
case AddrMode.ZEROPAGE_Y:
|
|
|
|
putCommand(0x96);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblySty(int move) {
|
|
|
|
addrModeForMove(move);
|
|
|
|
switch (addrMode & AddrMode.STANDARD_MASK) {
|
|
|
|
case AddrMode.ABSOLUTE:
|
|
|
|
putCommand(0x8c);
|
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE:
|
|
|
|
putCommand(0x84);
|
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE_X:
|
|
|
|
addrMode += 1;
|
|
|
|
goto case AddrMode.ZEROPAGE_X;
|
|
|
|
case AddrMode.ZEROPAGE_X:
|
|
|
|
putCommand(0x94);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyBit() {
|
|
|
|
readAddrMode();
|
|
|
|
switch (addrMode) {
|
|
|
|
case AddrMode.ABSOLUTE:
|
|
|
|
putCommand(0x2c);
|
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE:
|
|
|
|
putCommand(0x24);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void putJump() {
|
|
|
|
switch (addrMode) {
|
|
|
|
case AddrMode.ZEROPAGE:
|
|
|
|
addrMode = AddrMode.ABSOLUTE;
|
|
|
|
goto case AddrMode.ABSOLUTE;
|
|
|
|
case AddrMode.ABSOLUTE:
|
|
|
|
putCommand(0x4c);
|
|
|
|
break;
|
|
|
|
case AddrMode.INDIRECT:
|
2013-02-19 16:37:14 +00:00
|
|
|
if (pass2 && (value & 0xff) == 0xff)
|
2005-05-22 12:28:58 +00:00
|
|
|
warning("Buggy indirect jump");
|
|
|
|
putCommand(0x6c);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyJmp() {
|
|
|
|
readAddrMode();
|
|
|
|
putJump();
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyConditionalJump(ubyte b) {
|
|
|
|
noOpcode();
|
|
|
|
readAddrMode();
|
|
|
|
if ((addrMode == AddrMode.ABSOLUTE || addrMode == AddrMode.ZEROPAGE)
|
|
|
|
&& pass2 && origin >= 0 && value - origin - 2 >= -0x80 && value - origin - 2 <= 0x7f) {
|
|
|
|
warning("Plain branch instruction would be sufficient");
|
|
|
|
}
|
|
|
|
putByte(b);
|
|
|
|
putByte(3);
|
|
|
|
putJump();
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyJsr() {
|
|
|
|
readAbsoluteAddrMode();
|
|
|
|
putCommand(0x20);
|
|
|
|
}
|
|
|
|
|
|
|
|
ubyte calculateBranch(int offset) {
|
|
|
|
if (offset < -0x80 || offset > 0x7f) {
|
2013-02-19 16:37:14 +00:00
|
|
|
int dist = offset < 0 ? -offset - 0x80 : offset - 0x7f;
|
2010-12-28 12:44:00 +00:00
|
|
|
throw new AssemblyError("Branch out of range by " ~ to!string(dist) ~ " bytes");
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
return cast(ubyte) offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyBranch(ubyte b) {
|
|
|
|
readAbsoluteAddrMode();
|
|
|
|
if (inOpcode) {
|
|
|
|
putByte(b);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
checkOriginDefined();
|
|
|
|
putByte(b);
|
|
|
|
putByte(pass2 ? calculateBranch(value - origin - 1) : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyRepeat(ubyte b) {
|
|
|
|
noOpcode();
|
|
|
|
int offset = repeatOffset;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (offset >= 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("No instruction to repeat");
|
2013-02-19 16:37:14 +00:00
|
|
|
if (pass2 && wereManyInstructions)
|
2005-05-22 12:28:58 +00:00
|
|
|
warning("Repeating only the last instruction");
|
|
|
|
putByte(b);
|
|
|
|
putByte(calculateBranch(offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblySkip(ubyte b) {
|
|
|
|
noOpcode();
|
|
|
|
if (willSkip) {
|
2013-02-19 16:37:14 +00:00
|
|
|
skipOffsets[$ - 1] = 2;
|
2005-05-22 12:28:58 +00:00
|
|
|
willSkip = false;
|
|
|
|
}
|
|
|
|
putByte(b);
|
2013-02-19 16:37:14 +00:00
|
|
|
if (pass2)
|
2005-05-22 12:28:58 +00:00
|
|
|
putByte(calculateBranch(skipOffsets[skipOffsetsIndex++]));
|
2013-02-19 16:37:14 +00:00
|
|
|
else {
|
2005-05-22 12:28:58 +00:00
|
|
|
putByte(0);
|
|
|
|
skipOffsets ~= 0;
|
|
|
|
willSkip = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyInw() {
|
|
|
|
noOpcode();
|
|
|
|
readAddrMode();
|
|
|
|
switch (addrMode) {
|
|
|
|
case AddrMode.ABSOLUTE:
|
|
|
|
putCommand(0xee);
|
|
|
|
putWord(0x03d0);
|
|
|
|
value++;
|
|
|
|
putCommand(0xee);
|
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE:
|
|
|
|
putCommand(0xe6);
|
|
|
|
putWord(0x02d0);
|
|
|
|
value++;
|
|
|
|
putCommand(0xe6);
|
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE_X:
|
|
|
|
putCommand(0xfe);
|
|
|
|
putWord(0x03d0);
|
|
|
|
value++;
|
|
|
|
putCommand(0xfe);
|
|
|
|
break;
|
|
|
|
case AddrMode.ZEROPAGE_X:
|
|
|
|
putCommand(0xf6);
|
|
|
|
putWord(0x02d0);
|
|
|
|
value++;
|
|
|
|
putCommand(0xf6);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyMove() {
|
|
|
|
noOpcode();
|
|
|
|
readAddrMode();
|
|
|
|
value1 = value;
|
|
|
|
addrMode1 = addrMode;
|
2010-12-28 12:44:00 +00:00
|
|
|
bool unknown1 = unknownInPass1;
|
2005-05-22 12:28:58 +00:00
|
|
|
readAddrMode();
|
|
|
|
value2 = value;
|
|
|
|
addrMode2 = addrMode;
|
|
|
|
unknownInPass1 = unknown1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyMoveByte(MoveFunction load, MoveFunction store) {
|
|
|
|
assemblyMove();
|
|
|
|
load(1);
|
|
|
|
store(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyMoveWord(MoveFunction load, MoveFunction store, ubyte inc, ubyte dec) {
|
|
|
|
assemblyMove();
|
|
|
|
switch (addrMode2) {
|
|
|
|
case AddrMode.ABSOLUTE:
|
|
|
|
case AddrMode.ZEROPAGE:
|
|
|
|
case AddrMode.ABSOLUTE_X:
|
|
|
|
case AddrMode.ZEROPAGE_X:
|
|
|
|
case AddrMode.ABSOLUTE_Y:
|
|
|
|
case AddrMode.ZEROPAGE_Y:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
|
|
|
switch (addrMode1) {
|
|
|
|
case AddrMode.IMMEDIATE:
|
|
|
|
int high = value1 >> 8;
|
|
|
|
value1 &= 0xff;
|
|
|
|
load(1);
|
|
|
|
store(2);
|
|
|
|
value2++;
|
|
|
|
if (unknownInPass1) {
|
|
|
|
value1 = high;
|
|
|
|
load(1);
|
|
|
|
} else {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (inc != 0 && cast(ubyte) (value1 + 1) == high)
|
2005-05-22 12:28:58 +00:00
|
|
|
putByte(inc);
|
2013-02-19 16:37:14 +00:00
|
|
|
else if (dec != 0 && cast(ubyte) (value1 - 1) == high)
|
2005-05-22 12:28:58 +00:00
|
|
|
putByte(dec);
|
2013-02-19 16:37:14 +00:00
|
|
|
else if (value1 != high) {
|
2005-05-22 12:28:58 +00:00
|
|
|
value1 = high;
|
|
|
|
load(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
store(2);
|
|
|
|
break;
|
|
|
|
case AddrMode.ABSOLUTE:
|
|
|
|
case AddrMode.ZEROPAGE:
|
|
|
|
case AddrMode.ABSOLUTE_X:
|
|
|
|
case AddrMode.ZEROPAGE_X:
|
|
|
|
case AddrMode.ABSOLUTE_Y:
|
|
|
|
case AddrMode.ZEROPAGE_Y:
|
|
|
|
load(1);
|
|
|
|
store(2);
|
|
|
|
value1++;
|
|
|
|
value2++;
|
|
|
|
load(1);
|
|
|
|
store(2);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalAddrMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void storeDtaNumber(int val, char letter) {
|
|
|
|
int limit = 0xffff;
|
|
|
|
if (letter == 'b') limit = 0xff;
|
2013-02-19 16:37:14 +00:00
|
|
|
if ((!unknownInPass1 || pass2) && (val < -limit || val > limit))
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Value out of range");
|
2013-02-19 16:37:14 +00:00
|
|
|
final switch (letter) {
|
2005-05-22 12:28:58 +00:00
|
|
|
case 'a':
|
|
|
|
putWord(cast(ushort) val);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
case 'l':
|
|
|
|
putByte(cast(ubyte) val);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
putByte(cast(ubyte) (val >> 8));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyDtaInteger(char letter) {
|
|
|
|
if (readFunction() == "SIN") {
|
|
|
|
readWord();
|
|
|
|
int sinCenter = value;
|
|
|
|
readComma();
|
|
|
|
readWord();
|
|
|
|
int sinAmp = value;
|
|
|
|
readComma();
|
|
|
|
readKnownPositive();
|
|
|
|
int sinPeriod = value;
|
|
|
|
int sinMin = 0;
|
|
|
|
int sinMax = sinPeriod - 1;
|
|
|
|
switch (readChar()) {
|
|
|
|
case ')':
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
readUnsignedWord();
|
|
|
|
mustBeKnownInPass1();
|
|
|
|
sinMin = value;
|
|
|
|
readComma();
|
|
|
|
readUnsignedWord();
|
|
|
|
mustBeKnownInPass1();
|
|
|
|
sinMax = value;
|
|
|
|
if (readChar() != ')') {
|
|
|
|
illegalCharacter();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalCharacter();
|
|
|
|
}
|
|
|
|
while (sinMin <= sinMax) {
|
|
|
|
int val = sinCenter + cast(int) rint(sinAmp * sin(sinMin * 2 * PI / sinPeriod));
|
|
|
|
storeDtaNumber(val, letter);
|
|
|
|
sinMin++;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
readWord();
|
|
|
|
storeDtaNumber(value, letter);
|
|
|
|
}
|
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
bool realSign;
|
2005-05-22 12:28:58 +00:00
|
|
|
|
|
|
|
int realExponent;
|
|
|
|
|
|
|
|
long realMantissa;
|
|
|
|
|
|
|
|
void putReal() {
|
|
|
|
if (realMantissa == 0) {
|
|
|
|
putWord(0);
|
|
|
|
putWord(0);
|
|
|
|
putWord(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (realMantissa < 0x1000000000L) {
|
|
|
|
realMantissa <<= 4;
|
|
|
|
realExponent--;
|
|
|
|
}
|
|
|
|
if ((realExponent & 1) != 0) {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (realMantissa & 0xf)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Out of precision");
|
|
|
|
realMantissa >>= 4;
|
|
|
|
}
|
|
|
|
realExponent = (realExponent + 0x89) >> 1;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (realExponent < 64 - 49)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Out of precision");
|
2013-02-19 16:37:14 +00:00
|
|
|
if (realExponent > 64 + 49)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Number too big");
|
|
|
|
putByte(cast(ubyte) (realSign ? realExponent + 0x80 : realExponent));
|
|
|
|
putByte(cast(ubyte) (realMantissa >> 32));
|
|
|
|
putByte(cast(ubyte) (realMantissa >> 24));
|
|
|
|
putByte(cast(ubyte) (realMantissa >> 16));
|
|
|
|
putByte(cast(ubyte) (realMantissa >> 8));
|
|
|
|
putByte(cast(ubyte) realMantissa);
|
|
|
|
}
|
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
bool readSign() {
|
2005-05-22 12:28:58 +00:00
|
|
|
switch (readChar()) {
|
|
|
|
case '+':
|
|
|
|
return false;
|
|
|
|
case '-':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
column--;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void readExponent() {
|
2010-12-28 12:44:00 +00:00
|
|
|
bool sign = readSign();
|
2005-05-22 12:28:58 +00:00
|
|
|
char c = readChar();
|
2013-02-19 16:37:14 +00:00
|
|
|
if (c < '0' || c > '9')
|
2005-05-22 12:28:58 +00:00
|
|
|
illegalCharacter();
|
|
|
|
int e = c - '0';
|
|
|
|
c = readChar();
|
2013-02-19 16:37:14 +00:00
|
|
|
if (c >= '0' && c <= '9')
|
2005-05-22 12:28:58 +00:00
|
|
|
e = 10 * e + c - '0';
|
2013-02-19 16:37:14 +00:00
|
|
|
else
|
2005-05-22 12:28:58 +00:00
|
|
|
column--;
|
|
|
|
realExponent += sign ? -e : e;
|
|
|
|
putReal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void readFraction() {
|
|
|
|
for (;;) {
|
|
|
|
char c = readChar();
|
|
|
|
if (c >= '0' && c <= '9') {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (c != '0' && realMantissa >= 0x1000000000L)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Out of precision");
|
|
|
|
realMantissa <<= 4;
|
|
|
|
realMantissa += c - '0';
|
|
|
|
realExponent--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == 'E' || c == 'e') {
|
|
|
|
readExponent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
column--;
|
|
|
|
putReal();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyDtaReal() {
|
|
|
|
realSign = readSign();
|
|
|
|
realExponent = 0;
|
|
|
|
realMantissa = 0;
|
|
|
|
char c = readChar();
|
|
|
|
if (c == '.') {
|
|
|
|
readFraction();
|
|
|
|
return;
|
|
|
|
}
|
2013-02-19 16:37:14 +00:00
|
|
|
if (c < '0' || c > '9')
|
2005-05-22 12:28:58 +00:00
|
|
|
illegalCharacter();
|
|
|
|
do {
|
|
|
|
if (realMantissa < 0x1000000000L) {
|
|
|
|
realMantissa <<= 4;
|
|
|
|
realMantissa += c - '0';
|
|
|
|
} else {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (c != '0')
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Out of precision");
|
|
|
|
realExponent++;
|
|
|
|
}
|
|
|
|
c = readChar();
|
|
|
|
} while (c >= '0' && c <= '9');
|
|
|
|
switch (c) {
|
|
|
|
case '.':
|
|
|
|
readFraction();
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
case 'e':
|
|
|
|
readExponent();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
column--;
|
|
|
|
putReal();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyDtaNumbers(char letter) {
|
|
|
|
if (eol() || line[column] != '(') {
|
|
|
|
column--;
|
|
|
|
assemblyDtaInteger('b');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
column++;
|
|
|
|
for (;;) {
|
2013-02-19 16:37:14 +00:00
|
|
|
final switch (letter) {
|
2005-05-22 12:28:58 +00:00
|
|
|
case 'a':
|
|
|
|
case 'b':
|
|
|
|
case 'h':
|
|
|
|
case 'l':
|
|
|
|
assemblyDtaInteger(letter);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
assemblyDtaReal();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (readChar()) {
|
|
|
|
case ')':
|
|
|
|
return;
|
|
|
|
case ',':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
illegalCharacter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyDta() {
|
|
|
|
noOpcode();
|
|
|
|
readSpaces();
|
|
|
|
for (;;) {
|
|
|
|
switch (readChar()) {
|
|
|
|
case 'A':
|
|
|
|
case 'a':
|
|
|
|
assemblyDtaNumbers('a');
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
case 'b':
|
|
|
|
assemblyDtaNumbers('b');
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
case 'c':
|
|
|
|
ubyte[] s = readString();
|
|
|
|
if (s is null) {
|
|
|
|
column--;
|
|
|
|
assemblyDtaInteger('b');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
foreach (ubyte b; s) {
|
|
|
|
putByte(b);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
case 'd':
|
|
|
|
ubyte[] s = readString();
|
|
|
|
if (s is null) {
|
|
|
|
column--;
|
|
|
|
assemblyDtaInteger('b');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
foreach (ubyte b; s) {
|
2013-02-19 16:37:14 +00:00
|
|
|
final switch (b & 0x60) {
|
2005-05-22 12:28:58 +00:00
|
|
|
case 0x00:
|
2010-12-28 12:44:00 +00:00
|
|
|
putByte(cast(ubyte) (b + 0x40));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case 0x20:
|
|
|
|
case 0x40:
|
2010-12-28 12:44:00 +00:00
|
|
|
putByte(cast(ubyte) (b - 0x20));
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
case 0x60:
|
|
|
|
putByte(b);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
case 'h':
|
|
|
|
assemblyDtaNumbers('h');
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
case 'l':
|
|
|
|
assemblyDtaNumbers('l');
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
case 'r':
|
|
|
|
assemblyDtaNumbers('r');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
column--;
|
|
|
|
assemblyDtaInteger('b');
|
|
|
|
break;
|
|
|
|
}
|
2013-02-19 16:37:14 +00:00
|
|
|
if (eol() || line[column] != ',')
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
|
|
|
column++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyEqu() {
|
|
|
|
directive();
|
2013-02-19 16:37:14 +00:00
|
|
|
if (currentLabel is null)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Label name required");
|
|
|
|
currentLabel.value = 0;
|
|
|
|
readSpaces();
|
|
|
|
readValue();
|
|
|
|
currentLabel.value = value;
|
|
|
|
currentLabel.unknownInPass1 = unknownInPass1;
|
|
|
|
if (optionListing) {
|
|
|
|
listingLine[6] = '=';
|
|
|
|
int val = value;
|
|
|
|
listingLine[7] = ' ';
|
|
|
|
if (val < 0) {
|
|
|
|
listingLine[7] = '-';
|
|
|
|
val = -val;
|
|
|
|
}
|
|
|
|
listingColumn = 8;
|
2013-02-19 16:37:14 +00:00
|
|
|
if ((val & 0xffff0000) != 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
listWord(cast(ushort) (val >> 16));
|
2013-02-19 16:37:14 +00:00
|
|
|
else {
|
|
|
|
while (listingColumn < 12)
|
2005-05-22 12:28:58 +00:00
|
|
|
listingLine[listingColumn++] = ' ';
|
|
|
|
}
|
|
|
|
listWord(cast(ushort) val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyEnd() {
|
|
|
|
directive();
|
|
|
|
assert(!foundEnd);
|
|
|
|
foundEnd = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyIftEli() {
|
2013-02-19 16:37:14 +00:00
|
|
|
ifContexts[$ - 1].condition = true;
|
2005-05-22 12:28:58 +00:00
|
|
|
if (!inFalseCondition()) {
|
|
|
|
readSpaces();
|
|
|
|
readValue();
|
|
|
|
mustBeKnownInPass1();
|
2013-02-19 16:37:14 +00:00
|
|
|
if (value != 0)
|
|
|
|
ifContexts[$ - 1].aConditionMatched = true;
|
|
|
|
else
|
2005-05-22 12:28:58 +00:00
|
|
|
listLine();
|
2013-02-19 16:37:14 +00:00
|
|
|
ifContexts[$ - 1].condition = value != 0;
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkMissingIft() {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (ifContexts.length == 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Missing IFT");
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyIft() {
|
|
|
|
directive();
|
2013-02-19 16:37:14 +00:00
|
|
|
ifContexts.length++;
|
2005-05-22 12:28:58 +00:00
|
|
|
assemblyIftEli();
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyEliEls() {
|
|
|
|
directive();
|
|
|
|
checkMissingIft();
|
2013-02-19 16:37:14 +00:00
|
|
|
if (ifContexts[$ - 1].wasElse)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("EIF expected");
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyEli() {
|
|
|
|
assemblyEliEls();
|
2013-02-19 16:37:14 +00:00
|
|
|
if (ifContexts[$ - 1].aConditionMatched) {
|
|
|
|
ifContexts[$ - 1].condition = false;
|
2005-05-22 12:28:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
assemblyIftEli();
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyEls() {
|
|
|
|
assemblyEliEls();
|
2013-02-19 16:37:14 +00:00
|
|
|
with (ifContexts[$ - 1]) {
|
|
|
|
if (condition && aConditionMatched)
|
2005-05-22 12:28:58 +00:00
|
|
|
listLine();
|
|
|
|
wasElse = true;
|
|
|
|
condition = !aConditionMatched;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyEif() {
|
|
|
|
directive();
|
|
|
|
checkMissingIft();
|
2013-02-19 16:37:14 +00:00
|
|
|
ifContexts.length--;
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyErt() {
|
|
|
|
directive();
|
|
|
|
readSpaces();
|
|
|
|
readValue();
|
2013-02-19 16:37:14 +00:00
|
|
|
if (pass2 && value != 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("User-defined error");
|
|
|
|
}
|
|
|
|
|
2010-12-28 12:44:00 +00:00
|
|
|
bool readOption() {
|
2005-05-22 12:28:58 +00:00
|
|
|
switch (readChar()) {
|
|
|
|
case '-':
|
|
|
|
return false;
|
|
|
|
case '+':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
illegalCharacter();
|
|
|
|
}
|
2010-12-28 12:44:00 +00:00
|
|
|
assert(0);
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyOpt() {
|
|
|
|
directive();
|
|
|
|
readSpaces();
|
|
|
|
while (!eol()) {
|
|
|
|
switch (line[column++]) {
|
|
|
|
case 'F':
|
|
|
|
case 'f':
|
|
|
|
optionFill = readOption();
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
case 'g':
|
|
|
|
option5200 = readOption();
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
case 'h':
|
|
|
|
optionHeaders = readOption();
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
case 'l':
|
|
|
|
optionListing = readOption() && !pass2;
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
case 'o':
|
|
|
|
optionObject = readOption();
|
|
|
|
break;
|
2014-04-30 07:19:49 +00:00
|
|
|
case 'U':
|
|
|
|
case 'u':
|
|
|
|
optionUnusedLabels = readOption();
|
|
|
|
break;
|
2005-05-22 12:28:58 +00:00
|
|
|
default:
|
|
|
|
column--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void originWord(ushort value, char listingChar) {
|
|
|
|
objectWord(value);
|
|
|
|
listWord(value);
|
|
|
|
listingLine[listingColumn++] = listingChar;
|
|
|
|
}
|
|
|
|
|
2014-12-31 13:30:09 +00:00
|
|
|
OrgModifier readOrgModifier() {
|
|
|
|
readSpaces();
|
|
|
|
if (column + 2 < line.length && line[column + 1] == ':') {
|
|
|
|
switch (line[column]) {
|
|
|
|
case 'F':
|
|
|
|
case 'f':
|
|
|
|
checkHeadersOn();
|
|
|
|
column += 2;
|
|
|
|
return OrgModifier.FORCE_FFFF;
|
|
|
|
case 'A':
|
|
|
|
case 'a':
|
|
|
|
checkHeadersOn();
|
|
|
|
column += 2;
|
|
|
|
return OrgModifier.FORCE_HEADER;
|
|
|
|
case 'R':
|
|
|
|
case 'r':
|
|
|
|
column += 2;
|
|
|
|
return OrgModifier.RELOCATE;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return OrgModifier.NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setOrigin(int addr, OrgModifier modifier) {
|
2005-05-22 12:28:58 +00:00
|
|
|
origin = loadOrigin = addr;
|
2014-12-31 13:30:09 +00:00
|
|
|
bool requestedHeader = modifier != OrgModifier.NONE;
|
2005-05-22 12:28:58 +00:00
|
|
|
if (requestedHeader || loadingOrigin < 0 || (addr != loadingOrigin && !optionFill)) {
|
|
|
|
blockIndex++;
|
|
|
|
if (!pass2) {
|
|
|
|
assert(blockIndex == blockEnds.length);
|
|
|
|
blockEnds ~= cast(ushort) (addr - 1);
|
|
|
|
}
|
|
|
|
if (pass2 && optionHeaders) {
|
|
|
|
if (addr - 1 == blockEnds[blockIndex]) {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (requestedHeader)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Cannot generate an empty block");
|
|
|
|
return;
|
|
|
|
}
|
2014-12-31 13:30:09 +00:00
|
|
|
if (modifier == OrgModifier.FORCE_FFFF || objectBytes == 0) {
|
2005-05-22 12:28:58 +00:00
|
|
|
assert(requestedHeader || addr != loadingOrigin);
|
|
|
|
originWord(0xffff, '>');
|
|
|
|
listingLine[listingColumn++] = ' ';
|
|
|
|
}
|
|
|
|
if (requestedHeader || addr != loadingOrigin) {
|
|
|
|
originWord(cast(ushort) addr, '-');
|
|
|
|
originWord(blockEnds[blockIndex], '>');
|
|
|
|
listingLine[listingColumn++] = ' ';
|
|
|
|
loadingOrigin = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkHeadersOn() {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (!optionHeaders)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Illegal when Atari file headers disabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyOrg() {
|
|
|
|
noRepeatSkipDirective();
|
2014-12-31 13:30:09 +00:00
|
|
|
OrgModifier modifier = readOrgModifier();
|
2005-05-22 12:28:58 +00:00
|
|
|
readUnsignedWord();
|
|
|
|
mustBeKnownInPass1();
|
2014-12-31 13:30:09 +00:00
|
|
|
if (modifier == OrgModifier.RELOCATE) {
|
|
|
|
checkOriginDefined();
|
|
|
|
origin = value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setOrigin(value, modifier);
|
|
|
|
}
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyRunIni(ushort addr) {
|
|
|
|
noRepeatSkipDirective();
|
|
|
|
checkHeadersOn();
|
|
|
|
loadingOrigin = -1; // don't fill
|
2014-12-31 13:30:09 +00:00
|
|
|
OrgModifier modifier = readOrgModifier();
|
|
|
|
if (modifier == OrgModifier.RELOCATE)
|
|
|
|
throw new AssemblyError("r: invalid here");
|
|
|
|
setOrigin(addr, modifier);
|
2005-05-22 12:28:58 +00:00
|
|
|
readUnsignedWord();
|
2010-12-28 12:44:00 +00:00
|
|
|
putWord(cast(ushort) (value));
|
2005-05-22 12:28:58 +00:00
|
|
|
loadingOrigin = -1; // don't fill
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyIcl() {
|
|
|
|
directive();
|
2013-02-19 16:37:14 +00:00
|
|
|
string filename = readFilename();
|
2005-05-22 12:28:58 +00:00
|
|
|
checkNoExtraCharacters();
|
|
|
|
listLine();
|
2014-02-21 22:06:03 +00:00
|
|
|
includeLevel++;
|
2005-05-22 12:28:58 +00:00
|
|
|
assemblyFile(filename);
|
2014-02-21 22:06:03 +00:00
|
|
|
includeLevel--;
|
2005-05-22 12:28:58 +00:00
|
|
|
line = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyIns() {
|
2013-02-19 16:37:14 +00:00
|
|
|
string filename = readFilename();
|
2005-05-22 12:28:58 +00:00
|
|
|
int offset = 0;
|
|
|
|
int length = -1;
|
|
|
|
if (!eol() && line[column] == ',') {
|
|
|
|
column++;
|
|
|
|
readValue();
|
|
|
|
mustBeKnownInPass1();
|
|
|
|
offset = value;
|
|
|
|
if (!eol() && line[column] == ',') {
|
|
|
|
column++;
|
|
|
|
readKnownPositive();
|
|
|
|
length = value;
|
|
|
|
}
|
|
|
|
}
|
2013-03-19 10:12:12 +00:00
|
|
|
File stream = openInputFile(filename);
|
2010-12-28 12:44:00 +00:00
|
|
|
scope (exit) stream.close();
|
2005-05-22 12:28:58 +00:00
|
|
|
try {
|
2013-03-19 10:12:12 +00:00
|
|
|
stream.seek(offset, offset >= 0 ? SEEK_SET : SEEK_END);
|
|
|
|
} catch (Exception e) {
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Error seeking file");
|
|
|
|
}
|
2014-04-04 18:26:33 +00:00
|
|
|
if (inOpcode)
|
|
|
|
length = 1;
|
2005-05-22 12:28:58 +00:00
|
|
|
while (length != 0) {
|
2014-09-26 14:42:54 +00:00
|
|
|
int b = readByte(&stream);
|
|
|
|
if (b < 0) {
|
|
|
|
if (length > 0)
|
|
|
|
throw new AssemblyError("File is too short");
|
|
|
|
break;
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
2014-09-26 14:42:54 +00:00
|
|
|
putByte(cast(ubyte) b);
|
2005-05-22 12:28:58 +00:00
|
|
|
if (length > 0) length--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
void assemblyInstruction(string instruction) {
|
|
|
|
if (!inOpcode && origin < 0 && currentLabel !is null && instruction != "EQU")
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("No ORG specified");
|
|
|
|
instructionBegin = true;
|
|
|
|
switch (instruction) {
|
|
|
|
case "ADC":
|
|
|
|
assemblyAccumulator(0x60, 0, 0);
|
|
|
|
break;
|
|
|
|
case "ADD":
|
|
|
|
assemblyAccumulator(0x60, 0x18, 0);
|
|
|
|
break;
|
|
|
|
case "AND":
|
|
|
|
assemblyAccumulator(0x20, 0, 0);
|
|
|
|
break;
|
|
|
|
case "ASL":
|
|
|
|
assemblyShift(0x00);
|
|
|
|
break;
|
|
|
|
case "BCC":
|
|
|
|
assemblyBranch(0x90);
|
|
|
|
break;
|
|
|
|
case "BCS":
|
|
|
|
assemblyBranch(0xb0);
|
|
|
|
break;
|
|
|
|
case "BEQ":
|
|
|
|
assemblyBranch(0xf0);
|
|
|
|
break;
|
|
|
|
case "BIT":
|
|
|
|
assemblyBit();
|
|
|
|
break;
|
|
|
|
case "BMI":
|
|
|
|
assemblyBranch(0x30);
|
|
|
|
break;
|
|
|
|
case "BNE":
|
|
|
|
assemblyBranch(0xd0);
|
|
|
|
break;
|
|
|
|
case "BPL":
|
|
|
|
assemblyBranch(0x10);
|
|
|
|
break;
|
|
|
|
case "BRK":
|
|
|
|
putByte(0x00);
|
|
|
|
break;
|
|
|
|
case "BVC":
|
|
|
|
assemblyBranch(0x50);
|
|
|
|
break;
|
|
|
|
case "BVS":
|
|
|
|
assemblyBranch(0x70);
|
|
|
|
break;
|
|
|
|
case "CLC":
|
|
|
|
putByte(0x18);
|
|
|
|
break;
|
|
|
|
case "CLD":
|
|
|
|
putByte(0xd8);
|
|
|
|
break;
|
|
|
|
case "CLI":
|
|
|
|
putByte(0x58);
|
|
|
|
break;
|
|
|
|
case "CLV":
|
|
|
|
putByte(0xb8);
|
|
|
|
break;
|
|
|
|
case "CMP":
|
|
|
|
assemblyAccumulator(0xc0, 0, 0);
|
|
|
|
break;
|
|
|
|
case "CPX":
|
|
|
|
assemblyCompareIndex(0xe0);
|
|
|
|
break;
|
|
|
|
case "CPY":
|
|
|
|
assemblyCompareIndex(0xc0);
|
|
|
|
break;
|
|
|
|
case "DEC":
|
|
|
|
assemblyShift(0xc0);
|
|
|
|
break;
|
|
|
|
case "DEX":
|
|
|
|
putByte(0xca);
|
|
|
|
break;
|
|
|
|
case "DEY":
|
|
|
|
putByte(0x88);
|
|
|
|
break;
|
|
|
|
case "DTA":
|
|
|
|
assemblyDta();
|
|
|
|
break;
|
|
|
|
case "EIF":
|
|
|
|
assemblyEif();
|
|
|
|
break;
|
|
|
|
case "ELI":
|
|
|
|
assemblyEli();
|
|
|
|
break;
|
|
|
|
case "ELS":
|
|
|
|
assemblyEls();
|
|
|
|
break;
|
|
|
|
case "END":
|
|
|
|
assemblyEnd();
|
|
|
|
break;
|
|
|
|
case "EOR":
|
|
|
|
assemblyAccumulator(0x40, 0, 0);
|
|
|
|
break;
|
|
|
|
case "EQU":
|
|
|
|
assemblyEqu();
|
|
|
|
break;
|
|
|
|
case "ERT":
|
|
|
|
assemblyErt();
|
|
|
|
break;
|
|
|
|
case "ICL":
|
|
|
|
assemblyIcl();
|
|
|
|
break;
|
|
|
|
case "IFT":
|
|
|
|
assemblyIft();
|
|
|
|
break;
|
|
|
|
case "INC":
|
|
|
|
assemblyShift(0xe0);
|
|
|
|
break;
|
|
|
|
case "INI":
|
|
|
|
assemblyRunIni(0x2e2);
|
|
|
|
break;
|
|
|
|
case "INS":
|
|
|
|
assemblyIns();
|
|
|
|
break;
|
|
|
|
case "INX":
|
|
|
|
putByte(0xe8);
|
|
|
|
break;
|
|
|
|
case "INY":
|
|
|
|
putByte(0xc8);
|
|
|
|
break;
|
|
|
|
case "INW":
|
|
|
|
assemblyInw();
|
|
|
|
break;
|
|
|
|
case "JCC":
|
|
|
|
assemblyConditionalJump(0xb0);
|
|
|
|
break;
|
|
|
|
case "JCS":
|
|
|
|
assemblyConditionalJump(0x90);
|
|
|
|
break;
|
|
|
|
case "JEQ":
|
|
|
|
assemblyConditionalJump(0xd0);
|
|
|
|
break;
|
|
|
|
case "JMI":
|
|
|
|
assemblyConditionalJump(0x10);
|
|
|
|
break;
|
|
|
|
case "JMP":
|
|
|
|
assemblyJmp();
|
|
|
|
break;
|
|
|
|
case "JNE":
|
|
|
|
assemblyConditionalJump(0xf0);
|
|
|
|
break;
|
|
|
|
case "JPL":
|
|
|
|
assemblyConditionalJump(0x30);
|
|
|
|
break;
|
|
|
|
case "JSR":
|
|
|
|
assemblyJsr();
|
|
|
|
break;
|
|
|
|
case "JVC":
|
|
|
|
assemblyConditionalJump(0x70);
|
|
|
|
break;
|
|
|
|
case "JVS":
|
|
|
|
assemblyConditionalJump(0x50);
|
|
|
|
break;
|
|
|
|
case "LDA":
|
|
|
|
assemblyAccumulator(0xa0, 0, 0);
|
|
|
|
break;
|
|
|
|
case "LDX":
|
|
|
|
assemblyLdx(0);
|
|
|
|
break;
|
|
|
|
case "LDY":
|
|
|
|
assemblyLdy(0);
|
|
|
|
break;
|
|
|
|
case "LSR":
|
|
|
|
assemblyShift(0x40);
|
|
|
|
break;
|
|
|
|
case "MVA":
|
|
|
|
assemblyMoveByte(&assemblyLda, &assemblySta);
|
|
|
|
break;
|
|
|
|
case "MVX":
|
|
|
|
assemblyMoveByte(&assemblyLdx, &assemblyStx);
|
|
|
|
break;
|
|
|
|
case "MVY":
|
|
|
|
assemblyMoveByte(&assemblyLdy, &assemblySty);
|
|
|
|
break;
|
|
|
|
case "MWA":
|
|
|
|
assemblyMoveWord(&assemblyLda, &assemblySta, 0, 0);
|
|
|
|
break;
|
|
|
|
case "MWX":
|
|
|
|
assemblyMoveWord(&assemblyLdx, &assemblyStx, 0xe8, 0xca);
|
|
|
|
break;
|
|
|
|
case "MWY":
|
|
|
|
assemblyMoveWord(&assemblyLdy, &assemblySty, 0xc8, 0x88);
|
|
|
|
break;
|
|
|
|
case "NOP":
|
|
|
|
putByte(0xea);
|
|
|
|
break;
|
|
|
|
case "OPT":
|
|
|
|
assemblyOpt();
|
|
|
|
break;
|
|
|
|
case "ORA":
|
|
|
|
assemblyAccumulator(0x00, 0, 0);
|
|
|
|
break;
|
|
|
|
case "ORG":
|
|
|
|
assemblyOrg();
|
|
|
|
break;
|
|
|
|
case "PHA":
|
|
|
|
putByte(0x48);
|
|
|
|
break;
|
|
|
|
case "PHP":
|
|
|
|
putByte(0x08);
|
|
|
|
break;
|
|
|
|
case "PLA":
|
|
|
|
putByte(0x68);
|
|
|
|
break;
|
|
|
|
case "PLP":
|
|
|
|
putByte(0x28);
|
|
|
|
break;
|
|
|
|
case "RCC":
|
|
|
|
assemblyRepeat(0x90);
|
|
|
|
break;
|
|
|
|
case "RCS":
|
|
|
|
assemblyRepeat(0xb0);
|
|
|
|
break;
|
|
|
|
case "REQ":
|
|
|
|
assemblyRepeat(0xf0);
|
|
|
|
break;
|
|
|
|
case "RMI":
|
|
|
|
assemblyRepeat(0x30);
|
|
|
|
break;
|
|
|
|
case "RNE":
|
|
|
|
assemblyRepeat(0xd0);
|
|
|
|
break;
|
|
|
|
case "ROL":
|
|
|
|
assemblyShift(0x20);
|
|
|
|
break;
|
|
|
|
case "ROR":
|
|
|
|
assemblyShift(0x60);
|
|
|
|
break;
|
|
|
|
case "RPL":
|
|
|
|
assemblyRepeat(0x10);
|
|
|
|
break;
|
|
|
|
case "RTI":
|
|
|
|
putByte(0x40);
|
|
|
|
break;
|
|
|
|
case "RTS":
|
|
|
|
putByte(0x60);
|
|
|
|
break;
|
|
|
|
case "RUN":
|
|
|
|
assemblyRunIni(0x2e0);
|
|
|
|
break;
|
|
|
|
case "RVC":
|
|
|
|
assemblyRepeat(0x50);
|
|
|
|
break;
|
|
|
|
case "RVS":
|
|
|
|
assemblyRepeat(0x70);
|
|
|
|
break;
|
|
|
|
case "SBC":
|
|
|
|
assemblyAccumulator(0xe0, 0, 0);
|
|
|
|
break;
|
|
|
|
case "SCC":
|
|
|
|
assemblySkip(0x90);
|
|
|
|
break;
|
|
|
|
case "SCS":
|
|
|
|
assemblySkip(0xb0);
|
|
|
|
break;
|
|
|
|
case "SEC":
|
|
|
|
putByte(0x38);
|
|
|
|
break;
|
|
|
|
case "SED":
|
|
|
|
putByte(0xf8);
|
|
|
|
break;
|
|
|
|
case "SEI":
|
|
|
|
putByte(0x78);
|
|
|
|
break;
|
|
|
|
case "SEQ":
|
|
|
|
assemblySkip(0xf0);
|
|
|
|
break;
|
|
|
|
case "SMI":
|
|
|
|
assemblySkip(0x30);
|
|
|
|
break;
|
|
|
|
case "SNE":
|
|
|
|
assemblySkip(0xd0);
|
|
|
|
break;
|
|
|
|
case "SPL":
|
|
|
|
assemblySkip(0x10);
|
|
|
|
break;
|
|
|
|
case "STA":
|
|
|
|
assemblyAccumulator(0x80, 0, 0);
|
|
|
|
break;
|
|
|
|
case "STX":
|
|
|
|
assemblyStx(0);
|
|
|
|
break;
|
|
|
|
case "STY":
|
|
|
|
assemblySty(0);
|
|
|
|
break;
|
|
|
|
case "SUB":
|
|
|
|
assemblyAccumulator(0xe0, 0x38, 0);
|
|
|
|
break;
|
|
|
|
case "SVC":
|
|
|
|
assemblySkip(0x50);
|
|
|
|
break;
|
|
|
|
case "SVS":
|
|
|
|
assemblySkip(0x70);
|
|
|
|
break;
|
|
|
|
case "TAX":
|
|
|
|
putByte(0xaa);
|
|
|
|
break;
|
|
|
|
case "TAY":
|
|
|
|
putByte(0xa8);
|
|
|
|
break;
|
|
|
|
case "TSX":
|
|
|
|
putByte(0xba);
|
|
|
|
break;
|
|
|
|
case "TXA":
|
|
|
|
putByte(0x8a);
|
|
|
|
break;
|
|
|
|
case "TXS":
|
|
|
|
putByte(0x9a);
|
|
|
|
break;
|
|
|
|
case "TYA":
|
|
|
|
putByte(0x98);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new AssemblyError("Illegal instruction");
|
|
|
|
}
|
|
|
|
skipping = false;
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
debug ubyte[] testInstruction(string l) {
|
2005-05-22 12:28:58 +00:00
|
|
|
objectBuffer.length = 0;
|
2013-02-19 16:37:14 +00:00
|
|
|
line = l;
|
2005-05-22 12:28:58 +00:00
|
|
|
column = 0;
|
|
|
|
assemblyInstruction(readInstruction());
|
2010-12-28 12:44:00 +00:00
|
|
|
write(line, " assembles to");
|
2013-02-19 16:37:14 +00:00
|
|
|
foreach (ubyte b; objectBuffer)
|
2010-12-28 12:44:00 +00:00
|
|
|
writef(" %02x", b);
|
|
|
|
writeln();
|
2005-05-22 12:28:58 +00:00
|
|
|
return objectBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest {
|
|
|
|
assert(testInstruction("nop") == cast(ubyte[]) x"ea");
|
|
|
|
assert(testInstruction("add (5,0)") == cast(ubyte[]) x"18a2006105");
|
|
|
|
assert(testInstruction("mwa #$abcd $1234") == cast(ubyte[]) x"a9cd8d3412a9ab8d3512");
|
|
|
|
assert(testInstruction("dta 5,d'Foo'*,a($4589)") == cast(ubyte[]) x"05a6efef8945");
|
|
|
|
assert(testInstruction("dta r(1,12,123,1234567890,12345678900000,.5,.03,000.1664534589,1e97)")
|
|
|
|
== cast(ubyte[]) x"400100000000 401200000000 410123000000 441234567890 461234567890 3f5000000000 3f0300000000 3f1664534589 701000000000");
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyPair() {
|
|
|
|
assert(!inOpcode);
|
2013-02-19 16:37:14 +00:00
|
|
|
string instruction = readInstruction();
|
2005-05-22 12:28:58 +00:00
|
|
|
if (!eol() && line[column] == ':') {
|
|
|
|
pairing = true;
|
|
|
|
column++;
|
2013-02-19 16:37:14 +00:00
|
|
|
string instruction2 = readInstruction();
|
2005-05-22 12:28:58 +00:00
|
|
|
int savedColumn = column;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (willSkip)
|
2005-05-22 12:28:58 +00:00
|
|
|
warning("Skipping only the first instruction");
|
|
|
|
assemblyInstruction(instruction);
|
|
|
|
checkNoExtraCharacters();
|
|
|
|
column = savedColumn;
|
|
|
|
wereManyInstructions = false;
|
|
|
|
assemblyInstruction(instruction2);
|
|
|
|
wereManyInstructions = true;
|
|
|
|
} else {
|
|
|
|
pairing = false;
|
|
|
|
assemblyInstruction(instruction);
|
|
|
|
wereManyInstructions = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyLine() {
|
|
|
|
debug {
|
2010-12-28 12:44:00 +00:00
|
|
|
writeln(line);
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
2014-02-21 22:06:03 +00:00
|
|
|
lineNo++;
|
2005-05-22 12:28:58 +00:00
|
|
|
totalLines++;
|
|
|
|
column = 0;
|
|
|
|
listingColumn = 6;
|
|
|
|
if (origin >= 0) {
|
2010-12-28 12:44:00 +00:00
|
|
|
listWord(cast(ushort) origin);
|
2005-05-22 12:28:58 +00:00
|
|
|
listingLine[listingColumn++] = ' ';
|
|
|
|
}
|
2013-02-19 16:37:14 +00:00
|
|
|
string label = readLabel();
|
2005-05-22 12:28:58 +00:00
|
|
|
currentLabel = null;
|
2007-04-22 19:16:22 +00:00
|
|
|
if (label !is null) {
|
2005-05-22 12:28:58 +00:00
|
|
|
if (!inFalseCondition()) {
|
|
|
|
if (!pass2) {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (label in labelTable)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Label declared twice");
|
|
|
|
currentLabel = new Label(origin);
|
2013-02-19 16:37:14 +00:00
|
|
|
labelTable[label] = currentLabel;
|
2005-05-22 12:28:58 +00:00
|
|
|
} else {
|
|
|
|
assert(label in labelTable);
|
|
|
|
currentLabel = labelTable[label];
|
|
|
|
currentLabel.passed = true;
|
2014-04-30 07:19:49 +00:00
|
|
|
if (currentLabel.unused && getOption('u') && optionUnusedLabels)
|
2016-01-03 07:47:19 +00:00
|
|
|
warning("Unused label: " ~ label);
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (eol()) {
|
|
|
|
listCommentLine();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
readSpaces();
|
|
|
|
}
|
|
|
|
commentOrRep: for (;;) {
|
|
|
|
if (eol()) {
|
|
|
|
listCommentLine();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (line[column]) {
|
|
|
|
case '\t':
|
|
|
|
case ' ':
|
|
|
|
column++;
|
|
|
|
continue;
|
|
|
|
case '*':
|
|
|
|
case ';':
|
|
|
|
case '|':
|
|
|
|
listCommentLine();
|
|
|
|
return;
|
|
|
|
case ':':
|
|
|
|
if (inFalseCondition()) {
|
|
|
|
listCommentLine();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
column++;
|
|
|
|
readUnsignedWord();
|
|
|
|
mustBeKnownInPass1();
|
|
|
|
int repeatLimit = value;
|
|
|
|
if (repeatLimit == 0) {
|
|
|
|
listCommentLine();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
readSpaces();
|
|
|
|
repeating = true;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (repeatLimit == 1)
|
2005-05-22 12:28:58 +00:00
|
|
|
break;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (willSkip)
|
2005-05-22 12:28:58 +00:00
|
|
|
warning("Skipping only the first instruction");
|
|
|
|
int savedColumn = column;
|
|
|
|
for (repeatCounter = 0; repeatCounter < repeatLimit; repeatCounter++) {
|
|
|
|
column = savedColumn;
|
|
|
|
assemblyPair();
|
|
|
|
}
|
|
|
|
checkNoExtraCharacters();
|
|
|
|
listLine();
|
|
|
|
wereManyInstructions = true;
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
repeating = false;
|
|
|
|
break commentOrRep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (inFalseCondition()) {
|
|
|
|
switch (readInstruction()) {
|
|
|
|
case "END":
|
|
|
|
assemblyEnd();
|
|
|
|
break;
|
|
|
|
case "IFT":
|
|
|
|
assemblyIft();
|
|
|
|
break;
|
|
|
|
case "ELI":
|
|
|
|
assemblyEli();
|
|
|
|
break;
|
|
|
|
case "ELS":
|
|
|
|
assemblyEls();
|
|
|
|
break;
|
|
|
|
case "EIF":
|
|
|
|
assemblyEif();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
listCommentLine();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
checkNoExtraCharacters();
|
|
|
|
listCommentLine();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assemblyPair();
|
|
|
|
checkNoExtraCharacters();
|
|
|
|
listLine();
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
void assemblyFile(string filename) {
|
|
|
|
filename = filename.defaultExtension("asx");
|
|
|
|
if (getOption('p'))
|
|
|
|
filename = absolutePath(filename);
|
2013-03-19 10:12:12 +00:00
|
|
|
File stream = openInputFile(filename);
|
2010-12-28 12:44:00 +00:00
|
|
|
scope (exit) stream.close();
|
2014-02-21 22:06:03 +00:00
|
|
|
string oldFilename = currentFilename;
|
|
|
|
int oldLineNo = lineNo;
|
|
|
|
currentFilename = filename;
|
|
|
|
lineNo = 0;
|
|
|
|
foundEnd = false;
|
2013-02-19 16:37:14 +00:00
|
|
|
line = "";
|
2010-12-28 12:44:00 +00:00
|
|
|
readChar: while (!foundEnd) {
|
2014-09-26 14:42:54 +00:00
|
|
|
int b = readByte(&stream);
|
|
|
|
switch (b) {
|
|
|
|
case -1:
|
|
|
|
break readChar;
|
2010-12-28 12:44:00 +00:00
|
|
|
case '\r':
|
|
|
|
assemblyLine();
|
2013-02-19 16:37:14 +00:00
|
|
|
line = "";
|
2014-09-26 14:42:54 +00:00
|
|
|
b = readByte(&stream);
|
|
|
|
if (b < 0)
|
2010-12-28 12:44:00 +00:00
|
|
|
break readChar;
|
2014-09-26 14:42:54 +00:00
|
|
|
if (b != '\n')
|
|
|
|
line ~= cast(char) b;
|
2010-12-28 12:44:00 +00:00
|
|
|
break;
|
|
|
|
case '\n':
|
2014-09-26 14:42:54 +00:00
|
|
|
case 0x9b:
|
2005-05-22 12:28:58 +00:00
|
|
|
assemblyLine();
|
2013-02-19 16:37:14 +00:00
|
|
|
line = "";
|
2010-12-28 12:44:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-09-26 14:42:54 +00:00
|
|
|
line ~= cast(char) b;
|
2010-12-28 12:44:00 +00:00
|
|
|
break;
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-19 16:37:14 +00:00
|
|
|
if (!foundEnd)
|
2010-12-28 12:44:00 +00:00
|
|
|
assemblyLine();
|
|
|
|
foundEnd = false;
|
2014-02-21 22:06:03 +00:00
|
|
|
currentFilename = oldFilename;
|
|
|
|
lineNo = oldLineNo;
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void assemblyPass() {
|
|
|
|
origin = -1;
|
|
|
|
loadOrigin = -1;
|
|
|
|
loadingOrigin = -1;
|
|
|
|
blockIndex = -1;
|
|
|
|
optionFill = false;
|
|
|
|
option5200 = false;
|
|
|
|
optionHeaders = true;
|
|
|
|
optionListing = pass2;
|
|
|
|
optionObject = true;
|
2014-04-30 07:19:49 +00:00
|
|
|
optionUnusedLabels = true;
|
2005-05-22 12:28:58 +00:00
|
|
|
willSkip = false;
|
|
|
|
skipping = false;
|
|
|
|
repeatOffset = 0;
|
|
|
|
wereManyInstructions = false;
|
2014-02-21 22:06:03 +00:00
|
|
|
currentFilename = "command line";
|
|
|
|
lineNo = 0;
|
|
|
|
foreach (definition; commandLineDefinitions) {
|
2014-02-21 22:29:58 +00:00
|
|
|
line = replaceFirst(definition, "=", " equ ");
|
2014-02-21 22:06:03 +00:00
|
|
|
assemblyLine();
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
2014-02-21 22:06:03 +00:00
|
|
|
line = null;
|
2005-05-22 12:28:58 +00:00
|
|
|
totalLines = 0;
|
|
|
|
assemblyFile(sourceFilename);
|
2013-02-19 16:37:14 +00:00
|
|
|
if (ifContexts.length != 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Missing EIF");
|
2013-02-19 16:37:14 +00:00
|
|
|
if (willSkip)
|
2005-05-22 12:28:58 +00:00
|
|
|
throw new AssemblyError("Can't skip over this");
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
pure bool isOption(string arg) {
|
2005-05-22 12:28:58 +00:00
|
|
|
if (arg.length < 2) return false;
|
|
|
|
if (arg[0] == '-') return true;
|
|
|
|
if (arg[0] != '/') return false;
|
|
|
|
if (arg.length == 2) return true;
|
|
|
|
if (arg[2] == ':') return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setOption(char letter) {
|
|
|
|
assert(letter >= 'a' && letter <= 'z');
|
|
|
|
if (options[letter - 'a']) {
|
|
|
|
exitCode = 3;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
options[letter - 'a'] = true;
|
|
|
|
}
|
|
|
|
|
2013-02-19 16:37:14 +00:00
|
|
|
int main(string[] args) {
|
2005-05-22 12:28:58 +00:00
|
|
|
for (int i = 1; i < args.length; i++) {
|
2013-02-19 16:37:14 +00:00
|
|
|
string arg = args[i];
|
2005-05-22 12:28:58 +00:00
|
|
|
if (isOption(arg)) {
|
|
|
|
char letter = arg[1];
|
2009-10-17 19:47:06 +00:00
|
|
|
if (letter >= 'A' && letter <= 'Z')
|
|
|
|
letter += 'a' - 'A';
|
2005-05-22 12:28:58 +00:00
|
|
|
switch (letter) {
|
|
|
|
case 'c':
|
|
|
|
case 'i':
|
2009-10-17 19:47:06 +00:00
|
|
|
case 'm':
|
2005-05-22 12:28:58 +00:00
|
|
|
case 'p':
|
|
|
|
case 'q':
|
|
|
|
case 'u':
|
2013-02-19 16:37:14 +00:00
|
|
|
if (arg.length != 2)
|
2005-05-22 12:28:58 +00:00
|
|
|
exitCode = 3;
|
|
|
|
setOption(letter);
|
|
|
|
break;
|
|
|
|
case 'd':
|
2013-02-19 16:37:14 +00:00
|
|
|
string definition = null;
|
2005-05-22 12:28:58 +00:00
|
|
|
if (arg[0] == '/') {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (arg.length >= 3 && arg[2] == ':')
|
|
|
|
definition = arg[3 .. $];
|
|
|
|
} else if (i + 1 < args.length && !isOption(args[i + 1]))
|
2005-05-22 12:28:58 +00:00
|
|
|
definition = args[++i];
|
2014-02-21 22:29:58 +00:00
|
|
|
if (definition is null || find(definition, '=').empty)
|
2005-05-22 12:28:58 +00:00
|
|
|
exitCode = 3;
|
|
|
|
commandLineDefinitions ~= definition;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
case 't':
|
|
|
|
case 'o':
|
|
|
|
setOption(letter);
|
2013-02-19 16:37:14 +00:00
|
|
|
string filename = null;
|
2005-05-22 12:28:58 +00:00
|
|
|
if (arg[0] == '/') {
|
2013-02-19 16:37:14 +00:00
|
|
|
if (arg.length >= 3 && arg[2] == ':')
|
|
|
|
filename = arg[3 .. $];
|
|
|
|
} else if (i + 1 < args.length && !isOption(args[i + 1]))
|
2005-05-22 12:28:58 +00:00
|
|
|
filename = args[++i];
|
2013-02-19 16:37:14 +00:00
|
|
|
if (filename is null && (letter == 'o' || arg.length != 2))
|
2005-05-22 12:28:58 +00:00
|
|
|
exitCode = 3;
|
|
|
|
optionParameters[letter - 'a'] = filename;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exitCode = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-19 16:37:14 +00:00
|
|
|
if (sourceFilename !is null)
|
2005-05-22 12:28:58 +00:00
|
|
|
exitCode = 3;
|
|
|
|
sourceFilename = arg;
|
|
|
|
}
|
2013-02-19 16:37:14 +00:00
|
|
|
if (sourceFilename is null)
|
2005-05-22 12:28:58 +00:00
|
|
|
exitCode = 3;
|
2013-02-19 16:37:14 +00:00
|
|
|
if (!getOption('q'))
|
2010-12-28 12:44:00 +00:00
|
|
|
writeln(TITLE);
|
2005-05-22 12:28:58 +00:00
|
|
|
if (exitCode != 0) {
|
2010-12-28 12:44:00 +00:00
|
|
|
write(
|
2017-02-08 18:32:42 +00:00
|
|
|
`Syntax: xasm source [options]
|
|
|
|
/c Include false conditionals in listing
|
|
|
|
/d:label=value Define a label
|
|
|
|
/i Don't list included files
|
|
|
|
/l[:filename] Generate listing
|
|
|
|
/o:filename Set object file name
|
|
|
|
/M Print Makefile rule
|
|
|
|
/p Print absolute paths in listing and error messages
|
|
|
|
/q Suppress info messages
|
|
|
|
/t[:filename] List label table
|
|
|
|
/u Warn of unused labels
|
|
|
|
`);
|
2005-05-22 12:28:58 +00:00
|
|
|
return exitCode;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
assemblyPass();
|
|
|
|
pass2 = true;
|
|
|
|
assemblyPass();
|
2013-02-19 16:37:14 +00:00
|
|
|
if (getOption('t') && labelTable.length > 0)
|
2005-05-22 12:28:58 +00:00
|
|
|
listLabelTable();
|
|
|
|
} catch (AssemblyError e) {
|
2009-10-17 19:47:06 +00:00
|
|
|
warning(e.msg, true);
|
2005-05-22 12:28:58 +00:00
|
|
|
exitCode = 2;
|
|
|
|
}
|
2013-03-19 10:12:12 +00:00
|
|
|
listingStream.close();
|
|
|
|
objectStream.close();
|
2009-10-17 19:47:06 +00:00
|
|
|
if (exitCode <= 1) {
|
|
|
|
if (!getOption('q')) {
|
2010-12-28 12:44:00 +00:00
|
|
|
writefln("%d lines of source assembled", totalLines);
|
2013-02-19 16:37:14 +00:00
|
|
|
if (objectBytes > 0)
|
2010-12-28 12:44:00 +00:00
|
|
|
writefln("%d bytes written to the object file", objectBytes);
|
2009-10-17 19:47:06 +00:00
|
|
|
}
|
|
|
|
if (getOption('m')) {
|
2014-02-21 22:29:58 +00:00
|
|
|
writef("%s:", makeTarget);
|
|
|
|
foreach (filename; makeSources)
|
|
|
|
writef(" %s", makeEscape(filename));
|
|
|
|
write("\n\txasm");
|
2009-10-17 19:47:06 +00:00
|
|
|
for (int i = 1; i < args.length; i++) {
|
2013-02-19 16:37:14 +00:00
|
|
|
string arg = args[i];
|
2009-10-17 19:47:06 +00:00
|
|
|
if (isOption(arg)) {
|
|
|
|
char letter = arg[1];
|
|
|
|
if (letter >= 'A' && letter <= 'Z')
|
|
|
|
letter += 'a' - 'A';
|
|
|
|
switch (letter) {
|
|
|
|
case 'm':
|
|
|
|
break;
|
|
|
|
case 'o':
|
2013-02-19 16:37:14 +00:00
|
|
|
if (arg[0] == '/')
|
2010-12-28 12:44:00 +00:00
|
|
|
writef(" /%c:$@", arg[1]);
|
2013-02-19 16:37:14 +00:00
|
|
|
else {
|
2010-12-28 12:44:00 +00:00
|
|
|
writef(" -%c $@", arg[1]);
|
2009-10-17 19:47:06 +00:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (arg[0] == '-'
|
|
|
|
&& (letter == 'd' || letter == 'l' || letter == 't')
|
|
|
|
&& i + 1 < args.length && !isOption(args[i + 1])) {
|
2010-12-28 12:44:00 +00:00
|
|
|
writef(" %s %s", arg, makeEscape(args[++i]));
|
2009-10-17 19:47:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-12-28 12:44:00 +00:00
|
|
|
writef(" %s", makeEscape(arg));
|
2009-10-17 19:47:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-19 16:37:14 +00:00
|
|
|
write(" $<");
|
2009-10-17 19:47:06 +00:00
|
|
|
}
|
2010-12-28 12:44:00 +00:00
|
|
|
writeln();
|
2005-05-22 12:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return exitCode;
|
|
|
|
}
|