OSDN Git Service

[VM][FMTOWNS][MEMORY] Fix setup around memory banks by I/O 0404h and 0480h.
[csp-qt/common_source_project-fm7.git] / source / src / vm / fmgen / fmgen.h
1 // ---------------------------------------------------------------------------
2 //      FM Sound Generator
3 //      Copyright (C) cisc 1998, 2001.
4 // ---------------------------------------------------------------------------
5 //      $Id: fmgen.h,v 1.37 2003/08/25 13:33:11 cisc Exp $
6
7 #ifndef FM_GEN_H
8 #define FM_GEN_H
9
10 #include "types.h"
11 #include "common.h"
12 #include <memory>
13 // ---------------------------------------------------------------------------
14 //      出力サンプルの型
15 //
16 #define FM_SAMPLETYPE   int32                           // int16 or int32
17
18 // ---------------------------------------------------------------------------
19 //      定数その1
20 //      静的テーブルのサイズ
21
22 #define FM_LFOBITS              8                                       // 変更不可
23 #define FM_TLBITS               7
24
25 // ---------------------------------------------------------------------------
26
27 #define FM_TLENTS               (1 << FM_TLBITS)
28 #define FM_LFOENTS              (1 << FM_LFOBITS)
29 #define FM_TLPOS                (FM_TLENTS/4)
30
31 //      サイン波の精度は 2^(1/256)
32 #define FM_CLENTS               (0x1000 * 2)    // sin + TL + LFO
33
34 // ---------------------------------------------------------------------------
35
36 class CSP_Logger;
37 namespace FM
38 {       
39         //      Types ----------------------------------------------------------------
40         typedef FM_SAMPLETYPE   Sample;
41         typedef int32                   ISample;
42
43         enum OpType { typeN=0, typeM=1 };
44
45         void StoreSample(ISample& dest, int data);
46
47         class Chip;
48         //      Operator -------------------------------------------------------------
49         class Operator
50         {
51         protected:
52                 std::shared_ptr<CSP_Logger> p_logger;
53                 int tmp_eg_phase;
54                 int tmp_ams;
55                 int operators_num;
56         public:
57                 Operator();
58                 void    SetChip(Chip* chip) { chip_ = chip; }
59
60                 static void     MakeTimeTable(uint ratio);
61                 
62                 ISample Calc(ISample in);
63                 ISample CalcL(ISample in);
64                 ISample CalcFB(uint fb);
65                 ISample CalcFBL(uint fb);
66                 ISample CalcN(uint noise);
67                 void    Prepare();
68                 void    KeyOn();
69                 void    KeyOff();
70                 void    Reset();
71                 void    ResetFB();
72                 int             IsOn();
73
74                 void    SetDT(uint dt);
75                 void    SetDT2(uint dt2);
76                 void    SetMULTI(uint multi);
77                 void    SetTL(uint tl, bool csm);
78                 void    SetKS(uint ks);
79                 void    SetAR(uint ar);
80                 void    SetDR(uint dr);
81                 void    SetSR(uint sr);
82                 void    SetRR(uint rr);
83                 void    SetSL(uint sl);
84                 void    SetSSGEC(uint ssgec);
85                 void    SetFNum(uint fnum);
86                 void    SetDPBN(uint dp, uint bn);
87                 void    SetMode(bool modulator);
88                 void    SetAMON(bool on);
89                 void    SetMS(uint ms);
90                 void    Mute(bool);
91                 
92 //              static void SetAML(uint l);
93 //              static void SetPML(uint l);
94
95                 int             Out() { return out_; }
96
97                 int             dbgGetIn2() { return in2_; } 
98                 void    dbgStopPG() { pg_diff_ = 0; pg_diff_lfo_ = 0; }
99                 
100         protected:
101                 bool ProcessState(void *f, bool loading);
102                 
103         private:
104                 typedef uint32 Counter;
105                 
106                 Chip*   chip_;
107                 ISample out_, out2_;
108                 ISample in2_;
109
110         //      Phase Generator ------------------------------------------------------
111                 uint32  PGCalc();
112                 uint32  PGCalcL();
113
114                 uint    dp_;            // ΔP
115                 uint    detune_;                // Detune
116                 uint    detune2_;       // DT2
117                 uint    multiple_;      // Multiple
118                 uint32  pg_count_;      // Phase 現在値
119                 uint32  pg_diff_;       // Phase 差分値
120                 int32   pg_diff_lfo_;   // Phase 差分値 >> x
121
122         //      Envelop Generator ---------------------------------------------------
123                 enum    EGPhase { next, attack, decay, sustain, release, off };
124                 
125                 void    EGCalc();
126                 void    EGStep();
127                 void    ShiftPhase(EGPhase nextphase);
128                 void    SSGShiftPhase(int mode);
129                 void    SetEGRate(uint);
130                 void    EGUpdate();
131                 int             FBCalc(int fb);
132                 ISample LogToLin(uint a);
133
134                 
135                 OpType  type_;          // OP の種類 (M, N...)
136                 uint    bn_;            // Block/Note
137                 int             eg_level_;      // EG の出力値
138                 int             eg_level_on_next_phase_;        // 次の eg_phase_ に移る値
139                 int             eg_count_;              // EG の次の変移までの時間
140                 int             eg_count_diff_; // eg_count_ の差分
141                 int             eg_out_;                // EG+TL を合わせた出力値
142                 int             tl_out_;                // TL 分の出力値
143 //              int             pm_depth_;              // PM depth
144 //              int             am_depth_;              // AM depth
145                 int             eg_rate_;
146                 int             eg_curve_count_;
147                 int             ssg_offset_;
148                 int             ssg_vector_;
149                 int             ssg_phase_;
150
151
152                 uint    key_scale_rate_;                // key scale rate
153                 EGPhase eg_phase_;
154                 volatile uint*  ams_;
155                 uint    ms_;
156                 
157                 uint    tl_;                    // Total Level   (0-127)
158                 uint    tl_latch_;              // Total Level Latch (for CSM mode)
159                 uint    ar_;                    // Attack Rate   (0-63)
160                 uint    dr_;                    // Decay Rate    (0-63)
161                 uint    sr_;                    // Sustain Rate  (0-63)
162                 uint    sl_;                    // Sustain Level (0-127)
163                 uint    rr_;                    // Release Rate  (0-63)
164                 uint    ks_;                    // Keyscale      (0-3)
165                 uint    ssg_type_;      // SSG-Type Envelop Control
166
167                 bool    keyon_;
168                 bool    amon_;          // enable Amplitude Modulation
169                 bool    param_changed_; // パラメータが更新された
170                 bool    mute_;
171                 
172         //      Tables ---------------------------------------------------------------
173                 static Counter rate_table[16];
174                 static uint32 multable[4][16];
175
176                 static const uint8 notetable[128];
177                 static const int8 dttable[256];
178                 static const int8 decaytable1[64][8];
179                 static const int decaytable2[16];
180                 static const int8 attacktable[64][8];
181                 static const int ssgenvtable[8][2][3][2];
182
183                 static uint     sinetable[1024];
184                 static int32 cltable[FM_CLENTS];
185
186                 static bool tablehasmade;
187                 static void MakeTable();
188
189
190
191         //      friends --------------------------------------------------------------
192                 friend class Channel4;
193 //              friend void __stdcall FM_NextPhase(Operator* op);
194
195         public:
196                 int             dbgopout_;
197                 int             dbgpgout_;
198                 static const int32* dbgGetClTable() { return cltable; }
199                 static const uint* dbgGetSineTable() { return sinetable; }
200         };
201         
202         //      4-op Channel ---------------------------------------------------------
203         class Channel4
204         {
205         protected:
206                 int tmp_in_bufptr[3];
207                 int tmp_out_bufptr[3];
208                 int tmp_pms;
209                 int channel4s_num;
210                 std::shared_ptr<CSP_Logger> p_logger;
211         public:
212                 Channel4();
213                 void SetChip(Chip* chip);
214                 void SetType(OpType type);
215                 
216                 ISample Calc();
217                 ISample CalcL();
218                 ISample CalcN(uint noise);
219                 ISample CalcLN(uint noise);
220                 void SetFNum(uint fnum);
221                 void SetFB(uint fb);
222                 void SetKCKF(uint kc, uint kf);
223                 void SetAlgorithm(uint algo);
224                 int Prepare();
225                 void KeyControl(uint key);
226                 void Reset();
227                 void SetMS(uint ms);
228                 void Mute(bool);
229                 void Refresh();
230
231                 void dbgStopPG() { for (int i=0; i<4; i++) op[i].dbgStopPG(); }
232                 
233                 bool ProcessState(void *f, bool loading);
234                 
235         private:
236                 static const uint8 fbtable[8];
237                 uint    fb;
238                 int             buf[4];
239                 int*    in[3];                  // 各 OP の入力ポインタ
240                 int*    out[3];                 // 各 OP の出力ポインタ
241                 int*    pms;
242                 int             algo_;
243                 Chip*   chip_;
244
245                 static void MakeTable();
246
247                 static bool tablehasmade;
248                 static int      kftable[64];
249
250
251         public:
252                 Operator op[4];
253         };
254
255         //      Chip resource
256         class Chip
257         {
258         protected:
259                 std::shared_ptr<CSP_Logger> p_logger;
260                 int chip_num;
261         public:
262                 Chip();
263                 void    SetRatio(uint ratio);
264                 void    SetAML(uint l);
265                 void    SetPML(uint l);
266                 void    SetPMV(int pmv) { pmv_ = pmv; }
267
268                 uint32  GetMulValue(uint dt2, uint mul) { return multable_[dt2][mul]; }
269                 uint    GetAML() { return aml_; }
270                 uint    GetPML() { return pml_; }
271                 int             GetPMV() { return pmv_; }
272                 uint    GetRatio() { return ratio_; }
273
274                 bool ProcessState(void *f, bool loading);
275                 
276         private:
277                 void    MakeTable();
278
279                 uint    ratio_;
280                 uint    aml_;
281                 uint    pml_;
282                 int             pmv_;
283                 OpType  optype_;
284                 uint32  multable_[4][16];
285         };
286         DLL_PREFIX const char *get_libfmgen_version(void);
287 }
288
289 #endif // FM_GEN_H