408
|
1 /*
|
|
2 main.c
|
|
3
|
|
4 Copyright © 2010 William Astle
|
|
5
|
|
6 This file is part of LWTOOLS.
|
|
7
|
|
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
|
|
9 terms of the GNU General Public License as published by the Free Software
|
|
10 Foundation, either version 3 of the License, or (at your option) any later
|
|
11 version.
|
|
12
|
|
13 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
16 more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License along with
|
|
19 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20 */
|
|
21
|
|
22 #include <config.h>
|
|
23
|
|
24 #include <argp.h>
|
|
25 #include <stdio.h>
|
|
26 #include <stdlib.h>
|
|
27
|
|
28 #include <lw_alloc.h>
|
|
29 #include <lw_string.h>
|
|
30
|
|
31 #include "lwdisasm.h"
|
|
32
|
|
33 /* command line option handling */
|
|
34 const char *argp_program_version = "lwdisasmasm from " PACKAGE_STRING;
|
|
35 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
|
|
36 char *program_name;
|
|
37
|
|
38 static struct argp_option options[] =
|
|
39 {
|
|
40 { "output", 'o', "FILE", 0, "Output to FILE"},
|
|
41 { "debug", 'd', "LEVEL", OPTION_ARG_OPTIONAL, "Set debug mode"},
|
|
42 { "format", 'f', "TYPE", 0, "Select input format: decb, raw, obj, os9"},
|
|
43 { "decb", 'b', 0, 0, "Read DECB .bin format input, equivalent of --format=decb"},
|
|
44 { "raw", 'r', 0, 0, "Read raw binary format input, equivalent of --format=raw"},
|
|
45 { "obj", 0x100, 0, 0, "Read proprietary object file format, equivalent of --format=obj" },
|
|
46 { "6809", '9', 0, 0, "Set disassembler to 6809 only mode" },
|
|
47 { "6309", '3', 0, 0, "Set disassembler to 6309 mode (default)" },
|
|
48 { "data", 0x101, "RANGE", 0, "Set an address range as data" },
|
|
49 { "code", 0x102, "RANGE", 0, "Set an address range as code" },
|
|
50 { "entry", 0x103, "ADDR", 0, "Start disassembling at ADDR" },
|
|
51 { "base", 0x104, "ADDR", 0, "Set the base address of a raw file" },
|
|
52 { 0 }
|
|
53 };
|
|
54
|
|
55
|
|
56 static error_t parse_opts(int key, char *arg, struct argp_state *state)
|
|
57 {
|
|
58 disasmstate_t *as = state -> input;
|
|
59 int rangelow, rangehigh;
|
|
60 char *e;
|
|
61
|
|
62 switch (key)
|
|
63 {
|
|
64
|
|
65 case 0x101: // data range
|
|
66 rangelow = strtol(arg, &e, 0);
|
|
67 if (*e != ':')
|
|
68 {
|
|
69 fprintf(stderr, "Invalid range: %s\n", arg);
|
|
70 exit(1);
|
|
71 }
|
|
72 rangehigh = strtol(e + 1, NULL, 0);
|
|
73 if (rangelow < 0 || rangehigh < 0 || rangehigh < rangelow)
|
|
74 {
|
|
75 fprintf(stderr, "Invalid range: %s\n", arg);
|
|
76 exit(1);
|
|
77 }
|
|
78 // register the range as data
|
409
|
79 register_range(as, rangelow, rangehigh, type_data);
|
408
|
80 break;
|
|
81
|
|
82 case 0x102: // code range
|
|
83 rangelow = strtol(arg, &e, 0);
|
|
84 if (*e != ':')
|
|
85 {
|
|
86 fprintf(stderr, "Invalid range: %s\n", arg);
|
|
87 exit(1);
|
|
88 }
|
|
89 rangehigh = strtol(e + 1, NULL, 0);
|
|
90 if (rangelow < 0 || rangehigh < 0 || rangehigh < rangelow)
|
|
91 {
|
|
92 fprintf(stderr, "Invalid range: %s\n", arg);
|
|
93 exit(1);
|
|
94 }
|
|
95 // register the range as code
|
409
|
96 register_range(as, rangelow, rangehigh, type_code);
|
408
|
97 break;
|
|
98
|
|
99 case 0x103: // entry
|
|
100 as -> entry = strtol(arg, NULL, 0);
|
|
101 break;
|
|
102
|
|
103 case 0x104: // base
|
|
104 as -> base = strtol(arg, NULL, 0);
|
|
105 break;
|
|
106
|
|
107 case 'o':
|
|
108 if (as -> output_file)
|
|
109 lw_free(as -> output_file);
|
|
110 as -> output_file = lw_strdup(arg);
|
|
111 break;
|
|
112
|
|
113 case 'd':
|
|
114 if (!arg)
|
|
115 as -> debug_level = 50;
|
|
116 else
|
|
117 as -> debug_level = atoi(arg);
|
|
118 break;
|
|
119
|
|
120 case 'b':
|
|
121 as -> input_type = INPUT_DECB;
|
|
122 break;
|
|
123
|
|
124 case 'r':
|
|
125 as -> input_type = INPUT_RAW;
|
|
126 break;
|
|
127
|
|
128 case 0x100:
|
|
129 as -> input_type = INPUT_OBJ;
|
|
130 break;
|
|
131
|
|
132 case 'f':
|
|
133 if (!strcasecmp(arg, "decb"))
|
|
134 as -> input_type = INPUT_DECB;
|
|
135 else if (!strcasecmp(arg, "raw"))
|
|
136 as -> input_type = INPUT_RAW;
|
|
137 else if (!strcasecmp(arg, "obj"))
|
|
138 as -> input_type = INPUT_OBJ;
|
|
139 else if (!strcasecmp(arg, "os9"))
|
|
140 as -> input_type = INPUT_OS9;
|
|
141 else
|
|
142 {
|
|
143 fprintf(stderr, "Invalid input format: %s\n", arg);
|
|
144 exit(1);
|
|
145 }
|
|
146 break;
|
|
147
|
|
148 case '9':
|
|
149 as -> target = TARGET_6809;
|
|
150 break;
|
|
151
|
|
152 case '3':
|
|
153 as -> target = TARGET_6309;
|
|
154 break;
|
|
155
|
|
156 case ARGP_KEY_END:
|
|
157 break;
|
|
158
|
|
159 case ARGP_KEY_ARG:
|
|
160 if (as -> input_file)
|
|
161 {
|
|
162 fprintf(stderr, "Only one input file allowed\n");
|
|
163 exit(1);
|
|
164 }
|
|
165 as -> input_file = lw_strdup(arg);
|
|
166 break;
|
|
167
|
|
168 default:
|
|
169 return ARGP_ERR_UNKNOWN;
|
|
170 }
|
|
171 return 0;
|
|
172 }
|
|
173
|
|
174 static struct argp argp =
|
|
175 {
|
|
176 options,
|
|
177 parse_opts,
|
|
178 "<input file>",
|
|
179 "LWDISASM, a HD6309 and MC6809 disassembler"
|
|
180 };
|
|
181
|
|
182 /*
|
|
183 main function; parse command line, set up disassembler state, and run the
|
|
184 disassembler on the first file
|
|
185 */
|
|
186
|
409
|
187 void do_disasm_raw(disasmstate_t *as);
|
|
188
|
408
|
189 int main(int argc, char **argv)
|
|
190 {
|
|
191 disasmstate_t as = { 0 };
|
|
192 FILE *fp;
|
|
193
|
|
194 /* assembler state */
|
|
195 program_name = argv[0];
|
|
196
|
|
197 /* parse command line arguments */
|
|
198 argp_parse(&argp, argc, argv, 0, 0, &as);
|
|
199
|
|
200 if (as.input_file == NULL)
|
|
201 {
|
|
202 fprintf(stderr, "No input files specified.\n");
|
|
203 exit(1);
|
|
204 }
|
|
205
|
|
206 fp = fopen(as.input_file, "rb");
|
|
207 if (!fp)
|
|
208 {
|
|
209 perror("Cannot open input file");
|
|
210 exit(1);
|
|
211 }
|
|
212
|
|
213 fseek(fp, 0, SEEK_END);
|
|
214 as.filelen = ftell(fp);
|
|
215 rewind(fp);
|
|
216
|
|
217 as.filedata = lw_alloc(as.filelen);
|
|
218 0 == fread(as.filedata, as.filelen, 1, fp);
|
|
219
|
|
220 fclose(fp);
|
|
221
|
409
|
222 switch (as.target)
|
|
223 {
|
|
224 case TARGET_6309:
|
|
225 as.page0 = page0_6309;
|
|
226 as.page1 = page1_6309;
|
|
227 as.page2 = page2_6309;
|
|
228 break;
|
|
229
|
|
230 case TARGET_6809:
|
|
231 as.page0 = page0_6809;
|
|
232 as.page1 = page1_6809;
|
|
233 as.page2 = page2_6809;
|
|
234 break;
|
|
235
|
|
236 default:
|
|
237 as.page0 = page0_6809;
|
|
238 as.page1 = page1_6809;
|
|
239 as.page2 = page2_6809;
|
|
240 break;
|
|
241
|
|
242 }
|
|
243
|
|
244 switch (as.input_type)
|
|
245 {
|
|
246 case INPUT_RAW:
|
|
247 do_disasm_raw(&as);
|
|
248 break;
|
|
249
|
|
250 default:
|
|
251 fprintf(stderr, "Input type is not currently implemented\n");
|
|
252 exit(1);
|
|
253 }
|
|
254
|
408
|
255 exit(0);
|
|
256 }
|