git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/libc/asm/mos6502/rand.s

      1 ;
      2 ; Random number generation for mos6502.
      3 ;
      4 ; Re-implementation of rand / randw / inirand, originally from gbdk-lib/libc/asm/sm83/rand.s
      5 ;
      6 ; Also redirects arand / initarand to here, to avoid their extra memory cost.
      7 ;
      8 
      9 .area _ZP (PAG)
     10 ___rand_seed::
     11 .randlo: .ds 1
     12 .randhi: .ds 1
     13 
     14 .area _HOME
     15 
     16 .define .tmp "REGTEMP"
     17 
     18 ;
     19 ; Returns an 8-bit / 16-bit rand value and updates the seed.
     20 ;
     21 _arand::
     22 _rand::
     23 _randw::
     24     ; rand += 17 * rand
     25     lda *.randhi
     26     sta *.tmp
     27     lda *.randlo
     28     asl
     29     rol *.tmp
     30     asl
     31     rol *.tmp
     32     asl
     33     rol *.tmp
     34     asl
     35     rol *.tmp
     36     ;
     37     clc
     38     adc *.randlo
     39     sta *.randlo
     40     lda *.tmp
     41     adc *.randhi
     42     sta *.randhi
     43     ; rand += 0x5C93
     44     lda *.randlo
     45     clc
     46     adc #<0x5C93
     47     sta *.randlo
     48     tax
     49     lda *.randhi
     50     adc #>0x5C93
     51     sta *.randhi
     52     ; Return high byte of random number for 8-bit. Swapped low/high for 16-bit
     53     rts
     54 
     55 ;
     56 ; Sets the seed value.
     57 ;
     58 _initarand::
     59 _initrand::
     60 .initrand::
     61     sta *.randlo
     62     stx *.randhi
     63     rts

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.