                              1 ;/***************************************************************************
                              2 ; *                                                                         *
                              3 ; * Module  : rand.s                                                        *
                              4 ; *                                                                         *
                              5 ; * Purpose : A rand() generator using the linear congruential method       *
                              6 ; *                                                                         *
                              7 ; * Version : 1.01, January 7 1998                                          *
                              8 ; *             Added _initrand to set seed without recompiling             *
                              9 ; *           1, January 6 1998                                             *
                             10 ; *                                                                         *
                             11 ; * Author  : Luc Van den Borre ( Homepage : NOC.BASE.ORG )                 *
                             12 ; *                                                                         *
                             13 ; **************************************************************************/
                             14 
                             15 	;; BANKED:	checked
                             16 	
                             17 	;; Why use an algorithm for generating random numbers?
                             18 	;;
                             19 	;; - Given a certain seed value, the same sequence of random numbers is generated
                             20 	;;   every time. This is a good thing when debugging (reproducible). On the other
                             21 	;;   hand, you've got 2^16 seed values, each of which will produce a sequence of
                             22 	;;   numbers that stays different for any of the other sequences for 'an appreciable
                             23 	;;   time.' (I can't say how long exactly.)
                             24 	;;
                             25 	;; - The linear congruential method is one of the 'best' random number generators
                             26 	;;   around. However, this implementation uses a 16 bit accumulator, while at least
                             27 	;;   32 bits are needed for a generator that passes all the statistical tests.
                             28 	;;   Still, I'm relatively confident that this is random enough for even the most
                             29 	;;   demanding game.
                             30 	;;
                             31 	;;   Compare this to getting random values from one of the hardware registers
                             32 	;;   (not reproducible, might not have all values). An array might be the best bet
                             33 	;;   if you don't need a lot of values (or have lots of memory spare),
                             34 	;;   or if you want values to be within a certain range.
                             35 	;;   And both would be faster than this. Also, this definitely isn't the fastest
                             36 	;;   algorithm I know, and certainly for games less strict algorithms might be
                             37 	;;   appropriate (shift and xor ?).
                             38 	;;   It's your choice - but if you're doing Monte Carlo physics simulations on the
                             39 	;;   GameBoy, this is a safe bet!
                             40 
                             41 	.include	"global.s"
                    0001      1 	.NEAR_CALLS = 1         ; <near_calls> - tag so that sed can change this
                              2         
                              3 	;; Changed by astorgb.pl to 1
                    0000      4 	__RGBDS__	= 0
                              5 
                              6 	
                              7 	;;  Screen dimensions 
                    0013      8 	.MAXCURSPOSX	= 0x13	; In tiles
                    0011      9 	.MAXCURSPOSY	= 0x11
                             10 
                    0080     11 	.START		= 0x80
                    0040     12 	.SELECT		= 0x40
                    0020     13 	.B		= 0x20
                    0010     14 	.A		= 0x10
                    0008     15 	.DOWN		= 0x08
                    0004     16 	.UP		= 0x04
                    0002     17 	.LEFT		= 0x02
                    0001     18 	.RIGHT		= 0x01
                             19 
                    00A0     20 	.SCREENWIDTH	= 0xA0
                    0090     21 	.SCREENHEIGHT	= 0x90
                    0007     22 	.MINWNDPOSX	= 0x07
                    0000     23 	.MINWNDPOSY	= 0x00
                    00A6     24 	.MAXWNDPOSX	= 0xA6
                    008F     25 	.MAXWNDPOSY	= 0x8F
                             26 
                    0001     27 	.VBL_IFLAG	= 0x01
                    0002     28 	.LCD_IFLAG	= 0x02
                    0004     29 	.TIM_IFLAG	= 0x04
                    0008     30 	.SIO_IFLAG	= 0x08
                    0010     31 	.JOY_IFLAG	= 0x10
                             32  
                    0000     33 	.P1		= 0x00	; Joystick: 1.1.P15.P14.P13.P12.P11.P10
                    0001     34 	.SB		= 0x01	; Serial IO data buffer
                    0002     35 	.SC		= 0x02	; Serial IO control register
                    0004     36 	.DIV		= 0x04	; Divider register
                    0005     37 	.TIMA		= 0x05	; Timer counter
                    0006     38 	.TMA		= 0x06	; Timer modulo
                    0007     39 	.TAC		= 0x07	; Timer control
                    000F     40 	.IF		= 0x0F	; Interrupt flags: 0.0.0.JST.SIO.TIM.LCD.VBL
                    0010     41 	.NR10		= 0x10	; Sound register
                    0011     42 	.NR11		= 0x11	; Sound register
                    0012     43 	.NR12		= 0x12	; Sound register
                    0013     44 	.NR13		= 0x13	; Sound register
                    0014     45 	.NR14		= 0x14	; Sound register
                    0016     46 	.NR21		= 0x16	; Sound register
                    0017     47 	.NR22		= 0x17	; Sound register
                    0018     48 	.NR23		= 0x18	; Sound register
                    0019     49 	.NR24		= 0x19	; Sound register
                    001A     50 	.NR30		= 0x1A	; Sound register
                    001B     51 	.NR31		= 0x1B	; Sound register
                    001C     52 	.NR32		= 0x1C	; Sound register
                    001D     53 	.NR33		= 0x1D	; Sound register
                    001E     54 	.NR34		= 0x1E	; Sound register
                    0020     55 	.NR41		= 0x20	; Sound register
                    0021     56 	.NR42		= 0x21	; Sound register
                    0022     57 	.NR43		= 0x22	; Sound register
                    0023     58 	.NR44		= 0x23	; Sound register
                    0024     59 	.NR50		= 0x24	; Sound register
                    0025     60 	.NR51		= 0x25	; Sound register
                    0026     61 	.NR52		= 0x26	; Sound register
                    0040     62 	.LCDC		= 0x40	; LCD control
                    0041     63 	.STAT		= 0x41	; LCD status
                    0042     64 	.SCY		= 0x42	; Scroll Y
                    0043     65 	.SCX		= 0x43	; Scroll X
                    0044     66 	.LY		= 0x44	; LCDC Y-coordinate
                    0045     67 	.LYC		= 0x45	; LY compare
                    0046     68 	.DMA		= 0x46	; DMA transfer
                    0047     69 	.BGP		= 0x47	; BG palette data
                    0048     70 	.OBP0		= 0x48	; OBJ palette 0 data
                    0049     71 	.OBP1		= 0x49	; OBJ palette 1 data
                    004A     72 	.WY		= 0x4A	; Window Y coordinate
                    004B     73 	.WX		= 0x4B	; Window X coordinate
                    004D     74 	.KEY1		= 0x4D	; CPU speed
                    004F     75 	.VBK		= 0x4F	; VRAM bank
                    0051     76 	.HDMA1		= 0x51	; DMA control 1
                    0052     77 	.HDMA2		= 0x52	; DMA control 2
                    0053     78 	.HDMA3		= 0x53	; DMA control 3
                    0054     79 	.HDMA4		= 0x54	; DMA control 4
                    0055     80 	.HDMA5		= 0x55	; DMA control 5
                    0056     81 	.RP		= 0x56	; IR port
                    0068     82 	.BCPS		= 0x68	; BG color palette specification
                    0069     83 	.BCPD		= 0x69	; BG color palette data
                    006A     84 	.OCPS		= 0x6A	; OBJ color palette specification
                    006B     85 	.OCPD		= 0x6B	; OBJ color palette data
                    0070     86 	.SVBK		= 0x70	; WRAM bank
                    00FF     87 	.IE		= 0xFF	; Interrupt enable
                             88 
                    0001     89 	.G_MODE		= 0x01	; Graphic mode
                    0002     90 	.T_MODE		= 0x02	; Text mode (bit 2)
                    0002     91 	.T_MODE_OUT	= 0x02	; Text mode output only
                    0003     92 	.T_MODE_INOUT	= 0x03	; Text mode with input
                    0004     93 	.M_NO_SCROLL	= 0x04	; Disables scrolling of the screen in text mode
                    0008     94 	.M_NO_INTERP	= 0x08	; Disables special character interpretation
                             95 
                    2000     96 	.MBC1_ROM_PAGE	= 0x2000 ; Address to write to for MBC1 switching
                             97 	
                             98 	;; Status codes for IO
                    0000     99 	.IO_IDLE	= 0x00
                    0001    100 	.IO_SENDING	= 0x01
                    0002    101 	.IO_RECEIVING	= 0x02
                    0004    102 	.IO_ERROR	= 0x04
                            103 
                            104 	;; Type of IO data
                    0066    105 	.DT_IDLE	= 0x66
                    0055    106 	.DT_RECEIVING	= 0x55
                            107 
                            108 	;; Table of routines for modes
                    01E0    109 	.MODE_TABLE	= 0x01E0
                            110 
                            111 	;; C related
                            112 	;; Overheap of a banked call.  Used for parameters
                            113 	;;  = ret + real ret + bank
                            114 
                    0001    115 	.if .NEAR_CALLS
                    0002    116 	.BANKOV		= 2
                            117 
                            118 	.else
                            119 	.BANKOV		= 6
                            120 
                            121 	.endif
                            122 	
                            123 	.globl	banked_call
                            124 	.globl	banked_ret
                            125 	
                            126 	;; Global variables
                            127 	.globl	.mode
                            128 
                            129 	.globl	__cpu
                            130 
                            131 	.globl	__io_out
                            132 	.globl	__io_in
                            133 	.globl	__io_status
                            134 
                            135 	;; Global routines
                            136 	.globl	.set_mode
                            137 
                            138 	.globl	.reset
                            139 
                            140 	.globl	.display_off
                            141 
                            142 	.globl	.wait_vbl_done
                            143 
                            144 	;; Interrupt routines 
                            145 	.globl	.add_VBL
                            146 	.globl	.add_LCD
                            147 	.globl	.add_TIM
                            148 	.globl	.add_SIO
                            149 	.globl	.add_JOY
                            150 
                            151 	;; Symbols defined at link time
                            152 	.globl	.STACK
                            153 	.globl	.OAM
                            154 	.globl	.refresh_OAM
                            155 
                            156 	;; Initialization routine   
                            157 	.globl	.init
                            158 
                            159 	;; Main user routine	
                            160 	.globl	_main
                             42 
                             43 	.area	_BSS
   0000                      44 .randhi::			; Storage for last random number (or seed)
   0000                      45 	.ds	0x01
   0001                      46 .randlo::
   0001                      47 	.ds	0x01
                             48 
                             49 	.area	_CODE
                             50 
                             51 	;; Random number generator using the linear congruential method
                             52 	;;  X(n+1) = (a*X(n)+c) mod m
                             53 	;; with a = 17, m = 16 and c = $5c93 (arbitrarily)
                             54 	;; The seed value is also chosen arbitrarily as $a27e
                             55 	;; Ref : D. E. Knuth, "The Art of Computer Programming" , Volume 2
                             56 	;;
                             57 	;; Exit conditions
                             58 	;;   DE = Random number [0,2^16-1]
                             59 	;;
                             60 	;; Registers used:
                             61 	;;   A, HL (need not be saved) and DE (return register)
                             62 	;;
                             63 
   0000                      64 _rand::				; Banked
   0000                      65 _randw::			; Banked
   0000 FAr01s00             66 	LD	A,(.randlo)
   0003 6F                   67 	LD	L,A
   0004 5F                   68 	LD	E,A		; Save randlo
   0005 FAr00s00             69 	LD	A,(.randhi)
   0008 57                   70 	LD	D,A		; Save randhi
                             71 
   0009 CB 25                72 	SLA	L		; * 16
   000B 17                   73 	RLA
   000C CB 25                74 	SLA	L
   000E 17                   75 	RLA
   000F CB 25                76 	SLA	L
   0011 17                   77 	RLA
   0012 CB 25                78 	SLA	L
   0014 17                   79 	RLA
   0015 67                   80 	LD	H,A		; Save randhi*16
                             81 
   0016 7B                   82 	LD	A,E		; Old randlo
   0017 85                   83 	ADD	A,L		; Add randlo*16
   0018 6F                   84 	LD	L,A		; Save randlo*17
                             85 
   0019 7C                   86 	LD	A,H		; randhi*16
   001A 8A                   87 	ADC	A,D		; Add old randhi
   001B 67                   88 	LD	H,A		; Save randhi*17
                             89 
   001C 7D                   90 	LD	A,L		; randlo*17
   001D C6 93                91 	ADD	A,#0x93
   001F EAr01s00             92 	LD	(.randlo),A
   0022 57                   93 	LD	D,A		; Return register
   0023 7C                   94 	LD	A,H		; randhi*17
   0024 CE 5C                95 	ADC	A,#0x5c
   0026 EAr00s00             96 	LD	(.randhi),A
   0029 5F                   97 	LD	E,A		; Return register
                             98 
                             99 	;; Note D is the low byte,E the high byte. This is intentional because
                            100 	;; the high byte can be slightly 'more random' than the low byte, and I presume
                            101 	;; most will cast the return value to a UBYTE. As if someone will use this, tha!
   002A C9                  102 	RET
                            103 
                            104 	;; This sets the seed value. Call it whenever you like
                            105 	;;
                            106 	;; Exit conditions
                            107 	;;   None
                            108 	;;
                            109 	;; Registers used:
                            110 	;;   A, HL (need not be saved) and DE (return register)
                            111 	;;
                            112 	.area	_BASE
   0000                     113 _initrand::			; Non banked
   0000 F8 02               114 	LDA	HL,2(SP)
   0002                     115 .initrand::
   0002 2A                  116 	LD	A,(HL+)
   0003 EAr01s00            117 	LD	(.randlo),A
   0006 7E                  118 	LD	A,(HL)
   0007 EAr00s00            119 	LD	(.randhi),A
   000A C9                  120 	RET
