Rnd

From Maze

(Difference between revisions)
Jump to: navigation, search
Line 323:Line 323:
E504 60 RTS
E504 60 RTS
</pre>
</pre>
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
On May 4, 2010, at 2:43 PM, Ian Bogost wrote:
 +
 +
Yes, Randomness is a good area to consider here. It brings up all sorts of issues of encapsulation in programming and expression. A random number generator is a powerful and nontrivial piece of engineering compared to the function one invokes in "10 print."
 +
 +
The smallest effective 8-bit RND I know of in 6502 assembly is 9 bytes long, and considerably longer than the entire "10 print" program when typed in:
 +
 +
<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
 +
 +
Although it's interesting to note that in "10 print" the BASIC RND (which generates a number between 0 and 1) is actually less efficient than simply ORing the C64 TOD clock's tenth-of-seconds register with $01 — although that clock outputs in binary-coded decimal (BCD), so it's less truly random. The need to do the RND(1)*2 is sort of an artifact of this process.
 +
 +
Incidentally, the BASIC RND function on the C64 is at ROM address $E09A, so it's possible just to jump there to generate a random number into the 6502's accumulator:
 +
 +
jsr $E09A
 +
 +
I think it was possible on the C64 to get random values from the Sound Interface Device (SID) chip. Does anyone remember how?
 +
 +
reading from register 27 (decimal) returns the current value of oscillator 3. If it's set to noise, you get a random number generator. (http://www.waitingforfriday.com/index.php/Commodore_SID_6581_Datasheet#SID_Control_Registers)
 +
 +
</pre>

Revision as of 15:32, 10 May 2010

View this revision of Rnd.

Personal tools