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 */
36 /* Use dynamically loaded driver by default */
41 /* Use static local variables for speed */
42 #pragma static-locals (1);
46 void mandelbrot (signed short x1, signed short y1, signed short x2,
49 register unsigned char count;
50 register signed short r, r1, i;
51 register signed short xs, ys, xx, yy;
52 register signed short x, y;
55 xs = ((x2 - x1) / (SCREEN_X));
56 ys = ((y2 - y1) / (SCREEN_Y));
59 for (y = 0; y < (SCREEN_Y); y++) {
62 for (x = 0; x < (SCREEN_X); x++) {
67 for (count = 0; (count < maxiterations) &&
68 (fpabs (r) < tofp (2)) && (fpabs (i) < tofp (2));
70 r1 = (mulfp (r, r) - mulfp (i, i)) + xx;
71 /* i = (mulfp(mulfp(r,i),tofp(2)))+yy; */
72 i = (((signed long) r * i) >> (fpshift - 1)) + yy;
75 if (count == maxiterations) {
81 tgi_setcolor (count % MAXCOL);
99 /* Load the graphics driver */
100 cprintf ("initializing... mompls\r\n");
101 tgi_load_driver (tgi_stddrv);
103 /* Install the graphics driver */
104 tgi_install (tgi_static_stddrv);
106 err = tgi_geterror ();
107 if (err != TGI_ERR_OK) {
108 cprintf ("Error #%d initializing graphics.\r\n%s\r\n",
109 err, tgi_geterrormsg (err));
114 /* Initialize graphics */
120 /* calc mandelbrot set */
121 mandelbrot (tofp (-2), tofp (-2), tofp (2), tofp (2));
125 /* Fetch the character from the keyboard buffer and discard it */
128 /* shut down gfx mode and return to textmode */
131 /* Calculate stats */
132 sec = (t * 10) / CLK_TCK;
137 cprintf ("time : %lu.%us\n\r", sec, sec10);
139 /* Wait for a key, then end */
140 cputs ("Press any key when done...\n\r");