1
0
mirror of https://github.com/hsoft/collapseos.git synced 2025-04-05 06:38:40 +11:00

Implement RNG in kernel and basic dice app to demonstrate it

This commit is contained in:
Keith Stellyes 2019-10-13 12:44:43 -07:00
parent 28a0b84321
commit fdce67f8ba
8 changed files with 90 additions and 1 deletions

6
apps/dice/glue.asm Normal file
View 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
View 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
View 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

View File

@ -5,3 +5,4 @@
/cfsin/zasm /cfsin/zasm
/cfsin/ed /cfsin/ed
/cfsin/user.h /cfsin/user.h
/cfsin/dice

View File

@ -4,7 +4,7 @@ KERNEL = ../../kernel
APPS = ../../apps APPS = ../../apps
ZASMBIN = zasm/zasm ZASMBIN = zasm/zasm
ZASMSH = ../zasm.sh ZASMSH = ../zasm.sh
SHELLAPPS = $(addprefix cfsin/, zasm ed) SHELLAPPS = $(addprefix cfsin/, zasm ed dice)
CFSIN_CONTENTS = $(SHELLAPPS) cfsin/user.h CFSIN_CONTENTS = $(SHELLAPPS) cfsin/user.h
.PHONY: all .PHONY: all

2
tools/emul/dice/user.h Normal file
View File

@ -0,0 +1,2 @@
.equ USER_CODE 0x4200
.equ USER_RAMSTART 0x6000

View File

@ -39,10 +39,13 @@
jp printcrlf jp printcrlf
jp stdioPutC jp stdioPutC
jp stdioReadLine jp stdioReadLine
jp printHexPair
jp rnd
.inc "core.asm" .inc "core.asm"
.inc "err.h" .inc "err.h"
.inc "parse.asm" .inc "parse.asm"
.inc "random.asm"
.equ BLOCKDEV_RAMSTART RAMSTART .equ BLOCKDEV_RAMSTART RAMSTART
.equ BLOCKDEV_COUNT 4 .equ BLOCKDEV_COUNT 4

View File

@ -32,3 +32,5 @@
.equ printcrlf @+3 .equ printcrlf @+3
.equ stdioPutC @+3 .equ stdioPutC @+3
.equ stdioReadLine @+3 .equ stdioReadLine @+3
.equ printHexPair @+3
.equ rnd @+3