home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD1.bin
/
new
/
dev
/
cross
/
caz
/
src
/
caz.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-01-08
|
63KB
|
2,609 lines
#define _STRICT_ANSI
#include "defs.h"
#include "cazerrors.h"
/*#define DEBUG2 */ /* internal use for more debugging (print out coded MNE) */
/*#define DEBUG_STRTOINT*/ /* debug strtoint() funktion */
#define MODE_ALL 1 /* Verbose Mode: Print out all source text */
#define MODE_MNE 2 /* Verbose Mode: Print out only MNE text */
#define MODE_OPTION 4 /* if Verbose Mode is set via Commandline Option */
#define ACT_OBJ_ADDR(pobj) (((u_char *)(pobj->objbuffer+(pobj->actbyte-pobj->firstbyte))))
#define COPYRIGHT " ©1992-94 by Carsten Rose\n"
const char *vers = "\0$VER: Z80_Assenbler 1.26ß (7 Januar 94)";
extern char *directives[]; /* declared in File 'cazmne.c' */
extern struct command cmd[]; /* declared in File 'cazmne.c' */
static int init (struct listheader *plabel,struct objsize *pobj);
static void symboltable (struct listheader *plabel);
static int writesymboltable (struct listheader *plabel,char *filename);
static char *ibtoh (u_char byte,char *pbuf);
static char *iwtoh (unsigned short word,char *pbuf);
static void decodearg (char *str,u_long pa);
static int strcmp_lany_rupper(char *pany,char *pupper);
static void cutend (char *pos);
static char *skipspace (char *pos);
static char validquote (char quote);
static void killcomment (char *pos);
static int expandlist (struct listheader *plist);
static struct labelitem
*insertlabel (struct listheader *plabel,char *name,int type,u_short value,BOOL valid);
static int dolabel (struct listheader *plabel,struct objsize *pobjcode,char *lname);
static int finddirective (char *pos);
static struct command
*findmne (struct command *pcmd,char *pmne);
static u_long argtype (struct listheader *plabel,char *pos,u_short *pshort,int mode);
static int doobjadr (struct objsize *pobj,int n,int mode);
static u_char codebits (u_long mask);
static int writemnedata (struct objsize *pobj,u_long maskarg,u_short val,u_long maskcmd,u_char pos);
static int fillihbuffer (int addr,int nbytes,u_char *pbuf);
static int dointelhex (int addr,int size,u_char *data);
static int makecode (struct objsize *pobj,struct command *pcmd,int mode,u_long type1,u_short val1,u_long type2,u_short val2);
static int domne (struct listheader *plabel,struct objsize *pobj,char *ptok,int mode,u_short *pcycle);
static int pushfile (struct item_file **pstack,char *p_filenamenew,FILE **p_fp);
static int popfile (struct item_file **pstack,FILE **p_fp);
static int setdefaultbase (char *pbase);
static int dodirective (struct listheader *plabel,struct objsize *pobj,char *ptok,int direc,int mode,int *orgflag);
static int parseline (struct listheader *plabel,struct objsize *pobj,char *pwork,int mode);
static int readsrc (struct listheader *plabel,struct objsize *pobj,char *p_actname);
int main (int argc,char *argv[]);
int lnr=0,errors=0,
debugv=FALSE, /* debug values, set via Commandline option */
debugf=FALSE, /* debug functions, set via Commandline option */
verbose=FALSE, /* set via Commandline option */
intelhex=FALSE, /* set via Commandline option */
clockcycle=FALSE, /* set via Commandline option */
showsymbols=FALSE; /* set via Commandline option */
default_num_base; /* change via DEFNUM directive */
char errortext[BUFSIZ], /* Common Buffer for Error Messages */
line[BUFSIZ], /* Actual read Source text line (original) */
work[BUFSIZ], /* Actual read Source text line (truncated,modified) */
actfilename[FILENAME_MAX]; /* Actual Filename of assembled Source File */
struct listheader ih_head;
struct listheader label;
struct objsize object;
struct item_file *file_stack=NULL;
BOOL endflag=FALSE, /* Set if END is found */
assembleflag=TRUE; /* Normal TRUE, only if 'COND xx' xx==0 then FALSE */
FILE *fpin=NULL,*fpout=NULL;
static void showerror(char *errpos, char *fmt , ...)
{
/*
* Print parsing error:
*
* xxxx:........................
* ^
* 'Error Description'
*/
int i;
va_list pa;
char buffer[BUFSIZ];
if(debugf) puts("showerror");
/* If there are more than 1 source files - print Filenames */
if(file_stack)
fprintf(stderr,"Error in File:%s\n",actfilename);
fprintf(stderr,"%4d:%s",lnr,line); /* line number and Source code line */
i = strlen(line);
if(!i) /* not really needed */
return;
if(line[i-1]!='\n')
fputs("\n",stderr);
if(!errpos)
errpos=work;
i = (int)(errpos - work);
buffer[i]='\0';
while(i--)
{
if(line[i]=='\t')
buffer[i]='\t';
else
buffer[i]=' ';
}
fprintf(stderr," %s^ ",buffer); /* mark position */
va_start(pa,fmt);
vfprintf(stderr,fmt,pa); /* Print Errormessage */
va_end(pa);
fputs("\n",stderr);
errors++; /* count errors */
}
static int init(struct listheader *plabel,struct objsize *pobj)
{
/*
* Init listheader
*
* RC : NULL if all went fine
*/
if(debugf) puts("init");
plabel->nitem = 0;
plabel->actitem = 0;
plabel->sizeitem = sizeof(struct labelitem);
plabel->newnitem = NEWITEMOFF;
plabel->userdata = ERROR;
plabel->list = NULL;
ih_head.nitem = 0;
ih_head.actitem = 0;
ih_head.sizeitem = sizeof(u_char);
ih_head.newnitem = 4096;
ih_head.userdata = ERROR;
ih_head.list = NULL;
pobj->firstbyte = ERROR;
pobj->actbyte = ERROR;
pobj->lastbyte = ERROR;
pobj->objbuffer = NULL;
return(NULL);
}
static void symboltable(struct listheader *plabel)
{
int i;
struct labelitem *lst;
char *pltyp;
if(debugf) puts("symboltable");
lst = plabel->list;
i = plabel->actitem;
puts("Lable's:");
while(i--)
{
switch(lst->type)
{
case L_EQU: pltyp = "EQU";
break;
case L_DEFL: pltyp = "DEFL";
break;
case L_POSITION:
pltyp = "POSITION";
break;
deafult: pltyp = "unknown";
INTERNAL_ERROR
break;
}
printf("Name:%-32s Type:%-8s Value:0x%X\n",lst->name,pltyp,lst->value);
lst++;
}
}
static int writesymboltable(struct listheader *plabel,char *filename)
{
/* Write all Labels as EQU definition's with associated values into
* 'filename'
*
* RC: NULL if all went fine else
* ERROR
*/
int i;
struct labelitem *lst;
FILE *fplabel;
if(debugf) puts("writesymboltable");
lst = plabel->list;
i = plabel->actitem;
if(!(fplabel = fopen(filename,"wb"))) /* open file where to write Label's */
{
perror("Can't open file:");
return(ERROR);
}
while(i--)
{
if(fprintf(fplabel,"%-32s EQU 0x%X\n",lst->name,lst->value)<0)
{
perror("Writing Labelfile failed");
fclose(fplabel);
return(ERROR);
}
lst++;
}
fclose(fplabel);
return(NULL);
}
static char *ibtoh(unsigned char byte,char *pbuf)
{
/* Convert 'byte' in ASCII Hex Notation.
*
* Write ASCII Hex Code (2 Chars) at '*pbuf'.
*
* RC: pbuf+2 (points to next free char)
*/
unsigned char low, /* low 4 Bits of 'byte' */
high; /* high 4 Bits of 'byte' (normalized) */
low = 0x0F&byte;
high = (0xF0&byte)>>4;
/* convert Integer to ASCII Hex */
*pbuf++= (high<=9) ? high+'0' : (high-0x0A)+'A';
*pbuf++= (low<=9) ? low+'0' : (low-0x0A)+'A';
return(pbuf);
}
static char *iwtoh(u_short word,char *pbuf)
{
/* Convert 'word' in ASCII Hex Notation.
*
* Write ASCII Hex Code (4 Chars) at '*pbuf'.
*
* RC: pbuf+4 (points to next free char)
*/
char *pos;
u_char lowb,highb;
lowb = (u_char)(word & 0xFF);
highb = (u_char)(word >> 8);
pos=ibtoh(highb,pbuf);
pos=ibtoh(lowb,pos);
return(pos);
}
static void decodearg(char *str,u_long pa)
{
char pos[256];
if(debugf) puts("decodearg");
pos[0]='\0';
if( (pa^BRA)&BRA)
{
if(!( (pa®8)^REG8))
strcat(pos,"REG8 ");
else if(!( (pa®16)^REG16))
strcat(pos,"REG16 ");
else
{
if(pa®_A) strcat(pos,"REG_A ");
if(pa®_B) strcat(pos,"REG_B ");
if(pa®_C) strcat(pos,"REG_C ");
if(pa®_D) strcat(pos,"REG_D ");
if(pa®_F) strcat(pos,"REG_F ");
if(pa®_H) strcat(pos,"REG_H ");
if(pa®_I) strcat(pos,"REG_I ");
if(pa®_L) strc