2022-02-15 03:12:20 +00:00
|
|
|
/**
|
|
|
|
* Furnace Tracker - multi-system chiptune tracker
|
|
|
|
* Copyright (C) 2021-2022 tildearrow and contributors
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2021-12-04 06:19:54 +00:00
|
|
|
#include "nes.h"
|
|
|
|
#include "sound/nes/cpu_inline.h"
|
|
|
|
#include "../engine.h"
|
2022-05-02 07:54:23 +00:00
|
|
|
#include "../../ta-log.h"
|
|
|
|
#include <stddef.h>
|
2021-12-04 06:19:54 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2022-04-06 09:24:20 +00:00
|
|
|
struct _nla_table nla_table;
|
|
|
|
|
2022-01-28 05:55:51 +00:00
|
|
|
#define CHIP_DIVIDER 16
|
2021-12-04 06:19:54 +00:00
|
|
|
|
2022-05-02 04:20:20 +00:00
|
|
|
#define rWrite(a,v) if (!skipRegisterWrites) {doWrite(a,v); regPool[(a)&0x7f]=v; if (dumpWrites) {addWrite(a,v);} }
|
2021-12-21 06:29:07 +00:00
|
|
|
|
2022-02-02 07:14:42 +00:00
|
|
|
const char* regCheatSheetNES[]={
|
|
|
|
"S0Volume", "4000",
|
|
|
|
"S0Sweep", "4001",
|
|
|
|
"S0PeriodL", "4002",
|
|
|
|
"S0PeriodH", "4003",
|
|
|
|
"S1Volume", "4004",
|
|
|
|
"S1Sweep", "4005",
|
|
|
|
"S1PeriodL", "4006",
|
|
|
|
"S1PeriodH", "4007",
|
|
|
|
"TRVolume", "4008",
|
|
|
|
"TRPeriodL", "400A",
|
|
|
|
"TRPeriodH", "400B",
|
|
|
|
"NSVolume", "400C",
|
|
|
|
"NSPeriod", "400E",
|
|
|
|
"NSLength", "400F",
|
|
|
|
"DMCControl", "4010",
|
|
|
|
"DMCLoad", "4011",
|
|
|
|
"DMCAddr", "4012",
|
|
|
|
"DMCLength", "4013",
|
|
|
|
"APUControl", "4015",
|
|
|
|
"APUFrameCtl", "4017",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2022-05-02 07:12:14 +00:00
|
|
|
unsigned char _readDMC(void* user, unsigned short addr) {
|
|
|
|
return ((DivPlatformNES*)user)->readDMC(addr);
|
|
|
|
}
|
|
|
|
|
2022-02-03 23:38:57 +00:00
|
|
|
const char** DivPlatformNES::getRegisterSheet() {
|
|
|
|
return regCheatSheetNES;
|
|
|
|
}
|
|
|
|
|
2022-05-02 04:20:20 +00:00
|
|
|
void DivPlatformNES::doWrite(unsigned short addr, unsigned char data) {
|
|
|
|
if (useNP) {
|
|
|
|
nes1_NP->Write(addr,data);
|
|
|
|
nes2_NP->Write(addr,data);
|
|
|
|
} else {
|
|
|
|
apu_wr_reg(nes,addr,data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define doPCM \
|
2022-05-02 07:54:23 +00:00
|
|
|
if (!dpcmMode && dacSample!=-1) { \
|
2022-05-02 04:20:20 +00:00
|
|
|
dacPeriod+=dacRate; \
|
|
|
|
if (dacPeriod>=rate) { \
|
|
|
|
DivSample* s=parent->getSample(dacSample); \
|
2022-08-28 00:59:56 +00:00
|
|
|
if (s->samples>0) { \
|
2022-05-02 04:20:20 +00:00
|
|
|
if (!isMuted[4]) { \
|
|
|
|
unsigned char next=((unsigned char)s->data8[dacPos]+0x80)>>1; \
|
|
|
|
if (dacAntiClickOn && dacAntiClick<next) { \
|
|
|
|
dacAntiClick+=8; \
|
|
|
|
rWrite(0x4011,dacAntiClick); \
|
|
|
|
} else { \
|
|
|
|
dacAntiClickOn=false; \
|
|
|
|
rWrite(0x4011,next); \
|
|
|
|
} \
|
|
|
|
} \
|
2022-07-22 04:36:42 +00:00
|
|
|
dacPos++; \
|
2022-08-28 01:50:57 +00:00
|
|
|
if (s->isLoopable() && dacPos>=(unsigned int)s->loopEnd) { \
|
|
|
|
dacPos=s->loopStart; \
|
|
|
|
} else if (dacPos>=s->samples) { \
|
2022-07-22 04:36:42 +00:00
|
|
|
dacSample=-1; \
|
2022-05-02 04:20:20 +00:00
|
|
|
} \
|
|
|
|
dacPeriod-=rate; \
|
|
|
|
} else { \
|
|
|
|
dacSample=-1; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2023-01-03 06:09:46 +00:00
|
|
|
void DivPlatformNES::acquire_puNES(short** buf, size_t len) {
|
|
|
|
for (size_t i=0; i<len; i++) {
|
2022-05-02 04:20:20 +00:00
|
|
|
doPCM;
|
2021-12-06 10:21:42 +00:00
|
|
|
|
2022-01-08 22:44:17 +00:00
|
|
|
apu_tick(nes,NULL);
|
|
|
|
nes->apu.odd_cycle=!nes->apu.odd_cycle;
|
|
|
|
if (nes->apu.clocked) {
|
|
|
|
nes->apu.clocked=false;
|
2021-12-06 10:21:42 +00:00
|
|
|
}
|
2022-04-28 01:36:32 +00:00
|
|
|
int sample=(pulse_output(nes)+tnd_output(nes))<<6;
|
2022-02-24 22:56:19 +00:00
|
|
|
if (sample>32767) sample=32767;
|
|
|
|
if (sample<-32768) sample=-32768;
|
2023-01-02 09:53:37 +00:00
|
|
|
buf[0][i]=sample;
|
2022-04-30 19:36:12 +00:00
|
|
|
if (++writeOscBuf>=32) {
|
|
|
|
writeOscBuf=0;
|
2022-08-30 20:47:12 +00:00
|
|
|
oscBuf[0]->data[oscBuf[0]->needle++]=isMuted[0]?0:(nes->S1.output<<11);
|
|
|
|
oscBuf[1]->data[oscBuf[1]->needle++]=isMuted[1]?0:(nes->S2.output<<11);
|
|
|
|
oscBuf[2]->data[oscBuf[2]->needle++]=isMuted[2]?0:(nes->TR.output<<11);
|
|
|
|
oscBuf[3]->data[oscBuf[3]->needle++]=isMuted[3]?0:(nes->NS.output<<11);
|
|
|
|
oscBuf[4]->data[oscBuf[4]->needle++]=isMuted[4]?0:(nes->DMC.output<<8);
|
2022-04-30 19:36:12 +00:00
|
|
|
}
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 06:09:46 +00:00
|
|
|
void DivPlatformNES::acquire_NSFPlay(short** buf, size_t len) {
|
2022-05-02 04:20:20 +00:00
|
|
|
int out1[2];
|
|
|
|
int out2[2];
|
2023-01-03 06:09:46 +00:00
|
|
|
for (size_t i=0; i<len; i++) {
|
2022-05-02 04:20:20 +00:00
|
|
|
doPCM;
|
|
|
|
|
|
|
|
nes1_NP->Tick(1);
|
2022-05-02 04:48:02 +00:00
|
|
|
nes2_NP->TickFrameSequence(1);
|
2022-05-02 04:20:20 +00:00
|
|
|
nes2_NP->Tick(1);
|
|
|
|
nes1_NP->Render(out1);
|
|
|
|
nes2_NP->Render(out2);
|
|
|
|
|
2022-05-02 04:57:53 +00:00
|
|
|
int sample=(out1[0]+out1[1]+out2[0]+out2[1])<<1;
|
2022-05-02 04:20:20 +00:00
|
|
|
if (sample>32767) sample=32767;
|
|
|
|
if (sample<-32768) sample=-32768;
|
2023-01-02 09:53:37 +00:00
|
|
|
buf[0][i]=sample;
|
2022-05-02 05:05:28 +00:00
|
|
|
if (++writeOscBuf>=32) {
|
|
|
|
writeOscBuf=0;
|
|
|
|
oscBuf[0]->data[oscBuf[0]->needle++]=nes1_NP->out[0]<<11;
|
|
|
|
oscBuf[1]->data[oscBuf[1]->needle++]=nes1_NP->out[1]<<11;
|
|
|
|
oscBuf[2]->data[oscBuf[2]->needle++]=nes2_NP->out[0]<<11;
|
|
|
|
oscBuf[3]->data[oscBuf[3]->needle++]=nes2_NP->out[1]<<11;
|
|
|
|
oscBuf[4]->data[oscBuf[4]->needle++]=nes2_NP->out[2]<<8;
|
|
|
|
}
|
2022-05-02 04:20:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 09:53:37 +00:00
|
|
|
void DivPlatformNES::acquire(short** buf, size_t len) {
|
2022-05-02 04:20:20 +00:00
|
|
|
if (useNP) {
|
2023-01-03 06:09:46 +00:00
|
|
|
acquire_NSFPlay(buf,len);
|
2022-05-02 04:20:20 +00:00
|
|
|
} else {
|
2023-01-03 06:09:46 +00:00
|
|
|
acquire_puNES(buf,len);
|
2022-05-02 04:20:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 06:56:50 +00:00
|
|
|
static unsigned char noiseTable[253]={
|
2021-12-04 07:19:14 +00:00
|
|
|
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 4,
|
2021-12-04 06:19:54 +00:00
|
|
|
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4,
|
|
|
|
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4,
|
2021-12-04 07:19:14 +00:00
|
|
|
3, 2, 1, 0, 11, 10, 9, 8, 7, 6, 5, 4,
|
2021-12-04 06:19:54 +00:00
|
|
|
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4,
|
2021-12-04 07:19:14 +00:00
|
|
|
3, 2, 1, 0, 11, 10, 9, 8, 7, 6, 5, 4,
|
2021-12-04 06:19:54 +00:00
|
|
|
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4,
|
2021-12-04 07:19:14 +00:00
|
|
|
3, 2, 1, 0, 11, 10, 9, 8, 7, 6, 5, 4,
|
2021-12-04 06:19:54 +00:00
|
|
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
|
|
15
|
|
|
|
};
|
|
|
|
|
2022-05-02 08:42:40 +00:00
|
|
|
unsigned char DivPlatformNES::calcDPCMRate(int inRate) {
|
|
|
|
if (inRate<4450) return 0;
|
|
|
|
if (inRate<5000) return 1;
|
|
|
|
if (inRate<5400) return 2;
|
|
|
|
if (inRate<5900) return 3;
|
|
|
|
if (inRate<6650) return 4;
|
|
|
|
if (inRate<7450) return 5;
|
|
|
|
if (inRate<8100) return 6;
|
|
|
|
if (inRate<8800) return 7;
|
|
|
|
if (inRate<10200) return 8;
|
|
|
|
if (inRate<11700) return 9;
|
|
|
|
if (inRate<13300) return 10;
|
|
|
|
if (inRate<15900) return 11;
|
|
|
|
if (inRate<18900) return 12;
|
|
|
|
if (inRate<23500) return 13;
|
|
|
|
if (inRate<29000) return 14;
|
|
|
|
return 15;
|
|
|
|
}
|
|
|
|
|
2022-04-15 20:01:11 +00:00
|
|
|
void DivPlatformNES::tick(bool sysTick) {
|
2021-12-04 06:19:54 +00:00
|
|
|
for (int i=0; i<4; i++) {
|
|
|
|
chan[i].std.next();
|
2022-04-10 05:01:55 +00:00
|
|
|
if (chan[i].std.vol.had) {
|
2021-12-04 07:19:14 +00:00
|
|
|
// ok, why are the volumes like that?
|
2022-06-01 23:27:06 +00:00
|
|
|
chan[i].outVol=VOL_SCALE_LINEAR_BROKEN(chan[i].vol&15,MIN(15,chan[i].std.vol.val),15);
|
2021-12-04 07:19:14 +00:00
|
|
|
if (chan[i].outVol<0) chan[i].outVol=0;
|
2021-12-04 06:19:54 +00:00
|
|
|
if (i==2) { // triangle
|
2021-12-21 06:29:07 +00:00
|
|
|
rWrite(0x4000+i*4,(chan[i].outVol==0)?0:255);
|
2021-12-04 07:19:14 +00:00
|
|
|
chan[i].freqChanged=true;
|
2021-12-04 06:19:54 +00:00
|
|
|
} else {
|
2022-10-25 05:43:03 +00:00
|
|
|
rWrite(0x4000+i*4,(chan[i].envMode<<4)|chan[i].outVol|((chan[i].duty&3)<<6));
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-29 04:11:16 +00:00
|
|
|
if (NEW_ARP_STRAT) {
|
2022-12-17 06:21:08 +00:00
|
|
|
chan[i].handleArp();
|
|
|
|
} else if (chan[i].std.arp.had) {
|
2021-12-04 06:19:54 +00:00
|
|
|
if (i==3) { // noise
|
2022-08-22 20:59:45 +00:00
|
|
|
chan[i].baseFreq=parent->calcArp(chan[i].note,chan[i].std.arp.val);
|
2021-12-04 06:19:54 +00:00
|
|
|
if (chan[i].baseFreq>255) chan[i].baseFreq=255;
|
|
|
|
if (chan[i].baseFreq<0) chan[i].baseFreq=0;
|
|
|
|
} else {
|
|
|
|
if (!chan[i].inPorta) {
|
2022-08-22 20:59:45 +00:00
|
|
|
chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val));
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
chan[i].freqChanged=true;
|
|
|
|
}
|
2022-04-10 05:01:55 +00:00
|
|
|
if (chan[i].std.duty.had) {
|
|
|
|
chan[i].duty=chan[i].std.duty.val;
|
2022-02-07 05:49:01 +00:00
|
|
|
if (i==3) {
|
|
|
|
if (parent->song.properNoiseLayout) {
|
|
|
|
chan[i].duty&=1;
|
|
|
|
} else if (chan[i].duty>1) {
|
|
|
|
chan[i].duty=1;
|
|
|
|
}
|
|
|
|
}
|
2021-12-04 06:19:54 +00:00
|
|
|
if (i!=2) {
|
2022-10-25 05:43:03 +00:00
|
|
|
rWrite(0x4000+i*4,(chan[i].envMode<<4)|chan[i].outVol|((chan[i].duty&3)<<6));
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
2021-12-04 07:19:14 +00:00
|
|
|
if (i==3) { // noise
|
|
|
|
chan[i].freqChanged=true;
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-16 06:39:40 +00:00
|
|
|
if (chan[i].std.pitch.had) {
|
2022-04-28 06:31:16 +00:00
|
|
|
if (chan[i].std.pitch.mode) {
|
|
|
|
chan[i].pitch2+=chan[i].std.pitch.val;
|
2022-05-23 03:47:40 +00:00
|
|
|
CLAMP_VAR(chan[i].pitch2,-32768,32767);
|
2022-04-28 06:31:16 +00:00
|
|
|
} else {
|
|
|
|
chan[i].pitch2=chan[i].std.pitch.val;
|
|
|
|
}
|
2022-04-16 06:39:40 +00:00
|
|
|
chan[i].freqChanged=true;
|
|
|
|
}
|
2021-12-04 06:19:54 +00:00
|
|
|
if (chan[i].sweepChanged) {
|
|
|
|
chan[i].sweepChanged=false;
|
|
|
|
if (i==0) {
|
|
|
|
//rWrite(16+i*5,chan[i].sweep);
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 08:16:14 +00:00
|
|
|
if (i<2) if (chan[i].std.phaseReset.had) {
|
|
|
|
if (chan[i].std.phaseReset.val==1) {
|
|
|
|
chan[i].freqChanged=true;
|
|
|
|
chan[i].prevFreq=-1;
|
|
|
|
}
|
|
|
|
}
|
2021-12-04 06:19:54 +00:00
|
|
|
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
|
|
|
|
if (i==3) { // noise
|
2022-02-06 06:56:50 +00:00
|
|
|
int ntPos=chan[i].baseFreq;
|
2022-12-29 04:04:12 +00:00
|
|
|
if (NEW_ARP_STRAT) {
|
|
|
|
if (chan[i].fixedArp) {
|
|
|
|
ntPos=chan[i].baseNoteOverride;
|
|
|
|
} else {
|
|
|
|
ntPos+=chan[i].arpOff;
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 23:36:13 +00:00
|
|
|
ntPos+=chan[i].pitch2;
|
2022-12-29 04:04:12 +00:00
|
|
|
if (parent->song.properNoiseLayout) {
|
2022-12-29 04:57:06 +00:00
|
|
|
chan[i].freq=15-(ntPos&15);
|
|
|
|
} else {
|
2022-12-29 04:04:12 +00:00
|
|
|
if (ntPos<0) ntPos=0;
|
|
|
|
if (ntPos>252) ntPos=252;
|
|
|
|
chan[i].freq=noiseTable[ntPos];
|
|
|
|
}
|
2021-12-04 06:19:54 +00:00
|
|
|
} else {
|
2022-12-17 07:07:24 +00:00
|
|
|
chan[i].freq=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)-1;
|
2021-12-04 06:19:54 +00:00
|
|
|
if (chan[i].freq>2047) chan[i].freq=2047;
|
2022-03-14 05:26:06 +00:00
|
|
|
if (chan[i].freq<0) chan[i].freq=0;
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
|
|
|
if (chan[i].keyOn) {
|
|
|
|
}
|
|
|
|
if (chan[i].keyOff) {
|
|
|
|
//rWrite(16+i*5+2,8);
|
|
|
|
if (i==2) { // triangle
|
2021-12-21 06:29:07 +00:00
|
|
|
rWrite(0x4000+i*4,0x00);
|
2021-12-04 06:19:54 +00:00
|
|
|
} else {
|
2021-12-21 06:29:07 +00:00
|
|
|
rWrite(0x4000+i*4,0x30);
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i==3) { // noise
|
2021-12-21 06:29:07 +00:00
|
|
|
rWrite(0x4002+i*4,(chan[i].duty<<7)|chan[i].freq);
|
2022-10-25 05:43:03 +00:00
|
|
|
rWrite(0x4003+i*4,(chan[i].len<<3));
|
2021-12-04 06:19:54 +00:00
|
|
|
} else {
|
2021-12-21 06:29:07 +00:00
|
|
|
rWrite(0x4002+i*4,chan[i].freq&0xff);
|
2021-12-04 06:19:54 +00:00
|
|
|
if ((chan[i].prevFreq>>8)!=(chan[i].freq>>8) || i==2) {
|
2022-10-25 05:43:03 +00:00
|
|
|
rWrite(0x4003+i*4,(chan[i].len<<3)|(chan[i].freq>>8));
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
2021-12-08 06:31:35 +00:00
|
|
|
if (chan[i].freq!=65535 && chan[i].freq!=0) {
|
|
|
|
chan[i].prevFreq=chan[i].freq;
|
|
|
|
}
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
|
|
|
if (chan[i].keyOn) chan[i].keyOn=false;
|
|
|
|
if (chan[i].keyOff) chan[i].keyOff=false;
|
|
|
|
chan[i].freqChanged=false;
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 21:09:05 +00:00
|
|
|
|
|
|
|
// PCM
|
2022-05-02 21:05:07 +00:00
|
|
|
if (chan[4].freqChanged || chan[4].keyOn) {
|
2022-12-17 07:07:24 +00:00
|
|
|
chan[4].freq=parent->calcFreq(chan[4].baseFreq,chan[4].pitch,chan[4].fixedArp?chan[4].baseNoteOverride:chan[4].arpOff,chan[4].fixedArp,false);
|
2022-01-20 21:09:05 +00:00
|
|
|
if (chan[4].furnaceDac) {
|
2022-02-04 08:29:40 +00:00
|
|
|
double off=1.0;
|
|
|
|
if (dacSample>=0 && dacSample<parent->song.sampleLen) {
|
2022-02-25 03:52:20 +00:00
|
|
|
DivSample* s=parent->getSample(dacSample);
|
2022-02-04 08:29:40 +00:00
|
|
|
off=(double)s->centerRate/8363.0;
|
|
|
|
}
|
|
|
|
dacRate=MIN(chan[4].freq*off,32000);
|
2022-05-02 21:05:07 +00:00
|
|
|
if (chan[4].keyOn) {
|
|
|
|
if (dpcmMode && !skipRegisterWrites && dacSample>=0 && dacSample<parent->song.sampleLen) {
|
2022-09-26 01:07:21 +00:00
|
|
|
unsigned int dpcmAddr=sampleOffDPCM[dacSample];
|
2022-05-02 21:05:07 +00:00
|
|
|
unsigned int dpcmLen=(parent->getSample(dacSample)->lengthDPCM+15)>>4;
|
|
|
|
if (dpcmLen>255) dpcmLen=255;
|
2022-09-22 23:43:48 +00:00
|
|
|
goingToLoop=parent->getSample(dacSample)->isLoopable();
|
2022-05-02 21:05:07 +00:00
|
|
|
// write DPCM
|
|
|
|
rWrite(0x4015,15);
|
2022-09-22 23:43:48 +00:00
|
|
|
rWrite(0x4010,calcDPCMRate(dacRate)|(goingToLoop?0x40:0));
|
2022-05-02 21:05:07 +00:00
|
|
|
rWrite(0x4012,(dpcmAddr>>6)&0xff);
|
|
|
|
rWrite(0x4013,dpcmLen&0xff);
|
|
|
|
rWrite(0x4015,31);
|
|
|
|
dpcmBank=dpcmAddr>>14;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (dpcmMode) {
|
2022-09-22 23:43:48 +00:00
|
|
|
rWrite(0x4010,calcDPCMRate(dacRate)|(goingToLoop?0x40:0));
|
2022-05-02 21:05:07 +00:00
|
|
|
}
|
2022-05-02 08:42:40 +00:00
|
|
|
}
|
2022-05-02 21:05:07 +00:00
|
|
|
if (dumpWrites && !dpcmMode) addWrite(0xffff0001,dacRate);
|
2022-01-20 21:09:05 +00:00
|
|
|
}
|
2022-05-02 21:05:07 +00:00
|
|
|
if (chan[4].keyOn) chan[4].keyOn=false;
|
2022-01-20 21:09:05 +00:00
|
|
|
chan[4].freqChanged=false;
|
|
|
|
}
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DivPlatformNES::dispatch(DivCommand c) {
|
|
|
|
switch (c.cmd) {
|
|
|
|
case DIV_CMD_NOTE_ON:
|
|
|
|
if (c.chan==4) { // PCM
|
2022-04-21 07:24:06 +00:00
|
|
|
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_STD);
|
2022-01-20 21:09:05 +00:00
|
|
|
if (ins->type==DIV_INS_AMIGA) {
|
2022-12-29 08:12:00 +00:00
|
|
|
if (c.value!=DIV_NOTE_NULL) dacSample=ins->amiga.getSample(c.value);
|
2022-01-20 21:09:05 +00:00
|
|
|
if (dacSample<0 || dacSample>=parent->song.sampleLen) {
|
|
|
|
dacSample=-1;
|
2022-05-02 07:54:23 +00:00
|
|
|
if (dumpWrites && !dpcmMode) addWrite(0xffff0002,0);
|
2022-01-20 21:09:05 +00:00
|
|
|
break;
|
2022-01-24 22:48:58 +00:00
|
|
|
} else {
|
2022-05-02 07:54:23 +00:00
|
|
|
if (dumpWrites && !dpcmMode) addWrite(0xffff0000,dacSample);
|
2022-01-20 21:09:05 +00:00
|
|
|
}
|
|
|
|
dacPos=0;
|
|
|
|
dacPeriod=0;
|
|
|
|
if (c.value!=DIV_NOTE_NULL) {
|
2022-05-02 21:05:07 +00:00
|
|
|
chan[c.chan].baseFreq=parent->calcBaseFreq(1,1,c.value,false);
|
2022-01-20 21:09:05 +00:00
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
chan[c.chan].note=c.value;
|
|
|
|
}
|
|
|
|
chan[c.chan].active=true;
|
|
|
|
chan[c.chan].keyOn=true;
|
|
|
|
chan[c.chan].furnaceDac=true;
|
|
|
|
} else {
|
2022-02-03 20:43:41 +00:00
|
|
|
if (c.value!=DIV_NOTE_NULL) {
|
|
|
|
chan[c.chan].note=c.value;
|
|
|
|
}
|
|
|
|
dacSample=12*sampleBank+chan[c.chan].note%12;
|
2022-01-20 21:09:05 +00:00
|
|
|
if (dacSample>=parent->song.sampleLen) {
|
|
|
|
dacSample=-1;
|
2022-05-02 07:54:23 +00:00
|
|
|
if (dumpWrites && !dpcmMode) addWrite(0xffff0002,0);
|
2022-01-20 21:09:05 +00:00
|
|
|
break;
|
2022-01-24 22:48:58 +00:00
|
|
|
} else {
|
2022-05-02 07:54:23 +00:00
|
|
|
if (dumpWrites && !dpcmMode) addWrite(0xffff0000,dacSample);
|
2022-01-20 21:09:05 +00:00
|
|
|
}
|
|
|
|
dacPos=0;
|
|
|
|
dacPeriod=0;
|
2022-02-25 03:52:20 +00:00
|
|
|
dacRate=parent->getSample(dacSample)->rate;
|
2022-05-02 07:54:23 +00:00
|
|
|
if (dumpWrites && !dpcmMode) addWrite(0xffff0001,dacRate);
|
2022-01-20 21:09:05 +00:00
|
|
|
chan[c.chan].furnaceDac=false;
|
2022-05-02 07:54:23 +00:00
|
|
|
if (dpcmMode && !skipRegisterWrites) {
|
2022-09-26 01:07:21 +00:00
|
|
|
unsigned int dpcmAddr=sampleOffDPCM[dacSample];
|
2022-05-02 07:54:23 +00:00
|
|
|
unsigned int dpcmLen=(parent->getSample(dacSample)->lengthDPCM+15)>>4;
|
|
|
|
if (dpcmLen>255) dpcmLen=255;
|
2022-09-22 23:43:48 +00:00
|
|
|
goingToLoop=parent->getSample(dacSample)->isLoopable();
|
2022-05-02 07:54:23 +00:00
|
|
|
// write DPCM
|
|
|
|
rWrite(0x4015,15);
|
2022-09-22 23:43:48 +00:00
|
|
|
rWrite(0x4010,calcDPCMRate(dacRate)|(goingToLoop?0x40:0));
|
2022-05-02 07:54:23 +00:00
|
|
|
rWrite(0x4012,(dpcmAddr>>6)&0xff);
|
|
|
|
rWrite(0x4013,dpcmLen&0xff);
|
|
|
|
rWrite(0x4015,31);
|
|
|
|
dpcmBank=dpcmAddr>>14;
|
|
|
|
}
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else if (c.chan==3) { // noise
|
2022-01-19 05:01:34 +00:00
|
|
|
if (c.value!=DIV_NOTE_NULL) {
|
|
|
|
chan[c.chan].baseFreq=c.value;
|
|
|
|
}
|
2021-12-04 06:19:54 +00:00
|
|
|
} else {
|
2022-01-19 05:01:34 +00:00
|
|
|
if (c.value!=DIV_NOTE_NULL) {
|
2022-01-28 05:55:51 +00:00
|
|
|
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
|
2022-01-19 05:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c.value!=DIV_NOTE_NULL) {
|
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
chan[c.chan].note=c.value;
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
|
|
|
chan[c.chan].active=true;
|
|
|
|
chan[c.chan].keyOn=true;
|
2022-04-28 06:31:16 +00:00
|
|
|
chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_STD));
|
2022-06-01 00:03:26 +00:00
|
|
|
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
|
|
|
|
chan[c.chan].outVol=chan[c.chan].vol;
|
2022-10-08 05:53:01 +00:00
|
|
|
}
|
2022-10-15 09:17:42 +00:00
|
|
|
if (c.chan==2) {
|
|
|
|
rWrite(0x4000+c.chan*4,0xff);
|
|
|
|
} else if (!parent->song.brokenOutVol2) {
|
2022-10-25 05:43:03 +00:00
|
|
|
rWrite(0x4000+c.chan*4,(chan[c.chan].envMode<<4)|chan[c.chan].vol|((chan[c.chan].duty&3)<<6));
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIV_CMD_NOTE_OFF:
|
2022-01-24 22:48:58 +00:00
|
|
|
if (c.chan==4) {
|
|
|
|
dacSample=-1;
|
|
|
|
if (dumpWrites) addWrite(0xffff0002,0);
|
2022-05-02 08:42:40 +00:00
|
|
|
if (dpcmMode && !skipRegisterWrites) rWrite(0x4015,15);
|
2022-01-24 22:48:58 +00:00
|
|
|
}
|
2021-12-04 06:19:54 +00:00
|
|
|
chan[c.chan].active=false;
|
|
|
|
chan[c.chan].keyOff=true;
|
2022-04-28 06:31:16 +00:00
|
|
|
chan[c.chan].macroInit(NULL);
|
2021-12-04 06:19:54 +00:00
|
|
|
break;
|
2022-02-08 18:11:04 +00:00
|
|
|
case DIV_CMD_NOTE_OFF_ENV:
|
|
|
|
case DIV_CMD_ENV_RELEASE:
|
|
|
|
chan[c.chan].std.release();
|
|
|
|
break;
|
2021-12-04 06:19:54 +00:00
|
|
|
case DIV_CMD_INSTRUMENT:
|
2022-01-17 23:01:40 +00:00
|
|
|
if (chan[c.chan].ins!=c.value || c.value2==1) {
|
2021-12-04 06:19:54 +00:00
|
|
|
chan[c.chan].ins=c.value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIV_CMD_VOLUME:
|
|
|
|
if (chan[c.chan].vol!=c.value) {
|
|
|
|
chan[c.chan].vol=c.value;
|
2022-04-10 05:01:55 +00:00
|
|
|
if (!chan[c.chan].std.vol.has) {
|
2021-12-04 06:19:54 +00:00
|
|
|
chan[c.chan].outVol=c.value;
|
|
|
|
}
|
2021-12-29 05:52:20 +00:00
|
|
|
if (chan[c.chan].active) {
|
|
|
|
if (c.chan==2) {
|
|
|
|
rWrite(0x4000+c.chan*4,0xff);
|
|
|
|
} else {
|
2022-10-25 05:43:03 +00:00
|
|
|
rWrite(0x4000+c.chan*4,(chan[c.chan].envMode<<4)|chan[c.chan].vol|((chan[c.chan].duty&3)<<6));
|
2021-12-29 05:52:20 +00:00
|
|
|
}
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIV_CMD_GET_VOLUME:
|
|
|
|
return chan[c.chan].vol;
|
|
|
|
break;
|
|
|
|
case DIV_CMD_PITCH:
|
|
|
|
chan[c.chan].pitch=c.value;
|
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
break;
|
|
|
|
case DIV_CMD_NOTE_PORTA: {
|
2022-05-02 21:05:07 +00:00
|
|
|
int destFreq=(c.chan==4)?(parent->calcBaseFreq(1,1,c.value2,false)):(NOTE_PERIODIC(c.value2));
|
2021-12-04 06:19:54 +00:00
|
|
|
bool return2=false;
|
|
|
|
if (destFreq>chan[c.chan].baseFreq) {
|
|
|
|
chan[c.chan].baseFreq+=c.value;
|
|
|
|
if (chan[c.chan].baseFreq>=destFreq) {
|
|
|
|
chan[c.chan].baseFreq=destFreq;
|
|
|
|
return2=true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chan[c.chan].baseFreq-=c.value;
|
|
|
|
if (chan[c.chan].baseFreq<=destFreq) {
|
|
|
|
chan[c.chan].baseFreq=destFreq;
|
|
|
|
return2=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
if (return2) {
|
|
|
|
chan[c.chan].inPorta=false;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIV_CMD_STD_NOISE_MODE:
|
|
|
|
chan[c.chan].duty=c.value;
|
2021-12-04 07:19:14 +00:00
|
|
|
if (c.chan==3) { // noise
|
2021-12-04 06:19:54 +00:00
|
|
|
chan[c.chan].freqChanged=true;
|
2022-10-25 03:57:32 +00:00
|
|
|
} else if (c.chan<2) {
|
2022-10-25 05:43:03 +00:00
|
|
|
rWrite(0x4000+c.chan*4,(chan[c.chan].active?((chan[c.chan].envMode<<4)|chan[c.chan].outVol):0x30)|((chan[c.chan].duty&3)<<6));
|
2021-12-04 06:19:54 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-02-01 23:28:48 +00:00
|
|
|
case DIV_CMD_NES_SWEEP:
|
|
|
|
if (c.chan>1) break;
|
|
|
|
if (c.value2==0) {
|
|
|
|
chan[c.chan].sweep=0x08;
|
|
|
|
} else {
|
|
|
|
if (!c.value) { // down
|
|
|
|
chan[c.chan].sweep=0x88|(c.value2&0x77);
|
|
|
|
} else { // up
|
|
|
|
chan[c.chan].sweep=0x80|(c.value2&0x77);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rWrite(0x4001+(c.chan*4),chan[c.chan].sweep);
|
|
|
|
break;
|
2022-10-25 05:43:03 +00:00
|
|
|
case DIV_CMD_NES_ENV_MODE:
|
|
|
|
chan[c.chan].envMode=c.value&3;
|
|
|
|
if (c.chan==3) { // noise
|
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
} else if (c.chan<2) {
|
|
|
|
rWrite(0x4000+c.chan*4,(chan[c.chan].active?((chan[c.chan].envMode<<4)|chan[c.chan].outVol):0x30)|((chan[c.chan].duty&3)<<6));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIV_CMD_NES_LENGTH:
|
|
|
|
if (c.chan>=4) break;
|
|
|
|
chan[c.chan].len=c.value&0x1f;
|
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
chan[c.chan].prevFreq=-1;
|
|
|
|
break;
|
|
|
|
case DIV_CMD_NES_COUNT_MODE:
|
|
|
|
countMode=c.value;
|
|
|
|
rWrite(0x4017,countMode?0x80:0);
|
|
|
|
break;
|
2022-04-29 05:18:51 +00:00
|
|
|
case DIV_CMD_NES_DMC:
|
|
|
|
rWrite(0x4011,c.value&0x7f);
|
2022-05-02 07:54:23 +00:00
|
|
|
break;
|
|
|
|
case DIV_CMD_SAMPLE_MODE:
|
|
|
|
dpcmMode=c.value;
|
2022-05-02 08:42:40 +00:00
|
|
|
if (dumpWrites && dpcmMode) addWrite(0xffff0002,0);
|
|
|
|
dacSample=-1;
|
|
|
|
rWrite(0x4015,15);
|
|
|
|
rWrite(0x4010,0);
|
|
|
|
rWrite(0x4012,0);
|
|
|
|
rWrite(0x4013,0);
|
|
|
|
rWrite(0x4015,31);
|
2022-04-29 05:18:51 +00:00
|
|
|
break;
|
2021-12-09 08:13:37 +00:00
|
|
|
case DIV_CMD_SAMPLE_BANK:
|
|
|
|
sampleBank=c.value;
|
|
|
|
if (sampleBank>(parent->song.sample.size()/12)) {
|
|
|
|
sampleBank=parent->song.sample.size()/12;
|
|
|
|
}
|
|
|
|
break;
|
2021-12-04 06:19:54 +00:00
|
|
|
case DIV_CMD_LEGATO:
|
|
|
|
if (c.chan==3) break;
|
2022-05-02 21:05:07 +00:00
|
|
|
if (c.chan==4) {
|
2022-12-17 07:07:24 +00:00
|
|
|
chan[c.chan].baseFreq=parent->calcBaseFreq(1,1,c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)),false);
|
2022-05-02 21:05:07 +00:00
|
|
|
} else {
|
2022-12-17 07:07:24 +00:00
|
|
|
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
|
2022-05-02 21:05:07 +00:00
|
|
|
}
|
2021-12-04 06:19:54 +00:00
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
chan[c.chan].note=c.value;
|
|
|
|
break;
|
|
|
|
case DIV_CMD_PRE_PORTA:
|
2022-02-09 03:49:52 +00:00
|
|
|
if (chan[c.chan].active && c.value2) {
|
2022-04-28 06:31:16 +00:00
|
|
|
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_STD));
|
2022-02-09 03:46:58 +00:00
|
|
|
}
|
2022-12-17 07:10:20 +00:00
|
|
|
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);
|
2021-12-04 06:19:54 +00:00
|
|
|
chan[c.chan].inPorta=c.value;
|
|
|
|
break;
|
|
|
|
case DIV_CMD_GET_VOLMAX:
|
|
|
|
return 15;
|
|
|
|
break;
|
2022-12-17 05:09:56 +00:00
|
|
|
case DIV_CMD_MACRO_OFF:
|
|
|
|
chan[c.chan].std.mask(c.value,true);
|
|
|
|
break;
|
|
|
|
case DIV_CMD_MACRO_ON:
|
|
|
|
chan[c.chan].std.mask(c.value,false);
|
|
|
|
break;
|
2021-12-04 06:19:54 +00:00
|
|
|
case DIV_ALWAYS_SET_VOLUME:
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-12-18 08:25:42 +00:00
|
|
|
void DivPlatformNES::muteChannel(int ch, bool mute) {
|
|
|
|
isMuted[ch]=mute;
|
2022-05-02 04:57:53 +00:00
|
|
|
if (useNP) {
|
|
|
|
nes1_NP->SetMask(((int)isMuted[0])|(isMuted[1]<<1));
|
|
|
|
nes2_NP->SetMask(((int)isMuted[2])|(isMuted[3]<<1)|(isMuted[4]<<2));
|
|
|
|
} else {
|
|
|
|
nes->muted[ch]=mute;
|
|
|
|
}
|
2021-12-21 06:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformNES::forceIns() {
|
|
|
|
for (int i=0; i<5; i++) {
|
|
|
|
chan[i].insChanged=true;
|
|
|
|
chan[i].prevFreq=65535;
|
2021-12-18 08:25:42 +00:00
|
|
|
}
|
2022-02-01 23:28:48 +00:00
|
|
|
rWrite(0x4001,chan[0].sweep);
|
|
|
|
rWrite(0x4005,chan[1].sweep);
|
2022-10-25 05:43:03 +00:00
|
|
|
rWrite(0x4017,countMode?0x80:0);
|
2021-12-18 08:25:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 05:29:16 +00:00
|
|
|
void* DivPlatformNES::getChanState(int ch) {
|
|
|
|
return &chan[ch];
|
|
|
|
}
|
|
|
|
|
2022-06-05 23:17:00 +00:00
|
|
|
DivMacroInt* DivPlatformNES::getChanMacroInt(int ch) {
|
|
|
|
return &chan[ch].std;
|
|
|
|
}
|
|
|
|
|
2022-04-30 19:36:12 +00:00
|
|
|
DivDispatchOscBuffer* DivPlatformNES::getOscBuffer(int ch) {
|
|
|
|
return oscBuf[ch];
|
|
|
|
}
|
|
|
|
|
2022-02-22 03:31:27 +00:00
|
|
|
unsigned char* DivPlatformNES::getRegisterPool() {
|
|
|
|
return regPool;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DivPlatformNES::getRegisterPoolSize() {
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
2022-04-09 23:25:25 +00:00
|
|
|
float DivPlatformNES::getPostAmp() {
|
2022-04-28 01:36:32 +00:00
|
|
|
return 2.0f;
|
2022-04-09 23:25:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 18:14:38 +00:00
|
|
|
void DivPlatformNES::reset() {
|
|
|
|
for (int i=0; i<5; i++) {
|
|
|
|
chan[i]=DivPlatformNES::Channel();
|
2022-04-15 10:37:23 +00:00
|
|
|
chan[i].std.setEngine(parent);
|
2021-12-11 18:14:38 +00:00
|
|
|
}
|
2022-01-25 23:46:27 +00:00
|
|
|
if (dumpWrites) {
|
|
|
|
addWrite(0xffffffff,0);
|
|
|
|
}
|
2021-12-11 18:14:38 +00:00
|
|
|
|
|
|
|
dacPeriod=0;
|
|
|
|
dacPos=0;
|
|
|
|
dacRate=0;
|
|
|
|
dacSample=-1;
|
|
|
|
sampleBank=0;
|
2022-05-02 07:54:23 +00:00
|
|
|
dpcmBank=0;
|
|
|
|
dpcmMode=false;
|
2022-09-22 23:43:48 +00:00
|
|
|
goingToLoop=false;
|
2022-10-25 05:43:03 +00:00
|
|
|
countMode=false;
|
2021-12-11 18:14:38 +00:00
|
|
|
|
2022-05-02 04:20:20 +00:00
|
|
|
if (useNP) {
|
|
|
|
nes1_NP->Reset();
|
|
|
|
nes2_NP->Reset();
|
2022-05-02 04:57:53 +00:00
|
|
|
nes1_NP->SetMask(((int)isMuted[0])|(isMuted[1]<<1));
|
|
|
|
nes2_NP->SetMask(((int)isMuted[2])|(isMuted[3]<<1)|(isMuted[4]<<2));
|
2022-05-02 04:20:20 +00:00
|
|
|
} else {
|
|
|
|
apu_turn_on(nes,apuType);
|
|
|
|
nes->apu.cpu_cycles=0;
|
|
|
|
nes->apu.cpu_opcode_cycle=0;
|
|
|
|
}
|
2022-02-22 03:31:27 +00:00
|
|
|
memset(regPool,0,128);
|
2021-12-11 18:14:38 +00:00
|
|
|
|
2022-02-21 02:54:03 +00:00
|
|
|
rWrite(0x4015,0x1f);
|
2022-02-01 23:28:48 +00:00
|
|
|
rWrite(0x4001,chan[0].sweep);
|
|
|
|
rWrite(0x4005,chan[1].sweep);
|
2022-04-09 23:25:25 +00:00
|
|
|
|
|
|
|
dacAntiClickOn=true;
|
|
|
|
dacAntiClick=0;
|
2021-12-11 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 05:33:00 +00:00
|
|
|
bool DivPlatformNES::keyOffAffectsArp(int ch) {
|
2021-12-07 06:23:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-30 01:13:40 +00:00
|
|
|
void DivPlatformNES::setFlags(const DivConfig& flags) {
|
|
|
|
int clockSel=flags.getInt("clockSel",0);
|
|
|
|
if (clockSel==2) { // Dendy
|
2022-12-04 07:04:42 +00:00
|
|
|
chipClock=COLOR_PAL*2.0/5.0;
|
2022-01-29 05:20:27 +00:00
|
|
|
apuType=2;
|
2022-09-30 01:13:40 +00:00
|
|
|
} else if (clockSel==1) { // PAL
|
2022-12-04 07:04:42 +00:00
|
|
|
chipClock=COLOR_PAL*3.0/8.0;
|
2022-01-26 21:07:55 +00:00
|
|
|
apuType=1;
|
2022-01-29 05:20:27 +00:00
|
|
|
} else { // NTSC
|
2022-12-04 07:04:42 +00:00
|
|
|
chipClock=COLOR_NTSC/2.0;
|
2022-01-26 21:07:55 +00:00
|
|
|
apuType=0;
|
2022-05-02 04:20:20 +00:00
|
|
|
}
|
|
|
|
if (useNP) {
|
|
|
|
nes1_NP->SetClock(rate);
|
|
|
|
nes1_NP->SetRate(rate);
|
|
|
|
nes2_NP->SetClock(rate);
|
|
|
|
nes2_NP->SetRate(rate);
|
|
|
|
nes2_NP->SetPal(apuType==1);
|
|
|
|
} else {
|
2022-01-26 21:07:55 +00:00
|
|
|
nes->apu.type=apuType;
|
2021-12-08 06:56:40 +00:00
|
|
|
}
|
2022-12-04 07:04:42 +00:00
|
|
|
CHECK_CUSTOM_CLOCK;
|
|
|
|
rate=chipClock;
|
2022-04-30 19:36:12 +00:00
|
|
|
for (int i=0; i<5; i++) {
|
|
|
|
oscBuf[i]->rate=rate/32;
|
|
|
|
}
|
2021-12-15 22:32:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 00:36:02 +00:00
|
|
|
void DivPlatformNES::notifyInsDeletion(void* ins) {
|
|
|
|
for (int i=0; i<5; i++) {
|
|
|
|
chan[i].std.notifyInsDeletion((DivInstrument*)ins);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 23:08:19 +00:00
|
|
|
void DivPlatformNES::poke(unsigned int addr, unsigned short val) {
|
|
|
|
rWrite(addr,val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformNES::poke(std::vector<DivRegWrite>& wlist) {
|
|
|
|
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
|
|
|
|
}
|
|
|
|
|
2022-05-02 04:20:20 +00:00
|
|
|
void DivPlatformNES::setNSFPlay(bool use) {
|
|
|
|
useNP=use;
|
|
|
|
}
|
|
|
|
|
2022-05-02 07:12:14 +00:00
|
|
|
unsigned char DivPlatformNES::readDMC(unsigned short addr) {
|
2022-05-02 07:54:23 +00:00
|
|
|
return dpcmMem[(addr&0x3fff)|((dpcmBank&15)<<14)];
|
|
|
|
}
|
|
|
|
|
|
|
|
const void* DivPlatformNES::getSampleMem(int index) {
|
|
|
|
return index==0?dpcmMem:NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DivPlatformNES::getSampleMemCapacity(int index) {
|
|
|
|
return index==0?262144:0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DivPlatformNES::getSampleMemUsage(int index) {
|
|
|
|
return index==0?dpcmMemLen:0;
|
|
|
|
}
|
|
|
|
|
2022-11-26 23:44:04 +00:00
|
|
|
bool DivPlatformNES::isSampleLoaded(int index, int sample) {
|
|
|
|
if (index!=0) return false;
|
|
|
|
if (sample<0 || sample>255) return false;
|
|
|
|
return sampleLoaded[sample];
|
|
|
|
}
|
|
|
|
|
2022-11-27 04:50:20 +00:00
|
|
|
void DivPlatformNES::renderSamples(int sysID) {
|
2022-11-27 03:11:49 +00:00
|
|
|
memset(dpcmMem,0,getSampleMemCapacity(0));\
|
|
|
|
memset(sampleLoaded,0,256*sizeof(bool));
|
2022-05-02 07:54:23 +00:00
|
|
|
|
|
|
|
size_t memPos=0;
|
|
|
|
for (int i=0; i<parent->song.sampleLen; i++) {
|
|
|
|
DivSample* s=parent->song.sample[i];
|
2022-11-27 04:50:20 +00:00
|
|
|
if (!s->renderOn[0][sysID]) {
|
|
|
|
sampleOffDPCM[i]=0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-02 08:42:40 +00:00
|
|
|
unsigned int paddedLen=(s->lengthDPCM+63)&(~0x3f);
|
2022-05-02 07:54:23 +00:00
|
|
|
logV("%d padded length: %d",i,paddedLen);
|
2022-05-02 08:42:40 +00:00
|
|
|
if ((memPos&(~0x3fff))!=((memPos+paddedLen)&(~0x3fff))) {
|
|
|
|
memPos=(memPos+0x3fff)&(~0x3fff);
|
|
|
|
}
|
|
|
|
if (paddedLen>4081) {
|
|
|
|
paddedLen=4096;
|
2022-05-02 07:54:23 +00:00
|
|
|
}
|
|
|
|
if (memPos>=getSampleMemCapacity(0)) {
|
|
|
|
logW("out of DPCM memory for sample %d!",i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (memPos+paddedLen>=getSampleMemCapacity(0)) {
|
|
|
|
memcpy(dpcmMem+memPos,s->dataDPCM,getSampleMemCapacity(0)-memPos);
|
|
|
|
logW("out of DPCM memory for sample %d!",i);
|
|
|
|
} else {
|
2022-05-02 08:42:40 +00:00
|
|
|
memcpy(dpcmMem+memPos,s->dataDPCM,MIN(s->lengthDPCM,paddedLen));
|
2022-11-27 03:11:49 +00:00
|
|
|
sampleLoaded[i]=true;
|
2022-05-02 07:54:23 +00:00
|
|
|
}
|
2022-09-26 01:07:21 +00:00
|
|
|
sampleOffDPCM[i]=memPos;
|
2022-05-02 07:54:23 +00:00
|
|
|
memPos+=paddedLen;
|
|
|
|
}
|
|
|
|
dpcmMemLen=memPos;
|
2022-05-02 07:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 01:13:40 +00:00
|
|
|
int DivPlatformNES::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
|
2021-12-15 22:32:08 +00:00
|
|
|
parent=p;
|
2022-01-17 04:21:27 +00:00
|
|
|
dumpWrites=false;
|
2021-12-21 21:02:31 +00:00
|
|
|
skipRegisterWrites=false;
|
2022-05-02 04:20:20 +00:00
|
|
|
if (useNP) {
|
|
|
|
nes1_NP=new xgm::NES_APU;
|
|
|
|
nes1_NP->SetOption(xgm::NES_APU::OPT_NONLINEAR_MIXER,1);
|
|
|
|
nes2_NP=new xgm::NES_DMC;
|
|
|
|
nes2_NP->SetOption(xgm::NES_DMC::OPT_NONLINEAR_MIXER,1);
|
2022-05-02 07:12:14 +00:00
|
|
|
nes2_NP->SetMemory([this](unsigned short addr, unsigned int& data) {
|
|
|
|
data=readDMC(addr);
|
2022-05-02 04:20:20 +00:00
|
|
|
});
|
|
|
|
nes2_NP->SetAPU(nes1_NP);
|
|
|
|
} else {
|
|
|
|
nes=new struct NESAPU;
|
2022-05-02 07:12:14 +00:00
|
|
|
nes->readDMC=_readDMC;
|
|
|
|
nes->readDMCUser=this;
|
2022-05-02 04:20:20 +00:00
|
|
|
}
|
2022-04-30 19:36:12 +00:00
|
|
|
writeOscBuf=0;
|
2021-12-18 08:25:42 +00:00
|
|
|
for (int i=0; i<5; i++) {
|
|
|
|
isMuted[i]=false;
|
2022-05-02 04:20:20 +00:00
|
|
|
if (!useNP) nes->muted[i]=false;
|
2022-04-30 19:36:12 +00:00
|
|
|
oscBuf[i]=new DivDispatchOscBuffer;
|
2021-12-18 08:25:42 +00:00
|
|
|
}
|
2022-01-28 17:59:53 +00:00
|
|
|
setFlags(flags);
|
2021-12-04 06:19:54 +00:00
|
|
|
|
2022-05-02 07:54:23 +00:00
|
|
|
dpcmMem=new unsigned char[262144];
|
|
|
|
dpcmMemLen=0;
|
|
|
|
dpcmBank=0;
|
|
|
|
|
2021-12-04 06:19:54 +00:00
|
|
|
init_nla_table(500,500);
|
2021-12-11 18:14:38 +00:00
|
|
|
reset();
|
2021-12-04 06:19:54 +00:00
|
|
|
return 5;
|
|
|
|
}
|
2021-12-15 05:37:27 +00:00
|
|
|
|
|
|
|
void DivPlatformNES::quit() {
|
2022-04-30 19:36:12 +00:00
|
|
|
for (int i=0; i<5; i++) {
|
|
|
|
delete oscBuf[i];
|
|
|
|
}
|
2022-05-02 04:20:20 +00:00
|
|
|
if (useNP) {
|
|
|
|
delete nes1_NP;
|
|
|
|
delete nes2_NP;
|
|
|
|
} else {
|
|
|
|
delete nes;
|
|
|
|
}
|
2021-12-15 05:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DivPlatformNES::~DivPlatformNES() {
|
2022-01-17 18:29:35 +00:00
|
|
|
}
|