|

16-Bit Zufallsgenerator für AVR Mikrocontroller
Als Zufallsgenerator dient hier ein 16-Bit Schieberegister, welches über die XOR-verknüpften Bits
4, 13 und 15 zurückgekoppelt wird. Beim Aufruf wird der Wert im Register XL als obere Grenze verwendet, das
Resultat (0...XL-1) wird in XL zurückgegeben. Die Inhalte der Register r0 und r1 werden überschrieben, und
es werden 3 Bytes im RAM benötigt. Anwendungsgebiete sind zum Beispiel Spiele und Geräuschausgabe.
;################################################################################
;# #
;# smath16 - 16-bit random generator library for ATmega controllers #
;# verion 0.1 #
;# copyright (c) 2005/2006 Joerg Wolfram (joerg@jcwolfram.de) #
;# #
;# #
;# This library is free software; you can redistribute it and/or #
;# modify it under the terms of the GNU Lesser General Public #
;# License as published by the Free Software Foundation; either #
;# version 2 of the License, or (at your option) any later version. #
;# #
;# This library is distributed in the hope that it will be useful, #
;# but WITHOUT ANY WARRANTY; without even the implied warranty of #
;# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
;# Lesser General Public License for more details. #
;# #
;# You should have received a copy of the GNU Lesser General Public #
;# License along with this library; if not, write to the #
;# Free Software Foundation, Inc., 59 Temple Place - Suite 330, #
;# Boston, MA 02111-1307, USA. #
;# #
;################################################################################
;###############################################################################
;Interface:
;
; rand_rnd : XL=random(0...XL-1) XH=0
;
; rand_mem = 3 bytes of SRAM
;###############################################################################
librand16_rnd: push XL
lds XL,rand_mem+13 ;last result
clr r0 ;result=0
ror XL ;set carry with last result
lds XL,rand_mem+14 ;get LSB
rol XL ;rotrate left
sts rand_mem+14,XL ;put LSB
lds XH,rand_mem+15 ;get MSB
rol XH ;rotrate left
sts rand_mem+15,XH ;put MSB
sbrc XL,4 ;
com r0 ;
sbrc XH,5 ;
com r0 ;
sbrc XH,7 ;
com r0 ;
sts rand_mem+13,r0 ;store result
lds r0,rand_mem+14 ;get LSB
pop XL
mul r0,XL ;mult
mov XL,r1 ;get result
clr XH ;MSB is always zero
ret
|
|