NES: puNES acquireDirect(), part 1

no per-chan osc
This commit is contained in:
tildearrow 2025-03-07 01:29:18 -05:00
parent ad8437e5ae
commit dde97171ab
5 changed files with 348 additions and 276 deletions

View file

@ -62,7 +62,7 @@ const char** DivPlatformNES::getRegisterSheet() {
return regCheatSheetNES; return regCheatSheetNES;
} }
void DivPlatformNES::doWrite(unsigned short addr, unsigned char data) { void DivPlatformNES::doWrite(int ts, unsigned short addr, unsigned char data) {
if (useNP) { if (useNP) {
if (isE) { if (isE) {
e1_NP->Write(addr,data); e1_NP->Write(addr,data);
@ -72,13 +72,13 @@ void DivPlatformNES::doWrite(unsigned short addr, unsigned char data) {
nes2_NP->Write(addr,data); nes2_NP->Write(addr,data);
} }
} else { } else {
apu_wr_reg(nes,addr,data); apu_wr_reg(nes,ts,addr,data);
} }
} }
#define doPCM \ #define doPCM \
if (!dpcmMode && dacSample!=-1) { \ if (!dpcmMode && dacSample!=-1) { \
dacPeriod+=dacRate; \ dacPeriod+=dacRate*pcmAdvance; \
if (dacPeriod>=rate) { \ if (dacPeriod>=rate) { \
DivSample* s=parent->getSample(dacSample); \ DivSample* s=parent->getSample(dacSample); \
if (s->samples>0 && dacPos<s->samples) { \ if (s->samples>0 && dacPos<s->samples) { \
@ -86,10 +86,10 @@ void DivPlatformNES::doWrite(unsigned short addr, unsigned char data) {
unsigned char next=((unsigned char)s->data8[dacPos]+0x80)>>1; \ unsigned char next=((unsigned char)s->data8[dacPos]+0x80)>>1; \
if (dacAntiClickOn && dacAntiClick<next) { \ if (dacAntiClickOn && dacAntiClick<next) { \
dacAntiClick+=8; \ dacAntiClick+=8; \
doWrite(0x4011,dacAntiClick); \ doWrite(i,0x4011,dacAntiClick); \
} else { \ } else { \
dacAntiClickOn=false; \ dacAntiClickOn=false; \
doWrite(0x4011,next); \ doWrite(i,0x4011,next); \
} \ } \
} \ } \
dacPos++; \ dacPos++; \
@ -105,30 +105,43 @@ void DivPlatformNES::doWrite(unsigned short addr, unsigned char data) {
} \ } \
} }
void DivPlatformNES::acquire_puNES(short** buf, size_t len) { void DivPlatformNES::acquire_puNES(blip_buffer_t** bb, size_t len) {
for (int i=0; i<5; i++) { for (int i=0; i<5; i++) {
oscBuf[i]->begin(len); oscBuf[i]->begin(len);
} }
nes->timestamp=0;
nes->bb=bb[0];
for (size_t i=0; i<len; i++) { for (size_t i=0; i<len; i++) {
doPCM; // heuristic
int pcmAdvance=1;
if (writes.empty()) {
if (dpcmMode || dacSample==-1) {
break;
} else {
pcmAdvance=len-i;
if (dacPeriod>0) {
int remainTime=(rate-dacPeriod+dacRate-1)/dacRate;
if (remainTime<pcmAdvance) pcmAdvance=remainTime;
if (remainTime<1) pcmAdvance=1;
}
}
}
if (!writes.empty()) { if (!writes.empty()) {
pcmAdvance=1;
QueuedWrite w=writes.front(); QueuedWrite w=writes.front();
doWrite(w.addr,w.val); doWrite(i,w.addr,w.val);
regPool[w.addr&0x1f]=w.val; regPool[w.addr&0x1f]=w.val;
writes.pop(); writes.pop();
} }
apu_tick(nes,NULL); i+=pcmAdvance-1;
nes->apu.odd_cycle=!nes->apu.odd_cycle;
if (nes->apu.clocked) { doPCM;
nes->apu.clocked=false;
} /*
int sample=(pulse_output(nes)+tnd_output(nes))<<6;
if (sample>32767) sample=32767;
if (sample<-32768) sample=-32768;
buf[0][i]=sample;
if (++writeOscBuf>=32) { if (++writeOscBuf>=32) {
writeOscBuf=0; writeOscBuf=0;
oscBuf[0]->putSample(i,isMuted[0]?0:(nes->S1.output<<11)); oscBuf[0]->putSample(i,isMuted[0]?0:(nes->S1.output<<11));
@ -136,8 +149,9 @@ void DivPlatformNES::acquire_puNES(short** buf, size_t len) {
oscBuf[2]->putSample(i,isMuted[2]?0:(nes->TR.output<<11)); oscBuf[2]->putSample(i,isMuted[2]?0:(nes->TR.output<<11));
oscBuf[3]->putSample(i,isMuted[3]?0:(nes->NS.output<<11)); oscBuf[3]->putSample(i,isMuted[3]?0:(nes->NS.output<<11));
oscBuf[4]->putSample(i,isMuted[4]?0:(nes->DMC.output<<8)); oscBuf[4]->putSample(i,isMuted[4]?0:(nes->DMC.output<<8));
}*/
} }
} apu_tick(nes,len);
for (int i=0; i<5; i++) { for (int i=0; i<5; i++) {
oscBuf[i]->end(len); oscBuf[i]->end(len);
@ -147,6 +161,7 @@ void DivPlatformNES::acquire_puNES(short** buf, size_t len) {
void DivPlatformNES::acquire_NSFPlay(short** buf, size_t len) { void DivPlatformNES::acquire_NSFPlay(short** buf, size_t len) {
int out1[2]; int out1[2];
int out2[2]; int out2[2];
const int pcmAdvance=1;
for (int i=0; i<5; i++) { for (int i=0; i<5; i++) {
oscBuf[i]->begin(len); oscBuf[i]->begin(len);
@ -157,7 +172,7 @@ void DivPlatformNES::acquire_NSFPlay(short** buf, size_t len) {
if (!writes.empty()) { if (!writes.empty()) {
QueuedWrite w=writes.front(); QueuedWrite w=writes.front();
doWrite(w.addr,w.val); doWrite(i,w.addr,w.val);
regPool[w.addr&0x1f]=w.val; regPool[w.addr&0x1f]=w.val;
writes.pop(); writes.pop();
} }
@ -190,6 +205,7 @@ void DivPlatformNES::acquire_NSFPlay(short** buf, size_t len) {
void DivPlatformNES::acquire_NSFPlayE(short** buf, size_t len) { void DivPlatformNES::acquire_NSFPlayE(short** buf, size_t len) {
int out1[2]; int out1[2];
int out2[2]; int out2[2];
const int pcmAdvance=1;
for (int i=0; i<5; i++) { for (int i=0; i<5; i++) {
oscBuf[i]->begin(len); oscBuf[i]->begin(len);
@ -200,7 +216,7 @@ void DivPlatformNES::acquire_NSFPlayE(short** buf, size_t len) {
if (!writes.empty()) { if (!writes.empty()) {
QueuedWrite w=writes.front(); QueuedWrite w=writes.front();
doWrite(w.addr,w.val); doWrite(i,w.addr,w.val);
regPool[w.addr&0x1f]=w.val; regPool[w.addr&0x1f]=w.val;
writes.pop(); writes.pop();
} }
@ -231,15 +247,17 @@ void DivPlatformNES::acquire_NSFPlayE(short** buf, size_t len) {
} }
void DivPlatformNES::acquire(short** buf, size_t len) { void DivPlatformNES::acquire(short** buf, size_t len) {
if (useNP) { if (!useNP) return;
if (isE) { if (isE) {
acquire_NSFPlayE(buf,len); acquire_NSFPlayE(buf,len);
} else { } else {
acquire_NSFPlay(buf,len); acquire_NSFPlay(buf,len);
} }
} else {
acquire_puNES(buf,len);
} }
void DivPlatformNES::acquireDirect(blip_buffer_t** bb, size_t len) {
if (useNP) return;
acquire_puNES(bb,len);
} }
static unsigned char noiseTable[253]={ static unsigned char noiseTable[253]={
@ -920,6 +938,10 @@ bool DivPlatformNES::keyOffAffectsArp(int ch) {
return true; return true;
} }
bool DivPlatformNES::hasAcquireDirect() {
return (!useNP && !isE);
}
void DivPlatformNES::setFlags(const DivConfig& flags) { void DivPlatformNES::setFlags(const DivConfig& flags) {
int clockSel=flags.getInt("clockSel",0); int clockSel=flags.getInt("clockSel",0);
if (clockSel==2) { // Dendy if (clockSel==2) { // Dendy

View file

@ -85,14 +85,15 @@ class DivPlatformNES: public DivDispatch {
friend void putDispatchChip(void*,int); friend void putDispatchChip(void*,int);
friend void putDispatchChan(void*,int,int); friend void putDispatchChan(void*,int,int);
void doWrite(unsigned short addr, unsigned char data); void doWrite(int ts, unsigned short addr, unsigned char data);
unsigned char calcDPCMRate(int inRate); unsigned char calcDPCMRate(int inRate);
void acquire_puNES(short** buf, size_t len); void acquire_puNES(blip_buffer_t** bb, size_t len);
void acquire_NSFPlay(short** buf, size_t len); void acquire_NSFPlay(short** buf, size_t len);
void acquire_NSFPlayE(short** buf, size_t len); void acquire_NSFPlayE(short** buf, size_t len);
public: public:
void acquire(short** buf, size_t len); void acquire(short** buf, size_t len);
void acquireDirect(blip_buffer_t** bb, size_t len);
int dispatch(DivCommand c); int dispatch(DivCommand c);
void* getChanState(int chan); void* getChanState(int chan);
DivMacroInt* getChanMacroInt(int ch); DivMacroInt* getChanMacroInt(int ch);
@ -104,6 +105,7 @@ class DivPlatformNES: public DivDispatch {
void tick(bool sysTick=true); void tick(bool sysTick=true);
void muteChannel(int ch, bool mute); void muteChannel(int ch, bool mute);
bool keyOffAffectsArp(int ch); bool keyOffAffectsArp(int ch);
bool hasAcquireDirect();
float getPostAmp(); float getPostAmp();
unsigned char readDMC(unsigned short addr); unsigned char readDMC(unsigned short addr);
void setNSFPlay(bool use); void setNSFPlay(bool use);

View file

@ -21,9 +21,47 @@
#include <string.h> #include <string.h>
#include "apu.h" #include "apu.h"
void apu_tick(struct NESAPU* a, BYTE *hwtick) { void apu_tick(struct NESAPU* a, int len) {
if (len<=a->timestamp) return;
int rem=len-a->timestamp;
if (rem>1) {
// output now just in case
int sample=(pulse_output(a)+tnd_output(a))<<6;
if (sample!=a->lastSample) {
blip_add_delta(a->bb,a->timestamp,sample-a->lastSample);
a->lastSample=sample;
}
}
while (rem>0) {
// predict advance
int advance=rem;
if (a->r4017.jitter.delay) {
advance=1;
}
if (advance>a->apu.cycles) {
advance=a->apu.cycles;
}
if (advance>a->S1.frequency) {
advance=a->S1.frequency;
}
if (advance>a->S2.frequency) {
advance=a->S2.frequency;
}
if (advance>a->TR.frequency) {
advance=a->TR.frequency;
}
if (advance>a->NS.frequency) {
advance=a->NS.frequency;
}
if (advance>a->DMC.frequency) {
advance=a->DMC.frequency;
}
if (advance<1) advance=1;
/* sottraggo il numero di cicli eseguiti */ /* sottraggo il numero di cicli eseguiti */
a->apu.cycles--; a->apu.cycles-=advance;
/* /*
* questo flag sara' a TRUE solo nel ciclo * questo flag sara' a TRUE solo nel ciclo
* in cui viene eseguito il length counter. * in cui viene eseguito il length counter.
@ -178,33 +216,30 @@ void apu_tick(struct NESAPU* a, BYTE *hwtick) {
* valorizzandone l'output. * valorizzandone l'output.
*/ */
// SQUARE 1 TICK // SQUARE 1 TICK
if (!(--a->S1.frequency)) { if (!(a->S1.frequency-=advance)) {
square_output(a->S1, 0) square_output(a->S1, 0)
a->S1.frequency = (a->S1.timer + 1) << 1; a->S1.frequency = (a->S1.timer + 1) << 1;
a->S1.sequencer = (a->S1.sequencer + 1) & 0x07; a->S1.sequencer = (a->S1.sequencer + 1) & 0x07;
a->apu.clocked = TRUE;
} }
// SQUARE 2 TICK // SQUARE 2 TICK
if (!(--a->S2.frequency)) { if (!(a->S2.frequency-=advance)) {
square_output(a->S2, 0) square_output(a->S2, 0)
a->S2.frequency = (a->S2.timer + 1) << 1; a->S2.frequency = (a->S2.timer + 1) << 1;
a->S2.sequencer = (a->S2.sequencer + 1) & 0x07; a->S2.sequencer = (a->S2.sequencer + 1) & 0x07;
a->apu.clocked = TRUE;
} }
// TRIANGLE TICK // TRIANGLE TICK
if (!(--a->TR.frequency)) { if (!(a->TR.frequency-=advance)) {
a->TR.frequency = a->TR.timer + 1; a->TR.frequency = a->TR.timer + 1;
if (a->TR.length.value && a->TR.linear.value) { if (a->TR.length.value && a->TR.linear.value) {
a->TR.sequencer = (a->TR.sequencer + 1) & 0x1F; a->TR.sequencer = (a->TR.sequencer + 1) & 0x1F;
triangle_output() triangle_output()
a->apu.clocked = TRUE;
} }
} }
// NOISE TICK // NOISE TICK
if (!(--a->NS.frequency)) { if (!(a->NS.frequency-=advance)) {
if (a->NS.mode) { if (a->NS.mode) {
a->NS.shift = (a->NS.shift >> 1) | (((a->NS.shift ^ (a->NS.shift >> 6)) & 0x0001) << 14); a->NS.shift = (a->NS.shift >> 1) | (((a->NS.shift ^ (a->NS.shift >> 6)) & 0x0001) << 14);
} else { } else {
@ -213,11 +248,10 @@ void apu_tick(struct NESAPU* a, BYTE *hwtick) {
a->NS.shift &= 0x7FFF; a->NS.shift &= 0x7FFF;
noise_output() noise_output()
a->NS.frequency = noise_timer[a->apu.type][a->NS.timer]; a->NS.frequency = noise_timer[a->apu.type][a->NS.timer];
a->apu.clocked = TRUE;
} }
// DMC TICK // DMC TICK
if (!(--a->DMC.frequency)) { if (!(a->DMC.frequency-=advance)) {
if (!a->DMC.silence) { if (!a->DMC.silence) {
if (!(a->DMC.shift & 0x01)) { if (!(a->DMC.shift & 0x01)) {
if (a->DMC.counter > 1) { if (a->DMC.counter > 1) {
@ -242,7 +276,6 @@ void apu_tick(struct NESAPU* a, BYTE *hwtick) {
} }
} }
a->DMC.frequency = dmc_rate[a->apu.type][a->DMC.rate_index]; a->DMC.frequency = dmc_rate[a->apu.type][a->DMC.rate_index];
a->apu.clocked = TRUE;
} }
if (a->DMC.empty && a->DMC.remain) { if (a->DMC.empty && a->DMC.remain) {
BYTE tick = 4; BYTE tick = 4;
@ -260,8 +293,6 @@ void apu_tick(struct NESAPU* a, BYTE *hwtick) {
{ {
a->DMC.buffer = a->readDMC(a->readDMCUser,a->DMC.address); a->DMC.buffer = a->readDMC(a->readDMCUser,a->DMC.address);
} }
/* incremento gli hwtick da compiere */
if (hwtick) { hwtick[0] += tick; }
/* e naturalmente incremento anche quelli eseguiti dall'opcode */ /* e naturalmente incremento anche quelli eseguiti dall'opcode */
a->apu.cpu_cycles += tick; a->apu.cpu_cycles += tick;
/* salvo a che ciclo dell'istruzione avviene il dma */ /* salvo a che ciclo dell'istruzione avviene il dma */
@ -281,8 +312,25 @@ void apu_tick(struct NESAPU* a, BYTE *hwtick) {
} }
} }
a->r4011.cycles++; a->r4011.cycles+=advance;
if (advance&1) {
a->apu.odd_cycle=!a->apu.odd_cycle;
} }
// output sample
a->timestamp+=advance-1;
int sample=(pulse_output(a)+tnd_output(a))<<6;
if (sample!=a->lastSample) {
blip_add_delta(a->bb,a->timestamp,sample-a->lastSample);
a->lastSample=sample;
}
rem-=advance;
a->timestamp++;
}
a->timestamp=len;
}
void apu_turn_on(struct NESAPU* a, BYTE apu_type) { void apu_turn_on(struct NESAPU* a, BYTE apu_type) {
memset(&a->apu, 0x00, sizeof(a->apu)); memset(&a->apu, 0x00, sizeof(a->apu));
memset(&a->r4015, 0x00, sizeof(a->r4015)); memset(&a->r4015, 0x00, sizeof(a->r4015));
@ -322,4 +370,5 @@ void apu_turn_on(struct NESAPU* a, BYTE apu_type) {
a->apu.odd_cycle = 0; a->apu.odd_cycle = 0;
// come non viene inizializzato? Vorrei qualche spiegazione... // come non viene inizializzato? Vorrei qualche spiegazione...
a->r4011.frames = 0; a->r4011.frames = 0;
a->lastSample = 0;
} }

View file

@ -22,6 +22,7 @@
#define APU_H_ #define APU_H_
#include "common.h" #include "common.h"
#include "blip_buf.h"
enum dmc_types_of_dma { DMC_NORMAL, DMC_CPU_WRITE, DMC_R4014, DMC_NNL_DMA }; enum dmc_types_of_dma { DMC_NORMAL, DMC_CPU_WRITE, DMC_R4014, DMC_NNL_DMA };
enum apu_channels { APU_S1, APU_S2, APU_TR, APU_NS, APU_DMC, APU_EXTRA, APU_MASTER }; enum apu_channels { APU_S1, APU_S2, APU_TR, APU_NS, APU_DMC, APU_EXTRA, APU_MASTER };
@ -277,12 +278,6 @@ typedef struct _apu {
int cpu_cycles; int cpu_cycles;
int cpu_opcode_cycle; int cpu_opcode_cycle;
BYTE odd_cycle; BYTE odd_cycle;
/* ------------------------------------------------------- */
/* questi valori non e' necessario salvarli nei savestates */
/* ------------------------------------------------------- */
/* */ BYTE clocked; /* */
/* ------------------------------------------------------- */
} _apu; } _apu;
typedef struct _r4011 { typedef struct _r4011 {
BYTE value; BYTE value;
@ -433,8 +428,11 @@ EXTERNC struct NESAPU {
_apuTriangle TR; _apuTriangle TR;
_apuNoise NS; _apuNoise NS;
_apuDMC DMC; _apuDMC DMC;
blip_buffer_t* bb;
void* readDMCUser; void* readDMCUser;
unsigned char (*readDMC)(void*,unsigned short); unsigned char (*readDMC)(void*,unsigned short);
int timestamp;
int lastSample;
unsigned char muted[5]; unsigned char muted[5];
}; };
@ -540,7 +538,7 @@ static const WORD dmc_rate[3][16] = {
} }
}; };
EXTERNC void apu_tick(struct NESAPU* a, BYTE *hwtick); EXTERNC void apu_tick(struct NESAPU* a, int len);
EXTERNC void apu_turn_on(struct NESAPU* a, BYTE apu_type); EXTERNC void apu_turn_on(struct NESAPU* a, BYTE apu_type);
#undef EXTERNC #undef EXTERNC

View file

@ -39,7 +39,9 @@
r2006.value++;\ r2006.value++;\
} }
INLINE static void apu_wr_reg(struct NESAPU* a, WORD address, BYTE value) { INLINE static void apu_wr_reg(struct NESAPU* a, int ts, WORD address, BYTE value) {
apu_tick(a,ts);
if (!(address & 0x0010)) { if (!(address & 0x0010)) {
/* -------------------- square 1 --------------------*/ /* -------------------- square 1 --------------------*/
if (address <= 0x4003) { if (address <= 0x4003) {
@ -200,7 +202,6 @@ INLINE static void apu_wr_reg(struct NESAPU* a, WORD address, BYTE value) {
} else { } else {
a->DMC.counter = a->DMC.output = value; a->DMC.counter = a->DMC.output = value;
} }
a->apu.clocked = TRUE;
a->r4011.cycles = a->r4011.frames = 0; a->r4011.cycles = a->r4011.frames = 0;
a->r4011.value = value; a->r4011.value = value;