1+ __attribute__((naked )) void _start (void ) {
2+ __asm__ volatile (
3+ "li sp, 0x4000\n"
4+ "call main\n"
5+ "loop: j loop\n"
6+ );
7+ }
8+
9+ #define SCALE 1024
10+ #define MAX_ITER 32
11+
12+ void draw_mandelbrot (volatile unsigned char * frame , int cx , int cy , int zoom ) {
13+ int x_start = cx - zoom ;
14+ int y_start = cy - (zoom * 240 / 320 );
15+ int x_step = (zoom * 2 ) / 320 ;
16+ int y_step = (zoom * 2 * 240 / 320 ) / 240 ;
17+
18+ if (x_step < 1 ) x_step = 1 ;
19+ if (y_step < 1 ) y_step = 1 ;
20+
21+ for (int py = 0 ; py < 240 ; py ++ ) {
22+ int ci = y_start + py * y_step ;
23+ for (int px = 0 ; px < 320 ; px ++ ) {
24+ int cr = x_start + px * x_step ;
25+
26+ int zr = 0 ;
27+ int zi = 0 ;
28+ int iter = 0 ;
29+
30+ while (iter < MAX_ITER ) {
31+ int zr2 = (zr * zr ) >> 10 ;
32+ int zi2 = (zi * zi ) >> 10 ;
33+
34+ if (zr2 + zi2 > 4 * SCALE ) break ;
35+
36+ int new_zr = zr2 - zi2 + cr ;
37+ zi = ((2 * zr * zi ) >> 10 ) + ci ;
38+ zr = new_zr ;
39+ iter ++ ;
40+ }
41+
42+ unsigned char color ;
43+ if (iter == MAX_ITER ) {
44+ color = 0x00 ;
45+ } else {
46+ color = (unsigned char )(iter * 7 );
47+ }
48+
49+ frame [0x4000 * (320 * py + px )] = color ;
50+ }
51+ }
52+ }
53+
54+ int main () {
55+ volatile unsigned char * frame = (volatile unsigned char * )0x0 ;
56+
57+ int cx = -768 ;
58+ int cy = 0 ;
59+ int step = 0 ;
60+
61+ while (1 ) {
62+ int zoom = 1536 ;
63+ for (int i = 0 ; i < step ; i ++ ) {
64+ zoom = (zoom * 3 ) / 4 ;
65+ }
66+ draw_mandelbrot (frame , cx , cy , zoom );
67+ step ++ ;
68+ if (step > 8 ) step = 0 ;
69+ }
70+ }
0 commit comments