| 1 |
#include <cstring> |
| 2 |
#include "nes_n106.h" |
| 3 |
#include "common.h" |
| 4 |
|
| 5 |
namespace xgm { |
| 6 |
|
| 7 |
NES_N106::NES_N106 () |
| 8 |
{ |
| 9 |
option[OPT_SERIAL] = 0; |
| 10 |
option[OPT_PHASE_READ_ONLY] = 0; |
| 11 |
option[OPT_LIMIT_WAVELENGTH] = 0; |
| 12 |
SetClock (DEFAULT_CLOCK); |
| 13 |
SetRate (DEFAULT_RATE); |
| 14 |
for (int i=0; i < 8; ++i) |
| 15 |
{ |
| 16 |
sm[0][i] = 128; |
| 17 |
sm[1][i] = 128; |
| 18 |
} |
| 19 |
Reset(); |
| 20 |
} |
| 21 |
|
| 22 |
NES_N106::~NES_N106 () |
| 23 |
{ |
| 24 |
} |
| 25 |
|
| 26 |
void NES_N106::SetStereoMix (int trk, short mixl, short mixr) |
| 27 |
{ |
| 28 |
if (trk < 0 || trk >= 8) return; |
| 29 |
trk = 7-trk; // displayed channels are inverted |
| 30 |
sm[0][trk] = mixl; |
| 31 |
sm[1][trk] = mixr; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
void NES_N106::SetClock (double c) |
| 36 |
{ |
| 37 |
clock = c; |
| 38 |
} |
| 39 |
|
| 40 |
void NES_N106::SetRate (double r) |
| 41 |
{ |
| 42 |
rate = r; |
| 43 |
} |
| 44 |
|
| 45 |
void NES_N106::SetMask (int m) |
| 46 |
{ |
| 47 |
// bit reverse the mask, |
| 48 |
// N163 waves are displayed in reverse order |
| 49 |
mask = 0 |
| 50 |
| ((m & (1<<0)) ? (1<<7) : 0) |
| 51 |
| ((m & (1<<1)) ? (1<<6) : 0) |
| 52 |
| ((m & (1<<2)) ? (1<<5) : 0) |
| 53 |
| ((m & (1<<3)) ? (1<<4) : 0) |
| 54 |
| ((m & (1<<4)) ? (1<<3) : 0) |
| 55 |
| ((m & (1<<5)) ? (1<<2) : 0) |
| 56 |
| ((m & (1<<6)) ? (1<<1) : 0) |
| 57 |
| ((m & (1<<7)) ? (1<<0) : 0); |
| 58 |
} |
| 59 |
|
| 60 |
void NES_N106::SetOption (int id, int val) |
| 61 |
{ |
| 62 |
if (id<OPT_END) option[id] = val; |
| 63 |
} |
| 64 |
|
| 65 |
void NES_N106::Reset () |
| 66 |
{ |
| 67 |
master_disable = false; |
| 68 |
::memset(reg, 0, sizeof(reg)); |
| 69 |
reg_select = 0; |
| 70 |
reg_advance = false; |
| 71 |
tick_channel = 0; |
| 72 |
tick_clock = 0; |
| 73 |
render_channel = 0; |
| 74 |
render_clock = 0; |
| 75 |
render_subclock = 0; |
| 76 |
|
| 77 |
for (int i=0; i<8; ++i) fout[i] = 0; |
| 78 |
|
| 79 |
Write(0xE000, 0x00); // master disable off |
| 80 |
Write(0xF800, 0x80); // select $00 with auto-increment |
| 81 |
for (unsigned int i=0; i<0x80; ++i) // set all regs to 0 |
| 82 |
{ |
| 83 |
Write(0x4800, 0x00); |
| 84 |
} |
| 85 |
Write(0xF800, 0x00); // select $00 without auto-increment |
| 86 |
} |
| 87 |
|
| 88 |
void NES_N106::Tick (unsigned int clocks) |
| 89 |
{ |
| 90 |
if (master_disable) return; |
| 91 |
|
| 92 |
int channels = get_channels(); |
| 93 |
|
| 94 |
tick_clock += clocks; |
| 95 |
render_clock += clocks; // keep render in sync |
| 96 |
while (tick_clock > 0) |
| 97 |
{ |
| 98 |
int channel = 7-tick_channel; |
| 99 |
|
| 100 |
unsigned int phase = get_phase(channel); |
| 101 |
unsigned int freq = get_freq(channel); |
| 102 |
unsigned int len = get_len(channel); |
| 103 |
unsigned int off = get_off(channel); |
| 104 |
int vol = get_vol(channel); |
| 105 |
|
| 106 |
// accumulate 24-bit phase |
| 107 |
phase = (phase + freq) & 0x00FFFFFF; |
| 108 |
|
| 109 |
// wrap phase if wavelength exceeded |
| 110 |
unsigned int hilen = len << 16; |
| 111 |
while (phase >= hilen) phase -= hilen; |
| 112 |
|
| 113 |
// write back phase |
| 114 |
set_phase(phase, channel); |
| 115 |
|
| 116 |
// fetch sample (note: N163 output is centred at 8, and inverted w.r.t 2A03) |
| 117 |
int sample = 8 - get_sample(((phase >> 16) + off) & 0xFF); |
| 118 |
fout[channel] = sample * vol; |
| 119 |
|
| 120 |
// cycle to next channel every 15 clocks |
| 121 |
tick_clock -= 15; |
| 122 |
++tick_channel; |
| 123 |
if (tick_channel >= channels) |
| 124 |
tick_channel = 0; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
unsigned int NES_N106::Render (int b[2]) |
| 129 |
{ |
| 130 |
b[0] = 0; |
| 131 |
b[1] = 0; |
| 132 |
if (master_disable) return 2; |
| 133 |
|
| 134 |
int channels = get_channels(); |
| 135 |
|
| 136 |
if (option[OPT_SERIAL]) // hardware accurate serial multiplexing |
| 137 |
{ |
| 138 |
// this could be made more efficient than going clock-by-clock |
| 139 |
// but this way is simpler |
| 140 |
int clocks = render_clock; |
| 141 |
while (clocks > 0) |
| 142 |
{ |
| 143 |
int c = 7-render_channel; |
| 144 |
if (0 == ((mask >> c) & 1)) |
| 145 |
{ |
| 146 |
b[0] += fout[c] * sm[0][c]; |
| 147 |
b[1] += fout[c] * sm[1][c]; |
| 148 |
} |
| 149 |
|
| 150 |
++render_subclock; |
| 151 |
if (render_subclock >= 15) // each channel gets a 15-cycle slice |
| 152 |
{ |
| 153 |
render_subclock = 0; |
| 154 |
++render_channel; |
| 155 |
if (render_channel >= channels) |
| 156 |
render_channel = 0; |
| 157 |
} |
| 158 |
--clocks; |
| 159 |
} |
| 160 |
|
| 161 |
// increase output level by 1 bits (7 bits already added from sm) |
| 162 |
b[0] <<= 1; |
| 163 |
b[1] <<= 1; |
| 164 |
|
| 165 |
// average the output |
| 166 |
if (render_clock > 0) |
| 167 |
{ |
| 168 |
b[0] /= render_clock; |
| 169 |
b[1] /= render_clock; |
| 170 |
} |
| 171 |
render_clock = 0; |
| 172 |
} |
| 173 |
else // just mix all channels |
| 174 |
{ |
| 175 |
for (int i = (8-channels); i<8; ++i) |
| 176 |
{ |
| 177 |
if (0 == ((mask >> i) & 1)) |
| 178 |
{ |
| 179 |
b[0] += fout[i] * sm[0][i]; |
| 180 |
b[1] += fout[i] * sm[1][i]; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
// mix together, increase output level by 8 bits, roll off 7 bits from sm |
| 185 |
int MIX[9] = { 256/1, 256/1, 256/2, 256/3, 256/4, 256/5, 256/6, 256/6, 256/6 }; |
| 186 |
b[0] = (b[0] * MIX[channels]) >> 7; |
| 187 |
b[1] = (b[1] * MIX[channels]) >> 7; |
| 188 |
// when approximating the serial multiplex as a straight mix, once the |
| 189 |
// multiplex frequency gets below the nyquist frequency an average mix |
| 190 |
// begins to sound too quiet. To approximate this effect, I don't attenuate |
| 191 |
// any further after 6 channels are active. |
| 192 |
} |
| 193 |
|
| 194 |
// 8 bit approximation of master volume |
| 195 |
// max N163 vol vs max APU square |
| 196 |
// unfortunately, games have been measured as low as 3.4x and as high as 8.5x |
| 197 |
// with higher volumes on Erika, King of Kings, and Rolling Thunder |
| 198 |
// and lower volumes on others. Using 6.0x as a rough "one size fits all". |
| 199 |
const double MASTER_VOL = 6.0 * 1223.0; |
| 200 |
const double MAX_OUT = 15.0 * 15.0 * 256.0; // max digital value |
| 201 |
const int GAIN = int((MASTER_VOL / MAX_OUT) * 256.0f); |
| 202 |
b[0] = (b[0] * GAIN) >> 8; |
| 203 |
b[1] = (b[1] * GAIN) >> 8; |
| 204 |
|
| 205 |
return 2; |
| 206 |
} |
| 207 |
|
| 208 |
bool NES_N106::Write (unsigned int adr, unsigned int val, unsigned int id) |
| 209 |
{ |
| 210 |
if (adr == 0xE000) // master disable |
| 211 |
{ |
| 212 |
master_disable = ((val & 0x40) != 0); |
| 213 |
return true; |
| 214 |
} |
| 215 |
else if (adr == 0xF800) // register select |
| 216 |
{ |
| 217 |
reg_select = (val & 0x7F); |
| 218 |
reg_advance = (val & 0x80) != 0; |
| 219 |
return true; |
| 220 |
} |
| 221 |
else if (adr == 0x4800) // register write |
| 222 |
{ |
| 223 |
if (option[OPT_PHASE_READ_ONLY]) // old emulators didn't know phase was stored here |
| 224 |
{ |
| 225 |
int c = 15 - (reg_select/8); |
| 226 |
int r = reg_select & 7; |
| 227 |
if (c < get_channels() && |
| 228 |
(r == 1 || |
| 229 |
r == 3 || |
| 230 |
r == 5)) |
| 231 |
{ |
| 232 |
if (reg_advance) |
| 233 |
reg_select = (reg_select + 1) & 0x7F; |
| 234 |
return true; |
| 235 |
} |
| 236 |
} |
| 237 |
if (option[OPT_LIMIT_WAVELENGTH]) // old emulators ignored top 3 bits of length |
| 238 |
{ |
| 239 |
int c = 15 - (reg_select/8); |
| 240 |
int r = reg_select & 7; |
| 241 |
if (c < get_channels() && r == 4) |
| 242 |
{ |
| 243 |
val |= 0xE0; |
| 244 |
} |
| 245 |
} |
| 246 |
reg[reg_select] = val; |
| 247 |
if (reg_advance) |
| 248 |
reg_select = (reg_select + 1) & 0x7F; |
| 249 |
return true; |
| 250 |
} |
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
bool NES_N106::Read (unsigned int adr, unsigned int & val, unsigned int id) |
| 255 |
{ |
| 256 |
if (adr == 0x4800) // register read |
| 257 |
{ |
| 258 |
val = reg[reg_select]; |
| 259 |
if (reg_advance) |
| 260 |
reg_select = (reg_select + 1) & 0x7F; |
| 261 |
return true; |
| 262 |
} |
| 263 |
return false; |
| 264 |
} |
| 265 |
|
| 266 |
// |
| 267 |
// register decoding/encoding functions |
| 268 |
// |
| 269 |
|
| 270 |
inline unsigned int NES_N106::get_phase (int channel) |
| 271 |
{ |
| 272 |
// 24-bit phase stored in channel regs 1/3/5 |
| 273 |
channel = channel << 3; |
| 274 |
return (reg[0x41 + channel] ) |
| 275 |
+ (reg[0x43 + channel] << 8 ) |
| 276 |
+ (reg[0x45 + channel] << 16); |
| 277 |
} |
| 278 |
|
| 279 |
inline unsigned int NES_N106::get_freq (int channel) |
| 280 |
{ |
| 281 |
// 19-bit frequency stored in channel regs 0/2/4 |
| 282 |
channel = channel << 3; |
| 283 |
return ( reg[0x40 + channel] ) |
| 284 |
+ ( reg[0x42 + channel] << 8 ) |
| 285 |
+ ((reg[0x44 + channel] & 0x03) << 16); |
| 286 |
} |
| 287 |
|
| 288 |
inline unsigned int NES_N106::get_off (int channel) |
| 289 |
{ |
| 290 |
// 8-bit offset stored in channel reg 6 |
| 291 |
channel = channel << 3; |
| 292 |
return reg[0x46 + channel]; |
| 293 |
} |
| 294 |
|
| 295 |
inline unsigned int NES_N106::get_len (int channel) |
| 296 |
{ |
| 297 |
// 6-bit<<3 length stored obscurely in channel reg 4 |
| 298 |
channel = channel << 3; |
| 299 |
return 256 - (reg[0x44 + channel] & 0xFC); |
| 300 |
} |
| 301 |
|
| 302 |
inline int NES_N106::get_vol (int channel) |
| 303 |
{ |
| 304 |
// 4-bit volume stored in channel reg 7 |
| 305 |
channel = channel << 3; |
| 306 |
return reg[0x47 + channel] & 0x0F; |
| 307 |
} |
| 308 |
|
| 309 |
inline int NES_N106::get_sample (unsigned int index) |
| 310 |
{ |
| 311 |
// every sample becomes 2 samples in regs |
| 312 |
return (index&1) ? |
| 313 |
((reg[index>>1] >> 4) & 0x0F) : |
| 314 |
( reg[index>>1] & 0x0F) ; |
| 315 |
} |
| 316 |
|
| 317 |
inline int NES_N106::get_channels () |
| 318 |
{ |
| 319 |
// 3-bit channel count stored in reg 0x7F |
| 320 |
return ((reg[0x7F] >> 4) & 0x07) + 1; |
| 321 |
} |
| 322 |
|
| 323 |
inline void NES_N106::set_phase (unsigned int phase, int channel) |
| 324 |
{ |
| 325 |
// 24-bit phase stored in channel regs 1/3/5 |
| 326 |
channel = channel << 3; |
| 327 |
reg[0x41 + channel] = phase & 0xFF; |
| 328 |
reg[0x43 + channel] = (phase >> 8 ) & 0xFF; |
| 329 |
reg[0x45 + channel] = (phase >> 16) & 0xFF; |
| 330 |
} |
| 331 |
|
| 332 |
} //namespace |