Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting random number from 6502 assembler

Trying to generate a series of random numbers on my Commodore 64 (C64) using JSR $E09A and retrieving the number from $63 and $64. (which according to all the documentation I've seen is the same routine when you use RND(0) from BASIC. But can't get it to iterate. The following will work and place a different number in $63 and $64 when executed by itself.

. C000  A5 00    LDA $00
. C002  20 9A E0 JSR $E09A
. C005  00       BRK

Now when I try to iterate say 10 times with the following code, it never returns.

. C000  A0 0A    LDY #$0A
. C002  A9 00    LDA #$00
. C004  20 9A E0 JSR $E09A
. C007  88       DEY
. C008  D0 F8    BNE $C002
. C00A  00       BRK

Am I missing something so obvious I can't see it. I'm not worried about how "random" it is. At this point I just want a series of random numbers.

like image 505
Kenny Avatar asked Jul 05 '17 23:07

Kenny


1 Answers

The SID chip can actually generate numbers that are more random than BASIC's pseudo-random numbers. Start the generator with:

LDA #$FF  ; maximum frequency value
STA $D40E ; voice 3 frequency low byte
STA $D40F ; voice 3 frequency high byte
LDA #$80  ; noise waveform, gate bit off
STA $D412 ; voice 3 control register
RTS

Then you can get random numbers whenever you want with:

LDA $D41B ; get random value from 0-255
like image 88
Mike Avatar answered Sep 21 '22 22:09

Mike