sn76496.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /***************************************************************************
  2. sn76496.c
  3. Routines to emulate the Texas Instruments SN76489 / SN76496 programmable
  4. tone /noise generator. Also known as (or at least compatible with) TMS9919.
  5. Noise emulation is not accurate due to lack of documentation. The noise
  6. generator uses a shift register with a XOR-feedback network, but the exact
  7. layout is unknown. It can be set for either period or white noise; again,
  8. the details are unknown.
  9. 28/03/2005 : Sebastien Chevalier
  10. Update th SN76496Write func, according to SN76489 doc found on SMSPower.
  11. - On write with 0x80 set to 0, when LastRegister is other then TONE,
  12. the function is similar than update with 0x80 set to 1
  13. ***************************************************************************/
  14. #ifndef __GNUC__
  15. #pragma warning (disable:4244)
  16. #endif
  17. #include "sn76496.h"
  18. #define MAX_OUTPUT 0x4800 // was 0x7fff
  19. #define STEP 0x10000
  20. /* Formulas for noise generator */
  21. /* bit0 = output */
  22. /* noise feedback for white noise mode (verified on real SN76489 by John Kortink) */
  23. #define FB_WNOISE_T 0x3000 /* (15bits) bit15 = bit1 ^ bit2, TI */
  24. #define FB_WNOISE_S 0x9000 /* (16bits) bit16 = bit0 ^ bit3, Sega PSG */
  25. /* noise feedback for periodic noise mode */
  26. #define FB_PNOISE_T 0x4000 /* 15bit rotate for TI */
  27. #define FB_PNOISE_S 0x8000 /* 16bit rotate for Sega PSG */
  28. #define FB_WNOISE FB_WNOISE_S /* Sega */
  29. #define FB_PNOISE FB_PNOISE_S
  30. struct SN76496
  31. {
  32. //sound_stream * Channel;
  33. int SampleRate;
  34. unsigned int UpdateStep;
  35. int VolTable[16]; /* volume table */
  36. int Register[8]; /* registers */
  37. int LastRegister; /* last register written */
  38. int Volume[4]; /* volume of voice 0-2 and noise */
  39. unsigned int RNG; /* noise generator */
  40. int NoiseFB; /* noise feedback mask */
  41. int Period[4];
  42. int Count[4];
  43. int Output[4];
  44. int pad[1];
  45. };
  46. static struct SN76496 ono_sn; // one and only SN76496
  47. int *sn76496_regs;
  48. //static
  49. void SN76496Write(int data)
  50. {
  51. struct SN76496 *R = &ono_sn;
  52. int n, r, c;
  53. /* update the output buffer before changing the registers */
  54. //stream_update(R->Channel,0);
  55. r = R->LastRegister;
  56. if (data & 0x80)
  57. r = R->LastRegister = (data & 0x70) >> 4;
  58. c = r / 2;
  59. if (!(data & 0x80) && (r == 0 || r == 2 || r == 4))
  60. // data byte (tone only)
  61. R->Register[r] = (R->Register[r] & 0x0f) | ((data & 0x3f) << 4);
  62. else
  63. R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f);
  64. data = R->Register[r];
  65. switch (r)
  66. {
  67. case 0: /* tone 0 : frequency */
  68. case 2: /* tone 1 : frequency */
  69. case 4: /* tone 2 : frequency */
  70. R->Period[c] = R->UpdateStep * data;
  71. if (R->Period[c] == 0) R->Period[c] = R->UpdateStep;
  72. if (R->Count[c] > R->Period[c]) R->Count[c] = R->Period[c];
  73. if (r == 4)
  74. {
  75. /* update noise shift frequency */
  76. if ((R->Register[6] & 0x03) == 0x03)
  77. R->Period[3] = 2 * R->Period[2];
  78. }
  79. break;
  80. case 1: /* tone 0 : volume */
  81. case 3: /* tone 1 : volume */
  82. case 5: /* tone 2 : volume */
  83. case 7: /* noise : volume */
  84. R->Volume[c] = R->VolTable[data & 0x0f];
  85. break;
  86. case 6: /* noise : frequency, mode */
  87. n = data;
  88. R->NoiseFB = (n & 4) ? FB_WNOISE : FB_PNOISE;
  89. n &= 3;
  90. /* N/512,N/1024,N/2048,Tone #3 output */
  91. R->Period[3] = 2 * (n == 3 ? R->Period[2] : R->UpdateStep << (4 + n));
  92. /* reset noise shifter */
  93. R->RNG = FB_PNOISE;
  94. R->Output[3] = R->RNG & 1;
  95. break;
  96. }
  97. }
  98. /*
  99. WRITE8_HANDLER( SN76496_0_w ) { SN76496Write(0,data); }
  100. WRITE8_HANDLER( SN76496_1_w ) { SN76496Write(1,data); }
  101. WRITE8_HANDLER( SN76496_2_w ) { SN76496Write(2,data); }
  102. WRITE8_HANDLER( SN76496_3_w ) { SN76496Write(3,data); }
  103. WRITE8_HANDLER( SN76496_4_w ) { SN76496Write(4,data); }
  104. */
  105. //static
  106. void SN76496Update(short *buffer, int length, int stereo)
  107. {
  108. int i;
  109. struct SN76496 *R = &ono_sn;
  110. while (length > 0)
  111. {
  112. int vol[4];
  113. unsigned int out;
  114. int left;
  115. /* vol[] keeps track of how long each square wave stays */
  116. /* in the 1 position during the sample period. */
  117. vol[0] = vol[1] = vol[2] = vol[3] = 0;
  118. for (i = 0;i < 3;i++)
  119. {
  120. if (R->Output[i]) vol[i] += R->Count[i];
  121. R->Count[i] -= STEP;
  122. /* Period[i] is the half period of the square wave. Here, in each */
  123. /* loop I add Period[i] twice, so that at the end of the loop the */
  124. /* square wave is in the same status (0 or 1) it was at the start. */
  125. /* vol[i] is also incremented by Period[i], since the wave has been 1 */
  126. /* exactly half of the time, regardless of the initial position. */
  127. /* If we exit the loop in the middle, Output[i] has to be inverted */
  128. /* and vol[i] incremented only if the exit status of the square */
  129. /* wave is 1. */
  130. if (R->Count[i] < -2*R->Period[i] || R->Volume[i] == 0) {
  131. /* Cut off anything above the Nyquist frequency. */
  132. /* It will only create aliasing anyway. This is actually an */
  133. /* ideal lowpass filter with Nyquist corner frequency. */
  134. vol[i] += STEP/2; // mean value
  135. R->Count[i] = R->Output[i] = 0;
  136. }
  137. while (R->Count[i] < 0)
  138. {
  139. R->Count[i] += R->Period[i];
  140. if (R->Count[i] >= 0)
  141. {
  142. R->Output[i] ^= 1;
  143. if (R->Output[i]) vol[i] += R->Period[i];
  144. break;
  145. }
  146. R->Count[i] += R->Period[i];
  147. vol[i] += R->Period[i];
  148. }
  149. if (R->Output[i]) vol[i] -= R->Count[i];
  150. }
  151. left = STEP;
  152. if (R->Output[3]) vol[3] += R->Count[3];
  153. do
  154. {
  155. int nextevent;
  156. if (R->Count[3] < left) nextevent = R->Count[3];
  157. else nextevent = left;
  158. R->Count[3] -= nextevent;
  159. if (R->Count[3] <= 0)
  160. {
  161. R->Output[3] = R->RNG & 1;
  162. R->RNG >>= 1;
  163. if (R->Output[3])
  164. {
  165. R->RNG ^= R->NoiseFB;
  166. vol[3] += R->Period[3];
  167. }
  168. R->Count[3] += R->Period[3];
  169. }
  170. left -= nextevent;
  171. } while (left > 0 && R->Volume[3]);
  172. if (R->Output[3]) vol[3] -= R->Count[3];
  173. out = vol[0] * R->Volume[0] + vol[1] * R->Volume[1] +
  174. vol[2] * R->Volume[2] + vol[3] * R->Volume[3];
  175. if (out > MAX_OUTPUT * STEP) out = MAX_OUTPUT * STEP;
  176. if ((out /= STEP)) // will be optimized to shift; max 0x4800 = 18432
  177. *buffer += out;
  178. if(stereo) buffer+=2; // only left for stereo, to be mixed to right later
  179. else buffer++;
  180. length--;
  181. }
  182. }
  183. static void SN76496_set_clock(struct SN76496 *R,int clock)
  184. {
  185. /* the base clock for the tone generators is the chip clock divided by 16; */
  186. /* for the noise generator, it is clock / 256. */
  187. /* Here we calculate the number of steps which happen during one sample */
  188. /* at the given sample rate. No. of events = sample rate / (clock/16). */
  189. /* STEP is a multiplier used to turn the fraction into a fixed point */
  190. /* number. */
  191. R->UpdateStep = ((double)STEP * R->SampleRate * 16) / clock;
  192. }
  193. static void SN76496_set_gain(struct SN76496 *R,int gain)
  194. {
  195. int i;
  196. double out;
  197. gain &= 0xff;
  198. /* increase max output basing on gain (0.2 dB per step) */
  199. out = MAX_OUTPUT / 4.0;
  200. while (gain-- > 0)
  201. out *= 1.023292992; /* = (10 ^ (0.2/20)) */
  202. /* build volume table (2dB per step) */
  203. for (i = 0;i < 15;i++)
  204. {
  205. /* limit volume to avoid clipping */
  206. if (out > MAX_OUTPUT / 4) R->VolTable[i] = MAX_OUTPUT / 4;
  207. else R->VolTable[i] = out;
  208. out /= 1.258925412; /* = 10 ^ (2/20) = 2dB */
  209. }
  210. R->VolTable[15] = 0;
  211. }
  212. //static
  213. int SN76496_init(int clock,int sample_rate)
  214. {
  215. struct SN76496 *R = &ono_sn;
  216. int i;
  217. //R->Channel = stream_create(0,1, sample_rate,R,SN76496Update);
  218. sn76496_regs = R->Register;
  219. R->SampleRate = sample_rate;
  220. SN76496_set_clock(R,clock);
  221. for (i = 0;i < 4;i++) R->Volume[i] = 0;
  222. R->LastRegister = 0;
  223. for (i = 0;i < 8;i+=2)
  224. {
  225. R->Register[i] = 0;
  226. R->Register[i + 1] = 0x0f; /* volume = 0 */
  227. }
  228. for (i = 0;i < 4;i++)
  229. {
  230. R->Volume[i] = R->Output[i] = R->Count[i] = 0;
  231. R->Period[i] = R->UpdateStep;
  232. }
  233. R->RNG = FB_PNOISE;
  234. R->Output[3] = R->RNG & 1;
  235. // added
  236. SN76496_set_gain(R, 0);
  237. return 0;
  238. }