1 /* xa65 - 65xx/65816 cross-assembler and utility suite
2 *
3 * Copyright (C) 1989-1997 Andr� Fachat (a.fachat@physik.tu-chemnitz.de)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "xah.h"
24 #include "xa.h"
25 #include "xar.h"
26
27 /*
28 static relocateInfo *rlist = NULL;
29 static int mlist = 0, nlist = 0;
30 static int first = -1;
31 */
32
33 /* int rmode; */
34
rd_set(int pc,int afl,int l,int lab)35 int rd_set(int pc, int afl, int l, int lab) {
36 int p,pp;
37
38 /* if(!rmode) return 0; */
39
40 /*printf("set relocation @$%04x, l=%d, afl=%04x\n",pc, l, afl);*/
41
42 if(l==2 && ((afl & A_MASK)!=A_ADR)) {
43 errout(W_BYTRELOC);
44 /*printf("Warning: byte relocation in word value at PC=$%04x!\n",pc);*/
45 }
46 if(l==1 && ((afl&A_MASK)==A_ADR)) {
47 if((afl & A_FMASK) != (SEG_ZERO<<8)) errout(W_ADRRELOC);
48 /*printf("Warning: cutting address relocation in byte value at PC=$%04x!\n",pc);*/
49 afl = (afl & (~A_MASK)) | A_LOW;
50 }
51
52 if(afile->rd.nlist>=afile->rd.mlist) {
53 afile->rd.mlist+=500;
54 afile->rd.rlist=realloc(afile->rd.rlist, afile->rd.mlist*sizeof(relocateInfo));
55 }
56 if(!afile->rd.rlist) {
57 fprintf(stderr, "Oops: no memory for relocation table!\n");
58 exit(1);
59 }
60
61 afile->rd.rlist[afile->rd.nlist].adr = pc;
62 afile->rd.rlist[afile->rd.nlist].afl = afl;
63 afile->rd.rlist[afile->rd.nlist].lab = lab;
64 afile->rd.rlist[afile->rd.nlist].next= -1;
65
66 /* sorting this into the list is not optimized, to be honest... */
67 if(afile->rd.first<0) {
68 afile->rd.first = afile->rd.nlist;
69 } else {
70 p=afile->rd.first; pp=-1;
71 while(afile->rd.rlist[p].adr<pc && afile->rd.rlist[p].next>=0) {
72 pp=p;
73 p=afile->rd.rlist[p].next;
74 }
75 /*
76 printf("endloop: p=%d(%04x), pp=%d(%04x), nlist=%d(%04x)\n",
77 p,p<0?0:afile->rd.rlist[p].adr,pp,pp<0?0:afile->rd.rlist[pp].adr,afile->rd.nlist,afile->rd.nlist<0?0:afile->rd.rlist[afile->rd.nlist].adr);
78 */
79 if(afile->rd.rlist[p].next<0 && afile->rd.rlist[p].adr<pc) {
80 afile->rd.rlist[p].next=afile->rd.nlist;
81 } else
82 if(pp==-1) {
83 afile->rd.rlist[afile->rd.nlist].next = afile->rd.first;
84 afile->rd.first = afile->rd.nlist;
85 } else {
86 afile->rd.rlist[afile->rd.nlist].next = p;
87 afile->rd.rlist[pp].next = afile->rd.nlist;
88 }
89 }
90 afile->rd.nlist++;
91
92 return 0;
93 }
94
rd_write(FILE * fp,int pc)95 int rd_write(FILE *fp, int pc) {
96 int p=afile->rd.first;
97 int pc2, afl;
98
99 while(p>=0) {
100 pc2=afile->rd.rlist[p].adr;
101 afl=afile->rd.rlist[p].afl;
102 /*printf("rd_write: pc=%04x, pc2=%04x, afl=%x\n",pc,pc2,afl);*/
103 /* hack to switch undef and abs flag from internal to file format */
104 if( ((afl & A_FMASK)>>8) < SEG_TEXT) afl^=0x100;
105 if((pc2-pc) < 0) {
106 fprintf(stderr, "Oops, negative offset!\n");
107 } else {
108 while((pc2-pc)>254) {
109 fputc(255,fp);
110 pc+=254;
111 }
112 fputc(pc2-pc, fp);
113 pc=pc2;
114 fputc((afl>>8)&255, fp);
115 if((afile->rd.rlist[p].afl&A_FMASK)==(SEG_UNDEF<<8)) {
116 fputc(afile->rd.rlist[p].lab & 255, fp);
117 fputc((afile->rd.rlist[p].lab>>8) & 255, fp);
118 }
119 if((afl&A_MASK)==A_HIGH) fputc(afl&255,fp);
120 }
121 p=afile->rd.rlist[p].next;
122 }
123 fputc(0, fp);
124
125 free(afile->rd.rlist);
126 afile->rd.rlist = NULL;
127 afile->rd.mlist = afile->rd.nlist = 0;
128 afile->rd.first = -1;
129
130 return 0;
131 }
132
133