removed fastrnd8() because it was hilariously bad, just use rnd()

This commit is contained in:
Irmen de Jong 2021-04-28 01:53:12 +02:00
parent 306a1b7bc2
commit 8b84f87217
12 changed files with 59 additions and 106 deletions

View File

@ -573,6 +573,7 @@ _done
} }
} }
} }
; TODO mode 2,3
4 -> { 4 -> {
; lores 256c ; lores 256c
void addr_mul_24_for_lores_256c(y, x) ; 24 bits result is in r0 and r1L (highest byte) void addr_mul_24_for_lores_256c(y, x) ; 24 bits result is in r0 and r1L (highest byte)

View File

@ -244,25 +244,6 @@ randseed .proc
.pend .pend
fast_randbyte .proc
; -- fast but bad 8-bit pseudo random number generator into A
lda _seed
beq _eor
asl a
beq _done ; if the input was $80, skip the EOR
bcc _done
_eor eor #$1d ; xor with magic value see below for possible values
_done sta _seed
rts
_seed .byte $3a
; possible 'magic' eor bytes are:
; $1d, $2b, $2d, $4d, $5f, $63, $65, $69
; $71, $87, $8d, $a9, $c3, $cf, $e7, $f5
.pend
randbyte .proc randbyte .proc
; -- 8 bit pseudo random number generator into A (by just reusing randword) ; -- 8 bit pseudo random number generator into A (by just reusing randword)
jmp randword jmp randword
@ -271,45 +252,37 @@ randbyte .proc
randword .proc randword .proc
; -- 16 bit pseudo random number generator into AY ; -- 16 bit pseudo random number generator into AY
magic_eor = $3f1d ; rand64k ;Factors of 65535: 3 5 17 257
; possible magic eor words are: lda sr1+1
; $3f1d, $3f81, $3fa5, $3fc5, $4075, $409d, $40cd, $4109
; $413f, $414b, $4153, $4159, $4193, $4199, $41af, $41bb
lda _seed
beq _lowZero ; $0000 and $8000 are special values to test for
; Do a normal shift
asl _seed
lda _seed+1
rol a
bcc _noEor
_doEor ; high byte is in A
eor #>magic_eor
sta _seed+1
lda _seed
eor #<magic_eor
sta _seed
ldy _seed+1
rts
_lowZero lda _seed+1
beq _doEor ; High byte is also zero, so apply the EOR
; For speed, you could store 'magic' into 'seed' directly
; instead of running the EORs
; wasn't zero, check for $8000
asl a asl a
beq _noEor ; if $00 is left after the shift, then it was $80 asl a
bcs _doEor ; else, do the EOR based on the carry bit as usual eor sr1+1
asl a
_noEor sta _seed+1 eor sr1+1
tay asl a
lda _seed asl a
eor sr1+1
asl a
rol sr1 ;shift this left, "random" bit comes from low
rol sr1+1
; rand32k ;Factors of 32767: 7 31 151 are independent and can be combined
lda sr2+1
asl a
eor sr2+1
asl a
asl a
ror sr2 ;shift this right, random bit comes from high - nicer when eor with sr1
rol sr2+1
lda sr1+1 ;can be left out
eor sr2+1 ;if you dont use
tay ;y as suggested
lda sr1 ;mix up lowbytes of SR1
eor sr2 ;and SR2 to combine both
rts rts
_seed .word $2c9e sr1 .word $a55a
sr2 .word $7653
.pend .pend

View File

@ -387,14 +387,6 @@ func_sqrt16_into_A .proc
rts rts
.pend .pend
func_fastrnd8_stack .proc
; -- put a random ubyte on the estack (using fast but bad RNG)
jsr math.fast_randbyte
sta P8ESTACK_LO,x
dex
rts
.pend
func_rnd_stack .proc func_rnd_stack .proc
; -- put a random ubyte on the estack ; -- put a random ubyte on the estack
jsr math.randbyte jsr math.randbyte

View File

@ -138,7 +138,6 @@ private val functionSignatures: List<FSignature> = listOf(
FSignature("peekw" , true, listOf(FParam("address", setOf(DataType.UWORD))), DataType.UWORD), FSignature("peekw" , true, listOf(FParam("address", setOf(DataType.UWORD))), DataType.UWORD),
FSignature("poke" , false, listOf(FParam("address", setOf(DataType.UWORD)), FParam("value", setOf(DataType.UBYTE))), null), FSignature("poke" , false, listOf(FParam("address", setOf(DataType.UWORD)), FParam("value", setOf(DataType.UBYTE))), null),
FSignature("pokew" , false, listOf(FParam("address", setOf(DataType.UWORD)), FParam("value", setOf(DataType.UWORD))), null), FSignature("pokew" , false, listOf(FParam("address", setOf(DataType.UWORD)), FParam("value", setOf(DataType.UWORD))), null),
FSignature("fastrnd8" , false, emptyList(), DataType.UBYTE),
FSignature("rnd" , false, emptyList(), DataType.UBYTE), FSignature("rnd" , false, emptyList(), DataType.UBYTE),
FSignature("rndw" , false, emptyList(), DataType.UWORD), FSignature("rndw" , false, emptyList(), DataType.UWORD),
FSignature("rndf" , false, emptyList(), DataType.FLOAT), FSignature("rndf" , false, emptyList(), DataType.FLOAT),

View File

@ -52,7 +52,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
"ln", "log2", "sqrt", "rad", "ln", "log2", "sqrt", "rad",
"deg", "round", "floor", "ceil", "deg", "round", "floor", "ceil",
"rndf" -> funcVariousFloatFuncs(fcall, func, resultToStack, resultRegister, sscope) "rndf" -> funcVariousFloatFuncs(fcall, func, resultToStack, resultRegister, sscope)
"fastrnd8", "rnd", "rndw" -> funcRnd(func, resultToStack, resultRegister, sscope) "rnd", "rndw" -> funcRnd(func, resultToStack, resultRegister, sscope)
"sqrt16" -> funcSqrt16(fcall, func, resultToStack, resultRegister, sscope) "sqrt16" -> funcSqrt16(fcall, func, resultToStack, resultRegister, sscope)
"rol" -> funcRol(fcall) "rol" -> funcRol(fcall)
"rol2" -> funcRol2(fcall) "rol2" -> funcRol2(fcall)
@ -1115,14 +1115,6 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
private fun funcRnd(func: FSignature, resultToStack: Boolean, resultRegister: RegisterOrPair?, scope: Subroutine?) { private fun funcRnd(func: FSignature, resultToStack: Boolean, resultRegister: RegisterOrPair?, scope: Subroutine?) {
when(func.name) { when(func.name) {
"fastrnd8" -> {
if(resultToStack)
asmgen.out(" jsr prog8_lib.func_fastrnd8_stack")
else {
asmgen.out(" jsr math.fast_randbyte")
assignAsmGen.assignRegisterByte(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.A, scope, program, asmgen), CpuRegister.A)
}
}
"rnd" -> { "rnd" -> {
if(resultToStack) if(resultToStack)
asmgen.out(" jsr prog8_lib.func_rnd_stack") asmgen.out(" jsr prog8_lib.func_rnd_stack")

View File

@ -845,9 +845,6 @@ rndw()
rndf() rndf()
returns a pseudo-random float between 0.0 and 1.0 returns a pseudo-random float between 0.0 and 1.0
fastrnd8()
returns a pseudo-random byte from 0..255 (using a fast but not very good rng)
rol(x) rol(x)
Rotate the bits in x (byte or word) one position to the left. Rotate the bits in x (byte or word) one position to the left.
This uses the CPU's rotate semantics: bit 0 will be set to the current value of the Carry flag, This uses the CPU's rotate semantics: bit 0 will be set to the current value of the Carry flag,

View File

@ -42,7 +42,7 @@ main {
active_height-- active_height--
upwards = false upwards = false
} else { } else {
target_height = 8 + fastrnd8() % 16 target_height = 8 + rnd() % 16
if upwards if upwards
mountain = 233 mountain = 233
else else
@ -57,7 +57,7 @@ main {
txt.scroll_left(true) txt.scroll_left(true)
; float the balloon ; float the balloon
if fastrnd8() & %10000 if rnd() & %10000
c64.SPXY[1] ++ c64.SPXY[1] ++
else else
c64.SPXY[1] -- c64.SPXY[1] --
@ -71,10 +71,10 @@ main {
txt.setcc(39, yy, 160, 8) ; draw mountain txt.setcc(39, yy, 160, 8) ; draw mountain
} }
yy = fastrnd8() yy = rnd()
if yy > 100 { if yy > 100 {
; draw a star ; draw a star
txt.setcc(39, yy % (active_height-1), '.', fastrnd8()) txt.setcc(39, yy % (active_height-1), '.', rnd())
} }
if yy > 200 { if yy > 200 {
@ -85,7 +85,7 @@ main {
tree = 88 tree = 88
else if yy & %00100000 != 0 else if yy & %00100000 != 0
tree = 65 tree = 65
if fastrnd8() > 130 if rnd() > 130
treecolor = 13 treecolor = 13
txt.setcc(39, active_height, tree, treecolor) txt.setcc(39, active_height, tree, treecolor)
} }

View File

@ -24,12 +24,11 @@ main {
; Setup Starting Ball Positions ; Setup Starting Ball Positions
ubyte lp ubyte lp
for lp in 0 to ballCount-1 { for lp in 0 to ballCount-1 {
BX[lp] = fastrnd8() % txt.DEFAULT_WIDTH BX[lp] = rnd() % txt.DEFAULT_WIDTH
BY[lp] = fastrnd8() % txt.DEFAULT_HEIGHT BY[lp] = rnd() % txt.DEFAULT_HEIGHT
BC[lp] = fastrnd8() & 15 BC[lp] = rnd() & 15
DX[lp] = fastrnd8() & 1 DX[lp] = rnd() & 1
DY[lp] = fastrnd8() & 1 DY[lp] = rnd() & 1
void fastrnd8()
} }
; start clock ; start clock

View File

@ -42,7 +42,7 @@ main {
for i in 0 to 7 { for i in 0 to 7 {
c64.SPRPTR[i] = $0a00 / 64 c64.SPRPTR[i] = $0a00 / 64
c64.SPXY[i*2] = 50+25*i c64.SPXY[i*2] = 50+25*i
c64.SPXY[i*2+1] = fastrnd8() c64.SPXY[i*2+1] = rnd()
} }
c64.SPENA = 255 ; enable all sprites c64.SPENA = 255 ; enable all sprites
@ -60,7 +60,7 @@ irq {
ubyte @zp i ubyte @zp i
for i in 0 to 14 step 2 { for i in 0 to 14 step 2 {
c64.SPXY[i+1]-- c64.SPXY[i+1]--
ubyte @zp r = fastrnd8() ubyte @zp r = rnd()
if r>200 if r>200
c64.SPXY[i]++ c64.SPXY[i]++
else if r<40 else if r<40

View File

@ -1,29 +1,29 @@
%import conv %import conv
%import gfx2 %import gfx2
%import textio
main { main {
sub start() { sub start() {
gfx2.screen_mode(1) ; high res 4c gfx2.screen_mode(1) ; lo res
gfx2.text_charset(3) gfx2.text_charset(3)
gfx2.text(10, 10, 1, @"Hello!") gfx2.text(10, 10, 1, @"Hello!")
c64.SETTIM(0,0,0) c64.SETTIM(0,0,0)
ubyte yy
uword rw
repeat 2 { ;413 jiffies (lores mono) / 480 jiffies (highres mono) / 368 jiffies (lores 256c) / 442 jiffies (lores 4c)
uword xx repeat 50000 {
gfx2.monochrome_stipple(true) rw = rndw()
for xx in 0 to 319 { yy = (lsb(rw) & 127) + 20
gfx2.vertical_line(xx, 20, 200, 1) gfx2.plot(msb(rw), yy, 1)
}
gfx2.monochrome_stipple(false)
for xx in 0 to 319 {
gfx2.vertical_line(xx, 20, 200, 1)
}
for xx in 0 to 319 {
gfx2.vertical_line(xx, 20, 200, 0)
} }
repeat 50000 {
rw = rndw()
yy = (lsb(rw) & 127) + 20
gfx2.plot(msb(rw), yy, 0)
} }
uword time = c64.RDTIM16() uword time = c64.RDTIM16()

View File

@ -14,7 +14,7 @@
<keywords keywords="&amp;;-&gt;;@;\$;and;as;asmsub;break;clobbers;do;downto;else;false;for;goto;if;if_cc;if_cs;if_eq;if_mi;if_ne;if_neg;if_nz;if_pl;if_pos;if_vc;if_vs;if_z;in;inline;not;or;repeat;return;romsub;step;sub;to;true;until;when;while;xor;~" ignore_case="false" /> <keywords keywords="&amp;;-&gt;;@;\$;and;as;asmsub;break;clobbers;do;downto;else;false;for;goto;if;if_cc;if_cs;if_eq;if_mi;if_ne;if_neg;if_nz;if_pl;if_pos;if_vc;if_vs;if_z;in;inline;not;or;repeat;return;romsub;step;sub;to;true;until;when;while;xor;~" ignore_case="false" />
<keywords2 keywords="%address;%asm;%asmbinary;%asminclude;%breakpoint;%import;%launcher;%option;%output;%target;%zeropage;%zpreserved" /> <keywords2 keywords="%address;%asm;%asmbinary;%asminclude;%breakpoint;%import;%launcher;%option;%output;%target;%zeropage;%zpreserved" />
<keywords3 keywords="byte;const;float;str;struct;ubyte;uword;void;word;zp" /> <keywords3 keywords="byte;const;float;str;struct;ubyte;uword;void;word;zp" />
<keywords4 keywords="abs;acos;all;any;asin;atan;avg;callfar;ceil;cmp;cos;cos16;cos16u;cos8;cos8u;deg;fastrnd8;floor;len;ln;log2;lsb;lsl;lsr;max;memory;min;mkword;msb;offsetof;peek;peekw;poke;pokew;rad;reverse;rnd;rndf;rndw;rol;rol2;ror;ror2;round;sgn;sin;sin16;sin16u;sin8;sin8u;sizeof;sort;sqrt;sqrt16;sum;swap;tan" /> <keywords4 keywords="abs;acos;all;any;asin;atan;avg;callfar;callrom;ceil;cmp;cos;cos16;cos16u;cos8;cos8u;deg;floor;len;ln;log2;lsb;lsl;lsr;max;memory;min;mkword;msb;offsetof;peek;peekw;poke;pokew;rad;reverse;rnd;rndf;rndw;rol;rol2;ror;ror2;round;sgn;sin;sin16;sin16u;sin8;sin8u;sizeof;sort;sqrt;sqrt16;sum;swap;tan" />
</highlighting> </highlighting>
<extensionMap> <extensionMap>
<mapping ext="p8" /> <mapping ext="p8" />

View File

@ -16,7 +16,7 @@ syn keyword prog8BuiltInFunc any all len max min reverse sum sort
" Miscellaneous functions " Miscellaneous functions
syn keyword prog8BuiltInFunc cmp lsb msb mkword peek peekw poke pokew rnd rndw syn keyword prog8BuiltInFunc cmp lsb msb mkword peek peekw poke pokew rnd rndw
syn keyword prog8BuiltInFunc rndf fastrnd8 rol rol2 ror ror2 sizeof offsetof syn keyword prog8BuiltInFunc rndf rol rol2 ror ror2 sizeof offsetof
syn keyword prog8BuiltInFunc swap memory callfar callrom syn keyword prog8BuiltInFunc swap memory callfar callrom