furnace/src/engine/platform/sound/nes/apu.c

230 lines
5.9 KiB
C
Raw Normal View History

2021-12-03 16:04:07 -05:00
/*
* Copyright (C) 2010-2019 Fabio Cavallo (aka FHorse)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// additional modifications by tildearrow for furnace
#include <string.h>
#include "apu.h"
void apu_tick(struct NESAPU* a, BYTE *hwtick) {
2021-12-03 16:04:07 -05:00
/* sottraggo il numero di cicli eseguiti */
a->apu.cycles--;
2021-12-03 16:04:07 -05:00
/*
* questo flag sara' a TRUE solo nel ciclo
* in cui viene eseguito il length counter.
*/
a->apu.length_clocked = FALSE;
2021-12-03 16:04:07 -05:00
/*
* se e' settato il delay del $4017, essendo
* questo il ciclo successivo, valorizzo il
* registro.
*/
#if defined (VECCHIA_GESTIONE_JITTER)
if (r4017.jitter.delay) {
r4017.jitter.delay = FALSE;
r4017_jitter();
}
#else
if (a->r4017.jitter.delay) {
a->r4017.jitter.delay = FALSE;
2021-12-03 16:04:07 -05:00
r4017_jitter(0)
}
r4017_reset_frame()
#endif
/* quando apu.cycles e' a 0 devo eseguire uno step */
if (!a->apu.cycles) {
switch (a->apu.step) {
2021-12-03 16:04:07 -05:00
case 0:
/*
* nel mode 1 devo eseguire il
* length counter e lo sweep.
*/
if (a->apu.mode == APU_48HZ) {
2021-12-03 16:04:07 -05:00
length_clock()
sweep_clock()
}
envelope_clock()
/* triangle's linear counter */
linear_clock()
/* passo al prossimo step */
apu_change_step(++a->apu.step);
2021-12-03 16:04:07 -05:00
break;
case 1:
/* nel mode 0 devo eseguire il length counter */
if (a->apu.mode == APU_60HZ) {
2021-12-03 16:04:07 -05:00
length_clock()
sweep_clock()
}
envelope_clock()
/* triangle's linear counter */
linear_clock()
/* passo al prossimo step */
apu_change_step(++a->apu.step);
2021-12-03 16:04:07 -05:00
break;
case 2:
/*
* nel mode 1 devo eseguire il
* length counter e lo sweep.
*/
if (a->apu.mode == APU_48HZ) {
2021-12-03 16:04:07 -05:00
length_clock()
sweep_clock()
}
envelope_clock()
/* triangle's linear counter */
linear_clock()
/* passo al prossimo step */
apu_change_step(++a->apu.step);
2021-12-03 16:04:07 -05:00
break;
case 3:
/*
* gli step 3, 4 e 5 settano il bit 6 del $4015
* ma solo nel 4 genero un IRQ.
*/
if (a->apu.mode == APU_60HZ) {
2021-12-03 16:04:07 -05:00
/*
* se e' a 0 il bit 6 del $4017 (interrupt
* inhibit flag) allora devo generare un IRQ.
*/
if (!(a->r4017.value & 0x40)) {
2021-12-03 16:04:07 -05:00
/* setto il bit 6 del $4015 */
a->r4015.value |= 0x40;
2021-12-03 16:04:07 -05:00
}
} else {
/* nel mode 1 devo eseguire l'envelope */
envelope_clock()
/* triangle's linear counter */
linear_clock()
}
/* passo al prossimo step */
apu_change_step(++a->apu.step);
2021-12-03 16:04:07 -05:00
break;
case 4:
/*
* gli step 3, 4 e 5 settano il bit 6 del $4015
* ma solo nel 4 genero un IRQ.
*/
if (a->apu.mode == APU_60HZ) {
2021-12-03 16:04:07 -05:00
length_clock()
sweep_clock()
envelope_clock()
/* triangle's linear counter */
linear_clock()
/*
* se e' a 0 il bit 6 del $4017 (interrupt
* inhibit flag) allora devo generare un IRQ.
*/
if (!(a->r4017.value & 0x40)) {
2021-12-03 16:04:07 -05:00
/* setto il bit 6 del $4015 */
a->r4015.value |= 0x40;
2021-12-03 16:04:07 -05:00
}
}
/* passo al prossimo step */
apu_change_step(++a->apu.step);
2021-12-03 16:04:07 -05:00
break;
case 5:
/*
* gli step 3, 4 e 5 settano il bit 6 del $4015
* ma solo nel 4 genero un IRQ.
*/
if (a->apu.mode == APU_60HZ) {
2021-12-03 16:04:07 -05:00
/*
* se e' a 0 il bit 6 del $4017 (interrupt
* inhibit flag) allora devo generare un IRQ.
*/
if (!(a->r4017.value & 0x40)) {
2021-12-03 16:04:07 -05:00
/* setto il bit 6 del $4015 */
a->r4015.value |= 0x40;
2021-12-03 16:04:07 -05:00
}
a->apu.step++;
2021-12-03 16:04:07 -05:00
} else {
/* nel mode 1 devo ricominciare il ciclo */
a->apu.step = 0;
2021-12-03 16:04:07 -05:00
}
/* passo al prossimo step */
apu_change_step(a->apu.step);
2021-12-03 16:04:07 -05:00
break;
case 6:
/* da qui ci passo solo nel mode 0 */
envelope_clock()
/* triangle's linear counter */
linear_clock()
/* questo e' il passaggio finale del mode 0 */
a->apu.step = 1;
2021-12-03 16:04:07 -05:00
/* passo al prossimo step */
apu_change_step(a->apu.step);
2021-12-03 16:04:07 -05:00
break;
}
}
/*
* eseguo un ticket per ogni canale
* valorizzandone l'output.
*/
square_tick(a->S1, 0, a->apu)
square_tick(a->S2, 0, a->apu)
2021-12-03 16:04:07 -05:00
triangle_tick()
noise_tick()
dmc_tick()
// TODO
/*if (snd_apu_tick) {
snd_apu_tick();
}*/
a->r4011.cycles++;
2021-12-03 16:04:07 -05:00
}
2022-01-26 16:07:55 -05:00
void apu_turn_on(struct NESAPU* a, BYTE apu_type) {
memset(&a->apu, 0x00, sizeof(a->apu));
memset(&a->r4015, 0x00, sizeof(a->r4015));
memset(&a->r4017, 0x00, sizeof(a->r4017));
2021-12-03 16:04:07 -05:00
/* azzero tutte le variabili interne dei canali */
memset(&a->S1, 0x00, sizeof(a->S1));
memset(&a->S2, 0x00, sizeof(a->S2));
memset(&a->TR, 0x00, sizeof(a->TR));
memset(&a->NS, 0x00, sizeof(a->NS));
memset(&a->DMC, 0x00, sizeof(a->DMC));
2021-12-03 16:04:07 -05:00
/* al reset e' sempre settato a 60Hz */
a->apu.mode = APU_60HZ;
2022-01-26 16:07:55 -05:00
/* per favore non fatemi questo... e' terribile */
a->apu.type = apu_type;
apu_change_step(a->apu.step);
2021-12-03 16:04:07 -05:00
/* valori iniziali dei vari canali */
a->S1.frequency = 1;
a->S1.sweep.delay = 1;
a->S1.sweep.divider = 1;
a->S2.frequency = 1;
a->S2.sweep.delay = 1;
a->S2.sweep.divider = 1;
a->TR.frequency = 1;
a->TR.sequencer = 0;
a->NS.frequency = 1;
a->NS.shift = 1;
a->DMC.frequency = 1;
a->DMC.empty = TRUE;
a->DMC.silence = TRUE;
a->DMC.counter_out = 8;
2021-12-03 16:04:07 -05:00
// sembra che l'address del DMC al power on dia valorizzato a 0xC000
// e la lunghezza del sample sia settato a 1 byte.
// http://forums.nesdev.com/viewtopic.php?f=3&t=18278
a->DMC.length = 1;
a->DMC.address_start = 0xC000;
a->apu.odd_cycle = 0;
2021-12-03 16:04:07 -05:00
}