Added basic 'rng' function
authorLukas Krickl <lukas@krickl.dev>
Fri, 29 Nov 2024 05:00:01 +0000 (06:00 +0100)
committerLukas Krickl <lukas@krickl.dev>
Fri, 29 Nov 2024 05:00:01 +0000 (06:00 +0100)
src/main.s
src/rand.s [new file with mode: 0644]
src/wram.s

index edf2f160df220a03408d4486b695787763a2c178..9c450bdfc78ecdd7d9237526a9db1db849f1b8dd 100644 (file)
@@ -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 (file)
index 0000000..067d54e
--- /dev/null
@@ -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:
+
index 5e30e8d0c3f3d25251153bf908df4676509012f7..f7f36fe6de475918f423daf150330345ee4d6153 100644 (file)
@@ -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