Human68k CUI emulator with sound.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ym2151.h 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. ** File: ym2151.h - header file for software implementation of YM2151
  3. ** FM Operator Type-M(OPM)
  4. **
  5. ** (c) 1997-2002 Jarek Burczynski (s0246@poczta.onet.pl, bujar@mame.net)
  6. ** Some of the optimizing ideas by Tatsuyuki Satoh
  7. **
  8. ** Version 2.150 final beta May, 11th 2002
  9. **
  10. **
  11. ** I would like to thank following people for making this project possible:
  12. **
  13. ** Beauty Planets - for making a lot of real YM2151 samples and providing
  14. ** additional informations about the chip. Also for the time spent making
  15. ** the samples and the speed of replying to my endless requests.
  16. **
  17. ** Shigeharu Isoda - for general help, for taking time to scan his YM2151
  18. ** Japanese Manual first of all, and answering MANY of my questions.
  19. **
  20. ** Nao - for giving me some info about YM2151 and pointing me to Shigeharu.
  21. ** Also for creating fmemu (which I still use to test the emulator).
  22. **
  23. ** Aaron Giles and Chris Hardy - they made some samples of one of my favourite
  24. ** arcade games so I could compare it to my emulator.
  25. **
  26. ** Bryan McPhail and Tim (powerjaw) - for making some samples.
  27. **
  28. ** Ishmair - for the datasheet and motivation.
  29. */
  30. #pragma once
  31. #include <stdint.h>
  32. /* struct describing a single operator */
  33. struct ym2151_operator {
  34. uint32_t phase; /* accumulated operator phase */
  35. uint32_t freq; /* operator frequency count */
  36. int32_t dt1; /* current DT1 (detune 1 phase inc/decrement) value */
  37. uint32_t mul; /* frequency count multiply */
  38. uint32_t dt1_i; /* DT1 index * 32 */
  39. uint32_t dt2; /* current DT2 (detune 2) value */
  40. signed int *connect; /* operator output 'direction' */
  41. /* only M1 (operator 0) is filled with this data: */
  42. signed int *mem_connect; /* where to put the delayed sample (MEM) */
  43. int32_t mem_value; /* delayed sample (MEM) value */
  44. /* channel specific data; note: each operator number 0 contains channel specific data */
  45. uint32_t fb_shift; /* feedback shift value for operators 0 in each channel */
  46. int32_t fb_out_curr; /* operator feedback value (used only by operators 0) */
  47. int32_t fb_out_prev; /* previous feedback value (used only by operators 0) */
  48. uint32_t kc; /* channel KC (copied to all operators) */
  49. uint32_t kc_i; /* just for speedup */
  50. uint32_t pms; /* channel PMS */
  51. uint32_t ams; /* channel AMS */
  52. /* end of channel specific data */
  53. uint32_t AMmask; /* LFO Amplitude Modulation enable mask */
  54. uint32_t state; /* Envelope state: 4-attack(AR) 3-decay(D1R) 2-sustain(D2R) 1-release(RR) 0-off */
  55. uint8_t eg_sh_ar; /* (attack state) */
  56. uint8_t eg_sel_ar; /* (attack state) */
  57. uint32_t tl; /* Total attenuation Level */
  58. int32_t volume; /* current envelope attenuation level */
  59. uint8_t eg_sh_d1r; /* (decay state) */
  60. uint8_t eg_sel_d1r; /* (decay state) */
  61. uint32_t d1l; /* envelope switches to sustain state after reaching this level */
  62. uint8_t eg_sh_d2r; /* (sustain state) */
  63. uint8_t eg_sel_d2r; /* (sustain state) */
  64. uint8_t eg_sh_rr; /* (release state) */
  65. uint8_t eg_sel_rr; /* (release state) */
  66. uint32_t key; /* 0=last key was KEY OFF, 1=last key was KEY ON */
  67. uint32_t ks; /* key scale */
  68. uint32_t ar; /* attack rate */
  69. uint32_t d1r; /* decay rate */
  70. uint32_t d2r; /* sustain rate */
  71. uint32_t rr; /* release rate */
  72. uint32_t reserved0; /**/
  73. uint32_t reserved1; /**/
  74. };
  75. struct ym2151 {
  76. struct ym2151_operator oper[32]; /* the 32 operators */
  77. uint32_t pan[16]; /* channels output masks (0xffffffff = enable) */
  78. uint8_t Muted[8]; /* used for muting */
  79. uint32_t eg_cnt; /* global envelope generator counter */
  80. uint32_t eg_timer; /* global envelope generator counter works at frequency = chipclock/64/3 */
  81. uint32_t eg_timer_add; /* step of eg_timer */
  82. uint32_t eg_timer_overflow; /* envelope generator timer overlfows every 3 samples (on real chip) */
  83. uint32_t lfo_phase; /* accumulated LFO phase (0 to 255) */
  84. uint32_t lfo_timer; /* LFO timer */
  85. uint32_t lfo_timer_add; /* step of lfo_timer */
  86. uint32_t lfo_overflow; /* LFO generates new output when lfo_timer reaches this value */
  87. uint32_t lfo_counter; /* LFO phase increment counter */
  88. uint32_t lfo_counter_add; /* step of lfo_counter */
  89. uint8_t lfo_wsel; /* LFO waveform (0-saw, 1-square, 2-triangle, 3-random noise) */
  90. uint8_t amd; /* LFO Amplitude Modulation Depth */
  91. int8_t pmd; /* LFO Phase Modulation Depth */
  92. uint32_t lfa; /* LFO current AM output */
  93. int32_t lfp; /* LFO current PM output */
  94. uint8_t test; /* TEST register */
  95. uint8_t ct; /* output control pins (bit1-CT2, bit0-CT1) */
  96. uint32_t noise; /* noise enable/period register (bit 7 - noise enable, bits 4-0 - noise period */
  97. uint32_t noise_rng; /* 17 bit noise shift register */
  98. uint32_t noise_p; /* current noise 'phase'*/
  99. uint32_t noise_f; /* current noise period */
  100. uint32_t csm_req; /* CSM KEY ON / KEY OFF sequence request */
  101. uint32_t irq_enable; /* IRQ enable for timer B (bit 3) and timer A (bit 2); bit 7 - CSM mode (keyon to all slots, everytime timer A overflows) */
  102. uint32_t status; /* chip status (BUSY, IRQ Flags) */
  103. uint8_t connect[8]; /* channels connections */
  104. /* Frequency-deltas to get the closest frequency possible.
  105. * There are 11 octaves because of DT2 (max 950 cents over base frequency)
  106. * and LFO phase modulation (max 800 cents below AND over base frequency)
  107. * Summary: octave explanation
  108. * 0 note code - LFO PM
  109. * 1 note code
  110. * 2 note code
  111. * 3 note code
  112. * 4 note code
  113. * 5 note code
  114. * 6 note code
  115. * 7 note code
  116. * 8 note code
  117. * 9 note code + DT2 + LFO PM
  118. * 10 note code + DT2 + LFO PM
  119. */
  120. uint32_t freq[11*768]; /* 11 octaves, 768 'cents' per octave */
  121. /* Frequency deltas for DT1. These deltas alter operator frequency
  122. * after it has been taken from frequency-deltas table.
  123. */
  124. int32_t dt1_freq[8*32]; /* 8 DT1 levels, 32 KC values */
  125. uint32_t noise_tab[32]; /* 17bit Noise Generator periods */
  126. //void (*irqhandler)(const device_config *device, int irq); /* IRQ function handler */
  127. //write8_device_func porthandler; /* port write function handler */
  128. //const device_config *device;
  129. unsigned int clock; /* chip clock in Hz (passed from 2151intf.c) */
  130. unsigned int sampfreq; /* sampling frequency in Hz (passed from 2151intf.c) */
  131. /* these variables stay here for speedup purposes only */
  132. signed int chanout[8];
  133. signed int m2,c1,c2; /* Phase Modulation input for operators 2,3,4 */
  134. signed int mem; /* one sample delay memory */
  135. };
  136. /*
  137. * Initialize YM2151 emulator.
  138. *
  139. * 'clock' is the chip clock in Hz
  140. * 'rate' is sampling rate
  141. */
  142. void ym2151_init(struct ym2151 *ym2151, int clock, int rate);
  143. /*
  144. * Allocate and initialize YM2151 emulator instance.
  145. * @see ym2151_init
  146. */
  147. struct ym2151 *ym2151_new(int clock, int rate);
  148. /* shutdown the YM2151 emulators*/
  149. void ym2151_shutdown(struct ym2151 *ym2151);
  150. /* reset all chip registers for YM2151 number 'num'*/
  151. void ym2151_reset_chip(struct ym2151 *ym2151);
  152. /*
  153. * Generate samples
  154. *
  155. * '**buffers' is table of pointers to the buffers: left and right
  156. * 'length' is the number of samples that should be generated
  157. */
  158. void ym2151_update_one(struct ym2151 *ym2151, int16_t **buffers, int length);
  159. /* write 'v' to register 'r' on YM2151 */
  160. void ym2151_write_reg(struct ym2151 *ym2151, int r, int v);
  161. /* read status register on YM2151 */
  162. int ym2151_read_status(struct ym2151 *ym2151);
  163. /* set interrupt handler on YM2151 chip number 'n'*/
  164. //void ym2151_set_irq_handler(void *chip, void (*handler)(int irq));
  165. /* set port write handler on YM2151 chip number 'n'*/
  166. //void ym2151_set_port_write_handler(void *chip, write8_device_func handler);
  167. /* refresh chip when load state */
  168. void ym2151_postload(struct ym2151 *ym2151);
  169. void ym2151_set_mutemask(struct ym2151 *ym2151, uint32_t MuteMask);