Remarks: threadbare and thread
From Maze
(Difference between revisions)
(Blanked the page) | (Adding SID randomness discussion from Randomness chapter) | ||
| Line 1: | Line 1: | ||
| + | While its predecessors such as the Commodore PET were able to generate random numbers, the Commodore 64 added another separate source of randomness, the MOS Technology 6581 Sound Interface Device (SID) chip. The SID was inspired by professional synthesizer electronics, and featured among other things the ability to produce square waves, sawtooth waves, triangle waves, and pseudo-random "noise." | ||
| + | By setting oscillator 3 on the SID chip to generate noise, then reading a noise output value in from register 27, a C64 BASIC or assembly program can draw on a second source of random numbers that changed constantly at the currently set speed of the oscillator. Because the SID is a mixed-signal integrated circuit (with digital control ports and analog output ports), a common misconception about the SID has been that it is a source of "true" or "high-quality" hardware-based random number generation, producing analog noise that was somehow the result of an analog process -- perhaps thermal noise, shot noise, or some other physical measurement of quantum indeterminacy. | ||
| + | |||
| + | However, this is not the case. SID noise is digitally generated by a 23-bit linear feedback shift register (LFSR), another common form of deterministically generating pseudorandom numbers. In an LFSR, a few selected bits are used to calculate one additional bit, then all old bits are shifted over one, with the new bit added to the top, and the process is repeated. In the SID, the result is something like a continuously running RND(1). | ||
| + | |||
| + | <pre> | ||
| + | 5 s=54272:poke s+15,100:pokes+18,128 | ||
| + | 10 print peek(s+27):goto 10 | ||
| + | </pre> | ||
| + | |||
| + | <pre> | ||
| + | 10 s=54272:pokes+15,100:pokes+18,128:printchr$(205.5+(peek(s+27)and1));:goto10 | ||
| + | </pre> | ||
| + | |||
| + | <pre> | ||
| + | 5 s=54272:pokes+15,100:pokes+18,128 | ||
| + | 10 print chr$(205.5+(peek(s+27)and1));:goto10 | ||
| + | </pre> | ||
| + | |||
| + | <pre> | ||
| + | 6 poke 54272+wave, noise | ||
| + | 10 print chr$(205.5+peek(54272+osc3read));goto 10 | ||
| + | </pre> | ||
| + | |||
| + | In addition to these methods of generating a random number in BASIC, it is possible to generate a random number on the C64 in assembly language: | ||
| + | |||
| + | <pre> | ||
| + | Rand8 | ||
| + | lda random ; get seed (must be non-zero) | ||
| + | asl ; shift byte | ||
| + | bcc Rand8.no_eor ; branch if carry flag not set after left shift | ||
| + | eor #$CF ; otherwise EOR with literal $CF | ||
| + | Rand8.no_eor | ||
| + | sta random ; save number as next seed | ||
| + | |||
| + | </pre> | ||
