From 3a0c1c5ada65965e28d5ce8b45b527f2f9890674 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 23 Sep 2018 02:42:42 +0200 Subject: [PATCH] optimized mandelbrot example --- compiler/examples/imported.p8 | 38 ----------------------- compiler/examples/imported2.p8 | 41 ------------------------- compiler/examples/mandelbrot.p8 | 41 +++++++++++++------------ compiler/src/prog8/compiler/Compiler.kt | 2 ++ 4 files changed, 23 insertions(+), 99 deletions(-) delete mode 100644 compiler/examples/imported.p8 delete mode 100644 compiler/examples/imported2.p8 diff --git a/compiler/examples/imported.p8 b/compiler/examples/imported.p8 deleted file mode 100644 index 2251ef37f..000000000 --- a/compiler/examples/imported.p8 +++ /dev/null @@ -1,38 +0,0 @@ -%zeropage full -%address 33 -%option enable_floats -%option enable_floats - -~ extra { - ; this is imported - - X = 42 - return 44 -} - -~ extra2 { - ; this is imported - - X = 42 - return 44 - -label_in_extra2: - X = 33 - return - - sub sub_in_extra2() -> () { - return - } - sub another_sub_in_extra2() -> () { - return - } -} - - -~ main2 { - ; this is imported - - X = 42 - return 44 -} - diff --git a/compiler/examples/imported2.p8 b/compiler/examples/imported2.p8 deleted file mode 100644 index cf4b0e3af..000000000 --- a/compiler/examples/imported2.p8 +++ /dev/null @@ -1,41 +0,0 @@ -%zeropage full -%address 33 - -~ extra3 { - ; this is imported - - X = 42 - return 44 -} - -~ extra233 { - ; this is imported - - const byte snerp=33 - const byte snerp2 = snerp+22 - - X = 42+snerp - ;return 44+snerp - return - - sub foo234234() -> () { - A=99+snerp - ;return A+snerp2 - return - } - - sub thingy()->(X) { - ;return 99 - return - } -} - - -~ mainzzz { - ; this is imported - - X = 42 - ;return 44 - return -} - diff --git a/compiler/examples/mandelbrot.p8 b/compiler/examples/mandelbrot.p8 index c937d8591..b8f585c1c 100644 --- a/compiler/examples/mandelbrot.p8 +++ b/compiler/examples/mandelbrot.p8 @@ -4,48 +4,49 @@ sub start() -> () { - const word width = 320 - const word height = 256 + const word width = 320 // 2 + const word height = 256 // 2 + const word xoffset = 40 + const word yoffset = 20 word pixelx byte pixely float xx float yy float x float y - float x2 + float xsq + float ysq byte iter word plotx byte ploty - _vm_write_str("Calculating Mandelbrot fractal, have patience...\n") _vm_gfx_clearscr(11) + _vm_gfx_text(5, 5, 7, "Calculating Mandelbrot Fractal...") - for pixely in 0 to height-1 { - for pixelx in 0 to width-1 { - xx = pixelx/width/3+0.2 - yy = pixely/height/3.6+0.4 + for pixely in yoffset to yoffset+height-1 { + yy = (pixely-yoffset)/height/3.6+0.4 + + for pixelx in xoffset to xoffset+width-1 { + xx = (pixelx-xoffset)/width/3+0.2 x = 0.0 y = 0.0 - - for iter in 0 to 31 { - if (x*x + y*y > 4) break - x2 = x*x - y*y + xx + xsq = 0 + ysq = 0 + iter = 0 + while (iter<32 and xsq+ysq<4) { y = x*y*2 + yy - x = x2 + x = xsq - ysq + xx + xsq = x*x + ysq = y*y + iter++ } _vm_gfx_pixel(pixelx, pixely, iter) -; plotx = pixelx*2 -; ploty = pixely*2 -; _vm_gfx_pixel(plotx, ploty, iter) -; _vm_gfx_pixel(plotx+1, ploty, iter) -; _vm_gfx_pixel(plotx, ploty+1, iter) -; _vm_gfx_pixel(plotx+1, ploty+1, iter) } } - _vm_gfx_text(5, 5, 7, "Mandelbrot Fractal") + _vm_gfx_text(110, 160, 7, "Finished!") } } diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 08a6f64b5..b79134bc0 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -794,6 +794,7 @@ private class StatementTranslator(private val stackvmProg: StackVmProgram, priva val loopLabel = makeLabel("loop") val breakLabel = makeLabel("break") val continueLabel = makeLabel("continue") + stackvmProg.line(stmt.position) breakStmtLabelStack.push(breakLabel) continueStmtLabelStack.push(continueLabel) stackvmProg.instr(Opcode.JUMP, callLabel = continueLabel) @@ -827,6 +828,7 @@ private class StatementTranslator(private val stackvmProg: StackVmProgram, priva val loopLabel = makeLabel("loop") val continueLabel = makeLabel("continue") val breakLabel = makeLabel("break") + stackvmProg.line(stmt.position) breakStmtLabelStack.push(breakLabel) continueStmtLabelStack.push(continueLabel) stackvmProg.label(loopLabel)