Examples 2
Contents
Primes
This is a simple 1-block program to list all the prime numbers, it's really a kind of benchtest. You run it by typing, e.g. 1000 primes to list all the primes up to 1000.
: eloop 10 i@ 1- 12 i! ;
: primes
2 swap 2 . 3 . 5
do
dup dup * i < if
1+ then
1 over 1+ 3 do
r i mod 0= if
1- eloop then
2 +loop
if
i . then
2 +loop
drop ;
It was originally written for Mark Willis's TI 99/4a Turbo Forth and converted by David Bambrough and Carl Attril.
Calendar
David Bambrough converted Ricardo Lopez's Calendar program for the Jupiter Ace to FIGnition:
To use it, type (for example), 2013 april <exe> . The following screenshot shows the result from typing 2012 february <exe> 2012 may <exe>
Pong
A version of pong was originally developed for the Magnavox Odyssey home console by Ralph Baer, but first achieved mainstream acceptance with Atari's (improved) version of Pong in 1972. In that sense it's the video game that launched the industry. I've wanted to write a FIGnition version for quite a while, just to see how simple it can be. FIGnition's version requires firmware 0.9.8 or later, supports ball angles, and smooth bat movement, but currently lacks ball acceleration.
Here's a screenshot of FIGnition Pong in action:
The game itself is 162 lines long and occupies 2.9Kb of source or 862 bytes when compiled. It supports audio; different speeds and bat sizes (but no attract mode).
How to play (PONG needs Firmware 0.9.8):
Load the game by loading the last block (it will load the rest).
Type: speed batSize pong <exe> Usually I type 3 8 pong . The speed is the ratio of bat frames to ball frames, in this case 3 to 1.
The left player uses SW1 and SW6 to go up and down; the right player uses SW4 and SW7 to go up and down.
First person to 15 wins!
I2C Access
It's possible to connect I2C devices to FIGnition entirely using Forth. I2C devices require the addition of a 1K8 (minimum) resistor between U1 Pin 8 and 5V and another similar resistor between U1 Pin 28 and 5V.
Oleg Kobchenko developed an I2C library for this purpose (slightly modified here):
For example, a Maxim 517 DAC I2C device can be used with a FIGnition with the following code:
( Maxim DAC bit 0=reg)
$4C const max517/8Base
$40 const max519Base
: >dac ( val addr)
dup $FE and i2c{
1 and >i2c >i2c
}i2c
;
( demo code saw tooth)
max517/8Base const aDac
: demoDac
begin
256 0 do
i aDac >dac
loop
inkey until
;
FIGgyBird
Here's a FIGnition version of the popular game in only 200 lines of code and 1026 bytes (when compiled).
Here's a screenshot of FIGgyBirds in action:
Scientific Functions
FIGnition supports Floating-Point arithmetic from firmware 1.0.0 onwards. Here's a useful set of trigonometric and logarithmic scientific functions to support them:
So what's in the package?
The code is attached and should compile to 878b or so. I's designed for compactness rather than performance or accuracy. The commands are based on ZX Basic commands rather than modern 'C' function terminology. There's no reason why you can't rename them.
Most of FIGnition's tiny scientific package uses Chebyshev polynomials to approximate calculations to an accuracy adequate for single-precision floating-point arithmetic. The exception is sqrt, which uses a Newton-Raphson approximation.