From fc6a230af73d3689062ede031cbeef20b6e5d7f0 Mon Sep 17 00:00:00 2001 From: Luxferre Date: Mon, 12 Jan 2026 10:55:03 +0200 Subject: [PATCH] entropy management for NES port --- nes/eznes.h | 25 +++++++++++++++++-------- nes/nescoundrel.c | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/nes/eznes.h b/nes/eznes.h index d79301a..5a0db45 100644 --- a/nes/eznes.h +++ b/nes/eznes.h @@ -40,6 +40,13 @@ #define NES_GREEN 0x28 #define NES_YELLOW 0x2A +/* RNG stuff */ +static u16 _ez_entropy = 0; + +u16 ezrand() { + return ((u16) rand()) ^ _ez_entropy; +} + /* Graphics section */ void ppu_off() { @@ -58,6 +65,7 @@ u8 joy_input_wait() { while(joy_read(JOY_1)); /* wait for a new press */ while(1) { + ++_ez_entropy; joy = joy_read(JOY_1); pressed = joy & ~last_joy; if(pressed & JOY_UP_MASK) return EZJOY_UP; @@ -141,9 +149,6 @@ void init_sound() { /* Game initialization section */ void init_game(u8 bg, u8 c1, u8 c2, u8 c3, u8 c4) { -#ifdef _randomize - _randomize(); -#endif init_sound(); bgcolor(COLOR_BLACK); textcolor(0); @@ -156,6 +161,7 @@ void init_game(u8 bg, u8 c1, u8 c2, u8 c3, u8 c4) { void wait_for_start() { u8 joy; while(1) { + ++_ez_entropy; joy = joy_read(JOY_1); if(JOY_START(joy)) break; waitvsync(); @@ -165,14 +171,17 @@ void wait_for_start() { /* randomize until Start is pressed */ void wait_for_start_rand() { u8 joy; - u16 entropy = rand(); +#ifdef _randomize + _randomize(); +#endif + _ez_entropy = rand(); while(1) { - ++entropy; + ++_ez_entropy; joy = joy_read(JOY_1); if(JOY_START(joy)) break; waitvsync(); } - srand(entropy); + srand(_ez_entropy); } /* Generic helper methods */ @@ -181,8 +190,8 @@ void wait_for_start_rand() { void shuffle(s8 *array, u16 n) { u16 i, j, t; if(n > 1) { - for(i=n-1;i>0;i--) { - j = (u16) (rand()%(i+1)); + for(i=n-1;i>0;--i) { + j = (u16) (ezrand()%(i+1)); t = array[j]; array[j] = array[i]; array[i] = t; diff --git a/nes/nescoundrel.c b/nes/nescoundrel.c index 923a2c5..1bdb99a 100644 --- a/nes/nescoundrel.c +++ b/nes/nescoundrel.c @@ -102,7 +102,7 @@ void win_screen(s16 score) { ppu_off(); for(j=0;j<12;j+=4) for(i=3;i<32;i+=4) - colorxy(i, j, 1 + (rand() % 3)); + colorxy(i, j, 1 + (ezrand() % 3)); cputsxy(0, 3, " ___ ___ ___ ______ \r\n" \ " | Y | | _ \\ \r\n" \ " |. | |. |. | |\r\n" \