From 8b84f8721702fd98bf63014358d7f063b10985f7 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 28 Apr 2021 01:53:12 +0200 Subject: [PATCH] removed fastrnd8() because it was hilariously bad, just use rnd() --- compiler/res/prog8lib/cx16/gfx2.p8 | 1 + compiler/res/prog8lib/math.asm | 85 +++++++------------ compiler/res/prog8lib/prog8_funcs.asm | 8 -- .../compiler/functions/BuiltinFunctions.kt | 1 - .../cpu6502/codegen/BuiltinFunctionsAsmGen.kt | 10 +-- docs/source/programming.rst | 3 - examples/balloonflight.p8 | 10 +-- examples/balls.p8 | 11 ++- examples/sprites.p8 | 4 +- examples/test.p8 | 28 +++--- syntax-files/IDEA/Prog8.xml | 2 +- syntax-files/Vim/prog8_builtins.vim | 2 +- 12 files changed, 59 insertions(+), 106 deletions(-) diff --git a/compiler/res/prog8lib/cx16/gfx2.p8 b/compiler/res/prog8lib/cx16/gfx2.p8 index 9551030a9..284ef22d8 100644 --- a/compiler/res/prog8lib/cx16/gfx2.p8 +++ b/compiler/res/prog8lib/cx16/gfx2.p8 @@ -573,6 +573,7 @@ _done } } } + ; TODO mode 2,3 4 -> { ; lores 256c void addr_mul_24_for_lores_256c(y, x) ; 24 bits result is in r0 and r1L (highest byte) diff --git a/compiler/res/prog8lib/math.asm b/compiler/res/prog8lib/math.asm index cda53d70a..a06c8d0c3 100644 --- a/compiler/res/prog8lib/math.asm +++ b/compiler/res/prog8lib/math.asm @@ -244,25 +244,6 @@ randseed .proc .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 ; -- 8 bit pseudo random number generator into A (by just reusing randword) jmp randword @@ -271,45 +252,37 @@ randbyte .proc randword .proc ; -- 16 bit pseudo random number generator into AY -magic_eor = $3f1d - ; possible magic eor words are: - ; $3f1d, $3f81, $3fa5, $3fc5, $4075, $409d, $40cd, $4109 - ; $413f, $414b, $4153, $4159, $4193, $4199, $41af, $41bb + ; rand64k ;Factors of 65535: 3 5 17 257 + lda sr1+1 + asl a + asl a + eor sr1+1 + asl a + eor sr1+1 + asl a + 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 - lda _seed - beq _lowZero ; $0000 and $8000 are special values to test for +sr1 .word $a55a +sr2 .word $7653 - ; 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 # = listOf( 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("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("rndw" , false, emptyList(), DataType.UWORD), FSignature("rndf" , false, emptyList(), DataType.FLOAT), diff --git a/compiler/src/prog8/compiler/target/cpu6502/codegen/BuiltinFunctionsAsmGen.kt b/compiler/src/prog8/compiler/target/cpu6502/codegen/BuiltinFunctionsAsmGen.kt index b319b74a3..db1a4b6f2 100644 --- a/compiler/src/prog8/compiler/target/cpu6502/codegen/BuiltinFunctionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/cpu6502/codegen/BuiltinFunctionsAsmGen.kt @@ -52,7 +52,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val "ln", "log2", "sqrt", "rad", "deg", "round", "floor", "ceil", "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) "rol" -> funcRol(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?) { 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" -> { if(resultToStack) asmgen.out(" jsr prog8_lib.func_rnd_stack") diff --git a/docs/source/programming.rst b/docs/source/programming.rst index e5f610f6e..bd9702f75 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -845,9 +845,6 @@ rndw() rndf() 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) 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, diff --git a/examples/balloonflight.p8 b/examples/balloonflight.p8 index d8fd8fd0a..1b7fe3a5d 100644 --- a/examples/balloonflight.p8 +++ b/examples/balloonflight.p8 @@ -42,7 +42,7 @@ main { active_height-- upwards = false } else { - target_height = 8 + fastrnd8() % 16 + target_height = 8 + rnd() % 16 if upwards mountain = 233 else @@ -57,7 +57,7 @@ main { txt.scroll_left(true) ; float the balloon - if fastrnd8() & %10000 + if rnd() & %10000 c64.SPXY[1] ++ else c64.SPXY[1] -- @@ -71,10 +71,10 @@ main { txt.setcc(39, yy, 160, 8) ; draw mountain } - yy = fastrnd8() + yy = rnd() if yy > 100 { ; draw a star - txt.setcc(39, yy % (active_height-1), '.', fastrnd8()) + txt.setcc(39, yy % (active_height-1), '.', rnd()) } if yy > 200 { @@ -85,7 +85,7 @@ main { tree = 88 else if yy & %00100000 != 0 tree = 65 - if fastrnd8() > 130 + if rnd() > 130 treecolor = 13 txt.setcc(39, active_height, tree, treecolor) } diff --git a/examples/balls.p8 b/examples/balls.p8 index e56e91fac..fcf421980 100644 --- a/examples/balls.p8 +++ b/examples/balls.p8 @@ -24,12 +24,11 @@ main { ; Setup Starting Ball Positions ubyte lp for lp in 0 to ballCount-1 { - BX[lp] = fastrnd8() % txt.DEFAULT_WIDTH - BY[lp] = fastrnd8() % txt.DEFAULT_HEIGHT - BC[lp] = fastrnd8() & 15 - DX[lp] = fastrnd8() & 1 - DY[lp] = fastrnd8() & 1 - void fastrnd8() + BX[lp] = rnd() % txt.DEFAULT_WIDTH + BY[lp] = rnd() % txt.DEFAULT_HEIGHT + BC[lp] = rnd() & 15 + DX[lp] = rnd() & 1 + DY[lp] = rnd() & 1 } ; start clock diff --git a/examples/sprites.p8 b/examples/sprites.p8 index 442835ac1..938204527 100644 --- a/examples/sprites.p8 +++ b/examples/sprites.p8 @@ -42,7 +42,7 @@ main { for i in 0 to 7 { c64.SPRPTR[i] = $0a00 / 64 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 @@ -60,7 +60,7 @@ irq { ubyte @zp i for i in 0 to 14 step 2 { c64.SPXY[i+1]-- - ubyte @zp r = fastrnd8() + ubyte @zp r = rnd() if r>200 c64.SPXY[i]++ else if r<40 diff --git a/examples/test.p8 b/examples/test.p8 index fa5618701..6fb8d1d3d 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,29 +1,29 @@ %import conv %import gfx2 +%import textio main { sub start() { - gfx2.screen_mode(1) ; high res 4c + gfx2.screen_mode(1) ; lo res gfx2.text_charset(3) gfx2.text(10, 10, 1, @"Hello!") c64.SETTIM(0,0,0) + ubyte yy + uword rw - repeat 2 { - uword xx - gfx2.monochrome_stipple(true) - for xx in 0 to 319 { - gfx2.vertical_line(xx, 20, 200, 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) - } + ;413 jiffies (lores mono) / 480 jiffies (highres mono) / 368 jiffies (lores 256c) / 442 jiffies (lores 4c) + repeat 50000 { + rw = rndw() + yy = (lsb(rw) & 127) + 20 + gfx2.plot(msb(rw), yy, 1) + } + repeat 50000 { + rw = rndw() + yy = (lsb(rw) & 127) + 20 + gfx2.plot(msb(rw), yy, 0) } uword time = c64.RDTIM16() diff --git a/syntax-files/IDEA/Prog8.xml b/syntax-files/IDEA/Prog8.xml index c16e8a749..9ed08c9aa 100644 --- a/syntax-files/IDEA/Prog8.xml +++ b/syntax-files/IDEA/Prog8.xml @@ -14,7 +14,7 @@ - + diff --git a/syntax-files/Vim/prog8_builtins.vim b/syntax-files/Vim/prog8_builtins.vim index a5c2eb5e5..80e49c0f6 100644 --- a/syntax-files/Vim/prog8_builtins.vim +++ b/syntax-files/Vim/prog8_builtins.vim @@ -16,7 +16,7 @@ syn keyword prog8BuiltInFunc any all len max min reverse sum sort " Miscellaneous functions 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