IRANDOM.CPP 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /***************************************************************************
  15. ** C O N F I D E N T I A L --- W E S T W O O D A S S O C I A T E S **
  16. ***************************************************************************
  17. * *
  18. * Project Name : LIBRARY *
  19. * *
  20. * File Name : IRANDOM.C *
  21. * *
  22. * Programmer : Barry W. Green *
  23. * *
  24. * Last Update : 10 Feb, 1995 [BWG] *
  25. * *
  26. *-------------------------------------------------------------------------*
  27. * Functions: *
  28. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  29. #include <stdlib.h>
  30. #include <time.h>
  31. #include "misc.h"
  32. extern "C" unsigned long RandNumb = 0x12349876;
  33. /* IRANDOM ----------------------------------------------------------
  34. IRandom returns a random value between min and max inclusive.
  35. INPUTS: int min and int max
  36. RETURNS: int random number
  37. */
  38. int IRandom(int minval, int maxval)
  39. {
  40. int num,mask;
  41. // Keep minval and maxval straight.
  42. if (minval > maxval) {
  43. minval ^= maxval;
  44. maxval ^= minval;
  45. minval ^= maxval;
  46. }
  47. mask = Get_Random_Mask(maxval - minval);
  48. while( (num = (rand() & mask) + minval) > maxval ) ;
  49. return(num);
  50. }
  51. unsigned char Random(void)
  52. {
  53. __asm {
  54. lea esi, [RandNumb] //; get offset in segment of RandNumb
  55. xor eax,eax
  56. mov al,[esi]
  57. shr al,1 //; shift right 1 bit (bit0 in carry)
  58. shr al,1
  59. rcl [BYTE PTR esi+2],1 //; rcl byte 3 of RandNumb
  60. rcl [BYTE PTR esi+1],1 //; rcl byte 2 of RandNumb
  61. cmc //; complement carry
  62. sbb al,[esi] //; sbb byte 1 of RandNumb
  63. shr al,1 //; sets carry
  64. rcr [BYTE PTR esi],1 //; rcr byte 1 of RandNumb
  65. mov al,[esi] //; reload byte 1 of RandNumb
  66. xor al,[esi+1] //; xor with byte 2 of RandNumb
  67. }
  68. }
  69. int Get_Random_Mask(int maxval)
  70. {
  71. __asm {
  72. bsr ecx,[maxval] // ; put bit position of highest bit in ecx
  73. mov eax, 1 // ; set one bit on in eax
  74. jz invalid // ; if BSR shows maxval==0, return eax=1
  75. inc ecx // ; get one bit higher than count showed
  76. shl eax,cl // ; move our EAX bit into position
  77. dec eax // ; dec it to create the mask.
  78. invalid:
  79. }
  80. }
  81. #if (0)
  82. RandNumb DD 12349876H
  83. ;
  84. ; UBYTE Random(VOID);
  85. ; int Get_Random_Mask(int maxval);
  86. ;
  87. ; ----------------------------------------------------------------
  88. ;-----------------------------------------------------------------
  89. ; RANDOM
  90. ;
  91. ; UBYTE Random(VOID);
  92. ;
  93. ;*
  94. PROC Random C near
  95. USES esi
  96. lea esi, [RandNumb] ; get offset in segment of RandNumb
  97. xor eax,eax
  98. mov al,[esi]
  99. shr al,1 ; shift right 1 bit (bit0 in carry)
  100. shr al,1
  101. rcl [BYTE PTR esi+2],1 ; rcl byte 3 of RandNumb
  102. rcl [BYTE PTR esi+1],1 ; rcl byte 2 of RandNumb
  103. cmc ; complement carry
  104. sbb al,[esi] ; sbb byte 1 of RandNumb
  105. shr al,1 ; sets carry
  106. rcr [BYTE PTR esi],1 ; rcr byte 1 of RandNumb
  107. mov al,[esi] ; reload byte 1 of RandNumb
  108. xor al,[esi+1] ; xor with byte 2 of RandNumb
  109. ret
  110. ENDP Random
  111. ;-----------------------------------------------------------------
  112. ; GET_RANDOM_MASK - returns an AND value that is large enough that it
  113. ; encloses the 'maxval' parameter.
  114. ;
  115. ; int Get_Random_Mask(int maxval);
  116. ;
  117. ;*
  118. PROC Get_Random_Mask C near
  119. USES ecx
  120. ARG maxval:DWORD
  121. ; This function takes as a parameter a maximum value, for example, 61. It
  122. ; then tries to create an AND mask that is big enough to enclose that number.
  123. ; For our example case, that AND mask would be 0x3F. It does this by scanning
  124. ; for the highest bit in the number, then making an all-1's mask from that
  125. ; bit position down to bit 0.
  126. bsr ecx,[maxval] ; put bit position of highest bit in ecx
  127. mov eax,1 ; set one bit on in eax
  128. jz ??invalid ; if BSR shows maxval==0, return eax=1
  129. inc ecx ; get one bit higher than count showed
  130. shl eax,cl ; move our EAX bit into position
  131. dec eax ; dec it to create the mask.
  132. ??invalid:
  133. ret
  134. ENDP Get_Random_Mask
  135. ;----------------------------------------------------------------------------
  136. END
  137. #endif