Subversion Repositories pentevo

Rev

Rev 783 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed | ?url?

  1. /*
  2. typedef unsigned int UINT32;
  3. typedef signed int INT32;
  4. typedef unsigned short UINT16;
  5. typedef signed short INT16;
  6. typedef unsigned char UINT8;
  7. typedef signed char INT8;
  8. */
  9. typedef INT16 stream_sample_t;
  10. typedef stream_sample_t FMSAMPLE;
  11. #ifndef EMUL2203_H
  12. #define EMUL2203_H
  13.  
  14. /* select bit size of output : 8 or 16 */
  15. #define FM_SAMPLE_BITS 16
  16.  
  17. /* select timer system internal or external */
  18. #define FM_INTERNAL_TIMER 0
  19.  
  20. /* --- speedup optimize --- */
  21. /* busy flag enulation , The definition of FM_GET_TIME_NOW() is necessary. */
  22. #define FM_BUSY_FLAG_SUPPORT 1
  23.  
  24.  
  25. /* struct describing a single operator (SLOT) */
  26. typedef struct
  27. {
  28.         INT32   *DT;            /* detune          :dt_tab[DT] */
  29.         UINT8   KSR;            /* key scale rate  :3-KSR */
  30.         UINT32  ar;             /* attack rate  */
  31.         UINT32  d1r;            /* decay rate   */
  32.         UINT32  d2r;            /* sustain rate */
  33.         UINT32  rr;             /* release rate */
  34.         UINT8   ksr;            /* key scale rate  :kcode>>(3-KSR) */
  35.         UINT32  mul;            /* multiple        :ML_TABLE[ML] */
  36.  
  37.         /* Phase Generator */
  38.         UINT32  phase;          /* phase counter */
  39.         UINT32  Incr;           /* phase step */
  40.  
  41.         /* Envelope Generator */
  42.         UINT8   state;          /* phase type */
  43.         UINT32  tl;             /* total level: TL << 3 */
  44.         INT32   volume;         /* envelope counter */
  45.         UINT32  sl;             /* sustain level:sl_table[SL] */
  46.         UINT32  vol_out;        /* current output from EG circuit (without AM from LFO) */
  47.  
  48.         UINT8   eg_sh_ar;       /*  (attack state) */
  49.         UINT8   eg_sel_ar;      /*  (attack state) */
  50.         UINT8   eg_sh_d1r;      /*  (decay state) */
  51.         UINT8   eg_sel_d1r;     /*  (decay state) */
  52.         UINT8   eg_sh_d2r;      /*  (sustain state) */
  53.         UINT8   eg_sel_d2r;     /*  (sustain state) */
  54.         UINT8   eg_sh_rr;       /*  (release state) */
  55.         UINT8   eg_sel_rr;      /*  (release state) */
  56.  
  57.         UINT8   ssg;            /* SSG-EG waveform */
  58.         UINT8   ssgn;           /* SSG-EG negated output */
  59.  
  60.         UINT32  key;            /* 0=last key was KEY OFF, 1=KEY ON */
  61.  
  62. } FM_SLOT;
  63.  
  64. typedef struct
  65. {
  66.         FM_SLOT SLOT[4];        /* four SLOTs (operators) */
  67.  
  68.         UINT8   ALGO;           /* algorithm */
  69.         UINT8   FB;             /* feedback shift */
  70.         INT32   op1_out[2];     /* op1 output for feedback */
  71.  
  72.         INT32   *connect1;      /* SLOT1 output pointer */
  73.         INT32   *connect2;      /* SLOT2 output pointer */
  74.         INT32   *connect3;      /* SLOT3 output pointer */
  75.         INT32   *connect4;      /* SLOT4 output pointer */
  76.  
  77.         INT32   *mem_connect;/* where to put the delayed sample (MEM) */
  78.         INT32   mem_value;      /* delayed sample (MEM) value */
  79.  
  80.         INT32   pms;            /* channel PMS */
  81.         UINT8   ams;            /* channel AMS */
  82.  
  83.         UINT32  fc;             /* fnum,blk:adjusted to sample rate */
  84.         UINT8   kcode;          /* key code:                        */
  85.         UINT32  block_fnum;     /* current blk/fnum value for this slot (can be different betweeen slots of one channel in 3slot mode) */
  86. } FM_CH;
  87.  
  88. typedef struct
  89. {
  90.         void *  param;          /* this chip parameter  */
  91.         int     clock;          /* master clock  (Hz)   */
  92.         int     SSGclock;       /* clock for SSG (Hz)   */
  93.         int     rate;           /* sampling rate (Hz)   */
  94.         double  freqbase;       /* frequency base       */
  95.         double  TimerBase;      /* Timer base time      */
  96. #if FM_BUSY_FLAG_SUPPORT
  97.         double  BusyExpire;     /* ExpireTime of Busy clear */
  98. #endif
  99.         UINT8   address;        /* address register     */
  100.         UINT8   irq;            /* interrupt level      */
  101.         UINT8   irqmask;        /* irq mask             */
  102.         UINT8   status;         /* status flag          */
  103.         UINT32  mode;           /* mode  CSM / 3SLOT    */
  104.         UINT8   prescaler_sel;/* prescaler selector */
  105.         UINT8   fn_h;           /* freq latch           */
  106.         int     TA;             /* timer a              */
  107.         int     TAC;            /* timer a counter      */
  108.         UINT8   TB;             /* timer b              */
  109.         int     TBC;            /* timer b counter      */
  110.         /* local time tables */
  111.         INT32   dt_tab[8][32];/* DeTune table       */
  112.         /* Extention Timer and IRQ handler */
  113. //      FM_TIMERHANDLER Timer_Handler;
  114. //      FM_IRQHANDLER   IRQ_Handler;
  115. //      const struct ssg_callbacks *SSG;
  116. } FM_ST;
  117.  
  118. /***********************************************************/
  119. /* OPN unit                                                */
  120. /***********************************************************/
  121.  
  122. /* OPN 3slot struct */
  123. typedef struct
  124. {
  125.         UINT32  fc[3];                  /* fnum3,blk3: calculated */
  126.         UINT8   fn_h;                   /* freq3 latch */
  127.         UINT8   kcode[3];               /* key code */
  128.         UINT32  block_fnum[3];  /* current fnum value for this slot (can be different betweeen slots of one channel in 3slot mode) */
  129. } FM_3SLOT;
  130.  
  131. /* OPN/A/B common state */
  132. typedef struct
  133. {
  134.         UINT8   type;                   /* chip type */
  135.         FM_ST   ST;                     /* general state */
  136.         FM_3SLOT SL3;                   /* 3 slot mode state */
  137.         FM_CH   *P_CH;                  /* pointer of CH */
  138.  
  139.         UINT32  eg_cnt;                 /* global envelope generator counter */
  140.         UINT32  eg_timer;               /* global envelope generator counter works at frequency = chipclock/64/3 */
  141.         UINT32  eg_timer_add;   /* step of eg_timer */
  142.         UINT32  eg_timer_overflow;/* envelope generator timer overlfows every 3 samples (on real chip) */
  143.  
  144.  
  145.         /* there are 2048 FNUMs that can be generated using FNUM/BLK registers
  146.         but LFO works with one more bit of a precision so we really need 4096 elements */
  147.  
  148.         UINT32  fn_table[2048]; /* fnumber->increment counter */
  149.  
  150. } FM_OPN;
  151.  
  152. /* here's the virtual YM2203(OPN) */
  153. typedef struct
  154. {
  155.         UINT8 REGS[256];                /* registers         */
  156.         FM_OPN OPN;                             /* OPN state         */
  157.         FM_CH CH[3];                    /* channel state     */
  158. } YM2203;
  159.  
  160. #define FM_GET_TIME_NOW() 0
  161. //timer_get_time()
  162.  
  163. #ifndef PI
  164. #define PI 3.14159265358979323846
  165. #endif
  166.  
  167. /* -------------------- YM2203(OPN) Interface -------------------- */
  168.  
  169. /*
  170. ** Initialize YM2203 emulator(s).
  171. **
  172. ** 'num'           is the number of virtual YM2203's to allocate
  173. ** 'baseclock'
  174. ** 'rate'          is sampling rate
  175. ** 'TimerHandler'  timer callback handler when timer start and clear
  176. ** 'IRQHandler'    IRQ callback handler when changed IRQ level
  177. ** return      0 = success
  178. */
  179. void * YM2203Init(void *param, int index, int baseclock, int rate
  180. //                      ,FM_TIMERHANDLER TimerHandler,FM_IRQHANDLER IRQHandler, const struct ssg_callbacks *ssg
  181.                            );
  182.  
  183. /*
  184. ** shutdown the YM2203 emulators
  185. */
  186. void YM2203Shutdown(void *chip);
  187.  
  188. /*
  189. ** reset all chip registers for YM2203 number 'num'
  190. */
  191. void YM2203ResetChip(void *chip);
  192.  
  193. /*
  194. ** update one of chip
  195. */
  196. void YM2203UpdateOne(void *chip, FMSAMPLE *buffer, int length);
  197.  
  198. /*
  199. ** Write
  200. ** return : InterruptLevel
  201. */
  202. int YM2203Write(void *chip,int a,unsigned char v);
  203.  
  204. /*
  205. ** Read
  206. ** return : InterruptLevel
  207. */
  208. unsigned char YM2203Read(void *chip,int a);
  209.  
  210. /*
  211. **  Timer OverFlow
  212. */
  213. int YM2203TimerOver(void *chip, int c);
  214.  
  215. /*
  216. **  State Save
  217. */
  218. void YM2203Postload(void *chip);
  219.  
  220. void YM2203_save_state(void *chip, int index);
  221. void OPNPrescaler_w(FM_OPN *OPN , int addr, int pre_divider);
  222. #endif
  223.