1 /*****************************************************************************
2 * mandelbrot sample program for cc65. *
4 * (w)2002 by groepaz/hitmen, TGI support by Stefan Haubenthal *
5 *****************************************************************************/
16 /* Graphics definitions */
17 #define GRAPHMODE TGI_MODE_320_200_2
18 #define SCREEN_X (tgi_getmaxx()+1)
19 #define SCREEN_Y (tgi_getmaxy()+1)
20 #define MAXCOL (tgi_getmaxcolor()+1)
22 #define maxiterations 32
24 #define tofp(_x) ((_x)<<fpshift)
25 #define fromfp(_x) ((_x)>>fpshift)
26 #define fpabs(_x) (abs(_x))
28 #define mulfp(_a,_b) ((((signed long)_a)*(_b))>>fpshift)
29 #define divfp(_a,_b) ((((signed long)_a)<<fpshift)/(_b))
31 /* Use static local variables for speed */
32 #pragma staticlocals (1);
36 void mandelbrot (signed short x1, signed short y1, signed short x2,
39 register unsigned char count;
40 register signed short r, r1, i;
41 register signed short xs, ys, xx, yy;
42 register signed short x, y;
45 xs = ((x2 - x1) / (SCREEN_X));
46 ys = ((y2 - y1) / (SCREEN_Y));
49 for (y = 0; y < (SCREEN_Y); y++) {
52 for (x = 0; x < (SCREEN_X); x++) {
57 for (count = 0; (count < maxiterations) &&
58 (fpabs (r) < tofp (2)) && (fpabs (i) < tofp (2));
60 r1 = (mulfp (r, r) - mulfp (i, i)) + xx;
61 /* i = (mulfp(mulfp(r,i),tofp(2)))+yy; */
62 i = (((signed long) r * i) >> (fpshift - 1)) + yy;
65 if (count == maxiterations) {
71 tgi_setcolor (count % MAXCOL);
88 /* Load the graphics driver */
89 cprintf ("initializing... mompls\r\n");
91 err = tgi_geterror ();
92 if (err != TGI_ERR_OK) {
93 cprintf ("Error #%d initializing graphics.\r\n%s\r\n",
94 err, tgi_geterrormsg (err));
99 /* Initialize graphics */
105 /* calc mandelbrot set */
106 mandelbrot (tofp (-2), tofp (-2), tofp (2), tofp (2));
110 /* Fetch the character from the keyboard buffer and discard it */
113 /* shut down gfx mode and return to textmode */
116 /* Calculate stats */
117 sec = (t * 10) / CLK_TCK;
122 cprintf ("time : %lu.%us\n\r", sec, sec10);
124 /* Wait for a key, then end */
125 cputs ("Press any key when done...\n\r");