Console Mandelbrot in FORTH

The Mandelbrot Set printed out in a console with text.

I really like to play around with FORTH. It’s a “weird” programming language compared to all the other ones I’ve learned over time. There are already many articles out there about why you should give it a try, the best probably being this one at Hackaday.

One little program that is often demonstrate on old computers is a BASIC program to compute the Mandelbrot Set in the console. I thought I’d try my hand in “converting” the program to FORTH.

Here’s my version, taken from a gist I wrote earlier this year.

\ from proposal http://www.forth200x.org/fvalue.html
variable %var
: to 1 %var ! ;
: fvalue create f, does> %var @ if f! else f@ then 0 %var ! ;

0e0 fvalue i3
0e0 fvalue r3

59 value x1
21 value y1
-1e0 fvalue i1 
1e0 fvalue i2
-2e0 fvalue r1
1e0 fvalue r2
r2 r1 f- x1 s>f f/ fvalue s1 \ L30
i2 i1 f- y1 s>f f/ fvalue s2 \ L31

0e0 fvalue a
0e0 fvalue b
: single_iter { F: z1 F: z2 } ( F: z1 F: z2 -- F: z1' F: z2' F: mag )
  z1 fdup f* to a \ L90
  z2 fdup f* to b \ L91
  a b f- r3  f+ \ z1 \ L111
  2e0 z1 z2 f* f* i3 f+ \ z2 L110
  a b f+ \ mag \ line 100
;

: print_char ( F: x F: y -- )
  30 \ push the max in case we don't exit early
  30 0 do                          \ L80
    single_iter
    4e0 f> if drop i leave then
  loop                             \ L120
  fdrop fdrop \ clean z1 and z2
  62 swap - emit                   \ L130
;

: calc_i3 { y }
  i1 s2 y s>f f* f+ to i3 \ L50
;

: calc_r3 { x }
  r1 s1 x s>f f* f+ to r3 \ L70
;

: mandel
cr \ always start on a fresh clean line
y1 0 do                         \ L40
  i calc_i3
  x1 0 do                       \ L60
    i calc_r3
    r3 i3 print_char
  loop                            \ L140
  cr                              \ L150
loop                              \ L160
;

mandel

That prints out something like the screenshot at the top of this post.

The “L##” comments roughly refer to the lines in the BASIC version, if you’d like to compare the two. I tried to keep the memory usage and general complexity as equal as possible – that’s why there are so many value and fvalue variables. I’ve seen some examples that use the stack more but are harder to read and have to recompute a lot of things that the BASIC version doesn’t.

Here’s the original BASIC version:

10 x1=59
11 y1=21
20 i1=-1.0
21 i2=1.0
22 r1=-2.0
23 r2=1.0
30 s1=(r2-r1)/x1
31 s2=(i2-i1)/y1
40 for y=0  to y1
50 i3=i1+s2*y
60 for x=0 to x1
70 r3=r1+s1*x
71 z1=r3
72 z2=i3
80 for n=0 to 29
90 a=z1*z1
91 b=z2*z2
100 if a+b>4.0 goto 130
110 z2=2*z1*z2+i3
111 z1=a-b+r3
120 next n
130 print chr$(62-n);
140 next x
150 print
160 next y
170 end

I’m consistently intrigued by FORTH, so I’ll probably be sharing more programs and thoughts. I wrote a short explanation of the “polyfill” in the first four lines of the FORTH version is actually a great dive into the language’s extensibility.

WIP: Optical Bench

I’ve always wanted my own optical bench sense working in an ellipsometry lab in undergrad. After getting into photography, I increasingly wanted to design my own photo lenses, so I even had a use for an optical bench eventually. My Prusa Mini+ has unlocked so many projects and this is one of them. But the other key development was getting comfortable enough with OpenSCAD to whip up these lens mount models in under an hour. And that works great because these mounts print in under an hour. You really start living the rapid prototyping dream at that point.

These are all small lenses and most are very light weight because they are made from plastic, not glass. The heavier glass lenses need a couple quarters on their mounts’ bases to make them more stable. Once I have an actual table to mount these to, that won’t be a problem.

I have some really interesting large glass lenses that this design won’t work for. I’ve got some ideas of how to design mounts for those in my notebook.

I also need to find a good imager module that doesn’t have a lens (or one with a lens that is easy to remove). I have an old camcorder that might work – I haven’t dug into it to find out how much any of its parts still work.

If you’ve got any ideas for some additions, drop me a line.

Recommendation: Developments in Structural Form by Rowland J. Mainstone

The cover

I thought I’d share a book recommendation: Developments in Structural Form by Rowland J. Mainstone. While definitely a textbook from some intro architecture class, it’s also part history book and part architectural engineering primer. It traces how new techniques and materials were put into practice and, like you really want, has lots of pictures.

A peek inside the book

I found it at Orphelia’s Books in lower Fremont here in Seattle – a store I highly recommend. That was many years ago, but you can probably also find it at ThriftBooks. Check it out!

Showing Some Work

I recently read Show Your Work by Austin Kleon. It is a short read and I recommend grabbing an electronic copy from your local library and reading it over your lunch hour. Ryan sent it my way and that has been helpful

Kleon provides some practical advice, some of it perhaps a little conflicting, but overall it’s solid. I wrote up some notes and drafted a short review/musing that I’ll post later.

“So what?” as he says.

Well this post is coming from the WordPress app on my phone as a test of an easy way to get my process documented. To fully simulate what I will be posting more of, here is a picture of my workbench.

My workbench, covered in stuff

Starting from the bottom right and going counter clockwise:

  • A Sun keyboard I got for cheap at the recyclers. Unknown if it works
  • IBM Model M that auto switches to XT keyboard protocol
  • Z80 single board computer that I added a second board to… so I guess it’s a TBC not an SBC
  • My eBay power supply next to a power supply I built from parts
  • soldering iron, tools, a little tool holder I designed and 3D printed, oscilloscope
  • LCD monitor for experiments
  • A bunch of project trays. I’ll have to talk about these at some point but the short description is that a project that isn’t finished goes into a tray so the parts don’t get scattered but also doesn’t get hidden and forgotten
  • My KayPro XT compatible retro computing project.
  • My new hot air rework station amongst a pile of junk and parts
  • And finally in the center are my “current” projects: RaSCSI drive emulator and some prototype modular synth modules

In an effort to follow his advice in story telling – This photo tells a story with a three part structure. The tool holder is a completed project and represents the beginning of my ability to design and print useful objects. The RaSCSI and synth modules are what I have completed so far. Their promise cannot be fulfilled until I add their final prototyped features and send them out into the world as complete ideas. The KayPro XT is the future. It is a fully functioning computer with a case and upgraded features. It will become a virtual museum piece, accessible by KVM over the Internet – the first of a handful of machines I want to add to my virtual museum.

So I will be writing more posts from now on. I’ll be integrating this site with my personal site. I’ll be adding links to references and other works. Thank you for reading and feel free to contact me if you’d like to work on something together!