Skip to content

Instantly share code, notes, and snippets.

@bhickey
Created February 5, 2017 17:57
Show Gist options
  • Save bhickey/0de228c02cc60b5965582d2d946d8c38 to your computer and use it in GitHub Desktop.
Save bhickey/0de228c02cc60b5965582d2d946d8c38 to your computer and use it in GitHub Desktop.
6502 xorshift, 8-bit full period
; r ^= (r << 1)
; r ^= (r >> 1)
; r ^= (r << 2)
xorshift:
lda <RAND_SEED
asl a
eor <RAND_SEED
tax
asr a
eor x
tax
asr a
asr a
eor x
rts
@hata6502
Copy link

hata6502 commented Jul 5, 2020

Version without X register:

Random:
  lda random
  asl a
  eor random
  sta random
  lsr a
  eor random
  sta random
  asl a
  asl a
  eor random
  sta random
  rts

@hata6502
Copy link

hata6502 commented Jul 5, 2020

16 bit version:
(It may be incorrect...)

Random:
  lda random + 1
  pha
  lda random
  asl a
  rol random + 1
  asl a
  rol random + 1
  asl a
  rol random + 1
  asl a
  rol random + 1
  asl a
  rol random + 1
  asl a
  rol random + 1
  asl a
  rol random + 1
  eor random
  sta random
  pla
  eor random + 1
  sta random + 1
  lsr a
  eor random
  sta random
  eor random + 1
  sta random + 1
  rts

http://www.retroprogramming.com/2017/07/xorshift-pseudorandom-numbers-in-z80.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment