1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-06-28 19:29:39 +00:00

Converted source code line endings to CR/LF

This commit is contained in:
Curtis F Kaylor 2019-10-27 22:44:13 -04:00
parent 6703c8cb10
commit 3a7224ce9a
16 changed files with 648 additions and 629 deletions

Binary file not shown.

Binary file not shown.

19
src/.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/c02.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true
}
]
}

View File

@ -1,36 +1,36 @@
/************************************* /*************************************
* C02 Assembly Language Routines * * C02 Assembly Language Routines *
*************************************/ *************************************/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "common.h" #include "common.h"
#include "files.h" #include "files.h"
#include "asm.h" #include "asm.h"
/* Process comment */ /* Process comment */
void prccmt(void) { void prccmt(void) {
if (strlen(cmtasm)) { strcpy(asmcmt, ";"); strcat(asmcmt, cmtasm); } if (strlen(cmtasm)) { strcpy(asmcmt, ";"); strcat(asmcmt, cmtasm); }
else asmcmt[0] = 0; else asmcmt[0] = 0;
setcmt(""); setcmt("");
} }
/* output a single line of assembly code */ /* output a single line of assembly code */
void asmlin(char *opcode, char *oprnd) { void asmlin(char *opcode, char *oprnd) {
if (strlen(lblasm)) strcat(lblasm, LABSFX); if (strlen(lblasm)) strcat(lblasm, LABSFX);
prccmt(); prccmt();
fprintf(outfil, ASMFMT, lblasm, opcode, oprnd, asmcmt); fprintf(outfil, ASMFMT, lblasm, opcode, oprnd, asmcmt);
if (debug) printf(ASMFMT, lblasm, opcode, oprnd, asmcmt); if (debug) printf(ASMFMT, lblasm, opcode, oprnd, asmcmt);
lblasm[0] = 0; lblasm[0] = 0;
} }
/* output a single comment line */ /* output a single comment line */
void cmtlin(void) { void cmtlin(void) {
DEBUG("Writing Comment Line: %s\n", cmtasm) DEBUG("Writing Comment Line: %s\n", cmtasm)
fprintf(outfil, "; %s\n", cmtasm); fprintf(outfil, "; %s\n", cmtasm);
setcmt(""); setcmt("");
} }

View File

@ -1,9 +1,9 @@
/************************************* /*************************************
* C02 Assembly Language Routines * * C02 Assembly Language Routines *
*************************************/ *************************************/
char lblasm[LBLLEN+2]; //Label to emit on next asm line char lblasm[LBLLEN+2]; //Label to emit on next asm line
void asmlin(char *opcode, char *oprnd); //Output a line of assembly code void asmlin(char *opcode, char *oprnd); //Output a line of assembly code
void cmtlin(); //Output a comment lines void cmtlin(); //Output a comment lines
void prccmt(); //Process comment void prccmt(); //Process comment

View File

@ -1,60 +1,60 @@
/************************************* /*************************************
* C02 Common Definitions & Routines * * C02 Common Definitions & Routines *
*************************************/ *************************************/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <time.h> #include <time.h>
#include "common.h" #include "common.h"
struct timespec curtim; //Current Time struct timespec curtim; //Current Time
/* Error - Print Input File name & position and exit */ /* Error - Print Input File name & position and exit */
void exterr(int errnum) { void exterr(int errnum) {
fprintf(stderr, "Line %d Column %d of File %s\n", curlin, curcol, inpnam); fprintf(stderr, "Line %d Column %d of File %s\n", curlin, curcol, inpnam);
exit(errnum); exit(errnum);
} }
/* Error - print "Expected" error message * /* Error - print "Expected" error message *
and exit with general failure code * and exit with general failure code *
Args: expected - Description of what was expected */ Args: expected - Description of what was expected */
void expctd(char *expstr) { void expctd(char *expstr) {
fprintf(stderr, "Expected %s, but found '%c'\n", expstr, nxtchr); fprintf(stderr, "Expected %s, but found '%c'\n", expstr, nxtchr);
exterr(EXIT_FAILURE); exterr(EXIT_FAILURE);
} }
/* Print current position in file */ /* Print current position in file */
void prtpos(void) { if (inpnam[0]) printf("(%s: %d,%d) ", inpnam, curlin, curcol); } void prtpos(void) { if (inpnam[0]) printf("(%s: %d,%d) ", inpnam, curlin, curcol); }
/* Initialize elapsed time counter */ /* Initialize elapsed time counter */
void initim(void) { void initim(void) {
timespec_get (&curtim, TIME_UTC); timespec_get (&curtim, TIME_UTC);
bgntim = curtim.tv_sec; bgntim = curtim.tv_sec;
} }
/* Print elapsed time */ /* Print elapsed time */
void prttim(void) { void prttim(void) {
timespec_get (&curtim, TIME_UTC); timespec_get (&curtim, TIME_UTC);
printf("[%d", curtim.tv_sec - bgntim); printf("[%d", curtim.tv_sec - bgntim);
printf(".%06d]",curtim.tv_nsec/1000); printf(".%06d]",curtim.tv_nsec/1000);
} }
/* Set comment to string */ /* Set comment to string */
void setcmt(char *s) { strcpy(cmtasm, s); } void setcmt(char *s) { strcpy(cmtasm, s); }
/* Append string to comment */ /* Append string to comment */
void addcmt(char *s) { void addcmt(char *s) {
if (strlen(cmtasm)+strlen(s)<73) strcat(cmtasm, s); if (strlen(cmtasm)+strlen(s)<73) strcat(cmtasm, s);
} }
/* Append character to comment */ /* Append character to comment */
void chrcmt(char c) { void chrcmt(char c) {
if (strlen(cmtasm)>72) return; if (strlen(cmtasm)>72) return;
if (cmtasm[0] == 0 && c == ' ') return; if (cmtasm[0] == 0 && c == ' ') return;
int i = strlen(cmtasm); int i = strlen(cmtasm);
cmtasm[i++] = c; cmtasm[i++] = c;
cmtasm[i] = 0; cmtasm[i] = 0;
} }

View File

@ -1,138 +1,138 @@
/************************************ /************************************
* C02 Conditional Parsing Routines * * C02 Conditional Parsing Routines *
************************************/ ************************************/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "common.h" #include "common.h"
#include "asm.h" #include "asm.h"
#include "parse.h" #include "parse.h"
#include "vars.h" #include "vars.h"
#include "expr.h" #include "expr.h"
#include "label.h" #include "label.h"
#include "cond.h" #include "cond.h"
int cmprtr; //Encoded Comparison Operator int cmprtr; //Encoded Comparison Operator
int cmpenc; //Encoded Comparator Character int cmpenc; //Encoded Comparator Character
/* Encode Comparison Operator Character * /* Encode Comparison Operator Character *
* Args: Comparison Operator Character * * Args: Comparison Operator Character *
* Returns: Comparison Operator Bit Mask */ * Returns: Comparison Operator Bit Mask */
int enccmp(char c) { int enccmp(char c) {
int e; int e;
DEBUG("Encoding Comparison Character '%c'", c) DEBUG("Encoding Comparison Character '%c'", c)
switch(c) { switch(c) {
case '=': e = 1; break; case '=': e = 1; break;
case '<': e = 2; break; case '<': e = 2; break;
case '>': e = 4; break; case '>': e = 4; break;
default: e = 0; default: e = 0;
} }
if (e) { CCMNT(c); skpchr(); } if (e) { CCMNT(c); skpchr(); }
DETAIL(", encoded as %d\n", e); DETAIL(", encoded as %d\n", e);
return e; return e;
} }
/* Process and Compile Comparison Operator and * /* Process and Compile Comparison Operator and *
* Args: comparator - Encoded Comparison Operator * * Args: comparator - Encoded Comparison Operator *
* Uses: term - Term Being Compared Against * * Uses: term - Term Being Compared Against *
* label - Branch Target if Comparison is FALSE */ * label - Branch Target if Comparison is FALSE */
void prccmp(void) { void prccmp(void) {
DEBUG("Processing comparator %d", cmprtr) DETAIL(" with REVCMP=%d\n", revcmp) DEBUG("Processing comparator %d", cmprtr) DETAIL(" with REVCMP=%d\n", revcmp)
if (cmprtr > 7) { //Process Flag if (cmprtr > 7) { //Process Flag
cmprtr = (cmprtr ^ revcmp) & 1; //Apply Reversal cmprtr = (cmprtr ^ revcmp) & 1; //Apply Reversal
if (cmprtr) asmlin("BPL", cmplbl); if (cmprtr) asmlin("BPL", cmplbl);
else asmlin("BMI", cmplbl); else asmlin("BMI", cmplbl);
return; return;
} }
cmprtr = (cmprtr ^ revcmp) & 7; //Apply reversal cmprtr = (cmprtr ^ revcmp) & 7; //Apply reversal
switch(cmprtr) { switch(cmprtr) {
case 0: // Raw Expression (Skip) case 0: // Raw Expression (Skip)
asmlin("BEQ", cmplbl); break; asmlin("BEQ", cmplbl); break;
case 1: // = or == case 1: // = or ==
asmlin("CMP", term); asmlin("BNE", cmplbl); break; asmlin("CMP", term); asmlin("BNE", cmplbl); break;
case 2: // < case 2: // <
asmlin("CMP", term); asmlin("BCS", cmplbl); break; asmlin("CMP", term); asmlin("BCS", cmplbl); break;
case 3: // <= or =< case 3: // <= or =<
asmlin("CLC", ""); asmlin("SBC", term); asmlin("BCS", cmplbl); break; asmlin("CLC", ""); asmlin("SBC", term); asmlin("BCS", cmplbl); break;
case 4: // > case 4: // >
asmlin("CLC", ""); asmlin("SBC", term); asmlin("BCC", cmplbl); break; asmlin("CLC", ""); asmlin("SBC", term); asmlin("BCC", cmplbl); break;
case 5: // >= or => case 5: // >= or =>
asmlin("CMP", term); asmlin("BCC", cmplbl); break; asmlin("CMP", term); asmlin("BCC", cmplbl); break;
case 6: // <> or >< case 6: // <> or ><
asmlin("CMP", term); asmlin("BEQ", cmplbl); break; asmlin("CMP", term); asmlin("BEQ", cmplbl); break;
case 7: // Raw Expression (Normal) case 7: // Raw Expression (Normal)
asmlin("BNE", cmplbl); break; asmlin("BNE", cmplbl); break;
default: default:
ERROR("Unsupported comparison operator index %d\n", cmprtr, EXIT_FAILURE) ERROR("Unsupported comparison operator index %d\n", cmprtr, EXIT_FAILURE)
} }
} }
/* Parse Comparison */ /* Parse Comparison */
void prscmp(int revrse) { void prscmp(int revrse) {
skpspc(); skpspc();
cmpenc = enccmp(nxtchr); //Encode Comparison Character cmpenc = enccmp(nxtchr); //Encode Comparison Character
cmprtr = cmpenc; //Set Encoded Comparator cmprtr = cmpenc; //Set Encoded Comparator
if (cmprtr) { if (cmprtr) {
cmpenc = enccmp(nxtchr); //Encode Next Comparison Character cmpenc = enccmp(nxtchr); //Encode Next Comparison Character
if (cmpenc != 0) cmprtr = cmprtr | cmpenc; //Combine Encoded Comparator if (cmpenc != 0) cmprtr = cmprtr | cmpenc; //Combine Encoded Comparator
} }
skpspc(); skpspc();
if (cmprtr) prstrm(FALSE); if (cmprtr) prstrm(FALSE);
//prccmp(); - Do after check for logical operator //prccmp(); - Do after check for logical operator
DEBUG("Parsed comparator %d\n", cmprtr) DEBUG("Parsed comparator %d\n", cmprtr)
} }
/* Parse Flag Operator */ /* Parse Flag Operator */
void prsflg(int revrse) { void prsflg(int revrse) {
DEBUG("Parsing Flag Operator '%c'\n", nxtchr) DEBUG("Parsing Flag Operator '%c'\n", nxtchr)
if (match('+')) cmprtr = 8; //Bit 0 = 0 if (match('+')) cmprtr = 8; //Bit 0 = 0
else if (match('-')) cmprtr = 9; //Bit 1 = 1 else if (match('-')) cmprtr = 9; //Bit 1 = 1
else expctd("Flag operator"); else expctd("Flag operator");
skpchr(); skpchr();
} }
/* Parse Logical Operator * /* Parse Logical Operator *
* Sets: logops */ * Sets: logops */
void prslop(void) { void prslop(void) {
DEBUG("Checking for Logical Operator\n", 0) DEBUG("Checking for Logical Operator\n", 0)
logopr = LOPNONE; logopr = LOPNONE;
skpspc(); skpspc();
if (isalph()) { if (isalph()) {
getwrd(); //Get Logical Operator getwrd(); //Get Logical Operator
DEBUG("Parsing Logical Operator %s\n", word) DEBUG("Parsing Logical Operator %s\n", word)
if (wordis("AND")) logopr = LOPAND; if (wordis("AND")) logopr = LOPAND;
else if (wordis("OR")) logopr = LOPOR; else if (wordis("OR")) logopr = LOPOR;
else ERROR("Encountered invalid token \"%s\"\n", word, EXIT_FAILURE) else ERROR("Encountered invalid token \"%s\"\n", word, EXIT_FAILURE)
} }
DEBUG("Set LOGOPR to %d\n", logopr) DEBUG("Set LOGOPR to %d\n", logopr)
} }
/* Parse and Compile Conditional Expression * /* Parse and Compile Conditional Expression *
* Condition = <expression> <comparator> <term> */ * Condition = <expression> <comparator> <term> */
void prscnd(char trmntr, int revrse) { void prscnd(char trmntr, int revrse) {
DEBUG("Parsing condition with REVRSE=%d\n", revrse) DEBUG("Parsing condition with REVRSE=%d\n", revrse)
tmplbl[0] = 0; tmplbl[0] = 0;
do { do {
strcpy(cmplbl, cndlbl); DEBUG("Set CMPLBL to \"%s\"\n", cmplbl); strcpy(cmplbl, cndlbl); DEBUG("Set CMPLBL to \"%s\"\n", cmplbl);
revcmp = revrse; revcmp = revrse;
if (look('!')) revcmp = (revcmp) ? FALSE: TRUE; if (look('!')) revcmp = (revcmp) ? FALSE: TRUE;
DEBUG("Set REVCMP to %d\n", revcmp) DEBUG("Set REVCMP to %d\n", revcmp)
if (!look('.')) prsxpr(0); if (!look('.')) prsxpr(0);
if (look(':')) prsflg(revcmp); //Parse Flag Operator if (look(':')) prsflg(revcmp); //Parse Flag Operator
else prscmp(revcmp); //Parse Comparison Operator else prscmp(revcmp); //Parse Comparison Operator
prslop(); //Parse Logical Operator prslop(); //Parse Logical Operator
if (logopr == LOPOR) { if (logopr == LOPOR) {
revcmp = (revcmp) ? FALSE: TRUE; revcmp = (revcmp) ? FALSE: TRUE;
DEBUG("Set REVCMP to %d\n", revcmp) DEBUG("Set REVCMP to %d\n", revcmp)
} }
if (logopr && revcmp) { if (logopr && revcmp) {
if (!tmplbl[0]) newlbl(tmplbl); if (!tmplbl[0]) newlbl(tmplbl);
strcpy(cmplbl, tmplbl); DEBUG("Set CMPLBL to \"%s\"\n", cmplbl); strcpy(cmplbl, tmplbl); DEBUG("Set CMPLBL to \"%s\"\n", cmplbl);
} }
prccmp(); //Process Comparison/Flag Operator prccmp(); //Process Comparison/Flag Operator
} while (logopr); } while (logopr);
if (tmplbl[0]) setlbl(tmplbl); if (tmplbl[0]) setlbl(tmplbl);
expect(trmntr); expect(trmntr);
} }

View File

@ -1,10 +1,10 @@
/************************************ /************************************
* C02 Conditional Parsing Routines * * C02 Conditional Parsing Routines *
************************************/ ************************************/
enum LOGOPS {LOPNONE, LOPAND, LOPOR}; enum LOGOPS {LOPNONE, LOPAND, LOPOR};
int revcmp; //Reverse Comparison int revcmp; //Reverse Comparison
int logopr; //Logical Operator (set to LOGOPS) int logopr; //Logical Operator (set to LOGOPS)
void prscnd(char trmntr, int revrse); //Parse Conditional Expression void prscnd(char trmntr, int revrse); //Parse Conditional Expression

View File

@ -1,17 +1,17 @@
/************************************ /************************************
* C02 Declaration Compiling Routines * * C02 Declaration Compiling Routines *
************************************/ ************************************/
char fncnam[VARLEN+1]; //Function Name char fncnam[VARLEN+1]; //Function Name
char prmtra[VARLEN+1]; //Function Parameter A char prmtra[VARLEN+1]; //Function Parameter A
char prmtrx[VARLEN+1]; //Function Parameter X char prmtrx[VARLEN+1]; //Function Parameter X
char prmtry[VARLEN+3]; //Function Parameter Y char prmtry[VARLEN+3]; //Function Parameter Y
int prmcnt; //Number of Parameters int prmcnt; //Number of Parameters
//int lpemtd; //Location Prefix Emitted //int lpemtd; //Location Prefix Emitted
void addcon(int numval); //Add Constant void addcon(int numval); //Add Constant
int pmodfr(); //Check for and Parse Modifier int pmodfr(); //Check for and Parse Modifier
int ctype(int reqtyp); //Check for Type Keyword int ctype(int reqtyp); //Check for Type Keyword
int ptype(int m); //Check for and Parse Type Keyword int ptype(int m); //Check for and Parse Type Keyword
enum types {TNONE, TVOID, TENUM, TBITMASK,TCHAR, TINT, TSTRUCT}; enum types {TNONE, TVOID, TENUM, TBITMASK,TCHAR, TINT, TSTRUCT};

View File

@ -1,102 +1,102 @@
/****************************** /******************************
* C02 File Handling Routines * * C02 File Handling Routines *
******************************/ ******************************/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "common.h" #include "common.h"
#include "files.h" #include "files.h"
/* Error - Print textual description of system error * /* Error - Print textual description of system error *
* and exit with system error code */ * and exit with system error code */
void extsys(char *s) { void extsys(char *s) {
perror(s); perror(s);
exterr(errno); exterr(errno);
} }
/* Open Source File * /* Open Source File *
* Uses: srcnam - Source File Name * * Uses: srcnam - Source File Name *
* Sets: srcfil - Source File Handle */ * Sets: srcfil - Source File Handle */
void opnsrc(void) { void opnsrc(void) {
DEBUG("Processing Source File Name '%s'\n", srcnam) DEBUG("Processing Source File Name '%s'\n", srcnam)
if (strrchr(srcnam, '.') == NULL) strcat(srcnam, ".c02"); //if no extension. add ".c02" if (strrchr(srcnam, '.') == NULL) strcat(srcnam, ".c02"); //if no extension. add ".c02"
DEBUG("opening Source File '%s'\n", srcnam) DEBUG("opening Source File '%s'\n", srcnam)
srcfil = fopen(srcnam, "r"); //open file srcfil = fopen(srcnam, "r"); //open file
if (srcfil == NULL) extsys(srcnam); if (srcfil == NULL) extsys(srcnam);
} }
/* Close Source File */ /* Close Source File */
void clssrc(void) { fclose(srcfil); } void clssrc(void) { fclose(srcfil); }
/* Open Output File * /* Open Output File *
* Uses: outnam - Output File Name * * Uses: outnam - Output File Name *
* Sets: outfil - Output File Handle */ * Sets: outfil - Output File Handle */
void opnout(void) { void opnout(void) {
DEBUG("Processing Output File Name '%s'\n", outnam) DEBUG("Processing Output File Name '%s'\n", outnam)
if (strlen(outnam) == 0) //if Output File not specified if (strlen(outnam) == 0) //if Output File not specified
{ {
strcpy(outnam, srcnam); //copy Source Name to Ouput Name strcpy(outnam, srcnam); //copy Source Name to Ouput Name
char *dot = strrchr(outnam, '.'); //find extension char *dot = strrchr(outnam, '.'); //find extension
if (dot != NULL) *dot = 0; //and remove it if (dot != NULL) *dot = 0; //and remove it
DEBUG("Set Output File Name to '%s'\n", outnam) DEBUG("Set Output File Name to '%s'\n", outnam)
} }
if (strrchr(outnam, '.') == NULL) strcat(outnam, ".asm"); //if no extension, add ".asm" if (strrchr(outnam, '.') == NULL) strcat(outnam, ".asm"); //if no extension, add ".asm"
DEBUG("Opening Output File '%s'\n", outnam) DEBUG("Opening Output File '%s'\n", outnam)
outfil = fopen(outnam, "w"); //open file outfil = fopen(outnam, "w"); //open file
if (outfil == NULL) extsys(outnam); if (outfil == NULL) extsys(outnam);
} }
/* Close Output File */ /* Close Output File */
void clsout(void) { void clsout(void) {
fprintf(outfil, "\n"); fprintf(outfil, "\n");
fclose(outfil); fclose(outfil);
} }
/* Open Log File * /* Open Log File *
* Uses: srcnam - Source File Name * * Uses: srcnam - Source File Name *
* Sets: logfil - Log File Handle */ * Sets: logfil - Log File Handle */
void opnlog(void) { void opnlog(void) {
strcpy(lognam, srcnam); //set Log File Name to Source File Name strcpy(lognam, srcnam); //set Log File Name to Source File Name
char *dot = strrchr(lognam, '.'); //find file extension char *dot = strrchr(lognam, '.'); //find file extension
if (dot != NULL) *dot = 0; //and remove it if (dot != NULL) *dot = 0; //and remove it
strcat(lognam, ".log"); //add extension ".log" strcat(lognam, ".log"); //add extension ".log"
DEBUG("Opening Log File '%s'\n", lognam) DEBUG("Opening Log File '%s'\n", lognam)
logfil = fopen(lognam, "w"); logfil = fopen(lognam, "w");
if (logfil == NULL) extsys(lognam); if (logfil == NULL) extsys(lognam);
} }
/* Close Log File * /* Close Log File *
* Uses: logfil - Log File Handle */ * Uses: logfil - Log File Handle */
void clslog(void) { fclose(logfil); } void clslog(void) { fclose(logfil); }
/* Open Include file * /* Open Include file *
* Uses: incnam - Include File Name * * Uses: incnam - Include File Name *
* subnam - Include File Name (Subdirectory) * * subnam - Include File Name (Subdirectory) *
* Sets: incfil - Include File Handle */ * Sets: incfil - Include File Handle */
void opninc(int chksub) void opninc(int chksub)
{ {
if (chksub) { if (chksub) {
for (subidx=0; subidx<subcnt; subidx++) { for (subidx=0; subidx<subcnt; subidx++) {
DEBUG("Attempting to open include file '%s'\n", subnam[subidx]) DEBUG("Attempting to open include file '%s'\n", subnam[subidx])
incfil = fopen(subnam[subidx], "r"); incfil = fopen(subnam[subidx], "r");
if (incfil == NULL) DEBUG("Open failed\n", 0) if (incfil == NULL) DEBUG("Open failed\n", 0)
else { else {
strcpy(incnam, subnam[subidx]); strcpy(incnam, subnam[subidx]);
DEBUG("INCNAM set to '%s'\n", incnam); DEBUG("INCNAM set to '%s'\n", incnam);
return; return;
} }
} }
} }
DEBUG("Opening include file '%s'\n", incnam) DEBUG("Opening include file '%s'\n", incnam)
incfil = fopen(incnam, "r"); incfil = fopen(incnam, "r");
if (incfil == NULL) extsys(incnam); if (incfil == NULL) extsys(incnam);
} }
/* Close Include File * /* Close Include File *
* Uses: incfil - Include File Handle */ * Uses: incfil - Include File Handle */
void clsinc(void) { fclose(incfil); } void clsinc(void) { fclose(incfil); }

View File

@ -1,27 +1,27 @@
/****************************** /******************************
* C02 File Handling Routines * * C02 File Handling Routines *
******************************/ ******************************/
FILE *srcfil; //Source File (Input) FILE *srcfil; //Source File (Input)
FILE *outfil; //Assembler File (Output) FILE *outfil; //Assembler File (Output)
FILE *logfil; //Log File (Output) FILE *logfil; //Log File (Output)
FILE *incfil; //Include File Handle FILE *incfil; //Include File Handle
FILE *inpfil; //Current Input File FILE *inpfil; //Current Input File
char srcnam[FNAMLEN]; //Source File Name char srcnam[FNAMLEN]; //Source File Name
char outnam[FNAMLEN]; //Assembler File Name char outnam[FNAMLEN]; //Assembler File Name
char lognam[FNAMLEN]; //Log File Name char lognam[FNAMLEN]; //Log File Name
char incnam[FNAMLEN]; //Include File Name char incnam[FNAMLEN]; //Include File Name
char subnam[SUBMAX][FNAMLEN]; //Include File Name (Subdirectory) char subnam[SUBMAX][FNAMLEN]; //Include File Name (Subdirectory)
void opnsrc(); //Open Source File void opnsrc(); //Open Source File
void clssrc(); //Close Source File void clssrc(); //Close Source File
void opnout(); //Open Output File void opnout(); //Open Output File
void clsout(); //Close Output File void clsout(); //Close Output File
void opnlog(); //Open Log File void opnlog(); //Open Log File
void clslog(); //Close Log File void clslog(); //Close Log File
void opninc(int chksub); //Open Include File void opninc(int chksub); //Open Include File
void clsinc(); //Close Include File void clsinc(); //Close Include File

View File

@ -86,7 +86,7 @@ void pdefin(void) {
/* Parse ASCII Subdirective */ /* Parse ASCII Subdirective */
void pascii(void) { void pascii(void) {
getwrd(); //Get Pragma Subdirective getwrd(); //Get Subdirective Argument
DEBUG("Parsing subdirective '%s'\n", word) DEBUG("Parsing subdirective '%s'\n", word)
if (wordis("INVERT")) if (wordis("INVERT"))
invasc = TRUE; invasc = TRUE;

View File

@ -1,14 +1,14 @@
/************************************* /*************************************
* C02 Include File Parsing Routines * * C02 Include File Parsing Routines *
*************************************/ *************************************/
char line[255]; /*Entire line parsed from include file*/ char line[255]; /*Entire line parsed from include file*/
void logcon(); //Print Constant Table to Log File void logcon(); //Print Constant Table to Log File
void pdefin(); //Process define directive void pdefin(); //Process define directive
void pdefin(); //Process define directive void pdefin(); //Process define directive
void penumd(); //Process enum directive void penumd(); //Process enum directive
void phdrfl(); //Process command line header file void phdrfl(); //Process command line header file
void pincfl(); //Process include file void pincfl(); //Process include file
void pprgma(); //Parse Pragma Directive void pprgma(); //Parse Pragma Directive
void setsrc(); ///Set Input to Source File void setsrc(); ///Set Input to Source File

View File

@ -1,153 +1,153 @@
/****************************************************** /******************************************************
* C02 Label Parsing, Generation, and Lookup Routines * * C02 Label Parsing, Generation, and Lookup Routines *
******************************************************/ ******************************************************/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "common.h" #include "common.h"
#include "files.h" #include "files.h"
#include "asm.h" #include "asm.h"
#include "parse.h" #include "parse.h"
#include "label.h" #include "label.h"
#include "vars.h" #include "vars.h"
/* Add New Program Label */ /* Add New Program Label */
void addlab(char *name) { void addlab(char *name) {
if (fndvar(name)) ERROR("Label %s conflicts with variable with same name", name, EXIT_FAILURE) if (fndvar(name)) ERROR("Label %s conflicts with variable with same name", name, EXIT_FAILURE)
if (fndlab(name)) ERROR("Duplicate program label %s\n", name, EXIT_FAILURE) if (fndlab(name)) ERROR("Duplicate program label %s\n", name, EXIT_FAILURE)
DEBUG("Adding Program Label %s ", name) DEBUG("at index %d\n", labcnt) DEBUG("Adding Program Label %s ", name) DEBUG("at index %d\n", labcnt)
strcpy(labnam[labcnt++], name); strcpy(labnam[labcnt++], name);
} }
int fndlab(char *name) { int fndlab(char *name) {
DEBUG("Looking for Program Label %s\n", name) DEBUG("Looking for Program Label %s\n", name)
for (labidx=0; labidx<labcnt; labidx++) for (labidx=0; labidx<labcnt; labidx++)
if (strcmp(labnam[labidx], name) == 0) return TRUE; if (strcmp(labnam[labidx], name) == 0) return TRUE;
DEBUG("Label %s Not Found\n", name) DEBUG("Label %s Not Found\n", name)
return FALSE; return FALSE;
} }
/* Print Program Label Table to Log File */ /* Print Program Label Table to Log File */
void loglab(void) { void loglab(void) {
int i; int i;
fprintf(logfil, "\n%-10s\n", "Label"); fprintf(logfil, "\n%-10s\n", "Label");
for (i=0; i<labcnt; i++) { for (i=0; i<labcnt; i++) {
fprintf(logfil, "%-10s\n", labnam[i]); fprintf(logfil, "%-10s\n", labnam[i]);
} }
} }
const char lblflg[] = {LFNONE, LFNONE, LFNONE, LFBGN, LFEND, LFBGN, LFEND, LFEND, LFNONE, LFNONE}; //Label Type Flags const char lblflg[] = {LFNONE, LFNONE, LFNONE, LFBGN, LFEND, LFBGN, LFEND, LFEND, LFNONE, LFNONE}; //Label Type Flags
// enum ltypes {LTNONE, LTIF, LTELSE, LTLOOP, LTEND, LTDO, LTDWHL, LTSLCT, LTCASE, LTFUNC}; //Label Types // enum ltypes {LTNONE, LTIF, LTELSE, LTLOOP, LTEND, LTDO, LTDWHL, LTSLCT, LTCASE, LTFUNC}; //Label Types
/* Find Last Label of Specified Types * /* Find Last Label of Specified Types *
* Args: lbtyp1: First label type * * Args: lbtyp1: First label type *
* lbtyp2: Second label type * * lbtyp2: Second label type *
* Sets: tmplbl - Label name * * Sets: tmplbl - Label name *
* Returns: Index into label table * * Returns: Index into label table *
* (-1 if not found) */ * (-1 if not found) */
int lstlbl(int lbflag) { int lstlbl(int lbflag) {
int i; int i;
DEBUG("Searching for label flag %d\n", lbflag) DEBUG("Searching for label flag %d\n", lbflag)
for (i = lblcnt - 1; i>-1; i--) for (i = lblcnt - 1; i>-1; i--)
if (lblflg[lbltyp[i]] == lbflag) break; if (lblflg[lbltyp[i]] == lbflag) break;
DEBUG("Search produced label index %d\n", i) DEBUG("Search produced label index %d\n", i)
if (i>=0) strcpy(tmplbl, lblnam[i]); if (i>=0) strcpy(tmplbl, lblnam[i]);
return i; return i;
} }
/* Set Block Flag for Last Label */ /* Set Block Flag for Last Label */
void setblk(int blkflg) { lblblk[lblcnt-1] = blkflg; } void setblk(int blkflg) { lblblk[lblcnt-1] = blkflg; }
/* Set label for next line of * /* Set label for next line of *
* Assembly Language Code * * Assembly Language Code *
* to word */ * to word */
void setlbl(char *lblset) { void setlbl(char *lblset) {
DEBUG("Setting Label '%s'\n", lblset) DEBUG("Setting Label '%s'\n", lblset)
if (strlen(lblasm) > 0) { if (strlen(lblasm) > 0) {
DEBUG("Emitting Label '%s'\n'", lblasm); DEBUG("Emitting Label '%s'\n'", lblasm);
asmlin("",""); //Emit Block End Label on it's own line asmlin("",""); //Emit Block End Label on it's own line
} }
if (strlen(lblset) > LBLLEN) ERROR("Label '%s' exceeds maximum size\n", word, EXIT_FAILURE) if (strlen(lblset) > LBLLEN) ERROR("Label '%s' exceeds maximum size\n", word, EXIT_FAILURE)
strcpy(lblasm, lblset); strcpy(lblasm, lblset);
} }
/* Parse Program Label */ /* Parse Program Label */
void prslab(void) { void prslab(void) {
DEBUG("Parsing Label '%s''\n", word) DEBUG("Parsing Label '%s''\n", word)
addlab(word); addlab(word);
CCMNT(nxtchr); CCMNT(nxtchr);
skpchr(); //skip ':' skpchr(); //skip ':'
} }
/* generate new label */ /* generate new label */
void newlbl(char* lbname) { void newlbl(char* lbname) {
sprintf(lbname, LBLFMT, lblnxt++); sprintf(lbname, LBLFMT, lblnxt++);
DEBUG("Generated new label '%s'\n", lbname) DEBUG("Generated new label '%s'\n", lbname)
} }
/* Check Label Contents * /* Check Label Contents *
* If lbname is blank, generate new * * If lbname is blank, generate new *
* label and copy into lbname */ * label and copy into lbname */
void chklbl(char* lbname) { void chklbl(char* lbname) {
if (lbname[0]) return; if (lbname[0]) return;
newlbl(lbname); newlbl(lbname);
} }
/* Request Label * /* Request Label *
* if label is already set, returns that label * * if label is already set, returns that label *
* else generates new label and sets it */ * else generates new label and sets it */
void reqlbl(char* lbname) { void reqlbl(char* lbname) {
DEBUG("Requesting Label\n",0) DEBUG("Requesting Label\n",0)
if (lblasm[0] == 0) {newlbl(lbname); setlbl(lbname);} if (lblasm[0] == 0) {newlbl(lbname); setlbl(lbname);}
else {strcpy(lbname,lblasm); DEBUG("Found lblasm set to \"%s\"\n", lblasm)} else {strcpy(lbname,lblasm); DEBUG("Found lblasm set to \"%s\"\n", lblasm)}
} }
/* End Function Block */ /* End Function Block */
void endfnc(void) { void endfnc(void) {
DEBUG("Ending function definition with lsrtrn set to %d\n", lsrtrn) DEBUG("Ending function definition with lsrtrn set to %d\n", lsrtrn)
if (!lsrtrn) asmlin("RTS", ""); if (!lsrtrn) asmlin("RTS", "");
infunc = FALSE; infunc = FALSE;
DEBUG("Set infunc to %d\n", infunc) DEBUG("Set infunc to %d\n", infunc)
} }
/* Pop Label from Stack and Emit on Next Line */ /* Pop Label from Stack and Emit on Next Line */
int poplbl(void) { int poplbl(void) {
int lbtype = lbltyp[--lblcnt]; int lbtype = lbltyp[--lblcnt];
DEBUG("Popped label type %d\n", lbtype) DEBUG("Popped label type %d\n", lbtype)
switch (lbtype) { switch (lbtype) {
case LTFUNC: endfnc(); break; //Return From Subroutine case LTFUNC: endfnc(); break; //Return From Subroutine
case LTDO: strcpy(loplbl, lblnam[lblcnt]); break; case LTDO: strcpy(loplbl, lblnam[lblcnt]); break;
case LTDWHL: strcpy(endlbl, lblnam[lblcnt]); break; case LTDWHL: strcpy(endlbl, lblnam[lblcnt]); break;
case LTCASE: strcpy(cndlbl, lblnam[lblcnt]); break; case LTCASE: strcpy(cndlbl, lblnam[lblcnt]); break;
case LTLOOP: asmlin("JMP", lblnam[lblcnt--]); //Jump to Beginning of Loop case LTLOOP: asmlin("JMP", lblnam[lblcnt--]); //Jump to Beginning of Loop
default: setlbl(lblnam[lblcnt]); //Emit End of Loop Label default: setlbl(lblnam[lblcnt]); //Emit End of Loop Label
} }
if (lbtype != LTCASE) inblck = lblblk[lblcnt-1]; if (lbtype != LTCASE) inblck = lblblk[lblcnt-1];
return lbtype; return lbtype;
} }
/* Get Top Label and Return Type */ /* Get Top Label and Return Type */
int toplbl(char *rtlbl) { int toplbl(char *rtlbl) {
if (lblcnt) { if (lblcnt) {
strcpy(rtlbl, lblnam[lblcnt-1]); strcpy(rtlbl, lblnam[lblcnt-1]);
DEBUG("Found top label %s\n", rtlbl) DEBUG("Found top label %s\n", rtlbl)
return lbltyp[lblcnt-1]; return lbltyp[lblcnt-1];
} }
rtlbl[0] = 0; //Clear Label rtlbl[0] = 0; //Clear Label
return LTNONE; return LTNONE;
} }
/* Push Label onto Stack * /* Push Label onto Stack *
* Args: lbltyp - Label type * * Args: lbltyp - Label type *
* Uses: curlbl - Label to push */ * Uses: curlbl - Label to push */
void pshlbl(int lbtype, char* lbname) { void pshlbl(int lbtype, char* lbname) {
DEBUG("Pushing label type %d\n", lbtype) DEBUG("Pushing label type %d\n", lbtype)
strcpy(lblnam[lblcnt], lbname); strcpy(lblnam[lblcnt], lbname);
lbltyp[lblcnt] = lbtype; lbltyp[lblcnt] = lbtype;
lblblk[lblcnt++] = FALSE; lblblk[lblcnt++] = FALSE;
DEBUG("Pushed label '%s' onto stack\n", lbname) DEBUG("Pushed label '%s' onto stack\n", lbname)
} }

View File

@ -1,41 +1,41 @@
/****************************************************** /******************************************************
* C02 Label Parsing, Generation, and Lookup Routines * * C02 Label Parsing, Generation, and Lookup Routines *
******************************************************/ ******************************************************/
char labnam[MAXLAB+1][LABLEN+1]; //Program Label Names char labnam[MAXLAB+1][LABLEN+1]; //Program Label Names
int labcnt; //Number of Program Labels int labcnt; //Number of Program Labels
int labidx; //Index into labnam[] int labidx; //Index into labnam[]
char curlbl[LBLLEN+1]; //Most recently generated label char curlbl[LBLLEN+1]; //Most recently generated label
char cmplbl[LBLLEN+1]; //Label for Comparison char cmplbl[LBLLEN+1]; //Label for Comparison
char cndlbl[LBLLEN+1]; //Label for Conditional Code char cndlbl[LBLLEN+1]; //Label for Conditional Code
char endlbl[LBLLEN+1]; //End Label char endlbl[LBLLEN+1]; //End Label
char forlbl[LBLLEN+1]; //For Loop Label char forlbl[LBLLEN+1]; //For Loop Label
char loplbl[LBLLEN+1]; //Skip Increment Label char loplbl[LBLLEN+1]; //Skip Increment Label
char skplbl[LBLLEN+1]; //Skip Increment Label char skplbl[LBLLEN+1]; //Skip Increment Label
char tmplbl[LBLLEN+1]; //Temporary Label char tmplbl[LBLLEN+1]; //Temporary Label
char lblnam[MAXLBL+1][LBLLEN+1]; //Label Name Table char lblnam[MAXLBL+1][LBLLEN+1]; //Label Name Table
int lbltyp[MAXLBL+1]; //Label Type int lbltyp[MAXLBL+1]; //Label Type
int lblblk[MAXLBL+1]; //Label Ends Program Block int lblblk[MAXLBL+1]; //Label Ends Program Block
int lblcnt; //Number of Labels in stack int lblcnt; //Number of Labels in stack
int lblnxt; //Sequence of next label to be generated int lblnxt; //Sequence of next label to be generated
char lbltmp[LBLLEN+1]; //Label Temporary Storage char lbltmp[LBLLEN+1]; //Label Temporary Storage
enum ltypes {LTNONE, LTIF, LTELSE, LTLOOP, LTEND, LTDO, LTDWHL, LTSLCT, LTCASE, LTFUNC}; //Label Types enum ltypes {LTNONE, LTIF, LTELSE, LTLOOP, LTEND, LTDO, LTDWHL, LTSLCT, LTCASE, LTFUNC}; //Label Types
enum lflags {LFNONE, LFBGN, LFEND}; //Label Flag Types enum lflags {LFNONE, LFBGN, LFEND}; //Label Flag Types
void addlab(char *name); //Add Program Label void addlab(char *name); //Add Program Label
int fndlab(char *name); //Find Program Label int fndlab(char *name); //Find Program Label
void prslab(); //Parse Program Label void prslab(); //Parse Program Label
void loglab(void); //Print Program Label Table void loglab(void); //Print Program Label Table
void chklbl(char* lbname); //Check Label Contents void chklbl(char* lbname); //Check Label Contents
int lstlbl(int lbflag); //Find Last Label of Specified Types * int lstlbl(int lbflag); //Find Last Label of Specified Types *
void newlbl(char* lbname); //Generate New Block Label void newlbl(char* lbname); //Generate New Block Label
int poplbl(); //Pop Last Label and Emit on Next Line int poplbl(); //Pop Last Label and Emit on Next Line
void pshlbl(int lbtype, char* lbname); //Push Label onto Stack void pshlbl(int lbtype, char* lbname); //Push Label onto Stack
void reqlbl(char* lbname); //Require Label void reqlbl(char* lbname); //Require Label
void setblk(int blkflg); //Set Block Flag for Last Label void setblk(int blkflg); //Set Block Flag for Last Label
void setlbl(char *lblset); //Emit word as Label on Next Line void setlbl(char *lblset); //Emit word as Label on Next Line
int toplbl(char *rtlbl); //Get Top Label and Return Type int toplbl(char *rtlbl); //Get Top Label and Return Type

View File

@ -1,21 +1,21 @@
/************************************ /************************************
* C02 Statement Compiling Routines * * C02 Statement Compiling Routines *
************************************/ ************************************/
char asnvar[VARLEN+1]; //Assigned Variable Name char asnvar[VARLEN+1]; //Assigned Variable Name
int asntyp; //Assigned Variable Type int asntyp; //Assigned Variable Type
char asnidx[VARLEN+1] ; //Assigned Variable Index char asnidx[VARLEN+1] ; //Assigned Variable Index
int asnivt; //Assigned Index Variable Type int asnivt; //Assigned Index Variable Type
char ysnvar[VARLEN+1]; //Assigned Y Variable Name char ysnvar[VARLEN+1]; //Assigned Y Variable Name
char ysnidx[VARLEN+1] ; //Assigned Y Variable Index char ysnidx[VARLEN+1] ; //Assigned Y Variable Index
int ysnivt; //Assigned Y Index Variable Type int ysnivt; //Assigned Y Index Variable Type
char xsnvar[VARLEN+1]; //Assigned X Variable Name char xsnvar[VARLEN+1]; //Assigned X Variable Name
char xsnidx[VARLEN+1] ; //Assigned X Variable Index char xsnidx[VARLEN+1] ; //Assigned X Variable Index
int xsnivt; //Assigned X Index Variable Type int xsnivt; //Assigned X Index Variable Type
char xstmnt[LINELEN]; //Expected Statement char xstmnt[LINELEN]; //Expected Statement
void bgnblk(char blkchr); //End Program Block void bgnblk(char blkchr); //End Program Block
void endblk(int blkflg); //End Program Block void endblk(int blkflg); //End Program Block
void pdowhl(); //Parse and Compile WHILE after DO void pdowhl(); //Parse and Compile WHILE after DO
void pstmnt(); //Parse and Compile Program Statement void pstmnt(); //Parse and Compile Program Statement