fix red.
[kugel-rb/myfork.git] / apps / plugins / mandelbrot.c
blob058b468465dc7a4266a79218d2432180ec49164e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 Matthias Wientapper
11 * Heavily extended 2005 Jens Arnold
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "plugin.h"
25 #ifdef HAVE_LCD_BITMAP
26 #include "lib/grey.h"
27 #include "lib/xlcd.h"
29 PLUGIN_HEADER
31 /* variable button definitions */
32 #if CONFIG_KEYPAD == RECORDER_PAD
33 #define MANDELBROT_QUIT BUTTON_OFF
34 #define MANDELBROT_UP BUTTON_UP
35 #define MANDELBROT_DOWN BUTTON_DOWN
36 #define MANDELBROT_LEFT BUTTON_LEFT
37 #define MANDELBROT_RIGHT BUTTON_RIGHT
38 #define MANDELBROT_ZOOM_IN BUTTON_PLAY
39 #define MANDELBROT_ZOOM_OUT BUTTON_ON
40 #define MANDELBROT_MAXITER_INC BUTTON_F2
41 #define MANDELBROT_MAXITER_DEC BUTTON_F1
42 #define MANDELBROT_RESET BUTTON_F3
44 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
45 #define MANDELBROT_QUIT BUTTON_OFF
46 #define MANDELBROT_UP BUTTON_UP
47 #define MANDELBROT_DOWN BUTTON_DOWN
48 #define MANDELBROT_LEFT BUTTON_LEFT
49 #define MANDELBROT_RIGHT BUTTON_RIGHT
50 #define MANDELBROT_ZOOM_IN BUTTON_SELECT
51 #define MANDELBROT_ZOOM_OUT BUTTON_ON
52 #define MANDELBROT_MAXITER_INC BUTTON_F2
53 #define MANDELBROT_MAXITER_DEC BUTTON_F1
54 #define MANDELBROT_RESET BUTTON_F3
56 #elif CONFIG_KEYPAD == ONDIO_PAD
57 #define MANDELBROT_QUIT BUTTON_OFF
58 #define MANDELBROT_UP BUTTON_UP
59 #define MANDELBROT_DOWN BUTTON_DOWN
60 #define MANDELBROT_LEFT BUTTON_LEFT
61 #define MANDELBROT_RIGHT BUTTON_RIGHT
62 #define MANDELBROT_ZOOM_IN_PRE BUTTON_MENU
63 #define MANDELBROT_ZOOM_IN (BUTTON_MENU | BUTTON_REL)
64 #define MANDELBROT_ZOOM_IN2 (BUTTON_MENU | BUTTON_UP)
65 #define MANDELBROT_ZOOM_OUT (BUTTON_MENU | BUTTON_DOWN)
66 #define MANDELBROT_MAXITER_INC (BUTTON_MENU | BUTTON_RIGHT)
67 #define MANDELBROT_MAXITER_DEC (BUTTON_MENU | BUTTON_LEFT)
68 #define MANDELBROT_RESET (BUTTON_MENU | BUTTON_OFF)
70 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
71 (CONFIG_KEYPAD == IRIVER_H300_PAD)
72 #define MANDELBROT_QUIT BUTTON_OFF
73 #define MANDELBROT_UP BUTTON_UP
74 #define MANDELBROT_DOWN BUTTON_DOWN
75 #define MANDELBROT_LEFT BUTTON_LEFT
76 #define MANDELBROT_RIGHT BUTTON_RIGHT
77 #define MANDELBROT_ZOOM_IN BUTTON_SELECT
78 #define MANDELBROT_ZOOM_OUT BUTTON_MODE
79 #define MANDELBROT_MAXITER_INC (BUTTON_ON | BUTTON_RIGHT)
80 #define MANDELBROT_MAXITER_DEC (BUTTON_ON | BUTTON_LEFT)
81 #define MANDELBROT_RESET BUTTON_REC
83 #define MANDELBROT_RC_QUIT BUTTON_RC_STOP
85 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
86 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
87 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
88 #define MANDELBROT_QUIT (BUTTON_SELECT | BUTTON_MENU)
89 #define MANDELBROT_UP BUTTON_MENU
90 #define MANDELBROT_DOWN BUTTON_PLAY
91 #define MANDELBROT_LEFT BUTTON_LEFT
92 #define MANDELBROT_RIGHT BUTTON_RIGHT
93 #define MANDELBROT_ZOOM_IN BUTTON_SCROLL_FWD
94 #define MANDELBROT_ZOOM_OUT BUTTON_SCROLL_BACK
95 #define MANDELBROT_MAXITER_INC (BUTTON_SELECT | BUTTON_RIGHT)
96 #define MANDELBROT_MAXITER_DEC (BUTTON_SELECT | BUTTON_LEFT)
97 #define MANDELBROT_RESET (BUTTON_SELECT | BUTTON_PLAY)
99 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
100 #define MANDELBROT_QUIT BUTTON_POWER
101 #define MANDELBROT_UP BUTTON_UP
102 #define MANDELBROT_DOWN BUTTON_DOWN
103 #define MANDELBROT_LEFT BUTTON_LEFT
104 #define MANDELBROT_RIGHT BUTTON_RIGHT
105 #define MANDELBROT_ZOOM_IN_PRE BUTTON_SELECT
106 #define MANDELBROT_ZOOM_IN (BUTTON_SELECT | BUTTON_REL)
107 #define MANDELBROT_ZOOM_OUT_PRE BUTTON_SELECT
108 #define MANDELBROT_ZOOM_OUT (BUTTON_SELECT | BUTTON_REPEAT)
109 #define MANDELBROT_MAXITER_INC_PRE BUTTON_PLAY
110 #define MANDELBROT_MAXITER_INC (BUTTON_PLAY | BUTTON_REL)
111 #define MANDELBROT_MAXITER_DEC_PRE BUTTON_PLAY
112 #define MANDELBROT_MAXITER_DEC (BUTTON_PLAY | BUTTON_REPEAT)
113 #define MANDELBROT_RESET BUTTON_REC
115 #elif CONFIG_KEYPAD == GIGABEAT_PAD
116 #define MANDELBROT_QUIT BUTTON_POWER
117 #define MANDELBROT_UP BUTTON_UP
118 #define MANDELBROT_DOWN BUTTON_DOWN
119 #define MANDELBROT_LEFT BUTTON_LEFT
120 #define MANDELBROT_RIGHT BUTTON_RIGHT
121 #define MANDELBROT_ZOOM_IN_PRE BUTTON_SELECT
122 #define MANDELBROT_ZOOM_IN (BUTTON_SELECT | BUTTON_REL)
123 #define MANDELBROT_ZOOM_OUT_PRE BUTTON_SELECT
124 #define MANDELBROT_ZOOM_OUT (BUTTON_SELECT | BUTTON_REPEAT)
125 #define MANDELBROT_MAXITER_INC BUTTON_VOL_UP
126 #define MANDELBROT_MAXITER_DEC BUTTON_VOL_DOWN
127 #define MANDELBROT_RESET BUTTON_A
129 #elif CONFIG_KEYPAD == SANSA_E200_PAD
130 #define MANDELBROT_QUIT BUTTON_POWER
131 #define MANDELBROT_UP BUTTON_UP
132 #define MANDELBROT_DOWN BUTTON_DOWN
133 #define MANDELBROT_LEFT BUTTON_LEFT
134 #define MANDELBROT_RIGHT BUTTON_RIGHT
135 #define MANDELBROT_ZOOM_IN BUTTON_SCROLL_FWD
136 #define MANDELBROT_ZOOM_OUT BUTTON_SCROLL_BACK
137 #define MANDELBROT_MAXITER_INC (BUTTON_SELECT | BUTTON_RIGHT)
138 #define MANDELBROT_MAXITER_DEC (BUTTON_SELECT | BUTTON_LEFT)
139 #define MANDELBROT_RESET BUTTON_REC
141 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
142 #define MANDELBROT_QUIT (BUTTON_HOME|BUTTON_REPEAT)
143 #define MANDELBROT_UP BUTTON_UP
144 #define MANDELBROT_DOWN BUTTON_DOWN
145 #define MANDELBROT_LEFT BUTTON_LEFT
146 #define MANDELBROT_RIGHT BUTTON_RIGHT
147 #define MANDELBROT_ZOOM_IN BUTTON_SCROLL_FWD
148 #define MANDELBROT_ZOOM_OUT BUTTON_SCROLL_BACK
149 #define MANDELBROT_MAXITER_INC (BUTTON_SELECT | BUTTON_RIGHT)
150 #define MANDELBROT_MAXITER_DEC (BUTTON_SELECT | BUTTON_LEFT)
151 #define MANDELBROT_RESET (BUTTON_SELECT | BUTTON_REPEAT)
153 #elif CONFIG_KEYPAD == SANSA_C200_PAD
154 #define MANDELBROT_QUIT BUTTON_POWER
155 #define MANDELBROT_UP BUTTON_UP
156 #define MANDELBROT_DOWN BUTTON_DOWN
157 #define MANDELBROT_LEFT BUTTON_LEFT
158 #define MANDELBROT_RIGHT BUTTON_RIGHT
159 #define MANDELBROT_ZOOM_IN BUTTON_VOL_UP
160 #define MANDELBROT_ZOOM_OUT BUTTON_VOL_DOWN
161 #define MANDELBROT_MAXITER_INC (BUTTON_SELECT | BUTTON_RIGHT)
162 #define MANDELBROT_MAXITER_DEC (BUTTON_SELECT | BUTTON_LEFT)
163 #define MANDELBROT_RESET BUTTON_REC
165 #elif CONFIG_KEYPAD == SANSA_CLIP_PAD
166 #define MANDELBROT_QUIT BUTTON_POWER
167 #define MANDELBROT_UP BUTTON_UP
168 #define MANDELBROT_DOWN BUTTON_DOWN
169 #define MANDELBROT_LEFT BUTTON_LEFT
170 #define MANDELBROT_RIGHT BUTTON_RIGHT
171 #define MANDELBROT_ZOOM_IN BUTTON_VOL_UP
172 #define MANDELBROT_ZOOM_OUT BUTTON_VOL_DOWN
173 #define MANDELBROT_MAXITER_INC (BUTTON_SELECT | BUTTON_RIGHT)
174 #define MANDELBROT_MAXITER_DEC (BUTTON_SELECT | BUTTON_LEFT)
175 #define MANDELBROT_RESET BUTTON_HOME
177 #elif CONFIG_KEYPAD == SANSA_M200_PAD
178 #define MANDELBROT_QUIT BUTTON_POWER
179 #define MANDELBROT_UP BUTTON_UP
180 #define MANDELBROT_DOWN BUTTON_DOWN
181 #define MANDELBROT_LEFT BUTTON_LEFT
182 #define MANDELBROT_RIGHT BUTTON_RIGHT
183 #define MANDELBROT_ZOOM_IN BUTTON_VOL_UP
184 #define MANDELBROT_ZOOM_OUT BUTTON_VOL_DOWN
185 #define MANDELBROT_MAXITER_INC (BUTTON_SELECT | BUTTON_RIGHT)
186 #define MANDELBROT_MAXITER_DEC (BUTTON_SELECT | BUTTON_LEFT)
187 #define MANDELBROT_RESET (BUTTON_SELECT | BUTTON_UP)
189 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
190 #define MANDELBROT_QUIT BUTTON_POWER
191 #define MANDELBROT_UP BUTTON_SCROLL_UP
192 #define MANDELBROT_DOWN BUTTON_SCROLL_DOWN
193 #define MANDELBROT_LEFT BUTTON_LEFT
194 #define MANDELBROT_RIGHT BUTTON_RIGHT
195 #define MANDELBROT_ZOOM_IN_PRE BUTTON_PLAY
196 #define MANDELBROT_ZOOM_IN (BUTTON_PLAY | BUTTON_REL)
197 #define MANDELBROT_ZOOM_OUT_PRE BUTTON_PLAY
198 #define MANDELBROT_ZOOM_OUT (BUTTON_PLAY | BUTTON_REPEAT)
199 #define MANDELBROT_MAXITER_INC BUTTON_FF
200 #define MANDELBROT_MAXITER_DEC BUTTON_REW
201 #define MANDELBROT_RESET (BUTTON_PLAY | BUTTON_REW)
203 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
204 #define MANDELBROT_QUIT BUTTON_EQ
205 #define MANDELBROT_UP BUTTON_UP
206 #define MANDELBROT_DOWN BUTTON_DOWN
207 #define MANDELBROT_LEFT BUTTON_LEFT
208 #define MANDELBROT_RIGHT BUTTON_RIGHT
209 #define MANDELBROT_ZOOM_IN_PRE BUTTON_SELECT
210 #define MANDELBROT_ZOOM_IN (BUTTON_SELECT | BUTTON_REL)
211 #define MANDELBROT_ZOOM_OUT_PRE BUTTON_SELECT
212 #define MANDELBROT_ZOOM_OUT (BUTTON_SELECT | BUTTON_REPEAT)
213 #define MANDELBROT_MAXITER_INC (BUTTON_PLAY | BUTTON_RIGHT)
214 #define MANDELBROT_MAXITER_DEC (BUTTON_PLAY | BUTTON_LEFT)
215 #define MANDELBROT_RESET BUTTON_MODE
217 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
218 #define MANDELBROT_QUIT BUTTON_BACK
219 #define MANDELBROT_UP BUTTON_UP
220 #define MANDELBROT_DOWN BUTTON_DOWN
221 #define MANDELBROT_LEFT BUTTON_LEFT
222 #define MANDELBROT_RIGHT BUTTON_RIGHT
223 #define MANDELBROT_ZOOM_IN BUTTON_VOL_UP
224 #define MANDELBROT_ZOOM_OUT BUTTON_VOL_DOWN
225 #define MANDELBROT_MAXITER_INC BUTTON_PREV
226 #define MANDELBROT_MAXITER_DEC BUTTON_NEXT
227 #define MANDELBROT_RESET BUTTON_MENU
229 #elif CONFIG_KEYPAD == MROBE100_PAD
230 #define MANDELBROT_QUIT BUTTON_POWER
231 #define MANDELBROT_UP BUTTON_UP
232 #define MANDELBROT_DOWN BUTTON_DOWN
233 #define MANDELBROT_LEFT BUTTON_LEFT
234 #define MANDELBROT_RIGHT BUTTON_RIGHT
235 #define MANDELBROT_ZOOM_IN_PRE BUTTON_SELECT
236 #define MANDELBROT_ZOOM_IN (BUTTON_SELECT | BUTTON_REL)
237 #define MANDELBROT_ZOOM_OUT_PRE BUTTON_SELECT
238 #define MANDELBROT_ZOOM_OUT (BUTTON_SELECT | BUTTON_REPEAT)
239 #define MANDELBROT_MAXITER_INC BUTTON_MENU
240 #define MANDELBROT_MAXITER_DEC BUTTON_PLAY
241 #define MANDELBROT_RESET BUTTON_DISPLAY
243 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
244 #define MANDELBROT_QUIT BUTTON_RC_REC
245 #define MANDELBROT_UP BUTTON_RC_VOL_UP
246 #define MANDELBROT_DOWN BUTTON_RC_VOL_DOWN
247 #define MANDELBROT_LEFT BUTTON_RC_REW
248 #define MANDELBROT_RIGHT BUTTON_RC_FF
249 #define MANDELBROT_ZOOM_IN_PRE BUTTON_RC_PLAY
250 #define MANDELBROT_ZOOM_IN (BUTTON_RC_PLAY | BUTTON_REL)
251 #define MANDELBROT_ZOOM_OUT_PRE BUTTON_RC_PLAY
252 #define MANDELBROT_ZOOM_OUT (BUTTON_RC_PLAY | BUTTON_REPEAT)
253 #define MANDELBROT_MAXITER_INC_PRE BUTTON_RC_MODE
254 #define MANDELBROT_MAXITER_INC (BUTTON_RC_MODE|BUTTON_REL)
255 #define MANDELBROT_MAXITER_DEC_PRE BUTTON_RC_MODE
256 #define MANDELBROT_MAXITER_DEC (BUTTON_RC_MODE|BUTTON_REPEAT)
257 #define MANDELBROT_RESET BUTTON_RC_MENU
259 #elif CONFIG_KEYPAD == COWOND2_PAD
260 #define MANDELBROT_QUIT BUTTON_POWER
262 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
263 #define MANDELBROT_QUIT BUTTON_BACK
264 #define MANDELBROT_UP BUTTON_UP
265 #define MANDELBROT_DOWN BUTTON_DOWN
266 #define MANDELBROT_LEFT BUTTON_LEFT
267 #define MANDELBROT_RIGHT BUTTON_RIGHT
268 #define MANDELBROT_ZOOM_IN BUTTON_PLAY
269 #define MANDELBROT_ZOOM_OUT BUTTON_MENU
270 #define MANDELBROT_MAXITER_INC (BUTTON_UP | BUTTON_CUSTOM)
271 #define MANDELBROT_MAXITER_DEC (BUTTON_DOWN | BUTTON_CUSTOM)
272 #define MANDELBROT_RESET BUTTON_SELECT
274 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
275 #define MANDELBROT_QUIT BUTTON_POWER
276 #define MANDELBROT_UP BUTTON_UP
277 #define MANDELBROT_DOWN BUTTON_DOWN
278 #define MANDELBROT_LEFT BUTTON_LEFT
279 #define MANDELBROT_RIGHT BUTTON_RIGHT
280 #define MANDELBROT_ZOOM_IN BUTTON_VIEW
281 #define MANDELBROT_ZOOM_OUT BUTTON_PLAYLIST
282 #define MANDELBROT_MAXITER_INC BUTTON_VOL_UP
283 #define MANDELBROT_MAXITER_DEC BUTTON_VOL_DOWN
284 #define MANDELBROT_RESET BUTTON_MENU
286 #elif CONFIG_KEYPAD == ONDAVX747_PAD || CONFIG_KEYPAD == MROBE500_PAD
287 #define MANDELBROT_QUIT BUTTON_POWER
289 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
290 #define MANDELBROT_QUIT BUTTON_REC
291 #define MANDELBROT_UP BUTTON_UP
292 #define MANDELBROT_DOWN BUTTON_DOWN
293 #define MANDELBROT_LEFT BUTTON_LEFT
294 #define MANDELBROT_RIGHT BUTTON_RIGHT
295 #define MANDELBROT_ZOOM_IN_PRE (BUTTON_FFWD | BUTTON_UP)
296 #define MANDELBROT_ZOOM_IN (BUTTON_REW | BUTTON_UP)
297 #define MANDELBROT_ZOOM_OUT_PRE (BUTTON_FFWD | BUTTON_DOWN)
298 #define MANDELBROT_ZOOM_OUT (BUTTON_REW | BUTTON_DOWN)
299 #define MANDELBROT_MAXITER_INC (BUTTON_PLAY | BUTTON_UP)
300 #define MANDELBROT_MAXITER_DEC (BUTTON_FFWD | BUTTON_DOWN)
301 #define MANDELBROT_RESET (BUTTON_PLAY | BUTTON_RIGHT)
303 #else
304 #error No keymap defined!
305 #endif
307 #ifdef HAVE_TOUCHSCREEN
308 #ifndef MANDELBROT_UP
309 #define MANDELBROT_UP BUTTON_TOPMIDDLE
310 #endif
311 #ifndef MANDELBROT_DOWN
312 #define MANDELBROT_DOWN BUTTON_BOTTOMMIDDLE
313 #endif
314 #ifndef MANDELBROT_LEFT
315 #define MANDELBROT_LEFT BUTTON_MIDLEFT
316 #endif
317 #ifndef MANDELBROT_RIGHT
318 #define MANDELBROT_RIGHT BUTTON_MIDRIGHT
319 #endif
320 #ifndef MANDELBROT_ZOOM_IN_PRE
321 #define MANDELBROT_ZOOM_IN_PRE BUTTON_TOPRIGHT
322 #endif
323 #ifndef MANDELBROT_ZOOM_IN
324 #define MANDELBROT_ZOOM_IN (BUTTON_TOPRIGHT | BUTTON_REL)
325 #endif
326 #ifndef MANDELBROT_ZOOM_OUT_PRE
327 #define MANDELBROT_ZOOM_OUT_PRE BUTTON_TOPLEFT
328 #endif
329 #ifndef MANDELBROT_ZOOM_OUT
330 #define MANDELBROT_ZOOM_OUT (BUTTON_TOPLEFT | BUTTON_REL)
331 #endif
332 #ifndef MANDELBROT_MAXITER_INC
333 #define MANDELBROT_MAXITER_INC BUTTON_BOTTOMRIGHT
334 #endif
335 #ifndef MANDELBROT_MAXITER_DEC
336 #define MANDELBROT_MAXITER_DEC BUTTON_BOTTOMLEFT
337 #endif
338 #ifndef MANDELBROT_RESET
339 #define MANDELBROT_RESET BUTTON_CENTER
340 #endif
341 #endif
343 #if LCD_DEPTH < 8
344 #define USEGSLIB
345 #define MYLCD(fn) grey_ub_ ## fn
346 #define MYLCD_UPDATE()
347 #define MYXLCD(fn) grey_ub_ ## fn
348 #else
349 #define UPDATE_FREQ (HZ/50)
350 #define MYLCD(fn) rb->lcd_ ## fn
351 #define MYLCD_UPDATE() rb->lcd_update();
352 #define MYXLCD(fn) xlcd_ ## fn
353 #endif
355 /* Fixed point format s5.26: sign, 5 bits integer part, 26 bits fractional part */
356 static long x_min;
357 static long x_max;
358 static long x_step;
359 static long x_delta;
360 static long y_min;
361 static long y_max;
362 static long y_step;
363 static long y_delta;
365 static int px_min = 0;
366 static int px_max = LCD_WIDTH;
367 static int py_min = 0;
368 static int py_max = LCD_HEIGHT;
370 static int step_log2;
371 static unsigned max_iter;
373 #ifdef USEGSLIB
374 GREY_INFO_STRUCT
375 static unsigned char *gbuf;
376 static size_t gbuf_size = 0;
377 static unsigned char imgbuffer[LCD_HEIGHT];
378 #else
379 static fb_data imgbuffer[LCD_HEIGHT];
380 #endif
382 /* 8 entries cyclical, last entry is black (convergence) */
383 #ifdef HAVE_LCD_COLOR
384 static const fb_data color[9] = {
385 LCD_RGBPACK(255, 0, 159), LCD_RGBPACK(159, 0, 255), LCD_RGBPACK(0, 0, 255),
386 LCD_RGBPACK(0, 159, 255), LCD_RGBPACK(0, 255, 128), LCD_RGBPACK(128, 255, 0),
387 LCD_RGBPACK(255, 191, 0), LCD_RGBPACK(255, 0, 0), LCD_RGBPACK(0, 0, 0)
389 #else /* greyscale */
390 static const unsigned char color[9] = {
391 255, 223, 191, 159, 128, 96, 64, 32, 0
393 #endif
395 #if CONFIG_CPU == SH7034
397 #define MULS16_ASR10(a, b) muls16_asr10(a, b)
398 static inline short muls16_asr10(short a, short b)
400 short r;
401 asm (
402 "muls %[a],%[b] \n"
403 "sts macl,%[r] \n"
404 "shlr8 %[r] \n"
405 "shlr2 %[r] \n"
406 : /* outputs */
407 [r]"=r"(r)
408 : /* inputs */
409 [a]"r"(a),
410 [b]"r"(b)
412 return r;
415 #define MULS32_ASR26(a, b) muls32_asr26(a, b)
416 static inline long muls32_asr26(long a, long b)
418 long r, t1, t2, t3;
419 asm (
420 /* Signed 32bit * 32bit -> 64bit multiplication.
421 Notation: xxab * xxcd, where each letter represents 16 bits.
422 xx is the 64 bit sign extension. */
423 "swap.w %[a],%[t1] \n" /* t1 = ba */
424 "mulu %[t1],%[b] \n" /* a * d */
425 "swap.w %[b],%[t3] \n" /* t3 = dc */
426 "sts macl,%[t2] \n" /* t2 = a * d */
427 "mulu %[t1],%[t3] \n" /* a * c */
428 "sts macl,%[r] \n" /* hi = a * c */
429 "mulu %[a],%[t3] \n" /* b * c */
430 "clrt \n"
431 "sts macl,%[t3] \n" /* t3 = b * c */
432 "addc %[t2],%[t3] \n" /* t3 += t2, carry -> t2 */
433 "movt %[t2] \n"
434 "mulu %[a],%[b] \n" /* b * d */
435 "mov %[t3],%[t1] \n" /* t1t3 = t2t3 << 16 */
436 "xtrct %[t2],%[t1] \n"
437 "shll16 %[t3] \n"
438 "sts macl,%[t2] \n" /* lo = b * d */
439 "clrt \n" /* hi.lo += t1t3 */
440 "addc %[t3],%[t2] \n"
441 "addc %[t1],%[r] \n"
442 "cmp/pz %[a] \n" /* ab >= 0 ? */
443 "bt 1f \n"
444 "sub %[b],%[r] \n" /* no: hi -= cd (sign extension of ab is -1) */
445 "1: \n"
446 "cmp/pz %[b] \n" /* cd >= 0 ? */
447 "bt 2f \n"
448 "sub %[a],%[r] \n" /* no: hi -= ab (sign extension of cd is -1) */
449 "2: \n"
450 /* Shift right by 26 and return low 32 bits */
451 "shll2 %[r] \n" /* hi <<= 6 */
452 "shll2 %[r] \n"
453 "shll2 %[r] \n"
454 "shlr16 %[t2] \n" /* (unsigned)lo >>= 26 */
455 "shlr8 %[t2] \n"
456 "shlr2 %[t2] \n"
457 "or %[t2],%[r] \n" /* combine result */
458 : /* outputs */
459 [r] "=&r"(r),
460 [t1]"=&r"(t1),
461 [t2]"=&r"(t2),
462 [t3]"=&r"(t3)
463 : /* inputs */
464 [a] "r" (a),
465 [b] "r" (b)
467 return r;
470 #elif defined CPU_COLDFIRE
472 #define MULS16_ASR10(a, b) muls16_asr10(a, b)
473 static inline short muls16_asr10(short a, short b)
475 asm (
476 "muls.w %[a],%[b] \n"
477 "asr.l #8,%[b] \n"
478 "asr.l #2,%[b] \n"
479 : /* outputs */
480 [b]"+d"(b)
481 : /* inputs */
482 [a]"d" (a)
484 return b;
487 /* Needs the EMAC initialised to fractional mode w/o rounding and saturation */
488 #define MULS32_INIT() coldfire_set_macsr(EMAC_FRACTIONAL)
489 #define MULS32_ASR26(a, b) muls32_asr26(a, b)
490 static inline long muls32_asr26(long a, long b)
492 long r, t1;
493 asm (
494 "mac.l %[a], %[b], %%acc0 \n" /* multiply */
495 "move.l %%accext01, %[t1] \n" /* get low part */
496 "movclr.l %%acc0, %[r] \n" /* get high part */
497 "asl.l #5, %[r] \n" /* hi <<= 5, plus one free */
498 "lsr.l #3, %[t1] \n" /* lo >>= 3 */
499 "and.l #0x1f, %[t1] \n" /* mask out unrelated bits */
500 "or.l %[t1], %[r] \n" /* combine result */
501 : /* outputs */
502 [r] "=d"(r),
503 [t1]"=d"(t1)
504 : /* inputs */
505 [a] "d" (a),
506 [b] "d" (b)
508 return r;
511 #elif defined CPU_ARM
513 #define MULS32_ASR26(a, b) muls32_asr26(a, b)
514 static inline long muls32_asr26(long a, long b)
516 long r, t1;
517 asm (
518 "smull %[r], %[t1], %[a], %[b] \n"
519 "mov %[r], %[r], lsr #26 \n"
520 "orr %[r], %[r], %[t1], lsl #6 \n"
521 : /* outputs */
522 [r] "=&r,&r,&r"(r),
523 [t1]"=&r,&r,&r"(t1)
524 : /* inputs */
525 [a] "%r,%r,%r" (a),
526 [b] "r,0,1" (b)
528 return r;
531 #endif /* CPU */
533 /* default macros */
534 #ifndef MULS16_ASR10
535 #define MULS16_ASR10(a, b) ((short)(((long)(a) * (long)(b)) >> 10))
536 #endif
537 #ifndef MULS32_ASR26
538 #define MULS32_ASR26(a, b) ((long)(((long long)(a) * (long long)(b)) >> 26))
539 #endif
540 #ifndef MULS32_INIT
541 #define MULS32_INIT()
542 #endif
544 int ilog2_fp(long value) /* calculate integer log2(value_fp_6.26) */
546 int i = 0;
548 if (value <= 0) {
549 return -32767;
550 } else if (value > (1L<<26)) {
551 while (value >= (2L<<26)) {
552 value >>= 1;
553 i++;
555 } else {
556 while (value < (1L<<26)) {
557 value <<= 1;
558 i--;
561 return i;
564 void recalc_parameters(void)
566 x_step = (x_max - x_min) / LCD_WIDTH;
567 x_delta = (x_step * LCD_WIDTH) / 8;
568 y_step = (y_max - y_min) / LCD_HEIGHT;
569 y_delta = (y_step * LCD_HEIGHT) / 8;
570 step_log2 = ilog2_fp(MIN(x_step, y_step));
571 max_iter = MAX(15, -15 * step_log2 - 45);
574 #if CONFIG_LCD == LCD_SSD1815
575 /* Recorder, Ondio: pixel_height == 1.25 * pixel_width */
576 #define MB_HEIGHT (LCD_HEIGHT*5/4)
577 #else
578 /* square pixels */
579 #define MB_HEIGHT LCD_HEIGHT
580 #endif
582 #define MB_XOFS (-0x03000000L) /* -0.75 (s5.26) */
583 #if 3000*MB_HEIGHT/LCD_WIDTH >= 2400 /* width is limiting factor */
584 #define MB_XFAC (0x06000000LL) /* 1.5 (s5.26) */
585 #define MB_YFAC (MB_XFAC*MB_HEIGHT/LCD_WIDTH)
586 #else /* height is limiting factor */
587 #define MB_YFAC (0x04cccccdLL) /* 1.2 (s5.26) */
588 #define MB_XFAC (MB_YFAC*LCD_WIDTH/MB_HEIGHT)
589 #endif
591 void init_mandelbrot_set(void)
593 x_min = MB_XOFS-MB_XFAC;
594 x_max = MB_XOFS+MB_XFAC;
595 y_min = -MB_YFAC;
596 y_max = MB_YFAC;
597 recalc_parameters();
600 void calc_mandelbrot_low_prec(void)
602 long start_tick, last_yield;
603 #ifndef USEGSLIB
604 long next_update = *rb->current_tick;
605 int last_px = px_min;
606 #endif
607 unsigned n_iter;
608 long a32, b32;
609 short x, x2, y, y2, a, b;
610 int p_x, p_y;
612 start_tick = last_yield = *rb->current_tick;
614 for (p_x = 0, a32 = x_min; p_x < px_max; p_x++, a32 += x_step) {
615 if (p_x < px_min)
616 continue;
617 a = a32 >> 16;
618 for (p_y = LCD_HEIGHT-1, b32 = y_min; p_y >= py_min; p_y--, b32 += y_step) {
619 if (p_y >= py_max)
620 continue;
621 b = b32 >> 16;
622 x = a;
623 y = b;
624 n_iter = 0;
626 while (++n_iter <= max_iter) {
627 x2 = MULS16_ASR10(x, x);
628 y2 = MULS16_ASR10(y, y);
630 if (x2 + y2 > (4<<10)) break;
632 y = 2 * MULS16_ASR10(x, y) + b;
633 x = x2 - y2 + a;
636 if (n_iter > max_iter)
637 imgbuffer[p_y] = color[8];
638 else
639 imgbuffer[p_y] = color[n_iter & 7];
641 /* be nice to other threads:
642 * if at least one tick has passed, yield */
643 if (*rb->current_tick > last_yield) {
644 rb->yield();
645 last_yield = *rb->current_tick;
648 #ifdef USEGSLIB
649 grey_ub_gray_bitmap_part(imgbuffer, 0, py_min, 1,
650 p_x, py_min, 1, py_max - py_min);
651 #else
652 rb->lcd_bitmap_part(imgbuffer, 0, py_min, 1,
653 p_x, py_min, 1, py_max - py_min);
654 if ((p_x == px_max - 1) || TIME_AFTER(*rb->current_tick, next_update))
656 next_update = *rb->current_tick + UPDATE_FREQ;
657 rb->lcd_update_rect(last_px, py_min, p_x - last_px + 1,
658 py_max - py_min);
659 last_px = p_x;
661 #endif
665 void calc_mandelbrot_high_prec(void)
667 long start_tick, last_yield;
668 #ifndef USEGSLIB
669 long next_update = *rb->current_tick;
670 int last_px = px_min;
671 #endif
672 unsigned n_iter;
673 long x, x2, y, y2, a, b;
674 int p_x, p_y;
676 MULS32_INIT();
677 start_tick = last_yield = *rb->current_tick;
679 for (p_x = 0, a = x_min; p_x < px_max; p_x++, a += x_step) {
680 if (p_x < px_min)
681 continue;
682 for (p_y = LCD_HEIGHT-1, b = y_min; p_y >= py_min; p_y--, b += y_step) {
683 if (p_y >= py_max)
684 continue;
685 x = a;
686 y = b;
687 n_iter = 0;
689 while (++n_iter <= max_iter) {
690 x2 = MULS32_ASR26(x, x);
691 y2 = MULS32_ASR26(y, y);
693 if (x2 + y2 > (4L<<26)) break;
695 y = 2 * MULS32_ASR26(x, y) + b;
696 x = x2 - y2 + a;
699 if (n_iter > max_iter)
700 imgbuffer[p_y] = color[8];
701 else
702 imgbuffer[p_y] = color[n_iter & 7];
704 /* be nice to other threads:
705 * if at least one tick has passed, yield */
706 if (*rb->current_tick > last_yield) {
707 rb->yield();
708 last_yield = *rb->current_tick;
711 #ifdef USEGSLIB
712 grey_ub_gray_bitmap_part(imgbuffer, 0, py_min, 1,
713 p_x, py_min, 1, py_max - py_min);
714 #else
715 rb->lcd_bitmap_part(imgbuffer, 0, py_min, 1,
716 p_x, py_min, 1, py_max-py_min);
717 if ((p_x == px_max - 1) || TIME_AFTER(*rb->current_tick, next_update))
719 next_update = *rb->current_tick + UPDATE_FREQ;
720 rb->lcd_update_rect(last_px, py_min, p_x - last_px + 1,
721 py_max - py_min);
722 last_px = p_x;
724 #endif
728 void cleanup(void *parameter)
730 (void)parameter;
731 #ifdef USEGSLIB
732 grey_release();
733 #endif
736 #define REDRAW_NONE 0
737 #define REDRAW_PARTIAL 1
738 #define REDRAW_FULL 2
740 enum plugin_status plugin_start(const void* parameter)
742 int button;
743 int lastbutton = BUTTON_NONE;
744 int redraw = REDRAW_FULL;
746 (void)parameter;
748 #ifdef USEGSLIB
749 /* get the remainder of the plugin buffer */
750 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
752 /* initialize the greyscale buffer.*/
753 if (!grey_init(gbuf, gbuf_size, GREY_ON_COP,
754 LCD_WIDTH, LCD_HEIGHT, NULL))
756 rb->splash(HZ, "Couldn't init greyscale display");
757 return 0;
759 grey_show(true); /* switch on greyscale overlay */
760 #endif
762 #if LCD_DEPTH > 1
763 rb->lcd_set_backdrop(NULL);
764 #endif
766 init_mandelbrot_set();
768 /* main loop */
769 while (true) {
770 if (redraw > REDRAW_NONE) {
771 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
772 rb->cpu_boost(true);
773 #endif
774 if (redraw == REDRAW_FULL) {
775 MYLCD(clear_display)();
776 MYLCD_UPDATE();
779 if (step_log2 <= -10) /* select precision */
780 calc_mandelbrot_high_prec();
781 else
782 calc_mandelbrot_low_prec();
784 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
785 rb->cpu_boost(false);
786 #endif
787 px_min = 0;
788 px_max = LCD_WIDTH;
789 py_min = 0;
790 py_max = LCD_HEIGHT;
791 redraw = REDRAW_NONE;
794 button = rb->button_get(true);
795 switch (button) {
796 #ifdef MANDELBROT_RC_QUIT
797 case MANDELBROT_RC_QUIT:
798 #endif
799 case MANDELBROT_QUIT:
800 #ifdef USEGSLIB
801 grey_release();
802 #endif
803 return PLUGIN_OK;
805 case MANDELBROT_ZOOM_OUT:
806 #ifdef MANDELBROT_ZOOM_OUT_PRE
807 if (lastbutton != MANDELBROT_ZOOM_OUT_PRE)
808 break;
809 #endif
810 x_min -= x_delta;
811 x_max += x_delta;
812 y_min -= y_delta;
813 y_max += y_delta;
814 recalc_parameters();
815 redraw = REDRAW_FULL;
816 break;
819 case MANDELBROT_ZOOM_IN:
820 #ifdef MANDELBROT_ZOOM_IN_PRE
821 if (lastbutton != MANDELBROT_ZOOM_IN_PRE)
822 break;
823 #endif
824 #ifdef MANDELBROT_ZOOM_IN2
825 case MANDELBROT_ZOOM_IN2:
826 #endif
827 x_min += x_delta;
828 x_max -= x_delta;
829 y_min += y_delta;
830 y_max -= y_delta;
831 recalc_parameters();
832 redraw = REDRAW_FULL;
833 break;
835 case MANDELBROT_UP:
836 y_min += y_delta;
837 y_max += y_delta;
838 MYXLCD(scroll_down)(LCD_HEIGHT/8);
839 MYLCD_UPDATE();
840 py_max = (LCD_HEIGHT/8);
841 redraw = REDRAW_PARTIAL;
842 break;
844 case MANDELBROT_DOWN:
845 y_min -= y_delta;
846 y_max -= y_delta;
847 MYXLCD(scroll_up)(LCD_HEIGHT/8);
848 MYLCD_UPDATE();
849 py_min = (LCD_HEIGHT-LCD_HEIGHT/8);
850 redraw = REDRAW_PARTIAL;
851 break;
853 case MANDELBROT_LEFT:
854 x_min -= x_delta;
855 x_max -= x_delta;
856 MYXLCD(scroll_right)(LCD_WIDTH/8);
857 MYLCD_UPDATE();
858 px_max = (LCD_WIDTH/8);
859 redraw = REDRAW_PARTIAL;
860 break;
862 case MANDELBROT_RIGHT:
863 x_min += x_delta;
864 x_max += x_delta;
865 MYXLCD(scroll_left)(LCD_WIDTH/8);
866 MYLCD_UPDATE();
867 px_min = (LCD_WIDTH-LCD_WIDTH/8);
868 redraw = REDRAW_PARTIAL;
869 break;
871 case MANDELBROT_MAXITER_DEC:
872 #ifdef MANDELBROT_MAXITER_DEC_PRE
873 if (lastbutton != MANDELBROT_MAXITER_DEC_PRE)
874 break;
875 #endif
876 if (max_iter >= 15) {
877 max_iter -= max_iter / 3;
878 redraw = REDRAW_FULL;
880 break;
882 case MANDELBROT_MAXITER_INC:
883 #ifdef MANDELBROT_MAXITER_INC_PRE
884 if (lastbutton != MANDELBROT_MAXITER_INC_PRE)
885 break;
886 #endif
887 max_iter += max_iter / 2;
888 redraw = REDRAW_FULL;
889 break;
891 case MANDELBROT_RESET:
892 init_mandelbrot_set();
893 redraw = REDRAW_FULL;
894 break;
896 default:
897 if (rb->default_event_handler_ex(button, cleanup, NULL)
898 == SYS_USB_CONNECTED)
899 return PLUGIN_USB_CONNECTED;
900 break;
902 if (button != BUTTON_NONE)
903 lastbutton = button;
905 #ifdef USEGSLIB
906 grey_release();
907 #endif
908 return PLUGIN_OK;
910 #endif