changeset 126:d2b8166fa2a5

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 12 Jul 2018 14:35:42 +0300
parents c210b2e2cb47
children 4cebbee4b29b
files exporters.pde
diffstat 1 files changed, 125 insertions(+), 93 deletions(-) [+]
line wrap: on
line diff
--- a/exporters.pde	Thu Jul 12 14:24:53 2018 +0300
+++ b/exporters.pde	Thu Jul 12 14:35:42 2018 +0300
@@ -12,20 +12,26 @@
 
 public class MPWriteCtx
 {
-    public int m_offs, m_bpl, m_bytes, m_indent;
-    public String m_byteDef, m_eol, m_src;
-    public byte[] data;
+    public int m_offs, m_bpl, m_bytes, m_indent, m_state, m_intmode;
+    public String m_byteDef, m_eol, m_src, m_prefix;
+    public byte[] m_data;
 
     MPWriteCtx()
     {
         m_offs = 0;
-        m_bpl = 32;
+        m_data = new byte[1*1024];
+
+        setBPLOff();
         m_bytes = 0;
+        m_state = -1;
+
         m_indent = 4;
+
         m_byteDef = ".byte";
         m_src = "";
         m_eol = "\n";
-        m_data = new byte[1*1024];
+        m_prefix = "";
+        m_intmode = 1;
     }
 
     void setOffs(int offs)
@@ -38,6 +44,16 @@
         m_bpl = bpl;
     }
 
+    void setBPLOff()
+    {
+        m_bpl = -1;
+    }
+
+    void loadTemplate(String fname)
+    {
+        m_data = mpLoadBinaryFile("templates/" + fname);
+    }
+
     byte[] getData()
     {
         return m_data;
@@ -48,59 +64,72 @@
         return m_src;
     }
 
-    String getIndent()
+    String getByteStr(int val)
+    {
+        switch (m_intmode)
+        {
+            case 0: return str(val & 0xff);
+        }
+    }
+
+    boolean changeState(int nstate)
     {
-        String str = "";
-        for (int n = 0; n < m_indent; n++)
-            str += " ";
-        return str;
+        boolean changed = (nstate != m_state);
+        if (changed && m_state != -1)
+        {
+            m_src += m_eol;
+        }
+
+        m_state = nstate;
+        return changed;
+    }
+
+    void addEOL()
+    {
+        changeState(4);
+        m_src += m_eol;
     }
 
-    void addLabel(String blabel)
+    void addByteSep()
+    {
+        m_src += ",";
+    }
+
+    void addIndent()
+    {
+        for (int n = 0; n < m_indent; n++)
+            m_src += " ";
+    }
+
+    void addByteDef(boolean nl)
     {
-        eol();
-        m_src += blabel +":";
-        eol();
+        if (!nl)
+        {
+            m_src += m_eol;
+            addIndent();
+        }
+
+        m_src += m_byteDef +" ";
         m_bytes = 0;
+        m_state = 2;
+    }
+
+    void addLabel(String blabel, boolean nl)
+    {
+        changeState(3);
+        m_src += m_prefix + blabel +":"+ (nl ? " " : "");
     }
 
     void addComment(String bstr)
     {
-        indent();
-        m_src += "; "+ bstr;
-        eol();
-    }
-
-    void eol()
-    {
-        m_src += m_eol;
-    }
-
-    void eod()
-    {
-        eol();
-        eol();
+        changeState(4);
+        m_src += "; "+ bstr + m_eol;
     }
 
-    void addByteDef()
-    {
-        m_src += m_byteDef +" ";
-    }
-
-    void byteSep()
+    void addLine(String bstr)
     {
-        m_src += ",";
-    }
-
-    void indent()
-    {
-        m_src += getIndent();
-    }
-
-    void addLine(String str)
-    {
-        m_src += str;
-        eol();
+        changeState(4);
+        m_src += bstr + m_eol;
     }
 
     void writeByte(int bval, String blabel)
@@ -109,22 +138,23 @@
 
         if (blabel != null)
         {
-            m_src += blabel +": "+ m_byteDef +" "+ str(int(bval)) + m_eol;
+            addLabel(blabel, true);
+            addByteDef(true);
+            m_src += getByteStr(bval);
+            m_src += m_eol;
         }
         else
         {
-            m_src += str(int(bval));
+            if (changeState(2))
+                addByteDef(true);
+            else
+            if (m_bpl > 0 && m_bytes > m_bpl)
+                addByteDef(false);
 
-            if (m_bpl > 0 &&
-                ++m_bytes >= m_bpl)
-            {
-                eol();
-                indent();
-                addByteDef();
-                m_bytes = 0;
-            }
-            else
-                m_src += ",";
+            if (m_bytes++ > 0)
+                addByteSep();
+
+            m_src += getByteStr(bval);
         }
     }
 
@@ -133,11 +163,6 @@
         m_offs = boffs;
         writeByte(bval, blabel);
     }
-
-    void loadTemplate(String fname)
-    {
-        m_data = mpLoadBinaryFile("templates/" + fname);
-    }
 }
 
 
@@ -364,7 +389,7 @@
 }
 
 
-bool mpExportFormat(MPWriteCtx ctx, int subformat)
+boolean mpExportFormat(MPWriteCtx ctx, int subformat)
 {
     int val1, val2, val3;
     int y, y2, yy, x, yp, xp, ad, valu, valu2, bri;
@@ -515,7 +540,7 @@
 }
 
 
-bool mpExportMachinePRG(MPWriteCtx ctx)
+boolean mpExportMachinePRG(MPWriteCtx ctx)
 {
     // any common text headers
     ctx.addComment("machine=" + str(machine) + " (" + g_name + ")");
@@ -527,11 +552,11 @@
         ctx.addLabel("_bitmap");
         mpExportBitmapData(ctx, 40, 25);
 
-        ctx.addComment("The following two first values are border and background");
-        ctx.writeByteAt(0x2167, g_map[0]); //=border
-        ctx.writeByteAt(0x2168, g_map[1]); //=background mutta ei tarvita
+        ctx.writeByteAt(0x2167, g_map[0], "_border"); //=border
+        ctx.writeByteAt(0x2168, g_map[1], "_backg"); //=background mutta ei tarvita
 
         ctx.setOffs(0x2169);
+        ctx.addLabel("_screenram");
         mpExportColorData(ctx, 40, 25, 65536, 0);
 
         //c64show.prg
@@ -542,22 +567,23 @@
     }
     else
     if (machine == C64M) { //C64 MULTICOLOR
+        ctx.loadTemplate("c64mshow.prg");
 
-        ctx.loadTemplate("c64mshow.prg");
         ctx.setOffs(0x0239);
+        ctx.addLabel("_bitmap");
         mpExportBitmapData(ctx, 40, 25);
 
         // first color information
-        ctx.addComment("The following two first values are border and background");
-        ctx.writeByteAt(0x2179, g_map[0]);
-        ctx.writeByteAt(0x217a, g_map[1]);
+        ctx.writeByteAt(0x2179, g_map[0], "_border");
+        ctx.writeByteAt(0x217a, g_map[1], "_backg");
 
         ctx.setOffs(0x217B);
+        ctx.addLabel("_screenram");
         mpExportColorData(ctx, 40, 25, 65536, 1);
 
         // second color information
         ctx.setOffs(0x2563);
-        ctx.addComment("The following goes to $D800 onwards");
+        ctx.addLabel("_colorram");
         mpExportColorData(ctx, 40, 25, 65536 + 2000, 2);
 
         //c64 multicolor
@@ -653,6 +679,7 @@
     else
     if (machine == MSX) { // MSX
         ctx.loadTemplate("msxshow.com");
+
         ctx.addLine(".globl _bitmap,_nimi2");
         ctx.addLine(".area _CODE");
 
@@ -677,8 +704,8 @@
 
         //would need some cleaning up
 
-        //  ctx.addLine(".area  _DATA\n";
-        //  ctx.addLine(".globl _taustakuva\n\n";
+        //  ctx.addLine(".area  _DATA");
+        //  ctx.addLine(".globl _taustakuva");
         //  ctx.addLabel("_taustakuva");
         ctx.loadTemplate("specshow.tap");
 
@@ -689,11 +716,11 @@
 
         //TAP requires fiddling with the checksum
         //println("Checksum:"+hex(checksum,2));
-        ctx.setOffs(0x0223);
-        ctx.addByteDef();
-        ctx.writeByte(g_map[0]);
+        ctx.addLabel("paske");
+        ctx.writeByteAt(0x0223, g_map[0]);
         checksum = checksum ^ int(g_map[0]);
 
+        ctx.addLabel("jottain");
         for (int y = 0; y <= 2; y++)
         for (int y2 = 0; y2 < 8; y2++)
         for (int yy = 0; yy < 8; yy++)
@@ -717,7 +744,7 @@
             }
         }
 
-        ctx.addComment("attributes");
+        ctx.addLabel("attributes");
         for (int y = 0; y < 24; y++)
         {
             for (int x = 0; x < 32; x++)
@@ -769,14 +796,15 @@
 
             ctx.writeByte(ctx, val1 * 9 + val2 * 3 + val3);
         }
+        ctx.setBPLOff();
 
         valu = int(g_map[0]);
         val1 = int(g_g[valu] / (256 / g_palsteps));
         val2 = int(g_r[valu] / (256 / g_palsteps));
         val3 = int(g_b[valu] / (256 / g_palsteps));
 
+        ctx.setOffs(16469);
         ctx.addLabel("_border");
-        ctx.setOffs(16469);
         ctx.writeByte(val1 * 9 + val2 * 3 + val3);
 
         //cpc
@@ -798,10 +826,11 @@
     int pix1b0, pix1b1, pix1b2, pix1b3;
 
     ctx.setBPL(xwid / 2);
-    ctx.addByteDef();
 
     for (y2 = 0; y2 < 8; y2++)
     {
+        ctx.addByteDef();
+
         for (y = 0; y < 25; y++)
         for (x = 0; x < int(xwid / 2); x++)
         {
@@ -836,9 +865,9 @@
         ctx.addByteDef();
         for (int j = 0; j <= 47; j++)
             ctx.writeByte(0);
-        ctx.eol();
     }
-    ctx.eod();
+    ctx.setBPLOff();
+    ctx.addEOL();
 }
 
 
@@ -848,8 +877,9 @@
         xwid  = xx * 8,
         ywid = yy * 8;
 
-    ctx.setBPL(64);
+    ctx.setBPL(32);
     ctx.addByteDef();
+
     for (int y = 0; y < yy; y++)
     for (int x = 0; x < xx; x++)
     {
@@ -866,10 +896,11 @@
                 g_map[ad + 6] * 2 +
                 g_map[ad + 7] * 1;
 
-            ctx.writeByte(int(value));
+            ctx.writeByte(value);
         }
     }
-    ctx.eod();
+    ctx.setBPLOff();
+    ctx.addEOL();
 }
 
 
@@ -996,8 +1027,8 @@
 
             ctx.writeByte(valu1 * 16 + valu2);
         }
-        ctx.eol();
     }
+    ctx.addEOL();
 }
 
 
@@ -1221,12 +1252,13 @@
                 }
             }
 
-            if (idefix == 0) {
-                for (yy = cy * vertti; yy <= cy * vertti + vertti - 1; yy++) {
-                    for (xx = cx * 8; xx <= cx * 8 + 7; xx = xx + molox) {
-                        g_farge = idx[0];
-                        makepoint(xx, yy);
-                    }
+            if (idefix == 0)
+            {
+                for (yy = cy * vertti; yy <= cy * vertti + vertti - 1; yy++)
+                for (xx = cx * 8; xx <= cx * 8 + 7; xx = xx + molox)
+                {
+                    g_farge = idx[0];
+                    makepoint(xx, yy);
                 }
             }
         }