pwm.c 7.8 KB

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