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 */
25 #define printf(x...) fprintf(stderr,x)
29 extern void yyrestart(FILE *new_file);
30 extern void hexout(int byte, int memory_location, int end);
34 /* global variables */
36 FILE *fhex, *fmem, *list_fp;
39 extern char last_line_text[];
40 struct symbol *sym_list=NULL;
41 struct target *targ_list=NULL;
44 int expr_result, expr_ok, jump_dest, inst;
46 char symbol_name[1000];
47 struct area_struct area[NUM_AREAS];
48 int current_area=AREA_CSEG;
51 char *areaToString (int area) {
54 case AREA_CSEG: return "CSEG";
55 case AREA_DSEG: return "DSEG";
56 case AREA_OSEG: return "OSEG";
57 case AREA_ISEG: return "ISEG";
58 case AREA_BSEG: return "BSEG";
59 case AREA_XSEG: return "XSEG";
60 case AREA_XISEG: return "XISEG";
61 case AREA_XINIT: return "XINIT";
66 /* "mem" is replaced by area[current_area].alloc_position */
67 /* int mem=0; */ /* mem is location in memory */
69 /* add symbols to list when we find their definition in pass #1 */
70 /* we will evaluate their values in pass #2, and figure out if */
71 /* they are branch targets betweem passes 1 and 2. Every symbol */
72 /* should appear exactly once in this list, since it can't be redefined */
74 struct symbol * build_sym_list(char *thename)
76 struct symbol *new, *p;
78 /* printf(" Symbol: %s Line: %d\n", thename, lineno); */
79 new = (struct symbol *) malloc(sizeof(struct symbol));
80 new->name = (char *) malloc(strlen(thename)+1);
81 strcpy(new->name, thename);
87 new->line_def = lineno - 1;
88 new->area = current_area;
90 if (sym_list == NULL) return (sym_list = new);
92 while (p->next != NULL) p = p->next;
97 int assign_value(char *thename, int thevalue)
103 if (!(strcasecmp(thename, p->name))) {
110 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
114 int mk_bit(char *thename)
120 if (!(strcasecmp(thename, p->name))) {
126 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
131 int mk_reg(char *thename)
137 if (!(strcasecmp(thename, p->name))) {
143 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
149 int get_value(char *thename)
154 if (!(strcasecmp(thename, p->name)))
158 fprintf(stderr, "Internal Error! Couldn't find symbol value\n");
164 /* add every branch target to this list as we find them */
165 /* ok if multiple entries of same symbol name in this list */
167 struct target * build_target_list(char *thename)
169 struct target *new, *p;
170 new = (struct target *) malloc(sizeof(struct target));
171 new->name = (char *) malloc(strlen(thename)+1);
172 strcpy(new->name, thename);
174 if (targ_list == NULL) return (targ_list = new);
176 while (p->next != NULL) p = p->next;
181 /* figure out which symbols are branch targets */
185 struct symbol *p_sym;
186 struct target *p_targ;
188 while (p_targ != NULL) {
190 while (p_sym != NULL) {
191 if (!strcasecmp(p_sym->name, p_targ->name))
195 p_targ = p_targ->next;
199 void print_symbol_table()
204 printf("Sym in %-5s: %s\n", areaToString(p->area), p->name);
205 printf(" at: 0x%04X (%5d)", p->value, p->value);
206 printf(" Def:%s", p->isdef ? "Yes" : "No ");
207 printf(" Bit:%s", p->isbit ? "Yes" : "No ");
208 printf(" Target:%s", p->istarget ? "Yes" : "No ");
209 printf(" Line %d\n", p->line_def);
214 /* check that every symbol is in the table only once */
216 void check_redefine()
218 struct symbol *p1, *p2;
223 if (!strcasecmp(p1->name, p2->name)) {
224 fprintf(stderr, "Error: symbol '%s' redefined on line %d", p1->name, p2->line_def);
225 fprintf(stderr, ", first defined on line %d\n", p1->line_def);
234 int is_target(char *thename)
239 if (!strcasecmp(thename, p->name)) return (p->istarget);
245 int is_bit(char *thename)
250 if (!strcasecmp(thename, p->name)) return (p->isbit);
256 int is_reg(char *thename)
261 if (!strcasecmp(thename, p->name)) return (p->isreg);
268 int is_def(char *thename)
273 if (!strcasecmp(thename, p->name) && p->isdef) return(1);
279 /* this routine is used to dump a group of bytes to the output */
280 /* it is responsible for generating the list file and sending */
281 /* the bytes one at a time to the object code generator */
282 /* this routine is also responsible for generatine the list file */
283 /* though is it expected that the lexer has placed all the actual */
284 /* original text from the line in "last_line_text" */
286 void out(int *byte_list, int num)
290 if (num > 0) fprintf(list_fp, "%06X: ", MEM_POS);
291 else fprintf(list_fp, "\t");
293 for (i=0; i<num; i++) {
294 hexout(byte_list[i], MEM_POS + i, 0);
295 if (!first && (i % 4) == 0) fprintf(list_fp, "\t");
296 fprintf(list_fp, "%02X", byte_list[i]);
297 if ((i+1) % 4 == 0) {
298 if (first) fprintf(list_fp, "\t%s\n", last_line_text);
299 else fprintf(list_fp, "\n");
302 if (i<num-1) fprintf(list_fp, " ");
306 if (num < 3) fprintf(list_fp, "\t");
307 fprintf(list_fp, "\t%s\n", last_line_text);
309 if (num % 4) fprintf(list_fp, "\n");
314 /* add NOPs to align memory location on a valid branch target address */
318 static int nops[] = {NOP_OPCODE, NOP_OPCODE, NOP_OPCODE, NOP_OPCODE};
321 last_line_text[0] = '\0';
323 for(num=0; (MEM_POS + num) % BRANCH_SPACING; num++) ;
324 if (p3) out(nops, num);
328 /* print branch out of bounds error */
332 fprintf(stderr, "Error: branch out of bounds");
333 fprintf(stderr, " in line %d\n", lineno);
337 /* output the jump either direction on carry */
338 /* jump_dest and MEM_POS must have the proper values */
341 void do_jump_on_carry()
344 operand = REL4(jump_dest, MEM_POS);
348 if (operand > 15) boob_error();
349 out(0x20 + (operand & 15));
351 if (operand > 15) boob_error();
352 out(0x30 + (operand & 15));
358 /* turn a string like "10010110b" into an int */
360 int binary2int(char *str)
362 register int i, j=1, sum=0;
364 for (i=strlen(str)-2; i >= 0; i--) {
365 sum += j * (str[i] == '1');
374 /* todo: someday this will allow the user to control where the */
375 /* various memory areas go, and it will take care of assigning */
376 /* positions to area which follow others (such as OSEG getting */
377 /* set just after DSEG on the 2nd and 3rd passes when we have */
378 /* leared the size needed for each segment */
380 void init_areas(void)
382 area[AREA_CSEG].alloc_position = 0;
383 area[AREA_DSEG].alloc_position = 0x30;
384 area[AREA_OSEG].alloc_position = 0x80;
385 area[AREA_ISEG].alloc_position = 0;
386 area[AREA_BSEG].alloc_position = 0;
387 area[AREA_XSEG].alloc_position = 0;
388 area[AREA_XISEG].alloc_position = 0;
389 area[AREA_GSINIT].alloc_position = 0;
390 area[AREA_GSFINAL].alloc_position = 0;
391 area[AREA_HOME].alloc_position = 0;
395 /* pass #1 (p1=1) find all symbol defs and branch target names */
396 /* pass #2 (p2=1) align branch targets, evaluate all symbols */
397 /* pass #3 (p3=1) produce object code */
399 int main(int argc, char **argv)
401 char infilename[200], outfilename[200], listfilename[200];
403 if (argc < 2) print_usage();
404 strcpy(infilename, argv[1]);
405 if(strlen(infilename) > 3) {
406 if (strncasecmp(infilename+strlen(infilename)-3, ".xa", 3))
407 strcat(infilename, ".xa");
408 } else strcat(infilename, ".xa");
409 strcpy(outfilename, infilename);
410 outfilename[strlen(outfilename)-3] = '\0';
411 strcpy(listfilename, outfilename);
412 strcat(outfilename, ".hex");
413 strcat(listfilename, ".lst");
414 yyin = fopen(infilename, "r");
416 fprintf(stderr, "Can't open file '%s'.\n", infilename);
419 fhex = fopen(outfilename, "w");
421 fprintf(stderr, "Can't write file '%s'.\n", outfilename);
424 list_fp = fopen(listfilename, "w");
425 if (list_fp == NULL) {
426 fprintf(stderr, "Can't write file '%s'.\n", listfilename);
430 /* todo: add a command line option to supress verbose messages */
431 printf("\nPaul's XA51 Assembler\n");
432 printf("Copyright 1997,2002 Paul Stoffregen\n\n");
433 printf("This program is free software; you can redistribute it\n");
434 printf("and/or modify it under the terms of the GNU General Public\n");
435 printf("License, Version 2, published by the Free Software Foundation\n\n");
436 printf("This program is distributed in the hope that it will be useful,\n");
437 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
438 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
442 printf(" Building Symbol Table:\n");
448 print_symbol_table();
455 printf(" Aligning Branch Targets:\n");
459 // print_symbol_table();
465 printf(" Generating Object Code:\n");
470 hexout(0, 0, 1); /* flush and close intel hex file output */
477 fprintf(stderr, "Usage: xa_asm file\n");
478 fprintf(stderr, " or xa_asm file.asm\n");