1 /*****************************************************************************/
5 /* Main program for the co65 object file converter */
9 /* (C) 2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
45 #include "debugflag.h"
62 /*****************************************************************************/
64 /*****************************************************************************/
68 static void Usage (void)
69 /* Print usage information and exit */
71 printf ("Usage: %s [options] file\n"
73 " -V\t\t\tPrint the version number\n"
74 " -g\t\t\tAdd debug info to object file\n"
75 " -h\t\t\tHelp (this text)\n"
76 " -m model\t\tOverride the o65 model\n"
77 " -n\t\t\tDon't generate an output file\n"
78 " -o name\t\tName the output file\n"
79 " -v\t\t\tIncrease verbosity\n"
82 " --bss-label name\tDefine and export a BSS segment label\n"
83 " --bss-name seg\tSet the name of the BSS segment\n"
84 " --code-label name\tDefine and export a CODE segment label\n"
85 " --code-name seg\tSet the name of the CODE segment\n"
86 " --data-label name\tDefine and export a DATA segment label\n"
87 " --data-name seg\tSet the name of the DATA segment\n"
88 " --debug-info\t\tAdd debug info to object file\n"
89 " --help\t\tHelp (this text)\n"
90 " --no-output\t\tDon't generate an output file\n"
91 " --o65-model model\tOverride the o65 model\n"
92 " --verbose\t\tIncrease verbosity\n"
93 " --version\t\tPrint the version number\n"
94 " --zeropage-label name\tDefine and export a ZEROPAGE segment label\n"
95 " --zeropage-name seg\tSet the name of the ZEROPAGE segment\n",
101 static void CheckLabelName (const char* Label)
102 /* Check if the given label is a valid label name */
104 const char* L = Label;
106 if (strlen (L) < 256 && (IsAlpha (*L) || *L== '_')) {
108 if (!IsAlNum (*L) && *L != '_') {
115 Error ("Label name `%s' is invalid", Label);
121 static void CheckSegName (const char* Seg)
122 /* Abort if the given name is not a valid segment name */
124 /* Print an error and abort if the name is not ok */
125 if (!ValidSegName (Seg)) {
126 Error ("Segment name `%s' is invalid", Seg);
132 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
133 /* Handle the --bss-label option */
135 /* Check for a label name */
136 CheckLabelName (Arg);
139 BssLabel = xstrdup (Arg);
144 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
145 /* Handle the --bss-name option */
147 /* Check for a valid name */
151 BssSeg = xstrdup (Arg);
156 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
157 /* Handle the --code-label option */
159 /* Check for a label name */
160 CheckLabelName (Arg);
163 CodeLabel = xstrdup (Arg);
168 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
169 /* Handle the --code-name option */
171 /* Check for a valid name */
175 CodeSeg = xstrdup (Arg);
180 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
181 /* Handle the --data-label option */
183 /* Check for a label name */
184 CheckLabelName (Arg);
187 DataLabel = xstrdup (Arg);
192 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
193 /* Handle the --data-name option */
195 /* Check for a valid name */
199 DataSeg = xstrdup (Arg);
204 static void OptDebug (const char* Opt attribute ((unused)),
205 const char* Arg attribute ((unused)))
206 /* Enable debugging code */
213 static void OptDebugInfo (const char* Opt attribute ((unused)),
214 const char* Arg attribute ((unused)))
215 /* Add debug info to the object file */
222 static void OptHelp (const char* Opt attribute ((unused)),
223 const char* Arg attribute ((unused)))
224 /* Print usage information and exit */
232 static void OptNoOutput (const char* Opt attribute ((unused)),
233 const char* Arg attribute ((unused)))
234 /* Handle the --no-output option */
241 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
242 /* Handle the --o65-model option */
244 /* Search for the model name */
245 Model = FindModel (Arg);
246 if (Model == O65_MODEL_INVALID) {
247 Error ("Unknown o65 model `%s'", Arg);
253 static void OptVerbose (const char* Opt attribute ((unused)),
254 const char* Arg attribute ((unused)))
255 /* Increase verbosity */
262 static void OptVersion (const char* Opt attribute ((unused)),
263 const char* Arg attribute ((unused)))
264 /* Print the assembler version */
267 "co65 V%u.%u.%u - (C) Copyright 1998-2003 Ullrich von Bassewitz\n",
268 VER_MAJOR, VER_MINOR, VER_PATCH);
273 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
274 /* Handle the --zeropage-label option */
276 /* Check for a label name */
277 CheckLabelName (Arg);
280 ZeropageLabel = xstrdup (Arg);
285 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
286 /* Handle the --zeropage-name option */
288 /* Check for a valid name */
292 ZeropageSeg = xstrdup (Arg);
297 static void DoConversion (void)
298 /* Do file conversion */
300 /* Read the o65 file into memory */
301 O65Data* D = ReadO65File (InputName);
303 /* Do the conversion */
306 /* Free the o65 module data */
313 int main (int argc, char* argv [])
314 /* Converter main program */
316 /* Program long options */
317 static const LongOpt OptTab[] = {
318 { "--bss-label", 1, OptBssLabel },
319 { "--bss-name", 1, OptBssName },
320 { "--code-label", 1, OptCodeLabel },
321 { "--code-name", 1, OptCodeName },
322 { "--data-label", 1, OptDataLabel },
323 { "--data-name", 1, OptDataName },
324 { "--debug", 0, OptDebug },
325 { "--debug-info", 0, OptDebugInfo },
326 { "--help", 0, OptHelp },
327 { "--no-output", 0, OptNoOutput },
328 { "--o65-model", 1, OptO65Model },
329 { "--verbose", 0, OptVerbose },
330 { "--version", 0, OptVersion },
331 { "--zeropage-label", 1, OptZeropageLabel },
332 { "--zeropage-name", 1, OptZeropageName },
337 /* Initialize the cmdline module */
338 InitCmdLine (&argc, &argv, "co65");
340 /* Check the parameters */
342 while (I < ArgCount) {
344 /* Get the argument */
345 const char* Arg = ArgVec [I];
347 /* Check for an option */
348 if (Arg [0] == '-') {
352 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
356 OptDebugInfo (Arg, 0);
364 OptO65Model (Arg, GetArg (&I, 2));
368 OptNoOutput (Arg, 0);
372 OutputName = GetArg (&I, 2);
389 /* Filename. Check if we already had one */
391 Error ("Don't know what to do with `%s'\n", Arg);
401 /* Do we have an input file? */
402 if (InputName == 0) {
403 Error ("No input file\n");
406 /* Generate the name of the output file if none was specified */
407 if (OutputName == 0) {
408 OutputName = MakeFilename (InputName, AsmExt);
411 /* Do the conversion */
414 /* Return an apropriate exit code */