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