1 /* Paul's XA51 Assembler, Copyright 1997,2002 Paul Stoffregen (paul@pjrc.com)
3 * Paul's XA51 Assembler is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; version 2.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 /* adapted from the osu8asm project, 1995 */
18 /* http://www.pjrc.com/tech/osu8/index.html */
27 extern void yyrestart(FILE *new_file);
28 extern void hexout(int byte, int memory_location, int end);
32 /* global variables */
34 FILE *fhex, *fmem, *list_fp;
37 extern char last_line_text[];
38 struct symbol *sym_list=NULL;
39 struct target *targ_list=NULL;
42 int expr_result, expr_ok, jump_dest, inst;
44 char symbol_name[1000];
45 struct area_struct area[NUM_AREAS];
46 int current_area=AREA_CSEG;
49 /* "mem" is replaced by area[current_area].alloc_position */
50 /* int mem=0; */ /* mem is location in memory */
52 /* add symbols to list when we find their definition in pass #1 */
53 /* we will evaluate their values in pass #2, and figure out if */
54 /* they are branch targets betweem passes 1 and 2. Every symbol */
55 /* should appear exactly once in this list, since it can't be redefined */
57 struct symbol * build_sym_list(char *thename)
59 struct symbol *new, *p;
61 /* printf(" Symbol: %s Line: %d\n", thename, lineno); */
62 new = (struct symbol *) malloc(sizeof(struct symbol));
63 new->name = (char *) malloc(strlen(thename)+1);
64 strcpy(new->name, thename);
70 new->line_def = lineno - 1;
72 if (sym_list == NULL) return (sym_list = new);
74 while (p->next != NULL) p = p->next;
79 int assign_value(char *thename, int thevalue)
85 if (!(strcasecmp(thename, p->name))) {
92 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
96 int mk_bit(char *thename)
102 if (!(strcasecmp(thename, p->name))) {
108 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
113 int mk_reg(char *thename)
119 if (!(strcasecmp(thename, p->name))) {
125 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
131 int get_value(char *thename)
136 if (!(strcasecmp(thename, p->name)))
140 fprintf(stderr, "Internal Error! Couldn't find symbol value\n");
146 /* add every branch target to this list as we find them */
147 /* ok if multiple entries of same symbol name in this list */
149 struct target * build_target_list(char *thename)
151 struct target *new, *p;
152 new = (struct target *) malloc(sizeof(struct target));
153 new->name = (char *) malloc(strlen(thename)+1);
154 strcpy(new->name, thename);
156 if (targ_list == NULL) return (targ_list = new);
158 while (p->next != NULL) p = p->next;
163 /* figure out which symbols are branch targets */
167 struct symbol *p_sym;
168 struct target *p_targ;
170 while (p_targ != NULL) {
172 while (p_sym != NULL) {
173 if (!strcasecmp(p_sym->name, p_targ->name))
177 p_targ = p_targ->next;
181 void print_symbol_table()
186 printf("Sym:%12s = %5d (%04X) Def:", \
187 p->name, p->value, p->value);
188 if (p->isdef) printf("Yes"); else printf("No ");
190 if (p->isbit) printf("Yes"); else printf("No ");
192 if (p->istarget) printf("Yes"); else printf("No ");
193 printf(" Line %d\n", p->line_def);
198 /* check that every symbol is in the table only once */
200 void check_redefine()
202 struct symbol *p1, *p2;
207 if (!strcasecmp(p1->name, p2->name)) {
208 fprintf(stderr, "Error: symbol '%s' redefined on line %d", p1->name, p2->line_def);
209 fprintf(stderr, ", first defined on line %d\n", p1->line_def);
218 int is_target(char *thename)
223 if (!strcasecmp(thename, p->name)) return (p->istarget);
229 int is_bit(char *thename)
234 if (!strcasecmp(thename, p->name)) return (p->isbit);
240 int is_reg(char *thename)
245 if (!strcasecmp(thename, p->name)) return (p->isreg);
252 int is_def(char *thename)
257 if (!strcasecmp(thename, p->name) && p->isdef) return(1);
263 /* this routine is used to dump a group of bytes to the output */
264 /* it is responsible for generating the list file and sending */
265 /* the bytes one at a time to the object code generator */
266 /* this routine is also responsible for generatine the list file */
267 /* though is it expected that the lexer has placed all the actual */
268 /* original text from the line in "last_line_text" */
270 void out(int *byte_list, int num)
274 if (num > 0) fprintf(list_fp, "%06X: ", MEM_POS);
275 else fprintf(list_fp, "\t");
277 for (i=0; i<num; i++) {
278 hexout(byte_list[i], MEM_POS + i, 0);
279 if (!first && (i % 4) == 0) fprintf(list_fp, "\t");
280 fprintf(list_fp, "%02X", byte_list[i]);
281 if ((i+1) % 4 == 0) {
282 if (first) fprintf(list_fp, "\t%s\n", last_line_text);
283 else fprintf(list_fp, "\n");
286 if (i<num-1) fprintf(list_fp, " ");
290 if (num < 3) fprintf(list_fp, "\t");
291 fprintf(list_fp, "\t%s\n", last_line_text);
293 if (num % 4) fprintf(list_fp, "\n");
298 /* add NOPs to align memory location on a valid branch target address */
302 static int nops[] = {NOP_OPCODE, NOP_OPCODE, NOP_OPCODE, NOP_OPCODE};
305 last_line_text[0] = '\0';
307 for(num=0; (MEM_POS + num) % BRANCH_SPACING; num++) ;
308 if (p3) out(nops, num);
312 /* print branch out of bounds error */
316 fprintf(stderr, "Error: branch out of bounds");
317 fprintf(stderr, " in line %d\n", lineno);
321 /* output the jump either direction on carry */
322 /* jump_dest and MEM_POS must have the proper values */
325 void do_jump_on_carry()
328 operand = REL4(jump_dest, MEM_POS);
332 if (operand > 15) boob_error();
333 out(0x20 + (operand & 15));
335 if (operand > 15) boob_error();
336 out(0x30 + (operand & 15));
342 /* turn a string like "10010110b" into an int */
344 int binary2int(char *str)
346 register int i, j=1, sum=0;
348 for (i=strlen(str)-2; i >= 0; i--) {
349 sum += j * (str[i] == '1');
358 /* todo: someday this will allow the user to control where the */
359 /* various memory areas go, and it will take care of assigning */
360 /* positions to area which follow others (such as OSEG getting */
361 /* set just after DSEG on the 2nd and 3rd passes when we have */
362 /* leared the size needed for each segment */
364 void init_areas(void)
366 area[AREA_CSEG].alloc_position = 0;
367 area[AREA_DSEG].alloc_position = 0x30;
368 area[AREA_OSEG].alloc_position = 0x80;
369 area[AREA_ISEG].alloc_position = 0;
370 area[AREA_BSEG].alloc_position = 0;
371 area[AREA_XSEG].alloc_position = 0;
372 area[AREA_XISEG].alloc_position = 0;
373 area[AREA_GSINIT].alloc_position = 0;
374 area[AREA_GSFINAL].alloc_position = 0;
375 area[AREA_HOME].alloc_position = 0;
379 /* pass #1 (p1=1) find all symbol defs and branch target names */
380 /* pass #2 (p2=1) align branch targets, evaluate all symbols */
381 /* pass #3 (p3=1) produce object code */
383 int main(int argc, char **argv)
385 char infilename[200], outfilename[200], listfilename[200];
387 if (argc < 2) print_usage();
388 strcpy(infilename, argv[1]);
389 if(strlen(infilename) > 3) {
390 if (strncasecmp(infilename+strlen(infilename)-3, ".xa", 3))
391 strcat(infilename, ".xa");
392 } else strcat(infilename, ".xa");
393 strcpy(outfilename, infilename);
394 outfilename[strlen(outfilename)-3] = '\0';
395 strcpy(listfilename, outfilename);
396 strcat(outfilename, ".hex");
397 strcat(listfilename, ".lst");
398 yyin = fopen(infilename, "r");
400 fprintf(stderr, "Can't open file '%s'.\n", infilename);
403 fhex = fopen(outfilename, "w");
405 fprintf(stderr, "Can't write file '%s'.\n", outfilename);
408 list_fp = fopen(listfilename, "w");
409 if (list_fp == NULL) {
410 fprintf(stderr, "Can't write file '%s'.\n", listfilename);
414 /* todo: add a command line option to supress verbose messages */
415 printf("\nPaul's XA51 Assembler\n");
416 printf("Copyright 1997,2002 Paul Stoffregen\n\n");
417 printf("This program is free software; you can redistribute it\n");
418 printf("and/or modify it under the terms of the GNU General Public\n");
419 printf("License, Version 2, published by the Free Software Foundation\n\n");
420 printf("This program is distributed in the hope that it will be useful,\n");
421 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
422 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
426 printf(" Building Symbol Table:\n");
432 print_symbol_table();
439 printf(" Aligning Branch Targets:\n");
443 // print_symbol_table();
449 printf(" Generating Object Code:\n");
454 hexout(0, 0, 1); /* flush and close intel hex file output */
461 fprintf(stderr, "Usage: xa_asm file\n");
462 fprintf(stderr, " or xa_asm file.asm\n");