You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Audio_Registers.md
+65-32Lines changed: 65 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,8 +7,8 @@ As a rule of thumb, for any `x` in `1`, `2`, `3`, `4`:
7
7
-`NRx0` is some channel-specific feature (if present),
8
8
-`NRx1` controls the length timer,
9
9
-`NRx2` controls the volume and envelope,
10
-
-`NRx3` controls the wavelength (maybe only partially),
11
-
-`NRx4` has the channel's trigger and length timer enable bits;
10
+
-`NRx3` controls the period (maybe only partially),
11
+
-`NRx4` has the channel's trigger and length timer enable bits, as well as any leftover bits of period;
12
12
13
13
...but there are some exceptions.
14
14
@@ -35,7 +35,7 @@ Writing to those does NOT enable or disable the channels, despite many emulators
35
35
A channel is turned on by triggering it (i.e. setting bit 7 of `NRx4`)[^dac_off].
36
36
A channel is turned off when any of the following occurs:
37
37
- The channel's length timer, if enabled in `NRx4`, expires
38
-
- For CH1: when the wavelength sweep overflows[^freq_sweep_underflow]
38
+
- For CH1: when the period sweep overflows[^freq_sweep_underflow]
39
39
-[The channel's DAC](#DACs) is turned off
40
40
41
41
The envelope reaching a volume of 0 does NOT turn the channel off!
@@ -50,7 +50,7 @@ Actually, the low nibble of NR52 only reports whether the channels' *generation*
50
50
If [the DAC](#DACs) is off, then the write to NRx4 will be ineffective and won't turn the channel on.
51
51
52
52
[^freq_sweep_underflow]:
53
-
The wavelength sweep cannot normally underflow, so a "decreasing" sweep (`NR10` bit 3 set) won't turn the channel off.
53
+
The period sweep cannot normally underflow, so a "decreasing" sweep (`NR10` bit 3 set) won't turn the channel off.
54
54
55
55
### FF25 — NR51: Sound panning
56
56
@@ -82,46 +82,46 @@ Bit 3 - Mix VIN into right output (1=Enable)
82
82
Bit 2-0 - Right output volume (0-7)
83
83
```
84
84
85
-
## Sound Channel 1 — Pulse with wavelength sweep
85
+
## Sound Channel 1 — Pulse with period sweep
86
86
87
87
### FF10 — NR10: Channel 1 sweep
88
88
89
-
This register controls CH1's wavelength sweep functionality.
89
+
This register controls CH1's period sweep functionality.
90
90
91
91
```
92
92
Bit 6-4 - Sweep pace
93
93
Bit 3 - Sweep increase/decrease
94
-
0: Addition (wavelength increases)
95
-
1: Subtraction (wavelength decreases)
94
+
0: Addition (period increases)
95
+
1: Subtraction (period decreases)
96
96
Bit 2-0 - Sweep slope control (n: 0-7)
97
97
```
98
98
99
-
The <var>sweep pace</var> dictates how often the wavelength gets changed, in units of 128 Hz ticks[^div_apu] (7.8 ms).
99
+
The <var>sweep pace</var> dictates how often the period gets changed, in units of 128 Hz ticks[^div_apu] (7.8 ms).
100
100
The pace is only reloaded after the following sweep iteration, or when (re)triggering the channel.
101
101
However, if bits 4–6 are all set to 0, then iterations are instantly disabled, and the pace will be reloaded immediately if it's set to something else.
102
102
103
-
On each sweep iteration, the wavelength in [`NR13`](<#FF13 — NR13: Channel 1 wavelength low \[write-only\]>) and [`NR14`](<#FF14 — NR14: Channel 1 wavelength high & control>) is modified and written back.
103
+
On each sweep iteration, the period in [`NR13`](<#FF13 — NR13: Channel 1 period low \[write-only\]>) and [`NR14`](<#FF14 — NR14: Channel 1 period high & control>) is modified and written back.
104
104
That is, unless <var>n</var> (the slope) is 0, in which case iterations do nothing (in this case, subtraction mode should be set, see below).
105
105
106
-
On each tick, the new wavelength <math><msub><mi>L</mi><mrow><mi>t</mi><mo>+</mo><mn>1</mn></mrow></msub></math> is computed from the current one <math><msub><mi>L</mi><mi>t</mi></msub></math> as follows:
106
+
On each tick, the new period <math><msub><mi>L</mi><mrow><mi>t</mi><mo>+</mo><mn>1</mn></mrow></msub></math> is computed from the current one <math><msub><mi>L</mi><mi>t</mi></msub></math> as follows:
In addition mode, if the wavelength would overflow (i.e. <math><msub><mi>L</mi><mrow><mi>t</mi><mo>+</mo><mn>1</mn></mrow></msub></math> is strictly more than $7FF), the channel is turned off instead.
112
+
In addition mode, if the period value would overflow (i.e. <math><msub><mi>L</mi><mrow><mi>t</mi><mo>+</mo><mn>1</mn></mrow></msub></math> is strictly more than $7FF), the channel is turned off instead.
113
113
**This occurs even if sweep iterations are disabled** by <var>n</var> = 0.
114
114
115
-
Note that if the wavelength ever becomes 0, the wavelength sweep will never be able to change it.
116
-
For the same reason, the wavelength sweep cannot underflow the wavelength (which would turn the channel off).
115
+
Note that if the period ever becomes 0, the period sweep will never be able to change it.
116
+
For the same reason, the period sweep cannot underflow the period (which would turn the channel off).
This register controls both the channel's [length timer](<#Length timer>) and [duty cycle](https://en.wikipedia.org/wiki/Duty_cycle) (the ratio of the time spent low vs. high).
124
-
However, the selected duty cycle also alters the phase, although the effect is hardly noticeable.
124
+
The selected duty cycle also alters the phase, although the effect is hardly noticeable except in combination with other channels.
125
125
126
126
```
127
127
Bit 7-6 - Wave duty (Read/Write)
@@ -169,21 +169,44 @@ The envelope ticks at 64 Hz, and the channel's envelope will be increased / decr
169
169
170
170
Writes to this register while the channel is on require retriggering it afterwards.
### FF13 — NR13: Channel 1 period low \[write-only\]
173
173
174
-
This register stores the low 8 bits of the channel's 11-bit "[wavelength](<#Frequency>)".
174
+
This register stores the low 8 bits of the channel's 11-bit "[period value](<#Frequency>)".
175
175
The upper 3 bits are stored in the low 3 bits of `NR14`.
176
176
177
-
The actual signal frequency is <math><mfrac><mn>131072</mn><mrow><mn>2048</mn><mo>-</mo><mi>wavelen</mi></mrow></mfrac></math> Hz: the higher the value, the higher the frequency.
178
-
This is the whole wave's frequency; the rate at which the channel steps through the 8 "steps" in its wave form is 8× that, i.e. <math><mfrac><mn>1048576</mn><mrow><mn>2048</mn><mo>-</mo><mi>wavelen</mi></mrow></mfrac></math> Hz = <math><mfrac><mn>1</mn><mrow><mn>2048</mn><mo>-</mo><mn>wavelen</mn></mrow></mfrac></math> MiHz.
177
+
The period divider of pulse and wave channels is an up counter.
178
+
It adds one to the counter value each time it is clocked.
179
+
When the value reaches the maximum (2048 or $800), it reloads the counter value from the channel's period register.
180
+
This means it treats the value in the period as a *negative* number in 11-bit two's complement.
181
+
The higher the period value in the register, the lower the period, and the higher the frequency.
182
+
For example:
179
183
180
-
### FF14 — NR14: Channel 1 wavelength high & control
184
+
- Period value $500 means -$300, or 1 sample per 768 input cycles
185
+
- Period value $740 means -$C0, or 1 sample per 192 input cycles
186
+
187
+
The pulse channels' period dividers are clocked at 1048576 Hz, once per four dots, and their waveform is 8 samples long.
188
+
This makes their sample rate equal to <math><mfrac><mn>1048576</mn><mrow><mn>2048</mn><mo>-</mo><mi>period_value</mi></mrow></mfrac></math> Hz.
189
+
with a resulting tone frequency equal to <math><mfrac><mn>131072</mn><mrow><mn>2048</mn><mo>-</mo><mi>period_value</mi></mrow></mfrac></math> Hz.
190
+
191
+
- Period value $500 means -$300, or 1 sample per 768 input cycles
192
+
or (1048576 ÷ 768) = 1365.3 Hz sample rate
193
+
or (1048576 ÷ 768 ÷ 8) = 170.67 Hz tone frequency
194
+
- Period value $740 means -$C0, or 1 sample per 192 input cycles
195
+
or (1048576 ÷ 192) = 5461.3 Hz sample rate
196
+
or (1048576 ÷ 192 ÷ 8) = 682.67 Hz tone frequency
197
+
198
+
Period value $740 produces a higher frequency than $500.
199
+
Even though the period value $740 is not four times $500, $740 produces a frequency that is four times that of $500, or two octaves higher, because ($800 - $740) or 192 is one-quarter of ($800 - $500) or 768.
200
+
201
+
Period changes take
202
+
203
+
### FF14 — NR14: Channel 1 period high & control
181
204
182
205
```
183
206
Bit 7 - Trigger (1=Restart channel) (Write Only)
184
207
Bit 6 - Sound Length enable (Read/Write)
185
208
(1=Stop output when length in NR11 expires)
186
-
Bit 2-0 - "Wavelength"'s higher 3 bits (Write Only)
209
+
Bit 2-0 - Period value's higher 3 bits (Write Only)
187
210
```
188
211
189
212
Writing a value here with bit 7 set [triggers](<#Triggering>) the channel.
@@ -192,12 +215,12 @@ Bit 6 takes effect immediately upon writing to this register.
192
215
193
216
## Sound Channel 2 — Pulse
194
217
195
-
This sound channel works exactly like channel 1, except that it lacks a wavelength sweep (and thus an equivalent to [`NR10`](<#FF10 — NR10: Channel 1 sweep>)).
218
+
This sound channel works exactly like channel 1, except that it lacks a period sweep (and thus an equivalent to [`NR10`](<#FF10 — NR10: Channel 1 sweep>)).
### FF1D — NR33: Channel 3 period low \[write-only\]
250
273
251
-
This register stores the low 8 bits of the channel's 11-bit "[wavelength](<#Frequency>)".
274
+
This register stores the low 8 bits of the channel's 11-bit "[period value](<#Frequency>)".
252
275
The upper 3 bits are stored in the low 3 bits of `NR34`.
253
276
254
-
The actual signal frequency is <math><mfrac><mn>65536</mn><mrow><mn>2048</mn><mo>-</mo><mi>wavelen</mi></mrow></mfrac></math> Hz: the higher the value, the higher the frequency.
255
-
This is the whole wave's frequency; the rate at which the channel steps through the 8 "indices" in its wave form is 32 times that, i.e. <math><mfrac><mn>2097152</mn><mrow><mn>2048</mn><mo>-</mo><mi>wavelen</mi></mrow></mfrac></math>) Hz = <math><mfrac><mn>2</mn><mrow><mn>2048</mn><mo>-</mo><mi>wavelen</mi></mrow></mfrac></math> MiHz.
277
+
The wave channel's period divider is clocked at 2097152 Hz, once per two dots, and its waveform is 32 samples long.
278
+
This makes their sample rate equal to <math><mfrac><mn>2097152</mn><mrow><mn>2048</mn><mo>-</mo><mi>period_value</mi></mrow></mfrac></math> Hz.
279
+
with a resulting tone frequency equal to <math><mfrac><mn>65536</mn><mrow><mn>2048</mn><mo>-</mo><mi>period_value</mi></mrow></mfrac></math> Hz.
280
+
281
+
- Period value $500 means -$300, or 1 sample per 768 input cycles
282
+
or (2097152 ÷ 768) = 2730.7 Hz sample rate
283
+
or (2097152 ÷ 768 ÷ 32) = 85.333 Hz tone frequency
284
+
- Period value $740 means -$C0, or 1 sample per 192 input cycles
285
+
or (2097152 ÷ 192) = 10923 Hz sample rate
286
+
or (2097152 ÷ 192 ÷ 32) = 341.33 Hz tone frequency
287
+
288
+
Given the same period value, the tone frequency of the wave channel is generally half that of a pulse channel, or one octave lower.
256
289
257
290
::: warning DELAY
258
291
259
-
Wavelength changes (written to `NR33` or `NR34`) only take effect after the following time wave RAM is read.
292
+
Period changes (written to `NR33` or `NR34`) only take effect after the following time wave RAM is read.
### FF1E — NR34: Channel 3 wavelength high & control
297
+
### FF1E — NR34: Channel 3 period high & control
265
298
266
299
```
267
300
Bit 7 - Trigger (1=Restart Sound) (Write Only)
268
301
Bit 6 - Sound Length enable (Read/Write)
269
302
(1=Stop output when length in NR31 expires)
270
-
Bit 2-0 - "Wavelength"'s higher 3 bits (Write Only)
303
+
Bit 2-0 - Period value's higher 3 bits (Write Only)
271
304
```
272
305
273
306
Writing a value here with bit 7 set [triggers](<#Triggering>) the channel.
@@ -308,7 +341,7 @@ Accessing wave RAM while CH3 is **active** (i.e. playing) causes accesses to mis
308
341
- On other consoles, the byte accessed will be the one CH3 is currently reading[^wave_access]; that is, if CH3 is currently reading one of the first two samples, the CPU will really access $FF30, regardless of the address being used. ([Source](https://github.com/LIJI32/SameSuite/blob/master/apu/channel_3/channel_3_wave_ram_locked_write.asm))
309
342
310
343
Wave RAM *can* be accessed normally even if the DAC is on, as long as the channel is not active. ([Source](https://github.com/LIJI32/SameSuite/blob/master/apu/channel_3/channel_3_wave_ram_dac_on_rw.asm))
311
-
This is especially relevant on GBA, since ["DACs" are always enabled there](<#Game Boy Advance audio>).
344
+
This is especially relevant on GBA, whose [mixer behaves as if DACs are always enabled](<#Game Boy Advance audio>).
312
345
313
346
[^wave_access]:
314
347
The way it works is that wave RAM is a 16-byte memory buffer, and while it's playing, CH3 has priority over the CPU when choosing which of those 16 bytes is accessed.
Copy file name to clipboardExpand all lines: src/Audio_details.md
+5-4Lines changed: 5 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -123,7 +123,7 @@ Note that the envelope functionality changes the volume, but not the value store
123
123
If a DAC is enabled, the digital range $0 to $F is linearly translated to the analog range -1 to 1, in arbitrary units.
124
124
Importantly, the slope is negative: "digital 0" maps to "analog 1", not "analog -1".
125
125
126
-
If a DAC is disabled, it fades to an analog value of 0.
126
+
If a DAC is disabled, it fades to an analog value of 0, which corresponds to "digital 7.5".
127
127
The nature of this fade is not entirely deterministic and varies between models.
128
128
129
129
NR52's low 4 bits report whether the channels are turned on, not their DACs.
@@ -141,7 +141,7 @@ A channel can be deactivated in one of the following ways:
141
141
### Pulse channels (CH1, CH2)
142
142
143
143
Each pulse channel has an internal "duty step" counter, which is used to index into [the selected waveform](<#FF11 — NR11: Channel 1 length timer & duty cycle>) (each background stripe corresponds to one "duty step")[^pulse_lut].
144
-
The "duty step" increments at 8 times [the channel's frequency](<#FF13 — NR13: Channel 1 wavelength low \[write-only\]>)).
144
+
The "duty step" increments at the channel's sample rate, which is 8 times [the channel's frequency](<#FF13 — NR13: Channel 1 period low \[write-only\]>)).
145
145
146
146
The "duty step" counter cannot be reset, except by turning the APU off, which sets both back to 0.
147
147
Retriggering a pulse channel causes its "duty step timer" to reset, thus retriggering a pulse channel often enough will cause its "duty step" to never advance.
@@ -154,7 +154,8 @@ Actually, there is not LUT, but the manipulations done to the counter's bits are
154
154
### Wave channel (CH3)
155
155
156
156
CH3 has an internal "sample index" counter.
157
-
Each time it is ticked (as determined by the "wavelength" in NR33/NR34), that "sample index" is incremented, and then the corresponding "sample" (nibble) is read from wave RAM.
157
+
The "sample index" increments at the channel's sample rate, which is 32 times [the channel's frequency](<#FF1D — NR33: Channel 3 period low \[write-only\]>
158
+
Each time it increments, the corresponding "sample" (nibble) is read from wave RAM.
158
159
(This means that sample #0 is skipped when first starting up CH3.)
159
160
160
161
CH3 does not emit samples directly, but stores every sample read into a buffer, and emits that continuously; (re)triggering the channel does *not* clear nor refresh this buffer, so the last sample ever read will be emitted again.
@@ -169,7 +170,7 @@ This only matters when changing the setting mid-playback: the digital values bei
169
170
{{#include imgs/ch4_lfsr.svg:2:}}
170
171
171
172
CH4 revolves around a [LFSR](https://en.wikipedia.org/wiki/Linear-feedback_shift_register), pictured above.
172
-
The LFSR is 16-bit internally, but really acts as if it was 15-bit.
173
+
The LFSR has 16 bits: 15 bits for its current state and 1 bit to temporarily store the next bit to shift in.
173
174
174
175
When CH4 is ticked (at the frequency specified via [`NR43`](<#FF22 — NR43: Channel 4 frequency & randomness>)):
175
176
1. The result of <math><menclosenotation="top"><msub><mi>LFSR</mi><mn>0</mn></msub> <mo>⊕</mo> <msub><mi>LFSR</mi><mn>1</mn></msub></menclose></math> (`1` if bit 0 and bit 1 are identical, `0` otherwise) is written to bit 15.
0 commit comments