NES: envelope and length counter effects

This commit is contained in:
tildearrow 2022-10-25 00:43:03 -05:00
parent 07f247af12
commit 9a1447e392
6 changed files with 100 additions and 8 deletions

View File

@ -19,7 +19,65 @@ also known as Famicom. it is a five-channel sound generator: first two channels
- `x` is the time.
- `y` is the shift.
- set to 0 to disable it.
- `15xx`: set envelope mode.
- `0`: envelope + length counter (volume represents envelope duration).
- `1`: length counter (volume represents output volume).
- `2`: looping envelope (volume represents envelope duration).
- `3`: constant volume (default; volume represents output volume).
- pulse and noise channels only.
- you may need to apply a phase reset (using the macro) to make the envelope effective.
- `16xx`: set length counter.
- see table below for possible values.
- this will trigger phase reset.
- `17xx`: set frame counter mode.
- `0`: 4-step.
- NTSC: 120Hz sweeps and lengths; 240Hz envelope.
- PAL: 100Hz sweeps and lengths; 200Hz envelope.
- Dendy: 118.9Hz sweeps and lengths; 237.8Hz envelope.
- `1`: 5-step.
- NTSC: 96Hz sweeps and lengths; 192Hz envelope.
- PAL: 80Hz sweeps and lengths; 160Hz envelope.
- Dendy: 95.1Hz sweeps and lengths; 190.2Hz envelope.
- `18xx`: set PCM channel mode.
- `00`: PCM (software).
- `01`: DPCM (hardware).
- when in DPCM mode, samples will sound muffled (due to its nature), availables pitches are limited and loop point is ignored.
# length counter table
val | raw | NTSC | PAL | Dendy | NTSC 5-step | PAL 5-step | Dendy 5-step
----|-----|-------|-------|-------|-------------|------------|--------------
00 | 10 | 83ms | 100ms | 84ms | 104ms | 125ms | 105ms
01 | 254 | 2.1s | 2.5s | 2.1s | 2.6s | 3.2s | 2.7s
02 | 20 | 166ms | 200ms | 168ms | 208ms | 250ms | 210ms
03 | 2 | 17ms | 20ms | 17ms | 21ms | 25ms | 21ms
04 | 40 | 333ms | 400ms | 336ms | 417ms | 500ms | 421ms
05 | 4 | 33ms | 40ms | 34ms | 42ms | 50ms | 42ms
06 | 80 | 667ms | 800ms | 673ms | 833ms | 1.0s | 841ms
07 | 6 | 50ms | 60ms | 50ms | 63ms | 75ms | 63ms
08 | 160 | 1.3s | 1.6s | 1.3s | 1.7s | 2.0s | 1.7s
09 | 8 | 67ms | 80ms | 67ms | 83ms | 100ms | 84ms
0A | 60 | 500ms | 600ms | 505ms | 625ms | 750ms | 631ms
0B | 10 | 83ms | 100ms | 84ms | 104ms | 125ms | 105ms
0C | 14 | 117ms | 140ms | 118ms | 146ms | 175ms | 147ms
0D | 12 | 100ms | 120ms | 101ms | 125ms | 150ms | 126ms
0E | 26 | 217ms | 260ms | 219ms | 271ms | 325ms | 273ms
0F | 14 | 117ms | 140ms | 118ms | 145ms | 175ms | 147ms
10 | 12 | 100ms | 120ms | 101ms | 125ms | 150ms | 126ms
11 | 16 | 133ms | 160ms | 135ms | 167ms | 200ms | 168ms
12 | 24 | 200ms | 240ms | 202ms | 250ms | 300ms | 252ms
13 | 18 | 150ms | 180ms | 151ms | 188ms | 225ms | 189ms
14 | 48 | 400ms | 480ms | 404ms | 500ms | 600ms | 505ms
15 | 20 | 167ms | 200ms | 168ms | 208ms | 250ms | 210ms
16 | 96 | 800ms | 960ms | 807ms | 1.0s | 1.2s | 1.0s
17 | 22 | 183ms | 220ms | 185ms | 229ms | 275ms | 231ms
18 | 192 | 1.6s | 1.9s | 1.6s | 2.0s | 2.4s | 2.0s
19 | 24 | 200ms | 240ms | 202ms | 250ms | 300ms | 252ms
1A | 72 | 600ms | 720ms | 606ms | 750ms | 900ms | 757ms
1B | 26 | 217ms | 260ms | 219ms | 271ms | 325ms | 273ms
1C | 16 | 133ms | 160ms | 135ms | 167ms | 200ms | 168ms
1D | 28 | 233ms | 280ms | 235ms | 292ms | 350ms | 294ms
1E | 32 | 267ms | 320ms | 269ms | 333ms | 400ms | 336ms
1F | 30 | 250ms | 300ms | 252ms | 313ms | 375ms | 315ms
reference: [NESdev](https://www.nesdev.org/wiki/APU_Length_Counter)

View File

@ -205,6 +205,10 @@ enum DivDispatchCmds {
DIV_CMD_SNES_ECHO_FEEDBACK,
DIV_CMD_SNES_ECHO_FIR,
DIV_CMD_NES_ENV_MODE,
DIV_CMD_NES_LENGTH,
DIV_CMD_NES_COUNT_MODE,
DIV_ALWAYS_SET_VOLUME, // () -> alwaysSetVol
DIV_CMD_MAX

View File

@ -214,7 +214,7 @@ void DivPlatformNES::tick(bool sysTick) {
rWrite(0x4000+i*4,(chan[i].outVol==0)?0:255);
chan[i].freqChanged=true;
} else {
rWrite(0x4000+i*4,0x30|chan[i].outVol|((chan[i].duty&3)<<6));
rWrite(0x4000+i*4,(chan[i].envMode<<4)|chan[i].outVol|((chan[i].duty&3)<<6));
}
}
if (chan[i].std.arp.had) {
@ -239,7 +239,7 @@ void DivPlatformNES::tick(bool sysTick) {
}
}
if (i!=2) {
rWrite(0x4000+i*4,0x30|chan[i].outVol|((chan[i].duty&3)<<6));
rWrite(0x4000+i*4,(chan[i].envMode<<4)|chan[i].outVol|((chan[i].duty&3)<<6));
}
if (i==3) { // noise
chan[i].freqChanged=true;
@ -289,11 +289,11 @@ void DivPlatformNES::tick(bool sysTick) {
}
if (i==3) { // noise
rWrite(0x4002+i*4,(chan[i].duty<<7)|chan[i].freq);
rWrite(0x4003+i*4,0xf0);
rWrite(0x4003+i*4,(chan[i].len<<3));
} else {
rWrite(0x4002+i*4,chan[i].freq&0xff);
if ((chan[i].prevFreq>>8)!=(chan[i].freq>>8) || i==2) {
rWrite(0x4003+i*4,0xf8|(chan[i].freq>>8));
rWrite(0x4003+i*4,(chan[i].len<<3)|(chan[i].freq>>8));
}
if (chan[i].freq!=65535 && chan[i].freq!=0) {
chan[i].prevFreq=chan[i].freq;
@ -419,7 +419,7 @@ int DivPlatformNES::dispatch(DivCommand c) {
if (c.chan==2) {
rWrite(0x4000+c.chan*4,0xff);
} else if (!parent->song.brokenOutVol2) {
rWrite(0x4000+c.chan*4,0x30|chan[c.chan].vol|((chan[c.chan].duty&3)<<6));
rWrite(0x4000+c.chan*4,(chan[c.chan].envMode<<4)|chan[c.chan].vol|((chan[c.chan].duty&3)<<6));
}
break;
case DIV_CMD_NOTE_OFF:
@ -451,7 +451,7 @@ int DivPlatformNES::dispatch(DivCommand c) {
if (c.chan==2) {
rWrite(0x4000+c.chan*4,0xff);
} else {
rWrite(0x4000+c.chan*4,0x30|chan[c.chan].vol|((chan[c.chan].duty&3)<<6));
rWrite(0x4000+c.chan*4,(chan[c.chan].envMode<<4)|chan[c.chan].vol|((chan[c.chan].duty&3)<<6));
}
}
}
@ -491,7 +491,7 @@ int DivPlatformNES::dispatch(DivCommand c) {
if (c.chan==3) { // noise
chan[c.chan].freqChanged=true;
} else if (c.chan<2) {
rWrite(0x4000+c.chan*4,0x30|(chan[c.chan].active?chan[c.chan].outVol:0)|((chan[c.chan].duty&3)<<6));
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_SWEEP:
@ -507,6 +507,24 @@ int DivPlatformNES::dispatch(DivCommand c) {
}
rWrite(0x4001+(c.chan*4),chan[c.chan].sweep);
break;
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;
case DIV_CMD_NES_DMC:
rWrite(0x4011,c.value&0x7f);
break;
@ -572,6 +590,7 @@ void DivPlatformNES::forceIns() {
}
rWrite(0x4001,chan[0].sweep);
rWrite(0x4005,chan[1].sweep);
rWrite(0x4017,countMode?0x80:0);
}
void* DivPlatformNES::getChanState(int ch) {
@ -615,6 +634,7 @@ void DivPlatformNES::reset() {
dpcmBank=0;
dpcmMode=false;
goingToLoop=false;
countMode=false;
if (useNP) {
nes1_NP->Reset();

View File

@ -28,7 +28,7 @@
class DivPlatformNES: public DivDispatch {
struct Channel {
int freq, baseFreq, pitch, pitch2, prevFreq, note, ins;
unsigned char duty, sweep;
unsigned char duty, sweep, envMode, len;
bool active, insChanged, freqChanged, sweepChanged, keyOn, keyOff, inPorta, furnaceDac;
signed char vol, outVol, wave;
DivMacroInt std;
@ -46,6 +46,8 @@ class DivPlatformNES: public DivDispatch {
ins(-1),
duty(0),
sweep(8),
envMode(3),
len(0x1f),
active(false),
insChanged(true),
freqChanged(false),
@ -74,6 +76,7 @@ class DivPlatformNES: public DivDispatch {
bool dacAntiClickOn;
bool useNP;
bool goingToLoop;
bool countMode;
struct NESAPU* nes;
xgm::NES_APU* nes1_NP;
xgm::NES_DMC* nes2_NP;

View File

@ -206,6 +206,10 @@ const char* cmdName[]={
"SNES_ECHO_FEEDBACK",
"SNES_ECHO_FIR",
"DIV_CMD_NES_ENV_MODE",
"DIV_CMD_NES_LENGTH",
"DIV_CMD_NES_COUNT_MODE",
"ALWAYS_SET_VOLUME"
};

View File

@ -693,6 +693,9 @@ void DivEngine::registerSystems() {
{0x12, {DIV_CMD_STD_NOISE_MODE, "12xx: Set duty cycle/noise mode (pulse: 0 to 3; noise: 0 or 1)"}},
{0x13, {DIV_CMD_NES_SWEEP, "13xy: Sweep up (x: time; y: shift)",constVal<0>,effectVal}},
{0x14, {DIV_CMD_NES_SWEEP, "14xy: Sweep down (x: time; y: shift)",constVal<1>,effectVal}},
{0x15, {DIV_CMD_NES_ENV_MODE, "15xx: Set envelope mode (0: envelope, 1: length, 2: looping, 3: constant)"}},
{0x16, {DIV_CMD_NES_LENGTH, "16xx: Set length counter (refer to manual for a list of values)"}},
{0x17, {DIV_CMD_NES_COUNT_MODE, "17xx: Set frame counter mode (0: 4-step, 1: 5-step)"}},
{0x18, {DIV_CMD_SAMPLE_MODE, "18xx: Select PCM/DPCM mode (0: PCM; 1: DPCM)"}}
}
);