home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
276_01
/
az80.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-10-03
|
19KB
|
786 lines
/*
HEADER: CUG276;
TITLE: Z-80 Cross-Assembler (Portable);
FILENAME: AZ80.C;
VERSION: 0.1;
DATE: 08/27/1988;
SEE-ALSO: AZ80.H;
AUTHORS: William C. Colley III;
*/
/*
Z-80 Cross-Assembler in Portable C
Copyright (c) 1986-1988 William C. Colley, III
Revision History:
Ver Date Description
0.0 JUNE 1988 Derived from my S-6 cross-assembler. WCC3.
0.1 AUG 1988 Fixed a bug in the command line parser that puts it
into a VERY long loop if the user types a command line
like "AZ80 FILE.ASM -L". WCC3 per Alex Cameron.
This file contains the main program and line assembly routines for the
assembler. The main program parses the command line, feeds the source lines
to the line assembly routine, and sends the results to the listing and object
file output routines. It also coordinates the activities of everything. The
line assembly routines uses the expression analyzer and the lexical analyzer
to parse the source line and convert it into the object bytes that it
represents.
*/
/* Get global goodies: */
#include "az80.h"
/* Define global mailboxes for all modules: */
char errcode, line[MAXLINE + 1], title[MAXLINE];
int pass = 0;
int eject, filesp, forwd, listhex;
unsigned address, bytes, errors, listleft, obj[MAXLINE], pagelen, pc;
FILE *filestk[FILES], *source;
TOKEN arg, token;
/* Mainline routine. This routine parses the command line, sets up */
/* the assembler at the beginning of each pass, feeds the source text */
/* to the line assembler, feeds the result to the listing and hex file */
/* drivers, and cleans everything up at the end of the run. */
static int done, ifsp, off;
void main(argc,argv)
int argc;
char **argv;
{
SCRATCH unsigned *o;
int newline();
void asm_line();
void lclose(), lopen(), lputs();
void hclose(), hopen(), hputc();
void error(), fatal_error(), warning();
printf("Z-80 Cross-Assembler (Portable) Ver 0.1\n");
printf("Copyright (c) 1986-1988 William C. Colley, III\n\n");
while (--argc > 0) {
if (**++argv == '-') {
switch (toupper(*++*argv)) {
case 'L': if (!*++*argv) {
if (!--argc) { warning(NOLST); break; }
else ++argv;
}
lopen(*argv);
break;
case 'O': if (!*++*argv) {
if (!--argc) { warning(NOHEX); break; }
else ++argv;
}
hopen(*argv);
break;
default: warning(BADOPT);
}
}
else if (filestk[0]) warning(TWOASM);
else if (!(filestk[0] = fopen(*argv,"r"))) fatal_error(ASMOPEN);
}
if (!filestk[0]) fatal_error(NOASM);
while (++pass < 3) {
fseek(source = filestk[0],0L,0); done = off = FALSE;
errors = filesp = ifsp = pagelen = pc = 0; title[0] = '\0';
while (!done) {
errcode = ' ';
if (newline()) {
error('*');
strcpy(line,"\tEND\n");
done = eject = TRUE; listhex = FALSE;
bytes = 0;
}
else asm_line();
pc = word(pc + bytes);
if (pass == 2) {
lputs();
for (o = obj; bytes--; hputc(*o++));
}
}
}
fclose(filestk[0]); lclose(); hclose();
if (errors) printf("%d Error(s)\n",errors);
else printf("No Errors\n");
exit(errors);
}
/* Line assembly routine. This routine gets the contents of the */
/* argument field from the source file using the expression evaluator */
/* and lexical analyzer. It makes all validity checks on the */
/* arguments validity, fills a buffer with the machine code bytes and */
/* returns nothing. */
static char label[MAXLINE];
static int ifstack[IFDEPTH] = { ON };
static OPCODE *opcod;
void asm_line()
{
SCRATCH char *p;
SCRATCH int i;
int isalph(), popc();
OPCODE *find_code(), *find_operator();
void do_label(), flush(), normal_op(), pseudo_op();
void error(), pops(), pushc(), trash();
address = pc; bytes = 0; eject = forwd = listhex = FALSE;
for (i = 0; i < BIGINST; obj[i++] = NOP);
label[0] = '\0';
if ((i = popc()) != ' ' && i != '\n') {
if (isalph(i)) {
pushc(i); pops(label);
for (p = label; *(p + 1); ++p);
if (*p == ':') *p = '\0';
if (find_operator(label)) { label[0] = '\0'; error('L'); }
}
else {
error('L');
while ((i = popc()) != ' ' && i != '\n');
}
}
trash(); opcod = NULL;
if ((i = popc()) != '\n') {
if (!isalph(i)) error('S');
else {
pushc(i); pops(token.sval);
if (!(opcod = find_code(token.sval))) error('O');
}
if (!opcod) { listhex = TRUE; bytes = BIGINST; }
}
if (opcod && opcod -> attr & ISIF) { if (label[0]) error('L'); }
else if (off) { listhex = FALSE; flush(); return; }
if (!opcod) { do_label(); flush(); }
else {
listhex = TRUE;
if (opcod -> attr & PSEUDO) pseudo_op();
else normal_op();
while ((i = popc()) != '\n') if (i != ' ') error('T');
}
source = filestk[filesp];
return;
}
static void flush()
{
while (popc() != '\n');
}
static void do_label()
{
SCRATCH SYMBOL *l;
SYMBOL *find_symbol(), *new_symbol();
void error();
if (label[0]) {
listhex = TRUE;
if (pass == 1) {
if (!((l = new_symbol(label)) -> attr)) {
l -> attr = FORWD + VAL;
l -> valu = pc;
}
}
else {
if (l = find_symbol(label)) {
l -> attr = VAL;
if (l -> valu != pc) error('M');
}
else error('P');
}
}
}
static void normal_op()
{
SCRATCH unsigned opcode, tmp;
unsigned *o, *p;
static unsigned IM_tbl[] = { 0x00, 0x10, 0x18 };
int popc();
unsigned expr(), get_arg();
void do_label(), error(), grab_comma(), insert_prebyte(), pushc(), trash();
TOKEN *lex();
do_label(); opcode = opcod -> valu; o = obj;
if (opcode > 0xff) *o++ = high(opcode);
*o = low(opcode);
switch (opcod -> attr & OPTYPE) {
case LD:
p = o;
switch (get_arg()) {
case IX:
case IY: insert_prebyte(&o,arg.attr);
case BC:
case DE:
case HL:
case SP: *o = 0x01 + ((arg.attr & 007) << 3);
grab_comma();
switch (get_arg()) {
case NUM_IND: if (*o == 0x21) *o += 0x09;
else {
*o += 0x4a;
insert_prebyte(&o,
arg.attr);
}
case NUM: *++o = low(arg.valu);
*++o = high(arg.valu);
break;
case IX:
case IY: insert_prebyte(&o,
arg.attr);
case HL: if (*o == 0x31) {
*o = 0xf9; break;
}
default: error(arg.attr < NUM ?
'R' : 'S');
break;
}
break;
case I: *o++ = 0xed; *o = 0x47; goto do_ld_i;
case R: *o++ = 0xed; *o = 0x4f; goto do_ld_i;
case BC_IND: *o = 0x02; goto do_ld_i;
case DE_IND: *o = 0x12;
do_ld_i: grab_comma();
if (get_arg() != A)
error(arg.attr < NUM ? 'R' : 'S');
break;
case NUM_IND: *++o = low(arg.valu);
*++o = high(arg.valu);
grab_comma(); *p = 0x02;
switch (get_arg()) {
case A: *p = 0x32; break;
case BC:
case DE:
case SP: *p = 0x43;
case IX:
case IY: *p += (arg.attr & 007)
<< 3;
insert_prebyte(&o,
arg.attr);
break;
case HL: *p = 0x22; break;
default: error(arg.attr < NUM ?
'R' : 'S');
break;
}
break;
case A: grab_comma();
switch (get_arg()) {
case BC_IND: *o = 0x0a; break;
case DE_IND: *o = 0x1a; break;
case I: *o++ = 0xed; *o = 0x57;
break;
case R: *o++ = 0xed; *o = 0x5f;
break;
case NUM_IND: *o++ = 0x3a;
*o++ = low(arg.valu);
*o = high(arg.valu);
break;
default: *o += 0x38; goto do_ld_a;
}
break;
case IX_IND:
case IY_IND: insert_prebyte(&o,arg.attr); ++p;
*++o = low(arg.valu); arg.attr = HL_IND;
default: if (arg.attr <= A) {
*p += (arg.attr & 007) << 3;