libdragon
libdragon copied to clipboard
Add an internal unified PRNG for libdragon itself
I was skimming through the source code and noticed an abundance of static declared pseudo random functions, macros and snippets. src/audio/ay8910.c src/system.c src/fatfs/ff.c src/bb/bbfs.c src/joybus/cpak.c src/audio/libxm/play.c ...
Why is it like this; why not standardise it?
I also suggest this for seed generation:
uint32_t random_reg, count_reg;
asm volatile (
"mfc0 %0, $1 \n" // 5-bit unsigned counter, decrements every time a instruction is executed.
"mfc0 %1, $9 \n" // 32-bit unsigned counter, increments every half pipeline cycle.
: "=r" (random_reg), "=r" (count_reg)
);
uint32_t seed = (random_reg << 27) ^ count_reg;
(Original by Kaze Emanuar)