| 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 "k007232.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include "../../ta-log.h" |
| 23 |
#include <math.h> |
| 24 |
|
| 25 |
#define rWrite(a,v) {if(!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if(dumpWrites) addWrite(a,v);}} |
| 26 |
|
| 27 |
#define CHIP_DIVIDER 64 |
| 28 |
|
| 29 |
const char* regCheatSheetK007232[]={ |
| 30 |
// on-chip |
| 31 |
"CHX_FreqL", "X*6+0", |
| 32 |
"CHX_FreqH", "X*6+1", |
| 33 |
"CHX_StartL", "X*6+2", |
| 34 |
"CHX_StartM", "X*6+3", |
| 35 |
"CHX_StartH", "X*6+4", |
| 36 |
"CHX_Keyon", "X*6+5", |
| 37 |
"SLEV", "C", // external IO (Volume for Mono speaker) |
| 38 |
"Loop", "D", |
| 39 |
// off-chip |
| 40 |
"CHX_Volume", "X*2+10", |
| 41 |
"CHX_Bank", "X*2+12", |
| 42 |
NULL |
| 43 |
}; |
| 44 |
|
| 45 |
const char** DivPlatformK007232::getRegisterSheet() { |
| 46 |
return regCheatSheetK007232; |
| 47 |
} |
| 48 |
|
| 49 |
inline void DivPlatformK007232::chWrite(unsigned char ch, unsigned int addr, unsigned char val) { |
| 50 |
if (!skipRegisterWrites) { |
| 51 |
if ((ch<2) && (addr<6)) { |
| 52 |
rWrite((ch*6)+(addr&7),val); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
void DivPlatformK007232::acquireDirect(blip_buffer_t** bb, size_t len) { |
| 58 |
for (int i=0; i<2; i++) { |
| 59 |
oscBuf[i]->begin(len); |
| 60 |
} |
| 61 |
|
| 62 |
for (size_t h=0; h<len; h++) { |
| 63 |
int advance=len-h; |
| 64 |
if (!writes.empty()) { |
| 65 |
advance=1; |
| 66 |
QueuedWrite& w=writes.front(); |
| 67 |
// write on-chip register |
| 68 |
if (w.addr<=0xd) { |
| 69 |
k007232.write(w.addr,w.val); |
| 70 |
} |
| 71 |
regPool[w.addr]=w.val; |
| 72 |
writes.pop(); |
| 73 |
} else { |
| 74 |
for (int i=0; i<2; i++) { |
| 75 |
if (k007232.m_voice[i].m_busy) { |
| 76 |
const int remain=4096-k007232.m_voice[i].m_counter; |
| 77 |
if (remain<advance) advance=remain; |
| 78 |
} |
| 79 |
} |
| 80 |
if (advance<1) advance=1; |
| 81 |
} |
| 82 |
|
| 83 |
k007232.tick(advance); |
| 84 |
|
| 85 |
h+=advance-1; |
| 86 |
|
| 87 |
if (stereo) { |
| 88 |
const unsigned char vol1=regPool[0x10],vol2=regPool[0x11]; |
| 89 |
const signed int lout[2]={(k007232.output(0)*(vol1&0xf)),(k007232.output(1)*(vol2&0xf))}; |
| 90 |
const signed int rout[2]={(k007232.output(0)*((vol1>>4)&0xf)),(k007232.output(1)*((vol2>>4)&0xf))}; |
| 91 |
const int outL=(lout[0]+lout[1])<<4; |
| 92 |
const int outR=(rout[0]+rout[1])<<4; |
| 93 |
|
| 94 |
if (outL!=lastOut[0]) { |
| 95 |
blip_add_delta(bb[0],h,outL-lastOut[0]); |
| 96 |
lastOut[0]=outL; |
| 97 |
} |
| 98 |
if (outR!=lastOut[1]) { |
| 99 |
blip_add_delta(bb[1],h,outR-lastOut[1]); |
| 100 |
lastOut[1]=outR; |
| 101 |
} |
| 102 |
|
| 103 |
for (int i=0; i<2; i++) { |
| 104 |
oscBuf[i]->putSample(h,(lout[i]+rout[i])<<3); |
| 105 |
} |
| 106 |
} else { |
| 107 |
const unsigned char vol=regPool[0xc]; |
| 108 |
const signed int out[2]={(k007232.output(0)*(vol&0xf)),(k007232.output(1)*((vol>>4)&0xf))}; |
| 109 |
const int outFinal=(out[0]+out[1])<<4; |
| 110 |
|
| 111 |
if (outFinal!=lastOut[0]) { |
| 112 |
blip_add_delta(bb[0],h,outFinal-lastOut[0]); |
| 113 |
lastOut[0]=outFinal; |
| 114 |
} |
| 115 |
|
| 116 |
for (int i=0; i<2; i++) { |
| 117 |
oscBuf[i]->putSample(h,out[i]<<4); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
for (int i=0; i<2; i++) { |
| 123 |
oscBuf[i]->end(len); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
u8 DivPlatformK007232::read_sample(u8 ne, u32 address) { |
| 128 |
if ((sampleMem!=NULL) && (address<getSampleMemCapacity())) { |
| 129 |
return sampleMem[((regPool[0x12+(ne&1)]<<17)|(address&0x1ffff))&0xffffff]; |
| 130 |
} |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
void DivPlatformK007232::tick(bool sysTick) { |
| 135 |
for (int i=0; i<2; i++) { |
| 136 |
chan[i].std.next(); |
| 137 |
if (chan[i].std.vol.had) { |
| 138 |
const signed char macroVol=((chan[i].vol&0xf)*MIN(chan[i].macroVolMul,chan[i].std.vol.val))/chan[i].macroVolMul; |
| 139 |
if ((!isMuted[i]) && (macroVol!=chan[i].outVol)) { |
| 140 |
chan[i].outVol=macroVol; |
| 141 |
chan[i].volumeChanged=true; |
| 142 |
} |
| 143 |
} |
| 144 |
if (NEW_ARP_STRAT) { |
| 145 |
chan[i].handleArp(); |
| 146 |
} else if (chan[i].std.arp.had) { |
| 147 |
if (!chan[i].inPorta) { |
| 148 |
chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val)); |
| 149 |
} |
| 150 |
chan[i].freqChanged=true; |
| 151 |
} |
| 152 |
if (chan[i].std.pitch.had) { |
| 153 |
if (chan[i].std.pitch.mode) { |
| 154 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 155 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 156 |
} else { |
| 157 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 158 |
} |
| 159 |
chan[i].freqChanged=true; |
| 160 |
} |
| 161 |
// volume and panning registers are off-chip |
| 162 |
if (chan[i].std.panL.had) { |
| 163 |
chan[i].panning&=0xf0; |
| 164 |
chan[i].panning|=chan[i].std.panL.val&15; |
| 165 |
if ((!isMuted[i]) && stereo) { |
| 166 |
chan[i].volumeChanged=true; |
| 167 |
} |
| 168 |
} |
| 169 |
if (chan[i].std.panR.had) { |
| 170 |
chan[i].panning&=0x0f; |
| 171 |
chan[i].panning|=(chan[i].std.panR.val&15)<<4; |
| 172 |
if ((!isMuted[i]) && stereo) { |
| 173 |
chan[i].volumeChanged=true; |
| 174 |
} |
| 175 |
} |
| 176 |
if (chan[i].std.phaseReset.had) { |
| 177 |
if (chan[i].std.phaseReset.val==1 && chan[i].active) { |
| 178 |
chan[i].audPos=0; |
| 179 |
chan[i].setPos=true; |
| 180 |
} |
| 181 |
} |
| 182 |
if (chan[i].volumeChanged) { |
| 183 |
chan[i].resVol=isMuted[i]?0:chan[i].outVol&0xf; |
| 184 |
if (stereo) { |
| 185 |
chan[i].lvol=((chan[i].resVol&0xf)*((chan[i].panning>>0)&0xf))/15; |
| 186 |
chan[i].rvol=((chan[i].resVol&0xf)*((chan[i].panning>>4)&0xf))/15; |
| 187 |
const int newPan=(chan[i].lvol&0xf)|((chan[i].rvol&0xf)<<4); |
| 188 |
if (chan[i].prevPan!=newPan) { |
| 189 |
rWrite(0x10+i,(chan[i].lvol&0xf)|((chan[i].rvol&0xf)<<4)); |
| 190 |
chan[i].prevPan=newPan; |
| 191 |
} |
| 192 |
} else { |
| 193 |
const unsigned char prevVolume=lastVolume; |
| 194 |
lastVolume=(lastVolume&~(0xf<<(i<<2)))|((chan[i].resVol&0xf)<<(i<<2)); |
| 195 |
if (prevVolume!=lastVolume) { |
| 196 |
rWrite(0xc,lastVolume); |
| 197 |
} |
| 198 |
} |
| 199 |
chan[i].volumeChanged=false; |
| 200 |
} |
| 201 |
if (chan[i].setPos) { |
| 202 |
// force keyon |
| 203 |
chan[i].keyOn=true; |
| 204 |
chan[i].setPos=false; |
| 205 |
} else { |
| 206 |
chan[i].audPos=0; |
| 207 |
} |
| 208 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 209 |
double off=1.0; |
| 210 |
int sample=chan[i].sample; |
| 211 |
if (sample>=0 && sample<parent->song.sampleLen) { |
| 212 |
DivSample* s=parent->getSample(sample); |
| 213 |
if (s->centerRate<1) { |
| 214 |
off=1.0; |
| 215 |
} else { |
| 216 |
off=parent->getCenterRate()/s->centerRate; |
| 217 |
} |
| 218 |
} |
| 219 |
DivSample* s=parent->getSample(chan[i].sample); |
| 220 |
chan[i].freq=0x1000-(int)(off*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)); |
| 221 |
if (chan[i].freq>4095) chan[i].freq=4095; |
| 222 |
if (chan[i].freq<0) chan[i].freq=0; |
| 223 |
if (chan[i].keyOn) { |
| 224 |
unsigned int bank=0; |
| 225 |
unsigned int start=0; |
| 226 |
unsigned int loop=0; |
| 227 |
if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) { |
| 228 |
bank=sampleOffK007232[chan[i].sample]>>17; |
| 229 |
start=sampleOffK007232[chan[i].sample]&0x1ffff; |
| 230 |
loop=start+s->length8; |
| 231 |
} |
| 232 |
if (chan[i].audPos>0) { |
| 233 |
start=start+MIN(chan[i].audPos,MIN(131072-1,s->length8)); |
| 234 |
} |
| 235 |
start=MIN(start,MIN(getSampleMemCapacity(),131072)-1); |
| 236 |
loop=MIN(loop,MIN(getSampleMemCapacity(),131072)-1); |
| 237 |
// force keyoff first |
| 238 |
chWrite(i,2,0xff); |
| 239 |
chWrite(i,3,0xff); |
| 240 |
chWrite(i,4,0x1); |
| 241 |
chWrite(i,5,0); |
| 242 |
// keyon |
| 243 |
const unsigned char prevLoop=lastLoop; |
| 244 |
if (s->isLoopable()) { |
| 245 |
loop=start+s->loopStart; |
| 246 |
lastLoop|=(1<<i); |
| 247 |
} else { |
| 248 |
lastLoop&=~(1<<i); |
| 249 |
} |
| 250 |
if (prevLoop!=lastLoop) { |
| 251 |
rWrite(0xd,lastLoop); |
| 252 |
} |
| 253 |
if (chan[i].prevBank!=(int)bank) { |
| 254 |
rWrite(0x12+i,bank); |
| 255 |
chan[i].prevBank=bank; |
| 256 |
} |
| 257 |
if (chan[i].prevFreq!=chan[i].freq) { |
| 258 |
chWrite(i,0,chan[i].freq&0xff); |
| 259 |
chWrite(i,1,(chan[i].freq>>8)&0xf); |
| 260 |
chan[i].prevFreq=chan[i].freq; |
| 261 |
} |
| 262 |
chWrite(i,2,start&0xff); |
| 263 |
chWrite(i,3,start>>8); |
| 264 |
chWrite(i,4,start>>16); |
| 265 |
chWrite(i,5,0); |
| 266 |
if (s->isLoopable() && start!=loop) { |
| 267 |
chWrite(i,2,loop&0xff); |
| 268 |
chWrite(i,3,loop>>8); |
| 269 |
chWrite(i,4,loop>>16); |
| 270 |
} |
| 271 |
if (!chan[i].std.vol.had) { |
| 272 |
chan[i].outVol=chan[i].vol; |
| 273 |
if (!isMuted[i]) { |
| 274 |
chan[i].volumeChanged=true; |
| 275 |
} |
| 276 |
} |
| 277 |
chan[i].keyOn=false; |
| 278 |
} |
| 279 |
if (chan[i].keyOff) { |
| 280 |
chWrite(i,2,0xff); |
| 281 |
chWrite(i,3,0xff); |
| 282 |
chWrite(i,4,0x1); |
| 283 |
chWrite(i,5,0); |
| 284 |
const unsigned char prevLoop=lastLoop; |
| 285 |
lastLoop&=~(1<<i); |
| 286 |
if (prevLoop!=lastLoop) { |
| 287 |
rWrite(0xd,lastLoop); |
| 288 |
} |
| 289 |
chan[i].keyOff=false; |
| 290 |
} |
| 291 |
if (chan[i].freqChanged) { |
| 292 |
if (chan[i].prevFreq!=chan[i].freq) { |
| 293 |
chWrite(i,0,chan[i].freq&0xff); |
| 294 |
chWrite(i,1,(chan[i].freq>>8)&0xf); |
| 295 |
chan[i].prevFreq=chan[i].freq; |
| 296 |
} |
| 297 |
chan[i].freqChanged=false; |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
int DivPlatformK007232::dispatch(DivCommand c) { |
| 304 |
switch (c.cmd) { |
| 305 |
case DIV_CMD_NOTE_ON: { |
| 306 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA); |
| 307 |
chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:15; |
| 308 |
if (c.value!=DIV_NOTE_NULL) { |
| 309 |
chan[c.chan].sample=ins->amiga.getSample(c.value); |
| 310 |
chan[c.chan].sampleNote=c.value; |
| 311 |
c.value=ins->amiga.getFreq(c.value); |
| 312 |
chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote; |
| 313 |
} |
| 314 |
if (c.value!=DIV_NOTE_NULL) { |
| 315 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value); |
| 316 |
} |
| 317 |
if (chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) { |
| 318 |
chan[c.chan].sample=-1; |
| 319 |
} |
| 320 |
if (c.value!=DIV_NOTE_NULL) { |
| 321 |
chan[c.chan].freqChanged=true; |
| 322 |
chan[c.chan].note=c.value; |
| 323 |
} |
| 324 |
chan[c.chan].active=true; |
| 325 |
chan[c.chan].keyOn=true; |
| 326 |
chan[c.chan].macroInit(ins); |
| 327 |
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { |
| 328 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 329 |
if (!isMuted[c.chan]) { |
| 330 |
chan[c.chan].volumeChanged=true; |
| 331 |
} |
| 332 |
} |
| 333 |
break; |
| 334 |
} |
| 335 |
case DIV_CMD_NOTE_OFF: |
| 336 |
chan[c.chan].sample=-1; |
| 337 |
chan[c.chan].active=false; |
| 338 |
chan[c.chan].keyOff=true; |
| 339 |
chan[c.chan].macroInit(NULL); |
| 340 |
break; |
| 341 |
case DIV_CMD_NOTE_OFF_ENV: |
| 342 |
case DIV_CMD_ENV_RELEASE: |
| 343 |
chan[c.chan].std.release(); |
| 344 |
break; |
| 345 |
case DIV_CMD_INSTRUMENT: |
| 346 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 347 |
chan[c.chan].ins=c.value; |
| 348 |
} |
| 349 |
break; |
| 350 |
case DIV_CMD_VOLUME: |
| 351 |
if (chan[c.chan].vol!=c.value) { |
| 352 |
chan[c.chan].vol=c.value; |
| 353 |
if (!chan[c.chan].std.vol.has) { |
| 354 |
chan[c.chan].outVol=c.value; |
| 355 |
if (!isMuted[c.chan]) { |
| 356 |
chan[c.chan].volumeChanged=true; |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
break; |
| 361 |
case DIV_CMD_GET_VOLUME: |
| 362 |
if (chan[c.chan].std.vol.has) { |
| 363 |
return chan[c.chan].vol; |
| 364 |
} |
| 365 |
return chan[c.chan].outVol; |
| 366 |
break; |
| 367 |
case DIV_CMD_PANNING: |
| 368 |
chan[c.chan].panning=(c.value>>4)|(c.value2&0xf0); |
| 369 |
if (!isMuted[c.chan] && stereo) { |
| 370 |
chan[c.chan].volumeChanged=true; |
| 371 |
} |
| 372 |
break; |
| 373 |
case DIV_CMD_PITCH: |
| 374 |
chan[c.chan].pitch=c.value; |
| 375 |
chan[c.chan].freqChanged=true; |
| 376 |
break; |
| 377 |
case DIV_CMD_NOTE_PORTA: { |
| 378 |
const int destFreq=NOTE_PERIODIC(c.value2+chan[c.chan].sampleNoteDelta); |
| 379 |
bool return2=false; |
| 380 |
if (destFreq>chan[c.chan].baseFreq) { |
| 381 |
chan[c.chan].baseFreq+=c.value; |
| 382 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 383 |
chan[c.chan].baseFreq=destFreq; |
| 384 |
return2=true; |
| 385 |
} |
| 386 |
} else { |
| 387 |
chan[c.chan].baseFreq-=c.value; |
| 388 |
if (chan[c.chan].baseFreq<=destFreq) { |
| 389 |
chan[c.chan].baseFreq=destFreq; |
| 390 |
return2=true; |
| 391 |
} |
| 392 |
} |
| 393 |
chan[c.chan].freqChanged=true; |
| 394 |
if (return2) { |
| 395 |
chan[c.chan].inPorta=false; |
| 396 |
return 2; |
| 397 |
} |
| 398 |
break; |
| 399 |
} |
| 400 |
case DIV_CMD_LEGATO: { |
| 401 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val-12):(0))); |
| 402 |
chan[c.chan].freqChanged=true; |
| 403 |
chan[c.chan].note=c.value; |
| 404 |
break; |
| 405 |
} |
| 406 |
case DIV_CMD_PRE_PORTA: |
| 407 |
if (chan[c.chan].active && c.value2) { |
| 408 |
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA)); |
| 409 |
} |
| 410 |
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); |
| 411 |
chan[c.chan].inPorta=c.value; |
| 412 |
break; |
| 413 |
case DIV_CMD_SAMPLE_POS: |
| 414 |
chan[c.chan].audPos=c.value; |
| 415 |
chan[c.chan].setPos=true; |
| 416 |
break; |
| 417 |
case DIV_CMD_GET_VOLMAX: |
| 418 |
return 15; |
| 419 |
break; |
| 420 |
case DIV_CMD_MACRO_OFF: |
| 421 |
chan[c.chan].std.mask(c.value,true); |
| 422 |
break; |
| 423 |
case DIV_CMD_MACRO_ON: |
| 424 |
chan[c.chan].std.mask(c.value,false); |
| 425 |
break; |
| 426 |
case DIV_CMD_MACRO_RESTART: |
| 427 |
chan[c.chan].std.restart(c.value); |
| 428 |
break; |
| 429 |
default: |
| 430 |
break; |
| 431 |
} |
| 432 |
return 1; |
| 433 |
} |
| 434 |
|
| 435 |
void DivPlatformK007232::muteChannel(int ch, bool mute) { |
| 436 |
isMuted[ch]=mute; |
| 437 |
chan[ch].volumeChanged=true; |
| 438 |
} |
| 439 |
|
| 440 |
void DivPlatformK007232::forceIns() { |
| 441 |
while (!writes.empty()) writes.pop(); |
| 442 |
for (int i=0; i<2; i++) { |
| 443 |
chan[i].insChanged=true; |
| 444 |
chan[i].volumeChanged=true; |
| 445 |
chan[i].freqChanged=true; |
| 446 |
chan[i].prevFreq=-1; |
| 447 |
chan[i].prevBank=-1; |
| 448 |
} |
| 449 |
lastLoop=0; |
| 450 |
lastVolume=0; |
| 451 |
} |
| 452 |
|
| 453 |
void* DivPlatformK007232::getChanState(int ch) { |
| 454 |
return &chan[ch]; |
| 455 |
} |
| 456 |
|
| 457 |
DivMacroInt* DivPlatformK007232::getChanMacroInt(int ch) { |
| 458 |
return &chan[ch].std; |
| 459 |
} |
| 460 |
|
| 461 |
unsigned short DivPlatformK007232::getPan(int ch) { |
| 462 |
return stereo?(((chan[ch].panning&15)<<8)|((chan[ch].panning&0xf0)>>4)):0; |
| 463 |
} |
| 464 |
|
| 465 |
DivDispatchOscBuffer* DivPlatformK007232::getOscBuffer(int ch) { |
| 466 |
return oscBuf[ch]; |
| 467 |
} |
| 468 |
|
| 469 |
void DivPlatformK007232::reset() { |
| 470 |
while (!writes.empty()) { |
| 471 |
writes.pop(); |
| 472 |
} |
| 473 |
memset(regPool,0,20); |
| 474 |
k007232.reset(); |
| 475 |
lastLoop=0; |
| 476 |
lastVolume=0; |
| 477 |
lastOut[0]=0; |
| 478 |
lastOut[1]=0; |
| 479 |
for (int i=0; i<2; i++) { |
| 480 |
chan[i]=DivPlatformK007232::Channel(); |
| 481 |
chan[i].std.setEngine(parent); |
| 482 |
// keyoff all channels |
| 483 |
chWrite(i,0,0); |
| 484 |
chWrite(i,1,0); |
| 485 |
chWrite(i,2,0xff); |
| 486 |
chWrite(i,3,0xff); |
| 487 |
chWrite(i,4,1); |
| 488 |
chWrite(i,5,0); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
int DivPlatformK007232::getOutputCount() { |
| 493 |
return stereo?2:1; |
| 494 |
} |
| 495 |
|
| 496 |
bool DivPlatformK007232::hasAcquireDirect() { |
| 497 |
return true; |
| 498 |
} |
| 499 |
|
| 500 |
void DivPlatformK007232::notifyInsChange(int ins) { |
| 501 |
for (int i=0; i<2; i++) { |
| 502 |
if (chan[i].ins==ins) { |
| 503 |
chan[i].insChanged=true; |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
void DivPlatformK007232::notifyWaveChange(int wave) { |
| 509 |
} |
| 510 |
|
| 511 |
void DivPlatformK007232::notifyInsDeletion(void* ins) { |
| 512 |
for (int i=0; i<2; i++) { |
| 513 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
void DivPlatformK007232::setFlags(const DivConfig& flags) { |
| 518 |
chipClock=COLOR_NTSC; |
| 519 |
CHECK_CUSTOM_CLOCK; |
| 520 |
rate=chipClock/4; |
| 521 |
stereo=flags.getBool("stereo",false); |
| 522 |
for (int i=0; i<2; i++) { |
| 523 |
chan[i].volumeChanged=true; |
| 524 |
oscBuf[i]->setRate(rate); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
void DivPlatformK007232::poke(unsigned int addr, unsigned short val) { |
| 529 |
rWrite(addr&0x1f,val); |
| 530 |
} |
| 531 |
|
| 532 |
void DivPlatformK007232::poke(std::vector<DivRegWrite>& wlist) { |
| 533 |
for (DivRegWrite& i: wlist) rWrite(i.addr&0x1f,i.val); |
| 534 |
} |
| 535 |
|
| 536 |
unsigned char* DivPlatformK007232::getRegisterPool() { |
| 537 |
return regPool; |
| 538 |
} |
| 539 |
|
| 540 |
int DivPlatformK007232::getRegisterPoolSize() { |
| 541 |
return 20; |
| 542 |
} |
| 543 |
|
| 544 |
const void* DivPlatformK007232::getSampleMem(int index) { |
| 545 |
return index == 0 ? sampleMem : NULL; |
| 546 |
} |
| 547 |
|
| 548 |
size_t DivPlatformK007232::getSampleMemCapacity(int index) { |
| 549 |
return index == 0 ? 16777216 : 0; |
| 550 |
} |
| 551 |
|
| 552 |
size_t DivPlatformK007232::getSampleMemUsage(int index) { |
| 553 |
return index == 0 ? sampleMemLen : 0; |
| 554 |
} |
| 555 |
|
| 556 |
bool DivPlatformK007232::isSampleLoaded(int index, int sample) { |
| 557 |
if (index!=0) return false; |
| 558 |
if (sample<0 || sample>32767) return false; |
| 559 |
return sampleLoaded[sample]; |
| 560 |
} |
| 561 |
|
| 562 |
const DivMemoryComposition* DivPlatformK007232::getMemCompo(int index) { |
| 563 |
if (index!=0) return NULL; |
| 564 |
return &memCompo; |
| 565 |
} |
| 566 |
|
| 567 |
void DivPlatformK007232::renderSamples(int sysID) { |
| 568 |
memset(sampleMem,0xc0,getSampleMemCapacity()); |
| 569 |
memset(sampleOffK007232,0,32768*sizeof(unsigned int)); |
| 570 |
memset(sampleLoaded,0,32768*sizeof(bool)); |
| 571 |
|
| 572 |
memCompo=DivMemoryComposition(); |
| 573 |
memCompo.name="Sample ROM"; |
| 574 |
|
| 575 |
size_t memPos=0; |
| 576 |
for (int i=0; i<parent->song.sampleLen; i++) { |
| 577 |
DivSample* s=parent->song.sample[i]; |
| 578 |
if (!s->renderOn[0][sysID]) { |
| 579 |
sampleOffK007232[i]=0; |
| 580 |
continue; |
| 581 |
} |
| 582 |
|
| 583 |
const int length=s->getLoopEndPosition(DIV_SAMPLE_DEPTH_8BIT); |
| 584 |
int actualLength=MIN((int)(getSampleMemCapacity()-memPos)-2,length); |
| 585 |
if (actualLength>0) { |
| 586 |
if (actualLength>131072-2) { |
| 587 |
actualLength=131072-2; |
| 588 |
} |
| 589 |
if ((memPos&0xfe0000)!=((memPos+actualLength+1)&0xfe0000)) { |
| 590 |
memPos=(memPos+0x1ffff)&0xfe0000; |
| 591 |
} |
| 592 |
sampleOffK007232[i]=memPos; |
| 593 |
memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+actualLength+1)); |
| 594 |
for (int j=0; j<actualLength; j++) { |
| 595 |
// convert to 7 bit unsigned |
| 596 |
unsigned char val=(unsigned char)(s->data8[j])^0x80; |
| 597 |
sampleMem[memPos++]=(val>>1)&0x7f; |
| 598 |
} |
| 599 |
// write end of sample marker |
| 600 |
memset(&sampleMem[memPos],0xc0,1); |
| 601 |
memPos+=1; |
| 602 |
} |
| 603 |
if ((memPos+MAX(actualLength,0))>=(getSampleMemCapacity()-1)) { |
| 604 |
logW("out of K007232 PCM memory for sample %d!",i); |
| 605 |
break; |
| 606 |
} else { |
| 607 |
sampleLoaded[i]=true; |
| 608 |
} |
| 609 |
} |
| 610 |
sampleMemLen=memPos; |
| 611 |
|
| 612 |
memCompo.used=sampleMemLen; |
| 613 |
memCompo.capacity=16777216; |
| 614 |
} |
| 615 |
|
| 616 |
int DivPlatformK007232::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 617 |
parent=p; |
| 618 |
dumpWrites=false; |
| 619 |
skipRegisterWrites=false; |
| 620 |
|
| 621 |
for (int i=0; i<2; i++) { |
| 622 |
isMuted[i]=false; |
| 623 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 624 |
} |
| 625 |
sampleMem=new unsigned char[getSampleMemCapacity()]; |
| 626 |
sampleMemLen=0; |
| 627 |
setFlags(flags); |
| 628 |
reset(); |
| 629 |
|
| 630 |
return 2; |
| 631 |
} |
| 632 |
|
| 633 |
void DivPlatformK007232::quit() { |
| 634 |
delete[] sampleMem; |
| 635 |
for (int i=0; i<2; i++) { |
| 636 |
delete oscBuf[i]; |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
// initialization of important arrays |
| 641 |
DivPlatformK007232::DivPlatformK007232(): |
| 642 |
DivDispatch(), |
| 643 |
k007232_intf(), |
| 644 |
k007232(*this) { |
| 645 |
sampleOffK007232=new unsigned int[32768]; |
| 646 |
sampleLoaded=new bool[32768]; |
| 647 |
} |
| 648 |
|
| 649 |
DivPlatformK007232::~DivPlatformK007232() { |
| 650 |
delete[] sampleOffK007232; |
| 651 |
delete[] sampleLoaded; |
| 652 |
} |