]> git.sur5r.net Git - cc65/commitdiff
Added generation of C output.
authoruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 11 Mar 2012 12:38:54 +0000 (12:38 +0000)
committeruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 11 Mar 2012 12:38:54 +0000 (12:38 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@5607 b7a2c559-68d2-44c3-8de9-860c34a00d81

src/sp65/asm.c
src/sp65/c.c [new file with mode: 0644]
src/sp65/c.h [new file with mode: 0644]
src/sp65/make/gcc.mak
src/sp65/make/watcom.mak
src/sp65/output.c
src/sp65/output.h

index 4b699d40f698d68e932e323d927c2edc71e57712..61c4fea61999afdf2a8aee11f7bf3dfc1c1aebae 100644 (file)
@@ -45,7 +45,7 @@
 
 /* sp65 */
 #include "attr.h"
-#include "bin.h"
+#include "asm.h"
 #include "error.h"
 
 
diff --git a/src/sp65/c.c b/src/sp65/c.c
new file mode 100644 (file)
index 0000000..50f9bc4
--- /dev/null
@@ -0,0 +1,207 @@
+/*****************************************************************************/
+/*                                                                           */
+/*                                    c.c                                    */
+/*                                                                           */
+/*              C output for the sp65 sprite and bitmap utility              */
+/*                                                                           */
+/*                                                                           */
+/*                                                                           */
+/* (C) 2012,      Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
+/*                                                                           */
+/*                                                                           */
+/* This software is provided 'as-is', without any expressed or implied       */
+/* warranty.  In no event will the authors be held liable for any damages    */
+/* arising from the use of this software.                                    */
+/*                                                                           */
+/* Permission is granted to anyone to use this software for any purpose,     */
+/* including commercial applications, and to alter it and redistribute it    */
+/* freely, subject to the following restrictions:                            */
+/*                                                                           */
+/* 1. The origin of this software must not be misrepresented; you must not   */
+/*    claim that you wrote the original software. If you use this software   */
+/*    in a product, an acknowledgment in the product documentation would be  */
+/*    appreciated but is not required.                                       */
+/* 2. Altered source versions must be plainly marked as such, and must not   */
+/*    be misrepresented as being the original software.                      */
+/* 3. This notice may not be removed or altered from any source              */
+/*    distribution.                                                          */
+/*                                                                           */
+/*****************************************************************************/
+
+
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+/* common */
+#include "chartype.h"
+#include "check.h"
+#include "cmdline.h"
+#include "version.h"
+
+/* sp65 */
+#include "attr.h"
+#include "c.h"
+#include "error.h"
+
+
+
+/*****************************************************************************/
+/*                                   Code                                    */
+/*****************************************************************************/
+
+
+
+static int ValidIdentifier (const char* L)
+/* Check a C identifier for validity */
+{
+    /* Must begin with underscore or alphabetic character */
+    if (*L != '_' && !IsAlpha (*L)) {
+        return 0;
+    }
+    ++L;
+
+    /* Remainder must be as above plus digits */
+    while (*L) {
+        if (*L != '_' && !IsAlNum (*L)) {
+            return 0;
+        }
+        ++L;
+    }
+
+    /* Ok */
+    return 1;
+}
+
+
+
+static unsigned GetBytesPerLine (const Collection* A)
+/* Return the number of bytes per line from the attribute collection A */
+{
+    char        C;
+    unsigned    BytesPerLine = 16;
+
+    /* Check for a bytesperline attribute */
+    const char* V = GetAttrVal (A, "bytesperline");
+    if ((V && sscanf (V, "%u%c", &BytesPerLine, &C) != 1) ||
+        (BytesPerLine < 1 || BytesPerLine > 64)) {
+        Error ("Invalid value for attribute `bytesperline'");
+    }
+    return BytesPerLine;
+}
+
+
+
+static unsigned GetBase (const Collection* A)
+/* Return the number base from the attribute collection A */
+{
+    char        C;
+    unsigned    Base = 16;
+
+    /* Check for a base attribute */
+    const char* V = GetAttrVal (A, "base");
+    if ((V && sscanf (V, "%u%c", &Base, &C) != 1) || (Base != 10 && Base != 16)) {
+        Error ("Invalid value for attribute `base'");
+    }
+    return Base;
+}
+
+
+
+static const char* GetIdentifier (const Collection* A)
+/* Return the variable identifier from the attribute collection A */
+{
+    /* Check for a ident attribute */
+    const char* Ident = NeedAttrVal (A, "ident", "write");
+    if (!ValidIdentifier (Ident)) {
+        Error ("Invalid value for attribute `ident'");
+    }
+    return Ident;
+}
+
+
+
+void WriteCFile (const StrBuf* Data, const Collection* A)
+/* Write the contents of Data to a file in C format */
+{
+    FILE*       F;
+    const char* D;
+    unsigned    Size;
+
+
+    /* Get the file name */
+    const char* Name = NeedAttrVal (A, "name", "write");
+
+    /* Check the number of bytes per line */
+    unsigned BytesPerLine = GetBytesPerLine (A);
+
+    /* Get the number base */
+    unsigned Base = GetBase (A);
+
+    /* Get the identifier */
+    const char* Ident = GetIdentifier (A);
+
+    /* Open the output file */
+    F = fopen (Name, "w");
+    if (F == 0) {
+        Error ("Cannot open output file `%s': %s", Name, strerror (errno));
+    }
+
+    /* Write a readable header */
+    fprintf (F,
+             "/*\n"
+             " * This file was generated by %s %s\n"
+             " */\n"
+             "\n",
+             ProgName,
+             GetVersionAsString ());
+
+
+    /* Output the declaration and identifier */
+    fprintf (F, "const unsigned char %s[] = {\n", Ident);
+
+    /* Write the data */
+    D    = SB_GetConstBuf (Data);
+    Size = SB_GetLen (Data);
+    while (Size) {
+
+        unsigned I;
+
+        /* Output one line */
+        unsigned Chunk = Size;
+        if (Chunk > BytesPerLine) {
+            Chunk = BytesPerLine;
+        }
+        fputs ("    ", F);
+        for (I = 0; I < Chunk; ++I) {
+            switch (Base) {
+                case 10:
+                    fprintf (F, "%u,", *D++);
+                    break;
+                case 16:
+                    fprintf (F, "0x%02X,", *D++);
+                    break;
+
+            }
+        }
+        fputc ('\n', F);
+
+        /* Bump the counters */
+        Size -= Chunk;
+    }
+
+    /* Terminate the array */
+    fputs ("};\n", F);
+
+    /* Close the file */
+    if (fclose (F) != 0) {
+        Error ("Error closing output file `%s': %s", Name, strerror (errno));
+    }
+}
+
+
+
diff --git a/src/sp65/c.h b/src/sp65/c.h
new file mode 100644 (file)
index 0000000..8b32247
--- /dev/null
@@ -0,0 +1,63 @@
+/*****************************************************************************/
+/*                                                                           */
+/*                                    c.h                                    */
+/*                                                                           */
+/*              C output for the sp65 sprite and bitmap utility              */
+/*                                                                           */
+/*                                                                           */
+/*                                                                           */
+/* (C) 2012,      Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
+/*                                                                           */
+/*                                                                           */
+/* This software is provided 'as-is', without any expressed or implied       */
+/* warranty.  In no event will the authors be held liable for any damages    */
+/* arising from the use of this software.                                    */
+/*                                                                           */
+/* Permission is granted to anyone to use this software for any purpose,     */
+/* including commercial applications, and to alter it and redistribute it    */
+/* freely, subject to the following restrictions:                            */
+/*                                                                           */
+/* 1. The origin of this software must not be misrepresented; you must not   */
+/*    claim that you wrote the original software. If you use this software   */
+/*    in a product, an acknowledgment in the product documentation would be  */
+/*    appreciated but is not required.                                       */
+/* 2. Altered source versions must be plainly marked as such, and must not   */
+/*    be misrepresented as being the original software.                      */
+/* 3. This notice may not be removed or altered from any source              */
+/*    distribution.                                                          */
+/*                                                                           */
+/*****************************************************************************/
+
+
+
+#ifndef C_H
+#define C_H
+
+
+
+/* common */
+#include "coll.h"
+#include "strbuf.h"
+
+
+
+/*****************************************************************************/
+/*                                   Code                                    */
+/*****************************************************************************/
+
+
+
+void WriteCFile (const StrBuf* Data, const Collection* A);
+/* Write the contents of Data to a file in C format */
+
+
+
+/* End of c.h */
+
+#endif
+
+
+
index d72a7fc163515c79dc418993e80e7e01363ab90f..6a47c36ee1d7cb972f0a5876eb467a665b069563 100644 (file)
@@ -26,6 +26,7 @@ OBJS =        asm.o           \
         attr.o          \
         bin.o           \
         bitmap.o        \
+        c.o             \
         color.o         \
         convert.o       \
         error.o                \
index 41b4ac6019da6e66ebd70c69722a284bb429fdd6..c5b242a7879537c70d63629a87fab0725d6a9535 100644 (file)
@@ -64,6 +64,7 @@ OBJS =  asm.obj         \
         attr.obj        \
         bin.obj         \
         bitmap.obj      \
+        c.obj           \
         color.obj       \
         convert.obj     \
         error.obj      \
index 4726a5c6f7f2212623e35b53c0a74baca05ef8ff..40a7091033a39b30b8967176d3d37b14b01408d0 100644 (file)
@@ -42,6 +42,7 @@
 #include "asm.h"
 #include "attr.h"
 #include "bin.h"
+#include "c.h"
 #include "error.h"
 #include "output.h"
 
 
 
 
+/* Different types of output formats */
+enum OutputFormat {
+    ofAsm,                      /* Output assembler source */
+    ofBin,                      /* Output raw binary format */
+    ofC,                        /* Output C code */
+
+    ofCount                     /* Number of output formats without ofAuto */
+};
+
 typedef struct OutputFormatDesc OutputFormatDesc;
 struct OutputFormatDesc {
 
@@ -65,6 +75,7 @@ struct OutputFormatDesc {
 static OutputFormatDesc OutputFormatTable[ofCount] = {
     {   WriteAsmFile    },
     {   WriteBinFile    },
+    {   WriteCFile      },
 };
 
 /* Table that maps extensions to Output formats. Must be sorted alphabetically */
@@ -73,12 +84,14 @@ static const FileId FormatTable[] = {
     {   "A",    ofAsm           },
     {   "ASM",  ofAsm           },
     {   "BIN",  ofBin           },
+    {   "C",    ofC             },
     {   "INC",  ofAsm           },
     {   "S",    ofAsm           },
 
     {   "a",    ofAsm           },
     {   "asm",  ofAsm           },
     {   "bin",  ofBin           },
+    {   "c",    ofC             },
     {   "inc",  ofAsm           },
     {   "s",    ofAsm           },
 };
index abedd011eebdf1d7ee0b9e5c35c742c1dd56cf94..2c2bbb056a2c66de8b2b0b954d4c74a9a919cf59 100644 (file)
 
 
 
-/*****************************************************************************/
-/*                                   Data                                    */
-/*****************************************************************************/
-
-
-
-enum OutputFormat {
-    ofAuto      = -1,           /* Auto detect */
-    ofAsm,                      /* Output assembler source */
-    ofBin,                      /* Output raw binary format */
-
-    ofCount                     /* Number of output formats without ofAuto */
-};
-typedef enum OutputFormat OutputFormat;
-
-
-
-
 /*****************************************************************************/
 /*                                   Code                                    */
 /*****************************************************************************/