I'm fooling around with a random-number generator (used to randomly fill the background) and I'm running into a frustrating problem. Here's the RNG subroutine, adopted from here:
And then I'm doing a basic nested loop to fill the background (using count1 and count2 in RAM instead of X or Y - for testing purposes, not for code elegance):
As you might note from the comment, I was originally doing a count compare for $20 and $1E, but I was getting a noticeable pattern repeat from the RNG. When I eliminate the nested loop, all of the tiles are random. I can't figure out what's resetting the RNG when I branch back to LoadBackgroundLoop after resetting count1. It's likely I'm not understanding what's going on in the RNG code. Any suggestions?
FYI, the seed var is set to #$01 prior to LoadBackground, which is only called once. And I'm using NESASMv3.1
Code:
Rand:
LDA seed ; get seed
ASL A
BCC NoEOR ; branch if no carry
EOR #$CF ; else EOR with #$value
NoEOR:
STA seed ; save number as next seed
RTS
LDA seed ; get seed
ASL A
BCC NoEOR ; branch if no carry
EOR #$CF ; else EOR with #$value
NoEOR:
STA seed ; save number as next seed
RTS
And then I'm doing a basic nested loop to fill the background (using count1 and count2 in RAM instead of X or Y - for testing purposes, not for code elegance):
Code:
LoadBackground:
LDA $2002 ; read PPU status to reset the high/low latch
LDA #$20
STA $2006 ; write the high byte of $2000 address
LDA #$00
STA $2006 ; write the low byte of $2000 address
STA count1 ; reset count indices
STA count2
LoadBackgroundLoop:
JSR Rand ; run RNG
STA $2007 ; write tile to PPU
INC count1 ;Background is 32x30 ($20 x $1E) sprites, so use nested loops to fill 960 bytes
LDA count1
CMP #$E0
BNE LoadBackgroundLoop
INC count2
LDA #$00
STA count1
LDA count2
CMP #$01
BNE LoadBackgroundLoop
RTS
LDA $2002 ; read PPU status to reset the high/low latch
LDA #$20
STA $2006 ; write the high byte of $2000 address
LDA #$00
STA $2006 ; write the low byte of $2000 address
STA count1 ; reset count indices
STA count2
LoadBackgroundLoop:
JSR Rand ; run RNG
STA $2007 ; write tile to PPU
INC count1 ;Background is 32x30 ($20 x $1E) sprites, so use nested loops to fill 960 bytes
LDA count1
CMP #$E0
BNE LoadBackgroundLoop
INC count2
LDA #$00
STA count1
LDA count2
CMP #$01
BNE LoadBackgroundLoop
RTS
As you might note from the comment, I was originally doing a count compare for $20 and $1E, but I was getting a noticeable pattern repeat from the RNG. When I eliminate the nested loop, all of the tiles are random. I can't figure out what's resetting the RNG when I branch back to LoadBackgroundLoop after resetting count1. It's likely I'm not understanding what's going on in the RNG code. Any suggestions?
FYI, the seed var is set to #$01 prior to LoadBackground, which is only called once. And I'm using NESASMv3.1