| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "sm8521.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include <math.h> |
| 23 |
|
| 24 |
//#define rWrite(a,v) pendingWrites[a]=v; |
| 25 |
#define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} } |
| 26 |
|
| 27 |
#define CHIP_DIVIDER 32 |
| 28 |
|
| 29 |
const char* regCheatSheetSM8521[]={ |
| 30 |
"SGC", "40", |
| 31 |
"SG0L", "42", |
| 32 |
"SG1L", "44", |
| 33 |
"SG0TL", "46", |
| 34 |
"SG0TH", "47", |
| 35 |
"SG1TL", "48", |
| 36 |
"SG1TH", "49", |
| 37 |
"SG2L", "4A", |
| 38 |
"SG2TL", "4C", |
| 39 |
"SG2TH", "4D", |
| 40 |
"SGDA", "4E", |
| 41 |
"SG0Wn", "60+n", |
| 42 |
"SG1Wn", "70+n", |
| 43 |
NULL |
| 44 |
}; |
| 45 |
|
| 46 |
const char** DivPlatformSM8521::getRegisterSheet() { |
| 47 |
return regCheatSheetSM8521; |
| 48 |
} |
| 49 |
|
| 50 |
void DivPlatformSM8521::acquireDirect(blip_buffer_t** bb, size_t len) { |
| 51 |
while (!writes.empty()) { |
| 52 |
QueuedWrite w=writes.front(); |
| 53 |
sm8521_write(&sm8521,w.addr,w.val); |
| 54 |
regPool[w.addr&0xff]=w.val; |
| 55 |
writes.pop(); |
| 56 |
} |
| 57 |
|
| 58 |
for (int i=0; i<3; i++) { |
| 59 |
oscBuf[i]->begin(len); |
| 60 |
} |
| 61 |
|
| 62 |
for (size_t h=0; h<len; h++) { |
| 63 |
int advance=len-h; |
| 64 |
if (sm8521.sgc&1) { |
| 65 |
const int remain=(sm8521.sg[0].base.t+1)-sm8521.sg[0].base.counter; |
| 66 |
if (remain<advance) advance=remain; |
| 67 |
} |
| 68 |
if (sm8521.sgc&2) { |
| 69 |
const int remain=(sm8521.sg[1].base.t+1)-sm8521.sg[1].base.counter; |
| 70 |
if (remain<advance) advance=remain; |
| 71 |
} |
| 72 |
if (sm8521.sgc&4) { |
| 73 |
const int remain=(sm8521.noise.base.t+1)-sm8521.noise.base.counter; |
| 74 |
if (remain<advance) advance=remain; |
| 75 |
} |
| 76 |
if (advance<1) advance=1; |
| 77 |
sm8521_sound_tick(&sm8521,advance); |
| 78 |
|
| 79 |
h+=advance-1; |
| 80 |
|
| 81 |
int out=sm8521.out<<6; |
| 82 |
if (out!=lastOut) { |
| 83 |
blip_add_delta(bb[0],h,out-lastOut); |
| 84 |
lastOut=out; |
| 85 |
} |
| 86 |
for (int i=0; i<2; i++) { |
| 87 |
oscBuf[i]->putSample(h,sm8521.sg[i].base.out<<7); |
| 88 |
} |
| 89 |
oscBuf[2]->putSample(h,sm8521.noise.base.out<<7); |
| 90 |
} |
| 91 |
|
| 92 |
for (int i=0; i<3; i++) { |
| 93 |
oscBuf[i]->end(len); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
void DivPlatformSM8521::updateWave(int ch) { |
| 98 |
if (ch<2) { |
| 99 |
const unsigned char temp=regPool[0x40]; |
| 100 |
rWrite(0x40,temp&~(1<<ch)); |
| 101 |
for (int i=0; i<16; i++) { |
| 102 |
int nibble1=(chan[ch].ws.output[((i<<1)+chan[ch].antiClickWavePos-1)&31]-8)&0xf; |
| 103 |
int nibble2=(chan[ch].ws.output[((1+(i<<1))+chan[ch].antiClickWavePos-1)&31]-8)&0xf; |
| 104 |
rWrite(0x60+i+(ch*16),(nibble2<<4)|nibble1); |
| 105 |
} |
| 106 |
if (chan[ch].active) { |
| 107 |
rWrite(0x40,temp|(1<<ch)); |
| 108 |
} |
| 109 |
chan[ch].antiClickWavePos&=31; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
void DivPlatformSM8521::tick(bool sysTick) { |
| 114 |
unsigned char keyState=0x80; |
| 115 |
for (int i=0; i<3; i++) { |
| 116 |
// anti-click |
| 117 |
/* |
| 118 |
if (antiClickEnabled && sysTick && chan[i].freq>0) { |
| 119 |
chan[i].antiClickPeriodCount+=(chipClock/MAX(parent->getCurHz(),1.0f)); |
| 120 |
chan[i].antiClickWavePos+=chan[i].antiClickPeriodCount/chan[i].freq; |
| 121 |
chan[i].antiClickPeriodCount%=chan[i].freq; |
| 122 |
} |
| 123 |
*/ |
| 124 |
|
| 125 |
chan[i].std.next(); |
| 126 |
if (chan[i].std.vol.had) { |
| 127 |
chan[i].outVol=((chan[i].vol&31)*MIN(31,chan[i].std.vol.val))>>5; |
| 128 |
if (!isMuted[i]) { |
| 129 |
chan[i].volumeChanged=true; |
| 130 |
} |
| 131 |
} |
| 132 |
if (NEW_ARP_STRAT) { |
| 133 |
chan[i].handleArp(); |
| 134 |
} else if (chan[i].std.arp.had) { |
| 135 |
if (!chan[i].inPorta) { |
| 136 |
chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val)); |
| 137 |
} |
| 138 |
chan[i].freqChanged=true; |
| 139 |
} |
| 140 |
if (chan[i].volumeChanged) { |
| 141 |
if (isMuted[i]) { |
| 142 |
rWrite(volMap[i],0); |
| 143 |
} else { |
| 144 |
rWrite(volMap[i],chan[i].outVol&0x1f); |
| 145 |
} |
| 146 |
chan[i].volumeChanged=false; |
| 147 |
} |
| 148 |
if (chan[i].std.wave.had) { |
| 149 |
if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) { |
| 150 |
chan[i].wave=chan[i].std.wave.val; |
| 151 |
chan[i].ws.changeWave1(chan[i].wave); |
| 152 |
if (!chan[i].keyOff) chan[i].keyOn=true; |
| 153 |
} |
| 154 |
} |
| 155 |
if (chan[i].std.pitch.had) { |
| 156 |
if (chan[i].std.pitch.mode) { |
| 157 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 158 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 159 |
} else { |
| 160 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 161 |
} |
| 162 |
chan[i].freqChanged=true; |
| 163 |
} |
| 164 |
if (chan[i].std.phaseReset.had && chan[i].std.phaseReset.val==1) { |
| 165 |
chan[i].antiClickWavePos=0; |
| 166 |
chan[i].antiClickPeriodCount=0; |
| 167 |
} |
| 168 |
if (chan[i].active) { |
| 169 |
if (chan[i].ws.tick() || (chan[i].std.phaseReset.had && chan[i].std.phaseReset.val==1)) { |
| 170 |
updateWave(i); |
| 171 |
} |
| 172 |
} |
| 173 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 174 |
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,true,0,chan[i].pitch2,chipClock,CHIP_DIVIDER)-1; |
| 175 |
if (chan[i].freq<1) chan[i].freq=1; |
| 176 |
if (chan[i].freq>4095) chan[i].freq=4095; |
| 177 |
rWrite(freqMap[i][0],chan[i].freq>>8); |
| 178 |
rWrite(freqMap[i][1],chan[i].freq&0xff); |
| 179 |
if (chan[i].keyOn) { |
| 180 |
} |
| 181 |
if (chan[i].keyOff) { |
| 182 |
} |
| 183 |
if (chan[i].keyOn) chan[i].keyOn=false; |
| 184 |
if (chan[i].keyOff) chan[i].keyOff=false; |
| 185 |
chan[i].freqChanged=false; |
| 186 |
} |
| 187 |
if (!isMuted[i] && chan[i].active) { |
| 188 |
keyState|=(1<<i); |
| 189 |
} else { |
| 190 |
keyState&=~(1<<i); |
| 191 |
} |
| 192 |
} |
| 193 |
if (regPool[0x40]!=keyState) { |
| 194 |
rWrite(0x40,keyState); |
| 195 |
regPool[0x40]=keyState; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
int DivPlatformSM8521::dispatch(DivCommand c) { |
| 200 |
switch (c.cmd) { |
| 201 |
case DIV_CMD_NOTE_ON: { |
| 202 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SM8521); |
| 203 |
if (c.value!=DIV_NOTE_NULL) { |
| 204 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value); |
| 205 |
chan[c.chan].freqChanged=true; |
| 206 |
chan[c.chan].note=c.value; |
| 207 |
} |
| 208 |
chan[c.chan].active=true; |
| 209 |
chan[c.chan].keyOn=true; |
| 210 |
chan[c.chan].macroInit(ins); |
| 211 |
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { |
| 212 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 213 |
} |
| 214 |
if (chan[c.chan].wave<0) { |
| 215 |
chan[c.chan].wave=0; |
| 216 |
chan[c.chan].ws.changeWave1(chan[c.chan].wave); |
| 217 |
} |
| 218 |
chan[c.chan].ws.init(ins,32,15,chan[c.chan].insChanged); |
| 219 |
chan[c.chan].insChanged=false; |
| 220 |
if (!isMuted[c.chan]) { |
| 221 |
chan[c.chan].volumeChanged=true; |
| 222 |
} |
| 223 |
break; |
| 224 |
} |
| 225 |
case DIV_CMD_NOTE_OFF: |
| 226 |
chan[c.chan].active=false; |
| 227 |
chan[c.chan].keyOff=true; |
| 228 |
chan[c.chan].macroInit(NULL); |
| 229 |
break; |
| 230 |
case DIV_CMD_NOTE_OFF_ENV: |
| 231 |
case DIV_CMD_ENV_RELEASE: |
| 232 |
chan[c.chan].std.release(); |
| 233 |
break; |
| 234 |
case DIV_CMD_INSTRUMENT: |
| 235 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 236 |
chan[c.chan].ins=c.value; |
| 237 |
chan[c.chan].insChanged=true; |
| 238 |
} |
| 239 |
break; |
| 240 |
case DIV_CMD_VOLUME: |
| 241 |
if (chan[c.chan].vol!=c.value) { |
| 242 |
chan[c.chan].vol=c.value; |
| 243 |
if (!chan[c.chan].std.vol.has) { |
| 244 |
chan[c.chan].outVol=c.value; |
| 245 |
if (!isMuted[c.chan]) { |
| 246 |
chan[c.chan].volumeChanged=true; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
break; |
| 251 |
case DIV_CMD_GET_VOLUME: |
| 252 |
if (chan[c.chan].std.vol.has) { |
| 253 |
return chan[c.chan].vol; |
| 254 |
} |
| 255 |
return chan[c.chan].outVol; |
| 256 |
break; |
| 257 |
case DIV_CMD_PITCH: |
| 258 |
chan[c.chan].pitch=c.value; |
| 259 |
chan[c.chan].freqChanged=true; |
| 260 |
break; |
| 261 |
case DIV_CMD_WAVE: |
| 262 |
chan[c.chan].wave=c.value; |
| 263 |
chan[c.chan].ws.changeWave1(chan[c.chan].wave); |
| 264 |
chan[c.chan].keyOn=true; |
| 265 |
break; |
| 266 |
case DIV_CMD_NOTE_PORTA: { |
| 267 |
int destFreq=NOTE_PERIODIC(c.value2); |
| 268 |
bool return2=false; |
| 269 |
if (destFreq>chan[c.chan].baseFreq) { |
| 270 |
chan[c.chan].baseFreq+=c.value*((parent->song.linearPitch==2)?1:8); |
| 271 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 272 |
chan[c.chan].baseFreq=destFreq; |
| 273 |
return2=true; |
| 274 |
} |
| 275 |
} else { |
| 276 |
chan[c.chan].baseFreq-=c.value*((parent->song.linearPitch==2)?1:8); |
| 277 |
if (chan[c.chan].baseFreq<=destFreq) { |
| 278 |
chan[c.chan].baseFreq=destFreq; |
| 279 |
return2=true; |
| 280 |
} |
| 281 |
} |
| 282 |
chan[c.chan].freqChanged=true; |
| 283 |
if (return2) { |
| 284 |
chan[c.chan].inPorta=false; |
| 285 |
return 2; |
| 286 |
} |
| 287 |
break; |
| 288 |
} |
| 289 |
case DIV_CMD_LEGATO: |
| 290 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0))); |
| 291 |
chan[c.chan].freqChanged=true; |
| 292 |
chan[c.chan].note=c.value; |
| 293 |
break; |
| 294 |
case DIV_CMD_PRE_PORTA: |
| 295 |
if (chan[c.chan].active && c.value2) { |
| 296 |
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_SM8521)); |
| 297 |
} |
| 298 |
if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_PERIODIC(chan[c.chan].note); |
| 299 |
chan[c.chan].inPorta=c.value; |
| 300 |
break; |
| 301 |
case DIV_CMD_GET_VOLMAX: |
| 302 |
return 31; |
| 303 |
break; |
| 304 |
case DIV_CMD_MACRO_OFF: |
| 305 |
chan[c.chan].std.mask(c.value,true); |
| 306 |
break; |
| 307 |
case DIV_CMD_MACRO_ON: |
| 308 |
chan[c.chan].std.mask(c.value,false); |
| 309 |
break; |
| 310 |
case DIV_CMD_MACRO_RESTART: |
| 311 |
chan[c.chan].std.restart(c.value); |
| 312 |
break; |
| 313 |
default: |
| 314 |
break; |
| 315 |
} |
| 316 |
return 1; |
| 317 |
} |
| 318 |
|
| 319 |
void DivPlatformSM8521::muteChannel(int ch, bool mute) { |
| 320 |
isMuted[ch]=mute; |
| 321 |
chan[ch].volumeChanged=true; |
| 322 |
if (mute) { |
| 323 |
rWrite(0x40,regPool[0x40]&~(1<<ch)); |
| 324 |
} else if (chan[ch].active) { |
| 325 |
rWrite(0x40,regPool[0x40]|0x80|(1<<ch)); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
void DivPlatformSM8521::forceIns() { |
| 330 |
for (int i=0; i<3; i++) { |
| 331 |
chan[i].insChanged=true; |
| 332 |
chan[i].freqChanged=true; |
| 333 |
updateWave(i); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
void* DivPlatformSM8521::getChanState(int ch) { |
| 338 |
return &chan[ch]; |
| 339 |
} |
| 340 |
|
| 341 |
DivMacroInt* DivPlatformSM8521::getChanMacroInt(int ch) { |
| 342 |
return &chan[ch].std; |
| 343 |
} |
| 344 |
|
| 345 |
DivDispatchOscBuffer* DivPlatformSM8521::getOscBuffer(int ch) { |
| 346 |
return oscBuf[ch]; |
| 347 |
} |
| 348 |
|
| 349 |
unsigned char* DivPlatformSM8521::getRegisterPool() { |
| 350 |
return regPool; |
| 351 |
} |
| 352 |
|
| 353 |
int DivPlatformSM8521::getRegisterPoolSize() { |
| 354 |
return 256; |
| 355 |
} |
| 356 |
|
| 357 |
void DivPlatformSM8521::reset() { |
| 358 |
while (!writes.empty()) writes.pop(); |
| 359 |
memset(regPool,0,256); |
| 360 |
for (int i=0; i<3; i++) { |
| 361 |
chan[i]=DivPlatformSM8521::Channel(); |
| 362 |
chan[i].std.setEngine(parent); |
| 363 |
chan[i].ws.setEngine(parent); |
| 364 |
chan[i].ws.init(NULL,32,15,false); |
| 365 |
} |
| 366 |
if (dumpWrites) { |
| 367 |
addWrite(0xffffffff,0); |
| 368 |
} |
| 369 |
sm8521_reset(&sm8521); |
| 370 |
lastOut=0; |
| 371 |
rWrite(0x40,0x80); // initialize SGC |
| 372 |
} |
| 373 |
|
| 374 |
int DivPlatformSM8521::getOutputCount() { |
| 375 |
return 1; |
| 376 |
} |
| 377 |
|
| 378 |
bool DivPlatformSM8521::keyOffAffectsArp(int ch) { |
| 379 |
return true; |
| 380 |
} |
| 381 |
|
| 382 |
bool DivPlatformSM8521::hasAcquireDirect() { |
| 383 |
return true; |
| 384 |
} |
| 385 |
|
| 386 |
void DivPlatformSM8521::notifyWaveChange(int wave) { |
| 387 |
for (int i=0; i<2; i++) { |
| 388 |
if (chan[i].wave==wave) { |
| 389 |
chan[i].ws.changeWave1(wave); |
| 390 |
updateWave(i); |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
void DivPlatformSM8521::notifyInsDeletion(void* ins) { |
| 396 |
for (int i=0; i<3; i++) { |
| 397 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
void DivPlatformSM8521::setFlags(const DivConfig& flags) { |
| 402 |
chipClock=11059200; |
| 403 |
CHECK_CUSTOM_CLOCK; |
| 404 |
antiClickEnabled=!flags.getBool("noAntiClick",false); |
| 405 |
rate=chipClock/2; // CKIN -> fCLK(/2) -> Function blocks (/2) |
| 406 |
for (int i=0; i<3; i++) { |
| 407 |
oscBuf[i]->setRate(rate); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
void DivPlatformSM8521::poke(unsigned int addr, unsigned short val) { |
| 412 |
rWrite(addr,val); |
| 413 |
} |
| 414 |
|
| 415 |
void DivPlatformSM8521::poke(std::vector<DivRegWrite>& wlist) { |
| 416 |
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); |
| 417 |
} |
| 418 |
|
| 419 |
int DivPlatformSM8521::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 420 |
parent=p; |
| 421 |
dumpWrites=false; |
| 422 |
skipRegisterWrites=false; |
| 423 |
for (int i=0; i<3; i++) { |
| 424 |
isMuted[i]=false; |
| 425 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 426 |
} |
| 427 |
setFlags(flags); |
| 428 |
reset(); |
| 429 |
return 6; |
| 430 |
} |
| 431 |
|
| 432 |
void DivPlatformSM8521::quit() { |
| 433 |
for (int i=0; i<3; i++) { |
| 434 |
delete oscBuf[i]; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
DivPlatformSM8521::~DivPlatformSM8521() { |
| 439 |
} |