mirror of
https://github.com/hsoft/collapseos.git
synced 2025-04-04 13:38:39 +11:00
Implement RNG in kernel and basic dice app to demonstrate it
This commit is contained in:
parent
28a0b84321
commit
fdce67f8ba
6
apps/dice/glue.asm
Normal file
6
apps/dice/glue.asm
Normal file
@ -0,0 +1,6 @@
|
||||
.inc "user.h"
|
||||
|
||||
.org USER_CODE
|
||||
jp diceMain
|
||||
.equ DICE_RAMSTART USER_RAMSTART
|
||||
.inc "dice/main.asm"
|
46
apps/dice/main.asm
Normal file
46
apps/dice/main.asm
Normal file
@ -0,0 +1,46 @@
|
||||
; dice - A simple RNG application
|
||||
; Generates a random number in the range of 0000-FFFF
|
||||
; Perhaps would be handy to make more user-friendly in the-future
|
||||
; (and to use entropy). Right now, more proof-of-concept of RNG functionality
|
||||
; than anything.
|
||||
|
||||
.equ SEED DICE_RAMSTART
|
||||
|
||||
diceMain:
|
||||
ld hl, sInputSeed
|
||||
call printstr
|
||||
call stdioReadLine
|
||||
call printcrlf
|
||||
xor a ; ensure C is not set
|
||||
call parseHexPair
|
||||
jp c, unknownSeedInput
|
||||
ld (SEED), a
|
||||
call parseHexPair
|
||||
jp c, unknownSeedInput
|
||||
ld (SEED+1), a
|
||||
ld hl, (SEED)
|
||||
.diceMainLoop:
|
||||
call stdioReadLine
|
||||
ld a, (hl)
|
||||
cp 'q'
|
||||
jp z, diceExit
|
||||
ld hl, (SEED)
|
||||
call rnd
|
||||
ld (SEED), hl
|
||||
call printHexPair
|
||||
call printcrlf
|
||||
jp .diceMainLoop
|
||||
|
||||
unknownSeedInput:
|
||||
ld a, '?'
|
||||
call stdioPutC
|
||||
call printcrlf
|
||||
jp diceMain
|
||||
|
||||
diceExit:
|
||||
call printcrlf
|
||||
xor a
|
||||
ret
|
||||
|
||||
sInputSeed:
|
||||
.db "Please input seed in hex (0000-FFFF):", 0
|
29
kernel/random.asm
Normal file
29
kernel/random.asm
Normal file
@ -0,0 +1,29 @@
|
||||
; Part of kernel because ideally we have some sort of entropy. For now,
|
||||
; just having it be user-provided for prototypical purposes.
|
||||
; Implementation by John Metcalf of
|
||||
; http://www.retroprogramming.com/2017/07/xorshift-pseudorandom-numbers-in-z80.html
|
||||
; (Slightly altered for CollapseOS)
|
||||
;
|
||||
; 16-bit xorshift pseudorandom number generator
|
||||
;
|
||||
; in: hl = seed
|
||||
; out: hl = pseudorandom number (generally fed into the next call)
|
||||
; corrupts a
|
||||
|
||||
rnd:
|
||||
ld a,h
|
||||
rra
|
||||
ld a,l
|
||||
rra
|
||||
xor h
|
||||
ld h,a
|
||||
ld a,l
|
||||
rra
|
||||
ld a,h
|
||||
rra
|
||||
xor l
|
||||
ld l,a
|
||||
xor h
|
||||
ld h,a
|
||||
|
||||
ret
|
1
tools/emul/.gitignore
vendored
1
tools/emul/.gitignore
vendored
@ -5,3 +5,4 @@
|
||||
/cfsin/zasm
|
||||
/cfsin/ed
|
||||
/cfsin/user.h
|
||||
/cfsin/dice
|
||||
|
@ -4,7 +4,7 @@ KERNEL = ../../kernel
|
||||
APPS = ../../apps
|
||||
ZASMBIN = zasm/zasm
|
||||
ZASMSH = ../zasm.sh
|
||||
SHELLAPPS = $(addprefix cfsin/, zasm ed)
|
||||
SHELLAPPS = $(addprefix cfsin/, zasm ed dice)
|
||||
CFSIN_CONTENTS = $(SHELLAPPS) cfsin/user.h
|
||||
|
||||
.PHONY: all
|
||||
|
2
tools/emul/dice/user.h
Normal file
2
tools/emul/dice/user.h
Normal file
@ -0,0 +1,2 @@
|
||||
.equ USER_CODE 0x4200
|
||||
.equ USER_RAMSTART 0x6000
|
@ -39,10 +39,13 @@
|
||||
jp printcrlf
|
||||
jp stdioPutC
|
||||
jp stdioReadLine
|
||||
jp printHexPair
|
||||
jp rnd
|
||||
|
||||
.inc "core.asm"
|
||||
.inc "err.h"
|
||||
.inc "parse.asm"
|
||||
.inc "random.asm"
|
||||
|
||||
.equ BLOCKDEV_RAMSTART RAMSTART
|
||||
.equ BLOCKDEV_COUNT 4
|
||||
|
@ -32,3 +32,5 @@
|
||||
.equ printcrlf @+3
|
||||
.equ stdioPutC @+3
|
||||
.equ stdioReadLine @+3
|
||||
.equ printHexPair @+3
|
||||
.equ rnd @+3
|
Loading…
Reference in New Issue
Block a user