| 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 "bubsyswsg.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include <math.h> |
| 23 |
|
| 24 |
#define CHIP_DIVIDER 32 |
| 25 |
|
| 26 |
#define rWrite(a,v) {if(!skipRegisterWrites) {regPool[a]=v; if(dumpWrites) addWrite(a,v); }} |
| 27 |
|
| 28 |
const char* regCheatSheetBubSysWSG[]={ |
| 29 |
// K005289 timer |
| 30 |
"Freq_A", "0", |
| 31 |
"Freq_B", "1", |
| 32 |
// PROM, DAC control from External logic (Connected to AY PSG ports on Bubble System) |
| 33 |
"WaveVol_A", "2", |
| 34 |
"WaveVol_B", "3", |
| 35 |
NULL |
| 36 |
}; |
| 37 |
|
| 38 |
const char** DivPlatformBubSysWSG::getRegisterSheet() { |
| 39 |
return regCheatSheetBubSysWSG; |
| 40 |
} |
| 41 |
|
| 42 |
void DivPlatformBubSysWSG::acquireDirect(blip_buffer_t** bb, size_t len) { |
| 43 |
int chanOut=0; |
| 44 |
for (int i=0; i<2; i++) { |
| 45 |
oscBuf[i]->begin(len); |
| 46 |
} |
| 47 |
for (size_t h=0; h<len; h++) { |
| 48 |
int advance=len-h; |
| 49 |
// heuristic |
| 50 |
for (int i=0; i<2; i++) { |
| 51 |
const int remain=k005289.m_timer[i].m_counter; |
| 52 |
if (remain<advance) advance=remain; |
| 53 |
} |
| 54 |
if (advance<1) advance=1; |
| 55 |
|
| 56 |
signed int out=0; |
| 57 |
// K005289 part |
| 58 |
k005289.tick(advance); |
| 59 |
|
| 60 |
h+=advance-1; |
| 61 |
|
| 62 |
// Wavetable part |
| 63 |
for (int i=0; i<2; i++) { |
| 64 |
if (isMuted[i]) { |
| 65 |
oscBuf[i]->putSample(h,0); |
| 66 |
continue; |
| 67 |
} else { |
| 68 |
chanOut=chan[i].waveROM[k005289.addr(i)]*(regPool[2+i]&0xf); |
| 69 |
out+=chanOut; |
| 70 |
oscBuf[i]->putSample(h,chanOut<<7); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
out<<=6; // scale output to 16 bit |
| 75 |
|
| 76 |
if (out!=lastOut) { |
| 77 |
blip_add_delta(bb[0],h,out-lastOut); |
| 78 |
lastOut=out; |
| 79 |
} |
| 80 |
} |
| 81 |
for (int i=0; i<2; i++) { |
| 82 |
oscBuf[i]->end(len); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
void DivPlatformBubSysWSG::updateWave(int ch) { |
| 87 |
//DivWavetable* wt=parent->getWave(chan[ch].wave); |
| 88 |
for (int i=0; i<32; i++) { |
| 89 |
// convert to signed |
| 90 |
chan[ch].waveROM[i]=chan[ch].ws.output[i]-8; |
| 91 |
} |
| 92 |
if (chan[ch].active) { |
| 93 |
rWrite(2+ch,(ch<<5)|chan[ch].outVol); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
void DivPlatformBubSysWSG::tick(bool sysTick) { |
| 98 |
for (int i=0; i<2; i++) { |
| 99 |
chan[i].std.next(); |
| 100 |
if (chan[i].std.vol.had) { |
| 101 |
chan[i].outVol=((chan[i].vol&15)*MIN(15,chan[i].std.vol.val))/15; |
| 102 |
rWrite(2+i,(chan[i].wave<<5)|chan[i].outVol); |
| 103 |
} |
| 104 |
if (NEW_ARP_STRAT) { |
| 105 |
chan[i].handleArp(); |
| 106 |
} else if (chan[i].std.arp.had) { |
| 107 |
if (!chan[i].inPorta) { |
| 108 |
chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val)); |
| 109 |
} |
| 110 |
chan[i].freqChanged=true; |
| 111 |
} |
| 112 |
if (chan[i].std.wave.had) { |
| 113 |
if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) { |
| 114 |
chan[i].wave=chan[i].std.wave.val; |
| 115 |
chan[i].ws.changeWave1(chan[i].wave); |
| 116 |
if (!chan[i].keyOff) chan[i].keyOn=true; |
| 117 |
} |
| 118 |
} |
| 119 |
if (chan[i].std.pitch.had) { |
| 120 |
if (chan[i].std.pitch.mode) { |
| 121 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 122 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 123 |
} else { |
| 124 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 125 |
} |
| 126 |
chan[i].freqChanged=true; |
| 127 |
} |
| 128 |
if (chan[i].active) { |
| 129 |
if (chan[i].ws.tick()) { |
| 130 |
updateWave(i); |
| 131 |
} |
| 132 |
} |
| 133 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 134 |
//DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_SCC); |
| 135 |
chan[i].freq=0x1000-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); |
| 136 |
if (chan[i].freq<0) chan[i].freq=0; |
| 137 |
if (chan[i].freq>4095) chan[i].freq=4095; |
| 138 |
k005289.load(i,chan[i].freq); |
| 139 |
rWrite(i,chan[i].freq); |
| 140 |
k005289.update(i); |
| 141 |
if (chan[i].keyOn) { |
| 142 |
// ??? |
| 143 |
} |
| 144 |
if (chan[i].keyOff) { |
| 145 |
rWrite(2+i,(chan[i].wave<<5)|0); |
| 146 |
} |
| 147 |
if (chan[i].keyOn) chan[i].keyOn=false; |
| 148 |
if (chan[i].keyOff) chan[i].keyOff=false; |
| 149 |
chan[i].freqChanged=false; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
int DivPlatformBubSysWSG::dispatch(DivCommand c) { |
| 155 |
switch (c.cmd) { |
| 156 |
case DIV_CMD_NOTE_ON: { |
| 157 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SCC); |
| 158 |
if (c.value!=DIV_NOTE_NULL) { |
| 159 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value); |
| 160 |
chan[c.chan].freqChanged=true; |
| 161 |
chan[c.chan].note=c.value; |
| 162 |
} |
| 163 |
chan[c.chan].active=true; |
| 164 |
chan[c.chan].keyOn=true; |
| 165 |
rWrite(2+c.chan,(chan[c.chan].wave<<5)|chan[c.chan].vol); |
| 166 |
chan[c.chan].macroInit(ins); |
| 167 |
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { |
| 168 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 169 |
} |
| 170 |
if (chan[c.chan].wave<0) { |
| 171 |
chan[c.chan].wave=0; |
| 172 |
chan[c.chan].ws.changeWave1(chan[c.chan].wave); |
| 173 |
} |
| 174 |
chan[c.chan].ws.init(ins,32,15,chan[c.chan].insChanged); |
| 175 |
chan[c.chan].insChanged=false; |
| 176 |
break; |
| 177 |
} |
| 178 |
case DIV_CMD_NOTE_OFF: |
| 179 |
chan[c.chan].active=false; |
| 180 |
chan[c.chan].keyOff=true; |
| 181 |
chan[c.chan].macroInit(NULL); |
| 182 |
break; |
| 183 |
case DIV_CMD_NOTE_OFF_ENV: |
| 184 |
case DIV_CMD_ENV_RELEASE: |
| 185 |
chan[c.chan].std.release(); |
| 186 |
break; |
| 187 |
case DIV_CMD_INSTRUMENT: |
| 188 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 189 |
chan[c.chan].ins=c.value; |
| 190 |
} |
| 191 |
break; |
| 192 |
case DIV_CMD_VOLUME: |
| 193 |
if (chan[c.chan].vol!=c.value) { |
| 194 |
chan[c.chan].vol=c.value; |
| 195 |
if (!chan[c.chan].std.vol.has) { |
| 196 |
chan[c.chan].outVol=c.value; |
| 197 |
if (chan[c.chan].active) rWrite(2+c.chan,(chan[c.chan].wave<<5)|chan[c.chan].outVol); |
| 198 |
} |
| 199 |
} |
| 200 |
break; |
| 201 |
case DIV_CMD_GET_VOLUME: |
| 202 |
if (chan[c.chan].std.vol.has) { |
| 203 |
return chan[c.chan].vol; |
| 204 |
} |
| 205 |
return chan[c.chan].outVol; |
| 206 |
break; |
| 207 |
case DIV_CMD_PITCH: |
| 208 |
chan[c.chan].pitch=c.value; |
| 209 |
chan[c.chan].freqChanged=true; |
| 210 |
break; |
| 211 |
case DIV_CMD_WAVE: |
| 212 |
chan[c.chan].wave=c.value; |
| 213 |
chan[c.chan].ws.changeWave1(chan[c.chan].wave); |
| 214 |
chan[c.chan].keyOn=true; |
| 215 |
break; |
| 216 |
case DIV_CMD_NOTE_PORTA: { |
| 217 |
int destFreq=NOTE_PERIODIC(c.value2); |
| 218 |
bool return2=false; |
| 219 |
if (destFreq>chan[c.chan].baseFreq) { |
| 220 |
chan[c.chan].baseFreq+=c.value; |
| 221 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 222 |
chan[c.chan].baseFreq=destFreq; |
| 223 |
return2=true; |
| 224 |
} |
| 225 |
} else { |
| 226 |
chan[c.chan].baseFreq-=c.value; |
| 227 |
if (chan[c.chan].baseFreq<=destFreq) { |
| 228 |
chan[c.chan].baseFreq=destFreq; |
| 229 |
return2=true; |
| 230 |
} |
| 231 |
} |
| 232 |
chan[c.chan].freqChanged=true; |
| 233 |
if (return2) { |
| 234 |
chan[c.chan].inPorta=false; |
| 235 |
return 2; |
| 236 |
} |
| 237 |
break; |
| 238 |
} |
| 239 |
case DIV_CMD_LEGATO: |
| 240 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0))); |
| 241 |
chan[c.chan].freqChanged=true; |
| 242 |
chan[c.chan].note=c.value; |
| 243 |
break; |
| 244 |
case DIV_CMD_PRE_PORTA: |
| 245 |
if (chan[c.chan].active && c.value2) { |
| 246 |
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_SCC)); |
| 247 |
} |
| 248 |
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); |
| 249 |
chan[c.chan].inPorta=c.value; |
| 250 |
break; |
| 251 |
case DIV_CMD_GET_VOLMAX: |
| 252 |
return 15; |
| 253 |
break; |
| 254 |
case DIV_CMD_MACRO_OFF: |
| 255 |
chan[c.chan].std.mask(c.value,true); |
| 256 |
break; |
| 257 |
case DIV_CMD_MACRO_ON: |
| 258 |
chan[c.chan].std.mask(c.value,false); |
| 259 |
break; |
| 260 |
case DIV_CMD_MACRO_RESTART: |
| 261 |
chan[c.chan].std.restart(c.value); |
| 262 |
break; |
| 263 |
default: |
| 264 |
break; |
| 265 |
} |
| 266 |
return 1; |
| 267 |
} |
| 268 |
|
| 269 |
void DivPlatformBubSysWSG::muteChannel(int ch, bool mute) { |
| 270 |
isMuted[ch]=mute; |
| 271 |
//rWrite(2+ch,(chan[ch].wave<<5)|((chan[ch].active && isMuted[ch])?0:chan[ch].outVol)); |
| 272 |
} |
| 273 |
|
| 274 |
void DivPlatformBubSysWSG::forceIns() { |
| 275 |
for (int i=0; i<2; i++) { |
| 276 |
chan[i].insChanged=true; |
| 277 |
chan[i].freqChanged=true; |
| 278 |
updateWave(i); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
void* DivPlatformBubSysWSG::getChanState(int ch) { |
| 283 |
return &chan[ch]; |
| 284 |
} |
| 285 |
|
| 286 |
DivMacroInt* DivPlatformBubSysWSG::getChanMacroInt(int ch) { |
| 287 |
return &chan[ch].std; |
| 288 |
} |
| 289 |
|
| 290 |
DivDispatchOscBuffer* DivPlatformBubSysWSG::getOscBuffer(int ch) { |
| 291 |
return oscBuf[ch]; |
| 292 |
} |
| 293 |
|
| 294 |
unsigned char* DivPlatformBubSysWSG::getRegisterPool() { |
| 295 |
return (unsigned char*)regPool; |
| 296 |
} |
| 297 |
|
| 298 |
int DivPlatformBubSysWSG::getRegisterPoolSize() { |
| 299 |
return 4; |
| 300 |
} |
| 301 |
|
| 302 |
int DivPlatformBubSysWSG::getRegisterPoolDepth() { |
| 303 |
return 16; |
| 304 |
} |
| 305 |
|
| 306 |
void DivPlatformBubSysWSG::reset() { |
| 307 |
memset(regPool,0,4*2); |
| 308 |
for (int i=0; i<2; i++) { |
| 309 |
chan[i]=DivPlatformBubSysWSG::Channel(); |
| 310 |
chan[i].std.setEngine(parent); |
| 311 |
chan[i].ws.setEngine(parent,8); |
| 312 |
chan[i].ws.init(NULL,32,15,false); |
| 313 |
} |
| 314 |
if (dumpWrites) { |
| 315 |
addWrite(0xffffffff,0); |
| 316 |
} |
| 317 |
k005289.reset(); |
| 318 |
lastOut=0; |
| 319 |
} |
| 320 |
|
| 321 |
int DivPlatformBubSysWSG::getOutputCount() { |
| 322 |
return 1; |
| 323 |
} |
| 324 |
|
| 325 |
bool DivPlatformBubSysWSG::keyOffAffectsArp(int ch) { |
| 326 |
return true; |
| 327 |
} |
| 328 |
|
| 329 |
bool DivPlatformBubSysWSG::hasAcquireDirect() { |
| 330 |
return true; |
| 331 |
} |
| 332 |
|
| 333 |
void DivPlatformBubSysWSG::notifyWaveChange(int wave) { |
| 334 |
for (int i=0; i<2; i++) { |
| 335 |
if (chan[i].wave==wave) { |
| 336 |
chan[i].ws.changeWave1(chan[i].wave); |
| 337 |
updateWave(i); |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
void DivPlatformBubSysWSG::notifyInsDeletion(void* ins) { |
| 343 |
for (int i=0; i<2; i++) { |
| 344 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
void DivPlatformBubSysWSG::setFlags(const DivConfig& flags) { |
| 349 |
chipClock=COLOR_NTSC; |
| 350 |
CHECK_CUSTOM_CLOCK; |
| 351 |
rate=chipClock; |
| 352 |
for (int i=0; i<2; i++) { |
| 353 |
oscBuf[i]->setRate(rate); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
void DivPlatformBubSysWSG::poke(unsigned int addr, unsigned short val) { |
| 358 |
rWrite(addr,val); |
| 359 |
} |
| 360 |
|
| 361 |
void DivPlatformBubSysWSG::poke(std::vector<DivRegWrite>& wlist) { |
| 362 |
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); |
| 363 |
} |
| 364 |
|
| 365 |
int DivPlatformBubSysWSG::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 366 |
parent=p; |
| 367 |
dumpWrites=false; |
| 368 |
skipRegisterWrites=false; |
| 369 |
writeOscBuf=0; |
| 370 |
for (int i=0; i<2; i++) { |
| 371 |
isMuted[i]=false; |
| 372 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 373 |
} |
| 374 |
setFlags(flags); |
| 375 |
reset(); |
| 376 |
return 2; |
| 377 |
} |
| 378 |
|
| 379 |
void DivPlatformBubSysWSG::quit() { |
| 380 |
for (int i=0; i<2; i++) { |
| 381 |
delete oscBuf[i]; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
DivPlatformBubSysWSG::~DivPlatformBubSysWSG() { |
| 386 |
} |