pwm.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * PicoDrive
  3. * (C) notaz, 2009,2010,2013
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #include "../pico_int.h"
  9. static struct {
  10. int cycles;
  11. unsigned mult;
  12. int ptr;
  13. int irq_reload;
  14. int doing_fifo;
  15. int silent;
  16. int irq_timer;
  17. int irq_state;
  18. short current[2];
  19. } pwm;
  20. enum { PWM_IRQ_LOCKED, PWM_IRQ_STOPPED, PWM_IRQ_LOW, PWM_IRQ_HIGH };
  21. void p32x_pwm_ctl_changed(void)
  22. {
  23. int control = Pico32x.regs[0x30 / 2];
  24. int cycles = Pico32x.regs[0x32 / 2];
  25. int pwm_irq_opt = PicoIn.opt & POPT_PWM_IRQ_OPT;
  26. cycles = (cycles - 1) & 0x0fff;
  27. pwm.cycles = cycles;
  28. // supposedly we should stop FIFO when xMd is 0,
  29. // but mars test disagrees
  30. pwm.mult = 0;
  31. if ((control & 0x0f) != 0)
  32. pwm.mult = 0x10000 / cycles;
  33. pwm.irq_timer = (control & 0x0f00) >> 8;
  34. pwm.irq_timer = ((pwm.irq_timer - 1) & 0x0f) + 1;
  35. pwm.irq_reload = pwm.irq_timer;
  36. pwm.irq_state = pwm_irq_opt ? PWM_IRQ_STOPPED: PWM_IRQ_LOCKED;
  37. if (Pico32x.pwm_irq_cnt == 0)
  38. Pico32x.pwm_irq_cnt = pwm.irq_reload;
  39. }
  40. static void do_pwm_irq(SH2 *sh2, unsigned int m68k_cycles)
  41. {
  42. p32x_trigger_irq(sh2, m68k_cycles, P32XI_PWM);
  43. if (Pico32x.regs[0x30 / 2] & P32XP_RTP) {
  44. p32x_event_schedule(m68k_cycles, P32X_EVENT_PWM, pwm.cycles / 3 + 1);
  45. // note: might recurse
  46. p32x_dreq1_trigger();
  47. }
  48. }
  49. static int convert_sample(unsigned int v)
  50. {
  51. if (v > pwm.cycles)
  52. v = pwm.cycles;
  53. if (v == 0)
  54. return 0;
  55. return v * pwm.mult - 0x10000/2;
  56. }
  57. #define consume_fifo(sh2, m68k_cycles) { \
  58. int cycles_diff = ((m68k_cycles) * 3) - Pico32x.pwm_cycle_p; \
  59. if (cycles_diff >= pwm.cycles) \
  60. consume_fifo_do(sh2, m68k_cycles, cycles_diff); \
  61. }
  62. static void consume_fifo_do(SH2 *sh2, unsigned int m68k_cycles,
  63. int sh2_cycles_diff)
  64. {
  65. struct Pico32xMem *mem = Pico32xMem;
  66. unsigned short *fifo_l = mem->pwm_fifo[0];
  67. unsigned short *fifo_r = mem->pwm_fifo[1];
  68. int sum = 0;
  69. if (pwm.cycles == 0 || pwm.doing_fifo)
  70. return;
  71. elprintf(EL_PWM, "pwm: %u: consume %d/%d, %d,%d ptr %d",
  72. m68k_cycles, sh2_cycles_diff, sh2_cycles_diff / pwm.cycles,
  73. Pico32x.pwm_p[0], Pico32x.pwm_p[1], pwm.ptr);
  74. // this is for recursion from dreq1 writes
  75. pwm.doing_fifo = 1;
  76. while (sh2_cycles_diff >= pwm.cycles)
  77. {
  78. sh2_cycles_diff -= pwm.cycles;
  79. if (Pico32x.pwm_p[0] > 0) {
  80. mem->pwm_index[0] = (mem->pwm_index[0]+1) % 4;
  81. Pico32x.pwm_p[0]--;
  82. pwm.current[0] = convert_sample(fifo_l[mem->pwm_index[0]]);
  83. sum |= (u16)pwm.current[0];
  84. }
  85. if (Pico32x.pwm_p[1] > 0) {
  86. mem->pwm_index[1] = (mem->pwm_index[1]+1) % 4;
  87. Pico32x.pwm_p[1]--;
  88. pwm.current[1] = convert_sample(fifo_r[mem->pwm_index[1]]);
  89. sum |= (u16)pwm.current[1];
  90. }
  91. mem->pwm[pwm.ptr * 2 ] = pwm.current[0];
  92. mem->pwm[pwm.ptr * 2 + 1] = pwm.current[1];
  93. pwm.ptr = (pwm.ptr + 1) & (PWM_BUFF_LEN - 1);
  94. if (--Pico32x.pwm_irq_cnt == 0) {
  95. Pico32x.pwm_irq_cnt = pwm.irq_reload;
  96. do_pwm_irq(sh2, m68k_cycles);
  97. } else if (Pico32x.pwm_p[1] == 0 && pwm.irq_state >= PWM_IRQ_LOW) {
  98. // buffer underrun. Reduce reload rate if above programmed setting.
  99. if (pwm.irq_reload > pwm.irq_timer)
  100. pwm.irq_reload--;
  101. pwm.irq_state = PWM_IRQ_LOW;
  102. }
  103. }
  104. Pico32x.pwm_cycle_p = m68k_cycles * 3 - sh2_cycles_diff;
  105. pwm.doing_fifo = 0;
  106. if (sum != 0)
  107. pwm.silent = 0;
  108. }
  109. static int p32x_pwm_schedule_(SH2 *sh2, unsigned int m68k_now)
  110. {
  111. unsigned int pwm_now = m68k_now * 3;
  112. int cycles_diff_sh2;
  113. if (pwm.cycles == 0)
  114. return 0;
  115. cycles_diff_sh2 = pwm_now - Pico32x.pwm_cycle_p;
  116. if (cycles_diff_sh2 >= pwm.cycles)
  117. consume_fifo_do(sh2, m68k_now, cycles_diff_sh2);
  118. if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1))
  119. return 0; // masked by everyone
  120. cycles_diff_sh2 = pwm_now - Pico32x.pwm_cycle_p;
  121. return (Pico32x.pwm_irq_cnt * pwm.cycles
  122. - cycles_diff_sh2) / 3 + 1;
  123. }
  124. void p32x_pwm_schedule(unsigned int m68k_now)
  125. {
  126. int after = p32x_pwm_schedule_(NULL, m68k_now);
  127. if (after != 0)
  128. p32x_event_schedule(m68k_now, P32X_EVENT_PWM, after);
  129. }
  130. void p32x_pwm_schedule_sh2(SH2 *sh2)
  131. {
  132. int after = p32x_pwm_schedule_(sh2, sh2_cycles_done_m68k(sh2));
  133. if (after != 0)
  134. p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after);
  135. }
  136. void p32x_pwm_sync_to_sh2(SH2 *sh2)
  137. {
  138. int m68k_cycles = sh2_cycles_done_m68k(sh2);
  139. consume_fifo(sh2, m68k_cycles);
  140. }
  141. void p32x_pwm_irq_event(unsigned int m68k_now)
  142. {
  143. p32x_pwm_schedule(m68k_now);
  144. }
  145. unsigned int p32x_pwm_read16(u32 a, SH2 *sh2, unsigned int m68k_cycles)
  146. {
  147. unsigned int d = 0;
  148. consume_fifo(sh2, m68k_cycles);
  149. a &= 0x0e;
  150. switch (a/2) {
  151. case 0/2: // control
  152. case 2/2: // cycle
  153. d = Pico32x.regs[(0x30 + a) / 2];
  154. break;
  155. case 4/2: // L ch
  156. if (Pico32x.pwm_p[0] == 3)
  157. d |= P32XP_FULL;
  158. else if (Pico32x.pwm_p[0] == 0)
  159. d |= P32XP_EMPTY;
  160. break;
  161. case 6/2: // R ch
  162. case 8/2: // MONO
  163. if (Pico32x.pwm_p[1] == 3)
  164. d |= P32XP_FULL;
  165. else if (Pico32x.pwm_p[1] == 0)
  166. d |= P32XP_EMPTY;
  167. break;
  168. }
  169. elprintf(EL_PWM, "pwm: %u: r16 %02x %04x (p %d %d)",
  170. m68k_cycles, a, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
  171. return d;
  172. }
  173. void p32x_pwm_write16(u32 a, unsigned int d, SH2 *sh2, unsigned int m68k_cycles)
  174. {
  175. unsigned short *fifo;
  176. int idx;
  177. elprintf(EL_PWM, "pwm: %u: w16 %02x %04x (p %d %d)",
  178. m68k_cycles, a & 0x0e, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
  179. consume_fifo(sh2, m68k_cycles);
  180. a &= 0x0e;
  181. switch (a/2) {
  182. case 0/2: // control
  183. // avoiding pops..
  184. if ((Pico32x.regs[0x30 / 2] & 0x0f) == 0)
  185. Pico32xMem->pwm_fifo[0][0] = Pico32xMem->pwm_fifo[1][0] = 0;
  186. Pico32x.regs[0x30 / 2] = d;
  187. p32x_pwm_ctl_changed();
  188. Pico32x.pwm_irq_cnt = pwm.irq_reload; // ?
  189. break;
  190. case 2/2: // cycle
  191. Pico32x.regs[0x32 / 2] = d & 0x0fff;
  192. p32x_pwm_ctl_changed();
  193. break;
  194. case 8/2: // MONO
  195. case 6/2: // R ch
  196. fifo = Pico32xMem->pwm_fifo[1];
  197. idx = Pico32xMem->pwm_index[1];
  198. if (Pico32x.pwm_p[1] < 3) {
  199. if (Pico32x.pwm_p[1] == 2 && pwm.irq_state >= PWM_IRQ_STOPPED) {
  200. // buffer full. If there was no buffer underrun after last fill,
  201. // try increasing reload rate to reduce IRQs
  202. if (pwm.irq_reload < 3 && pwm.irq_state == PWM_IRQ_HIGH)
  203. pwm.irq_reload ++;
  204. pwm.irq_state = PWM_IRQ_HIGH;
  205. }
  206. Pico32x.pwm_p[1]++;
  207. } else {
  208. // buffer overflow. Some roms always fill the complete buffer even if
  209. // reload rate is set below max. Lock reload rate to programmed setting.
  210. pwm.irq_reload = pwm.irq_timer;
  211. pwm.irq_state = PWM_IRQ_LOCKED;
  212. idx = (idx+1) % 4;
  213. Pico32xMem->pwm_index[1] = idx;
  214. }
  215. fifo[(idx+Pico32x.pwm_p[1]) % 4] = (d - 1) & 0x0fff;
  216. if (a != 8) break; // fallthrough if MONO
  217. case 4/2: // L ch
  218. fifo = Pico32xMem->pwm_fifo[0];
  219. idx = Pico32xMem->pwm_index[0];
  220. if (Pico32x.pwm_p[0] < 3)
  221. Pico32x.pwm_p[0]++;
  222. else {
  223. idx = (idx+1) % 4;
  224. Pico32xMem->pwm_index[0] = idx;
  225. }
  226. fifo[(idx+Pico32x.pwm_p[0]) % 4] = (d - 1) & 0x0fff;
  227. break;
  228. }
  229. }
  230. void p32x_pwm_update(int *buf32, int length, int stereo)
  231. {
  232. short *pwmb;
  233. int step;
  234. int p = 0;
  235. int xmd;
  236. consume_fifo(NULL, SekCyclesDone());
  237. xmd = Pico32x.regs[0x30 / 2] & 0x0f;
  238. if (xmd == 0 || xmd == 0x06 || xmd == 0x09 || xmd == 0x0f)
  239. goto out; // invalid?
  240. if (pwm.silent)
  241. return;
  242. step = (pwm.ptr << 16) / length;
  243. pwmb = Pico32xMem->pwm;
  244. if (stereo)
  245. {
  246. if (xmd == 0x05) {
  247. // normal
  248. while (length-- > 0) {
  249. *buf32++ += pwmb[0];
  250. *buf32++ += pwmb[1];
  251. p += step;
  252. pwmb += (p >> 16) * 2;
  253. p &= 0xffff;
  254. }
  255. }
  256. else if (xmd == 0x0a) {
  257. // channel swap
  258. while (length-- > 0) {
  259. *buf32++ += pwmb[1];
  260. *buf32++ += pwmb[0];
  261. p += step;
  262. pwmb += (p >> 16) * 2;
  263. p &= 0xffff;
  264. }
  265. }
  266. else {
  267. // mono - LMD, RMD specify dst
  268. if (xmd & 0x06) // src is R
  269. pwmb++;
  270. if (xmd & 0x0c) // dst is R
  271. buf32++;
  272. while (length-- > 0) {
  273. *buf32 += *pwmb;
  274. p += step;
  275. pwmb += (p >> 16) * 2;
  276. p &= 0xffff;
  277. buf32 += 2;
  278. }
  279. }
  280. }
  281. else
  282. {
  283. // mostly unused
  284. while (length-- > 0) {
  285. *buf32++ += pwmb[0];
  286. p += step;
  287. pwmb += (p >> 16) * 2;
  288. p &= 0xffff;
  289. }
  290. }
  291. elprintf(EL_PWM, "pwm_update: pwm.ptr %d, len %d, step %04x, done %d",
  292. pwm.ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);
  293. out:
  294. pwm.ptr = 0;
  295. pwm.silent = pwm.current[0] == 0 && pwm.current[1] == 0;
  296. }
  297. void p32x_pwm_state_loaded(void)
  298. {
  299. int cycles_diff_sh2;
  300. p32x_pwm_ctl_changed();
  301. // for old savestates
  302. cycles_diff_sh2 = Pico.t.m68c_cnt * 3 - Pico32x.pwm_cycle_p;
  303. if (cycles_diff_sh2 >= pwm.cycles || cycles_diff_sh2 < 0) {
  304. Pico32x.pwm_irq_cnt = pwm.irq_reload;
  305. Pico32x.pwm_cycle_p = Pico.t.m68c_cnt * 3;
  306. p32x_pwm_schedule(Pico.t.m68c_cnt);
  307. }
  308. }
  309. // vim:shiftwidth=2:ts=2:expandtab