Skip to content

Commit

Permalink
more inspiring code example
Browse files Browse the repository at this point in the history
  • Loading branch information
irmen committed Apr 12, 2019
1 parent a9bbe0b commit f219ae4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 77 deletions.
79 changes: 41 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

; 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)
memset(sieve, 256, false)

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
}
}


you get a program that outputs this when loaded on a C-64:
when compiled an ran on a C-64 you'll get:

![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:
Expand Down
2 changes: 1 addition & 1 deletion compiler/res/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.6 (beta)
1.6
Binary file removed docs/source/_static/hello_screen.png
Binary file not shown.
Binary file added docs/source/_static/primes_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 41 additions & 38 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

; 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)
memset(sieve, 256, false)

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
}
}


you get a program that outputs this when loaded on a C-64:
when compiled an ran on a C-64 you'll get:

.. image:: _static/hello_screen.png
.. image:: _static/primes_example.png
:align: center
:alt: result when run on C-64

Expand Down

0 comments on commit f219ae4

Please sign in to comment.