fmopl.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #ifndef __FMOPL_H_
  2. #define __FMOPL_H_
  3. #define HAS_YM3812 1
  4. /* --- select emulation chips --- */
  5. #define BUILD_YM3812 (HAS_YM3812)
  6. #define BUILD_YM3526 (HAS_YM3526)
  7. #define BUILD_Y8950 (HAS_Y8950)
  8. /* select output bits size of output : 8 or 16 */
  9. #define OPL_SAMPLE_BITS 16
  10. /* compiler dependence */
  11. #ifndef OSD_CPU_H
  12. #define OSD_CPU_H
  13. typedef unsigned char UINT8; /* unsigned 8bit */
  14. typedef unsigned short UINT16; /* unsigned 16bit */
  15. typedef unsigned int UINT32; /* unsigned 32bit */
  16. typedef signed char INT8; /* signed 8bit */
  17. typedef signed short INT16; /* signed 16bit */
  18. typedef signed int INT32; /* signed 32bit */
  19. #endif
  20. #if (OPL_SAMPLE_BITS==16)
  21. typedef INT16 OPLSAMPLE;
  22. #endif
  23. #if (OPL_SAMPLE_BITS==8)
  24. typedef INT8 OPLSAMPLE;
  25. #endif
  26. typedef void (*OPL_TIMERHANDLER)(int channel,double interval_Sec);
  27. typedef void (*OPL_IRQHANDLER)(int param,int irq);
  28. typedef void (*OPL_UPDATEHANDLER)(int param,int min_interval_us);
  29. typedef void (*OPL_PORTHANDLER_W)(int param,unsigned char data);
  30. typedef unsigned char (*OPL_PORTHANDLER_R)(int param);
  31. #define OPL_TYPE_WAVESEL 0x01 /* waveform select */
  32. #define OPL_TYPE_ADPCM 0x02 /* DELTA-T ADPCM unit */
  33. #define OPL_TYPE_KEYBOARD 0x04 /* keyboard interface */
  34. #define OPL_TYPE_IO 0x08 /* I/O port */
  35. /* ---------- Generic interface section ---------- */
  36. #define OPL_TYPE_YM3526 (0)
  37. #define OPL_TYPE_YM3812 (OPL_TYPE_WAVESEL)
  38. #define OPL_TYPE_Y8950 (OPL_TYPE_ADPCM|OPL_TYPE_KEYBOARD|OPL_TYPE_IO)
  39. typedef struct{
  40. UINT32 ar; /* attack rate: AR<<2 */
  41. UINT32 dr; /* decay rate: DR<<2 */
  42. UINT32 rr; /* release rate:RR<<2 */
  43. UINT8 KSR; /* key scale rate */
  44. UINT8 ksl; /* keyscale level */
  45. UINT8 ksr; /* key scale rate: kcode>>KSR */
  46. UINT8 mul; /* multiple: mul_tab[ML] */
  47. /* Phase Generator */
  48. UINT32 Cnt; /* frequency counter */
  49. UINT32 Incr; /* frequency counter step */
  50. UINT8 FB; /* feedback shift value */
  51. INT32 *connect1; /* slot1 output pointer */
  52. INT32 op1_out[2]; /* slot1 output for feedback */
  53. UINT8 CON; /* connection (algorithm) type */
  54. /* Envelope Generator */
  55. UINT8 eg_type; /* percussive/non-percussive mode */
  56. UINT8 state; /* phase type */
  57. UINT32 TL; /* total level: TL << 2 */
  58. INT32 TLL; /* adjusted now TL */
  59. INT32 volume; /* envelope counter */
  60. UINT32 sl; /* sustain level: sl_tab[SL] */
  61. UINT8 eg_sh_ar; /* (attack state) */
  62. UINT8 eg_sel_ar; /* (attack state) */
  63. UINT8 eg_sh_dr; /* (decay state) */
  64. UINT8 eg_sel_dr; /* (decay state) */
  65. UINT8 eg_sh_rr; /* (release state) */
  66. UINT8 eg_sel_rr; /* (release state) */
  67. UINT32 key; /* 0 = KEY OFF, >0 = KEY ON */
  68. /* LFO */
  69. UINT32 AMmask; /* LFO Amplitude Modulation enable mask */
  70. UINT8 vib; /* LFO Phase Modulation enable flag (active high)*/
  71. /* waveform select */
  72. unsigned int wavetable;
  73. } OPL_SLOT;
  74. typedef struct{
  75. OPL_SLOT SLOT[2];
  76. /* phase generator state */
  77. UINT32 block_fnum; /* block+fnum */
  78. UINT32 fc; /* Freq. Increment base */
  79. UINT32 ksl_base; /* KeyScaleLevel Base step */
  80. UINT8 kcode; /* key code (for key scaling) */
  81. } OPL_CH;
  82. /* OPL state */
  83. typedef struct fm_opl_f {
  84. /* FM channel slots */
  85. OPL_CH P_CH[9]; /* OPL/OPL2 chips have 9 channels*/
  86. UINT32 eg_cnt; /* global envelope generator counter */
  87. UINT32 eg_timer; /* global envelope generator counter works at frequency = chipclock/72 */
  88. UINT32 eg_timer_add; /* step of eg_timer */
  89. UINT32 eg_timer_overflow; /* envelope generator timer overlfows every 1 sample (on real chip) */
  90. UINT8 rhythm; /* Rhythm mode */
  91. UINT32 fn_tab[1024]; /* fnumber->increment counter */
  92. /* LFO */
  93. UINT8 lfo_am_depth;
  94. UINT8 lfo_pm_depth_range;
  95. UINT32 lfo_am_cnt;
  96. UINT32 lfo_am_inc;
  97. UINT32 lfo_pm_cnt;
  98. UINT32 lfo_pm_inc;
  99. UINT32 noise_rng; /* 23 bit noise shift register */
  100. UINT32 noise_p; /* current noise 'phase' */
  101. UINT32 noise_f; /* current noise period */
  102. UINT8 wavesel; /* waveform select enable flag */
  103. int T[2]; /* timer counters */
  104. UINT8 st[2]; /* timer enable */
  105. #if BUILD_Y8950
  106. /* Delta-T ADPCM unit (Y8950) */
  107. YM_DELTAT *deltat;
  108. /* Keyboard and I/O ports interface */
  109. UINT8 portDirection;
  110. UINT8 portLatch;
  111. OPL_PORTHANDLER_R porthandler_r;
  112. OPL_PORTHANDLER_W porthandler_w;
  113. int port_param;
  114. OPL_PORTHANDLER_R keyboardhandler_r;
  115. OPL_PORTHANDLER_W keyboardhandler_w;
  116. int keyboard_param;
  117. #endif
  118. /* external event callback handlers */
  119. OPL_TIMERHANDLER TimerHandler; /* TIMER handler */
  120. int TimerParam; /* TIMER parameter */
  121. OPL_IRQHANDLER IRQHandler; /* IRQ handler */
  122. int IRQParam; /* IRQ parameter */
  123. OPL_UPDATEHANDLER UpdateHandler;/* stream update handler */
  124. int UpdateParam; /* stream update parameter */
  125. UINT8 type; /* chip type */
  126. UINT8 address; /* address register */
  127. UINT8 status; /* status flag */
  128. UINT8 statusmask; /* status mask */
  129. UINT8 mode; /* Reg.08 : CSM,notesel,etc. */
  130. int clock; /* master clock (Hz) */
  131. int rate; /* sampling rate (Hz) */
  132. double freqbase; /* frequency base */
  133. double TimerBase; /* Timer base time (==sampling time)*/
  134. } FM_OPL;
  135. extern FM_OPL *OPLCreate(int type, int clock, int rate);
  136. extern void OPLDestroy(FM_OPL *OPL);
  137. extern int OPLWrite(FM_OPL *OPL,int a,int v);
  138. extern void OPLResetChip(FM_OPL *OPL);
  139. #if BUILD_YM3812
  140. int YM3812Init(int num, int clock, int rate);
  141. void YM3812Shutdown(void);
  142. void YM3812ResetChip(int which);
  143. int YM3812Write(int which, int a, int v);
  144. unsigned char YM3812Read(int which, int a);
  145. int YM3812TimerOver(int which, int c);
  146. void YM3812UpdateOne(FM_OPL *OPL, INT16 *buffer, int length);
  147. void YM3812SetTimerHandler(int which, OPL_TIMERHANDLER TimerHandler, int channelOffset);
  148. void YM3812SetIRQHandler(int which, OPL_IRQHANDLER IRQHandler, int param);
  149. void YM3812SetUpdateHandler(int which, OPL_UPDATEHANDLER UpdateHandler, int param);
  150. #endif
  151. #if BUILD_YM3526
  152. /*
  153. ** Initialize YM3526 emulator(s).
  154. **
  155. ** 'num' is the number of virtual YM3526's to allocate
  156. ** 'clock' is the chip clock in Hz
  157. ** 'rate' is sampling rate
  158. */
  159. int YM3526Init(int num, int clock, int rate);
  160. /* shutdown the YM3526 emulators*/
  161. void YM3526Shutdown(void);
  162. void YM3526ResetChip(int which);
  163. int YM3526Write(int which, int a, int v);
  164. unsigned char YM3526Read(int which, int a);
  165. int YM3526TimerOver(int which, int c);
  166. /*
  167. ** Generate samples for one of the YM3526's
  168. **
  169. ** 'which' is the virtual YM3526 number
  170. ** '*buffer' is the output buffer pointer
  171. ** 'length' is the number of samples that should be generated
  172. */
  173. void YM3526UpdateOne(int which, INT16 *buffer, int length);
  174. void YM3526SetTimerHandler(int which, OPL_TIMERHANDLER TimerHandler, int channelOffset);
  175. void YM3526SetIRQHandler(int which, OPL_IRQHANDLER IRQHandler, int param);
  176. void YM3526SetUpdateHandler(int which, OPL_UPDATEHANDLER UpdateHandler, int param);
  177. #endif
  178. #if BUILD_Y8950
  179. /* Y8950 port handlers */
  180. void Y8950SetPortHandler(int which, OPL_PORTHANDLER_W PortHandler_w, OPL_PORTHANDLER_R PortHandler_r, int param);
  181. void Y8950SetKeyboardHandler(int which, OPL_PORTHANDLER_W KeyboardHandler_w, OPL_PORTHANDLER_R KeyboardHandler_r, int param);
  182. void Y8950SetDeltaTMemory(int which, void * deltat_mem_ptr, int deltat_mem_size );
  183. int Y8950Init (int num, int clock, int rate);
  184. void Y8950Shutdown (void);
  185. void Y8950ResetChip (int which);
  186. int Y8950Write (int which, int a, int v);
  187. unsigned char Y8950Read (int which, int a);
  188. int Y8950TimerOver (int which, int c);
  189. void Y8950UpdateOne (int which, INT16 *buffer, int length);
  190. void Y8950SetTimerHandler (int which, OPL_TIMERHANDLER TimerHandler, int channelOffset);
  191. void Y8950SetIRQHandler (int which, OPL_IRQHANDLER IRQHandler, int param);
  192. void Y8950SetUpdateHandler (int which, OPL_UPDATEHANDLER UpdateHandler, int param);
  193. #endif
  194. #endif /* __FMOPL_H_ */