FDS audio: Difference between revisions

From NESdev Wiki
Jump to navigationJump to search
(→‎Frequency calculation and timing: rewrite, explaining symmetry between units)
(→‎What each unit does in a clock: rewrite, new mod formula)
Line 241: Line 241:
For the wave output, disabling the unit also resets the wave position to 0 (i.e. the $4040 value).
For the wave output, disabling the unit also resets the wave position to 0 (i.e. the $4040 value).


== What each unit does in a clock ==
== Unit tick ==


=== Volume envelope unit ===
=== Envelopes ===


Every clock, the Volume Envelope Unit alters the Volume_Gain depending on which mode we're in ($4080.D6).
If enabled, when the volume or mod table envelope is ticked by its timer,
it will do one of the following based on $4080/$4084 bit 6:


*Increase Mode: If Volume Gain is less than 32, it's increased by 1
*Increase: if gain is less than 32, increase it by 1
*Decrease Mode: If Volume Gain is greater than 0, it's decreased by 1
*Decrease: if gain is more than 0, decrease it by 1


Note that despite Volume Gain in only increased if less than 32, it still can be greater than $20 (up to $3F) if set that way through a write to $4080.
Note that the gains manually can be set higher than 32, but they can no longer be increased at this point.
The gain can still be decreased if above 32.


Also on a clock, the output volume is set to the smaller of Volume Gain or 32 whichever is less (so while Volume Gain may be higher than 32, the actual output volume caps at 32).
The volume gain's final output will always be clamped to 32, even though the internal gain can be higher.
 
Otherwise, the volume and mod envelops are functionally identical.
=== Sweep envelope unit ===
Sweep Envelope unit behaves just like the Volume Envelope, only it alters Sweep Gain instead of Volume Gain.  The Envelope Unit never pushes Sweep Gain above $20, but it still can get above $20 if set that way via $4084.
 
Increase/Decrease mode is determined by bit 6 of $4084
 
Sweep Gain is used when calculating the Frequency change in the Modulation Unit:


=== Modulation unit ===
=== Modulation unit ===
The Modulation Unit, when clocked, takes 1 step through the Modulation Table (set by writes to $4088).  The Sweep Bias is adjusted based on the 3-bit value in the list [+0, +1, +2, +4, reset to 0, -4, -2, -1].
The address of the Modulation unit is incremented ''every other clock'' to use the next 3-bit value in the table.  This address wraps at 32 and can be reset to zero by any write to $4085.
After the Sweep Bias is adjusted, it wraps to fit within a signed 7-bit value. That is, if it goes greater than 63, it wraps around to -64, and if it goes below -64, it wraps to 63.


When the modulation unit is ticked, it applies the modulation value at the current position to the mod counter, then advances to the next position. Note that because values are written to the table in pairs, the same value will always be written twice on consecutive ticks.


The Modulation Unit works by altering the Frequency of the Main Unit by a value calculated from the Sweep Gain and Sweep Bias values. The logic for this calculation is complicated, represented by the following pseudocode:
Each 3-bit value in the mod table corresponds to one of the following adjustment of the mod counter when ticked:
0 = %000 -->  0
1 = %001 --> +1
2 = %010 --> +2
3 = %011 --> +4
4 = %100 --> reset to 0
5 = %101 --> -4
6 = %110 --> -2
  7 = %111 --> -1


  temp = Sweep_Bias * Sweep_Gain;
The mod counter is a signed 7-bit value, and will wrap if overflowed, i.e. 63 + 1 = -64 after wrap, and -64 - 1 = 63.
  if (temp & 0x0F)
  {
    temp /= 16;
    if( Sweep_Bias < 0 )    temp -= 1;
    else                    temp += 2;
  }
  else
    temp /= 16;
 
  if (temp > 193)
    temp -= 258;  // not a typo... for some reason the wraps are inconsistent
  if( temp < -64 )
    temp += 256;
 
  Mod = Freq * temp / 64;


The value of the mod counter will modify the pitch of the wave output unit in a complicated way,
described by the following C-style code


In this code, Freq is the 12-bit MAIN UNIT frequency, and Mod is the amount that
// pitch  = $4082/4083 (12-bit unsigned pitch value)
frequency is altered (to bend the playback frequency in either direction). This
// counter = $4085 (7-bit signed mod counter)
generated 'Mod' value is used in the frequency calculation of the main unit (given
// gain    = $4084 (6-bit unsigned mod gain)
earlier):
// 1. multiply counter by gain, lose lowest 4 bits of result but "round" in a strange way
temp = bias * gain;
remainder = temp & 0xF;
temp >>= 4;
if ((remainder > 0) && ((temp & 0x80) == 0))
{
    if (bias < 0) temp -= 1;
    else temp += 2;
}
// 2. wrap if a certain range is exceeded
if (temp >= 192) temp -= 256;
else if (temp < -64) temp += 256;
// 3. multiply result by pitch, then round to nearest while dropping 6 bits
temp = pitch * temp;
remainder = temp & 0x3F;
temp >>= 6;
if (remainder >= 32) temp += 1;
   
// final mod result is in temp
wave_pitch = pitch + temp;


    Hz = NES * ( (Freq + Mod) / 65536 )
Note that the resulting pitch value can go higher than 4095 (it will not wrap to 12-bits),
and if it goes lower than 0 it is presumably clamped to 0 (again, will not wrap).


If at any time the Modulation unit is off, 'Mod' is zero.  Otherwise 'Mod' is the
=== Wave output unit ===
above calculated value.  If Freq + Mod produces a number less than or equal to zero,
the channel is presumably silenced (<0 Hz output isn't really possible/audible).


=== Main unit ===
When this unit is ticked, it advances to the next position in its wave table.
When the main Unit is clocked, the next entry in the 64 entry waveform table
(accessed via regs $4040-$407F) is output.  Once the 64'th sample is played ($407F),
the table restarts ($4040 is played) and it continues to loop.


== Unit activity ==
== Unit activity ==

Revision as of 08:40, 17 July 2013

The Famicom Disk System audio is an audio channel generated by the 2C33 chip on the Famicom Disk System's RAM card and output through the Famicom cart edge connector's expansion audio pins.

This channel rapidly repeats a wavetable set up by the CPU in a manner similar to channel 3 of the Game Boy but with more sophisticated modulation. By changing the waveform, the program can have it simulate many different instruments. Many NSF composers have found this useful.

Registers

Master I/O enable ($4023)

This register must be written to with bit 1 set for the sound registers to function. The FDS bios initializes this by writing $00 followed by $83 to it.

7  bit  0
---------
xxxx xxSD
       ||
       |+- Enable disk I/O registers
       +-- Enable sound I/O registers

Wavetable RAM ($4040-$407F)

The 64-step waveform to be fed to the DAC. Each step consists of an unsigned value in the range [0, 63]. This can always be read by the CPU. However, it cannot be modified unless it is write-enabled, and it cannot be write-enabled while the sound is being played. (See also $4089 below.)

7  bit  0  (read/write)
---- ----
OOSS SSSS
|||| ||||
||++-++++- Sample
++-------- Returns 01 on read, likely from open bus

Volume envelope ($4080)

The envelope speed is set by this register whether or not the envelope is enabled by the high bit, but the current volume is set only if the high bit is set.

The volume gain can range from 0 to 63; however, volume values above 32 are clamped to 32 before output.

Writing to this register immediately resets the clock timer that ticks the volume envelope (delaying the next tick slightly).

7  bit  0  (write; read through $4090)
---- ----
DMVV VVVV
|||| ||||
||++-++++- (D=0) Volume envelope speed
||         (D=1) Volume gain and envelope speed.
|+-------- Volume change direction (0: decrease; 1: increase)
+--------- Volume envelope mode (0: on; 1: off)

Frequency low ($4082)

7  bit  0  (write)
---- ----
FFFF FFFF
|||| ||||
++++-++++- Bits 0-7 of frequency

Frequency high ($4083)

The high bit of this register halts the waveform and resets its phase to 0. Note that if halted it will output the constant value at $4040, and writes to the volume register $4080 or master volume $4089 will affect the output. The envelopes are not ticked while the waveform is halted.

Bit 6 halts just the envelopes without halting the waveform, and also resets both of their timers.

7  bit  0  (write)
---- ----
MExx FFFF
||   ||||
||   ++++- Bits 8-11 of frequency
|+-------- Disable volume and sweep envelopes (but not modulation)
+--------- Halt waveform and reset phase to 0, disable envelopes

Mod envelope ($4084)

The envelope speed is set by this register whether or not the envelope is enabled by the high bit, but the current mod gain is set only if the high bit is set.

Writing to this register immediately resets the clock timer that ticks the modulator envelope (delaying the next tick slightly).

7  bit  0  (write; read through $4092)
---- ----
DMSS SSSS
|||| ||||
||++-++++- (D=0) Mod envelope speed
||         (D=1) Mod gain and envelope speed.
|+-------- Mod envelope direction (0: decrease; 1: increase)
+--------- Mod envelope mode (0: on; 1: off)

Mod counter ($4085)

This directly sets the 7-bit signed modulator counter that is otherwise controlled by the mod unit.

Because the current playback position of the modulator unit is generally hard to predict while active, it is bad practice to write $4085 unless the mod unit is disabled via $4087, because it will generally result in a detuned note. Bio Miracle Bokutte Upa does this, and it requires cycle-accurate timing to emulate correctly. Some emulators incorrectly treat $4085 as a phase-reset for the mod table, which will obviate this timing issue.

It is generally good practice to write 0 to $4085 to reset the counter after writing the mod table via $4088.

7  bit  0  (write)
---- ----
xBBB BBBB
 ||| ||||
 +++-++++- Mod counter (7-bit signed; minimum $40; maximum $3F)

Mod frequency low ($4086)

If the 12-bit frequency is set to 0, modulation is disabled.

7  bit  0  (write)
---- ----
FFFF FFFF
|||| ||||
++++-++++- Bits 0-7 of modulation unit frequency

Mod frequency high ($4087)

Setting the high bit of this register halts the mod unit, and allows the mod table to be written via $4088.

7  bit  0  (write)
---- ----
Dxxx FFFF
|    ||||
|    ++++- Bits 8-11 of modulation frequency
+--------- Disable modulation

Mod table write ($4088)

This register has no effect unless the mod unit is disabled via the high bit of $4087.

The mod table is a ring buffer containing 64 entries. Writing to this register replaces two consecutive entries starting at the current mod table playback position with the written value, then advances the playback position to the following entry.

Writing to this register 32 times will effectively reset the phase of the mod table, having advanced the playback position back to its starting point.

Writing to this register also resets the timer counting clocks until the next tick, delaying the first tick after the mod unit is re-enabled.

7  bit  0  (write)
---- ----
xxxx xMMM
      |||
      +++- Modulation input

Wave write / master volume ($4089)

When the high bit is set, the current waveform output is held at its current level until the bit is cleared again. During this time, the wave unit will continue to run, even though the output level is held.

7  bit  0  (write)
---- ----
Wxxx xxVV
|      ||
|      ++- Master volume (0: full; 1: 2/3; 2: 2/4; 3: 2/5)
|          Output volume = current volume (see $4080 above) * master volume
+--------- Wavetable write enable
           (0: write protect RAM; 1: write enable RAM and hold channel)

Envelope speed ($408A)

This sets a clock multiplier for the volume and modulator envelopes. Few FDS NSFs write to this register. The BIOS initializes this to $FF.

7  bit  0  (write)
---- ----
SSSS SSSS
|||| ||||
++++-++++- Sets speed of volume envelope and sweep envelope
           (0: disable them)

Volume gain ($4090)

7  bit  0  (read; write through $4080)
---- ----
OOVV VVVV
|||| ||||
||++-++++- Current volume gain level
++-------- Returns 01 on read, likely from open bus

Mod gain ($4092)

7  bit  0  (read; write through $4084)
---- ----
OOVV VVVV
|||| ||||
||++-++++- Current mod gain level
++-------- Returns 01 on read, likely from open bus

Frequency calculation and timing

Envelopes

The volume and modulator envelopes tick once after a specific number of CPU clocks, calculated in the following way:

c = CPU clocks per tick
e = envelope speed ($4080/4084)
m = master envelope speed ($408A)
c =  8 * (e + 1) * m

To determine the frequency:

f = frequency of tick
n = CPU clock rate (~1789772 Hz)
f = n / c

Writing the envelope control registers for these units will reset the timer for the respective unit, meaning that it will next tick c cycles after the write.

Wavetables

The wave output and modulator accumulate a 16-bit phase value by adding the current 12-bit pitch value ($4082/$4083 or $4086/$4087) once per CPU clock. When the accumulator overflows, the table output position will advance by one.

f = frequency of tick
n = CPU clock rate (~1789772 Hz)
p = current pitch value ($4082/$4083 or $4086/$4087) plus modulation if wave output
f = n * p / 65536

Note that to determine the frequency of the entire waveform, divide this result by 64 (the length of the wavetable).

The pitch value for the wave output not simply the 12-bit value from registers $4082/$4083, but is modified by the modulator counter in an obtuse way. See below for details.

Disabling either unit via the high bit of $4083/$4087 immediately resets the accumulator, delaying the next tick after they are enabled again until the next overflow. For the wave output, disabling the unit also resets the wave position to 0 (i.e. the $4040 value).

Unit tick

Envelopes

If enabled, when the volume or mod table envelope is ticked by its timer, it will do one of the following based on $4080/$4084 bit 6:

  • Increase: if gain is less than 32, increase it by 1
  • Decrease: if gain is more than 0, decrease it by 1

Note that the gains manually can be set higher than 32, but they can no longer be increased at this point. The gain can still be decreased if above 32.

The volume gain's final output will always be clamped to 32, even though the internal gain can be higher. Otherwise, the volume and mod envelops are functionally identical.

Modulation unit

When the modulation unit is ticked, it applies the modulation value at the current position to the mod counter, then advances to the next position. Note that because values are written to the table in pairs, the same value will always be written twice on consecutive ticks.

Each 3-bit value in the mod table corresponds to one of the following adjustment of the mod counter when ticked:

0 = %000 -->  0
1 = %001 --> +1
2 = %010 --> +2
3 = %011 --> +4
4 = %100 --> reset to 0
5 = %101 --> -4
6 = %110 --> -2
7 = %111 --> -1

The mod counter is a signed 7-bit value, and will wrap if overflowed, i.e. 63 + 1 = -64 after wrap, and -64 - 1 = 63.

The value of the mod counter will modify the pitch of the wave output unit in a complicated way, described by the following C-style code

// pitch   = $4082/4083 (12-bit unsigned pitch value)
// counter = $4085 (7-bit signed mod counter)
// gain    = $4084 (6-bit unsigned mod gain)

// 1. multiply counter by gain, lose lowest 4 bits of result but "round" in a strange way
temp = bias * gain;
remainder = temp & 0xF;
temp >>= 4;
if ((remainder > 0) && ((temp & 0x80) == 0))
{
    if (bias < 0) temp -= 1;
    else temp += 2;
}

// 2. wrap if a certain range is exceeded
if (temp >= 192) temp -= 256;
else if (temp < -64) temp += 256;

// 3. multiply result by pitch, then round to nearest while dropping 6 bits
temp = pitch * temp;
remainder = temp & 0x3F;
temp >>= 6;
if (remainder >= 32) temp += 1;

// final mod result is in temp
wave_pitch = pitch + temp;

Note that the resulting pitch value can go higher than 4095 (it will not wrap to 12-bits), and if it goes lower than 0 it is presumably clamped to 0 (again, will not wrap).

Wave output unit

When this unit is ticked, it advances to the next position in its wave table.

Unit activity

Remember that each unit can be active regardless of the activitiy of other units. For example, even though the main unit is off and the channel is silent, this does not mean the Volume Envelope or Modulation units are inactive.

If any of the supplied conditions are false, the unit is inactive and will not be clocked. All conditions must be true for the unit to be active.

  • Volume envelope unit
    • Volume Envelope must be enabled (bit 7 of $4080 must be off), AND
    • Envelope Speed must be nonzero (set by $408A), AND
    • Envelope must be enabled (bit 6 of $4083 must be off)
  • Sweep envelope unit
    • Sweep Envelope must be enabled (bit 7 of $4084 must be off), AND
    • Envelope Speed must be nonzero (set by $408A), AND
    • Envelope must be enabled (bit 6 of $4083 must be off)
  • Modulation unit
    • Modulation must be enabled (bit 7 of $4087 must be off), AND
    • Modulation frequency must be non-zero (set by $4086/$4087)
  • Main unit:
    • Main Unit must be enabled (bit 7 of $4083 must be off), AND
    • Main Unit Frequency must be non-zero (set by $4082/$4083), AND
    • 'Freq + Mod' must be greater than zero (see Frequency Calculation section), AND
    • Write Mode must be off (bit 7 of $4089 must be off)

References