FDS audio

From NESdev Wiki
Revision as of 17:07, 3 July 2013 by Rainwarrior (talk | contribs) (→‎Modulation append ($4088): reword speculation about implementation)
Jump to navigationJump to search

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)

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

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.

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 volume register can range from 0 to 63; however, volume values above 32 are clipped to 32 before output.

7  bit  0  (write; read through $4090)
---- ----
DMVV VVVV
|||| ||||
||++-++++- (D=0) Volume envelope speed
||         (D=1) Current volume level
|+-------- 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)

7  bit  0  (write)
---- ----
MExx FFFF
||   ||||
||   ++++- Bits 8-11 of frequency
|+-------- Disable volume and sweep envelopes (but not modulation)
+--------- 1: Silence channel (or halt counter?)

Sweep envelope ($4084)

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

Sweep bias ($4085)

A write resets the modulation unit's address to zero (see $4088) and changes the intensity and direction of modulation. See also "Modulation Unit".

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

Modulation 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 frequency

Modulation frequency high ($4087)

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

Modulation append ($4088)

The modulation table is a ring buffer 32 entries in length. Writing to the modulation append register removes the first entry, shifts each entry one space toward the front, and adds the written value as the last entry. However, the modulation unit uses each entry twice, so it is as if each write happens twice in a 64-entry ring buffer.

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

Waveform write enable ($4089)

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 silence channel)

Envelope speed ($408A)

Few FDS NSFs write to this register. BIOS or game code appears to set 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

Sweep gain ($4092)

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

Frequency calculation and timing

The 4 different areas of the FDS Sound channel (Volume Envelope, Sweep Envelope, Modulation Unit, and Main Unit) are clocked at independent rates. This section covers how to calculate the number of clocks per second (in Hz) from the various values written to the registers.

Volume envelope unit

Hz = (232 * 960) / (Envelope_Speed * (Volume_Envelope_Speed + 1)), where

  • Hz = how many times per second a clock occurs
  • Envelope_Speed = value written to $408A (if zero, Envelope is disabled)
  • Volume_Envelope_Speed = value set by writes to $4080

When Disch was working with this formula to fit in his NSF player, he was converting the formula to work with NES CPU cycles (~1798772 a second). This involved taking the reciprocal of the above formula and dividing by the NES frequency. This produced a somewhat simpler formula:

Cyc = 8 * Envelope_Speed * (Volume_Envelope_Speed + 1), where

  • Cyc = Number of NES CPU cycles that need to pass for 1 clock to occur

For this alternate formula in Hertz:

Hz = NES / Cyc, where

  • NES = NTSC NES CPU frequency (1789772.7272, or half the NTSC color subcarrier frequency)

Since all the other NES channels revolve around the CPU clock rate, this second formula seems more likely, since it also uses the NES clock as the base. In any case, the 2 formulas produce very similar results. If you work the conversion yourself... you end up simplifying:

1789772.7272 / (232 * 960) = ~8.036


Sweep envelope unit

Hz = (232 * 960) / (Envelope_Speed * (Sweep_Envelope_Speed + 1)), where

  • Sweep_Envelope_Speed = value set by writes to $4084
or

Cyc = 8 * Envelope_Speed * (Sweep_Envelope_Speed + 1)
Hz = NES / Cyc

Modulation unit

Hz = NES * ModFreq / 65536, where

  • ModFreq = 12-bit Modulation Frequency value set by $4086/$4087

Note that this frequency is how many times per second the modulation unit gets clocked... not how many times it goes through the entire Frequency Modulation Table (which would be this value / 64, since there are 32 entries in the table and each is used twice)

Main unit

Hz = NES * ( (Freq + Mod) / 65536 ), where

  • Freq = 12-bit Frequency set by $4082/$4083
  • Mod = Frequency change based on the Modulation unit (see next section)

Note again that this is the how many times per second the main unit gets clocked (taking 1 step further in the table). The entire table is 64 entries, so the frequency of it playing the entire wave would be this value / 64.

What each unit does in a clock

Volume envelope unit

Every clock, the Volume Envelope Unit alters the Volume_Gain depending on which mode we're in ($4080.D6).

  • Increase Mode: If Volume Gain is less than 32, it's increased by 1
  • Decrease Mode: If Volume Gain is greater than 0, it's decreased 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.

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).

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

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.


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:

 temp = Sweep_Bias * Sweep_Gain;
 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;


In this code, Freq is the 12-bit MAIN UNIT frequency, and Mod is the amount that frequency is altered (to bend the playback frequency in either direction). This generated 'Mod' value is used in the frequency calculation of the main unit (given earlier):

   Hz = NES * ( (Freq + Mod) / 65536 )

If at any time the Modulation unit is off, 'Mod' is zero. Otherwise 'Mod' is the 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 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

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