; inits the random seed
rand_init:
- xor a, a
- ld a, [srand]
- ld a, [srand+1]
+ ld a, SRAND_INITIAL
+ ld [srand], a
ret
-#define rand_table_end (rand_table + 512)
; gets a pseudo-random number
; and advances to the next seed
; uses:
- ; hl, a, b, de
+ ; a, hl, de
; 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
+ ld a, [srand]
+ cp a, (rand_table_end - rand_table)
+ jr nz, @not_end REL
+ ld a, 0
+@not_end:
+
+ ld d, 0
+ ld e, a ; de = offset into table
- 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 de, rand_table
+ ld hl, rand_table
add hl, de
- ld a, [hl] ; next value
+ inc a
+ ld [srand], a ; srand++
+
+ ld a, [hl]
+
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:
-
+.db 0x09, 0xd7, 0xf4, 0xb1, 0x77, 0x61, 0xdd, 0x0e, 0xee
+.db 0x21, 0x6e, 0x86, 0x44, 0x49, 0xc5, 0x89, 0x52, 0x76
+.db 0x1b, 0x92, 0x90, 0x6b, 0x2b, 0xcb, 0x97, 0xae, 0x11
+.db 0x1f, 0xb6, 0x5b, 0x95, 0x5a, 0x05, 0xfb, 0x01, 0x19
+.db 0x0d, 0xd8, 0xdc, 0x6f, 0xc6, 0x41, 0x25, 0x68, 0x16
+.db 0x50, 0x82, 0x2f, 0x6a, 0x7e, 0x57, 0xa9, 0x9c, 0x27
+.db 0xbd, 0xb8, 0x60, 0xa8, 0xfa, 0x32, 0xf7, 0xe2, 0xde
+.db 0xa2, 0xda, 0xf3, 0x67, 0x7c, 0x46, 0x4f, 0xea, 0xa5
+.db 0x9e, 0x1c, 0x43, 0xcd, 0xd2, 0x1a, 0x71, 0x7a, 0xc3
+.db 0x8b, 0x4e, 0xa0, 0xa6, 0xf9, 0x33, 0xca, 0x59, 0x30
+.db 0xaa, 0xb2, 0x42, 0x91, 0xf8, 0x47, 0x24, 0x3c, 0xa4
+.db 0xe8, 0x5e, 0xff, 0xcf, 0x8d, 0xd6, 0x9a, 0x15, 0x58
+.db 0x22, 0xfe, 0x3e, 0x2d, 0xd5, 0x9f, 0x4b, 0x07, 0x7f
+.db 0x88, 0x14, 0x6d, 0xb9, 0x02, 0x29, 0xab, 0x38, 0x37
+.db 0x17, 0x6c, 0x64, 0x18, 0xe4, 0x2c, 0x56, 0xd4, 0x31
+.db 0x0f, 0xa7, 0xd0, 0xe7, 0x74, 0xc1, 0xd1, 0xb5, 0xaf
+.db 0x65, 0xf2, 0xba, 0xa1, 0x99, 0xe0, 0x69, 0x75, 0x10
+.db 0x3a, 0x06, 0x9d, 0xe3, 0x7b, 0xeb, 0x9b, 0xe1, 0x81
+.db 0x20, 0xf5, 0xe9, 0x5f, 0xe6, 0xdf, 0xa3, 0x2a, 0x35
+.db 0xd9, 0xf6, 0xdb, 0x26, 0x72, 0x79, 0x36, 0x96, 0x3b
+.db 0xb4, 0xe5, 0x04, 0x8f, 0x8a, 0xcc, 0xbe, 0xbc, 0x78
+.db 0x2e, 0x3f, 0x1e, 0x84, 0x4c, 0xf0, 0x7d, 0x28, 0x03
+.db 0xf1, 0x53, 0x8c, 0x0a, 0xb0, 0x3d, 0x94, 0xc9, 0x4a
+.db 0x93, 0x45, 0x0b, 0xc8, 0x83, 0x4d, 0xbf, 0x5d, 0x34
+.db 0xef, 0xd3, 0xfd, 0x98, 0x51, 0x54, 0x66, 0x40, 0xce
+.db 0xc7, 0x12, 0x80, 0xfc, 0x48, 0xbb, 0x55, 0xec, 0x00
+.db 0xc2, 0x13, 0x39, 0x87, 0xc4, 0x0c, 0xc0, 0xad, 0x1d
+.db 0x63, 0x23, 0xac, 0x5c, 0x8e, 0xed, 0xb3, 0x85, 0x08
+.db 0x73, 0x70, 0x62
+rand_table_end: