From 8f9ed6e585a361e957650b871fc56a0406c2dac8 Mon Sep 17 00:00:00 2001 From: YamaArashi Date: Thu, 2 Feb 2017 16:30:08 -0800 Subject: [PATCH] decompile rng --- asm/contest.s | 6 ++-- asm/contest_link_80FC4F4.s | 4 +-- asm/rng.s | 65 -------------------------------------- asm/rom_8034C54.s | 4 +-- asm/rom_8184DA4.s | 4 +-- asm/rom_81BAD84.s | 4 +-- include/rng.h | 18 +++++++++++ ld_script.txt | 2 +- src/rng.c | 35 ++++++++++++++++++++ sym_common.txt | 4 +-- sym_ewram.txt | 7 ++-- 11 files changed, 69 insertions(+), 84 deletions(-) delete mode 100644 asm/rng.s create mode 100644 include/rng.h create mode 100644 src/rng.c diff --git a/asm/contest.s b/asm/contest.s index 66da30fc3..d7440df0c 100644 --- a/asm/contest.s +++ b/asm/contest.s @@ -850,7 +850,7 @@ sub_80D7DE8: @ 80D7DE8 adds r0, r5 ldr r1, =sub_80D80C8 str r1, [r0] - ldr r1, =gUnknown_03005D80 + ldr r1, =gRngValue ldr r0, =gUnknown_030060B8 ldr r0, [r0] str r0, [r1] @@ -2191,7 +2191,7 @@ sub_80D8A88: @ 80D8A88 strb r0, [r1, 0x10] ldr r0, [r2] ldr r1, [r0] - ldr r0, =gUnknown_03005D80 + ldr r0, =gRngValue ldr r0, [r0] str r0, [r1, 0x18] ldr r0, =gUnknown_02039F2A @@ -5369,7 +5369,7 @@ _080DA65C: bl sub_80DF750 _080DA668: ldr r1, =gUnknown_030060B8 - ldr r0, =gUnknown_03005D80 + ldr r0, =gRngValue ldr r0, [r0] str r0, [r1] ldr r4, =gStringVar4 diff --git a/asm/contest_link_80FC4F4.s b/asm/contest_link_80FC4F4.s index ea9ebe2c8..be38d0d16 100644 --- a/asm/contest_link_80FC4F4.s +++ b/asm/contest_link_80FC4F4.s @@ -416,7 +416,7 @@ _080FC830: lsls r0, 24 cmp r0, 0 beq _080FC880 - ldr r0, =gUnknown_03005D80 + ldr r0, =gRngValue movs r1, 0x4 bl sub_80FC4F4 cmp r0, 0x1 @@ -429,7 +429,7 @@ _080FC858: lsls r0, 24 cmp r0, 0 beq _080FC880 - ldr r0, =gUnknown_03005D80 + ldr r0, =gRngValue ldr r4, =gUnknown_020223C4 adds r1, r4, 0 movs r2, 0x4 diff --git a/asm/rng.s b/asm/rng.s deleted file mode 100644 index a408b139c..000000000 --- a/asm/rng.s +++ /dev/null @@ -1,65 +0,0 @@ - .include "asm/macros.inc" - .include "constants/constants.inc" - - .syntax unified - - .text - - thumb_func_start Random -@ u16 GenerateRandomNumber() -Random: @ 806F5CC - ldr r2, =gUnknown_03005D80 - ldr r1, [r2] - ldr r0, =0x41c64e6d - muls r0, r1 - ldr r1, =0x00006073 - adds r0, r1 - str r0, [r2] - ldr r2, =gUnknown_020249C0 - ldr r1, [r2] - adds r1, 0x1 - str r1, [r2] - lsrs r0, 16 - bx lr - .pool - thumb_func_end Random - - thumb_func_start SeedRng -@ void SeedRng(u16 seed) -SeedRng: @ 806F5F8 - lsls r0, 16 - lsrs r0, 16 - ldr r1, =gUnknown_03005D80 - str r0, [r1] - ldr r1, =gUnknown_020249BC - movs r0, 0 - strb r0, [r1] - bx lr - .pool - thumb_func_end SeedRng - - thumb_func_start SeedRng2 -SeedRng2: @ 806F610 - lsls r0, 16 - lsrs r0, 16 - ldr r1, =gUnknown_03005D84 - str r0, [r1] - bx lr - .pool - thumb_func_end SeedRng2 - - thumb_func_start Random2 -Random2: @ 806F620 - ldr r2, =gUnknown_03005D84 - ldr r1, [r2] - ldr r0, =0x41c64e6d - muls r0, r1 - ldr r1, =0x00006073 - adds r0, r1 - str r0, [r2] - lsrs r0, 16 - bx lr - .pool - thumb_func_end Random2 - - .align 2, 0 @ Don't pad with nop. diff --git a/asm/rom_8034C54.s b/asm/rom_8034C54.s index 3e4ceeaf8..e2cd2a933 100644 --- a/asm/rom_8034C54.s +++ b/asm/rom_8034C54.s @@ -13834,7 +13834,7 @@ _0803C628: ldrb r0, [r0] cmp r0, 0 bne _0803C690 - ldr r1, =gUnknown_03005D80 + ldr r1, =gRngValue ldr r0, =gUnknown_0203BD30 ldr r0, [r0] str r0, [r1] @@ -71061,7 +71061,7 @@ sub_805C158: @ 805C158 cmp r0, 0 bne _0805C194 ldr r1, =gUnknown_0203BD30 - ldr r0, =gUnknown_03005D80 + ldr r0, =gRngValue ldr r0, [r0] str r0, [r1] bl sub_805D19C diff --git a/asm/rom_8184DA4.s b/asm/rom_8184DA4.s index 626e3ecb9..2674198c5 100644 --- a/asm/rom_8184DA4.s +++ b/asm/rom_8184DA4.s @@ -96,7 +96,7 @@ sub_8184E58: @ 8184E58 cmp r0, 0x1 bne _08184EA0 ldr r1, =gUnknown_0203BD2C - ldr r0, =gUnknown_03005D80 + ldr r0, =gRngValue ldr r0, [r0] str r0, [r1] ldr r4, =gUnknown_0203C7AE @@ -111,7 +111,7 @@ sub_8184E58: @ 8184E58 _08184EA0: cmp r0, 0x2 bne _08184EAC - ldr r0, =gUnknown_03005D80 + ldr r0, =gRngValue ldr r1, =gUnknown_0203BD2C ldr r1, [r1] str r1, [r0] diff --git a/asm/rom_81BAD84.s b/asm/rom_81BAD84.s index 64d41a2bb..26d45ae7e 100644 --- a/asm/rom_81BAD84.s +++ b/asm/rom_81BAD84.s @@ -60550,7 +60550,7 @@ _081DA284: lsls r0, 24 cmp r0, 0 beq _081DA2D4 - ldr r0, =gUnknown_03005D80 + ldr r0, =gRngValue movs r1, 0x4 bl sub_80FC4F4 cmp r0, 0x1 @@ -60563,7 +60563,7 @@ _081DA2AC: lsls r0, 24 cmp r0, 0 beq _081DA2D4 - ldr r0, =gUnknown_03005D80 + ldr r0, =gRngValue ldr r4, =gUnknown_020223C4 adds r1, r4, 0 movs r2, 0x4 diff --git a/include/rng.h b/include/rng.h new file mode 100644 index 000000000..ae98e575f --- /dev/null +++ b/include/rng.h @@ -0,0 +1,18 @@ +#ifndef GUARD_RNG_H +#define GUARD_RNG_H + +extern u32 gRngValue; +extern u32 gRng2Value; + +//Returns a 16-bit pseudorandom number +u16 Random(void); +u16 Random2(void); + +//Returns a 32-bit pseudorandom number +#define Random32() (Random() | (Random() << 16)) + +//Sets the initial seed value of the pseudorandom number generator +void SeedRng(u16 seed); +void SeedRng2(u16 seed); + +#endif // GUARD_RNG_H diff --git a/ld_script.txt b/ld_script.txt index 0fe19328f..05f72c340 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -60,7 +60,7 @@ SECTIONS { asm/pokemon_2.o(.text); asm/pokemon_3.o(.text); asm/trig.o(.text); - asm/rng.o(.text); + src/rng.o(.text); asm/util.o(.text); asm/blend_palette.o(.text); asm/daycare.o(.text); diff --git a/src/rng.c b/src/rng.c new file mode 100644 index 000000000..9e473ef62 --- /dev/null +++ b/src/rng.c @@ -0,0 +1,35 @@ +#include "global.h" +#include "rng.h" + +// The number 1103515245 comes from the example implementation of rand and srand +// in the ISO C standard. + +extern u32 gRngValue; +extern u32 gRng2Value; + +EWRAM_DATA u8 sUnknown = 0; +EWRAM_DATA u32 sRandCount = 0; + +u16 Random() +{ + gRngValue = 1103515245 * gRngValue + 24691; + sRandCount++; + return gRngValue >> 16; +} + +void SeedRng(u16 seed) +{ + gRngValue = seed; + sUnknown = 0; +} + +void SeedRng2(u16 seed) +{ + gRng2Value = seed; +} + +u16 Random2(void) +{ + gRng2Value = 1103515245 * gRng2Value + 24691; + return gRng2Value >> 16; +} diff --git a/sym_common.txt b/sym_common.txt index 03d6a758c..19935ff3c 100644 --- a/sym_common.txt +++ b/sym_common.txt @@ -267,10 +267,10 @@ gUnknown_03005D78: @ 3005D78 gUnknown_03005D7C: @ 3005D7C .space 0x4 -gUnknown_03005D80: @ 3005D80 +gRngValue: @ 3005D80 .space 0x4 -gUnknown_03005D84: @ 3005D84 +gRng2Value: @ 3005D84 .space 0x4 gUnknown_03005D88: @ 3005D88 diff --git a/sym_ewram.txt b/sym_ewram.txt index ed7833558..a096d6bfc 100644 --- a/sym_ewram.txt +++ b/sym_ewram.txt @@ -699,12 +699,9 @@ gUnknown_0202499C: @ 202499C gUnknown_020249B4: @ 20249B4 .space 0x8 -gUnknown_020249BC: @ 20249BC - .space 0x4 - -gUnknown_020249C0: @ 20249C0 - .space 0x4 + .include "src/rng.o" + .align 2 gUnknown_020249C4: @ 20249C4 .space 0x64