home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.68.zip / src / gsid.cpp < prev    next >
C/C++ Source or Header  |  2009-01-03  |  4KB  |  168 lines

  1. /*
  2.  * GOATTRACKER reSID interface
  3.  */
  4.  
  5. #define GSID_C
  6.  
  7. #include <stdlib.h>
  8. #include "resid/sid.h"
  9. #include "resid-fp/sid.h"
  10.  
  11. extern "C" {
  12.  
  13. #include "gsid.h"
  14. #include "gsound.h"
  15.  
  16. int clockrate;
  17. int samplerate;
  18. unsigned char sidreg[NUMSIDREGS];
  19. unsigned char sidorder[] =
  20.   {0x15,0x16,0x18,0x17,
  21.    0x02,0x03,0x06,0x05,0x00,0x01,0x04,
  22.    0x09,0x0a,0x0d,0x0c,0x07,0x08,0x0b,
  23.    0x10,0x11,0x14,0x13,0x0e,0x0f,0x12};
  24.  
  25. unsigned char altsidorder[] =
  26.   {0x15,0x16,0x18,0x17,
  27.    0x04,0x00,0x01,0x02,0x03,0x05,0x06,
  28.    0x0b,0x07,0x08,0x09,0x0a,0x0c,0x0d,
  29.    0x12,0x0e,0x0f,0x10,0x11,0x13,0x14};
  30.  
  31. SID *sid = 0;
  32. SIDFP *sidfp = 0;
  33.  
  34. extern unsigned residdelay;
  35. extern unsigned adparam;
  36.  
  37. void sid_init(int speed, unsigned m, unsigned ntsc, unsigned interpolate, unsigned customclockrate, unsigned usefp)
  38. {
  39.   int c;
  40.  
  41.   if (ntsc) clockrate = NTSCCLOCKRATE;
  42.     else clockrate = PALCLOCKRATE;
  43.  
  44.   if (customclockrate)
  45.     clockrate = customclockrate;
  46.  
  47.   samplerate = speed;
  48.  
  49.   if (!usefp)
  50.   {
  51.     if (sidfp)
  52.     {
  53.       delete sidfp;
  54.       sidfp = NULL;
  55.     }
  56.  
  57.     if (!sid) sid = new SID;
  58.   }
  59.   else
  60.   {
  61.     if (sid)
  62.     {
  63.       delete sid;
  64.       sid = NULL;
  65.     }
  66.     
  67.     if (!sidfp) sidfp = new SIDFP;
  68.   }
  69.  
  70.   switch(interpolate)
  71.   {
  72.     case 0:
  73.     if (sid) sid->set_sampling_parameters(clockrate, SAMPLE_FAST, speed, 20000);
  74.     if (sidfp) sidfp->set_sampling_parameters(clockrate, SAMPLE_FAST, speed, 20000);
  75.     break;
  76.  
  77.     default:
  78.     if (sid) sid->set_sampling_parameters(clockrate, SAMPLE_INTERPOLATE, speed, 20000);
  79.     if (sidfp) sidfp->set_sampling_parameters(clockrate, SAMPLE_INTERPOLATE, speed, 20000);
  80.     break;
  81.   }
  82.  
  83.   if (sid) sid->reset();
  84.   if (sidfp) sidfp->reset();
  85.   for (c = 0; c < NUMSIDREGS; c++)
  86.   {
  87.     sidreg[c] = 0x00;
  88.   }
  89.   if (m == 1)
  90.   {
  91.     if (sid) sid->set_chip_model(MOS8580);
  92.     if (sidfp) sidfp->set_chip_model(MOS8580FP);
  93.   }
  94.   else
  95.   {
  96.     if (sid) sid->set_chip_model(MOS6581);
  97.     if (sidfp) sidfp->set_chip_model(MOS6581FP);
  98.   }
  99. }
  100.  
  101. unsigned char sid_getorder(unsigned char index)
  102. {
  103.   if (adparam >= 0xf000)
  104.     return altsidorder[index];
  105.   else
  106.     return sidorder[index];
  107. }
  108.  
  109. int sid_fillbuffer(short *ptr, int samples)
  110. {
  111.   int tdelta;
  112.   int tdelta2;
  113.   int result = 0;
  114.   int total = 0;
  115.   int c;
  116.  
  117.   int badline = rand() % NUMSIDREGS;
  118.  
  119.   tdelta = clockrate * samples / samplerate + 4;
  120.  
  121.   for (c = 0; c < NUMSIDREGS; c++)
  122.   {
  123.     unsigned char o = sid_getorder(c);
  124.  
  125.       // Extra delay for loading the waveform (and mt_chngate,x)
  126.       if ((o == 4) || (o == 11) || (o == 18))
  127.       {
  128.         tdelta2 = SIDWAVEDELAY;
  129.       if (sid) result = sid->clock(tdelta2, ptr, samples);
  130.       if (sidfp) result = sidfp->clock(tdelta2, ptr, samples);
  131.       total += result;
  132.       ptr += result;
  133.       samples -= result;
  134.       tdelta -= SIDWAVEDELAY;
  135.     }
  136.  
  137.     // Possible random badline delay once per writing
  138.     if ((badline == c) && (residdelay))
  139.       {
  140.       tdelta2 = residdelay;
  141.       if (sid) result = sid->clock(tdelta2, ptr, samples);
  142.       if (sidfp) result = sidfp->clock(tdelta2, ptr, samples);
  143.       total += result;
  144.       ptr += result;
  145.       samples -= result;
  146.       tdelta -= residdelay;
  147.     }
  148.  
  149.     if (sid) sid->write(o, sidreg[o]);
  150.     if (sidfp) sidfp->write(o, sidreg[o]);
  151.  
  152.     tdelta2 = SIDWRITEDELAY;
  153.     if (sid) result = sid->clock(tdelta2, ptr, samples);
  154.     if (sidfp) result = sidfp->clock(tdelta2, ptr, samples);
  155.     total += result;
  156.     ptr += result;
  157.     samples -= result;
  158.     tdelta -= SIDWRITEDELAY;
  159.   }
  160.   if (sid) result = sid->clock(tdelta, ptr, samples);
  161.   if (sidfp) result = sidfp->clock(tdelta, ptr, samples);
  162.   total += result;
  163.  
  164.   return total;
  165. }
  166.  
  167. }
  168.