diff --git a/README.md b/README.md index 3ca6032ab..84423ebf8 100644 --- a/README.md +++ b/README.md @@ -50,55 +50,58 @@ of the [Vice emulator](http://vice-emu.sourceforge.net/) Example code ------------ -When this code is compiled:: +This code calculates prime numbers using the Sieve of Eratosthenes algorithm:: - %import c64lib %import c64utils - %import c64flt + %zeropage basicsafe ~ main { + + ubyte[256] sieve + ubyte candidate_prime = 2 + sub start() { - ; set text color and activate lowercase charset - c64.COLOR = 13 - c64.VMCSB |= 2 + memset(sieve, 256, false) - ; use optimized routine to write text - c64scr.print("Hello!\n") - - ; use iteration to write text - str question = "How are you?\n" - for ubyte char in question - c64.CHROUT(char) - - ; use indexed loop to write characters - str bye = "Goodbye!\n" - for ubyte c in 0 to len(bye) - c64.CHROUT(bye[c]) - - - float clock_seconds = ((mkword(c64.TIME_LO, c64.TIME_MID) as float) - + (c64.TIME_HI as float)*65536.0) - / 60 - float hours = floor(clock_seconds / 3600) - clock_seconds -= hours*3600 - float minutes = floor(clock_seconds / 60) - clock_seconds = floor(clock_seconds - minutes * 60.0) - - c64scr.print("system time in ti$ is ") - c64flt.print_f(hours) - c64.CHROUT(':') - c64flt.print_f(minutes) - c64.CHROUT(':') - c64flt.print_f(clock_seconds) + c64scr.print("prime numbers up to 255:\n\n") + ubyte amount=0 + while true { + ubyte prime = find_next_prime() + if prime==0 + break + c64scr.print_ub(prime) + c64scr.print(", ") + amount++ + } c64.CHROUT('\n') + c64scr.print("number of primes (expected 54): ") + c64scr.print_ub(amount) + c64.CHROUT('\n') + } + + + sub find_next_prime() -> ubyte { + + while sieve[candidate_prime] { + candidate_prime++ + if candidate_prime==0 + return 0 + } + + sieve[candidate_prime] = true + uword multiple = candidate_prime + while multiple < len(sieve) { + sieve[lsb(multiple)] = true + multiple += candidate_prime + } + return candidate_prime } } +when compiled an ran on a C-64 you'll get: -you get a program that outputs this when loaded on a C-64: - -![c64 screen](docs/source/_static/hello_screen.png) +![c64 screen](docs/source/_static/primes_example.png) One of the included examples (wizzine.p8) animates a bunch of sprite balloons and looks like this: diff --git a/compiler/res/version.txt b/compiler/res/version.txt index 54ea73dde..810ee4e91 100644 --- a/compiler/res/version.txt +++ b/compiler/res/version.txt @@ -1 +1 @@ -1.6 (beta) +1.6 diff --git a/docs/source/_static/hello_screen.png b/docs/source/_static/hello_screen.png deleted file mode 100644 index 6e195427e..000000000 Binary files a/docs/source/_static/hello_screen.png and /dev/null differ diff --git a/docs/source/_static/primes_example.png b/docs/source/_static/primes_example.png new file mode 100644 index 000000000..39012d0f5 Binary files /dev/null and b/docs/source/_static/primes_example.png differ diff --git a/docs/source/index.rst b/docs/source/index.rst index 463d71f8c..69db6ce13 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -40,55 +40,58 @@ This software is licensed under the GNU GPL 3.0, see https://www.gnu.org/license Code example ------------ -When this code is compiled:: +This code calculates prime numbers using the Sieve of Eratosthenes algorithm:: - %import c64lib %import c64utils - %import c64flt + %zeropage basicsafe ~ main { + + ubyte[256] sieve + ubyte candidate_prime = 2 + sub start() { - ; set text color and activate lowercase charset - c64.COLOR = 13 - c64.VMCSB |= 2 + memset(sieve, 256, false) - ; use optimized routine to write text - c64scr.print("Hello!\n") - - ; use iteration to write text - str question = "How are you?\n" - for ubyte char in question - c64.CHROUT(char) - - ; use indexed loop to write characters - str bye = "Goodbye!\n" - for ubyte c in 0 to len(bye) - c64.CHROUT(bye[c]) - - - float clock_seconds = ((mkword(c64.TIME_LO, c64.TIME_MID) as float) - + (c64.TIME_HI as float)*65536.0) - / 60 - float hours = floor(clock_seconds / 3600) - clock_seconds -= hours*3600 - float minutes = floor(clock_seconds / 60) - clock_seconds = floor(clock_seconds - minutes * 60.0) - - c64scr.print("system time in ti$ is ") - c64flt.print_f(hours) - c64.CHROUT(':') - c64flt.print_f(minutes) - c64.CHROUT(':') - c64flt.print_f(clock_seconds) + c64scr.print("prime numbers up to 255:\n\n") + ubyte amount=0 + while true { + ubyte prime = find_next_prime() + if prime==0 + break + c64scr.print_ub(prime) + c64scr.print(", ") + amount++ + } c64.CHROUT('\n') + c64scr.print("number of primes (expected 54): ") + c64scr.print_ub(amount) + c64.CHROUT('\n') + } + + + sub find_next_prime() -> ubyte { + + while sieve[candidate_prime] { + candidate_prime++ + if candidate_prime==0 + return 0 + } + + sieve[candidate_prime] = true + uword multiple = candidate_prime + while multiple < len(sieve) { + sieve[lsb(multiple)] = true + multiple += candidate_prime + } + return candidate_prime } } +when compiled an ran on a C-64 you'll get: -you get a program that outputs this when loaded on a C-64: - -.. image:: _static/hello_screen.png +.. image:: _static/primes_example.png :align: center :alt: result when run on C-64