The [[APU|NES APU]] triangle channel generates a pseudo-triangle wave. It has no volume control; the waveform is only either cycling or suspended. [[#Linear counter control ($4008, write)|Linear Counter]] [[APU Length Counter|Length Counter]] | | v v Timer ---> Gate ----------> Gate ---> [[APU#Glossary|Sequencer]] ---> (to mixer) = Registers = == Linear counter control ($4008, write) == 7 bit 0 ---- ---- ICCC CCCC |||| |||| |+++-++++- Linear counter reload value +--------- Linear counter control flag ''and'' [[APU Length Counter|length counter]] halt flag The '''linear counter''' is an extra hardware duration timer that provides higher accuracy than the [[APU Length Counter|length counter]]. It works completely seperately from length counter. When the [[APU Frame Counter]] generates the linear counter clock, these actions occur in order: # If the [[#Length counter load value and timer period upper 3 bits ($400B, write)|linear counter reload flag]] is set the counter is reloaded with the counter reload value, otherwise, if the counter is non-zero, it is decremented. # If the counter control flag is clear, the linear counter reload flag is cleared. Note that the counter reload flag is not cleared unless the control flag is also clear, so when both are already set a value written to $4008 will be reloaded at the next linear counter clock. == Timer period lower 8 bits ($400A, write) == 7 bit 0 ---- ---- TTTT TTTT |||| |||| ++++-++++- Lower 8 bits of timer period value == Length counter load value and timer period upper 3 bits ($400B, write) == 7 bit 0 ---- ---- LLLL LTTT |||| |||| |||| |+++- Upper 3 bits of timer period value ++++-+---- [[APU Length Counter|Length counter]] load value Writing to this register sets [[#Linear counter control ($4008, write)|linear counter reload flag]] too. = Timer operation = The [[APU#Glossary|sequencer]] is clocked by a [[APU#Glossary|timer]] where the period is the 11-bit value (from [[#Timer period lower 8 bits ($400A, write)|$400A]] and [[#Length counter load value and timer period upper 3 bits ($400B, write)|$400B]]) ''plus one''. Given "t" as the timer value, the timer counts t, t-1, ..., 0, t, t-1, ..., clocking the waveform generator when it goes ''from 0 to t''. Unlike the [[APU Pulse|pulse]] channels, the timer of the triangle channel ticks at the rate of the [[Cycle reference chart#Clock rates|CPU clock]] rather than the APU (CPU/2) clock. So given the following: * fCPU = the [[Cycle reference chart#Clock rates|clock rate]] of the CPU * tval = the value written to the timer high and low registers * f = the frequency of the wave generated by this channel The following relationships hold: * f = fCPU ÷ (32 × (tval + 1)) * tval = fCPU ÷ (32 × f) - 1 Unlike the [[APU Pulse|pulse]] channels, the triangle channel supports frequencies up to the maximum frequency the timer will allow, meaning frequencies up to fCPU/32 (about 55.9 kHz for NTSC) are possible - far above the audible range. The sequencer is clocked by the timer as long as both the linear counter and the [[APU Length Counter|length counter]] are '''nonzero'''. The sequencer sends the following looping 32-step sequence of values to the [[APU Mixer|mixer]]:
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15= Halting triangle = The triangle channel stops the sequence ''only'' when either the length counter or the linear counter is zero. # '''Write $80 to [[#Linear counter control ($4008, write)|$4008]]''', which will halt and reload the linear counter with 0 (which will halt the [[APU#Glossary|sequencer]] because the counter is 0) at the next frame sequence tick. It can be simply resumed by writing $FF to $4008. Note that when using this method, the linear counter control flag and reload flag should be permanently set. #* Optionally: a write to [[APU Frame Counter|$4017]] after the write to $4008 can trigger an immediate frame sequence tick, if the 1/4 frame delay of the frame sequence is undesirable. This has all the other consequences of the frame sequence tick, however. # Use [[APU#Status ($4015)|$4015]] to turn off the channel, which will clear its length counter. To resume, turn it back on via $4015, then write to [[#Length counter load value and timer period upper 3 bits ($400B, write)|$400B]] to reload the length counter. #* When using DPCM samples, it is difficult to manipulate a single bit in $4015 without risking conflict with the DPCM control. # Either the length counter or linear counter can be used to automatically halt the triangle after a period of time. #* Unfortunately this is limited to very short durations because the linear counter can only go as high as 127 quarter frames. {{APU nav}} [[Category:APU]]