ct, count, copyfrom, test sind alles zeropage words
t_rand ist die Tabelle, wo die 16bit Werte abgelegt werden (also ist sie am Ende exakt doppelt so groß, we man Zahlen haben möchte.
Code: Alles auswählen
.zpvar .word count,copyfrom,ct,test
.proc gen_zufall ;800 16bit Zufallszahlen ohne Wiederholung erstellen
mwa #0 count
.local gen_rand16
lda random
sta copyfrom
@ lda random
and #$f
cmp #4
bcs @-
sta copyfrom+1
cpw copyfrom #800 ;gewünschte maximale Zahl
bcs gen_rand16
;--- Test ob schon eingeschrieben in unserer Tabelle
mwa #0 ct
mwa #t_rand test
lp ldy #1
lda copyfrom+1
cmp (test),y
bcc next ;kleiner
bne next ;größer
dey
lda copyfrom
cmp (test),y
beq gen_rand16 ;gleich -> neue Zahl erstellen
next inw ct
adw test #2
cpw count ct
bcs lp
;---
set lda copyfrom
chg sta t_rand
lda copyfrom+1
chg1 sta t_rand+1
inw count
adw chg+1 #2
adw chg1+1 #2
cpw count #799 ;gewünschte Anzahl-1
jcc gen_rand16
.endl
rts
.endp
;---
t_rand
.ds 1600
Ich benötigte exakt 800 Zahlen, zufällig und alle von 0 bis 799. Wenn Ihr nur einfach Zufallszahlen benötigt, die sich nicht wiederholen, kann das:
Code: Alles auswählen
and #$f
cmp #4
bcs @-
Code: Alles auswählen
cpw copyfrom #800 ;gewünschte maximale Zahl
bcs gen_rand16
Wo doppelte Zahlen kein Problem sind, einfach alles zwischen den beiden ";---" weglassen. Dann ist das auch echt schnell.