more inspiring code example

This commit is contained in:
Irmen de Jong 2019-04-12 22:34:43 +02:00
parent a9bbe0bc40
commit f219ae43f7
5 changed files with 81 additions and 75 deletions

View File

@ -50,55 +50,58 @@ of the [Vice emulator](http://vice-emu.sourceforge.net/)
Example code Example code
------------ ------------
When this code is compiled:: This code calculates prime numbers using the Sieve of Eratosthenes algorithm::
%import c64lib
%import c64utils %import c64utils
%import c64flt %zeropage basicsafe
~ main { ~ main {
ubyte[256] sieve
ubyte candidate_prime = 2
sub start() { sub start() {
; set text color and activate lowercase charset memset(sieve, 256, false)
c64.COLOR = 13
c64.VMCSB |= 2
; use optimized routine to write text c64scr.print("prime numbers up to 255:\n\n")
c64scr.print("Hello!\n") ubyte amount=0
while true {
; use iteration to write text ubyte prime = find_next_prime()
str question = "How are you?\n" if prime==0
for ubyte char in question break
c64.CHROUT(char) c64scr.print_ub(prime)
c64scr.print(", ")
; use indexed loop to write characters amount++
str bye = "Goodbye!\n" }
for ubyte c in 0 to len(bye) c64.CHROUT('\n')
c64.CHROUT(bye[c]) c64scr.print("number of primes (expected 54): ")
c64scr.print_ub(amount)
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)
c64.CHROUT('\n') 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/primes_example.png)
![c64 screen](docs/source/_static/hello_screen.png)
One of the included examples (wizzine.p8) animates a bunch of sprite balloons and looks like this: One of the included examples (wizzine.p8) animates a bunch of sprite balloons and looks like this:

View File

@ -1 +1 @@
1.6 (beta) 1.6

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

View File

@ -40,55 +40,58 @@ This software is licensed under the GNU GPL 3.0, see https://www.gnu.org/license
Code example Code example
------------ ------------
When this code is compiled:: This code calculates prime numbers using the Sieve of Eratosthenes algorithm::
%import c64lib
%import c64utils %import c64utils
%import c64flt %zeropage basicsafe
~ main { ~ main {
ubyte[256] sieve
ubyte candidate_prime = 2
sub start() { sub start() {
; set text color and activate lowercase charset memset(sieve, 256, false)
c64.COLOR = 13
c64.VMCSB |= 2
; use optimized routine to write text c64scr.print("prime numbers up to 255:\n\n")
c64scr.print("Hello!\n") ubyte amount=0
while true {
; use iteration to write text ubyte prime = find_next_prime()
str question = "How are you?\n" if prime==0
for ubyte char in question break
c64.CHROUT(char) c64scr.print_ub(prime)
c64scr.print(", ")
; use indexed loop to write characters amount++
str bye = "Goodbye!\n" }
for ubyte c in 0 to len(bye) c64.CHROUT('\n')
c64.CHROUT(bye[c]) c64scr.print("number of primes (expected 54): ")
c64scr.print_ub(amount)
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)
c64.CHROUT('\n') 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/primes_example.png
.. image:: _static/hello_screen.png
:align: center :align: center
:alt: result when run on C-64 :alt: result when run on C-64