Thanks to Ross Ridge for calling the function to change the value in register Y. I knew that this must be something obvious!
By saving Y before the JSR and restoring after, it will now iterate correctly. Here is a quick solution:
Edit: Updated 7/10/17 - to show the full code and include the JeremyP offer . This is essentially a coin flip iterator (50,000 reps) for random experiments
.C 033c A9 00 LDA #$00 .C 033e 85 FB STA $FB ; set up register for counter .C 0340 85 FC STA $FC .C 0342 A2 C8 LDX #$C8 ; outer loop= 200 .C 0344 86 FD STX $FD .C 0346 A0 FA LDY #$FA ; inner loop=250 .C 0348 84 FE STY $FE .C 034a 20 94 E0 JSR $E094 ; Get random# Vic20 Address (E09B for C64) .C 034d A5 63 LDA $64 .C 034f C9 80 CMP #$80 ; >128 = HEADS .C 0351 90 0D BCC $0360 ; else continue loop .C 0353 18 CLC ; increment 2 byte number .C 0354 A5 FB LDA $FB .C 0356 69 01 ADC #$01 ; LSB .C 0358 85 FB STA $FB .C 035a A5 FC LDA $FC .C 035c 69 00 ADC #$00 ; MSB .C 035e 85 FC STA $FC .C 0360 C6 FE DEC $FE .C 0362 D0 E6 BNE $034A ; end inner loop .C 0364 C6 FD DEC $FD .C 0366 D0 DE BNE $0346 ; end outer loop .C 0368 60 RTS ; return to basic
I can get a random number LDA $63 or LDA $64 inside the loop and use it for my purposes.
This turned out to be much slower than expected, taking only half the time he would have done at BASIC. The RND function takes many cycles, however I found this Compute! article that uses a SID chip as a random number generator.
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
Once enabled, it generates numbers independently and should not be executed again. A loop that repeats the calls of LDA $D41B will give you a new random number at each iteration. In my test, 50,000 iterations took 1.25 seconds, and a million took just over 24 seconds. Pretty impressive for a 1 megahertz computer!
source share