This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

RAM dump for testing random number functions

RAM dump for testing random number functions
by on (#60760)
Simple question: what program or emulator will let me do a full memory dump, or preferably just a RAM dump, in a nice simple hex format such as this:

Code:
00 00 32 FA FA 45 6B 00 01 01 AF AF 6E 00 00 00


I want to be able to search it in notepad for repeated data.

This is because I am testing random number generating functions and I want to see how random the functions I'm testing actually are.

To make this thread a little more useful, here are some other threads I dug up on the topic of random numbers: here and here, and this one on generating an initial number based on player input.

The goal I'm working towards is writing a program that can generate a 32x32 maze like this at random:

Image

But I want it to only be random the first time I generate it. The idea is, there are multiple floors to this maze, and players can move up and down floors. Obviously we don't have the RAM to save every floor, but I'd be satisfied with just being able to generate any floor a second time using seed values.

I don't want there to be only 256 possible mazes, though. I'd like to be able to seed twice, once using the floor number and again using the original generated seed from player input, so that there can be 256 unique mazes per floor.

So far it seems like this routine posted by blargg will let me do exactly what I want, by setting rand_h and rand_l to different seeds:

Code:
; Requires two bytes in memory that don't get modified by anything else.
; They don't need to be next to each other. Set them to reseed the generator.
rand_h = $00
rand_l = $01

; Generate pseudo-random 8-bit value and return in A.
; Preserved: X, Y
random:
      ; See "linear-congruential random number generator" for more.
      ; rand = (rand * 5 + 0x3611) & 0xffff;
      ; return (rand >> 8) & 0xff;
      lda   rand_h      ; multiply by 5
      sta   temp
      lda   rand_l
      asl   a           ; rand = rand * 4 + rand
      rol   temp
      asl   a
      rol   temp
      clc
      adc   rand_l
      pha
      lda   temp
      adc   rand_h
      sta   rand_h
      pla               ; rand = rand + 0x3611
      clc
      adc   #$11
      sta   rand_l
      lda   rand_h
      adc   #$36
      sta   rand_h
      rts               ; return high 8 bits

by on (#60763)
If you have MSYS installed, then you have GNU Coreutils, so man od
Then to turn ramdump.bin from binary to a hex dump, use this:
od -A n -t x1 ramdump.bin

by on (#60771)
A good way to test a RNG is to make a loop that does something like that :
Code:
Loop
 jsr RandomNmr
 sta $4011
 jmp Loop


and listen to the result. If it sounds like true white noise, then it's a good RNG. If it sounds repeating often, then it's a bad RNG. Also the "pitch" of the white noise gives information about the CPU times it takes to compute a random number.

Well I've used this exact routine that Blargg posted for a while, and it is excellent. It only uses the A register, no X and Y which makes it very useful. It is pretty fast too, and generate numbers that looks truly random, even when the initial seed is just $0000. So I recommend this routine to you. However, it might have some inconvenient I can't think of.

by on (#60772)
FCEUX + Lua
Code:
file = io.open ("ramdump.txt","w")

for i=0,0x7FF do
   file:write(string.format("%02X ",memory.readbyte(i)))
end

file:close()


You could also hook writes to a specific memory location:
Code:
MAX_VALUES = 6000
num_values = 0

file = io.open ("rand.txt","w")

function memhook()
   if(num_values < MAX_VALUES) then
      file:write(string.format("%02X ",memory.getregister("a")))
      num_values = num_values + 1
   end
end

memory.register(0xFF,memhook)

while(num_values < MAX_VALUES) do
   emu.message(num_values.."/"..MAX_VALUES)
   emu.frameadvance()
end

file:close()


Just put your rand function in an endless loop and sta $FF its result. The script will log exactly MAX_VALUES writes to $FF.

by on (#60780)
Thanks, I will try these.

It never occurred to me to actually listen to the random numbers, that is very unique! I wanted to be able to look at the data but I will have to do that just for kicks now. :)

by on (#60789)
FCEUXD SP (Tools -> Hex Editor -> File -> Dump to file) has a RAM Dump option. Nintendulator has something that looks very similar to this, but I haven't managed to figure out how it works.

VirtuaNES also has a similar feature but it doesn't dump to a file -- it just dumps to a window where you can navigate around. This might be sufficient for you.

by on (#60791)
- RockNES.

by on (#60808)
Yes, writing to $4011 is a very novel idea, employing your ears as a pattern-recognition device, which they're excellent at. Hmmm, along the same lines, you could output it as a bitmap to the screen, and let your eyes look for patterns as well. If an 8K log is sufficient, just write it to battery RAM and examine the emulator's save file for it.