Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save UnQuaiz/6a81b82d065b054148d853caf66de2cf to your computer and use it in GitHub Desktop.

Select an option

Save UnQuaiz/6a81b82d065b054148d853caf66de2cf to your computer and use it in GitHub Desktop.

Revisions

  1. UnQuaiz renamed this gist Aug 18, 2019. 1 changed file with 0 additions and 0 deletions.
  2. @JohnConnolly0 JohnConnolly0 created this gist Feb 19, 2015.
    21 changes: 21 additions & 0 deletions ZX Spectrum Random Numbers
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    LD A,R ; Load the A register with the refresh register
    LD L,A ; Copy register A into register L
    AND %00111111 ; This masking prevents the address we are forming from accessing RAM
    LD H,A ; Copy register A into register H
    LD A,(HL) ; Load the pseudo-random value into A

    ; HOW THIS WORKS

    ; The refresh register in the Z80 is highly unpredictable since it is incremented every cycle.
    ; Because it may be at any value when this routine is called, it is very good for random numbers.
    ; This routine increases the randomness of the number since it forms an address based on the
    ; refresh counter's current status and accesses the memory at that address.

    ; It can also be modified to get a sixteen-bit pseudo-random number by changing line 5 to LD D,(HL)
    ; and adding these two lines to the end:
    ; INC L
    ; LD E,(HL)

    ; This routine was written for the ZX Spectrum which has a 16KB ROM. If you plan to use this routine
    ; on another Z80 system, change the binary value at the AND instruction. For example, if you had a
    ; Z80 computer with an 8KB ROM, you would change the binary value to %00011111.