Add an awesome random number generator made by lua :3

Co-authored-by: Lua MacDougall <lua@foxgirl.dev>
This commit is contained in:
Ry 2022-07-26 22:53:18 -07:00
parent 3824682ab0
commit 09e27bac54
3 changed files with 65 additions and 3 deletions

View File

@ -84,6 +84,10 @@ string_to_int: jmp [0xF0047000]
play_audio: jmp [0xF0048000] play_audio: jmp [0xF0048000]
stop_audio: jmp [0xF0048004] stop_audio: jmp [0xF0048004]
; random number jump table
random: jmp [0xF0049000]
random_range: jmp [0xF0049004]
; event types ; event types
const EVENT_TYPE_MOUSE_CLICK: 0x00000000 const EVENT_TYPE_MOUSE_CLICK: 0x00000000
const EVENT_TYPE_MOUSE_RELEASE: 0x00000001 const EVENT_TYPE_MOUSE_RELEASE: 0x00000001

View File

@ -14,12 +14,25 @@ const TEXT_COLOR: 0xFFFFFFFF
entry: entry:
mov rsp, SYSTEM_STACK mov rsp, SYSTEM_STACK
mov [0x000003FC], system_vsync_handler
; disable audio playback ; disable audio playback
mov r0, 0x80000600 mov r0, 0x80000600
out r0, 0 out r0, 0
; seed the random number generator
entry_seed:
mov [0x000003FC], entry_seed_done
ise
mov r0, 2166136261
entry_seed_loop:
mul r0, 16777619
rjmp entry_seed_loop
entry_seed_done:
mov [RANDOM_STATE], r0
; set up the vsync interrupt
mov [0x000003FC], system_vsync_handler
ise
; clear the interrupt vector for interrupt 0xFE - audio buffer switch ; clear the interrupt vector for interrupt 0xFE - audio buffer switch
mov [0x000003F8], 0x00000000 mov [0x000003F8], 0x00000000
@ -53,7 +66,6 @@ draw_startup_text:
mov r12, FOX32ROM_VERSION_PATCH mov r12, FOX32ROM_VERSION_PATCH
call draw_format_str_to_background call draw_format_str_to_background
ise
event_loop: event_loop:
call get_next_event call get_next_event
@ -132,6 +144,7 @@ get_rom_version:
#include "mouse.asm" #include "mouse.asm"
#include "overlay.asm" #include "overlay.asm"
#include "panic.asm" #include "panic.asm"
#include "random.asm"
#include "ryfs.asm" #include "ryfs.asm"
#include "string.asm" #include "string.asm"
#include "vsync.asm" #include "vsync.asm"
@ -235,6 +248,11 @@ get_rom_version:
data.32 play_audio data.32 play_audio
data.32 stop_audio data.32 stop_audio
; random number jump table
org.pad 0xF0049000
data.32 random
data.32 random_range
org.pad 0xF004F000 org.pad 0xF004F000
standard_font_width: standard_font_width:
data.16 8 data.16 8

40
random.asm Normal file
View File

@ -0,0 +1,40 @@
; random number generation routines
; by lua :3 - 2022 https://foxgirl.dev/
const RANDOM_STATE: 0x0228218E ; 4 bytes
; generate a random number
; inputs:
; none
; outputs:
; r0: random number
random:
push r1
mov r0, [RANDOM_STATE]
mov r1, r0
sla r1, 13
xor r0, r1
mov r1, r0
srl r1, 17
xor r0, r1
mov r1, r0
sla r1, 5
xor r0, r1
mov [RANDOM_STATE], r0
pop r1
ret
; generate a random number in the range [r1, r2)
; inputs:
; r1: minimum value, inclusive
; r2: maximum value, exclusive
; outputs:
; r0: random number
random_range:
call random
sub r2, r1
rem r0, r2
add r0, r1
ret