No-computation "RNG" by baking random numbers into the ROM

Discussion of hardware and software development for Super NES and Super Famicom.

Moderator: Moderators

Forum rules
  • For making cartridges of your Super NES games, see Reproduction.
Sik
Posts: 1589
Joined: Thu Aug 12, 2010 3:43 am

Re: No-computation "RNG" by baking random numbers into the R

Post by Sik » Sun May 31, 2015 10:15 am

byuu wrote:The SNES' H/V counter at reset is deterministic.
So the S-CPU and the S-PPU are synchronized? (otherwise where would be at least a bit of variance)

Is RAM still undefined on power on or is it always filled with FF or what? Same question for video RAM, for that matter (you can read back from it during blanking, right?).

tepples
Posts: 22281
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: No-computation "RNG" by baking random numbers into the R

Post by tepples » Sun May 31, 2015 11:09 am

In the previous topic about the Pretendo demo, we found a couple entropy sources in the NES to set the PRNG's seed. One is readback of uninitialized OAM, which is 27*64=1728 bits of DRAM, but that doesn't work reliably on pre-2C02G PPUs. Another is the analog result of bus conflicts inside the PPU when trying to read back from VRAM while rendering is turned on, which is what Pretendo uses. I don't know which also apply to the Super NES.

Another that will probably apply more cleanly to the Super NES is reading the controller dozens of times per frame. This can be done "in the background" with a scanline interrupt, such as the NES's MMC3 IRQ or the Super NES's VTIME IRQ, with a programmable cycle timer such as the NES's FME-7 IRQ, or by (ab)using the NES DMC as a timer. The latter produces a 4.2 kHz timebase (roughly one tick per 3.8 lines) during which player 1 A and Start can be read in a DMC IRQ handler. Fortunately, autoreading isn't the only way to read a Super NES controller, as bit banging ultraslow ports $4016/$4017 still works the same way as it did on the NES.

hackfresh
Posts: 100
Joined: Sun May 03, 2015 8:19 pm

Re: No-computation "RNG" by baking random numbers into the R

Post by hackfresh » Tue Jun 02, 2015 1:59 am

Tecmo super bowl generates 3 psuedo-random numbers every frame by using prime numbers. It stores them in 3B,3C,3D. It adds the prime number to itself once a frame.

Code: Select all

update_randoms:

$3B= random 1
$3C= random 2
$3D= random 3

LDA *$3B                    ; random1 += 131
CLC                            
ADC #$83                   
STA *$3B                     
LDA  *3C                    ; random2 += 13
ADC #$0D
STA *3C
LDA  *3D                   ; random 3 += 17  
ADC #$11
STA  *3D
RTS
There are also functions to update each random number individually as well as other routines to make the number "more random" since sometimes you need to use multiple random numbers in the same frame.

Code: Select all

L_D8F7 (MORE RANDOM) 
	JSR update_randoms
	LDA *$3B
	AND #$03
	BEQ @Loop3
	CMP #$01
	BEQ @Loop2
	CMP ##$02
	BEQ @Loop1
@Loop0:
	LDA *$3D
	RTS
@Loop1:
	LDA *$3C
	RTS
@Loop2:
	LDA *$3D
	CLC
	ADC *$3C
	RTS
@Loop3:
	LDA *$3D
	CLC
	ADC  *$3C
	ADC *$3B
	RTS

Code: Select all

L_12_9F83:		(this routine is used often in the part of the game that simulates game scores and season stats in a skip mode)				
	LDA $3B						
	ADC $3B						
	CLC						
	ADC $3B							
	CLC						
	ADC #$11					
	STA $3B						
	LDA $3C						
	ADC $3C						
	CLC						
	ADC $3C						
	CLC						
	ADC $3C						
	CLC						
	ADC $3C						
	CLC						
	ADC #$13					
	STA $3C						
	LDA $3D						
	ADC $3D						
	CLC						
	ADC $3D						
	CLC						
	ADC $3D						
	CLC						
	ADC $3D						
	CLC						
	ADC $3D						
	CLC						
	ADC $3D						
	CLC						
	ADC #$AD					
	STA $3D					
	RTS	

Post Reply