/ source / audiolib / include / opl3.h
opl3.h
  1  //-------------------------------------------------------------------------
  2  /*
  3  Copyright (C) 2013-2019 Nuke.YKT
  4  
  5  This file is part of NBlood.
  6  
  7  NBlood is free software; you can redistribute it and/or
  8  modify it under the terms of the GNU General Public License version 2
  9  as published by the Free Software Foundation.
 10  
 11  This program is distributed in the hope that it will be useful,
 12  but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 14  
 15  See the GNU General Public License for more details.
 16  
 17  You should have received a copy of the GNU General Public License
 18  along with this program; if not, write to the Free Software
 19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 20  */
 21  //-------------------------------------------------------------------------
 22  //
 23  //  Nuked OPL3 emulator.
 24  //  Thanks:
 25  //      MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh):
 26  //          Feedback and Rhythm part calculation information.
 27  //      forums.submarine.org.uk(carbon14, opl3):
 28  //          Tremolo and phase generator calculation information.
 29  //      OPLx decapsulated(Matthew Gambrell, Olli Niemitalo):
 30  //          OPL2 ROMs.
 31  //      siliconpr0n.org(John McMaster, digshadow):
 32  //          YMF262 and VRC VII decaps and die shots.
 33  //
 34  // version: 1.8
 35  //
 36  
 37  #ifndef OPL_OPL3_H
 38  #define OPL_OPL3_H
 39  
 40  #include "compat.h"
 41  
 42  #ifdef __cplusplus
 43  extern "C" {
 44  #endif
 45  
 46  #ifndef OPL_ENABLE_STEREOEXT
 47  #define OPL_ENABLE_STEREOEXT 1
 48  #endif
 49  
 50  #define OPL_WRITEBUF_SIZE   1024
 51  EDUKE32_STATIC_ASSERT(isPow2(OPL_WRITEBUF_SIZE));
 52  #define OPL_WRITEBUF_DELAY  2
 53  
 54  typedef struct _opl3_slot opl3_slot;
 55  typedef struct _opl3_channel opl3_channel;
 56  typedef struct _opl3_chip opl3_chip;
 57  
 58  struct _opl3_slot {
 59      opl3_channel *channel;
 60      opl3_chip *chip;
 61      int16_t *mod;
 62      uint8_t *trem;
 63      uint32_t pg_reset;
 64      uint32_t pg_phase;
 65      uint16_t pg_phase_out;
 66      int16_t out;
 67      int16_t fbmod;
 68      int16_t prout;
 69      uint16_t eg_rout;
 70      uint16_t eg_out;
 71      uint8_t eg_inc;
 72      uint8_t eg_gen;
 73      uint8_t eg_rate;
 74      uint8_t eg_ksl;
 75      uint8_t reg_vib;
 76      uint8_t reg_type;
 77      uint8_t reg_ksr;
 78      uint8_t reg_mult;
 79      uint8_t reg_ksl;
 80      uint8_t reg_tl;
 81      uint8_t reg_ar;
 82      uint8_t reg_dr;
 83      uint8_t reg_sl;
 84      uint8_t reg_rr;
 85      uint8_t reg_wf;
 86      uint8_t key;
 87      uint8_t slot_num;
 88  };
 89  
 90  struct _opl3_channel {
 91      opl3_slot *slots[2];
 92      opl3_channel *pair;
 93      opl3_chip *chip;
 94      int16_t *out[4];
 95  
 96  #if OPL_ENABLE_STEREOEXT
 97      int32_t leftpan;
 98      int32_t rightpan;
 99  #endif
100  
101      uint8_t chtype;
102      uint16_t f_num;
103      uint8_t block;
104      uint8_t fb;
105      uint8_t con;
106      uint8_t alg;
107      uint8_t ksv;
108      uint16_t cha, chb;
109      uint8_t ch_num;
110  };
111  
112  typedef struct _opl3_writebuf {
113      uint64_t time;
114      uint16_t reg;
115      uint8_t data;
116  } opl3_writebuf;
117  
118  struct _opl3_chip {
119      opl3_channel channel[18];
120      opl3_slot slot[36];
121      uint16_t timer;
122      uint64_t eg_timer;
123      uint8_t eg_timerrem;
124      uint8_t eg_state;
125      uint8_t eg_add;
126      uint8_t newm;
127      uint8_t nts;
128      uint8_t rhy;
129      uint8_t vibpos;
130      uint8_t vibshift;
131      uint8_t tremolo;
132      uint8_t tremolopos;
133      uint8_t tremoloshift;
134      uint32_t noise;
135      int16_t zeromod;
136      int32_t mixbuff[2];
137      uint8_t rm_hh_bit2;
138      uint8_t rm_hh_bit3;
139      uint8_t rm_hh_bit7;
140      uint8_t rm_hh_bit8;
141      uint8_t rm_tc_bit3;
142      uint8_t rm_tc_bit5;
143  
144  #if OPL_ENABLE_STEREOEXT
145      uint8_t stereoext;
146  #endif
147  
148      /* OPL3L */
149      int32_t rateratio;
150      int32_t samplecnt;
151      int16_t oldsamples[2];
152      int16_t samples[2];
153  
154      uint64_t writebuf_samplecnt;
155      uint32_t writebuf_cur;
156      uint32_t writebuf_last;
157      uint64_t writebuf_lasttime;
158      opl3_writebuf writebuf[OPL_WRITEBUF_SIZE];
159  };
160  
161  void OPL3_Generate(opl3_chip *chip, int16_t *buf);
162  void OPL3_GenerateResampled(opl3_chip *chip, int16_t *buf);
163  void OPL3_Reset(opl3_chip *chip, uint32_t samplerate);
164  void OPL3_WriteReg(opl3_chip *chip, uint16_t reg, uint8_t v);
165  void OPL3_WriteRegBuffered(opl3_chip *chip, uint16_t reg, uint8_t v);
166  void OPL3_GenerateStream(opl3_chip *chip, int16_t *sndptr, uint32_t numsamples);
167  
168  #ifdef __cplusplus
169  }
170  #endif
171  
172  #endif