changeset 1097:0e3cb9e61170

Code cleanup, in preparation for bug hunting.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 03 Mar 2015 02:36:45 +0200
parents 96e0798f602b
children 441a1ca43b02
files tools/xm2jss.c
diffstat 1 files changed, 101 insertions(+), 81 deletions(-) [+]
line wrap: on
line diff
--- a/tools/xm2jss.c	Tue Mar 03 02:35:36 2015 +0200
+++ b/tools/xm2jss.c	Tue Mar 03 02:36:45 2015 +0200
@@ -648,7 +648,7 @@
          mapInstruments[jsetMaxInstruments + 1],
          mapPatterns[jsetMaxPatterns + 1];
     JSSModule *r = NULL;
-    int i, n8, n16;
+    int i, n8, n16, unused;
 
     // Allocate a new module
     if ((r = jssAllocateModule()) == NULL)
@@ -703,14 +703,14 @@
                 // Check all notes
                 for (row = 0; row < p->nrows; row++)
                 for (channel = 0; channel < p->nchannels; channel++, n++)
+                if (n->instrument != jsetNotSet)
                 {
-                    if (n->instrument != jsetNotSet)
+                    if (n->instrument >= 0 && n->instrument < m->nextInstruments)
+                        usedExtInstruments[n->instrument] = TRUE;
+                    else
                     {
-                        if (optStripExtInstr || (n->instrument >= 0 && n->instrument < m->nextInstruments))
-                            usedExtInstruments[n->instrument] = TRUE;
-                        else
-                            dmMsg(2, "Pattern 0x%x, row=0x%x, chn=%d has invalid instrument 0x%x\n",
-                            pattern, row, channel, n->instrument);
+                        dmMsg(2, "Pattern 0x%x, row=0x%x, chn=%d has invalid instrument 0x%x\n",
+                        pattern, row, channel, n->instrument);
                     }
                 }
             }
@@ -763,117 +763,137 @@
     // Create pattern mappings
     //
     r->npatterns = 0;
-    dmMsg(1, "Unused patterns: ");
+    dmMsg(1, "Creating pattern remaps...\n");
 
-    for (i = 0; i <= jsetMaxPatterns; i++)
-    if (m->patterns[i] != NULL)
+    for (unused = i = 0; i <= jsetMaxPatterns; i++)
+    if (usedPatterns[i])
     {
-        if (!usedPatterns[i])
+        JSSPattern *pat;
+
+        if (i >= m->npatterns)
         {
-            dmPrint(2, "0x%x, ", i);
+            dmErrorMsg("Pattern 0x%x >= 0x%x, but used!\n", i, m->npatterns);
+            continue;
         }
-        else
+
+        if ((pat = m->patterns[i]) == NULL)
         {
-            if (i >= m->npatterns)
-                dmErrorMsg("Pattern 0x%x >= 0x%x, but used!\n", i, m->npatterns);
-            
-            mapPatterns[i] = r->npatterns;
-            r->patterns[r->npatterns] = m->patterns[i];
-            (r->npatterns)++;
+            dmErrorMsg("Pattern 0x%x used but is NULL.\n", i);
+            continue;
         }
+        
+        mapPatterns[i] = r->npatterns;
+        r->patterns[r->npatterns] = m->patterns[i];
+        (r->npatterns)++;
     }
-    dmPrint(2, "\n");
+    else
+    if (m->patterns[i] != NULL)
+        unused++;
 
-    dmMsg(1, "%d used patterns, %d unused.\n",
-        r->npatterns, m->npatterns - r->npatterns);
+    dmMsg(1, "%d used patterns (%d unused).\n",
+        r->npatterns, unused);
     
     
     //
     // Re-map instruments
     //
-    dmMsg(1, "Unused instruments: ");
-    for (n8 = n16 = i = 0; i < jsetMaxInstruments; i++)
-    if (m->instruments[i] != NULL)
+    dmMsg(1, "Creating sample instrument remaps...\n");
+    for (unused = n8 = n16 = i = 0; i < jsetMaxInstruments; i++)
+    if (usedInstruments[i])
     {
-        if (!usedInstruments[i] && !optStripInstr)
+        JSSInstrument *ip;
+        if (optStripInstr)
+            continue;
+
+        if (i >= m->ninstruments)
         {
-            dmPrint(2, "0x%x, ", i);
+            dmErrorMsg("Instrument 0x%x >= 0x%x, but used!\n",
+                i, m->ninstruments);
+            continue;
         }
-        else
+
+        if ((ip = m->instruments[i]) == NULL)
         {
-            JSSInstrument *ip = m->instruments[i];
-            if (i >= m->ninstruments)
-                dmErrorMsg("Instrument 0x%x >= 0x%x, but used!\n", i, m->ninstruments);
-            
-            mapInstruments[i] = r->ninstruments;
-            r->instruments[r->ninstruments] = ip;
-            (r->ninstruments)++;
+            dmErrorMsg("Instrument 0x%x used but is NULL.\n", i);
+            continue;
+        }
+
+        dmPrint(2, "%02x -> %02x : ", i, r->ninstruments);
+        mapInstruments[i] = r->ninstruments;
+        r->instruments[r->ninstruments] = ip;
+        (r->ninstruments)++;
 
-            if (ip->flags & jsf16bit)
-                n16++;
-            else
-                n8++;
-        }
+        if (ip->flags & jsf16bit)
+            n16++;
+        else
+            n8++;
     }
+    else
+    if (m->instruments[i] != NULL)
+        unused++;
+
     dmPrint(2, "\n");
-    dmMsg(1, "Total of (%d)  16-bit, (%d) 8-bit samples, (%d) instruments.\n",
-        n16, n8, r->ninstruments);
+    dmMsg(1, "Total of %d [16-bit] + %d [8-bit] samples = %d instruments (%d unused).\n",
+        n16, n8, r->ninstruments, unused);
     
+    //
     // Re-map ext.instruments
-    dmMsg(1, "Unused ext.instruments: ");
-    for (i = 0; i < jsetMaxInstruments; i++)
+    //
+    dmMsg(1, "Creating ext.instrument remaps...\n");
+    for (unused = i = 0; i < jsetMaxInstruments; i++)
     if (usedExtInstruments[i])
     {
-        if (i >= m->nextInstruments && !optStripExtInstr)
+        JSSExtInstrument *eip;
+        int note;
+        if (optStripExtInstr)
+            continue;
+
+        if (i >= m->nextInstruments)
         {
             dmErrorMsg("Ext.instrument 0x%x >= 0x%x, but used!\n",
                 i, m->nextInstruments);
+            continue;
         }
-        else
-        if (m->extInstruments[i] != NULL)
+
+        if ((eip = m->extInstruments[i]) == NULL)
         {
-            JSSExtInstrument *e = m->extInstruments[i];
-            int note;
-            
-            mapExtInstruments[i] = r->nextInstruments;
-            r->extInstruments[r->nextInstruments] = e;
-            (r->nextInstruments)++;
-            
-            // Re-map sNumForNotes
-            for (note = 0; note < jsetNNotes; note++)
+            dmErrorMsg("Extended instrument 0x%x used but is NULL.\n", i);
+            continue;
+        }
+
+        dmPrint(2, "%02x -> %02x : ", i, r->nextInstruments);
+        mapExtInstruments[i] = r->nextInstruments;
+        r->extInstruments[r->nextInstruments] = eip;
+        (r->nextInstruments)++;
+
+        // Re-map sNumForNotes table for this ext.instrument
+        for (note = 0; note < jsetNNotes; note++)
+        {
+            int q = eip->sNumForNotes[note];
+            if (q != jsetNotSet)
             {
-                int q = e->sNumForNotes[note];
-                if (q != jsetNotSet)
+                int map;
+                if (q >= 0 && q < jsetMaxInstruments)
                 {
-                    int map;
-                    if (q >= 0 && q < jsetMaxInstruments)
-                    {
-                        map = mapInstruments[q];
-                    }
-                    else
-                    {
-                        map = jsetNotSet;
-                        dmErrorMsg("e=%d, note=%d, q=%d/%d\n", i, note, q, r->ninstruments);
-                    }
-                    e->sNumForNotes[note] = map;
+                    map = mapInstruments[q];
                 }
+                else
+                {
+                    map = jsetNotSet;
+                    dmErrorMsg("e=%d, note=%d, q=%d/%d\n", i, note, q, r->ninstruments);
+                }
+                dmPrint(3, "%02x.%02x ", q, map);
+                eip->sNumForNotes[note] = map;
             }
         }
-        else
-        {
-            dmPrint(2, "[0x%x==NULL], ", i);
-            mapExtInstruments[i] = jsetNotSet;
-        }
     }
     else
-    {
-        if (i < m->nextInstruments && m->extInstruments[i] != NULL)
-        {
-            dmPrint(2, "0x%x, ", i);
-        }
-    }
+    if (m->extInstruments[i] != NULL)
+        unused++;
+
     dmPrint(2, "\n");
-    dmMsg(1, "%d extended instruments.\n", r->nextInstruments);
+    dmMsg(1, "%d extended instruments (%d unused).\n",
+        r->nextInstruments, unused);
 
     //
     // Remap pattern data with remapped instrument data