| 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 |
#ifndef _YM2610SHARED_H |
| 21 |
#define _YM2610SHARED_H |
| 22 |
|
| 23 |
#include "fmshared_OPN.h" |
| 24 |
#include "../engine.h" |
| 25 |
#include "../../ta-log.h" |
| 26 |
#include "ay.h" |
| 27 |
#include "sound/ymfm/ymfm.h" |
| 28 |
#include "sound/ymfm/ymfm_opn.h" |
| 29 |
#include <string.h> |
| 30 |
extern "C" { |
| 31 |
#include "../../../extern/YM2608-LLE/fmopna_2610.h" |
| 32 |
} |
| 33 |
|
| 34 |
#define CHIP_FREQBASE fmFreqBase |
| 35 |
#define CHIP_DIVIDER fmDivBase |
| 36 |
|
| 37 |
class DivYM2610Interface: public DivOPNInterface { |
| 38 |
public: |
| 39 |
unsigned char* adpcmAMem; |
| 40 |
unsigned char* adpcmBMem; |
| 41 |
int sampleBank; |
| 42 |
uint8_t ymfm_external_read(ymfm::access_class type, uint32_t address); |
| 43 |
void ymfm_external_write(ymfm::access_class type, uint32_t address, uint8_t data); |
| 44 |
DivYM2610Interface(): |
| 45 |
adpcmAMem(NULL), |
| 46 |
adpcmBMem(NULL), |
| 47 |
sampleBank(0) {} |
| 48 |
}; |
| 49 |
|
| 50 |
class DivPlatformYM2610Base: public DivPlatformOPN { |
| 51 |
protected: |
| 52 |
OPNChannelStereo chan[17]; |
| 53 |
DivDispatchOscBuffer* oscBuf[17]; |
| 54 |
bool isMuted[17]; |
| 55 |
|
| 56 |
ym3438_t fm_nuked; |
| 57 |
ymfm::ym2610b* fm; |
| 58 |
ymfm::ym2610b::output_data fmout; |
| 59 |
DivPlatformAY8910* ay; |
| 60 |
fmopna_2610_t fm_lle; |
| 61 |
unsigned int dacVal; |
| 62 |
unsigned int dacVal2; |
| 63 |
int dacOut[2]; |
| 64 |
int rssOut[6]; |
| 65 |
bool lastSH; |
| 66 |
bool lastSH2; |
| 67 |
bool lastS; |
| 68 |
unsigned char rmpx, pmpx, roe, poe, rssCycle, rssSubCycle; |
| 69 |
unsigned int adMemAddrA; |
| 70 |
unsigned int adMemAddrB; |
| 71 |
|
| 72 |
unsigned char* adpcmAMem; |
| 73 |
size_t adpcmAMemLen; |
| 74 |
unsigned char* adpcmBMem; |
| 75 |
size_t adpcmBMemLen; |
| 76 |
DivYM2610Interface iface; |
| 77 |
|
| 78 |
unsigned int* sampleOffA; |
| 79 |
unsigned int* sampleOffB; |
| 80 |
|
| 81 |
unsigned char sampleBank; |
| 82 |
|
| 83 |
bool extMode, noExtMacros; |
| 84 |
|
| 85 |
bool* sampleLoaded[2]; |
| 86 |
|
| 87 |
unsigned char writeADPCMAOff, writeADPCMAOn; |
| 88 |
int globalADPCMAVolume; |
| 89 |
|
| 90 |
DivMemoryComposition memCompoA; |
| 91 |
DivMemoryComposition memCompoB; |
| 92 |
|
| 93 |
double NOTE_OPNB(int ch, int note) { |
| 94 |
if (ch>=adpcmBChanOffs) { // ADPCM |
| 95 |
return NOTE_ADPCMB(note); |
| 96 |
} else if (ch>=psgChanOffs) { // PSG |
| 97 |
return NOTE_PERIODIC(note); |
| 98 |
} |
| 99 |
// FM |
| 100 |
return NOTE_FNUM_BLOCK(note,11,chan[ch].state.block); |
| 101 |
} |
| 102 |
double NOTE_ADPCMB(int note) { |
| 103 |
if (chan[adpcmBChanOffs].sample>=0 && chan[adpcmBChanOffs].sample<parent->song.sampleLen) { |
| 104 |
double off=65535.0*(double)(parent->getSample(chan[adpcmBChanOffs].sample)->centerRate)/parent->getCenterRate(); |
| 105 |
return parent->calcBaseFreq((double)chipClock/144,off,note,false); |
| 106 |
} |
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
public: |
| 111 |
void fillStream(std::vector<DivDelayedWrite>& stream, int sRate, size_t len) { |
| 112 |
ay->fillStream(stream,sRate,len); |
| 113 |
} |
| 114 |
|
| 115 |
void reset() { |
| 116 |
writeADPCMAOff=0; |
| 117 |
writeADPCMAOn=0; |
| 118 |
globalADPCMAVolume=0x3f; |
| 119 |
|
| 120 |
OPN2_Reset(&fm_nuked); |
| 121 |
OPN2_SetChipType(&fm_nuked,ym3438_mode_opn); |
| 122 |
|
| 123 |
memset(&fm_lle,0,sizeof(fmopna_2610_t)); |
| 124 |
|
| 125 |
if (useCombo==2) { |
| 126 |
fm_lle.input.cs=1; |
| 127 |
fm_lle.input.rd=0; |
| 128 |
fm_lle.input.wr=0; |
| 129 |
fm_lle.input.a0=0; |
| 130 |
fm_lle.input.a1=0; |
| 131 |
fm_lle.input.data=0; |
| 132 |
fm_lle.input.rad=0; |
| 133 |
fm_lle.input.pad=0; |
| 134 |
fm_lle.input.test=1; |
| 135 |
|
| 136 |
fm_lle.input.ic=1; |
| 137 |
for (size_t h=0; h<576; h++) { |
| 138 |
FMOPNA_2610_Clock(&fm_lle,0); |
| 139 |
FMOPNA_2610_Clock(&fm_lle,1); |
| 140 |
} |
| 141 |
|
| 142 |
fm_lle.input.ic=0; |
| 143 |
for (size_t h=0; h<576; h++) { |
| 144 |
FMOPNA_2610_Clock(&fm_lle,0); |
| 145 |
FMOPNA_2610_Clock(&fm_lle,1); |
| 146 |
} |
| 147 |
|
| 148 |
fm_lle.input.ic=1; |
| 149 |
for (size_t h=0; h<576; h++) { |
| 150 |
FMOPNA_2610_Clock(&fm_lle,0); |
| 151 |
FMOPNA_2610_Clock(&fm_lle,1); |
| 152 |
} |
| 153 |
|
| 154 |
for (int i=0; i<6; i++) { |
| 155 |
rssOut[i]=0; |
| 156 |
} |
| 157 |
|
| 158 |
dacVal=0; |
| 159 |
dacVal2=0; |
| 160 |
dacOut[0]=0; |
| 161 |
dacOut[1]=0; |
| 162 |
lastSH=0; |
| 163 |
lastSH2=0; |
| 164 |
lastS=0; |
| 165 |
rmpx=0; |
| 166 |
pmpx=0; |
| 167 |
roe=0; |
| 168 |
poe=0; |
| 169 |
rssCycle=0; |
| 170 |
rssSubCycle=0; |
| 171 |
adMemAddrA=0; |
| 172 |
adMemAddrB=0; |
| 173 |
} |
| 174 |
|
| 175 |
ay->reset(); |
| 176 |
ay->getRegisterWrites().clear(); |
| 177 |
ay->flushWrites(); |
| 178 |
} |
| 179 |
|
| 180 |
void muteChannel(int ch, bool mute) { |
| 181 |
isMuted[ch]=mute; |
| 182 |
if (ch>=adpcmBChanOffs) { // ADPCM-B |
| 183 |
immWrite(0x11,isMuted[ch]?0:(chan[ch].pan<<6)); |
| 184 |
} |
| 185 |
if (ch>=adpcmAChanOffs) { // ADPCM-A |
| 186 |
immWrite(0x108+(ch-adpcmAChanOffs),isMuted[ch]?0:((chan[ch].pan<<6)|chan[ch].outVol)); |
| 187 |
return; |
| 188 |
} |
| 189 |
if (ch>=psgChanOffs) { // PSG |
| 190 |
ay->muteChannel(ch-psgChanOffs,mute); |
| 191 |
return; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
int getOutputCount() { |
| 196 |
return 2; |
| 197 |
} |
| 198 |
|
| 199 |
const void* getSampleMem(int index) { |
| 200 |
return index == 0 ? adpcmAMem : index == 1 ? adpcmBMem : NULL; |
| 201 |
} |
| 202 |
|
| 203 |
size_t getSampleMemCapacity(int index) { |
| 204 |
return index == 0 ? 16777216 : index == 1 ? 16777216 : 0; |
| 205 |
} |
| 206 |
|
| 207 |
const char* getSampleMemName(int index=0) { |
| 208 |
return index == 0 ? "ADPCM-A" : index == 1 ? "ADPCM-B" : NULL; |
| 209 |
} |
| 210 |
|
| 211 |
size_t getSampleMemUsage(int index) { |
| 212 |
return index == 0 ? adpcmAMemLen : index == 1 ? adpcmBMemLen : 0; |
| 213 |
} |
| 214 |
|
| 215 |
bool isSampleLoaded(int index, int sample) { |
| 216 |
if (index<0 || index>1) return false; |
| 217 |
if (sample<0 || sample>32767) return false; |
| 218 |
return sampleLoaded[index][sample]; |
| 219 |
} |
| 220 |
|
| 221 |
const DivMemoryComposition* getMemCompo(int index) { |
| 222 |
if (index==0) return &memCompoA; |
| 223 |
if (index==1) return &memCompoB; |
| 224 |
return NULL; |
| 225 |
} |
| 226 |
|
| 227 |
void renderSamples(int sysID) { |
| 228 |
memset(adpcmAMem,0,getSampleMemCapacity(0)); |
| 229 |
memset(sampleOffA,0,32768*sizeof(unsigned int)); |
| 230 |
memset(sampleOffB,0,32768*sizeof(unsigned int)); |
| 231 |
memset(sampleLoaded[0],0,32768*sizeof(bool)); |
| 232 |
memset(sampleLoaded[1],0,32768*sizeof(bool)); |
| 233 |
|
| 234 |
memCompoA=DivMemoryComposition(); |
| 235 |
memCompoA.name="ADPCM-A"; |
| 236 |
|
| 237 |
memCompoB=DivMemoryComposition(); |
| 238 |
memCompoB.name="ADPCM-B"; |
| 239 |
|
| 240 |
size_t memPos=0; |
| 241 |
for (int i=0; i<parent->song.sampleLen; i++) { |
| 242 |
DivSample* s=parent->song.sample[i]; |
| 243 |
if (!s->renderOn[0][sysID]) { |
| 244 |
sampleOffA[i]=0; |
| 245 |
continue; |
| 246 |
} |
| 247 |
|
| 248 |
int paddedLen=(s->lengthA+255)&(~0xff); |
| 249 |
if ((memPos&0xf00000)!=((memPos+paddedLen)&0xf00000)) { |
| 250 |
memPos=(memPos+0xfffff)&0xf00000; |
| 251 |
} |
| 252 |
if (memPos>=getSampleMemCapacity(0)) { |
| 253 |
logW("out of ADPCM-A memory for sample %d!",i); |
| 254 |
break; |
| 255 |
} |
| 256 |
if (memPos+paddedLen>=getSampleMemCapacity(0)) { |
| 257 |
memcpy(adpcmAMem+memPos,s->dataA,getSampleMemCapacity(0)-memPos); |
| 258 |
logW("out of ADPCM-A memory for sample %d!",i); |
| 259 |
} else { |
| 260 |
memcpy(adpcmAMem+memPos,s->dataA,paddedLen); |
| 261 |
sampleLoaded[0][i]=true; |
| 262 |
} |
| 263 |
sampleOffA[i]=memPos; |
| 264 |
memCompoA.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+paddedLen)); |
| 265 |
memPos+=paddedLen; |
| 266 |
} |
| 267 |
adpcmAMemLen=memPos+256; |
| 268 |
|
| 269 |
memCompoA.used=adpcmAMemLen; |
| 270 |
memCompoA.capacity=getSampleMemCapacity(0); |
| 271 |
|
| 272 |
memset(adpcmBMem,0,getSampleMemCapacity(1)); |
| 273 |
|
| 274 |
memPos=0; |
| 275 |
for (int i=0; i<parent->song.sampleLen; i++) { |
| 276 |
DivSample* s=parent->song.sample[i]; |
| 277 |
if (!s->renderOn[1][sysID]) { |
| 278 |
sampleOffB[i]=0; |
| 279 |
continue; |
| 280 |
} |
| 281 |
|
| 282 |
int paddedLen=(s->lengthB+255)&(~0xff); |
| 283 |
if ((memPos&0xf00000)!=((memPos+paddedLen)&0xf00000)) { |
| 284 |
memPos=(memPos+0xfffff)&0xf00000; |
| 285 |
} |
| 286 |
if (memPos>=getSampleMemCapacity(1)) { |
| 287 |
logW("out of ADPCM-B memory for sample %d!",i); |
| 288 |
break; |
| 289 |
} |
| 290 |
if (memPos+paddedLen>=getSampleMemCapacity(1)) { |
| 291 |
memcpy(adpcmBMem+memPos,s->dataB,getSampleMemCapacity(1)-memPos); |
| 292 |
logW("out of ADPCM-B memory for sample %d!",i); |
| 293 |
} else { |
| 294 |
memcpy(adpcmBMem+memPos,s->dataB,paddedLen); |
| 295 |
sampleLoaded[1][i]=true; |
| 296 |
} |
| 297 |
sampleOffB[i]=memPos; |
| 298 |
memCompoB.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+paddedLen)); |
| 299 |
memPos+=paddedLen; |
| 300 |
} |
| 301 |
adpcmBMemLen=memPos+256; |
| 302 |
|
| 303 |
memCompoB.used=adpcmBMemLen; |
| 304 |
memCompoB.capacity=getSampleMemCapacity(1); |
| 305 |
} |
| 306 |
|
| 307 |
void setFlags(const DivConfig& flags) { |
| 308 |
switch (flags.getInt("clockSel",0)) { |
| 309 |
case 0x01: |
| 310 |
chipClock=24167829/3; |
| 311 |
break; |
| 312 |
default: |
| 313 |
chipClock=8000000.0; |
| 314 |
break; |
| 315 |
} |
| 316 |
CHECK_CUSTOM_CLOCK; |
| 317 |
noExtMacros=flags.getBool("noExtMacros",false); |
| 318 |
fbAllOps=flags.getBool("fbAllOps",false); |
| 319 |
ssgVol=flags.getInt("ssgVol",128); |
| 320 |
fmVol=flags.getInt("fmVol",256); |
| 321 |
if (useCombo==2) { |
| 322 |
rate=chipClock/144; |
| 323 |
} else { |
| 324 |
rate=fm->sample_rate(chipClock); |
| 325 |
} |
| 326 |
for (int i=0; i<17; i++) { |
| 327 |
oscBuf[i]->setRate(rate); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
int init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 332 |
parent=p; |
| 333 |
ayFlags.set("chipType",1); |
| 334 |
dumpWrites=false; |
| 335 |
skipRegisterWrites=false; |
| 336 |
for (int i=0; i<17; i++) { |
| 337 |
isMuted[i]=false; |
| 338 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 339 |
} |
| 340 |
adpcmAMem=new unsigned char[getSampleMemCapacity(0)]; |
| 341 |
adpcmAMemLen=0; |
| 342 |
adpcmBMem=new unsigned char[getSampleMemCapacity(1)]; |
| 343 |
adpcmBMemLen=0; |
| 344 |
iface.adpcmAMem=adpcmAMem; |
| 345 |
iface.adpcmBMem=adpcmBMem; |
| 346 |
iface.sampleBank=0; |
| 347 |
fm=new ymfm::ym2610b(iface); |
| 348 |
fm->set_fidelity(ymfm::OPN_FIDELITY_MED); |
| 349 |
setFlags(flags); |
| 350 |
// YM2149, 2MHz |
| 351 |
ay=new DivPlatformAY8910(true,chipClock,32,144); |
| 352 |
ay->setCore(0); |
| 353 |
ay->init(p,3,sugRate,ayFlags); |
| 354 |
ay->toggleRegisterDump(true); |
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
void quit() { |
| 359 |
for (int i=0; i<17; i++) { |
| 360 |
delete oscBuf[i]; |
| 361 |
} |
| 362 |
ay->quit(); |
| 363 |
delete ay; |
| 364 |
delete[] adpcmAMem; |
| 365 |
delete[] adpcmBMem; |
| 366 |
} |
| 367 |
|
| 368 |
DivPlatformYM2610Base(int ext, int psg, int adpcmA, int adpcmB, int chanCount): |
| 369 |
DivPlatformOPN(ext,psg,adpcmA,adpcmB,chanCount,9440540.0, 72, 32, false, 16) { |
| 370 |
sampleOffA=new unsigned int[32768]; |
| 371 |
sampleOffB=new unsigned int[32768]; |
| 372 |
sampleLoaded[0]=new bool[32768]; |
| 373 |
sampleLoaded[1]=new bool[32768]; |
| 374 |
} |
| 375 |
~DivPlatformYM2610Base() { |
| 376 |
delete[] sampleOffA; |
| 377 |
delete[] sampleOffB; |
| 378 |
delete[] sampleLoaded[0]; |
| 379 |
delete[] sampleLoaded[1]; |
| 380 |
} |
| 381 |
}; |
| 382 |
|
| 383 |
#endif |