| 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 "msm6258.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include "../../ta-log.h" |
| 23 |
#include "sound/oki/okim6258.h" |
| 24 |
#include <string.h> |
| 25 |
#include <math.h> |
| 26 |
|
| 27 |
#define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} } |
| 28 |
|
| 29 |
const char** DivPlatformMSM6258::getRegisterSheet() { |
| 30 |
return NULL; |
| 31 |
} |
| 32 |
|
| 33 |
static const int msmRates[4]={ |
| 34 |
4, 3, 2, 2 |
| 35 |
}; |
| 36 |
|
| 37 |
int DivPlatformMSM6258::calcVGMRate() { |
| 38 |
int ret=chipClock/((clockSel+1)*512*msmRates[rateSel&3]); |
| 39 |
logD("MSM rate: %d",ret); |
| 40 |
return ret; |
| 41 |
} |
| 42 |
|
| 43 |
void DivPlatformMSM6258::acquire(short** buf, size_t len) { |
| 44 |
oscBuf[0]->begin(len); |
| 45 |
|
| 46 |
for (size_t h=0; h<len; h++) { |
| 47 |
if (--msmClockCount<0) { |
| 48 |
if (--msmDividerCount<=0) { |
| 49 |
if (!writes.empty()) { |
| 50 |
QueuedWrite& w=writes.front(); |
| 51 |
switch (w.addr) { |
| 52 |
case 0: |
| 53 |
msm->ctrl_w(w.val); |
| 54 |
break; |
| 55 |
case 2: |
| 56 |
msmPan=w.val; |
| 57 |
break; |
| 58 |
case 8: |
| 59 |
msmClock=w.val; |
| 60 |
break; |
| 61 |
case 12: |
| 62 |
msmDivider=4-(w.val&3); |
| 63 |
if (msmDivider<2) msmDivider=2; |
| 64 |
break; |
| 65 |
} |
| 66 |
writes.pop(); |
| 67 |
} |
| 68 |
|
| 69 |
if (sample>=0 && sample<parent->song.sampleLen) { |
| 70 |
DivSample* s=parent->getSample(sample); |
| 71 |
unsigned char nextData=(s->dataVOX[samplePos]>>4)|(s->dataVOX[samplePos]<<4); |
| 72 |
if (msm->data_w(nextData)) { |
| 73 |
samplePos++; |
| 74 |
if (samplePos>=(int)s->lengthVOX) { |
| 75 |
sample=-1; |
| 76 |
samplePos=0; |
| 77 |
msm->ctrl_w(1); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
msm->sound_stream_update(&msmOut,1); |
| 83 |
msmDividerCount=msmDivider; |
| 84 |
} |
| 85 |
msmClockCount=msmClock; |
| 86 |
} |
| 87 |
|
| 88 |
if (isMuted[0]) { |
| 89 |
buf[0][h]=0; |
| 90 |
buf[1][h]=0; |
| 91 |
oscBuf[0]->putSample(h,0); |
| 92 |
} else { |
| 93 |
buf[0][h]=(msmPan&2)?0:msmOut; |
| 94 |
buf[1][h]=(msmPan&1)?0:msmOut; |
| 95 |
oscBuf[0]->putSample(h,(msmPan!=3)?(msmOut>>1):0); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
oscBuf[0]->end(len); |
| 100 |
} |
| 101 |
|
| 102 |
void DivPlatformMSM6258::tick(bool sysTick) { |
| 103 |
for (int i=0; i<1; i++) { |
| 104 |
if (!parent->song.disableSampleMacro) { |
| 105 |
chan[i].std.next(); |
| 106 |
if (chan[i].std.duty.had) { |
| 107 |
if (rateSel!=(chan[i].std.duty.val&3)) { |
| 108 |
rateSel=chan[i].std.duty.val&3; |
| 109 |
rWrite(12,rateSel); |
| 110 |
updateSampleFreq=true; |
| 111 |
} |
| 112 |
} |
| 113 |
if (chan[i].std.panL.had) { |
| 114 |
if (chan[i].pan!=(chan[i].std.panL.val&3)) { |
| 115 |
chan[i].pan=chan[i].std.panL.val&3; |
| 116 |
rWrite(2,(~chan[i].pan)&3); |
| 117 |
} |
| 118 |
} |
| 119 |
if (chan[i].std.ex1.had) { |
| 120 |
if (clockSel!=(chan[i].std.ex1.val&1)) { |
| 121 |
clockSel=chan[i].std.ex1.val&1; |
| 122 |
rWrite(8,clockSel); |
| 123 |
updateSampleFreq=true; |
| 124 |
} |
| 125 |
} |
| 126 |
if (chan[i].std.phaseReset.had) { |
| 127 |
if (chan[i].std.phaseReset.val && chan[i].active) { |
| 128 |
chan[i].keyOn=true; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
if (updateSampleFreq) { |
| 133 |
int newRate=calcVGMRate(); |
| 134 |
if (dumpWrites) addWrite(0xffff0001,newRate); |
| 135 |
updateSampleFreq=false; |
| 136 |
} |
| 137 |
if (chan[i].keyOn || chan[i].keyOff) { |
| 138 |
samplePos=0; |
| 139 |
// turn off |
| 140 |
if (dumpWrites) addWrite(0xffff0002,0); |
| 141 |
rWrite(0,1); |
| 142 |
if (chan[i].active && !chan[i].keyOff) { |
| 143 |
if (sample>=0 && sample<parent->song.sampleLen) { |
| 144 |
// turn on |
| 145 |
rWrite(0,2); |
| 146 |
if (dumpWrites) addWrite(0xffff0000,sample); |
| 147 |
int newRate=calcVGMRate(); |
| 148 |
if (dumpWrites) addWrite(0xffff0001,newRate); |
| 149 |
} else { |
| 150 |
sample=-1; |
| 151 |
} |
| 152 |
} else { |
| 153 |
sample=-1; |
| 154 |
} |
| 155 |
chan[i].keyOn=false; |
| 156 |
chan[i].keyOff=false; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
int DivPlatformMSM6258::dispatch(DivCommand c) { |
| 162 |
switch (c.cmd) { |
| 163 |
case DIV_CMD_NOTE_ON: { |
| 164 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_FM); |
| 165 |
if (ins->type==DIV_INS_MSM6258 || ins->type==DIV_INS_AMIGA) { |
| 166 |
chan[c.chan].furnacePCM=true; |
| 167 |
} else { |
| 168 |
chan[c.chan].furnacePCM=false; |
| 169 |
} |
| 170 |
if (skipRegisterWrites) break; |
| 171 |
if (chan[c.chan].furnacePCM) { |
| 172 |
chan[c.chan].macroInit(ins); |
| 173 |
if (!chan[c.chan].std.vol.will) { |
| 174 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 175 |
} |
| 176 |
if (c.value!=DIV_NOTE_NULL) sample=ins->amiga.getSample(c.value); |
| 177 |
samplePos=0; |
| 178 |
if (sample>=0 && sample<parent->song.sampleLen) { |
| 179 |
//DivSample* s=parent->getSample(chan[c.chan].sample); |
| 180 |
if (c.value!=DIV_NOTE_NULL) { |
| 181 |
chan[c.chan].note=c.value; |
| 182 |
} |
| 183 |
chan[c.chan].active=true; |
| 184 |
chan[c.chan].keyOn=true; |
| 185 |
} else { |
| 186 |
break; |
| 187 |
} |
| 188 |
} else { |
| 189 |
chan[c.chan].sample=-1; |
| 190 |
chan[c.chan].macroInit(NULL); |
| 191 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 192 |
if ((12*sampleBank+c.value%12)<0 || (12*sampleBank+c.value%12)>=parent->song.sampleLen) { |
| 193 |
break; |
| 194 |
} |
| 195 |
//DivSample* s=parent->getSample(12*sampleBank+c.value%12); |
| 196 |
sample=12*sampleBank+c.value%12; |
| 197 |
samplePos=0; |
| 198 |
chan[c.chan].active=true; |
| 199 |
chan[c.chan].keyOn=true; |
| 200 |
} |
| 201 |
break; |
| 202 |
} |
| 203 |
case DIV_CMD_NOTE_OFF: |
| 204 |
chan[c.chan].keyOff=true; |
| 205 |
chan[c.chan].keyOn=false; |
| 206 |
chan[c.chan].active=false; |
| 207 |
chan[c.chan].macroInit(NULL); |
| 208 |
break; |
| 209 |
case DIV_CMD_NOTE_OFF_ENV: |
| 210 |
chan[c.chan].keyOff=true; |
| 211 |
chan[c.chan].keyOn=false; |
| 212 |
chan[c.chan].active=false; |
| 213 |
chan[c.chan].std.release(); |
| 214 |
break; |
| 215 |
case DIV_CMD_ENV_RELEASE: |
| 216 |
chan[c.chan].std.release(); |
| 217 |
break; |
| 218 |
case DIV_CMD_VOLUME: { |
| 219 |
chan[c.chan].vol=c.value; |
| 220 |
if (!chan[c.chan].std.vol.has) { |
| 221 |
chan[c.chan].outVol=c.value; |
| 222 |
} |
| 223 |
break; |
| 224 |
} |
| 225 |
case DIV_CMD_GET_VOLUME: { |
| 226 |
return chan[c.chan].vol; |
| 227 |
break; |
| 228 |
} |
| 229 |
case DIV_CMD_INSTRUMENT: |
| 230 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 231 |
chan[c.chan].insChanged=true; |
| 232 |
} |
| 233 |
chan[c.chan].ins=c.value; |
| 234 |
break; |
| 235 |
case DIV_CMD_PITCH: { |
| 236 |
break; |
| 237 |
} |
| 238 |
case DIV_CMD_NOTE_PORTA: { |
| 239 |
return 2; |
| 240 |
} |
| 241 |
case DIV_CMD_SAMPLE_BANK: |
| 242 |
sampleBank=c.value; |
| 243 |
if (sampleBank>(parent->song.sample.size()/12)) { |
| 244 |
sampleBank=parent->song.sample.size()/12; |
| 245 |
} |
| 246 |
break; |
| 247 |
case DIV_CMD_SAMPLE_FREQ: |
| 248 |
rateSel=c.value&3; |
| 249 |
rWrite(12,rateSel); |
| 250 |
updateSampleFreq=true; |
| 251 |
break; |
| 252 |
case DIV_CMD_SAMPLE_MODE: |
| 253 |
clockSel=c.value&1; |
| 254 |
rWrite(8,clockSel); |
| 255 |
updateSampleFreq=true; |
| 256 |
break; |
| 257 |
case DIV_CMD_PANNING: { |
| 258 |
if (c.value==0 && c.value2==0) { |
| 259 |
chan[c.chan].pan=3; |
| 260 |
} else { |
| 261 |
chan[c.chan].pan=(c.value2>0)|((c.value>0)<<1); |
| 262 |
} |
| 263 |
rWrite(2,(~chan[c.chan].pan)&3); |
| 264 |
break; |
| 265 |
} |
| 266 |
case DIV_CMD_LEGATO: { |
| 267 |
break; |
| 268 |
} |
| 269 |
case DIV_CMD_MACRO_OFF: |
| 270 |
chan[c.chan].std.mask(c.value,true); |
| 271 |
break; |
| 272 |
case DIV_CMD_MACRO_ON: |
| 273 |
chan[c.chan].std.mask(c.value,false); |
| 274 |
break; |
| 275 |
case DIV_CMD_MACRO_RESTART: |
| 276 |
chan[c.chan].std.restart(c.value); |
| 277 |
break; |
| 278 |
case DIV_CMD_GET_VOLMAX: |
| 279 |
return 8; |
| 280 |
break; |
| 281 |
case DIV_CMD_PRE_PORTA: |
| 282 |
break; |
| 283 |
case DIV_CMD_PRE_NOTE: |
| 284 |
break; |
| 285 |
default: |
| 286 |
//printf("WARNING: unimplemented command %d\n",c.cmd); |
| 287 |
break; |
| 288 |
} |
| 289 |
return 1; |
| 290 |
} |
| 291 |
|
| 292 |
void DivPlatformMSM6258::muteChannel(int ch, bool mute) { |
| 293 |
isMuted[ch]=mute; |
| 294 |
} |
| 295 |
|
| 296 |
void DivPlatformMSM6258::forceIns() { |
| 297 |
while (!writes.empty()) writes.pop(); |
| 298 |
for (int i=0; i<1; i++) { |
| 299 |
chan[i].insChanged=true; |
| 300 |
} |
| 301 |
rWrite(12,rateSel); |
| 302 |
rWrite(8,clockSel); |
| 303 |
rWrite(2,(~chan[0].pan)&3); |
| 304 |
} |
| 305 |
|
| 306 |
void* DivPlatformMSM6258::getChanState(int ch) { |
| 307 |
return &chan[ch]; |
| 308 |
} |
| 309 |
|
| 310 |
DivMacroInt* DivPlatformMSM6258::getChanMacroInt(int ch) { |
| 311 |
return &chan[ch].std; |
| 312 |
} |
| 313 |
|
| 314 |
unsigned short DivPlatformMSM6258::getPan(int ch) { |
| 315 |
return ((chan[ch].pan&2)<<7)|(chan[ch].pan&1); |
| 316 |
} |
| 317 |
|
| 318 |
DivDispatchOscBuffer* DivPlatformMSM6258::getOscBuffer(int ch) { |
| 319 |
return oscBuf[ch]; |
| 320 |
} |
| 321 |
|
| 322 |
unsigned char* DivPlatformMSM6258::getRegisterPool() { |
| 323 |
return NULL; |
| 324 |
} |
| 325 |
|
| 326 |
int DivPlatformMSM6258::getRegisterPoolSize() { |
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
void DivPlatformMSM6258::poke(unsigned int addr, unsigned short val) { |
| 331 |
//immWrite(addr,val); |
| 332 |
} |
| 333 |
|
| 334 |
void DivPlatformMSM6258::poke(std::vector<DivRegWrite>& wlist) { |
| 335 |
//for (DivRegWrite& i: wlist) immWrite(i.addr,i.val); |
| 336 |
} |
| 337 |
|
| 338 |
void DivPlatformMSM6258::reset() { |
| 339 |
while (!writes.empty()) writes.pop(); |
| 340 |
msm->device_reset(); |
| 341 |
msmClock=chipClock; |
| 342 |
msmDivider=2; |
| 343 |
msmDividerCount=0; |
| 344 |
msmClock=0; |
| 345 |
msmClockCount=0; |
| 346 |
msmPan=0; |
| 347 |
rateSel=2; |
| 348 |
clockSel=0; |
| 349 |
updateSampleFreq=true; |
| 350 |
if (dumpWrites) { |
| 351 |
addWrite(0xffffffff,0); |
| 352 |
addWrite(0xffff0001,calcVGMRate()); |
| 353 |
} |
| 354 |
for (int i=0; i<1; i++) { |
| 355 |
chan[i]=DivPlatformMSM6258::Channel(); |
| 356 |
chan[i].std.setEngine(parent); |
| 357 |
} |
| 358 |
for (int i=0; i<1; i++) { |
| 359 |
chan[i].vol=8; |
| 360 |
chan[i].outVol=8; |
| 361 |
} |
| 362 |
|
| 363 |
sampleBank=0; |
| 364 |
sample=-1; |
| 365 |
samplePos=0; |
| 366 |
|
| 367 |
delay=0; |
| 368 |
} |
| 369 |
|
| 370 |
int DivPlatformMSM6258::getOutputCount() { |
| 371 |
return 2; |
| 372 |
} |
| 373 |
|
| 374 |
bool DivPlatformMSM6258::keyOffAffectsArp(int ch) { |
| 375 |
return false; |
| 376 |
} |
| 377 |
|
| 378 |
bool DivPlatformMSM6258::getLegacyAlwaysSetVolume() { |
| 379 |
return false; |
| 380 |
} |
| 381 |
|
| 382 |
void DivPlatformMSM6258::notifyInsChange(int ins) { |
| 383 |
for (int i=0; i<1; i++) { |
| 384 |
if (chan[i].ins==ins) { |
| 385 |
chan[i].insChanged=true; |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
void DivPlatformMSM6258::notifyInsDeletion(void* ins) { |
| 391 |
for (int i=0; i<1; i++) { |
| 392 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
void DivPlatformMSM6258::setFlags(const DivConfig& flags) { |
| 397 |
switch (flags.getInt("clockSel",0)) { |
| 398 |
case 3: |
| 399 |
chipClock=8192000; |
| 400 |
break; |
| 401 |
case 2: |
| 402 |
chipClock=8000000; |
| 403 |
break; |
| 404 |
case 1: |
| 405 |
chipClock=4096000; |
| 406 |
break; |
| 407 |
default: |
| 408 |
chipClock=4000000; |
| 409 |
break; |
| 410 |
} |
| 411 |
CHECK_CUSTOM_CLOCK; |
| 412 |
rate=chipClock/256; |
| 413 |
for (int i=0; i<1; i++) { |
| 414 |
oscBuf[i]->setRate(rate); |
| 415 |
} |
| 416 |
variableRate=flags.getBool("variableRate",false); |
| 417 |
} |
| 418 |
|
| 419 |
int DivPlatformMSM6258::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 420 |
parent=p; |
| 421 |
dumpWrites=false; |
| 422 |
skipRegisterWrites=false; |
| 423 |
updateOsc=0; |
| 424 |
for (int i=0; i<1; i++) { |
| 425 |
isMuted[i]=false; |
| 426 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 427 |
} |
| 428 |
msm=new okim6258_device(4000000); |
| 429 |
msm->set_start_div(okim6258_device::FOSC_DIV_BY_1024); |
| 430 |
msm->set_type(okim6258_device::TYPE_4BITS); |
| 431 |
msm->set_outbits(okim6258_device::OUTPUT_12BITS); |
| 432 |
msm->device_start(); |
| 433 |
setFlags(flags); |
| 434 |
reset(); |
| 435 |
return 4; |
| 436 |
} |
| 437 |
|
| 438 |
void DivPlatformMSM6258::quit() { |
| 439 |
for (int i=0; i<1; i++) { |
| 440 |
delete oscBuf[i]; |
| 441 |
} |
| 442 |
delete msm; |
| 443 |
} |
| 444 |
|
| 445 |
DivPlatformMSM6258::~DivPlatformMSM6258() { |
| 446 |
} |