1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-11-22 01:31:33 +00:00
C02/a02.c

778 lines
25 KiB
C
Raw Normal View History

2019-11-13 07:20:10 +00:00
/* Simple 6502 Assembler *
* for C02 Compiler *
* Uses DASM Syntax but *
* supports 65C02 Op Codes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "a02.h"
2019-11-16 17:18:37 +00:00
#define DEBUG FALSE
int debug; //Ouput Debug Info
2019-11-13 07:20:10 +00:00
#define INCLEN 255
char incpath[INCLEN];
2019-11-13 07:20:10 +00:00
enum otypes {BINFIL, PRGFIL}; //Object File Types
char objtyp; //Object File Type
int orgadr; //Origin Address
int curadr; //Current Address
int lstadr; //List Address
struct sym {int block; char name[MAXLBL+1]; int bytes, value, refrd;};
2019-11-13 07:20:10 +00:00
struct sym symbol; //Current Symbol
2019-11-14 16:15:51 +00:00
struct sym symtbl[MAXSYM]; //Global Symbol Table
2019-11-13 07:20:10 +00:00
int symcnt; //Number of Global Labels
int blkcnt; //Number of Block Labels Generated
2019-11-13 07:20:10 +00:00
int blknum; //Local Label Block Number (0 = Global)
char label[MAXSTR]; //Assembly Line Label
char mnmnc[MAXSTR]; //Opcode Mnemonic
char oprnd[MAXSTR]; //Operand Text
2019-11-13 07:20:10 +00:00
char cmmnt[MAXSTR]; //Assembly Line Comment
char mcode[MAXSTR]; //Generated Bytes
char strng[MAXSTR]; //Parsed String
int opridx; //Index into Operand
char cpdchr; //Last Character Copied into oprnd
2019-11-13 07:20:10 +00:00
unsigned char token, opmod; //OpCode Token, Modifier
unsigned int amode; //Addressing Modes
int zpage, opval; //ZeroPage Flag, Operand Value
char hexadr[6]; //Current Address in Hexadecimal
char bytstr[5]; //String Representation of Byte
char inplin[MAXSTR]; //Input Buffer
char *linptr; //Pointer into Input Buffer
int lineno; //Input File Line Number
2019-11-14 16:15:51 +00:00
int savlno; //Line Number (Saved)
2019-11-13 07:20:10 +00:00
int passno; //Assembler Pass Number (1 or 2)
int endasm; //End Assembly Flag
2019-11-13 07:20:10 +00:00
char prgnam[256]; //Assembler Path and Name (from Command Line)
char inpnam[256]; //Input File Name
char outnam[256]; //Output File Name
char lstnam[256]; //List File Name
2019-11-14 16:15:51 +00:00
char incnam[256]; //Include File Name
2019-11-13 07:20:10 +00:00
FILE *inpfil; //Input File Pointer
FILE *outfil; //Output File Pointer
FILE *lstfil; //List File Pointer
2019-11-14 16:15:51 +00:00
FILE *incfil; //Include File Pointer
2019-11-13 07:20:10 +00:00
2019-11-16 17:18:37 +00:00
/* Print Usage Info and Exit */
void usage(char* appnam) {
2019-11-16 17:22:42 +00:00
printf("Usage: %s [opts] asmfile objfile [lstfile]\n", appnam);
2019-11-16 17:18:37 +00:00
printf(" Opts: -p - Commodore PRG format\n");
printf(" -d - Output Debug Info\n");
exit(EXIT_FAILURE);
}
/* Print Symbol Table */
void prtsym(void) {
fprintf(lstfil, "\n%s Symbol Table\nBlock Name Size Value Rfd\n", "Global");
for (int i=0; i<symcnt; i++) {
int refrd = (symtbl[i].refrd) ? '*' : ' ';
fprintf(lstfil, "%5d %-8s %4d %5d %c \n", symtbl[i].block, symtbl[i].name, symtbl[i].bytes, symtbl[i].value, refrd);
}
}
/* Print Error Dump Info */
void xdump(void) {
puts("Error Dump");
oprnd[opridx]=0; printf("Operand: %s\n", oprnd);
}
/* Print Error Message to Stream */
void xprint(FILE *fp, char* format, char *s) {
if (lineno) fprintf(fp, "%04d: ", lineno);
fprintf(fp, format, s);
}
2019-11-13 07:20:10 +00:00
/* Print Error Message and Exit */
void xerror(char* format, char *s) {
if (debug) xdump();
xprint(stderr, format, s);
if (lstfil) xprint(stderr, format, s);
if (lstfil && symcnt) prtsym();
2019-11-13 07:20:10 +00:00
exit(EXIT_FAILURE);
}
2019-11-14 16:15:51 +00:00
/* Open File with Error Checking */
FILE * opnfil(char* name, char* mode, char* path) {
2019-11-16 17:18:37 +00:00
if (debug) printf("Opening file '%s' with mode '%s'\n", name, mode);
2019-11-14 16:15:51 +00:00
FILE *fp = fopen(name, mode);
if (!fp) {
char spec[256];
char *token = strtok(path, " ");
while (token) {
strcpy(spec, path); strcat(spec, "\\"); strcat(spec, name);
if (debug) printf("Opening file \"%s\" with mode \"%s\"\n", spec, mode);
fp = fopen(spec, mode); if (fp) break;
token = strtok(NULL, " ");
}
}
2019-11-14 16:15:51 +00:00
if (!fp) xerror("Error Opening File '%s'\n", name);
if (debug) printf("File Opened\n");
2019-11-14 16:15:51 +00:00
return fp;
}
2019-11-13 07:20:10 +00:00
/* Skip Character in Input Line *
* Args: c - Character to Skip *
* Updates: linptr */
int skpchr(char c) {
if (*linptr == c) {linptr++; return TRUE;}
else return FALSE;
}
/* Skip Spaces in Input Line *
* Updates: linptr */
void skpspc(void) {
while (*linptr && *linptr <= ' ') linptr++;
}
/* Parse Word from Input Line *
* Args: skip - Skip Spaces Flag *
* *word - Buffer for Word *
* Updates: linptr *
* Returns: Word Found (TRUE/FALSE) */
int pword(int skip, char* word) {
int wrdlen = 0;
if (skip) skpspc();
2019-11-14 16:15:51 +00:00
while (isalnum(*linptr) || *linptr == '_') {
2019-11-13 07:20:10 +00:00
word[wrdlen++] = toupper(*linptr);
linptr++;
}
word[wrdlen] = 0; //Terminate String
if (wrdlen) return TRUE; else return FALSE;
}
struct sym *fndsym(int block, char* name) {
if (debug) printf("Searching for Symbol %s in block %d\n", name, block);
2019-11-13 07:20:10 +00:00
for (int i=0; i < symcnt; i++) {
if (symtbl[i].block != block || strcmp(symtbl[i].name,name)) continue;
if (debug) printf("Found Symbol %s, block %d, size %d, value %04x\n",
symtbl[i].name, symtbl[i].block, symtbl[i].bytes, symtbl[i].value);
2019-11-13 07:20:10 +00:00
return &symtbl[i];
}
return NULL;
}
/* Set Symbol Value and Size */
void setsym(int value, int bytes) {
symbol.value = value;
if (bytes) symbol.bytes = bytes;
else symbol.bytes = (value > 0xFF) ? 2 : 1;
symbol.refrd = FALSE;
if (debug) printf("Set Symbol %s in block %d to value %d, size %d, referred=False\n",
symbol.name, symbol.block, symbol.bytes, symbol.value);
}
/* Add Character to Beginning of String */
void pfxstr(char c, char* s) {
for (int i=strlen(s)+1; i; i--)
s[i] = s[i-1]; //Copy All Characters to the Right
s[0] = c; //Insert Character at Beginning
2019-11-13 07:20:10 +00:00
}
/* Parse Label from Input Line
* Sets: label
* Updates: linptr
* Returns: Label Found (TRUE/FALSE) */
int plabel(void) {
2019-11-16 17:18:37 +00:00
if (debug) puts("Parsing Label");
2019-11-13 07:20:10 +00:00
int block = (skpchr('.')) ? blknum : 0; //Local Label Block Number
int found = pword(FALSE, label); //Parse Word without Skipping Spaces
2019-11-16 17:18:37 +00:00
if (debug) {
2019-11-13 07:20:10 +00:00
if (found) printf("Found Label %s\n", label);
else puts("No Label Found");
}
skpchr(':'); //Skip Optional Label Terminator
if (found && passno == 1) {
if (label[0] && fndsym(block, label)) xerror("Duplicate Label %s Encountered\n", label);
2019-11-16 17:18:37 +00:00
if (debug) printf("Initializing Symbol %s\n", label);
2019-11-13 07:20:10 +00:00
symbol.block = block;
2019-11-14 16:15:51 +00:00
if (strlen(label) > MAXLBL) xerror("Label %s Too Long\n", label);
2019-11-13 07:20:10 +00:00
strcpy(symbol.name, label);
setsym(curadr, 0);
}
if (block) pfxstr('.', label);
skpspc(); //Skip to Mnemonic, Comment, or EOL
2019-11-13 07:20:10 +00:00
return found;
}
/* Copy Character to Operand and Increment */
int cpychr(int c) {
if (c && toupper(*linptr) != c) return FALSE;
cpdchr = *linptr;
if (opridx < MAXSTR) oprnd[opridx++] = toupper(cpdchr);
2019-11-14 16:15:51 +00:00
linptr++;
2019-11-13 07:20:10 +00:00
return TRUE;
}
/* Evaluate Binary Number */
int evlbin() {
int result = 0;
cpychr('%');
while (isdigit(*linptr)) {
if (*linptr > '1') break;
result = (result << 1) + *linptr - '0';
cpychr(0);
}
return result;
}
/* Evaluate Character Constant */
2019-11-13 07:20:10 +00:00
int evlchr() {
int result = 0;
cpychr('\'');
result = *linptr;
cpychr(0);
cpychr('\'');
return result;
}
/* Evaluate Decimal Number */
int evldec() {
int result = 0;
while (isdigit(*linptr)) {
result = result * 10 + *linptr - '0';
cpychr(0);
}
return result;
}
/* Evaluate Hexadecimal Number */
int evlhex() {
int result = 0;
cpychr('$');
while (isxdigit(*linptr)) {
int digit = toupper(*linptr) - '0';
2019-11-13 07:20:10 +00:00
if (digit > 9) digit = digit - 7;
result = (result << 4) + digit;
cpychr(0);
}
return result;
}
/* Evaluate Symbol */
struct sym *evlsym() {
char name[MAXSTR];
int block = (cpychr('.')) ? blknum : 0;
pword(TRUE, name);
2019-11-14 16:15:51 +00:00
for (int i=0; name[i]; i++) if (opridx<MAXSTR) oprnd[opridx++] = name[i];
2019-11-13 07:20:10 +00:00
struct sym *result = fndsym(block, name);
if (passno == 2 && result == NULL) xerror("Undefined Symbol %s\n", name);
if (result) result->refrd = TRUE; //Symbol was Referenced
2019-11-13 07:20:10 +00:00
return result;
}
/* Evaluate Term in Operand */
int evltrm() {
int result;
skpspc();
if (isalpha(*linptr) || *linptr == '.') {
struct sym *target = evlsym();
result = (target) ? target->value : 0x100;
}
else if (isdigit(*linptr))
result = evldec();
else switch(*linptr) {
case '$': result = evlhex(); break;
case '%': result = evlbin(); break;
case '\'': result = evlchr(); break;
default: result = -1;
}
skpspc();
2019-11-16 17:18:37 +00:00
if (debug) printf("Term Evaluated to %d\n", result);
2019-11-13 07:20:10 +00:00
return result;
}
/* Evaluate Operand */
int evlopd(int maxsiz) {
int result = 0;
int hilo = 0; //Return LSB (1) or MSB (2)
int prns; //Optional Parenthesis after Hi/Low Operator
2019-11-16 17:18:37 +00:00
if (debug) puts("Evaluating Operand");
2019-11-13 07:20:10 +00:00
skpspc();
if (cpychr('<')) hilo = 1;
else if (cpychr('>')) hilo = 2;
if (hilo) prns = cpychr('(');
result = evltrm();
if (result >= 0)
while (cpychr('+') || cpychr('-')) {
int opertr = cpdchr;
2019-11-13 07:20:10 +00:00
int opdval = evltrm();
if (opdval < 0) break;
if (opertr == '+') result += opdval;
else result -= opdval;
2019-11-13 07:20:10 +00:00
}
if (hilo) {
if (result < 0) xerror("Hi/Low Operator Requires Operand", "");
if (prns) cpychr(')'); //
switch (hilo) {
case 1: result = result & 0xFF; break; //LSB
case 2: result = result >> 8; break; //MSB
}
}
2019-11-16 17:18:37 +00:00
if (debug) printf("Operand Evaluated to %d\n", result);
2019-11-13 07:20:10 +00:00
if (result > maxsiz) xerror("Operand Value too Large\n", "");
return result;
}
/* Write Byte to Output File */
void outbyt(int b) {
if (curadr > -1) curadr++;
if (passno != 2) return;
fputc(b & 0xFF, outfil);
sprintf(bytstr, "%02X ", b);
2019-11-14 16:15:51 +00:00
if (strlen(mcode) < 9) strcat(mcode, bytstr);
2019-11-13 07:20:10 +00:00
}
/* Write Word to Output File */
void outwrd(int w) {
outbyt(w & 0xff);
outbyt(w >> 8);
}
2019-11-15 19:30:43 +00:00
/* Lookup Opcode */
int lkpopc(struct opc opl[]) {
2019-11-16 17:18:37 +00:00
if (debug) printf("Looking up Mnemonic %s\n", mnmnc);
2019-11-15 19:30:43 +00:00
token = 0xFF; //Set Token to Invalid
char mne[5]; strncpy(mne, mnmnc, 4); mne[4] = 0; //Truncate Mnemonic to Four Characters
for (int i=0; opl[i].name[0]; i++) {
if (strcmp(opl[i].name, mne)) continue;
token = opl[i].token;
amode = opl[i].amode;
2019-11-16 17:18:37 +00:00
if (debug) printf("Found token %02X, amode %04X\n", token, amode);
2019-11-15 19:30:43 +00:00
return TRUE;
}
return FALSE;
}
2019-11-13 07:20:10 +00:00
/* Assemble BYTE Pseudo-Op */
void asmbyt(void) {
2019-11-16 17:18:37 +00:00
if (debug) puts("Assembling BYTE Pseudo-Op");
2019-11-13 07:20:10 +00:00
do {
if (cpychr('"')) { //String Operand
while (!cpychr('"')) {outbyt(*linptr); cpychr(0); }
skpspc();
} else
outbyt(evlopd(0xFF)); //Evaluate Operand
} while (cpychr(','));
}
/* Assemble HEX Pseudo-Op */
void asmhex(void) {
2019-11-16 17:18:37 +00:00
if (debug) puts("Assembling HEX Pseudo-Op");
do {outbyt(evlhex(0xFF)); } while (cpychr(','));
}
2019-11-13 07:20:10 +00:00
/* Assemble WORD Pseudo-Op */
void asmwrd(void) {
do {
outwrd(evlopd(0xFFFF)); //Evaluate Operand
} while (cpychr(','));
}
/* Assemble ALIGN Pseudo-Op */
void asmaln(void) {
2019-11-16 17:18:37 +00:00
if (debug) puts("Assembling ALIGN Pseudo-Op");
int size = evlopd(0xFFFF); if (size < 2) return;
2019-11-16 17:18:37 +00:00
if (debug) printf("Aligning to %d Bytes\n", size);
int fill = size - (curadr % size); if (fill == size) return;
2019-11-16 17:18:37 +00:00
if (debug) printf("Filling %d Bytes\n", fill);
for (int i=0; i<fill; i++) outbyt(0);
}
2019-11-14 16:15:51 +00:00
/* Assemble FILL Pseudo-Op */
void asmfll(void) {
2019-11-16 17:18:37 +00:00
if (debug) puts("Assembling FILL Pseudo-Op");
2019-11-14 16:15:51 +00:00
int size = evlopd(0xFFFF);
2019-11-16 17:18:37 +00:00
if (debug) printf("Filling %d Bytes\n", size);
2019-11-14 16:15:51 +00:00
for (int i=0; i<size; i++) outbyt(0);
}
2019-11-13 07:20:10 +00:00
/* Assemble EQU Pseudo-Op */
void asmequ(void) {
if (label[0] == 0) xerror("EQUate without Label", 0);
setsym(evlopd(0xFFFF), 0);
}
/* Assemble ORG Pseudo-Op */
void asmorg(void) {
orgadr = evlopd(0xFFFF);
if (passno == 1 && symbol.name[0]) {
symbol.value = orgadr;
symbol.bytes = 2;
}
if (passno == 2 && objtyp == PRGFIL)
outwrd(orgadr);
2019-11-14 16:15:51 +00:00
curadr = orgadr;
lstadr = orgadr;
2019-11-13 07:20:10 +00:00
}
/* Assemble PROCESSOR Pseudo-Op */
void asmprc(void) {
skpspc();
while (isalnum(*linptr)) cpychr(0);
2019-11-16 17:18:37 +00:00
if (debug) printf("Ignoring Operand %s\n", oprnd);
2019-11-13 07:20:10 +00:00
}
/* Assemble SUBROUTINE Pseudo-Op */
void asmsub(void) {
blknum = ++blkcnt;
2019-11-13 07:20:10 +00:00
sprintf(oprnd, "%d", blknum); opridx = strlen(oprnd);
2019-11-16 17:18:37 +00:00
if (debug) printf("Block Number set to %s\n", oprnd);
2019-11-13 07:20:10 +00:00
}
/* Assemble ENDSUBROUTINE Pseudo-Op */
void asmens(void) {
blknum = 0;
sprintf(oprnd, "%d", blknum); opridx = strlen(oprnd);
if (debug) printf("Block Number reset to %s\n", oprnd);
}
2019-11-14 16:15:51 +00:00
/* Assemble INCLUDE Pseudo-Op */
void asminf(void) {
int incidx = 0;
2019-11-16 17:18:37 +00:00
if (debug) puts("Assembling INCLUDE Pseudo-Op");
2019-11-14 16:15:51 +00:00
if (incfil) xerror("Nested INCLUDE not Allowed", "");
if (!cpychr('"')) xerror("File Name Must be Quoted", "");
while (*linptr && !cpychr('"')) {
char c = *linptr; if (c == '/') c = '\\'; //Reverse Slashes for DOS/Windows
incnam[incidx++] = c;
cpychr(0);
2019-11-13 07:20:10 +00:00
}
2019-11-14 16:15:51 +00:00
incnam[incidx] = 0; //Terminate Include Name
if (incidx == 0) xerror("INCLUDE requires file name\n", "");
2019-11-16 17:37:51 +00:00
if (debug) printf("Include File Set to Name to '%s'\n", incnam);
2019-11-13 07:20:10 +00:00
}
/* Assemble END Pseudo-Op */
void asmend(void) {
endasm = TRUE;
}
2019-11-13 07:20:10 +00:00
/* Assemble Pseudo-Op */
int asmpso(int dot) {
if (lkpopc(psolst) == FALSE && dot == FALSE) return FALSE;
2019-11-14 16:15:51 +00:00
skpspc();
2019-11-16 17:18:37 +00:00
if (debug) printf("Assembling Pseudo-Op %s, Token '%c'\n", mnmnc, token);
2019-11-13 07:20:10 +00:00
switch (token) {
case '=': asmequ(); break; //EQU
case 'B': asmbyt(); break; //BYTE or DC
case 'H': asmhex(); break; //HEX
2019-11-13 07:20:10 +00:00
case 'W': asmwrd(); break; //WORD
case 'F': asmfll(); break; //FILL or DS
2019-11-14 16:15:51 +00:00
case 'S': asmsub(); break; //SUBRoutine
case 'M': asmens(); break; //ENDSubroutine
2019-11-14 16:15:51 +00:00
case 'I': asminf(); break; //INCLude
2019-11-13 07:20:10 +00:00
case '*': asmorg(); break; //ORG
2019-11-14 16:15:51 +00:00
case 'P': asmprc(); break; //PROCessor
case 'E': asmend(); break; //END
case 'A': asmaln(); break; //ALIGn
2019-11-13 07:20:10 +00:00
default: xerror("Undefined Pseudo-Op %s\n", mnmnc);
}
if (dot) pfxstr('.', mnmnc);
2019-11-13 07:20:10 +00:00
return TRUE;
}
/* Check for Valid Addressing Mode */
int chkmod(int mode) {
2019-11-14 16:15:51 +00:00
char* s = NULL; //Pointer to Addressing Mode Description
for (int i=0; amdesc[i].amode; i++)
if (amdesc[i].amode == mode) {s = amdesc[i].desc; break;}
2019-11-16 17:18:37 +00:00
if (debug) printf("Checking Addressing Mode %s, %04X against %04X\n", s, mode, amode);
2019-11-13 07:20:10 +00:00
if (mode & amode) return TRUE;
2019-11-14 16:15:51 +00:00
xerror("Invalid Addressing Mode %s", s);
2019-11-13 07:20:10 +00:00
}
/* Assemble Branch Opcode */
void asmbrn(int setzp) {
2019-11-13 07:20:10 +00:00
int offset = 0;
int ofsadj = (setzp) ? 2 : 3; //Offset Adjustment
2019-11-16 17:18:37 +00:00
if (debug) printf("Assembling Branch Opcode Token 0x%02X\n", token);
zpage = setzp;
2019-11-13 07:20:10 +00:00
if (isalpha(*linptr) || *linptr =='.') {
struct sym *target = evlsym();
if (target) offset = (target->value - curadr - ofsadj);
2019-11-13 07:20:10 +00:00
}
else if (cpychr('+')) offset = evlopd(0xFF);
else if (cpychr('-')) offset = -evlopd(0xFF);
else {
opval = evlopd(0xFFFF);
if (opval < 0) xerror("Illegal Branch Operand\n", "");
offset = opval - curadr - 2;
}
if (debug) printf("Calculated Branch Offset of %d\n", offset);
2019-11-13 07:20:10 +00:00
if ((offset > 127 || offset < -128) && passno == 2)
xerror("Branch Out of Range\n", "");
2019-11-16 17:18:37 +00:00
if (debug) printf("Branch Offset %d\n", offset);
2019-11-13 07:20:10 +00:00
opval = offset & 0xFF;
}
/* Assemble Zero Page, Relative Opcode */
void asmzpr(void) {
int bitno = -1;
if (debug) printf("Assembling ZeroPage (Relative) Opcode Token 0x%02X\n", token);
if (strlen(mnmnc) < 4) {opmod = evlopd(7) << 4; cpychr(','); skpspc();} //Set Modifier to Bit Position
int zpval = evlopd(0xFF); cpychr(','); skpspc();//Get ZeroPage Operand
if (zpval < 0) xerror ("Instruction %s requires Multiple Operands\n", mnmnc);
if (amode == 0x0004) {zpage = TRUE; opval = zpval;} //RMB, SMB - Zero Page Operand
else {asmbrn(FALSE); opval = opval << 8 | zpval;} //BBR, BBS - Combine Operanda
}
2019-11-13 07:20:10 +00:00
/* Assemble Immediate Mode Instruction */
void asmimd(void) {
2019-11-16 17:18:37 +00:00
if (debug) printf("Assembling Immediate Opcode Token 0x%02X\n", token);
2019-11-13 07:20:10 +00:00
opval = evlopd(0xFF);
zpage = TRUE;
opmod = 0x08; //Immediate
}
/* Assemble Indirect Mode Instruction */
void asmind(void) {
2019-11-16 17:18:37 +00:00
if (debug) puts("Assembling Indirect Mode Instruction");
2019-11-13 07:20:10 +00:00
zpage = TRUE; opval = evlopd(0xFFFF);
if (cpychr(',') && cpychr('X') && chkmod(INDCX)) cpychr(')'); ////(Indirect,X) opmod=0
else if (cpychr(')')) {
if (cpychr(',') && cpychr('Y') && chkmod(INDCY)) opmod = 0x10; //(Indirect),Y
else if (chkmod(INDCT)) opmod = 0x11; //(Indirect)
if (token == 0x4C) zpage = FALSE; //JMP (Indirect Absolute)
}
else chkmod(0); //Illegal Addressing Mode
if (zpage && opval > 0x00FF) xerror("Operand Value too Large\n", "");
}
/* Assemble Implied/Accumulator/Absolute/ZeroPage Mode Instruction */
void asmiaz(void) {
opval = evlopd(0xFFFF);
if (opval < 0) {
if (amode != IMPLD) //Implied
if (chkmod(ACMLT)) opmod = 0x08; //Accumulator
return;
}
2019-11-16 17:18:37 +00:00
if (debug) printf("Assembling Absolute/ZeroPage 0x%02X\n", token);
2019-11-13 07:20:10 +00:00
zpage = (opval <= 0xff) ? TRUE : FALSE;
if (zpage && chkmod(ZPAGE)) opmod = 0x04; //ZeroPage
else if (chkmod(ABSLT)) opmod = 0x0C; //Absolute
if (cpychr(',')) {
if (cpychr('X')) {
if (zpage && chkmod(ZPAGX)) opmod = 0x14; //ZeroPage,X
else if (chkmod(ABSLX)) opmod = 0x1C; //Absolute,X
} else if (cpychr('Y')) {
if (zpage && (token == 0x82 || token == 0xA2))
opmod = 0x14; //ZeroPage,Y
else {zpage = FALSE; opmod = 0x18;} //Absolute,Y
} else chkmod(0); //Illegal Addressing Mode
}
}
/* Fix Opcode (if needed) */
unsigned char fixopc(void) {
2019-11-16 17:18:37 +00:00
if (debug) printf("Fixing OpCode $%02X+$%02X\n", token, opmod);
2019-11-13 07:20:10 +00:00
for (int i=0; opfix[i].token; i++)
if (opfix[i].token == token && opfix[i].opmod == opmod)
return opfix[i].opcode;
return token + opmod;
}
2019-11-16 17:18:37 +00:00
/* Ouput Opcode debug Info */
2019-11-14 16:15:51 +00:00
void dbgopc(void) {
printf("token=$%02X, opmod=$%02X, Address Mode: ", token, opmod);
if (amode == 0x1004) puts("ZeroPage, Relative");
else if (amode == 0x0004) puts("ZeroPage");
else switch (opmod) {
2019-11-14 16:15:51 +00:00
case 0x00: if (amode == IMPLD) puts("Implied"); else puts("(Indirect,X)"); break;
case 0x08: if (opval < 0) puts("Accumulator"); else puts("#Immediate"); break;
case 0x10: puts("(Indirect),Y"); break;
case 0x11: puts("(Indirect)"); break;
case 0x04: puts("ZeroPage"); break;
case 0x0C: puts("Absolute"); break;
2019-11-15 19:30:43 +00:00
case 0x14: if ((token == 0x82 || token == 0xA2)) puts("ZeroPage,Y");
else puts("ZeroPage,X"); break;
2019-11-14 16:15:51 +00:00
case 0x1C: puts("Absolute,X"); break;
case 0x18: puts("Absolute,Y"); break;
default: puts("UNKOWN");
}
if (opval < 0) puts("No Operand");
else {
printf("Operand Value %d, ", opval);
if (zpage) puts("Zero Page"); else puts("Absolute");
}
}
2019-11-13 07:20:10 +00:00
/* Assemble Opcode */
int asmopc(int dot) {
2019-11-13 07:20:10 +00:00
opmod = 0;
if (asmpso(dot)) return TRUE; //Check For/Assemble Pseudo-Op
2019-11-13 07:20:10 +00:00
if (lkpopc(opclst) == FALSE) xerror("Invalid Mnemonic %s\n", mnmnc);
2019-11-16 17:18:37 +00:00
if (debug) printf("Assembling Opcode Token 0x%02X, ", token);
if (debug) printf("Addressing Mode Mask 0x%04X\n", amode);
2019-11-13 07:20:10 +00:00
skpspc();
if (amode == RELTV) asmbrn(TRUE); //Branch (Relative) Instruction
else if (amode == 0x0004 || amode == 0x1004) asmzpr(); //Branch (Relative) Instruction
2019-11-13 07:20:10 +00:00
else if (cpychr('#')) asmimd(); //Assemble Implied Instruction
else if (cpychr('(')) asmind(); //Assemble Indirect Instruction
else asmiaz(); //Assemble Implied/Accumulator/Absolute/ZeroPage Instruction
2019-11-16 17:18:37 +00:00
if (debug) dbgopc();
int opcode = fixopc();
2019-11-16 17:18:37 +00:00
if (debug) printf("Writing OpCode $%02X\n", opcode);
2019-11-13 07:20:10 +00:00
outbyt(opcode);
if (debug) printf("Writing %s Operand %d\n", zpgabs[-zpage], opval);
2019-11-13 07:20:10 +00:00
if (opval >= 0) {
if (zpage) outbyt(opval); //Byte Operand
else outwrd(opval); //Word Operand
2019-11-13 07:20:10 +00:00
}
return TRUE;
}
/* Parse Opcode Mnemonic from Input Line
* Sets: mnmnc
* Updates: linptr
* Returns: Label Found (TRUE/FALSE) */
int pmnmnc(void) {
2019-11-16 17:18:37 +00:00
if (debug) puts("Parsing Mnemonic");
int dot = cpychr('.'); //Optional Dot before Pseudo-Op
2019-11-13 07:20:10 +00:00
int found = pword(TRUE, mnmnc);
opridx = 0; //Initialize Operand Index
if (found) asmopc(dot);
2019-11-13 07:20:10 +00:00
oprnd[opridx] = 0; //Terminate Operand String
return found;
}
/* Parse Comment from Input Line
* Sets: cmmnt
* Updates: linptr */
void pcmmnt(void) {
skpspc();
int i = 0;
while (*linptr >= ' ') cmmnt[i++] = *linptr++;
cmmnt[i] = 0; //Terminate Comment
2019-11-16 17:18:37 +00:00
if (debug) {if (i) printf("Comment: %s\n", cmmnt); else puts("No Comment Found");}
2019-11-13 07:20:10 +00:00
}
/* Add Label to Symbol Table */
void addsym() {
if (symbol.value<0) xerror("Origin Not Set", "");
memcpy(&symtbl[symcnt++], &symbol, sizeof(symbol));
if (debug) printf("Added symbol %s in block %d to Symbol Table\n", symbol.name, symbol.block);
2019-11-13 07:20:10 +00:00
}
2019-11-14 16:15:51 +00:00
/* Open Include File */
void opninc(void) {
2019-11-16 17:18:37 +00:00
if (debug) printf("Opening Include File %s\n", incnam);
2019-11-14 16:15:51 +00:00
if (lstfil) fputs("\n", lstfil);
incfil = opnfil(incnam, "r", incpath);
2019-11-14 16:15:51 +00:00
savlno = lineno;
lineno = 1;
}
/* Close Include File */
void clsinc(void) {
2019-11-16 17:18:37 +00:00
if (debug) printf("Closing Include File %s\n", incnam);
2019-11-14 16:15:51 +00:00
if (lstfil) fputs("\n", lstfil);
fclose(incfil);
incfil = NULL;
incnam[0] = 0;
lineno = savlno;
endasm = FALSE; //Clear End Flag for Return to Main File
2019-11-14 16:15:51 +00:00
}
2019-11-13 07:20:10 +00:00
/* Assemble Input File (Two Pass) *
* Args: pass - Assembly Pass (1 or 2) *
* Requires: inpfil - Input File Pointer *
* Uses: inplin - Input Line Buffer *
* lineno - Input File Line Number */
void asmfil(int pass) {
endasm = FALSE; //Reset End Assembly Flag
passno = pass; //Assembly Pass Number
2019-11-16 17:18:37 +00:00
if (debug) printf("Assembling Pass %d\n", pass);
2019-11-13 07:20:10 +00:00
lineno = 1; //Initialize Input File Line Number
blkcnt = 0; //Initialize Local Block Count
blknum = 0; // and Local Block Number
2019-11-13 07:20:10 +00:00
orgadr = -1; //Origin Address Not Set
curadr = orgadr; //Set Current Address to Origin
2019-11-16 17:37:51 +00:00
if (debug) printf("Rewinding Input File\n");
2019-11-13 07:20:10 +00:00
rewind(inpfil); //Start at Beginning of Input File
2019-11-14 16:15:51 +00:00
while (TRUE) {
if (incfil) linptr = fgets(inplin, MAXSTR, incfil);
else linptr = fgets(inplin, MAXSTR, inpfil);
if (endasm || linptr == NULL) {if (incfil) {clsinc(); continue;} else break;}
if (debug) printf("%1d %05d %04X: %s", passno, lineno, curadr, inplin);
2019-11-13 07:20:10 +00:00
lstadr = curadr; //Set List Address
mcode[0] = 0; //Clear Generated Macbine Code
plabel(); //Parse Label
pmnmnc(); //Parse Mnemonic
pcmmnt(); //Parse Comment
if (passno == 1 && label[0]) addsym(); //Add Label to Table
if (passno == 2) {
if (lstadr < 0) hexadr[0] = 0; else sprintf(hexadr, "%04X", lstadr);
fprintf(lstfil, "%05d %-4s %-9s%-7s %-5s %-16s %s\n", lineno, hexadr, mcode, label, mnmnc, oprnd, cmmnt );
2019-11-14 16:15:51 +00:00
fflush(lstfil); //Flush Output Buffer in case of Segmentation Fault
2019-11-13 07:20:10 +00:00
}
lineno++;
2019-11-14 16:15:51 +00:00
if (incnam[0] && incfil == NULL) opninc(); //Open Include File
2019-11-13 07:20:10 +00:00
}
}
/* Parse Command Line Option */
int pcoptn(char *argval) {
if (argval[0] != '-') return FALSE;
char option = toupper(argval[1]);
2019-11-16 17:18:37 +00:00
if (debug) printf(" Option '%c'\n", option);
2019-11-13 07:20:10 +00:00
switch(option) {
2019-11-16 17:18:37 +00:00
case 'D': debug = TRUE; break; //Enable debug Output
2019-11-13 07:20:10 +00:00
case 'P': objtyp = PRGFIL; break; //Commodore PRG File
default: xerror("Illegal Command Line Option %s\n", argval);
}
return TRUE;
}
/* Parse Command Line Arguments */
void pcargs(int argc, char *argv[]) {
int argnum = 0;
for (int arg = 0; arg<argc; arg++) {
2019-11-16 17:18:37 +00:00
if (debug) printf("Arg %d='%s'\n", arg, argv[arg]);
2019-11-13 07:20:10 +00:00
if (arg == 0) {strcpy(prgnam, argv[arg]); continue;}
if (strcmp(argv[arg], "-i") ==0) {
strcpy(incpath, argv[++arg]);
if (debug) printf("Include Path set to \"%s\"\n", incpath);
continue;
}
2019-11-13 07:20:10 +00:00
if (pcoptn(argv[arg])) continue;
switch (argnum++) {
case 0: strcpy(inpnam, argv[arg]); break;
case 1: strcpy(outnam, argv[arg]); break;
case 2: strcpy(lstnam, argv[arg]); break;
default: xerror("Too Many Arguments\n", "");
}
}
2019-11-16 17:18:37 +00:00
if (argnum<2) usage(argv[0]);
2019-11-13 07:20:10 +00:00
}
int main(int argc, char *argv[]) {
debug = DEBUG; //Initialize Debug Flag
incpath[0] = 0; //Initialize Include File Path
lstnam[0] = 0; lstfil = NULL; //Default to No List File
incnam[0] = 0; incfil = NULL; //Include File Not Opened
lineno = 0; //No Line Number (yet)
objtyp = BINFIL; //Default Object File Type to Binary
pcargs(argc, argv); //Parse Command Line Arguments
inpfil = opnfil(inpnam, "r", ""); //Open Input File
outfil = opnfil(outnam, "wb", ""); //Open Output File
if (lstnam[0]) //If List File Name Specified
lstfil = opnfil(lstnam, "w", ""); // Open List File
asmfil(1); //Assemble Input File (First Pass)
asmfil(2); //Assemble Input File (First Pass)
if (lstfil && symcnt) prtsym(); //Print Symbol Table
exit(0); //Exit with No Errors
2019-11-13 07:20:10 +00:00
}