OSDN Git Service

[VM][General] Merge upstream 2016-02-13. Still don't implement OSD/Gui part of joysti...
[csp-qt/common_source_project-fm7.git] / source / src / vm / sn76489an.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.08.18 -
6
7         [ SN76489AN ]
8 */
9
10 #include "sn76489an.h"
11
12 #ifdef HAS_SN76489
13 // SN76489
14 #define NOISE_FB        0x4000
15 #define NOISE_DST_TAP   1
16 #define NOISE_SRC_TAP   2
17 #else
18 // SN76489A, SN76496
19 #define NOISE_FB        0x10000
20 #define NOISE_DST_TAP   4
21 #define NOISE_SRC_TAP   8
22 #endif
23 #define NOISE_MODE      ((regs[6] & 4) ? 1 : 0)
24
25 void SN76489AN::initialize()
26 {
27         mute = false;
28         cs = we = true;
29 }
30
31 void SN76489AN::reset()
32 {
33         for(int i = 0; i < 4; i++) {
34                 ch[i].count = 0;
35                 ch[i].period = 1;
36                 ch[i].volume = 0;
37                 ch[i].signal = false;
38         }
39         for(int i = 0; i < 8; i += 2) {
40                 regs[i + 0] = 0;
41                 regs[i + 1] = 0x0f;     // volume = 0
42         }
43         noise_gen = NOISE_FB;
44         ch[3].signal = false;
45 }
46
47 void SN76489AN::write_io8(uint32 addr, uint32 data)
48 {
49         if(data & 0x80) {
50                 index = (data >> 4) & 7;
51                 int c = index >> 1;
52                 
53                 switch(index & 7) {
54                 case 0: case 2: case 4:
55                         // tone : frequency
56                         regs[index] = (regs[index] & 0x3f0) | (data & 0x0f);
57                         ch[c].period = regs[index] ? regs[index] : 0x400;
58 //                      ch[c].count = 0;
59                         break;
60                 case 1: case 3: case 5: case 7:
61                         // tone / noise : volume
62                         regs[index] = data & 0x0f;
63                         ch[c].volume = volume_table[data & 0x0f];
64                         break;
65                 case 6:
66                         // noise : frequency, mode
67                         regs[6] = data;
68                         data &= 3;
69                         ch[3].period = (data == 3) ? (ch[2].period << 1) : (1 << (data + 5));
70 //                      ch[3].count = 0;
71                         noise_gen = NOISE_FB;
72                         ch[3].signal = false;
73                         break;
74                 }
75         } else {
76                 int c = index >> 1;
77                 
78                 switch(index & 0x07) {
79                 case 0: case 2: case 4:
80                         // tone : frequency
81                         regs[index] = (regs[index] & 0x0f) | (((uint16)data << 4) & 0x3f0);
82                         ch[c].period = regs[index] ? regs[index] : 0x400;
83 //                      ch[c].count = 0;
84                         // update noise shift frequency
85                         if(index == 4 && (regs[6] & 3) == 3) {
86                                 ch[3].period = ch[2].period << 1;
87                         }
88                         break;
89                 }
90         }
91 }
92
93 void SN76489AN::write_signal(int id, uint32 data, uint32 mask)
94 {
95         if(id == SIG_SN76489AN_MUTE) {
96                 mute = ((data & mask) != 0);
97         } else if(id == SIG_SN76489AN_DATA) {
98                 val = data & mask;
99         } else if(id == SIG_SN76489AN_CS) {
100                 bool next = ((data & mask) != 0);
101                 if(cs != next) {
102                         if(!(cs = next) && !we) {
103                                 write_io8(0, val);
104                         }
105                 }
106         } else if(id == SIG_SN76489AN_CS) {
107                 bool next = ((data & mask) != 0);
108                 if(cs != next) {
109                         cs = next;
110                         if(!cs && !we) {
111                                 write_io8(0, val);
112                         }
113                 }
114         } else if(id == SIG_SN76489AN_WE) {
115                 bool next = ((data & mask) != 0);
116                 if(we != next) {
117                         we = next;
118                         if(!cs && !we) {
119                                 write_io8(0, val);
120                         }
121                 }
122         }
123 }
124
125 void SN76489AN::mix(int32* buffer, int cnt)
126 {
127         if(mute) {
128                 return;
129         }
130         for(int i = 0; i < cnt; i++) {
131                 int32 vol_l = 0, vol_r = 0;
132                 for(int j = 0; j < 4; j++) {
133                         if(!ch[j].volume) {
134                                 continue;
135                         }
136                         bool prev_signal = ch[j].signal;
137                         int prev_count = ch[j].count;
138                         ch[j].count -= diff;
139                         if(ch[j].count < 0) {
140                                 ch[j].count += ch[j].period << 8;
141                                 if(j == 3) {
142                                         if(((noise_gen & NOISE_DST_TAP) ? 1 : 0) ^ (((noise_gen & NOISE_SRC_TAP) ? 1 : 0) * NOISE_MODE)) {
143                                                 noise_gen >>= 1;
144                                                 noise_gen |= NOISE_FB;
145                                         } else {
146                                                 noise_gen >>= 1;
147                                         }
148                                         ch[3].signal = ((noise_gen & 1) != 0);
149                                 } else {
150                                         ch[j].signal = !ch[j].signal;
151                                 }
152                         }
153                         int32 sample = (prev_signal != ch[j].signal && prev_count < diff) ? (ch[j].volume * (2 * prev_count - diff)) / diff : ch[j].volume;
154                         int32 vol_tmp_l = apply_volume(sample, volume_l);
155                         int32 vol_tmp_r = apply_volume(sample, volume_r);
156                         
157                         vol_l += prev_signal ? vol_tmp_l : -vol_tmp_l;
158                         vol_r += prev_signal ? vol_tmp_r : -vol_tmp_r;
159                 }
160                 *buffer++ += vol_l; // L
161                 *buffer++ += vol_r; // R
162         }
163 }
164
165 void SN76489AN::set_volume(int ch, int decibel_l, int decibel_r)
166 {
167         volume_l = decibel_to_volume(decibel_l);
168         volume_r = decibel_to_volume(decibel_r);
169 }
170
171 void SN76489AN::init(int rate, int clock, int volume)
172 {
173         // create gain
174         double vol = volume;
175         for(int i = 0; i < 15; i++) {
176                 volume_table[i] = (int)vol;
177                 vol /= 1.258925412;
178         }
179         volume_table[15] = 0;
180         diff = (int)(16.0 * (double)clock / (double)rate + 0.5);
181 }
182
183 #define STATE_VERSION   1
184
185 void SN76489AN::save_state(FILEIO* state_fio)
186 {
187         state_fio->FputUint32(STATE_VERSION);
188         state_fio->FputInt32(this_device_id);
189         
190         state_fio->Fwrite(regs, sizeof(regs), 1);
191         state_fio->FputInt32(index);
192         state_fio->Fwrite(ch, sizeof(ch), 1);
193         state_fio->FputUint32(noise_gen);
194         state_fio->FputBool(mute);
195         state_fio->FputBool(cs);
196         state_fio->FputBool(we);
197         state_fio->FputUint8(val);
198 }
199
200 bool SN76489AN::load_state(FILEIO* state_fio)
201 {
202         if(state_fio->FgetUint32() != STATE_VERSION) {
203                 return false;
204         }
205         if(state_fio->FgetInt32() != this_device_id) {
206                 return false;
207         }
208         state_fio->Fread(regs, sizeof(regs), 1);
209         index = state_fio->FgetInt32();
210         state_fio->Fwrite(ch, sizeof(ch), 1);
211         noise_gen = state_fio->FgetUint32();
212         mute = state_fio->FgetBool();
213         cs = state_fio->FgetBool();
214         we = state_fio->FgetBool();
215         val = state_fio->FgetUint8();
216         return true;
217 }
218