Namco 163 audio

From NESdev Wiki
Revision as of 13:10, 11 March 2012 by Rainwarrior (talk | contribs) (→‎Sound RAM $7C: length value is actually 6-bit according to jsr, some games like Rolling Thunder make use of long samples this way)
Jump to navigationJump to search

Namco 163 is a variant of the Namco 106 mapper offering up to 8 additional sound channels that play wavetable samples. In addition, each waveform can be of a configurable length, and each channel has linear volume control. Chip has $80 bytes of sound RAM shared by channel registers and wavetable samples; at least $40 bytes are dedicated to samples, with more available if not all channels are used.

Like the Ensoniq DOC in the Apple IIGS, the chip is apparently unable to clock every channel at once, so it cycles though channels, clocking one each cycle. Because of this, the chip allows the game to configure the number of enabled channels. When fewer channels are enabled, the channels are clocked more often, allowing for higher tones with longer, more detailed waveforms. When more channels are enabled, clocking slows down since each channel has to wait its turn, resulting in lower tones.

Registers

Address Port ($F800-$FFFF)

7  bit  0   (write only)
---- ----
IAAA AAAA
|||| ||||
|+++-++++- Address
+--------- Auto-increment

Writing to this register sets the internal address. If the 'I' bit is set, the address will increment on writes and reads to the Data Port ($4800), wrapping $7F->$00 (address is 7 bits wide)

Data Port ($4800-$4FFF)

7  bit  0   (read / write)
---- ----
DDDD DDDD
|||| ||||
++++-++++- Data

This Port accesses the N163's internal $80 bytes of sound RAM. Which of the $80 bytes is determined by the Address register ($F800). When read, the appropriate byte is returned. When written, the appropriate byte is set.

This RAM is used primarily for wavetables. The sound channel control registers are also set by writing to certain addresses in sound RAM:

Sound RAM $78

7  bit  0
---------
FFFF FFFF
|||| ||||
++++-++++- Low 8 bits of Frequency

Sound RAM $7A

7  bit  0
---------
FFFF FFFF
|||| ||||
++++-++++- Middle 8 bits of Frequency

Sound RAM $7C

7  bit  0
---------
LLLL LLFF
|||| ||||
|||| ||++- High 2 bits of Frequency
++++-++--- Length of waveform ((64-L)*4 4-bit samples)

Sound RAM $7E

7  bit  0
---------
AAAA AAAA
|||| ||||
++++-++++- Address of waveform (in 4-bit samples)

Sound RAM $7F

7  bit  0
---------
.CCC VVVV
 ||| ||||
 ||| ++++- Linear Volume
 +++------ Enabled Channels (1+C)

Note 'C' is available on register $7F ONLY. Those bits have no effect in other registers.

When C=0, only channel 8 enabled
When C=1, channels 8 and 7 enabled
When C=2, channels 8, 7, 6 enabled
etc

Other Channels

Above Sound RAM register descriptions ($78-$7F) are for the 8th channel. The other 7 channels are accessed via the same pattern, but each 8 bytes before the last:

Channel 8:  $78, $7A, $7C, $7E, $7F
Channel 7:  $70, $72, $74, $76, $77
Channel 6:  $68, $6A, $6C, $6E, $6F
Channel 5:  $60, $62, $64, $66, $67
Channel 4:  $58, $5A, $5C, $5E, $5F
Channel 3:  $50, $52, $54, $56, $57
Channel 2:  $48, $4A, $4C, $4E, $4F
Channel 1:  $40, $42, $44, $46, $47

Again note that the 'C' bits in the final register is only available at address $7F.

When channels are disabled, their registers are unused, and can be used for waveform data instead.

Operation

Each enabled channel cycles through its waveform at a rate determined by the 18-bit frequency value 'F'. Each step in the waveform is 4-bits wide, and the number of steps is determined by the 'L' bits ((8-L)*4). Two samples are stored to a byte, which is little-endian (unlike the Game Boy's wavetable channel).

The 'A' bits dictate where in the internal sound RAM the waveform starts. 'A' is the address in 4-bit samples, therefore a value of $02 would be the low 4 bits of address $01. A value of $03 would be the high 4 bits of address $01.

For a visual example, assume you have the following sound RAM:

$00:    00 00 00 A8 EC FE FF FF  EE AC 57 13 01 00 00 11
$10:    53 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00

And assume a channel has a 'A' value of $06, and a 'L' value of 1. That channel's wavetable would form a would-be sine wave, looking like the following:

F-     *****
E-   **     **
D-
C-  *         *
B-
A- *           *
9-
8-*
7-              *
6-
5-               *           *
4-
3-                *         *
2-
1-                 **     **
0-                   *****

The generated tone (in Hz) can be calculated from the following formula:

           F * 21477272.7272
Hz = -----------------------------
     $40000 * 45 * (C+1) * (8-L)*4

Where a 60 Hz tone would be a channel cycling through its entire waveform 60 times in one second.

A common method of emulating sound is to have the channel take another step through its wavetable every X CPU cycles. Therefore, an alternative formula:

    $40000 * 45 * (C+1)              $F0000 * (C+1)
X = -------------------    or    X = --------------
          12 * F                           F

Where X is the number of CPU cycles between steps in the channel's waveform.

References