Linear-feedback shift register
In computing, a linear-feedback shift register (LFSR) is a shift register whose input bit is a linear function of its previous state.
The most commonly used linear function of single bits is exclusive-or (XOR). Thus, an LFSR is most often a shift register whose input bit is driven by the XOR of some bits of the overall shift register value.
The initial value of the LFSR is called the seed, and because the operation of the register is deterministic, the stream of values produced by the register is completely determined by its current (or previous) state. Likewise, because the register has a finite number of possible states, it must eventually enter a repeating cycle. However, an LFSR with a well-chosen feedback function can produce a sequence of bits that appears random and has a very long cycle.
Applications of LFSRs include generating pseudo-random numbers, pseudo-noise sequences, fast digital counters, and whitening sequences. Both hardware and software implementations of LFSRs are common.
The mathematics of a cyclic redundancy check, used to provide a quick check against transmission errors, are closely related to those of an LFSR.
{:deps
{org.clojure/clojure {:mvn/version "1.10.0"}
org.clojure/tools.deps.alpha
{:git/url "https://github.com/clojure/tools.deps.alpha.git"
:sha "f6c080bd0049211021ea59e516d1785b08302515"}
compliment {:mvn/version "0.3.9"}}}
(use clojure.tools.deps.alpha.repl)
(clojure-version)
(add-lib org.clojure/core.async {:mvn/version "0.4.490"})
(require [clojure.core.async :as async])
(async/timeout 100)
(defn lfsr [s {[long x long y long z] :taps}]
(concat (drop 1 s)
[(bit-xor (nth (- x 1))
(nth s (- y 1))
(nth s (- z 1)))]))
APU Noise
The NES APU noise channel generates pseudo-random 1-bit noise at 16 different frequencies.
The noise channel contains the following: envelope generator, timer, Linear Feedback Shift Register, length counter.
$400C - Length counter halt, constant volume/envelope flag, and volume/envelope divider period (write)
$400E - Mode and period (write)
Bit 7 - Mode flag
Bits 3-0 - The timer period is set to entry P of the following (NTSC):
4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4068
$400F - Length counter load and envelope restart (write)
The shift register is 15 bits wide, with bits numbered:
14 - 13 - 12 - 11 - 10 - 9 - 8 - 7 - 6 - 5 - 4 - 3 - 2 - 1 - 0
When the timer clocks the shift register, the following actions occur in order:
Feedback is calculated as the exclusive-OR of bit 0 and one other bit: bit 6 if Mode flag is set, otherwise bit 1.
The shift register is shifted right by one bit.
Bit 14, the leftmost bit, is set to the feedback calculated earlier.
This results in a pseudo-random bit sequence, 32767 steps long when Mode flag is clear, and randomly 93 or 31 steps long otherwise. (The particular 31- or 93-step sequence depends on where in the 32767-step sequence the shift register was when Mode flag was set).
The mixer receives the current envelope volume except when
Bit 0 of the shift register is set, or
The length counter is zero
On power-up, the shift register is loaded with the value 1.
The earliest revisions of the 2A03 CPU ignored the Mode flag, treating it as always 0. These CPUs were used in the first batch of Famicom consoles that were recalled, in Vs. System boards, and in the arcade games that used the 2A03 as a sound coprocessor.[1]
The 93-step sequence is about a quarter tone (50 cents) sharp relative to A440 tuning. The approximate frequencies and pitches (in LilyPond's variant of Helmholtz notation) are as follows:
(def header
[["<b>Period setting</b>"] ["<b>Sample rate (Hz)</b>"] ["<b>Fundamental</b>"] ["<b>MIDI Note</b>"] ["<b>Pitch</b>"]])
(def values
[["$80" 447443.2 4811.2 110.41 "d''''' + 41¢"]
["$81" 223721.6 2405.6 98.41 "d'''' + 41¢"]
["$82" 111860.8 1202.8 86.41 "d''' + 41¢"]
["$83" 55930.4 601.4 74.41 "d'' + 41¢"]
["$84" 27965.2 300.7 62.41 "d' + 41¢"]
["$85" 18643.5 200.5 55.39 "g + 39¢"]
["$86" 13982.6 150.4 50.41 "d + 41¢"]
["$87" 11186.1 120.3 46.55 "a#, + 55¢"]
["$88" 8860.3 95.3 42.51 "f#, + 51¢"]
["$89" 7046.3 75.8 38.55 "d, + 55¢"]
["$8A" 4709.9 50.6 31.57 "g,, + 57¢"]
["$8B" 3523.2 37.9 26.55 "d,, + 55¢"]
["$8C" 2348.8 25.3 19.53 "g,,, + 53¢"]
["$8D" 1761.6 18.9 14.55 "d,,, + 55¢"]
["$8E" 879.9 9.5 2.53 "d,,,, + 53¢"]
["$8F" 440.0 4.7 -9.47 "d,,,,, + 53¢"]])
(defn transpose [v]
(mapv (fn [ind]
(mapv (get % ind)
(filter (contains? % ind) v)))
(->> (map count v)
(apply max)
range)))
{:nextjournal/viewer :plotly}
{:data [{:header {:values header
:fill {:color "blue"}
:font {:family "Arial" :size 14 :color "white"}
:line {:color "black" :width 1}}
:cells {:values (transpose values)
:align "center"
:fill {:color [(interpose "turquoise" (repeat 9 "yellow"))]}
:font {:family "Arial" :size 12 :color ["black"]}}
:type "table"}]
:layout {:title "Pitches of 93-step noise on NTSC" :margin {:t 50}}}