git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/libc/asm/sm83/arand.s

      1 ;/***************************************************************************
      2 ; *                                                                         *
      3 ; * Module  : arand.s                                                       *
      4 ; *                                                                         *
      5 ; * Purpose : A random number generator using the lagged additive method    *
      6 ; *                                                                         *
      7 ; * Version : 1, January 11 1998                                            *
      8 ; *                                                                         *
      9 ; * Author  : Luc Van den Borre ( Homepage : NOC.BASE.ORG )                 *
     10 ; *                                                                         *
     11 ; **************************************************************************/
     12 
     13 	.globl	.initrand
     14 	.globl	_rand
     15 
     16 	.area	_DATA
     17 
     18 .randarr:
     19 	.ds	55
     20 .raxj:
     21 	.ds	0x01
     22 .raxk:
     23 	.ds	0x01
     24 
     25 	.area	_CODE
     26 
     27 	;; arand() operates on an array of 55 arbitrary values (here : bytes).
     28 	;; It adds two values of the array together, replaces one of the values
     29 	;; with the result, and returns the result.
     30 	;; At start, the indices into the array refer to the 55th and 24th element.
     31 	;; After each call, each index is decreased, and looped around if necessary.
     32 	;; This kind of works, but the values produces are less good than those by
     33 	;; rand(), mainly because it operates on bytes instead of words.
     34 	;; Ref : D. E. Knuth, "The Art of Computer Programming" , Volume 2
     35 	;;
     36 	;; Exit conditions
     37 	;;   DE = Random number (byte!)
     38 	;;
     39 	;; Registers used:
     40 	;;   all
     41 	;;
     42 _arand::			; Banked
     43 	PUSH	BC
     44 	LD	D,#0
     45 	LD	HL,#.randarr-1
     46 	LD	A,(.raxj)
     47 	LD	E,A
     48 	DEC	A		; Decrease the pointer
     49 	JR	NZ,1$
     50 	LD	A,#55
     51 1$:
     52 	LD	(.raxj),A
     53 	ADD	HL,DE
     54 	LD	B,(HL)
     55 
     56 	LD	HL,#.randarr-1	; Ooh...
     57 	LD	A,(.raxk)
     58 	LD	E,A
     59 	DEC	A		; Decrease the pointer
     60 	JR	NZ,2$
     61 	LD	A,#55
     62 2$:
     63 	LD	(.raxk),A
     64 	ADD	HL,DE
     65 	LD	A,(HL)
     66 
     67 	ADD	B
     68 	LD	(HL),A		; Store new value
     69 
     70 	POP	BC
     71 
     72 	LD	D,#0
     73 	LD	E,A
     74 
     75 	RET
     76 
     77 	;; _initarand calls the _rand function to fill the array with random values
     78 	;; Note that this also sets the seed value of the _rand function first,
     79 	;; like _initrand
     80 	;;
     81 	;; Exit conditions
     82 	;;   None
     83 	;;
     84 	;; Registers used:
     85 	;;   all
     86 	;;
     87 _initarand::			; Banked
     88 	LDA	HL,2(SP)
     89 	CALL	.initrand
     90 
     91 	PUSH	BC
     92 	LD	A,#55
     93 	LD	HL,#.randarr
     94 1$:
     95 	DEC	A
     96 	LD	(.raxj),A
     97 	LD	B,H
     98 	LD	C,L
     99 	CALL	_rand
    100 	LD	H,B
    101 	LD	L,C
    102 
    103 	LD	A, E
    104 	LD	(HL+),A
    105 	
    106 	LD	A,(.raxj)
    107 	CP	#0
    108 	JR	NZ,1$
    109 
    110 	LD	A,#24		; Now the array has been filled,set the pointers
    111 	LD	(.raxj),A
    112 	LD	A,#55
    113 	LD	(.raxk),A
    114 
    115 	POP	BC
    116 	RET

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.