1 /*****************************************************************************
2 * mandelbrot sample program for cc65. *
4 * (w)2002 by groepaz/hitmen, TGI support by Stefan Haubenthal *
5 *****************************************************************************/
16 /* Graphics definitions */
17 #if defined(__APPLE2__) || defined(__APPLE2ENH__)
18 # define GRAPHMODE TGI_MODE_280_192_8
20 # define GRAPHMODE TGI_MODE_320_200_2
22 #define SCREEN_X (tgi_getxres())
23 #define SCREEN_Y (tgi_getyres())
24 #define MAXCOL (tgi_getcolorcount())
26 #define maxiterations 32
28 #define tofp(_x) ((_x)<<fpshift)
29 #define fromfp(_x) ((_x)>>fpshift)
30 #define fpabs(_x) (abs(_x))
32 #define mulfp(_a,_b) ((((signed long)_a)*(_b))>>fpshift)
33 #define divfp(_a,_b) ((((signed long)_a)<<fpshift)/(_b))
35 /* Workaround missing clock stuff */
36 #if defined(__APPLE2__) || defined(__APPLE2ENH__)
41 /* Use static local variables for speed */
42 #pragma staticlocals (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);
98 /* Load the graphics driver */
99 cprintf ("initializing... mompls\r\n");
100 tgi_load (GRAPHMODE);
101 err = tgi_geterror ();
102 if (err != TGI_ERR_OK) {
103 cprintf ("Error #%d initializing graphics.\r\n%s\r\n",
104 err, tgi_geterrormsg (err));
109 /* Initialize graphics */
115 /* calc mandelbrot set */
116 mandelbrot (tofp (-2), tofp (-2), tofp (2), tofp (2));
120 /* Fetch the character from the keyboard buffer and discard it */
123 /* shut down gfx mode and return to textmode */
126 /* Calculate stats */
127 sec = (t * 10) / CLK_TCK;
132 cprintf ("time : %lu.%us\n\r", sec, sec10);
134 /* Wait for a key, then end */
135 cputs ("Press any key when done...\n\r");