| 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 "n163.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 |
#define rWriteMask(a,v,m) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v,m)); if (dumpWrites) {addWrite(a,v);} } |
| 27 |
#define chWrite(c,a,v) \ |
| 28 |
if (c<=chanMax) { \ |
| 29 |
rWrite(0x78-(c<<3)+(a&7),v) \ |
| 30 |
} |
| 31 |
|
| 32 |
#define chWriteMask(c,a,v,m) \ |
| 33 |
if (c<=chanMax) { \ |
| 34 |
rWriteMask(0x78-(c<<3)+(a&7),v,m) \ |
| 35 |
} |
| 36 |
|
| 37 |
#define CHIP_FREQBASE (15*524288) |
| 38 |
|
| 39 |
const char* regCheatSheetN163[]={ |
| 40 |
"FreqL7", "40", |
| 41 |
"AccL7", "41", |
| 42 |
"FreqM7", "42", |
| 43 |
"AccM7", "43", |
| 44 |
"WavLen_FreqH7", "44", |
| 45 |
"AccH7", "45", |
| 46 |
"WavPos7", "46", |
| 47 |
"Vol7", "47", |
| 48 |
"FreqL6", "48", |
| 49 |
"AccL6", "49", |
| 50 |
"FreqM6", "4A", |
| 51 |
"AccM6", "4B", |
| 52 |
"WavLen_FreqH6", "4C", |
| 53 |
"AccH6", "4D", |
| 54 |
"WavPos6", "4E", |
| 55 |
"Vol6", "4F", |
| 56 |
"FreqL5", "50", |
| 57 |
"AccL5", "51", |
| 58 |
"FreqM5", "52", |
| 59 |
"AccM5", "53", |
| 60 |
"WavLen_FreqH5", "54", |
| 61 |
"AccH5", "55", |
| 62 |
"WavPos5", "56", |
| 63 |
"Vol5", "57", |
| 64 |
"FreqL4", "58", |
| 65 |
"AccL4", "59", |
| 66 |
"FreqM4", "5A", |
| 67 |
"AccM4", "5B", |
| 68 |
"WavLen_FreqH4", "5C", |
| 69 |
"AccH4", "5D", |
| 70 |
"WavPos4", "5E", |
| 71 |
"Vol4", "5F", |
| 72 |
"FreqL3", "60", |
| 73 |
"AccL3", "61", |
| 74 |
"FreqM3", "62", |
| 75 |
"AccM3", "63", |
| 76 |
"WavLen_FreqH3", "64", |
| 77 |
"AccH3", "65", |
| 78 |
"WavPos3", "66", |
| 79 |
"Vol3", "67", |
| 80 |
"FreqL2", "68", |
| 81 |
"AccL2", "69", |
| 82 |
"FreqM2", "6A", |
| 83 |
"AccM2", "6B", |
| 84 |
"WavLen_FreqH2", "6C", |
| 85 |
"AccH2", "6D", |
| 86 |
"WavPos2", "6E", |
| 87 |
"Vol2", "6F", |
| 88 |
"FreqL1", "70", |
| 89 |
"AccL1", "71", |
| 90 |
"FreqM1", "72", |
| 91 |
"AccM1", "73", |
| 92 |
"WavLen_FreqH1", "74", |
| 93 |
"AccH1", "75", |
| 94 |
"WavPos1", "76", |
| 95 |
"Vol1", "77", |
| 96 |
"FreqL0", "78", |
| 97 |
"AccL0", "79", |
| 98 |
"FreqM0", "7A", |
| 99 |
"AccM0", "7B", |
| 100 |
"WavLen_FreqH0", "7C", |
| 101 |
"AccH0", "7D", |
| 102 |
"WavPos0", "7E", |
| 103 |
"ChanMax_Vol0", "7F", |
| 104 |
NULL |
| 105 |
}; |
| 106 |
|
| 107 |
const char** DivPlatformN163::getRegisterSheet() { |
| 108 |
return regCheatSheetN163; |
| 109 |
} |
| 110 |
|
| 111 |
void DivPlatformN163::acquire(short** buf, size_t len) { |
| 112 |
for (int i=0; i<8; i++) { |
| 113 |
oscBuf[i]->begin(len); |
| 114 |
} |
| 115 |
|
| 116 |
for (size_t i=0; i<len; i++) { |
| 117 |
n163.tick(); |
| 118 |
int out=(n163.out()<<6)*2; // scale to 16 bit |
| 119 |
if (out>32767) out=32767; |
| 120 |
if (out<-32768) out=-32768; |
| 121 |
buf[0][i]=out; |
| 122 |
|
| 123 |
if (n163.voice_cycle()==0x78) for (int j=0; j<8; j++) { |
| 124 |
oscBuf[j]->putSample(i,n163.voice_out(j)<<7); |
| 125 |
} |
| 126 |
|
| 127 |
// command queue |
| 128 |
while (!writes.empty()) { |
| 129 |
QueuedWrite w=writes.front(); |
| 130 |
n163.addr_w(w.addr); |
| 131 |
n163.data_w((n163.data_r()&~w.mask)|(w.val&w.mask)); |
| 132 |
writes.pop(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
for (int i=0; i<8; i++) { |
| 137 |
oscBuf[i]->end(len); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void DivPlatformN163::updateWave(int ch, int wave, int pos, int len) { |
| 142 |
len&=0xfc; // 4 nibble boundary |
| 143 |
if (wave<0) { |
| 144 |
// load from wave synth |
| 145 |
if (ch>=0) { |
| 146 |
for (int i=0; i<len; i++) { |
| 147 |
unsigned char addr=(pos+i); // address (nibble each) |
| 148 |
if (addr>=((0x78-(chanMax<<3))<<1)) { // avoid conflict with channel register area |
| 149 |
break; |
| 150 |
} |
| 151 |
unsigned char mask=(addr&1)?0xf0:0x0f; |
| 152 |
int data=chan[ch].ws.output[i]; |
| 153 |
rWriteMask(addr>>1,(addr&1)?(data<<4):(data&0xf),mask); |
| 154 |
} |
| 155 |
} |
| 156 |
} else { |
| 157 |
// load from custom |
| 158 |
DivWavetable* wt=parent->getWave(wave); |
| 159 |
for (int i=0; i<wt->len; i++) { |
| 160 |
unsigned char addr=(pos+i); // address (nibble each) |
| 161 |
if (addr>=((0x78-(chanMax<<3))<<1)) { // avoid conflict with channel register area |
| 162 |
break; |
| 163 |
} |
| 164 |
unsigned char mask=(addr&1)?0xf0:0x0f; |
| 165 |
if (wt->max<1 || wt->len<1) { |
| 166 |
rWriteMask(addr>>1,0,mask); |
| 167 |
} else { |
| 168 |
int data=wt->data[i]*15/wt->max; |
| 169 |
if (data<0) data=0; |
| 170 |
if (data>15) data=15; |
| 171 |
rWriteMask(addr>>1,(addr&1)?(data<<4):(data&0xf),mask); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
void DivPlatformN163::updateWaveCh(int ch) { |
| 178 |
if (ch<=chanMax) { |
| 179 |
//logV("updateWave with pos %d and len %d",chan[ch].wavePos,chan[ch].waveLen); |
| 180 |
updateWave(ch,-1,chan[ch].wavePos,chan[ch].waveLen); |
| 181 |
if (chan[ch].active && !isMuted[ch]) { |
| 182 |
chan[ch].volumeChanged=true; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
void DivPlatformN163::tick(bool sysTick) { |
| 188 |
for (int i=0; i<=chanMax; i++) { |
| 189 |
chan[i].std.next(); |
| 190 |
if (chan[i].std.vol.had) { |
| 191 |
chan[i].outVol=(MIN(15,chan[i].std.vol.val)*(chan[i].vol&15))/15; |
| 192 |
if (chan[i].outVol<0) chan[i].outVol=0; |
| 193 |
if (chan[i].outVol>15) chan[i].outVol=15; |
| 194 |
if (chan[i].resVol!=chan[i].outVol) { |
| 195 |
chan[i].resVol=chan[i].outVol; |
| 196 |
if (!isMuted[i]) { |
| 197 |
chan[i].volumeChanged=true; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
if (NEW_ARP_STRAT) { |
| 202 |
chan[i].handleArp(); |
| 203 |
} else if (chan[i].std.arp.had) { |
| 204 |
if (!chan[i].inPorta) { |
| 205 |
chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val)); |
| 206 |
} |
| 207 |
chan[i].freqChanged=true; |
| 208 |
} |
| 209 |
if (chan[i].std.duty.had) { |
| 210 |
if (chan[i].curWavePos!=chan[i].std.duty.val) { |
| 211 |
chan[i].curWavePos=chan[i].std.duty.val; |
| 212 |
chan[i].waveChanged=true; |
| 213 |
} |
| 214 |
} |
| 215 |
if (chan[i].std.wave.had) { |
| 216 |
if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) { |
| 217 |
chan[i].wave=chan[i].std.wave.val; |
| 218 |
chan[i].ws.changeWave1(chan[i].wave); |
| 219 |
if (chan[i].waveMode) { |
| 220 |
chan[i].waveUpdated=true; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
if (chan[i].std.pitch.had) { |
| 225 |
if (chan[i].std.pitch.mode) { |
| 226 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 227 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 228 |
} else { |
| 229 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 230 |
} |
| 231 |
chan[i].freqChanged=true; |
| 232 |
} |
| 233 |
if (chan[i].std.ex1.had) { |
| 234 |
if (chan[i].curWaveLen!=(chan[i].std.ex1.val&0xfc)) { |
| 235 |
chan[i].curWaveLen=chan[i].std.ex1.val&0xfc; |
| 236 |
chan[i].freqChanged=true; |
| 237 |
} |
| 238 |
} |
| 239 |
if (chan[i].volumeChanged) { |
| 240 |
if (chan[i].active && !isMuted[i]) { |
| 241 |
chWriteMask(i,0x7,chan[i].resVol&0xf,0xf); |
| 242 |
} else { |
| 243 |
chWriteMask(i,0x7,0,0xf); |
| 244 |
} |
| 245 |
chan[i].volumeChanged=false; |
| 246 |
} |
| 247 |
if (chan[i].waveChanged) { |
| 248 |
chWrite(i,0x6,chan[i].curWavePos); |
| 249 |
if (chan[i].active) { |
| 250 |
chan[i].freqChanged=true; |
| 251 |
} |
| 252 |
chan[i].waveChanged=false; |
| 253 |
} |
| 254 |
if (chan[i].active) { |
| 255 |
if (chan[i].ws.tick()) { |
| 256 |
chan[i].waveUpdated=true; |
| 257 |
} |
| 258 |
} |
| 259 |
if (chan[i].waveUpdated) { |
| 260 |
updateWaveCh(i); |
| 261 |
if (chan[i].active) { |
| 262 |
if (!chan[i].keyOff) chan[i].keyOn=true; |
| 263 |
} |
| 264 |
chan[i].waveUpdated=false; |
| 265 |
} |
| 266 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 267 |
// TODO: what is this mess? |
| 268 |
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,2,chan[i].pitch2,chipClock,CHIP_FREQBASE); |
| 269 |
if (lenCompensate) { |
| 270 |
chan[i].freq=(((chan[i].freq*chan[i].curWaveLen)*(chanMax+1))/256); |
| 271 |
} else { |
| 272 |
chan[i].freq*=(chanMax+1); |
| 273 |
chan[i].freq>>=3; |
| 274 |
} |
| 275 |
if (chan[i].freq<0) chan[i].freq=0; |
| 276 |
if (chan[i].freq>0x3ffff) chan[i].freq=0x3ffff; |
| 277 |
if (chan[i].keyOn) { |
| 278 |
} |
| 279 |
if (chan[i].keyOff && !isMuted[i]) { |
| 280 |
chWriteMask(i,0x7,0,0xf); |
| 281 |
} |
| 282 |
chWrite(i,0x0,chan[i].freq&0xff); |
| 283 |
chWrite(i,0x2,chan[i].freq>>8); |
| 284 |
chWrite(i,0x4,((256-chan[i].curWaveLen)&0xfc)|((chan[i].freq>>16)&3)); |
| 285 |
if (chan[i].keyOn) chan[i].keyOn=false; |
| 286 |
if (chan[i].keyOff) chan[i].keyOff=false; |
| 287 |
chan[i].freqChanged=false; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
// update memory composition positions |
| 292 |
for (int i=0; i<=chanMax; i++) { |
| 293 |
memCompo.entries[i].begin=chan[i].wavePos>>1; |
| 294 |
memCompo.entries[i].end=(chan[i].wavePos+chan[i].waveLen)>>1; |
| 295 |
memCompo.entries[i+8].begin=chan[i].curWavePos>>1; |
| 296 |
memCompo.entries[i+8].end=(chan[i].curWavePos+chan[i].curWaveLen)>>1; |
| 297 |
} |
| 298 |
|
| 299 |
// update register pool |
| 300 |
for (int i=0; i<128; i++) { |
| 301 |
regPool[i]=n163.reg(i); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
int DivPlatformN163::dispatch(DivCommand c) { |
| 306 |
switch (c.cmd) { |
| 307 |
case DIV_CMD_NOTE_ON: { |
| 308 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_N163); |
| 309 |
if (chan[c.chan].insChanged) { |
| 310 |
if (ins->n163.wave>=0) { |
| 311 |
chan[c.chan].wave=ins->n163.wave; |
| 312 |
} |
| 313 |
chan[c.chan].wavePos=ins->n163.perChanPos?ins->n163.wavePosCh[c.chan&7]:ins->n163.wavePos; |
| 314 |
chan[c.chan].waveLen=ins->n163.perChanPos?ins->n163.waveLenCh[c.chan&7]:ins->n163.waveLen; |
| 315 |
chan[c.chan].waveMode=ins->n163.waveMode; |
| 316 |
chan[c.chan].curWavePos=chan[c.chan].wavePos; |
| 317 |
chan[c.chan].curWaveLen=chan[c.chan].waveLen; |
| 318 |
chan[c.chan].ws.init(NULL,chan[c.chan].waveLen,15,true); |
| 319 |
if (chan[c.chan].wave<0) { |
| 320 |
chan[c.chan].wave=0; |
| 321 |
} |
| 322 |
chan[c.chan].ws.changeWave1(chan[c.chan].wave); |
| 323 |
chan[c.chan].waveChanged=true; |
| 324 |
if (chan[c.chan].waveMode) { |
| 325 |
chan[c.chan].waveUpdated=true; |
| 326 |
} |
| 327 |
} |
| 328 |
if (c.value!=DIV_NOTE_NULL) { |
| 329 |
chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value); |
| 330 |
chan[c.chan].freqChanged=true; |
| 331 |
chan[c.chan].note=c.value; |
| 332 |
} |
| 333 |
chan[c.chan].active=true; |
| 334 |
chan[c.chan].keyOn=true; |
| 335 |
chan[c.chan].resVol=chan[c.chan].vol; |
| 336 |
if (!isMuted[c.chan]) { |
| 337 |
chan[c.chan].volumeChanged=true; |
| 338 |
} |
| 339 |
chan[c.chan].macroInit(ins); |
| 340 |
chan[c.chan].ws.init(ins,chan[c.chan].waveLen,15,chan[c.chan].insChanged); |
| 341 |
chan[c.chan].insChanged=false; |
| 342 |
break; |
| 343 |
} |
| 344 |
case DIV_CMD_NOTE_OFF: |
| 345 |
chan[c.chan].active=false; |
| 346 |
chan[c.chan].keyOff=true; |
| 347 |
chan[c.chan].keyOn=false; |
| 348 |
chan[c.chan].macroInit(NULL); |
| 349 |
break; |
| 350 |
case DIV_CMD_NOTE_OFF_ENV: |
| 351 |
chan[c.chan].std.release(); |
| 352 |
break; |
| 353 |
case DIV_CMD_ENV_RELEASE: |
| 354 |
chan[c.chan].std.release(); |
| 355 |
break; |
| 356 |
case DIV_CMD_INSTRUMENT: |
| 357 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 358 |
chan[c.chan].insChanged=true; |
| 359 |
chan[c.chan].ins=c.value; |
| 360 |
} |
| 361 |
break; |
| 362 |
case DIV_CMD_VOLUME: |
| 363 |
if (chan[c.chan].vol!=c.value) { |
| 364 |
chan[c.chan].vol=c.value; |
| 365 |
if (!chan[c.chan].std.vol.has) { |
| 366 |
chan[c.chan].outVol=c.value; |
| 367 |
chan[c.chan].resVol=chan[c.chan].outVol; |
| 368 |
} else { |
| 369 |
chan[c.chan].resVol=chan[c.chan].vol; |
| 370 |
} |
| 371 |
if (!isMuted[c.chan]) { |
| 372 |
chan[c.chan].volumeChanged=true; |
| 373 |
} |
| 374 |
} |
| 375 |
break; |
| 376 |
case DIV_CMD_GET_VOLUME: |
| 377 |
return chan[c.chan].vol; |
| 378 |
break; |
| 379 |
case DIV_CMD_PITCH: |
| 380 |
chan[c.chan].pitch=c.value; |
| 381 |
chan[c.chan].freqChanged=true; |
| 382 |
break; |
| 383 |
case DIV_CMD_NOTE_PORTA: { |
| 384 |
double destFreqD=NOTE_FREQUENCY(c.value2); |
| 385 |
if (destFreqD>2000000000.0) destFreqD=2000000000.0; |
| 386 |
int destFreq=destFreqD; |
| 387 |
bool return2=false; |
| 388 |
if (destFreq>chan[c.chan].baseFreq) { |
| 389 |
chan[c.chan].baseFreq+=c.value*((parent->song.linearPitch==2)?1:16); |
| 390 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 391 |
chan[c.chan].baseFreq=destFreq; |
| 392 |
return2=true; |
| 393 |
} |
| 394 |
} else { |
| 395 |
chan[c.chan].baseFreq-=c.value*((parent->song.linearPitch==2)?1:16); |
| 396 |
if (chan[c.chan].baseFreq<=destFreq) { |
| 397 |
chan[c.chan].baseFreq=destFreq; |
| 398 |
return2=true; |
| 399 |
} |
| 400 |
} |
| 401 |
chan[c.chan].freqChanged=true; |
| 402 |
if (return2) { |
| 403 |
chan[c.chan].inPorta=false; |
| 404 |
return 2; |
| 405 |
} |
| 406 |
break; |
| 407 |
} |
| 408 |
case DIV_CMD_WAVE: |
| 409 |
chan[c.chan].wave=c.value; |
| 410 |
chan[c.chan].ws.changeWave1(chan[c.chan].wave); |
| 411 |
if (chan[c.chan].waveMode) { |
| 412 |
chan[c.chan].waveUpdated=true; |
| 413 |
} |
| 414 |
chan[c.chan].keyOn=true; |
| 415 |
break; |
| 416 |
case DIV_CMD_N163_WAVE_POSITION: |
| 417 |
chan[c.chan].curWavePos=c.value; |
| 418 |
chan[c.chan].waveChanged=true; |
| 419 |
break; |
| 420 |
case DIV_CMD_N163_WAVE_LENGTH: |
| 421 |
chan[c.chan].curWaveLen=c.value&0xfc; |
| 422 |
chan[c.chan].freqChanged=true; |
| 423 |
break; |
| 424 |
case DIV_CMD_N163_WAVE_LOADPOS: |
| 425 |
chan[c.chan].wavePos=c.value; |
| 426 |
if (chan[c.chan].waveMode) { |
| 427 |
chan[c.chan].waveUpdated=true; |
| 428 |
} |
| 429 |
break; |
| 430 |
case DIV_CMD_N163_WAVE_LOADLEN: |
| 431 |
chan[c.chan].waveLen=c.value&0xfc; |
| 432 |
if (chan[c.chan].waveMode) { |
| 433 |
chan[c.chan].waveUpdated=true; |
| 434 |
} |
| 435 |
break; |
| 436 |
case DIV_CMD_N163_GLOBAL_WAVE_LOAD: |
| 437 |
loadWave=c.value; |
| 438 |
if (loadWave>=0 && loadWave<parent->song.waveLen) { |
| 439 |
updateWave(-1,loadWave,loadPos,-1); |
| 440 |
} |
| 441 |
break; |
| 442 |
case DIV_CMD_N163_GLOBAL_WAVE_LOADPOS: |
| 443 |
loadPos=c.value; |
| 444 |
break; |
| 445 |
case DIV_CMD_N163_CHANNEL_LIMIT: |
| 446 |
if (chanMax!=(c.value&0x7)) { |
| 447 |
chanMax=c.value&0x7; |
| 448 |
rWriteMask(0x7f,chanMax<<4,0x70); |
| 449 |
forceIns(); |
| 450 |
} |
| 451 |
break; |
| 452 |
case DIV_CMD_LEGATO: |
| 453 |
chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0))); |
| 454 |
chan[c.chan].freqChanged=true; |
| 455 |
chan[c.chan].note=c.value; |
| 456 |
break; |
| 457 |
case DIV_CMD_PRE_PORTA: |
| 458 |
if (chan[c.chan].active && c.value2) { |
| 459 |
if (parent->song.resetMacroOnPorta) { |
| 460 |
chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_N163)); |
| 461 |
chan[c.chan].keyOn=true; |
| 462 |
} |
| 463 |
} |
| 464 |
if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_FREQUENCY(chan[c.chan].note); |
| 465 |
chan[c.chan].inPorta=c.value; |
| 466 |
break; |
| 467 |
case DIV_CMD_GET_VOLMAX: |
| 468 |
return 15; |
| 469 |
break; |
| 470 |
case DIV_CMD_MACRO_OFF: |
| 471 |
chan[c.chan].std.mask(c.value,true); |
| 472 |
break; |
| 473 |
case DIV_CMD_MACRO_ON: |
| 474 |
chan[c.chan].std.mask(c.value,false); |
| 475 |
break; |
| 476 |
case DIV_CMD_MACRO_RESTART: |
| 477 |
chan[c.chan].std.restart(c.value); |
| 478 |
break; |
| 479 |
default: |
| 480 |
break; |
| 481 |
} |
| 482 |
return 1; |
| 483 |
} |
| 484 |
|
| 485 |
void DivPlatformN163::muteChannel(int ch, bool mute) { |
| 486 |
isMuted[ch]=mute; |
| 487 |
chan[ch].volumeChanged=true; |
| 488 |
} |
| 489 |
|
| 490 |
void DivPlatformN163::forceIns() { |
| 491 |
for (int i=0; i<=chanMax; i++) { |
| 492 |
chan[i].insChanged=true; |
| 493 |
if (chan[i].active) { |
| 494 |
chan[i].keyOn=true; |
| 495 |
chan[i].freqChanged=true; |
| 496 |
chan[i].volumeChanged=true; |
| 497 |
chan[i].waveChanged=true; |
| 498 |
} |
| 499 |
} |
| 500 |
memCompo.entries[16].begin=120-chanMax*8; |
| 501 |
} |
| 502 |
|
| 503 |
void DivPlatformN163::notifyWaveChange(int wave) { |
| 504 |
for (int i=0; i<8; i++) { |
| 505 |
if (chan[i].wave==wave) { |
| 506 |
if (chan[i].waveMode) { |
| 507 |
chan[i].ws.changeWave1(wave); |
| 508 |
chan[i].waveUpdated=true; |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
void DivPlatformN163::notifyInsChange(int ins) { |
| 515 |
for (int i=0; i<8; i++) { |
| 516 |
if (chan[i].ins==ins) { |
| 517 |
chan[i].insChanged=true; |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
void DivPlatformN163::notifyInsDeletion(void* ins) { |
| 523 |
for (int i=0; i<8; i++) { |
| 524 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
void* DivPlatformN163::getChanState(int ch) { |
| 529 |
return &chan[ch]; |
| 530 |
} |
| 531 |
|
| 532 |
DivMacroInt* DivPlatformN163::getChanMacroInt(int ch) { |
| 533 |
return &chan[ch].std; |
| 534 |
} |
| 535 |
|
| 536 |
DivDispatchOscBuffer* DivPlatformN163::getOscBuffer(int ch) { |
| 537 |
return oscBuf[ch]; |
| 538 |
} |
| 539 |
|
| 540 |
unsigned char* DivPlatformN163::getRegisterPool() { |
| 541 |
return regPool; |
| 542 |
} |
| 543 |
|
| 544 |
int DivPlatformN163::getRegisterPoolSize() { |
| 545 |
return 128; |
| 546 |
} |
| 547 |
|
| 548 |
void DivPlatformN163::reset() { |
| 549 |
while (!writes.empty()) writes.pop(); |
| 550 |
for (int i=0; i<8; i++) { |
| 551 |
chan[i]=DivPlatformN163::Channel(); |
| 552 |
chan[i].std.setEngine(parent); |
| 553 |
chan[i].ws.setEngine(parent); |
| 554 |
chan[i].ws.init(NULL,32,15,false); |
| 555 |
} |
| 556 |
|
| 557 |
n163.reset(); |
| 558 |
memset(regPool,0,128); |
| 559 |
|
| 560 |
n163.set_disable(false); |
| 561 |
n163.set_multiplex(multiplex); |
| 562 |
chanMax=initChanMax; |
| 563 |
loadWave=-1; |
| 564 |
loadPos=0; |
| 565 |
rWrite(0x7f,initChanMax<<4); |
| 566 |
|
| 567 |
memCompo.entries[16].begin=120-chanMax*8; |
| 568 |
} |
| 569 |
|
| 570 |
void DivPlatformN163::poke(unsigned int addr, unsigned short val) { |
| 571 |
rWrite(addr,val); |
| 572 |
} |
| 573 |
|
| 574 |
void DivPlatformN163::poke(std::vector<DivRegWrite>& wlist) { |
| 575 |
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); |
| 576 |
} |
| 577 |
|
| 578 |
const DivMemoryComposition* DivPlatformN163::getMemCompo(int index) { |
| 579 |
if (index!=0) return NULL; |
| 580 |
return &memCompo; |
| 581 |
} |
| 582 |
|
| 583 |
void DivPlatformN163::setFlags(const DivConfig& flags) { |
| 584 |
switch (flags.getInt("clockSel",0)) { |
| 585 |
case 1: // PAL |
| 586 |
chipClock=COLOR_PAL*3.0/8.0; |
| 587 |
break; |
| 588 |
case 2: // Dendy |
| 589 |
chipClock=COLOR_PAL*2.0/5.0; |
| 590 |
break; |
| 591 |
default: // NTSC |
| 592 |
chipClock=COLOR_NTSC/2.0; |
| 593 |
break; |
| 594 |
} |
| 595 |
CHECK_CUSTOM_CLOCK; |
| 596 |
initChanMax=chanMax=flags.getInt("channels",0)&7; |
| 597 |
multiplex=!flags.getBool("multiplex",false); // not accurate in real hardware |
| 598 |
rate=chipClock; |
| 599 |
rate/=15; |
| 600 |
n163.set_multiplex(multiplex); |
| 601 |
rWrite(0x7f,initChanMax<<4); |
| 602 |
for (int i=0; i<8; i++) { |
| 603 |
oscBuf[i]->setRate(rate);//=rate/(initChanMax+1); |
| 604 |
} |
| 605 |
|
| 606 |
lenCompensate=flags.getBool("lenCompensate",false); |
| 607 |
|
| 608 |
// needed to make sure changing channel count won't trigger glitches |
| 609 |
reset(); |
| 610 |
} |
| 611 |
|
| 612 |
int DivPlatformN163::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 613 |
parent=p; |
| 614 |
dumpWrites=false; |
| 615 |
skipRegisterWrites=false; |
| 616 |
for (int i=0; i<8; i++) { |
| 617 |
isMuted[i]=false; |
| 618 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 619 |
} |
| 620 |
|
| 621 |
memCompo.used=0; |
| 622 |
memCompo.capacity=128; |
| 623 |
memCompo.memory=regPool; |
| 624 |
memCompo.waveformView=DIV_MEMORY_WAVE_4BIT; |
| 625 |
|
| 626 |
for (int i=0; i<8; i++) { |
| 627 |
memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_N163_LOAD,fmt::sprintf("Channel %d (load)",i),-1,0,0)); |
| 628 |
} |
| 629 |
for (int i=0; i<8; i++) { |
| 630 |
memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_N163_PLAY,fmt::sprintf("Channel %d (play)",i),-1,0,0)); |
| 631 |
} |
| 632 |
memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_RESERVED,"Registers",-1,127,128)); |
| 633 |
|
| 634 |
setFlags(flags); |
| 635 |
|
| 636 |
reset(); |
| 637 |
|
| 638 |
return 8; |
| 639 |
} |
| 640 |
|
| 641 |
void DivPlatformN163::quit() { |
| 642 |
for (int i=0; i<8; i++) { |
| 643 |
delete oscBuf[i]; |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
DivPlatformN163::~DivPlatformN163() { |
| 648 |
} |