| 1 |
#include "nes_vrc6.h" |
| 2 |
#include "common.h" |
| 3 |
|
| 4 |
namespace xgm |
| 5 |
{ |
| 6 |
|
| 7 |
NES_VRC6::NES_VRC6 () |
| 8 |
{ |
| 9 |
SetClock (DEFAULT_CLOCK); |
| 10 |
SetRate (DEFAULT_RATE); |
| 11 |
|
| 12 |
halt = false; |
| 13 |
freq_shift = 0; |
| 14 |
|
| 15 |
for(int c=0;c<2;++c) |
| 16 |
for(int t=0;t<3;++t) |
| 17 |
sm[c][t] = 128; |
| 18 |
} |
| 19 |
|
| 20 |
NES_VRC6::~NES_VRC6 () |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
void NES_VRC6::SetStereoMix(int trk, short mixl, short mixr) |
| 25 |
{ |
| 26 |
if (trk < 0) return; |
| 27 |
if (trk > 2) return; |
| 28 |
sm[0][trk] = mixl; |
| 29 |
sm[1][trk] = mixr; |
| 30 |
} |
| 31 |
|
| 32 |
void NES_VRC6::SetClock (double c) |
| 33 |
{ |
| 34 |
clock = c; |
| 35 |
} |
| 36 |
|
| 37 |
void NES_VRC6::SetRate (double r) |
| 38 |
{ |
| 39 |
rate = r ? r : DEFAULT_RATE; |
| 40 |
} |
| 41 |
|
| 42 |
void NES_VRC6::SetOption (int id, int val) |
| 43 |
{ |
| 44 |
if(id<OPT_END) |
| 45 |
{ |
| 46 |
//option[id] = val; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
void NES_VRC6::Reset () |
| 51 |
{ |
| 52 |
Write (0x9003, 0); |
| 53 |
for (int i = 0; i < 3; i++) |
| 54 |
{ |
| 55 |
Write (0x9000 + i, 0); |
| 56 |
Write (0xa000 + i, 0); |
| 57 |
Write (0xb000 + i, 0); |
| 58 |
} |
| 59 |
count14 = 0; |
| 60 |
mask = 0; |
| 61 |
counter[0] = 0; |
| 62 |
counter[1] = 0; |
| 63 |
counter[2] = 0; |
| 64 |
phase[0] = 0; |
| 65 |
phase[0] = 1; |
| 66 |
phase[0] = 2; |
| 67 |
} |
| 68 |
|
| 69 |
short NES_VRC6::calc_sqr (int i, unsigned int clocks) |
| 70 |
{ |
| 71 |
static const short sqrtbl[8][16] = { |
| 72 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, |
| 73 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}, |
| 74 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1}, |
| 75 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}, |
| 76 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, |
| 77 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, |
| 78 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}, |
| 79 |
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1} |
| 80 |
}; |
| 81 |
|
| 82 |
if (!enable[i]) |
| 83 |
return 0; |
| 84 |
|
| 85 |
if (!halt) |
| 86 |
{ |
| 87 |
counter[i] += clocks; |
| 88 |
while(counter[i] > freq2[i]) |
| 89 |
{ |
| 90 |
phase[i] = (phase[i] + 1) & 15; |
| 91 |
counter[i] -= (freq2[i] + 1); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return (gate[i] |
| 96 |
|| sqrtbl[duty[i]][phase[i]])? volume[i] : 0; |
| 97 |
} |
| 98 |
|
| 99 |
short NES_VRC6::calc_saw (unsigned int clocks) |
| 100 |
{ |
| 101 |
if (!enable[2]) |
| 102 |
return 0; |
| 103 |
|
| 104 |
if (!halt) |
| 105 |
{ |
| 106 |
counter[2] += clocks; |
| 107 |
while(counter[2] > freq2[2]) |
| 108 |
{ |
| 109 |
counter[2] -= (freq2[2] + 1); |
| 110 |
|
| 111 |
// accumulate saw |
| 112 |
++count14; |
| 113 |
if (count14 >= 14) |
| 114 |
{ |
| 115 |
count14 = 0; |
| 116 |
phase[2] = 0; |
| 117 |
} |
| 118 |
else if (0 == (count14 & 1)) // only accumulate on even ticks |
| 119 |
{ |
| 120 |
phase[2] = (phase[2] + volume[2]) & 0xFF; // note 8-bit wrapping behaviour |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// only top 5 bits of saw are output |
| 126 |
return phase[2] >> 3; |
| 127 |
} |
| 128 |
|
| 129 |
void NES_VRC6::Tick (unsigned int clocks) |
| 130 |
{ |
| 131 |
out[0] = calc_sqr(0,clocks); |
| 132 |
out[1] = calc_sqr(1,clocks); |
| 133 |
out[2] = calc_saw(clocks); |
| 134 |
} |
| 135 |
|
| 136 |
unsigned int NES_VRC6::Render (int b[2]) |
| 137 |
{ |
| 138 |
int m[3]; |
| 139 |
m[0] = out[0]; |
| 140 |
m[1] = out[1]; |
| 141 |
m[2] = out[2]; |
| 142 |
|
| 143 |
// note: signal is inverted compared to 2A03 |
| 144 |
|
| 145 |
m[0] = (mask & 1) ? 0 : -m[0]; |
| 146 |
m[1] = (mask & 2) ? 0 : -m[1]; |
| 147 |
m[2] = (mask & 4) ? 0 : -m[2]; |
| 148 |
|
| 149 |
b[0] = m[0] * sm[0][0]; |
| 150 |
b[0] += m[1] * sm[0][1]; |
| 151 |
b[0] += m[2] * sm[0][2]; |
| 152 |
//b[0] >>= (7 - 7); |
| 153 |
|
| 154 |
b[1] = m[0] * sm[1][0]; |
| 155 |
b[1] += m[1] * sm[1][1]; |
| 156 |
b[1] += m[2] * sm[1][2]; |
| 157 |
//b[1] >>= (7 - 7); |
| 158 |
|
| 159 |
// master volume adjustment |
| 160 |
const int MASTER = int(256.0 * 1223.0 / 1920.0); |
| 161 |
b[0] = (b[0] * MASTER) >> 8; |
| 162 |
b[1] = (b[1] * MASTER) >> 8; |
| 163 |
|
| 164 |
return 2; |
| 165 |
} |
| 166 |
|
| 167 |
bool NES_VRC6::Write (unsigned int adr, unsigned int val, unsigned int id) |
| 168 |
{ |
| 169 |
int ch, cmap[4] = { 0, 0, 1, 2 }; |
| 170 |
|
| 171 |
switch (adr) |
| 172 |
{ |
| 173 |
case 0x9000: |
| 174 |
case 0xa000: |
| 175 |
ch = cmap[(adr >> 12) & 3]; |
| 176 |
volume[ch] = val & 15; |
| 177 |
duty[ch] = (val >> 4) & 7; |
| 178 |
gate[ch] = (val >> 7) & 1; |
| 179 |
break; |
| 180 |
case 0xb000: |
| 181 |
volume[2] = val & 63; |
| 182 |
break; |
| 183 |
|
| 184 |
case 0x9001: |
| 185 |
case 0xa001: |
| 186 |
case 0xb001: |
| 187 |
ch = cmap[(adr >> 12) & 3]; |
| 188 |
freq[ch] = (freq[ch] & 0xf00) | val; |
| 189 |
freq2[ch] = (freq[ch] >> freq_shift); |
| 190 |
if (counter[ch] > freq2[ch]) counter[ch] = freq2[ch]; |
| 191 |
break; |
| 192 |
|
| 193 |
case 0x9002: |
| 194 |
case 0xa002: |
| 195 |
case 0xb002: |
| 196 |
ch = cmap[(adr >> 12) & 3]; |
| 197 |
freq[ch] = ((val & 0xf) << 8) + (freq[ch] & 0xff); |
| 198 |
freq2[ch] = (freq[ch] >> freq_shift); |
| 199 |
if (counter[ch] > freq2[ch]) counter[ch] = freq2[ch]; |
| 200 |
if (!enable[ch]) // if enable is being turned on, phase should be reset |
| 201 |
{ |
| 202 |
if (ch == 2) |
| 203 |
{ |
| 204 |
count14 = 0; // reset saw |
| 205 |
} |
| 206 |
phase[ch] = 0; |
| 207 |
} |
| 208 |
enable[ch] = (val >> 7) & 1; |
| 209 |
break; |
| 210 |
|
| 211 |
case 0x9003: |
| 212 |
halt = val & 1; |
| 213 |
freq_shift = |
| 214 |
(val & 4) ? 8 : |
| 215 |
(val & 2) ? 4 : |
| 216 |
0; |
| 217 |
freq2[0] = (freq[0] >> freq_shift); |
| 218 |
freq2[1] = (freq[1] >> freq_shift); |
| 219 |
freq2[2] = (freq[2] >> freq_shift); |
| 220 |
if (counter[0] > freq2[0]) counter[0] = freq2[0]; |
| 221 |
if (counter[1] > freq2[1]) counter[1] = freq2[1]; |
| 222 |
if (counter[2] > freq2[2]) counter[2] = freq2[2]; |
| 223 |
break; |
| 224 |
|
| 225 |
default: |
| 226 |
return false; |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
return true; |
| 231 |
} |
| 232 |
|
| 233 |
bool NES_VRC6::Read (unsigned int adr, unsigned int & val, unsigned int id) |
| 234 |
{ |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
} // namespace |