;;;;;rand.asm
;;Random functions for TI-85 (Z80 assembler).
;;Chris Busch (cbusch@d.umn.edu)
;;Please distribute as rand.asm.  Please no cutting and pasting. Use
;;#include "rand.asm" instead.
#ifndef RAND_ASM
#define RAND_ASM
;;A=rand()
;;Generates a 7 bit random number
;;If you want a good 2 bit random number (0-3) have the following 3 lines 
;;after the CALL_( rand).
;;Basically, the middle/upper bits are the most random.
;    CALL_(rand)
;    srl   a       ;;
;    srl   a       ;;
;    and   $03     ;; get a good random 2 bit number
;Note: Destroys B.  Returns A as random number.
#ifndef XOR_RAND
rand:
    ld    a,(randvar)  ;;must be defined in text area
    ld    b,a
    ld    a,0
    add   a,b
    sla   b
    sla   b
    add   a,b
    sla   b
    sla   b
    add   a,b
    inc   a
    ld    (randvar),a
    srl   a
    ret
;;end rand
#endif

;;Chris Busch (cbusch@d.umn.edu)
;;If you want a faster random function, but not one as good use this one.
;;rand returns 7 bit poor random number, but FAST!
;;A=rand(){
;;  randvar = (randvar ^ 220)+1;
;;  return randvar>>1; //use only 6 middle bits
;;}
;;Note, if you wish to use this rand #define XOR_RAND before the 
;;#include "rand.asm".
#ifdef  XOR_RAND
rand:
   ld    a,(randvar)     ;randvar must be textarea var
   xor   220d
   inc   a
   ld    (randvar),a
   srl   a
   ret
;;end rand
#endif

#endif
;;;;;end rand.asm
