home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.72_stereo.zip / src / greloc.c < prev    next >
C/C++ Source or Header  |  2011-07-03  |  55KB  |  2,124 lines

  1. //
  2. // GOATTRACKER v2 packer/relocator
  3. //
  4.  
  5. #define GRELOC_C
  6.  
  7. #include "goattrk2.h"
  8. #include "membuf.h"
  9. #include "parse.h"
  10.  
  11. char *playeroptname[] =
  12. {
  13.   "Buffered SID-writes",
  14.   "Sound effect support",
  15.   "Volume change support",
  16.   "Store author-info",
  17.   "Use zeropage ghostregs",
  18.   "Disable optimization"
  19. };
  20.  
  21. char *tableleftname[] = {
  22.   "mt_wavetbl",
  23.   "mt_pulsetimetbl",
  24.   "mt_filttimetbl",
  25.   "mt_speedlefttbl"};
  26.  
  27. char *tablerightname[] = {
  28.   "mt_notetbl",
  29.   "mt_pulsespdtbl",
  30.   "mt_filtspdtbl",
  31.   "mt_speedrighttbl"};
  32.  
  33. unsigned char chnused[MAX_CHN];
  34. unsigned char pattused[MAX_PATT];
  35. unsigned char pattmap[MAX_PATT];
  36. unsigned char instrused[MAX_INSTR];
  37. unsigned char instrmap[MAX_INSTR];
  38. unsigned char tableused[MAX_TABLES][MAX_TABLELEN+1];
  39. unsigned char tablemap[MAX_TABLES][MAX_TABLELEN+1];
  40. int pattoffset[MAX_PATT];
  41. int pattsize[MAX_PATT];
  42. int songoffset[MAX_SONGS][MAX_CHN];
  43. int songsize[MAX_SONGS][MAX_CHN];
  44. int tableerror;
  45. int channels;
  46. int fixedparams;
  47. int simplepulse;
  48. int firstnote;
  49. int lastnote;
  50. int patternlastnote;
  51. int nofilter;
  52. int nofiltermod;
  53. int nopulse;
  54. int nopulsemod;
  55. int nowavedelay;
  56. int norepeat;
  57. int notrans;
  58. int noportamento;
  59. int notoneporta;
  60. int novib;
  61. int noinsvib;
  62. int nosetad;
  63. int nosetsr;
  64. int nosetwave;
  65. int nosetwaveptr;
  66. int nosetpulseptr;
  67. int nosetfiltptr;
  68. int nosetfiltcutoff;
  69. int nosetfiltctrl;
  70. int nosetmastervol;
  71. int nofunktempo;
  72. int noglobaltempo;
  73. int nochanneltempo;
  74. int nogate;
  75. int noeffects;
  76. int nowavecmd;
  77. int nofirstwavecmd;
  78. int nocalculatedspeed;
  79. int nonormalspeed;
  80. int nozerospeed;
  81.  
  82. struct membuf src = STATIC_MEMBUF_INIT;
  83. struct membuf dest = STATIC_MEMBUF_INIT;
  84.  
  85. void relocator(void)
  86. {
  87.   char packedsongname[MAX_FILENAME];
  88.   char packedfilter[MAX_FILENAME];
  89.   unsigned char *packeddata = NULL;
  90.   char *playername = "player.s";
  91.  
  92.   int tableerrortype = TYPE_NONE;
  93.   int tableerrorcause = CAUSE_NONE;
  94.   int tableerrorsource1 = 0;
  95.   int tableerrorsource2 = 0;
  96.   int patterns = 0;
  97.   int songs = 0;
  98.   int instruments = 0;
  99.   int numlegato = 0;
  100.   int numnohr = 0;
  101.   int numnormal = 0;
  102.   int freenormal;
  103.   int freenohr;
  104.   int freelegato;
  105.   int transuprange = 0;
  106.   int transdownrange = 0;
  107.   int pattdatasize = 0;
  108.   int patttblsize = 0;
  109.   int songdatasize = 0;
  110.   int songtblsize = 0;
  111.   int instrsize = 0;
  112.   int wavetblsize = 0;
  113.   int pulsetblsize = 0;
  114.   int filttblsize = 0;
  115.   int speedtblsize = 0;
  116.   int playersize = 0;
  117.   int packedsize = 0;
  118.  
  119.   channels = 6;
  120.   fixedparams = 1;
  121.   simplepulse = 1;
  122.   firstnote = MAX_NOTES-1;
  123.   lastnote = 0;
  124.   patternlastnote = 0;
  125.   noeffects = 1;
  126.   nogate = 1;
  127.   nofilter = 1;
  128.   nofiltermod = 1;
  129.   nopulse = 1;
  130.   nopulsemod = 1;
  131.   nowavedelay = 1;
  132.   nowavecmd = 1;
  133.   norepeat = 1;
  134.   notrans = 1;
  135.   noportamento = 1;
  136.   notoneporta = 1;
  137.   novib = 1;
  138.   noinsvib = 1;
  139.   nosetad = 1;
  140.   nosetsr = 1;
  141.   nosetwave = 1;
  142.   nosetwaveptr = 1;
  143.   nosetpulseptr = 1;
  144.   nosetfiltptr = 1;
  145.   nosetfiltcutoff = 1;
  146.   nosetfiltctrl = 1;
  147.   nosetmastervol = 1;
  148.   nofunktempo = 1;
  149.   noglobaltempo = 1;
  150.   nochanneltempo = 1;
  151.   nofirstwavecmd = 1;
  152.   nocalculatedspeed = 1;
  153.   nonormalspeed = 1;
  154.   nozerospeed = 1;
  155.  
  156.   FILE *songhandle = NULL;
  157.   int selectdone;
  158.   int opt = 0;
  159.   unsigned char speedcode[] = {0xa2,0x00,0x8e,0x04,0xdc,0xa2,0x00,0x8e,0x05,0xdc};
  160.  
  161.   int c,d,e;
  162.  
  163.   unsigned char patttemp[512];
  164.   unsigned char *songwork = NULL;
  165.   unsigned char *pattwork = NULL;
  166.   unsigned char *instrwork = NULL;
  167.  
  168.   stopsong();
  169.  
  170.   memset(pattused, 0, sizeof pattused);
  171.   memset(instrused, 0, sizeof instrused);
  172.   memset(chnused, 0, sizeof chnused);
  173.   memset(tableused, 0, sizeof tableused);
  174.   memset(tablemap, 0, sizeof tablemap);
  175.   tableerror = 0;
  176.  
  177.   membuf_free(&src);
  178.   membuf_free(&dest);
  179.  
  180.   // Process song-orderlists
  181.   countpatternlengths();
  182.   // Calculate amount of songs with nonzero length
  183.   for (c = 0; c < MAX_SONGS; c++)
  184.   {
  185.     if ((songlen[c][0]) &&
  186.         (songlen[c][1]) &&
  187.         (songlen[c][2]))
  188.     {
  189.       // See which patterns are used in this song
  190.       for (d = 0; d < MAX_CHN; d++)
  191.       {
  192.         songdatasize += songlen[c][d]+2;
  193.         for (e = 0; e < songlen[c][d]; e++)
  194.         {
  195.           if (songorder[c][d][e] < REPEAT)
  196.           {
  197.             int f;
  198.             int num = songorder[c][d][e];
  199.  
  200.             pattused[num] = 1;
  201.             for (f = 0; f < pattlen[num]; f++)
  202.             {
  203.               if ((pattern[num][f*4] != REST) || (pattern[num][f*4+1]) || (pattern[num][f*4+2]))
  204.                 chnused[d] = 1;
  205.             }
  206.           }
  207.           else
  208.           {
  209.             if (songorder[c][d][e] >= TRANSDOWN)
  210.             {
  211.               notrans = 0;
  212.               if (songorder[c][d][e] < TRANSUP)
  213.               {
  214.                 int newtransdownrange = -(songorder[c][d][e] - TRANSUP);
  215.                 if (newtransdownrange > transdownrange) transdownrange = newtransdownrange;
  216.               }
  217.               else
  218.               {
  219.                 int newtransuprange = songorder[c][d][e] - TRANSUP;
  220.                 if (newtransuprange > transuprange) transuprange = newtransuprange;
  221.               }
  222.             }
  223.             else norepeat = 0;
  224.           }
  225.         }
  226.         if (songorder[c][d][songlen[c][d]+1] >= songlen[c][d])
  227.         {
  228.           sprintf(textbuffer, "ILLEGAL SONG RESTART POSITION! (SUBTUNE %02X, CHANNEL %d)", c, d+1);
  229.           clearscreen();
  230.           printtextc(MAX_ROWS/2, 15, textbuffer);
  231.           fliptoscreen();
  232.           waitkeynoupdate();
  233.           goto PRCLEANUP;
  234.         }
  235.       }
  236.       songs++;
  237.     }
  238.   }
  239.  
  240.   if (!songs)
  241.   {
  242.     clearscreen();
  243.     printtextc(MAX_ROWS/2, CTITLE, "NO SONGS, NO DATA TO SAVE!");
  244.     fliptoscreen();
  245.     waitkeynoupdate();
  246.     goto PRCLEANUP;
  247.   }
  248.  
  249.   // Build the pattern-mapping
  250.   // Instrument 1 is always used
  251.   instrused[1] = 1;
  252.   for (c = 0; c < MAX_PATT; c++)
  253.   {
  254.     if (pattused[c])
  255.     {
  256.       pattmap[c] = patterns;
  257.       patterns++;
  258.  
  259.       // See which instruments/tablecommands are used
  260.       for (d = 0; d < pattlen[c]; d++)
  261.       {
  262.         tableerror = 0;
  263.  
  264.         if ((pattern[c][d*4] == KEYOFF) || (pattern[c][d*4] == KEYON))
  265.           nogate = 0;
  266.         if (pattern[c][d*4+1])
  267.           instrused[pattern[c][d*4+1]] = 1;
  268.         if (pattern[c][d*4+2])
  269.           noeffects = 0;
  270.         if ((pattern[c][d*4+2] >= CMD_SETWAVEPTR) && (pattern[c][d*4+2] <= CMD_SETFILTERPTR))
  271.           exectable(pattern[c][d*4+2] - CMD_SETWAVEPTR, pattern[c][d*4+3]);
  272.         if ((pattern[c][d*4+2] >= CMD_PORTAUP) && (pattern[c][d*4+2] <= CMD_VIBRATO))
  273.         {
  274.           exectable(STBL, pattern[c][d*4+3]);
  275.           calcspeedtest(pattern[c][d*4+3]);
  276.         }
  277.         if (pattern[c][d*4+2] == CMD_FUNKTEMPO)
  278.           exectable(STBL, pattern[c][d*4+3]);
  279.         if (pattern[c][d*4+2] == CMD_FUNKTEMPO)
  280.         {
  281.           nofunktempo = 0;
  282.           noglobaltempo = 0;
  283.         }
  284.         if ((pattern[c][d*4+2] == CMD_SETTEMPO) && ((pattern[c][d*4+3] & 0x7f) < 3)) nofunktempo = 0;
  285.  
  286.         // See, which are the highest/lowest notes used
  287.         if ((pattern[c][d*4] >= FIRSTNOTE) && (pattern[c][d*4] <= LASTNOTE))
  288.         {
  289.           int newfirstnote = pattern[c][d*4] - FIRSTNOTE - transdownrange;
  290.           int newlastnote = pattern[c][d*4] - FIRSTNOTE + transuprange;
  291.           if (newfirstnote < 0) newfirstnote = 0;
  292.           if (newlastnote > MAX_NOTES-1) newlastnote = MAX_NOTES-1;
  293.  
  294.           if (newfirstnote < firstnote) firstnote = newfirstnote;
  295.           if (newlastnote > lastnote)
  296.           {
  297.             patternlastnote = newlastnote;
  298.             lastnote = newlastnote;
  299.           }
  300.           if (newfirstnote > lastnote)
  301.           {
  302.             patternlastnote = newfirstnote;
  303.             lastnote = newfirstnote;
  304.           }
  305.         }
  306.         if ((tableerror) && (!tableerrortype))
  307.         {
  308.           tableerrortype = tableerror;
  309.           tableerrorcause = CAUSE_PATTERN;
  310.           tableerrorsource1 = c;
  311.           tableerrorsource2 = d;
  312.         }
  313.       }
  314.     }
  315.   }
  316.  
  317.   // Count amount of normal, nohr, and legato instruments
  318.   // Also see if special first wave parameters are used
  319.   for (c = 0; c < MAX_INSTR; c++)
  320.   {
  321.     if (instrused[c])
  322.     {
  323.       if (instr[c].gatetimer & 0x40) numlegato++;
  324.       else
  325.       {
  326.         if (instr[c].gatetimer & 0x80) numnohr++;
  327.         else numnormal++;
  328.       }
  329.       if ((!instr[c].firstwave) || (instr[c].firstwave >= 0xfe))
  330.         nofirstwavecmd = 0;
  331.     }
  332.   }
  333.   freenormal = 1;
  334.   freenohr = freenormal + numnormal;
  335.   freelegato = freenohr + numnohr;
  336.  
  337.   // Build the instrument-mapping
  338.   for (c = 0; c < MAX_INSTR; c++)
  339.   {
  340.     if (instrused[c])
  341.     {
  342.       if (instr[c].gatetimer & 0x40) instrmap[c] = freelegato++;
  343.       else
  344.       {
  345.         if (instr[c].gatetimer & 0x80) instrmap[c] = freenohr++;
  346.         else instrmap[c] = freenormal++;
  347.       }
  348.       instruments++;
  349.       for (d = 0; d < MAX_TABLES; d++)
  350.       {
  351.         tableerror = 0;
  352.         exectable(d, instr[c].ptr[d]);
  353.         if (d == STBL) calcspeedtest(instr[c].ptr[d]);
  354.         if ((tableerror) && (!tableerrortype))
  355.         {
  356.           tableerrortype = tableerror;
  357.           tableerrorcause = CAUSE_INSTRUMENT;
  358.           tableerrorsource1 = c;
  359.           tableerrorsource2 = d;
  360.         }
  361.       }
  362.     }
  363.   }
  364.  
  365.   // Execute tableprograms invoked from wavetable commands
  366.   for (c = 0; c < MAX_TABLELEN; c++)
  367.   {
  368.     if (tableused[WTBL][c+1])
  369.     {
  370.       if ((ltable[WTBL][c] >= WAVECMD) && (ltable[WTBL][c] <= WAVELASTCMD))
  371.       {
  372.         d = -1;
  373.         tableerror = 0;
  374.  
  375.         switch(ltable[WTBL][c] - WAVECMD)
  376.         {
  377.           case CMD_PORTAUP:
  378.           case CMD_PORTADOWN:
  379.           case CMD_TONEPORTA:
  380.           case CMD_VIBRATO:
  381.           d = STBL;
  382.           calcspeedtest(rtable[WTBL][c]);
  383.           break;
  384.  
  385.           case CMD_SETPULSEPTR:
  386.           d = PTBL;
  387.           nopulse = 0;
  388.            break;
  389.            
  390.            case CMD_SETFILTERPTR:
  391.            d = FTBL;
  392.           nofilter = 0;
  393.           break;
  394.  
  395.           case CMD_DONOTHING:
  396.           case CMD_SETWAVEPTR:
  397.           case CMD_FUNKTEMPO:
  398.           sprintf(textbuffer, "ILLEGAL WAVETABLE COMMAND (ROW %02X, COMMAND %X)", c+1, ltable[WTBL][c] - WAVECMD);
  399.           clearscreen();
  400.           printtextc(MAX_ROWS/2, 15, textbuffer);
  401.           fliptoscreen();
  402.           waitkeynoupdate();
  403.           goto PRCLEANUP;
  404.         }
  405.  
  406.         if (d != -1) exectable(d, rtable[WTBL][c]);
  407.  
  408.         if ((tableerror) && (!tableerrortype))
  409.         {
  410.           tableerrortype = tableerror;
  411.           tableerrorcause = CAUSE_WAVECMD;
  412.           tableerrorsource1 = c+1;
  413.           tableerrorsource2 = d;
  414.         }
  415.       }
  416.     }
  417.   }
  418.  
  419.   // Build the table-mapping
  420.   for (c = 0; c < MAX_TABLES; c++)
  421.   {
  422.     int e = 1;
  423.     for (d = 0; d < MAX_TABLELEN; d++)
  424.     {
  425.       if (tableused[c][d+1])
  426.       {
  427.         tablemap[c][d+1] = e;
  428.         e++;
  429.       }
  430.     }
  431.   }
  432.  
  433.   // Check for table errors
  434.   if (tableerrorcause)
  435.   {
  436.     clearscreen();
  437.     switch(tableerrortype)
  438.     {
  439.       case TYPE_JUMP:
  440.       sprintf(textbuffer, "TABLE POINTER POINTS TO A JUMP! ");
  441.       break;
  442.  
  443.       case TYPE_OVERFLOW:
  444.       sprintf(textbuffer, "TABLE EXECUTION OVERFLOWS! ");
  445.       break;
  446.     }
  447.     switch (tableerrorcause)
  448.     {
  449.       case CAUSE_PATTERN:
  450.       sprintf(textbuffer + strlen(textbuffer), "(PATTERN %02X, ROW %02d)", tableerrorsource1, tableerrorsource2);
  451.       break;
  452.  
  453.       case CAUSE_WAVECMD:
  454.       sprintf(textbuffer + strlen(textbuffer), "WAVETABLE CMD (ROW %02X, ", tableerrorsource1);
  455.       goto TABLETYPE;
  456.  
  457.       case CAUSE_INSTRUMENT:
  458.       sprintf(textbuffer + strlen(textbuffer), "(INSTRUMENT %02X, ", tableerrorsource1);
  459.       TABLETYPE:
  460.       switch (tableerrorsource2)
  461.       {
  462.         case WTBL:
  463.         strcat(textbuffer, "WAVE");
  464.         break;
  465.  
  466.         case PTBL:
  467.         strcat(textbuffer, "PULSE");
  468.         break;
  469.  
  470.         case FTBL:
  471.         strcat(textbuffer, "FILTER");
  472.         break;
  473.       }
  474.       strcat(textbuffer, ")");
  475.       break;
  476.     }
  477.     printtextc(MAX_ROWS/2, 15, textbuffer);
  478.  
  479.     fliptoscreen();
  480.     waitkeynoupdate();
  481.     goto PRCLEANUP;
  482.   }
  483.  
  484.   // Find duplicate ranges in tables
  485.   for (c = 0; c < MAX_TABLES; c++)
  486.     findtableduplicates(c);
  487.  
  488.   // Select playroutine options
  489.   clearscreen();
  490.   printblankc(0, 0, 15+16, MAX_COLUMNS);
  491.   if (!strlen(loadedsongfilename))
  492.     sprintf(textbuffer, "%s Packer/Relocator", programname);
  493.   else
  494.     sprintf(textbuffer, "%s Packer/Relocator - %s", programname, loadedsongfilename);
  495.   textbuffer[MAX_COLUMNS] = 0;
  496.   printtext(0, 0, 15+16, textbuffer);
  497.   printtext(1, 2, CTITLE, "SELECT PLAYROUTINE OPTIONS: (CURSORS=MOVE/CHANGE, ENTER=ACCEPT, ESC=CANCEL)");
  498.  
  499.   playerversion |= PLAYER_BUFFERED;
  500.   playerversion &= ~PLAYER_ZPGHOSTREGS;
  501.  
  502.   selectdone = 0;
  503.   while (!selectdone)
  504.   {
  505.     for (c = 0; c < MAX_OPTIONS; c++)
  506.     {
  507.       int color = CNORMAL;
  508.       if (opt == c) color = CEDIT;
  509.  
  510.       printtext(1, 3+c, color, playeroptname[c]);
  511.       if (playerversion & (PLAYER_BUFFERED << c))
  512.         printtext(24, 3+c, color, "Yes");
  513.       else
  514.         printtext(24, 3+c, color, "No ");
  515.     }
  516.     fliptoscreen();
  517.     waitkeynoupdate();
  518.  
  519.     if (win_quitted)
  520.     {
  521.       exitprogram = 1;
  522.       goto PRCLEANUP;
  523.     }
  524.  
  525.     switch(rawkey)
  526.     {
  527.       case KEY_LEFT:
  528.       case KEY_RIGHT:
  529.       case KEY_SPACE:
  530.       playerversion ^= (PLAYER_BUFFERED << opt);
  531.       if (opt)
  532.       {
  533.         if ((playerversion & PLAYER_SOUNDEFFECTS) || (playerversion & PLAYER_ZPGHOSTREGS))
  534.           playerversion |= PLAYER_BUFFERED;
  535.       }
  536.       else
  537.       {
  538.         if (!(playerversion & PLAYER_BUFFERED))
  539.         {
  540.           playerversion &= ~PLAYER_SOUNDEFFECTS;
  541.           playerversion &= ~PLAYER_ZPGHOSTREGS;
  542.         }
  543.       }
  544.       break;
  545.  
  546.       case KEY_UP:
  547.       opt--;
  548.       if (opt < 0) opt = MAX_OPTIONS-1;
  549.       break;
  550.  
  551.       case KEY_DOWN:
  552.       opt++;
  553.       if (opt >= MAX_OPTIONS) opt = 0;
  554.       break;
  555.  
  556.       case KEY_ESC:
  557.       selectdone = -1;
  558.       break;
  559.  
  560.       case KEY_ENTER:
  561.       selectdone = 1;
  562.       break;
  563.     }
  564.     
  565.     playerversion |= PLAYER_BUFFERED;
  566.     playerversion &= ~PLAYER_ZPGHOSTREGS;
  567.   }
  568.   if (selectdone == -1) goto PRCLEANUP;
  569.  
  570.   // Disable optimizations if necessary
  571.   if (playerversion & PLAYER_NOOPTIMIZATION)
  572.   {
  573.     fixedparams = 0;
  574.     if (!numlegato) numlegato++;
  575.  
  576.     simplepulse = 0;
  577.     firstnote = 0;
  578.     lastnote = MAX_NOTES-1;
  579.     nogate = 0;
  580.     noeffects = 0;
  581.     nofilter = 0;
  582.     nofiltermod = 0;
  583.     nopulse = 0;
  584.     nopulsemod = 0;
  585.     nowavedelay = 0;
  586.     nowavecmd = 0;
  587.     norepeat = 0;
  588.     notrans = 0;
  589.     noportamento = 0;
  590.     notoneporta = 0;
  591.     novib = 0;
  592.     noinsvib = 0;
  593.     nosetad = 0;
  594.     nosetsr = 0;
  595.     nosetwave = 0;
  596.     nosetwaveptr = 0;
  597.     nosetpulseptr = 0;
  598.     nosetfiltptr = 0;
  599.     nosetfiltcutoff = 0;
  600.     nosetfiltctrl = 0;
  601.     nosetmastervol = 0;
  602.     nofunktempo = 0;
  603.     noglobaltempo = 0;
  604.     nochanneltempo = 0;
  605.     nofirstwavecmd = 0;
  606.     nocalculatedspeed = 0;
  607.     nonormalspeed = 0;
  608.     nozerospeed = 0;
  609.   }
  610.  
  611.   // Make sure buffering is used if it is needed
  612.   if ((playerversion & PLAYER_SOUNDEFFECTS) || (playerversion & PLAYER_ZPGHOSTREGS))
  613.     playerversion |= PLAYER_BUFFERED;
  614.  
  615.   // Allocate memory for song-orderlists
  616.   songtblsize = songs*6;
  617.   songwork = malloc(songdatasize);
  618.   if (!songwork)
  619.   {
  620.     clearscreen();
  621.     printtextc(MAX_ROWS/2, CTITLE, "OUT OF MEMORY IN PACKER/RELOCATOR!");
  622.     fliptoscreen();
  623.     waitkeynoupdate();
  624.     goto PRCLEANUP;
  625.   }
  626.  
  627.   // Generate songorderlists & songtable
  628.   songdatasize = 0;
  629.   for (c = 0; c < songs; c++)
  630.   {
  631.     if ((songlen[c][0]) &&
  632.         (songlen[c][1]) &&
  633.         (songlen[c][2]))
  634.     {
  635.       for (d = 0; d < MAX_CHN; d++)
  636.       {
  637.         songoffset[c][d] = songdatasize;
  638.         songsize[c][d] = songlen[c][d] + 2;
  639.  
  640.         for (e = 0; e < songlen[c][d]; e++)
  641.         {
  642.           // Pattern
  643.           if (songorder[c][d][e] < REPEAT)
  644.             songwork[songdatasize++] = pattmap[songorder[c][d][e]];
  645.           else
  646.           {
  647.             // Transpose
  648.             if (songorder[c][d][e] >= TRANSDOWN)
  649.             {
  650.               songwork[songdatasize++] = songorder[c][d][e];
  651.             }
  652.             // Repeat sequence: must be swapped
  653.             else
  654.             {
  655.               // See that repeat amount is more than 1
  656.               if (songorder[c][d][e] > REPEAT)
  657.               {
  658.                 // Insanity check that a pattern indeed follows
  659.                 if (songorder[c][d][e+1] < REPEAT)
  660.                 {
  661.                   songwork[songdatasize++] = pattmap[songorder[c][d][e+1]];
  662.                   songwork[songdatasize++] = songorder[c][d][e];
  663.                   e++;
  664.                 }
  665.                 else
  666.                   songwork[songdatasize++] = songorder[c][d][e];
  667.               }
  668.             }
  669.           }
  670.         }
  671.         // Endmark & repeat position
  672.         songwork[songdatasize++] = songorder[c][d][e++];
  673.         songwork[songdatasize++] = songorder[c][d][e++];
  674.       }
  675.     }
  676.     else
  677.     {
  678.       for (d = 0; d < MAX_CHN; d++)
  679.       {
  680.         songoffset[c][d] = songdatasize;
  681.         songsize[c][d] = 0;
  682.       }
  683.     }
  684.   }
  685.  
  686.   // Calculate total size of patterns
  687.   for (c = 0; c < MAX_PATT; c++)
  688.   {
  689.     if (pattused[c])
  690.     {
  691.       int result = packpattern(patttemp, pattern[c], pattlen[c]);
  692.  
  693.       if (result < 0)
  694.       {
  695.         clearscreen();
  696.         sprintf(textbuffer, "PATTERN %02X IS TOO COMPLEX (OVER 256 BYTES PACKED)!", c);
  697.         printtextc(MAX_ROWS/2, 15, textbuffer);
  698.         fliptoscreen();
  699.         waitkeynoupdate();
  700.         goto PRCLEANUP;
  701.       }
  702.       pattdatasize += result;
  703.     }
  704.   }
  705.  
  706.   patttblsize = patterns*2;
  707.   pattwork = malloc(pattdatasize);
  708.   if (!pattwork)
  709.   {
  710.     clearscreen();
  711.     printtextc(MAX_ROWS/2, CTITLE, "OUT OF MEMORY IN PACKER/RELOCATOR!");
  712.     fliptoscreen();
  713.     waitkeynoupdate();
  714.     goto PRCLEANUP;
  715.   }
  716.  
  717.   // This time pack the patterns for real
  718.   pattdatasize = 0;
  719.   d = 0;
  720.   for (c = 0; c < MAX_PATT; c++)
  721.   {
  722.     if (pattused[c])
  723.     {
  724.       pattoffset[d] = pattdatasize;
  725.       pattsize[d] = packpattern(&pattwork[pattdatasize], pattern[c], pattlen[c]);
  726.       pattdatasize += pattsize[d];
  727.       d++;
  728.     }
  729.   }
  730.  
  731.   // Then process instruments
  732.   instrsize = instruments*9;
  733.   instrwork = malloc(instrsize);
  734.   if (!instrwork)
  735.   {
  736.     clearscreen();
  737.     printtextc(MAX_ROWS/2, CTITLE, "OUT OF MEMORY IN PACKER/RELOCATOR!");
  738.     fliptoscreen();
  739.     waitkeynoupdate();
  740.     goto PRCLEANUP;
  741.   }
  742.  
  743.   for (c = 1; c < MAX_INSTR; c++)
  744.   {
  745.     if (instrused[c])
  746.     {
  747.       d = instrmap[c] - 1;
  748.       instrwork[d] = instr[c].ad;
  749.       instrwork[d+instruments] = instr[c].sr;
  750.       instrwork[d+instruments*2] = tablemap[WTBL][instr[c].ptr[WTBL]];
  751.       instrwork[d+instruments*3] = tablemap[PTBL][instr[c].ptr[PTBL]];
  752.       instrwork[d+instruments*4] = tablemap[FTBL][instr[c].ptr[FTBL]];
  753.       if (instr[c].vibdelay)
  754.       {
  755.         instrwork[d+instruments*5] = tablemap[STBL][instr[c].ptr[STBL]];
  756.         instrwork[d+instruments*6] = instr[c].vibdelay - 1;
  757.       }
  758.       else
  759.       {
  760.         instrwork[d+instruments*5] = 0;
  761.         instrwork[d+instruments*6] = 0;
  762.       }
  763.       instrwork[d+instruments*7] = instr[c].gatetimer & 0x3f;
  764.       instrwork[d+instruments*8] = instr[c].firstwave;
  765.  
  766.       if (instr[c].ptr[STBL])
  767.       {
  768.         novib = 0;
  769.         noinsvib = 0;
  770.       }
  771.       if (instr[c].ptr[PTBL])
  772.         nopulse = 0;
  773.       if (instr[c].ptr[FTBL])
  774.         nofilter = 0;
  775.  
  776.       // See if all instruments use same gatetimer & firstwave parameters
  777.       if ((instr[c].gatetimer != instr[1].gatetimer) ||
  778.           (instr[c].firstwave != instr[1].firstwave))
  779.         fixedparams = 0;
  780.       // or if special firstwave commands are in use
  781.       if ((!instr[c].firstwave) || (instr[c].firstwave >= 0xfe))
  782.         fixedparams = 0;
  783.     }
  784.   }
  785.  
  786.   // Disable sameparam optimization for multispeed stability
  787.   if (multiplier > 1)
  788.   {
  789.     fixedparams = 0;
  790.     numlegato++;
  791.     numnohr++;
  792.   }
  793.  
  794.   if (fixedparams) instrsize -= instruments*2;
  795.   if (noinsvib) instrsize -= instruments*2;
  796.   if (nopulse) instrsize -= instruments;
  797.   if (nofilter) instrsize -= instruments;
  798.  
  799.   // Process tables
  800.   for (c = 0; c < MAX_TABLELEN; c++)
  801.   {
  802.     if (tableused[WTBL][c+1])
  803.     {
  804.       wavetblsize += 2;
  805.       if ((ltable[WTBL][c] >= WAVEDELAY) && (ltable[WTBL][c] <= WAVELASTDELAY)) nowavedelay = 0;
  806.       if ((ltable[WTBL][c] >= WAVECMD) && (ltable[WTBL][c] <= WAVELASTCMD))
  807.       {
  808.         nowavecmd = 0;
  809.         noeffects = 0;
  810.         switch (ltable[WTBL][c] - WAVECMD)
  811.         {
  812.           case CMD_PORTAUP:
  813.           case CMD_PORTADOWN:
  814.           noportamento = 0;
  815.           break;
  816.  
  817.           case CMD_TONEPORTA:
  818.           notoneporta = 0;
  819.           break;
  820.  
  821.           case CMD_VIBRATO:
  822.           novib = 0;
  823.           break;
  824.  
  825.           case CMD_SETAD:
  826.           nosetad = 0;
  827.           break;
  828.  
  829.           case CMD_SETSR:
  830.           nosetsr = 0;
  831.           break;
  832.  
  833.           case CMD_SETWAVE:
  834.           nosetwave = 0;
  835.           break;
  836.           
  837.           case CMD_SETPULSEPTR:
  838.           nosetpulseptr = 0;
  839.           break;
  840.  
  841.           case CMD_SETFILTERPTR:
  842.           nosetfiltptr = 0;
  843.           break;
  844.  
  845.           case CMD_SETFILTERCUTOFF:
  846.           nosetfiltcutoff = 0;
  847.           break;
  848.           
  849.           case CMD_SETFILTERCTRL:
  850.           nosetfiltctrl = 0;
  851.           break;
  852.  
  853.           case CMD_SETMASTERVOL:
  854.           nosetmastervol = 0;
  855.           break;
  856.         }
  857.       }
  858.       if (ltable[WTBL][c] < WAVECMD)
  859.       {
  860.         if (rtable[WTBL][c] <= 0x80)
  861.         {
  862.           int newlastnote = rtable[WTBL][c] + patternlastnote;
  863.           if (newlastnote > MAX_NOTES - 1) newlastnote = MAX_NOTES - 1;
  864.           if (rtable[WTBL][c] >= 0x20) firstnote = 0;
  865.            if (newlastnote > lastnote) lastnote = newlastnote;
  866.         }
  867.         else
  868.         {
  869.           int newfirstnote = rtable[WTBL][c] & 0x7f;
  870.           int newlastnote = rtable[WTBL][c] & 0x7f;
  871.           if (newlastnote > MAX_NOTES - 1) newlastnote = MAX_NOTES - 1;
  872.           if (newfirstnote < firstnote) firstnote = newfirstnote;
  873.           if (newlastnote > lastnote) lastnote = newlastnote;
  874.         }
  875.       }
  876.     }
  877.   }
  878.   for (c = 0; c < MAX_TABLELEN; c++)
  879.   {
  880.     if (tableused[PTBL][c+1])
  881.     {
  882.       pulsetblsize += 2;
  883.       if ((ltable[PTBL][c] >= 0x80) && (ltable[PTBL][c] != 0xff))
  884.       {
  885.         if (rtable[PTBL][c] & 0xf) simplepulse = 0;
  886.       }
  887.       if (ltable[PTBL][c] < 0x80)
  888.       {
  889.         nopulsemod = 0;
  890.         if (rtable[PTBL][c] & 0xf) simplepulse = 0;
  891.       }
  892.     }
  893.   }
  894.   for (c = 0; c < MAX_TABLELEN; c++)
  895.   {
  896.     if (tableused[FTBL][c+1])
  897.     {
  898.       filttblsize += 2;
  899.       if (ltable[FTBL][c] < 0x80) nofiltermod = 0;
  900.     }
  901.   }
  902.   for (c = 0; c < MAX_TABLELEN; c++)
  903.   {
  904.     if (tableused[STBL][c+1]) speedtblsize += 2;
  905.   }
  906.   // Zero entry of speedtable
  907.   if ((!novib) || (!nofunktempo) || (!noportamento) || (!notoneporta))
  908.     speedtblsize += 2;
  909.  
  910.   if (nopulse) pulsetblsize = 0;
  911.   if (nofilter) filttblsize = 0;
  912.  
  913.   sprintf(textbuffer, "SELECT START ADDRESS: (CURSORS=MOVE, ENTER=ACCEPT, ESC=CANCEL)");
  914.   printtext(1, 10, 15, textbuffer);
  915.  
  916.   selectdone = 0;
  917.   while (!selectdone)
  918.   {
  919.     sprintf(textbuffer, "$%04X", playeradr);
  920.     printtext(1, 11, 10, textbuffer);
  921.  
  922.     fliptoscreen();
  923.     waitkeynoupdate();
  924.  
  925.     if (win_quitted)
  926.     {
  927.       exitprogram = 1;
  928.       goto PRCLEANUP;
  929.     }
  930.  
  931.     switch(rawkey)
  932.     {
  933.       case KEY_LEFT:
  934.       playeradr -= 0x0400;
  935.       playeradr &= 0xff00;
  936.       break;
  937.  
  938.       case KEY_UP:
  939.       playeradr += 0x0100;
  940.       playeradr &= 0xff00;
  941.       break;
  942.  
  943.       case KEY_RIGHT:
  944.       playeradr += 0x0400;
  945.       playeradr &= 0xff00;
  946.       break;
  947.  
  948.       case KEY_DOWN:
  949.       playeradr -= 0x0100;
  950.       playeradr &= 0xff00;
  951.       break;
  952.  
  953.       case KEY_ESC:
  954.       selectdone = -1;
  955.       break;
  956.  
  957.       case KEY_ENTER:
  958.       selectdone = 1;
  959.       break;
  960.     }
  961.   }
  962.  
  963.   if (selectdone == -1) goto PRCLEANUP;
  964.  
  965.   sprintf(textbuffer, "SELECT ZEROPAGE ADDRESS: (CURSORS=MOVE, ENTER=ACCEPT, ESC=CANCEL)");
  966.   printtext(1, 13, 15, textbuffer);
  967.  
  968.   selectdone = 0;
  969.   while (!selectdone)
  970.   {
  971.     if (playerversion & PLAYER_ZPGHOSTREGS)
  972.     {
  973.       if (zeropageadr < 0x02) zeropageadr = 0xe5;
  974.       if (zeropageadr > 0xe5) zeropageadr = 0x02;
  975.     }
  976.     else
  977.     {
  978.       if (zeropageadr < 0x02) zeropageadr = 0xfe;
  979.       if (zeropageadr > 0xfe) zeropageadr = 0x02;
  980.     }
  981.  
  982.     if (!(playerversion & PLAYER_ZPGHOSTREGS))
  983.     {
  984.       if (zeropageadr < 0x90)
  985.         sprintf(textbuffer, "$%02X-$%02X (Used by BASIC interpreter)    ", zeropageadr, zeropageadr+1);
  986.       if ((zeropageadr >= 0x90) && (zeropageadr < 0xfb))
  987.         sprintf(textbuffer, "$%02X-$%02X (Used by KERNAL routines)      ", zeropageadr, zeropageadr+1);
  988.       if ((zeropageadr >= 0xfb) && (zeropageadr < 0xfe))
  989.         sprintf(textbuffer, "$%02X-$%02X (Unused)                       ", zeropageadr, zeropageadr+1);
  990.       if (zeropageadr >= 0xfe)
  991.         sprintf(textbuffer, "$%02X-$%02X ($FF used by BASIC interpreter)", zeropageadr, zeropageadr+1);
  992.     }
  993.     else
  994.     {
  995.       sprintf(textbuffer, "$%02X-$%02X (ghostregs start at %02X)", zeropageadr, zeropageadr+26, zeropageadr);
  996.     }
  997.  
  998.     printtext(1, 14, 10, textbuffer);
  999.  
  1000.     fliptoscreen();
  1001.     waitkeynoupdate();
  1002.  
  1003.     if (win_quitted)
  1004.     {
  1005.       exitprogram = 1;
  1006.       goto PRCLEANUP;
  1007.     }
  1008.  
  1009.     switch(rawkey)
  1010.     {
  1011.       case KEY_LEFT:
  1012.       zeropageadr -= 0x10;
  1013.       break;
  1014.  
  1015.       case KEY_UP:
  1016.       zeropageadr++;
  1017.       break;
  1018.  
  1019.       case KEY_RIGHT:
  1020.       zeropageadr += 0x10;
  1021.       break;
  1022.  
  1023.       case KEY_DOWN:
  1024.       zeropageadr--;
  1025.       break;
  1026.  
  1027.       case KEY_ESC:
  1028.       selectdone = -1;
  1029.       break;
  1030.  
  1031.       case KEY_ENTER:
  1032.       selectdone = 1;
  1033.       break;
  1034.     }
  1035.   }
  1036.  
  1037.   if (selectdone == -1) goto PRCLEANUP;
  1038.  
  1039.   // Validate frequencytable parameters
  1040.   if (lastnote < firstnote)
  1041.     lastnote = firstnote;
  1042.   if (firstnote < 0) firstnote = 0;
  1043.   if (!nocalculatedspeed)
  1044.     lastnote++; // Calculated speeds need the next frequency value
  1045.   if (lastnote > MAX_NOTES-1) lastnote = MAX_NOTES-1;
  1046.   // For sound effect support, always use the full table
  1047.   if (playerversion & PLAYER_SOUNDEFFECTS)
  1048.   {
  1049.     firstnote = 0;
  1050.     lastnote = MAX_NOTES-1;
  1051.   }
  1052.  
  1053.   // Insert baseaddresses
  1054.   insertdefine("base", playeradr);
  1055.   insertdefine("zpbase", zeropageadr);
  1056.   insertdefine("SIDBASE", sidaddress & 0xffff);
  1057.   insertdefine("SID2BASE", sidaddress >> 16);
  1058.  
  1059.   // Insert conditionals
  1060.   insertdefine("SOUNDSUPPORT", (playerversion & PLAYER_SOUNDEFFECTS) ? 1 : 0);
  1061.   insertdefine("VOLSUPPORT", (playerversion & PLAYER_VOLUME) ? 1 : 0);
  1062.   insertdefine("BUFFEREDWRITES", (playerversion & PLAYER_BUFFERED) ? 1 : 0);
  1063.   insertdefine("ZPGHOSTREGS", (playerversion & PLAYER_ZPGHOSTREGS) ? 1 : 0);
  1064.   insertdefine("FIXEDPARAMS", fixedparams);
  1065.   insertdefine("SIMPLEPULSE", simplepulse);
  1066.   insertdefine("PULSEOPTIMIZATION", optimizepulse);
  1067.   insertdefine("REALTIMEOPTIMIZATION", optimizerealtime);
  1068.   insertdefine("NOAUTHORINFO", (playerversion & PLAYER_AUTHORINFO) ? 0 : 1);
  1069.   insertdefine("NOEFFECTS", noeffects);
  1070.   insertdefine("NOGATE", nogate);
  1071.   insertdefine("NOFILTER", nofilter);
  1072.   insertdefine("NOFILTERMOD", nofiltermod);
  1073.   insertdefine("NOPULSE", nopulse);
  1074.   insertdefine("NOPULSEMOD", nopulsemod);
  1075.   insertdefine("NOWAVEDELAY", nowavedelay);
  1076.   insertdefine("NOWAVECMD", nowavecmd);
  1077.   insertdefine("NOREPEAT", norepeat);
  1078.   insertdefine("NOTRANS", notrans);
  1079.   insertdefine("NOPORTAMENTO", noportamento);
  1080.   insertdefine("NOTONEPORTA", notoneporta);
  1081.   insertdefine("NOVIB", novib);
  1082.   insertdefine("NOINSTRVIB", noinsvib);
  1083.   insertdefine("NOSETAD", nosetad);
  1084.   insertdefine("NOSETSR", nosetsr);
  1085.   insertdefine("NOSETWAVE", nosetwave);
  1086.   insertdefine("NOSETWAVEPTR", nosetwaveptr);
  1087.   insertdefine("NOSETPULSEPTR", nosetpulseptr);
  1088.   insertdefine("NOSETFILTPTR", nosetfiltptr);
  1089.   insertdefine("NOSETFILTCTRL", nosetfiltctrl);
  1090.   insertdefine("NOSETFILTCUTOFF", nosetfiltcutoff);
  1091.   insertdefine("NOSETMASTERVOL", nosetmastervol);
  1092.   insertdefine("NOFUNKTEMPO", nofunktempo);
  1093.   insertdefine("NOGLOBALTEMPO", noglobaltempo);
  1094.   insertdefine("NOCHANNELTEMPO", nochanneltempo);
  1095.   insertdefine("NOFIRSTWAVECMD", nofirstwavecmd);
  1096.   insertdefine("NOCALCULATEDSPEED", nocalculatedspeed);
  1097.   insertdefine("NONORMALSPEED", nonormalspeed);
  1098.   insertdefine("NOZEROSPEED", nozerospeed);
  1099.  
  1100.   // Insert parameters
  1101.   insertdefine("NUMCHANNELS", channels);
  1102.   insertdefine("NUMSONGS", songs);
  1103.   insertdefine("FIRSTNOTE", firstnote);
  1104.   insertdefine("FIRSTNOHRINSTR", numnormal + 1);
  1105.   insertdefine("FIRSTLEGATOINSTR", numnormal + numnohr + 1);
  1106.   insertdefine("NUMHRINSTR", numnormal);
  1107.   insertdefine("NUMNOHRINSTR", numnohr);
  1108.   insertdefine("NUMLEGATOINSTR", numlegato);
  1109.   insertdefine("ADPARAM", adparam >> 8);
  1110.   insertdefine("SRPARAM", adparam & 0xff);
  1111.   if ((instr[MAX_INSTR-1].ad >= 2) && (!(instr[MAX_INSTR-1].ptr[WTBL])))
  1112.     insertdefine("DEFAULTTEMPO", instr[MAX_INSTR-1].ad - 1);
  1113.   else
  1114.     insertdefine("DEFAULTTEMPO", multiplier ? (multiplier*6-1) : 5);
  1115.  
  1116.   // Fixed firstwave & gatetimer
  1117.   if (fixedparams)
  1118.   {
  1119.     insertdefine("FIRSTWAVEPARAM", instr[1].firstwave);
  1120.     insertdefine("GATETIMERPARAM", instr[1].gatetimer & 0x3f);
  1121.   }
  1122.  
  1123.   // Insert source code of player
  1124.   if (adparam >= 0xf000)
  1125.     playername = "altplayer.s";
  1126.     
  1127.   if (!insertfile(playername))
  1128.   {
  1129.     clearscreen();
  1130.     printtextc(MAX_ROWS/2, CTITLE, "COULD NOT OPEN PLAYROUTINE!");
  1131.     fliptoscreen();
  1132.     waitkeynoupdate();
  1133.     goto PRCLEANUP;
  1134.   }
  1135.  
  1136.   // Insert frequencytable
  1137.   insertlabel("mt_freqtbllo");
  1138.   insertbytes(&freqtbllo[firstnote], lastnote-firstnote+1);
  1139.   insertlabel("mt_freqtblhi");
  1140.   insertbytes(&freqtblhi[firstnote], lastnote-firstnote+1);
  1141.  
  1142.   // Insert songtable
  1143.   insertlabel("mt_songtbllo");
  1144.   for (c = 0; c < songs*6; c++)
  1145.   {
  1146.     sprintf(textbuffer, "mt_song%d", c);
  1147.     insertaddrlo(textbuffer);
  1148.   }
  1149.   insertlabel("mt_songtblhi");
  1150.   for (c = 0; c < songs*6; c++)
  1151.   {
  1152.     sprintf(textbuffer, "mt_song%d", c);
  1153.     insertaddrhi(textbuffer);
  1154.   }
  1155.  
  1156.   // Insert patterntable
  1157.   insertlabel("mt_patttbllo");
  1158.   for (c = 0; c < patterns; c++)
  1159.   {
  1160.     sprintf(textbuffer, "mt_patt%d", c);
  1161.     insertaddrlo(textbuffer);
  1162.   }
  1163.   insertlabel("mt_patttblhi");
  1164.   for (c = 0; c < patterns; c++)
  1165.   {
  1166.     sprintf(textbuffer, "mt_patt%d", c);
  1167.     insertaddrhi(textbuffer);
  1168.   }
  1169.  
  1170.   // Insert instruments
  1171.   insertlabel("mt_insad");
  1172.   insertbytes(&instrwork[0], instruments);
  1173.   insertlabel("mt_inssr");
  1174.   insertbytes(&instrwork[instruments], instruments);
  1175.   insertlabel("mt_inswaveptr");
  1176.   insertbytes(&instrwork[instruments*2], instruments);
  1177.   if (!nopulse)
  1178.   {
  1179.     insertlabel("mt_inspulseptr");
  1180.     insertbytes(&instrwork[instruments*3], instruments);
  1181.   }
  1182.   if (!nofilter)
  1183.   {
  1184.     insertlabel("mt_insfiltptr");
  1185.     insertbytes(&instrwork[instruments*4], instruments);
  1186.   }
  1187.   if (!noinsvib)
  1188.   {
  1189.     insertlabel("mt_insvibparam");
  1190.     insertbytes(&instrwork[instruments*5], instruments);
  1191.     insertlabel("mt_insvibdelay");
  1192.     insertbytes(&instrwork[instruments*6], instruments);
  1193.   }
  1194.   if (!fixedparams)
  1195.   {
  1196.     insertlabel("mt_insgatetimer");
  1197.     insertbytes(&instrwork[instruments*7], instruments);
  1198.     insertlabel("mt_insfirstwave");
  1199.     insertbytes(&instrwork[instruments*8], instruments);
  1200.   }
  1201.  
  1202.   // Insert tables
  1203.   for (c = 0; c < MAX_TABLES; c++)
  1204.   {
  1205.     if ((c == PTBL) && (nopulse)) goto SKIPTABLE;
  1206.     if ((c == FTBL) && (nofilter)) goto SKIPTABLE;
  1207.  
  1208.     // Write table left side
  1209.     // Extra zero for speedtable
  1210.     if ((c == STBL) && ((!novib) || (!nofunktempo) || (!noportamento) || (!notoneporta))) insertbyte(0);
  1211.     // Table label
  1212.     insertlabel(tableleftname[c]);
  1213.  
  1214.     // Table data
  1215.     for (d = 0; d < MAX_TABLELEN; d++)
  1216.     {
  1217.       if (tableused[c][d+1])
  1218.       {
  1219.         switch (c)
  1220.         {
  1221.           // In wavetable, convert waveform values for the playroutine
  1222.           case WTBL:
  1223.           {
  1224.             unsigned char wave = ltable[c][d];
  1225.             if ((ltable[c][d] >= WAVESILENT) && (ltable[c][d] <= WAVELASTSILENT)) wave &= 0xf;
  1226.             if ((ltable[c][d] > WAVELASTDELAY) && (ltable[c][d] <= WAVELASTSILENT) && (!nowavedelay)) wave += 0x10;
  1227.             insertbyte(wave);
  1228.           }
  1229.           break;
  1230.  
  1231.           case PTBL:
  1232.           if ((simplepulse) && (ltable[c][d] != 0xff) && (ltable[c][d] > 0x80))
  1233.             insertbyte(0x80);
  1234.           else
  1235.             insertbyte(ltable[c][d]);
  1236.           break;
  1237.  
  1238.           // In filtertable, modify passband bits
  1239.           case FTBL:
  1240.           if ((ltable[c][d] != 0xff) && (ltable[c][d] > 0x80))
  1241.             insertbyte(((ltable[c][d] & 0x70) >> 1) | 0x80);
  1242.           else
  1243.             insertbyte(ltable[c][d]);
  1244.           break;
  1245.  
  1246.           default:
  1247.           insertbyte(ltable[c][d]);
  1248.           break;
  1249.         }
  1250.       }
  1251.     }
  1252.  
  1253.     // Write table right side, remapping jumps as necessary
  1254.     // Extra zero for speedtable
  1255.     if ((c == STBL) && ((!novib) || (!nofunktempo) || (!noportamento) || (!notoneporta))) insertbyte(0);
  1256.     // Table label
  1257.     insertlabel(tablerightname[c]);
  1258.  
  1259.     for (d = 0; d < MAX_TABLELEN; d++)
  1260.     {
  1261.       if (tableused[c][d+1])
  1262.       {
  1263.         if ((ltable[c][d] != 0xff) || (c == STBL))
  1264.         {
  1265.           switch(c)
  1266.           {
  1267.             case WTBL:
  1268.             if ((ltable[c][d] >= WAVECMD) && (ltable[c][d] <= WAVELASTCMD))
  1269.             {
  1270.               // Remap table-referencing commands
  1271.               switch (ltable[c][d] - WAVECMD)
  1272.               {
  1273.                 case CMD_PORTAUP:
  1274.                 case CMD_PORTADOWN:
  1275.                 case CMD_TONEPORTA:
  1276.                 case CMD_VIBRATO:
  1277.                 insertbyte(tablemap[STBL][rtable[c][d]]);
  1278.                 break;
  1279.  
  1280.                 case CMD_SETPULSEPTR:
  1281.                 insertbyte(tablemap[PTBL][rtable[c][d]]);
  1282.                 break;
  1283.  
  1284.                 case CMD_SETFILTERPTR:
  1285.                 insertbyte(tablemap[FTBL][rtable[c][d]]);
  1286.                 break;
  1287.  
  1288.                 default:
  1289.                 insertbyte(rtable[c][d]);
  1290.                 break;
  1291.               }
  1292.             }
  1293.             else
  1294.             {
  1295.               // For normal notes, reverse all right side high bits
  1296.               insertbyte(rtable[c][d] ^ 0x80);
  1297.             }
  1298.             break;
  1299.  
  1300.             case PTBL:
  1301.             if (simplepulse)
  1302.             {
  1303.               if (ltable[c][d] >= 0x80)
  1304.                 insertbyte((ltable[c][d] & 0x0f) | (rtable[c][d] & 0xf0));
  1305.               else
  1306.               {
  1307.                 int pulsespeed = rtable[c][d] >> 4;
  1308.                 if (rtable[c][d] & 0x80)
  1309.                 {
  1310.                   pulsespeed |= 0xf0;
  1311.                   pulsespeed--;
  1312.                 }
  1313.                 pulsespeed = swapnybbles(pulsespeed);
  1314.                 insertbyte(pulsespeed);
  1315.               }
  1316.             }
  1317.             else
  1318.               insertbyte(rtable[c][d]);
  1319.             break;
  1320.  
  1321.             default:
  1322.             insertbyte(rtable[c][d]);
  1323.             break;
  1324.           }
  1325.         }
  1326.         else
  1327.           insertbyte(tablemap[c][rtable[c][d]]);
  1328.       }
  1329.     }
  1330.  
  1331.     SKIPTABLE: ;
  1332.   }
  1333.  
  1334.   // Insert orderlists
  1335.   for (c = 0; c < songs; c++)
  1336.   {
  1337.     for (d = 0; d < MAX_CHN; d++)
  1338.     {
  1339.       sprintf(textbuffer, "mt_song%d", c*6+d);
  1340.       insertlabel(textbuffer);
  1341.       insertbytes(&songwork[songoffset[c][d]], songsize[c][d]);
  1342.     }
  1343.   }
  1344.  
  1345.   // Insert patterns
  1346.   for (c = 0; c < patterns; c++)
  1347.   {
  1348.     sprintf(textbuffer, "mt_patt%d", c);
  1349.     insertlabel(textbuffer);
  1350.     insertbytes(&pattwork[pattoffset[c]], pattsize[c]);
  1351.   }
  1352.  
  1353.   //{
  1354.   //  FILE *handle = fopen("debug.s", "wb");
  1355.   //  fwrite(membuf_get(&src), membuf_memlen(&src), 1, handle);
  1356.   //  fclose(handle);
  1357.   //}
  1358.  
  1359.   // Assemble; on error fail in a rude way (the parser does so too)
  1360.   if (assemble(&src, &dest)) exit(1);
  1361.  
  1362.   packeddata = membuf_get(&dest);
  1363.   packedsize = membuf_memlen(&dest);
  1364.   playersize = packedsize - songtblsize - songdatasize - patttblsize - pattdatasize - instrsize - wavetblsize - pulsetblsize - filttblsize - speedtblsize;
  1365.  
  1366.   // Copy author info
  1367.   if (playerversion & PLAYER_AUTHORINFO)
  1368.   {
  1369.     for (c = 0; c < 32; c++)
  1370.     {
  1371.       packeddata[32+c] = authorname[c];
  1372.       // Convert 0 to space
  1373.       if (packeddata[32+c] == 0) packeddata[32+c] = 0x20;
  1374.     }
  1375.   }
  1376.  
  1377.   // Print results
  1378.   clearscreen();
  1379.   printblankc(0, 0, 15+16, MAX_COLUMNS);
  1380.   if (!strlen(loadedsongfilename))
  1381.     sprintf(textbuffer, "%s Packer/Relocator", programname);
  1382.   else
  1383.     sprintf(textbuffer, "%s Packer/Relocator - %s", programname, loadedsongfilename);
  1384.   textbuffer[80] = 0;
  1385.   printtext(0, 0, 15+16, textbuffer);
  1386.  
  1387.   sprintf(textbuffer, "PACKING RESULTS:");
  1388.   printtext(1, 2, 15, textbuffer);
  1389.  
  1390.   sprintf(textbuffer, "Playroutine:     %d bytes", playersize);
  1391.   printtext(1, 3, 7, textbuffer);
  1392.   sprintf(textbuffer, "Songtable:       %d bytes", songtblsize);
  1393.   printtext(1, 4, 7, textbuffer);
  1394.   sprintf(textbuffer, "Song-orderlists: %d bytes", songdatasize);
  1395.   printtext(1, 5, 7, textbuffer);
  1396.   sprintf(textbuffer, "Patterntable:    %d bytes", patttblsize);
  1397.   printtext(1, 6, 7, textbuffer);
  1398.   sprintf(textbuffer, "Patterns:        %d bytes", pattdatasize);
  1399.   printtext(1, 7, 7, textbuffer);
  1400.   sprintf(textbuffer, "Instruments:     %d bytes", instrsize);
  1401.   printtext(1, 8, 7, textbuffer);
  1402.   sprintf(textbuffer, "Tables:          %d bytes", wavetblsize+pulsetblsize+filttblsize+speedtblsize);
  1403.   printtext(1, 9, 7, textbuffer);
  1404.   sprintf(textbuffer, "Total size:      %d bytes", packedsize);
  1405.   printtext(1, 11, 7, textbuffer);
  1406.   fliptoscreen();
  1407.  
  1408.  
  1409.   // Now ask for fileformat
  1410.   printtext(1, 13, CTITLE, "SELECT FORMAT TO SAVE IN: (CURSORS=MOVE, ENTER=ACCEPT, ESC=CANCEL)");
  1411.  
  1412.   selectdone = 0;
  1413.  
  1414.   while (!selectdone)
  1415.   {
  1416.     switch(fileformat)
  1417.     {
  1418.       case FORMAT_SID:
  1419.       printtext(1, 14, CEDIT, "SID - SIDPlay music file format          ");
  1420.       strcpy(packedfilter, "*.sid");
  1421.       break;
  1422.  
  1423.       case FORMAT_PRG:
  1424.       printtext(1, 14, CEDIT, "PRG - C64 native format                  ");
  1425.       strcpy(packedfilter, "*.prg");
  1426.       break;
  1427.  
  1428.       case FORMAT_BIN:
  1429.       printtext(1, 14, CEDIT, "BIN - Raw binary format (no startaddress)");
  1430.       strcpy(packedfilter, "*.bin");
  1431.       break;
  1432.     }
  1433.  
  1434.     fliptoscreen();
  1435.     waitkeynoupdate();
  1436.  
  1437.     if (win_quitted)
  1438.     {
  1439.       exitprogram = 1;
  1440.       goto PRCLEANUP;
  1441.     }
  1442.  
  1443.     switch(rawkey)
  1444.     {
  1445.       case KEY_LEFT:
  1446.       case KEY_DOWN:
  1447.       fileformat--;
  1448.       if (fileformat < FORMAT_SID) fileformat = FORMAT_BIN;
  1449.       break;
  1450.  
  1451.       case KEY_RIGHT:
  1452.       case KEY_UP:
  1453.       fileformat++;
  1454.       if (fileformat > FORMAT_BIN) fileformat = FORMAT_SID;
  1455.       break;
  1456.  
  1457.       case KEY_ESC:
  1458.       selectdone = -1;
  1459.       break;
  1460.  
  1461.       case KEY_ENTER:
  1462.       selectdone = 1;
  1463.       break;
  1464.     }
  1465.   }
  1466.   if (selectdone == -1) goto PRCLEANUP;
  1467.  
  1468.   // By default, copy loaded song name up to the extension
  1469.   memset(packedsongname, 0, sizeof packedsongname);
  1470.   for (c = 0; c < strlen(loadedsongfilename); c++)
  1471.   {
  1472.     if (loadedsongfilename[c] == '.') break;
  1473.     packedsongname[c] = loadedsongfilename[c];
  1474.   }
  1475.   switch (fileformat)
  1476.   {
  1477.     case FORMAT_PRG:
  1478.     strcat(packedsongname, ".prg");
  1479.     break;
  1480.  
  1481.     case FORMAT_BIN:
  1482.     strcat(packedsongname, ".bin");
  1483.     break;
  1484.  
  1485.     case FORMAT_SID:
  1486.     strcat(packedsongname, ".sid");
  1487.     break;
  1488.   }
  1489.  
  1490.   // Now ask for filename, retry if unsuccessful
  1491.   while (!songhandle)
  1492.   {
  1493.     if (!fileselector(packedsongname, packedpath, packedfilter, "Save Music+Playroutine", 3))
  1494.       goto PRCLEANUP;
  1495.  
  1496.     if (strlen(packedsongname) < MAX_FILENAME-4)
  1497.     {
  1498.       int extfound = 0;
  1499.       for (c = strlen(packedsongname)-1; c >= 0; c--)
  1500.       {
  1501.         if (packedsongname[c] == '.') extfound = 1;
  1502.       }
  1503.       if (!extfound)
  1504.       {
  1505.         switch (fileformat)
  1506.         {
  1507.           case FORMAT_PRG:
  1508.           strcat(packedsongname, ".prg");
  1509.           break;
  1510.  
  1511.           case FORMAT_BIN:
  1512.           strcat(packedsongname, ".bin");
  1513.           break;
  1514.  
  1515.           case FORMAT_SID:
  1516.           strcat(packedsongname, ".sid");
  1517.           break;
  1518.         }
  1519.       }
  1520.     }
  1521.     songhandle = fopen(packedsongname, "wb");
  1522.   }
  1523.  
  1524.   if (fileformat == FORMAT_PRG)
  1525.   {
  1526.     fwritele16(songhandle, playeradr);
  1527.   }
  1528.   if (fileformat == FORMAT_SID)
  1529.   {
  1530.     unsigned char ident[] = {'P', 'S', 'I', 'D', 0x00, 0x02, 0x00, 0x7c};
  1531.     unsigned char byte;
  1532.     // Identification
  1533.     fwrite(ident, sizeof ident, 1, songhandle);
  1534.  
  1535.     // Load address
  1536.     byte = 0x00;
  1537.     fwrite8(songhandle, byte);
  1538.     fwrite8(songhandle, byte);
  1539.  
  1540.     // Init address
  1541.     if ((multiplier > 1) || (!multiplier))
  1542.     {
  1543.       unsigned speedvalue;
  1544.       byte = (playeradr-10) >> 8;
  1545.       fwrite8(songhandle, byte);
  1546.       byte = (playeradr-10) & 0xff;
  1547.       fwrite8(songhandle, byte);
  1548.  
  1549.       if (multiplier)
  1550.       {
  1551.         if (ntsc) speedvalue = 0x42c6/multiplier;
  1552.         else speedvalue = 0x4cc7/multiplier;
  1553.       }
  1554.       else
  1555.       {
  1556.         if (ntsc) speedvalue = 0x42c6*2;
  1557.         else speedvalue = 0x4cc7*2;
  1558.       }
  1559.       speedcode[1] = speedvalue & 0xff;
  1560.       speedcode[6] = speedvalue >> 8;
  1561.     }
  1562.     else
  1563.     {
  1564.       byte = (playeradr) >> 8;
  1565.       fwrite8(songhandle, byte);
  1566.       byte = (playeradr) & 0xff;
  1567.       fwrite8(songhandle, byte);
  1568.     }
  1569.  
  1570.     // Play address
  1571.     byte = (playeradr+3) >> 8;
  1572.     fwrite8(songhandle, byte);
  1573.     byte = (playeradr+3) & 0xff;
  1574.     fwrite8(songhandle, byte);
  1575.  
  1576.     // Number of subtunes
  1577.     byte = 0x00;
  1578.     fwrite8(songhandle, byte);
  1579.     byte = songs;
  1580.     fwrite8(songhandle, byte);
  1581.  
  1582.     // Default subtune
  1583.     byte = 0x00;
  1584.     fwrite8(songhandle, byte);
  1585.     byte = 0x01;
  1586.     fwrite8(songhandle, byte);
  1587.  
  1588.     // Song speed bits
  1589.     byte = 0x00;
  1590.     if ((ntsc) || (multiplier > 1) || (!multiplier)) byte = 0xff;
  1591.     fwrite8(songhandle, byte);
  1592.     fwrite8(songhandle, byte);
  1593.     fwrite8(songhandle, byte);
  1594.     fwrite8(songhandle, byte);
  1595.  
  1596.     // Songname etc.
  1597.     fwrite(songname, sizeof songname, 1, songhandle);
  1598.     fwrite(authorname, sizeof authorname, 1, songhandle);
  1599.     fwrite(copyrightname, sizeof copyrightname, 1, songhandle);
  1600.  
  1601.     // Flags
  1602.     byte = 0x00;
  1603.     fwrite8(songhandle, byte);
  1604.     if (ntsc) byte = 8;
  1605.       else byte = 4;
  1606.     if (sidmodel) byte |= 32;
  1607.       else byte |= 16;
  1608.     fwrite8(songhandle, byte);
  1609.  
  1610.     // Reserved longword
  1611.     byte = 0x00;
  1612.     fwrite8(songhandle, byte);
  1613.     fwrite8(songhandle, byte);
  1614.     fwrite8(songhandle, byte);
  1615.     fwrite8(songhandle, byte);
  1616.  
  1617.     // Load address
  1618.     if ((multiplier > 1) || (!multiplier))
  1619.     {
  1620.       byte = (playeradr-10) & 0xff;
  1621.       fwrite8(songhandle, byte);
  1622.       byte = (playeradr-10) >> 8;
  1623.       fwrite8(songhandle, byte);
  1624.     }
  1625.     else
  1626.     {
  1627.       byte = (playeradr) & 0xff;
  1628.       fwrite8(songhandle, byte);
  1629.       byte = (playeradr) >> 8;
  1630.       fwrite8(songhandle, byte);
  1631.     }
  1632.     if ((multiplier > 1) || (!multiplier)) fwrite(speedcode, 10, 1, songhandle);
  1633.   }
  1634.  
  1635.   fwrite(packeddata, packedsize, 1, songhandle);
  1636.   fclose(songhandle);
  1637.  
  1638.   PRCLEANUP:
  1639.   membuf_free(&src);
  1640.   membuf_free(&dest);
  1641.  
  1642.   if (pattwork) free(pattwork);
  1643.   if (songwork) free(songwork);
  1644.   if (instrwork) free(instrwork);
  1645.   printmainscreen();
  1646.   key = 0;
  1647.   rawkey = 0;
  1648. }
  1649.  
  1650. int packpattern(unsigned char *dest, unsigned char *src, int rows)
  1651. {
  1652.   unsigned char temp1[MAX_PATTROWS*4];
  1653.   unsigned char temp2[512];
  1654.   unsigned char instr = 0;
  1655.   int command = -1;
  1656.   int databyte = -1;
  1657.   int destsizeim = 0;
  1658.   int destsize = 0;
  1659.   int c, d;
  1660.  
  1661.   // First optimize instrument changes
  1662.   for (c = 0; c < rows; c++)
  1663.   {
  1664.     if ((c) && (src[c*4+1]) && (src[c*4+1] == instr))
  1665.     {
  1666.       temp1[c*4] = src[c*4];
  1667.       temp1[c*4+1] = 0;
  1668.       temp1[c*4+2] = src[c*4+2];
  1669.       temp1[c*4+3] = src[c*4+3];
  1670.     }
  1671.     else
  1672.     {
  1673.       temp1[c*4] = src[c*4];
  1674.       temp1[c*4+1] = src[c*4+1];
  1675.       temp1[c*4+2] = src[c*4+2];
  1676.       temp1[c*4+3] = src[c*4+3];
  1677.       if (src[c*4+1])
  1678.         instr = src[c*4+1];
  1679.     }
  1680.  
  1681.     switch(temp1[c*4+2])
  1682.     {
  1683.       // Remap speedtable commands
  1684.       case CMD_PORTAUP:
  1685.       case CMD_PORTADOWN:
  1686.       noportamento = 0;
  1687.       temp1[c*4+3] = tablemap[STBL][temp1[c*4+3]];
  1688.       break;
  1689.  
  1690.       case CMD_TONEPORTA:
  1691.       notoneporta = 0;
  1692.       temp1[c*4+3] = tablemap[STBL][temp1[c*4+3]];
  1693.       break;
  1694.  
  1695.       case CMD_VIBRATO:
  1696.       novib = 0;
  1697.       temp1[c*4+3] = tablemap[STBL][temp1[c*4+3]];
  1698.       break;
  1699.  
  1700.       case CMD_SETAD:
  1701.       nosetad = 0;
  1702.       break;
  1703.  
  1704.       case CMD_SETSR:
  1705.       nosetsr = 0;
  1706.       break;
  1707.       
  1708.       case CMD_SETWAVE:
  1709.       nosetwave = 0;
  1710.       break;
  1711.  
  1712.       // Remap table commands
  1713.       case CMD_SETWAVEPTR:
  1714.       nosetwaveptr = 0;
  1715.       temp1[c*4+3] = tablemap[WTBL][temp1[c*4+3]];
  1716.       break;
  1717.  
  1718.       case CMD_SETPULSEPTR:
  1719.       nosetpulseptr = 0;
  1720.       nopulse = 0;
  1721.       temp1[c*4+3] = tablemap[PTBL][temp1[c*4+3]];
  1722.       break;
  1723.  
  1724.       case CMD_SETFILTERPTR:
  1725.       nosetfiltptr = 0;
  1726.       nofilter = 0;
  1727.       temp1[c*4+3] = tablemap[FTBL][temp1[c*4+3]];
  1728.       break;
  1729.  
  1730.       case CMD_SETFILTERCTRL:
  1731.       nosetfiltctrl = 0;
  1732.       nofilter = 0;
  1733.       break;
  1734.  
  1735.       case CMD_SETFILTERCUTOFF:
  1736.       nosetfiltcutoff = 0;
  1737.       nofilter = 0;
  1738.       break;
  1739.  
  1740.       case CMD_SETMASTERVOL:
  1741.       nosetmastervol = 0;
  1742.       // If no authorinfo being saved, erase timingmarks (not supported)
  1743.       if (!(playerversion & PLAYER_AUTHORINFO))
  1744.       {
  1745.         if (temp1[c*4+3] > 0x0f)
  1746.         {
  1747.           temp1[c*4+2] = 0;
  1748.           temp1[c*4+3] = 0;
  1749.         }
  1750.       }
  1751.       break;
  1752.  
  1753.       case CMD_FUNKTEMPO:
  1754.       nofunktempo = 0;
  1755.       temp1[c*4+3] = tablemap[STBL][temp1[c*4+3]];
  1756.       break;
  1757.  
  1758.       case CMD_SETTEMPO:
  1759.       if (temp1[c*4+3] >= 0x80) nochanneltempo = 0;
  1760.       else noglobaltempo = 0;
  1761.       // Decrease databyte of all tempo commands for the playroutine
  1762.       // Do not touch funktempo
  1763.       if ((temp1[c*4+3] & 0x7f) >= 3)
  1764.         temp1[c*4+3]--;
  1765.       break;
  1766.  
  1767.     }
  1768.   }
  1769.  
  1770.   if (noeffects)
  1771.   {
  1772.     command = 0;
  1773.     databyte = 0;
  1774.   }
  1775.  
  1776.   // Write in playroutine format
  1777.   for (c = 0; c < rows; c++)
  1778.   {
  1779.     // Instrument change with mapping
  1780.     if (temp1[c*4+1])
  1781.     {
  1782.       temp2[destsizeim++] = instrmap[INSTRCHG+temp1[c*4+1]];
  1783.     }
  1784.     // Rest+FX
  1785.     if (temp1[c*4] == REST)
  1786.     {
  1787.       if ((temp1[c*4+2] != command) || (temp1[c*4+3] != databyte))
  1788.       {
  1789.         command = temp1[c*4+2];
  1790.         databyte = temp1[c*4+3];
  1791.         temp2[destsizeim++] = FXONLY+command;
  1792.         if (command)
  1793.           temp2[destsizeim++] = databyte;
  1794.       }
  1795.       else
  1796.         temp2[destsizeim++] = REST;
  1797.     }
  1798.     else
  1799.     {
  1800.       // Normal note
  1801.       if ((temp1[c*4+2] != command) || (temp1[c*4+3] != databyte))
  1802.       {
  1803.         command = temp1[c*4+2];
  1804.         databyte = temp1[c*4+3];
  1805.         temp2[destsizeim++] = FX+command;
  1806.         if (command)
  1807.           temp2[destsizeim++] = databyte;
  1808.       }
  1809.       temp2[destsizeim++] = temp1[c*4];
  1810.     }
  1811.   }
  1812.  
  1813.   // Final step: optimize long singlebyte rests with "packed rest"
  1814.   for (c = 0; c < destsizeim;)
  1815.   {
  1816.     int packok = 1;
  1817.  
  1818.     // Never pack first row or sequencer goes crazy
  1819.     if (!c) packok = 0;
  1820.     
  1821.     // There must be no instrument or command changes on the row to be packed
  1822.     if (temp2[c] < FX)
  1823.     {
  1824.       dest[destsize++] = temp2[c++];
  1825.       packok = 0;
  1826.     }
  1827.     if ((temp2[c] >= FXONLY) && (temp2[c] < FIRSTNOTE))
  1828.     {
  1829.       int fxnum = temp2[c] - FXONLY;
  1830.       dest[destsize++] = temp2[c++];
  1831.       if (fxnum) dest[destsize++] = temp2[c++];
  1832.       packok = 0;
  1833.       goto NEXTROW;
  1834.     }
  1835.     if (temp2[c] < FXONLY)
  1836.      {
  1837.       int fxnum = temp2[c] - FX;
  1838.       dest[destsize++] = temp2[c++];
  1839.       if (fxnum) dest[destsize++] = temp2[c++];
  1840.       packok = 0;
  1841.     }
  1842.  
  1843.     if (temp2[c] != REST) packok = 0;
  1844.  
  1845.     if (!packok)
  1846.       dest[destsize++] = temp2[c++];
  1847.     else
  1848.     {
  1849.       for (d = c; d < destsizeim; )
  1850.       {
  1851.         if (temp2[d] == REST)
  1852.         {
  1853.           d++;
  1854.           if (d-c == 64) break;
  1855.         }
  1856.         else break;
  1857.       }
  1858.       d -= c;
  1859.       if (d > 1)
  1860.       {
  1861.         dest[destsize++] = -d;
  1862.         c += d;
  1863.       }
  1864.       else
  1865.         dest[destsize++] = temp2[c++];
  1866.     }
  1867.     NEXTROW: {}
  1868.   }
  1869.   // See if pattern too big
  1870.   if (destsize > 256) return -1;
  1871.  
  1872.   // If less than 256 bytes, insert endmark
  1873.   if (destsize < 256)
  1874.   dest[destsize++] = 0x00;
  1875.  
  1876.   return destsize;
  1877. }
  1878.  
  1879. int testoverlap(int area1start, int area1size, int area2start, int area2size)
  1880. {
  1881.   int area1last = area1start+area1size-1;
  1882.   int area2last = area2start+area2size-1;
  1883.  
  1884.   if (area1start == area2start) return 1;
  1885.  
  1886.   if (area1start < area2start)
  1887.   {
  1888.     if (area1last < area2start) return 0;
  1889.     else return 1;
  1890.   }
  1891.   else
  1892.   {
  1893.     if (area2last < area1start) return 0;
  1894.     else return 1;
  1895.   }
  1896. }
  1897.  
  1898. unsigned char swapnybbles(unsigned char n)
  1899. {
  1900.   unsigned char highnybble = n >> 4;
  1901.   unsigned char lownybble = n & 0xf;
  1902.  
  1903.   return (lownybble << 4) | highnybble;
  1904. }
  1905.  
  1906. int insertfile(char *name)
  1907. {
  1908.   int size;
  1909.   int handle = io_open(name);
  1910.   if (handle == -1) return 0;
  1911.  
  1912.   size = io_lseek(handle, 0, SEEK_END);
  1913.   io_lseek(handle, 0, SEEK_SET);
  1914.   while (size--)
  1915.   {
  1916.     membuf_append_char(&src, io_read8(handle));
  1917.   }
  1918.   io_close(handle);
  1919.   return 1;
  1920. }
  1921.  
  1922. void inserttext(const char *text)
  1923. {
  1924.   membuf_append(&src, text, strlen(text));
  1925. }
  1926.  
  1927. void insertdefine(const char *name, int value)
  1928. {
  1929.   char insertbuffer[80];
  1930.  
  1931.   sprintf(insertbuffer, "%-16s = %d\n", name, value);
  1932.   inserttext(insertbuffer);
  1933. }
  1934.  
  1935. void insertlabel(const char *name)
  1936. {
  1937.   char insertbuffer[80];
  1938.  
  1939.   sprintf(insertbuffer, "%s:\n", name);
  1940.   inserttext(insertbuffer);
  1941. }
  1942.  
  1943. void insertbytes(const unsigned char *bytes, int size)
  1944. {
  1945.   char insertbuffer[80];
  1946.   int row = 0;
  1947.  
  1948.   while (size--)
  1949.   {
  1950.     if (!row)
  1951.     {
  1952.       inserttext("                .BYTE (");
  1953.       sprintf(insertbuffer, "$%02x", *bytes);
  1954.       inserttext(insertbuffer);
  1955.       bytes++;
  1956.       row++;
  1957.     }
  1958.     else
  1959.     {
  1960.       sprintf(insertbuffer, ",$%02x", *bytes);
  1961.       inserttext(insertbuffer);
  1962.       bytes++;
  1963.       row++;
  1964.       if (row == MAX_BYTES_PER_ROW)
  1965.       {
  1966.         inserttext(")\n");
  1967.         row = 0;
  1968.       }
  1969.     }
  1970.   }
  1971.   if (row) inserttext(")\n");
  1972. }
  1973.  
  1974. void insertbyte(unsigned char byte)
  1975. {
  1976.   char insertbuffer[80];
  1977.  
  1978.   sprintf(insertbuffer, "                .BYTE ($%02x)\n", byte);
  1979.   inserttext(insertbuffer);
  1980. }
  1981.  
  1982. void insertaddrlo(const char *name)
  1983. {
  1984.   char insertbuffer[80];
  1985.  
  1986.   sprintf(insertbuffer, "                .BYTE (%s %% 256)\n", name);
  1987.   inserttext(insertbuffer);
  1988. }
  1989.  
  1990. void insertaddrhi(const char *name)
  1991. {
  1992.   char insertbuffer[80];
  1993.  
  1994.   sprintf(insertbuffer, "                .BYTE (%s / 256)\n", name);
  1995.   inserttext(insertbuffer);
  1996. }
  1997.  
  1998. void findtableduplicates(int num)
  1999. {
  2000.   int c,d,e;
  2001.  
  2002.   if (num == STBL)
  2003.   {
  2004.     for (c = 1; c <= MAX_TABLELEN; c++)
  2005.     {
  2006.       if (tableused[num][c])
  2007.       {
  2008.         for (d = c+1; d <= MAX_TABLELEN; d++)
  2009.         {
  2010.           if (tableused[num][d])
  2011.           {
  2012.             if ((ltable[num][d-1] == ltable[num][c-1]) && (rtable[num][d-1] == rtable[num][c-1]))
  2013.             {
  2014.               // Duplicate found, remove and map to the original
  2015.               tableused[num][d] = 0;
  2016.               for (e = d; e <= MAX_TABLELEN; e++)
  2017.                 if (tableused[num][e]) tablemap[num][e]--;
  2018.               tablemap[num][d] = tablemap[num][c];
  2019.             }
  2020.           }
  2021.         }
  2022.       }
  2023.     }
  2024.   }
  2025.   else
  2026.   {
  2027.     for (c = 1; c <= MAX_TABLELEN; c++)
  2028.     {
  2029.       if (isusedandselfcontained(num, c))
  2030.       {
  2031.         for (d = c + gettablepartlen(num, c - 1); d <= MAX_TABLELEN; )
  2032.         {
  2033.           int len = gettablepartlen(num, d - 1);
  2034.  
  2035.           if (isusedandselfcontained(num, d))
  2036.           {
  2037.             for (e = 0; e < len; e++)
  2038.             {
  2039.               if (e < len-1)
  2040.               {
  2041.                 // Is table data the same?
  2042.                 if ((ltable[num][d+e-1] != ltable[num][c+e-1]) || (rtable[num][d+e-1] != rtable[num][c+e-1]))
  2043.                   break;
  2044.               }
  2045.               else
  2046.               {
  2047.                 // Do both parts have a jump in the end?
  2048.                 if (ltable[num][d+e-1] != ltable[num][c+e-1])
  2049.                   break;
  2050.                 // Do both parts end?
  2051.                 if (rtable[num][d+e-1] == 0)
  2052.                 {
  2053.                   if (rtable[num][c+e-1] != 0)
  2054.                     break;
  2055.                 }
  2056.                 else
  2057.                 {
  2058.                   // Do both parts loop in the same way?
  2059.                   if ((rtable[num][d+e-1] - d) != (rtable[num][c+e-1] - c))
  2060.                     break;
  2061.                 }
  2062.               }
  2063.             }
  2064.             if (e == len)
  2065.             {
  2066.               // Duplicate found, remove and map to the original
  2067.               for (e = 0; e < len; e++)
  2068.                 tableused[num][d+e] = 0;
  2069.               for (e = d; e < MAX_TABLELEN; e++)
  2070.                 if (tableused[num][e]) tablemap[num][e] -= len;
  2071.               for (e = 0; e < len; e++)
  2072.                 tablemap[num][d+e] = tablemap[num][c+e];
  2073.             }
  2074.           }
  2075.           d += len;
  2076.         }
  2077.       }
  2078.     }
  2079.   }
  2080. }
  2081.  
  2082. int isusedandselfcontained(int num, int start)
  2083. {
  2084.   int len = gettablepartlen(num, start - 1);
  2085.   int end = start + len - 1;
  2086.   int c;
  2087.  
  2088.   // Don't use jumps only
  2089.   if (len == 1) return 0;
  2090.  
  2091.   // Check that whole table is used
  2092.   for (c = start; c <= end; c++)
  2093.   {
  2094.     if (tableused[num][c] == 0) return 0;
  2095.   }
  2096.   // Check for jump to outside
  2097.   if (rtable[num][end-1] != 0)
  2098.   {
  2099.     if ((rtable[num][end-1] < start) || (rtable[num][end-1] > end)) return 0;
  2100.   }
  2101.   // Check for jump from outside
  2102.   for (c = 1; c < start; c++)
  2103.     if ((tableused[num][c]) && (ltable[num][c-1] == 0xff) && (rtable[num][c-1] >= start) && (rtable[num][c-1] <= end)) return 0;
  2104.   for (c = end+1; c <= MAX_TABLELEN; c++)
  2105.     if ((tableused[num][c]) && (ltable[num][c-1] == 0xff) && (rtable[num][c-1] >= start) && (rtable[num][c-1] <= end)) return 0;
  2106.  
  2107.   // OK!
  2108.   return 1;
  2109. }
  2110.  
  2111. void calcspeedtest(unsigned char pos)
  2112. {
  2113.   if (!pos) 
  2114.   {
  2115.     nozerospeed = 0;
  2116.     return;
  2117.   }
  2118.  
  2119.   if (ltable[STBL][pos-1] >= 0x80) nocalculatedspeed = 0;
  2120.   else nonormalspeed = 0;
  2121. }
  2122.  
  2123.  
  2124.