Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.89 KB | None | 0 0
  1. #include <math.h>
  2. #include "burnint.h"
  3. #include "msm6295.h"
  4. //#include "burn_sound.h"
  5.  
  6. #define true 1
  7. #define false 0
  8.  
  9. typedef char bool;
  10.  
  11. unsigned char* MSM6295ROM;
  12. unsigned char* MSM6295SampleInfo[MAX_MSM6295][4];
  13. unsigned char* MSM6295SampleData[MAX_MSM6295][4];
  14.  
  15. unsigned int nMSM6295Status[MAX_MSM6295];
  16.  
  17. typedef struct {
  18. int nOutput;
  19. int nVolume;
  20. int nPosition;
  21. int nSampleCount;
  22. int nSample;
  23. int nStep;
  24. int nDelta;
  25.  
  26. int nBufPos;
  27. }MSM6295ChannelInfo;
  28.  
  29. static struct {
  30. int nVolume;
  31. int nSampleRate;
  32. int nSampleSize;
  33. int nFractionalPosition;
  34.  
  35. // All current settings for each channel
  36. MSM6295ChannelInfo ChannelInfo[4];
  37.  
  38. // Used for sending commands
  39. bool bIsCommand;
  40. int nSampleInfo;
  41.  
  42. } MSM6295[MAX_MSM6295];
  43.  
  44. static unsigned int MSM6295VolumeTable[16];
  45. static int MSM6295DeltaTable[49 * 16];
  46. static int MSM6295StepShift[8] = {-1, -1, -1, -1, 2, 4, 6, 8};
  47.  
  48. static int* MSM6295ChannelData[MAX_MSM6295][4];
  49.  
  50. static int* pBuffer = NULL;
  51. static int nLastChip;
  52.  
  53. static bool bAdd;
  54.  
  55. void MSM6295Reset(int nChip)
  56. {
  57. nMSM6295Status[nChip] = 0;
  58. MSM6295[nChip].bIsCommand = false;
  59.  
  60. MSM6295[nChip].nFractionalPosition = 0;
  61. unsigned int nChannel;
  62. for (nChannel = 0; nChannel < 4; nChannel++) {
  63. // Set initial bank information
  64. MSM6295SampleInfo[nChip][nChannel] = MSM6295ROM + (nChip * 0x0100000) + (nChannel << 8);
  65. MSM6295SampleData[nChip][nChannel] = MSM6295ROM + (nChip * 0x0100000) + (nChannel << 16);
  66.  
  67. memset(MSM6295ChannelData[nChip][nChannel], 0, 0x1000 * sizeof(int));
  68. MSM6295[nChip].ChannelInfo[nChannel].nBufPos = 4;
  69. }
  70. }
  71.  
  72. int MSM6295Scan(int nChip, int nAction)
  73. {
  74. int nSampleSize = MSM6295[nChip].nSampleSize;
  75. SCAN_VAR(MSM6295[nChip]);
  76. MSM6295[nChip].nSampleSize = nSampleSize;
  77.  
  78. SCAN_VAR(nMSM6295Status[nChip]);
  79. unsigned int i;
  80. for (i = 0; i < 4; i++) {
  81. MSM6295SampleInfo[nChip][i] -= (unsigned int)MSM6295ROM;
  82. SCAN_VAR(MSM6295SampleInfo[nChip][i]);
  83. MSM6295SampleInfo[nChip][i] += (unsigned int)MSM6295ROM;
  84.  
  85. MSM6295SampleData[nChip][i] -= (unsigned int)MSM6295ROM;
  86. SCAN_VAR(MSM6295SampleData[nChip][i]);
  87. MSM6295SampleData[nChip][i] += (unsigned int)MSM6295ROM;
  88. }
  89.  
  90. return 0;
  91. }
  92.  
  93. void MSM6295Render_Linear(int nChip, int* pBuf, int nSegmentLength)
  94. {
  95. static int nPreviousSample[MAX_MSM6295], nCurrentSample[MAX_MSM6295];
  96. int nVolume = MSM6295[nChip].nVolume;
  97. int nFractionalPosition = MSM6295[nChip].nFractionalPosition;
  98.  
  99. int nChannel, nDelta, nSample;
  100. MSM6295ChannelInfo* pChannelInfo;
  101.  
  102. while (nSegmentLength--)
  103. {
  104. if (nFractionalPosition >= 0x1000) {
  105.  
  106. nPreviousSample[nChip] = nCurrentSample[nChip];
  107.  
  108. do {
  109. nCurrentSample[nChip] = 0;
  110.  
  111. for (nChannel = 0; nChannel < 4; nChannel++) {
  112. if (nMSM6295Status[nChip] & (1 << nChannel)) {
  113. pChannelInfo = &MSM6295[nChip].ChannelInfo[nChannel];
  114.  
  115. // Check for end of sample
  116. if (pChannelInfo->nSampleCount-- == 0) {
  117. nMSM6295Status[nChip] &= ~(1 << nChannel);
  118. continue;
  119. }
  120.  
  121. // Get new delta from ROM
  122. if (pChannelInfo->nPosition & 1) {
  123. nDelta = pChannelInfo->nDelta & 0x0F;
  124. } else {
  125. pChannelInfo->nDelta = MSM6295SampleData[nChip][(pChannelInfo->nPosition >> 17) & 3][(pChannelInfo->nPosition >> 1) & 0xFFFF];
  126. nDelta = pChannelInfo->nDelta >> 4;
  127. }
  128.  
  129. // Compute new sample
  130. nSample = pChannelInfo->nSample + MSM6295DeltaTable[(pChannelInfo->nStep << 4) + nDelta];
  131.  
  132. if (nSample > 2047) {
  133. nSample = 2047;
  134. } else {
  135. if (nSample < -2048) {
  136. nSample = -2048;
  137. }
  138. }
  139. pChannelInfo->nSample = nSample;
  140. pChannelInfo->nOutput = (nSample * pChannelInfo->nVolume);
  141.  
  142. // Update step value
  143. pChannelInfo->nStep = pChannelInfo->nStep + MSM6295StepShift[nDelta & 7];
  144. if (pChannelInfo->nStep > 48) {
  145. pChannelInfo->nStep = 48;
  146. } else {
  147. if (pChannelInfo->nStep < 0) {
  148. pChannelInfo->nStep = 0;
  149. }
  150. }
  151.  
  152. nCurrentSample[nChip] += pChannelInfo->nOutput / 16;
  153.  
  154. // Advance sample position
  155. pChannelInfo->nPosition++;
  156. }
  157. }
  158.  
  159. nFractionalPosition -= 0x1000;
  160.  
  161. } while (nFractionalPosition >= 0x1000);
  162. }
  163.  
  164. // Compute linearly interpolated sample
  165. nSample = nPreviousSample[nChip] + (((nCurrentSample[nChip] - nPreviousSample[nChip]) * nFractionalPosition) >> 12);
  166.  
  167.  
  168.  
  169. // Scale all 4 channels
  170. nSample *= nVolume;
  171. *pBuf++ += nSample;
  172. nFractionalPosition += MSM6295[nChip].nSampleSize;
  173. }
  174.  
  175. MSM6295[nChip].nFractionalPosition = nFractionalPosition;
  176. }
  177.  
  178. int MSM6295Render(int nChip, short* pSoundBuf, int nSegmentLength)
  179. {
  180. if (nChip == 0) {
  181. memset(pBuffer, 0, nSegmentLength * sizeof(int));
  182. }
  183.  
  184. if (0)//nInterpolation >= 3)
  185. {
  186. MSM6295Render_Cubic(nChip, pBuffer, nSegmentLength);
  187. } else {
  188. MSM6295Render_Linear(nChip, pBuffer, nSegmentLength);
  189. }
  190.  
  191. if (nChip == nLastChip) {
  192. /* if (bBurnUseMMX) {
  193. if (bAdd) {
  194. // BurnSoundCopyClamp_Mono_Add_A(pBuffer, pSoundBuf, nSegmentLength);
  195. } else {
  196. // BurnSoundCopyClamp_Mono_A(pBuffer, pSoundBuf, nSegmentLength);
  197. }
  198. } else
  199. */
  200. {
  201. // VBt à remettre
  202.  
  203. if (bAdd) {
  204. BurnSoundCopyClamp_Mono_Add_C(pBuffer, pSoundBuf, nSegmentLength);
  205. } else {
  206. BurnSoundCopyClamp_Mono_C(pBuffer, pSoundBuf, nSegmentLength);
  207. }
  208. }
  209. }
  210.  
  211. return 0;
  212. }
  213.  
  214.  
  215.  
  216. int MSM6295RenderVBT(int nChip, short* pSoundBuf, int nSegmentLength)
  217. {
  218. // if (nChip == 0) {
  219. memset(pBuffer, 0, nSegmentLength * sizeof(int));
  220. // }
  221.  
  222. MSM6295Render_Linear(nChip, pBuffer, nSegmentLength);
  223. BurnSoundCopyClamp_Mono_C(pBuffer, pSoundBuf, nSegmentLength);
  224.  
  225. return 0;
  226. }
  227.  
  228. void MSM6295Command(int nChip, unsigned char nCommand)
  229. {
  230. if (MSM6295[nChip].bIsCommand) {
  231. // Process second half of command
  232. int nChannel, nSampleStart, nSampleCount;
  233. int nVolume = nCommand & 0x0F;
  234. nCommand >>= 4;
  235.  
  236. MSM6295[nChip].bIsCommand = false;
  237.  
  238. for (nChannel = 0; nChannel < 4; nChannel++) {
  239. if (nCommand & (0x01 << nChannel)) {
  240. int nBank = (MSM6295[nChip].nSampleInfo & 0x0300) >> 8;
  241. MSM6295[nChip].nSampleInfo &= 0xFF;
  242.  
  243. nSampleStart = MSM6295SampleInfo[nChip][nBank][MSM6295[nChip].nSampleInfo + 0];
  244. nSampleStart <<= 8;
  245. nSampleStart |= MSM6295SampleInfo[nChip][nBank][MSM6295[nChip].nSampleInfo + 1];
  246. nSampleStart <<= 8;
  247. nSampleStart |= MSM6295SampleInfo[nChip][nBank][MSM6295[nChip].nSampleInfo + 2];
  248. nSampleStart <<= 1;
  249.  
  250. nSampleCount = MSM6295SampleInfo[nChip][nBank][MSM6295[nChip].nSampleInfo + 3];
  251. nSampleCount <<= 8;
  252. nSampleCount |= MSM6295SampleInfo[nChip][nBank][MSM6295[nChip].nSampleInfo + 4];
  253. nSampleCount <<= 8;
  254. nSampleCount |= MSM6295SampleInfo[nChip][nBank][MSM6295[nChip].nSampleInfo + 5];
  255. nSampleCount <<= 1;
  256.  
  257. if (nSampleCount < 0x80000) {
  258. nSampleCount -= nSampleStart;
  259.  
  260. // Start playing channel
  261. MSM6295[nChip].ChannelInfo[nChannel].nVolume = MSM6295VolumeTable[nVolume];
  262. MSM6295[nChip].ChannelInfo[nChannel].nPosition = nSampleStart;
  263. MSM6295[nChip].ChannelInfo[nChannel].nSampleCount = nSampleCount;
  264. MSM6295[nChip].ChannelInfo[nChannel].nStep = 0;
  265. MSM6295[nChip].ChannelInfo[nChannel].nSample = -1;
  266.  
  267. MSM6295[nChip].ChannelInfo[nChannel].nOutput = 0;
  268.  
  269. nMSM6295Status[nChip] |= nCommand;
  270.  
  271. /* if (nInterpolation >= 3) {
  272. MSM6295ChannelData[nChip][nChannel][0] = 0;
  273. MSM6295ChannelData[nChip][nChannel][1] = 0;
  274. MSM6295ChannelData[nChip][nChannel][2] = 0;
  275. MSM6295ChannelData[nChip][nChannel][3] = 0;
  276. MSM6295[nChip].ChannelInfo[nChannel].nBufPos = 4;
  277. }
  278. */
  279. }
  280. }
  281. }
  282.  
  283. } else {
  284. // Process command
  285. if (nCommand & 0x80) {
  286. MSM6295[nChip].nSampleInfo = (nCommand & 0x7F) << 3;
  287. MSM6295[nChip].bIsCommand = true;
  288. } else {
  289. // Stop playing samples
  290. nCommand >>= 3;
  291. nMSM6295Status[nChip] &= ~nCommand;
  292. }
  293. }
  294. }
  295.  
  296. void MSM6295Exit(int nChip)
  297. {
  298. free(pBuffer);
  299. pBuffer = NULL;
  300. unsigned int nChannel;
  301. for (nChannel = 0; nChannel < 4; nChannel++) {
  302. free(MSM6295ChannelData[nChip][nChannel]);
  303. MSM6295ChannelData[nChip][nChannel] = NULL;
  304. }
  305. }
  306.  
  307. int MSM6295Init(int nChip, int nSamplerate, float fMaxVolume, bool bAddSignal)
  308. {
  309. if (nBurnSoundRate > 0) {
  310. if (pBuffer == NULL) {
  311. pBuffer = (int*)malloc(nBurnSoundRate * sizeof(int));
  312. }
  313. }
  314.  
  315. bAdd = bAddSignal;
  316.  
  317. // Convert volume from percentage
  318. MSM6295[nChip].nVolume = (int)(fMaxVolume * 256.0 / 100.0 + 0.5);
  319.  
  320. MSM6295[nChip].nSampleRate = nSamplerate;
  321. if (nBurnSoundRate > 0) {
  322. MSM6295[nChip].nSampleSize = (nSamplerate << 12) / nBurnSoundRate;
  323. } else {
  324. MSM6295[nChip].nSampleSize = (nSamplerate << 12) / 11025;
  325. }
  326.  
  327. MSM6295[nChip].nFractionalPosition = 0;
  328.  
  329. nMSM6295Status[nChip] = 0;
  330. MSM6295[nChip].bIsCommand = false;
  331.  
  332. if (nChip == 0) {
  333. nLastChip = 0;
  334. } else {
  335. if (nLastChip < nChip) {
  336. nLastChip = nChip;
  337. }
  338. }
  339. unsigned int i,n;
  340. // Compute sample deltas
  341. // vbt correct
  342. for (i = 0; i < 49; i++) {
  343. int nStep = (int)(pow(1.1, (double)i) * 16.0);
  344. for (n = 0; n < 16; n++) {
  345. int nDelta = nStep >> 3;
  346. if (n & 1) {
  347. nDelta += nStep >> 2;
  348. }
  349. if (n & 2) {
  350. nDelta += nStep >> 1;
  351. }
  352. if (n & 4) {
  353. nDelta += nStep;
  354. }
  355. if (n & 8) {
  356. nDelta = -nDelta;
  357. }
  358. MSM6295DeltaTable[(i << 4) + n] = nDelta;
  359. }
  360.  
  361. }
  362.  
  363. // Compute volume levels
  364. // vbt correct
  365. for (i = 0; i < 16; i++) {
  366. double nVolume = 256.0;
  367. for (n = i; n > 0; n--) {
  368. nVolume /= 1.412537545;
  369. }
  370. MSM6295VolumeTable[i] = (unsigned int)(nVolume + 0.5);
  371. }
  372. unsigned int nChannel;
  373. for (nChannel = 0; nChannel < 4; nChannel++) {
  374. MSM6295ChannelData[nChip][nChannel] = (int*)malloc(0x1000 * sizeof(int));
  375. }
  376.  
  377. MSM6295Reset(nChip);
  378.  
  379. return 0;
  380. }
  381.  
  382. //------
  383.  
  384. void BurnSoundCopyClamp_Mono_C(int *Src, short *Dest, int Len)
  385. {
  386. while (Len--) {
  387. Dest[0] = CLIP((*Src >> 8));
  388. // Dest[1] = CLIP((*Src >> 8));
  389.  
  390. Src++;
  391. // Dest += 2;
  392. Dest += 1;
  393. }
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement