Commodore 64 Randomness
Sometimes using BASIC'S random number generator just isn't convenient, especially in machine language. Most ML programmers find other sources for random numbers. Here's a method for generating random numbers in machine language by using voice 3 of the SID Chip. Set the high-byte of the voice 3 frequency control ($D40F, 54287) to 255, and turn on bit 7 of the control register. (This selects the noise waveform.) Now you can read the upper eight bits of the waveform output from oscillator 3 at $D41B (54299) for random numbers between 1 and 255. Here's an example:
LDA #$FF ;load accumulator with 255 STA $D40F ;store accumulator in high byte of voice 3 LDA $80 ;load accumulator with 128 (binary 10000000) STA $D412 ;set bit 7 of voice 3 control register LDA $D41B ;load accumulator with oscillator output
David Jones
Thanks for the example. To use the voice 3 noise waveform from BASIC, enter:
10 POKE 54287, 255 20 POKE 54290, 128 30 PRINT PEEK(54299)
PEEKing 54299 will reveal a number between 1 and 255. You can continue to read this location without setting up the voice again, but you cannot use voice 3 for sound and for random numbers simultaneously—unless you want a high-pitched rushing sound.