Random Background Generation Partial Load

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
mathe-matician
Posts: 14
Joined: Sat Aug 24, 2019 2:21 pm
Contact:

Random Background Generation Partial Load

Post by mathe-matician »

Hey all,

I'm trying to load a random background of 8x8 tiles. I first get a random number by the amount of time it takes the player to press start on the start screen by adding to the seed:

Code: Select all

Start_Controls:
	LDA seed2
	CLC
	ADC #$01
	STA seed2
	LDA seed2+1
	CLC
	ADC #$03
	STA seed2+1
	LDA buttons1
	AND #%00010000
	BEQ Start_ControlsDone
I'm then trying to load these tiles randomly 1 by 1 to $6000 by:

Code: Select all

	LDA #$00
	STA Point+0
	STA Point+1
BGTORAM:
	LDA $6000	
	ASL
	TAY

	LDA bg_point+0,y
	STA Point+0
	LDA bg_point+1,y
	STA Point+1
	
	LDX #$04
	LDY #$00
	TYA
	PHA
PRNG_MAP:	
	LDY #$08	
	LDA seed2+0
One:
	ASL
	ROL seed2+1
	BCC Two
	EOR #$39	
Two:	
	DEY
	BNE One
	STA seed2+0
	CMP #$00

	LDA seed2
	CMP #$0C
	BCC LoadRock		; if seed2 < 12:
	CMP #$AA
	BCC LoadGrass		;if seed2 < 170 and is greater than 12
	CMP #$FF
	BCC LoadTree		;if seed2 < 255 and is greater than 170

	JMP BGDone
LoadRock:
	PLA
	TAY
	LDA #$3B		;Rock tile
	STA (Point), y
	JMP LoadCycle
LoadGrass:
	PLA
	TAY
	LDA #$00		;Grass tile
	STA (Point), y
	JMP LoadCycle
LoadTree:
	PLA
	TAY
	LDA #$04		;Tree tile
	STA (Point), y
LoadCycle:
	INY
	TYA
	PHA
	CPY #$00
	BNE PRNG_MAP
	INC Point+1
	DEX
	BNE PRNG_MAP
BGDone:	
With bg_point being:

Code: Select all

bg_point:
	.dw $6000
This will sometimes load the entire background, sometimes only a quarter or half and sometimes only 1 line of background tiles. When I look in the debugger $6000 isn't actually getting filled all the way - or at least is getting filled with zeros after a certain amount of time.

Does this have to do with my "random" seed where it is getting set to 0 at some point? Or is my PRNG_MAP routine funky? Any suggestions would be great!
Attachments
lumber_update17.nes
(24.02 KiB) Downloaded 119 times
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Random Background Generation Partial Load

Post by Quietust »

For some reason, I'm not seeing any randomness - every time I reset and press Start (no matter how long I wait between them), I get the exact same layout.

Of course, in order to even get the ROM to work, I had to set the Battery flag in the .NES header, since the emulator I use doesn't map RAM at $6000-$7FFF unless it's for a mapper that actually supports expansion RAM in the first place (and mapper 0 only supports it with a battery so it can handle Family Basic).

Edit: tracing through the ROM, I've run into a number of other things which seem strange:
  1. you enable NMIs immediately after reset, and you also have loops which poll for VBLANK - these will conflict with each other
  2. Your code which fills random data into $6000 is happening before I pressed Start. I'm guessing this is a result of #1 above (and is why I'm not seeing any randomness)
Edit #2: I've found out why it's aborting early - once the RNG produces the number $FF, it completely exits out of the loop (since the condition for LoadTree only covers 170-254).
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
mathe-matician
Posts: 14
Joined: Sat Aug 24, 2019 2:21 pm
Contact:

Re: Random Background Generation Partial Load

Post by mathe-matician »

Quietust wrote: Thu Mar 26, 2020 2:22 pm For some reason, I'm not seeing any randomness - every time I reset and press Start (no matter how long I wait between them), I get the exact same layout.

Of course, in order to even get the ROM to work, I had to set the Battery flag in the .NES header, since the emulator I use doesn't map RAM at $6000-$7FFF unless it's for a mapper that actually supports expansion RAM in the first place (and mapper 0 only supports it with a battery so it can handle Family Basic).

Edit: tracing through the ROM, I've run into a number of other things which seem strange:
  1. you enable NMIs immediately after reset, and you also have loops which poll for VBLANK - these will conflict with each other
  2. Your code which fills random data into $6000 is happening before I pressed Start. I'm guessing this is a result of #1 above (and is why I'm not seeing any randomness)
Edit #2: I've found out why it's aborting early - once the RNG produces the number $FF, it completely exits out of the loop (since the condition for LoadTree only covers 170-254).
Yes! It works now! Thank you Quietust.
Post Reply