RosettaCodeData/Task/Knuth-shuffle/6502-Assembly/knuth-shuffle.6502
2023-07-01 13:44:08 -04:00

69 lines
872 B
Text

define sysRandom $fe
define tempMask $ff
define range $00
define tempX $01
define tempY $02
define tempRandIndex $03
define temp $04
CreateIdentityTable:
txa
sta $0200,x
sta $1000,x
inx
bne CreateIdentityTable
;creates a sorted array from 0-255 starting at addr $1000
;also creates another one at $0200 for our test input
lda #1
sta range
ConstrainRNG:
ldx #255
;max range of RNG
lda range
bne outerloop
jmp end
outerloop:
cpx range
bcc continue ;if X >= range, we need to lower X
pha
txa
sta tempX
lsr
cmp range
bcc continue2
tax
pla
jmp outerloop
continue2:
pla
ldx tempX
continue:
ldy range
KnuthShuffle:
lda sysRandom
and $1000,x ;and with range constrictor
tay
lda $0200,y
sty tempRandIndex
sta temp
ldy range
lda $0200,y
pha
lda temp
sta $0200,y
pla
ldy tempRandIndex
sta $0200,y
dec range
jmp ConstrainRNG
end:
brk