| 1 |
// T6W28_Snd_Emu |
| 2 |
|
| 3 |
#include "T6W28_Apu.h" |
| 4 |
#include <stdlib.h> |
| 5 |
#include <assert.h> |
| 6 |
|
| 7 |
#undef require |
| 8 |
#define require( expr ) if (! (expr) ) return; |
| 9 |
|
| 10 |
/* Copyright (C) 2003-2006 Shay Green. This module is free software; you |
| 11 |
can redistribute it and/or modify it under the terms of the GNU Lesser |
| 12 |
General Public License as published by the Free Software Foundation; either |
| 13 |
version 2.1 of the License, or (at your option) any later version. This |
| 14 |
module is distributed in the hope that it will be useful, but WITHOUT ANY |
| 15 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 |
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 17 |
more details. You should have received a copy of the GNU Lesser General |
| 18 |
Public License along with this module; if not, write to the Free Software |
| 19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ |
| 20 |
|
| 21 |
// T6W28_Osc |
| 22 |
|
| 23 |
namespace MDFN_IEN_NGP |
| 24 |
{ |
| 25 |
|
| 26 |
T6W28_Osc::T6W28_Osc() |
| 27 |
{ |
| 28 |
outputs [0] = NULL; // always stays NULL |
| 29 |
outputs [1] = NULL; |
| 30 |
oscBuf=NULL; |
| 31 |
} |
| 32 |
|
| 33 |
void T6W28_Osc::reset() |
| 34 |
{ |
| 35 |
delay = 0; |
| 36 |
last_amp_left = 0; |
| 37 |
last_amp_right = 0; |
| 38 |
|
| 39 |
volume_left = 0; |
| 40 |
volume_right = 0; |
| 41 |
} |
| 42 |
|
| 43 |
// T6W28_Square |
| 44 |
|
| 45 |
void T6W28_Square::reset() |
| 46 |
{ |
| 47 |
period = 0; |
| 48 |
phase = 0; |
| 49 |
T6W28_Osc::reset(); |
| 50 |
} |
| 51 |
|
| 52 |
// <<7 shift needed |
| 53 |
void T6W28_Square::run( sms_time_t time, sms_time_t end_time ) |
| 54 |
{ |
| 55 |
if ((!volume_left && !volume_right) || period <= 128 ) |
| 56 |
{ |
| 57 |
// ignore 16kHz and higher |
| 58 |
if ( last_amp_left ) |
| 59 |
{ |
| 60 |
blip_add_delta( outputs[0], time, -last_amp_left ); |
| 61 |
last_amp_left = 0; |
| 62 |
} |
| 63 |
|
| 64 |
if ( last_amp_right ) |
| 65 |
{ |
| 66 |
blip_add_delta( outputs[1], time, -last_amp_right ); |
| 67 |
last_amp_right = 0; |
| 68 |
} |
| 69 |
|
| 70 |
oscBuf->putSample(time,0); |
| 71 |
|
| 72 |
time += delay; |
| 73 |
if ( !period ) |
| 74 |
{ |
| 75 |
time = end_time; |
| 76 |
} |
| 77 |
else if ( time < end_time ) |
| 78 |
{ |
| 79 |
// keep calculating phase |
| 80 |
int count = (end_time - time + period - 1) / period; |
| 81 |
phase = (phase + count) & 1; |
| 82 |
time += count * period; |
| 83 |
} |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
int amp_left = phase ? volume_left : -volume_left; |
| 88 |
int amp_right = phase ? volume_right : -volume_right; |
| 89 |
|
| 90 |
{ |
| 91 |
int delta_left = amp_left - last_amp_left; |
| 92 |
int delta_right = amp_right - last_amp_right; |
| 93 |
|
| 94 |
if ( delta_left ) |
| 95 |
{ |
| 96 |
last_amp_left = amp_left; |
| 97 |
blip_add_delta( outputs[0], time, delta_left ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( delta_right ) |
| 101 |
{ |
| 102 |
last_amp_right = amp_right; |
| 103 |
blip_add_delta( outputs[1], time, delta_right ); |
| 104 |
} |
| 105 |
|
| 106 |
oscBuf->putSample(time,(amp_left+amp_right)>>2); |
| 107 |
} |
| 108 |
|
| 109 |
time += delay; |
| 110 |
if ( time < end_time ) |
| 111 |
{ |
| 112 |
blip_buffer_t* const output_left = this->outputs[0]; |
| 113 |
blip_buffer_t* const output_right = this->outputs[1]; |
| 114 |
|
| 115 |
int delta_left = amp_left * 2; |
| 116 |
int delta_right = amp_right * 2; |
| 117 |
do |
| 118 |
{ |
| 119 |
delta_left = -delta_left; |
| 120 |
delta_right = -delta_right; |
| 121 |
phase ^= 1; |
| 122 |
amp_left = phase ? volume_left : -volume_left; |
| 123 |
amp_right = phase ? volume_right : -volume_right; |
| 124 |
|
| 125 |
blip_add_delta( output_left, time, delta_left ); |
| 126 |
blip_add_delta( output_right, time, delta_right ); |
| 127 |
|
| 128 |
oscBuf->putSample(time,(amp_left+amp_right)>>2); |
| 129 |
time += period; |
| 130 |
|
| 131 |
} |
| 132 |
while ( time < end_time ); |
| 133 |
|
| 134 |
this->last_amp_left = phase ? volume_left : -volume_left; |
| 135 |
this->last_amp_right = phase ? volume_right : -volume_right; |
| 136 |
} |
| 137 |
} |
| 138 |
delay = time - end_time; |
| 139 |
} |
| 140 |
|
| 141 |
// T6W28_Noise |
| 142 |
|
| 143 |
static const int noise_periods [3] = { 0x100, 0x200, 0x400 }; |
| 144 |
|
| 145 |
void T6W28_Noise::reset() |
| 146 |
{ |
| 147 |
period = &noise_periods [0]; |
| 148 |
shifter = 0xfffe; |
| 149 |
tap = 12; |
| 150 |
period_extra = 0; |
| 151 |
T6W28_Osc::reset(); |
| 152 |
} |
| 153 |
|
| 154 |
void T6W28_Noise::run( sms_time_t time, sms_time_t end_time ) |
| 155 |
{ |
| 156 |
int amp_left = volume_left; |
| 157 |
int amp_right = volume_right; |
| 158 |
|
| 159 |
if ( shifter & 1 ) |
| 160 |
{ |
| 161 |
amp_left = -amp_left; |
| 162 |
amp_right = -amp_right; |
| 163 |
} |
| 164 |
|
| 165 |
{ |
| 166 |
int delta_left = amp_left - last_amp_left; |
| 167 |
int delta_right = amp_right - last_amp_right; |
| 168 |
|
| 169 |
if ( delta_left ) |
| 170 |
{ |
| 171 |
last_amp_left = amp_left; |
| 172 |
blip_add_delta( outputs[0], time, delta_left ); |
| 173 |
} |
| 174 |
|
| 175 |
if ( delta_right ) |
| 176 |
{ |
| 177 |
last_amp_right = amp_right; |
| 178 |
blip_add_delta( outputs[1], time, delta_right ); |
| 179 |
} |
| 180 |
|
| 181 |
oscBuf->putSample(time,(amp_left+amp_right)>>2); |
| 182 |
} |
| 183 |
|
| 184 |
time += delay; |
| 185 |
|
| 186 |
if ( !volume_left && !volume_right ) |
| 187 |
time = end_time; |
| 188 |
|
| 189 |
if ( time < end_time ) |
| 190 |
{ |
| 191 |
blip_buffer_t* const output_left = this->outputs[0]; |
| 192 |
blip_buffer_t* const output_right = this->outputs[1]; |
| 193 |
|
| 194 |
unsigned l_shifter = this->shifter; |
| 195 |
int delta_left = amp_left * 2; |
| 196 |
int delta_right = amp_right * 2; |
| 197 |
|
| 198 |
int l_period = *this->period * 2; |
| 199 |
if ( !l_period ) |
| 200 |
l_period = 16; |
| 201 |
|
| 202 |
do |
| 203 |
{ |
| 204 |
int prev_l_shifter=l_shifter; |
| 205 |
if (tap==16) { |
| 206 |
l_shifter = ((l_shifter >> 14) & 1) | (l_shifter << 1); |
| 207 |
} else { |
| 208 |
l_shifter = ((((l_shifter >> 15)&1) ^ ((l_shifter >> 12)&1)) & 1) | (l_shifter << 1); |
| 209 |
} |
| 210 |
if ( (prev_l_shifter^l_shifter)&1 ) |
| 211 |
{ |
| 212 |
delta_left = -delta_left; |
| 213 |
blip_add_delta( output_left, time, delta_left ); |
| 214 |
|
| 215 |
delta_right = -delta_right; |
| 216 |
blip_add_delta( output_right, time, delta_right ); |
| 217 |
|
| 218 |
amp_left=-amp_left; |
| 219 |
amp_right=-amp_right; |
| 220 |
|
| 221 |
oscBuf->putSample(time,(amp_left+amp_right)>>2); |
| 222 |
} |
| 223 |
time += l_period; |
| 224 |
} |
| 225 |
while ( time < end_time ); |
| 226 |
|
| 227 |
this->shifter = l_shifter; |
| 228 |
this->last_amp_left = delta_left >> 1; |
| 229 |
this->last_amp_right = delta_right >> 1; |
| 230 |
} |
| 231 |
delay = time - end_time; |
| 232 |
} |
| 233 |
|
| 234 |
// T6W28_Apu |
| 235 |
|
| 236 |
T6W28_Apu::T6W28_Apu() |
| 237 |
{ |
| 238 |
for ( int i = 0; i < 3; i++ ) |
| 239 |
{ |
| 240 |
oscs [i] = &squares [i]; |
| 241 |
} |
| 242 |
oscs [3] = &noise; |
| 243 |
|
| 244 |
reset(); |
| 245 |
} |
| 246 |
|
| 247 |
T6W28_Apu::~T6W28_Apu() |
| 248 |
{ |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
void T6W28_Apu::osc_output( int index, DivDispatchOscBuffer* oscBuf ) |
| 253 |
{ |
| 254 |
require( (unsigned int) index < osc_count ); |
| 255 |
T6W28_Osc& osc = *oscs [index]; |
| 256 |
osc.oscBuf = oscBuf; |
| 257 |
} |
| 258 |
|
| 259 |
void T6W28_Apu::output( blip_buffer_t* left, blip_buffer_t* right ) |
| 260 |
{ |
| 261 |
for ( int i = 0; i < osc_count; i++ ) { |
| 262 |
T6W28_Osc& osc = *oscs [i]; |
| 263 |
osc.outputs[0]=left; |
| 264 |
osc.outputs[1]=right; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
void T6W28_Apu::reset() |
| 269 |
{ |
| 270 |
last_time = 0; |
| 271 |
latch_left = 0; |
| 272 |
latch_right = 0; |
| 273 |
|
| 274 |
squares [0].reset(); |
| 275 |
squares [1].reset(); |
| 276 |
squares [2].reset(); |
| 277 |
noise.reset(); |
| 278 |
} |
| 279 |
|
| 280 |
void T6W28_Apu::run_until( sms_time_t end_time ) |
| 281 |
{ |
| 282 |
require( end_time >= last_time ); // end_time must not be before previous time |
| 283 |
|
| 284 |
if ( end_time > last_time ) |
| 285 |
{ |
| 286 |
// run oscillators |
| 287 |
for ( int i = 0; i < osc_count; ++i ) |
| 288 |
{ |
| 289 |
T6W28_Osc& osc = *oscs [i]; |
| 290 |
if ( osc.outputs[1] ) |
| 291 |
{ |
| 292 |
if ( i < 3 ) |
| 293 |
squares [i].run( last_time, end_time ); |
| 294 |
else |
| 295 |
noise.run( last_time, end_time ); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
last_time = end_time; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
bool T6W28_Apu::end_frame( sms_time_t end_time ) |
| 304 |
{ |
| 305 |
if ( end_time > last_time ) |
| 306 |
run_until( end_time ); |
| 307 |
|
| 308 |
assert( last_time >= end_time ); |
| 309 |
last_time -= end_time; |
| 310 |
assert(last_time == 0); |
| 311 |
|
| 312 |
return(1); |
| 313 |
} |
| 314 |
|
| 315 |
static const int volumes [16] = { |
| 316 |
// volumes [i] = 64 * pow( 1.26, 15 - i ) / pow( 1.26, 15 ) |
| 317 |
64<<7, 50<<7, 39<<7, 31<<7, 24<<7, 19<<7, 15<<7, 12<<7, 9<<7, 7<<7, 5<<7, 4<<7, 3<<7, 2<<7, 1<<7, 0 |
| 318 |
}; |
| 319 |
|
| 320 |
void T6W28_Apu::write_data_left( sms_time_t time, int data ) |
| 321 |
{ |
| 322 |
require( (unsigned) data <= 0xFF ); |
| 323 |
|
| 324 |
run_until( time ); |
| 325 |
|
| 326 |
if ( data & 0x80 ) |
| 327 |
latch_left = data; |
| 328 |
|
| 329 |
int index = (latch_left >> 5) & 3; |
| 330 |
|
| 331 |
if ( latch_left & 0x10 ) |
| 332 |
{ |
| 333 |
oscs [index]->volume_left = volumes [data & 15]; |
| 334 |
} |
| 335 |
else if ( index < 3 ) |
| 336 |
{ |
| 337 |
T6W28_Square& sq = squares [index]; |
| 338 |
if ( data & 0x80 ) |
| 339 |
sq.period = (sq.period & 0xFF00) | (data << 4 & 0x00FF); |
| 340 |
else |
| 341 |
sq.period = (sq.period & 0x00FF) | (data << 8 & 0x3F00); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
void T6W28_Apu::write_data_right( sms_time_t time, int data ) |
| 346 |
{ |
| 347 |
require( (unsigned) data <= 0xFF ); |
| 348 |
|
| 349 |
run_until( time ); |
| 350 |
|
| 351 |
if ( data & 0x80 ) |
| 352 |
latch_right = data; |
| 353 |
|
| 354 |
int index = (latch_right >> 5) & 3; |
| 355 |
//printf("%d\n", index); |
| 356 |
|
| 357 |
if ( latch_right & 0x10 ) |
| 358 |
{ |
| 359 |
oscs [index]->volume_right = volumes [data & 15]; |
| 360 |
} |
| 361 |
else if ( index == 2 ) |
| 362 |
{ |
| 363 |
if ( data & 0x80 ) |
| 364 |
noise.period_extra = (noise.period_extra & 0xFF00) | (data << 4 & 0x00FF); |
| 365 |
else |
| 366 |
noise.period_extra = (noise.period_extra & 0x00FF) | (data << 8 & 0x3F00); |
| 367 |
} |
| 368 |
else if(index == 3) |
| 369 |
{ |
| 370 |
int select = data & 3; |
| 371 |
if ( select < 3 ) |
| 372 |
noise.period = &noise_periods [select]; |
| 373 |
else |
| 374 |
noise.period = &noise.period_extra; |
| 375 |
|
| 376 |
int const tap_disabled = 16; |
| 377 |
noise.tap = (data & 0x04) ? 12 : tap_disabled; |
| 378 |
noise.shifter = 0xfffe; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
} |