adsp.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. #include <../base.hpp>
  2. #define ADSP_CPP
  3. #include "adsp_tables.cpp"
  4. void aDSP::enter() { loop:
  5. run();
  6. goto loop;
  7. }
  8. uint8 aDSP::readb(uint16 addr) {
  9. return spcram[addr];
  10. }
  11. void aDSP::writeb(uint16 addr, uint8 data) {
  12. spcram[addr] = data;
  13. }
  14. uint16 aDSP::readw(uint16 addr) {
  15. return (readb(addr + 0)) | (readb(addr + 1) << 8);
  16. }
  17. void aDSP::writew(uint16 addr, uint16 data) {
  18. writeb(addr + 0, data);
  19. writeb(addr + 1, data >> 8);
  20. }
  21. uint8 aDSP::read(uint8 addr) {
  22. addr &= 127;
  23. int v = addr >> 4;
  24. int n = addr & 15;
  25. switch(addr) {
  26. case 0x00: case 0x10: case 0x20: case 0x30:
  27. case 0x40: case 0x50: case 0x60: case 0x70:
  28. return voice[v].VOLL;
  29. case 0x01: case 0x11: case 0x21: case 0x31:
  30. case 0x41: case 0x51: case 0x61: case 0x71:
  31. return voice[v].VOLR;
  32. case 0x02: case 0x12: case 0x22: case 0x32:
  33. case 0x42: case 0x52: case 0x62: case 0x72:
  34. return voice[v].PITCH;
  35. case 0x03: case 0x13: case 0x23: case 0x33:
  36. case 0x43: case 0x53: case 0x63: case 0x73:
  37. return voice[v].PITCH >> 8;
  38. case 0x04: case 0x14: case 0x24: case 0x34:
  39. case 0x44: case 0x54: case 0x64: case 0x74:
  40. return voice[v].SRCN;
  41. case 0x05: case 0x15: case 0x25: case 0x35:
  42. case 0x45: case 0x55: case 0x65: case 0x75:
  43. return voice[v].ADSR1;
  44. case 0x06: case 0x16: case 0x26: case 0x36:
  45. case 0x46: case 0x56: case 0x66: case 0x76:
  46. return voice[v].ADSR2;
  47. case 0x07: case 0x17: case 0x27: case 0x37:
  48. case 0x47: case 0x57: case 0x67: case 0x77:
  49. return voice[v].GAIN;
  50. case 0x08: case 0x18: case 0x28: case 0x38:
  51. case 0x48: case 0x58: case 0x68: case 0x78:
  52. return voice[v].ENVX;
  53. case 0x09: case 0x19: case 0x29: case 0x39:
  54. case 0x49: case 0x59: case 0x69: case 0x79:
  55. return voice[v].OUTX;
  56. case 0x0f: case 0x1f: case 0x2f: case 0x3f:
  57. case 0x4f: case 0x5f: case 0x6f: case 0x7f:
  58. return status.FIR[v];
  59. case 0x0c: return status.MVOLL;
  60. case 0x1c: return status.MVOLR;
  61. case 0x2c: return status.EVOLL;
  62. case 0x3c: return status.EVOLR;
  63. case 0x4c: return status.KON;
  64. case 0x5c: return status.KOFF;
  65. case 0x6c: return status.FLG;
  66. case 0x7c: return status.ENDX;
  67. case 0x0d: return status.EFB;
  68. case 0x2d: return status.PMON;
  69. case 0x3d: return status.NON;
  70. case 0x4d: return status.EON;
  71. case 0x5d: return status.DIR;
  72. case 0x6d: return status.ESA;
  73. case 0x7d: return status.EDL;
  74. }
  75. return dspram[addr];
  76. }
  77. void aDSP::write(uint8 addr, uint8 data) {
  78. //0x80-0xff is a read-only mirror of 0x00-0x7f
  79. if(addr & 0x80)return;
  80. int v = addr >> 4;
  81. int n = addr & 15;
  82. switch(addr) {
  83. case 0x00: case 0x10: case 0x20: case 0x30:
  84. case 0x40: case 0x50: case 0x60: case 0x70:
  85. voice[v].VOLL = data;
  86. break;
  87. case 0x01: case 0x11: case 0x21: case 0x31:
  88. case 0x41: case 0x51: case 0x61: case 0x71:
  89. voice[v].VOLR = data;
  90. break;
  91. case 0x02: case 0x12: case 0x22: case 0x32:
  92. case 0x42: case 0x52: case 0x62: case 0x72:
  93. voice[v].PITCH &= 0xff00;
  94. voice[v].PITCH |= data;
  95. break;
  96. case 0x03: case 0x13: case 0x23: case 0x33:
  97. case 0x43: case 0x53: case 0x63: case 0x73:
  98. voice[v].PITCH &= 0x00ff;
  99. voice[v].PITCH |= data << 8;
  100. break;
  101. case 0x04: case 0x14: case 0x24: case 0x34:
  102. case 0x44: case 0x54: case 0x64: case 0x74:
  103. voice[v].SRCN = data;
  104. break;
  105. case 0x05: case 0x15: case 0x25: case 0x35:
  106. case 0x45: case 0x55: case 0x65: case 0x75:
  107. voice[v].ADSR1 = data;
  108. voice[v].AdjustEnvelope();
  109. break;
  110. case 0x06: case 0x16: case 0x26: case 0x36:
  111. case 0x46: case 0x56: case 0x66: case 0x76:
  112. voice[v].ADSR2 = data;
  113. //sustain_level = 0-7, 7 is a special case handled by ATTACK envx mode
  114. voice[v].env_sustain = (voice[v].ADSR_sus_level() + 1) << 8;
  115. voice[v].AdjustEnvelope();
  116. break;
  117. case 0x07: case 0x17: case 0x27: case 0x37:
  118. case 0x47: case 0x57: case 0x67: case 0x77:
  119. voice[v].GAIN = data;
  120. voice[v].AdjustEnvelope();
  121. break;
  122. case 0x08: case 0x18: case 0x28: case 0x38:
  123. case 0x48: case 0x58: case 0x68: case 0x78:
  124. voice[v].ENVX = data;
  125. break;
  126. case 0x09: case 0x19: case 0x29: case 0x39:
  127. case 0x49: case 0x59: case 0x69: case 0x79:
  128. voice[v].OUTX = data;
  129. break;
  130. case 0x0f: case 0x1f: case 0x2f: case 0x3f:
  131. case 0x4f: case 0x5f: case 0x6f: case 0x7f:
  132. status.FIR[v] = data;
  133. break;
  134. case 0x0c: status.MVOLL = data; break;
  135. case 0x1c: status.MVOLR = data; break;
  136. case 0x2c: status.EVOLL = data; break;
  137. case 0x3c: status.EVOLR = data; break;
  138. case 0x4c:
  139. status.KON = data;
  140. status.kon = data;
  141. break;
  142. case 0x5c:
  143. status.KOFF = data;
  144. break;
  145. case 0x6c:
  146. status.FLG = data;
  147. status.noise_rate = rate_table[data & 0x1f];
  148. break;
  149. case 0x7c:
  150. //read-only register, writes clear all bits of ENDX
  151. status.ENDX = 0;
  152. break;
  153. case 0x0d: status.EFB = data; break;
  154. case 0x2d: status.PMON = data; break;
  155. case 0x3d: status.NON = data; break;
  156. case 0x4d: status.EON = data; break;
  157. case 0x5d: status.DIR = data; break;
  158. case 0x6d: status.ESA = data; break;
  159. case 0x7d: status.EDL = data; break;
  160. }
  161. dspram[addr] = data;
  162. }
  163. void aDSP::power() {
  164. spcram = r_smp->get_spcram_handle();
  165. memset(dspram, 0x00, 128);
  166. for(int v = 0; v < 8; v++) {
  167. voice[v].VOLL = 0;
  168. voice[v].VOLR = 0;
  169. voice[v].PITCH = 0;
  170. voice[v].SRCN = 0;
  171. voice[v].ADSR1 = 0;
  172. voice[v].ADSR2 = 0;
  173. voice[v].GAIN = 0;
  174. status.FIR[v] = 0;
  175. }
  176. status.FLG = 0xe0;
  177. status.MVOLL = status.MVOLR = 0;
  178. status.EVOLL = status.EVOLR = 0;
  179. status.ENDX = 0;
  180. status.EFB = 0;
  181. status.PMON = 0;
  182. status.NON = 0;
  183. status.EON = 0;
  184. status.DIR = 0;
  185. status.ESA = 0;
  186. status.EDL = 0;
  187. status.echo_length = 0;
  188. reset();
  189. }
  190. void aDSP::reset() {
  191. status.KON = 0x00;
  192. status.KOFF = 0x00;
  193. status.FLG |= 0xe0;
  194. status.kon = 0x00;
  195. status.esa = 0x00;
  196. status.noise_ctr = 0;
  197. status.noise_rate = 0;
  198. status.noise_sample = 0x4000;
  199. status.echo_index = 0;
  200. status.fir_buffer_index = 0;
  201. for(int v = 0; v < 8; v++) {
  202. voice[v].ENVX = 0;
  203. voice[v].OUTX = 0;
  204. voice[v].pitch_ctr = 0;
  205. voice[v].brr_index = 0;
  206. voice[v].brr_ptr = readw((status.DIR << 8) + (voice[v].SRCN << 2));
  207. voice[v].brr_looped = false;
  208. voice[v].brr_data[0] = 0;
  209. voice[v].brr_data[1] = 0;
  210. voice[v].brr_data[2] = 0;
  211. voice[v].brr_data[3] = 0;
  212. voice[v].brr_data_index = 0;
  213. voice[v].envx = 0;
  214. voice[v].env_ctr = 0;
  215. voice[v].env_rate = 0;
  216. voice[v].env_state = SILENCE;
  217. voice[v].env_mode = DIRECT;
  218. status.fir_buffer[0][v] = 0;
  219. status.fir_buffer[1][v] = 0;
  220. }
  221. dsp_counter = 0;
  222. }
  223. void aDSP::run() {
  224. uint8 pmon;
  225. int32 sample;
  226. int32 msamplel, msampler;
  227. int32 esamplel, esampler;
  228. int32 fir_samplel, fir_sampler;
  229. pmon = status.PMON & ~status.NON & ~1;
  230. if((dsp_counter++ & 1) == 0) {
  231. for(uint v = 0; v < 8; v++) {
  232. if(status.soft_reset()) {
  233. if(voice[v].env_state != SILENCE) {
  234. voice[v].env_state = SILENCE;
  235. voice[v].AdjustEnvelope();
  236. }
  237. }
  238. if(status.KOFF & (1 << v)) {
  239. if(voice[v].env_state != SILENCE && voice[v].env_state != RELEASE) {
  240. voice[v].env_state = RELEASE;
  241. voice[v].AdjustEnvelope();
  242. }
  243. }
  244. if(status.kon & (1 << v)) {
  245. voice[v].brr_ptr = readw((status.DIR << 8) + (voice[v].SRCN << 2));
  246. voice[v].brr_index = -9;
  247. voice[v].brr_looped = false;
  248. voice[v].brr_data[0] = 0;
  249. voice[v].brr_data[1] = 0;
  250. voice[v].brr_data[2] = 0;
  251. voice[v].brr_data[3] = 0;
  252. voice[v].envx = 0;
  253. voice[v].env_state = ATTACK;
  254. voice[v].AdjustEnvelope();
  255. }
  256. }
  257. status.ENDX &= ~status.kon;
  258. status.kon = 0;
  259. }
  260. /*****
  261. * update noise
  262. *****/
  263. status.noise_ctr += status.noise_rate;
  264. if(status.noise_ctr >= 0x7800) {
  265. status.noise_ctr -= 0x7800;
  266. status.noise_sample = (status.noise_sample >> 1) | (((status.noise_sample << 14) ^ (status.noise_sample << 13)) & 0x4000);
  267. }
  268. msamplel = msampler = 0;
  269. esamplel = esampler = 0;
  270. /*****
  271. * process voice channels
  272. *****/
  273. for(int v = 0; v < 8; v++) {
  274. if(voice[v].brr_index < -1) {
  275. voice[v].brr_index++;
  276. voice[v].OUTX = voice[v].outx = 0;
  277. voice[v].ENVX = 0;
  278. continue;
  279. }
  280. if(voice[v].brr_index >= 0) {
  281. if(pmon & (1 << v)) {
  282. voice[v].pitch_ctr += (voice[v].pitch_rate() * (voice[v - 1].outx + 0x8000)) >> 15;
  283. } else {
  284. voice[v].pitch_ctr += voice[v].pitch_rate();
  285. }
  286. } else {
  287. voice[v].pitch_ctr = 0x3000;
  288. voice[v].brr_index = 0;
  289. }
  290. /*****
  291. * decode BRR samples
  292. *****/
  293. while(voice[v].pitch_ctr >= 0) {
  294. voice[v].pitch_ctr -= 0x1000;
  295. voice[v].brr_data_index++;
  296. voice[v].brr_data_index &= 3;
  297. if(voice[v].brr_index == 0) {
  298. voice[v].brr_header = readb(voice[v].brr_ptr);
  299. if(voice[v].brr_header_flags() == BRR_END) {
  300. status.ENDX |= (1 << v);
  301. voice[v].env_state = SILENCE;
  302. voice[v].AdjustEnvelope();
  303. }
  304. }
  305. #define S(x) voice[v].brr_data[(voice[v].brr_data_index + (x)) & 3]
  306. if(voice[v].env_state != SILENCE) {
  307. sample = readb(voice[v].brr_ptr + 1 + (voice[v].brr_index >> 1));
  308. if(voice[v].brr_index & 1) {
  309. sample = sclip<4>(sample);
  310. } else {
  311. sample = sclip<4>(sample >> 4);
  312. }
  313. if(voice[v].brr_header_shift() <= 12) {
  314. sample = (sample << voice[v].brr_header_shift() >> 1);
  315. } else {
  316. sample &= ~0x7ff;
  317. }
  318. switch(voice[v].brr_header_filter()) {
  319. case 0: //direct
  320. break;
  321. case 1: //15/16
  322. sample += S(-1) + ((-S(-1)) >> 4);
  323. break;
  324. case 2: //61/32 - 15/16
  325. sample += (S(-1) << 1) + ((-((S(-1) << 1) + S(-1))) >> 5)
  326. - S(-2) + (S(-2) >> 4);
  327. break;
  328. case 3: //115/64 - 13/16
  329. sample += (S(-1) << 1) + ((-(S(-1) + (S(-1) << 2) + (S(-1) << 3))) >> 6)
  330. - S(-2) + (((S(-2) << 1) + S(-2)) >> 4);
  331. break;
  332. }
  333. S(0) = sample = sclip<15>(sclamp<16>(sample));
  334. } else {
  335. S(0) = sample = 0;
  336. }
  337. if(++voice[v].brr_index > 15) {
  338. voice[v].brr_index = 0;
  339. if(voice[v].brr_header_flags() & BRR_END) {
  340. if(voice[v].brr_header_flags() & BRR_LOOP) {
  341. status.ENDX |= (1 << v);
  342. }
  343. voice[v].brr_ptr = readw((status.DIR << 8) + (voice[v].SRCN << 2) + 2);
  344. voice[v].brr_looped = true;
  345. } else {
  346. voice[v].brr_ptr += 9;
  347. }
  348. }
  349. }
  350. /*****
  351. * volume envelope adjust
  352. *****/
  353. voice[v].env_ctr += voice[v].env_rate;
  354. if(voice[v].env_ctr >= 0x7800) {
  355. voice[v].env_ctr -= 0x7800;
  356. switch(voice[v].env_mode) {
  357. case DIRECT:
  358. voice[v].env_rate = 0;
  359. break;
  360. case LINEAR_DEC:
  361. voice[v].envx -= 32;
  362. if(voice[v].envx <= 0) {
  363. voice[v].envx = 0;
  364. voice[v].env_rate = 0;
  365. voice[v].env_mode = DIRECT;
  366. }
  367. break;
  368. case LINEAR_INC:
  369. voice[v].envx += 32;
  370. if(voice[v].envx >= 0x7ff) {
  371. voice[v].envx = 0x7ff;
  372. voice[v].env_rate = 0;
  373. voice[v].env_mode = DIRECT;
  374. if(voice[v].ADSR_enabled() && voice[v].env_state == ATTACK) {
  375. voice[v].env_state = ((voice[v].env_sustain == 0x800) ? SUSTAIN : DECAY);
  376. voice[v].AdjustEnvelope();
  377. }
  378. }
  379. break;
  380. case EXP_DEC:
  381. //multiply by 255/256ths
  382. voice[v].envx -= ((voice[v].envx - 1) >> 8) + 1;
  383. if(voice[v].ADSR_enabled() && voice[v].env_state == DECAY && voice[v].envx <= voice[v].env_sustain) {
  384. voice[v].env_state = SUSTAIN;
  385. voice[v].AdjustEnvelope();
  386. } else if(voice[v].envx <= 0) {
  387. voice[v].envx = 0;
  388. voice[v].env_rate = 0;
  389. voice[v].env_mode = DIRECT;
  390. }
  391. break;
  392. case BENT_INC:
  393. if(voice[v].envx < 0x600) {
  394. voice[v].envx += 32;
  395. } else {
  396. voice[v].envx += 8;
  397. if(voice[v].envx >= 0x7ff) {
  398. voice[v].envx = 0x7ff;
  399. voice[v].env_rate = 0;
  400. voice[v].env_mode = DIRECT;
  401. }
  402. }
  403. break;
  404. case FAST_ATTACK:
  405. voice[v].envx += 0x400;
  406. if(voice[v].envx >= 0x7ff) {
  407. voice[v].envx = 0x7ff;
  408. //attack raises to max envx. if sustain is also set to max envx, skip decay phase
  409. voice[v].env_state = ((voice[v].env_sustain == 0x800) ? SUSTAIN : DECAY);
  410. voice[v].AdjustEnvelope();
  411. }
  412. break;
  413. case RELEASE_DEC:
  414. voice[v].envx -= 8;
  415. if(voice[v].envx <= 0) {
  416. voice[v].env_state = SILENCE;
  417. voice[v].AdjustEnvelope();
  418. }
  419. break;
  420. }
  421. }
  422. voice[v].ENVX = voice[v].envx >> 4;
  423. /*****
  424. * gaussian interpolation / noise
  425. *****/
  426. if(status.NON & (1 << v)) {
  427. sample = sclip<15>(status.noise_sample);
  428. } else {
  429. int32 d = voice[v].pitch_ctr >> 4; //-256 <= sample <= -1
  430. sample = ((gaussian_table[ -1 - d] * S(-3)) >> 11);
  431. sample += ((gaussian_table[255 - d] * S(-2)) >> 11);
  432. sample += ((gaussian_table[512 + d] * S(-1)) >> 11);
  433. sample = sclip <15>(sample);
  434. sample += ((gaussian_table[256 + d] * S( 0)) >> 11);
  435. sample = sclamp<15>(sample);
  436. }
  437. #undef S
  438. /*****
  439. * envelope / volume adjust
  440. *****/
  441. sample = (sample * voice[v].envx) >> 11;
  442. voice[v].outx = sample << 1;
  443. voice[v].OUTX = sample >> 7;
  444. if(!status.mute()) {
  445. msamplel += ((sample * voice[v].VOLL) >> 7) << 1;
  446. msampler += ((sample * voice[v].VOLR) >> 7) << 1;
  447. }
  448. if((status.EON & (1 << v)) && status.echo_write()) {
  449. esamplel += ((sample * voice[v].VOLL) >> 7) << 1;
  450. esampler += ((sample * voice[v].VOLR) >> 7) << 1;
  451. }
  452. }
  453. /*****
  454. * echo (FIR) adjust
  455. *****/
  456. #define F(c,x) status.fir_buffer[c][(status.fir_buffer_index + (x)) & 7]
  457. status.fir_buffer_index++;
  458. F(0,0) = readw((status.esa << 8) + status.echo_index + 0);
  459. F(1,0) = readw((status.esa << 8) + status.echo_index + 2);
  460. fir_samplel = (F(0,-0) * status.FIR[7] +
  461. F(0,-1) * status.FIR[6] +
  462. F(0,-2) * status.FIR[5] +
  463. F(0,-3) * status.FIR[4] +
  464. F(0,-4) * status.FIR[3] +
  465. F(0,-5) * status.FIR[2] +
  466. F(0,-6) * status.FIR[1] +
  467. F(0,-7) * status.FIR[0]);
  468. fir_sampler = (F(1,-0) * status.FIR[7] +
  469. F(1,-1) * status.FIR[6] +
  470. F(1,-2) * status.FIR[5] +
  471. F(1,-3) * status.FIR[4] +
  472. F(1,-4) * status.FIR[3] +
  473. F(1,-5) * status.FIR[2] +
  474. F(1,-6) * status.FIR[1] +
  475. F(1,-7) * status.FIR[0]);
  476. #undef F
  477. /*****
  478. * update echo buffer
  479. *****/
  480. if(status.echo_write()) {
  481. esamplel += (fir_samplel * status.EFB) >> 14;
  482. esampler += (fir_sampler * status.EFB) >> 14;
  483. esamplel = sclamp<16>(esamplel);
  484. esampler = sclamp<16>(esampler);
  485. writew((status.esa << 8) + status.echo_index + 0, esamplel);
  486. writew((status.esa << 8) + status.echo_index + 2, esampler);
  487. }
  488. status.echo_index += 4;
  489. if(status.echo_index >= status.echo_length) {
  490. status.echo_index = 0;
  491. status.echo_length = (status.EDL & 0x0f) << 11;
  492. }
  493. //ESA read occurs at roughly 22/32th sample
  494. //ESA fetch occurs at roughly 29/32th sample
  495. //as this is not a subsample-level S-DSP emulator,
  496. //simulate ~25/32th delay by caching ESA for one
  497. //complete sample ...
  498. status.esa = status.ESA;
  499. /*****
  500. * main output adjust
  501. *****/
  502. if(!status.mute()) {
  503. msamplel = (msamplel * status.MVOLL) >> 7;
  504. msampler = (msampler * status.MVOLR) >> 7;
  505. msamplel += (fir_samplel * status.EVOLL) >> 14;
  506. msampler += (fir_sampler * status.EVOLR) >> 14;
  507. msamplel = sclamp<16>(msamplel);
  508. msampler = sclamp<16>(msampler);
  509. }
  510. snes.audio.update(msamplel, msampler);
  511. scheduler.addclocks_dsp(32 * 3 * 8);
  512. }
  513. aDSP::aDSP() {}
  514. aDSP::~aDSP() {}