Forth Lesson 14: Difference between revisions

From OLPC
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
{{Forth Lessons}}

== Some Cute Hacks ==
== Some Cute Hacks ==



Revision as of 01:29, 20 August 2007

Mitch Bradley's Forth and
Open Firmware Lessons:

Some Cute Hacks

Small scrolling region

If you want to play around with graphics, it is nice to make the text scroller smaller so it doesn't get in the way of the graphics.

ok screen-ih iselect
ok 5 to #lines
ok screen-height  char-height 5 *  -  to window-top
ok iunselect

That makes the scroller 5 lines high and moves it to the bottom of the screen.

You could put that in a file on a USB key to run it automatically - /boot/olpc.fth .

The shortest way to say it would be:

ok screen-ih iselect  5 to #lines  316 to window-top  iunselect

316 is the hex number that results from the computation "screen-height char-height 5 * -". Obviously it is dependent on specifics of the OLPC machine and the number of lines in the scroller, but you get the idea.

Mandelbrot set

This snippet has been used to test other Open Firmware implementations. (Note: takes some time to compute.)

hex 4666 dup negate do i 4000 dup 2* negate do " *" 0 dup 2dup 1e 0 do
2swap * e >>a 2* 5 pick + -rot - j + dup dup * e >>a rot dup dup * e >>a 
rot swap 2dup + 10000 > if 3drop 3drop "  " 0 dup 2dup leave then loop 
2drop 2drop type 268 +loop cr drop 5de +loop

Refactored, this may be a bit faster.

: f* * e >>a ; : sq over dup f* ; 4666 dup negate do 4000 dup 2* negate do
i j 1e begin 1- ?dup while -rot sq sq 2dup + 10000 < while - i + -rot f* 2* j +
rot repeat 2drop drop bl else bl a + then emit 2drop 268 +loop cr 5de +loop

--IanOsgood 21:09, 19 August 2007 (EDT)