# Mandelbrot Set on the C64

Well, it took 36 hours, but at last my C64 finished calculating and drawing the non-diverging section of the Mandelbrot set.

Here is the code, in Basic:

10 rem ** mandelbrot hires
20 rem
40 yu = 1.5 : yd = -1.5
50 xu = 2 : xd = -2
60 xr = xu – xd : yr = yu – yd
70 xs = xr/320 : ys = yr/200
110 v = 53248
120 rem set graphic ram address
130 ga = 8192
140 rem set video ram address
150 vr = 1024
160 rem set border to black
170 poke v+32,0

500 gosub 20000 : rem turn on hires
510 gosub 21000 : rem graphic ram area
520 gosub 22000 : rem set color ram
530 gosub 23000 : rem clr graphic ram

1000 for xc = 0 to 319
1010 for yc = 0 to 199
1012 x0 = xc*(xr/320) + xd

1014 y0 = (yc-200)*(yr/200) – yd
1020 x = 0
1030 y = 0
1040 it = 0
1050 mx = 250
1060 a = x*x+y*y
1070 if a > 4 then goto 1200
1080 b = x*x-y*y + x0
1090 y = 2*x*y + y0
1100 x = b
1110 it = it + 1
1120 if it < mx then goto 1060
1150 gosub 30000 : rem set (xc,yc)
1200 next yc
1210 next xc
1400 poke 198,0 : wait 198,1
1450 gosub 24000
1500 end

20000 rem turn on hi res graphics
20010 rem 1. set bits 5/6 of v+17
20020 rem 2. clr bit 4 of v+22
20030 poke v+17,peek(v+17) or (11*16)
20040 poke v+22,peek(v+22) and (255-16)
20050 return
21000 rem set graphic ram area
21010 rem 1. set bit 3 of v+24
21020 poke v+24, peek(v+24) or 8
21030 return
22000 rem set color ram
22010 rem 1. color ram is 1024-2023
22020 rem 2. set background 1 – white
22030 rem 3. set foreground 0 – black
22040 co = 0*16 + 1
22050 for i = vr to vr+1000
22060 poke i,co
22070 next i
22080 return
23000 rem clear graphic ram
23010 rem 1. graphic ram is ga to
23020 rem ga + 8000
23030 for i = ga to ga +8000
23040 poke i,0
23050 next i
23060 return

24000 rem turn graphics off
24010 rem 1. clr bits 5/6 of v+17
24020 rem 2. clr bit 4 of v+22
24030 rem 3. clr bit 3 of v+24
24040 poke v+17,peek(v+17) and (255-96)
24050 poke v+22,peek(v+22) and (255-16)
24060 poke v+24,peek(v+24) and (255-8)
24070 return
30000 rem set pixel
30010 ra = 320*int(yc/8)+(yc and 7)

30020 ba = 8*int(xc/8)
30030 ma = 2^(7-(xc and 7))
30040 ad = ga + ra + ba