From 7aa0760a27980453400814e65de62d811a8fc722 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Fri, 29 Nov 2024 06:00:01 +0100 Subject: [PATCH] Added basic 'rng' function --- src/main.s | 2 ++ src/rand.s | 39 +++++++++++++++++++++++++++++++++++++++ src/wram.s | 4 ++++ 3 files changed, 45 insertions(+) create mode 100644 src/rand.s diff --git a/src/main.s b/src/main.s index edf2f16..9c450bd 100644 --- a/src/main.s +++ b/src/main.s @@ -18,6 +18,7 @@ entry: call lcd_off call video_init call audio_init + call rand_init call lcd_on call vblank_wait @@ -46,6 +47,7 @@ main: jr @forever REL #endif +#include "rand.s" #include "strings.s" #include "video.s" #include "mem.s" diff --git a/src/rand.s b/src/rand.s new file mode 100644 index 0000000..067d54e --- /dev/null +++ b/src/rand.s @@ -0,0 +1,39 @@ + ; inits the random seed +rand_init: + xor a, a + ld a, [srand] + ld a, [srand+1] + ret + + +#define rand_table_end (rand_table + 512) + ; gets a pseudo-random number + ; and advances to the next seed + ; uses: + ; hl, a, b + ; returns: + ; a: random value +rand: + ld a, [srand+1] ; low value + ld l, a ; load into low + ld a, [srand] ; high value + ld h, a ; load into high + + ; store back + + inc hl ; srand++ + ld a, l + ld [srand+1], a + ld a, h + and a, ((rand_table_end - rand_table - 1) >> 8); overflow at max size + ld [srand], a + + ld a, [hl] ; next value + ret + + ; table of "rng" values + ; currently this will literally just read the rom following this + ; label treating the code as pseudo random numbers + ; TODO: genrate better rng table +rand_table: + diff --git a/src/wram.s b/src/wram.s index 5e30e8d..f7f36fe 100644 --- a/src/wram.s +++ b/src/wram.s @@ -143,6 +143,10 @@ game_over_timer: .adv 1 game_mode: .adv 1 +#define SRAND_LEN 512 + ; seed for the rng + ; 16 bit signed int +srand: .adv 2 #define WHO_PLAYER 0xFF ; who is currently in control of the game -- 2.30.2