Advertisement
TerusTheBird

CrazyBus RNG

Dec 3rd, 2021
2,262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;=============================================================================
  2. ; NAME
  3. ;    RandomSeed - seed random number generator, call once at beginning of
  4. ;          your program.  CurrentTime provides useful values for this
  5. ;
  6. ; SYSNOPSIS     RandomSeed( SeedValue )  D0
  7. ;
  8. ; FUNCTION     Seeds the random number generator
  9. ;
  10. ; INPUTS    SeedValue - a longword containing any value you like
  11. ;
  12. ; RESULT    Random number generator is initialised
  13. ;
  14. ;============================================================================
  15.  
  16. _RandomSeed
  17.         MOVE.L    4(SP),D0    ;entry point for C functions
  18. RandomSeed    
  19.         ADD.L    D0,D1        ;user seed in d0 (d1 too)
  20.         MOVEM.L    D0/D1,RND
  21.  
  22. ; drops through to the main random function (not user callable)
  23.  
  24. LongRnd        
  25.         MOVEM.L    D2-D3,-(SP)    
  26.         MOVEM.L    RND,D0/D1    ;D0=LSB's, D1=MSB's of random number
  27.         ANDI.B    #$0E,D0        ;ensure upper 59 bits are an...
  28.         ORI.B    #$20,D0        ;...odd binary number
  29.         MOVE.L    D0,D2
  30.         MOVE.L    D1,D3
  31.         ADD.L    D2,D2        ;accounts for 1 of 17 left shifts
  32.         ADDX.L    D3,D3        ;[D2/D3] = RND*2
  33.         ADD.L    D2,D0
  34.         ADDX.L    D3,D1        ;[D0/D1] = RND*3
  35.         SWAP    D3            ;shift [D2/D3] additional 16 times
  36.         SWAP    D2
  37.         MOVE.W    D2,D3
  38.         CLR.W    D2
  39.         ADD.L    D2,D0        ;add to [D0/D1]
  40.         ADDX.L    D3,D1
  41.         MOVEM.L    D0/D1,RND    ;save for next time through
  42.         MOVE.L    D1,D0        ;most random part to D0
  43.         MOVEM.L    (SP)+,D2-D3
  44.         RTS
  45.  
  46. ;=============================================================================
  47. ; NAME
  48. ;    Random - returns a random integer in the specified range
  49. ;
  50. ; SYSNOPSIS      RndNum = Random( UpperLimit )    D0          D0
  51. ;
  52. ; FUNCTION    returns a random integer in the range 0 to UpperLimit-1
  53. ;
  54. ; INPUTS     UpperLimit - a long(or short will do) in the range 1-65535
  55. ;
  56. ; RESULT     a random integer is returned to you, real quick!
  57. ;
  58. ; LIMITATIONS
  59. ;    range was limited to 1-65535 to avoid problems with the DIVU instruction
  60. ;    which can return real wierd values if the result is larger than 16 bits.
  61. ;
  62. ;============================================================================
  63.  
  64. _Random        
  65.         MOVE.L    4(SP),D0    ;C entry point
  66. Random    MOVE.W    D2,-(SP)
  67.         MOVE.W    D0,D2        ;save upper limit
  68.         BEQ.S    10$            ;range of 0 returns 0 always
  69.         BSR.S    LongRnd        ;get a longword random number
  70.         CLR.W    D0            ;use upper word (it's most random)
  71.         SWAP    D0
  72.         DIVU.W    D2,D0        ;divide by range...
  73.         CLR.W    D0            ;...and use remainder for the value
  74.         SWAP    D0            ;result in D0.W
  75. 10$        MOVE.W    (SP)+,D2
  76.         RTS
  77.  
  78.         BSS
  79. RND        DS.L    2            ;random number
  80.         END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement