Debian Codegraph @

src/engine/platform/sound/nes/apu.cpp

raw
1 /*
2 * Copyright (C) 2010-2019 Fabio Cavallo (aka FHorse)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 // additional modifications by tildearrow for furnace
20
21 #include <string.h>
22 #include "apu.h"
23
24 void apu_tick(struct NESAPU* a, int len) {
25 if (len<=a->timestamp) return;
26
27 int rem=len-a->timestamp;
28 if (rem>1) {
29 // output now just in case
30 int sample=(pulse_output(a)+tnd_output(a))<<6;
31 if (sample!=a->lastSample) {
32 blip_add_delta(a->bb,a->timestamp,sample-a->lastSample);
33 a->lastSample=sample;
34 }
35 }
36
37 while (rem>0) {
38 // predict advance
39 int advance=rem;
40 if (a->r4017.jitter.delay) {
41 advance=1;
42 }
43 if (advance>a->apu.cycles) {
44 advance=a->apu.cycles;
45 }
46 if (advance>a->S1.frequency) {
47 advance=a->S1.frequency;
48 }
49 if (advance>a->S2.frequency) {
50 advance=a->S2.frequency;
51 }
52 if (advance>a->TR.frequency) {
53 advance=a->TR.frequency;
54 }
55 if (advance>a->NS.frequency) {
56 advance=a->NS.frequency;
57 }
58 if (advance>a->DMC.frequency) {
59 advance=a->DMC.frequency;
60 }
61 if (advance<1) advance=1;
62
63 int postTS=a->timestamp+advance-1;
64
65 /* sottraggo il numero di cicli eseguiti */
66 a->apu.cycles-=advance;
67 /*
68 * questo flag sara' a TRUE solo nel ciclo
69 * in cui viene eseguito il length counter.
70 */
71 a->apu.length_clocked = FALSE;
72 /*
73 * se e' settato il delay del $4017, essendo
74 * questo il ciclo successivo, valorizzo il
75 * registro.
76 */
77 #if defined (VECCHIA_GESTIONE_JITTER)
78 if (r4017.jitter.delay) {
79 r4017.jitter.delay = FALSE;
80 r4017_jitter();
81 }
82 #else
83 if (a->r4017.jitter.delay) {
84 a->r4017.jitter.delay = FALSE;
85 r4017_jitter(0)
86 }
87 r4017_reset_frame()
88 #endif
89
90 /* quando apu.cycles e' a 0 devo eseguire uno step */
91 if (!a->apu.cycles) {
92 switch (a->apu.step) {
93 case 0:
94 /*
95 * nel mode 1 devo eseguire il
96 * length counter e lo sweep.
97 */
98 if (a->apu.mode == APU_48HZ) {
99 length_clock()
100 sweep_clock()
101 }
102 envelope_clock()
103 /* triangle's linear counter */
104 linear_clock()
105 /* passo al prossimo step */
106 apu_change_step(++a->apu.step);
107 break;
108 case 1:
109 /* nel mode 0 devo eseguire il length counter */
110 if (a->apu.mode == APU_60HZ) {
111 length_clock()
112 sweep_clock()
113 }
114 envelope_clock()
115 /* triangle's linear counter */
116 linear_clock()
117 /* passo al prossimo step */
118 apu_change_step(++a->apu.step);
119 break;
120 case 2:
121 /*
122 * nel mode 1 devo eseguire il
123 * length counter e lo sweep.
124 */
125 if (a->apu.mode == APU_48HZ) {
126 length_clock()
127 sweep_clock()
128 }
129 envelope_clock()
130 /* triangle's linear counter */
131 linear_clock()
132 /* passo al prossimo step */
133 apu_change_step(++a->apu.step);
134 break;
135 case 3:
136 /*
137 * gli step 3, 4 e 5 settano il bit 6 del $4015
138 * ma solo nel 4 genero un IRQ.
139 */
140 if (a->apu.mode == APU_60HZ) {
141 /*
142 * se e' a 0 il bit 6 del $4017 (interrupt
143 * inhibit flag) allora devo generare un IRQ.
144 */
145 if (!(a->r4017.value & 0x40)) {
146 /* setto il bit 6 del $4015 */
147 a->r4015.value |= 0x40;
148 }
149 } else {
150 /* nel mode 1 devo eseguire l'envelope */
151 envelope_clock()
152 /* triangle's linear counter */
153 linear_clock()
154 }
155 /* passo al prossimo step */
156 apu_change_step(++a->apu.step);
157 break;
158 case 4:
159 /*
160 * gli step 3, 4 e 5 settano il bit 6 del $4015
161 * ma solo nel 4 genero un IRQ.
162 */
163 if (a->apu.mode == APU_60HZ) {
164 length_clock()
165 sweep_clock()
166 envelope_clock()
167 /* triangle's linear counter */
168 linear_clock()
169 /*
170 * se e' a 0 il bit 6 del $4017 (interrupt
171 * inhibit flag) allora devo generare un IRQ.
172 */
173 if (!(a->r4017.value & 0x40)) {
174 /* setto il bit 6 del $4015 */
175 a->r4015.value |= 0x40;
176 }
177 }
178 /* passo al prossimo step */
179 apu_change_step(++a->apu.step);
180 break;
181 case 5:
182 /*
183 * gli step 3, 4 e 5 settano il bit 6 del $4015
184 * ma solo nel 4 genero un IRQ.
185 */
186 if (a->apu.mode == APU_60HZ) {
187 /*
188 * se e' a 0 il bit 6 del $4017 (interrupt
189 * inhibit flag) allora devo generare un IRQ.
190 */
191 if (!(a->r4017.value & 0x40)) {
192 /* setto il bit 6 del $4015 */
193 a->r4015.value |= 0x40;
194 }
195 a->apu.step++;
196 } else {
197 /* nel mode 1 devo ricominciare il ciclo */
198 a->apu.step = 0;
199 }
200 /* passo al prossimo step */
201 apu_change_step(a->apu.step);
202 break;
203 case 6:
204 /* da qui ci passo solo nel mode 0 */
205 envelope_clock()
206 /* triangle's linear counter */
207 linear_clock()
208 /* questo e' il passaggio finale del mode 0 */
209 a->apu.step = 1;
210 /* passo al prossimo step */
211 apu_change_step(a->apu.step);
212 break;
213 }
214 }
215
216 /*
217 * eseguo un ticket per ogni canale
218 * valorizzandone l'output.
219 */
220 // SQUARE 1 TICK
221 if (!(a->S1.frequency-=advance)) {
222 square_output(a->S1, 0)
223 a->S1.frequency = (a->S1.timer + 1) << 1;
224 a->S1.sequencer = (a->S1.sequencer + 1) & 0x07;
225 a->oscBuf[0]->putSample(postTS,a->muted[0]?0:(a->S1.output<<11));
226 }
227
228 // SQUARE 2 TICK
229 if (!(a->S2.frequency-=advance)) {
230 square_output(a->S2, 0)
231 a->S2.frequency = (a->S2.timer + 1) << 1;
232 a->S2.sequencer = (a->S2.sequencer + 1) & 0x07;
233 a->oscBuf[1]->putSample(postTS,a->muted[1]?0:(a->S2.output<<11));
234 }
235
236 // TRIANGLE TICK
237 if (!(a->TR.frequency-=advance)) {
238 a->TR.frequency = a->TR.timer + 1;
239 if (a->TR.length.value && a->TR.linear.value) {
240 a->TR.sequencer = (a->TR.sequencer + 1) & 0x1F;
241 triangle_output()
242 a->oscBuf[2]->putSample(postTS,a->muted[2]?0:(a->TR.output<<11));
243 }
244 }
245
246 // NOISE TICK
247 if (!(a->NS.frequency-=advance)) {
248 if (a->NS.mode) {
249 a->NS.shift = (a->NS.shift >> 1) | (((a->NS.shift ^ (a->NS.shift >> 6)) & 0x0001) << 14);
250 } else {
251 a->NS.shift = (a->NS.shift >> 1) | (((a->NS.shift ^ (a->NS.shift >> 1)) & 0x0001) << 14);
252 }
253 a->NS.shift &= 0x7FFF;
254 noise_output()
255 a->NS.frequency = noise_timer[a->apu.type][a->NS.timer];
256 a->oscBuf[3]->putSample(postTS,a->muted[3]?0:(a->NS.output<<11));
257 }
258
259 // DMC TICK
260 if (!(a->DMC.frequency-=advance)) {
261 if (!a->DMC.silence) {
262 if (!(a->DMC.shift & 0x01)) {
263 if (a->DMC.counter > 1) {
264 a->DMC.counter -= 2;
265 }
266 } else {
267 if (a->DMC.counter < 126) {
268 a->DMC.counter += 2;
269 }
270 }
271 }
272 a->DMC.shift >>= 1;
273 dmc_output();
274 if (!(--a->DMC.counter_out)) {
275 a->DMC.counter_out = 8;
276 if (!a->DMC.empty) {
277 a->DMC.shift = a->DMC.buffer;
278 a->DMC.empty = TRUE;
279 a->DMC.silence = FALSE;
280 } else {
281 a->DMC.silence = TRUE;
282 }
283 }
284 a->DMC.frequency = dmc_rate[a->apu.type][a->DMC.rate_index];
285 a->oscBuf[4]->putSample(postTS,a->muted[4]?0:(a->DMC.output<<8));
286 }
287 if (a->DMC.empty && a->DMC.remain) {
288 BYTE tick = 4;
289 switch (a->DMC.tick_type) {
290 case DMC_CPU_WRITE:
291 tick = 3;
292 break;
293 case DMC_R4014:
294 tick = 2;
295 break;
296 case DMC_NNL_DMA:
297 tick = 1;
298 break;
299 }
300 {
301 a->DMC.buffer = a->readDMC(a->readDMCUser,a->DMC.address);
302 }
303 /* e naturalmente incremento anche quelli eseguiti dall'opcode */
304 a->apu.cpu_cycles += tick;
305 /* salvo a che ciclo dell'istruzione avviene il dma */
306 a->DMC.dma_cycle = a->apu.cpu_opcode_cycle;
307 /* il DMC non e' vuoto */
308 a->DMC.empty = FALSE;
309 if (++a->DMC.address > 0xFFFF) {
310 a->DMC.address = 0x8000;
311 }
312 if (!(--a->DMC.remain)) {
313 if (a->DMC.loop) {
314 a->DMC.remain = a->DMC.length;
315 a->DMC.address = a->DMC.address_start;
316 } else if (a->DMC.irq_enabled) {
317 a->r4015.value |= 0x80;
318 }
319 }
320 }
321
322 a->r4011.cycles+=advance;
323 if (advance&1) {
324 a->apu.odd_cycle=!a->apu.odd_cycle;
325 }
326
327 // output sample
328 a->timestamp=postTS;
329 int sample=(pulse_output(a)+tnd_output(a))<<6;
330 if (sample!=a->lastSample) {
331 blip_add_delta(a->bb,a->timestamp,sample-a->lastSample);
332 a->lastSample=sample;
333 }
334
335 rem-=advance;
336 a->timestamp++;
337 }
338 a->timestamp=len;
339 }
340
341 void apu_turn_on(struct NESAPU* a, BYTE apu_type) {
342 memset(&a->apu, 0x00, sizeof(a->apu));
343 memset(&a->r4015, 0x00, sizeof(a->r4015));
344 memset(&a->r4017, 0x00, sizeof(a->r4017));
345 /* azzero tutte le variabili interne dei canali */
346 memset(&a->S1, 0x00, sizeof(a->S1));
347 memset(&a->S2, 0x00, sizeof(a->S2));
348 memset(&a->TR, 0x00, sizeof(a->TR));
349 memset(&a->NS, 0x00, sizeof(a->NS));
350 memset(&a->DMC, 0x00, sizeof(a->DMC));
351 /* al reset e' sempre settato a 60Hz */
352 a->apu.mode = APU_60HZ;
353 /* per favore non fatemi questo... e' terribile */
354 a->apu.type = apu_type;
355 apu_change_step(a->apu.step);
356 /* valori iniziali dei vari canali */
357 a->S1.frequency = 1;
358 a->S1.sweep.delay = 1;
359 a->S1.sweep.divider = 1;
360 a->S2.frequency = 1;
361 a->S2.sweep.delay = 1;
362 a->S2.sweep.divider = 1;
363 a->TR.frequency = 1;
364 /* questo era 0 ma produce click nell'audio */
365 a->TR.sequencer = 7;
366 a->NS.frequency = 1;
367 a->NS.shift = 1;
368 a->DMC.frequency = 1;
369 a->DMC.empty = TRUE;
370 a->DMC.silence = TRUE;
371 a->DMC.counter_out = 8;
372 // sembra che l'address del DMC al power on dia valorizzato a 0xC000
373 // e la lunghezza del sample sia settato a 1 byte.
374 // http://forums.nesdev.com/viewtopic.php?f=3&t=18278
375 a->DMC.length = 1;
376 a->DMC.address_start = 0xC000;
377 a->apu.odd_cycle = 0;
378 // come non viene inizializzato? Vorrei qualche spiegazione...
379 a->r4011.frames = 0;
380 a->lastSample = 0;
381
382 a->S1.timer=2048;
383 a->S2.timer=2048;
384 a->TR.timer=2048;
385 a->NS.timer=0x0FE4;
386 }