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 */
20 #define printf(x...) fprintf(stderr,x)
28 extern void yyrestart(FILE *new_file);
29 extern void hexout(int byte, int memory_location, int end);
33 /* global variables */
35 FILE *fhex, *fmem, *list_fp;
38 extern char last_line_text[];
39 struct symbol *sym_list=NULL;
40 struct target *targ_list=NULL;
43 int expr_result, expr_ok, jump_dest, inst;
45 char symbol_name[1000];
46 struct area_struct area[NUM_AREAS];
47 int current_area=AREA_CSEG;
50 char *areaToString (int area) {
53 case AREA_CSEG: return "CSEG";
54 case AREA_DSEG: return "DSEG";
55 case AREA_OSEG: return "OSEG";
56 case AREA_ISEG: return "ISEG";
57 case AREA_BSEG: return "BSEG";
58 case AREA_XSEG: return "XSEG";
59 case AREA_XISEG: return "XISEG";
60 case AREA_XINIT: return "XINIT";
65 /* "mem" is replaced by area[current_area].alloc_position */
66 /* int mem=0; */ /* mem is location in memory */
68 /* add symbols to list when we find their definition in pass #1 */
69 /* we will evaluate their values in pass #2, and figure out if */
70 /* they are branch targets betweem passes 1 and 2. Every symbol */
71 /* should appear exactly once in this list, since it can't be redefined */
73 struct symbol * build_sym_list(char *thename)
75 struct symbol *new, *p;
77 /* printf(" Symbol: %s Line: %d\n", thename, lineno); */
78 new = (struct symbol *) malloc(sizeof(struct symbol));
79 new->name = (char *) malloc(strlen(thename)+1);
80 strcpy(new->name, thename);
86 new->line_def = lineno - 1;
87 new->area = current_area;
89 if (sym_list == NULL) return (sym_list = new);
91 while (p->next != NULL) p = p->next;
96 int assign_value(char *thename, int thevalue)
102 if (!(strcasecmp(thename, p->name))) {
109 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
113 int mk_bit(char *thename)
119 if (!(strcasecmp(thename, p->name))) {
125 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
130 int mk_reg(char *thename)
136 if (!(strcasecmp(thename, p->name))) {
142 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
148 int get_value(char *thename)
153 if (!(strcasecmp(thename, p->name)))
157 fprintf(stderr, "Internal Error! Couldn't find symbol value\n");
163 /* add every branch target to this list as we find them */
164 /* ok if multiple entries of same symbol name in this list */
166 struct target * build_target_list(char *thename)
168 struct target *new, *p;
169 new = (struct target *) malloc(sizeof(struct target));
170 new->name = (char *) malloc(strlen(thename)+1);
171 strcpy(new->name, thename);
173 if (targ_list == NULL) return (targ_list = new);
175 while (p->next != NULL) p = p->next;
180 /* figure out which symbols are branch targets */
184 struct symbol *p_sym;
185 struct target *p_targ;
187 while (p_targ != NULL) {
189 while (p_sym != NULL) {
190 if (!strcasecmp(p_sym->name, p_targ->name))
194 p_targ = p_targ->next;
198 void print_symbol_table()
203 printf("Sym in %-5s: %s\n", areaToString(p->area), p->name);
204 printf(" at: 0x%04X (%5d)", p->value, p->value);
205 printf(" Def:%s", p->isdef ? "Yes" : "No ");
206 printf(" Bit:%s", p->isbit ? "Yes" : "No ");
207 printf(" Target:%s", p->istarget ? "Yes" : "No ");
208 printf(" Line %d\n", p->line_def);
213 /* check that every symbol is in the table only once */
215 void check_redefine()
217 struct symbol *p1, *p2;
222 if (!strcasecmp(p1->name, p2->name)) {
223 fprintf(stderr, "Error: symbol '%s' redefined on line %d", p1->name, p2->line_def);
224 fprintf(stderr, ", first defined on line %d\n", p1->line_def);
233 int is_target(char *thename)
238 if (!strcasecmp(thename, p->name)) return (p->istarget);
244 int is_bit(char *thename)
249 if (!strcasecmp(thename, p->name)) return (p->isbit);
255 int is_reg(char *thename)
260 if (!strcasecmp(thename, p->name)) return (p->isreg);
267 int is_def(char *thename)
272 if (!strcasecmp(thename, p->name) && p->isdef) return(1);
278 /* this routine is used to dump a group of bytes to the output */
279 /* it is responsible for generating the list file and sending */
280 /* the bytes one at a time to the object code generator */
281 /* this routine is also responsible for generatine the list file */
282 /* though is it expected that the lexer has placed all the actual */
283 /* original text from the line in "last_line_text" */
285 void out(int *byte_list, int num)
289 if (num > 0) fprintf(list_fp, "%06X: ", MEM_POS);
290 else fprintf(list_fp, "\t");
292 for (i=0; i<num; i++) {
293 hexout(byte_list[i], MEM_POS + i, 0);
294 if (!first && (i % 4) == 0) fprintf(list_fp, "\t");
295 fprintf(list_fp, "%02X", byte_list[i]);
296 if ((i+1) % 4 == 0) {
297 if (first) fprintf(list_fp, "\t%s\n", last_line_text);
298 else fprintf(list_fp, "\n");
301 if (i<num-1) fprintf(list_fp, " ");
305 if (num < 3) fprintf(list_fp, "\t");
306 fprintf(list_fp, "\t%s\n", last_line_text);
308 if (num % 4) fprintf(list_fp, "\n");
313 /* add NOPs to align memory location on a valid branch target address */
317 static int nops[] = {NOP_OPCODE, NOP_OPCODE, NOP_OPCODE, NOP_OPCODE};
320 last_line_text[0] = '\0';
322 for(num=0; (MEM_POS + num) % BRANCH_SPACING; num++) ;
323 if (p3) out(nops, num);
327 /* print branch out of bounds error */
331 fprintf(stderr, "Error: branch out of bounds");
332 fprintf(stderr, " in line %d\n", lineno);
336 /* output the jump either direction on carry */
337 /* jump_dest and MEM_POS must have the proper values */
340 void do_jump_on_carry()
343 operand = REL4(jump_dest, MEM_POS);
347 if (operand > 15) boob_error();
348 out(0x20 + (operand & 15));
350 if (operand > 15) boob_error();
351 out(0x30 + (operand & 15));
357 /* turn a string like "10010110b" into an int */
359 int binary2int(char *str)
361 register int i, j=1, sum=0;
363 for (i=strlen(str)-2; i >= 0; i--) {
364 sum += j * (str[i] == '1');
373 /* todo: someday this will allow the user to control where the */
374 /* various memory areas go, and it will take care of assigning */
375 /* positions to area which follow others (such as OSEG getting */
376 /* set just after DSEG on the 2nd and 3rd passes when we have */
377 /* leared the size needed for each segment */
379 void init_areas(void)
381 area[AREA_CSEG].alloc_position = 0;
382 area[AREA_DSEG].alloc_position = 0x30;
383 area[AREA_OSEG].alloc_position = 0x80;
384 area[AREA_ISEG].alloc_position = 0;
385 area[AREA_BSEG].alloc_position = 0;
386 area[AREA_XSEG].alloc_position = 0;
387 area[AREA_XISEG].alloc_position = 0;
388 area[AREA_GSINIT].alloc_position = 0;
389 area[AREA_GSFINAL].alloc_position = 0;
390 area[AREA_HOME].alloc_position = 0;
394 /* pass #1 (p1=1) find all symbol defs and branch target names */
395 /* pass #2 (p2=1) align branch targets, evaluate all symbols */
396 /* pass #3 (p3=1) produce object code */
398 int main(int argc, char **argv)
400 char infilename[200], outfilename[200], listfilename[200];
402 if (argc < 2) print_usage();
403 strcpy(infilename, argv[1]);
404 if(strlen(infilename) > 3) {
405 if (strncasecmp(infilename+strlen(infilename)-3, ".xa", 3))
406 strcat(infilename, ".xa");
407 } else strcat(infilename, ".xa");
408 strcpy(outfilename, infilename);
409 outfilename[strlen(outfilename)-3] = '\0';
410 strcpy(listfilename, outfilename);
411 strcat(outfilename, ".hex");
412 strcat(listfilename, ".lst");
413 yyin = fopen(infilename, "r");
415 fprintf(stderr, "Can't open file '%s'.\n", infilename);
418 fhex = fopen(outfilename, "w");
420 fprintf(stderr, "Can't write file '%s'.\n", outfilename);
423 list_fp = fopen(listfilename, "w");
424 if (list_fp == NULL) {
425 fprintf(stderr, "Can't write file '%s'.\n", listfilename);
429 /* todo: add a command line option to supress verbose messages */
430 printf("\nPaul's XA51 Assembler\n");
431 printf("Copyright 1997,2002 Paul Stoffregen\n\n");
432 printf("This program is free software; you can redistribute it\n");
433 printf("and/or modify it under the terms of the GNU General Public\n");
434 printf("License, Version 2, published by the Free Software Foundation\n\n");
435 printf("This program is distributed in the hope that it will be useful,\n");
436 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
437 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
441 printf(" Building Symbol Table:\n");
447 print_symbol_table();
454 printf(" Aligning Branch Targets:\n");
458 // print_symbol_table();
464 printf(" Generating Object Code:\n");
469 hexout(0, 0, 1); /* flush and close intel hex file output */
476 fprintf(stderr, "Usage: xa_asm file\n");
477 fprintf(stderr, " or xa_asm file.asm\n");