-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathasm48.c
More file actions
268 lines (230 loc) · 6.55 KB
/
Copy pathasm48.c
File metadata and controls
268 lines (230 loc) · 6.55 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Assembler for the Intel 8048 microcontroller family.
* Copyright (c) 2002,2003 David H. Hovemeyer <daveho@cs.umd.edu>
*
* Enhanced in 2012, 2013 by JustBurn and sy2002 of MEGA
* http://www.adventurevision.net
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_GETOPT
#include <unistd.h>
#endif
#include "asm48.h"
#ifndef HAVE_GETOPT
extern int opterr, optind, optopt, optreset;
extern char *optarg;
int getopt(int nargc, char * const *nargv, const char *ostr);
#endif
/* The list of generated instructions. */
struct Instruction *ins_head, *ins_tail;
/* Memory pool for internal objects. */
struct Pool *gen_pool;
/* Memory pool for assembled instructions. */
struct Pool *asm_pool;
/* Offset of instruction currently being assembled. */
int cur_offset;
/* Filename of currently source being assembled. */
char *cur_file;
/* Bank usage */
int bank_display = 0;
int bank_usage[BANK_USAGE_MAX];
/*
* Modify current filename
*/
void cur_file_set(const char *filename)
{
const char *s = filename + strlen(filename);
while (s != filename) {
if ((*s == '\\') || (*s == '/')) { s++; break; }
s--;
}
cur_file = strdup(s); // It will leak but is required
}
/*
* Assemble all instructions (to resolve forward references).
*/
static void assemble(void)
{
struct Instruction *cur = ins_head;
while (cur != NULL) {
cur->vtable->assemble(cur);
cur = cur->next;
}
}
/*
* Print command line usage information.
*/
static void usage(void)
{
const char *msg =
"Usage: asm48 [options] <input file>\n"
"Options:\n"
" -v Print version number only and exit\n"
" -t Print ROM bank usage table\n"
" -s <filename> Export symbols list\n"
" -o <filename> Specify the name of the output file\n"
" -f (bin|hex) Specify output format (binary or Intel hex; default bin)\n";
fprintf(stderr, "%s", msg);
}
/*
* Output a flat binary file.
*/
static void output_bin(const char *filename)
{
FILE *fp = fopen(filename, "wb");
if (fp == NULL)
err_printf("Couldn't open %s for output: %s\n", filename, strerror(errno));
if (fwrite(asm_pool->buf, 1, cur_offset, fp) != cur_offset)
err_printf("Failed to write %d bytes of output to %s: %s\n", cur_offset, filename, strerror(errno));
printf(" Assembled %d bytes.\n", cur_offset);
fclose(fp);
}
/*
* Output Intel hex format.
*/
static void output_hex(const char *filename)
{
extern int memory[];
unsigned char *ptr = (unsigned char *) asm_pool->buf;
int i;
char cmd_str[256];
for (i = 0; i < cur_offset; ++i)
memory[i] = *ptr++;
sprintf(cmd_str, "S 0 %x %s", cur_offset - 1, filename);
save_file(cmd_str);
}
/* Name of input file. */
static const char *input_file;
/* Name of output file. */
static char *output_file = NULL;
/* Output function to emit assembled instructions. */
static void (*output_func)(const char *) = &output_bin;
/* Suffix of output file (if automatically generated from input filename. */
static const char *output_suffix = ".bin";
/* Name of symbols file. */
static char *symbols_file = NULL;
/*
* Parse command line options.
*/
static void parse_options(int argc, char **argv)
{
int opt;
opterr = 0;
while ((opt = getopt(argc, argv, "vts:o:f:")) != -1) {
switch (opt) {
case 'v':
exit(0);
case 't':
bank_display = 1;
break;
case 's':
symbols_file = optarg;
break;
case 'o':
output_file = optarg;
break;
case 'f':
if (strcmp(optarg, "bin") == 0) {
output_func = &output_bin;
output_suffix = ".bin";
} else if (strcmp(optarg, "hex") == 0) {
output_func = &output_hex;
output_suffix = ".hex";
} else {
fprintf(stderr, "Unknown output format \"%s\"\n", optarg);
usage();
exit(1);
}
break;
case '?':
fprintf(stderr, "Unknown option '%c'\n", optopt);
usage();
exit(1);
}
}
/*
* The last command line argument should be
* the input file.
*/
if (optind != argc - 1) {
usage();
exit(1);
}
input_file = argv[optind];
/*
* If no output file was specified, transform the name of the input file,
* adding a suitable file extension.
*/
if (output_file == NULL) {
char *input_suffix = strrchr(input_file, '.');
size_t ilen = strlen(input_file);
size_t osfxlen = strlen(output_suffix);
if (input_suffix == NULL) {
output_file = (char *) malloc(ilen + osfxlen + 1);
strcpy(output_file, input_file);
strcat(output_file, output_suffix);
} else {
size_t ibaselen = (input_suffix - input_file);
output_file = (char *) malloc(ibaselen + osfxlen + 1);
memcpy(output_file, input_file, ibaselen);
strcpy(output_file + ibaselen, output_suffix);
}
}
}
/*
* main() function.
*/
int main(int argc, char **argv)
{
extern FILE *yyin;
extern void yyparse(void);
int i;
fprintf(stderr, "*** asm48 v" VERSION " ***\n");
parse_options(argc, argv);
yyin = fopen(input_file, "r");
if (yyin == NULL) {
err_printf("Couldn't open input file %s: %s\n", input_file, strerror(errno));
exit(1);
}
memset(bank_usage, 0, sizeof(bank_usage));
gen_pool = create_pool(GEN_POOL_SIZE);
asm_pool = create_pool(ASM_POOL_SIZE);
cur_file_set(input_file);
yyparse();
assemble();
if (symbols_file) export_symbols(symbols_file);
output_func(output_file);
if (bank_display) {
printf("\n ROM banks usage:\n");
for (i=0; i<BANK_USAGE_MAX; i++) {
if (bank_usage[i]) {
printf(" bank%3d, %4d occupied, %4d free, %3d%% usage\n",
i, bank_usage[i], 256 - bank_usage[i], bank_usage[i] * 100 / 256);
}
}
}
return 0;
}