Source: http://www.dusko-lolic.from.hr/i8052fract/
Updated: 2011-02-05 (YYYY-MM-DD)

Home page


Intel 8052AH-BASIC factal generator

It all began one rainy day of the year 2004. I had time to spare. I was strolling through some electronic componets price lists when I came across 8052AH-BASIC chip being sold for next to nothing. Saddened by the fact that such a jewel had reached the end of its life cycle, and longing for the days when I couldn't afford one, I felt the temptation and fell for it. It was easy (as it was meant to be with this chip) to put together a very simple SBC to start playing with it. This fractal generator is one of the weirdest thing that came out as a result. It started as a simple numerical loop without anything being stored, just to see how long it would take. But add this, add that... it grew up to be an excellent example of Intel's BASIC-52 capabilities. Now it has esaped from my archives and landed here. We won't mind it, will we?

Download the source here.

System requirements are very modest. On the PC side, an ANSI compatible terminal is needed. Tera Term had been used and is still recommended. Windows built in HyperTerminal will barely suffice as it somewhat messes with ANSI codes, and is useless for program uploading (line transmit delay cannot be defined). A test program is included to check for ANSI compatibility, the result should look like this. ANSI is used not just to display colours, but also to move the cursor around. Yes, you won't have to calculate and type in numerical values to zoom and pan, this humble BASIC program is interactive!
Host 8052AH-BASIC system should have enough XRAM to hold at least one full frame. How large is a frame? As large as you wish. A typical frame is 80x60 pixels (characters). One pixel takes one byte. Add 36 bytes to each frame to store plot data. The smallest image the program supports is 12 x 9 pixels, but you won't see much in a window that small. The high resolution example below shows what can be squeezed into 32K (192 x 144 pixels).

XRAM  max frame size (Xa x Ya) number of 80x60 frames
8K  72 x 54 --
16K  128 x 96 2
32K  192 x 144 5
64K  284 x 213 12

Let's see some artwork!

This is the most familiar basic Mandelbrot set. Image size is 80 x 60 pixels. Font in terminal is Lucida Console 6pt.
At the bottom of the window the plot data is shown:
- Buffer index, selecting one of the several buffers (frames) in the memory. The first buffer is selected here (numbering starts from zero).
- Xmin, Ymin: real (X,Y) coordinates of the bottom left corner of the frame.
- Xmax: real X coordinate of the bottom right corner. The accompanying Ymax coordinate is calculated from the proportions of the window (and the proportions of the font used).
- Iteration limit. Well known parameter to every fractal-freak. The maximum value for the limit is 254. Increasing it can drastically prolong calculations.
- Coordinates of the selection window (in pixels) that will be used to draw next view into the set.

Zooming in...
The image on the right is derived from the one on the left with scale colors function. It simply scales iteration loop counter so that all six colors (the seventh, black, is reserved for inside of the set points) are evenly distributed. Color scaled version is meant to be the final output, although it doesn't look nearly as colorful. Unscaled color view is just a kind of raw, quick data preview that is also displayed as the calculation progresses. Scale color function will not alter the original data in any way, it simply brings out the hidden structure in a new way at the moment of displaying. Maybe the unscaled version looks more fractalial, but scaled is more mathematically correct. You be the judge.

Another view. Mind you, pixels of this size should be viewed from a distance...

Nice bubbles, with and without scaled colours...

And now, let's go high resolution!
The resolution here is 192 x 144 pixels, maximum that can fit in 32K of XRAM. Coordinates are the same as above. Once you find something interesting in lo-res fast mode, the resolution can be changed (line 30 in the program) and the first buffer (index 0) recalculated without loosing the plot parameters. Other buffers are in that case lost (plot data gets overwritten with image data). Terminal font is set to 2pt Lucida Console in order to fit more pixels on the screen, that's why the text is illegible.
Total running time for this was 18 hr 1336.36 s!

Quick instructions

Provided in the case that someone actually wants to try this. You will probably need real hardware since MCS-51 simulators usually don't know what to do with ANSI codes in their inbuilt terminal emulations.

main menu keys:
B - select buffer
D - draw image from current buffer
L - set iteration limit
S - scale colors (the same as D, but with precisely determined colors)
W - select zoom window
C - calculate new subset

select window keys:
O,P,Q,A - move cursor left, down, up, right
0 (zero) - mark window lower-left corner
W - mark window width
ENTER - return to main menu

With the first run of the program the buffers will be empty, start the calculation (key C) of the set with shown coordinates. Choose the destination buffer, it can be the same as origin (0). Let it calculate the basic set, it will take some time. The program can be stopped, edited, and run again without loosing any completed work as the data file is stored above MTOP. With the basic set completed we can zoom into it. Press the 'W' key, the cursor will take the '+' form  and move up. It should exactly hit the bottom left corner of the drawn picture. Move it up with the 'Q' key if it doesn't and press 'ENTER'. The cursor is now in the shape of asterik '*' indicating window select mode. Move it with O,P,Q,A keys to the bottom left corner of the desired selection area, press '0' (zero) to confirm. Move it to the top right position of the imaginary selection window, confirm with the 'W' key. A bit of imagination is required as the window border is not visible! Only the width of the window matters, the height is adjusted to preserve the exact aspect ratio. Return to the main menu with 'ENTER'. Window coordinates in pixels are now displayed, press 'C' to calculate the new subset based on the selected window.

A look inside

Some of the key lines will be explained.

30 XA=80 : REM X axis in pixels
The REM says it all, this is the X axis (image width) frame size in pixels. The minimum is 12.


40 YP=1.6 : REM font H/W proportions
In any text editor choose the same font as used in terminal emulator and draw an ASCII square made of any character (fixed width font is assumed), like this (5 x 5 characters)...

#####
#####
#####
#####
#####

With a ruler measure the height and the width, divide the two.  That's the ratio in line 40.

50 YA=XA*0.75 : PRINT CHR(27),"[30;47m"
This is what limits the choice for the X axis, Y axis is 3/4 (0.75) of that and it has to be the whole number.

470 DBY(18H)=I
Active buffer index is kept at this location instaead of plain variable so that it is preserved if the program is stopped and run again.

820 PRINT SPC (XA-12),((YA-Y-1)*XA+X+1)/(YA*XA)*100,"%",
This is where the progress is displayed during calculation. XA-12 is what limits minimum X axis to 12. Code can be added here to display progress somewhere else while the terminal is not connected, e.g. external LCD, LED bar, a hand attached to a stepper motor, speech synthesizer etc...

1070 IF SC.AND.C<>0 THEN C=INT((C-I1)/(I9-I1)*6+1)
This is where the color scaling is done. Fell free to experiment. Once the frame is calculated, you can stop the program without loosing the calculated data, change this line, run the program to redraw stored image and see what it loks like with new scaling. Try two-fold scaling.

4090 XMIN=-2.1 : YMIN=-1.2 : XMAX=.61 : IL=40 : REM initial position
If you know the coordinates you want to draw, enter them here. The program will use them as the starting point.

The hardware

Wonder what that fractal monster looks like? Here it is...

 

Bare necessities. Good ol' 8052AH-BASIC leisurely paced at 11.052 MHz (just 921 KHz core clock!), two 32K x 8 cache SRAMs cannibalised from a deceased  i386 motherboard (overkill in speed, but conveniently small footprint compared to normal SRAM), one standard 74HCT573 address latch, and 74LS00 + 74HCT04 acting as chipset. Chip families are irrelevant, it was on whatever can be found first basis. Partially visible blob at the top is reset button. No need for MAX232 interface, just a couple of resistors and a diode preparing signal for a plain inverter.
All the RAM is combined and can be accesed either as code or XRAM, the usual feature of almost every MCS-51 development system. This makes it possible to simulate a system with EPROM memory at address 8000H wich in BASIC-52 terms means that several programs can be stored in memory simultaneously and additional basic commands can be defined.


Comments