| 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 |
#include <stdio.h> |
| 20 |
#include <string.h> |
| 21 |
#include "fds.h" |
| 22 |
|
| 23 |
enum { TRANSFERED_8BIT = 0x02, END_OF_HEAD = 0x40 }; |
| 24 |
|
| 25 |
static const BYTE volume_wave[4] = { 39, 26, 19, 15 }; |
| 26 |
|
| 27 |
void fds_reset(struct _fds* fds) { |
| 28 |
memset(fds,0,sizeof(struct _fds)); |
| 29 |
} |
| 30 |
|
| 31 |
void extcl_apu_tick_FDS(struct _fds* fds) { |
| 32 |
SWORD freq; |
| 33 |
|
| 34 |
/* volume unit */ |
| 35 |
if (fds->snd.volume.mode) { |
| 36 |
fds->snd.volume.gain = fds->snd.volume.speed; |
| 37 |
} else if (!fds->snd.envelope.disabled && fds->snd.envelope.speed) { |
| 38 |
if (fds->snd.volume.counter) { |
| 39 |
fds->snd.volume.counter--; |
| 40 |
} else { |
| 41 |
fds->snd.volume.counter = (fds->snd.envelope.speed << 3) * (fds->snd.volume.speed + 1); |
| 42 |
if (fds->snd.volume.increase) { |
| 43 |
if (fds->snd.volume.gain < 32) { |
| 44 |
fds->snd.volume.gain++; |
| 45 |
} |
| 46 |
} else if (fds->snd.volume.gain) { |
| 47 |
fds->snd.volume.gain--; |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/* sweep unit */ |
| 53 |
if (fds->snd.sweep.mode) { |
| 54 |
fds->snd.sweep.gain = fds->snd.sweep.speed; |
| 55 |
} else if (!fds->snd.envelope.disabled && fds->snd.envelope.speed) { |
| 56 |
if (fds->snd.sweep.counter) { |
| 57 |
fds->snd.sweep.counter--; |
| 58 |
} else { |
| 59 |
fds->snd.sweep.counter = (fds->snd.envelope.speed << 3) * (fds->snd.sweep.speed + 1); |
| 60 |
if (fds->snd.sweep.increase) { |
| 61 |
if (fds->snd.sweep.gain < 32) { |
| 62 |
fds->snd.sweep.gain++; |
| 63 |
} |
| 64 |
} else if (fds->snd.sweep.gain) { |
| 65 |
fds->snd.sweep.gain--; |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/* modulation unit */ |
| 71 |
freq = fds->snd.main.frequency; |
| 72 |
|
| 73 |
if (!fds->snd.modulation.disabled && fds->snd.modulation.frequency) { |
| 74 |
if ((fds->snd.modulation.counter -= fds->snd.modulation.frequency) < 0) { |
| 75 |
SWORD temp, temp2, a, d; |
| 76 |
SBYTE adj = fds->snd.modulation.data[fds->snd.modulation.index]; |
| 77 |
|
| 78 |
fds->snd.modulation.counter += 65536; |
| 79 |
|
| 80 |
if (++fds->snd.modulation.index == 64) { |
| 81 |
fds->snd.modulation.index = 0; |
| 82 |
} |
| 83 |
|
| 84 |
if (adj == -4) { |
| 85 |
fds->snd.sweep.bias = 0; |
| 86 |
} else { |
| 87 |
fds->snd.sweep.bias += adj; |
| 88 |
} |
| 89 |
|
| 90 |
temp = fds->snd.sweep.bias * ((fds->snd.sweep.gain < 32) ? fds->snd.sweep.gain : 32); |
| 91 |
|
| 92 |
a = 64; |
| 93 |
d = 0; |
| 94 |
|
| 95 |
if (temp <= 0) { |
| 96 |
d = 15; |
| 97 |
} else if (temp < 3040) { //95 * 32 |
| 98 |
a = 66; |
| 99 |
d = -31; |
| 100 |
} |
| 101 |
|
| 102 |
temp2 = a + (SBYTE) ((temp - d) / 16 - a); |
| 103 |
|
| 104 |
fds->snd.modulation.mod = freq * temp2 / 64; |
| 105 |
} |
| 106 |
|
| 107 |
if (freq) { |
| 108 |
freq += fds->snd.modulation.mod; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/* main unit */ |
| 113 |
if (fds->snd.main.silence) { |
| 114 |
fds->snd.main.output = 0; |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
if (freq && !fds->snd.wave.writable) { |
| 119 |
if ((fds->snd.wave.counter -= freq) < 0) { |
| 120 |
WORD level; |
| 121 |
|
| 122 |
fds->snd.wave.counter += 65536; |
| 123 |
|
| 124 |
level = (fds->snd.volume.gain < 32 ? fds->snd.volume.gain : 32) |
| 125 |
* volume_wave[fds->snd.wave.volume]; |
| 126 |
|
| 127 |
/* valore massimo dell'output (63 * (39 * 32)) = 78624 */ |
| 128 |
/*fds->snd.main.output = (fds->snd.wave.data[fds->snd.wave.index] * level) >> 4;*/ |
| 129 |
fds->snd.main.output = (fds->snd.wave.data[fds->snd.wave.index] * level) >> 3; |
| 130 |
|
| 131 |
if (++fds->snd.wave.index == 64) { |
| 132 |
fds->snd.wave.index = 0; |
| 133 |
} |
| 134 |
|
| 135 |
fds->snd.wave.clocked = TRUE; |
| 136 |
} |
| 137 |
} |
| 138 |
} |