rix.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * Adplug - Replayer for many OPL2/OPL3 audio file formats.
  3. * Copyright (C) 1999 - 2007 Simon Peter, <dn.tlp@gmx.net>, et al.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * rix.cpp - Softstar RIX OPL Format Player by palxex <palxex.ys168.com>
  20. * BSPAL <BSPAL.ys168.com>
  21. */
  22. #include <cstring>
  23. #include <cstdlib>
  24. #include "rix.h"
  25. using namespace std;
  26. #if !defined(_WIN32) || defined(__SYMBIAN32__)
  27. #define stricmp strcasecmp
  28. #endif
  29. #if defined(__hppa__) || \
  30. defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
  31. (defined(__MIPS__) && defined(__MISPEB__)) || \
  32. defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
  33. defined(__sparc__)
  34. // big endian
  35. #define RIX_SWAP32(a) (((a) << 24) | (((a) << 8) & 0x00FF0000) | (((a) >> 8) & 0x0000FF00) | ((a) >> 24))
  36. #else
  37. // little endian
  38. #define RIX_SWAP32(a) (a)
  39. #endif
  40. const uint8_t CrixPlayer::adflag[] = {0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1};
  41. const uint8_t CrixPlayer::reg_data[] = {0,1,2,3,4,5,8,9,10,11,12,13,16,17,18,19,20,21};
  42. const uint8_t CrixPlayer::ad_C0_offs[] = {0,1,2,0,1,2,3,4,5,3,4,5,6,7,8,6,7,8};
  43. const uint8_t CrixPlayer::modify[] = {0,3,1,4,2,5,6,9,7,10,8,11,12,15,13,16,14,17,12,\
  44. 15,16,0,14,0,17,0,13,0};
  45. const uint8_t CrixPlayer::bd_reg_data[] = {
  46. 0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x08,0x04,0x02,0x01,
  47. 0x00,0x01,0x01,0x03,0x0F,0x05,0x00,0x01,0x03,0x0F,0x00,
  48. 0x00,0x00,0x01,0x00,0x00,0x01,0x01,0x0F,0x07,0x00,0x02,
  49. 0x04,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x0A,
  50. 0x04,0x00,0x08,0x0C,0x0B,0x00,0x00,0x00,0x01,0x00,0x00,
  51. 0x00,0x00,0x0D,0x04,0x00,0x06,0x0F,0x00,0x00,0x00,0x00,
  52. 0x01,0x00,0x00,0x0C,0x00,0x0F,0x0B,0x00,0x08,0x05,0x00,
  53. 0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x0F,0x0B,0x00,
  54. 0x07,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
  55. 0x0F,0x0B,0x00,0x05,0x05,0x00,0x00,0x00,0x00,0x00,0x00,
  56. 0x00,0x01,0x00,0x0F,0x0B,0x00,0x07,0x05,0x00,0x00,0x00,
  57. 0x00,0x00,0x00};
  58. uint8_t CrixPlayer::for40reg[] = {0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  59. 0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F};
  60. const uint16_t CrixPlayer::mus_time = 0x4268;
  61. /*** public methods *************************************/
  62. CPlayer *CrixPlayer::factory(Copl *newopl)
  63. {
  64. return new CrixPlayer(newopl);
  65. }
  66. CrixPlayer::CrixPlayer(Copl *newopl)
  67. : CPlayer(newopl), flag_mkf(0), file_buffer(0), rix_buf(0)
  68. {
  69. }
  70. CrixPlayer::~CrixPlayer()
  71. {
  72. if(file_buffer)
  73. delete [] file_buffer;
  74. }
  75. bool CrixPlayer::load(const std::string &filename, const CFileProvider &fp)
  76. {
  77. binistream *f = fp.open(filename); if(!f) return false;
  78. uint32_t i=0;
  79. if(stricmp(filename.substr(filename.length()-4,4).c_str(),".mkf")==0)
  80. {
  81. flag_mkf=1;
  82. f->seek(0);
  83. int offset=f->readInt(4);
  84. f->seek(offset);
  85. }
  86. if(f->readInt(2)!=0x55aa){ fp.close(f);return false; }
  87. file_buffer = new uint8_t [fp.filesize(f) + 1];
  88. f->seek(0);
  89. while(!f->eof())
  90. file_buffer[i++]=f->readInt(1);
  91. length=i;
  92. fp.close(f);
  93. if(!flag_mkf)
  94. rix_buf=file_buffer;
  95. rewind(0);
  96. return true;
  97. }
  98. bool CrixPlayer::update()
  99. {
  100. int_08h_entry();
  101. return !play_end;
  102. }
  103. void CrixPlayer::rewind(int subsong)
  104. {
  105. I = 0; T = 0;
  106. mus_block = 0;
  107. ins_block = 0;
  108. rhythm = 0;
  109. music_on = 0;
  110. pause_flag = 0;
  111. band = 0;
  112. band_low = 0;
  113. e0_reg_flag = 0;
  114. bd_modify = 0;
  115. sustain = 0;
  116. play_end = 0;
  117. pos = index = 0;
  118. memset(f_buffer, 0, sizeof(f_buffer));
  119. memset(a0b0_data2, 0, sizeof(a0b0_data2));
  120. memset(a0b0_data3, 0, sizeof(a0b0_data3));
  121. memset(a0b0_data4, 0, sizeof(a0b0_data4));
  122. memset(a0b0_data5, 0, sizeof(a0b0_data5));
  123. memset(addrs_head, 0, sizeof(addrs_head));
  124. memset(insbuf, 0, sizeof(insbuf));
  125. memset(displace, 0, sizeof(displace));
  126. memset(reg_bufs, 0, sizeof(reg_bufs));
  127. memset(for40reg, 0x7F, sizeof(for40reg));
  128. if(flag_mkf)
  129. {
  130. uint32_t *buf_index=(uint32_t *)file_buffer;
  131. int offset1=RIX_SWAP32(buf_index[subsong]),offset2;
  132. while((offset2=RIX_SWAP32(buf_index[++subsong]))==offset1);
  133. length=offset2-offset1+1;
  134. rix_buf=file_buffer+offset1;
  135. }
  136. opl->init();
  137. opl->write(1,32); // go to OPL2 mode
  138. set_new_int();
  139. data_initial();
  140. }
  141. unsigned int CrixPlayer::getsubsongs()
  142. {
  143. if(flag_mkf)
  144. {
  145. uint32_t *buf_index=(uint32_t *)file_buffer;
  146. int songs=RIX_SWAP32(buf_index[0])/4,i=0;
  147. for(i=0;i<songs;i++)
  148. if(buf_index[i+1]==buf_index[i])
  149. songs--;
  150. return songs;
  151. }
  152. else
  153. return 1;
  154. }
  155. float CrixPlayer::getrefresh()
  156. {
  157. return 70.0f;
  158. }
  159. /*------------------Implemention----------------------------*/
  160. inline void CrixPlayer::set_new_int()
  161. {
  162. // if(!ad_initial()) exit(1);
  163. ad_initial();
  164. }
  165. /*----------------------------------------------------------*/
  166. inline void CrixPlayer::Pause()
  167. {
  168. register uint16_t i;
  169. pause_flag = 1;
  170. for(i=0;i<11;i++)
  171. switch_ad_bd(i);
  172. }
  173. /*----------------------------------------------------------*/
  174. inline void CrixPlayer::ad_a0b0l_reg_(uint16_t index,uint16_t p2,uint16_t p3)
  175. {
  176. // uint16_t i = p2+a0b0_data2[index];
  177. a0b0_data4[index] = p3;
  178. a0b0_data3[index] = p2;
  179. }
  180. inline void CrixPlayer::data_initial()
  181. {
  182. rhythm = rix_buf[2];
  183. mus_block = (rix_buf[0x0D]<<8)+rix_buf[0x0C];
  184. ins_block = (rix_buf[0x09]<<8)+rix_buf[0x08];
  185. I = mus_block+1;
  186. if(rhythm != 0)
  187. {
  188. // ad_a0b0_reg(6);
  189. // ad_a0b0_reg(7);
  190. // ad_a0b0_reg(8);
  191. ad_a0b0l_reg_(8,0x18,0);
  192. ad_a0b0l_reg_(7,0x1F,0);
  193. }
  194. bd_modify = 0;
  195. // ad_bd_reg();
  196. band = 0; music_on = 1;
  197. }
  198. /*----------------------------------------------------------*/
  199. inline uint16_t CrixPlayer::ad_initial()
  200. {
  201. uint16_t i,j,k = 0;
  202. for(i=0;i<25;i++)
  203. {
  204. uint32_t res = ((uint32_t)i*24+10000)*52088/250000*0x24000/0x1B503;
  205. f_buffer[i*12]=((uint16_t)res+4)>>3;
  206. for(int t=1;t<12;t++)
  207. {
  208. res*=1.06;
  209. f_buffer[i*12+t]=((uint16_t)res+4)>>3;
  210. }
  211. }
  212. for(i=0;i<8;i++)
  213. for(j=0;j<12;j++)
  214. {
  215. a0b0_data5[k] = i;
  216. addrs_head[k] = j;
  217. k++;
  218. }
  219. //ad_bd_reg();
  220. //ad_08_reg();
  221. //for(i=0;i<9;i++) ad_a0b0_reg(i);
  222. e0_reg_flag = 0x20;
  223. //for(i=0;i<18;i++) ad_bop(0xE0+reg_data[i],0);
  224. //ad_bop(1,e0_reg_flag);
  225. return 1;//ad_test();
  226. }
  227. /*----------------------------------------------------------*/
  228. inline void CrixPlayer::ad_bop(uint16_t reg,uint16_t value)
  229. {
  230. //if(reg == 2 || reg == 3)
  231. // AdPlug_LogWrite("switch OPL2/3 mode!\n");
  232. opl->write(reg & 0xff, value & 0xff);
  233. }
  234. /*--------------------------------------------------------------*/
  235. inline void CrixPlayer::int_08h_entry()
  236. {
  237. uint16_t band_sus = 1;
  238. while(band_sus)
  239. {
  240. if(sustain <= 0)
  241. {
  242. band_sus = rix_proc();
  243. if(band_sus) sustain += band_sus;
  244. else
  245. {
  246. play_end=1;
  247. break;
  248. }
  249. }
  250. else
  251. {
  252. if(band_sus) sustain -= 14; /* aging */
  253. break;
  254. }
  255. }
  256. }
  257. /*--------------------------------------------------------------*/
  258. inline uint16_t CrixPlayer::rix_proc()
  259. {
  260. uint8_t ctrl = 0;
  261. if(music_on == 0||pause_flag == 1) return 0;
  262. band = 0;
  263. while(rix_buf[I] != 0x80 && I<length-1)
  264. {
  265. band_low = rix_buf[I-1];
  266. ctrl = rix_buf[I]; I+=2;
  267. switch(ctrl&0xF0)
  268. {
  269. case 0x90: rix_get_ins(); rix_90_pro(ctrl&0x0F); break;
  270. case 0xA0: rix_A0_pro(ctrl&0x0F,((uint16_t)band_low)<<6); break;
  271. case 0xB0: rix_B0_pro(ctrl&0x0F,band_low); break;
  272. case 0xC0: switch_ad_bd(ctrl&0x0F);
  273. if(band_low != 0) rix_C0_pro(ctrl&0x0F,band_low);
  274. break;
  275. default: band = (ctrl<<8)+band_low; break;
  276. }
  277. if(band != 0) return band;
  278. }
  279. music_ctrl();
  280. I = mus_block+1;
  281. band = 0; music_on = 1;
  282. return 0;
  283. }
  284. /*--------------------------------------------------------------*/
  285. inline void CrixPlayer::rix_get_ins()
  286. {
  287. int i;
  288. uint8_t *baddr = (&rix_buf[ins_block])+(band_low<<6);
  289. for(i = 0; i < 28; i++)
  290. insbuf[i] = (baddr[i * 2 + 1] << 8) + baddr[i * 2];
  291. }
  292. /*--------------------------------------------------------------*/
  293. inline void CrixPlayer::rix_90_pro(uint16_t ctrl_l)
  294. {
  295. if(rhythm == 0 || ctrl_l < 6)
  296. {
  297. ins_to_reg(modify[ctrl_l*2],insbuf,insbuf[26]);
  298. ins_to_reg(modify[ctrl_l*2+1],insbuf+13,insbuf[27]);
  299. return;
  300. }
  301. else if(ctrl_l > 6)
  302. {
  303. ins_to_reg(modify[ctrl_l*2+6],insbuf,insbuf[26]);
  304. return;
  305. }
  306. else
  307. {
  308. ins_to_reg(12,insbuf,insbuf[26]);
  309. ins_to_reg(15,insbuf+13,insbuf[27]);
  310. return;
  311. }
  312. }
  313. /*--------------------------------------------------------------*/
  314. inline void CrixPlayer::rix_A0_pro(uint16_t ctrl_l,uint16_t index)
  315. {
  316. if(rhythm == 0 || ctrl_l <= 6)
  317. {
  318. prepare_a0b0(ctrl_l,index>0x3FFF?0x3FFF:index);
  319. ad_a0b0l_reg(ctrl_l,a0b0_data3[ctrl_l],a0b0_data4[ctrl_l]);
  320. }
  321. else return;
  322. }
  323. /*--------------------------------------------------------------*/
  324. inline void CrixPlayer::prepare_a0b0(uint16_t index,uint16_t v) /* important !*/
  325. {
  326. short high = 0,low = 0; uint32_t res;
  327. int res1 = (v-0x2000)*0x19;
  328. if(res1 == (int)0xff) return;
  329. low = res1/0x2000;
  330. if(low < 0)
  331. {
  332. low = 0x18-low; high = (signed short)low<0?0xFFFF:0;
  333. res = high; res<<=16; res+=low;
  334. low = ((signed short)res)/(signed short)0xFFE7;
  335. a0b0_data2[index] = low;
  336. low = res;
  337. res = low - 0x18;
  338. high = (signed short)res%0x19;
  339. low = (signed short)res/0x19;
  340. if(high != 0) {low = 0x19; low = low-high;}
  341. }
  342. else
  343. {
  344. res = high = low;
  345. low = (signed short)res/(signed short)0x19;
  346. a0b0_data2[index] = low;
  347. res = high;
  348. low = (signed short)res%(signed short)0x19;
  349. }
  350. low = (signed short)low*(signed short)0x18;
  351. displace[index] = low;
  352. }
  353. /*--------------------------------------------------------------*/
  354. inline void CrixPlayer::ad_a0b0l_reg(uint16_t index,uint16_t p2,uint16_t p3)
  355. {
  356. uint16_t data; uint16_t i = p2+a0b0_data2[index];
  357. a0b0_data4[index] = p3;
  358. a0b0_data3[index] = p2;
  359. i = ((signed short)i<=0x5F?i:0x5F);
  360. i = ((signed short)i>=0?i:0);
  361. data = f_buffer[addrs_head[i]+displace[index]/2];
  362. ad_bop(0xA0+index,data);
  363. data = a0b0_data5[i]*4+(p3<1?0:0x20)+((data>>8)&3);
  364. ad_bop(0xB0+index,data);
  365. }
  366. /*--------------------------------------------------------------*/
  367. inline void CrixPlayer::rix_B0_pro(uint16_t ctrl_l,uint16_t index)
  368. {
  369. register int temp = 0;
  370. if(rhythm == 0 || ctrl_l < 6) temp = modify[ctrl_l*2+1];
  371. else
  372. {
  373. temp = ctrl_l > 6?ctrl_l*2:ctrl_l*2+1;
  374. temp = modify[temp+6];
  375. }
  376. for40reg[temp] = index>0x7F?0x7F:index;
  377. ad_40_reg(temp);
  378. }
  379. /*--------------------------------------------------------------*/
  380. inline void CrixPlayer::rix_C0_pro(uint16_t ctrl_l,uint16_t index)
  381. {
  382. register uint16_t i = index>=12?index-12:0;
  383. if(ctrl_l < 6 || rhythm == 0)
  384. {
  385. ad_a0b0l_reg(ctrl_l,i,1);
  386. return;
  387. }
  388. else
  389. {
  390. if(ctrl_l != 6)
  391. {
  392. if(ctrl_l == 8)
  393. {
  394. ad_a0b0l_reg(ctrl_l,i,0);
  395. ad_a0b0l_reg(7,i+7,0);
  396. }
  397. }
  398. else ad_a0b0l_reg(ctrl_l,i,0);
  399. bd_modify |= bd_reg_data[ctrl_l];
  400. ad_bd_reg();
  401. return;
  402. }
  403. }
  404. /*--------------------------------------------------------------*/
  405. inline void CrixPlayer::switch_ad_bd(uint16_t index)
  406. {
  407. if(rhythm == 0 || index < 6) ad_a0b0l_reg(index,a0b0_data3[index],0);
  408. else
  409. {
  410. bd_modify &= (~bd_reg_data[index]),
  411. ad_bd_reg();
  412. }
  413. }
  414. /*--------------------------------------------------------------*/
  415. inline void CrixPlayer::ins_to_reg(uint16_t index,uint16_t* insb,uint16_t value)
  416. {
  417. register uint16_t i;
  418. for(i=0;i<13;i++) reg_bufs[index].v[i] = insb[i];
  419. reg_bufs[index].v[13] = value&3;
  420. ad_bd_reg(),ad_08_reg(),
  421. ad_40_reg(index),ad_C0_reg(index),ad_60_reg(index),
  422. ad_80_reg(index),ad_20_reg(index),ad_E0_reg(index);
  423. }
  424. /*--------------------------------------------------------------*/
  425. inline void CrixPlayer::ad_E0_reg(uint16_t index)
  426. {
  427. uint16_t data = e0_reg_flag == 0?0:(reg_bufs[index].v[13]&3);
  428. ad_bop(0xE0+reg_data[index],data);
  429. }
  430. /*--------------------------------------------------------------*/
  431. inline void CrixPlayer::ad_20_reg(uint16_t index)
  432. {
  433. uint16_t data = (reg_bufs[index].v[9] < 1?0:0x80);
  434. data += (reg_bufs[index].v[10] < 1?0:0x40);
  435. data += (reg_bufs[index].v[5] < 1?0:0x20);
  436. data += (reg_bufs[index].v[11] < 1?0:0x10);
  437. data += (reg_bufs[index].v[1]&0x0F);
  438. ad_bop(0x20+reg_data[index],data);
  439. }
  440. /*--------------------------------------------------------------*/
  441. inline void CrixPlayer::ad_80_reg(uint16_t index)
  442. {
  443. uint16_t data = (reg_bufs[index].v[7]&0x0F),temp = reg_bufs[index].v[4];
  444. data |= (temp << 4);
  445. ad_bop(0x80+reg_data[index],data);
  446. }
  447. /*--------------------------------------------------------------*/
  448. inline void CrixPlayer::ad_60_reg(uint16_t index)
  449. {
  450. uint16_t data = reg_bufs[index].v[6]&0x0F,temp = reg_bufs[index].v[3];
  451. data |= (temp << 4);
  452. ad_bop(0x60+reg_data[index],data);
  453. }
  454. /*--------------------------------------------------------------*/
  455. inline void CrixPlayer::ad_C0_reg(uint16_t index)
  456. {
  457. uint16_t data = reg_bufs[index].v[2];
  458. if(adflag[index] == 1) return;
  459. data *= 2,
  460. data |= (reg_bufs[index].v[12] < 1?1:0);
  461. ad_bop(0xC0+ad_C0_offs[index],data);
  462. }
  463. /*--------------------------------------------------------------*/
  464. inline void CrixPlayer::ad_40_reg(uint16_t index)
  465. {
  466. uint32_t res = 0;
  467. uint16_t data = 0,temp = reg_bufs[index].v[0];
  468. data = 0x3F - (0x3F & reg_bufs[index].v[8]),
  469. data *= for40reg[index],
  470. data *= 2,
  471. data += 0x7F,
  472. res = data;
  473. data = res/0xFE,
  474. data -= 0x3F,
  475. data = -data,
  476. data |= temp<<6;
  477. ad_bop(0x40+reg_data[index],data);
  478. }
  479. /*--------------------------------------------------------------*/
  480. inline void CrixPlayer::ad_bd_reg()
  481. {
  482. uint16_t data = rhythm < 1? 0:0x20;
  483. data |= bd_modify;
  484. ad_bop(0xBD,data);
  485. }
  486. /*--------------------------------------------------------------*/
  487. inline void CrixPlayer::ad_a0b0_reg(uint16_t index)
  488. {
  489. ad_bop(0xA0+index,0);
  490. ad_bop(0xB0+index,0);
  491. }
  492. /*--------------------------------------------------------------*/
  493. inline void CrixPlayer::music_ctrl()
  494. {
  495. register int i;
  496. for(i=0;i<11;i++)
  497. switch_ad_bd(i);
  498. }