If you use polynomial counter distortion, then calculating the pitch becomes somewhat more complicated. Here is how the 4-bit counter works:
- it is a 4-bit linear feedback shift register clocked at 250 kHz (assuming a standard system clock frequency and the BFh port correctly set for that)
- on each 250 kHz cycle, it is shifted to the left by 1 bit, and the new 0. bit (as well as the output) becomes the result of a XOR operation between the original 2. and 3. bits
- this loops a sequence with a length of 15, as the value of 0 is invalid (15, 14, 12, 8, 1, 2, 4, 9, 3, 6, 13, 10, 5, 11, 7, which outputs 100010011010111)
- when distortion with this counter is enabled, the tone generator samples the output of the counter at a frequency of 250000 Hz / (N + 1), where N is the 12-bit value written to the DAVE tone generator frequency register
What makes the distortion tricky is that the output becomes a pattern that depends on (N + 1) modulo 15. If that is zero, then it outputs a steady state (f = 0 Hz, no tone). If it is 1, 2, 4, 7, 8, 11, 13, or 14, then a full sequence length of 15 is played, although the exact pattern varies (f = 16666.67 / (N + 1) Hz). With a remainder of 3, 6, 9, or 12, the pattern length is 5 (f = 50000 / (N + 1) Hz). Otherwise (remainder = 5 or 10) it is 3 (f = 83333.33 / (N + 1) Hz). Since the PITCH parameter in IS-BASIC is not the actual DAVE frequency code, getting the correct pitch may require some trial and error.
The DAVE frequency code is calculated from the PITCH value as follows, at least in theory:
N = 125000 / (2 ^ ((PITCH - 46) / 12) * 440) - 1 (rounded to the nearest integer)