1 /*****************************************************************************\
2 ** mandelbrot sample program for cc65. **
4 ** (w) 2002 by groepaz/hitmen, TGI support by Stefan Haubenthal **
5 \*****************************************************************************/
17 /* Graphics definitions */
18 #define SCREEN_X (tgi_getxres())
19 #define SCREEN_Y (tgi_getyres())
20 #define MAXCOL (tgi_getcolorcount())
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 /* Workaround missing clock stuff */
37 /* Use dynamically loaded driver by default */
42 /* Use static local variables for speed */
43 #pragma static-locals (1);
47 void mandelbrot (signed short x1, signed short y1, signed short x2,
50 register unsigned char count;
51 register signed short r, r1, i;
52 register signed short xs, ys, xx, yy;
53 register signed short x, y;
56 xs = ((x2 - x1) / (SCREEN_X));
57 ys = ((y2 - y1) / (SCREEN_Y));
60 for (y = 0; y < (SCREEN_Y); y++) {
63 for (x = 0; x < (SCREEN_X); x++) {
68 for (count = 0; (count < maxiterations) &&
69 (fpabs (r) < tofp (2)) && (fpabs (i) < tofp (2));
71 r1 = (mulfp (r, r) - mulfp (i, i)) + xx;
72 /* i = (mulfp(mulfp(r,i),tofp(2)))+yy; */
73 i = (((signed long) r * i) >> (fpshift - 1)) + yy;
76 if (count == maxiterations) {
82 tgi_setcolor (count % MAXCOL);
101 /* Load the graphics driver */
102 cprintf ("initializing... mompls\r\n");
103 tgi_load_driver (tgi_stddrv);
105 /* Install the graphics driver */
106 tgi_install (tgi_static_stddrv);
108 err = tgi_geterror ();
109 if (err != TGI_ERR_OK) {
110 cprintf ("Error #%d initializing graphics.\r\n%s\r\n",
111 err, tgi_geterrormsg (err));
112 if (doesclrscrafterexit ()) {
119 /* Initialize graphics */
125 /* Calc mandelbrot set */
126 mandelbrot (tofp (-2), tofp (-2), tofp (2), tofp (2));
130 /* Fetch the character from the keyboard buffer and discard it */
133 /* Shut down gfx mode and return to textmode */
136 /* Calculate stats */
137 sec = (t * 10) / CLK_TCK;
142 cprintf ("time : %lu.%us\n\r", sec, sec10);
144 if (doesclrscrafterexit ()) {
145 /* Wait for a key, then end */
146 cputs ("Press any key when done...\n\r");