···11+SECTION "DungeonVariables", WRAM0
22+33+wDungeonGrid:: ds 512
44+wEntranceId:: db
55+wCurrentRoom:: db
66+wCurrentWidth:: db
77+wCurrentHeight:: db
+12-1
gb/src/main.asm
···11INCLUDE "hardware.inc/hardware.inc"
22 rev_Check_hardware_inc 4.0
3344+SECTION "GameVariables", WRAM0
55+66+randstate:: ds 4
77+48SECTION "Header", ROM0[$100]
59610 ; This is your ROM's entry point
···20242125EntryPoint:
2226 ; Here is where the fun begins, happy coding :)
2323- jr @2727+ ld b, 0
2828+ ld c, 10
2929+ call srand
3030+ call rand
3131+ jp Done
3232+3333+Done:
3434+ jp Done
+35
gb/src/utils.asm
···11+SECTION "Math", ROM0
22+;; From: https://github.com/pinobatch/libbet/blob/master/src/rand.z80#L34-L54
33+; Generates a pseudorandom 16-bit integer in BC
44+; using the LCG formula from cc65 rand():
55+; x[i + 1] = x[i] * 0x01010101 + 0xB3B3B3B3
66+; @return A=B=state bits 31-24 (which have the best entropy),
77+; C=state bits 23-16, HL trashed
88+rand::
99+ ; Add 0xB3 then multiply by 0x01010101
1010+ ld hl, randstate+0
1111+ ld a, [hl]
1212+ add a, $B3
1313+ ld [hl+], a
1414+ adc a, [hl]
1515+ ld [hl+], a
1616+ adc a, [hl]
1717+ ld [hl+], a
1818+ ld c, a
1919+ adc a, [hl]
2020+ ld [hl], a
2121+ ld b, a
2222+ ret
2323+2424+; Sets the random seed to BC.
2525+; C expects startup code to behave as if srand(1) was called.
2626+; AHL trashed
2727+srand::
2828+ ld hl,randstate+3
2929+ xor a
3030+ ld [hl-],a
3131+ ld [hl-],a
3232+ ld a,b
3333+ ld [hl-],a
3434+ ld [hl],c
3535+ ret