Search code examples
assemblyrandom68000

Assembly Random Number within Range using Easy 68K (68000)


I'm creating a simple black jack game using the Easy 68K simulator and need to use a random number to assign the cards. My cards must be in the range 2 to 11. I seem to be getting the same number every time, and it isn't within the range I expect. My card value needs to end up in D3, so I have the following random number code:

 CLR.L       D0
 CLR.L       D3
 MOVE.B      #8, D0         ;Access time
 TRAP        #15
 AND.L       #$5FFFFF,D1    ;prevent overflow in divu
 DIVU        #10, D1
 SWAP        D1
 ADDQ.W      #1, D1
 MOVE        D1, D3

which I came to by modifying the code on this site: https://notendur.hi.is/voe1/3.%20onn/Tolvuhogun/EASy68K/Examples/tutorial3.X68

I am hoping to find help generating a number 2 through 11. I have been searching the internet for several hours. I know I need to access the time by using Move.B #8, D0, but beyond that, I haven't made much progress. Any help would be very much appreciated!


Solution

  • The mask being used in the AND.L ("prevent overflow in divu") was only good for a division by 100 -- you'll need to mask with 0x7FFFF for a division by 10.

    Why:

    0xFFFFFFFF / #100 = 0x28f5c28 - too big for a single word!
    0x5FFFFF   / #100 = 0xF5C2    - that fits
    
    0x5FFFFF / #10  = 0x99999 - too big for a single word!
    0x7FFFF  / #10  = 0xCCCC  - that fits
    

    Additionally, the code you have will give you a number from 1 to 10 (0-9 + 1). If you want 2 to 11, you'll have to add 2, not 1.


    Here's a more advanced random number generator, borrowed from the Mac OS QuickDraw source code. Note that you may need to translate the syntax somewhat (it was written over 25 years ago!) and/or modify the way it loads and stores its seed.

    ;--------------------------------------------------------------
    ;
    ;  FUNCTION Random: INTEGER;
    ;
    ;  returns a signed 16 bit number, and updates unsigned 32 bit randSeed.
    ;
    ;  recursion is randSeed := (randSeed * 16807) MOD 2147483647.
    ;
    ;  See paper by Linus Schrage, A More Portable Fortran Random Number Generator
    ;  ACM Trans Math Software Vol 5, No. 2, June 1979, Pages 132-138.
    ;
    ;  Clobbers D0-D2, A0
    ;
    ;
    ;  GET LO 16 BITS OF SEED AND FORM LO PRODUCT
    ;  xalo := A * LoWord(seed)
    ;
            MOVE.L  GRAFGLOBALS(A5),A0      ;POINT TO QuickDraw GLOBALS
            MOVE    #16807,D0               ;GET A = 7^5
            MOVE    D0,D2                   ;GET A = 7^5
            MULU    RANDSEED+2(A0),D0       ;CALC LO PRODUCT = XALO
    ;
    ;  FORM 31 HIGHEST BITS OF LO PRODUCT
    ;  fhi:=HiWord(seed) * ORD4(a) + HiWord(xalo);
    ;
            MOVE.L  D0,D1                   ;COPY xalo
            CLR.W   D1
            SWAP    D1                      ;GET HiWord(xalo) as a long
            MULU    RANDSEED(A0),D2         ;MULT BY HiWord(seed)
            ADD.L   D1,D2                   ;ADD LEFTLO = FHI
    ;
    ;  GET OVERFLOW PAST 31ST BIT OF FULL PRODUCT
    ;  k:=fhi DIV 32768;
    ;
            MOVE.L  D2,D1                   ;COPY FHI
            ADD.L   D1,D1                   ;CALC 2 TIMES FHI
            CLR.W   D1
            SWAP    D1                      ;CALC FHI SHIFTED RIGHT 15 FOR K
    ;
    ;  ASSEMBLE ALL THE PARTS AND PRE-SUBTRACT P
    ;  seed:=((BitAnd(XALO,$0000FFFF) - P) + BitAnd(fhi,$00007FFF) * b16) + K;
    ;
            AND.L   #$0000FFFF,D0           ;GET LO WORD XALO
            SUB.L   #$7FFFFFFF,D0           ;SUBTRACT P = 2^31-1
            AND.L   #$00007FFF,D2           ;BitAnd(fhi,$00007FFF)
            SWAP    D2                      ;TIMES 64K
            ADD.L   D1,D2                   ;PLUS K
            ADD.L   D2,D0                   ;CALC TOTAL
    ;
    ;  IF seed < 0 THEN seed:=seed+p;
    ;
            BPL.S   UPDATE
            ADD.L   #$7FFFFFFF,D0
    UPDATE  MOVE.L  D0,RANDSEED(A0)         ;UPDATE SEED
            CMP.W   #$8000,D0               ;IS NUMBER -32768 ?
            BNE.S   NUMOK                   ;NO, CONTINUE
            CLR     D0                      ;YES, RETURN ZERO INSTEAD
    NUMOK   MOVE.W  D0,4(SP)                ;RETURN LO WORD AS RESULT
            RTS