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 */
72 "Usage: %s [options] file\n"
74 " -V\t\t\tPrint the version number\n"
75 " -g\t\t\tAdd debug info to object file\n"
76 " -h\t\t\tHelp (this text)\n"
77 " -m model\t\tOverride the o65 model\n"
78 " -n\t\t\tDon't generate an output file\n"
79 " -o name\t\tName the output file\n"
80 " -v\t\t\tIncrease verbosity\n"
83 " --bss-label name\tDefine and export a BSS segment label\n"
84 " --bss-name seg\tSet the name of the BSS segment\n"
85 " --code-label name\tDefine and export a CODE segment label\n"
86 " --code-name seg\tSet the name of the CODE segment\n"
87 " --data-label name\tDefine and export a DATA segment label\n"
88 " --data-name seg\tSet the name of the DATA segment\n"
89 " --debug-info\t\tAdd debug info to object file\n"
90 " --help\t\tHelp (this text)\n"
91 " --no-output\t\tDon't generate an output file\n"
92 " --o65-model model\tOverride the o65 model\n"
93 " --verbose\t\tIncrease verbosity\n"
94 " --version\t\tPrint the version number\n"
95 " --zeropage-label name\tDefine and export a ZEROPAGE segment label\n"
96 " --zeropage-name seg\tSet the name of the ZEROPAGE segment\n",
102 static void CheckLabelName (const char* Label)
103 /* Check if the given label is a valid label name */
105 const char* L = Label;
107 if (strlen (L) < 256 && (IsAlpha (*L) || *L== '_')) {
109 if (!IsAlNum (*L) && *L != '_') {
116 Error ("Label name `%s' is invalid", Label);
122 static void CheckSegName (const char* Seg)
123 /* Abort if the given name is not a valid segment name */
125 /* Print an error and abort if the name is not ok */
126 if (!ValidSegName (Seg)) {
127 Error ("Segment name `%s' is invalid", Seg);
133 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
134 /* Handle the --bss-label option */
136 /* Check for a label name */
137 CheckLabelName (Arg);
140 BssLabel = xstrdup (Arg);
145 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
146 /* Handle the --bss-name option */
148 /* Check for a valid name */
152 BssSeg = xstrdup (Arg);
157 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
158 /* Handle the --code-label option */
160 /* Check for a label name */
161 CheckLabelName (Arg);
164 CodeLabel = xstrdup (Arg);
169 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
170 /* Handle the --code-name option */
172 /* Check for a valid name */
176 CodeSeg = xstrdup (Arg);
181 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
182 /* Handle the --data-label option */
184 /* Check for a label name */
185 CheckLabelName (Arg);
188 DataLabel = xstrdup (Arg);
193 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
194 /* Handle the --data-name option */
196 /* Check for a valid name */
200 DataSeg = xstrdup (Arg);
205 static void OptDebug (const char* Opt attribute ((unused)),
206 const char* Arg attribute ((unused)))
207 /* Enable debugging code */
214 static void OptDebugInfo (const char* Opt attribute ((unused)),
215 const char* Arg attribute ((unused)))
216 /* Add debug info to the object file */
223 static void OptHelp (const char* Opt attribute ((unused)),
224 const char* Arg attribute ((unused)))
225 /* Print usage information and exit */
233 static void OptNoOutput (const char* Opt attribute ((unused)),
234 const char* Arg attribute ((unused)))
235 /* Handle the --no-output option */
242 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
243 /* Handle the --o65-model option */
245 /* Search for the model name */
246 Model = FindModel (Arg);
247 if (Model == O65_MODEL_INVALID) {
248 Error ("Unknown o65 model `%s'", Arg);
254 static void OptVerbose (const char* Opt attribute ((unused)),
255 const char* Arg attribute ((unused)))
256 /* Increase verbosity */
263 static void OptVersion (const char* Opt attribute ((unused)),
264 const char* Arg attribute ((unused)))
265 /* Print the assembler version */
268 "co65 V%u.%u.%u - (C) Copyright 1998-2003 Ullrich von Bassewitz\n",
269 VER_MAJOR, VER_MINOR, VER_PATCH);
274 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
275 /* Handle the --zeropage-label option */
277 /* Check for a label name */
278 CheckLabelName (Arg);
281 ZeropageLabel = xstrdup (Arg);
286 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
287 /* Handle the --zeropage-name option */
289 /* Check for a valid name */
293 ZeropageSeg = xstrdup (Arg);
298 static void DoConversion (void)
299 /* Do file conversion */
301 /* Read the o65 file into memory */
302 O65Data* D = ReadO65File (InputName);
304 /* Do the conversion */
307 /* Free the o65 module data */
314 int main (int argc, char* argv [])
315 /* Converter main program */
317 /* Program long options */
318 static const LongOpt OptTab[] = {
319 { "--bss-label", 1, OptBssLabel },
320 { "--bss-name", 1, OptBssName },
321 { "--code-label", 1, OptCodeLabel },
322 { "--code-name", 1, OptCodeName },
323 { "--data-label", 1, OptDataLabel },
324 { "--data-name", 1, OptDataName },
325 { "--debug", 0, OptDebug },
326 { "--debug-info", 0, OptDebugInfo },
327 { "--help", 0, OptHelp },
328 { "--no-output", 0, OptNoOutput },
329 { "--o65-model", 1, OptO65Model },
330 { "--verbose", 0, OptVerbose },
331 { "--version", 0, OptVersion },
332 { "--zeropage-label", 1, OptZeropageLabel },
333 { "--zeropage-name", 1, OptZeropageName },
338 /* Initialize the cmdline module */
339 InitCmdLine (&argc, &argv, "co65");
341 /* Check the parameters */
343 while (I < ArgCount) {
345 /* Get the argument */
346 const char* Arg = ArgVec [I];
348 /* Check for an option */
349 if (Arg [0] == '-') {
353 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
357 OptDebugInfo (Arg, 0);
365 OptO65Model (Arg, GetArg (&I, 2));
369 OptNoOutput (Arg, 0);
373 OutputName = GetArg (&I, 2);
390 /* Filename. Check if we already had one */
392 Error ("Don't know what to do with `%s'\n", Arg);
402 /* Do we have an input file? */
403 if (InputName == 0) {
404 Error ("No input file\n");
407 /* Generate the name of the output file if none was specified */
408 if (OutputName == 0) {
409 OutputName = MakeFilename (InputName, AsmExt);
412 /* Do the conversion */
415 /* Return an apropriate exit code */