| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* Original author: Piotr Fusik (http://asap.sourceforge.net) |
| 6 |
* Rewritten based on Mikey emulation by Waldemar Pawlaszek |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License along |
| 19 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 20 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "AltASAP.hpp" |
| 24 |
#include <array> |
| 25 |
#include <vector> |
| 26 |
#include <cassert> |
| 27 |
#include <algorithm> |
| 28 |
#include <limits> |
| 29 |
|
| 30 |
namespace AltASAP |
| 31 |
{ |
| 32 |
|
| 33 |
namespace |
| 34 |
{ |
| 35 |
|
| 36 |
static constexpr int64_t CNT_MAX = std::numeric_limits<int64_t>::max() & ~7; |
| 37 |
static constexpr int MuteFrequency = 1; |
| 38 |
static constexpr int MuteInit = 2; |
| 39 |
static constexpr int MuteSerialInput = 8; |
| 40 |
//just some magick value to match the audio level of mzpokeysnd |
| 41 |
static constexpr int16_t MAGICK_VOLUME_BOOSTER = 160; |
| 42 |
static constexpr int16_t MAGICK_OSC_VOLUME_BOOSTER = 6; |
| 43 |
|
| 44 |
struct PokeyBase |
| 45 |
{ |
| 46 |
int64_t mPolyIndex; |
| 47 |
int mAudctl; |
| 48 |
int mSkctl; |
| 49 |
bool mInit; |
| 50 |
std::array<uint8_t, 511> mPoly9Lookup; |
| 51 |
std::array<uint8_t, 16385> mPoly17Lookup; |
| 52 |
|
| 53 |
|
| 54 |
PokeyBase() : mPolyIndex{ 15 * 31 * 131071 }, mAudctl{ 0 }, mSkctl{ 3 }, mInit{ false }, mPoly9Lookup{}, mPoly17Lookup{} |
| 55 |
{ |
| 56 |
int reg = 0x1ff; |
| 57 |
for ( int i = 0; i < 511; i++ ) |
| 58 |
{ |
| 59 |
reg = ( ( ( reg >> 5 ^ reg ) & 1 ) << 8 ) + ( reg >> 1 ); |
| 60 |
mPoly9Lookup[i] = reg & 0xff; |
| 61 |
} |
| 62 |
reg = 0x1ffff; |
| 63 |
for ( int i = 0; i < 16385; i++ ) |
| 64 |
{ |
| 65 |
reg = ( ( ( reg >> 5 ^ reg ) & 0xff ) << 9 ) + ( reg >> 8 ); |
| 66 |
mPoly17Lookup[i] = reg >> 1 & 0xff; |
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
/* |
| 74 |
"Queue" holding event timepoints. |
| 75 |
- 4 channel timer fire points |
| 76 |
- 1 sample point |
| 77 |
Three LSBs are used to encode event kind: 0-3 are channels, 4 is sampling. |
| 78 |
Channel sequence is 2,3,0,1 |
| 79 |
*/ |
| 80 |
class ActionQueue |
| 81 |
{ |
| 82 |
public: |
| 83 |
|
| 84 |
ActionQueue() : mTab{ CNT_MAX | 0, CNT_MAX | 1, CNT_MAX | 2, CNT_MAX | 3, CNT_MAX | 4 } |
| 85 |
{ |
| 86 |
} |
| 87 |
|
| 88 |
void insert( int idx, int64_t value ) |
| 89 |
{ |
| 90 |
assert( idx < (int)mTab.size() ); |
| 91 |
idx ^= 2; |
| 92 |
mTab[idx] = value | idx; |
| 93 |
} |
| 94 |
|
| 95 |
void insertSampling( int64_t value ) |
| 96 |
{ |
| 97 |
mTab[4] = value | 4; |
| 98 |
} |
| 99 |
|
| 100 |
void disable( int idx ) |
| 101 |
{ |
| 102 |
assert( idx < (int)mTab.size() ); |
| 103 |
idx ^= 2; |
| 104 |
mTab[idx] = CNT_MAX | idx; |
| 105 |
} |
| 106 |
|
| 107 |
bool enqueued( int idx ) |
| 108 |
{ |
| 109 |
assert( idx < (int)mTab.size() ); |
| 110 |
idx ^= 2; |
| 111 |
return mTab[idx] < CNT_MAX; |
| 112 |
} |
| 113 |
|
| 114 |
int64_t pop() |
| 115 |
{ |
| 116 |
int64_t min1 = std::min( mTab[0], mTab[1] ); |
| 117 |
int64_t min2 = std::min( mTab[2], mTab[3] ); |
| 118 |
int64_t min3 = std::min( min1, mTab[4] ); |
| 119 |
int64_t min4 = std::min( min2, min3 ); |
| 120 |
|
| 121 |
return min4 ^ 2; |
| 122 |
} |
| 123 |
|
| 124 |
private: |
| 125 |
std::array<int64_t, 5> mTab; |
| 126 |
}; |
| 127 |
|
| 128 |
class AudioChannel |
| 129 |
{ |
| 130 |
public: |
| 131 |
AudioChannel( ActionQueue& queue, uint32_t number ) : mQueue{ queue }, mNumber{ number }, mAudf{ 0 }, mAudc{ 0 }, mPeriodCycles{ 28 }, mMute{ MuteFrequency }, mDelta{ 0 }, mOut{ 0 } |
| 132 |
{ |
| 133 |
} |
| 134 |
|
| 135 |
int16_t getOutput() const |
| 136 |
{ |
| 137 |
return (int16_t)mDelta; |
| 138 |
} |
| 139 |
|
| 140 |
void fillRegisterPool( uint8_t* regs ) |
| 141 |
{ |
| 142 |
regs[0] = (uint8_t)mAudf; |
| 143 |
regs[1] = (uint8_t)mAudc; |
| 144 |
} |
| 145 |
|
| 146 |
void renewTrigger( int64_t cycle ) |
| 147 |
{ |
| 148 |
overrideTrigger( cycle + mPeriodCycles ); |
| 149 |
} |
| 150 |
|
| 151 |
void overrideTrigger( int64_t cycle ) |
| 152 |
{ |
| 153 |
mQueue.insert( mNumber, cycle << 3 ); |
| 154 |
} |
| 155 |
|
| 156 |
void doStimer( int64_t cycle ) |
| 157 |
{ |
| 158 |
if ( mQueue.enqueued( mNumber ) ) |
| 159 |
renewTrigger( cycle ); |
| 160 |
} |
| 161 |
|
| 162 |
void trigger( int64_t cycle, PokeyBase const& pokey ) |
| 163 |
{ |
| 164 |
renewTrigger( cycle ); |
| 165 |
if ( ( mAudc & 0xb0 ) == 0xa0 ) |
| 166 |
mOut ^= 1; |
| 167 |
else if ( ( mAudc & 0x10 ) != 0 || pokey.mInit ) |
| 168 |
return; |
| 169 |
else |
| 170 |
{ |
| 171 |
int64_t poly = cycle + pokey.mPolyIndex - mNumber; |
| 172 |
if ( mAudc < 0x80 && ( 0x65bd44e0 & 1 << ( poly % 31 ) ) == 0 ) // 0000011100100010101111011010011 |
| 173 |
return; |
| 174 |
if ( ( mAudc & 0x20 ) != 0 ) |
| 175 |
mOut ^= 1; |
| 176 |
else |
| 177 |
{ |
| 178 |
uint32_t newOut; |
| 179 |
if ( ( mAudc & 0x40 ) != 0 ) |
| 180 |
newOut = 0x5370u >> (int)( poly % 15 ); // 000011101100101 |
| 181 |
else if ( pokey.mAudctl < 0x80 ) |
| 182 |
{ |
| 183 |
poly %= 131071; |
| 184 |
newOut = pokey.mPoly17Lookup[poly >> 3] >> ( poly & 7 ); |
| 185 |
} |
| 186 |
else |
| 187 |
newOut = pokey.mPoly9Lookup[poly % 511]; |
| 188 |
newOut &= 1; |
| 189 |
if ( mOut == newOut ) |
| 190 |
return; |
| 191 |
mOut = newOut; |
| 192 |
} |
| 193 |
} |
| 194 |
toggle(); |
| 195 |
} |
| 196 |
|
| 197 |
uint32_t mute() const |
| 198 |
{ |
| 199 |
return mMute; |
| 200 |
} |
| 201 |
|
| 202 |
uint32_t audf() const |
| 203 |
{ |
| 204 |
return mAudf; |
| 205 |
} |
| 206 |
|
| 207 |
uint32_t audc() const |
| 208 |
{ |
| 209 |
return mAudc; |
| 210 |
} |
| 211 |
|
| 212 |
void setAudf( uint32_t value ) |
| 213 |
{ |
| 214 |
mAudf = value; |
| 215 |
} |
| 216 |
|
| 217 |
void setAudc( int64_t cycle, uint32_t value ) |
| 218 |
{ |
| 219 |
if ( mAudc == value ) |
| 220 |
return; |
| 221 |
mAudc = value; |
| 222 |
int32_t volume = value & 0x0f; |
| 223 |
if ( ( value & 0x10 ) != 0 ) |
| 224 |
{ |
| 225 |
mDelta = volume * MAGICK_VOLUME_BOOSTER; |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
muteUltrasound( cycle ); |
| 230 |
if ( mDelta > 0 ) |
| 231 |
mDelta = volume * MAGICK_VOLUME_BOOSTER; |
| 232 |
else |
| 233 |
mDelta = -volume * MAGICK_VOLUME_BOOSTER; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void toggle() |
| 238 |
{ |
| 239 |
mDelta = -mDelta; |
| 240 |
} |
| 241 |
|
| 242 |
void setPeriodCycles( int64_t value ) |
| 243 |
{ |
| 244 |
mPeriodCycles = value; |
| 245 |
} |
| 246 |
|
| 247 |
void setMute( bool enable, int mask, int64_t cycle ) |
| 248 |
{ |
| 249 |
if ( enable ) |
| 250 |
{ |
| 251 |
mMute |= mask; |
| 252 |
mQueue.disable( mNumber ); |
| 253 |
} |
| 254 |
else |
| 255 |
{ |
| 256 |
mMute &= ~mask; |
| 257 |
if ( mMute == 0 && !mQueue.enqueued( mNumber ) ) |
| 258 |
overrideTrigger( cycle ); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
void muteUltrasound( int64_t cycle ) |
| 263 |
{ |
| 264 |
static constexpr int UltrasoundCycles = 112; |
| 265 |
setMute( mPeriodCycles <= UltrasoundCycles && ( mAudc & 0xb0 ) == 0xa0, MuteFrequency, cycle ); |
| 266 |
} |
| 267 |
|
| 268 |
private: |
| 269 |
ActionQueue& mQueue; |
| 270 |
uint32_t mNumber; |
| 271 |
uint32_t mAudf; |
| 272 |
uint32_t mAudc; |
| 273 |
int64_t mPeriodCycles; |
| 274 |
|
| 275 |
uint32_t mMute; |
| 276 |
int32_t mDelta; |
| 277 |
uint32_t mOut; |
| 278 |
}; |
| 279 |
|
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
class PokeyPimpl : public PokeyBase |
| 284 |
{ |
| 285 |
public: |
| 286 |
|
| 287 |
PokeyPimpl( uint32_t pokeyClock, uint32_t sampleRate ) : PokeyBase{}, mQueue{ std::make_unique<ActionQueue>() }, |
| 288 |
mAudioChannels{ AudioChannel{ *mQueue, 0u }, AudioChannel{ *mQueue, 1u }, AudioChannel{ *mQueue, 2u }, AudioChannel{ *mQueue, 3u } }, |
| 289 |
mRegisterPool{}, mTick{}, mNextTick{}, |
| 290 |
mReloadCycles1{ 28 }, mReloadCycles3{ 28 }, mDivCycles{ 28 }, mSampleRate{ sampleRate }, mSamplesRemainder{}, |
| 291 |
mTicksPerSample{ ( pokeyClock * 8 ) / mSampleRate, ( pokeyClock * 8 ) % mSampleRate } |
| 292 |
{ |
| 293 |
std::fill_n( mRegisterPool.data(), mRegisterPool.size(), (uint8_t)0xff ); |
| 294 |
enqueueSampling(); |
| 295 |
} |
| 296 |
|
| 297 |
~PokeyPimpl() {} |
| 298 |
|
| 299 |
void write( uint8_t address, uint8_t value ) |
| 300 |
{ |
| 301 |
auto cycle = mTick >> 3; |
| 302 |
|
| 303 |
switch ( address & 0xf ) |
| 304 |
{ |
| 305 |
case 0x00: |
| 306 |
if ( value == mAudioChannels[0].audf() ) |
| 307 |
break; |
| 308 |
mAudioChannels[0].setAudf( value ); |
| 309 |
switch ( mAudctl & 0x50 ) |
| 310 |
{ |
| 311 |
case 0x00: |
| 312 |
mAudioChannels[0].setPeriodCycles( mDivCycles * ( value + 1 ) ); |
| 313 |
break; |
| 314 |
case 0x10: |
| 315 |
mAudioChannels[1].setPeriodCycles( mDivCycles * ( value + ( mAudioChannels[1].audf() << 8 ) + 1 ) ); |
| 316 |
mReloadCycles1 = mDivCycles * ( value + 1 ); |
| 317 |
mAudioChannels[1].muteUltrasound( cycle ); |
| 318 |
break; |
| 319 |
case 0x40: |
| 320 |
mAudioChannels[0].setPeriodCycles( value + 4 ); |
| 321 |
break; |
| 322 |
case 0x50: |
| 323 |
mAudioChannels[1].setPeriodCycles( value + ( mAudioChannels[1].audf() << 8 ) + 7 ); |
| 324 |
mReloadCycles1 = value + 4; |
| 325 |
mAudioChannels[1].muteUltrasound( cycle ); |
| 326 |
break; |
| 327 |
default: |
| 328 |
assert( false ); |
| 329 |
} |
| 330 |
mAudioChannels[0].muteUltrasound( cycle ); |
| 331 |
break; |
| 332 |
case 0x01: |
| 333 |
mAudioChannels[0].setAudc( cycle, value ); |
| 334 |
break; |
| 335 |
case 0x02: |
| 336 |
if ( value == mAudioChannels[1].audf() ) |
| 337 |
break; |
| 338 |
mAudioChannels[1].setAudf( value ); |
| 339 |
switch ( mAudctl & 0x50 ) |
| 340 |
{ |
| 341 |
case 0x00: |
| 342 |
case 0x40: |
| 343 |
mAudioChannels[1].setPeriodCycles( mDivCycles * ( value + 1 ) ); |
| 344 |
break; |
| 345 |
case 0x10: |
| 346 |
mAudioChannels[1].setPeriodCycles( mDivCycles * ( mAudioChannels[0].audf() + ( value << 8 ) + 1 ) ); |
| 347 |
break; |
| 348 |
case 0x50: |
| 349 |
mAudioChannels[1].setPeriodCycles( mAudioChannels[0].audf() + ( value << 8 ) + 7 ); |
| 350 |
break; |
| 351 |
default: |
| 352 |
assert( false ); |
| 353 |
} |
| 354 |
mAudioChannels[1].muteUltrasound( cycle ); |
| 355 |
break; |
| 356 |
case 0x03: |
| 357 |
mAudioChannels[1].setAudc( cycle, value ); |
| 358 |
break; |
| 359 |
case 0x04: |
| 360 |
if ( value == mAudioChannels[2].audf() ) |
| 361 |
break; |
| 362 |
mAudioChannels[2].setAudf( value ); |
| 363 |
switch ( mAudctl & 0x28 ) |
| 364 |
{ |
| 365 |
case 0x00: |
| 366 |
mAudioChannels[2].setPeriodCycles( mDivCycles * ( value + 1 ) ); |
| 367 |
break; |
| 368 |
case 0x08: |
| 369 |
mAudioChannels[3].setPeriodCycles( mDivCycles * ( value + ( mAudioChannels[3].audf() << 8 ) + 1 ) ); |
| 370 |
mReloadCycles3 = mDivCycles * ( value + 1 ); |
| 371 |
mAudioChannels[3].muteUltrasound( cycle ); |
| 372 |
break; |
| 373 |
case 0x20: |
| 374 |
mAudioChannels[2].setPeriodCycles( value + 4 ); |
| 375 |
break; |
| 376 |
case 0x28: |
| 377 |
mAudioChannels[3].setPeriodCycles( value + ( mAudioChannels[3].audf() << 8 ) + 7 ); |
| 378 |
mReloadCycles3 = value + 4; |
| 379 |
mAudioChannels[3].muteUltrasound( cycle ); |
| 380 |
break; |
| 381 |
default: |
| 382 |
assert( false ); |
| 383 |
} |
| 384 |
mAudioChannels[2].muteUltrasound( cycle ); |
| 385 |
break; |
| 386 |
case 0x05: |
| 387 |
mAudioChannels[2].setAudc( cycle, value ); |
| 388 |
break; |
| 389 |
case 0x06: |
| 390 |
if ( value == mAudioChannels[3].audf() ) |
| 391 |
break; |
| 392 |
mAudioChannels[3].setAudf( value ); |
| 393 |
switch ( mAudctl & 0x28 ) |
| 394 |
{ |
| 395 |
case 0x00: |
| 396 |
case 0x20: |
| 397 |
mAudioChannels[3].setPeriodCycles( mDivCycles * ( value + 1 ) ); |
| 398 |
break; |
| 399 |
case 0x08: |
| 400 |
mAudioChannels[3].setPeriodCycles( mDivCycles * ( mAudioChannels[2].audf() + ( value << 8 ) + 1 ) ); |
| 401 |
break; |
| 402 |
case 0x28: |
| 403 |
mAudioChannels[3].setPeriodCycles( mAudioChannels[2].audf() + ( value << 8 ) + 7 ); |
| 404 |
break; |
| 405 |
default: |
| 406 |
assert( false ); |
| 407 |
} |
| 408 |
mAudioChannels[3].muteUltrasound( cycle ); |
| 409 |
break; |
| 410 |
case 0x07: |
| 411 |
mAudioChannels[3].setAudc( cycle, value ); |
| 412 |
break; |
| 413 |
case 0x08: |
| 414 |
if ( value == mAudctl ) |
| 415 |
break; |
| 416 |
mAudctl = value; |
| 417 |
mDivCycles = ( value & 1 ) != 0 ? 114 : 28; |
| 418 |
switch ( value & 0x50 ) |
| 419 |
{ |
| 420 |
case 0x00: |
| 421 |
mAudioChannels[0].setPeriodCycles( mDivCycles * ( mAudioChannels[0].audf() + 1 ) ); |
| 422 |
mAudioChannels[1].setPeriodCycles( mDivCycles * ( mAudioChannels[1].audf() + 1 ) ); |
| 423 |
break; |
| 424 |
case 0x10: |
| 425 |
mAudioChannels[0].setPeriodCycles( (int64_t)mDivCycles << 8 ); |
| 426 |
mAudioChannels[1].setPeriodCycles( mDivCycles * ( mAudioChannels[0].audf() + ( mAudioChannels[1].audf() << 8 ) + 1 ) ); |
| 427 |
mReloadCycles1 = mDivCycles * ( mAudioChannels[0].audf() + 1 ); |
| 428 |
break; |
| 429 |
case 0x40: |
| 430 |
mAudioChannels[0].setPeriodCycles( mAudioChannels[0].audf() + 4 ); |
| 431 |
mAudioChannels[1].setPeriodCycles( mDivCycles * ( mAudioChannels[1].audf() + 1 ) ); |
| 432 |
break; |
| 433 |
case 0x50: |
| 434 |
mAudioChannels[0].setPeriodCycles( 256 ); |
| 435 |
mAudioChannels[1].setPeriodCycles( mAudioChannels[0].audf() + ( mAudioChannels[1].audf() << 8 ) + 7 ); |
| 436 |
mReloadCycles1 = mAudioChannels[0].audf() + 4; |
| 437 |
break; |
| 438 |
default: |
| 439 |
assert( false ); |
| 440 |
} |
| 441 |
mAudioChannels[0].muteUltrasound( cycle ); |
| 442 |
mAudioChannels[1].muteUltrasound( cycle ); |
| 443 |
switch ( value & 0x28 ) |
| 444 |
{ |
| 445 |
case 0x00: |
| 446 |
mAudioChannels[2].setPeriodCycles( mDivCycles * ( mAudioChannels[2].audf() + 1 ) ); |
| 447 |
mAudioChannels[3].setPeriodCycles( mDivCycles * ( mAudioChannels[3].audf() + 1 ) ); |
| 448 |
break; |
| 449 |
case 0x08: |
| 450 |
mAudioChannels[2].setPeriodCycles( (int64_t)mDivCycles << 8 ); |
| 451 |
mAudioChannels[3].setPeriodCycles( mDivCycles * ( mAudioChannels[2].audf() + ( mAudioChannels[3].audf() << 8 ) + 1 ) ); |
| 452 |
mReloadCycles3 = mDivCycles * ( mAudioChannels[2].audf() + 1 ); |
| 453 |
break; |
| 454 |
case 0x20: |
| 455 |
mAudioChannels[2].setPeriodCycles( mAudioChannels[2].audf() + 4 ); |
| 456 |
mAudioChannels[3].setPeriodCycles( mDivCycles * ( mAudioChannels[3].audf() + 1 ) ); |
| 457 |
break; |
| 458 |
case 0x28: |
| 459 |
mAudioChannels[2].setPeriodCycles( 256 ); |
| 460 |
mAudioChannels[3].setPeriodCycles( mAudioChannels[2].audf() + ( mAudioChannels[3].audf() << 8 ) + 7 ); |
| 461 |
mReloadCycles3 = mAudioChannels[2].audf() + 4; |
| 462 |
break; |
| 463 |
default: |
| 464 |
assert( false ); |
| 465 |
} |
| 466 |
mAudioChannels[2].muteUltrasound( cycle ); |
| 467 |
mAudioChannels[3].muteUltrasound( cycle ); |
| 468 |
initMute( cycle ); |
| 469 |
break; |
| 470 |
case 0x09: |
| 471 |
for ( int i = 0; i < 4; i++ ) |
| 472 |
mAudioChannels[i].doStimer( cycle ); |
| 473 |
break; |
| 474 |
case 0x0f: |
| 475 |
{ |
| 476 |
if ( value == mSkctl ) |
| 477 |
break; |
| 478 |
mSkctl = value; |
| 479 |
bool init = ( value & 3 ) == 0; |
| 480 |
if ( mInit && !init ) |
| 481 |
mPolyIndex = ( ( mAudctl & 0x80 ) != 0 ? 15 * 31 * 511 - 1 : 15 * 31 * 131071 - 1 ) - cycle; |
| 482 |
mInit = init; |
| 483 |
initMute( cycle ); |
| 484 |
mAudioChannels[2].setMute( ( value & 0x10 ) != 0, MuteSerialInput, cycle ); |
| 485 |
mAudioChannels[3].setMute( ( value & 0x10 ) != 0, MuteSerialInput, cycle ); |
| 486 |
break; |
| 487 |
} |
| 488 |
default: |
| 489 |
break; |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
int16_t sampleAudio( short* oscb ) |
| 494 |
{ |
| 495 |
for ( ;; ) |
| 496 |
{ |
| 497 |
int64_t value = mQueue->pop(); |
| 498 |
if ( ( value & 7 ) == 6 ) // 6 == 4 ^ 2 |
| 499 |
{ |
| 500 |
int16_t ch0 = mAudioChannels[0].getOutput(); |
| 501 |
int16_t ch1 = mAudioChannels[1].getOutput(); |
| 502 |
int16_t ch2 = mAudioChannels[2].getOutput(); |
| 503 |
int16_t ch3 = mAudioChannels[3].getOutput(); |
| 504 |
|
| 505 |
if ( oscb != nullptr ) |
| 506 |
{ |
| 507 |
oscb[0]=ch0 * MAGICK_OSC_VOLUME_BOOSTER; |
| 508 |
oscb[1]=ch1 * MAGICK_OSC_VOLUME_BOOSTER; |
| 509 |
oscb[2]=ch2 * MAGICK_OSC_VOLUME_BOOSTER; |
| 510 |
oscb[3]=ch3 * MAGICK_OSC_VOLUME_BOOSTER; |
| 511 |
} |
| 512 |
|
| 513 |
enqueueSampling(); |
| 514 |
return ch0 + ch1 + ch2 + ch3; |
| 515 |
} |
| 516 |
else |
| 517 |
{ |
| 518 |
fireTimer( value ); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
uint8_t const* getRegisterPool() |
| 524 |
{ |
| 525 |
for ( size_t i = 0; i < mAudioChannels.size(); ++i ) |
| 526 |
{ |
| 527 |
mAudioChannels[i].fillRegisterPool( mRegisterPool.data() + 2 * i ); |
| 528 |
} |
| 529 |
|
| 530 |
mRegisterPool[8] = mAudctl; |
| 531 |
|
| 532 |
return mRegisterPool.data(); |
| 533 |
} |
| 534 |
|
| 535 |
private: |
| 536 |
|
| 537 |
void initMute( int64_t cycle ) |
| 538 |
{ |
| 539 |
mAudioChannels[0].setMute( mInit && ( mAudctl & 0x40 ) == 0, MuteInit, cycle ); |
| 540 |
mAudioChannels[1].setMute( mInit && ( mAudctl & 0x50 ) != 0x50, MuteInit, cycle ); |
| 541 |
mAudioChannels[2].setMute( mInit && ( mAudctl & 0x20 ) == 0, MuteInit, cycle ); |
| 542 |
mAudioChannels[3].setMute( mInit && ( mAudctl & 0x28 ) != 0x28, MuteInit, cycle ); |
| 543 |
} |
| 544 |
|
| 545 |
void fireTimer( int64_t tick ) |
| 546 |
{ |
| 547 |
mTick = tick & ~7; |
| 548 |
size_t ch = tick & 3; |
| 549 |
auto cycle = tick >> 3; |
| 550 |
|
| 551 |
switch ( ch ) |
| 552 |
{ |
| 553 |
case 0: |
| 554 |
if ( ( mSkctl & 0x88 ) == 8 ) // two-tone, sending 1 (i.e. timer1) |
| 555 |
mAudioChannels[1].renewTrigger( cycle ); |
| 556 |
mAudioChannels[0].trigger( cycle, *this ); |
| 557 |
break; |
| 558 |
case 1: |
| 559 |
if ( ( mAudctl & 0x10 ) != 0 ) |
| 560 |
mAudioChannels[0].overrideTrigger( cycle + mReloadCycles1 ); |
| 561 |
else if ( ( mSkctl & 8 ) != 0 ) // two-tone |
| 562 |
mAudioChannels[0].renewTrigger( cycle ); |
| 563 |
mAudioChannels[1].trigger( cycle, *this ); |
| 564 |
break; |
| 565 |
case 2: |
| 566 |
if ( ( mAudctl & 4 ) != 0 && mAudioChannels[0].getOutput() > 0 && mAudioChannels[0].mute() == 0 ) |
| 567 |
mAudioChannels[0].toggle(); |
| 568 |
mAudioChannels[2].trigger( cycle, *this ); |
| 569 |
break; |
| 570 |
case 3: |
| 571 |
if ( ( mAudctl & 8 ) != 0 ) |
| 572 |
mAudioChannels[2].overrideTrigger( cycle + mReloadCycles3 ); |
| 573 |
if ( ( mAudctl & 2 ) != 0 && mAudioChannels[1].getOutput() > 0 && mAudioChannels[1].mute() == 0 ) |
| 574 |
mAudioChannels[1].toggle(); |
| 575 |
mAudioChannels[3].trigger( cycle, *this ); |
| 576 |
break; |
| 577 |
default: |
| 578 |
break; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
void enqueueSampling() |
| 583 |
{ |
| 584 |
mTick = mNextTick & ~7; |
| 585 |
mNextTick = mNextTick + mTicksPerSample.first; |
| 586 |
mSamplesRemainder += mTicksPerSample.second; |
| 587 |
if ( mSamplesRemainder > mSampleRate ) |
| 588 |
{ |
| 589 |
mSamplesRemainder %= mSampleRate; |
| 590 |
mNextTick += 1; |
| 591 |
} |
| 592 |
|
| 593 |
mQueue->insertSampling( mNextTick & ~7 ); |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
private: |
| 598 |
|
| 599 |
std::unique_ptr<ActionQueue> mQueue; |
| 600 |
std::array<AudioChannel, 4> mAudioChannels; |
| 601 |
|
| 602 |
std::array<uint8_t, 4 * 2 + 1> mRegisterPool; |
| 603 |
|
| 604 |
uint64_t mTick; |
| 605 |
uint64_t mNextTick; |
| 606 |
int64_t mReloadCycles1; |
| 607 |
int64_t mReloadCycles3; |
| 608 |
int64_t mDivCycles; |
| 609 |
uint32_t mSampleRate; |
| 610 |
uint32_t mSamplesRemainder; |
| 611 |
std::pair<uint32_t, uint32_t> mTicksPerSample; |
| 612 |
}; |
| 613 |
|
| 614 |
//Initializing periods with safe defaults |
| 615 |
Pokey::Pokey() : mPokeyClock{ (uint32_t)3579545.4545454545454545454545454545454644484879993035705375307 / 2 }, mSampleRate{ mPokeyClock / 7 }, mPokey{} |
| 616 |
{ |
| 617 |
} |
| 618 |
|
| 619 |
void Pokey::init( uint32_t pokeyClock, uint32_t sampleRate ) |
| 620 |
{ |
| 621 |
mPokey.reset(); |
| 622 |
mPokeyClock = pokeyClock; |
| 623 |
mSampleRate = sampleRate; |
| 624 |
} |
| 625 |
|
| 626 |
void Pokey::reset() |
| 627 |
{ |
| 628 |
mPokey = std::make_unique<PokeyPimpl>( mPokeyClock, mSampleRate ); |
| 629 |
} |
| 630 |
|
| 631 |
Pokey::~Pokey() |
| 632 |
{ |
| 633 |
} |
| 634 |
|
| 635 |
void Pokey::write( uint8_t address, uint8_t value ) |
| 636 |
{ |
| 637 |
assert( mPokey ); |
| 638 |
mPokey->write( address, value ); |
| 639 |
} |
| 640 |
|
| 641 |
int16_t Pokey::sampleAudio( short* oscb ) |
| 642 |
{ |
| 643 |
assert( mPokey ); |
| 644 |
return mPokey->sampleAudio( oscb ); |
| 645 |
} |
| 646 |
|
| 647 |
uint8_t const* Pokey::getRegisterPool() |
| 648 |
{ |
| 649 |
assert( mPokey ); |
| 650 |
return mPokey->getRegisterPool(); |
| 651 |
} |
| 652 |
|
| 653 |
} |