1 /*****************************************************************************
2 * mandelbrot sample program for cc65. *
4 * (w)2002 by groepaz/hitmen, TGI support by Stefan Haubenthal *
5 *****************************************************************************/
16 /* Graphics definitions */
17 #define SCREEN_X (tgi_getxres())
18 #define SCREEN_Y (tgi_getyres())
19 #define MAXCOL (tgi_getcolorcount())
21 #define maxiterations 32
23 #define tofp(_x) ((_x)<<fpshift)
24 #define fromfp(_x) ((_x)>>fpshift)
25 #define fpabs(_x) (abs(_x))
27 #define mulfp(_a,_b) ((((signed long)_a)*(_b))>>fpshift)
28 #define divfp(_a,_b) ((((signed long)_a)<<fpshift)/(_b))
30 /* Workaround missing clock stuff */
31 #if defined(__APPLE2__) || defined(__APPLE2ENH__)
36 /* Use static local variables for speed */
37 #pragma static-locals (1);
41 void mandelbrot (signed short x1, signed short y1, signed short x2,
44 register unsigned char count;
45 register signed short r, r1, i;
46 register signed short xs, ys, xx, yy;
47 register signed short x, y;
50 xs = ((x2 - x1) / (SCREEN_X));
51 ys = ((y2 - y1) / (SCREEN_Y));
54 for (y = 0; y < (SCREEN_Y); y++) {
57 for (x = 0; x < (SCREEN_X); x++) {
62 for (count = 0; (count < maxiterations) &&
63 (fpabs (r) < tofp (2)) && (fpabs (i) < tofp (2));
65 r1 = (mulfp (r, r) - mulfp (i, i)) + xx;
66 /* i = (mulfp(mulfp(r,i),tofp(2)))+yy; */
67 i = (((signed long) r * i) >> (fpshift - 1)) + yy;
70 if (count == maxiterations) {
76 tgi_setcolor (count % MAXCOL);
93 /* Load the graphics driver */
94 cprintf ("initializing... mompls\r\n");
95 tgi_load_driver (tgi_stddrv);
96 err = tgi_geterror ();
97 if (err != TGI_ERR_OK) {
98 cprintf ("Error #%d initializing graphics.\r\n%s\r\n",
99 err, tgi_geterrormsg (err));
104 /* Initialize graphics */
110 /* calc mandelbrot set */
111 mandelbrot (tofp (-2), tofp (-2), tofp (2), tofp (2));
115 /* Fetch the character from the keyboard buffer and discard it */
118 /* shut down gfx mode and return to textmode */
121 /* Calculate stats */
122 sec = (t * 10) / CLK_TCK;
127 cprintf ("time : %lu.%us\n\r", sec, sec10);
129 /* Wait for a key, then end */
130 cputs ("Press any key when done...\n\r");