-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsoloasm.c
166 lines (150 loc) · 4.51 KB
/
soloasm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**********************************************************************
axasm Copyright 2006, 2007, 2008, 2009
by Al Williams ([email protected]).
This file is part of axasm.
axasm is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public Licenses as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
axasm is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY: without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with axasm (see LICENSE.TXT).
If not, see http://www.gnu.org/licenses/.
If a non-GPL license is desired, contact the author.
This is the retargetable assembler core file
***********************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#define _SOLO_MAIN
#include "soloasm.h"
extern unsigned genasm(unsigned);
static FILE *outfile;
int main(int argc, char *argv[])
{
unsigned int endmem,i,j;
unsigned int siz;
unsigned mask;
int opt=0;
int mode=0;
outfile=stdout;
while (opt!=-1)
{
opt=getopt(argc,argv,"b8vhixo:");
switch (opt)
{
case '8':
mode=5; // 8 bit binary mode
break;
case 'b':
mode=4;
break;
case 'v':
mode=3;
break;
case 'i':
mode=2;
break;
case 'x':
mode=1;
break;
case 'h':
mode=0;
break;
case 'o':
// note -b must be before -o
outfile=fopen(optarg,mode==4?"r+":"w");
if (!outfile)
{
perror(optarg);
return 1;
}
break;
}
}
_solo_info.psize=32; //default size
endmem=genasm(1); // let labels get filled in
if (_solo_info.err==0) endmem=genasm(2); // pass 2 for real
if (_solo_info.err!=0)
{
fprintf(stderr,"Fatal error. No output generated.\n");
return 1;
}
siz=_solo_info.psize;
switch (mode)
{
case 1:
fprintf(outfile,";Generated by soloasm\n");
fprintf(outfile,"MEMORY_INITIALIZATION_RADIX=16;\n");
fprintf(outfile,"MEMORY_INITIALIZATION_VECTOR=\n");
for (i=_solo_info.begin;i<=_solo_info.end;i++) fprintf(outfile,"%0*X%c\n",siz/4,_solo_info.ary[i],i==_solo_info.end?';':',');
break;
case 2: // basic 64k intel hex file only; psize must be multiple of 8
mask=0xFF;
unsigned bytesper=16/(siz/8);
for (i=_solo_info.begin;i<=_solo_info.end;i+=bytesper)
{
/* need to compute # of output words on this line */
unsigned bsize;
unsigned lsize=bytesper; // nominal line size (round up)
if (i+lsize>_solo_info.end)
lsize=(_solo_info.end-i)+1;
bsize=lsize*bytesper;
unsigned csum=lsize+(i>>8)+(i&0xFF);
unsigned byt;
fprintf(outfile,":");
fprintf(outfile,"%02x%04x00",bsize,i);
for (j=0;j<lsize;j++)
{
fprintf(outfile,"%02x",_solo_info.ary[i+j]&mask);
csum+=_solo_info.ary[i+j]&mask;
if (siz>8)
{
fprintf(outfile,"%02x",byt=(_solo_info.ary[i+j]>>8)&mask);
csum+=byt;
}
if (siz>16)
{
fprintf(outfile,"%02x",byt=(_solo_info.ary[i+j]>>16)&mask);
csum+=byt;
}
if (siz>24)
{
fprintf(outfile,"%02x",byt=(_solo_info.ary[i+j]>>24)&mask);
csum+=byt;
}
}
fprintf(outfile,"%02x\n",((~csum)+1)&0xFF);
}
fprintf(outfile,":00000001FF\n");
break;
case 4: // binary for 32 bit targets only (arch dependent)
fseek(outfile,_solo_info.begin,SEEK_SET);
fwrite(_solo_info.ary+_solo_info.begin,1,(_solo_info.end+1-_solo_info.begin)*(siz/8),outfile);
break;
case 5:
fseek(outfile,_solo_info.begin,SEEK_SET);
for (int i=_solo_info.begin;i<=_solo_info.end;i++)
fputc(_solo_info.ary[i],outfile);
break;
default: // including mode=0;
if (mode==3) fprintf(outfile,"@%X ",_solo_info.begin);
for (i=_solo_info.begin;i<=_solo_info.end;i+=16)
{
if (mode!=3 && mode!=0)fprintf(outfile,"%0*X: ",siz==8?4:8,i);
for (j=0;j<16;j++)
if (i+j<=_solo_info.end)
{
if (mode==0) fprintf(outfile,"%0*X: ",siz==8?4:8,i+j);
fprintf(outfile,"%0*X ",(siz+3)/4,_solo_info.ary[i+j]);
if (mode==0) fprintf(outfile,"\n");
}
if (mode!=0) fprintf(outfile,"\n");
}
}
if (outfile!=stdout) fclose(outfile);
return 0;
}